s2n_kyber_evp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "s2n_kyber_evp.h"
  16. #include <openssl/evp.h>
  17. #include <stddef.h>
  18. #include "error/s2n_errno.h"
  19. #include "tls/s2n_kem.h"
  20. #include "utils/s2n_safety.h"
  21. #include "utils/s2n_safety_macros.h"
  22. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && !defined(S2N_NO_PQ)
  23. DEFINE_POINTER_CLEANUP_FUNC(EVP_PKEY *, EVP_PKEY_free);
  24. DEFINE_POINTER_CLEANUP_FUNC(EVP_PKEY_CTX *, EVP_PKEY_CTX_free);
  25. int s2n_kyber_evp_generate_keypair(IN const struct s2n_kem *kem, OUT uint8_t *public_key,
  26. OUT uint8_t *secret_key)
  27. {
  28. DEFER_CLEANUP(EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_KEM, NULL), EVP_PKEY_CTX_free_pointer);
  29. POSIX_GUARD_PTR(kyber_pkey_ctx);
  30. POSIX_GUARD_OSSL(EVP_PKEY_CTX_kem_set_params(kyber_pkey_ctx, kem->kem_nid), S2N_ERR_PQ_CRYPTO);
  31. POSIX_GUARD_OSSL(EVP_PKEY_keygen_init(kyber_pkey_ctx), S2N_ERR_PQ_CRYPTO);
  32. DEFER_CLEANUP(EVP_PKEY *kyber_pkey = NULL, EVP_PKEY_free_pointer);
  33. POSIX_GUARD_OSSL(EVP_PKEY_keygen(kyber_pkey_ctx, &kyber_pkey), S2N_ERR_PQ_CRYPTO);
  34. POSIX_GUARD_PTR(kyber_pkey);
  35. size_t public_key_size = kem->public_key_length;
  36. POSIX_GUARD_OSSL(EVP_PKEY_get_raw_public_key(kyber_pkey, public_key, &public_key_size), S2N_ERR_PQ_CRYPTO);
  37. POSIX_ENSURE_EQ(kem->public_key_length, public_key_size);
  38. size_t private_key_size = kem->private_key_length;
  39. POSIX_GUARD_OSSL(EVP_PKEY_get_raw_private_key(kyber_pkey, secret_key, &private_key_size), S2N_ERR_PQ_CRYPTO);
  40. POSIX_ENSURE_EQ(kem->private_key_length, private_key_size);
  41. return S2N_SUCCESS;
  42. }
  43. int s2n_kyber_evp_encapsulate(IN const struct s2n_kem *kem, OUT uint8_t *ciphertext, OUT uint8_t *shared_secret,
  44. IN const uint8_t *public_key)
  45. {
  46. DEFER_CLEANUP(EVP_PKEY *kyber_pkey = EVP_PKEY_kem_new_raw_public_key(kem->kem_nid, public_key, kem->public_key_length), EVP_PKEY_free_pointer);
  47. POSIX_GUARD_PTR(kyber_pkey);
  48. DEFER_CLEANUP(EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new(kyber_pkey, NULL), EVP_PKEY_CTX_free_pointer);
  49. POSIX_GUARD_PTR(kyber_pkey_ctx);
  50. size_t ciphertext_size = kem->ciphertext_length;
  51. size_t shared_secret_size = kem->shared_secret_key_length;
  52. POSIX_GUARD_OSSL(EVP_PKEY_encapsulate(kyber_pkey_ctx, ciphertext, &ciphertext_size, shared_secret,
  53. &shared_secret_size),
  54. S2N_ERR_PQ_CRYPTO);
  55. POSIX_ENSURE_EQ(kem->ciphertext_length, ciphertext_size);
  56. POSIX_ENSURE_EQ(kem->shared_secret_key_length, shared_secret_size);
  57. return S2N_SUCCESS;
  58. }
  59. int s2n_kyber_evp_decapsulate(IN const struct s2n_kem *kem, OUT uint8_t *shared_secret, IN const uint8_t *ciphertext,
  60. IN const uint8_t *private_key)
  61. {
  62. DEFER_CLEANUP(EVP_PKEY *kyber_pkey = EVP_PKEY_kem_new_raw_secret_key(kem->kem_nid, private_key, kem->private_key_length), EVP_PKEY_free_pointer);
  63. POSIX_GUARD_PTR(kyber_pkey);
  64. DEFER_CLEANUP(EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new(kyber_pkey, NULL), EVP_PKEY_CTX_free_pointer);
  65. POSIX_GUARD_PTR(kyber_pkey_ctx);
  66. size_t shared_secret_size = kem->shared_secret_key_length;
  67. POSIX_GUARD_OSSL(EVP_PKEY_decapsulate(kyber_pkey_ctx, shared_secret, &shared_secret_size,
  68. (uint8_t *) ciphertext, kem->ciphertext_length),
  69. S2N_ERR_PQ_CRYPTO);
  70. POSIX_ENSURE_EQ(kem->shared_secret_key_length, shared_secret_size);
  71. return S2N_SUCCESS;
  72. }
  73. #else
  74. int s2n_kyber_512_evp_generate_keypair(IN const struct s2n_kem *kem, OUT uint8_t *public_key, OUT uint8_t *secret_key)
  75. {
  76. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  77. }
  78. int s2n_kyber_512_evp_encapsulate(IN const struct s2n_kem *kem, OUT uint8_t *ciphertext, OUT uint8_t *shared_secret,
  79. IN const uint8_t *public_key)
  80. {
  81. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  82. }
  83. int s2n_kyber_512_evp_decapsulate(IN const struct s2n_kem *kem, OUT uint8_t *shared_secret, IN const uint8_t *ciphertext,
  84. IN const uint8_t *secret_key)
  85. {
  86. POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
  87. }
  88. #endif