s2n_prf.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_hash.h"
  18. #include "crypto/s2n_hmac.h"
  19. #include "crypto/s2n_openssl.h"
  20. #include "utils/s2n_blob.h"
  21. /* Enough to support TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 2*SHA384_DIGEST_LEN + 2*AES256_KEY_SIZE */
  22. #define S2N_MAX_KEY_BLOCK_LEN 160
  23. #if defined(OPENSSL_IS_AWSLC)
  24. #define S2N_LIBCRYPTO_SUPPORTS_TLS_PRF 1
  25. #else
  26. #define S2N_LIBCRYPTO_SUPPORTS_TLS_PRF 0
  27. #endif
  28. union p_hash_state {
  29. struct s2n_hmac_state s2n_hmac;
  30. struct s2n_evp_hmac_state evp_hmac;
  31. };
  32. struct s2n_prf_working_space {
  33. union p_hash_state p_hash;
  34. uint8_t digest0[S2N_MAX_DIGEST_LEN];
  35. uint8_t digest1[S2N_MAX_DIGEST_LEN];
  36. };
  37. /* The s2n p_hash implementation is abstracted to allow for separate implementations, using
  38. * either s2n's formally verified HMAC or OpenSSL's EVP HMAC, for use by the TLS PRF. */
  39. struct s2n_p_hash_hmac {
  40. int (*alloc)(struct s2n_prf_working_space *ws);
  41. int (*init)(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret);
  42. int (*update)(struct s2n_prf_working_space *ws, const void *data, uint32_t size);
  43. int (*final)(struct s2n_prf_working_space *ws, void *digest, uint32_t size);
  44. int (*reset)(struct s2n_prf_working_space *ws);
  45. int (*cleanup)(struct s2n_prf_working_space *ws);
  46. int (*free)(struct s2n_prf_working_space *ws);
  47. };
  48. /* TLS key expansion results in an array of contiguous data which is then
  49. * interpreted as the MAC, KEY and IV for the client and server.
  50. *
  51. * The following is the memory layout of the key material:
  52. *
  53. * [ CLIENT_MAC, SERVER_MAC, CLIENT_KEY, SERVER_KEY, CLIENT_IV, SERVER_IV ]
  54. */
  55. struct s2n_key_material {
  56. /* key material data resulting from key expansion */
  57. uint8_t key_block[S2N_MAX_KEY_BLOCK_LEN];
  58. /* pointers into data representing specific key information */
  59. struct s2n_blob client_mac;
  60. struct s2n_blob server_mac;
  61. struct s2n_blob client_key;
  62. struct s2n_blob server_key;
  63. struct s2n_blob client_iv;
  64. struct s2n_blob server_iv;
  65. };
  66. S2N_RESULT s2n_key_material_init(struct s2n_key_material *key_material, struct s2n_connection *conn);
  67. #include "tls/s2n_connection.h"
  68. S2N_RESULT s2n_prf_new(struct s2n_connection *conn);
  69. S2N_RESULT s2n_prf_wipe(struct s2n_connection *conn);
  70. S2N_RESULT s2n_prf_free(struct s2n_connection *conn);
  71. int s2n_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label, struct s2n_blob *seed_a,
  72. struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out);
  73. int s2n_prf_calculate_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret);
  74. int s2n_tls_prf_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret);
  75. int s2n_hybrid_prf_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret);
  76. S2N_RESULT s2n_tls_prf_extended_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret, struct s2n_blob *session_hash, struct s2n_blob *sha1_hash);
  77. S2N_RESULT s2n_prf_get_digest_for_ems(struct s2n_connection *conn, struct s2n_blob *message, s2n_hash_algorithm hash_alg, struct s2n_blob *output);
  78. S2N_RESULT s2n_prf_generate_key_material(struct s2n_connection *conn, struct s2n_key_material *key_material);
  79. int s2n_prf_key_expansion(struct s2n_connection *conn);
  80. int s2n_prf_server_finished(struct s2n_connection *conn);
  81. int s2n_prf_client_finished(struct s2n_connection *conn);
  82. bool s2n_libcrypto_supports_tls_prf();
  83. S2N_RESULT s2n_custom_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label,
  84. struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out);
  85. S2N_RESULT s2n_libcrypto_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label,
  86. struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out);