hpke.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* Copyright (c) 2020, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H
  15. #define OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H
  16. #include <openssl/aead.h>
  17. #include <openssl/base.h>
  18. #include <openssl/curve25519.h>
  19. #include <openssl/digest.h>
  20. #if defined(__cplusplus)
  21. extern "C" {
  22. #endif
  23. // Hybrid Public Key Encryption.
  24. //
  25. // Hybrid Public Key Encryption (HPKE) enables a sender to encrypt messages to a
  26. // receiver with a public key.
  27. //
  28. // See RFC 9180.
  29. // Parameters.
  30. //
  31. // An HPKE context is parameterized by KEM, KDF, and AEAD algorithms,
  32. // represented by |EVP_HPKE_KEM|, |EVP_HPKE_KDF|, and |EVP_HPKE_AEAD| types,
  33. // respectively.
  34. // The following constants are KEM identifiers.
  35. #define EVP_HPKE_DHKEM_X25519_HKDF_SHA256 0x0020
  36. // The following functions are KEM algorithms which may be used with HPKE. Note
  37. // that, while some HPKE KEMs use KDFs internally, this is separate from the
  38. // |EVP_HPKE_KDF| selection.
  39. OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void);
  40. // EVP_HPKE_KEM_id returns the HPKE KEM identifier for |kem|, which
  41. // will be one of the |EVP_HPKE_KEM_*| constants.
  42. OPENSSL_EXPORT uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem);
  43. // The following constants are KDF identifiers.
  44. #define EVP_HPKE_HKDF_SHA256 0x0001
  45. // The following functions are KDF algorithms which may be used with HPKE.
  46. OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void);
  47. // EVP_HPKE_KDF_id returns the HPKE KDF identifier for |kdf|.
  48. OPENSSL_EXPORT uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf);
  49. // The following constants are AEAD identifiers.
  50. #define EVP_HPKE_AES_128_GCM 0x0001
  51. #define EVP_HPKE_AES_256_GCM 0x0002
  52. #define EVP_HPKE_CHACHA20_POLY1305 0x0003
  53. // The following functions are AEAD algorithms which may be used with HPKE.
  54. OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void);
  55. OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void);
  56. OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void);
  57. // EVP_HPKE_AEAD_id returns the HPKE AEAD identifier for |aead|.
  58. OPENSSL_EXPORT uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead);
  59. // EVP_HPKE_AEAD_aead returns the |EVP_AEAD| corresponding to |aead|.
  60. OPENSSL_EXPORT const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead);
  61. // Recipient keys.
  62. //
  63. // An HPKE recipient maintains a long-term KEM key. This library represents keys
  64. // with the |EVP_HPKE_KEY| type.
  65. // EVP_HPKE_KEY_zero sets an uninitialized |EVP_HPKE_KEY| to the zero state. The
  66. // caller should then use |EVP_HPKE_KEY_init|, |EVP_HPKE_KEY_copy|, or
  67. // |EVP_HPKE_KEY_generate| to finish initializing |key|.
  68. //
  69. // It is safe, but not necessary to call |EVP_HPKE_KEY_cleanup| in this state.
  70. // This may be used for more uniform cleanup of |EVP_HPKE_KEY|.
  71. OPENSSL_EXPORT void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key);
  72. // EVP_HPKE_KEY_cleanup releases memory referenced by |key|.
  73. OPENSSL_EXPORT void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key);
  74. // EVP_HPKE_KEY_new returns a newly-allocated |EVP_HPKE_KEY|, or NULL on error.
  75. // The caller must call |EVP_HPKE_KEY_free| on the result to release it.
  76. //
  77. // This is a convenience function for callers that need a heap-allocated
  78. // |EVP_HPKE_KEY|.
  79. OPENSSL_EXPORT EVP_HPKE_KEY *EVP_HPKE_KEY_new(void);
  80. // EVP_HPKE_KEY_free releases memory associated with |key|, which must have been
  81. // created with |EVP_HPKE_KEY_new|.
  82. OPENSSL_EXPORT void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key);
  83. // EVP_HPKE_KEY_copy sets |dst| to a copy of |src|. It returns one on success
  84. // and zero on error. On success, the caller must call |EVP_HPKE_KEY_cleanup| to
  85. // release |dst|. On failure, calling |EVP_HPKE_KEY_cleanup| is safe, but not
  86. // necessary.
  87. OPENSSL_EXPORT int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst,
  88. const EVP_HPKE_KEY *src);
  89. // EVP_HPKE_KEY_init decodes |priv_key| as a private key for |kem| and
  90. // initializes |key| with the result. It returns one on success and zero if
  91. // |priv_key| was invalid. On success, the caller must call
  92. // |EVP_HPKE_KEY_cleanup| to release the key. On failure, calling
  93. // |EVP_HPKE_KEY_cleanup| is safe, but not necessary.
  94. OPENSSL_EXPORT int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem,
  95. const uint8_t *priv_key,
  96. size_t priv_key_len);
  97. // EVP_HPKE_KEY_generate sets |key| to a newly-generated key using |kem|.
  98. OPENSSL_EXPORT int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key,
  99. const EVP_HPKE_KEM *kem);
  100. // EVP_HPKE_KEY_kem returns the HPKE KEM used by |key|.
  101. OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key);
  102. // EVP_HPKE_MAX_PUBLIC_KEY_LENGTH is the maximum length of a public key for all
  103. // KEMs supported by this library.
  104. #define EVP_HPKE_MAX_PUBLIC_KEY_LENGTH 32
  105. // EVP_HPKE_KEY_public_key writes |key|'s public key to |out| and sets
  106. // |*out_len| to the number of bytes written. On success, it returns one and
  107. // writes at most |max_out| bytes. If |max_out| is too small, it returns zero.
  108. // Setting |max_out| to |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH| will ensure the public
  109. // key fits.
  110. OPENSSL_EXPORT int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key,
  111. uint8_t *out, size_t *out_len,
  112. size_t max_out);
  113. // EVP_HPKE_MAX_PRIVATE_KEY_LENGTH is the maximum length of a private key for
  114. // all KEMs supported by this library.
  115. #define EVP_HPKE_MAX_PRIVATE_KEY_LENGTH 32
  116. // EVP_HPKE_KEY_private_key writes |key|'s private key to |out| and sets
  117. // |*out_len| to the number of bytes written. On success, it returns one and
  118. // writes at most |max_out| bytes. If |max_out| is too small, it returns zero.
  119. // Setting |max_out| to |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH| will ensure the
  120. // private key fits.
  121. OPENSSL_EXPORT int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key,
  122. uint8_t *out, size_t *out_len,
  123. size_t max_out);
  124. // Encryption contexts.
  125. //
  126. // An HPKE encryption context is represented by the |EVP_HPKE_CTX| type.
  127. // EVP_HPKE_CTX_zero sets an uninitialized |EVP_HPKE_CTX| to the zero state. The
  128. // caller should then use one of the |EVP_HPKE_CTX_setup_*| functions to finish
  129. // setting up |ctx|.
  130. //
  131. // It is safe, but not necessary to call |EVP_HPKE_CTX_cleanup| in this state.
  132. // This may be used for more uniform cleanup of |EVP_HPKE_CTX|.
  133. OPENSSL_EXPORT void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx);
  134. // EVP_HPKE_CTX_cleanup releases memory referenced by |ctx|. |ctx| must have
  135. // been initialized with |EVP_HPKE_CTX_zero| or one of the
  136. // |EVP_HPKE_CTX_setup_*| functions.
  137. OPENSSL_EXPORT void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx);
  138. // EVP_HPKE_CTX_new returns a newly-allocated |EVP_HPKE_CTX|, or NULL on error.
  139. // The caller must call |EVP_HPKE_CTX_free| on the result to release it.
  140. //
  141. // This is a convenience function for callers that need a heap-allocated
  142. // |EVP_HPKE_CTX|.
  143. OPENSSL_EXPORT EVP_HPKE_CTX *EVP_HPKE_CTX_new(void);
  144. // EVP_HPKE_CTX_free releases memory associated with |ctx|, which must have been
  145. // created with |EVP_HPKE_CTX_new|.
  146. OPENSSL_EXPORT void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx);
  147. // EVP_HPKE_MAX_ENC_LENGTH is the maximum length of "enc", the encapsulated
  148. // shared secret, for all supported KEMs in this library.
  149. #define EVP_HPKE_MAX_ENC_LENGTH 32
  150. // EVP_HPKE_CTX_setup_sender implements the SetupBaseS HPKE operation. It
  151. // encapsulates a shared secret for |peer_public_key| and sets up |ctx| as a
  152. // sender context. It writes the encapsulated shared secret to |out_enc| and
  153. // sets |*out_enc_len| to the number of bytes written. It writes at most
  154. // |max_enc| bytes and fails if the buffer is too small. Setting |max_enc| to at
  155. // least |EVP_HPKE_MAX_ENC_LENGTH| will ensure the buffer is large enough.
  156. //
  157. // This function returns one on success and zero on error. Note that
  158. // |peer_public_key| may be invalid, in which case this function will return an
  159. // error.
  160. //
  161. // On success, callers may call |EVP_HPKE_CTX_seal| to encrypt messages for the
  162. // recipient. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On
  163. // failure, calling |EVP_HPKE_CTX_cleanup| is safe, but not required.
  164. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender(
  165. EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
  166. const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
  167. const uint8_t *peer_public_key, size_t peer_public_key_len,
  168. const uint8_t *info, size_t info_len);
  169. // EVP_HPKE_CTX_setup_sender_with_seed_for_testing behaves like
  170. // |EVP_HPKE_CTX_setup_sender|, but takes a seed to behave deterministically.
  171. // The seed's format depends on |kem|. For X25519, it is the sender's
  172. // ephemeral private key.
  173. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
  174. EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
  175. const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
  176. const uint8_t *peer_public_key, size_t peer_public_key_len,
  177. const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len);
  178. // EVP_HPKE_CTX_setup_recipient implements the SetupBaseR HPKE operation. It
  179. // decapsulates the shared secret in |enc| with |key| and sets up |ctx| as a
  180. // recipient context. It returns one on success and zero on failure. Note that
  181. // |enc| may be invalid, in which case this function will return an error.
  182. //
  183. // On success, callers may call |EVP_HPKE_CTX_open| to decrypt messages from the
  184. // sender. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On failure,
  185. // calling |EVP_HPKE_CTX_cleanup| is safe, but not required.
  186. OPENSSL_EXPORT int EVP_HPKE_CTX_setup_recipient(
  187. EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
  188. const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
  189. const uint8_t *info, size_t info_len);
  190. // Using an HPKE context.
  191. //
  192. // Once set up, callers may encrypt or decrypt with an |EVP_HPKE_CTX| using the
  193. // following functions.
  194. // EVP_HPKE_CTX_open uses the HPKE context |ctx| to authenticate |in_len| bytes
  195. // from |in| and |ad_len| bytes from |ad| and to decrypt at most |in_len| bytes
  196. // into |out|. It returns one on success, and zero otherwise.
  197. //
  198. // This operation will fail if the |ctx| context is not set up as a receiver.
  199. //
  200. // Note that HPKE encryption is stateful and ordered. The sender's first call to
  201. // |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to
  202. // |EVP_HPKE_CTX_open|, etc.
  203. //
  204. // At most |in_len| bytes are written to |out|. In order to ensure success,
  205. // |max_out_len| should be at least |in_len|. On successful return, |*out_len|
  206. // is set to the actual number of bytes written.
  207. OPENSSL_EXPORT int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out,
  208. size_t *out_len, size_t max_out_len,
  209. const uint8_t *in, size_t in_len,
  210. const uint8_t *ad, size_t ad_len);
  211. // EVP_HPKE_CTX_seal uses the HPKE context |ctx| to encrypt and authenticate
  212. // |in_len| bytes of ciphertext |in| and authenticate |ad_len| bytes from |ad|,
  213. // writing the result to |out|. It returns one on success and zero otherwise.
  214. //
  215. // This operation will fail if the |ctx| context is not set up as a sender.
  216. //
  217. // Note that HPKE encryption is stateful and ordered. The sender's first call to
  218. // |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to
  219. // |EVP_HPKE_CTX_open|, etc.
  220. //
  221. // At most, |max_out_len| encrypted bytes are written to |out|. On successful
  222. // return, |*out_len| is set to the actual number of bytes written.
  223. //
  224. // To ensure success, |max_out_len| should be |in_len| plus the result of
  225. // |EVP_HPKE_CTX_max_overhead| or |EVP_HPKE_MAX_OVERHEAD|.
  226. OPENSSL_EXPORT int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out,
  227. size_t *out_len, size_t max_out_len,
  228. const uint8_t *in, size_t in_len,
  229. const uint8_t *ad, size_t ad_len);
  230. // EVP_HPKE_CTX_export uses the HPKE context |ctx| to export a secret of
  231. // |secret_len| bytes into |out|. This function uses |context_len| bytes from
  232. // |context| as a context string for the secret. This is necessary to separate
  233. // different uses of exported secrets and bind relevant caller-specific context
  234. // into the output. It returns one on success and zero otherwise.
  235. OPENSSL_EXPORT int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out,
  236. size_t secret_len,
  237. const uint8_t *context,
  238. size_t context_len);
  239. // EVP_HPKE_MAX_OVERHEAD contains the largest value that
  240. // |EVP_HPKE_CTX_max_overhead| would ever return for any context.
  241. #define EVP_HPKE_MAX_OVERHEAD EVP_AEAD_MAX_OVERHEAD
  242. // EVP_HPKE_CTX_max_overhead returns the maximum number of additional bytes
  243. // added by sealing data with |EVP_HPKE_CTX_seal|. The |ctx| context must be set
  244. // up as a sender.
  245. OPENSSL_EXPORT size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx);
  246. // EVP_HPKE_CTX_aead returns |ctx|'s configured AEAD, or NULL if the context has
  247. // not been set up.
  248. OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx);
  249. // EVP_HPKE_CTX_kdf returns |ctx|'s configured KDF, or NULL if the context has
  250. // not been set up.
  251. OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx);
  252. // Private structures.
  253. //
  254. // The following structures are exported so their types are stack-allocatable,
  255. // but accessing or modifying their fields is forbidden.
  256. struct evp_hpke_ctx_st {
  257. const EVP_HPKE_AEAD *aead;
  258. const EVP_HPKE_KDF *kdf;
  259. EVP_AEAD_CTX aead_ctx;
  260. uint8_t base_nonce[EVP_AEAD_MAX_NONCE_LENGTH];
  261. uint8_t exporter_secret[EVP_MAX_MD_SIZE];
  262. uint64_t seq;
  263. int is_sender;
  264. };
  265. struct evp_hpke_key_st {
  266. const EVP_HPKE_KEM *kem;
  267. uint8_t private_key[X25519_PRIVATE_KEY_LEN];
  268. uint8_t public_key[X25519_PUBLIC_VALUE_LEN];
  269. };
  270. #if defined(__cplusplus)
  271. } // extern C
  272. #endif
  273. #if !defined(BORINGSSL_NO_CXX)
  274. extern "C++" {
  275. BSSL_NAMESPACE_BEGIN
  276. using ScopedEVP_HPKE_CTX =
  277. internal::StackAllocated<EVP_HPKE_CTX, void, EVP_HPKE_CTX_zero,
  278. EVP_HPKE_CTX_cleanup>;
  279. using ScopedEVP_HPKE_KEY =
  280. internal::StackAllocated<EVP_HPKE_KEY, void, EVP_HPKE_KEY_zero,
  281. EVP_HPKE_KEY_cleanup>;
  282. BORINGSSL_MAKE_DELETER(EVP_HPKE_CTX, EVP_HPKE_CTX_free)
  283. BORINGSSL_MAKE_DELETER(EVP_HPKE_KEY, EVP_HPKE_KEY_free)
  284. BSSL_NAMESPACE_END
  285. } // extern C++
  286. #endif
  287. #endif // OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H