ssl_tls13_keys.h 11 KB

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