ssl_tls13_keys.c 12 KB

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