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 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 22 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 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" |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 33 | #include "ssl_tls13_invasive.h" |
| 34 | |
| 35 | #include "psa/crypto.h" |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 36 | |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 37 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 38 | .name = string, |
| 39 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 40 | struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels = |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 41 | { |
| 42 | /* This seems to work in C, despite the string literal being one |
| 43 | * character too long due to the 0-termination. */ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 44 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 47 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 48 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 49 | /* |
| 50 | * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule. |
| 51 | * |
| 52 | * The HkdfLabel is specified in RFC 8446 as follows: |
| 53 | * |
| 54 | * struct HkdfLabel { |
| 55 | * uint16 length; // Length of expanded key material |
| 56 | * opaque label<7..255>; // Always prefixed by "tls13 " |
| 57 | * opaque context<0..255>; // Usually a communication transcript hash |
| 58 | * }; |
| 59 | * |
| 60 | * Parameters: |
| 61 | * - desired_length: Length of expanded key material |
| 62 | * Even though the standard allows expansion to up to |
| 63 | * 2**16 Bytes, TLS 1.3 never uses expansion to more than |
| 64 | * 255 Bytes, so we require `desired_length` to be at most |
| 65 | * 255. This allows us to save a few Bytes of code by |
| 66 | * hardcoding the writing of the high bytes. |
Xiaofei Bai | feecbbb | 2021-11-23 07:24:58 +0000 | [diff] [blame] | 67 | * - (label, label_len): label + label length, without "tls13 " prefix |
| 68 | * The label length MUST be less than or equal to |
| 69 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN |
| 70 | * It is the caller's responsibility to ensure this. |
| 71 | * All (label, label length) pairs used in TLS 1.3 |
| 72 | * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(). |
| 73 | * - (ctx, ctx_len): context + context length |
| 74 | * The context length MUST be less than or equal to |
| 75 | * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN |
| 76 | * It is the caller's responsibility to ensure this. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 77 | * - dst: Target buffer for HkdfLabel structure, |
| 78 | * This MUST be a writable buffer of size |
| 79 | * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes. |
Xiaofei Bai | feecbbb | 2021-11-23 07:24:58 +0000 | [diff] [blame] | 80 | * - dst_len: Pointer at which to store the actual length of |
| 81 | * the HkdfLabel structure on success. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 82 | */ |
| 83 | |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 84 | static const char tls13_label_prefix[6] = "tls13 "; |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 85 | |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 86 | #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] | 87 | ( 2 /* expansion length */ \ |
| 88 | + 1 /* label length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 89 | + label_len \ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 90 | + 1 /* context length */ \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 91 | + context_len ) |
| 92 | |
| 93 | #define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \ |
| 94 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 95 | sizeof(tls13_label_prefix) + \ |
Hanno Becker | 9cb0a14 | 2020-09-08 10:48:14 +0100 | [diff] [blame] | 96 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \ |
| 97 | MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 98 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 99 | static void ssl_tls13_hkdf_encode_label( |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 100 | size_t desired_length, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 101 | const unsigned char *label, size_t label_len, |
| 102 | const unsigned char *ctx, size_t ctx_len, |
| 103 | unsigned char *dst, size_t *dst_len ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 104 | { |
Hanno Becker | 2dfe132 | 2020-09-10 09:23:12 +0100 | [diff] [blame] | 105 | size_t total_label_len = |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 106 | sizeof(tls13_label_prefix) + label_len; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 107 | size_t total_hkdf_lbl_len = |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 108 | SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, ctx_len ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 109 | |
| 110 | unsigned char *p = dst; |
| 111 | |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 112 | /* Add the size of the expanded key material. |
| 113 | * We're hardcoding the high byte to 0 here assuming that we never use |
| 114 | * TLS 1.3 HKDF key expansion to more than 255 Bytes. */ |
| 115 | #if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255 |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 116 | #error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \ |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 117 | value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN" |
| 118 | #endif |
| 119 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 120 | *p++ = 0; |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 121 | *p++ = MBEDTLS_BYTE_0( desired_length ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 122 | |
| 123 | /* Add label incl. prefix */ |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 124 | *p++ = MBEDTLS_BYTE_0( total_label_len ); |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 125 | memcpy( p, tls13_label_prefix, sizeof(tls13_label_prefix) ); |
| 126 | p += sizeof(tls13_label_prefix); |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 127 | memcpy( p, label, label_len ); |
| 128 | p += label_len; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 129 | |
| 130 | /* Add context value */ |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 131 | *p++ = MBEDTLS_BYTE_0( ctx_len ); |
| 132 | if( ctx_len != 0 ) |
| 133 | memcpy( p, ctx, ctx_len ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 134 | |
| 135 | /* Return total length to the caller. */ |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 136 | *dst_len = total_hkdf_lbl_len; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 139 | MBEDTLS_STATIC_TESTABLE |
Gabor Mezei | 62bf024 | 2022-02-07 18:12:07 +0100 | [diff] [blame] | 140 | psa_status_t mbedtls_psa_hkdf_extract( psa_algorithm_t alg, |
| 141 | const unsigned char *salt, size_t salt_len, |
| 142 | const unsigned char *ikm, size_t ikm_len, |
| 143 | unsigned char *prk, size_t prk_size, |
| 144 | size_t *prk_len ) |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 145 | { |
| 146 | unsigned char null_salt[PSA_MAC_MAX_SIZE] = { '\0' }; |
| 147 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 148 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gabor Mezei | 26c6741 | 2022-02-15 15:46:17 +0100 | [diff] [blame] | 149 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 0e7c6f4 | 2022-02-15 15:47:54 +0100 | [diff] [blame] | 150 | psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 151 | |
| 152 | if( salt == NULL || salt_len == 0 ) |
| 153 | { |
| 154 | size_t hash_len; |
| 155 | |
| 156 | if( salt_len != 0 ) |
| 157 | { |
Gabor Mezei | c5efb8e | 2022-02-08 13:15:45 +0100 | [diff] [blame] | 158 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 161 | hash_len = PSA_HASH_LENGTH( alg ); |
| 162 | |
| 163 | if( hash_len == 0 ) |
| 164 | { |
Gabor Mezei | c5efb8e | 2022-02-08 13:15:45 +0100 | [diff] [blame] | 165 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Gabor Mezei | d860e0f | 2022-02-15 16:02:59 +0100 | [diff] [blame] | 168 | /* salt_len <= sizeof( salt ) because |
| 169 | PSA_HASH_LENGTH( alg ) <= PSA_MAC_MAX_SIZE. */ |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 170 | salt = null_salt; |
| 171 | salt_len = hash_len; |
| 172 | } |
| 173 | |
| 174 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 175 | psa_set_key_algorithm( &attributes, alg ); |
| 176 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 177 | |
Gabor Mezei | 26c6741 | 2022-02-15 15:46:17 +0100 | [diff] [blame] | 178 | status = psa_import_key( &attributes, salt, salt_len, &key ); |
| 179 | if( status != PSA_SUCCESS ) |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 180 | { |
| 181 | goto cleanup; |
| 182 | } |
| 183 | |
Gabor Mezei | 26c6741 | 2022-02-15 15:46:17 +0100 | [diff] [blame] | 184 | status = psa_mac_compute( key, alg, ikm, ikm_len, prk, prk_size, prk_len ); |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 185 | |
| 186 | cleanup: |
Gabor Mezei | 0e7c6f4 | 2022-02-15 15:47:54 +0100 | [diff] [blame] | 187 | destroy_status = psa_destroy_key( key ); |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 188 | |
Gabor Mezei | 0e7c6f4 | 2022-02-15 15:47:54 +0100 | [diff] [blame] | 189 | return( ( status == PSA_SUCCESS ) ? destroy_status : status ); |
Gabor Mezei | 9f4bb31 | 2022-01-31 16:33:47 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | MBEDTLS_STATIC_TESTABLE |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 193 | psa_status_t mbedtls_psa_hkdf_expand( psa_algorithm_t alg, |
| 194 | const unsigned char *prk, size_t prk_len, |
| 195 | const unsigned char *info, size_t info_len, |
| 196 | unsigned char *okm, size_t okm_len ) |
| 197 | { |
| 198 | size_t hash_len; |
| 199 | size_t where = 0; |
| 200 | size_t n; |
| 201 | size_t t_len = 0; |
| 202 | size_t i; |
| 203 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 204 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 205 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 206 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 8d5a4cb | 2022-02-15 16:23:17 +0100 | [diff] [blame] | 207 | psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 208 | unsigned char t[PSA_MAC_MAX_SIZE]; |
| 209 | |
| 210 | if( okm == NULL ) |
| 211 | { |
| 212 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 213 | } |
| 214 | |
| 215 | hash_len = PSA_HASH_LENGTH( alg ); |
| 216 | |
| 217 | if( prk_len < hash_len || hash_len == 0 ) |
| 218 | { |
| 219 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 220 | } |
| 221 | |
| 222 | if( info == NULL ) |
| 223 | { |
| 224 | info = (const unsigned char *) ""; |
| 225 | info_len = 0; |
| 226 | } |
| 227 | |
| 228 | n = okm_len / hash_len; |
| 229 | |
| 230 | if( okm_len % hash_len != 0 ) |
| 231 | { |
| 232 | n++; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Per RFC 5869 Section 2.3, okm_len must not exceed |
| 237 | * 255 times the hash length |
| 238 | */ |
| 239 | if( n > 255 ) |
| 240 | { |
| 241 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 242 | } |
| 243 | |
| 244 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 245 | psa_set_key_algorithm( &attributes, alg ); |
| 246 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 247 | |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 248 | status = psa_import_key( &attributes, prk, prk_len, &key ); |
| 249 | if( status != PSA_SUCCESS ) |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 250 | { |
| 251 | goto cleanup; |
| 252 | } |
| 253 | |
| 254 | memset( t, 0, hash_len ); |
| 255 | |
| 256 | /* |
| 257 | * Compute T = T(1) | T(2) | T(3) | ... | T(N) |
| 258 | * Where T(N) is defined in RFC 5869 Section 2.3 |
| 259 | */ |
| 260 | for( i = 1; i <= n; i++ ) |
| 261 | { |
| 262 | size_t num_to_copy; |
| 263 | unsigned char c = i & 0xff; |
| 264 | size_t len; |
| 265 | |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 266 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 267 | if( status != PSA_SUCCESS ) |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 268 | { |
| 269 | goto cleanup; |
| 270 | } |
| 271 | |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 272 | status = psa_mac_update( &operation, t, t_len ); |
| 273 | if( status != PSA_SUCCESS ) |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 274 | { |
| 275 | goto cleanup; |
| 276 | } |
| 277 | |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 278 | status = psa_mac_update( &operation, info, info_len ); |
| 279 | if( status != PSA_SUCCESS ) |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 280 | { |
| 281 | goto cleanup; |
| 282 | } |
| 283 | |
Gabor Mezei | 8e36025 | 2022-02-17 11:50:02 +0100 | [diff] [blame] | 284 | /* The constant concatenated to the end of each T(n) is a single octet. */ |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 285 | status = psa_mac_update( &operation, &c, 1 ); |
| 286 | if( status != PSA_SUCCESS ) |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 287 | { |
| 288 | goto cleanup; |
| 289 | } |
| 290 | |
Gabor Mezei | 833713c | 2022-02-15 16:16:08 +0100 | [diff] [blame] | 291 | status = psa_mac_sign_finish( &operation, t, PSA_MAC_MAX_SIZE, &len ); |
| 292 | if( status != PSA_SUCCESS ) |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 293 | { |
| 294 | goto cleanup; |
| 295 | } |
| 296 | |
| 297 | num_to_copy = i != n ? hash_len : okm_len - where; |
| 298 | memcpy( okm + where, t, num_to_copy ); |
| 299 | where += hash_len; |
| 300 | t_len = hash_len; |
| 301 | } |
| 302 | |
| 303 | cleanup: |
Gabor Mezei | 8d5a4cb | 2022-02-15 16:23:17 +0100 | [diff] [blame] | 304 | if( status != PSA_SUCCESS ) |
| 305 | psa_mac_abort( &operation ); |
| 306 | destroy_status = psa_destroy_key( key ); |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 307 | |
Gabor Mezei | 8d5a4cb | 2022-02-15 16:23:17 +0100 | [diff] [blame] | 308 | mbedtls_platform_zeroize( t, sizeof( t ) ); |
| 309 | |
| 310 | return( ( status == PSA_SUCCESS ) ? destroy_status : status ); |
Gabor Mezei | a3eecd2 | 2022-02-09 16:57:26 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 313 | int mbedtls_ssl_tls13_hkdf_expand_label( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 314 | psa_algorithm_t hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 315 | const unsigned char *secret, size_t secret_len, |
| 316 | const unsigned char *label, size_t label_len, |
| 317 | const unsigned char *ctx, size_t ctx_len, |
| 318 | unsigned char *buf, size_t buf_len ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 319 | { |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 320 | unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ]; |
| 321 | size_t hkdf_label_len; |
| 322 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 323 | if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 324 | { |
| 325 | /* Should never happen since this is an internal |
| 326 | * function, and we know statically which labels |
| 327 | * are allowed. */ |
| 328 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 329 | } |
| 330 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 331 | if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 332 | { |
| 333 | /* Should not happen, as above. */ |
| 334 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 335 | } |
| 336 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 337 | if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN ) |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 338 | { |
| 339 | /* Should not happen, as above. */ |
| 340 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 341 | } |
| 342 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 343 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 344 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 345 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 346 | ssl_tls13_hkdf_encode_label( buf_len, |
| 347 | label, label_len, |
| 348 | ctx, ctx_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 349 | hkdf_label, |
| 350 | &hkdf_label_len ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 351 | |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 352 | return( psa_ssl_status_to_mbedtls( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 353 | mbedtls_psa_hkdf_expand( PSA_ALG_HMAC( hash_alg ), |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 354 | secret, secret_len, |
| 355 | hkdf_label, hkdf_label_len, |
| 356 | buf, buf_len ) ) ); |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 359 | /* |
| 360 | * The traffic keying material is generated from the following inputs: |
| 361 | * |
| 362 | * - One secret value per sender. |
| 363 | * - A purpose value indicating the specific value being generated |
| 364 | * - The desired lengths of key and IV. |
| 365 | * |
| 366 | * The expansion itself is based on HKDF: |
| 367 | * |
| 368 | * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length ) |
| 369 | * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length ) |
| 370 | * |
| 371 | * [sender] denotes the sending side and the Secret value is provided |
| 372 | * by the function caller. Note that we generate server and client side |
| 373 | * keys in a single function call. |
| 374 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 375 | int mbedtls_ssl_tls13_make_traffic_keys( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 376 | psa_algorithm_t hash_alg, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 377 | const unsigned char *client_secret, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 378 | const unsigned char *server_secret, size_t secret_len, |
| 379 | size_t key_len, size_t iv_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 380 | mbedtls_ssl_key_set *keys ) |
| 381 | { |
| 382 | int ret = 0; |
| 383 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 384 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 385 | client_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 386 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 387 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 388 | keys->client_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 389 | if( ret != 0 ) |
| 390 | return( ret ); |
| 391 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 392 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 393 | server_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 394 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ), |
| 395 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 396 | keys->server_write_key, key_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 397 | if( ret != 0 ) |
| 398 | return( ret ); |
| 399 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 400 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 401 | client_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 402 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 403 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 404 | keys->client_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 405 | if( ret != 0 ) |
| 406 | return( ret ); |
| 407 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 408 | ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 409 | server_secret, secret_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 410 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ), |
| 411 | NULL, 0, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 412 | keys->server_write_iv, iv_len ); |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 413 | if( ret != 0 ) |
| 414 | return( ret ); |
| 415 | |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 416 | keys->key_len = key_len; |
| 417 | keys->iv_len = iv_len; |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 418 | |
| 419 | return( 0 ); |
| 420 | } |
| 421 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 422 | int mbedtls_ssl_tls13_derive_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 423 | psa_algorithm_t hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 424 | const unsigned char *secret, size_t secret_len, |
| 425 | const unsigned char *label, size_t label_len, |
| 426 | const unsigned char *ctx, size_t ctx_len, |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 427 | int ctx_hashed, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 428 | unsigned char *dstbuf, size_t dstbuf_len ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 429 | { |
| 430 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 431 | unsigned char hashed_context[ PSA_HASH_MAX_SIZE ]; |
| 432 | /* |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 433 | const mbedtls_md_info_t *md_info; |
| 434 | md_info = mbedtls_md_info_from_type( hash_alg ); |
| 435 | if( md_info == NULL ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 436 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 437 | */ |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 438 | if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED ) |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 439 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 440 | /* |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 441 | ret = mbedtls_md( md_info, ctx, ctx_len, hashed_context ); |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 442 | if( ret != 0 ) |
| 443 | return( ret ); |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 444 | ctx_len = mbedtls_md_get_size( md_info ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 445 | */ |
| 446 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 447 | |
| 448 | status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context, |
| 449 | PSA_HASH_LENGTH( hash_alg ), &ctx_len ); |
| 450 | if( status != PSA_SUCCESS ) |
| 451 | { |
| 452 | ret = psa_ssl_status_to_mbedtls( status ); |
| 453 | return ret; |
| 454 | } |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 455 | } |
| 456 | else |
| 457 | { |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 458 | if( ctx_len > sizeof(hashed_context) ) |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 459 | { |
| 460 | /* This should never happen since this function is internal |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 461 | * and the code sets `ctx_hashed` correctly. |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 462 | * Let's double-check nonetheless to not run at the risk |
| 463 | * of getting a stack overflow. */ |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 464 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Hanno Becker | 97a2156 | 2020-09-09 12:57:16 +0100 | [diff] [blame] | 465 | } |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 466 | |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 467 | memcpy( hashed_context, ctx, ctx_len ); |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 470 | return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg, |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 471 | secret, secret_len, |
| 472 | label, label_len, |
| 473 | hashed_context, ctx_len, |
| 474 | dstbuf, dstbuf_len ) ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 475 | |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 478 | int mbedtls_ssl_tls13_evolve_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 479 | psa_algorithm_t hash_alg, |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 480 | const unsigned char *secret_old, |
| 481 | const unsigned char *input, size_t input_len, |
| 482 | unsigned char *secret_new ) |
| 483 | { |
| 484 | int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 485 | size_t hlen, ilen; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 486 | unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 }; |
Jerry Yu | 6eaa41c | 2021-11-22 18:16:45 +0800 | [diff] [blame] | 487 | unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 }; |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 488 | size_t secret_len; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 489 | |
| 490 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 491 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 492 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 493 | hlen = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 494 | |
| 495 | /* For non-initial runs, call Derive-Secret( ., "derived", "") |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 496 | * on the old secret. */ |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 497 | if( secret_old != NULL ) |
| 498 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 499 | ret = mbedtls_ssl_tls13_derive_secret( |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 500 | hash_alg, |
| 501 | secret_old, hlen, |
| 502 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ), |
| 503 | NULL, 0, /* context */ |
| 504 | MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 505 | tmp_secret, hlen ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 506 | if( ret != 0 ) |
| 507 | goto cleanup; |
| 508 | } |
| 509 | |
| 510 | if( input != NULL ) |
| 511 | { |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 512 | memcpy( tmp_input, input, input_len ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 513 | ilen = input_len; |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | ilen = hlen; |
| 518 | } |
| 519 | |
| 520 | /* HKDF-Extract takes a salt and input key material. |
| 521 | * The salt is the old secret, and the input key material |
| 522 | * is the input secret (PSK / ECDHE). */ |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 523 | ret = psa_ssl_status_to_mbedtls( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 524 | mbedtls_psa_hkdf_extract( PSA_ALG_HMAC( hash_alg ), |
Gabor Mezei | 58db653 | 2022-03-21 12:12:37 +0100 | [diff] [blame] | 525 | tmp_secret, hlen, |
| 526 | tmp_input, ilen, |
| 527 | secret_new, hlen, &secret_len ) ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 528 | |
| 529 | cleanup: |
| 530 | |
Hanno Becker | 59b50a1 | 2020-09-09 10:56:56 +0100 | [diff] [blame] | 531 | mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) ); |
| 532 | mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) ); |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 533 | return( ret ); |
| 534 | } |
| 535 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 536 | int mbedtls_ssl_tls13_derive_early_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 537 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 538 | unsigned char const *early_secret, |
| 539 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 540 | mbedtls_ssl_tls13_early_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 541 | { |
| 542 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 543 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 544 | |
| 545 | /* We should never call this function with an unknown hash, |
| 546 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 547 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 548 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 549 | |
| 550 | /* |
| 551 | * 0 |
| 552 | * | |
| 553 | * v |
| 554 | * PSK -> HKDF-Extract = Early Secret |
| 555 | * | |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 556 | * +-----> Derive-Secret(., "c e traffic", ClientHello) |
| 557 | * | = client_early_traffic_secret |
| 558 | * | |
| 559 | * +-----> Derive-Secret(., "e exp master", ClientHello) |
| 560 | * | = early_exporter_master_secret |
| 561 | * v |
| 562 | */ |
| 563 | |
| 564 | /* Create client_early_traffic_secret */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 565 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 566 | early_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 567 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ), |
| 568 | transcript, transcript_len, |
| 569 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 570 | derived->client_early_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 571 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 572 | if( ret != 0 ) |
| 573 | return( ret ); |
| 574 | |
| 575 | /* Create early exporter */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 576 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 577 | early_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 578 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ), |
| 579 | transcript, transcript_len, |
| 580 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 581 | derived->early_exporter_master_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 582 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 583 | if( ret != 0 ) |
| 584 | return( ret ); |
| 585 | |
| 586 | return( 0 ); |
| 587 | } |
| 588 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 589 | int mbedtls_ssl_tls13_derive_handshake_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 590 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 591 | unsigned char const *handshake_secret, |
| 592 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 593 | mbedtls_ssl_tls13_handshake_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 594 | { |
| 595 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 596 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 597 | |
| 598 | /* We should never call this function with an unknown hash, |
| 599 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 600 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 601 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 602 | |
| 603 | /* |
| 604 | * |
| 605 | * Handshake Secret |
| 606 | * | |
| 607 | * +-----> Derive-Secret( ., "c hs traffic", |
| 608 | * | ClientHello...ServerHello ) |
| 609 | * | = client_handshake_traffic_secret |
| 610 | * | |
| 611 | * +-----> Derive-Secret( ., "s hs traffic", |
| 612 | * | ClientHello...ServerHello ) |
| 613 | * | = server_handshake_traffic_secret |
| 614 | * |
| 615 | */ |
| 616 | |
| 617 | /* |
| 618 | * Compute client_handshake_traffic_secret with |
| 619 | * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello ) |
| 620 | */ |
| 621 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 622 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 623 | handshake_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 624 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ), |
| 625 | transcript, transcript_len, |
| 626 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 627 | derived->client_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 628 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 629 | if( ret != 0 ) |
| 630 | return( ret ); |
| 631 | |
| 632 | /* |
| 633 | * Compute server_handshake_traffic_secret with |
| 634 | * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello ) |
| 635 | */ |
| 636 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 637 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 638 | handshake_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 639 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ), |
| 640 | transcript, transcript_len, |
| 641 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 642 | derived->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 643 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 644 | if( ret != 0 ) |
| 645 | return( ret ); |
| 646 | |
| 647 | return( 0 ); |
| 648 | } |
| 649 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 650 | int mbedtls_ssl_tls13_derive_application_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 651 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 652 | unsigned char const *application_secret, |
| 653 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 654 | mbedtls_ssl_tls13_application_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 655 | { |
| 656 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 657 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 658 | |
| 659 | /* We should never call this function with an unknown hash, |
| 660 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 661 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 662 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 663 | |
| 664 | /* Generate {client,server}_application_traffic_secret_0 |
| 665 | * |
| 666 | * Master Secret |
| 667 | * | |
| 668 | * +-----> Derive-Secret( ., "c ap traffic", |
| 669 | * | ClientHello...server Finished ) |
| 670 | * | = client_application_traffic_secret_0 |
| 671 | * | |
| 672 | * +-----> Derive-Secret( ., "s ap traffic", |
| 673 | * | ClientHello...Server Finished ) |
| 674 | * | = server_application_traffic_secret_0 |
| 675 | * | |
| 676 | * +-----> Derive-Secret( ., "exp master", |
| 677 | * | ClientHello...server Finished) |
| 678 | * | = exporter_master_secret |
| 679 | * |
| 680 | */ |
| 681 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 682 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 683 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 684 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ), |
| 685 | transcript, transcript_len, |
| 686 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 687 | derived->client_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 688 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 689 | if( ret != 0 ) |
| 690 | return( ret ); |
| 691 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 692 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 693 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 694 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ), |
| 695 | transcript, transcript_len, |
| 696 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 697 | derived->server_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 698 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 699 | if( ret != 0 ) |
| 700 | return( ret ); |
| 701 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 702 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 703 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 704 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ), |
| 705 | transcript, transcript_len, |
| 706 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 707 | derived->exporter_master_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 708 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 709 | if( ret != 0 ) |
| 710 | return( ret ); |
| 711 | |
| 712 | return( 0 ); |
| 713 | } |
| 714 | |
| 715 | /* Generate resumption_master_secret for use with the ticket exchange. |
| 716 | * |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 717 | * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets() |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 718 | * because it uses the transcript hash up to and including ClientFinished. */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 719 | int mbedtls_ssl_tls13_derive_resumption_master_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 720 | psa_algorithm_t hash_alg, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 721 | unsigned char const *application_secret, |
| 722 | unsigned char const *transcript, size_t transcript_len, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 723 | mbedtls_ssl_tls13_application_secrets *derived ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 724 | { |
| 725 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 726 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 727 | |
| 728 | /* We should never call this function with an unknown hash, |
| 729 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 730 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 731 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 732 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 733 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 734 | application_secret, hash_len, |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 735 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ), |
| 736 | transcript, transcript_len, |
| 737 | MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED, |
| 738 | derived->resumption_master_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 739 | hash_len ); |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 740 | |
| 741 | if( ret != 0 ) |
| 742 | return( ret ); |
| 743 | |
| 744 | return( 0 ); |
| 745 | } |
| 746 | |
XiaokangQian | a4c99f2 | 2021-11-11 06:46:35 +0000 | [diff] [blame] | 747 | int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl ) |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 748 | { |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 749 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 750 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 751 | psa_algorithm_t const hash_alg = mbedtls_psa_translate_md( |
| 752 | handshake->ciphersuite_info->mac ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 753 | |
| 754 | /* |
| 755 | * Compute MasterSecret |
| 756 | */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 757 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 758 | handshake->tls13_master_secrets.handshake, |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 759 | NULL, 0, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 760 | handshake->tls13_master_secrets.app ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 761 | if( ret != 0 ) |
| 762 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 763 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 764 | return( ret ); |
| 765 | } |
| 766 | |
| 767 | MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret", |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 768 | handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 769 | |
| 770 | return( 0 ); |
| 771 | } |
| 772 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 773 | static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 774 | unsigned char const *base_key, |
| 775 | unsigned char const *transcript, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 776 | unsigned char *dst, |
| 777 | size_t *dst_len ) |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 778 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 779 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 780 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 781 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 782 | size_t hash_len = PSA_HASH_LENGTH( hash_alg ); |
| 783 | unsigned char finished_key[PSA_MAC_MAX_SIZE]; |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 784 | int ret; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 785 | psa_algorithm_t alg; |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 786 | |
| 787 | /* We should never call this function with an unknown hash, |
| 788 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 789 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 790 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 791 | |
| 792 | /* TLS 1.3 Finished message |
| 793 | * |
| 794 | * struct { |
| 795 | * opaque verify_data[Hash.length]; |
| 796 | * } Finished; |
| 797 | * |
| 798 | * verify_data = |
| 799 | * HMAC( finished_key, |
| 800 | * Hash( Handshake Context + |
| 801 | * Certificate* + |
| 802 | * CertificateVerify* ) |
| 803 | * ) |
| 804 | * |
| 805 | * finished_key = |
| 806 | * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length ) |
| 807 | */ |
| 808 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 809 | ret = mbedtls_ssl_tls13_hkdf_expand_label( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 810 | hash_alg, base_key, hash_len, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 811 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ), |
| 812 | NULL, 0, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 813 | finished_key, hash_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 814 | if( ret != 0 ) |
| 815 | goto exit; |
| 816 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 817 | alg = PSA_ALG_HMAC( hash_alg ); |
| 818 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 819 | psa_set_key_algorithm( &attributes, alg ); |
| 820 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 821 | |
| 822 | status = psa_import_key( &attributes, finished_key, hash_len, &key ); |
| 823 | if( status != PSA_SUCCESS ) |
| 824 | { |
| 825 | ret = psa_ssl_status_to_mbedtls( status ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 826 | goto exit; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 827 | } |
| 828 | |
| 829 | status = psa_mac_compute( key, alg, transcript, hash_len, |
| 830 | dst, hash_len, dst_len ); |
| 831 | ret = psa_ssl_status_to_mbedtls( status ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 832 | |
| 833 | exit: |
| 834 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 835 | status = psa_destroy_key( key ); |
| 836 | if( ret == 0 ) |
| 837 | ret = psa_ssl_status_to_mbedtls( status ); |
| 838 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 839 | mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 840 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 841 | return( ret ); |
| 842 | } |
| 843 | |
XiaokangQian | c5c39d5 | 2021-11-09 11:55:10 +0000 | [diff] [blame] | 844 | int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl, |
XiaokangQian | aaa0e19 | 2021-11-10 03:07:04 +0000 | [diff] [blame] | 845 | unsigned char* dst, |
| 846 | size_t dst_len, |
| 847 | size_t *actual_len, |
| 848 | int from ) |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 849 | { |
XiaokangQian | a763498 | 2021-10-22 06:32:32 +0000 | [diff] [blame] | 850 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 851 | |
XiaokangQian | aaa0e19 | 2021-11-10 03:07:04 +0000 | [diff] [blame] | 852 | unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE]; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 853 | size_t transcript_len; |
| 854 | |
Jerry Yu | 4a2fa5d | 2021-12-10 10:19:34 +0800 | [diff] [blame] | 855 | unsigned char *base_key = NULL; |
Jerry Yu | b737f6a | 2021-12-10 17:55:23 +0800 | [diff] [blame] | 856 | size_t base_key_len = 0; |
Jerry Yu | 9c07473 | 2021-12-10 17:12:43 +0800 | [diff] [blame] | 857 | mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = |
| 858 | &ssl->handshake->tls13_hs_secrets; |
Jerry Yu | a5563f6 | 2021-12-10 18:14:36 +0800 | [diff] [blame] | 859 | |
| 860 | mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 861 | |
| 862 | psa_algorithm_t hash_alg = mbedtls_psa_translate_md( |
| 863 | ssl->handshake->ciphersuite_info->mac ); |
| 864 | size_t const hash_lem = PSA_HASH_LENGTH( hash_alg ); |
Jerry Yu | a5563f6 | 2021-12-10 18:14:36 +0800 | [diff] [blame] | 865 | |
| 866 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) ); |
| 867 | |
Jerry Yu | b737f6a | 2021-12-10 17:55:23 +0800 | [diff] [blame] | 868 | if( from == MBEDTLS_SSL_IS_CLIENT ) |
| 869 | { |
| 870 | base_key = tls13_hs_secrets->client_handshake_traffic_secret; |
| 871 | base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret ); |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | base_key = tls13_hs_secrets->server_handshake_traffic_secret; |
| 876 | base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret ); |
| 877 | } |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 878 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 879 | if( dst_len < hash_lem ) |
Jerry Yu | 9c07473 | 2021-12-10 17:12:43 +0800 | [diff] [blame] | 880 | { |
| 881 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 882 | goto exit; |
| 883 | } |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 884 | |
| 885 | ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type, |
| 886 | transcript, sizeof( transcript ), |
| 887 | &transcript_len ); |
| 888 | if( ret != 0 ) |
| 889 | { |
| 890 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret ); |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 891 | goto exit; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 892 | } |
| 893 | MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len ); |
| 894 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 895 | ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 896 | if( ret != 0 ) |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 897 | goto exit; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 898 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 899 | MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_lem ); |
XiaokangQian | c5c39d5 | 2021-11-09 11:55:10 +0000 | [diff] [blame] | 900 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) ); |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 901 | |
| 902 | exit: |
Jerry Yu | 4a2fa5d | 2021-12-10 10:19:34 +0800 | [diff] [blame] | 903 | /* Erase handshake secrets */ |
Jerry Yu | b737f6a | 2021-12-10 17:55:23 +0800 | [diff] [blame] | 904 | mbedtls_platform_zeroize( base_key, base_key_len ); |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 905 | mbedtls_platform_zeroize( transcript, sizeof( transcript ) ); |
XiaokangQian | 61bdbbc | 2021-10-28 08:03:38 +0000 | [diff] [blame] | 906 | return( ret ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 909 | int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 910 | const psa_algorithm_t hash_alg, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 911 | unsigned char const *psk, size_t psk_len, |
| 912 | int psk_type, |
| 913 | unsigned char const *transcript, |
| 914 | unsigned char *result ) |
| 915 | { |
| 916 | int ret = 0; |
| 917 | unsigned char binder_key[MBEDTLS_MD_MAX_SIZE]; |
| 918 | unsigned char early_secret[MBEDTLS_MD_MAX_SIZE]; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 919 | size_t const hash_len = PSA_HASH_LENGTH( hash_alg ); |
| 920 | size_t actual_len; |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 921 | |
Hanno Becker | 28e5f1e | 2021-05-26 09:29:49 +0100 | [diff] [blame] | 922 | #if !defined(MBEDTLS_DEBUG_C) |
| 923 | ssl = NULL; /* make sure we don't use it except for debug */ |
| 924 | ((void) ssl); |
| 925 | #endif |
| 926 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 927 | /* We should never call this function with an unknown hash, |
| 928 | * but add an assertion anyway. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 929 | if( ! PSA_ALG_IS_HASH( hash_alg ) ) |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 930 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 931 | |
| 932 | /* |
| 933 | * 0 |
| 934 | * | |
| 935 | * v |
| 936 | * PSK -> HKDF-Extract = Early Secret |
| 937 | * | |
| 938 | * +-----> Derive-Secret(., "ext binder" | "res binder", "") |
| 939 | * | = binder_key |
| 940 | * v |
| 941 | */ |
| 942 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 943 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 944 | NULL, /* Old secret */ |
| 945 | psk, psk_len, /* Input */ |
| 946 | early_secret ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 947 | if( ret != 0 ) |
| 948 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 949 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 950 | goto exit; |
| 951 | } |
| 952 | |
| 953 | if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION ) |
| 954 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 955 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 956 | early_secret, hash_len, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 957 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ), |
| 958 | NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 959 | binder_key, hash_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 960 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) ); |
| 961 | } |
| 962 | else |
| 963 | { |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 964 | ret = mbedtls_ssl_tls13_derive_secret( hash_alg, |
| 965 | early_secret, hash_len, |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 966 | MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ), |
| 967 | NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 968 | binder_key, hash_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 969 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) ); |
| 970 | } |
| 971 | |
| 972 | if( ret != 0 ) |
| 973 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 974 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 975 | goto exit; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * The binding_value is computed in the same way as the Finished message |
| 980 | * but with the BaseKey being the binder_key. |
| 981 | */ |
| 982 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 983 | ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript, |
| 984 | result, &actual_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 985 | if( ret != 0 ) |
| 986 | goto exit; |
| 987 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 988 | MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len ); |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 989 | |
| 990 | exit: |
| 991 | |
| 992 | mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) ); |
| 993 | mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) ); |
| 994 | return( ret ); |
| 995 | } |
| 996 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 997 | int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform, |
| 998 | int endpoint, |
| 999 | int ciphersuite, |
| 1000 | mbedtls_ssl_key_set const *traffic_keys, |
| 1001 | mbedtls_ssl_context *ssl /* DEBUG ONLY */ ) |
| 1002 | { |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1003 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1004 | int ret; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1005 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1006 | mbedtls_cipher_info_t const *cipher_info; |
| 1007 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 1008 | unsigned char const *key_enc; |
| 1009 | unsigned char const *iv_enc; |
| 1010 | unsigned char const *key_dec; |
| 1011 | unsigned char const *iv_dec; |
| 1012 | |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1013 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1014 | psa_key_type_t key_type; |
| 1015 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1016 | psa_algorithm_t alg; |
| 1017 | size_t key_bits; |
| 1018 | psa_status_t status = PSA_SUCCESS; |
| 1019 | #endif |
| 1020 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1021 | #if !defined(MBEDTLS_DEBUG_C) |
| 1022 | ssl = NULL; /* make sure we don't use it except for those cases */ |
| 1023 | (void) ssl; |
| 1024 | #endif |
| 1025 | |
| 1026 | ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite ); |
Hanno Becker | 7887a77 | 2021-04-20 05:27:57 +0100 | [diff] [blame] | 1027 | if( ciphersuite_info == NULL ) |
| 1028 | { |
| 1029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found", |
| 1030 | ciphersuite ) ); |
| 1031 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1032 | } |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1033 | |
| 1034 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
| 1035 | if( cipher_info == NULL ) |
| 1036 | { |
Hanno Becker | 7887a77 | 2021-04-20 05:27:57 +0100 | [diff] [blame] | 1037 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found", |
| 1038 | ciphersuite_info->cipher ) ); |
Hanno Becker | f62a730 | 2021-04-21 05:21:28 +0100 | [diff] [blame] | 1039 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1042 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1043 | /* |
| 1044 | * Setup cipher contexts in target transform |
| 1045 | */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1046 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
| 1047 | cipher_info ) ) != 0 ) |
| 1048 | { |
| 1049 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 1050 | return( ret ); |
| 1051 | } |
| 1052 | |
| 1053 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
| 1054 | cipher_info ) ) != 0 ) |
| 1055 | { |
| 1056 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
| 1057 | return( ret ); |
| 1058 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1059 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1060 | |
| 1061 | #if defined(MBEDTLS_SSL_SRV_C) |
| 1062 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1063 | { |
| 1064 | key_enc = traffic_keys->server_write_key; |
| 1065 | key_dec = traffic_keys->client_write_key; |
| 1066 | iv_enc = traffic_keys->server_write_iv; |
| 1067 | iv_dec = traffic_keys->client_write_iv; |
| 1068 | } |
| 1069 | else |
| 1070 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 1071 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1072 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1073 | { |
| 1074 | key_enc = traffic_keys->client_write_key; |
| 1075 | key_dec = traffic_keys->server_write_key; |
| 1076 | iv_enc = traffic_keys->client_write_iv; |
| 1077 | iv_dec = traffic_keys->server_write_iv; |
| 1078 | } |
| 1079 | else |
| 1080 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1081 | { |
| 1082 | /* should not happen */ |
| 1083 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1084 | } |
| 1085 | |
| 1086 | memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len ); |
| 1087 | memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len ); |
| 1088 | |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1089 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1090 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, |
| 1091 | key_enc, cipher_info->key_bitlen, |
| 1092 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 1093 | { |
| 1094 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 1095 | return( ret ); |
| 1096 | } |
| 1097 | |
| 1098 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, |
| 1099 | key_dec, cipher_info->key_bitlen, |
| 1100 | MBEDTLS_DECRYPT ) ) != 0 ) |
| 1101 | { |
| 1102 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
| 1103 | return( ret ); |
| 1104 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1105 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1106 | |
Przemyslaw Stekiel | dd7b501 | 2022-01-17 15:28:57 +0100 | [diff] [blame] | 1107 | /* |
| 1108 | * Setup other fields in SSL transform |
| 1109 | */ |
| 1110 | |
| 1111 | if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 ) |
| 1112 | transform->taglen = 8; |
| 1113 | else |
| 1114 | transform->taglen = 16; |
| 1115 | |
| 1116 | transform->ivlen = traffic_keys->iv_len; |
| 1117 | transform->maclen = 0; |
| 1118 | transform->fixed_ivlen = transform->ivlen; |
| 1119 | transform->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4; |
| 1120 | |
| 1121 | /* We add the true record content type (1 Byte) to the plaintext and |
| 1122 | * then pad to the configured granularity. The mimimum length of the |
| 1123 | * type-extended and padded plaintext is therefore the padding |
| 1124 | * granularity. */ |
| 1125 | transform->minlen = |
| 1126 | transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
| 1127 | |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1128 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 1129 | /* |
| 1130 | * Setup psa keys and alg |
| 1131 | */ |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1132 | if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type, |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1133 | transform->taglen, |
| 1134 | &alg, |
| 1135 | &key_type, |
| 1136 | &key_bits ) ) != PSA_SUCCESS ) |
| 1137 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1138 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) ); |
| 1139 | return( psa_ssl_status_to_mbedtls( status ) ); |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1142 | transform->psa_alg = alg; |
| 1143 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1144 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1145 | { |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1146 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 1147 | psa_set_key_algorithm( &attributes, alg ); |
| 1148 | psa_set_key_type( &attributes, key_type ); |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1149 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1150 | if( ( status = psa_import_key( &attributes, |
| 1151 | key_enc, |
| 1152 | PSA_BITS_TO_BYTES( key_bits ), |
| 1153 | &transform->psa_key_enc ) ) != PSA_SUCCESS ) |
| 1154 | { |
| 1155 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) ); |
| 1156 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1157 | } |
Przemyslaw Stekiel | fe7397d | 2022-01-17 15:47:07 +0100 | [diff] [blame] | 1158 | |
Przemyslaw Stekiel | f9cd608 | 2022-02-01 11:25:55 +0100 | [diff] [blame] | 1159 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 1160 | |
| 1161 | if( ( status = psa_import_key( &attributes, |
| 1162 | key_dec, |
| 1163 | PSA_BITS_TO_BYTES( key_bits ), |
| 1164 | &transform->psa_key_dec ) ) != PSA_SUCCESS ) |
| 1165 | { |
| 1166 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) ); |
| 1167 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1168 | } |
Przemyslaw Stekiel | ae77b0a | 2022-01-12 10:29:03 +0100 | [diff] [blame] | 1169 | } |
| 1170 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1171 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 1172 | return( 0 ); |
| 1173 | } |
| 1174 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1175 | int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl ) |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1176 | { |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 1177 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1178 | psa_algorithm_t hash_alg; |
Jerry Yu | 5ccfcd4 | 2021-10-11 16:39:29 +0800 | [diff] [blame] | 1179 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Jerry Yu | 6ca7c7f | 2021-09-28 18:51:40 +0800 | [diff] [blame] | 1180 | |
Jerry Yu | 5ccfcd4 | 2021-10-11 16:39:29 +0800 | [diff] [blame] | 1181 | if( handshake->ciphersuite_info == NULL ) |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1182 | { |
| 1183 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) ); |
| 1184 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1185 | } |
Jerry Yu | e3131ef | 2021-09-16 13:14:15 +0800 | [diff] [blame] | 1186 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1187 | hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac ); |
Jerry Yu | e06f453 | 2021-09-23 18:35:07 +0800 | [diff] [blame] | 1188 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1189 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1190 | handshake->tls13_master_secrets.early ); |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1191 | if( ret != 0 ) |
| 1192 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1193 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 1194 | return( ret ); |
| 1195 | } |
| 1196 | |
| 1197 | return( 0 ); |
| 1198 | } |
| 1199 | |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1200 | /* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1201 | * protecting the handshake messages, as described in Section 7 of TLS 1.3. */ |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1202 | int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl, |
| 1203 | mbedtls_ssl_key_set *traffic_keys ) |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1204 | { |
| 1205 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1206 | |
| 1207 | mbedtls_md_type_t md_type; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1208 | |
| 1209 | psa_algorithm_t hash_alg; |
| 1210 | size_t hash_len; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1211 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1212 | unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE]; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1213 | size_t transcript_len; |
| 1214 | |
| 1215 | mbedtls_cipher_info_t const *cipher_info; |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1216 | size_t key_len, iv_len; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1217 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1218 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
| 1219 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info; |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1220 | mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets; |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1221 | |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1222 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1223 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1224 | cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher ); |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1225 | key_len = cipher_info->key_bitlen >> 3; |
| 1226 | iv_len = cipher_info->iv_size; |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1227 | |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1228 | md_type = ciphersuite_info->mac; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1229 | |
| 1230 | hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac ); |
| 1231 | hash_len = PSA_HASH_LENGTH( hash_alg ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1232 | |
| 1233 | ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type, |
| 1234 | transcript, |
| 1235 | sizeof( transcript ), |
| 1236 | &transcript_len ); |
| 1237 | if( ret != 0 ) |
| 1238 | { |
| 1239 | MBEDTLS_SSL_DEBUG_RET( 1, |
| 1240 | "mbedtls_ssl_get_handshake_transcript", |
| 1241 | ret ); |
| 1242 | return( ret ); |
| 1243 | } |
| 1244 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1245 | ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1246 | handshake->tls13_master_secrets.handshake, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1247 | transcript, transcript_len, tls13_hs_secrets ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1248 | if( ret != 0 ) |
| 1249 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1250 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets", |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1251 | ret ); |
| 1252 | return( ret ); |
| 1253 | } |
| 1254 | |
| 1255 | MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret", |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1256 | tls13_hs_secrets->client_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1257 | hash_len ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1258 | MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret", |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1259 | tls13_hs_secrets->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1260 | hash_len ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1261 | |
| 1262 | /* |
| 1263 | * Export client handshake traffic secret |
| 1264 | */ |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1265 | if( ssl->f_export_keys != NULL ) |
| 1266 | { |
| 1267 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1268 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1269 | tls13_hs_secrets->client_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1270 | hash_len, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1271 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1272 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1273 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ ); |
| 1274 | |
| 1275 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1276 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1277 | tls13_hs_secrets->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1278 | hash_len, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1279 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1280 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1281 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ ); |
| 1282 | } |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1283 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1284 | ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg, |
Jerry Yu | 435208a | 2021-10-13 11:22:16 +0800 | [diff] [blame] | 1285 | tls13_hs_secrets->client_handshake_traffic_secret, |
| 1286 | tls13_hs_secrets->server_handshake_traffic_secret, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1287 | hash_len, key_len, iv_len, traffic_keys ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1288 | if( ret != 0 ) |
| 1289 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1290 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1291 | goto exit; |
| 1292 | } |
| 1293 | |
| 1294 | MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key", |
| 1295 | traffic_keys->client_write_key, |
| 1296 | traffic_keys->key_len); |
| 1297 | |
| 1298 | MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key", |
| 1299 | traffic_keys->server_write_key, |
| 1300 | traffic_keys->key_len); |
| 1301 | |
| 1302 | MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv", |
| 1303 | traffic_keys->client_write_iv, |
| 1304 | traffic_keys->iv_len); |
| 1305 | |
| 1306 | MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv", |
| 1307 | traffic_keys->server_write_iv, |
| 1308 | traffic_keys->iv_len); |
| 1309 | |
Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 1310 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) ); |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 1311 | |
| 1312 | exit: |
| 1313 | |
| 1314 | return( ret ); |
| 1315 | } |
| 1316 | |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1317 | int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl ) |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1318 | { |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1319 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1320 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C) |
| 1321 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 1322 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */ |
Jerry Yu | 5ccfcd4 | 2021-10-11 16:39:29 +0800 | [diff] [blame] | 1323 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1324 | psa_algorithm_t const hash_alg = mbedtls_psa_translate_md( |
| 1325 | handshake->ciphersuite_info->mac ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1326 | |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1327 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) |
| 1328 | /* |
| 1329 | * Compute ECDHE secret used to compute the handshake secret from which |
| 1330 | * client_handshake_traffic_secret and server_handshake_traffic_secret |
| 1331 | * are derived in the handshake secret derivation stage. |
| 1332 | */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1333 | if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) ) |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1334 | { |
| 1335 | if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) ) |
| 1336 | { |
| 1337 | #if defined(MBEDTLS_ECDH_C) |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1338 | /* Compute ECDH shared secret. */ |
| 1339 | status = psa_raw_key_agreement( |
| 1340 | PSA_ALG_ECDH, handshake->ecdh_psa_privkey, |
| 1341 | handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len, |
| 1342 | handshake->premaster, sizeof( handshake->premaster ), |
| 1343 | &handshake->pmslen ); |
| 1344 | if( status != PSA_SUCCESS ) |
| 1345 | { |
| 1346 | ret = psa_ssl_status_to_mbedtls( status ); |
| 1347 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret ); |
| 1348 | return( ret ); |
| 1349 | } |
| 1350 | |
| 1351 | status = psa_destroy_key( handshake->ecdh_psa_privkey ); |
| 1352 | if( status != PSA_SUCCESS ) |
| 1353 | { |
| 1354 | ret = psa_ssl_status_to_mbedtls( status ); |
| 1355 | MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret ); |
| 1356 | return( ret ); |
| 1357 | } |
| 1358 | |
| 1359 | handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1360 | #endif /* MBEDTLS_ECDH_C */ |
| 1361 | } |
| 1362 | else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) ) |
| 1363 | { |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1364 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) ); |
| 1365 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1366 | } |
| 1367 | } |
| 1368 | #else |
| 1369 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 1370 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */ |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1371 | |
| 1372 | /* |
Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 1373 | * Compute the Handshake Secret |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1374 | */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1375 | ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1376 | handshake->tls13_master_secrets.early, |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1377 | handshake->premaster, handshake->pmslen, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1378 | handshake->tls13_master_secrets.handshake ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1379 | if( ret != 0 ) |
| 1380 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1381 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1382 | return( ret ); |
| 1383 | } |
| 1384 | |
| 1385 | MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret", |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1386 | handshake->tls13_master_secrets.handshake, |
| 1387 | PSA_HASH_LENGTH( hash_alg ) ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1388 | |
| 1389 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) |
Przemyslaw Stekiel | c0824bf | 2022-02-10 10:37:15 +0100 | [diff] [blame] | 1390 | mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) ); |
Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 1391 | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */ |
| 1392 | return( 0 ); |
| 1393 | } |
| 1394 | |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1395 | /* Generate application traffic keys since any records following a 1-RTT Finished message |
| 1396 | * MUST be encrypted under the application traffic key. |
| 1397 | */ |
XiaokangQian | d0aa3e9 | 2021-11-10 06:17:40 +0000 | [diff] [blame] | 1398 | int mbedtls_ssl_tls13_generate_application_keys( |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1399 | mbedtls_ssl_context *ssl, |
| 1400 | mbedtls_ssl_key_set *traffic_keys ) |
| 1401 | { |
XiaokangQian | a763498 | 2021-10-22 06:32:32 +0000 | [diff] [blame] | 1402 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1403 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1404 | |
| 1405 | /* Address at which to store the application secrets */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1406 | mbedtls_ssl_tls13_application_secrets * const app_secrets = |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1407 | &ssl->session_negotiate->app_secrets; |
| 1408 | |
| 1409 | /* Holding the transcript up to and including the ServerFinished */ |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1410 | unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE]; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1411 | size_t transcript_len; |
| 1412 | |
| 1413 | /* Variables relating to the hash for the chosen ciphersuite. */ |
| 1414 | mbedtls_md_type_t md_type; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1415 | |
| 1416 | psa_algorithm_t hash_alg; |
| 1417 | size_t hash_len; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1418 | |
| 1419 | /* Variables relating to the cipher for the chosen ciphersuite. */ |
| 1420 | mbedtls_cipher_info_t const *cipher_info; |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1421 | size_t key_len, iv_len; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1422 | |
| 1423 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) ); |
| 1424 | |
| 1425 | /* Extract basic information about hash and ciphersuite */ |
| 1426 | |
| 1427 | cipher_info = mbedtls_cipher_info_from_type( |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1428 | handshake->ciphersuite_info->cipher ); |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1429 | key_len = cipher_info->key_bitlen / 8; |
| 1430 | iv_len = cipher_info->iv_size; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1431 | |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1432 | md_type = handshake->ciphersuite_info->mac; |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1433 | |
| 1434 | hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac ); |
| 1435 | hash_len = PSA_HASH_LENGTH( hash_alg ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Compute current handshake transcript. It's the caller's responsiblity |
| 1438 | * to call this at the right time, that is, after the ServerFinished. */ |
| 1439 | |
| 1440 | ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type, |
| 1441 | transcript, sizeof( transcript ), |
| 1442 | &transcript_len ); |
| 1443 | if( ret != 0 ) |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1444 | goto cleanup; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1445 | |
| 1446 | /* Compute application secrets from master secret and transcript hash. */ |
| 1447 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1448 | ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg, |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1449 | handshake->tls13_master_secrets.app, |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1450 | transcript, transcript_len, |
| 1451 | app_secrets ); |
Jerry Yu | 4a2fa5d | 2021-12-10 10:19:34 +0800 | [diff] [blame] | 1452 | /* Erase master secrets */ |
Jerry Yu | 23ab7a4 | 2021-12-08 14:34:10 +0800 | [diff] [blame] | 1453 | mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets, |
Jerry Yu | 27224f5 | 2021-12-09 10:56:50 +0800 | [diff] [blame] | 1454 | sizeof( ssl->handshake->tls13_master_secrets ) ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1455 | if( ret != 0 ) |
| 1456 | { |
| 1457 | MBEDTLS_SSL_DEBUG_RET( 1, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1458 | "mbedtls_ssl_tls13_derive_application_secrets", ret ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1459 | goto cleanup; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* Derive first epoch of IV + Key for application traffic. */ |
| 1463 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1464 | ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg, |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1465 | app_secrets->client_application_traffic_secret_N, |
| 1466 | app_secrets->server_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1467 | hash_len, key_len, iv_len, traffic_keys ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1468 | if( ret != 0 ) |
| 1469 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1470 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1471 | goto cleanup; |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret", |
| 1475 | app_secrets->client_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1476 | hash_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1477 | |
| 1478 | MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret", |
| 1479 | app_secrets->server_application_traffic_secret_N, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1480 | hash_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1481 | |
XiaokangQian | ac0385c | 2021-11-03 06:40:11 +0000 | [diff] [blame] | 1482 | /* |
| 1483 | * Export client/server application traffic secret 0 |
| 1484 | */ |
| 1485 | if( ssl->f_export_keys != NULL ) |
| 1486 | { |
| 1487 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1488 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1489 | app_secrets->client_application_traffic_secret_N, hash_len, |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1490 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1491 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
XiaokangQian | b51f884 | 2021-11-04 03:02:47 +0000 | [diff] [blame] | 1492 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by |
| 1493 | a new constant for TLS 1.3! */ ); |
XiaokangQian | ac0385c | 2021-11-03 06:40:11 +0000 | [diff] [blame] | 1494 | |
| 1495 | ssl->f_export_keys( ssl->p_export_keys, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1496 | MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET, |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame^] | 1497 | app_secrets->server_application_traffic_secret_N, hash_len, |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1498 | handshake->randbytes, |
lhuang04 | a3890a3 | 2022-01-04 09:47:20 -0800 | [diff] [blame] | 1499 | handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN, |
XiaokangQian | b51f884 | 2021-11-04 03:02:47 +0000 | [diff] [blame] | 1500 | MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by |
| 1501 | a new constant for TLS 1.3! */ ); |
XiaokangQian | ac0385c | 2021-11-03 06:40:11 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1504 | MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1505 | traffic_keys->client_write_key, key_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1506 | MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1507 | traffic_keys->server_write_key, key_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1508 | MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1509 | traffic_keys->client_write_iv, iv_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1510 | MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV", |
Xiaofei Bai | b797284 | 2021-11-18 07:29:56 +0000 | [diff] [blame] | 1511 | traffic_keys->server_write_iv, iv_len ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1512 | |
| 1513 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1514 | |
| 1515 | cleanup: |
Jerry Yu | 2c70a39 | 2021-12-08 13:28:49 +0800 | [diff] [blame] | 1516 | /* randbytes is not used again */ |
| 1517 | mbedtls_platform_zeroize( ssl->handshake->randbytes, |
| 1518 | sizeof( ssl->handshake->randbytes ) ); |
XiaokangQian | 3306284 | 2021-11-11 03:37:45 +0000 | [diff] [blame] | 1519 | mbedtls_platform_zeroize( transcript, sizeof( transcript ) ); |
XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 1520 | return( ret ); |
XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1523 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |