s2n_kem.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #pragma once
  16. #include <stdint.h>
  17. #include "crypto/s2n_ecc_evp.h"
  18. #include "stuffer/s2n_stuffer.h"
  19. #include "tls/s2n_crypto_constants.h"
  20. #include "utils/s2n_blob.h"
  21. typedef uint16_t kem_extension_size;
  22. typedef uint16_t kem_public_key_size;
  23. typedef uint16_t kem_private_key_size;
  24. typedef uint16_t kem_shared_secret_size;
  25. typedef uint16_t kem_ciphertext_key_size;
  26. #define IN /* Indicates a necessary function input */
  27. #define OUT /* Indicates a function output */
  28. struct s2n_kem {
  29. const char *name;
  30. int kem_nid;
  31. const kem_extension_size kem_extension_id;
  32. const kem_public_key_size public_key_length;
  33. const kem_private_key_size private_key_length;
  34. const kem_shared_secret_size shared_secret_key_length;
  35. const kem_ciphertext_key_size ciphertext_length;
  36. /* NIST Post Quantum KEM submissions require the following API for compatibility */
  37. int (*generate_keypair)(IN const struct s2n_kem *kem, OUT uint8_t *public_key, OUT uint8_t *private_key);
  38. int (*encapsulate)(IN const struct s2n_kem *kem, OUT uint8_t *ciphertext, OUT uint8_t *shared_secret, IN const uint8_t *public_key);
  39. int (*decapsulate)(IN const struct s2n_kem *kem, OUT uint8_t *shared_secret, IN const uint8_t *ciphertext, IN const uint8_t *private_key);
  40. };
  41. struct s2n_kem_params {
  42. const struct s2n_kem *kem;
  43. struct s2n_blob public_key;
  44. struct s2n_blob private_key;
  45. struct s2n_blob shared_secret;
  46. /* Store whether the client included the length prefix of the PQ and ECC Shares in their ClientHello, so that the
  47. * server can match the client's behavior. For the client side, store whether it should send the length prefix. */
  48. bool len_prefixed;
  49. };
  50. struct s2n_iana_to_kem {
  51. const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN];
  52. const struct s2n_kem **kems;
  53. uint8_t kem_count;
  54. };
  55. struct s2n_kem_group {
  56. const char *name;
  57. uint16_t iana_id;
  58. const struct s2n_ecc_named_curve *curve;
  59. const struct s2n_kem *kem;
  60. };
  61. struct s2n_kem_group_params {
  62. const struct s2n_kem_group *kem_group;
  63. struct s2n_kem_params kem_params;
  64. struct s2n_ecc_evp_params ecc_params;
  65. };
  66. extern const struct s2n_kem s2n_kyber_512_r3;
  67. extern const struct s2n_kem s2n_kyber_768_r3;
  68. extern const struct s2n_kem s2n_kyber_1024_r3;
  69. /* x25519 based tls13_kem_groups require EVP_APIS_SUPPORTED */
  70. /* Kyber758+ requires S2N_LIBCRYPTO_SUPPORTS_KYBER */
  71. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && EVP_APIS_SUPPORTED
  72. #define S2N_SUPPORTED_KEM_GROUPS_COUNT 6
  73. #elif defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && !EVP_APIS_SUPPORTED
  74. #define S2N_SUPPORTED_KEM_GROUPS_COUNT 4
  75. #elif !defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && EVP_APIS_SUPPORTED
  76. #define S2N_SUPPORTED_KEM_GROUPS_COUNT 2
  77. #else
  78. #define S2N_SUPPORTED_KEM_GROUPS_COUNT 1
  79. #endif
  80. extern const struct s2n_kem_group *ALL_SUPPORTED_KEM_GROUPS[S2N_SUPPORTED_KEM_GROUPS_COUNT];
  81. /* NIST curve KEM Groups */
  82. extern const struct s2n_kem_group s2n_secp256r1_kyber_512_r3;
  83. extern const struct s2n_kem_group s2n_secp256r1_kyber_768_r3;
  84. extern const struct s2n_kem_group s2n_secp384r1_kyber_768_r3;
  85. extern const struct s2n_kem_group s2n_secp521r1_kyber_1024_r3;
  86. /* x25519 KEM Groups */
  87. extern const struct s2n_kem_group s2n_x25519_kyber_512_r3;
  88. extern const struct s2n_kem_group s2n_x25519_kyber_768_r3;
  89. S2N_RESULT s2n_kem_generate_keypair(struct s2n_kem_params *kem_params);
  90. S2N_RESULT s2n_kem_encapsulate(struct s2n_kem_params *kem_params, struct s2n_blob *ciphertext);
  91. S2N_RESULT s2n_kem_decapsulate(struct s2n_kem_params *kem_params, const struct s2n_blob *ciphertext);
  92. int s2n_choose_kem_with_peer_pref_list(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN],
  93. struct s2n_blob *client_kem_ids, const struct s2n_kem *server_kem_pref_list[],
  94. const uint8_t num_server_supported_kems, const struct s2n_kem **chosen_kem);
  95. int s2n_choose_kem_without_peer_pref_list(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN],
  96. const struct s2n_kem *server_kem_pref_list[], const uint8_t num_server_supported_kems,
  97. const struct s2n_kem **chosen_kem);
  98. int s2n_kem_free(struct s2n_kem_params *kem_params);
  99. int s2n_kem_group_free(struct s2n_kem_group_params *kem_group_params);
  100. int s2n_cipher_suite_to_kem(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN],
  101. const struct s2n_iana_to_kem **supported_params);
  102. int s2n_get_kem_from_extension_id(kem_extension_size kem_id, const struct s2n_kem **kem);
  103. int s2n_kem_send_public_key(struct s2n_stuffer *out, struct s2n_kem_params *kem_params);
  104. int s2n_kem_recv_public_key(struct s2n_stuffer *in, struct s2n_kem_params *kem_params);
  105. int s2n_kem_send_ciphertext(struct s2n_stuffer *out, struct s2n_kem_params *kem_params);
  106. int s2n_kem_recv_ciphertext(struct s2n_stuffer *in, struct s2n_kem_params *kem_params);
  107. /* The following are API signatures for PQ KEMs as defined by NIST. All functions return 0
  108. * on success, and !0 on failure. Avoid calling these functions directly within s2n. Instead,
  109. * use s2n_kem_{generate_keypair, encapsulate, decapsulate}, or
  110. * s2n_kem_{send_public_key, recv_public_key, send_ciphertext, recv_ciphertext}.
  111. *
  112. * int *_keypair(OUT pk, OUT sk) - Generate public/private keypair
  113. * pk - generated public key
  114. * sk - generated secret key
  115. *
  116. * int *_enc(OUT ct, OUT ss, IN pk) - Generate a shared secret and encapsulate it
  117. * ct - key encapsulation message (ciphertext)
  118. * ss - plaintext shared secret
  119. * pk - public key to use for encapsulation
  120. *
  121. * int *_dec(OUT ss, IN ct, IN sk) - Decapsulate a key encapsulation message and recover the shared secret
  122. * ss - plaintext shared secret
  123. * ct - key encapsulation message (ciphertext)
  124. * sk - secret key to use for decapsulation */
  125. /* If s2n is compiled with support for PQ crypto, these functions will be defined in the respective KEM directories.
  126. * If s2n is compiled without support for PQ, stubs of these functions are defined in s2n_kem.c. */
  127. /* sikep503r1 */
  128. /* kyber512r3 */
  129. #define S2N_KYBER_512_R3_PUBLIC_KEY_BYTES 800
  130. #define S2N_KYBER_512_R3_SECRET_KEY_BYTES 1632
  131. #define S2N_KYBER_512_R3_CIPHERTEXT_BYTES 768
  132. #define S2N_KYBER_512_R3_SHARED_SECRET_BYTES 32
  133. int s2n_kyber_512_r3_crypto_kem_keypair(IN const struct s2n_kem *kem, OUT uint8_t *pk, OUT uint8_t *sk);
  134. int s2n_kyber_512_r3_crypto_kem_enc(IN const struct s2n_kem *kem, OUT uint8_t *ct, OUT uint8_t *ss, IN const uint8_t *pk);
  135. int s2n_kyber_512_r3_crypto_kem_dec(IN const struct s2n_kem *kem, OUT uint8_t *ss, IN const uint8_t *ct, IN const uint8_t *sk);
  136. /* kyber768r3 */
  137. #define S2N_KYBER_768_R3_PUBLIC_KEY_BYTES 1184
  138. #define S2N_KYBER_768_R3_SECRET_KEY_BYTES 2400
  139. #define S2N_KYBER_768_R3_CIPHERTEXT_BYTES 1088
  140. #define S2N_KYBER_768_R3_SHARED_SECRET_BYTES 32
  141. /* kyber1024r3 */
  142. #define S2N_KYBER_1024_R3_PUBLIC_KEY_BYTES 1568
  143. #define S2N_KYBER_1024_R3_SECRET_KEY_BYTES 3168
  144. #define S2N_KYBER_1024_R3_CIPHERTEXT_BYTES 1568
  145. #define S2N_KYBER_1024_R3_SHARED_SECRET_BYTES 32