ssl_tls13_keys.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. MBEDTLS_SSL_TLS1_3_LABEL( client_cv , "TLS 1.3, client CertificateVerify" ) \
  44. MBEDTLS_SSL_TLS1_3_LABEL( server_cv , "TLS 1.3, server CertificateVerify" )
  45. #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
  46. const unsigned char name [ sizeof(string) - 1 ];
  47. union mbedtls_ssl_tls1_3_labels_union
  48. {
  49. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  50. };
  51. struct mbedtls_ssl_tls1_3_labels_struct
  52. {
  53. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  54. };
  55. #undef MBEDTLS_SSL_TLS1_3_LABEL
  56. extern const struct mbedtls_ssl_tls1_3_labels_struct mbedtls_ssl_tls1_3_labels;
  57. #define MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL ) \
  58. sizeof(mbedtls_ssl_tls1_3_labels.LABEL)
  59. #define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
  60. mbedtls_ssl_tls1_3_labels.LABEL, \
  61. MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL )
  62. #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
  63. sizeof( union mbedtls_ssl_tls1_3_labels_union )
  64. /* The maximum length of HKDF contexts used in the TLS 1.3 standard.
  65. * Since contexts are always hashes of message transcripts, this can
  66. * be approximated from above by the maximum hash size. */
  67. #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
  68. MBEDTLS_MD_MAX_SIZE
  69. /* Maximum desired length for expanded key material generated
  70. * by HKDF-Expand-Label.
  71. *
  72. * Warning: If this ever needs to be increased, the implementation
  73. * ssl_tls1_3_hkdf_encode_label() in ssl_tls13_keys.c needs to be
  74. * adjusted since it currently assumes that HKDF key expansion
  75. * is never used with more than 255 Bytes of output. */
  76. #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
  77. /**
  78. * \brief The \c HKDF-Expand-Label function from
  79. * the TLS 1.3 standard RFC 8446.
  80. *
  81. * <tt>
  82. * HKDF-Expand-Label( Secret, Label, Context, Length ) =
  83. * HKDF-Expand( Secret, HkdfLabel, Length )
  84. * </tt>
  85. *
  86. * \param hash_alg The identifier for the hash algorithm to use.
  87. * \param secret The \c Secret argument to \c HKDF-Expand-Label.
  88. * This must be a readable buffer of length \p slen Bytes.
  89. * \param slen The length of \p secret in Bytes.
  90. * \param label The \c Label argument to \c HKDF-Expand-Label.
  91. * This must be a readable buffer of length \p llen Bytes.
  92. * \param llen The length of \p label in Bytes.
  93. * \param ctx The \c Context argument to \c HKDF-Expand-Label.
  94. * This must be a readable buffer of length \p clen Bytes.
  95. * \param clen The length of \p context in Bytes.
  96. * \param buf The destination buffer to hold the expanded secret.
  97. * This must be a writable buffer of length \p blen Bytes.
  98. * \param blen The desired size of the expanded secret in Bytes.
  99. *
  100. * \returns \c 0 on success.
  101. * \return A negative error code on failure.
  102. */
  103. int mbedtls_ssl_tls1_3_hkdf_expand_label(
  104. mbedtls_md_type_t hash_alg,
  105. const unsigned char *secret, size_t slen,
  106. const unsigned char *label, size_t llen,
  107. const unsigned char *ctx, size_t clen,
  108. unsigned char *buf, size_t blen );
  109. /**
  110. * \brief This function is part of the TLS 1.3 key schedule.
  111. * It extracts key and IV for the actual client/server traffic
  112. * from the client/server traffic secrets.
  113. *
  114. * From RFC 8446:
  115. *
  116. * <tt>
  117. * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
  118. * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
  119. * </tt>
  120. *
  121. * \param hash_alg The identifier for the hash algorithm to be used
  122. * for the HKDF-based expansion of the secret.
  123. * \param client_secret The client traffic secret.
  124. * This must be a readable buffer of size \p slen Bytes
  125. * \param server_secret The server traffic secret.
  126. * This must be a readable buffer of size \p slen Bytes
  127. * \param slen Length of the secrets \p client_secret and
  128. * \p server_secret in Bytes.
  129. * \param key_len The desired length of the key to be extracted in Bytes.
  130. * \param iv_len The desired length of the IV to be extracted in Bytes.
  131. * \param keys The address of the structure holding the generated
  132. * keys and IVs.
  133. *
  134. * \returns \c 0 on success.
  135. * \returns A negative error code on failure.
  136. */
  137. int mbedtls_ssl_tls1_3_make_traffic_keys(
  138. mbedtls_md_type_t hash_alg,
  139. const unsigned char *client_secret,
  140. const unsigned char *server_secret,
  141. size_t slen, size_t key_len, size_t iv_len,
  142. mbedtls_ssl_key_set *keys );
  143. #define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
  144. #define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
  145. /**
  146. * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
  147. *
  148. * <tt>
  149. * Derive-Secret( Secret, Label, Messages ) =
  150. * HKDF-Expand-Label( Secret, Label,
  151. * Hash( Messages ),
  152. * Hash.Length ) )
  153. * </tt>
  154. *
  155. * \param hash_alg The identifier for the hash function used for the
  156. * applications of HKDF.
  157. * \param secret The \c Secret argument to the \c Derive-Secret function.
  158. * This must be a readable buffer of length \p slen Bytes.
  159. * \param slen The length of \p secret in Bytes.
  160. * \param label The \c Label argument to the \c Derive-Secret function.
  161. * This must be a readable buffer of length \p llen Bytes.
  162. * \param llen The length of \p label in Bytes.
  163. * \param ctx The hash of the \c Messages argument to the
  164. * \c Derive-Secret function, or the \c Messages argument
  165. * itself, depending on \p context_already_hashed.
  166. * \param clen The length of \p hash.
  167. * \param ctx_hashed This indicates whether the \p ctx contains the hash of
  168. * the \c Messages argument in the application of the
  169. * \c Derive-Secret function
  170. * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
  171. * it is the content of \c Messages itself, in which case
  172. * the function takes care of the hashing
  173. * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
  174. * \param dstbuf The target buffer to write the output of
  175. * \c Derive-Secret to. This must be a writable buffer of
  176. * size \p buflen Bytes.
  177. * \param buflen The length of \p dstbuf in Bytes.
  178. *
  179. * \returns \c 0 on success.
  180. * \returns A negative error code on failure.
  181. */
  182. int mbedtls_ssl_tls1_3_derive_secret(
  183. mbedtls_md_type_t hash_alg,
  184. const unsigned char *secret, size_t slen,
  185. const unsigned char *label, size_t llen,
  186. const unsigned char *ctx, size_t clen,
  187. int ctx_hashed,
  188. unsigned char *dstbuf, size_t buflen );
  189. /**
  190. * \brief Derive TLS 1.3 early data key material from early secret.
  191. *
  192. * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
  193. * with the appropriate labels.
  194. *
  195. * <tt>
  196. * Early Secret
  197. * |
  198. * +-----> Derive-Secret(., "c e traffic", ClientHello)
  199. * | = client_early_traffic_secret
  200. * |
  201. * +-----> Derive-Secret(., "e exp master", ClientHello)
  202. * . = early_exporter_master_secret
  203. * .
  204. * .
  205. * </tt>
  206. *
  207. * \note To obtain the actual key and IV for the early data traffic,
  208. * the client secret derived by this function need to be
  209. * further processed by mbedtls_ssl_tls1_3_make_traffic_keys().
  210. *
  211. * \note The binder key, which is also generated from the early secret,
  212. * is omitted here. Its calculation is part of the separate routine
  213. * mbedtls_ssl_tls1_3_create_psk_binder().
  214. *
  215. * \param md_type The hash algorithm associated with the PSK for which
  216. * early data key material is being derived.
  217. * \param early_secret The early secret from which the early data key material
  218. * should be derived. This must be a readable buffer whose
  219. * length is the digest size of the hash algorithm
  220. * represented by \p md_size.
  221. * \param transcript The transcript of the handshake so far, calculated with
  222. * respect to \p md_type. This must be a readable buffer
  223. * whose length is the digest size of the hash algorithm
  224. * represented by \p md_size.
  225. * \param derived The address of the structure in which to store
  226. * the early data key material.
  227. *
  228. * \returns \c 0 on success.
  229. * \returns A negative error code on failure.
  230. */
  231. int mbedtls_ssl_tls1_3_derive_early_secrets(
  232. mbedtls_md_type_t md_type,
  233. unsigned char const *early_secret,
  234. unsigned char const *transcript, size_t transcript_len,
  235. mbedtls_ssl_tls1_3_early_secrets *derived );
  236. /**
  237. * \brief Derive TLS 1.3 handshake key material from the handshake secret.
  238. *
  239. * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
  240. * with the appropriate labels from the standard.
  241. *
  242. * <tt>
  243. * Handshake Secret
  244. * |
  245. * +-----> Derive-Secret( ., "c hs traffic",
  246. * | ClientHello...ServerHello )
  247. * | = client_handshake_traffic_secret
  248. * |
  249. * +-----> Derive-Secret( ., "s hs traffic",
  250. * . ClientHello...ServerHello )
  251. * . = server_handshake_traffic_secret
  252. * .
  253. * </tt>
  254. *
  255. * \note To obtain the actual key and IV for the encrypted handshake traffic,
  256. * the client and server secret derived by this function need to be
  257. * further processed by mbedtls_ssl_tls1_3_make_traffic_keys().
  258. *
  259. * \param md_type The hash algorithm associated with the ciphersuite
  260. * that's being used for the connection.
  261. * \param handshake_secret The handshake secret from which the handshake key
  262. * material should be derived. This must be a readable
  263. * buffer whose length is the digest size of the hash
  264. * algorithm represented by \p md_size.
  265. * \param transcript The transcript of the handshake so far, calculated
  266. * with respect to \p md_type. This must be a readable
  267. * buffer whose length is the digest size of the hash
  268. * algorithm represented by \p md_size.
  269. * \param derived The address of the structure in which to
  270. * store the handshake key material.
  271. *
  272. * \returns \c 0 on success.
  273. * \returns A negative error code on failure.
  274. */
  275. int mbedtls_ssl_tls1_3_derive_handshake_secrets(
  276. mbedtls_md_type_t md_type,
  277. unsigned char const *handshake_secret,
  278. unsigned char const *transcript, size_t transcript_len,
  279. mbedtls_ssl_tls1_3_handshake_secrets *derived );
  280. /**
  281. * \brief Derive TLS 1.3 application key material from the master secret.
  282. *
  283. * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
  284. * with the appropriate labels from the standard.
  285. *
  286. * <tt>
  287. * Master Secret
  288. * |
  289. * +-----> Derive-Secret( ., "c ap traffic",
  290. * | ClientHello...server Finished )
  291. * | = client_application_traffic_secret_0
  292. * |
  293. * +-----> Derive-Secret( ., "s ap traffic",
  294. * | ClientHello...Server Finished )
  295. * | = server_application_traffic_secret_0
  296. * |
  297. * +-----> Derive-Secret( ., "exp master",
  298. * . ClientHello...server Finished)
  299. * . = exporter_master_secret
  300. * .
  301. * </tt>
  302. *
  303. * \note To obtain the actual key and IV for the (0-th) application traffic,
  304. * the client and server secret derived by this function need to be
  305. * further processed by mbedtls_ssl_tls1_3_make_traffic_keys().
  306. *
  307. * \param md_type The hash algorithm associated with the ciphersuite
  308. * that's being used for the connection.
  309. * \param master_secret The master secret from which the application key
  310. * material should be derived. This must be a readable
  311. * buffer whose length is the digest size of the hash
  312. * algorithm represented by \p md_size.
  313. * \param transcript The transcript of the handshake up to and including
  314. * the ServerFinished message, calculated with respect
  315. * to \p md_type. This must be a readable buffer whose
  316. * length is the digest size of the hash algorithm
  317. * represented by \p md_type.
  318. * \param derived The address of the structure in which to
  319. * store the application key material.
  320. *
  321. * \returns \c 0 on success.
  322. * \returns A negative error code on failure.
  323. */
  324. int mbedtls_ssl_tls1_3_derive_application_secrets(
  325. mbedtls_md_type_t md_type,
  326. unsigned char const *master_secret,
  327. unsigned char const *transcript, size_t transcript_len,
  328. mbedtls_ssl_tls1_3_application_secrets *derived );
  329. /**
  330. * \brief Derive TLS 1.3 resumption master secret from the master secret.
  331. *
  332. * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
  333. * with the appropriate labels from the standard.
  334. *
  335. * \param md_type The hash algorithm used in the application for which
  336. * key material is being derived.
  337. * \param application_secret The application secret from which the resumption master
  338. * secret should be derived. This must be a readable
  339. * buffer whose length is the digest size of the hash
  340. * algorithm represented by \p md_size.
  341. * \param transcript The transcript of the handshake up to and including
  342. * the ClientFinished message, calculated with respect
  343. * to \p md_type. This must be a readable buffer whose
  344. * length is the digest size of the hash algorithm
  345. * represented by \p md_type.
  346. * \param transcript_len The length of \p transcript in Bytes.
  347. * \param derived The address of the structure in which to
  348. * store the resumption master secret.
  349. *
  350. * \returns \c 0 on success.
  351. * \returns A negative error code on failure.
  352. */
  353. int mbedtls_ssl_tls1_3_derive_resumption_master_secret(
  354. mbedtls_md_type_t md_type,
  355. unsigned char const *application_secret,
  356. unsigned char const *transcript, size_t transcript_len,
  357. mbedtls_ssl_tls1_3_application_secrets *derived );
  358. /**
  359. * \brief Compute the next secret in the TLS 1.3 key schedule
  360. *
  361. * The TLS 1.3 key schedule proceeds as follows to compute
  362. * the three main secrets during the handshake: The early
  363. * secret for early data, the handshake secret for all
  364. * other encrypted handshake messages, and the master
  365. * secret for all application traffic.
  366. *
  367. * <tt>
  368. * 0
  369. * |
  370. * v
  371. * PSK -> HKDF-Extract = Early Secret
  372. * |
  373. * v
  374. * Derive-Secret( ., "derived", "" )
  375. * |
  376. * v
  377. * (EC)DHE -> HKDF-Extract = Handshake Secret
  378. * |
  379. * v
  380. * Derive-Secret( ., "derived", "" )
  381. * |
  382. * v
  383. * 0 -> HKDF-Extract = Master Secret
  384. * </tt>
  385. *
  386. * Each of the three secrets in turn is the basis for further
  387. * key derivations, such as the derivation of traffic keys and IVs;
  388. * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys().
  389. *
  390. * This function implements one step in this evolution of secrets:
  391. *
  392. * <tt>
  393. * old_secret
  394. * |
  395. * v
  396. * Derive-Secret( ., "derived", "" )
  397. * |
  398. * v
  399. * input -> HKDF-Extract = new_secret
  400. * </tt>
  401. *
  402. * \param hash_alg The identifier for the hash function used for the
  403. * applications of HKDF.
  404. * \param secret_old The address of the buffer holding the old secret
  405. * on function entry. If not \c NULL, this must be a
  406. * readable buffer whose size matches the output size
  407. * of the hash function represented by \p hash_alg.
  408. * If \c NULL, an all \c 0 array will be used instead.
  409. * \param input The address of the buffer holding the additional
  410. * input for the key derivation (e.g., the PSK or the
  411. * ephemeral (EC)DH secret). If not \c NULL, this must be
  412. * a readable buffer whose size \p input_len Bytes.
  413. * If \c NULL, an all \c 0 array will be used instead.
  414. * \param input_len The length of \p input in Bytes.
  415. * \param secret_new The address of the buffer holding the new secret
  416. * on function exit. This must be a writable buffer
  417. * whose size matches the output size of the hash
  418. * function represented by \p hash_alg.
  419. * This may be the same as \p secret_old.
  420. *
  421. * \returns \c 0 on success.
  422. * \returns A negative error code on failure.
  423. */
  424. int mbedtls_ssl_tls1_3_evolve_secret(
  425. mbedtls_md_type_t hash_alg,
  426. const unsigned char *secret_old,
  427. const unsigned char *input, size_t input_len,
  428. unsigned char *secret_new );
  429. #define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0
  430. #define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1
  431. /**
  432. * \brief Calculate a TLS 1.3 PSK binder.
  433. *
  434. * \param ssl The SSL context. This is used for debugging only and may
  435. * be \c NULL if MBEDTLS_DEBUG_C is disabled.
  436. * \param md_type The hash algorithm associated to the PSK \p psk.
  437. * \param psk The buffer holding the PSK for which to create a binder.
  438. * \param psk_len The size of \p psk in bytes.
  439. * \param psk_type This indicates whether the PSK \p psk is externally
  440. * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
  441. * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
  442. * \param transcript The handshake transcript up to the point where the
  443. * PSK binder calculation happens. This must be readable,
  444. * and its size must be equal to the digest size of
  445. * the hash algorithm represented by \p md_type.
  446. * \param result The address at which to store the PSK binder on success.
  447. * This must be writable, and its size must be equal to the
  448. * digest size of the hash algorithm represented by
  449. * \p md_type.
  450. *
  451. * \returns \c 0 on success.
  452. * \returns A negative error code on failure.
  453. */
  454. int mbedtls_ssl_tls1_3_create_psk_binder( mbedtls_ssl_context *ssl,
  455. const mbedtls_md_type_t md_type,
  456. unsigned char const *psk, size_t psk_len,
  457. int psk_type,
  458. unsigned char const *transcript,
  459. unsigned char *result );
  460. /**
  461. * \bref Setup an SSL transform structure representing the
  462. * record protection mechanism used by TLS 1.3
  463. *
  464. * \param transform The SSL transform structure to be created. This must have
  465. * been initialized through mbedtls_ssl_transform_init() and
  466. * not used in any other way prior to calling this function.
  467. * In particular, this function does not clean up the
  468. * transform structure prior to installing the new keys.
  469. * \param endpoint Indicates whether the transform is for the client
  470. * (value #MBEDTLS_SSL_IS_CLIENT) or the server
  471. * (value #MBEDTLS_SSL_IS_SERVER).
  472. * \param ciphersuite The numerical identifier for the ciphersuite to use.
  473. * This must be one of the identifiers listed in
  474. * ssl_ciphersuites.h.
  475. * \param traffic_keys The key material to use. No reference is stored in
  476. * the SSL transform being generated, and the caller
  477. * should destroy the key material afterwards.
  478. * \param ssl (Debug-only) The SSL context to use for debug output
  479. * in case of failure. This parameter is only needed if
  480. * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
  481. *
  482. * \return \c 0 on success. In this case, \p transform is ready to
  483. * be used with mbedtls_ssl_transform_decrypt() and
  484. * mbedtls_ssl_transform_encrypt().
  485. * \return A negative error code on failure.
  486. */
  487. int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
  488. int endpoint,
  489. int ciphersuite,
  490. mbedtls_ssl_key_set const *traffic_keys,
  491. mbedtls_ssl_context *ssl );
  492. /*
  493. * TLS 1.3 key schedule evolutions
  494. *
  495. * Early -> Handshake -> Application
  496. *
  497. * Small wrappers around mbedtls_ssl_tls1_3_evolve_secret().
  498. */
  499. /**
  500. * \brief Begin TLS 1.3 key schedule by calculating early secret.
  501. *
  502. * The TLS 1.3 key schedule can be viewed as a simple state machine
  503. * with states Initial -> Early -> Handshake -> Application, and
  504. * this function represents the Initial -> Early transition.
  505. *
  506. * \param ssl The SSL context to operate on.
  507. *
  508. * \returns \c 0 on success.
  509. * \returns A negative error code on failure.
  510. */
  511. int mbedtls_ssl_tls1_3_key_schedule_stage_early( mbedtls_ssl_context *ssl );
  512. /**
  513. * \brief Transition into handshake stage of TLS 1.3 key schedule.
  514. *
  515. * The TLS 1.3 key schedule can be viewed as a simple state machine
  516. * with states Initial -> Early -> Handshake -> Application, and
  517. * this function represents the Early -> Handshake transition.
  518. *
  519. * In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys()
  520. * can be used to derive the handshake traffic keys.
  521. *
  522. * \param ssl The SSL context to operate on. This must be in key schedule
  523. * stage \c Early.
  524. *
  525. * \returns \c 0 on success.
  526. * \returns A negative error code on failure.
  527. */
  528. int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl );
  529. /**
  530. * \brief Compute TLS 1.3 handshake traffic keys.
  531. *
  532. * \param ssl The SSL context to operate on. This must be in
  533. * key schedule stage \c Handshake, see
  534. * mbedtls_ssl_tls13_key_schedule_stage_handshake().
  535. * \param traffic_keys The address at which to store the handshake traffic key
  536. * keys. This must be writable but may be uninitialized.
  537. *
  538. * \returns \c 0 on success.
  539. * \returns A negative error code on failure.
  540. */
  541. int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
  542. mbedtls_ssl_key_set *traffic_keys );
  543. /**
  544. * \brief Transition into application stage of TLS 1.3 key schedule.
  545. *
  546. * The TLS 1.3 key schedule can be viewed as a simple state machine
  547. * with states Initial -> Early -> Handshake -> Application, and
  548. * this function represents the Handshake -> Application transition.
  549. *
  550. * In the handshake stage, mbedtls_ssl_tls13_generate_application_keys()
  551. * can be used to derive the handshake traffic keys.
  552. *
  553. * \param ssl The SSL context to operate on. This must be in key schedule
  554. * stage \c Handshake.
  555. *
  556. * \returns \c 0 on success.
  557. * \returns A negative error code on failure.
  558. */
  559. int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl );
  560. /**
  561. * \brief Compute TLS 1.3 application traffic keys.
  562. *
  563. * \param ssl The SSL context to operate on. This must be in
  564. * key schedule stage \c Application, see
  565. * mbedtls_ssl_tls13_key_schedule_stage_application().
  566. * \param traffic_keys The address at which to store the application traffic key
  567. * keys. This must be writable but may be uninitialized.
  568. *
  569. * \returns \c 0 on success.
  570. * \returns A negative error code on failure.
  571. */
  572. int mbedtls_ssl_tls13_generate_application_keys(
  573. mbedtls_ssl_context* ssl, mbedtls_ssl_key_set *traffic_keys );
  574. /**
  575. * \brief Calculate the verify_data value for the client or server TLS 1.3
  576. * Finished message.
  577. *
  578. * \param ssl The SSL context to operate on. This must be in
  579. * key schedule stage \c Handshake, see
  580. * mbedtls_ssl_tls13_key_schedule_stage_application().
  581. * \param dst The address at which to write the verify_data value.
  582. * \param dst_len The size of \p dst in bytes.
  583. * \param actual_len The address at which to store the amount of data
  584. * actually written to \p dst upon success.
  585. * \param which The message to calculate the `verify_data` for:
  586. * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
  587. * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
  588. *
  589. * \note Both client and server call this function twice, once to
  590. * generate their own Finished message, and once to verify the
  591. * peer's Finished message.
  592. * \returns \c 0 on success.
  593. * \returns A negative error code on failure.
  594. */
  595. int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context *ssl,
  596. unsigned char *dst,
  597. size_t dst_len,
  598. size_t *actual_len,
  599. int which );
  600. #endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */