ssl_tls13_keys.h 28 KB

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