s2n_hkdf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "crypto/s2n_hkdf.h"
  16. #include "crypto/s2n_fips.h"
  17. #include "crypto/s2n_hmac.h"
  18. #include "error/s2n_errno.h"
  19. #include "stuffer/s2n_stuffer.h"
  20. #include "utils/s2n_blob.h"
  21. #include "utils/s2n_mem.h"
  22. #include "utils/s2n_safety.h"
  23. #ifdef S2N_LIBCRYPTO_SUPPORTS_HKDF
  24. #error #include <openssl/hkdf.h>
  25. #endif
  26. #define MAX_DIGEST_SIZE 64 /* Current highest is SHA512 */
  27. #define MAX_HKDF_ROUNDS 255
  28. /* Reference: RFC 5869 */
  29. struct s2n_hkdf_impl {
  30. int (*hkdf)(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  31. const struct s2n_blob *key, const struct s2n_blob *info, struct s2n_blob *output);
  32. int (*hkdf_extract)(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  33. const struct s2n_blob *key, struct s2n_blob *pseudo_rand_key);
  34. int (*hkdf_expand)(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *pseudo_rand_key,
  35. const struct s2n_blob *info, struct s2n_blob *output);
  36. };
  37. static int s2n_custom_hkdf_extract(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  38. const struct s2n_blob *key, struct s2n_blob *pseudo_rand_key)
  39. {
  40. uint8_t hmac_size = 0;
  41. POSIX_GUARD(s2n_hmac_digest_size(alg, &hmac_size));
  42. POSIX_ENSURE(hmac_size <= pseudo_rand_key->size, S2N_ERR_HKDF_OUTPUT_SIZE);
  43. pseudo_rand_key->size = hmac_size;
  44. POSIX_GUARD(s2n_hmac_init(hmac, alg, salt->data, salt->size));
  45. POSIX_GUARD(s2n_hmac_update(hmac, key->data, key->size));
  46. POSIX_GUARD(s2n_hmac_digest(hmac, pseudo_rand_key->data, pseudo_rand_key->size));
  47. POSIX_GUARD(s2n_hmac_reset(hmac));
  48. return S2N_SUCCESS;
  49. }
  50. static int s2n_custom_hkdf_expand(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg,
  51. const struct s2n_blob *pseudo_rand_key, const struct s2n_blob *info, struct s2n_blob *output)
  52. {
  53. uint8_t prev[MAX_DIGEST_SIZE] = { 0 };
  54. uint32_t done_len = 0;
  55. uint8_t hash_len = 0;
  56. POSIX_GUARD(s2n_hmac_digest_size(alg, &hash_len));
  57. POSIX_ENSURE_GT(hash_len, 0);
  58. uint32_t total_rounds = output->size / hash_len;
  59. if (output->size % hash_len) {
  60. total_rounds++;
  61. }
  62. POSIX_ENSURE(total_rounds > 0, S2N_ERR_HKDF_OUTPUT_SIZE);
  63. POSIX_ENSURE(total_rounds <= MAX_HKDF_ROUNDS, S2N_ERR_HKDF_OUTPUT_SIZE);
  64. for (uint32_t curr_round = 1; curr_round <= total_rounds; curr_round++) {
  65. uint32_t cat_len;
  66. POSIX_GUARD(s2n_hmac_init(hmac, alg, pseudo_rand_key->data, pseudo_rand_key->size));
  67. if (curr_round != 1) {
  68. POSIX_GUARD(s2n_hmac_update(hmac, prev, hash_len));
  69. }
  70. POSIX_GUARD(s2n_hmac_update(hmac, info->data, info->size));
  71. POSIX_GUARD(s2n_hmac_update(hmac, &curr_round, 1));
  72. POSIX_GUARD(s2n_hmac_digest(hmac, prev, hash_len));
  73. cat_len = hash_len;
  74. if (done_len + hash_len > output->size) {
  75. cat_len = output->size - done_len;
  76. }
  77. POSIX_CHECKED_MEMCPY(output->data + done_len, prev, cat_len);
  78. done_len += cat_len;
  79. POSIX_GUARD(s2n_hmac_reset(hmac));
  80. }
  81. return S2N_SUCCESS;
  82. }
  83. static int s2n_custom_hkdf(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  84. const struct s2n_blob *key, const struct s2n_blob *info, struct s2n_blob *output)
  85. {
  86. uint8_t prk_pad[MAX_DIGEST_SIZE] = { 0 };
  87. struct s2n_blob pseudo_rand_key = { 0 };
  88. POSIX_GUARD(s2n_blob_init(&pseudo_rand_key, prk_pad, sizeof(prk_pad)));
  89. POSIX_GUARD(s2n_custom_hkdf_extract(hmac, alg, salt, key, &pseudo_rand_key));
  90. POSIX_GUARD(s2n_custom_hkdf_expand(hmac, alg, &pseudo_rand_key, info, output));
  91. return S2N_SUCCESS;
  92. }
  93. const struct s2n_hkdf_impl s2n_custom_hkdf_impl = {
  94. .hkdf = &s2n_custom_hkdf,
  95. .hkdf_extract = &s2n_custom_hkdf_extract,
  96. .hkdf_expand = &s2n_custom_hkdf_expand,
  97. };
  98. #ifdef S2N_LIBCRYPTO_SUPPORTS_HKDF
  99. static int s2n_libcrypto_hkdf_extract(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  100. const struct s2n_blob *key, struct s2n_blob *pseudo_rand_key)
  101. {
  102. const EVP_MD *digest = NULL;
  103. POSIX_GUARD_RESULT(s2n_hmac_md_from_alg(alg, &digest));
  104. /* The out_len argument of HKDF_extract is set to the number of bytes written to out_key, and
  105. * is not used to ensure that out_key is large enough to contain the PRK. Ensure that the PRK
  106. * output will fit in the blob.
  107. */
  108. uint8_t hmac_size = 0;
  109. POSIX_GUARD(s2n_hmac_digest_size(alg, &hmac_size));
  110. POSIX_ENSURE(hmac_size <= pseudo_rand_key->size, S2N_ERR_HKDF_OUTPUT_SIZE);
  111. size_t bytes_written = 0;
  112. POSIX_GUARD_OSSL(HKDF_extract(pseudo_rand_key->data, &bytes_written, digest, key->data, key->size,
  113. salt->data, salt->size),
  114. S2N_ERR_HKDF);
  115. /* HKDF_extract updates the out_len argument based on the digest size. Update the blob's size based on this. */
  116. POSIX_ENSURE_LTE(bytes_written, pseudo_rand_key->size);
  117. pseudo_rand_key->size = bytes_written;
  118. return S2N_SUCCESS;
  119. }
  120. static int s2n_libcrypto_hkdf_expand(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg,
  121. const struct s2n_blob *pseudo_rand_key, const struct s2n_blob *info, struct s2n_blob *output)
  122. {
  123. POSIX_ENSURE(output->size > 0, S2N_ERR_HKDF_OUTPUT_SIZE);
  124. const EVP_MD *digest = NULL;
  125. POSIX_GUARD_RESULT(s2n_hmac_md_from_alg(alg, &digest));
  126. POSIX_GUARD_OSSL(HKDF_expand(output->data, output->size, digest, pseudo_rand_key->data, pseudo_rand_key->size,
  127. info->data, info->size),
  128. S2N_ERR_HKDF);
  129. return S2N_SUCCESS;
  130. }
  131. static int s2n_libcrypto_hkdf(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  132. const struct s2n_blob *key, const struct s2n_blob *info, struct s2n_blob *output)
  133. {
  134. POSIX_ENSURE(output->size > 0, S2N_ERR_HKDF_OUTPUT_SIZE);
  135. const EVP_MD *digest = NULL;
  136. POSIX_GUARD_RESULT(s2n_hmac_md_from_alg(alg, &digest));
  137. POSIX_GUARD_OSSL(HKDF(output->data, output->size, digest, key->data, key->size, salt->data, salt->size,
  138. info->data, info->size),
  139. S2N_ERR_HKDF);
  140. return S2N_SUCCESS;
  141. }
  142. #else
  143. static int s2n_libcrypto_hkdf_extract(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  144. const struct s2n_blob *key, struct s2n_blob *pseudo_rand_key)
  145. {
  146. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  147. }
  148. static int s2n_libcrypto_hkdf_expand(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg,
  149. const struct s2n_blob *pseudo_rand_key, const struct s2n_blob *info, struct s2n_blob *output)
  150. {
  151. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  152. }
  153. static int s2n_libcrypto_hkdf(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  154. const struct s2n_blob *key, const struct s2n_blob *info, struct s2n_blob *output)
  155. {
  156. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  157. }
  158. #endif /* S2N_LIBCRYPTO_SUPPORTS_HKDF */
  159. const struct s2n_hkdf_impl s2n_libcrypto_hkdf_impl = {
  160. .hkdf = &s2n_libcrypto_hkdf,
  161. .hkdf_extract = &s2n_libcrypto_hkdf_extract,
  162. .hkdf_expand = &s2n_libcrypto_hkdf_expand,
  163. };
  164. static const struct s2n_hkdf_impl *s2n_get_hkdf_implementation()
  165. {
  166. /* By default, s2n-tls uses a custom HKDF implementation. When operating in FIPS mode, the
  167. * FIPS-validated libcrypto implementation is used instead, if an implementation is provided.
  168. */
  169. if (s2n_is_in_fips_mode() && s2n_libcrypto_supports_hkdf()) {
  170. return &s2n_libcrypto_hkdf_impl;
  171. }
  172. return &s2n_custom_hkdf_impl;
  173. }
  174. int s2n_hkdf_extract(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  175. const struct s2n_blob *key, struct s2n_blob *pseudo_rand_key)
  176. {
  177. POSIX_ENSURE_REF(hmac);
  178. POSIX_ENSURE_REF(salt);
  179. POSIX_ENSURE_REF(key);
  180. POSIX_ENSURE_REF(pseudo_rand_key);
  181. const struct s2n_hkdf_impl *hkdf_implementation = s2n_get_hkdf_implementation();
  182. POSIX_ENSURE_REF(hkdf_implementation);
  183. POSIX_GUARD(hkdf_implementation->hkdf_extract(hmac, alg, salt, key, pseudo_rand_key));
  184. return S2N_SUCCESS;
  185. }
  186. static int s2n_hkdf_expand(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *pseudo_rand_key,
  187. const struct s2n_blob *info, struct s2n_blob *output)
  188. {
  189. POSIX_ENSURE_REF(hmac);
  190. POSIX_ENSURE_REF(pseudo_rand_key);
  191. POSIX_ENSURE_REF(info);
  192. POSIX_ENSURE_REF(output);
  193. const struct s2n_hkdf_impl *hkdf_implementation = s2n_get_hkdf_implementation();
  194. POSIX_ENSURE_REF(hkdf_implementation);
  195. POSIX_GUARD(hkdf_implementation->hkdf_expand(hmac, alg, pseudo_rand_key, info, output));
  196. return S2N_SUCCESS;
  197. }
  198. int s2n_hkdf_expand_label(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *secret, const struct s2n_blob *label,
  199. const struct s2n_blob *context, struct s2n_blob *output)
  200. {
  201. POSIX_ENSURE_REF(label);
  202. POSIX_ENSURE_REF(context);
  203. POSIX_ENSURE_REF(output);
  204. /* Per RFC8446: 7.1, a HKDF label is a 2 byte length field, and two 1...255 byte arrays with a one byte length field each. */
  205. uint8_t hkdf_label_buf[2 + 256 + 256];
  206. struct s2n_blob hkdf_label_blob = { 0 };
  207. struct s2n_stuffer hkdf_label = { 0 };
  208. POSIX_ENSURE_LTE(label->size, S2N_MAX_HKDF_EXPAND_LABEL_LENGTH);
  209. POSIX_GUARD(s2n_blob_init(&hkdf_label_blob, hkdf_label_buf, sizeof(hkdf_label_buf)));
  210. POSIX_GUARD(s2n_stuffer_init(&hkdf_label, &hkdf_label_blob));
  211. POSIX_GUARD(s2n_stuffer_write_uint16(&hkdf_label, output->size));
  212. POSIX_GUARD(s2n_stuffer_write_uint8(&hkdf_label, label->size + sizeof("tls13 ") - 1));
  213. POSIX_GUARD(s2n_stuffer_write_str(&hkdf_label, "tls13 "));
  214. POSIX_GUARD(s2n_stuffer_write(&hkdf_label, label));
  215. POSIX_GUARD(s2n_stuffer_write_uint8(&hkdf_label, context->size));
  216. POSIX_GUARD(s2n_stuffer_write(&hkdf_label, context));
  217. hkdf_label_blob.size = s2n_stuffer_data_available(&hkdf_label);
  218. POSIX_GUARD(s2n_hkdf_expand(hmac, alg, secret, &hkdf_label_blob, output));
  219. return S2N_SUCCESS;
  220. }
  221. int s2n_hkdf(struct s2n_hmac_state *hmac, s2n_hmac_algorithm alg, const struct s2n_blob *salt,
  222. const struct s2n_blob *key, const struct s2n_blob *info, struct s2n_blob *output)
  223. {
  224. POSIX_ENSURE_REF(hmac);
  225. POSIX_ENSURE_REF(salt);
  226. POSIX_ENSURE_REF(key);
  227. POSIX_ENSURE_REF(info);
  228. POSIX_ENSURE_REF(output);
  229. const struct s2n_hkdf_impl *hkdf_implementation = s2n_get_hkdf_implementation();
  230. POSIX_ENSURE_REF(hkdf_implementation);
  231. POSIX_GUARD(hkdf_implementation->hkdf(hmac, alg, salt, key, info, output));
  232. return S2N_SUCCESS;
  233. }
  234. bool s2n_libcrypto_supports_hkdf()
  235. {
  236. #ifdef S2N_LIBCRYPTO_SUPPORTS_HKDF
  237. return true;
  238. #else
  239. return false;
  240. #endif
  241. }