ssl_tls13_keys.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * TLS 1.3 key schedule
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  9. #include "mbedtls/hkdf.h"
  10. #include "mbedtls/ssl_internal.h"
  11. #include "ssl_tls13_keys.h"
  12. #include "psa/crypto_sizes.h"
  13. #include <stdint.h>
  14. #include <string.h>
  15. #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
  16. .name = string,
  17. #define TLS1_3_EVOLVE_INPUT_SIZE (PSA_HASH_MAX_SIZE > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE) ? \
  18. PSA_HASH_MAX_SIZE : PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE
  19. struct mbedtls_ssl_tls1_3_labels_struct const mbedtls_ssl_tls1_3_labels =
  20. {
  21. /* This seems to work in C, despite the string literal being one
  22. * character too long due to the 0-termination. */
  23. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  24. };
  25. #undef MBEDTLS_SSL_TLS1_3_LABEL
  26. /*
  27. * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
  28. *
  29. * The HkdfLabel is specified in RFC 8446 as follows:
  30. *
  31. * struct HkdfLabel {
  32. * uint16 length; // Length of expanded key material
  33. * opaque label<7..255>; // Always prefixed by "tls13 "
  34. * opaque context<0..255>; // Usually a communication transcript hash
  35. * };
  36. *
  37. * Parameters:
  38. * - desired_length: Length of expanded key material
  39. * Even though the standard allows expansion to up to
  40. * 2**16 Bytes, TLS 1.3 never uses expansion to more than
  41. * 255 Bytes, so we require `desired_length` to be at most
  42. * 255. This allows us to save a few Bytes of code by
  43. * hardcoding the writing of the high bytes.
  44. * - (label, llen): label + label length, without "tls13 " prefix
  45. * The label length MUST be less than or equal to
  46. * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
  47. * It is the caller's responsibility to ensure this.
  48. * All (label, label length) pairs used in TLS 1.3
  49. * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
  50. * - (ctx, clen): context + context length
  51. * The context length MUST be less than or equal to
  52. * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
  53. * It is the caller's responsibility to ensure this.
  54. * - dst: Target buffer for HkdfLabel structure,
  55. * This MUST be a writable buffer of size
  56. * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
  57. * - dlen: Pointer at which to store the actual length of
  58. * the HkdfLabel structure on success.
  59. */
  60. static const char tls1_3_label_prefix[6] = "tls13 ";
  61. #define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
  62. (2 /* expansion length */ \
  63. + 1 /* label length */ \
  64. + label_len \
  65. + 1 /* context length */ \
  66. + context_len)
  67. #define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
  68. SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
  69. sizeof(tls1_3_label_prefix) + \
  70. MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
  71. MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN)
  72. static void ssl_tls1_3_hkdf_encode_label(
  73. size_t desired_length,
  74. const unsigned char *label, size_t llen,
  75. const unsigned char *ctx, size_t clen,
  76. unsigned char *dst, size_t *dlen)
  77. {
  78. size_t total_label_len =
  79. sizeof(tls1_3_label_prefix) + llen;
  80. size_t total_hkdf_lbl_len =
  81. SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(total_label_len, clen);
  82. unsigned char *p = dst;
  83. /* Add the size of the expanded key material.
  84. * We're hardcoding the high byte to 0 here assuming that we never use
  85. * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
  86. #if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
  87. #error "The implementation of ssl_tls1_3_hkdf_encode_label() is not fit for the \
  88. value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
  89. #endif
  90. *p++ = 0;
  91. *p++ = MBEDTLS_BYTE_0(desired_length);
  92. /* Add label incl. prefix */
  93. *p++ = MBEDTLS_BYTE_0(total_label_len);
  94. memcpy(p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix));
  95. p += sizeof(tls1_3_label_prefix);
  96. memcpy(p, label, llen);
  97. p += llen;
  98. /* Add context value */
  99. *p++ = MBEDTLS_BYTE_0(clen);
  100. if (clen != 0) {
  101. memcpy(p, ctx, clen);
  102. }
  103. /* Return total length to the caller. */
  104. *dlen = total_hkdf_lbl_len;
  105. }
  106. int mbedtls_ssl_tls1_3_hkdf_expand_label(
  107. mbedtls_md_type_t hash_alg,
  108. const unsigned char *secret, size_t slen,
  109. const unsigned char *label, size_t llen,
  110. const unsigned char *ctx, size_t clen,
  111. unsigned char *buf, size_t blen)
  112. {
  113. const mbedtls_md_info_t *md;
  114. unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
  115. size_t hkdf_label_len;
  116. if (llen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
  117. /* Should never happen since this is an internal
  118. * function, and we know statically which labels
  119. * are allowed. */
  120. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  121. }
  122. if (clen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
  123. /* Should not happen, as above. */
  124. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  125. }
  126. if (blen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
  127. /* Should not happen, as above. */
  128. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  129. }
  130. md = mbedtls_md_info_from_type(hash_alg);
  131. if (md == NULL) {
  132. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  133. }
  134. ssl_tls1_3_hkdf_encode_label(blen,
  135. label, llen,
  136. ctx, clen,
  137. hkdf_label,
  138. &hkdf_label_len);
  139. return mbedtls_hkdf_expand(md,
  140. secret, slen,
  141. hkdf_label, hkdf_label_len,
  142. buf, blen);
  143. }
  144. /*
  145. * The traffic keying material is generated from the following inputs:
  146. *
  147. * - One secret value per sender.
  148. * - A purpose value indicating the specific value being generated
  149. * - The desired lengths of key and IV.
  150. *
  151. * The expansion itself is based on HKDF:
  152. *
  153. * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
  154. * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
  155. *
  156. * [sender] denotes the sending side and the Secret value is provided
  157. * by the function caller. Note that we generate server and client side
  158. * keys in a single function call.
  159. */
  160. int mbedtls_ssl_tls1_3_make_traffic_keys(
  161. mbedtls_md_type_t hash_alg,
  162. const unsigned char *client_secret,
  163. const unsigned char *server_secret,
  164. size_t slen, size_t key_len, size_t iv_len,
  165. mbedtls_ssl_key_set *keys)
  166. {
  167. int ret = 0;
  168. ret = mbedtls_ssl_tls1_3_hkdf_expand_label(hash_alg,
  169. client_secret, slen,
  170. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
  171. NULL, 0,
  172. keys->client_write_key, key_len);
  173. if (ret != 0) {
  174. return ret;
  175. }
  176. ret = mbedtls_ssl_tls1_3_hkdf_expand_label(hash_alg,
  177. server_secret, slen,
  178. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
  179. NULL, 0,
  180. keys->server_write_key, key_len);
  181. if (ret != 0) {
  182. return ret;
  183. }
  184. ret = mbedtls_ssl_tls1_3_hkdf_expand_label(hash_alg,
  185. client_secret, slen,
  186. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
  187. NULL, 0,
  188. keys->client_write_iv, iv_len);
  189. if (ret != 0) {
  190. return ret;
  191. }
  192. ret = mbedtls_ssl_tls1_3_hkdf_expand_label(hash_alg,
  193. server_secret, slen,
  194. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
  195. NULL, 0,
  196. keys->server_write_iv, iv_len);
  197. if (ret != 0) {
  198. return ret;
  199. }
  200. keys->key_len = key_len;
  201. keys->iv_len = iv_len;
  202. return 0;
  203. }
  204. int mbedtls_ssl_tls1_3_derive_secret(
  205. mbedtls_md_type_t hash_alg,
  206. const unsigned char *secret, size_t slen,
  207. const unsigned char *label, size_t llen,
  208. const unsigned char *ctx, size_t clen,
  209. int ctx_hashed,
  210. unsigned char *dstbuf, size_t buflen)
  211. {
  212. int ret;
  213. unsigned char hashed_context[MBEDTLS_MD_MAX_SIZE];
  214. const mbedtls_md_info_t *md;
  215. md = mbedtls_md_info_from_type(hash_alg);
  216. if (md == NULL) {
  217. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  218. }
  219. if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
  220. ret = mbedtls_md(md, ctx, clen, hashed_context);
  221. if (ret != 0) {
  222. return ret;
  223. }
  224. clen = mbedtls_md_get_size(md);
  225. } else {
  226. if (clen > sizeof(hashed_context)) {
  227. /* This should never happen since this function is internal
  228. * and the code sets `ctx_hashed` correctly.
  229. * Let's double-check nonetheless to not run at the risk
  230. * of getting a stack overflow. */
  231. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  232. }
  233. memcpy(hashed_context, ctx, clen);
  234. }
  235. return mbedtls_ssl_tls1_3_hkdf_expand_label(hash_alg,
  236. secret, slen,
  237. label, llen,
  238. hashed_context, clen,
  239. dstbuf, buflen);
  240. }
  241. int mbedtls_ssl_tls1_3_evolve_secret(
  242. mbedtls_md_type_t hash_alg,
  243. const unsigned char *secret_old,
  244. const unsigned char *input, size_t input_len,
  245. unsigned char *secret_new)
  246. {
  247. int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  248. size_t hlen, ilen;
  249. unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
  250. unsigned char tmp_input[TLS1_3_EVOLVE_INPUT_SIZE] = { 0 };
  251. const mbedtls_md_info_t *md;
  252. md = mbedtls_md_info_from_type(hash_alg);
  253. if (md == NULL) {
  254. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  255. }
  256. hlen = mbedtls_md_get_size(md);
  257. /* For non-initial runs, call Derive-Secret( ., "derived", "")
  258. * on the old secret. */
  259. if (secret_old != NULL) {
  260. ret = mbedtls_ssl_tls1_3_derive_secret(
  261. hash_alg,
  262. secret_old, hlen,
  263. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
  264. NULL, 0, /* context */
  265. MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
  266. tmp_secret, hlen);
  267. if (ret != 0) {
  268. goto cleanup;
  269. }
  270. }
  271. if (input != NULL) {
  272. memcpy(tmp_input, input, input_len);
  273. ilen = input_len;
  274. } else {
  275. ilen = hlen;
  276. }
  277. /* HKDF-Extract takes a salt and input key material.
  278. * The salt is the old secret, and the input key material
  279. * is the input secret (PSK / ECDHE). */
  280. ret = mbedtls_hkdf_extract(md,
  281. tmp_secret, hlen,
  282. tmp_input, ilen,
  283. secret_new);
  284. if (ret != 0) {
  285. goto cleanup;
  286. }
  287. ret = 0;
  288. cleanup:
  289. mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
  290. mbedtls_platform_zeroize(tmp_input, sizeof(tmp_input));
  291. return ret;
  292. }
  293. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */