s2n_ecc_evp.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <openssl/evp.h>
  17. #include "crypto/s2n_hash.h"
  18. #include "stuffer/s2n_stuffer.h"
  19. #include "tls/s2n_kex_data.h"
  20. #include "tls/s2n_tls_parameters.h"
  21. #include "utils/s2n_safety.h"
  22. /* Share sizes are described here: https://tools.ietf.org/html/rfc8446#section-4.2.8.2
  23. * and include the extra "legacy_form" byte */
  24. #define SECP256R1_SHARE_SIZE ((32 * 2) + 1)
  25. #define SECP384R1_SHARE_SIZE ((48 * 2) + 1)
  26. #define SECP521R1_SHARE_SIZE ((66 * 2) + 1)
  27. #define X25519_SHARE_SIZE (32)
  28. struct s2n_ecc_named_curve {
  29. /* See https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8 */
  30. uint16_t iana_id;
  31. /* See nid_list in openssl/ssl/t1_lib.c */
  32. int libcrypto_nid;
  33. const char *name;
  34. const uint8_t share_size;
  35. int (*generate_key)(const struct s2n_ecc_named_curve *named_curve, EVP_PKEY **evp_pkey);
  36. };
  37. extern const struct s2n_ecc_named_curve s2n_ecc_curve_secp256r1;
  38. extern const struct s2n_ecc_named_curve s2n_ecc_curve_secp384r1;
  39. extern const struct s2n_ecc_named_curve s2n_ecc_curve_secp521r1;
  40. extern const struct s2n_ecc_named_curve s2n_ecc_curve_x25519;
  41. /* BoringSSL only supports using EVP_PKEY_X25519 with "modern" EC EVP APIs. BoringSSL has a note to possibly add this in
  42. * the future. See https://github.com/google/boringssl/blob/master/crypto/evp/p_x25519_asn1.c#L233
  43. */
  44. #if S2N_OPENSSL_VERSION_AT_LEAST(1, 1, 0) && !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
  45. #define EVP_APIS_SUPPORTED 1
  46. #define S2N_ECC_EVP_SUPPORTED_CURVES_COUNT 4
  47. #else
  48. #define EVP_APIS_SUPPORTED 0
  49. #define S2N_ECC_EVP_SUPPORTED_CURVES_COUNT 3
  50. #endif
  51. extern const struct s2n_ecc_named_curve *const s2n_all_supported_curves_list[];
  52. extern const size_t s2n_all_supported_curves_list_len;
  53. struct s2n_ecc_evp_params {
  54. const struct s2n_ecc_named_curve *negotiated_curve;
  55. EVP_PKEY *evp_pkey;
  56. };
  57. int s2n_ecc_evp_generate_ephemeral_key(struct s2n_ecc_evp_params *ecc_evp_params);
  58. int s2n_ecc_evp_compute_shared_secret_from_params(struct s2n_ecc_evp_params *private_ecc_evp_params,
  59. struct s2n_ecc_evp_params *public_ecc_evp_params,
  60. struct s2n_blob *shared_key);
  61. int s2n_ecc_evp_write_params_point(struct s2n_ecc_evp_params *ecc_evp_params, struct s2n_stuffer *out);
  62. int s2n_ecc_evp_read_params_point(struct s2n_stuffer *in, int point_size, struct s2n_blob *point_blob);
  63. int s2n_ecc_evp_compute_shared_secret_as_server(struct s2n_ecc_evp_params *server_ecc_evp_params,
  64. struct s2n_stuffer *Yc_in, struct s2n_blob *shared_key);
  65. int s2n_ecc_evp_compute_shared_secret_as_client(struct s2n_ecc_evp_params *server_ecc_evp_params,
  66. struct s2n_stuffer *Yc_out, struct s2n_blob *shared_key);
  67. int s2n_ecc_evp_parse_params_point(struct s2n_blob *point_blob, struct s2n_ecc_evp_params *ecc_evp_params);
  68. int s2n_ecc_evp_write_params(struct s2n_ecc_evp_params *ecc_evp_params, struct s2n_stuffer *out,
  69. struct s2n_blob *written);
  70. int s2n_ecc_evp_read_params(struct s2n_stuffer *in, struct s2n_blob *data_to_verify,
  71. struct s2n_ecdhe_raw_server_params *raw_server_ecc_params);
  72. int s2n_ecc_evp_parse_params(struct s2n_connection *conn,
  73. struct s2n_ecdhe_raw_server_params *raw_server_ecc_params,
  74. struct s2n_ecc_evp_params *ecc_evp_params);
  75. int s2n_ecc_evp_find_supported_curve(struct s2n_connection *conn, struct s2n_blob *iana_ids, const struct s2n_ecc_named_curve **found);
  76. int s2n_ecc_evp_params_free(struct s2n_ecc_evp_params *ecc_evp_params);
  77. int s2n_is_evp_apis_supported();