ssl_tls13_keys.h 11 KB

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