(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** CCSDS Security Algorithm Registry (350.0-B / 352.0-B). *) (* {1 Encryption Algorithms} *) type encryption = | AES_128_GCM (** Algorithm ID 0x01 *) | AES_256_GCM (** Algorithm ID 0x02 *) | AES_128_CCM (** Algorithm ID 0x03 *) | AES_256_CCM (** Algorithm ID 0x04 *) let encryption_of_int = function | 0x01 -> Some AES_128_GCM | 0x02 -> Some AES_256_GCM | 0x03 -> Some AES_128_CCM | 0x04 -> Some AES_256_CCM | _ -> None let int_of_encryption = function | AES_128_GCM -> 0x01 | AES_256_GCM -> 0x02 | AES_128_CCM -> 0x03 | AES_256_CCM -> 0x04 let pp_encryption ppf = function | AES_128_GCM -> Fmt.string ppf "AES-128-GCM" | AES_256_GCM -> Fmt.string ppf "AES-256-GCM" | AES_128_CCM -> Fmt.string ppf "AES-128-CCM" | AES_256_CCM -> Fmt.string ppf "AES-256-CCM" (* {1 Authentication Algorithms} *) type authentication = | AES_128_CMAC (** Algorithm ID 0x01 *) | AES_256_CMAC (** Algorithm ID 0x02 *) | HMAC_SHA_256 (** Algorithm ID 0x03 *) | HMAC_SHA_384 (** Algorithm ID 0x04 *) | HMAC_SHA_512 (** Algorithm ID 0x05 *) let authentication_of_int = function | 0x01 -> Some AES_128_CMAC | 0x02 -> Some AES_256_CMAC | 0x03 -> Some HMAC_SHA_256 | 0x04 -> Some HMAC_SHA_384 | 0x05 -> Some HMAC_SHA_512 | _ -> None let int_of_authentication = function | AES_128_CMAC -> 0x01 | AES_256_CMAC -> 0x02 | HMAC_SHA_256 -> 0x03 | HMAC_SHA_384 -> 0x04 | HMAC_SHA_512 -> 0x05 let pp_authentication ppf = function | AES_128_CMAC -> Fmt.string ppf "AES-128-CMAC" | AES_256_CMAC -> Fmt.string ppf "AES-256-CMAC" | HMAC_SHA_256 -> Fmt.string ppf "HMAC-SHA-256" | HMAC_SHA_384 -> Fmt.string ppf "HMAC-SHA-384" | HMAC_SHA_512 -> Fmt.string ppf "HMAC-SHA-512" (* {1 Key Derivation Functions} *) type key_derivation = HKDF_SHA_256 | PBKDF2_SHA_256 let pp_key_derivation ppf = function | HKDF_SHA_256 -> Fmt.string ppf "HKDF-SHA-256" | PBKDF2_SHA_256 -> Fmt.string ppf "PBKDF2-SHA-256" (* {1 Algorithm Suites} *) type suite = { encryption : encryption; authentication : authentication; key_len : int; iv_len : int; mac_len : int; } let aes_128_gcm_cmac = { encryption = AES_128_GCM; authentication = AES_128_CMAC; key_len = 16; iv_len = 12; mac_len = 16; } let aes_256_gcm_cmac = { encryption = AES_256_GCM; authentication = AES_256_CMAC; key_len = 32; iv_len = 12; mac_len = 16; } let aes_256_gcm_hmac_256 = { encryption = AES_256_GCM; authentication = HMAC_SHA_256; key_len = 32; iv_len = 12; mac_len = 32; } let aes_256_ccm_hmac_256 = { encryption = AES_256_CCM; authentication = HMAC_SHA_256; key_len = 32; iv_len = 12; mac_len = 32; } let pp_suite ppf s = Fmt.pf ppf "{ enc=%a; auth=%a; key=%d; iv=%d; mac=%d }" pp_encryption s.encryption pp_authentication s.authentication s.key_len s.iv_len s.mac_len (* {1 SA Negotiation} *) type proposal = { encryptions : encryption list; authentications : authentication list; key_derivations : key_derivation list; } type selection = { encryption : encryption; authentication : authentication; key_derivation : key_derivation option; } let negotiate initiator responder = let find_common l1 l2 = List.find_opt (fun x -> List.mem x l2) l1 in match ( find_common initiator.encryptions responder.encryptions, find_common initiator.authentications responder.authentications ) with | Some enc, Some auth -> let kdf = find_common initiator.key_derivations responder.key_derivations in Some { encryption = enc; authentication = auth; key_derivation = kdf } | _ -> None let pp_proposal ppf p = Fmt.pf ppf "{ enc=[%a]; auth=[%a]; kdf=[%a] }" Fmt.(list ~sep:comma pp_encryption) p.encryptions Fmt.(list ~sep:comma pp_authentication) p.authentications Fmt.(list ~sep:comma pp_key_derivation) p.key_derivations let pp_selection ppf s = Fmt.pf ppf "{ enc=%a; auth=%a; kdf=%a }" pp_encryption s.encryption pp_authentication s.authentication Fmt.(option ~none:(any "none") pp_key_derivation) s.key_derivation