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