s2n_tls13_keys.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #pragma once
  16. #include <stdint.h>
  17. #include "crypto/s2n_hkdf.h"
  18. #include "crypto/s2n_hmac.h"
  19. #include "stuffer/s2n_stuffer.h"
  20. #include "tls/s2n_psk.h"
  21. #include "tls/s2n_tls_parameters.h"
  22. #include "utils/s2n_blob.h"
  23. #include "utils/s2n_mem.h"
  24. #include "utils/s2n_safety.h"
  25. /* Unlike TLS1.2 secrets, TLS1.3 secret lengths vary depending
  26. * on the hash algorithm used to calculate them.
  27. * We allocate enough space for the largest possible secret.
  28. * At the moment, that is 48 bytes for S2N_HASH_SHA384 and
  29. * matches the TLS1.2 secret length.
  30. */
  31. #define S2N_TLS13_SECRET_MAX_LEN SHA384_DIGEST_LENGTH
  32. struct s2n_tls13_keys {
  33. s2n_hmac_algorithm hmac_algorithm;
  34. s2n_hash_algorithm hash_algorithm;
  35. uint8_t size;
  36. /* we only need to keep these 2 rolling secrets at any point,
  37. * since other secrets to be used can be generated from these
  38. */
  39. struct s2n_blob extract_secret;
  40. struct s2n_blob derive_secret;
  41. uint8_t extract_secret_bytes[S2N_TLS13_SECRET_MAX_LEN];
  42. uint8_t derive_secret_bytes[S2N_TLS13_SECRET_MAX_LEN];
  43. struct s2n_hmac_state hmac;
  44. };
  45. /* Defines TLS 1.3 HKDF Labels */
  46. extern const struct s2n_blob s2n_tls13_label_derived_secret;
  47. extern const struct s2n_blob s2n_tls13_label_external_psk_binder_key;
  48. extern const struct s2n_blob s2n_tls13_label_resumption_psk_binder_key;
  49. extern const struct s2n_blob s2n_tls13_label_client_early_traffic_secret;
  50. extern const struct s2n_blob s2n_tls13_label_early_exporter_master_secret;
  51. extern const struct s2n_blob s2n_tls13_label_client_handshake_traffic_secret;
  52. extern const struct s2n_blob s2n_tls13_label_server_handshake_traffic_secret;
  53. extern const struct s2n_blob s2n_tls13_label_client_application_traffic_secret;
  54. extern const struct s2n_blob s2n_tls13_label_server_application_traffic_secret;
  55. extern const struct s2n_blob s2n_tls13_label_exporter_master_secret;
  56. extern const struct s2n_blob s2n_tls13_label_resumption_master_secret;
  57. extern const struct s2n_blob s2n_tls13_label_finished;
  58. extern const struct s2n_blob s2n_tls13_label_exporter;
  59. /* Traffic secret labels */
  60. extern const struct s2n_blob s2n_tls13_label_traffic_secret_key;
  61. extern const struct s2n_blob s2n_tls13_label_traffic_secret_iv;
  62. #define s2n_tls13_key_blob(name, bytes) \
  63. s2n_stack_blob(name, bytes, S2N_TLS13_SECRET_MAX_LEN)
  64. int s2n_tls13_keys_init(struct s2n_tls13_keys *handshake, s2n_hmac_algorithm alg);
  65. int s2n_tls13_keys_free(struct s2n_tls13_keys *keys);
  66. int s2n_tls13_derive_traffic_keys(struct s2n_tls13_keys *handshake, struct s2n_blob *secret, struct s2n_blob *key, struct s2n_blob *iv);
  67. int s2n_tls13_derive_finished_key(struct s2n_tls13_keys *keys, struct s2n_blob *secret_key, struct s2n_blob *output_finish_key);
  68. 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);
  69. int s2n_tls13_update_application_traffic_secret(struct s2n_tls13_keys *keys, struct s2n_blob *old_secret, struct s2n_blob *new_secret);
  70. S2N_RESULT s2n_tls13_derive_session_ticket_secret(struct s2n_tls13_keys *keys, struct s2n_blob *resumption_secret,
  71. struct s2n_blob *ticket_nonce, struct s2n_blob *secret_blob);