Skip to content

Commit 8b10ecf

Browse files
alexw91lrstewart
andauthored
Remove s2n's internal Kyber512 implementation, and rely on AWS-LC for Kyber support (#4283)
--------- Co-authored-by: Lindsay Stewart <[email protected]>
1 parent a9ce5e4 commit 8b10ecf

File tree

108 files changed

+114
-7849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+114
-7849
lines changed

CMakeLists.txt

+1-59
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ set(VERSION_MAJOR 1)
1919
set(VERSION_MINOR 0)
2020
set(VERSION_PATCH 0)
2121

22-
option(S2N_NO_PQ "Disables all Post Quantum Crypto code. You likely want this
23-
for older compilers or uncommon platforms." OFF)
24-
option(S2N_NO_PQ_ASM "Turns off the ASM for PQ Crypto even if it's available for the toolchain.
25-
You likely want this on older compilers." OFF)
2622
option(SEARCH_LIBCRYPTO "Set this if you want to let S2N search libcrypto for you,
2723
otherwise a crypto target needs to be defined." ON)
2824
option(UNSAFE_TREAT_WARNINGS_AS_ERRORS "Compiler warnings are treated as errors. Warnings may
@@ -62,47 +58,25 @@ file(GLOB_RECURSE TLS_SRC "tls/*.c")
6258
file(GLOB UTILS_HEADERS "utils/*.h")
6359
file(GLOB UTILS_SRC "utils/*.c")
6460

65-
# Always include the top-level pq-crypto/ files
66-
file(GLOB PQ_HEADERS "pq-crypto/*.h")
67-
file(GLOB PQ_SRC "pq-crypto/*.c")
68-
6961
message(STATUS "Detected CMAKE_SYSTEM_PROCESSOR as ${CMAKE_SYSTEM_PROCESSOR}")
7062

7163
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
72-
message(STATUS "Detected 32-Bit system - disabling PQ crypto assembly optimizations")
73-
set(S2N_NO_PQ_ASM ON)
64+
message(STATUS "Detected 32-Bit system")
7465
else()
7566
message(STATUS "Detected 64-Bit system")
7667
endif()
7768

78-
if(S2N_NO_PQ)
79-
# PQ is disabled, so we do not include any PQ crypto code
80-
message(STATUS "S2N_NO_PQ flag was detected - disabling PQ crypto")
81-
set(S2N_NO_PQ_ASM ON)
82-
else()
83-
# PQ is enabled, so include all of the PQ crypto code
84-
file(GLOB PQ_HEADERS
85-
"pq-crypto/*.h"
86-
"pq-crypto/kyber_r3/*.h")
87-
88-
file(GLOB PQ_SRC
89-
"pq-crypto/*.c"
90-
"pq-crypto/kyber_r3/*.c")
91-
endif()
92-
9369
##be nice to visual studio users
9470
if(MSVC)
9571
source_group("Header Files\\s2n\\api" FILES ${API_HEADERS} ${API_UNSTABLE_HEADERS})
9672
source_group("Header Files\\s2n\\crypto" FILES ${CRYPTO_HEADERS})
9773
source_group("Header Files\\s2n\\error" FILES ${ERROR_HEADERS})
98-
source_group("Header Files\\s2n\\pq-crypto" FILES ${PQ_HEADERS})
9974
source_group("Header Files\\s2n\\stuffer" FILES ${STUFFER_HEADERS})
10075
source_group("Header Files\\s2n\\tls" FILES ${TLS_HEADERS})
10176
source_group("Header Files\\s2n\\utils" FILES ${UTILS_HEADERS})
10277

10378
source_group("Source Files\\crypto" FILES ${CRYPTO_SRC})
10479
source_group("Source Files\\error" FILES ${ERROR_SRC})
105-
source_group("Source Files\\pq-crypto" FILES ${PQ_SRC})
10680
source_group("Source Files\\stuffer" FILES ${STUFFER_SRC})
10781
source_group("Source Files\\tls" FILES ${TLS_SRC})
10882
source_group("Source Files\\utils" FILES ${UTILS_SRC})
@@ -135,7 +109,6 @@ file(GLOB S2N_HEADERS
135109
${API_UNSTABLE_HEADERS}
136110
${CRYPTO_HEADERS}
137111
${ERROR_HEADERS}
138-
${PQ_HEADERS}
139112
${STUFFER_HEADERS}
140113
${TLS_HEADERS}
141114
${UTILS_HEADERS}
@@ -144,7 +117,6 @@ file(GLOB S2N_HEADERS
144117
file(GLOB S2N_SRC
145118
${CRYPTO_SRC}
146119
${ERROR_SRC}
147-
${PQ_SRC}
148120
${STUFFER_SRC}
149121
${TLS_SRC}
150122
${UTILS_SRC}
@@ -186,10 +158,6 @@ if(NOT APPLE)
186158
set(CMAKE_SHARED_LINKER_FLAGS -Wl,-z,noexecstack,-z,relro,-z,now)
187159
endif()
188160

189-
if(S2N_NO_PQ)
190-
add_definitions(-DS2N_NO_PQ)
191-
endif()
192-
193161
# Whether to fail the build when compiling s2n's portable C code with non-portable assembly optimizations. Doing this
194162
# can lead to runtime crashes if build artifacts are built on modern hardware, but deployed to older hardware without
195163
# newer CPU instructions. s2n, by default, should be backwards compatible with older CPU types so this flag should be
@@ -367,32 +335,6 @@ if (NOT S2N_EXECINFO_AVAILABLE)
367335
endif()
368336
feature_probe_result(S2N_STACKTRACE ${S2N_STACKTRACE})
369337

370-
set(S2N_KYBER512R3_AVX2_BMI2 FALSE)
371-
if(NOT S2N_NO_PQ_ASM)
372-
# Kyber Round-3 code has several different optimizations which require
373-
# specific compiler flags to be supported by the compiler.
374-
# So for each needed instruction set extension we check if the compiler
375-
# supports it and set proper compiler flags to be added later to the
376-
# Kyber compilation units.
377-
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|amd64|AMD64)$")
378-
# Some platforms support -mavx2 flag but not m256 intrinsics required to use them. Only enable Kyber assembly
379-
# optimizations if both are supported. See https://github.com/aws/s2n-tls/pull/3005 for more info.
380-
if(S2N_KYBER512R3_AVX2_BMI2_SUPPORTED AND S2N_KYBER512R3_M256_INTRINSICS_SUPPORTED)
381-
set(S2N_KYBER512R3_AVX2_BMI2 TRUE)
382-
enable_language(ASM)
383-
384-
# add the assembly files to the project
385-
FILE(GLOB KYBER512R3_AVX2_BMI2_ASM_SRCS "pq-crypto/kyber_r3/*_avx2.S")
386-
target_sources(${PROJECT_NAME} PRIVATE ${KYBER512R3_AVX2_BMI2_ASM_SRCS})
387-
388-
# compile the C files with avx flags
389-
FILE(GLOB KYBER512R3_AVX2_BMI2_SRCS "pq-crypto/kyber_r3/*_avx2.c")
390-
set_source_files_properties(${KYBER512R3_AVX2_BMI2_SRCS} PROPERTIES COMPILE_FLAGS ${S2N_KYBER512R3_AVX2_BMI2_SUPPORTED_FLAGS})
391-
endif()
392-
endif()
393-
endif()
394-
feature_probe_result(S2N_KYBER512R3_AVX2_BMI2 ${S2N_KYBER512R3_AVX2_BMI2})
395-
396338
if (S2N_INTERN_LIBCRYPTO)
397339

398340
# Check if the AWS::crypto target has beeen added and handle it

LICENSE

-22
Original file line numberDiff line numberDiff line change
@@ -200,25 +200,3 @@
200200
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201201
See the License for the specific language governing permissions and
202202
limitations under the License.
203-
204-
205-
============================================================================
206-
S2N SUBCOMPONENTS:
207-
208-
The s2n Project contains subcomponents with separate copyright notices
209-
and license terms. Your use of the source code for these subcomponents is
210-
subject to the terms and conditions of the following licenses.
211-
212-
213-
========================================================================
214-
Third party MIT licenses
215-
========================================================================
216-
217-
The following components are provided under the MIT License. See project link for details.
218-
219-
220-
SIKE
221-
-> s2n/pq-crypto/sike_r1/LICENSE.txt
222-
223-
224-

Makefile

+1-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ include s2n.mk
4747

4848
.PHONY : libs
4949
libs:
50-
$(MAKE) -C pq-crypto
5150
$(MAKE) -C utils
5251
$(MAKE) -C error
5352
$(MAKE) -C stuffer
@@ -101,12 +100,11 @@ run-lcov:
101100
$(MAKE) -C bin lcov
102101
$(MAKE) -C crypto lcov
103102
$(MAKE) -C error lcov
104-
$(MAKE) -C pq-crypto run-lcov
105103
$(MAKE) -C stuffer lcov
106104
$(MAKE) -C tests lcov
107105
$(MAKE) -C tls run-lcov
108106
$(MAKE) -C utils lcov
109-
lcov -a crypto/coverage.info -a error/coverage.info -a pq-crypto/coverage.info -a pq-crypto/kyber_r3/coverage.info -a stuffer/coverage.info -a tls/coverage.info -a $(wildcard tls/*/coverage.info) -a utils/coverage.info --output ${COVERAGE_DIR}/all_coverage.info
107+
lcov -a crypto/coverage.info -a error/coverage.info -a stuffer/coverage.info -a tls/coverage.info -a $(wildcard tls/*/coverage.info) -a utils/coverage.info --output ${COVERAGE_DIR}/all_coverage.info
110108

111109
.PHONY : run-genhtml
112110
run-genhtml:
@@ -115,7 +113,6 @@ run-genhtml:
115113

116114
.PHONY : indent
117115
indent:
118-
$(MAKE) -C pq-crypto indentsource
119116
$(MAKE) -C tests indentsource
120117
$(MAKE) -C stuffer indentsource
121118
$(MAKE) -C crypto indentsource
@@ -147,7 +144,6 @@ uninstall:
147144

148145
.PHONY : clean
149146
clean:
150-
$(MAKE) -C pq-crypto clean
151147
$(MAKE) -C tests clean
152148
$(MAKE) -C stuffer decruft
153149
$(MAKE) -C crypto decruft

bindings/rust/generate.sh

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ cp -r \
1919
../../api \
2020
../../crypto \
2121
../../error \
22-
../../pq-crypto \
2322
../../stuffer \
2423
../../tls \
2524
../../utils \

bindings/rust/s2n-tls-sys/build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ fn build_vendored() {
9393

9494
let mut build = builder(&libcrypto);
9595

96+
// TODO: update rust bindings to handle no pq-crypto dir
97+
9698
let pq = option_env("CARGO_FEATURE_PQ").is_some();
9799

98100
// TODO each pq section needs to be built separately since it

codebuild/bin/grep_simple_mistakes.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ FAILED=0
1818
# Grep for any instances of raw memcpy() function. s2n code should instead be
1919
# using one of the *_ENSURE_MEMCPY macros.
2020
#############################################
21-
S2N_FILES_ASSERT_NOT_USING_MEMCPY=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*" -not -path "*/pq-crypto/*")
21+
S2N_FILES_ASSERT_NOT_USING_MEMCPY=$(find "$PWD" -type f -name "s2n*.[ch]" -not -path "*/tests/*")
2222
for file in $S2N_FILES_ASSERT_NOT_USING_MEMCPY; do
2323
RESULT_NUM_LINES=`grep 'memcpy(' $file | wc -l`
2424
if [ "${RESULT_NUM_LINES}" != 0 ]; then
@@ -180,7 +180,7 @@ done
180180
## Assert that there are no new uses of S2N_ERROR_IF
181181
# TODO add crypto, tls (see https://github.com/aws/s2n-tls/issues/2635)
182182
#############################################
183-
S2N_ERROR_IF_FREE="bin error pq-crypto scram stuffer utils tests"
183+
S2N_ERROR_IF_FREE="bin error scram stuffer utils tests"
184184
for dir in $S2N_ERROR_IF_FREE; do
185185
files=$(find "$dir" -type f -name "*.c" -path "*")
186186
for file in $files; do

compliance/generate_report.sh

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ duvet \
1515
--source-pattern '(*=,*#)bin/**/*.[ch]' \
1616
--source-pattern '(*=,*#)crypto/**/*.[ch]' \
1717
--source-pattern '(*=,*#)error/**/*.[ch]' \
18-
--source-pattern '(*=,*#)pq-crypto/**/*.[ch]' \
1918
--source-pattern '(*=,*#)stuffer/**/*.[ch]' \
2019
--source-pattern '(*=,*#)tests/**/*.[ch]' \
2120
--source-pattern '(*=,*#)tls/**/*.[ch]' \

pq-crypto/s2n_kyber_evp.c crypto/s2n_kyber_evp.c

+6-21
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616
#include <openssl/evp.h>
1717
#include <stddef.h>
1818

19+
#include "crypto/s2n_pq.h"
1920
#include "error/s2n_errno.h"
20-
#include "pq-crypto/s2n_pq.h"
2121
#include "tls/s2n_kem.h"
2222
#include "utils/s2n_safety.h"
2323
#include "utils/s2n_safety_macros.h"
2424

25-
#if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER) && !defined(S2N_NO_PQ)
25+
#if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
2626

2727
DEFINE_POINTER_CLEANUP_FUNC(EVP_PKEY *, EVP_PKEY_free);
2828
DEFINE_POINTER_CLEANUP_FUNC(EVP_PKEY_CTX *, EVP_PKEY_CTX_free);
2929

3030
int s2n_kyber_evp_generate_keypair(IN const struct s2n_kem *kem, OUT uint8_t *public_key,
3131
OUT uint8_t *secret_key)
3232
{
33-
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
3433
DEFER_CLEANUP(EVP_PKEY_CTX *kyber_pkey_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_KEM, NULL), EVP_PKEY_CTX_free_pointer);
3534
POSIX_GUARD_PTR(kyber_pkey_ctx);
3635
POSIX_GUARD_OSSL(EVP_PKEY_CTX_kem_set_params(kyber_pkey_ctx, kem->kem_nid), S2N_ERR_PQ_CRYPTO);
@@ -53,7 +52,6 @@ int s2n_kyber_evp_generate_keypair(IN const struct s2n_kem *kem, OUT uint8_t *pu
5352
int s2n_kyber_evp_encapsulate(IN const struct s2n_kem *kem, OUT uint8_t *ciphertext, OUT uint8_t *shared_secret,
5453
IN const uint8_t *public_key)
5554
{
56-
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
5755
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);
5856
POSIX_GUARD_PTR(kyber_pkey);
5957

@@ -74,7 +72,6 @@ int s2n_kyber_evp_encapsulate(IN const struct s2n_kem *kem, OUT uint8_t *ciphert
7472
int s2n_kyber_evp_decapsulate(IN const struct s2n_kem *kem, OUT uint8_t *shared_secret, IN const uint8_t *ciphertext,
7573
IN const uint8_t *private_key)
7674
{
77-
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
7875
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);
7976
POSIX_GUARD_PTR(kyber_pkey);
8077

@@ -90,36 +87,24 @@ int s2n_kyber_evp_decapsulate(IN const struct s2n_kem *kem, OUT uint8_t *shared_
9087
return S2N_SUCCESS;
9188
}
9289

93-
#elif !defined(S2N_NO_PQ) /* Use interned Kyber512 implementation, otherwise bail. */
90+
#else /* If !S2N_LIBCRYPTO_SUPPORTS_KYBER, we won't have a Kyber impl so define relevant stubs here. */
9491

9592
int s2n_kyber_evp_generate_keypair(IN const struct s2n_kem *kem, OUT uint8_t *public_key,
9693
OUT uint8_t *secret_key)
9794
{
98-
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
99-
if (kem == &s2n_kyber_512_r3) {
100-
return s2n_kyber_512_r3_crypto_kem_keypair(kem, public_key, secret_key);
101-
}
102-
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
95+
POSIX_BAIL(S2N_ERR_NO_SUPPORTED_LIBCRYPTO_API);
10396
}
10497

10598
int s2n_kyber_evp_encapsulate(IN const struct s2n_kem *kem, OUT uint8_t *ciphertext, OUT uint8_t *shared_secret,
10699
IN const uint8_t *public_key)
107100
{
108-
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
109-
if (kem == &s2n_kyber_512_r3) {
110-
return s2n_kyber_512_r3_crypto_kem_enc(kem, ciphertext, shared_secret, public_key);
111-
}
112-
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
101+
POSIX_BAIL(S2N_ERR_NO_SUPPORTED_LIBCRYPTO_API);
113102
}
114103

115104
int s2n_kyber_evp_decapsulate(IN const struct s2n_kem *kem, OUT uint8_t *shared_secret, IN const uint8_t *ciphertext,
116105
IN const uint8_t *secret_key)
117106
{
118-
POSIX_ENSURE(s2n_pq_is_enabled(), S2N_ERR_PQ_DISABLED);
119-
if (kem == &s2n_kyber_512_r3) {
120-
return s2n_kyber_512_r3_crypto_kem_dec(kem, shared_secret, ciphertext, secret_key);
121-
}
122-
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
107+
POSIX_BAIL(S2N_ERR_NO_SUPPORTED_LIBCRYPTO_API);
123108
}
124109

125110
#endif
File renamed without changes.

pq-crypto/s2n_pq_random.h crypto/s2n_pq.c

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
#pragma once
16+
#include "s2n_pq.h"
1717

18-
#include "utils/s2n_result.h"
18+
#include "crypto/s2n_openssl.h"
1919

20-
typedef S2N_RESULT (*s2n_get_random_bytes_callback)(uint8_t *buffer, uint32_t num_bytes);
20+
bool s2n_libcrypto_supports_kyber()
21+
{
22+
/* S2N_LIBCRYPTO_SUPPORTS_KYBER will be auto-detected and #defined if
23+
* ./tests/features/S2N_LIBCRYPTO_SUPPORTS_KYBER.c successfully compiles
24+
*/
25+
#if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
26+
return true;
27+
#else
28+
return false;
29+
#endif
30+
}
2131

22-
S2N_RESULT s2n_get_random_bytes(uint8_t *buffer, uint32_t num_bytes);
23-
S2N_RESULT s2n_set_rand_bytes_callback_for_testing(s2n_get_random_bytes_callback rand_bytes_callback);
32+
bool s2n_pq_is_enabled()
33+
{
34+
return s2n_libcrypto_supports_kyber();
35+
}

pq-crypto/s2n_pq.h crypto/s2n_pq.h

-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@
1818
#include <stdbool.h>
1919

2020
#include "crypto/s2n_fips.h"
21-
#include "pq-crypto/s2n_pq_asm.h"
2221
#include "utils/s2n_result.h"
2322
#include "utils/s2n_safety.h"
2423

25-
bool s2n_kyber512r3_is_avx2_bmi2_enabled(void);
26-
S2N_RESULT s2n_try_enable_kyber512r3_opt_avx2_bmi2(void);
27-
S2N_RESULT s2n_disable_kyber512r3_opt_avx2_bmi2(void);
28-
2924
bool s2n_pq_is_enabled(void);
3025
bool s2n_libcrypto_supports_kyber(void);
31-
S2N_RESULT s2n_pq_init(void);

error/s2n_errno.c

-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ static const char *no_such_error = "Internal s2n error";
263263
ERR_ENTRY(S2N_ERR_INVALID_STATE, "Invalid state, this is the result of invalid use of an API. Check the API documentation for the function that raised this error for more info") \
264264
ERR_ENTRY(S2N_ERR_UNSUPPORTED_WITH_QUIC, "Functionality not supported when running with QUIC support enabled") \
265265
ERR_ENTRY(S2N_ERR_PQ_CRYPTO, "An error occurred in a post-quantum crypto function") \
266-
ERR_ENTRY(S2N_ERR_PQ_DISABLED, "Post-quantum crypto is disabled") \
267266
ERR_ENTRY(S2N_ERR_DUPLICATE_PSK_IDENTITIES, "The list of pre-shared keys provided contains duplicate psk identities") \
268267
ERR_ENTRY(S2N_ERR_OFFERED_PSKS_TOO_LONG, "The total pre-shared key data is too long to send over the wire") \
269268
ERR_ENTRY(S2N_ERR_INVALID_SESSION_TICKET, "Session ticket data is not valid") \

error/s2n_errno.h

-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ typedef enum {
222222
S2N_ERR_ASYNC_CALLBACK_FAILED,
223223
S2N_ERR_ASYNC_MORE_THAN_ONE,
224224
S2N_ERR_PQ_CRYPTO,
225-
S2N_ERR_PQ_DISABLED,
226225
S2N_ERR_INVALID_CERT_STATE,
227226
S2N_ERR_INVALID_EARLY_DATA_STATE,
228227
S2N_ERR_PKEY_CTX_INIT,

lib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# permissions and limitations under the License.
1414
#
1515

16-
OBJS = $(wildcard ../utils/*.o ../stuffer/*.o ../tls/*.o ../tls/*/*.o ../iana/*.o ../crypto/*.o ../error/*.o ../pq-crypto/*.o ../pq-crypto/kyber_r3/*.o)
16+
OBJS = $(wildcard ../utils/*.o ../stuffer/*.o ../tls/*.o ../tls/*/*.o ../iana/*.o ../crypto/*.o ../error/*.o)
1717

1818
.PHONY : all
1919
all: libs2n.a libs2n.so libs2n.dylib

0 commit comments

Comments
 (0)