s2n_kem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "tls/s2n_kem.h"
  16. #include "pq-crypto/s2n_kyber_evp.h"
  17. #include "pq-crypto/s2n_pq.h"
  18. #include "stuffer/s2n_stuffer.h"
  19. #include "tls/extensions/s2n_key_share.h"
  20. #include "tls/s2n_tls_parameters.h"
  21. #include "utils/s2n_mem.h"
  22. #include "utils/s2n_safety.h"
  23. /* If S2N_NO_PQ is set or linked libcrypto doesn't support Kyber, bail on KEM calls.
  24. * These should never be called.
  25. */
  26. int s2n_kyber_kem_keypair_not_supported(IN const struct s2n_kem *kem, OUT uint8_t *pk, OUT uint8_t *sk)
  27. {
  28. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  29. }
  30. int s2n_kyber_kem_enc_not_supported(IN const struct s2n_kem *kem, OUT uint8_t *ct, OUT uint8_t *ss,
  31. IN const uint8_t *pk)
  32. {
  33. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  34. }
  35. int s2n_kyber_kem_dec_not_supported(IN const struct s2n_kem *kem, OUT uint8_t *ss, IN const uint8_t *ct,
  36. IN const uint8_t *sk)
  37. {
  38. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  39. }
  40. #if defined(S2N_NO_PQ)
  41. /* If S2N_NO_PQ was defined at compile time, the PQ KEM code will have been entirely excluded
  42. * from compilation. We define stubs of these functions here to error if they are called. */
  43. int s2n_kyber_512_r3_crypto_kem_keypair(IN const struct s2n_kem *kem, OUT uint8_t *pk, OUT uint8_t *sk)
  44. {
  45. return s2n_kyber_kem_keypair_not_supported(kem, pk, sk);
  46. }
  47. int s2n_kyber_512_r3_crypto_kem_enc(IN const struct s2n_kem *kem, OUT uint8_t *ct, OUT uint8_t *ss,
  48. IN const uint8_t *pk)
  49. {
  50. return s2n_kyber_kem_enc_not_supported(kem, ct, ss, pk);
  51. }
  52. int s2n_kyber_512_r3_crypto_kem_dec(IN const struct s2n_kem *kem, OUT uint8_t *ss, IN const uint8_t *ct,
  53. IN const uint8_t *sk)
  54. {
  55. return s2n_kyber_kem_dec_not_supported(kem, ss, ct, sk);
  56. }
  57. #endif
  58. /* The KEM IDs and names come from https://tools.ietf.org/html/draft-campagna-tls-bike-sike-hybrid */
  59. const struct s2n_kem s2n_kyber_512_r3 = {
  60. .name = "kyber512r3",
  61. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  62. .kem_nid = NID_KYBER512_R3,
  63. #else
  64. .kem_nid = NID_undef,
  65. #endif
  66. .kem_extension_id = TLS_PQ_KEM_EXTENSION_ID_KYBER_512_R3,
  67. .public_key_length = S2N_KYBER_512_R3_PUBLIC_KEY_BYTES,
  68. .private_key_length = S2N_KYBER_512_R3_SECRET_KEY_BYTES,
  69. .shared_secret_key_length = S2N_KYBER_512_R3_SHARED_SECRET_BYTES,
  70. .ciphertext_length = S2N_KYBER_512_R3_CIPHERTEXT_BYTES,
  71. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && !defined(S2N_NO_PQ)
  72. .generate_keypair = &s2n_kyber_evp_generate_keypair,
  73. .encapsulate = &s2n_kyber_evp_encapsulate,
  74. .decapsulate = &s2n_kyber_evp_decapsulate,
  75. #else
  76. .generate_keypair = &s2n_kyber_512_r3_crypto_kem_keypair,
  77. .encapsulate = &s2n_kyber_512_r3_crypto_kem_enc,
  78. .decapsulate = &s2n_kyber_512_r3_crypto_kem_dec,
  79. #endif
  80. };
  81. const struct s2n_kem s2n_kyber_768_r3 = {
  82. .name = "kyber768r3",
  83. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  84. .kem_nid = NID_KYBER768_R3,
  85. #else
  86. .kem_nid = NID_undef,
  87. #endif
  88. .kem_extension_id = 0, /* This is not used in TLS 1.2's KEM extension */
  89. .public_key_length = S2N_KYBER_768_R3_PUBLIC_KEY_BYTES,
  90. .private_key_length = S2N_KYBER_768_R3_SECRET_KEY_BYTES,
  91. .shared_secret_key_length = S2N_KYBER_768_R3_SHARED_SECRET_BYTES,
  92. .ciphertext_length = S2N_KYBER_768_R3_CIPHERTEXT_BYTES,
  93. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && !defined(S2N_NO_PQ)
  94. .generate_keypair = &s2n_kyber_evp_generate_keypair,
  95. .encapsulate = &s2n_kyber_evp_encapsulate,
  96. .decapsulate = &s2n_kyber_evp_decapsulate,
  97. #else
  98. .generate_keypair = &s2n_kyber_kem_keypair_not_supported,
  99. .encapsulate = &s2n_kyber_kem_enc_not_supported,
  100. .decapsulate = &s2n_kyber_kem_dec_not_supported,
  101. #endif
  102. };
  103. const struct s2n_kem s2n_kyber_1024_r3 = {
  104. .name = "kyber1024r3",
  105. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  106. .kem_nid = NID_KYBER1024_R3,
  107. #else
  108. .kem_nid = NID_undef,
  109. #endif
  110. .kem_extension_id = 0, /* This is not used in TLS 1.2's KEM extension */
  111. .public_key_length = S2N_KYBER_1024_R3_PUBLIC_KEY_BYTES,
  112. .private_key_length = S2N_KYBER_1024_R3_SECRET_KEY_BYTES,
  113. .shared_secret_key_length = S2N_KYBER_1024_R3_SHARED_SECRET_BYTES,
  114. .ciphertext_length = S2N_KYBER_1024_R3_CIPHERTEXT_BYTES,
  115. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && !defined(S2N_NO_PQ)
  116. .generate_keypair = &s2n_kyber_evp_generate_keypair,
  117. .encapsulate = &s2n_kyber_evp_encapsulate,
  118. .decapsulate = &s2n_kyber_evp_decapsulate,
  119. #else
  120. .generate_keypair = &s2n_kyber_kem_keypair_not_supported,
  121. .encapsulate = &s2n_kyber_kem_enc_not_supported,
  122. .decapsulate = &s2n_kyber_kem_dec_not_supported,
  123. #endif
  124. };
  125. const struct s2n_kem *tls12_kyber_kems[] = {
  126. &s2n_kyber_512_r3,
  127. };
  128. const struct s2n_iana_to_kem kem_mapping[1] = {
  129. {
  130. .iana_value = { TLS_ECDHE_KYBER_RSA_WITH_AES_256_GCM_SHA384 },
  131. .kems = tls12_kyber_kems,
  132. .kem_count = s2n_array_len(tls12_kyber_kems),
  133. },
  134. };
  135. /* Specific assignments of KEM group IDs and names have not yet been
  136. * published in an RFC (or draft). There is consensus in the
  137. * community to use values in the proposed reserved range defined in
  138. * https://tools.ietf.org/html/draft-stebila-tls-hybrid-design.
  139. * Values for interoperability are defined in
  140. * https://github.com/open-quantum-safe/oqs-provider/blob/main/oqs-template/oqs-kem-info.md
  141. * and
  142. * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml
  143. *
  144. * The structure of the hybrid share is:
  145. * size of ECC key share (2 bytes)
  146. * || ECC key share (variable bytes)
  147. * || size of PQ key share (2 bytes)
  148. * || PQ key share (variable bytes) */
  149. const struct s2n_kem_group s2n_secp256r1_kyber_512_r3 = {
  150. .name = "secp256r1_kyber-512-r3",
  151. .iana_id = TLS_PQ_KEM_GROUP_ID_SECP256R1_KYBER_512_R3,
  152. .curve = &s2n_ecc_curve_secp256r1,
  153. .kem = &s2n_kyber_512_r3,
  154. };
  155. const struct s2n_kem_group s2n_secp256r1_kyber_768_r3 = {
  156. .name = "SecP256r1Kyber768Draft00",
  157. .iana_id = TLS_PQ_KEM_GROUP_ID_SECP256R1_KYBER_768_R3,
  158. .curve = &s2n_ecc_curve_secp256r1,
  159. .kem = &s2n_kyber_768_r3,
  160. };
  161. const struct s2n_kem_group s2n_secp384r1_kyber_768_r3 = {
  162. .name = "secp384r1_kyber-768-r3",
  163. .iana_id = TLS_PQ_KEM_GROUP_ID_SECP384R1_KYBER_768_R3,
  164. .curve = &s2n_ecc_curve_secp384r1,
  165. .kem = &s2n_kyber_768_r3,
  166. };
  167. const struct s2n_kem_group s2n_secp521r1_kyber_1024_r3 = {
  168. .name = "secp521r1_kyber-1024-r3",
  169. .iana_id = TLS_PQ_KEM_GROUP_ID_SECP521R1_KYBER_1024_R3,
  170. .curve = &s2n_ecc_curve_secp521r1,
  171. .kem = &s2n_kyber_1024_r3,
  172. };
  173. const struct s2n_kem_group s2n_x25519_kyber_512_r3 = {
  174. .name = "x25519_kyber-512-r3",
  175. .iana_id = TLS_PQ_KEM_GROUP_ID_X25519_KYBER_512_R3,
  176. .curve = &s2n_ecc_curve_x25519,
  177. .kem = &s2n_kyber_512_r3,
  178. };
  179. const struct s2n_kem_group s2n_x25519_kyber_768_r3 = {
  180. .name = "X25519Kyber768Draft00",
  181. .iana_id = TLS_PQ_KEM_GROUP_ID_X25519_KYBER_768_R3,
  182. .curve = &s2n_ecc_curve_x25519,
  183. .kem = &s2n_kyber_768_r3,
  184. };
  185. const struct s2n_kem_group *ALL_SUPPORTED_KEM_GROUPS[S2N_SUPPORTED_KEM_GROUPS_COUNT] = {
  186. &s2n_secp256r1_kyber_512_r3,
  187. /* x25519 based tls13_kem_groups require EVP_APIS_SUPPORTED */
  188. #if EVP_APIS_SUPPORTED
  189. &s2n_x25519_kyber_512_r3,
  190. #endif
  191. /* Kyber 768+ is only available from libcrypto */
  192. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  193. &s2n_secp256r1_kyber_768_r3,
  194. &s2n_secp384r1_kyber_768_r3,
  195. &s2n_secp521r1_kyber_1024_r3,
  196. #endif
  197. #if EVP_APIS_SUPPORTED && defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  198. &s2n_x25519_kyber_768_r3,
  199. #endif
  200. };
  201. /* Helper safety macro to call the NIST PQ KEM functions. The NIST
  202. * functions may return any non-zero value to indicate failure. */
  203. #define GUARD_PQ_AS_RESULT(x) RESULT_ENSURE((x) == 0, S2N_ERR_PQ_CRYPTO)
  204. S2N_RESULT s2n_kem_generate_keypair(struct s2n_kem_params *kem_params)
  205. {
  206. RESULT_ENSURE_REF(kem_params);
  207. RESULT_ENSURE_REF(kem_params->kem);
  208. const struct s2n_kem *kem = kem_params->kem;
  209. RESULT_ENSURE_REF(kem->generate_keypair);
  210. RESULT_ENSURE_REF(kem_params->public_key.data);
  211. RESULT_ENSURE(kem_params->public_key.size == kem->public_key_length, S2N_ERR_SAFETY);
  212. /* Need to save the private key for decapsulation */
  213. RESULT_GUARD_POSIX(s2n_realloc(&kem_params->private_key, kem->private_key_length));
  214. GUARD_PQ_AS_RESULT(kem->generate_keypair(kem, kem_params->public_key.data, kem_params->private_key.data));
  215. return S2N_RESULT_OK;
  216. }
  217. S2N_RESULT s2n_kem_encapsulate(struct s2n_kem_params *kem_params, struct s2n_blob *ciphertext)
  218. {
  219. RESULT_ENSURE_REF(kem_params);
  220. RESULT_ENSURE_REF(kem_params->kem);
  221. const struct s2n_kem *kem = kem_params->kem;
  222. RESULT_ENSURE_REF(kem->encapsulate);
  223. RESULT_ENSURE(kem_params->public_key.size == kem->public_key_length, S2N_ERR_SAFETY);
  224. RESULT_ENSURE_REF(kem_params->public_key.data);
  225. RESULT_ENSURE_REF(ciphertext);
  226. RESULT_ENSURE_REF(ciphertext->data);
  227. RESULT_ENSURE(ciphertext->size == kem->ciphertext_length, S2N_ERR_SAFETY);
  228. /* Need to save the shared secret for key derivation */
  229. RESULT_GUARD_POSIX(s2n_alloc(&(kem_params->shared_secret), kem->shared_secret_key_length));
  230. GUARD_PQ_AS_RESULT(kem->encapsulate(kem, ciphertext->data, kem_params->shared_secret.data, kem_params->public_key.data));
  231. return S2N_RESULT_OK;
  232. }
  233. S2N_RESULT s2n_kem_decapsulate(struct s2n_kem_params *kem_params, const struct s2n_blob *ciphertext)
  234. {
  235. RESULT_ENSURE_REF(kem_params);
  236. RESULT_ENSURE_REF(kem_params->kem);
  237. const struct s2n_kem *kem = kem_params->kem;
  238. RESULT_ENSURE_REF(kem->decapsulate);
  239. RESULT_ENSURE(kem_params->private_key.size == kem->private_key_length, S2N_ERR_SAFETY);
  240. RESULT_ENSURE_REF(kem_params->private_key.data);
  241. RESULT_ENSURE_REF(ciphertext);
  242. RESULT_ENSURE_REF(ciphertext->data);
  243. RESULT_ENSURE(ciphertext->size == kem->ciphertext_length, S2N_ERR_SAFETY);
  244. /* Need to save the shared secret for key derivation */
  245. RESULT_GUARD_POSIX(s2n_alloc(&(kem_params->shared_secret), kem->shared_secret_key_length));
  246. GUARD_PQ_AS_RESULT(kem->decapsulate(kem, kem_params->shared_secret.data, ciphertext->data, kem_params->private_key.data));
  247. return S2N_RESULT_OK;
  248. }
  249. static int s2n_kem_check_kem_compatibility(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], const struct s2n_kem *candidate_kem,
  250. uint8_t *kem_is_compatible)
  251. {
  252. const struct s2n_iana_to_kem *compatible_kems = NULL;
  253. POSIX_GUARD(s2n_cipher_suite_to_kem(iana_value, &compatible_kems));
  254. for (uint8_t i = 0; i < compatible_kems->kem_count; i++) {
  255. if (candidate_kem->kem_extension_id == compatible_kems->kems[i]->kem_extension_id) {
  256. *kem_is_compatible = 1;
  257. return S2N_SUCCESS;
  258. }
  259. }
  260. *kem_is_compatible = 0;
  261. return S2N_SUCCESS;
  262. }
  263. int s2n_choose_kem_with_peer_pref_list(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], struct s2n_blob *client_kem_ids,
  264. const struct s2n_kem *server_kem_pref_list[], const uint8_t num_server_supported_kems, const struct s2n_kem **chosen_kem)
  265. {
  266. struct s2n_stuffer client_kem_ids_stuffer = { 0 };
  267. POSIX_GUARD(s2n_stuffer_init(&client_kem_ids_stuffer, client_kem_ids));
  268. POSIX_GUARD(s2n_stuffer_write(&client_kem_ids_stuffer, client_kem_ids));
  269. /* Each KEM ID is 2 bytes */
  270. uint8_t num_client_candidate_kems = client_kem_ids->size / 2;
  271. for (uint8_t i = 0; i < num_server_supported_kems; i++) {
  272. const struct s2n_kem *candidate_server_kem = (server_kem_pref_list[i]);
  273. uint8_t server_kem_is_compatible = 0;
  274. POSIX_GUARD(s2n_kem_check_kem_compatibility(iana_value, candidate_server_kem, &server_kem_is_compatible));
  275. if (!server_kem_is_compatible) {
  276. continue;
  277. }
  278. for (uint8_t j = 0; j < num_client_candidate_kems; j++) {
  279. kem_extension_size candidate_client_kem_id;
  280. POSIX_GUARD(s2n_stuffer_read_uint16(&client_kem_ids_stuffer, &candidate_client_kem_id));
  281. if (candidate_server_kem->kem_extension_id == candidate_client_kem_id) {
  282. *chosen_kem = candidate_server_kem;
  283. return S2N_SUCCESS;
  284. }
  285. }
  286. POSIX_GUARD(s2n_stuffer_reread(&client_kem_ids_stuffer));
  287. }
  288. /* Client and server did not propose any mutually supported KEMs compatible with the ciphersuite */
  289. POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
  290. }
  291. int s2n_choose_kem_without_peer_pref_list(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], const struct s2n_kem *server_kem_pref_list[],
  292. const uint8_t num_server_supported_kems, const struct s2n_kem **chosen_kem)
  293. {
  294. for (uint8_t i = 0; i < num_server_supported_kems; i++) {
  295. uint8_t kem_is_compatible = 0;
  296. POSIX_GUARD(s2n_kem_check_kem_compatibility(iana_value, server_kem_pref_list[i], &kem_is_compatible));
  297. if (kem_is_compatible) {
  298. *chosen_kem = server_kem_pref_list[i];
  299. return S2N_SUCCESS;
  300. }
  301. }
  302. /* The server preference list did not contain any KEM extensions compatible with the ciphersuite */
  303. POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
  304. }
  305. int s2n_kem_free(struct s2n_kem_params *kem_params)
  306. {
  307. if (kem_params != NULL) {
  308. POSIX_GUARD(s2n_free_or_wipe(&kem_params->private_key));
  309. POSIX_GUARD(s2n_free_or_wipe(&kem_params->public_key));
  310. POSIX_GUARD(s2n_free_or_wipe(&kem_params->shared_secret));
  311. }
  312. return S2N_SUCCESS;
  313. }
  314. int s2n_kem_group_free(struct s2n_kem_group_params *kem_group_params)
  315. {
  316. if (kem_group_params != NULL) {
  317. POSIX_GUARD(s2n_kem_free(&kem_group_params->kem_params));
  318. POSIX_GUARD(s2n_ecc_evp_params_free(&kem_group_params->ecc_params));
  319. }
  320. return S2N_SUCCESS;
  321. }
  322. int s2n_cipher_suite_to_kem(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], const struct s2n_iana_to_kem **compatible_params)
  323. {
  324. for (size_t i = 0; i < s2n_array_len(kem_mapping); i++) {
  325. const struct s2n_iana_to_kem *candidate = &kem_mapping[i];
  326. if (memcmp(iana_value, candidate->iana_value, S2N_TLS_CIPHER_SUITE_LEN) == 0) {
  327. *compatible_params = candidate;
  328. return S2N_SUCCESS;
  329. }
  330. }
  331. POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
  332. }
  333. int s2n_get_kem_from_extension_id(kem_extension_size kem_id, const struct s2n_kem **kem)
  334. {
  335. for (size_t i = 0; i < s2n_array_len(kem_mapping); i++) {
  336. const struct s2n_iana_to_kem *iana_to_kem = &kem_mapping[i];
  337. for (int j = 0; j < iana_to_kem->kem_count; j++) {
  338. const struct s2n_kem *candidate_kem = iana_to_kem->kems[j];
  339. if (candidate_kem->kem_extension_id == kem_id) {
  340. *kem = candidate_kem;
  341. return S2N_SUCCESS;
  342. }
  343. }
  344. }
  345. POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
  346. }
  347. int s2n_kem_send_public_key(struct s2n_stuffer *out, struct s2n_kem_params *kem_params)
  348. {
  349. POSIX_ENSURE_REF(out);
  350. POSIX_ENSURE_REF(kem_params);
  351. POSIX_ENSURE_REF(kem_params->kem);
  352. const struct s2n_kem *kem = kem_params->kem;
  353. if (kem_params->len_prefixed) {
  354. POSIX_GUARD(s2n_stuffer_write_uint16(out, kem->public_key_length));
  355. }
  356. /* We don't need to store the public key after sending it.
  357. * We write it directly to *out. */
  358. kem_params->public_key.data = s2n_stuffer_raw_write(out, kem->public_key_length);
  359. POSIX_ENSURE_REF(kem_params->public_key.data);
  360. kem_params->public_key.size = kem->public_key_length;
  361. /* Saves the private key in kem_params */
  362. POSIX_GUARD_RESULT(s2n_kem_generate_keypair(kem_params));
  363. /* After using s2n_stuffer_raw_write() above to write the public
  364. * key to the stuffer, we want to ensure that kem_params->public_key.data
  365. * does not continue to point at *out, else we may unexpectedly
  366. * overwrite part of the stuffer when s2n_kem_free() is called. */
  367. kem_params->public_key.data = NULL;
  368. kem_params->public_key.size = 0;
  369. return S2N_SUCCESS;
  370. }
  371. int s2n_kem_recv_public_key(struct s2n_stuffer *in, struct s2n_kem_params *kem_params)
  372. {
  373. POSIX_ENSURE_REF(in);
  374. POSIX_ENSURE_REF(kem_params);
  375. POSIX_ENSURE_REF(kem_params->kem);
  376. const struct s2n_kem *kem = kem_params->kem;
  377. if (kem_params->len_prefixed) {
  378. kem_public_key_size public_key_length = 0;
  379. POSIX_GUARD(s2n_stuffer_read_uint16(in, &public_key_length));
  380. POSIX_ENSURE(public_key_length == kem->public_key_length, S2N_ERR_BAD_MESSAGE);
  381. }
  382. /* Alloc memory for the public key; the peer receiving it will need it
  383. * later during the handshake to encapsulate the shared secret. */
  384. POSIX_GUARD(s2n_alloc(&(kem_params->public_key), kem->public_key_length));
  385. POSIX_GUARD(s2n_stuffer_read_bytes(in, kem_params->public_key.data, kem->public_key_length));
  386. return S2N_SUCCESS;
  387. }
  388. int s2n_kem_send_ciphertext(struct s2n_stuffer *out, struct s2n_kem_params *kem_params)
  389. {
  390. POSIX_ENSURE_REF(out);
  391. POSIX_ENSURE_REF(kem_params);
  392. POSIX_ENSURE_REF(kem_params->kem);
  393. POSIX_ENSURE_REF(kem_params->public_key.data);
  394. const struct s2n_kem *kem = kem_params->kem;
  395. if (kem_params->len_prefixed) {
  396. POSIX_GUARD(s2n_stuffer_write_uint16(out, kem->ciphertext_length));
  397. }
  398. /* Ciphertext will get written to *out */
  399. struct s2n_blob ciphertext = { 0 };
  400. POSIX_GUARD(s2n_blob_init(&ciphertext, s2n_stuffer_raw_write(out, kem->ciphertext_length), kem->ciphertext_length));
  401. POSIX_ENSURE_REF(ciphertext.data);
  402. /* Saves the shared secret in kem_params */
  403. POSIX_GUARD_RESULT(s2n_kem_encapsulate(kem_params, &ciphertext));
  404. return S2N_SUCCESS;
  405. }
  406. int s2n_kem_recv_ciphertext(struct s2n_stuffer *in, struct s2n_kem_params *kem_params)
  407. {
  408. POSIX_ENSURE_REF(in);
  409. POSIX_ENSURE_REF(kem_params);
  410. POSIX_ENSURE_REF(kem_params->kem);
  411. POSIX_ENSURE_REF(kem_params->private_key.data);
  412. const struct s2n_kem *kem = kem_params->kem;
  413. if (kem_params->len_prefixed) {
  414. kem_ciphertext_key_size ciphertext_length = 0;
  415. POSIX_GUARD(s2n_stuffer_read_uint16(in, &ciphertext_length));
  416. POSIX_ENSURE(ciphertext_length == kem->ciphertext_length, S2N_ERR_BAD_MESSAGE);
  417. }
  418. const struct s2n_blob ciphertext = { .data = s2n_stuffer_raw_read(in, kem->ciphertext_length), .size = kem->ciphertext_length };
  419. POSIX_ENSURE_REF(ciphertext.data);
  420. /* Saves the shared secret in kem_params */
  421. POSIX_GUARD_RESULT(s2n_kem_decapsulate(kem_params, &ciphertext));
  422. return S2N_SUCCESS;
  423. }