ssl_tls13_keys.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #if !defined(MBEDTLS_SSL_TLS1_3_KEYS_H)
  20. #define MBEDTLS_SSL_TLS1_3_KEYS_H
  21. /* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
  22. * the point of use. See e.g. the definition of mbedtls_ssl_tls1_3_labels_union
  23. * below. */
  24. #define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
  25. MBEDTLS_SSL_TLS1_3_LABEL( finished , "finished" ) \
  26. MBEDTLS_SSL_TLS1_3_LABEL( resumption , "resumption" ) \
  27. MBEDTLS_SSL_TLS1_3_LABEL( traffic_upd , "traffic upd" ) \
  28. MBEDTLS_SSL_TLS1_3_LABEL( exporter , "exporter" ) \
  29. MBEDTLS_SSL_TLS1_3_LABEL( key , "key" ) \
  30. MBEDTLS_SSL_TLS1_3_LABEL( iv , "iv" ) \
  31. MBEDTLS_SSL_TLS1_3_LABEL( c_hs_traffic, "c hs traffic" ) \
  32. MBEDTLS_SSL_TLS1_3_LABEL( c_ap_traffic, "c ap traffic" ) \
  33. MBEDTLS_SSL_TLS1_3_LABEL( c_e_traffic , "c e traffic" ) \
  34. MBEDTLS_SSL_TLS1_3_LABEL( s_hs_traffic, "s hs traffic" ) \
  35. MBEDTLS_SSL_TLS1_3_LABEL( s_ap_traffic, "s ap traffic" ) \
  36. MBEDTLS_SSL_TLS1_3_LABEL( s_e_traffic , "s e traffic" ) \
  37. MBEDTLS_SSL_TLS1_3_LABEL( e_exp_master, "e exp master" ) \
  38. MBEDTLS_SSL_TLS1_3_LABEL( res_master , "res master" ) \
  39. MBEDTLS_SSL_TLS1_3_LABEL( exp_master , "exp master" ) \
  40. MBEDTLS_SSL_TLS1_3_LABEL( ext_binder , "ext binder" ) \
  41. MBEDTLS_SSL_TLS1_3_LABEL( res_binder , "res binder" ) \
  42. MBEDTLS_SSL_TLS1_3_LABEL( derived , "derived" )
  43. #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
  44. const unsigned char name [ sizeof(string) - 1 ];
  45. union mbedtls_ssl_tls1_3_labels_union
  46. {
  47. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  48. };
  49. struct mbedtls_ssl_tls1_3_labels_struct
  50. {
  51. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  52. };
  53. #undef MBEDTLS_SSL_TLS1_3_LABEL
  54. extern const struct mbedtls_ssl_tls1_3_labels_struct mbedtls_ssl_tls1_3_labels;
  55. #define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
  56. mbedtls_ssl_tls1_3_labels.LABEL, \
  57. sizeof(mbedtls_ssl_tls1_3_labels.LABEL)
  58. #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
  59. sizeof( union mbedtls_ssl_tls1_3_labels_union )
  60. /* The maximum length of HKDF contexts used in the TLS 1.3 standard.
  61. * Since contexts are always hashes of message transcripts, this can
  62. * be approximated from above by the maximum hash size. */
  63. #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
  64. MBEDTLS_MD_MAX_SIZE
  65. /* Maximum desired length for expanded key material generated
  66. * by HKDF-Expand-Label.
  67. *
  68. * Warning: If this ever needs to be increased, the implementation
  69. * ssl_tls1_3_hkdf_encode_label() in ssl_tls13_keys.c needs to be
  70. * adjusted since it currently assumes that HKDF key expansion
  71. * is never used with more than 255 Bytes of output. */
  72. #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
  73. /**
  74. * \brief The \c HKDF-Expand-Label function from
  75. * the TLS 1.3 standard RFC 8446.
  76. *
  77. * <tt>
  78. * HKDF-Expand-Label( Secret, Label, Context, Length ) =
  79. * HKDF-Expand( Secret, HkdfLabel, Length )
  80. * </tt>
  81. *
  82. * \param hash_alg The identifier for the hash algorithm to use.
  83. * \param secret The \c Secret argument to \c HKDF-Expand-Label.
  84. * This must be a readable buffer of length \p slen Bytes.
  85. * \param slen The length of \p secret in Bytes.
  86. * \param label The \c Label argument to \c HKDF-Expand-Label.
  87. * This must be a readable buffer of length \p llen Bytes.
  88. * \param llen The length of \p label in Bytes.
  89. * \param ctx The \c Context argument to \c HKDF-Expand-Label.
  90. * This must be a readable buffer of length \p clen Bytes.
  91. * \param clen The length of \p context in Bytes.
  92. * \param buf The destination buffer to hold the expanded secret.
  93. * This must be a writable buffer of length \p blen Bytes.
  94. * \param blen The desired size of the expanded secret in Bytes.
  95. *
  96. * \returns \c 0 on success.
  97. * \return A negative error code on failure.
  98. */
  99. int mbedtls_ssl_tls1_3_hkdf_expand_label(
  100. mbedtls_md_type_t hash_alg,
  101. const unsigned char *secret, size_t slen,
  102. const unsigned char *label, size_t llen,
  103. const unsigned char *ctx, size_t clen,
  104. unsigned char *buf, size_t blen );
  105. /**
  106. * \brief This function is part of the TLS 1.3 key schedule.
  107. * It extracts key and IV for the actual client/server traffic
  108. * from the client/server traffic secrets.
  109. *
  110. * From RFC 8446:
  111. *
  112. * <tt>
  113. * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
  114. * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
  115. * </tt>
  116. *
  117. * \param hash_alg The identifier for the hash algorithm to be used
  118. * for the HKDF-based expansion of the secret.
  119. * \param client_secret The client traffic secret.
  120. * This must be a readable buffer of size \p slen Bytes
  121. * \param server_secret The server traffic secret.
  122. * This must be a readable buffer of size \p slen Bytes
  123. * \param slen Length of the secrets \p client_secret and
  124. * \p server_secret in Bytes.
  125. * \param key_len The desired length of the key to be extracted in Bytes.
  126. * \param iv_len The desired length of the IV to be extracted in Bytes.
  127. * \param keys The address of the structure holding the generated
  128. * keys and IVs.
  129. *
  130. * \returns \c 0 on success.
  131. * \returns A negative error code on failure.
  132. */
  133. int mbedtls_ssl_tls1_3_make_traffic_keys(
  134. mbedtls_md_type_t hash_alg,
  135. const unsigned char *client_secret,
  136. const unsigned char *server_secret,
  137. size_t slen, size_t key_len, size_t iv_len,
  138. mbedtls_ssl_key_set *keys );
  139. #define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
  140. #define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
  141. /**
  142. * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
  143. *
  144. * <tt>
  145. * Derive-Secret( Secret, Label, Messages ) =
  146. * HKDF-Expand-Label( Secret, Label,
  147. * Hash( Messages ),
  148. * Hash.Length ) )
  149. * </tt>
  150. *
  151. * \param hash_alg The identifier for the hash function used for the
  152. * applications of HKDF.
  153. * \param secret The \c Secret argument to the \c Derive-Secret function.
  154. * This must be a readable buffer of length \p slen Bytes.
  155. * \param slen The length of \p secret in Bytes.
  156. * \param label The \c Label argument to the \c Derive-Secret function.
  157. * This must be a readable buffer of length \p llen Bytes.
  158. * \param llen The length of \p label in Bytes.
  159. * \param ctx The hash of the \c Messages argument to the
  160. * \c Derive-Secret function, or the \c Messages argument
  161. * itself, depending on \p context_already_hashed.
  162. * \param clen The length of \p hash.
  163. * \param ctx_hashed This indicates whether the \p ctx contains the hash of
  164. * the \c Messages argument in the application of the
  165. * \c Derive-Secret function
  166. * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
  167. * it is the content of \c Messages itself, in which case
  168. * the function takes care of the hashing
  169. * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
  170. * \param dstbuf The target buffer to write the output of
  171. * \c Derive-Secret to. This must be a writable buffer of
  172. * size \p buflen Bytes.
  173. * \param buflen The length of \p dstbuf in Bytes.
  174. *
  175. * \returns \c 0 on success.
  176. * \returns A negative error code on failure.
  177. */
  178. int mbedtls_ssl_tls1_3_derive_secret(
  179. mbedtls_md_type_t hash_alg,
  180. const unsigned char *secret, size_t slen,
  181. const unsigned char *label, size_t llen,
  182. const unsigned char *ctx, size_t clen,
  183. int ctx_hashed,
  184. unsigned char *dstbuf, size_t buflen );
  185. /**
  186. * \brief Compute the next secret in the TLS 1.3 key schedule
  187. *
  188. * The TLS 1.3 key schedule proceeds as follows to compute
  189. * the three main secrets during the handshake: The early
  190. * secret for early data, the handshake secret for all
  191. * other encrypted handshake messages, and the master
  192. * secret for all application traffic.
  193. *
  194. * <tt>
  195. * 0
  196. * |
  197. * v
  198. * PSK -> HKDF-Extract = Early Secret
  199. * |
  200. * v
  201. * Derive-Secret( ., "derived", "" )
  202. * |
  203. * v
  204. * (EC)DHE -> HKDF-Extract = Handshake Secret
  205. * |
  206. * v
  207. * Derive-Secret( ., "derived", "" )
  208. * |
  209. * v
  210. * 0 -> HKDF-Extract = Master Secret
  211. * </tt>
  212. *
  213. * Each of the three secrets in turn is the basis for further
  214. * key derivations, such as the derivation of traffic keys and IVs;
  215. * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys().
  216. *
  217. * This function implements one step in this evolution of secrets:
  218. *
  219. * <tt>
  220. * old_secret
  221. * |
  222. * v
  223. * Derive-Secret( ., "derived", "" )
  224. * |
  225. * v
  226. * input -> HKDF-Extract = new_secret
  227. * </tt>
  228. *
  229. * \param hash_alg The identifier for the hash function used for the
  230. * applications of HKDF.
  231. * \param secret_old The address of the buffer holding the old secret
  232. * on function entry. If not \c NULL, this must be a
  233. * readable buffer whose size matches the output size
  234. * of the hash function represented by \p hash_alg.
  235. * If \c NULL, an all \c 0 array will be used instead.
  236. * \param input The address of the buffer holding the additional
  237. * input for the key derivation (e.g., the PSK or the
  238. * ephemeral (EC)DH secret). If not \c NULL, this must be
  239. * a readable buffer whose size \p input_len Bytes.
  240. * If \c NULL, an all \c 0 array will be used instead.
  241. * \param input_len The length of \p input in Bytes.
  242. * \param secret_new The address of the buffer holding the new secret
  243. * on function exit. This must be a writable buffer
  244. * whose size matches the output size of the hash
  245. * function represented by \p hash_alg.
  246. * This may be the same as \p secret_old.
  247. *
  248. * \returns \c 0 on success.
  249. * \returns A negative error code on failure.
  250. */
  251. int mbedtls_ssl_tls1_3_evolve_secret(
  252. mbedtls_md_type_t hash_alg,
  253. const unsigned char *secret_old,
  254. const unsigned char *input, size_t input_len,
  255. unsigned char *secret_new );
  256. #endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */