2020-08-21 14:20:06 +02:00
|
|
|
/*
|
|
|
|
* TLS 1.3 key schedule
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 ( the "License" ); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#if !defined(MBEDTLS_SSL_TLS1_3_KEYS_H)
|
|
|
|
#define MBEDTLS_SSL_TLS1_3_KEYS_H
|
|
|
|
|
2020-09-09 11:11:21 +02:00
|
|
|
/* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
|
2021-11-12 09:53:56 +01:00
|
|
|
* the point of use. See e.g. the definition of mbedtls_ssl_tls13_labels_union
|
2020-09-08 12:33:48 +02:00
|
|
|
* below. */
|
2021-09-19 14:27:17 +02:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( finished , "finished" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( resumption , "resumption" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( traffic_upd , "traffic upd" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( exporter , "exporter" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( key , "key" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( iv , "iv" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( c_hs_traffic, "c hs traffic" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( c_ap_traffic, "c ap traffic" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( c_e_traffic , "c e traffic" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( s_hs_traffic, "s hs traffic" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( s_ap_traffic, "s ap traffic" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( s_e_traffic , "s e traffic" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( e_exp_master, "e exp master" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( res_master , "res master" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( exp_master , "exp master" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( ext_binder , "ext binder" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( res_binder , "res binder" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( derived , "derived" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( client_cv , "TLS 1.3, client CertificateVerify" ) \
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL( server_cv , "TLS 1.3, server CertificateVerify" )
|
2020-09-08 11:43:52 +02:00
|
|
|
|
2022-03-30 11:33:06 +02:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
|
|
|
|
#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
|
|
|
|
|
|
|
|
#define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0
|
|
|
|
#define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
|
|
|
|
2020-09-09 13:46:09 +02:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
|
2020-09-08 11:43:52 +02:00
|
|
|
const unsigned char name [ sizeof(string) - 1 ];
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
union mbedtls_ssl_tls13_labels_union
|
2020-08-21 14:20:06 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL_LIST
|
|
|
|
};
|
2021-11-12 09:53:56 +01:00
|
|
|
struct mbedtls_ssl_tls13_labels_struct
|
2020-08-21 14:20:06 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_TLS1_3_LABEL_LIST
|
|
|
|
};
|
2020-09-08 12:33:48 +02:00
|
|
|
#undef MBEDTLS_SSL_TLS1_3_LABEL
|
2020-09-08 11:43:52 +02:00
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
extern const struct mbedtls_ssl_tls13_labels_struct mbedtls_ssl_tls13_labels;
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2021-09-19 14:27:17 +02:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL ) \
|
2021-11-12 09:53:56 +01:00
|
|
|
sizeof(mbedtls_ssl_tls13_labels.LABEL)
|
2021-09-19 14:27:17 +02:00
|
|
|
|
2020-08-21 14:20:06 +02:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_labels.LABEL, \
|
2021-09-19 14:27:17 +02:00
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL )
|
2020-08-21 14:20:06 +02:00
|
|
|
|
|
|
|
#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
|
2021-11-12 09:53:56 +01:00
|
|
|
sizeof( union mbedtls_ssl_tls13_labels_union )
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2020-09-16 10:24:14 +02:00
|
|
|
/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
|
2020-08-21 14:20:06 +02:00
|
|
|
* Since contexts are always hashes of message transcripts, this can
|
|
|
|
* be approximated from above by the maximum hash size. */
|
|
|
|
#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
|
|
|
|
MBEDTLS_MD_MAX_SIZE
|
|
|
|
|
|
|
|
/* Maximum desired length for expanded key material generated
|
2020-09-16 10:45:27 +02:00
|
|
|
* by HKDF-Expand-Label.
|
|
|
|
*
|
|
|
|
* Warning: If this ever needs to be increased, the implementation
|
2021-11-12 09:53:56 +01:00
|
|
|
* ssl_tls13_hkdf_encode_label() in ssl_tls13_keys.c needs to be
|
2020-09-16 10:45:27 +02:00
|
|
|
* adjusted since it currently assumes that HKDF key expansion
|
|
|
|
* is never used with more than 255 Bytes of output. */
|
2020-08-21 14:20:06 +02:00
|
|
|
#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
|
|
|
|
|
|
|
|
/**
|
2021-11-23 07:31:16 +01:00
|
|
|
* \brief The \c HKDF-Expand-Label function from
|
|
|
|
* the TLS 1.3 standard RFC 8446.
|
2020-08-21 14:20:06 +02:00
|
|
|
*
|
|
|
|
* <tt>
|
2021-11-23 07:31:16 +01:00
|
|
|
* HKDF-Expand-Label( Secret, Label, Context, Length ) =
|
2020-08-21 14:20:06 +02:00
|
|
|
* HKDF-Expand( Secret, HkdfLabel, Length )
|
|
|
|
* </tt>
|
|
|
|
*
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param hash_alg The identifier for the hash algorithm to use.
|
|
|
|
* \param secret The \c Secret argument to \c HKDF-Expand-Label.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of length
|
|
|
|
* \p secret_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param secret_len The length of \p secret in Bytes.
|
|
|
|
* \param label The \c Label argument to \c HKDF-Expand-Label.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of length
|
|
|
|
* \p label_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param label_len The length of \p label in Bytes.
|
|
|
|
* \param ctx The \c Context argument to \c HKDF-Expand-Label.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of length \p ctx_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param ctx_len The length of \p context in Bytes.
|
|
|
|
* \param buf The destination buffer to hold the expanded secret.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a writable buffer of length \p buf_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param buf_len The desired size of the expanded secret in Bytes.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \return A negative error code on failure.
|
2020-08-21 14:20:06 +02:00
|
|
|
*/
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_hkdf_expand_label(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2021-11-23 07:31:16 +01:00
|
|
|
const unsigned char *secret, size_t secret_len,
|
|
|
|
const unsigned char *label, size_t label_len,
|
|
|
|
const unsigned char *ctx, size_t ctx_len,
|
|
|
|
unsigned char *buf, size_t buf_len );
|
2020-08-21 14:20:06 +02:00
|
|
|
|
2020-08-21 14:03:34 +02:00
|
|
|
/**
|
|
|
|
* \brief This function is part of the TLS 1.3 key schedule.
|
|
|
|
* It extracts key and IV for the actual client/server traffic
|
|
|
|
* from the client/server traffic secrets.
|
|
|
|
*
|
|
|
|
* From RFC 8446:
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
|
|
|
|
* [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
|
|
|
|
* </tt>
|
|
|
|
*
|
|
|
|
* \param hash_alg The identifier for the hash algorithm to be used
|
|
|
|
* for the HKDF-based expansion of the secret.
|
|
|
|
* \param client_secret The client traffic secret.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of size
|
|
|
|
* \p secret_len Bytes
|
2020-08-21 14:03:34 +02:00
|
|
|
* \param server_secret The server traffic secret.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of size
|
|
|
|
* \p secret_len Bytes
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param secret_len Length of the secrets \p client_secret and
|
2020-08-21 14:03:34 +02:00
|
|
|
* \p server_secret in Bytes.
|
2020-09-08 12:01:00 +02:00
|
|
|
* \param key_len The desired length of the key to be extracted in Bytes.
|
|
|
|
* \param iv_len The desired length of the IV to be extracted in Bytes.
|
2020-08-21 14:03:34 +02:00
|
|
|
* \param keys The address of the structure holding the generated
|
|
|
|
* keys and IVs.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_make_traffic_keys(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2020-08-21 14:03:34 +02:00
|
|
|
const unsigned char *client_secret,
|
2021-11-23 07:31:16 +01:00
|
|
|
const unsigned char *server_secret, size_t secret_len,
|
|
|
|
size_t key_len, size_t iv_len,
|
2020-08-21 14:03:34 +02:00
|
|
|
mbedtls_ssl_key_set *keys );
|
|
|
|
|
2020-08-21 14:27:44 +02:00
|
|
|
/**
|
|
|
|
* \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* Derive-Secret( Secret, Label, Messages ) =
|
|
|
|
* HKDF-Expand-Label( Secret, Label,
|
|
|
|
* Hash( Messages ),
|
|
|
|
* Hash.Length ) )
|
|
|
|
* </tt>
|
|
|
|
*
|
2020-09-09 13:58:29 +02:00
|
|
|
* \param hash_alg The identifier for the hash function used for the
|
|
|
|
* applications of HKDF.
|
|
|
|
* \param secret The \c Secret argument to the \c Derive-Secret function.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of length
|
|
|
|
* \p secret_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param secret_len The length of \p secret in Bytes.
|
2020-09-09 13:58:29 +02:00
|
|
|
* \param label The \c Label argument to the \c Derive-Secret function.
|
2021-12-02 07:36:27 +01:00
|
|
|
* This must be a readable buffer of length
|
|
|
|
* \p label_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param label_len The length of \p label in Bytes.
|
2020-09-09 13:58:29 +02:00
|
|
|
* \param ctx The hash of the \c Messages argument to the
|
|
|
|
* \c Derive-Secret function, or the \c Messages argument
|
2021-12-02 07:36:27 +01:00
|
|
|
* itself, depending on \p ctx_hashed.
|
|
|
|
* \param ctx_len The length of \p ctx in Bytes.
|
2020-09-09 13:58:29 +02:00
|
|
|
* \param ctx_hashed This indicates whether the \p ctx contains the hash of
|
|
|
|
* the \c Messages argument in the application of the
|
|
|
|
* \c Derive-Secret function
|
|
|
|
* (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
|
|
|
|
* it is the content of \c Messages itself, in which case
|
|
|
|
* the function takes care of the hashing
|
|
|
|
* (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
|
|
|
|
* \param dstbuf The target buffer to write the output of
|
|
|
|
* \c Derive-Secret to. This must be a writable buffer of
|
2021-12-02 07:36:27 +01:00
|
|
|
* size \p dtsbuf_len Bytes.
|
2021-11-23 07:31:16 +01:00
|
|
|
* \param dstbuf_len The length of \p dstbuf in Bytes.
|
2020-08-21 14:27:44 +02:00
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_secret(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2021-11-23 07:31:16 +01:00
|
|
|
const unsigned char *secret, size_t secret_len,
|
|
|
|
const unsigned char *label, size_t label_len,
|
|
|
|
const unsigned char *ctx, size_t ctx_len,
|
2020-09-09 13:58:29 +02:00
|
|
|
int ctx_hashed,
|
2021-11-23 07:31:16 +01:00
|
|
|
unsigned char *dstbuf, size_t dstbuf_len );
|
2020-08-21 14:27:44 +02:00
|
|
|
|
2021-05-24 07:39:41 +02:00
|
|
|
/**
|
|
|
|
* \brief Derive TLS 1.3 early data key material from early secret.
|
|
|
|
*
|
2021-11-12 09:53:56 +01:00
|
|
|
* This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
|
2021-05-24 07:39:41 +02:00
|
|
|
* with the appropriate labels.
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* Early Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret(., "c e traffic", ClientHello)
|
|
|
|
* | = client_early_traffic_secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret(., "e exp master", ClientHello)
|
|
|
|
* . = early_exporter_master_secret
|
|
|
|
* .
|
|
|
|
* .
|
|
|
|
* </tt>
|
|
|
|
*
|
|
|
|
* \note To obtain the actual key and IV for the early data traffic,
|
|
|
|
* the client secret derived by this function need to be
|
2021-11-12 09:53:56 +01:00
|
|
|
* further processed by mbedtls_ssl_tls13_make_traffic_keys().
|
2021-05-24 07:39:41 +02:00
|
|
|
*
|
|
|
|
* \note The binder key, which is also generated from the early secret,
|
|
|
|
* is omitted here. Its calculation is part of the separate routine
|
2021-11-12 09:53:56 +01:00
|
|
|
* mbedtls_ssl_tls13_create_psk_binder().
|
2021-05-24 07:39:41 +02:00
|
|
|
*
|
2022-03-26 17:04:19 +01:00
|
|
|
* \param hash_alg The hash algorithm associated with the PSK for which
|
2021-05-24 07:39:41 +02:00
|
|
|
* early data key material is being derived.
|
|
|
|
* \param early_secret The early secret from which the early data key material
|
|
|
|
* should be derived. This must be a readable buffer whose
|
|
|
|
* length is the digest size of the hash algorithm
|
|
|
|
* represented by \p md_size.
|
|
|
|
* \param transcript The transcript of the handshake so far, calculated with
|
2022-03-26 17:04:19 +01:00
|
|
|
* respect to \p hash_alg. This must be a readable buffer
|
2021-05-24 07:39:41 +02:00
|
|
|
* whose length is the digest size of the hash algorithm
|
|
|
|
* represented by \p md_size.
|
|
|
|
* \param derived The address of the structure in which to store
|
|
|
|
* the early data key material.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_early_secrets(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2021-05-24 07:39:41 +02:00
|
|
|
unsigned char const *early_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_early_secrets *derived );
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Derive TLS 1.3 handshake key material from the handshake secret.
|
|
|
|
*
|
2021-11-12 09:53:56 +01:00
|
|
|
* This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
|
2021-05-24 07:39:41 +02:00
|
|
|
* with the appropriate labels from the standard.
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* Handshake Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "c hs traffic",
|
|
|
|
* | ClientHello...ServerHello )
|
|
|
|
* | = client_handshake_traffic_secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "s hs traffic",
|
|
|
|
* . ClientHello...ServerHello )
|
|
|
|
* . = server_handshake_traffic_secret
|
|
|
|
* .
|
|
|
|
* </tt>
|
|
|
|
*
|
|
|
|
* \note To obtain the actual key and IV for the encrypted handshake traffic,
|
|
|
|
* the client and server secret derived by this function need to be
|
2021-11-12 09:53:56 +01:00
|
|
|
* further processed by mbedtls_ssl_tls13_make_traffic_keys().
|
2021-05-24 07:39:41 +02:00
|
|
|
*
|
2022-03-26 17:04:19 +01:00
|
|
|
* \param hash_alg The hash algorithm associated with the ciphersuite
|
2021-05-24 07:39:41 +02:00
|
|
|
* that's being used for the connection.
|
|
|
|
* \param handshake_secret The handshake secret from which the handshake key
|
|
|
|
* material should be derived. This must be a readable
|
|
|
|
* buffer whose length is the digest size of the hash
|
|
|
|
* algorithm represented by \p md_size.
|
|
|
|
* \param transcript The transcript of the handshake so far, calculated
|
2022-03-26 17:04:19 +01:00
|
|
|
* with respect to \p hash_alg. This must be a readable
|
2021-05-24 07:39:41 +02:00
|
|
|
* buffer whose length is the digest size of the hash
|
|
|
|
* algorithm represented by \p md_size.
|
|
|
|
* \param derived The address of the structure in which to
|
|
|
|
* store the handshake key material.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_handshake_secrets(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2021-05-24 07:39:41 +02:00
|
|
|
unsigned char const *handshake_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_handshake_secrets *derived );
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Derive TLS 1.3 application key material from the master secret.
|
|
|
|
*
|
2021-11-12 09:53:56 +01:00
|
|
|
* This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
|
2021-05-24 07:39:41 +02:00
|
|
|
* with the appropriate labels from the standard.
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* Master Secret
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "c ap traffic",
|
|
|
|
* | ClientHello...server Finished )
|
|
|
|
* | = client_application_traffic_secret_0
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "s ap traffic",
|
|
|
|
* | ClientHello...Server Finished )
|
|
|
|
* | = server_application_traffic_secret_0
|
|
|
|
* |
|
|
|
|
* +-----> Derive-Secret( ., "exp master",
|
|
|
|
* . ClientHello...server Finished)
|
|
|
|
* . = exporter_master_secret
|
|
|
|
* .
|
|
|
|
* </tt>
|
|
|
|
*
|
|
|
|
* \note To obtain the actual key and IV for the (0-th) application traffic,
|
|
|
|
* the client and server secret derived by this function need to be
|
2021-11-12 09:53:56 +01:00
|
|
|
* further processed by mbedtls_ssl_tls13_make_traffic_keys().
|
2021-05-24 07:39:41 +02:00
|
|
|
*
|
2022-03-26 17:04:19 +01:00
|
|
|
* \param hash_alg The hash algorithm associated with the ciphersuite
|
2021-05-24 07:39:41 +02:00
|
|
|
* that's being used for the connection.
|
|
|
|
* \param master_secret The master secret from which the application key
|
|
|
|
* material should be derived. This must be a readable
|
|
|
|
* buffer whose length is the digest size of the hash
|
|
|
|
* algorithm represented by \p md_size.
|
|
|
|
* \param transcript The transcript of the handshake up to and including
|
|
|
|
* the ServerFinished message, calculated with respect
|
2022-03-26 17:04:19 +01:00
|
|
|
* to \p hash_alg. This must be a readable buffer whose
|
2021-05-24 07:39:41 +02:00
|
|
|
* length is the digest size of the hash algorithm
|
2022-03-26 17:04:19 +01:00
|
|
|
* represented by \p hash_alg.
|
2021-05-24 07:39:41 +02:00
|
|
|
* \param derived The address of the structure in which to
|
|
|
|
* store the application key material.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_application_secrets(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2021-05-24 07:39:41 +02:00
|
|
|
unsigned char const *master_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_application_secrets *derived );
|
2021-05-24 07:39:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Derive TLS 1.3 resumption master secret from the master secret.
|
|
|
|
*
|
2021-11-12 09:53:56 +01:00
|
|
|
* This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
|
2021-05-24 07:39:41 +02:00
|
|
|
* with the appropriate labels from the standard.
|
|
|
|
*
|
2022-03-26 17:04:19 +01:00
|
|
|
* \param hash_alg The hash algorithm used in the application for which
|
2021-05-24 07:39:41 +02:00
|
|
|
* key material is being derived.
|
|
|
|
* \param application_secret The application secret from which the resumption master
|
|
|
|
* secret should be derived. This must be a readable
|
|
|
|
* buffer whose length is the digest size of the hash
|
|
|
|
* algorithm represented by \p md_size.
|
|
|
|
* \param transcript The transcript of the handshake up to and including
|
|
|
|
* the ClientFinished message, calculated with respect
|
2022-03-26 17:04:19 +01:00
|
|
|
* to \p hash_alg. This must be a readable buffer whose
|
2021-05-24 07:39:41 +02:00
|
|
|
* length is the digest size of the hash algorithm
|
2022-03-26 17:04:19 +01:00
|
|
|
* represented by \p hash_alg.
|
2021-05-24 07:39:41 +02:00
|
|
|
* \param transcript_len The length of \p transcript in Bytes.
|
|
|
|
* \param derived The address of the structure in which to
|
|
|
|
* store the resumption master secret.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_derive_resumption_master_secret(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2021-05-24 07:39:41 +02:00
|
|
|
unsigned char const *application_secret,
|
|
|
|
unsigned char const *transcript, size_t transcript_len,
|
2021-11-12 09:53:56 +01:00
|
|
|
mbedtls_ssl_tls13_application_secrets *derived );
|
2021-05-24 07:39:41 +02:00
|
|
|
|
2020-08-20 14:42:46 +02:00
|
|
|
/**
|
|
|
|
* \brief Compute the next secret in the TLS 1.3 key schedule
|
|
|
|
*
|
|
|
|
* The TLS 1.3 key schedule proceeds as follows to compute
|
|
|
|
* the three main secrets during the handshake: The early
|
|
|
|
* secret for early data, the handshake secret for all
|
|
|
|
* other encrypted handshake messages, and the master
|
|
|
|
* secret for all application traffic.
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* 0
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* PSK -> HKDF-Extract = Early Secret
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* Derive-Secret( ., "derived", "" )
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* (EC)DHE -> HKDF-Extract = Handshake Secret
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* Derive-Secret( ., "derived", "" )
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* 0 -> HKDF-Extract = Master Secret
|
|
|
|
* </tt>
|
|
|
|
*
|
|
|
|
* Each of the three secrets in turn is the basis for further
|
|
|
|
* key derivations, such as the derivation of traffic keys and IVs;
|
2021-11-12 09:53:56 +01:00
|
|
|
* see e.g. mbedtls_ssl_tls13_make_traffic_keys().
|
2020-08-20 14:42:46 +02:00
|
|
|
*
|
|
|
|
* This function implements one step in this evolution of secrets:
|
|
|
|
*
|
|
|
|
* <tt>
|
|
|
|
* old_secret
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* Derive-Secret( ., "derived", "" )
|
|
|
|
* |
|
|
|
|
* v
|
|
|
|
* input -> HKDF-Extract = new_secret
|
|
|
|
* </tt>
|
|
|
|
*
|
|
|
|
* \param hash_alg The identifier for the hash function used for the
|
|
|
|
* applications of HKDF.
|
|
|
|
* \param secret_old The address of the buffer holding the old secret
|
|
|
|
* on function entry. If not \c NULL, this must be a
|
|
|
|
* readable buffer whose size matches the output size
|
|
|
|
* of the hash function represented by \p hash_alg.
|
|
|
|
* If \c NULL, an all \c 0 array will be used instead.
|
|
|
|
* \param input The address of the buffer holding the additional
|
|
|
|
* input for the key derivation (e.g., the PSK or the
|
|
|
|
* ephemeral (EC)DH secret). If not \c NULL, this must be
|
|
|
|
* a readable buffer whose size \p input_len Bytes.
|
|
|
|
* If \c NULL, an all \c 0 array will be used instead.
|
|
|
|
* \param input_len The length of \p input in Bytes.
|
|
|
|
* \param secret_new The address of the buffer holding the new secret
|
|
|
|
* on function exit. This must be a writable buffer
|
|
|
|
* whose size matches the output size of the hash
|
|
|
|
* function represented by \p hash_alg.
|
|
|
|
* This may be the same as \p secret_old.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
|
|
|
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_evolve_secret(
|
2022-03-26 17:04:19 +01:00
|
|
|
psa_algorithm_t hash_alg,
|
2020-08-20 14:42:46 +02:00
|
|
|
const unsigned char *secret_old,
|
|
|
|
const unsigned char *input, size_t input_len,
|
|
|
|
unsigned char *secret_new );
|
|
|
|
|
2021-05-24 07:44:14 +02:00
|
|
|
/**
|
|
|
|
* \brief Calculate a TLS 1.3 PSK binder.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context. This is used for debugging only and may
|
|
|
|
* be \c NULL if MBEDTLS_DEBUG_C is disabled.
|
2022-03-26 17:04:19 +01:00
|
|
|
* \param hash_alg The hash algorithm associated to the PSK \p psk.
|
2021-05-24 07:44:14 +02:00
|
|
|
* \param psk The buffer holding the PSK for which to create a binder.
|
|
|
|
* \param psk_len The size of \p psk in bytes.
|
2021-05-26 05:47:29 +02:00
|
|
|
* \param psk_type This indicates whether the PSK \p psk is externally
|
2021-05-24 07:44:14 +02:00
|
|
|
* provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
|
|
|
|
* resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
|
|
|
|
* \param transcript The handshake transcript up to the point where the
|
|
|
|
* PSK binder calculation happens. This must be readable,
|
|
|
|
* and its size must be equal to the digest size of
|
2022-03-26 17:04:19 +01:00
|
|
|
* the hash algorithm represented by \p hash_alg.
|
2021-05-24 07:44:14 +02:00
|
|
|
* \param result The address at which to store the PSK binder on success.
|
|
|
|
* This must be writable, and its size must be equal to the
|
|
|
|
* digest size of the hash algorithm represented by
|
2022-03-26 17:04:19 +01:00
|
|
|
* \p hash_alg.
|
2021-05-24 07:44:14 +02:00
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
|
2022-03-26 17:04:19 +01:00
|
|
|
const psa_algorithm_t hash_alg,
|
2021-05-24 07:44:14 +02:00
|
|
|
unsigned char const *psk, size_t psk_len,
|
|
|
|
int psk_type,
|
|
|
|
unsigned char const *transcript,
|
|
|
|
unsigned char *result );
|
|
|
|
|
2021-03-22 08:50:44 +01:00
|
|
|
/**
|
|
|
|
* \bref Setup an SSL transform structure representing the
|
|
|
|
* record protection mechanism used by TLS 1.3
|
|
|
|
*
|
|
|
|
* \param transform The SSL transform structure to be created. This must have
|
|
|
|
* been initialized through mbedtls_ssl_transform_init() and
|
|
|
|
* not used in any other way prior to calling this function.
|
|
|
|
* In particular, this function does not clean up the
|
|
|
|
* transform structure prior to installing the new keys.
|
|
|
|
* \param endpoint Indicates whether the transform is for the client
|
|
|
|
* (value #MBEDTLS_SSL_IS_CLIENT) or the server
|
|
|
|
* (value #MBEDTLS_SSL_IS_SERVER).
|
|
|
|
* \param ciphersuite The numerical identifier for the ciphersuite to use.
|
|
|
|
* This must be one of the identifiers listed in
|
|
|
|
* ssl_ciphersuites.h.
|
|
|
|
* \param traffic_keys The key material to use. No reference is stored in
|
|
|
|
* the SSL transform being generated, and the caller
|
|
|
|
* should destroy the key material afterwards.
|
|
|
|
* \param ssl (Debug-only) The SSL context to use for debug output
|
|
|
|
* in case of failure. This parameter is only needed if
|
|
|
|
* #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
|
|
|
|
*
|
|
|
|
* \return \c 0 on success. In this case, \p transform is ready to
|
|
|
|
* be used with mbedtls_ssl_transform_decrypt() and
|
|
|
|
* mbedtls_ssl_transform_encrypt().
|
|
|
|
* \return A negative error code on failure.
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
|
|
|
|
int endpoint,
|
|
|
|
int ciphersuite,
|
|
|
|
mbedtls_ssl_key_set const *traffic_keys,
|
|
|
|
mbedtls_ssl_context *ssl );
|
|
|
|
|
2021-09-09 08:31:24 +02:00
|
|
|
/*
|
|
|
|
* TLS 1.3 key schedule evolutions
|
|
|
|
*
|
2021-10-08 09:14:45 +02:00
|
|
|
* Early -> Handshake -> Application
|
2021-09-09 08:31:24 +02:00
|
|
|
*
|
2021-11-12 09:53:56 +01:00
|
|
|
* Small wrappers around mbedtls_ssl_tls13_evolve_secret().
|
2021-09-09 08:31:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-10-08 09:14:45 +02:00
|
|
|
* \brief Begin TLS 1.3 key schedule by calculating early secret.
|
2021-09-09 08:31:24 +02:00
|
|
|
*
|
|
|
|
* The TLS 1.3 key schedule can be viewed as a simple state machine
|
|
|
|
* with states Initial -> Early -> Handshake -> Application, and
|
|
|
|
* this function represents the Initial -> Early transition.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-12 09:53:56 +01:00
|
|
|
int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl );
|
2021-09-09 08:42:55 +02:00
|
|
|
|
2021-09-09 11:14:45 +02:00
|
|
|
/**
|
|
|
|
* \brief Transition into handshake stage of TLS 1.3 key schedule.
|
|
|
|
*
|
|
|
|
* The TLS 1.3 key schedule can be viewed as a simple state machine
|
|
|
|
* with states Initial -> Early -> Handshake -> Application, and
|
|
|
|
* this function represents the Early -> Handshake transition.
|
|
|
|
*
|
2021-10-11 16:30:19 +02:00
|
|
|
* In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys()
|
2021-09-09 11:14:45 +02:00
|
|
|
* can be used to derive the handshake traffic keys.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in key schedule
|
|
|
|
* stage \c Early.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-10-11 11:47:07 +02:00
|
|
|
int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl );
|
2021-09-09 11:14:45 +02:00
|
|
|
|
2021-09-16 12:59:08 +02:00
|
|
|
/**
|
|
|
|
* \brief Compute TLS 1.3 handshake traffic keys.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in
|
|
|
|
* key schedule stage \c Handshake, see
|
2021-10-11 11:47:07 +02:00
|
|
|
* mbedtls_ssl_tls13_key_schedule_stage_handshake().
|
2021-09-16 12:59:08 +02:00
|
|
|
* \param traffic_keys The address at which to store the handshake traffic key
|
|
|
|
* keys. This must be writable but may be uninitialized.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-10-11 16:30:19 +02:00
|
|
|
int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_key_set *traffic_keys );
|
2021-09-16 12:59:08 +02:00
|
|
|
|
2021-09-18 08:20:25 +02:00
|
|
|
/**
|
|
|
|
* \brief Transition into application stage of TLS 1.3 key schedule.
|
|
|
|
*
|
|
|
|
* The TLS 1.3 key schedule can be viewed as a simple state machine
|
|
|
|
* with states Initial -> Early -> Handshake -> Application, and
|
|
|
|
* this function represents the Handshake -> Application transition.
|
|
|
|
*
|
2021-11-10 07:17:40 +01:00
|
|
|
* In the handshake stage, mbedtls_ssl_tls13_generate_application_keys()
|
2021-09-18 08:20:25 +02:00
|
|
|
* can be used to derive the handshake traffic keys.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in key schedule
|
|
|
|
* stage \c Handshake.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-11 07:46:35 +01:00
|
|
|
int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl );
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Compute TLS 1.3 application traffic keys.
|
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in
|
|
|
|
* key schedule stage \c Application, see
|
2021-10-12 10:43:37 +02:00
|
|
|
* mbedtls_ssl_tls13_key_schedule_stage_application().
|
2021-09-18 08:20:25 +02:00
|
|
|
* \param traffic_keys The address at which to store the application traffic key
|
|
|
|
* keys. This must be writable but may be uninitialized.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-10 07:17:40 +01:00
|
|
|
int mbedtls_ssl_tls13_generate_application_keys(
|
2021-09-18 08:20:25 +02:00
|
|
|
mbedtls_ssl_context* ssl, mbedtls_ssl_key_set *traffic_keys );
|
|
|
|
|
|
|
|
/**
|
2021-11-09 12:55:10 +01:00
|
|
|
* \brief Calculate the verify_data value for the client or server TLS 1.3
|
|
|
|
* Finished message.
|
2021-09-18 08:20:25 +02:00
|
|
|
*
|
|
|
|
* \param ssl The SSL context to operate on. This must be in
|
|
|
|
* key schedule stage \c Handshake, see
|
2021-10-12 10:43:37 +02:00
|
|
|
* mbedtls_ssl_tls13_key_schedule_stage_application().
|
2021-11-09 12:55:10 +01:00
|
|
|
* \param dst The address at which to write the verify_data value.
|
2021-09-18 08:20:25 +02:00
|
|
|
* \param dst_len The size of \p dst in bytes.
|
|
|
|
* \param actual_len The address at which to store the amount of data
|
|
|
|
* actually written to \p dst upon success.
|
2021-11-10 04:07:04 +01:00
|
|
|
* \param which The message to calculate the `verify_data` for:
|
2021-09-18 08:20:25 +02:00
|
|
|
* - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
|
|
|
|
* - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
|
|
|
|
*
|
|
|
|
* \note Both client and server call this function twice, once to
|
|
|
|
* generate their own Finished message, and once to verify the
|
|
|
|
* peer's Finished message.
|
|
|
|
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2021-11-09 12:55:10 +01:00
|
|
|
int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context *ssl,
|
2021-11-10 04:07:04 +01:00
|
|
|
unsigned char *dst,
|
|
|
|
size_t dst_len,
|
|
|
|
size_t *actual_len,
|
|
|
|
int which );
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2022-05-05 04:19:22 +02:00
|
|
|
/**
|
|
|
|
* \brief Compute TLS 1.3 handshake transform
|
|
|
|
*
|
2022-05-06 05:16:55 +02:00
|
|
|
* \param ssl The SSL context to operate on. The early secret must have been
|
2022-05-05 04:19:22 +02:00
|
|
|
* computed.
|
|
|
|
*
|
|
|
|
* \returns \c 0 on success.
|
|
|
|
* \returns A negative error code on failure.
|
|
|
|
*/
|
2022-05-06 05:16:55 +02:00
|
|
|
int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl );
|
2022-05-05 04:19:22 +02:00
|
|
|
|
2022-03-30 11:33:06 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2020-08-21 14:20:06 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */
|