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 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 27 | #include "mbedtls/hkdf.h" |
| 28 | #include "mbedtls/debug.h" |
| 29 | #include "mbedtls/error.h" |
| 30 | |
| 31 | #include "ssl_misc.h" |
| 32 | #include "ssl_tls13_keys.h" |
| 33 | |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 34 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 35 | .name = string, |
| 36 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 37 | struct mbedtls_ssl_tls1_3_labels_struct const mbedtls_ssl_tls1_3_labels = |
| 38 | { |
| 39 | /* This seems to work in C, despite the string literal being one |
| 40 | * character too long due to the 0-termination. */ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 41 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 44 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 45 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 46 | /* |
| 47 | * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule. |
| 48 | * |
| 49 | * The HkdfLabel is specified in RFC 8446 as follows: |
| 50 | * |
| 51 | * struct HkdfLabel { |
| 52 | * uint16 length; // Length of expanded key material |
| 53 | * opaque label<7..255>; // Always prefixed by "tls13 " |
| 54 | * opaque context<0..255>; // Usually a communication transcript hash |
| 55 | * }; |
| 56 | * |
| 57 | * Parameters: |
| 58 | * - desired_length: Length of expanded key material |
| 59 | * Even though the standard allows expansion to up to |
| 60 | * 2**16 Bytes, TLS 1.3 never uses expansion to more than |
| 61 | * 255 Bytes, so we require `desired_length` to be at most |
| 62 | * 255. This allows us to save a few Bytes of code by |
| 63 | * hardcoding the writing of the high bytes. |
| 64 | * - (label, llen): label + label length, without "tls13 " prefix |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 65 | * The label length MUST be less than or equal to |
| 66 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN |
| 67 | * It is the caller's responsibility to ensure this. |
Hanno Becker | 815869a | 2020-09-08 11:16:16 +0100 | [diff] [blame] | 68 | * All (label, label length) pairs used in TLS 1.3 |
| 69 | * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(). |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 70 | * - (ctx, clen): context + context length |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 71 | * The context length MUST be less than or equal to |
| 72 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN |
| 73 | * It is the caller's responsibility to ensure this. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 74 | * - dst: Target buffer for HkdfLabel structure, |
| 75 | * This MUST be a writable buffer of size |
| 76 | * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes. |
| 77 | * - dlen: Pointer at which to store the actual length of |
| 78 | * the HkdfLabel structure on success. |
| 79 | */ |
| 80 | |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 81 | static const char tls1_3_label_prefix[6] = "tls13 "; |
| 82 | |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 83 | #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] | 84 | ( 2 /* expansion length */ \ |
| 85 | + 1 /* label length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 86 | + label_len \ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 87 | + 1 /* context length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 88 | + context_len ) |
| 89 | |
| 90 | #define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \ |
| 91 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \ |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 92 | sizeof(tls1_3_label_prefix) + \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 93 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \ |
| 94 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 95 | |
| 96 | static void ssl_tls1_3_hkdf_encode_label( |
| 97 | size_t desired_length, |
| 98 | const unsigned char *label, size_t llen, |
| 99 | const unsigned char *ctx, size_t clen, |
| 100 | unsigned char *dst, size_t *dlen ) |
| 101 | { |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 102 | size_t total_label_len = |
| 103 | sizeof(tls1_3_label_prefix) + llen; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 104 | size_t total_hkdf_lbl_len = |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 105 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, clen ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 106 | |
| 107 | unsigned char *p = dst; |
| 108 | |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 109 | /* Add the size of the expanded key material. |
| 110 | * We're hardcoding the high byte to 0 here assuming that we never use |
| 111 | * TLS 1.3 HKDF key expansion to more than 255 Bytes. */ |
| 112 | #if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255 |
| 113 | #error "The implementation of ssl_tls1_3_hkdf_encode_label() is not fit for the \ |
| 114 | value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN" |
| 115 | #endif |
| 116 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 117 | *p++ = 0; |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 118 | *p++ = MBEDTLS_BYTE_0( desired_length ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 119 | |
| 120 | /* Add label incl. prefix */ |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 121 | *p++ = MBEDTLS_BYTE_0( total_label_len ); |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 122 | memcpy( p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix) ); |
| 123 | p += sizeof(tls1_3_label_prefix); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 124 | memcpy( p, label, llen ); |
| 125 | p += llen; |
| 126 | |
| 127 | /* Add context value */ |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 128 | *p++ = MBEDTLS_BYTE_0( clen ); |
Hanno Becker | 00debc7 | 2020-09-08 11:12:24 +0100 | [diff] [blame] | 129 | if( clen != 0 ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 130 | memcpy( p, ctx, clen ); |
| 131 | |
| 132 | /* Return total length to the caller. */ |
| 133 | *dlen = total_hkdf_lbl_len; |
| 134 | } |
| 135 | |
| 136 | int mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 137 | mbedtls_md_type_t hash_alg, |
| 138 | const unsigned char *secret, size_t slen, |
| 139 | const unsigned char *label, size_t llen, |
| 140 | const unsigned char *ctx, size_t clen, |
| 141 | unsigned char *buf, size_t blen ) |
| 142 | { |
| 143 | const mbedtls_md_info_t *md; |
| 144 | unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ]; |
| 145 | size_t hkdf_label_len; |
| 146 | |
| 147 | if( llen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN ) |
| 148 | { |
| 149 | /* Should never happen since this is an internal |
| 150 | * function, and we know statically which labels |
| 151 | * are allowed. */ |
| 152 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 153 | } |
| 154 | |
| 155 | if( clen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
| 156 | { |
| 157 | /* Should not happen, as above. */ |
| 158 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 159 | } |
| 160 | |
| 161 | if( blen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN ) |
| 162 | { |
| 163 | /* Should not happen, as above. */ |
| 164 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 165 | } |
| 166 | |
| 167 | md = mbedtls_md_info_from_type( hash_alg ); |
| 168 | if( md == NULL ) |
| 169 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 170 | |
| 171 | ssl_tls1_3_hkdf_encode_label( blen, |
| 172 | label, llen, |
| 173 | ctx, clen, |
| 174 | hkdf_label, |
| 175 | &hkdf_label_len ); |
| 176 | |
| 177 | return( mbedtls_hkdf_expand( md, |
| 178 | secret, slen, |
| 179 | hkdf_label, hkdf_label_len, |
| 180 | buf, blen ) ); |
| 181 | } |
| 182 | |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 183 | /* |
| 184 | * The traffic keying material is generated from the following inputs: |
| 185 | * |
| 186 | * - One secret value per sender. |
| 187 | * - A purpose value indicating the specific value being generated |
| 188 | * - The desired lengths of key and IV. |
| 189 | * |
| 190 | * The expansion itself is based on HKDF: |
| 191 | * |
| 192 | * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length ) |
| 193 | * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length ) |
| 194 | * |
| 195 | * [sender] denotes the sending side and the Secret value is provided |
| 196 | * by the function caller. Note that we generate server and client side |
| 197 | * keys in a single function call. |
| 198 | */ |
| 199 | int mbedtls_ssl_tls1_3_make_traffic_keys( |
| 200 | mbedtls_md_type_t hash_alg, |
| 201 | const unsigned char *client_secret, |
| 202 | const unsigned char *server_secret, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 203 | size_t slen, size_t key_len, size_t iv_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 204 | mbedtls_ssl_key_set *keys ) |
| 205 | { |
| 206 | int ret = 0; |
| 207 | |
| 208 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 209 | client_secret, slen, |
| 210 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 211 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 212 | keys->client_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 213 | if( ret != 0 ) |
| 214 | return( ret ); |
| 215 | |
| 216 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 217 | server_secret, slen, |
| 218 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 219 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 220 | keys->server_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 221 | if( ret != 0 ) |
| 222 | return( ret ); |
| 223 | |
| 224 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 225 | client_secret, slen, |
| 226 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 227 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 228 | keys->client_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 229 | if( ret != 0 ) |
| 230 | return( ret ); |
| 231 | |
| 232 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 233 | server_secret, slen, |
| 234 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 235 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 236 | keys->server_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 237 | if( ret != 0 ) |
| 238 | return( ret ); |
| 239 | |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 240 | keys->key_len = key_len; |
| 241 | keys->iv_len = iv_len; |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 242 | |
| 243 | return( 0 ); |
| 244 | } |
| 245 | |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 246 | int mbedtls_ssl_tls1_3_derive_secret( |
| 247 | mbedtls_md_type_t hash_alg, |
| 248 | const unsigned char *secret, size_t slen, |
| 249 | const unsigned char *label, size_t llen, |
| 250 | const unsigned char *ctx, size_t clen, |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 251 | int ctx_hashed, |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 252 | unsigned char *dstbuf, size_t buflen ) |
| 253 | { |
| 254 | int ret; |
| 255 | unsigned char hashed_context[ MBEDTLS_MD_MAX_SIZE ]; |
| 256 | |
| 257 | const mbedtls_md_info_t *md; |
| 258 | md = mbedtls_md_info_from_type( hash_alg ); |
| 259 | if( md == NULL ) |
| 260 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 261 | |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 262 | if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 263 | { |
| 264 | ret = mbedtls_md( md, ctx, clen, hashed_context ); |
| 265 | if( ret != 0 ) |
| 266 | return( ret ); |
| 267 | clen = mbedtls_md_get_size( md ); |
| 268 | } |
| 269 | else |
| 270 | { |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 271 | if( clen > sizeof(hashed_context) ) |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 272 | { |
| 273 | /* This should never happen since this function is internal |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 274 | * and the code sets `ctx_hashed` correctly. |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 275 | * Let's double-check nonetheless to not run at the risk |
| 276 | * of getting a stack overflow. */ |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 277 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 278 | } |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 279 | |
| 280 | memcpy( hashed_context, ctx, clen ); |
| 281 | } |
| 282 | |
| 283 | return( mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg, |
| 284 | secret, slen, |
| 285 | label, llen, |
| 286 | hashed_context, clen, |
| 287 | dstbuf, buflen ) ); |
| 288 | } |
| 289 | |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 290 | int mbedtls_ssl_tls1_3_evolve_secret( |
| 291 | mbedtls_md_type_t hash_alg, |
| 292 | const unsigned char *secret_old, |
| 293 | const unsigned char *input, size_t input_len, |
| 294 | unsigned char *secret_new ) |
| 295 | { |
| 296 | int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 297 | size_t hlen, ilen; |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 298 | unsigned char tmp_secret[ MBEDTLS_MD_MAX_SIZE ] = { 0 }; |
| 299 | unsigned char tmp_input [ MBEDTLS_MD_MAX_SIZE ] = { 0 }; |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 300 | |
| 301 | const mbedtls_md_info_t *md; |
| 302 | md = mbedtls_md_info_from_type( hash_alg ); |
| 303 | if( md == NULL ) |
| 304 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 305 | |
| 306 | hlen = mbedtls_md_get_size( md ); |
| 307 | |
| 308 | /* For non-initial runs, call Derive-Secret( ., "derived", "") |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 309 | * on the old secret. */ |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 310 | if( secret_old != NULL ) |
| 311 | { |
| 312 | ret = mbedtls_ssl_tls1_3_derive_secret( |
| 313 | hash_alg, |
| 314 | secret_old, hlen, |
| 315 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ), |
| 316 | NULL, 0, /* context */ |
| 317 | MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 318 | tmp_secret, hlen ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 319 | if( ret != 0 ) |
| 320 | goto cleanup; |
| 321 | } |
| 322 | |
| 323 | if( input != NULL ) |
| 324 | { |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 325 | memcpy( tmp_input, input, input_len ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 326 | ilen = input_len; |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | ilen = hlen; |
| 331 | } |
| 332 | |
| 333 | /* HKDF-Extract takes a salt and input key material. |
| 334 | * The salt is the old secret, and the input key material |
| 335 | * is the input secret (PSK / ECDHE). */ |
| 336 | ret = mbedtls_hkdf_extract( md, |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 337 | tmp_secret, hlen, |
| 338 | tmp_input, ilen, |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 339 | secret_new ); |
| 340 | if( ret != 0 ) |
| 341 | goto cleanup; |
| 342 | |
| 343 | ret = 0; |
| 344 | |
| 345 | cleanup: |
| 346 | |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 347 | mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) ); |
| 348 | mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 349 | return( ret ); |
| 350 | } |
| 351 | |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 352 | int mbedtls_ssl_tls1_3_derive_early_secrets( |
| 353 | mbedtls_md_type_t md_type, |
| 354 | unsigned char const *early_secret, |
| 355 | unsigned char const *transcript, size_t transcript_len, |
| 356 | mbedtls_ssl_tls1_3_early_secrets *derived ) |
| 357 | { |
| 358 | int ret; |
| 359 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 360 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 361 | |
| 362 | /* We should never call this function with an unknown hash, |
| 363 | * but add an assertion anyway. */ |
| 364 | if( md_info == 0 ) |
| 365 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 366 | |
| 367 | /* |
| 368 | * 0 |
| 369 | * | |
| 370 | * v |
| 371 | * PSK -> HKDF-Extract = Early Secret |
| 372 | * | |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 373 | * +-----> Derive-Secret(., "c e traffic", ClientHello) |
| 374 | * | = client_early_traffic_secret |
| 375 | * | |
| 376 | * +-----> Derive-Secret(., "e exp master", ClientHello) |
| 377 | * | = early_exporter_master_secret |
| 378 | * v |
| 379 | */ |
| 380 | |
| 381 | /* Create client_early_traffic_secret */ |
| 382 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 383 | early_secret, md_size, |
| 384 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ), |
| 385 | transcript, transcript_len, |
| 386 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 387 | derived->client_early_traffic_secret, |
| 388 | md_size ); |
| 389 | if( ret != 0 ) |
| 390 | return( ret ); |
| 391 | |
| 392 | /* Create early exporter */ |
| 393 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 394 | early_secret, md_size, |
| 395 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ), |
| 396 | transcript, transcript_len, |
| 397 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 398 | derived->early_exporter_master_secret, |
| 399 | md_size ); |
| 400 | if( ret != 0 ) |
| 401 | return( ret ); |
| 402 | |
| 403 | return( 0 ); |
| 404 | } |
| 405 | |
| 406 | int mbedtls_ssl_tls1_3_derive_handshake_secrets( |
| 407 | mbedtls_md_type_t md_type, |
| 408 | unsigned char const *handshake_secret, |
| 409 | unsigned char const *transcript, size_t transcript_len, |
| 410 | mbedtls_ssl_tls1_3_handshake_secrets *derived ) |
| 411 | { |
| 412 | int ret; |
| 413 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 414 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 415 | |
| 416 | /* We should never call this function with an unknown hash, |
| 417 | * but add an assertion anyway. */ |
| 418 | if( md_info == 0 ) |
| 419 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 420 | |
| 421 | /* |
| 422 | * |
| 423 | * Handshake Secret |
| 424 | * | |
| 425 | * +-----> Derive-Secret( ., "c hs traffic", |
| 426 | * | ClientHello...ServerHello ) |
| 427 | * | = client_handshake_traffic_secret |
| 428 | * | |
| 429 | * +-----> Derive-Secret( ., "s hs traffic", |
| 430 | * | ClientHello...ServerHello ) |
| 431 | * | = server_handshake_traffic_secret |
| 432 | * |
| 433 | */ |
| 434 | |
| 435 | /* |
| 436 | * Compute client_handshake_traffic_secret with |
| 437 | * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello ) |
| 438 | */ |
| 439 | |
| 440 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 441 | handshake_secret, md_size, |
| 442 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ), |
| 443 | transcript, transcript_len, |
| 444 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 445 | derived->client_handshake_traffic_secret, |
| 446 | md_size ); |
| 447 | if( ret != 0 ) |
| 448 | return( ret ); |
| 449 | |
| 450 | /* |
| 451 | * Compute server_handshake_traffic_secret with |
| 452 | * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello ) |
| 453 | */ |
| 454 | |
| 455 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 456 | handshake_secret, md_size, |
| 457 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ), |
| 458 | transcript, transcript_len, |
| 459 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 460 | derived->server_handshake_traffic_secret, |
| 461 | md_size ); |
| 462 | if( ret != 0 ) |
| 463 | return( ret ); |
| 464 | |
| 465 | return( 0 ); |
| 466 | } |
| 467 | |
| 468 | int mbedtls_ssl_tls1_3_derive_application_secrets( |
| 469 | mbedtls_md_type_t md_type, |
| 470 | unsigned char const *application_secret, |
| 471 | unsigned char const *transcript, size_t transcript_len, |
| 472 | mbedtls_ssl_tls1_3_application_secrets *derived ) |
| 473 | { |
| 474 | int ret; |
| 475 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 476 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 477 | |
| 478 | /* We should never call this function with an unknown hash, |
| 479 | * but add an assertion anyway. */ |
| 480 | if( md_info == 0 ) |
| 481 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 482 | |
| 483 | /* Generate {client,server}_application_traffic_secret_0 |
| 484 | * |
| 485 | * Master Secret |
| 486 | * | |
| 487 | * +-----> Derive-Secret( ., "c ap traffic", |
| 488 | * | ClientHello...server Finished ) |
| 489 | * | = client_application_traffic_secret_0 |
| 490 | * | |
| 491 | * +-----> Derive-Secret( ., "s ap traffic", |
| 492 | * | ClientHello...Server Finished ) |
| 493 | * | = server_application_traffic_secret_0 |
| 494 | * | |
| 495 | * +-----> Derive-Secret( ., "exp master", |
| 496 | * | ClientHello...server Finished) |
| 497 | * | = exporter_master_secret |
| 498 | * |
| 499 | */ |
| 500 | |
| 501 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 502 | application_secret, md_size, |
| 503 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ), |
| 504 | transcript, transcript_len, |
| 505 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 506 | derived->client_application_traffic_secret_N, |
| 507 | md_size ); |
| 508 | if( ret != 0 ) |
| 509 | return( ret ); |
| 510 | |
| 511 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 512 | application_secret, md_size, |
| 513 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ), |
| 514 | transcript, transcript_len, |
| 515 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 516 | derived->server_application_traffic_secret_N, |
| 517 | md_size ); |
| 518 | if( ret != 0 ) |
| 519 | return( ret ); |
| 520 | |
| 521 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 522 | application_secret, md_size, |
| 523 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ), |
| 524 | transcript, transcript_len, |
| 525 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 526 | derived->exporter_master_secret, |
| 527 | md_size ); |
| 528 | if( ret != 0 ) |
| 529 | return( ret ); |
| 530 | |
| 531 | return( 0 ); |
| 532 | } |
| 533 | |
| 534 | /* Generate resumption_master_secret for use with the ticket exchange. |
| 535 | * |
| 536 | * This is not integrated with mbedtls_ssl_tls1_3_derive_application_secrets() |
| 537 | * because it uses the transcript hash up to and including ClientFinished. */ |
| 538 | int mbedtls_ssl_tls1_3_derive_resumption_master_secret( |
| 539 | mbedtls_md_type_t md_type, |
| 540 | unsigned char const *application_secret, |
| 541 | unsigned char const *transcript, size_t transcript_len, |
| 542 | mbedtls_ssl_tls1_3_application_secrets *derived ) |
| 543 | { |
| 544 | int ret; |
| 545 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 546 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 547 | |
| 548 | /* We should never call this function with an unknown hash, |
| 549 | * but add an assertion anyway. */ |
| 550 | if( md_info == 0 ) |
| 551 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 552 | |
| 553 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 554 | application_secret, md_size, |
| 555 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ), |
| 556 | transcript, transcript_len, |
| 557 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 558 | derived->resumption_master_secret, |
| 559 | md_size ); |
| 560 | |
| 561 | if( ret != 0 ) |
| 562 | return( ret ); |
| 563 | |
| 564 | return( 0 ); |
| 565 | } |
| 566 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 567 | static int ssl_tls1_3_calc_finished_core( mbedtls_md_type_t md_type, |
| 568 | unsigned char const *base_key, |
| 569 | unsigned char const *transcript, |
| 570 | unsigned char *dst ) |
| 571 | { |
| 572 | const mbedtls_md_info_t* const md_info = mbedtls_md_info_from_type( md_type ); |
| 573 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 574 | unsigned char finished_key[MBEDTLS_MD_MAX_SIZE]; |
| 575 | int ret; |
| 576 | |
| 577 | /* We should never call this function with an unknown hash, |
| 578 | * but add an assertion anyway. */ |
| 579 | if( md_info == 0 ) |
| 580 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 581 | |
| 582 | /* TLS 1.3 Finished message |
| 583 | * |
| 584 | * struct { |
| 585 | * opaque verify_data[Hash.length]; |
| 586 | * } Finished; |
| 587 | * |
| 588 | * verify_data = |
| 589 | * HMAC( finished_key, |
| 590 | * Hash( Handshake Context + |
| 591 | * Certificate* + |
| 592 | * CertificateVerify* ) |
| 593 | * ) |
| 594 | * |
| 595 | * finished_key = |
| 596 | * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length ) |
| 597 | */ |
| 598 | |
| 599 | ret = mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 600 | md_type, base_key, md_size, |
| 601 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ), |
| 602 | NULL, 0, |
| 603 | finished_key, md_size ); |
| 604 | if( ret != 0 ) |
| 605 | goto exit; |
| 606 | |
| 607 | ret = mbedtls_md_hmac( md_info, finished_key, md_size, transcript, md_size, dst ); |
| 608 | if( ret != 0 ) |
| 609 | goto exit; |
| 610 | |
| 611 | exit: |
| 612 | |
| 613 | mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) ); |
| 614 | return( ret ); |
| 615 | } |
| 616 | |
| 617 | int mbedtls_ssl_tls1_3_create_psk_binder( mbedtls_ssl_context *ssl, |
| 618 | const mbedtls_md_type_t md_type, |
| 619 | unsigned char const *psk, size_t psk_len, |
| 620 | int psk_type, |
| 621 | unsigned char const *transcript, |
| 622 | unsigned char *result ) |
| 623 | { |
| 624 | int ret = 0; |
| 625 | unsigned char binder_key[MBEDTLS_MD_MAX_SIZE]; |
| 626 | unsigned char early_secret[MBEDTLS_MD_MAX_SIZE]; |
| 627 | mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type( md_type ); |
| 628 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 629 | |
Hanno Becker | 28e5f1e | 2021-05-26 09:29:49 +0100 | [diff] [blame] | 630 | #if !defined(MBEDTLS_DEBUG_C) |
| 631 | ssl = NULL; /* make sure we don't use it except for debug */ |
| 632 | ((void) ssl); |
| 633 | #endif |
| 634 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 635 | /* We should never call this function with an unknown hash, |
| 636 | * but add an assertion anyway. */ |
| 637 | if( md_info == 0 ) |
| 638 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 639 | |
| 640 | /* |
| 641 | * 0 |
| 642 | * | |
| 643 | * v |
| 644 | * PSK -> HKDF-Extract = Early Secret |
| 645 | * | |
| 646 | * +-----> Derive-Secret(., "ext binder" | "res binder", "") |
| 647 | * | = binder_key |
| 648 | * v |
| 649 | */ |
| 650 | |
| 651 | ret = mbedtls_ssl_tls1_3_evolve_secret( md_type, |
| 652 | NULL, /* Old secret */ |
| 653 | psk, psk_len, /* Input */ |
| 654 | early_secret ); |
| 655 | if( ret != 0 ) |
| 656 | { |
| 657 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_evolve_secret", ret ); |
| 658 | goto exit; |
| 659 | } |
| 660 | |
| 661 | if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION ) |
| 662 | { |
| 663 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 664 | early_secret, md_size, |
| 665 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ), |
| 666 | NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
| 667 | binder_key, md_size ); |
| 668 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) ); |
| 669 | } |
| 670 | else |
| 671 | { |
| 672 | ret = mbedtls_ssl_tls1_3_derive_secret( md_type, |
| 673 | early_secret, md_size, |
| 674 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ), |
| 675 | NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
| 676 | binder_key, md_size ); |
| 677 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) ); |
| 678 | } |
| 679 | |
| 680 | if( ret != 0 ) |
| 681 | { |
| 682 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_derive_secret", ret ); |
| 683 | goto exit; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * The binding_value is computed in the same way as the Finished message |
| 688 | * but with the BaseKey being the binder_key. |
| 689 | */ |
| 690 | |
| 691 | ret = ssl_tls1_3_calc_finished_core( md_type, binder_key, transcript, result ); |
| 692 | if( ret != 0 ) |
| 693 | goto exit; |
| 694 | |
| 695 | MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, md_size ); |
| 696 | |
| 697 | exit: |
| 698 | |
| 699 | mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) ); |
| 700 | mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) ); |
| 701 | return( ret ); |
| 702 | } |
| 703 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 704 | int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform, |
| 705 | int endpoint, |
| 706 | int ciphersuite, |
| 707 | mbedtls_ssl_key_set const *traffic_keys, |
| 708 | mbedtls_ssl_context *ssl /* DEBUG ONLY */ ) |
| 709 | { |
| 710 | int ret; |
| 711 | mbedtls_cipher_info_t const *cipher_info; |
| 712 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 713 | unsigned char const *key_enc; |
| 714 | unsigned char const *iv_enc; |
| 715 | unsigned char const *key_dec; |
| 716 | unsigned char const *iv_dec; |
| 717 | |
| 718 | #if !defined(MBEDTLS_DEBUG_C) |
| 719 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 720 | (void) ssl; |
| 721 | #endif |
| 722 | |
| 723 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Hanno Becker | 7887a77 | 2021-04-20 05:27:57 +0100 | [diff] [blame] | 724 | if( ciphersuite_info == NULL ) |
| 725 | { |
| 726 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 727 | ciphersuite ) ); |
| 728 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 729 | } |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 730 | |
| 731 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 732 | if( cipher_info == NULL ) |
| 733 | { |
Hanno Becker | 7887a77 | 2021-04-20 05:27:57 +0100 | [diff] [blame] | 734 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 735 | ciphersuite_info->cipher ) ); |
Hanno Becker | f62a730 | 2021-04-21 05:21:28 +0100 | [diff] [blame] | 736 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Setup cipher contexts in target transform |
| 741 | */ |
| 742 | |
| 743 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 744 | cipher_info ) ) != 0 ) |
| 745 | { |
| 746 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 747 | return( ret ); |
| 748 | } |
| 749 | |
| 750 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 751 | cipher_info ) ) != 0 ) |
| 752 | { |
| 753 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 754 | return( ret ); |
| 755 | } |
| 756 | |
| 757 | #if defined(MBEDTLS_SSL_SRV_C) |
| 758 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 759 | { |
| 760 | key_enc = traffic_keys->server_write_key; |
| 761 | key_dec = traffic_keys->client_write_key; |
| 762 | iv_enc = traffic_keys->server_write_iv; |
| 763 | iv_dec = traffic_keys->client_write_iv; |
| 764 | } |
| 765 | else |
| 766 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 767 | #if defined(MBEDTLS_SSL_CLI_C) |
| 768 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 769 | { |
| 770 | key_enc = traffic_keys->client_write_key; |
| 771 | key_dec = traffic_keys->server_write_key; |
| 772 | iv_enc = traffic_keys->client_write_iv; |
| 773 | iv_dec = traffic_keys->server_write_iv; |
| 774 | } |
| 775 | else |
| 776 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 777 | { |
| 778 | /* should not happen */ |
| 779 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 780 | } |
| 781 | |
| 782 | memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len ); |
| 783 | memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len ); |
| 784 | |
| 785 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, |
| 786 | key_enc, cipher_info->key_bitlen, |
| 787 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 788 | { |
| 789 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 790 | return( ret ); |
| 791 | } |
| 792 | |
| 793 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, |
| 794 | key_dec, cipher_info->key_bitlen, |
| 795 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 796 | { |
| 797 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 798 | return( ret ); |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Setup other fields in SSL transform |
| 803 | */ |
| 804 | |
| 805 | if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 ) |
| 806 | transform->taglen = 8; |
| 807 | else |
| 808 | transform->taglen = 16; |
| 809 | |
| 810 | transform->ivlen = traffic_keys->iv_len; |
| 811 | transform->maclen = 0; |
| 812 | transform->fixed_ivlen = transform->ivlen; |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 813 | transform->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4; |
| 814 | |
Hanno Becker | edd5bf0 | 2021-04-20 05:32:16 +0100 | [diff] [blame] | 815 | /* We add the true record content type (1 Byte) to the plaintext and |
| 816 | * then pad to the configured granularity. The mimimum length of the |
| 817 | * type-extended and padded plaintext is therefore the padding |
| 818 | * granularity. */ |
| 819 | transform->minlen = |
Hanno Becker | dfba065 | 2021-08-01 19:16:57 +0100 | [diff] [blame] | 820 | transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
Hanno Becker | edd5bf0 | 2021-04-20 05:32:16 +0100 | [diff] [blame] | 821 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 822 | return( 0 ); |
| 823 | } |
| 824 | |
Jerry Yu | 4836952 | 2021-09-18 16:09:01 +0800 | [diff] [blame] | 825 | int mbedtls_ssl_tls1_3_key_schedule_stage_early( mbedtls_ssl_context *ssl ) |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 826 | { |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 827 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 828 | mbedtls_md_type_t md_type; |
| 829 | const unsigned char *input = NULL; |
| 830 | size_t input_len = 0; |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 831 | if( ssl->handshake->ciphersuite_info == NULL ) |
| 832 | { |
| 833 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) ); |
| 834 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 835 | } |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 836 | |
| 837 | md_type = ssl->handshake->ciphersuite_info->mac; |
Jerry Yu | e06f453 | 2021-09-23 18:35:07 +0800 | [diff] [blame^] | 838 | |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 839 | ret = mbedtls_ssl_tls1_3_evolve_secret( md_type, NULL, input, input_len, |
Jerry Yu | 4836952 | 2021-09-18 16:09:01 +0800 | [diff] [blame] | 840 | ssl->handshake->tls1_3_master_secrets.early ); |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 841 | if( ret != 0 ) |
| 842 | { |
| 843 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_evolve_secret", ret ); |
| 844 | return( ret ); |
| 845 | } |
| 846 | |
| 847 | return( 0 ); |
| 848 | } |
| 849 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 850 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |