Branch data Line data Source code
1 : : /* 2 : : * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 : : * 4 : : * Licensed under the Apache License, Version 2.0 (the "License"). 5 : : * You may not use this file except in compliance with the License. 6 : : * A copy of the License is located at 7 : : * 8 : : * http://aws.amazon.com/apache2.0 9 : : * 10 : : * or in the "license" file accompanying this file. This file is distributed 11 : : * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 : : * express or implied. See the License for the specific language governing 13 : : * permissions and limitations under the License. 14 : : */ 15 : : 16 : : #pragma once 17 : : 18 : : #include <stdint.h> 19 : : 20 : : #include "crypto/s2n_hkdf.h" 21 : : #include "crypto/s2n_hmac.h" 22 : : #include "stuffer/s2n_stuffer.h" 23 : : #include "tls/s2n_psk.h" 24 : : #include "tls/s2n_tls_parameters.h" 25 : : #include "utils/s2n_blob.h" 26 : : #include "utils/s2n_mem.h" 27 : : #include "utils/s2n_safety.h" 28 : : 29 : : /* Unlike TLS1.2 secrets, TLS1.3 secret lengths vary depending 30 : : * on the hash algorithm used to calculate them. 31 : : * We allocate enough space for the largest possible secret. 32 : : * At the moment, that is 48 bytes for S2N_HASH_SHA384 and 33 : : * matches the TLS1.2 secret length. 34 : : */ 35 : : #define S2N_TLS13_SECRET_MAX_LEN SHA384_DIGEST_LENGTH 36 : : 37 : : struct s2n_tls13_keys { 38 : : s2n_hmac_algorithm hmac_algorithm; 39 : : s2n_hash_algorithm hash_algorithm; 40 : : 41 : : uint8_t size; 42 : : 43 : : /* we only need to keep these 2 rolling secrets at any point, 44 : : * since other secrets to be used can be generated from these 45 : : */ 46 : : struct s2n_blob extract_secret; 47 : : struct s2n_blob derive_secret; 48 : : uint8_t extract_secret_bytes[S2N_TLS13_SECRET_MAX_LEN]; 49 : : uint8_t derive_secret_bytes[S2N_TLS13_SECRET_MAX_LEN]; 50 : : 51 : : struct s2n_hmac_state hmac; 52 : : }; 53 : : 54 : : /* Defines TLS 1.3 HKDF Labels */ 55 : : extern const struct s2n_blob s2n_tls13_label_derived_secret; 56 : : extern const struct s2n_blob s2n_tls13_label_external_psk_binder_key; 57 : : extern const struct s2n_blob s2n_tls13_label_resumption_psk_binder_key; 58 : : 59 : : extern const struct s2n_blob s2n_tls13_label_client_early_traffic_secret; 60 : : extern const struct s2n_blob s2n_tls13_label_early_exporter_master_secret; 61 : : 62 : : extern const struct s2n_blob s2n_tls13_label_client_handshake_traffic_secret; 63 : : extern const struct s2n_blob s2n_tls13_label_server_handshake_traffic_secret; 64 : : 65 : : extern const struct s2n_blob s2n_tls13_label_client_application_traffic_secret; 66 : : extern const struct s2n_blob s2n_tls13_label_server_application_traffic_secret; 67 : : 68 : : extern const struct s2n_blob s2n_tls13_label_exporter_master_secret; 69 : : extern const struct s2n_blob s2n_tls13_label_resumption_master_secret; 70 : : 71 : : extern const struct s2n_blob s2n_tls13_label_finished; 72 : : 73 : : extern const struct s2n_blob s2n_tls13_label_exporter; 74 : : 75 : : /* Traffic secret labels */ 76 : : 77 : : extern const struct s2n_blob s2n_tls13_label_traffic_secret_key; 78 : : extern const struct s2n_blob s2n_tls13_label_traffic_secret_iv; 79 : : 80 : : #define s2n_tls13_key_blob(name, bytes) \ 81 : 26493 : s2n_stack_blob(name, bytes, S2N_TLS13_SECRET_MAX_LEN) 82 : : 83 : : int s2n_tls13_keys_init(struct s2n_tls13_keys *handshake, s2n_hmac_algorithm alg); 84 : : int s2n_tls13_keys_free(struct s2n_tls13_keys *keys); 85 : : 86 : : int s2n_tls13_derive_traffic_keys(struct s2n_tls13_keys *handshake, struct s2n_blob *secret, struct s2n_blob *key, struct s2n_blob *iv); 87 : : int s2n_tls13_derive_finished_key(struct s2n_tls13_keys *keys, struct s2n_blob *secret_key, struct s2n_blob *output_finish_key); 88 : : int s2n_tls13_calculate_finished_mac(struct s2n_tls13_keys *keys, struct s2n_blob *finished_key, struct s2n_hash_state *hash_state, struct s2n_blob *finished_verify); 89 : : int s2n_tls13_update_application_traffic_secret(struct s2n_tls13_keys *keys, struct s2n_blob *old_secret, struct s2n_blob *new_secret); 90 : : S2N_RESULT s2n_tls13_derive_session_ticket_secret(struct s2n_tls13_keys *keys, struct s2n_blob *resumption_secret, 91 : : struct s2n_blob *ticket_nonce, struct s2n_blob *secret_blob);