diff options
author | robot-contrib <robot-contrib@yandex-team.com> | 2022-12-22 08:12:51 +0300 |
---|---|---|
committer | robot-contrib <robot-contrib@yandex-team.com> | 2022-12-22 08:12:51 +0300 |
commit | 5e473ae99515dcc385d93e469a3b5bcce2a316e6 (patch) | |
tree | 2069fa52e25be8cdc325780d273ddcff70b4fb8a /contrib/restricted/aws/s2n/pq-crypto | |
parent | fb9ac8a6a226ef5b6aa8fc8d58563d5e69680766 (diff) | |
download | ydb-5e473ae99515dcc385d93e469a3b5bcce2a316e6.tar.gz |
Update contrib/restricted/aws/s2n to 1.3.30
Diffstat (limited to 'contrib/restricted/aws/s2n/pq-crypto')
5 files changed, 121 insertions, 3 deletions
diff --git a/contrib/restricted/aws/s2n/pq-crypto/kyber_r3/kyber512r3_kem.c b/contrib/restricted/aws/s2n/pq-crypto/kyber_r3/kyber512r3_kem.c index e35b3df805..06c86ccad2 100644 --- a/contrib/restricted/aws/s2n/pq-crypto/kyber_r3/kyber512r3_kem.c +++ b/contrib/restricted/aws/s2n/pq-crypto/kyber_r3/kyber512r3_kem.c @@ -24,7 +24,7 @@ S2N_ENSURE_PORTABLE_OPTIMIZATIONS * * Returns 0 (success) **************************************************/ -int s2n_kyber_512_r3_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) +int s2n_kyber_512_r3_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) { POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED); #if defined(S2N_KYBER512R3_AVX2_BMI2) @@ -60,7 +60,7 @@ int s2n_kyber_512_r3_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) * * Returns 0 (success) **************************************************/ -int s2n_kyber_512_r3_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) +int s2n_kyber_512_r3_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk) { POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED); uint8_t buf[2*S2N_KYBER_512_R3_SYMBYTES]; @@ -109,7 +109,7 @@ int s2n_kyber_512_r3_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const * * On failure, ss will contain a pseudo-random value. **************************************************/ -int s2n_kyber_512_r3_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) +int s2n_kyber_512_r3_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk) { POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED); uint8_t buf[2*S2N_KYBER_512_R3_SYMBYTES]; diff --git a/contrib/restricted/aws/s2n/pq-crypto/s2n_kyber_512_evp.c b/contrib/restricted/aws/s2n/pq-crypto/s2n_kyber_512_evp.c new file mode 100644 index 0000000000..36e6d3d85e --- /dev/null +++ b/contrib/restricted/aws/s2n/pq-crypto/s2n_kyber_512_evp.c @@ -0,0 +1,86 @@ +/* +* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"). +* You may not use this file except in compliance with the License. +* A copy of the License is located at +* +* http://aws.amazon.com/apache2.0 +* +* or in the "license" file accompanying this file. This file is distributed +* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +* express or implied. See the License for the specific language governing +* permissions and limitations under the License. +*/ + +#include "s2n_kyber_512_evp.h" + +#include <openssl/evp.h> +#include <stddef.h> + +#include "error/s2n_errno.h" +#include "tls/s2n_kem.h" +#include "utils/s2n_safety_macros.h" + +#if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER512) +int s2n_kyber_512_evp_generate_keypair(uint8_t *public_key, uint8_t *private_key) { + EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_KYBER512, NULL); + POSIX_GUARD_PTR(kyber_pkey_ctx); + POSIX_ENSURE(EVP_PKEY_keygen_init(kyber_pkey_ctx), S2N_FAILURE); + + EVP_PKEY *kyber_pkey = NULL; + POSIX_ENSURE(EVP_PKEY_keygen(kyber_pkey_ctx, &kyber_pkey), S2N_FAILURE); + + size_t public_key_size = S2N_KYBER_512_R3_PUBLIC_KEY_BYTES; + size_t private_key_size = S2N_KYBER_512_R3_SECRET_KEY_BYTES; + POSIX_ENSURE(EVP_PKEY_get_raw_public_key(kyber_pkey, public_key, &public_key_size), S2N_FAILURE); + POSIX_ENSURE(EVP_PKEY_get_raw_private_key(kyber_pkey, private_key, &private_key_size), S2N_FAILURE); + + return S2N_SUCCESS; +} + +int s2n_kyber_512_evp_encapsulate(uint8_t *ciphertext, uint8_t *shared_secret, + const uint8_t *public_key) { + size_t public_key_size = S2N_KYBER_512_R3_PUBLIC_KEY_BYTES; + EVP_PKEY *kyber_pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_KYBER512, NULL, public_key, public_key_size); + POSIX_GUARD_PTR(kyber_pkey); + + EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new(kyber_pkey, NULL); + POSIX_GUARD_PTR(kyber_pkey_ctx); + + size_t cipher_text_size = S2N_KYBER_512_R3_CIPHERTEXT_BYTES; + size_t shared_secret_size = S2N_KYBER_512_R3_SHARED_SECRET_BYTES; + POSIX_ENSURE(EVP_PKEY_encapsulate(kyber_pkey_ctx, ciphertext, &cipher_text_size, shared_secret, + &shared_secret_size), S2N_FAILURE); + return S2N_SUCCESS; +} + +int s2n_kyber_512_evp_decapsulate(uint8_t *shared_secret, const uint8_t *ciphertext, + const uint8_t *private_key) { + size_t private_key_size = S2N_KYBER_512_R3_SECRET_KEY_BYTES; + EVP_PKEY *kyber_pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_KYBER512, NULL, private_key, private_key_size); + POSIX_GUARD_PTR(kyber_pkey); + + EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new(kyber_pkey, NULL); + POSIX_GUARD_PTR(kyber_pkey_ctx); + + size_t shared_secret_size = S2N_KYBER_512_R3_SHARED_SECRET_BYTES; + POSIX_ENSURE(EVP_PKEY_decapsulate(kyber_pkey_ctx, shared_secret, &shared_secret_size, (uint8_t *) ciphertext, + S2N_KYBER_512_R3_CIPHERTEXT_BYTES), S2N_FAILURE); + return S2N_SUCCESS; +} +#else +int s2n_kyber_512_evp_generate_keypair(OUT uint8_t *public_key, OUT uint8_t *private_key) { + POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); +} + +int s2n_kyber_512_evp_encapsulate(OUT uint8_t *ciphertext, OUT uint8_t *shared_secret, + IN const uint8_t *public_key) { + POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); +} + +int s2n_kyber_512_evp_decapsulate(OUT uint8_t *shared_secret, IN const uint8_t *ciphertext, + IN const uint8_t *private_key) { + POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); +} +#endif diff --git a/contrib/restricted/aws/s2n/pq-crypto/s2n_kyber_512_evp.h b/contrib/restricted/aws/s2n/pq-crypto/s2n_kyber_512_evp.h new file mode 100644 index 0000000000..1a62a7cc92 --- /dev/null +++ b/contrib/restricted/aws/s2n/pq-crypto/s2n_kyber_512_evp.h @@ -0,0 +1,22 @@ +/* +* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"). +* You may not use this file except in compliance with the License. +* A copy of the License is located at +* +* http://aws.amazon.com/apache2.0 +* +* or in the "license" file accompanying this file. This file is distributed +* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +* express or implied. See the License for the specific language governing +* permissions and limitations under the License. +*/ + +#pragma once + +#include "tls/s2n_kem.h" + +int s2n_kyber_512_evp_generate_keypair(OUT uint8_t *public_key, OUT uint8_t *private_key); +int s2n_kyber_512_evp_encapsulate(OUT uint8_t *ciphertext, OUT uint8_t *shared_secret, IN const uint8_t *public_key); +int s2n_kyber_512_evp_decapsulate(OUT uint8_t *shared_secret, IN const uint8_t *ciphertext, IN const uint8_t *private_key); diff --git a/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.c b/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.c index e684aed377..abaf8f2d65 100644 --- a/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.c +++ b/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.c @@ -14,7 +14,9 @@ */ #include "s2n_pq.h" + #include "crypto/s2n_openssl.h" +#include "s2n_kyber_512_evp.h" static bool kyber512r3_avx2_bmi2_enabled = false; @@ -97,6 +99,13 @@ bool s2n_pq_is_enabled() { #endif } +bool s2n_libcrypto_supports_kyber_512() { +#if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER512) + return true; +#else + return false; +#endif +} S2N_RESULT s2n_disable_kyber512r3_opt_avx2_bmi2() { kyber512r3_avx2_bmi2_enabled = false; diff --git a/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.h b/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.h index 1c9ca37812..6ac074dc49 100644 --- a/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.h +++ b/contrib/restricted/aws/s2n/pq-crypto/s2n_pq.h @@ -26,4 +26,5 @@ S2N_RESULT s2n_try_enable_kyber512r3_opt_avx2_bmi2(void); S2N_RESULT s2n_disable_kyber512r3_opt_avx2_bmi2(void); bool s2n_pq_is_enabled(void); +bool s2n_libcrypto_supports_kyber_512(void); S2N_RESULT s2n_pq_init(void); |