ssl_tls13_keys.h 28 KB

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