ssl_tls13_keys.c 12 KB

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