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