; SOVM/1 -- the shared substrate, in CDDL (RFC 8610).
;
; Normative. The record blocks on the /substrate/ pages are rendered from this
; file, and where a page and this file could disagree, this file is the
; specification. What CDDL cannot state -- deterministic encoding, low-S,
; point validation, what a signature means -- is stated on the pages and not
; here.
;
; CDDL has no import. A layer is checked as its own file appended to this one,
; which is how a layer's rules name the rules below (the RFC 9052 convention).
; Published at https://sovm.io/spec/cddl/substrate.cddl

; A durable signed record (section 4), generic over its content type and its
; payload. It travels untagged: the frame or member that carries a record is
; defined as carrying exactly that record, so a tagged twin would be a second
; valid byte form of it.
;
; The protected bucket admits exactly the three headers below. A header the
; profile does not name, critical or not, has no member to occupy.
SOVM_Sign1<ContentType, Payload> = [
  protected: bstr .cbor {
    1 => -7,            ; alg: ES256, the one signature suite (section 5)
    3 => ContentType,   ; content type
    4 => site_id,       ; kid: the signer
  },
  unprotected: empty_header_map,
  payload: bstr .cbor Payload,
  signature: es256_signature,
]

; A wrapped-key record (section 4), where a layer defines one: a COSE_Encrypt
; (RFC 9052 section 5.1). It has no signer, so no kid. The content is encrypted
; under A256GCM with a content key drawn for this record alone; the unprotected
; bucket is empty, so the A256GCM nonce (RFC 9053 section 4.1) travels in the
; protected bucket. Exactly one recipient carries the content key.
SOVM_Encrypt<ContentType> = [
  protected: bstr .cbor {
    1 => 3,             ; alg: A256GCM, the content suite (section 5)
    3 => ContentType,   ; content type
    5 => aes_gcm_iv,    ; IV
  },
  unprotected: empty_header_map,
  ciphertext: bstr,
  recipients: [SOVM_KeyWrapRecipient],
]

; The one recipient of a wrapped-key record (section 4): the content key under
; AES Key Wrap (RFC 9053 section 6.2.1). RFC 9053 makes a key-wrap recipient's
; protected bucket empty, so alg is the one header the profile carries
; unprotected. Three members: a recipient nests no recipients of its own.
SOVM_KeyWrapRecipient = [
  protected: bstr .size 0,
  unprotected: {
    1 => -5,            ; alg: A256KW, the key-wrap suite (section 5)
  },
  ciphertext: aes_kw_wrapped_key,
]

; The unprotected bucket MUST be empty (section 4).
empty_header_map = {}

; The fixed-width r || s encoding, 32 bytes each (section 5). DER is not this.
es256_signature = bstr .size 64

; A P-256 public key in the 33-byte SEC 1 compressed encoding, the only
; encoding a derivation or comparison takes (section 9).
p256_public_key = bstr .size 33

; A node's identity: BLAKE3(IK_pub) truncated to 16 bytes.
site_id = bstr .size 16

; A mesh's identity, 16 bytes.
mesh_id = bstr .size 16

; The one hash (/substrate/ section 1): BLAKE3, 32-byte output, over the
; signed input (the RFC 9052 Sig_structure) of a signed structure and over the
; exact bytes of a stored or encrypted one.
blake3_hash = bstr .size 32

; The 96-bit A256GCM nonce.
aes_gcm_iv = bstr .size 12

; A 256-bit content key under AES Key Wrap: the key plus RFC 3394's 64-bit
; integrity block.
aes_kw_wrapped_key = bstr .size 40
