Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 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 | |
Hanno Becker | 58c5cea | 2020-09-08 10:31:33 +0100 | [diff] [blame] | 20 | #include "common.h" |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 21 | |
| 22 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 23 | |
| 24 | #include "mbedtls/hkdf.h" |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 25 | #include "mbedtls/ssl_internal.h" |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 26 | #include "ssl_tls13_keys.h" |
Andrzej Kurek | dc4a252 | 2022-10-18 09:28:40 -0400 | [diff] [blame] | 27 | #include "psa/crypto_sizes.h" |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 28 | |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 32 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 33 | .name = string, |
| 34 | |
Andrzej Kurek | dc4a252 | 2022-10-18 09:28:40 -0400 | [diff] [blame] | 35 | #define TLS1_3_EVOLVE_INPUT_SIZE ( PSA_HASH_MAX_SIZE > PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ) ? \ |
| 36 | PSA_HASH_MAX_SIZE : PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE |
| 37 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 38 | struct mbedtls_ssl_tls1_3_labels_struct const mbedtls_ssl_tls1_3_labels = |
| 39 | { |
| 40 | /* This seems to work in C, despite the string literal being one |
| 41 | * character too long due to the 0-termination. */ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 42 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 45 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 46 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 47 | /* |
| 48 | * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule. |
| 49 | * |
| 50 | * The HkdfLabel is specified in RFC 8446 as follows: |
| 51 | * |
| 52 | * struct HkdfLabel { |
| 53 | * uint16 length; // Length of expanded key material |
| 54 | * opaque label<7..255>; // Always prefixed by "tls13 " |
| 55 | * opaque context<0..255>; // Usually a communication transcript hash |
| 56 | * }; |
| 57 | * |
| 58 | * Parameters: |
| 59 | * - desired_length: Length of expanded key material |
| 60 | * Even though the standard allows expansion to up to |
| 61 | * 2**16 Bytes, TLS 1.3 never uses expansion to more than |
| 62 | * 255 Bytes, so we require `desired_length` to be at most |
| 63 | * 255. This allows us to save a few Bytes of code by |
| 64 | * hardcoding the writing of the high bytes. |
| 65 | * - (label, llen): label + label length, without "tls13 " prefix |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 66 | * The label length MUST be less than or equal to |
| 67 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN |
| 68 | * It is the caller's responsibility to ensure this. |
Hanno Becker | 815869a | 2020-09-08 11:16:16 +0100 | [diff] [blame] | 69 | * All (label, label length) pairs used in TLS 1.3 |
| 70 | * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(). |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 71 | * - (ctx, clen): context + context length |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 72 | * The context length MUST be less than or equal to |
| 73 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN |
| 74 | * It is the caller's responsibility to ensure this. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 75 | * - dst: Target buffer for HkdfLabel structure, |
| 76 | * This MUST be a writable buffer of size |
| 77 | * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes. |
| 78 | * - dlen: Pointer at which to store the actual length of |
| 79 | * the HkdfLabel structure on success. |
| 80 | */ |
| 81 | |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 82 | static const char tls1_3_label_prefix[6] = "tls13 "; |
| 83 | |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 84 | #define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 85 | ( 2 /* expansion length */ \ |
| 86 | + 1 /* label length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 87 | + label_len \ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 88 | + 1 /* context length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 89 | + context_len ) |
| 90 | |
| 91 | #define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \ |
| 92 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \ |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 93 | sizeof(tls1_3_label_prefix) + \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 94 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \ |
| 95 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 96 | |
| 97 | static void ssl_tls1_3_hkdf_encode_label( |
| 98 | size_t desired_length, |
| 99 | const unsigned char *label, size_t llen, |
| 100 | const unsigned char *ctx, size_t clen, |
| 101 | unsigned char *dst, size_t *dlen ) |
| 102 | { |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 103 | size_t total_label_len = |
| 104 | sizeof(tls1_3_label_prefix) + llen; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 105 | size_t total_hkdf_lbl_len = |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 106 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, clen ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 107 | |
| 108 | unsigned char *p = dst; |
| 109 | |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 110 | /* Add the size of the expanded key material. |
| 111 | * We're hardcoding the high byte to 0 here assuming that we never use |
| 112 | * TLS 1.3 HKDF key expansion to more than 255 Bytes. */ |
| 113 | #if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255 |
| 114 | #error "The implementation of ssl_tls1_3_hkdf_encode_label() is not fit for the \ |
| 115 | value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN" |
| 116 | #endif |
| 117 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 118 | *p++ = 0; |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 119 | *p++ = MBEDTLS_BYTE_0( desired_length ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 120 | |
| 121 | /* Add label incl. prefix */ |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 122 | *p++ = MBEDTLS_BYTE_0( total_label_len ); |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 123 | memcpy( p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix) ); |
| 124 | p += sizeof(tls1_3_label_prefix); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 125 | memcpy( p, label, llen ); |
| 126 | p += llen; |
| 127 | |
| 128 | /* Add context value */ |
Joe Subbiani | c045dc1 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 129 | *p++ = MBEDTLS_BYTE_0( clen ); |
Hanno Becker | 00debc7 | 2020-09-08 11:12:24 +0100 | [diff] [blame] | 130 | if( clen != 0 ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 131 | memcpy( p, ctx, clen ); |
| 132 | |
| 133 | /* Return total length to the caller. */ |
| 134 | *dlen = total_hkdf_lbl_len; |
| 135 | } |
| 136 | |
| 137 | int mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 138 | mbedtls_md_type_t hash_alg, |
| 139 | const unsigned char *secret, size_t slen, |
| 140 | const unsigned char *label, size_t llen, |
| 141 | const unsigned char *ctx, size_t clen, |
| 142 | unsigned char *buf, size_t blen ) |
| 143 | { |
| 144 | const mbedtls_md_info_t *md; |
| 145 | unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ]; |
| 146 | size_t hkdf_label_len; |
| 147 | |
| 148 | if( llen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN ) |
| 149 | { |
| 150 | /* Should never happen since this is an internal |
| 151 | * function, and we know statically which labels |
| 152 | * are allowed. */ |
| 153 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 154 | } |
| 155 | |
| 156 | if( clen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
| 157 | { |
| 158 | /* Should not happen, as above. */ |
| 159 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 160 | } |
| 161 | |
| 162 | if( blen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN ) |
| 163 | { |
| 164 | /* Should not happen, as above. */ |
| 165 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 166 | } |
| 167 | |
| 168 | md = mbedtls_md_info_from_type( hash_alg ); |
| 169 | if( md == NULL ) |
| 170 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 171 | |
| 172 | ssl_tls1_3_hkdf_encode_label( blen, |
| 173 | label, llen, |
| 174 | ctx, clen, |
| 175 | hkdf_label, |
| 176 | &hkdf_label_len ); |
| 177 | |
| 178 | return( mbedtls_hkdf_expand( md, |
| 179 | secret, slen, |
| 180 | hkdf_label, hkdf_label_len, |
| 181 | buf, blen ) ); |
| 182 | } |
| 183 | |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 184 | /* |
| 185 | * The traffic keying material is generated from the following inputs: |
| 186 | * |
| 187 | * - One secret value per sender. |
| 188 | * - A purpose value indicating the specific value being generated |
| 189 | * - The desired lengths of key and IV. |
| 190 | * |
| 191 | * The expansion itself is based on HKDF: |
| 192 | * |
| 193 | * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length ) |
| 194 | * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length ) |
| 195 | * |
| 196 | * [sender] denotes the sending side and the Secret value is provided |
| 197 | * by the function caller. Note that we generate server and client side |
| 198 | * keys in a single function call. |
| 199 | */ |
| 200 | int mbedtls_ssl_tls1_3_make_traffic_keys( |
| 201 | mbedtls_md_type_t hash_alg, |
| 202 | const unsigned char *client_secret, |
| 203 | const unsigned char *server_secret, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 204 | size_t slen, size_t key_len, size_t iv_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 205 | mbedtls_ssl_key_set *keys ) |
| 206 | { |
| 207 | int ret = 0; |
| 208 | |
| 209 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 210 | client_secret, slen, |
| 211 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 212 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 213 | keys->client_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 214 | if( ret != 0 ) |
| 215 | return( ret ); |
| 216 | |
| 217 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 218 | server_secret, slen, |
| 219 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 220 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 221 | keys->server_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 222 | if( ret != 0 ) |
| 223 | return( ret ); |
| 224 | |
| 225 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 226 | client_secret, slen, |
| 227 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 228 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 229 | keys->client_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 230 | if( ret != 0 ) |
| 231 | return( ret ); |
| 232 | |
| 233 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 234 | server_secret, slen, |
| 235 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 236 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 237 | keys->server_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 238 | if( ret != 0 ) |
| 239 | return( ret ); |
| 240 | |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 241 | keys->key_len = key_len; |
| 242 | keys->iv_len = iv_len; |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 243 | |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 247 | int mbedtls_ssl_tls1_3_derive_secret( |
| 248 | mbedtls_md_type_t hash_alg, |
| 249 | const unsigned char *secret, size_t slen, |
| 250 | const unsigned char *label, size_t llen, |
| 251 | const unsigned char *ctx, size_t clen, |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 252 | int ctx_hashed, |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 253 | unsigned char *dstbuf, size_t buflen ) |
| 254 | { |
| 255 | int ret; |
| 256 | unsigned char hashed_context[ MBEDTLS_MD_MAX_SIZE ]; |
| 257 | |
| 258 | const mbedtls_md_info_t *md; |
| 259 | md = mbedtls_md_info_from_type( hash_alg ); |
| 260 | if( md == NULL ) |
| 261 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 262 | |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 263 | if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 264 | { |
| 265 | ret = mbedtls_md( md, ctx, clen, hashed_context ); |
| 266 | if( ret != 0 ) |
| 267 | return( ret ); |
| 268 | clen = mbedtls_md_get_size( md ); |
| 269 | } |
| 270 | else |
| 271 | { |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 272 | if( clen > sizeof(hashed_context) ) |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 273 | { |
| 274 | /* This should never happen since this function is internal |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 275 | * and the code sets `ctx_hashed` correctly. |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 276 | * Let's double-check nonetheless to not run at the risk |
| 277 | * of getting a stack overflow. */ |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 278 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 279 | } |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 280 | |
| 281 | memcpy( hashed_context, ctx, clen ); |
| 282 | } |
| 283 | |
| 284 | return( mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 285 | secret, slen, |
| 286 | label, llen, |
| 287 | hashed_context, clen, |
| 288 | dstbuf, buflen ) ); |
| 289 | } |
| 290 | |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 291 | int mbedtls_ssl_tls1_3_evolve_secret( |
| 292 | mbedtls_md_type_t hash_alg, |
| 293 | const unsigned char *secret_old, |
| 294 | const unsigned char *input, size_t input_len, |
| 295 | unsigned char *secret_new ) |
| 296 | { |
| 297 | int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 298 | size_t hlen, ilen; |
Andrzej Kurek | dc4a252 | 2022-10-18 09:28:40 -0400 | [diff] [blame] | 299 | unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 }; |
| 300 | unsigned char tmp_input [ TLS1_3_EVOLVE_INPUT_SIZE ] = { 0 }; |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 301 | |
| 302 | const mbedtls_md_info_t *md; |
| 303 | md = mbedtls_md_info_from_type( hash_alg ); |
| 304 | if( md == NULL ) |
| 305 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 306 | |
| 307 | hlen = mbedtls_md_get_size( md ); |
| 308 | |
| 309 | /* For non-initial runs, call Derive-Secret( ., "derived", "") |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 310 | * on the old secret. */ |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 311 | if( secret_old != NULL ) |
| 312 | { |
| 313 | ret = mbedtls_ssl_tls1_3_derive_secret( |
| 314 | hash_alg, |
| 315 | secret_old, hlen, |
| 316 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ), |
| 317 | NULL, 0, /* context */ |
| 318 | MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 319 | tmp_secret, hlen ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 320 | if( ret != 0 ) |
| 321 | goto cleanup; |
| 322 | } |
| 323 | |
| 324 | if( input != NULL ) |
| 325 | { |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 326 | memcpy( tmp_input, input, input_len ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 327 | ilen = input_len; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | ilen = hlen; |
| 332 | } |
| 333 | |
| 334 | /* HKDF-Extract takes a salt and input key material. |
| 335 | * The salt is the old secret, and the input key material |
| 336 | * is the input secret (PSK / ECDHE). */ |
| 337 | ret = mbedtls_hkdf_extract( md, |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 338 | tmp_secret, hlen, |
| 339 | tmp_input, ilen, |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 340 | secret_new ); |
| 341 | if( ret != 0 ) |
| 342 | goto cleanup; |
| 343 | |
| 344 | ret = 0; |
| 345 | |
| 346 | cleanup: |
| 347 | |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 348 | mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) ); |
| 349 | mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 350 | return( ret ); |
| 351 | } |
| 352 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 353 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |