///|
fn require_len(bytes : Bytes, n : Int32, what : String) -> Unit raise CoseError {
  if bytes.length() != n {
    raise BadStructure("\{what} must be \{n} bytes")
  }
}

///|
fn ed25519_seed(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Okp(okp) =>
      if okp.crv != 6 {
        raise AlgMismatch("EdDSA signing requires an Ed25519 OKP key")
      } else {
        match okp.d {
          Some(seed) => {
            require_len(seed, 32, "Ed25519 seed")
            seed
          }
          None => raise BadStructure("Ed25519 private key is missing d")
        }
      }
    _ => raise AlgMismatch("EdDSA signing requires an OKP key")
  }
}

///|
fn ed25519_public(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Okp(okp) =>
      if okp.crv != 6 {
        raise AlgMismatch("EdDSA verification requires an Ed25519 OKP key")
      } else if okp.x.length() == 32 {
        okp.x
      } else {
        match okp.d {
          Some(seed) => {
            require_len(seed, 32, "Ed25519 seed")
            @lib.ed25519_public_key(seed)
          }
          None => raise BadStructure("Ed25519 public key is missing x")
        }
      }
    _ => raise AlgMismatch("EdDSA verification requires an OKP key")
  }
}

///|
/// Ed448 seed. RFC 9053 uses pure Ed448 with an empty context (RFC 8032).
fn ed448_seed(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Okp(okp) =>
      if okp.crv != 7 {
        raise AlgMismatch("Ed448 signing requires an Ed448 OKP key")
      } else {
        match okp.d {
          Some(seed) => {
            require_len(seed, 57, "Ed448 seed")
            seed
          }
          None => raise BadStructure("Ed448 private key is missing d")
        }
      }
    _ => raise AlgMismatch("Ed448 signing requires an OKP key")
  }
}

///|
fn ed448_public(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Okp(okp) =>
      if okp.crv != 7 {
        raise AlgMismatch("Ed448 verification requires an Ed448 OKP key")
      } else if okp.x.length() == 57 {
        okp.x
      } else {
        match okp.d {
          Some(seed) => {
            require_len(seed, 57, "Ed448 seed")
            @lib.ed448_public_key(seed)
          }
          None => raise BadStructure("Ed448 public key is missing x")
        }
      }
    _ => raise AlgMismatch("Ed448 verification requires an OKP key")
  }
}

///|
fn eddsa_sign(key : CoseKey, tbs : Bytes) -> Bytes raise CoseError {
  match key {
    CoseKey::Okp(okp) =>
      if okp.crv == 6 {
        @lib.ed25519_sign(ed25519_seed(key), tbs)
      } else if okp.crv == 7 {
        @lib.ed448_sign(ed448_seed(key), tbs)
      } else {
        raise AlgMismatch("EdDSA requires an Ed25519 or Ed448 OKP key")
      }
    _ => raise AlgMismatch("EdDSA signing requires an OKP key")
  }
}

///|
fn eddsa_verify(
  key : CoseKey,
  tbs : Bytes,
  signature : Bytes,
) -> Bool raise CoseError {
  match key {
    CoseKey::Okp(okp) =>
      if okp.crv == 6 {
        @lib.ed25519_verify(ed25519_public(key), tbs, signature)
      } else if okp.crv == 7 {
        @lib.ed448_verify(ed448_public(key), tbs, signature)
      } else {
        raise AlgMismatch("EdDSA requires an Ed25519 or Ed448 OKP key")
      }
    _ => raise AlgMismatch("EdDSA verification requires an OKP key")
  }
}

///|
fn p256_private(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Ec2(ec2) =>
      if ec2.crv != 1 {
        raise AlgMismatch("ES256 signing requires a P-256 key")
      } else {
        match ec2.d {
          Some(d) => {
            require_len(d, 32, "P-256 private scalar")
            d
          }
          None => raise BadStructure("P-256 private key is missing d")
        }
      }
    _ => raise AlgMismatch("ES256 signing requires an EC2 key")
  }
}

///|
fn p256_public(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Ec2(ec2) =>
      if ec2.crv != 1 {
        raise AlgMismatch("ES256 verification requires a P-256 key")
      } else {
        match (ec2.y, ec2.d) {
          (Some(y), _) => {
            require_len(ec2.x, 32, "P-256 x")
            require_len(y, 32, "P-256 y")
            let out = Array::make(65, b'\x00')
            out[0] = b'\x04'
            for i = 0; i < 32; i = i + 1 {
              out[1 + i] = ec2.x[i]
              out[33 + i] = y[i]
            }
            Bytes::from_array(out)
          }
          (None, Some(d)) => {
            require_len(d, 32, "P-256 private scalar")
            @lib.ecdsa_p256_public_key(d)
          }
          (None, None) => raise BadStructure("P-256 public key is missing y")
        }
      }
    _ => raise AlgMismatch("ES256 verification requires an EC2 key")
  }
}

///|
fn symmetric_key(key : CoseKey) -> Bytes raise CoseError {
  match key {
    CoseKey::Sym(sym) => sym.k
    _ => raise AlgMismatch("this algorithm requires a symmetric key")
  }
}

///|
fn sign_with(
  alg : CoseAlg,
  key : CoseKey,
  tbs : Bytes,
) -> Bytes raise CoseError {
  match alg.normalize() {
    CoseAlg::EdDSA => eddsa_sign(key, tbs)
    CoseAlg::ES256 => @lib.ecdsa_p256_sign(p256_private(key), tbs)
    other => raise UnsupportedAlg(other.name())
  }
}

///|
fn verify_with(
  alg : CoseAlg,
  key : CoseKey,
  tbs : Bytes,
  signature : Bytes,
) -> Unit raise CoseError {
  let ok = match alg.normalize() {
    CoseAlg::EdDSA => eddsa_verify(key, tbs, signature)
    CoseAlg::ES256 => @lib.ecdsa_p256_verify(p256_public(key), tbs, signature)
    other => raise UnsupportedAlg(other.name())
  }
  if !ok {
    raise VerificationFailed
  }
}

///|

///|
/// One AES block encryption. mooncry's CBC helper always PKCS#7-pads and
/// prefixes the IV, so the first ciphertext block is AES(block XOR prev).
fn aes_block(key : Bytes, block : Bytes, prev : Bytes) -> Bytes raise CoseError {
  if block.length() != 16 || prev.length() != 16 {
    raise BadStructure("AES block must be 16 bytes")
  }
  let packed = @lib.aes_encrypt_cbc(block, key, prev)
  bytes_slice(packed, 16, 32)
}

///|
/// AES-CBC-MAC (RFC 9053 ยง3.2, CBC-MAC from the Handbook of Applied
/// Cryptography). The IV is all zeros. A partial final block is zero-padded.
/// A message that is already a multiple of the block size is not padded.
fn aes_cbc_mac(
  key : Bytes,
  message : Bytes,
  tag_len : Int32,
) -> Bytes raise CoseError {
  if key.length() != 16 && key.length() != 32 {
    raise BadStructure("AES-CBC-MAC key must be 16 or 32 bytes")
  }
  let mut state = Bytes::make(16, b'\x00')
  if message.length() > 0 {
    let blocks = (message.length() + 15) / 16
    for i = 0; i < blocks; i = i + 1 {
      let block = Array::make(16, b'\x00')
      let start = i * 16
      for j = 0; j < 16; j = j + 1 {
        let at = start + j
        if at < message.length() {
          block[j] = message[at]
        }
      }
      state = aes_block(key, Bytes::from_array(block), state)
    }
  }
  bytes_slice(state, 0, tag_len)
}

///|
fn aes_mac_key_ops(key : CoseKey) -> Unit raise CoseError {
  let ops = match key {
    CoseKey::Sym(sym) => sym.key_ops
    _ => []
  }
  if ops.length() == 0 {
    return
  }
  for i = 0; i < ops.length(); i = i + 1 {
    if ops[i] == 9 || ops[i] == 10 {
      return
    }
  }
  raise BadStructure("key_ops does not allow AES-CBC-MAC")
}

///|
fn mac_with(alg : CoseAlg, key : CoseKey, tbs : Bytes) -> Bytes raise CoseError {
  let raw = symmetric_key(key)
  match alg.normalize() {
    CoseAlg::HMAC_256_256 => @lib.hmac_sha256(raw, tbs)
    CoseAlg::HMAC_256_64 => bytes_slice(@lib.hmac_sha256(raw, tbs), 0, 8)
    CoseAlg::HMAC_384_384 => @lib.hmac_sha384(raw, tbs)
    CoseAlg::HMAC_512_512 => @lib.hmac_sha512(raw, tbs)
    CoseAlg::AesMac128_64 => {
      aes_mac_key_ops(key)
      require_len(raw, 16, "AES-MAC 128/64 key")
      aes_cbc_mac(raw, tbs, 8)
    }
    CoseAlg::AesMac256_64 => {
      aes_mac_key_ops(key)
      require_len(raw, 32, "AES-MAC 256/64 key")
      aes_cbc_mac(raw, tbs, 8)
    }
    CoseAlg::AesMac128_128 => {
      aes_mac_key_ops(key)
      require_len(raw, 16, "AES-MAC 128/128 key")
      aes_cbc_mac(raw, tbs, 16)
    }
    CoseAlg::AesMac256_128 => {
      aes_mac_key_ops(key)
      require_len(raw, 32, "AES-MAC 256/128 key")
      aes_cbc_mac(raw, tbs, 16)
    }
    other => raise UnsupportedAlg(other.name())
  }
}

///|
fn check_mac(
  alg : CoseAlg,
  key : CoseKey,
  tbs : Bytes,
  tag : Bytes,
) -> Unit raise CoseError {
  let expected = mac_with(alg, key, tbs)
  if !bytes_eq(expected, tag) {
    raise VerificationFailed
  }
}

///|
fn iv_size(alg : CoseAlg) -> Int32 raise CoseError {
  match alg.normalize() {
    CoseAlg::A128GCM
    | CoseAlg::A192GCM
    | CoseAlg::A256GCM
    | CoseAlg::ChaCha20Poly1305 => 12
    CoseAlg::AesCcm16_64_128
    | CoseAlg::AesCcm16_64_256
    | CoseAlg::AesCcm16_128_128
    | CoseAlg::AesCcm16_128_256 => 13
    CoseAlg::AesCcm64_64_128
    | CoseAlg::AesCcm64_64_256
    | CoseAlg::AesCcm64_128_128
    | CoseAlg::AesCcm64_128_256 => 7
    other => raise UnsupportedAlg(other.name())
  }
}

///|
fn partial_iv(prot : Header, unprot : Header) -> Bytes? raise CoseError {
  match (prot.partial_iv, unprot.partial_iv) {
    (Some(left), Some(right)) =>
      if bytes_eq(left, right) {
        Some(left)
      } else {
        raise BadStructure(
          "protected partial IV does not match unprotected partial IV",
        )
      }
    (Some(part), None) => Some(part)
    (None, Some(part)) => Some(part)
    (None, None) => None
  }
}

///|
/// Build the AEAD nonce. A full IV is used as-is. A partial IV is XORed onto
/// the right-hand side of the key's Base IV, or onto zeros when the key has
/// no Base IV (RFC 9052 / RFC 9053).
fn resolve_iv(
  alg : CoseAlg,
  prot : Header,
  unprot : Header,
  base_iv : Bytes?,
) -> Bytes raise CoseError {
  let size = iv_size(alg)
  match (prot.iv, unprot.iv) {
    (Some(left), Some(right)) => {
      if !bytes_eq(left, right) {
        raise BadStructure("protected IV does not match unprotected IV")
      }
      require_len(left, size, "IV")
      return left
    }
    (Some(iv), None) | (None, Some(iv)) => {
      require_len(iv, size, "IV")
      return iv
    }
    (None, None) => ()
  }
  match partial_iv(prot, unprot) {
    None => raise BadStructure("missing IV")
    Some(part) => {
      if part.length() == 0 || part.length() > size {
        raise BadStructure("partial IV length is outside the IV size")
      }
      let base = match base_iv {
        Some(b) => {
          require_len(b, size, "Base IV")
          b
        }
        None => Bytes::make(size, b'\x00')
      }
      let out = Array::make(size, b'\x00')
      for i = 0; i < size; i = i + 1 {
        out[i] = base[i]
      }
      let start = size - part.length()
      for i = 0; i < part.length(); i = i + 1 {
        let at = start + i
        out[at] = (out[at].to_int() ^ part[i].to_int()).to_byte()
      }
      Bytes::from_array(out)
    }
  }
}

///|
fn gcm_seal(
  key : Bytes,
  iv : Bytes,
  aad : Bytes,
  plaintext : Bytes,
) -> Bytes raise CoseError {
  if key.length() != 16 && key.length() != 24 && key.length() != 32 {
    raise BadStructure("AES-GCM key must be 16, 24, or 32 bytes")
  }
  if iv.length() != 12 {
    raise BadStructure("AES-GCM IV must be 12 bytes")
  }
  let boxed = @lib.aes_gcm_encrypt(plaintext, key, iv, aad)
  bytes_concat(boxed.ct, boxed.tag)
}

///|
fn gcm_open(
  key : Bytes,
  iv : Bytes,
  aad : Bytes,
  ciphertext : Bytes,
) -> Bytes raise CoseError {
  if key.length() != 16 && key.length() != 24 && key.length() != 32 {
    raise BadStructure("AES-GCM key must be 16, 24, or 32 bytes")
  }
  if iv.length() != 12 {
    raise BadStructure("AES-GCM IV must be 12 bytes")
  }
  if ciphertext.length() < 16 {
    raise DecryptionFailed
  }
  let split = ciphertext.length() - 16
  let ct = bytes_slice(ciphertext, 0, split)
  let tag = bytes_slice(ciphertext, split, ciphertext.length())
  match @lib.aes_gcm_decrypt(ct, key, iv, aad, tag) {
    Ok(pt) => pt
    Err(_) => raise DecryptionFailed
  }
}

///|
fn ccm_tag_len(alg : CoseAlg) -> Int32 {
  match alg.normalize() {
    CoseAlg::AesCcm16_64_128
    | CoseAlg::AesCcm16_64_256
    | CoseAlg::AesCcm64_64_128
    | CoseAlg::AesCcm64_64_256 => 8
    _ => 16
  }
}

///|
fn material_len(alg : CoseAlg) -> Int32 raise CoseError {
  match alg.normalize() {
    CoseAlg::HMAC_256_64 | CoseAlg::HMAC_256_256 => 32
    CoseAlg::HMAC_384_384 => 48
    CoseAlg::HMAC_512_512 => 64
    CoseAlg::AesMac128_64 | CoseAlg::AesMac128_128 | CoseAlg::A128KW => 16
    CoseAlg::A192KW => 24
    CoseAlg::AesMac256_64 | CoseAlg::AesMac256_128 | CoseAlg::A256KW => 32
    other => content_key_len(other)
  }
}

///|
fn content_key_len(alg : CoseAlg) -> Int32 raise CoseError {
  match alg.normalize() {
    CoseAlg::A128GCM
    | CoseAlg::AesCcm16_64_128
    | CoseAlg::AesCcm64_64_128
    | CoseAlg::AesCcm16_128_128
    | CoseAlg::AesCcm64_128_128 => 16
    CoseAlg::A192GCM => 24
    CoseAlg::A256GCM
    | CoseAlg::ChaCha20Poly1305
    | CoseAlg::AesCcm16_64_256
    | CoseAlg::AesCcm64_64_256
    | CoseAlg::AesCcm16_128_256
    | CoseAlg::AesCcm64_128_256 => 32
    other => raise UnsupportedAlg(other.name())
  }
}

///|
fn content_seal(
  alg : CoseAlg,
  key : Bytes,
  iv : Bytes,
  aad : Bytes,
  plaintext : Bytes,
) -> Bytes raise CoseError {
  let normalized = alg.normalize()
  require_len(key, content_key_len(normalized), "content key")
  require_len(iv, iv_size(normalized), "IV")
  match normalized {
    CoseAlg::A128GCM | CoseAlg::A192GCM | CoseAlg::A256GCM =>
      gcm_seal(key, iv, aad, plaintext)
    CoseAlg::ChaCha20Poly1305 =>
      @lib.chacha20_poly1305_encrypt(key, iv, aad, plaintext)
    CoseAlg::AesCcm16_64_128
    | CoseAlg::AesCcm16_64_256
    | CoseAlg::AesCcm64_64_128
    | CoseAlg::AesCcm64_64_256
    | CoseAlg::AesCcm16_128_128
    | CoseAlg::AesCcm16_128_256
    | CoseAlg::AesCcm64_128_128
    | CoseAlg::AesCcm64_128_256 =>
      @lib.aes_ccm_encrypt(plaintext, key, iv, aad, ccm_tag_len(normalized))
    other => raise UnsupportedAlg(other.name())
  }
}

///|
fn content_open(
  alg : CoseAlg,
  key : Bytes,
  iv : Bytes,
  aad : Bytes,
  ciphertext : Bytes,
) -> Bytes raise CoseError {
  let normalized = alg.normalize()
  require_len(key, content_key_len(normalized), "content key")
  require_len(iv, iv_size(normalized), "IV")
  match normalized {
    CoseAlg::A128GCM | CoseAlg::A192GCM | CoseAlg::A256GCM =>
      gcm_open(key, iv, aad, ciphertext)
    CoseAlg::ChaCha20Poly1305 =>
      match @lib.chacha20_poly1305_decrypt_or(key, iv, aad, ciphertext) {
        Ok(pt) => pt
        Err(_) => raise DecryptionFailed
      }
    CoseAlg::AesCcm16_64_128
    | CoseAlg::AesCcm16_64_256
    | CoseAlg::AesCcm64_64_128
    | CoseAlg::AesCcm64_64_256
    | CoseAlg::AesCcm16_128_128
    | CoseAlg::AesCcm16_128_256
    | CoseAlg::AesCcm64_128_128
    | CoseAlg::AesCcm64_128_256 =>
      match
        @lib.aes_ccm_decrypt_or(
          ciphertext,
          key,
          iv,
          aad,
          ccm_tag_len(normalized),
        ) {
        Ok(pt) => pt
        Err(_) => raise DecryptionFailed
      }
    other => raise UnsupportedAlg(other.name())
  }
}

///|
fn kw_wrap(alg : CoseAlg, kek : Bytes, cek : Bytes) -> Bytes raise CoseError {
  let want = match alg.normalize() {
    CoseAlg::A128KW => 16
    CoseAlg::A192KW => 24
    CoseAlg::A256KW => 32
    other => raise UnsupportedAlg(other.name())
  }
  require_len(kek, want, alg.name())
  if cek.length() == 0 || cek.length() % 8 != 0 {
    raise BadStructure("content key length must be a non-zero multiple of 8")
  }
  @lib.aes_kw_wrap(kek, cek)
}

///|
fn kw_unwrap(
  alg : CoseAlg,
  kek : Bytes,
  wrapped : Bytes,
) -> Bytes raise CoseError {
  let want = match alg.normalize() {
    CoseAlg::A128KW => 16
    CoseAlg::A192KW => 24
    CoseAlg::A256KW => 32
    other => raise UnsupportedAlg(other.name())
  }
  require_len(kek, want, alg.name())
  match @lib.aes_kw_unwrap(kek, wrapped) {
    Ok(cek) => cek
    Err(_) => raise DecryptionFailed
  }
}