From 109faef770994e71b6bafcc015e2e96b88a4af8c Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 3 Oct 2019 18:30:58 +0100 Subject: [PATCH] Use .note.GNU-stack section in ASM. (#137) Motivation: Well-formed Linux assembly code declares a .note.GNU-stack section in order to help out the linker. When absent, the linker makes some suboptimal linking decisions that can cause pain in running programs. Modifications: - Amend the vendoring script to add the .note.GNU-stack section to all assembly files on Linux. - Updated BoringSSL Results: Better linker behaviour. --- Package.swift | 2 +- Sources/CNIOBoringSSL/crypto/asn1/tasn_enc.c | 2 +- .../crypto/chacha/chacha-armv4.ios.arm.S | 4 + .../crypto/chacha/chacha-armv4.linux.arm.S | 4 + .../crypto/chacha/chacha-armv8.ios.aarch64.S | 4 + .../chacha/chacha-armv8.linux.aarch64.S | 4 + .../crypto/chacha/chacha-x86.linux.x86.S | 4 + .../chacha/chacha-x86_64.linux.x86_64.S | 4 + .../crypto/chacha/chacha-x86_64.mac.x86_64.S | 4 + .../aes128gcmsiv-x86_64.linux.x86_64.S | 4 + .../aes128gcmsiv-x86_64.mac.x86_64.S | 4 + .../chacha20_poly1305_x86_64.linux.x86_64.S | 4 + .../chacha20_poly1305_x86_64.mac.x86_64.S | 4 + Sources/CNIOBoringSSL/crypto/cpu-intel.c | 8 +- Sources/CNIOBoringSSL/crypto/crypto.c | 2 +- .../crypto/curve25519/asm/x25519-asm-arm.S | 4 + Sources/CNIOBoringSSL/crypto/dsa/dsa.c | 2 +- .../CNIOBoringSSL/crypto/ec_extra/ec_asn1.c | 24 +- Sources/CNIOBoringSSL/crypto/err/err_data.c | 854 +++++------ Sources/CNIOBoringSSL/crypto/evp/evp.c | 5 + .../crypto/fipsmodule/aes-586.linux.x86.S | 4 + .../crypto/fipsmodule/aes-armv4.ios.arm.S | 4 + .../crypto/fipsmodule/aes-armv4.linux.arm.S | 4 + .../fipsmodule/aes-x86_64.linux.x86_64.S | 4 + .../crypto/fipsmodule/aes-x86_64.mac.x86_64.S | 4 + .../crypto/fipsmodule/aes/internal.h | 23 +- .../aesni-gcm-x86_64.linux.x86_64.S | 8 +- .../fipsmodule/aesni-gcm-x86_64.mac.x86_64.S | 8 +- .../crypto/fipsmodule/aesni-x86.linux.x86.S | 12 +- .../fipsmodule/aesni-x86_64.linux.x86_64.S | 16 +- .../fipsmodule/aesni-x86_64.mac.x86_64.S | 16 +- .../crypto/fipsmodule/aesv8-armx32.ios.arm.S | 4 + .../fipsmodule/aesv8-armx32.linux.arm.S | 4 + .../fipsmodule/aesv8-armx64.ios.aarch64.S | 4 + .../fipsmodule/aesv8-armx64.linux.aarch64.S | 4 + .../crypto/fipsmodule/armv4-mont.ios.arm.S | 4 + .../crypto/fipsmodule/armv4-mont.linux.arm.S | 4 + .../fipsmodule/armv8-mont.ios.aarch64.S | 4 + .../fipsmodule/armv8-mont.linux.aarch64.S | 4 + .../crypto/fipsmodule/bn-586.linux.x86.S | 4 + .../CNIOBoringSSL/crypto/fipsmodule/bn/ctx.c | 4 + .../crypto/fipsmodule/bn/prime.c | 18 +- .../crypto/fipsmodule/bsaes-armv7.ios.arm.S | 4 + .../crypto/fipsmodule/bsaes-armv7.linux.arm.S | 4 + .../crypto/fipsmodule/cipher/cipher.c | 5 +- .../crypto/fipsmodule/cipher/e_aes.c | 106 +- .../crypto/fipsmodule/co-586.linux.x86.S | 4 + .../crypto/fipsmodule/digest/digest.c | 2 + .../crypto/fipsmodule/ec/ec_key.c | 4 +- .../crypto/fipsmodule/ghash-armv4.ios.arm.S | 4 + .../crypto/fipsmodule/ghash-armv4.linux.arm.S | 4 + .../fipsmodule/ghash-neon-armv8.ios.aarch64.S | 4 + .../ghash-neon-armv8.linux.aarch64.S | 4 + .../fipsmodule/ghash-ssse3-x86.linux.x86.S | 4 + .../ghash-ssse3-x86_64.linux.x86_64.S | 4 + .../ghash-ssse3-x86_64.mac.x86_64.S | 4 + .../crypto/fipsmodule/ghash-x86.linux.x86.S | 4 + .../fipsmodule/ghash-x86_64.linux.x86_64.S | 4 + .../fipsmodule/ghash-x86_64.mac.x86_64.S | 4 + .../fipsmodule/ghashv8-armx32.ios.arm.S | 4 + .../fipsmodule/ghashv8-armx32.linux.arm.S | 4 + .../fipsmodule/ghashv8-armx64.ios.aarch64.S | 4 + .../fipsmodule/ghashv8-armx64.linux.aarch64.S | 4 + .../crypto/fipsmodule/md5-586.linux.x86.S | 4 + .../fipsmodule/md5-x86_64.linux.x86_64.S | 4 + .../crypto/fipsmodule/md5-x86_64.mac.x86_64.S | 4 + .../fipsmodule/p256-x86_64-asm.linux.x86_64.S | 4 + .../fipsmodule/p256-x86_64-asm.mac.x86_64.S | 4 + .../p256_beeu-x86_64-asm.linux.x86_64.S | 4 + .../p256_beeu-x86_64-asm.mac.x86_64.S | 4 + .../crypto/fipsmodule/rand/internal.h | 13 + .../crypto/fipsmodule/rand/rand.c | 14 +- .../crypto/fipsmodule/rand/urandom.c | 215 ++- .../fipsmodule/rdrand-x86_64.linux.x86_64.S | 4 + .../fipsmodule/rdrand-x86_64.mac.x86_64.S | 4 + .../crypto/fipsmodule/rsa/rsa_impl.c | 2 +- .../fipsmodule/rsaz-avx2.linux.x86_64.S | 4 + .../crypto/fipsmodule/rsaz-avx2.mac.x86_64.S | 4 + .../crypto/fipsmodule/self_check/self_check.c | 67 +- .../crypto/fipsmodule/sha1-586.linux.x86.S | 4 + .../fipsmodule/sha1-armv4-large.ios.arm.S | 4 + .../fipsmodule/sha1-armv4-large.linux.arm.S | 4 + .../fipsmodule/sha1-armv8.ios.aarch64.S | 4 + .../fipsmodule/sha1-armv8.linux.aarch64.S | 4 + .../fipsmodule/sha1-x86_64.linux.x86_64.S | 4 + .../fipsmodule/sha1-x86_64.mac.x86_64.S | 4 + .../crypto/fipsmodule/sha256-586.linux.x86.S | 4 + .../crypto/fipsmodule/sha256-armv4.ios.arm.S | 4 + .../fipsmodule/sha256-armv4.linux.arm.S | 4 + .../fipsmodule/sha256-armv8.ios.aarch64.S | 4 + .../fipsmodule/sha256-armv8.linux.aarch64.S | 4 + .../fipsmodule/sha256-x86_64.linux.x86_64.S | 4 + .../fipsmodule/sha256-x86_64.mac.x86_64.S | 4 + .../crypto/fipsmodule/sha512-586.linux.x86.S | 4 + .../crypto/fipsmodule/sha512-armv4.ios.arm.S | 4 + .../fipsmodule/sha512-armv4.linux.arm.S | 4 + .../fipsmodule/sha512-armv8.ios.aarch64.S | 4 + .../fipsmodule/sha512-armv8.linux.aarch64.S | 4 + .../fipsmodule/sha512-x86_64.linux.x86_64.S | 4 + .../fipsmodule/sha512-x86_64.mac.x86_64.S | 4 + .../crypto/fipsmodule/vpaes-armv7.ios.arm.S | 1272 +++++++++++++++++ .../crypto/fipsmodule/vpaes-armv7.linux.arm.S | 1242 ++++++++++++++++ .../fipsmodule/vpaes-armv8.ios.aarch64.S | 4 + .../fipsmodule/vpaes-armv8.linux.aarch64.S | 4 + .../crypto/fipsmodule/vpaes-x86.linux.x86.S | 10 +- .../fipsmodule/vpaes-x86_64.linux.x86_64.S | 12 +- .../fipsmodule/vpaes-x86_64.mac.x86_64.S | 12 +- .../crypto/fipsmodule/x86-mont.linux.x86.S | 4 + .../fipsmodule/x86_64-mont.linux.x86_64.S | 4 + .../fipsmodule/x86_64-mont.mac.x86_64.S | 4 + .../fipsmodule/x86_64-mont5.linux.x86_64.S | 4 + .../fipsmodule/x86_64-mont5.mac.x86_64.S | 4 + .../crypto/hrss/asm/poly_rq_mul.S | 4 + Sources/CNIOBoringSSL/crypto/internal.h | 18 + .../crypto/poly1305/poly1305_arm_asm.S | 4 + .../sike/asm/fp-armv8.ios.aarch64.S | 4 + .../sike/asm/fp-armv8.linux.aarch64.S | 4 + .../sike/asm/fp-x86_64.linux.x86_64.S | 4 + .../sike/asm/fp-x86_64.mac.x86_64.S | 4 + Sources/CNIOBoringSSL/crypto/x509v3/v3_alt.c | 9 +- Sources/CNIOBoringSSL/crypto/x509v3/v3_purp.c | 2 +- Sources/CNIOBoringSSL/hash.txt | 2 +- .../include/CNIOBoringSSL_aead.h | 13 + .../include/CNIOBoringSSL_base.h | 5 + .../CNIOBoringSSL_boringssl_prefix_symbols.h | 7 +- ...IOBoringSSL_boringssl_prefix_symbols_asm.h | 7 +- .../include/CNIOBoringSSL_cipher.h | 7 +- .../CNIOBoringSSL/include/CNIOBoringSSL_cpu.h | 7 +- .../include/CNIOBoringSSL_crypto.h | 8 +- .../include/CNIOBoringSSL_digest.h | 10 +- .../include/CNIOBoringSSL_ec_key.h | 4 +- .../CNIOBoringSSL/include/CNIOBoringSSL_evp.h | 10 + .../CNIOBoringSSL/include/CNIOBoringSSL_ssl.h | 70 +- .../include/boringssl_prefix_symbols_nasm.inc | 14 +- Sources/CNIOBoringSSL/ssl/d1_both.cc | 4 +- Sources/CNIOBoringSSL/ssl/handoff.cc | 4 +- Sources/CNIOBoringSSL/ssl/handshake.cc | 34 +- Sources/CNIOBoringSSL/ssl/handshake_client.cc | 13 +- Sources/CNIOBoringSSL/ssl/internal.h | 81 +- Sources/CNIOBoringSSL/ssl/s3_both.cc | 6 +- Sources/CNIOBoringSSL/ssl/ssl_buffer.cc | 8 +- Sources/CNIOBoringSSL/ssl/ssl_cipher.cc | 4 + Sources/CNIOBoringSSL/ssl/ssl_lib.cc | 134 +- Sources/CNIOBoringSSL/ssl/ssl_versions.cc | 44 +- Sources/CNIOBoringSSL/ssl/tls13_both.cc | 15 +- Sources/CNIOBoringSSL/ssl/tls13_client.cc | 50 +- Sources/CNIOBoringSSL/ssl/tls13_enc.cc | 472 +++--- Sources/CNIOBoringSSL/ssl/tls13_server.cc | 122 +- scripts/vendor-boringssl.sh | 7 + 149 files changed, 4445 insertions(+), 1078 deletions(-) create mode 100644 Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.ios.arm.S create mode 100644 Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.linux.arm.S diff --git a/Package.swift b/Package.swift index f500333d..b625ca3a 100644 --- a/Package.swift +++ b/Package.swift @@ -22,7 +22,7 @@ import PackageDescription // Sources/CNIOBoringSSL directory. The source repository is at // https://boringssl.googlesource.com/boringssl. // -// BoringSSL Commit: 9806ae005b3085683a2fcf2953e4170893e0973d +// BoringSSL Commit: 6e7255c17e1a7348a2377fbc804441dd284806e2 let package = Package( name: "swift-nio-ssl", diff --git a/Sources/CNIOBoringSSL/crypto/asn1/tasn_enc.c b/Sources/CNIOBoringSSL/crypto/asn1/tasn_enc.c index e05a55ea..ecf6230c 100644 --- a/Sources/CNIOBoringSSL/crypto/asn1/tasn_enc.c +++ b/Sources/CNIOBoringSSL/crypto/asn1/tasn_enc.c @@ -192,7 +192,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, /* Use indefinite length constructed if requested */ if (aclass & ASN1_TFLG_NDEF) ndef = 2; - /* fall through */ + OPENSSL_FALLTHROUGH; case ASN1_ITYPE_SEQUENCE: i = asn1_enc_restore(&seqcontlen, out, pval, it); diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.ios.arm.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.ios.arm.S index f358889b..15de9034 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.ios.arm.S @@ -1499,3 +1499,7 @@ OPENSSL_armcap_P: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.linux.arm.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.linux.arm.S index 21e6d4ec..0ea2f2df 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv4.linux.arm.S @@ -1493,3 +1493,7 @@ ChaCha20_neon: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S index 76e22084..41fbc171 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.ios.aarch64.S @@ -1983,3 +1983,7 @@ Ldone_512_neon: #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S index 8fddd49c..06a671ff 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-armv8.linux.aarch64.S @@ -1985,3 +1985,7 @@ ChaCha20_512_neon: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86.linux.x86.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86.linux.x86.S index 1b041a98..dcffd50e 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86.linux.x86.S @@ -975,3 +975,7 @@ ChaCha20_ssse3: .byte 114,103,62,0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.linux.x86_64.S index 99e2c008..580bba11 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.linux.x86_64.S @@ -1633,3 +1633,7 @@ ChaCha20_8x: .size ChaCha20_8x,.-ChaCha20_8x #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.mac.x86_64.S index 40a91dae..8a4543d9 100644 --- a/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/chacha/chacha-x86_64.mac.x86_64.S @@ -1626,3 +1626,7 @@ L$8x_epilogue: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S index 3bc1aeab..210c5ba4 100644 --- a/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S @@ -3079,3 +3079,7 @@ aes256gcmsiv_kdf: .size aes256gcmsiv_kdf, .-aes256gcmsiv_kdf #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S index a9a2839f..02049a16 100644 --- a/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S @@ -3069,3 +3069,7 @@ _aes256gcmsiv_kdf: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S index 5339d0f3..16b4b6e0 100644 --- a/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S @@ -8987,3 +8987,7 @@ seal_avx2_short_tail: .cfi_endproc #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S index 8d904153..12f6967a 100644 --- a/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S @@ -8986,3 +8986,7 @@ seal_avx2_short_tail: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/cpu-intel.c b/Sources/CNIOBoringSSL/crypto/cpu-intel.c index dcd07fb9..e67490e0 100644 --- a/Sources/CNIOBoringSSL/crypto/cpu-intel.c +++ b/Sources/CNIOBoringSSL/crypto/cpu-intel.c @@ -164,17 +164,23 @@ void OPENSSL_cpuid_setup(void) { if (is_amd) { // See https://www.amd.com/system/files/TechDocs/25481.pdf, page 10. const uint32_t base_family = (eax >> 8) & 15; + const uint32_t base_model = (eax >> 4) & 15; uint32_t family = base_family; + uint32_t model = base_model; if (base_family == 0xf) { const uint32_t ext_family = (eax >> 20) & 255; family += ext_family; + const uint32_t ext_model = (eax >> 16) & 15; + model |= ext_model << 4; } - if (family < 0x17) { + if (family < 0x17 || (family == 0x17 && 0x70 <= model && model <= 0x7f)) { // Disable RDRAND on AMD families before 0x17 (Zen) due to reported // failures after suspend. // https://bugzilla.redhat.com/show_bug.cgi?id=1150286 + // Also disable for family 0x17, models 0x70–0x7f, due to possible RDRAND + // failures there too. ecx &= ~(1u << 30); } } diff --git a/Sources/CNIOBoringSSL/crypto/crypto.c b/Sources/CNIOBoringSSL/crypto/crypto.c index ccca679c..d3d862a1 100644 --- a/Sources/CNIOBoringSSL/crypto/crypto.c +++ b/Sources/CNIOBoringSSL/crypto/crypto.c @@ -60,7 +60,7 @@ // that tests the capability values will still skip the constructor but, so // far, the init constructor function only sets the capability variables. -#if !defined(NDEBUG) && !defined(BORINGSSL_FIPS) +#if defined(BORINGSSL_DISPATCH_TEST) // This value must be explicitly initialised to zero in order to work around a // bug in libtool or the linker on OS X. // diff --git a/Sources/CNIOBoringSSL/crypto/curve25519/asm/x25519-asm-arm.S b/Sources/CNIOBoringSSL/crypto/curve25519/asm/x25519-asm-arm.S index 35d01747..f7cfa89c 100644 --- a/Sources/CNIOBoringSSL/crypto/curve25519/asm/x25519-asm-arm.S +++ b/Sources/CNIOBoringSSL/crypto/curve25519/asm/x25519-asm-arm.S @@ -2133,3 +2133,7 @@ bx lr #endif /* !OPENSSL_NO_ASM && __arm__ && !__APPLE__ */ #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/dsa/dsa.c b/Sources/CNIOBoringSSL/crypto/dsa/dsa.c index 5a03320f..bb1a18ff 100644 --- a/Sources/CNIOBoringSSL/crypto/dsa/dsa.c +++ b/Sources/CNIOBoringSSL/crypto/dsa/dsa.c @@ -79,7 +79,7 @@ #define OPENSSL_DSA_MAX_MODULUS_BITS 10000 // Primality test according to FIPS PUB 186[-1], Appendix 2.1: 50 rounds of -// Rabin-Miller +// Miller-Rabin. #define DSS_prime_checks 50 static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx_in, BIGNUM **out_kinv, diff --git a/Sources/CNIOBoringSSL/crypto/ec_extra/ec_asn1.c b/Sources/CNIOBoringSSL/crypto/ec_extra/ec_asn1.c index 538f9b34..10b5b38a 100644 --- a/Sources/CNIOBoringSSL/crypto/ec_extra/ec_asn1.c +++ b/Sources/CNIOBoringSSL/crypto/ec_extra/ec_asn1.c @@ -264,7 +264,8 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a, CBS *out_base_y, CBS *out_order) { // See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an // ECParameters while RFC 5480 calls it a SpecifiedECDomain. - CBS params, field_id, field_type, curve, base; + CBS params, field_id, field_type, curve, base, cofactor; + int has_cofactor; uint64_t version; if (!CBS_get_asn1(in, ¶ms, CBS_ASN1_SEQUENCE) || !CBS_get_asn1_uint64(¶ms, &version) || @@ -272,7 +273,8 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a, !CBS_get_asn1(¶ms, &field_id, CBS_ASN1_SEQUENCE) || !CBS_get_asn1(&field_id, &field_type, CBS_ASN1_OBJECT) || CBS_len(&field_type) != sizeof(kPrimeField) || - OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != 0 || + OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) != + 0 || !CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) || !is_unsigned_integer(out_prime) || CBS_len(&field_id) != 0 || @@ -280,16 +282,26 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a, !CBS_get_asn1(&curve, out_a, CBS_ASN1_OCTETSTRING) || !CBS_get_asn1(&curve, out_b, CBS_ASN1_OCTETSTRING) || // |curve| has an optional BIT STRING seed which we ignore. + !CBS_get_optional_asn1(&curve, NULL, NULL, CBS_ASN1_BITSTRING) || + CBS_len(&curve) != 0 || !CBS_get_asn1(¶ms, &base, CBS_ASN1_OCTETSTRING) || !CBS_get_asn1(¶ms, out_order, CBS_ASN1_INTEGER) || - !is_unsigned_integer(out_order)) { + !is_unsigned_integer(out_order) || + !CBS_get_optional_asn1(¶ms, &cofactor, &has_cofactor, + CBS_ASN1_INTEGER) || + CBS_len(¶ms) != 0) { OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR); return 0; } - // |params| has an optional cofactor which we ignore. With the optional seed - // in |curve|, a group already has arbitrarily many encodings. Parse enough to - // uniquely determine the curve. + if (has_cofactor) { + // We only support prime-order curves so the cofactor must be one. + if (CBS_len(&cofactor) != 1 || + CBS_data(&cofactor)[0] != 1) { + OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP); + return 0; + } + } // Require that the base point use uncompressed form. uint8_t form; diff --git a/Sources/CNIOBoringSSL/crypto/err/err_data.c b/Sources/CNIOBoringSSL/crypto/err/err_data.c index a581acee..03c71559 100644 --- a/Sources/CNIOBoringSSL/crypto/err/err_data.c +++ b/Sources/CNIOBoringSSL/crypto/err/err_data.c @@ -75,54 +75,54 @@ const uint32_t kOpenSSLReasonValues[] = { 0xc3b00ea, 0xc3b88d6, 0x10320847, - 0x10329585, - 0x10331591, - 0x103395aa, - 0x103415bd, + 0x1032959f, + 0x103315ab, + 0x103395c4, + 0x103415d7, 0x10348f27, 0x10350c60, - 0x103595d0, - 0x103615fa, - 0x1036960d, - 0x1037162c, - 0x10379645, - 0x1038165a, - 0x10389678, - 0x10391687, - 0x103996a3, - 0x103a16be, - 0x103a96cd, - 0x103b16e9, - 0x103b9704, - 0x103c172a, + 0x103595ea, + 0x10361614, + 0x10369627, + 0x10371646, + 0x1037965f, + 0x10381674, + 0x10389692, + 0x103916a1, + 0x103996bd, + 0x103a16d8, + 0x103a96e7, + 0x103b1703, + 0x103b971e, + 0x103c1744, 0x103c80ea, - 0x103d173b, - 0x103d974f, - 0x103e176e, - 0x103e977d, - 0x103f1794, - 0x103f97a7, + 0x103d1755, + 0x103d9769, + 0x103e1788, + 0x103e9797, + 0x103f17ae, + 0x103f97c1, 0x10400c24, - 0x104097ba, - 0x104117d8, - 0x104197eb, - 0x10421805, - 0x10429815, - 0x10431829, - 0x1043983f, - 0x10441857, - 0x1044986c, - 0x10451880, - 0x10459892, + 0x104097d4, + 0x104117f2, + 0x10419805, + 0x1042181f, + 0x1042982f, + 0x10431843, + 0x10439859, + 0x10441871, + 0x10449886, + 0x1045189a, + 0x104598ac, 0x104605fd, 0x1046894f, - 0x104718a7, - 0x104798be, - 0x104818d3, - 0x104898e1, + 0x104718c1, + 0x104798d8, + 0x104818ed, + 0x104898fb, 0x10490e73, - 0x1049971b, - 0x104a15e5, + 0x10499735, + 0x104a15ff, 0x14320c07, 0x14328c15, 0x14330c24, @@ -148,39 +148,40 @@ const uint32_t kOpenSSLReasonValues[] = { 0x183a1091, 0x183a90b7, 0x183b0c7f, - 0x183b90ec, - 0x183c10fe, - 0x183c9109, - 0x183d1119, - 0x183d912a, - 0x183e113b, - 0x183e914d, - 0x183f1176, - 0x183f918f, - 0x184011a7, + 0x183b9106, + 0x183c1118, + 0x183c9123, + 0x183d1133, + 0x183d9144, + 0x183e1155, + 0x183e9167, + 0x183f1190, + 0x183f91a9, + 0x184011c1, 0x184086d5, 0x184110da, 0x184190a5, 0x184210c4, 0x18428c6c, 0x18431080, - 0x203211e1, - 0x203291ce, - 0x243211ed, + 0x184390ec, + 0x203211fb, + 0x203291e8, + 0x24321207, 0x24328995, - 0x243311ff, - 0x2433920c, - 0x24341219, - 0x2434922b, - 0x2435123a, - 0x24359257, - 0x24361264, - 0x24369272, - 0x24371280, - 0x2437928e, - 0x24381297, - 0x243892a4, - 0x243912b7, + 0x24331219, + 0x24339226, + 0x24341233, + 0x24349245, + 0x24351254, + 0x24359271, + 0x2436127e, + 0x2436928c, + 0x2437129a, + 0x243792a8, + 0x243812b1, + 0x243892be, + 0x243912d1, 0x28320c54, 0x28328c7f, 0x28330c24, @@ -189,44 +190,44 @@ const uint32_t kOpenSSLReasonValues[] = { 0x283480ac, 0x283500ea, 0x28358c6c, - 0x2c322ef2, - 0x2c3292ce, - 0x2c332f00, - 0x2c33af12, - 0x2c342f26, - 0x2c34af38, - 0x2c352f53, - 0x2c35af65, - 0x2c362f78, + 0x2c322f0c, + 0x2c3292e8, + 0x2c332f1a, + 0x2c33af2c, + 0x2c342f40, + 0x2c34af52, + 0x2c352f6d, + 0x2c35af7f, + 0x2c362f92, 0x2c36832d, - 0x2c372f85, - 0x2c37af97, - 0x2c382fbc, - 0x2c38afd3, - 0x2c392fe1, - 0x2c39aff1, - 0x2c3a3003, - 0x2c3ab017, - 0x2c3b3028, - 0x2c3bb047, - 0x2c3c12e0, - 0x2c3c92f6, - 0x2c3d305b, - 0x2c3d930f, - 0x2c3e3078, - 0x2c3eb086, - 0x2c3f309e, - 0x2c3fb0b6, - 0x2c4030e0, - 0x2c4091e1, - 0x2c4130f1, - 0x2c41b104, - 0x2c4211a7, - 0x2c42b115, + 0x2c372f9f, + 0x2c37afb1, + 0x2c382fd6, + 0x2c38afed, + 0x2c392ffb, + 0x2c39b00b, + 0x2c3a301d, + 0x2c3ab031, + 0x2c3b3042, + 0x2c3bb061, + 0x2c3c12fa, + 0x2c3c9310, + 0x2c3d3075, + 0x2c3d9329, + 0x2c3e3092, + 0x2c3eb0a0, + 0x2c3f30b8, + 0x2c3fb0d0, + 0x2c4030fa, + 0x2c4091fb, + 0x2c41310b, + 0x2c41b11e, + 0x2c4211c1, + 0x2c42b12f, 0x2c430722, - 0x2c43b039, - 0x2c442faa, - 0x2c44b0c3, + 0x2c43b053, + 0x2c442fc4, + 0x2c44b0dd, 0x30320000, 0x30328015, 0x3033001f, @@ -361,241 +362,241 @@ const uint32_t kOpenSSLReasonValues[] = { 0x3c418d67, 0x3c420e73, 0x3c428dfd, - 0x40321957, - 0x4032996d, - 0x4033199b, - 0x403399a5, - 0x403419bc, - 0x403499da, - 0x403519ea, - 0x403599fc, - 0x40361a09, - 0x40369a15, - 0x40371a2a, - 0x40379a3c, - 0x40381a47, - 0x40389a59, + 0x40321971, + 0x40329987, + 0x403319b5, + 0x403399bf, + 0x403419d6, + 0x403499f4, + 0x40351a04, + 0x40359a16, + 0x40361a23, + 0x40369a2f, + 0x40371a44, + 0x40379a56, + 0x40381a61, + 0x40389a73, 0x40390f27, - 0x40399a69, - 0x403a1a7c, - 0x403a9a9d, - 0x403b1aae, - 0x403b9abe, + 0x40399a83, + 0x403a1a96, + 0x403a9ab7, + 0x403b1ac8, + 0x403b9ad8, 0x403c0064, 0x403c8083, - 0x403d1b42, - 0x403d9b58, - 0x403e1b67, - 0x403e9b9f, - 0x403f1bb9, - 0x403f9be1, - 0x40401bf6, - 0x40409c0a, - 0x40411c27, - 0x40419c42, - 0x40421c5b, - 0x40429c6e, - 0x40431c82, - 0x40439c9a, - 0x40441cb1, + 0x403d1b5c, + 0x403d9b72, + 0x403e1b81, + 0x403e9bb9, + 0x403f1bd3, + 0x403f9bfb, + 0x40401c10, + 0x40409c24, + 0x40411c41, + 0x40419c5c, + 0x40421c75, + 0x40429c88, + 0x40431c9c, + 0x40439cb4, + 0x40441ccb, 0x404480ac, - 0x40451cc6, - 0x40459cd8, - 0x40461cfc, - 0x40469d1c, - 0x40471d2a, - 0x40479d51, - 0x40481dc2, - 0x40489df5, - 0x40491e0c, - 0x40499e26, - 0x404a1e3d, - 0x404a9e5b, - 0x404b1e73, - 0x404b9e8a, - 0x404c1ea0, - 0x404c9eb2, - 0x404d1ed3, - 0x404d9f0c, - 0x404e1f20, - 0x404e9f2d, - 0x404f1f74, - 0x404f9fba, - 0x40502011, - 0x4050a025, - 0x40512058, - 0x40522068, - 0x4052a08c, - 0x405320a4, - 0x4053a0b7, - 0x405420cc, - 0x4054a0ef, - 0x405520fd, - 0x4055a13a, - 0x40562147, - 0x4056a160, - 0x40572178, - 0x4057a18b, - 0x405821a0, - 0x4058a1c7, - 0x405921f6, - 0x4059a223, - 0x405a2237, - 0x405aa247, - 0x405b225f, - 0x405ba270, - 0x405c2283, - 0x405ca2c2, - 0x405d22cf, - 0x405da2f4, - 0x405e2332, + 0x40451ce0, + 0x40459cf2, + 0x40461d16, + 0x40469d36, + 0x40471d44, + 0x40479d6b, + 0x40481ddc, + 0x40489e0f, + 0x40491e26, + 0x40499e40, + 0x404a1e57, + 0x404a9e75, + 0x404b1e8d, + 0x404b9ea4, + 0x404c1eba, + 0x404c9ecc, + 0x404d1eed, + 0x404d9f26, + 0x404e1f3a, + 0x404e9f47, + 0x404f1f8e, + 0x404f9fd4, + 0x4050202b, + 0x4050a03f, + 0x40512072, + 0x40522082, + 0x4052a0a6, + 0x405320be, + 0x4053a0d1, + 0x405420e6, + 0x4054a109, + 0x40552117, + 0x4055a154, + 0x40562161, + 0x4056a17a, + 0x40572192, + 0x4057a1a5, + 0x405821ba, + 0x4058a1e1, + 0x40592210, + 0x4059a23d, + 0x405a2251, + 0x405aa261, + 0x405b2279, + 0x405ba28a, + 0x405c229d, + 0x405ca2dc, + 0x405d22e9, + 0x405da30e, + 0x405e234c, 0x405e8ab3, - 0x405f2353, - 0x405fa360, - 0x4060236e, - 0x4060a390, - 0x406123f1, - 0x4061a429, - 0x40622440, - 0x4062a451, - 0x40632476, - 0x4063a48b, - 0x406424a2, - 0x4064a4ce, - 0x406524e9, - 0x4065a500, - 0x40662518, - 0x4066a542, - 0x4067256d, - 0x4067a5b2, - 0x406825fa, - 0x4068a61b, - 0x4069264d, - 0x4069a67b, - 0x406a269c, - 0x406aa6bc, - 0x406b2844, - 0x406ba867, - 0x406c287d, - 0x406cab20, - 0x406d2b4f, - 0x406dab77, - 0x406e2ba5, - 0x406eabf2, - 0x406f2c2d, - 0x406fac65, - 0x40702c78, - 0x4070ac95, + 0x405f236d, + 0x405fa37a, + 0x40602388, + 0x4060a3aa, + 0x4061240b, + 0x4061a443, + 0x4062245a, + 0x4062a46b, + 0x40632490, + 0x4063a4a5, + 0x406424bc, + 0x4064a4e8, + 0x40652503, + 0x4065a51a, + 0x40662532, + 0x4066a55c, + 0x40672587, + 0x4067a5cc, + 0x40682614, + 0x4068a635, + 0x40692667, + 0x4069a695, + 0x406a26b6, + 0x406aa6d6, + 0x406b285e, + 0x406ba881, + 0x406c2897, + 0x406cab3a, + 0x406d2b69, + 0x406dab91, + 0x406e2bbf, + 0x406eac0c, + 0x406f2c47, + 0x406fac7f, + 0x40702c92, + 0x4070acaf, 0x40710802, - 0x4071aca7, - 0x40722cba, - 0x4072acf0, - 0x40732d08, - 0x407394e0, - 0x40742d1c, - 0x4074ad36, - 0x40752d47, - 0x4075ad5b, - 0x40762d69, - 0x407692a4, - 0x40772d8e, - 0x4077adb0, - 0x40782dcb, - 0x4078ae04, - 0x40792e1b, - 0x4079ae31, - 0x407a2e5d, - 0x407aae70, - 0x407b2e85, - 0x407bae97, - 0x407c2ec8, - 0x407caed1, - 0x407d2636, - 0x407d9fca, - 0x407e2de0, - 0x407ea1d7, - 0x407f1d3e, - 0x407f9ae4, - 0x40801f84, - 0x40809d66, - 0x4081207a, - 0x40819f5e, - 0x40822b90, - 0x40829aca, - 0x408321b2, - 0x4083a4b3, - 0x40841d7a, - 0x4084a20f, - 0x40852294, - 0x4085a3b8, - 0x40862314, - 0x40869fe4, - 0x40872bd6, - 0x4087a406, - 0x40881b2b, - 0x4088a5c5, - 0x40891b7a, - 0x40899b07, - 0x408a28b5, - 0x408a98f8, - 0x408b2eac, - 0x408bac42, - 0x408c22a4, - 0x408c9914, - 0x408d1ddb, - 0x408d9dac, - 0x408e1ef5, - 0x408ea11a, - 0x408f25d9, - 0x408fa3d4, - 0x4090258e, - 0x4090a2e6, - 0x4091289d, - 0x4091993a, - 0x40921bc7, - 0x4092ac11, - 0x40932cd3, - 0x40939ff5, - 0x40941d8e, - 0x4094a8ce, - 0x40952462, - 0x4095ae3d, - 0x40962bbd, - 0x40969f9d, - 0x40972040, - 0x40979f44, - 0x41f4276f, - 0x41f92801, - 0x41fe26f4, - 0x41fea911, - 0x41ff2a02, - 0x42032788, - 0x420827aa, - 0x4208a7e6, - 0x420926d8, - 0x4209a820, - 0x420a272f, - 0x420aa70f, - 0x420b274f, - 0x420ba7c8, - 0x420c2a1e, - 0x420ca8de, - 0x420d28f8, - 0x420da92f, - 0x42122949, - 0x421729e5, - 0x4217a98b, - 0x421c29ad, - 0x421f2968, - 0x42212a35, - 0x422629c8, - 0x422b2b04, - 0x422baab2, - 0x422c2aec, - 0x422caa71, - 0x422d2a50, - 0x422daad1, - 0x422e2a97, + 0x4071acc1, + 0x40722cd4, + 0x4072ad0a, + 0x40732d22, + 0x407394fa, + 0x40742d36, + 0x4074ad50, + 0x40752d61, + 0x4075ad75, + 0x40762d83, + 0x407692be, + 0x40772da8, + 0x4077adca, + 0x40782de5, + 0x4078ae1e, + 0x40792e35, + 0x4079ae4b, + 0x407a2e77, + 0x407aae8a, + 0x407b2e9f, + 0x407baeb1, + 0x407c2ee2, + 0x407caeeb, + 0x407d2650, + 0x407d9fe4, + 0x407e2dfa, + 0x407ea1f1, + 0x407f1d58, + 0x407f9afe, + 0x40801f9e, + 0x40809d80, + 0x40812094, + 0x40819f78, + 0x40822baa, + 0x40829ae4, + 0x408321cc, + 0x4083a4cd, + 0x40841d94, + 0x4084a229, + 0x408522ae, + 0x4085a3d2, + 0x4086232e, + 0x40869ffe, + 0x40872bf0, + 0x4087a420, + 0x40881b45, + 0x4088a5df, + 0x40891b94, + 0x40899b21, + 0x408a28cf, + 0x408a9912, + 0x408b2ec6, + 0x408bac5c, + 0x408c22be, + 0x408c992e, + 0x408d1df5, + 0x408d9dc6, + 0x408e1f0f, + 0x408ea134, + 0x408f25f3, + 0x408fa3ee, + 0x409025a8, + 0x4090a300, + 0x409128b7, + 0x40919954, + 0x40921be1, + 0x4092ac2b, + 0x40932ced, + 0x4093a00f, + 0x40941da8, + 0x4094a8e8, + 0x4095247c, + 0x4095ae57, + 0x40962bd7, + 0x40969fb7, + 0x4097205a, + 0x40979f5e, + 0x41f42789, + 0x41f9281b, + 0x41fe270e, + 0x41fea92b, + 0x41ff2a1c, + 0x420327a2, + 0x420827c4, + 0x4208a800, + 0x420926f2, + 0x4209a83a, + 0x420a2749, + 0x420aa729, + 0x420b2769, + 0x420ba7e2, + 0x420c2a38, + 0x420ca8f8, + 0x420d2912, + 0x420da949, + 0x42122963, + 0x421729ff, + 0x4217a9a5, + 0x421c29c7, + 0x421f2982, + 0x42212a4f, + 0x422629e2, + 0x422b2b1e, + 0x422baacc, + 0x422c2b06, + 0x422caa8b, + 0x422d2a6a, + 0x422daaeb, + 0x422e2ab1, 0x4432072d, 0x4432873c, 0x44330748, @@ -613,106 +614,106 @@ const uint32_t kOpenSSLReasonValues[] = { 0x44390802, 0x44398810, 0x443a0823, - 0x483212ce, - 0x483292e0, - 0x483312f6, - 0x4833930f, - 0x4c321334, - 0x4c329344, - 0x4c331357, - 0x4c339377, + 0x483212e8, + 0x483292fa, + 0x48331310, + 0x48339329, + 0x4c32134e, + 0x4c32935e, + 0x4c331371, + 0x4c339391, 0x4c3400ac, 0x4c3480ea, - 0x4c351383, - 0x4c359391, - 0x4c3613ad, - 0x4c3693d3, - 0x4c3713e2, - 0x4c3793f0, - 0x4c381405, - 0x4c389411, - 0x4c391431, - 0x4c39945b, - 0x4c3a1474, - 0x4c3a948d, + 0x4c35139d, + 0x4c3593ab, + 0x4c3613c7, + 0x4c3693ed, + 0x4c3713fc, + 0x4c37940a, + 0x4c38141f, + 0x4c38942b, + 0x4c39144b, + 0x4c399475, + 0x4c3a148e, + 0x4c3a94a7, 0x4c3b05fd, - 0x4c3b94a6, - 0x4c3c14b8, - 0x4c3c94c7, - 0x4c3d14e0, + 0x4c3b94c0, + 0x4c3c14d2, + 0x4c3c94e1, + 0x4c3d14fa, 0x4c3d8c47, - 0x4c3e154d, - 0x4c3e94ef, - 0x4c3f156f, - 0x4c3f92a4, - 0x4c401505, - 0x4c409320, - 0x4c41153d, - 0x4c4193c0, - 0x4c421529, - 0x50323127, - 0x5032b136, - 0x50333141, - 0x5033b151, - 0x5034316a, - 0x5034b184, - 0x50353192, - 0x5035b1a8, - 0x503631ba, - 0x5036b1d0, - 0x503731e9, - 0x5037b1fc, - 0x50383214, - 0x5038b225, - 0x5039323a, - 0x5039b24e, - 0x503a326e, - 0x503ab284, - 0x503b329c, - 0x503bb2ae, - 0x503c32ca, - 0x503cb2e1, - 0x503d32fa, - 0x503db310, - 0x503e331d, - 0x503eb333, - 0x503f3345, + 0x4c3e1567, + 0x4c3e9509, + 0x4c3f1589, + 0x4c3f92be, + 0x4c40151f, + 0x4c40933a, + 0x4c411557, + 0x4c4193da, + 0x4c421543, + 0x50323141, + 0x5032b150, + 0x5033315b, + 0x5033b16b, + 0x50343184, + 0x5034b19e, + 0x503531ac, + 0x5035b1c2, + 0x503631d4, + 0x5036b1ea, + 0x50373203, + 0x5037b216, + 0x5038322e, + 0x5038b23f, + 0x50393254, + 0x5039b268, + 0x503a3288, + 0x503ab29e, + 0x503b32b6, + 0x503bb2c8, + 0x503c32e4, + 0x503cb2fb, + 0x503d3314, + 0x503db32a, + 0x503e3337, + 0x503eb34d, + 0x503f335f, 0x503f837b, - 0x50403358, - 0x5040b368, - 0x50413382, - 0x5041b391, - 0x504233ab, - 0x5042b3c8, - 0x504333d8, - 0x5043b3e8, - 0x504433f7, + 0x50403372, + 0x5040b382, + 0x5041339c, + 0x5041b3ab, + 0x504233c5, + 0x5042b3e2, + 0x504333f2, + 0x5043b402, + 0x50443411, 0x50448431, - 0x5045340b, - 0x5045b429, - 0x5046343c, - 0x5046b452, - 0x50473464, - 0x5047b479, - 0x5048349f, - 0x5048b4ad, - 0x504934c0, - 0x5049b4d5, - 0x504a34eb, - 0x504ab4fb, - 0x504b351b, - 0x504bb52e, - 0x504c3551, - 0x504cb57f, - 0x504d3591, - 0x504db5ae, - 0x504e35c9, - 0x504eb5e5, - 0x504f35f7, - 0x504fb60e, - 0x5050361d, + 0x50453425, + 0x5045b443, + 0x50463456, + 0x5046b46c, + 0x5047347e, + 0x5047b493, + 0x504834b9, + 0x5048b4c7, + 0x504934da, + 0x5049b4ef, + 0x504a3505, + 0x504ab515, + 0x504b3535, + 0x504bb548, + 0x504c356b, + 0x504cb599, + 0x504d35ab, + 0x504db5c8, + 0x504e35e3, + 0x504eb5ff, + 0x504f3611, + 0x504fb628, + 0x50503637, 0x505086f1, - 0x50513630, + 0x5051364a, 0x58320f65, 0x68320f27, 0x68328c7f, @@ -753,7 +754,7 @@ const uint32_t kOpenSSLReasonValues[] = { 0x783d8b4c, 0x783e0aa2, 0x783e8a54, - 0x7c3211bd, + 0x7c3211d7, }; const size_t kOpenSSLReasonValuesLen = sizeof(kOpenSSLReasonValues) / sizeof(kOpenSSLReasonValues[0]); @@ -984,6 +985,7 @@ const char kOpenSSLReasonStringData[] = "KEYS_NOT_SET\0" "MEMORY_LIMIT_EXCEEDED\0" "NOT_A_PRIVATE_KEY\0" + "NOT_XOF_OR_INVALID_LENGTH\0" "NO_DEFAULT_DIGEST\0" "NO_KEY_SET\0" "NO_MDC2_SUPPORT\0" diff --git a/Sources/CNIOBoringSSL/crypto/evp/evp.c b/Sources/CNIOBoringSSL/crypto/evp/evp.c index 806e69eb..b215346a 100644 --- a/Sources/CNIOBoringSSL/crypto/evp/evp.c +++ b/Sources/CNIOBoringSSL/crypto/evp/evp.c @@ -71,6 +71,11 @@ #include "../internal.h" +// Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|. +// +// TODO(davidben): Fix Node to not touch the error queue itself and remove this. +OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH) + EVP_PKEY *EVP_PKEY_new(void) { EVP_PKEY *ret; diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-586.linux.x86.S index 26e7592d..8417a873 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-586.linux.x86.S @@ -3263,3 +3263,7 @@ aes_nohw_set_decrypt_key: .byte 111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.ios.arm.S index fee19932..af3265ce 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.ios.arm.S @@ -1234,3 +1234,7 @@ Ldec_loop: .align 2 #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.linux.arm.S index 86079fca..f3225862 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-armv4.linux.arm.S @@ -1222,3 +1222,7 @@ _armv4_AES_decrypt: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.linux.x86_64.S index ab34f070..8d8af479 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.linux.x86_64.S @@ -2665,3 +2665,7 @@ aes_nohw_cbc_encrypt: .align 64 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.mac.x86_64.S index 8bd6fc76..89bc4c12 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes-x86_64.mac.x86_64.S @@ -2646,3 +2646,7 @@ L$AES_Td: .p2align 6 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes/internal.h b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes/internal.h index 9d92560f..10560184 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aes/internal.h +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aes/internal.h @@ -38,6 +38,7 @@ OPENSSL_INLINE int hwaes_capable(void) { #if defined(OPENSSL_X86_64) #define VPAES_CTR32 #endif +#define VPAES_CBC OPENSSL_INLINE int vpaes_capable(void) { return (OPENSSL_ia32cap_get()[1] & (1 << (41 - 32))) != 0; } @@ -49,11 +50,15 @@ OPENSSL_INLINE int hwaes_capable(void) { return CRYPTO_is_ARMv8_AES_capable(); } #if defined(OPENSSL_ARM) #define BSAES +#define VPAES +#define VPAES_CTR32 OPENSSL_INLINE int bsaes_capable(void) { return CRYPTO_is_NEON_capable(); } +OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); } #endif #if defined(OPENSSL_AARCH64) #define VPAES +#define VPAES_CBC #define VPAES_CTR32 OPENSSL_INLINE int vpaes_capable(void) { return CRYPTO_is_NEON_capable(); } #endif @@ -130,12 +135,14 @@ void aes_hw_ecb_encrypt(const uint8_t *in, uint8_t *out, size_t length, #if defined(BSAES) -// On platforms where BSAES gets defined (just above), then these functions are -// provided by asm. Note |bsaes_cbc_encrypt| requires |enc| to be zero. +// Note |bsaes_cbc_encrypt| requires |enc| to be zero. void bsaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, const AES_KEY *key, uint8_t ivec[16], int enc); void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, const uint8_t ivec[16]); +// VPAES to BSAES conversions are available on all BSAES platforms. +void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes); +void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes, const AES_KEY *vpaes); #else OPENSSL_INLINE char bsaes_capable(void) { return 0; } @@ -152,6 +159,16 @@ OPENSSL_INLINE void bsaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, const uint8_t ivec[16]) { abort(); } + +OPENSSL_INLINE void vpaes_encrypt_key_to_bsaes(AES_KEY *out_bsaes, + const AES_KEY *vpaes) { + abort(); +} + +OPENSSL_INLINE void vpaes_decrypt_key_to_bsaes(AES_KEY *out_bsaes, + const AES_KEY *vpaes) { + abort(); +} #endif // !BSAES @@ -164,8 +181,10 @@ int vpaes_set_decrypt_key(const uint8_t *userKey, int bits, AES_KEY *key); void vpaes_encrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); void vpaes_decrypt(const uint8_t *in, uint8_t *out, const AES_KEY *key); +#if defined(VPAES_CBC) void vpaes_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length, const AES_KEY *key, uint8_t *ivec, int enc); +#endif #if defined(VPAES_CTR32) void vpaes_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t len, const AES_KEY *key, const uint8_t ivec[16]); diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S index 6b74f109..6b05267a 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S @@ -558,12 +558,10 @@ _aesni_ctr32_6x: .align 32 aesni_gcm_encrypt: .cfi_startproc -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST .extern BORINGSSL_function_hit .hidden BORINGSSL_function_hit movb $1,BORINGSSL_function_hit+2(%rip) -#endif #endif xorq %r10,%r10 @@ -854,3 +852,7 @@ aesni_gcm_encrypt: .align 64 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S index 7645f0ca..7342b75b 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S @@ -558,11 +558,9 @@ L$handle_ctr32_2: .p2align 5 _aesni_gcm_encrypt: -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+2(%rip) -#endif #endif xorq %r10,%r10 @@ -853,3 +851,7 @@ L$one_lsb: .p2align 6 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86.linux.x86.S index 76ca6fd4..b3173b0e 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86.linux.x86.S @@ -8,7 +8,7 @@ #include #endif .text -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST #endif .globl aes_hw_encrypt .hidden aes_hw_encrypt @@ -16,7 +16,7 @@ .align 16 aes_hw_encrypt: .L_aes_hw_encrypt_begin: -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST pushl %ebx pushl %edx call .L000pic @@ -847,7 +847,7 @@ aes_hw_ctr32_encrypt_blocks: pushl %ebx pushl %esi pushl %edi -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST pushl %ebx pushl %edx call .L038pic @@ -2442,7 +2442,7 @@ _aesni_set_encrypt_key: .align 16 aes_hw_set_encrypt_key: .L_aes_hw_set_encrypt_key_begin: -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST pushl %ebx pushl %edx call .L116pic @@ -2513,3 +2513,7 @@ aes_hw_set_decrypt_key: .byte 115,108,46,111,114,103,62,0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.linux.x86_64.S index a128811e..ce3d1b5c 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.linux.x86_64.S @@ -22,12 +22,10 @@ .align 16 aes_hw_encrypt: .cfi_startproc -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST .extern BORINGSSL_function_hit .hidden BORINGSSL_function_hit movb $1,BORINGSSL_function_hit+1(%rip) -#endif #endif movups (%rdi),%xmm2 movl 240(%rdx),%eax @@ -889,10 +887,8 @@ aes_hw_ecb_encrypt: .align 16 aes_hw_ctr32_encrypt_blocks: .cfi_startproc -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,BORINGSSL_function_hit(%rip) -#endif #endif cmpq $1,%rdx jne .Lctr32_bulk @@ -2113,11 +2109,9 @@ aes_hw_set_decrypt_key: aes_hw_set_encrypt_key: __aesni_set_encrypt_key: .cfi_startproc -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,BORINGSSL_function_hit+3(%rip) #endif -#endif .byte 0x48,0x83,0xEC,0x08 .cfi_adjust_cfa_offset 8 movq $-1,%rax @@ -2512,3 +2506,7 @@ __aesni_set_encrypt_key: .align 64 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.mac.x86_64.S index 5dc179d9..f4803a2f 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesni-x86_64.mac.x86_64.S @@ -21,11 +21,9 @@ .p2align 4 _aes_hw_encrypt: -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+1(%rip) -#endif #endif movups (%rdi),%xmm2 movl 240(%rdx),%eax @@ -887,10 +885,8 @@ L$ecb_ret: .p2align 4 _aes_hw_ctr32_encrypt_blocks: -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit(%rip) -#endif #endif cmpq $1,%rdx jne L$ctr32_bulk @@ -2111,11 +2107,9 @@ L$SEH_end_set_decrypt_key: _aes_hw_set_encrypt_key: __aesni_set_encrypt_key: -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+3(%rip) #endif -#endif .byte 0x48,0x83,0xEC,0x08 movq $-1,%rax @@ -2510,3 +2504,7 @@ L$key_rcon1b: .p2align 6 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.ios.arm.S index 9608ac2e..264fb0e8 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.ios.arm.S @@ -791,3 +791,7 @@ Lctr32_done: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.linux.arm.S index a39e35c6..3b7c706e 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx32.linux.arm.S @@ -781,3 +781,7 @@ aes_hw_ctr32_encrypt_blocks: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S index 453716d4..d85faf99 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.ios.aarch64.S @@ -773,3 +773,7 @@ Lctr32_done: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S index 50cfc4d1..b209c1c8 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/aesv8-armx64.linux.aarch64.S @@ -775,3 +775,7 @@ aes_hw_ctr32_encrypt_blocks: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.ios.arm.S index 9fd9e2fa..ea71ff9e 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.ios.arm.S @@ -983,3 +983,7 @@ OPENSSL_armcap_P: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.linux.arm.S index a6fb72c7..96f19616 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv4-mont.linux.arm.S @@ -977,3 +977,7 @@ bn_mul8x_mont_neon: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.ios.aarch64.S index 03518682..df0a026a 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.ios.aarch64.S @@ -1421,3 +1421,7 @@ Lmul4x_done: .align 4 #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.linux.aarch64.S index e4274e47..990a48c2 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/armv8-mont.linux.aarch64.S @@ -1423,3 +1423,7 @@ __bn_mul4x_mont: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/bn-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/bn-586.linux.x86.S index 2eb6acfa..2aa20979 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/bn-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/bn-586.linux.x86.S @@ -1544,3 +1544,7 @@ bn_sub_part_words: .size bn_sub_part_words,.-.L_bn_sub_part_words_begin #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/ctx.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/ctx.c index 94e5b5b3..0c471c4e 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/ctx.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/ctx.c @@ -126,6 +126,10 @@ void BN_CTX_free(BN_CTX *ctx) { return; } + // All |BN_CTX_start| calls must be matched with |BN_CTX_end|, otherwise the + // function may use more memory than expected, potentially without bound if + // done in a loop. Assert that all |BIGNUM|s have been released. + assert(ctx->used == 0 || ctx->error); sk_BIGNUM_pop_free(ctx->bignums, BN_free); BN_STACK_cleanup(&ctx->stack); OPENSSL_free(ctx); diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/prime.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/prime.c index 2565cc49..623f5b07 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/prime.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/bn/prime.c @@ -594,10 +594,10 @@ int bn_odd_number_is_obviously_composite(const BIGNUM *bn) { return bn_trial_division(&prime, bn) && !BN_is_word(bn, prime); } -int BN_primality_test(int *is_probably_prime, const BIGNUM *w, +int BN_primality_test(int *out_is_probably_prime, const BIGNUM *w, int iterations, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb) { - *is_probably_prime = 0; + *out_is_probably_prime = 0; // To support RSA key generation, this function should treat |w| as secret if // it is a large prime. Composite numbers are discarded, so they may return @@ -609,13 +609,13 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w, if (!BN_is_odd(w)) { // The only even prime is two. - *is_probably_prime = BN_is_word(w, 2); + *out_is_probably_prime = BN_is_word(w, 2); return 1; } // Miller-Rabin does not work for three. if (BN_is_word(w, 3)) { - *is_probably_prime = 1; + *out_is_probably_prime = 1; return 1; } @@ -623,7 +623,7 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w, // Perform additional trial division checks to discard small primes. uint16_t prime; if (bn_trial_division(&prime, w)) { - *is_probably_prime = BN_is_word(w, prime); + *out_is_probably_prime = BN_is_word(w, prime); return 1; } if (!BN_GENCB_call(cb, 1, -1)) { @@ -691,9 +691,9 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w, // discard out-of-range values. To avoid leaking information on |w|, we use // |bn_rand_secret_range| which, rather than discarding bad values, adjusts // them to be in range. Though not uniformly selected, these adjusted values - // are still usable as Rabin-Miller checks. + // are still usable as Miller-Rabin checks. // - // Rabin-Miller is already probabilistic, so we could reach the desired + // Miller-Rabin is already probabilistic, so we could reach the desired // confidence levels by just suitably increasing the iteration count. However, // to align with FIPS 186-4, we use a more pessimal analysis: we do not count // the non-uniform values towards the iteration count. As a result, this @@ -771,7 +771,7 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w, // Step 4.6. We did not see z = w-1 before z = 1, so w must be composite. // (For any prime, the value of z immediately preceding 1 must be -1. // There are no non-trivial square roots of 1 modulo a prime.) - *is_probably_prime = 0; + *out_is_probably_prime = 0; ret = 1; goto err; } @@ -783,7 +783,7 @@ int BN_primality_test(int *is_probably_prime, const BIGNUM *w, } assert(uniform_iterations >= (crypto_word_t)iterations); - *is_probably_prime = 1; + *out_is_probably_prime = 1; ret = 1; err: diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.ios.arm.S index f5cc090e..3d9be856 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.ios.arm.S @@ -1537,3 +1537,7 @@ Lctr_enc_bzero:@ wipe key schedule [if any] #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.linux.arm.S index ec359478..0c370814 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/bsaes-armv7.linux.arm.S @@ -1529,3 +1529,7 @@ bsaes_ctr32_encrypt_blocks: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/cipher.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/cipher.c index 3f7b99b7..69c65638 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/cipher.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/cipher.c @@ -125,9 +125,10 @@ int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in) { return 1; } -void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) { +int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) { EVP_CIPHER_CTX_cleanup(ctx); EVP_CIPHER_CTX_init(ctx); + return 1; } int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, @@ -191,7 +192,7 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, case EVP_CIPH_CFB_MODE: ctx->num = 0; - // fall-through + OPENSSL_FALLTHROUGH; case EVP_CIPH_CBC_MODE: assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv)); diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/e_aes.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/e_aes.c index 177778ab..f7aaa753 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/e_aes.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/cipher/e_aes.c @@ -68,6 +68,48 @@ OPENSSL_MSVC_PRAGMA(warning(push)) OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) // Unreachable code. +#if defined(BSAES) +static void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in, + uint8_t *out, size_t blocks, + const AES_KEY *key, + const uint8_t ivec[16]) { + // |bsaes_ctr32_encrypt_blocks| is faster than |vpaes_ctr32_encrypt_blocks|, + // but it takes at least one full 8-block batch to amortize the conversion. + if (blocks < 8) { + vpaes_ctr32_encrypt_blocks(in, out, blocks, key, ivec); + return; + } + + size_t bsaes_blocks = blocks; + if (bsaes_blocks % 8 < 6) { + // |bsaes_ctr32_encrypt_blocks| internally works in 8-block batches. If the + // final batch is too small (under six blocks), it is faster to loop over + // |vpaes_encrypt|. Round |bsaes_blocks| down to a multiple of 8. + bsaes_blocks -= bsaes_blocks % 8; + } + + AES_KEY bsaes; + vpaes_encrypt_key_to_bsaes(&bsaes, key); + bsaes_ctr32_encrypt_blocks(in, out, bsaes_blocks, &bsaes, ivec); + OPENSSL_cleanse(&bsaes, sizeof(bsaes)); + + in += 16 * bsaes_blocks; + out += 16 * bsaes_blocks; + blocks -= bsaes_blocks; + + union { + uint32_t u32[4]; + uint8_t u8[16]; + } new_ivec; + memcpy(new_ivec.u8, ivec, 16); + uint32_t ctr = CRYPTO_bswap4(new_ivec.u32[3]) + bsaes_blocks; + new_ivec.u32[3] = CRYPTO_bswap4(ctr); + + // Finish any remaining blocks with |vpaes_ctr32_encrypt_blocks|. + vpaes_ctr32_encrypt_blocks(in, out, blocks, key, new_ivec.u8); +} +#endif // BSAES + typedef struct { union { double align; @@ -110,14 +152,23 @@ static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, dat->stream.cbc = aes_hw_cbc_encrypt; } } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) { - ret = aes_nohw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); + assert(vpaes_capable()); + ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); + if (ret == 0) { + vpaes_decrypt_key_to_bsaes(&dat->ks.ks, &dat->ks.ks); + } // If |dat->stream.cbc| is provided, |dat->block| is never used. dat->block = NULL; dat->stream.cbc = bsaes_cbc_encrypt; } else if (vpaes_capable()) { ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = vpaes_decrypt; - dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? vpaes_cbc_encrypt : NULL; + dat->stream.cbc = NULL; +#if defined(VPAES_CBC) + if (mode == EVP_CIPH_CBC_MODE) { + dat->stream.cbc = vpaes_cbc_encrypt; + } +#endif } else { ret = aes_nohw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = aes_nohw_decrypt; @@ -137,23 +188,23 @@ static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, } else if (mode == EVP_CIPH_CTR_MODE) { dat->stream.ctr = aes_hw_ctr32_encrypt_blocks; } - } else if (bsaes_capable() && mode == EVP_CIPH_CTR_MODE) { - ret = aes_nohw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); - // If |dat->stream.ctr| is provided, |dat->block| is never used. - dat->block = NULL; - dat->stream.ctr = bsaes_ctr32_encrypt_blocks; } else if (vpaes_capable()) { ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = vpaes_encrypt; dat->stream.cbc = NULL; +#if defined(VPAES_CBC) if (mode == EVP_CIPH_CBC_MODE) { dat->stream.cbc = vpaes_cbc_encrypt; } -#if defined(VPAES_CTR32) - if (mode == EVP_CIPH_CTR_MODE) { - dat->stream.ctr = vpaes_ctr32_encrypt_blocks; - } #endif + if (mode == EVP_CIPH_CTR_MODE) { +#if defined(BSAES) + assert(bsaes_capable()); + dat->stream.ctr = vpaes_ctr32_encrypt_blocks_with_bsaes; +#elif defined(VPAES_CTR32) + dat->stream.ctr = vpaes_ctr32_encrypt_blocks; +#endif + } } else { ret = aes_nohw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks); dat->block = aes_nohw_encrypt; @@ -242,17 +293,6 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key, return aes_hw_ctr32_encrypt_blocks; } - if (bsaes_capable()) { - aes_nohw_set_encrypt_key(key, key_bytes * 8, aes_key); - if (gcm_key != NULL) { - CRYPTO_gcm128_init_key(gcm_key, aes_key, aes_nohw_encrypt, 0); - } - if (out_block) { - *out_block = aes_nohw_encrypt; - } - return bsaes_ctr32_encrypt_blocks; - } - if (vpaes_capable()) { vpaes_set_encrypt_key(key, key_bytes * 8, aes_key); if (out_block) { @@ -261,7 +301,10 @@ ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key, if (gcm_key != NULL) { CRYPTO_gcm128_init_key(gcm_key, aes_key, vpaes_encrypt, 0); } -#if defined(VPAES_CTR32) +#if defined(BSAES) + assert(bsaes_capable()); + return vpaes_ctr32_encrypt_blocks_with_bsaes; +#elif defined(VPAES_CTR32) return vpaes_ctr32_encrypt_blocks; #else return NULL; @@ -847,7 +890,7 @@ static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx, size_t key_len, size_t tag_len) { const size_t key_bits = key_len * 8; - if (key_bits != 128 && key_bits != 256) { + if (key_bits != 128 && key_bits != 192 && key_bits != 256) { OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); return 0; // EVP_AEAD_CTX_init should catch this. } @@ -1020,6 +1063,21 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm) { out->open_gather = aead_aes_gcm_open_gather; } +DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_192_gcm) { + memset(out, 0, sizeof(EVP_AEAD)); + + out->key_len = 24; + out->nonce_len = 12; + out->overhead = EVP_AEAD_AES_GCM_TAG_LEN; + out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN; + out->seal_scatter_supports_extra_in = 1; + + out->init = aead_aes_gcm_init; + out->cleanup = aead_aes_gcm_cleanup; + out->seal_scatter = aead_aes_gcm_seal_scatter; + out->open_gather = aead_aes_gcm_open_gather; +} + DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) { memset(out, 0, sizeof(EVP_AEAD)); diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/co-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/co-586.linux.x86.S index 0a60aad6..f190e683 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/co-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/co-586.linux.x86.S @@ -1266,3 +1266,7 @@ bn_sqr_comba4: .size bn_sqr_comba4,.-.L_bn_sqr_comba4_begin #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/digest/digest.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/digest/digest.c index d5c786ce..c5619463 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/digest/digest.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/digest/digest.c @@ -120,6 +120,8 @@ int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len) { return 0; } +uint32_t EVP_MD_meth_get_flags(const EVP_MD *md) { return EVP_MD_flags(md); } + int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) { // |in->digest| may be NULL if this is a signing |EVP_MD_CTX| for, e.g., // Ed25519 which does not hash with |EVP_MD_CTX|. diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ec/ec_key.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/ec/ec_key.c index 65286728..4a7464eb 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ec/ec_key.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ec/ec_key.c @@ -369,8 +369,8 @@ int EC_KEY_check_fips(const EC_KEY *key) { return 1; } -int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, - BIGNUM *y) { +int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, const BIGNUM *x, + const BIGNUM *y) { EC_POINT *point = NULL; int ok = 0; diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.ios.arm.S index b5a13838..a52fefc6 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.ios.arm.S @@ -601,3 +601,7 @@ Lgmult_neon: .align 2 #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.linux.arm.S index 3c3d30a1..df71148f 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-armv4.linux.arm.S @@ -591,3 +591,7 @@ gcm_ghash_neon: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S index b8fb3e25..7841d54b 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S @@ -339,3 +339,7 @@ Lmasks: .align 2 #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S index a9bed32c..ab85fa9b 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S @@ -341,3 +341,7 @@ gcm_ghash_neon: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86.linux.x86.S index aeb5b7d6..386d54fd 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86.linux.x86.S @@ -294,3 +294,7 @@ gcm_ghash_ssse3: .long 252645135,252645135,252645135,252645135 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S index e135bf26..3dcca86d 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S @@ -427,3 +427,7 @@ gcm_ghash_ssse3: .quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S index f2925fe8..ec5156ff 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S @@ -427,3 +427,7 @@ L$low4_mask: .quad 0x0f0f0f0f0f0f0f0f, 0x0f0f0f0f0f0f0f0f #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86.linux.x86.S index 8750570c..d448059f 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86.linux.x86.S @@ -1075,3 +1075,7 @@ gcm_ghash_clmul: .byte 0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.linux.x86_64.S index e30fc7d1..c538f518 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.linux.x86_64.S @@ -1872,3 +1872,7 @@ gcm_ghash_avx: .align 64 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.mac.x86_64.S index 27809817..0cfb0794 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghash-x86_64.mac.x86_64.S @@ -1859,3 +1859,7 @@ L$rem_8bit: .p2align 6 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.ios.arm.S index 6b4f6fb3..4793acdb 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.ios.arm.S @@ -257,3 +257,7 @@ Ldone_v8: .align 2 #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.linux.arm.S index d4918d69..84e35abc 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx32.linux.arm.S @@ -253,3 +253,7 @@ gcm_ghash_v8: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S index 6c0be7ce..936cabd2 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S @@ -247,3 +247,7 @@ Ldone_v8: .align 2 #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S index 7e01c4cc..55a8a040 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S @@ -249,3 +249,7 @@ gcm_ghash_v8: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-586.linux.x86.S index b09dacbb..ec02e420 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-586.linux.x86.S @@ -688,3 +688,7 @@ md5_block_asm_data_order: .size md5_block_asm_data_order,.-.L_md5_block_asm_data_order_begin #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.linux.x86_64.S index 63b7188d..e4e4b047 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.linux.x86_64.S @@ -702,3 +702,7 @@ md5_block_asm_data_order: .size md5_block_asm_data_order,.-md5_block_asm_data_order #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.mac.x86_64.S index 0e39999f..23638026 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/md5-x86_64.mac.x86_64.S @@ -697,3 +697,7 @@ L$epilogue: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S index 35302668..a996d29a 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S @@ -4543,3 +4543,7 @@ ecp_nistz256_point_add_affinex: .size ecp_nistz256_point_add_affinex,.-ecp_nistz256_point_add_affinex #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S index 1f3b6aa0..97909207 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S @@ -4468,3 +4468,7 @@ L$add_affinex_epilogue: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S index c9ab8587..5eb35260 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S @@ -343,3 +343,7 @@ beeu_mod_inverse_vartime: .size beeu_mod_inverse_vartime, .-beeu_mod_inverse_vartime #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S index 2d4fa2cd..2cda0d2f 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S @@ -329,3 +329,7 @@ L$beeu_finish: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/internal.h b/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/internal.h index cbcafda3..29ab87af 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/internal.h +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/internal.h @@ -26,6 +26,11 @@ extern "C" { #endif +#if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_FUCHSIA) && \ + !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && !defined(OPENSSL_TRUSTY) +#define OPENSSL_URANDOM +#endif + // RAND_bytes_with_additional_data samples from the RNG after mixing 32 bytes // from |user_additional_data| in. void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len, @@ -35,6 +40,14 @@ void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len, // system. void CRYPTO_sysrand(uint8_t *buf, size_t len); +#if defined(OPENSSL_URANDOM) && defined(BORINGSSL_FIPS) +// CRYPTO_sysrand_if_available fills |len| bytes at |buf| with entropy from the +// operating system, if the entropy pool is initialized. If it is uninitialized, +// it will not block and will instead fill |buf| with all zeros or early +// /dev/urandom output. +void CRYPTO_sysrand_if_available(uint8_t *buf, size_t len); +#endif + // rand_fork_unsafe_buffering_enabled returns whether fork-unsafe buffering has // been enabled via |RAND_enable_fork_unsafe_buffering|. int rand_fork_unsafe_buffering_enabled(void); diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/rand.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/rand.c index b86fd8d2..3574f84a 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/rand.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/rand.c @@ -179,7 +179,8 @@ static void rand_get_seed(struct rand_thread_state *state, #define FIPS_OVERREAD 10 uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD]; - if (!hwrand(entropy, sizeof(entropy))) { + int used_hwrand = hwrand(entropy, sizeof(entropy)); + if (!used_hwrand) { CRYPTO_sysrand(entropy, sizeof(entropy)); } @@ -210,6 +211,17 @@ static void rand_get_seed(struct rand_thread_state *state, seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j]; } } + +#if defined(OPENSSL_URANDOM) + // If we used RDRAND, also opportunistically read from the system. This avoids + // solely relying on the hardware once the entropy pool has been initialized. + if (used_hwrand) { + CRYPTO_sysrand_if_available(entropy, CTR_DRBG_ENTROPY_LEN); + for (size_t i = 0; i < CTR_DRBG_ENTROPY_LEN; i++) { + seed[i] ^= entropy[i]; + } + } +#endif } #else diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/urandom.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/urandom.c index 92ed5536..e65cae49 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/urandom.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rand/urandom.c @@ -18,8 +18,9 @@ #include -#if !defined(OPENSSL_WINDOWS) && !defined(OPENSSL_FUCHSIA) && \ - !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) && !defined(OPENSSL_TRUSTY) +#include "internal.h" + +#if defined(OPENSSL_URANDOM) #include #include @@ -53,10 +54,13 @@ #endif #endif // OPENSSL_LINUX +#if defined(OPENSSL_MACOS) +#include +#endif + #include #include -#include "internal.h" #include "../delocate.h" #include "../../internal.h" @@ -133,6 +137,12 @@ DEFINE_BSS_GET(int, urandom_fd_requested) // urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|. DEFINE_BSS_GET(int, urandom_fd) +#if defined(USE_NR_getrandom) +// getrandom_ready is one if |getrandom| had been initialized by the time +// |init_once| was called and zero otherwise. +DEFINE_BSS_GET(int, getrandom_ready) +#endif + DEFINE_STATIC_ONCE(rand_once) // init_once initializes the state of this module to values previously @@ -145,44 +155,46 @@ static void init_once(void) { CRYPTO_STATIC_MUTEX_unlock_read(rand_lock_bss_get()); #if defined(USE_NR_getrandom) + int have_getrandom; uint8_t dummy; ssize_t getrandom_ret = boringssl_getrandom(&dummy, sizeof(dummy), GRND_NONBLOCK); - - if (getrandom_ret == -1 && errno == EAGAIN) { - // Attempt to get the path of the current process to aid in debugging when - // something blocks. - const char *current_process = ""; -#if defined(OPENSSL_HAS_GETAUXVAL) - const unsigned long getauxval_ret = getauxval(AT_EXECFN); - if (getauxval_ret != 0) { - current_process = (const char *)getauxval_ret; - } -#endif - - fprintf(stderr, - "%s: getrandom indicates that the entropy pool has not been " - "initialized. Rather than continue with poor entropy, this process " - "will block until entropy is available.\n", - current_process); - - getrandom_ret = - boringssl_getrandom(&dummy, sizeof(dummy), 0 /* no flags */); - } - if (getrandom_ret == 1) { - *urandom_fd_bss_get() = kHaveGetrandom; - return; - } - - // Ignore ENOSYS and fallthrough to using /dev/urandom, below. Otherwise it's - // a fatal error. - if (getrandom_ret != -1 || errno != ENOSYS) { + *getrandom_ready_bss_get() = 1; + have_getrandom = 1; + } else if (getrandom_ret == -1 && errno == EAGAIN) { + // We have getrandom, but the entropy pool has not been initialized yet. + have_getrandom = 1; + } else if (getrandom_ret == -1 && errno == ENOSYS) { + // Fallthrough to using /dev/urandom, below. + have_getrandom = 0; + } else { + // Other errors are fatal. perror("getrandom"); abort(); } + + if (have_getrandom) { + *urandom_fd_bss_get() = kHaveGetrandom; + return; + } #endif // USE_NR_getrandom +#if defined(OPENSSL_MACOS) + // getentropy is available in macOS 10.12 and up. iOS 10 and up may also + // support it, but the header is missing. See https://crbug.com/boringssl/287. + if (__builtin_available(macos 10.12, *)) { + *urandom_fd_bss_get() = kHaveGetrandom; + return; + } +#endif + + // Android FIPS builds must support getrandom. +#if defined(BORINGSSL_FIPS) && defined(OPENSSL_ANDROID) + perror("getrandom not found"); + abort(); +#endif + if (fd == kUnset) { do { fd = open("/dev/urandom", O_RDONLY); @@ -208,6 +220,70 @@ static void init_once(void) { } } + int flags = fcntl(fd, F_GETFD); + if (flags == -1) { + // Native Client doesn't implement |fcntl|. + if (errno != ENOSYS) { + perror("failed to get flags from urandom fd"); + abort(); + } + } else { + flags |= FD_CLOEXEC; + if (fcntl(fd, F_SETFD, flags) == -1) { + perror("failed to set FD_CLOEXEC on urandom fd"); + abort(); + } + } + *urandom_fd_bss_get() = fd; +} + +DEFINE_STATIC_ONCE(wait_for_entropy_once) + +static void wait_for_entropy(void) { + int fd = *urandom_fd_bss_get(); + if (fd == kHaveGetrandom) { + // |getrandom| and |getentropy| support blocking in |fill_with_entropy| + // directly. For |getrandom|, we first probe with a non-blocking call to aid + // debugging. +#if defined(USE_NR_getrandom) + if (*getrandom_ready_bss_get()) { + // The entropy pool was already initialized in |init_once|. + return; + } + + uint8_t dummy; + ssize_t getrandom_ret = + boringssl_getrandom(&dummy, sizeof(dummy), GRND_NONBLOCK); + if (getrandom_ret == -1 && errno == EAGAIN) { + // Attempt to get the path of the current process to aid in debugging when + // something blocks. + const char *current_process = ""; +#if defined(OPENSSL_HAS_GETAUXVAL) + const unsigned long getauxval_ret = getauxval(AT_EXECFN); + if (getauxval_ret != 0) { + current_process = (const char *)getauxval_ret; + } +#endif + + fprintf( + stderr, + "%s: getrandom indicates that the entropy pool has not been " + "initialized. Rather than continue with poor entropy, this process " + "will block until entropy is available.\n", + current_process); + + getrandom_ret = + boringssl_getrandom(&dummy, sizeof(dummy), 0 /* no flags */); + } + + if (getrandom_ret != 1) { + perror("getrandom"); + abort(); + } +#endif // USE_NR_getrandom + return; + } + #if defined(BORINGSSL_FIPS) // In FIPS mode we ensure that the kernel has sufficient entropy before // continuing. This is automatically handled by getrandom, which requires @@ -229,23 +305,7 @@ static void init_once(void) { usleep(250000); } -#endif - - int flags = fcntl(fd, F_GETFD); - if (flags == -1) { - // Native Client doesn't implement |fcntl|. - if (errno != ENOSYS) { - perror("failed to get flags from urandom fd"); - abort(); - } - } else { - flags |= FD_CLOEXEC; - if (fcntl(fd, F_SETFD, flags) == -1) { - perror("failed to set FD_CLOEXEC on urandom fd"); - abort(); - } - } - *urandom_fd_bss_get() = fd; +#endif // BORINGSSL_FIPS } void RAND_set_urandom_fd(int fd) { @@ -283,14 +343,41 @@ void RAND_set_urandom_fd(int fd) { } // fill_with_entropy writes |len| bytes of entropy into |out|. It returns one -// on success and zero on error. -static char fill_with_entropy(uint8_t *out, size_t len) { +// on success and zero on error. If |block| is one, this function will block +// until the entropy pool is initialized. Otherwise, this function may fail, +// setting |errno| to |EAGAIN| if the entropy pool has not yet been initialized. +static int fill_with_entropy(uint8_t *out, size_t len, int block) { + if (len == 0) { + return 1; + } + + CRYPTO_once(rand_once_bss_get(), init_once); + if (block) { + CRYPTO_once(wait_for_entropy_once_bss_get(), wait_for_entropy); + } + + // Clear |errno| so it has defined value if |read| or |getrandom| + // "successfully" returns zero. + errno = 0; while (len > 0) { ssize_t r; if (*urandom_fd_bss_get() == kHaveGetrandom) { #if defined(USE_NR_getrandom) - r = boringssl_getrandom(out, len, 0 /* no flags */); + r = boringssl_getrandom(out, len, block ? 0 : GRND_NONBLOCK); +#elif defined(OPENSSL_MACOS) + if (__builtin_available(macos 10.12, *)) { + // |getentropy| can only request 256 bytes at a time. + size_t todo = len <= 256 ? len : 256; + if (getentropy(out, todo) != 0) { + r = -1; + } else { + r = (ssize_t)todo; + } + } else { + fprintf(stderr, "urandom fd corrupt.\n"); + abort(); + } #else // USE_NR_getrandom fprintf(stderr, "urandom fd corrupt.\n"); abort(); @@ -313,13 +400,7 @@ static char fill_with_entropy(uint8_t *out, size_t len) { // CRYPTO_sysrand puts |requested| random bytes into |out|. void CRYPTO_sysrand(uint8_t *out, size_t requested) { - if (requested == 0) { - return; - } - - CRYPTO_once(rand_once_bss_get(), init_once); - - if (!fill_with_entropy(out, requested)) { + if (!fill_with_entropy(out, requested, /*block=*/1)) { perror("entropy fill failed"); abort(); } @@ -331,5 +412,17 @@ void CRYPTO_sysrand(uint8_t *out, size_t requested) { #endif } -#endif /* !OPENSSL_WINDOWS && !defined(OPENSSL_FUCHSIA) && \ - !BORINGSSL_UNSAFE_DETERMINISTIC_MODE && !OPENSSL_TRUSTY */ +#if defined(BORINGSSL_FIPS) +void CRYPTO_sysrand_if_available(uint8_t *out, size_t requested) { + // Return all zeros if |fill_with_entropy| fails. + OPENSSL_memset(out, 0, requested); + + if (!fill_with_entropy(out, requested, /*block=*/0) && + errno != EAGAIN) { + perror("opportunistic entropy fill failed"); + abort(); + } +} +#endif // BORINGSSL_FIPS + +#endif // OPENSSL_URANDOM diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S index e08a5c5c..8d8237f6 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S @@ -63,3 +63,7 @@ CRYPTO_rdrand_multiple8_buf: .size CRYPTO_rdrand_multiple8_buf,.-CRYPTO_rdrand_multiple8_buf #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S index fa9fb5ac..c4989a1f 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S @@ -63,3 +63,7 @@ L$err: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c index dcc97984..803b9281 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rsa/rsa_impl.c @@ -1035,7 +1035,7 @@ static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, } // RSA key generation's bottleneck is discarding composites. If it fails - // trial division, do not bother computing a GCD or performing Rabin-Miller. + // trial division, do not bother computing a GCD or performing Miller-Rabin. if (!bn_odd_number_is_obviously_composite(out)) { // Check gcd(out-1, e) is one (steps 4.5 and 5.6). int relatively_prime; diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.linux.x86_64.S index 54a89b01..78e469aa 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.linux.x86_64.S @@ -1749,3 +1749,7 @@ rsaz_1024_gather5_avx2: .align 64 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.mac.x86_64.S index fa6ebb64..09529abf 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/rsaz-avx2.mac.x86_64.S @@ -1749,3 +1749,7 @@ L$inc: .p2align 6 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/self_check/self_check.c b/Sources/CNIOBoringSSL/crypto/fipsmodule/self_check/self_check.c index 105d6a3d..67184043 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/self_check/self_check.c +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/self_check/self_check.c @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -35,6 +36,21 @@ // compile this. #if !defined(_MSC_VER) +#if defined(BORINGSSL_FIPS) && defined(OPENSSL_ANDROID) +// FIPS builds on Android will test for flag files, named after the module hash, +// in /dev/boringssl/selftest/. If such a flag file exists, it's assumed that +// self-tests have already passed and thus do not need to be repeated. (The +// integrity tests always run, however.) +// +// If self-tests complete successfully and the environment variable named in +// |kFlagWriteEnableEnvVar| is present, then the flag file will be created. The +// flag file isn't written without the environment variable being set in order +// to avoid SELinux violations on Android. +#define BORINGSSL_FIPS_SELF_TEST_FLAG_FILE +static const char kFlagPrefix[] = "/dev/boringssl/selftest/"; +static const char kFlagWriteEnableEnvVar[] = "BORINGSSL_SELF_TEST_CREATE_FLAG"; +#endif + static void hexdump(const uint8_t *in, size_t len) { for (size_t i = 0; i < len; i++) { fprintf(stderr, "%02x", in[i]); @@ -227,7 +243,42 @@ static EC_KEY *self_test_ecdsa_key(void) { return ec_key; } -int BORINGSSL_self_test(void) { +#if defined(OPENSSL_ANDROID) +static const size_t kModuleDigestSize = SHA256_DIGEST_LENGTH; +#else +static const size_t kModuleDigestSize = SHA512_DIGEST_LENGTH; +#endif + +int boringssl_fips_self_test( + const uint8_t *module_hash, size_t module_hash_len) { +#if defined(BORINGSSL_FIPS_SELF_TEST_FLAG_FILE) + char flag_path[sizeof(kFlagPrefix) + 2*kModuleDigestSize]; + if (module_hash_len != 0) { + if (module_hash_len != kModuleDigestSize) { + fprintf(stderr, + "module hash of length %zu does not match expected length %zu\n", + module_hash_len, kModuleDigestSize); + BORINGSSL_FIPS_abort(); + } + + // Test whether the flag file exists. + memcpy(flag_path, kFlagPrefix, sizeof(kFlagPrefix) - 1); + static const char kHexTable[17] = "0123456789abcdef"; + for (size_t i = 0; i < kModuleDigestSize; i++) { + flag_path[sizeof(kFlagPrefix) - 1 + 2 * i] = + kHexTable[module_hash[i] >> 4]; + flag_path[sizeof(kFlagPrefix) - 1 + 2 * i + 1] = + kHexTable[module_hash[i] & 15]; + } + flag_path[sizeof(flag_path) - 1] = 0; + + if (access(flag_path, F_OK) == 0) { + // Flag file found. Skip self-tests. + return 1; + } + } +#endif // BORINGSSL_FIPS_SELF_TEST_FLAG_FILE + static const uint8_t kAESKey[16] = "BoringCrypto Key"; static const uint8_t kAESIV[16] = {0}; static const uint8_t kPlaintext[64] = @@ -577,6 +628,16 @@ int BORINGSSL_self_test(void) { ret = 1; +#if defined(BORINGSSL_FIPS_SELF_TEST_FLAG_FILE) + // Tests were successful. Write flag file if requested. + if (module_hash_len != 0 && getenv(kFlagWriteEnableEnvVar) != NULL) { + const int fd = open(flag_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd >= 0) { + close(fd); + } + } +#endif // BORINGSSL_FIPS_SELF_TEST_FLAG_FILE + err: EVP_AEAD_CTX_cleanup(&aead_ctx); RSA_free(rsa_key); @@ -586,4 +647,8 @@ err: return ret; } +int BORINGSSL_self_test(void) { + return boringssl_fips_self_test(NULL, 0); +} + #endif // !_MSC_VER diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-586.linux.x86.S index 9348034b..fe6b96e5 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-586.linux.x86.S @@ -3808,3 +3808,7 @@ _sha1_block_data_order_avx: .byte 114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.ios.arm.S index 7e3df3cf..c85ee85c 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.ios.arm.S @@ -1519,3 +1519,7 @@ OPENSSL_armcap_P: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.linux.arm.S index cbdaea83..83ef801c 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv4-large.linux.arm.S @@ -1511,3 +1511,7 @@ sha1_block_data_order_armv8: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S index 513cfe4c..04d3f2fa 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.ios.aarch64.S @@ -1233,3 +1233,7 @@ Lconst: .private_extern _OPENSSL_armcap_P #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S index 1249b4bb..fd4953a6 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-armv8.linux.aarch64.S @@ -1235,3 +1235,7 @@ sha1_block_armv8: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.linux.x86_64.S index 3afa3be5..c34fb084 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.linux.x86_64.S @@ -3601,3 +3601,7 @@ K_XX_XX: .align 64 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.mac.x86_64.S index 91689cc5..6e9acec9 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha1-x86_64.mac.x86_64.S @@ -3600,3 +3600,7 @@ K_XX_XX: .p2align 6 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-586.linux.x86.S index 8ae387b4..2c7d44e1 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-586.linux.x86.S @@ -5567,3 +5567,7 @@ sha256_block_data_order: .size sha256_block_data_order,.-.L_sha256_block_data_order_begin #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.ios.arm.S index 472ef8e6..405080fd 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.ios.arm.S @@ -2847,3 +2847,7 @@ OPENSSL_armcap_P: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.linux.arm.S index 0e9ae5f2..df05c0cf 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv4.linux.arm.S @@ -2839,3 +2839,7 @@ sha256_block_data_order_armv8: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.ios.aarch64.S index 52954e9b..f1563692 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.ios.aarch64.S @@ -1211,3 +1211,7 @@ Loop_hw: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.linux.aarch64.S index aed09dcb..0ce6f777 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-armv8.linux.aarch64.S @@ -1213,3 +1213,7 @@ sha256_block_armv8: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S index d1fb4237..fd55e248 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.linux.x86_64.S @@ -3973,3 +3973,7 @@ sha256_block_data_order_avx: .size sha256_block_data_order_avx,.-sha256_block_data_order_avx #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S index 2a9538b2..fd9470c2 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha256-x86_64.mac.x86_64.S @@ -3972,3 +3972,7 @@ L$epilogue_avx: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-586.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-586.linux.x86.S index 1f7f2539..a3dd7b3d 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-586.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-586.linux.x86.S @@ -2837,3 +2837,7 @@ sha512_block_data_order: .byte 62,0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.ios.arm.S index ade09cb0..4dcdaf5a 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.ios.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.ios.arm.S @@ -1900,3 +1900,7 @@ OPENSSL_armcap_P: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.linux.arm.S index 5dd2e19d..5b85ae47 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.linux.arm.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv4.linux.arm.S @@ -1894,3 +1894,7 @@ sha512_block_data_order_neon: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.ios.aarch64.S index 143d2f30..c6ec7cbf 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.ios.aarch64.S @@ -1083,3 +1083,7 @@ LK512: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.linux.aarch64.S index 59afe412..027c7158 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-armv8.linux.aarch64.S @@ -1085,3 +1085,7 @@ sha512_block_data_order: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.linux.x86_64.S index a6cd236c..d58f3687 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.linux.x86_64.S @@ -2992,3 +2992,7 @@ sha512_block_data_order_avx: .size sha512_block_data_order_avx,.-sha512_block_data_order_avx #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.mac.x86_64.S index 61b10981..100fc792 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/sha512-x86_64.mac.x86_64.S @@ -2991,3 +2991,7 @@ L$epilogue_avx: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.ios.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.ios.arm.S new file mode 100644 index 00000000..37912592 --- /dev/null +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.ios.arm.S @@ -0,0 +1,1272 @@ +#define BORINGSSL_PREFIX CNIOBoringSSL +#if defined(__arm__) && defined(__APPLE__) +// This file is generated from a similarly-named Perl script in the BoringSSL +// source tree. Do not edit by hand. + +#if !defined(__has_feature) +#define __has_feature(x) 0 +#endif +#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM) +#define OPENSSL_NO_ASM +#endif + +#if !defined(OPENSSL_NO_ASM) +#if defined(BORINGSSL_PREFIX) +#include +#endif +.syntax unified + + + + +#if defined(__thumb2__) +.thumb +#else +.code 32 +#endif + +.text + + +.align 7 @ totally strategic alignment +_vpaes_consts: +Lk_mc_forward:@ mc_forward +.quad 0x0407060500030201, 0x0C0F0E0D080B0A09 +.quad 0x080B0A0904070605, 0x000302010C0F0E0D +.quad 0x0C0F0E0D080B0A09, 0x0407060500030201 +.quad 0x000302010C0F0E0D, 0x080B0A0904070605 +Lk_mc_backward:@ mc_backward +.quad 0x0605040702010003, 0x0E0D0C0F0A09080B +.quad 0x020100030E0D0C0F, 0x0A09080B06050407 +.quad 0x0E0D0C0F0A09080B, 0x0605040702010003 +.quad 0x0A09080B06050407, 0x020100030E0D0C0F +Lk_sr:@ sr +.quad 0x0706050403020100, 0x0F0E0D0C0B0A0908 +.quad 0x030E09040F0A0500, 0x0B06010C07020D08 +.quad 0x0F060D040B020900, 0x070E050C030A0108 +.quad 0x0B0E0104070A0D00, 0x0306090C0F020508 + +@ +@ "Hot" constants +@ +Lk_inv:@ inv, inva +.quad 0x0E05060F0D080180, 0x040703090A0B0C02 +.quad 0x01040A060F0B0780, 0x030D0E0C02050809 +Lk_ipt:@ input transform (lo, hi) +.quad 0xC2B2E8985A2A7000, 0xCABAE09052227808 +.quad 0x4C01307D317C4D00, 0xCD80B1FCB0FDCC81 +Lk_sbo:@ sbou, sbot +.quad 0xD0D26D176FBDC700, 0x15AABF7AC502A878 +.quad 0xCFE474A55FBB6A00, 0x8E1E90D1412B35FA +Lk_sb1:@ sb1u, sb1t +.quad 0x3618D415FAE22300, 0x3BF7CCC10D2ED9EF +.quad 0xB19BE18FCB503E00, 0xA5DF7A6E142AF544 +Lk_sb2:@ sb2u, sb2t +.quad 0x69EB88400AE12900, 0xC2A163C8AB82234A +.quad 0xE27A93C60B712400, 0x5EB7E955BC982FCD + +.byte 86,101,99,116,111,114,32,80,101,114,109,117,116,97,116,105,111,110,32,65,69,83,32,102,111,114,32,65,82,77,118,55,32,78,69,79,78,44,32,77,105,107,101,32,72,97,109,98,117,114,103,32,40,83,116,97,110,102,111,114,100,32,85,110,105,118,101,114,115,105,116,121,41,0 +.align 2 + +.align 6 +@@ +@@ _aes_preheat +@@ +@@ Fills q9-q15 as specified below. +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_preheat +#endif +.align 4 +_vpaes_preheat: + adr r10, Lk_inv + vmov.i8 q9, #0x0f @ Lk_s0F + vld1.64 {q10,q11}, [r10]! @ Lk_inv + add r10, r10, #64 @ Skip Lk_ipt, Lk_sbo + vld1.64 {q12,q13}, [r10]! @ Lk_sb1 + vld1.64 {q14,q15}, [r10] @ Lk_sb2 + bx lr + +@@ +@@ _aes_encrypt_core +@@ +@@ AES-encrypt q0. +@@ +@@ Inputs: +@@ q0 = input +@@ q9-q15 as in _vpaes_preheat +@@ [r2] = scheduled keys +@@ +@@ Output in q0 +@@ Clobbers q1-q5, r8-r11 +@@ Preserves q6-q8 so you get some local vectors +@@ +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_encrypt_core +#endif +.align 4 +_vpaes_encrypt_core: + mov r9, r2 + ldr r8, [r2,#240] @ pull rounds + adr r11, Lk_ipt + @ vmovdqa .Lk_ipt(%rip), %xmm2 # iptlo + @ vmovdqa .Lk_ipt+16(%rip), %xmm3 # ipthi + vld1.64 {q2, q3}, [r11] + adr r11, Lk_mc_forward+16 + vld1.64 {q5}, [r9]! @ vmovdqu (%r9), %xmm5 # round0 key + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 + vtbl.8 d2, {q2}, d2 @ vpshufb %xmm1, %xmm2, %xmm1 + vtbl.8 d3, {q2}, d3 + vtbl.8 d4, {q3}, d0 @ vpshufb %xmm0, %xmm3, %xmm2 + vtbl.8 d5, {q3}, d1 + veor q0, q1, q5 @ vpxor %xmm5, %xmm1, %xmm0 + veor q0, q0, q2 @ vpxor %xmm2, %xmm0, %xmm0 + + @ .Lenc_entry ends with a bnz instruction which is normally paired with + @ subs in .Lenc_loop. + tst r8, r8 + b Lenc_entry + +.align 4 +Lenc_loop: + @ middle of middle round + add r10, r11, #0x40 + vtbl.8 d8, {q13}, d4 @ vpshufb %xmm2, %xmm13, %xmm4 # 4 = sb1u + vtbl.8 d9, {q13}, d5 + vld1.64 {q1}, [r11]! @ vmovdqa -0x40(%r11,%r10), %xmm1 # Lk_mc_forward[] + vtbl.8 d0, {q12}, d6 @ vpshufb %xmm3, %xmm12, %xmm0 # 0 = sb1t + vtbl.8 d1, {q12}, d7 + veor q4, q4, q5 @ vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k + vtbl.8 d10, {q15}, d4 @ vpshufb %xmm2, %xmm15, %xmm5 # 4 = sb2u + vtbl.8 d11, {q15}, d5 + veor q0, q0, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 0 = A + vtbl.8 d4, {q14}, d6 @ vpshufb %xmm3, %xmm14, %xmm2 # 2 = sb2t + vtbl.8 d5, {q14}, d7 + vld1.64 {q4}, [r10] @ vmovdqa (%r11,%r10), %xmm4 # Lk_mc_backward[] + vtbl.8 d6, {q0}, d2 @ vpshufb %xmm1, %xmm0, %xmm3 # 0 = B + vtbl.8 d7, {q0}, d3 + veor q2, q2, q5 @ vpxor %xmm5, %xmm2, %xmm2 # 2 = 2A + @ Write to q5 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d10, {q0}, d8 @ vpshufb %xmm4, %xmm0, %xmm0 # 3 = D + vtbl.8 d11, {q0}, d9 + veor q3, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 # 0 = 2A+B + vtbl.8 d8, {q3}, d2 @ vpshufb %xmm1, %xmm3, %xmm4 # 0 = 2B+C + vtbl.8 d9, {q3}, d3 + @ Here we restore the original q0/q5 usage. + veor q0, q5, q3 @ vpxor %xmm3, %xmm0, %xmm0 # 3 = 2A+B+D + and r11, r11, #~(1<<6) @ and $0x30, %r11 # ... mod 4 + veor q0, q0, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 0 = 2A+3B+C+D + subs r8, r8, #1 @ nr-- + +Lenc_entry: + @ top of round + vand q1, q0, q9 @ vpand %xmm0, %xmm9, %xmm1 # 0 = k + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 # 1 = i + vtbl.8 d10, {q11}, d2 @ vpshufb %xmm1, %xmm11, %xmm5 # 2 = a/k + vtbl.8 d11, {q11}, d3 + veor q1, q1, q0 @ vpxor %xmm0, %xmm1, %xmm1 # 0 = j + vtbl.8 d6, {q10}, d0 @ vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i + vtbl.8 d7, {q10}, d1 + vtbl.8 d8, {q10}, d2 @ vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j + vtbl.8 d9, {q10}, d3 + veor q3, q3, q5 @ vpxor %xmm5, %xmm3, %xmm3 # 3 = iak = 1/i + a/k + veor q4, q4, q5 @ vpxor %xmm5, %xmm4, %xmm4 # 4 = jak = 1/j + a/k + vtbl.8 d4, {q10}, d6 @ vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak + vtbl.8 d5, {q10}, d7 + vtbl.8 d6, {q10}, d8 @ vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak + vtbl.8 d7, {q10}, d9 + veor q2, q2, q1 @ vpxor %xmm1, %xmm2, %xmm2 # 2 = io + veor q3, q3, q0 @ vpxor %xmm0, %xmm3, %xmm3 # 3 = jo + vld1.64 {q5}, [r9]! @ vmovdqu (%r9), %xmm5 + bne Lenc_loop + + @ middle of last round + add r10, r11, #0x80 + + adr r11, Lk_sbo + @ Read to q1 instead of q4, so the vtbl.8 instruction below does not + @ overlap table and destination registers. + vld1.64 {q1}, [r11]! @ vmovdqa -0x60(%r10), %xmm4 # 3 : sbou + vld1.64 {q0}, [r11] @ vmovdqa -0x50(%r10), %xmm0 # 0 : sbot Lk_sbo+16 + vtbl.8 d8, {q1}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou + vtbl.8 d9, {q1}, d5 + vld1.64 {q1}, [r10] @ vmovdqa 0x40(%r11,%r10), %xmm1 # Lk_sr[] + @ Write to q2 instead of q0 below, to avoid overlapping table and + @ destination registers. + vtbl.8 d4, {q0}, d6 @ vpshufb %xmm3, %xmm0, %xmm0 # 0 = sb1t + vtbl.8 d5, {q0}, d7 + veor q4, q4, q5 @ vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k + veor q2, q2, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 0 = A + @ Here we restore the original q0/q2 usage. + vtbl.8 d0, {q2}, d2 @ vpshufb %xmm1, %xmm0, %xmm0 + vtbl.8 d1, {q2}, d3 + bx lr + + +.globl _vpaes_encrypt +.private_extern _vpaes_encrypt +#ifdef __thumb2__ +.thumb_func _vpaes_encrypt +#endif +.align 4 +_vpaes_encrypt: + @ _vpaes_encrypt_core uses r8-r11. Round up to r7-r11 to maintain stack + @ alignment. + stmdb sp!, {r7,r8,r9,r10,r11,lr} + @ _vpaes_encrypt_core uses q4-q5 (d8-d11), which are callee-saved. + vstmdb sp!, {d8,d9,d10,d11} + + vld1.64 {q0}, [r0] + bl _vpaes_preheat + bl _vpaes_encrypt_core + vst1.64 {q0}, [r1] + + vldmia sp!, {d8,d9,d10,d11} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return + + +@ +@ Decryption stuff +@ + +.align 4 +_vpaes_decrypt_consts: +Lk_dipt:@ decryption input transform +.quad 0x0F505B040B545F00, 0x154A411E114E451A +.quad 0x86E383E660056500, 0x12771772F491F194 +Lk_dsbo:@ decryption sbox final output +.quad 0x1387EA537EF94000, 0xC7AA6DB9D4943E2D +.quad 0x12D7560F93441D00, 0xCA4B8159D8C58E9C +Lk_dsb9:@ decryption sbox output *9*u, *9*t +.quad 0x851C03539A86D600, 0xCAD51F504F994CC9 +.quad 0xC03B1789ECD74900, 0x725E2C9EB2FBA565 +Lk_dsbd:@ decryption sbox output *D*u, *D*t +.quad 0x7D57CCDFE6B1A200, 0xF56E9B13882A4439 +.quad 0x3CE2FAF724C6CB00, 0x2931180D15DEEFD3 +Lk_dsbb:@ decryption sbox output *B*u, *B*t +.quad 0xD022649296B44200, 0x602646F6B0F2D404 +.quad 0xC19498A6CD596700, 0xF3FF0C3E3255AA6B +Lk_dsbe:@ decryption sbox output *E*u, *E*t +.quad 0x46F2929626D4D000, 0x2242600464B4F6B0 +.quad 0x0C55A6CDFFAAC100, 0x9467F36B98593E32 + + +@@ +@@ Decryption core +@@ +@@ Same API as encryption core, except it clobbers q12-q15 rather than using +@@ the values from _vpaes_preheat. q9-q11 must still be set from +@@ _vpaes_preheat. +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_decrypt_core +#endif +.align 4 +_vpaes_decrypt_core: + mov r9, r2 + ldr r8, [r2,#240] @ pull rounds + + @ This function performs shuffles with various constants. The x86_64 + @ version loads them on-demand into %xmm0-%xmm5. This does not work well + @ for ARMv7 because those registers are shuffle destinations. The ARMv8 + @ version preloads those constants into registers, but ARMv7 has half + @ the registers to work with. Instead, we load them on-demand into + @ q12-q15, registers normally use for preloaded constants. This is fine + @ because decryption doesn't use those constants. The values are + @ constant, so this does not interfere with potential 2x optimizations. + adr r7, Lk_dipt + + vld1.64 {q12,q13}, [r7] @ vmovdqa Lk_dipt(%rip), %xmm2 # iptlo + lsl r11, r8, #4 @ mov %rax, %r11; shl $4, %r11 + eor r11, r11, #0x30 @ xor $0x30, %r11 + adr r10, Lk_sr + and r11, r11, #0x30 @ and $0x30, %r11 + add r11, r11, r10 + adr r10, Lk_mc_forward+48 + + vld1.64 {q4}, [r9]! @ vmovdqu (%r9), %xmm4 # round0 key + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 + vtbl.8 d4, {q12}, d2 @ vpshufb %xmm1, %xmm2, %xmm2 + vtbl.8 d5, {q12}, d3 + vld1.64 {q5}, [r10] @ vmovdqa Lk_mc_forward+48(%rip), %xmm5 + @ vmovdqa .Lk_dipt+16(%rip), %xmm1 # ipthi + vtbl.8 d0, {q13}, d0 @ vpshufb %xmm0, %xmm1, %xmm0 + vtbl.8 d1, {q13}, d1 + veor q2, q2, q4 @ vpxor %xmm4, %xmm2, %xmm2 + veor q0, q0, q2 @ vpxor %xmm2, %xmm0, %xmm0 + + @ .Ldec_entry ends with a bnz instruction which is normally paired with + @ subs in .Ldec_loop. + tst r8, r8 + b Ldec_entry + +.align 4 +Ldec_loop: +@ +@ Inverse mix columns +@ + + @ We load .Lk_dsb* into q12-q15 on-demand. See the comment at the top of + @ the function. + adr r10, Lk_dsb9 + vld1.64 {q12,q13}, [r10]! @ vmovdqa -0x20(%r10),%xmm4 # 4 : sb9u + @ vmovdqa -0x10(%r10),%xmm1 # 0 : sb9t + @ Load sbd* ahead of time. + vld1.64 {q14,q15}, [r10]! @ vmovdqa 0x00(%r10),%xmm4 # 4 : sbdu + @ vmovdqa 0x10(%r10),%xmm1 # 0 : sbdt + vtbl.8 d8, {q12}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sb9u + vtbl.8 d9, {q12}, d5 + vtbl.8 d2, {q13}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb9t + vtbl.8 d3, {q13}, d7 + veor q0, q4, q0 @ vpxor %xmm4, %xmm0, %xmm0 + + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + + @ Load sbb* ahead of time. + vld1.64 {q12,q13}, [r10]! @ vmovdqa 0x20(%r10),%xmm4 # 4 : sbbu + @ vmovdqa 0x30(%r10),%xmm1 # 0 : sbbt + + vtbl.8 d8, {q14}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbdu + vtbl.8 d9, {q14}, d5 + @ Write to q1 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d2, {q0}, d10 @ vpshufb %xmm5, %xmm0, %xmm0 # MC ch + vtbl.8 d3, {q0}, d11 + @ Here we restore the original q0/q1 usage. This instruction is + @ reordered from the ARMv8 version so we do not clobber the vtbl.8 + @ below. + veor q0, q1, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 4 = ch + vtbl.8 d2, {q15}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbdt + vtbl.8 d3, {q15}, d7 + @ vmovdqa 0x20(%r10), %xmm4 # 4 : sbbu + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + @ vmovdqa 0x30(%r10), %xmm1 # 0 : sbbt + + @ Load sbd* ahead of time. + vld1.64 {q14,q15}, [r10]! @ vmovdqa 0x40(%r10),%xmm4 # 4 : sbeu + @ vmovdqa 0x50(%r10),%xmm1 # 0 : sbet + + vtbl.8 d8, {q12}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbbu + vtbl.8 d9, {q12}, d5 + @ Write to q1 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d2, {q0}, d10 @ vpshufb %xmm5, %xmm0, %xmm0 # MC ch + vtbl.8 d3, {q0}, d11 + @ Here we restore the original q0/q1 usage. This instruction is + @ reordered from the ARMv8 version so we do not clobber the vtbl.8 + @ below. + veor q0, q1, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 4 = ch + vtbl.8 d2, {q13}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbbt + vtbl.8 d3, {q13}, d7 + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + + vtbl.8 d8, {q14}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbeu + vtbl.8 d9, {q14}, d5 + @ Write to q1 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d2, {q0}, d10 @ vpshufb %xmm5, %xmm0, %xmm0 # MC ch + vtbl.8 d3, {q0}, d11 + @ Here we restore the original q0/q1 usage. This instruction is + @ reordered from the ARMv8 version so we do not clobber the vtbl.8 + @ below. + veor q0, q1, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 4 = ch + vtbl.8 d2, {q15}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbet + vtbl.8 d3, {q15}, d7 + vext.8 q5, q5, q5, #12 @ vpalignr $12, %xmm5, %xmm5, %xmm5 + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + subs r8, r8, #1 @ sub $1,%rax # nr-- + +Ldec_entry: + @ top of round + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 # 0 = k + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 # 1 = i + vtbl.8 d4, {q11}, d2 @ vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k + vtbl.8 d5, {q11}, d3 + veor q1, q1, q0 @ vpxor %xmm0, %xmm1, %xmm1 # 0 = j + vtbl.8 d6, {q10}, d0 @ vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i + vtbl.8 d7, {q10}, d1 + vtbl.8 d8, {q10}, d2 @ vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j + vtbl.8 d9, {q10}, d3 + veor q3, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k + veor q4, q4, q2 @ vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k + vtbl.8 d4, {q10}, d6 @ vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak + vtbl.8 d5, {q10}, d7 + vtbl.8 d6, {q10}, d8 @ vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak + vtbl.8 d7, {q10}, d9 + veor q2, q2, q1 @ vpxor %xmm1, %xmm2, %xmm2 # 2 = io + veor q3, q3, q0 @ vpxor %xmm0, %xmm3, %xmm3 # 3 = jo + vld1.64 {q0}, [r9]! @ vmovdqu (%r9), %xmm0 + bne Ldec_loop + + @ middle of last round + + adr r10, Lk_dsbo + + @ Write to q1 rather than q4 to avoid overlapping table and destination. + vld1.64 {q1}, [r10]! @ vmovdqa 0x60(%r10), %xmm4 # 3 : sbou + vtbl.8 d8, {q1}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou + vtbl.8 d9, {q1}, d5 + @ Write to q2 rather than q1 to avoid overlapping table and destination. + vld1.64 {q2}, [r10] @ vmovdqa 0x70(%r10), %xmm1 # 0 : sbot + vtbl.8 d2, {q2}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb1t + vtbl.8 d3, {q2}, d7 + vld1.64 {q2}, [r11] @ vmovdqa -0x160(%r11), %xmm2 # Lk_sr-Lk_dsbd=-0x160 + veor q4, q4, q0 @ vpxor %xmm0, %xmm4, %xmm4 # 4 = sb1u + k + @ Write to q1 rather than q0 so the table and destination registers + @ below do not overlap. + veor q1, q1, q4 @ vpxor %xmm4, %xmm1, %xmm0 # 0 = A + vtbl.8 d0, {q1}, d4 @ vpshufb %xmm2, %xmm0, %xmm0 + vtbl.8 d1, {q1}, d5 + bx lr + + +.globl _vpaes_decrypt +.private_extern _vpaes_decrypt +#ifdef __thumb2__ +.thumb_func _vpaes_decrypt +#endif +.align 4 +_vpaes_decrypt: + @ _vpaes_decrypt_core uses r7-r11. + stmdb sp!, {r7,r8,r9,r10,r11,lr} + @ _vpaes_decrypt_core uses q4-q5 (d8-d11), which are callee-saved. + vstmdb sp!, {d8,d9,d10,d11} + + vld1.64 {q0}, [r0] + bl _vpaes_preheat + bl _vpaes_decrypt_core + vst1.64 {q0}, [r1] + + vldmia sp!, {d8,d9,d10,d11} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@ @@ +@@ AES key schedule @@ +@@ @@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +@ This function diverges from both x86_64 and armv7 in which constants are +@ pinned. x86_64 has a common preheat function for all operations. aarch64 +@ separates them because it has enough registers to pin nearly all constants. +@ armv7 does not have enough registers, but needing explicit loads and stores +@ also complicates using x86_64's register allocation directly. +@ +@ We pin some constants for convenience and leave q14 and q15 free to load +@ others on demand. + +@ +@ Key schedule constants +@ + +.align 4 +_vpaes_key_consts: +Lk_dksd:@ decryption key schedule: invskew x*D +.quad 0xFEB91A5DA3E44700, 0x0740E3A45A1DBEF9 +.quad 0x41C277F4B5368300, 0x5FDC69EAAB289D1E +Lk_dksb:@ decryption key schedule: invskew x*B +.quad 0x9A4FCA1F8550D500, 0x03D653861CC94C99 +.quad 0x115BEDA7B6FC4A00, 0xD993256F7E3482C8 +Lk_dkse:@ decryption key schedule: invskew x*E + 0x63 +.quad 0xD5031CCA1FC9D600, 0x53859A4C994F5086 +.quad 0xA23196054FDC7BE8, 0xCD5EF96A20B31487 +Lk_dks9:@ decryption key schedule: invskew x*9 +.quad 0xB6116FC87ED9A700, 0x4AED933482255BFC +.quad 0x4576516227143300, 0x8BB89FACE9DAFDCE + +Lk_rcon:@ rcon +.quad 0x1F8391B9AF9DEEB6, 0x702A98084D7C7D81 + +Lk_opt:@ output transform +.quad 0xFF9F4929D6B66000, 0xF7974121DEBE6808 +.quad 0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0 +Lk_deskew:@ deskew tables: inverts the sbox's "skew" +.quad 0x07E4A34047A4E300, 0x1DFEB95A5DBEF91A +.quad 0x5F36B5DC83EA6900, 0x2841C2ABF49D1E77 + + +#ifdef __thumb2__ +.thumb_func _vpaes_key_preheat +#endif +.align 4 +_vpaes_key_preheat: + adr r11, Lk_rcon + vmov.i8 q12, #0x5b @ Lk_s63 + adr r10, Lk_inv @ Must be aligned to 8 mod 16. + vmov.i8 q9, #0x0f @ Lk_s0F + vld1.64 {q10,q11}, [r10] @ Lk_inv + vld1.64 {q8}, [r11] @ Lk_rcon + bx lr + + +#ifdef __thumb2__ +.thumb_func _vpaes_schedule_core +#endif +.align 4 +_vpaes_schedule_core: + @ We only need to save lr, but ARM requires an 8-byte stack alignment, + @ so save an extra register. + stmdb sp!, {r3,lr} + + bl _vpaes_key_preheat @ load the tables + + adr r11, Lk_ipt @ Must be aligned to 8 mod 16. + vld1.64 {q0}, [r0]! @ vmovdqu (%rdi), %xmm0 # load key (unaligned) + + @ input transform + @ Use q4 here rather than q3 so .Lschedule_am_decrypting does not + @ overlap table and destination. + vmov q4, q0 @ vmovdqa %xmm0, %xmm3 + bl _vpaes_schedule_transform + adr r10, Lk_sr @ Must be aligned to 8 mod 16. + vmov q7, q0 @ vmovdqa %xmm0, %xmm7 + + add r8, r8, r10 + tst r3, r3 + bne Lschedule_am_decrypting + + @ encrypting, output zeroth round key after transform + vst1.64 {q0}, [r2] @ vmovdqu %xmm0, (%rdx) + b Lschedule_go + +Lschedule_am_decrypting: + @ decrypting, output zeroth round key after shiftrows + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10), %xmm1 + vtbl.8 d6, {q4}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q4}, d3 + vst1.64 {q3}, [r2] @ vmovdqu %xmm3, (%rdx) + eor r8, r8, #0x30 @ xor $0x30, %r8 + +Lschedule_go: + cmp r1, #192 @ cmp $192, %esi + bhi Lschedule_256 + beq Lschedule_192 + @ 128: fall though + +@@ +@@ .schedule_128 +@@ +@@ 128-bit specific part of key schedule. +@@ +@@ This schedule is really simple, because all its parts +@@ are accomplished by the subroutines. +@@ +Lschedule_128: + mov r0, #10 @ mov $10, %esi + +Loop_schedule_128: + bl _vpaes_schedule_round + subs r0, r0, #1 @ dec %esi + beq Lschedule_mangle_last + bl _vpaes_schedule_mangle @ write output + b Loop_schedule_128 + +@@ +@@ .aes_schedule_192 +@@ +@@ 192-bit specific part of key schedule. +@@ +@@ The main body of this schedule is the same as the 128-bit +@@ schedule, but with more smearing. The long, high side is +@@ stored in q7 as before, and the short, low side is in +@@ the high bits of q6. +@@ +@@ This schedule is somewhat nastier, however, because each +@@ round produces 192 bits of key material, or 1.5 round keys. +@@ Therefore, on each cycle we do 2 rounds and produce 3 round +@@ keys. +@@ +.align 4 +Lschedule_192: + sub r0, r0, #8 + vld1.64 {q0}, [r0] @ vmovdqu 8(%rdi),%xmm0 # load key part 2 (very unaligned) + bl _vpaes_schedule_transform @ input transform + vmov q6, q0 @ vmovdqa %xmm0, %xmm6 # save short part + vmov.i8 d12, #0 @ vpxor %xmm4, %xmm4, %xmm4 # clear 4 + @ vmovhlps %xmm4, %xmm6, %xmm6 # clobber low side with zeros + mov r0, #4 @ mov $4, %esi + +Loop_schedule_192: + bl _vpaes_schedule_round + vext.8 q0, q6, q0, #8 @ vpalignr $8,%xmm6,%xmm0,%xmm0 + bl _vpaes_schedule_mangle @ save key n + bl _vpaes_schedule_192_smear + bl _vpaes_schedule_mangle @ save key n+1 + bl _vpaes_schedule_round + subs r0, r0, #1 @ dec %esi + beq Lschedule_mangle_last + bl _vpaes_schedule_mangle @ save key n+2 + bl _vpaes_schedule_192_smear + b Loop_schedule_192 + +@@ +@@ .aes_schedule_256 +@@ +@@ 256-bit specific part of key schedule. +@@ +@@ The structure here is very similar to the 128-bit +@@ schedule, but with an additional "low side" in +@@ q6. The low side's rounds are the same as the +@@ high side's, except no rcon and no rotation. +@@ +.align 4 +Lschedule_256: + vld1.64 {q0}, [r0] @ vmovdqu 16(%rdi),%xmm0 # load key part 2 (unaligned) + bl _vpaes_schedule_transform @ input transform + mov r0, #7 @ mov $7, %esi + +Loop_schedule_256: + bl _vpaes_schedule_mangle @ output low result + vmov q6, q0 @ vmovdqa %xmm0, %xmm6 # save cur_lo in xmm6 + + @ high round + bl _vpaes_schedule_round + subs r0, r0, #1 @ dec %esi + beq Lschedule_mangle_last + bl _vpaes_schedule_mangle + + @ low round. swap xmm7 and xmm6 + vdup.32 q0, d1[1] @ vpshufd $0xFF, %xmm0, %xmm0 + vmov.i8 q4, #0 + vmov q5, q7 @ vmovdqa %xmm7, %xmm5 + vmov q7, q6 @ vmovdqa %xmm6, %xmm7 + bl _vpaes_schedule_low_round + vmov q7, q5 @ vmovdqa %xmm5, %xmm7 + + b Loop_schedule_256 + +@@ +@@ .aes_schedule_mangle_last +@@ +@@ Mangler for last round of key schedule +@@ Mangles q0 +@@ when encrypting, outputs out(q0) ^ 63 +@@ when decrypting, outputs unskew(q0) +@@ +@@ Always called right before return... jumps to cleanup and exits +@@ +.align 4 +Lschedule_mangle_last: + @ schedule last round key from xmm0 + adr r11, Lk_deskew @ lea Lk_deskew(%rip),%r11 # prepare to deskew + tst r3, r3 + bne Lschedule_mangle_last_dec + + @ encrypting + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10),%xmm1 + adr r11, Lk_opt @ lea Lk_opt(%rip), %r11 # prepare to output transform + add r2, r2, #32 @ add $32, %rdx + vmov q2, q0 + vtbl.8 d0, {q2}, d2 @ vpshufb %xmm1, %xmm0, %xmm0 # output permute + vtbl.8 d1, {q2}, d3 + +Lschedule_mangle_last_dec: + sub r2, r2, #16 @ add $-16, %rdx + veor q0, q0, q12 @ vpxor Lk_s63(%rip), %xmm0, %xmm0 + bl _vpaes_schedule_transform @ output transform + vst1.64 {q0}, [r2] @ vmovdqu %xmm0, (%rdx) # save last key + + @ cleanup + veor q0, q0, q0 @ vpxor %xmm0, %xmm0, %xmm0 + veor q1, q1, q1 @ vpxor %xmm1, %xmm1, %xmm1 + veor q2, q2, q2 @ vpxor %xmm2, %xmm2, %xmm2 + veor q3, q3, q3 @ vpxor %xmm3, %xmm3, %xmm3 + veor q4, q4, q4 @ vpxor %xmm4, %xmm4, %xmm4 + veor q5, q5, q5 @ vpxor %xmm5, %xmm5, %xmm5 + veor q6, q6, q6 @ vpxor %xmm6, %xmm6, %xmm6 + veor q7, q7, q7 @ vpxor %xmm7, %xmm7, %xmm7 + ldmia sp!, {r3,pc} @ return + + +@@ +@@ .aes_schedule_192_smear +@@ +@@ Smear the short, low side in the 192-bit key schedule. +@@ +@@ Inputs: +@@ q7: high side, b a x y +@@ q6: low side, d c 0 0 +@@ +@@ Outputs: +@@ q6: b+c+d b+c 0 0 +@@ q0: b+c+d b+c b a +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_schedule_192_smear +#endif +.align 4 +_vpaes_schedule_192_smear: + vmov.i8 q1, #0 + vdup.32 q0, d15[1] + vshl.i64 q1, q6, #32 @ vpshufd $0x80, %xmm6, %xmm1 # d c 0 0 -> c 0 0 0 + vmov d0, d15 @ vpshufd $0xFE, %xmm7, %xmm0 # b a _ _ -> b b b a + veor q6, q6, q1 @ vpxor %xmm1, %xmm6, %xmm6 # -> c+d c 0 0 + veor q1, q1, q1 @ vpxor %xmm1, %xmm1, %xmm1 + veor q6, q6, q0 @ vpxor %xmm0, %xmm6, %xmm6 # -> b+c+d b+c b a + vmov q0, q6 @ vmovdqa %xmm6, %xmm0 + vmov d12, d2 @ vmovhlps %xmm1, %xmm6, %xmm6 # clobber low side with zeros + bx lr + + +@@ +@@ .aes_schedule_round +@@ +@@ Runs one main round of the key schedule on q0, q7 +@@ +@@ Specifically, runs subbytes on the high dword of q0 +@@ then rotates it by one byte and xors into the low dword of +@@ q7. +@@ +@@ Adds rcon from low byte of q8, then rotates q8 for +@@ next rcon. +@@ +@@ Smears the dwords of q7 by xoring the low into the +@@ second low, result into third, result into highest. +@@ +@@ Returns results in q7 = q0. +@@ Clobbers q1-q4, r11. +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_schedule_round +#endif +.align 4 +_vpaes_schedule_round: + @ extract rcon from xmm8 + vmov.i8 q4, #0 @ vpxor %xmm4, %xmm4, %xmm4 + vext.8 q1, q8, q4, #15 @ vpalignr $15, %xmm8, %xmm4, %xmm1 + vext.8 q8, q8, q8, #15 @ vpalignr $15, %xmm8, %xmm8, %xmm8 + veor q7, q7, q1 @ vpxor %xmm1, %xmm7, %xmm7 + + @ rotate + vdup.32 q0, d1[1] @ vpshufd $0xFF, %xmm0, %xmm0 + vext.8 q0, q0, q0, #1 @ vpalignr $1, %xmm0, %xmm0, %xmm0 + + @ fall through... + + @ low round: same as high round, but no rotation and no rcon. +_vpaes_schedule_low_round: + @ The x86_64 version pins .Lk_sb1 in %xmm13 and .Lk_sb1+16 in %xmm12. + @ We pin other values in _vpaes_key_preheat, so load them now. + adr r11, Lk_sb1 + vld1.64 {q14,q15}, [r11] + + @ smear xmm7 + vext.8 q1, q4, q7, #12 @ vpslldq $4, %xmm7, %xmm1 + veor q7, q7, q1 @ vpxor %xmm1, %xmm7, %xmm7 + vext.8 q4, q4, q7, #8 @ vpslldq $8, %xmm7, %xmm4 + + @ subbytes + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 # 0 = k + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 # 1 = i + veor q7, q7, q4 @ vpxor %xmm4, %xmm7, %xmm7 + vtbl.8 d4, {q11}, d2 @ vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k + vtbl.8 d5, {q11}, d3 + veor q1, q1, q0 @ vpxor %xmm0, %xmm1, %xmm1 # 0 = j + vtbl.8 d6, {q10}, d0 @ vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i + vtbl.8 d7, {q10}, d1 + veor q3, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k + vtbl.8 d8, {q10}, d2 @ vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j + vtbl.8 d9, {q10}, d3 + veor q7, q7, q12 @ vpxor Lk_s63(%rip), %xmm7, %xmm7 + vtbl.8 d6, {q10}, d6 @ vpshufb %xmm3, %xmm10, %xmm3 # 2 = 1/iak + vtbl.8 d7, {q10}, d7 + veor q4, q4, q2 @ vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k + vtbl.8 d4, {q10}, d8 @ vpshufb %xmm4, %xmm10, %xmm2 # 3 = 1/jak + vtbl.8 d5, {q10}, d9 + veor q3, q3, q1 @ vpxor %xmm1, %xmm3, %xmm3 # 2 = io + veor q2, q2, q0 @ vpxor %xmm0, %xmm2, %xmm2 # 3 = jo + vtbl.8 d8, {q15}, d6 @ vpshufb %xmm3, %xmm13, %xmm4 # 4 = sbou + vtbl.8 d9, {q15}, d7 + vtbl.8 d2, {q14}, d4 @ vpshufb %xmm2, %xmm12, %xmm1 # 0 = sb1t + vtbl.8 d3, {q14}, d5 + veor q1, q1, q4 @ vpxor %xmm4, %xmm1, %xmm1 # 0 = sbox output + + @ add in smeared stuff + veor q0, q1, q7 @ vpxor %xmm7, %xmm1, %xmm0 + veor q7, q1, q7 @ vmovdqa %xmm0, %xmm7 + bx lr + + +@@ +@@ .aes_schedule_transform +@@ +@@ Linear-transform q0 according to tables at [r11] +@@ +@@ Requires that q9 = 0x0F0F... as in preheat +@@ Output in q0 +@@ Clobbers q1, q2, q14, q15 +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_schedule_transform +#endif +.align 4 +_vpaes_schedule_transform: + vld1.64 {q14,q15}, [r11] @ vmovdqa (%r11), %xmm2 # lo + @ vmovdqa 16(%r11), %xmm1 # hi + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 + vtbl.8 d4, {q14}, d2 @ vpshufb %xmm1, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d3 + vtbl.8 d0, {q15}, d0 @ vpshufb %xmm0, %xmm1, %xmm0 + vtbl.8 d1, {q15}, d1 + veor q0, q0, q2 @ vpxor %xmm2, %xmm0, %xmm0 + bx lr + + +@@ +@@ .aes_schedule_mangle +@@ +@@ Mangles q0 from (basis-transformed) standard version +@@ to our version. +@@ +@@ On encrypt, +@@ xor with 0x63 +@@ multiply by circulant 0,1,1,1 +@@ apply shiftrows transform +@@ +@@ On decrypt, +@@ xor with 0x63 +@@ multiply by "inverse mixcolumns" circulant E,B,D,9 +@@ deskew +@@ apply shiftrows transform +@@ +@@ +@@ Writes out to [r2], and increments or decrements it +@@ Keeps track of round number mod 4 in r8 +@@ Preserves q0 +@@ Clobbers q1-q5 +@@ +#ifdef __thumb2__ +.thumb_func _vpaes_schedule_mangle +#endif +.align 4 +_vpaes_schedule_mangle: + tst r3, r3 + vmov q4, q0 @ vmovdqa %xmm0, %xmm4 # save xmm0 for later + adr r11, Lk_mc_forward @ Must be aligned to 8 mod 16. + vld1.64 {q5}, [r11] @ vmovdqa Lk_mc_forward(%rip),%xmm5 + bne Lschedule_mangle_dec + + @ encrypting + @ Write to q2 so we do not overlap table and destination below. + veor q2, q0, q12 @ vpxor Lk_s63(%rip), %xmm0, %xmm4 + add r2, r2, #16 @ add $16, %rdx + vtbl.8 d8, {q2}, d10 @ vpshufb %xmm5, %xmm4, %xmm4 + vtbl.8 d9, {q2}, d11 + vtbl.8 d2, {q4}, d10 @ vpshufb %xmm5, %xmm4, %xmm1 + vtbl.8 d3, {q4}, d11 + vtbl.8 d6, {q1}, d10 @ vpshufb %xmm5, %xmm1, %xmm3 + vtbl.8 d7, {q1}, d11 + veor q4, q4, q1 @ vpxor %xmm1, %xmm4, %xmm4 + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10), %xmm1 + veor q3, q3, q4 @ vpxor %xmm4, %xmm3, %xmm3 + + b Lschedule_mangle_both +.align 4 +Lschedule_mangle_dec: + @ inverse mix columns + adr r11, Lk_dksd @ lea Lk_dksd(%rip),%r11 + vshr.u8 q1, q4, #4 @ vpsrlb $4, %xmm4, %xmm1 # 1 = hi + vand q4, q4, q9 @ vpand %xmm9, %xmm4, %xmm4 # 4 = lo + + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x00(%r11), %xmm2 + @ vmovdqa 0x10(%r11), %xmm3 + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + vtbl.8 d6, {q15}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q15}, d3 + @ Load .Lk_dksb ahead of time. + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x20(%r11), %xmm2 + @ vmovdqa 0x30(%r11), %xmm3 + @ Write to q13 so we do not overlap table and destination. + veor q13, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 + vtbl.8 d6, {q13}, d10 @ vpshufb %xmm5, %xmm3, %xmm3 + vtbl.8 d7, {q13}, d11 + + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + veor q2, q2, q3 @ vpxor %xmm3, %xmm2, %xmm2 + vtbl.8 d6, {q15}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q15}, d3 + @ Load .Lk_dkse ahead of time. + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x40(%r11), %xmm2 + @ vmovdqa 0x50(%r11), %xmm3 + @ Write to q13 so we do not overlap table and destination. + veor q13, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 + vtbl.8 d6, {q13}, d10 @ vpshufb %xmm5, %xmm3, %xmm3 + vtbl.8 d7, {q13}, d11 + + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + veor q2, q2, q3 @ vpxor %xmm3, %xmm2, %xmm2 + vtbl.8 d6, {q15}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q15}, d3 + @ Load .Lk_dkse ahead of time. + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x60(%r11), %xmm2 + @ vmovdqa 0x70(%r11), %xmm4 + @ Write to q13 so we do not overlap table and destination. + veor q13, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 + + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + vtbl.8 d6, {q13}, d10 @ vpshufb %xmm5, %xmm3, %xmm3 + vtbl.8 d7, {q13}, d11 + vtbl.8 d8, {q15}, d2 @ vpshufb %xmm1, %xmm4, %xmm4 + vtbl.8 d9, {q15}, d3 + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10), %xmm1 + veor q2, q2, q3 @ vpxor %xmm3, %xmm2, %xmm2 + veor q3, q4, q2 @ vpxor %xmm2, %xmm4, %xmm3 + + sub r2, r2, #16 @ add $-16, %rdx + +Lschedule_mangle_both: + @ Write to q2 so table and destination do not overlap. + vtbl.8 d4, {q3}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d5, {q3}, d3 + add r8, r8, #64-16 @ add $-16, %r8 + and r8, r8, #~(1<<6) @ and $0x30, %r8 + vst1.64 {q2}, [r2] @ vmovdqu %xmm3, (%rdx) + bx lr + + +.globl _vpaes_set_encrypt_key +.private_extern _vpaes_set_encrypt_key +#ifdef __thumb2__ +.thumb_func _vpaes_set_encrypt_key +#endif +.align 4 +_vpaes_set_encrypt_key: + stmdb sp!, {r7,r8,r9,r10,r11, lr} + vstmdb sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + + lsr r9, r1, #5 @ shr $5,%eax + add r9, r9, #5 @ $5,%eax + str r9, [r2,#240] @ mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5; + + mov r3, #0 @ mov $0,%ecx + mov r8, #0x30 @ mov $0x30,%r8d + bl _vpaes_schedule_core + eor r0, r0, r0 + + vldmia sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return + + +.globl _vpaes_set_decrypt_key +.private_extern _vpaes_set_decrypt_key +#ifdef __thumb2__ +.thumb_func _vpaes_set_decrypt_key +#endif +.align 4 +_vpaes_set_decrypt_key: + stmdb sp!, {r7,r8,r9,r10,r11, lr} + vstmdb sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + + lsr r9, r1, #5 @ shr $5,%eax + add r9, r9, #5 @ $5,%eax + str r9, [r2,#240] @ mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5; + lsl r9, r9, #4 @ shl $4,%eax + add r2, r2, #16 @ lea 16(%rdx,%rax),%rdx + add r2, r2, r9 + + mov r3, #1 @ mov $1,%ecx + lsr r8, r1, #1 @ shr $1,%r8d + and r8, r8, #32 @ and $32,%r8d + eor r8, r8, #32 @ xor $32,%r8d # nbits==192?0:32 + bl _vpaes_schedule_core + + vldmia sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return + + +@ Additional constants for converting to bsaes. + +.align 4 +_vpaes_convert_consts: +@ .Lk_opt_then_skew applies skew(opt(x)) XOR 0x63, where skew is the linear +@ transform in the AES S-box. 0x63 is incorporated into the low half of the +@ table. This was computed with the following script: +@ +@ def u64s_to_u128(x, y): +@ return x | (y << 64) +@ def u128_to_u64s(w): +@ return w & ((1<<64)-1), w >> 64 +@ def get_byte(w, i): +@ return (w >> (i*8)) & 0xff +@ def apply_table(table, b): +@ lo = b & 0xf +@ hi = b >> 4 +@ return get_byte(table[0], lo) ^ get_byte(table[1], hi) +@ def opt(b): +@ table = [ +@ u64s_to_u128(0xFF9F4929D6B66000, 0xF7974121DEBE6808), +@ u64s_to_u128(0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0), +@ ] +@ return apply_table(table, b) +@ def rot_byte(b, n): +@ return 0xff & ((b << n) | (b >> (8-n))) +@ def skew(x): +@ return (x ^ rot_byte(x, 1) ^ rot_byte(x, 2) ^ rot_byte(x, 3) ^ +@ rot_byte(x, 4)) +@ table = [0, 0] +@ for i in range(16): +@ table[0] |= (skew(opt(i)) ^ 0x63) << (i*8) +@ table[1] |= skew(opt(i<<4)) << (i*8) +@ print(" .quad 0x%016x, 0x%016x" % u128_to_u64s(table[0])) +@ print(" .quad 0x%016x, 0x%016x" % u128_to_u64s(table[1])) +Lk_opt_then_skew: +.quad 0x9cb8436798bc4763, 0x6440bb9f6044bf9b +.quad 0x1f30062936192f00, 0xb49bad829db284ab + +@ .Lk_decrypt_transform is a permutation which performs an 8-bit left-rotation +@ followed by a byte-swap on each 32-bit word of a vector. E.g., 0x11223344 +@ becomes 0x22334411 and then 0x11443322. +Lk_decrypt_transform: +.quad 0x0704050603000102, 0x0f0c0d0e0b08090a + + +@ void vpaes_encrypt_key_to_bsaes(AES_KEY *bsaes, const AES_KEY *vpaes); +.globl _vpaes_encrypt_key_to_bsaes +.private_extern _vpaes_encrypt_key_to_bsaes +#ifdef __thumb2__ +.thumb_func _vpaes_encrypt_key_to_bsaes +#endif +.align 4 +_vpaes_encrypt_key_to_bsaes: + stmdb sp!, {r11, lr} + + @ See _vpaes_schedule_core for the key schedule logic. In particular, + @ _vpaes_schedule_transform(.Lk_ipt) (section 2.2 of the paper), + @ _vpaes_schedule_mangle (section 4.3), and .Lschedule_mangle_last + @ contain the transformations not in the bsaes representation. This + @ function inverts those transforms. + @ + @ Note also that bsaes-armv7.pl expects aes-armv4.pl's key + @ representation, which does not match the other aes_nohw_* + @ implementations. The ARM aes_nohw_* stores each 32-bit word + @ byteswapped, as a convenience for (unsupported) big-endian ARM, at the + @ cost of extra REV and VREV32 operations in little-endian ARM. + + vmov.i8 q9, #0x0f @ Required by _vpaes_schedule_transform + adr r2, Lk_mc_forward @ Must be aligned to 8 mod 16. + add r3, r2, 0x90 @ Lk_sr+0x10-Lk_mc_forward = 0x90 (Apple's toolchain doesn't support the expression) + + vld1.64 {q12}, [r2] + vmov.i8 q10, #0x5b @ Lk_s63 from vpaes-x86_64 + adr r11, Lk_opt @ Must be aligned to 8 mod 16. + vmov.i8 q11, #0x63 @ LK_s63 without Lk_ipt applied + + @ vpaes stores one fewer round count than bsaes, but the number of keys + @ is the same. + ldr r2, [r1,#240] + add r2, r2, #1 + str r2, [r0,#240] + + @ The first key is transformed with _vpaes_schedule_transform(.Lk_ipt). + @ Invert this with .Lk_opt. + vld1.64 {q0}, [r1]! + bl _vpaes_schedule_transform + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + + @ The middle keys have _vpaes_schedule_transform(.Lk_ipt) applied, + @ followed by _vpaes_schedule_mangle. _vpaes_schedule_mangle XORs 0x63, + @ multiplies by the circulant 0,1,1,1, then applies ShiftRows. +Loop_enc_key_to_bsaes: + vld1.64 {q0}, [r1]! + + @ Invert the ShiftRows step (see .Lschedule_mangle_both). Note we cycle + @ r3 in the opposite direction and start at .Lk_sr+0x10 instead of 0x30. + @ We use r3 rather than r8 to avoid a callee-saved register. + vld1.64 {q1}, [r3] + vtbl.8 d4, {q0}, d2 + vtbl.8 d5, {q0}, d3 + add r3, r3, #16 + and r3, r3, #~(1<<6) + vmov q0, q2 + + @ Handle the last key differently. + subs r2, r2, #1 + beq Loop_enc_key_to_bsaes_last + + @ Multiply by the circulant. This is its own inverse. + vtbl.8 d2, {q0}, d24 + vtbl.8 d3, {q0}, d25 + vmov q0, q1 + vtbl.8 d4, {q1}, d24 + vtbl.8 d5, {q1}, d25 + veor q0, q0, q2 + vtbl.8 d2, {q2}, d24 + vtbl.8 d3, {q2}, d25 + veor q0, q0, q1 + + @ XOR and finish. + veor q0, q0, q10 + bl _vpaes_schedule_transform + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + b Loop_enc_key_to_bsaes + +Loop_enc_key_to_bsaes_last: + @ The final key does not have a basis transform (note + @ .Lschedule_mangle_last inverts the original transform). It only XORs + @ 0x63 and applies ShiftRows. The latter was already inverted in the + @ loop. Note that, because we act on the original representation, we use + @ q11, not q10. + veor q0, q0, q11 + vrev32.8 q0, q0 + vst1.64 {q0}, [r0] + + @ Wipe registers which contained key material. + veor q0, q0, q0 + veor q1, q1, q1 + veor q2, q2, q2 + + ldmia sp!, {r11, pc} @ return + + +@ void vpaes_decrypt_key_to_bsaes(AES_KEY *vpaes, const AES_KEY *bsaes); +.globl _vpaes_decrypt_key_to_bsaes +.private_extern _vpaes_decrypt_key_to_bsaes +#ifdef __thumb2__ +.thumb_func _vpaes_decrypt_key_to_bsaes +#endif +.align 4 +_vpaes_decrypt_key_to_bsaes: + stmdb sp!, {r11, lr} + + @ See _vpaes_schedule_core for the key schedule logic. Note vpaes + @ computes the decryption key schedule in reverse. Additionally, + @ aes-x86_64.pl shares some transformations, so we must only partially + @ invert vpaes's transformations. In general, vpaes computes in a + @ different basis (.Lk_ipt and .Lk_opt) and applies the inverses of + @ MixColumns, ShiftRows, and the affine part of the AES S-box (which is + @ split into a linear skew and XOR of 0x63). We undo all but MixColumns. + @ + @ Note also that bsaes-armv7.pl expects aes-armv4.pl's key + @ representation, which does not match the other aes_nohw_* + @ implementations. The ARM aes_nohw_* stores each 32-bit word + @ byteswapped, as a convenience for (unsupported) big-endian ARM, at the + @ cost of extra REV and VREV32 operations in little-endian ARM. + + adr r2, Lk_decrypt_transform + adr r3, Lk_sr+0x30 + adr r11, Lk_opt_then_skew @ Input to _vpaes_schedule_transform. + vld1.64 {q12}, [r2] @ Reuse q12 from encryption. + vmov.i8 q9, #0x0f @ Required by _vpaes_schedule_transform + + @ vpaes stores one fewer round count than bsaes, but the number of keys + @ is the same. + ldr r2, [r1,#240] + add r2, r2, #1 + str r2, [r0,#240] + + @ Undo the basis change and reapply the S-box affine transform. See + @ .Lschedule_mangle_last. + vld1.64 {q0}, [r1]! + bl _vpaes_schedule_transform + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + + @ See _vpaes_schedule_mangle for the transform on the middle keys. Note + @ it simultaneously inverts MixColumns and the S-box affine transform. + @ See .Lk_dksd through .Lk_dks9. +Loop_dec_key_to_bsaes: + vld1.64 {q0}, [r1]! + + @ Invert the ShiftRows step (see .Lschedule_mangle_both). Note going + @ forwards cancels inverting for which direction we cycle r3. We use r3 + @ rather than r8 to avoid a callee-saved register. + vld1.64 {q1}, [r3] + vtbl.8 d4, {q0}, d2 + vtbl.8 d5, {q0}, d3 + add r3, r3, #64-16 + and r3, r3, #~(1<<6) + vmov q0, q2 + + @ Handle the last key differently. + subs r2, r2, #1 + beq Loop_dec_key_to_bsaes_last + + @ Undo the basis change and reapply the S-box affine transform. + bl _vpaes_schedule_transform + + @ Rotate each word by 8 bytes (cycle the rows) and then byte-swap. We + @ combine the two operations in .Lk_decrypt_transform. + @ + @ TODO(davidben): Where does the rotation come from? + vtbl.8 d2, {q0}, d24 + vtbl.8 d3, {q0}, d25 + + vst1.64 {q1}, [r0]! + b Loop_dec_key_to_bsaes + +Loop_dec_key_to_bsaes_last: + @ The final key only inverts ShiftRows (already done in the loop). See + @ .Lschedule_am_decrypting. Its basis is not transformed. + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + + @ Wipe registers which contained key material. + veor q0, q0, q0 + veor q1, q1, q1 + veor q2, q2, q2 + + ldmia sp!, {r11, pc} @ return + +.globl _vpaes_ctr32_encrypt_blocks +.private_extern _vpaes_ctr32_encrypt_blocks +#ifdef __thumb2__ +.thumb_func _vpaes_ctr32_encrypt_blocks +#endif +.align 4 +_vpaes_ctr32_encrypt_blocks: + mov ip, sp + stmdb sp!, {r7,r8,r9,r10,r11, lr} + @ This function uses q4-q7 (d8-d15), which are callee-saved. + vstmdb sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + + cmp r2, #0 + @ r8 is passed on the stack. + ldr r8, [ip] + beq Lctr32_done + + @ _vpaes_encrypt_core expects the key in r2, so swap r2 and r3. + mov r9, r3 + mov r3, r2 + mov r2, r9 + + @ Load the IV and counter portion. + ldr r7, [r8, #12] + vld1.8 {q7}, [r8] + + bl _vpaes_preheat + rev r7, r7 @ The counter is big-endian. + +Lctr32_loop: + vmov q0, q7 + vld1.8 {q6}, [r0]! @ Load input ahead of time + bl _vpaes_encrypt_core + veor q0, q0, q6 @ XOR input and result + vst1.8 {q0}, [r1]! + subs r3, r3, #1 + @ Update the counter. + add r7, r7, #1 + rev r9, r7 + vmov.32 d15[1], r9 + bne Lctr32_loop + +Lctr32_done: + vldmia sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return + +#endif // !OPENSSL_NO_ASM +#endif // defined(__arm__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.linux.arm.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.linux.arm.S new file mode 100644 index 00000000..7599d7ef --- /dev/null +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv7.linux.arm.S @@ -0,0 +1,1242 @@ +#define BORINGSSL_PREFIX CNIOBoringSSL +#if defined(__arm__) && defined(__linux__) +// This file is generated from a similarly-named Perl script in the BoringSSL +// source tree. Do not edit by hand. + +#if !defined(__has_feature) +#define __has_feature(x) 0 +#endif +#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM) +#define OPENSSL_NO_ASM +#endif + +#if !defined(OPENSSL_NO_ASM) +#if defined(__arm__) +#if defined(BORINGSSL_PREFIX) +#include +#endif +.syntax unified + +.arch armv7-a +.fpu neon + +#if defined(__thumb2__) +.thumb +#else +.code 32 +#endif + +.text + +.type _vpaes_consts,%object +.align 7 @ totally strategic alignment +_vpaes_consts: +.Lk_mc_forward:@ mc_forward +.quad 0x0407060500030201, 0x0C0F0E0D080B0A09 +.quad 0x080B0A0904070605, 0x000302010C0F0E0D +.quad 0x0C0F0E0D080B0A09, 0x0407060500030201 +.quad 0x000302010C0F0E0D, 0x080B0A0904070605 +.Lk_mc_backward:@ mc_backward +.quad 0x0605040702010003, 0x0E0D0C0F0A09080B +.quad 0x020100030E0D0C0F, 0x0A09080B06050407 +.quad 0x0E0D0C0F0A09080B, 0x0605040702010003 +.quad 0x0A09080B06050407, 0x020100030E0D0C0F +.Lk_sr:@ sr +.quad 0x0706050403020100, 0x0F0E0D0C0B0A0908 +.quad 0x030E09040F0A0500, 0x0B06010C07020D08 +.quad 0x0F060D040B020900, 0x070E050C030A0108 +.quad 0x0B0E0104070A0D00, 0x0306090C0F020508 + +@ +@ "Hot" constants +@ +.Lk_inv:@ inv, inva +.quad 0x0E05060F0D080180, 0x040703090A0B0C02 +.quad 0x01040A060F0B0780, 0x030D0E0C02050809 +.Lk_ipt:@ input transform (lo, hi) +.quad 0xC2B2E8985A2A7000, 0xCABAE09052227808 +.quad 0x4C01307D317C4D00, 0xCD80B1FCB0FDCC81 +.Lk_sbo:@ sbou, sbot +.quad 0xD0D26D176FBDC700, 0x15AABF7AC502A878 +.quad 0xCFE474A55FBB6A00, 0x8E1E90D1412B35FA +.Lk_sb1:@ sb1u, sb1t +.quad 0x3618D415FAE22300, 0x3BF7CCC10D2ED9EF +.quad 0xB19BE18FCB503E00, 0xA5DF7A6E142AF544 +.Lk_sb2:@ sb2u, sb2t +.quad 0x69EB88400AE12900, 0xC2A163C8AB82234A +.quad 0xE27A93C60B712400, 0x5EB7E955BC982FCD + +.byte 86,101,99,116,111,114,32,80,101,114,109,117,116,97,116,105,111,110,32,65,69,83,32,102,111,114,32,65,82,77,118,55,32,78,69,79,78,44,32,77,105,107,101,32,72,97,109,98,117,114,103,32,40,83,116,97,110,102,111,114,100,32,85,110,105,118,101,114,115,105,116,121,41,0 +.align 2 +.size _vpaes_consts,.-_vpaes_consts +.align 6 +@@ +@@ _aes_preheat +@@ +@@ Fills q9-q15 as specified below. +@@ +.type _vpaes_preheat,%function +.align 4 +_vpaes_preheat: + adr r10, .Lk_inv + vmov.i8 q9, #0x0f @ .Lk_s0F + vld1.64 {q10,q11}, [r10]! @ .Lk_inv + add r10, r10, #64 @ Skip .Lk_ipt, .Lk_sbo + vld1.64 {q12,q13}, [r10]! @ .Lk_sb1 + vld1.64 {q14,q15}, [r10] @ .Lk_sb2 + bx lr + +@@ +@@ _aes_encrypt_core +@@ +@@ AES-encrypt q0. +@@ +@@ Inputs: +@@ q0 = input +@@ q9-q15 as in _vpaes_preheat +@@ [r2] = scheduled keys +@@ +@@ Output in q0 +@@ Clobbers q1-q5, r8-r11 +@@ Preserves q6-q8 so you get some local vectors +@@ +@@ +.type _vpaes_encrypt_core,%function +.align 4 +_vpaes_encrypt_core: + mov r9, r2 + ldr r8, [r2,#240] @ pull rounds + adr r11, .Lk_ipt + @ vmovdqa .Lk_ipt(%rip), %xmm2 # iptlo + @ vmovdqa .Lk_ipt+16(%rip), %xmm3 # ipthi + vld1.64 {q2, q3}, [r11] + adr r11, .Lk_mc_forward+16 + vld1.64 {q5}, [r9]! @ vmovdqu (%r9), %xmm5 # round0 key + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 + vtbl.8 d2, {q2}, d2 @ vpshufb %xmm1, %xmm2, %xmm1 + vtbl.8 d3, {q2}, d3 + vtbl.8 d4, {q3}, d0 @ vpshufb %xmm0, %xmm3, %xmm2 + vtbl.8 d5, {q3}, d1 + veor q0, q1, q5 @ vpxor %xmm5, %xmm1, %xmm0 + veor q0, q0, q2 @ vpxor %xmm2, %xmm0, %xmm0 + + @ .Lenc_entry ends with a bnz instruction which is normally paired with + @ subs in .Lenc_loop. + tst r8, r8 + b .Lenc_entry + +.align 4 +.Lenc_loop: + @ middle of middle round + add r10, r11, #0x40 + vtbl.8 d8, {q13}, d4 @ vpshufb %xmm2, %xmm13, %xmm4 # 4 = sb1u + vtbl.8 d9, {q13}, d5 + vld1.64 {q1}, [r11]! @ vmovdqa -0x40(%r11,%r10), %xmm1 # .Lk_mc_forward[] + vtbl.8 d0, {q12}, d6 @ vpshufb %xmm3, %xmm12, %xmm0 # 0 = sb1t + vtbl.8 d1, {q12}, d7 + veor q4, q4, q5 @ vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k + vtbl.8 d10, {q15}, d4 @ vpshufb %xmm2, %xmm15, %xmm5 # 4 = sb2u + vtbl.8 d11, {q15}, d5 + veor q0, q0, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 0 = A + vtbl.8 d4, {q14}, d6 @ vpshufb %xmm3, %xmm14, %xmm2 # 2 = sb2t + vtbl.8 d5, {q14}, d7 + vld1.64 {q4}, [r10] @ vmovdqa (%r11,%r10), %xmm4 # .Lk_mc_backward[] + vtbl.8 d6, {q0}, d2 @ vpshufb %xmm1, %xmm0, %xmm3 # 0 = B + vtbl.8 d7, {q0}, d3 + veor q2, q2, q5 @ vpxor %xmm5, %xmm2, %xmm2 # 2 = 2A + @ Write to q5 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d10, {q0}, d8 @ vpshufb %xmm4, %xmm0, %xmm0 # 3 = D + vtbl.8 d11, {q0}, d9 + veor q3, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 # 0 = 2A+B + vtbl.8 d8, {q3}, d2 @ vpshufb %xmm1, %xmm3, %xmm4 # 0 = 2B+C + vtbl.8 d9, {q3}, d3 + @ Here we restore the original q0/q5 usage. + veor q0, q5, q3 @ vpxor %xmm3, %xmm0, %xmm0 # 3 = 2A+B+D + and r11, r11, #~(1<<6) @ and $0x30, %r11 # ... mod 4 + veor q0, q0, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 0 = 2A+3B+C+D + subs r8, r8, #1 @ nr-- + +.Lenc_entry: + @ top of round + vand q1, q0, q9 @ vpand %xmm0, %xmm9, %xmm1 # 0 = k + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 # 1 = i + vtbl.8 d10, {q11}, d2 @ vpshufb %xmm1, %xmm11, %xmm5 # 2 = a/k + vtbl.8 d11, {q11}, d3 + veor q1, q1, q0 @ vpxor %xmm0, %xmm1, %xmm1 # 0 = j + vtbl.8 d6, {q10}, d0 @ vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i + vtbl.8 d7, {q10}, d1 + vtbl.8 d8, {q10}, d2 @ vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j + vtbl.8 d9, {q10}, d3 + veor q3, q3, q5 @ vpxor %xmm5, %xmm3, %xmm3 # 3 = iak = 1/i + a/k + veor q4, q4, q5 @ vpxor %xmm5, %xmm4, %xmm4 # 4 = jak = 1/j + a/k + vtbl.8 d4, {q10}, d6 @ vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak + vtbl.8 d5, {q10}, d7 + vtbl.8 d6, {q10}, d8 @ vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak + vtbl.8 d7, {q10}, d9 + veor q2, q2, q1 @ vpxor %xmm1, %xmm2, %xmm2 # 2 = io + veor q3, q3, q0 @ vpxor %xmm0, %xmm3, %xmm3 # 3 = jo + vld1.64 {q5}, [r9]! @ vmovdqu (%r9), %xmm5 + bne .Lenc_loop + + @ middle of last round + add r10, r11, #0x80 + + adr r11, .Lk_sbo + @ Read to q1 instead of q4, so the vtbl.8 instruction below does not + @ overlap table and destination registers. + vld1.64 {q1}, [r11]! @ vmovdqa -0x60(%r10), %xmm4 # 3 : sbou + vld1.64 {q0}, [r11] @ vmovdqa -0x50(%r10), %xmm0 # 0 : sbot .Lk_sbo+16 + vtbl.8 d8, {q1}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou + vtbl.8 d9, {q1}, d5 + vld1.64 {q1}, [r10] @ vmovdqa 0x40(%r11,%r10), %xmm1 # .Lk_sr[] + @ Write to q2 instead of q0 below, to avoid overlapping table and + @ destination registers. + vtbl.8 d4, {q0}, d6 @ vpshufb %xmm3, %xmm0, %xmm0 # 0 = sb1t + vtbl.8 d5, {q0}, d7 + veor q4, q4, q5 @ vpxor %xmm5, %xmm4, %xmm4 # 4 = sb1u + k + veor q2, q2, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 0 = A + @ Here we restore the original q0/q2 usage. + vtbl.8 d0, {q2}, d2 @ vpshufb %xmm1, %xmm0, %xmm0 + vtbl.8 d1, {q2}, d3 + bx lr +.size _vpaes_encrypt_core,.-_vpaes_encrypt_core + +.globl vpaes_encrypt +.hidden vpaes_encrypt +.type vpaes_encrypt,%function +.align 4 +vpaes_encrypt: + @ _vpaes_encrypt_core uses r8-r11. Round up to r7-r11 to maintain stack + @ alignment. + stmdb sp!, {r7,r8,r9,r10,r11,lr} + @ _vpaes_encrypt_core uses q4-q5 (d8-d11), which are callee-saved. + vstmdb sp!, {d8,d9,d10,d11} + + vld1.64 {q0}, [r0] + bl _vpaes_preheat + bl _vpaes_encrypt_core + vst1.64 {q0}, [r1] + + vldmia sp!, {d8,d9,d10,d11} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return +.size vpaes_encrypt,.-vpaes_encrypt + +@ +@ Decryption stuff +@ +.type _vpaes_decrypt_consts,%object +.align 4 +_vpaes_decrypt_consts: +.Lk_dipt:@ decryption input transform +.quad 0x0F505B040B545F00, 0x154A411E114E451A +.quad 0x86E383E660056500, 0x12771772F491F194 +.Lk_dsbo:@ decryption sbox final output +.quad 0x1387EA537EF94000, 0xC7AA6DB9D4943E2D +.quad 0x12D7560F93441D00, 0xCA4B8159D8C58E9C +.Lk_dsb9:@ decryption sbox output *9*u, *9*t +.quad 0x851C03539A86D600, 0xCAD51F504F994CC9 +.quad 0xC03B1789ECD74900, 0x725E2C9EB2FBA565 +.Lk_dsbd:@ decryption sbox output *D*u, *D*t +.quad 0x7D57CCDFE6B1A200, 0xF56E9B13882A4439 +.quad 0x3CE2FAF724C6CB00, 0x2931180D15DEEFD3 +.Lk_dsbb:@ decryption sbox output *B*u, *B*t +.quad 0xD022649296B44200, 0x602646F6B0F2D404 +.quad 0xC19498A6CD596700, 0xF3FF0C3E3255AA6B +.Lk_dsbe:@ decryption sbox output *E*u, *E*t +.quad 0x46F2929626D4D000, 0x2242600464B4F6B0 +.quad 0x0C55A6CDFFAAC100, 0x9467F36B98593E32 +.size _vpaes_decrypt_consts,.-_vpaes_decrypt_consts + +@@ +@@ Decryption core +@@ +@@ Same API as encryption core, except it clobbers q12-q15 rather than using +@@ the values from _vpaes_preheat. q9-q11 must still be set from +@@ _vpaes_preheat. +@@ +.type _vpaes_decrypt_core,%function +.align 4 +_vpaes_decrypt_core: + mov r9, r2 + ldr r8, [r2,#240] @ pull rounds + + @ This function performs shuffles with various constants. The x86_64 + @ version loads them on-demand into %xmm0-%xmm5. This does not work well + @ for ARMv7 because those registers are shuffle destinations. The ARMv8 + @ version preloads those constants into registers, but ARMv7 has half + @ the registers to work with. Instead, we load them on-demand into + @ q12-q15, registers normally use for preloaded constants. This is fine + @ because decryption doesn't use those constants. The values are + @ constant, so this does not interfere with potential 2x optimizations. + adr r7, .Lk_dipt + + vld1.64 {q12,q13}, [r7] @ vmovdqa .Lk_dipt(%rip), %xmm2 # iptlo + lsl r11, r8, #4 @ mov %rax, %r11; shl $4, %r11 + eor r11, r11, #0x30 @ xor $0x30, %r11 + adr r10, .Lk_sr + and r11, r11, #0x30 @ and $0x30, %r11 + add r11, r11, r10 + adr r10, .Lk_mc_forward+48 + + vld1.64 {q4}, [r9]! @ vmovdqu (%r9), %xmm4 # round0 key + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 + vtbl.8 d4, {q12}, d2 @ vpshufb %xmm1, %xmm2, %xmm2 + vtbl.8 d5, {q12}, d3 + vld1.64 {q5}, [r10] @ vmovdqa .Lk_mc_forward+48(%rip), %xmm5 + @ vmovdqa .Lk_dipt+16(%rip), %xmm1 # ipthi + vtbl.8 d0, {q13}, d0 @ vpshufb %xmm0, %xmm1, %xmm0 + vtbl.8 d1, {q13}, d1 + veor q2, q2, q4 @ vpxor %xmm4, %xmm2, %xmm2 + veor q0, q0, q2 @ vpxor %xmm2, %xmm0, %xmm0 + + @ .Ldec_entry ends with a bnz instruction which is normally paired with + @ subs in .Ldec_loop. + tst r8, r8 + b .Ldec_entry + +.align 4 +.Ldec_loop: +@ +@ Inverse mix columns +@ + + @ We load .Lk_dsb* into q12-q15 on-demand. See the comment at the top of + @ the function. + adr r10, .Lk_dsb9 + vld1.64 {q12,q13}, [r10]! @ vmovdqa -0x20(%r10),%xmm4 # 4 : sb9u + @ vmovdqa -0x10(%r10),%xmm1 # 0 : sb9t + @ Load sbd* ahead of time. + vld1.64 {q14,q15}, [r10]! @ vmovdqa 0x00(%r10),%xmm4 # 4 : sbdu + @ vmovdqa 0x10(%r10),%xmm1 # 0 : sbdt + vtbl.8 d8, {q12}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sb9u + vtbl.8 d9, {q12}, d5 + vtbl.8 d2, {q13}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb9t + vtbl.8 d3, {q13}, d7 + veor q0, q4, q0 @ vpxor %xmm4, %xmm0, %xmm0 + + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + + @ Load sbb* ahead of time. + vld1.64 {q12,q13}, [r10]! @ vmovdqa 0x20(%r10),%xmm4 # 4 : sbbu + @ vmovdqa 0x30(%r10),%xmm1 # 0 : sbbt + + vtbl.8 d8, {q14}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbdu + vtbl.8 d9, {q14}, d5 + @ Write to q1 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d2, {q0}, d10 @ vpshufb %xmm5, %xmm0, %xmm0 # MC ch + vtbl.8 d3, {q0}, d11 + @ Here we restore the original q0/q1 usage. This instruction is + @ reordered from the ARMv8 version so we do not clobber the vtbl.8 + @ below. + veor q0, q1, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 4 = ch + vtbl.8 d2, {q15}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbdt + vtbl.8 d3, {q15}, d7 + @ vmovdqa 0x20(%r10), %xmm4 # 4 : sbbu + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + @ vmovdqa 0x30(%r10), %xmm1 # 0 : sbbt + + @ Load sbd* ahead of time. + vld1.64 {q14,q15}, [r10]! @ vmovdqa 0x40(%r10),%xmm4 # 4 : sbeu + @ vmovdqa 0x50(%r10),%xmm1 # 0 : sbet + + vtbl.8 d8, {q12}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbbu + vtbl.8 d9, {q12}, d5 + @ Write to q1 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d2, {q0}, d10 @ vpshufb %xmm5, %xmm0, %xmm0 # MC ch + vtbl.8 d3, {q0}, d11 + @ Here we restore the original q0/q1 usage. This instruction is + @ reordered from the ARMv8 version so we do not clobber the vtbl.8 + @ below. + veor q0, q1, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 4 = ch + vtbl.8 d2, {q13}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbbt + vtbl.8 d3, {q13}, d7 + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + + vtbl.8 d8, {q14}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbeu + vtbl.8 d9, {q14}, d5 + @ Write to q1 instead of q0, so the table and destination registers do + @ not overlap. + vtbl.8 d2, {q0}, d10 @ vpshufb %xmm5, %xmm0, %xmm0 # MC ch + vtbl.8 d3, {q0}, d11 + @ Here we restore the original q0/q1 usage. This instruction is + @ reordered from the ARMv8 version so we do not clobber the vtbl.8 + @ below. + veor q0, q1, q4 @ vpxor %xmm4, %xmm0, %xmm0 # 4 = ch + vtbl.8 d2, {q15}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sbet + vtbl.8 d3, {q15}, d7 + vext.8 q5, q5, q5, #12 @ vpalignr $12, %xmm5, %xmm5, %xmm5 + veor q0, q0, q1 @ vpxor %xmm1, %xmm0, %xmm0 # 0 = ch + subs r8, r8, #1 @ sub $1,%rax # nr-- + +.Ldec_entry: + @ top of round + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 # 0 = k + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 # 1 = i + vtbl.8 d4, {q11}, d2 @ vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k + vtbl.8 d5, {q11}, d3 + veor q1, q1, q0 @ vpxor %xmm0, %xmm1, %xmm1 # 0 = j + vtbl.8 d6, {q10}, d0 @ vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i + vtbl.8 d7, {q10}, d1 + vtbl.8 d8, {q10}, d2 @ vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j + vtbl.8 d9, {q10}, d3 + veor q3, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k + veor q4, q4, q2 @ vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k + vtbl.8 d4, {q10}, d6 @ vpshufb %xmm3, %xmm10, %xmm2 # 2 = 1/iak + vtbl.8 d5, {q10}, d7 + vtbl.8 d6, {q10}, d8 @ vpshufb %xmm4, %xmm10, %xmm3 # 3 = 1/jak + vtbl.8 d7, {q10}, d9 + veor q2, q2, q1 @ vpxor %xmm1, %xmm2, %xmm2 # 2 = io + veor q3, q3, q0 @ vpxor %xmm0, %xmm3, %xmm3 # 3 = jo + vld1.64 {q0}, [r9]! @ vmovdqu (%r9), %xmm0 + bne .Ldec_loop + + @ middle of last round + + adr r10, .Lk_dsbo + + @ Write to q1 rather than q4 to avoid overlapping table and destination. + vld1.64 {q1}, [r10]! @ vmovdqa 0x60(%r10), %xmm4 # 3 : sbou + vtbl.8 d8, {q1}, d4 @ vpshufb %xmm2, %xmm4, %xmm4 # 4 = sbou + vtbl.8 d9, {q1}, d5 + @ Write to q2 rather than q1 to avoid overlapping table and destination. + vld1.64 {q2}, [r10] @ vmovdqa 0x70(%r10), %xmm1 # 0 : sbot + vtbl.8 d2, {q2}, d6 @ vpshufb %xmm3, %xmm1, %xmm1 # 0 = sb1t + vtbl.8 d3, {q2}, d7 + vld1.64 {q2}, [r11] @ vmovdqa -0x160(%r11), %xmm2 # .Lk_sr-.Lk_dsbd=-0x160 + veor q4, q4, q0 @ vpxor %xmm0, %xmm4, %xmm4 # 4 = sb1u + k + @ Write to q1 rather than q0 so the table and destination registers + @ below do not overlap. + veor q1, q1, q4 @ vpxor %xmm4, %xmm1, %xmm0 # 0 = A + vtbl.8 d0, {q1}, d4 @ vpshufb %xmm2, %xmm0, %xmm0 + vtbl.8 d1, {q1}, d5 + bx lr +.size _vpaes_decrypt_core,.-_vpaes_decrypt_core + +.globl vpaes_decrypt +.hidden vpaes_decrypt +.type vpaes_decrypt,%function +.align 4 +vpaes_decrypt: + @ _vpaes_decrypt_core uses r7-r11. + stmdb sp!, {r7,r8,r9,r10,r11,lr} + @ _vpaes_decrypt_core uses q4-q5 (d8-d11), which are callee-saved. + vstmdb sp!, {d8,d9,d10,d11} + + vld1.64 {q0}, [r0] + bl _vpaes_preheat + bl _vpaes_decrypt_core + vst1.64 {q0}, [r1] + + vldmia sp!, {d8,d9,d10,d11} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return +.size vpaes_decrypt,.-vpaes_decrypt +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@ @@ +@@ AES key schedule @@ +@@ @@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + +@ This function diverges from both x86_64 and armv7 in which constants are +@ pinned. x86_64 has a common preheat function for all operations. aarch64 +@ separates them because it has enough registers to pin nearly all constants. +@ armv7 does not have enough registers, but needing explicit loads and stores +@ also complicates using x86_64's register allocation directly. +@ +@ We pin some constants for convenience and leave q14 and q15 free to load +@ others on demand. + +@ +@ Key schedule constants +@ +.type _vpaes_key_consts,%object +.align 4 +_vpaes_key_consts: +.Lk_dksd:@ decryption key schedule: invskew x*D +.quad 0xFEB91A5DA3E44700, 0x0740E3A45A1DBEF9 +.quad 0x41C277F4B5368300, 0x5FDC69EAAB289D1E +.Lk_dksb:@ decryption key schedule: invskew x*B +.quad 0x9A4FCA1F8550D500, 0x03D653861CC94C99 +.quad 0x115BEDA7B6FC4A00, 0xD993256F7E3482C8 +.Lk_dkse:@ decryption key schedule: invskew x*E + 0x63 +.quad 0xD5031CCA1FC9D600, 0x53859A4C994F5086 +.quad 0xA23196054FDC7BE8, 0xCD5EF96A20B31487 +.Lk_dks9:@ decryption key schedule: invskew x*9 +.quad 0xB6116FC87ED9A700, 0x4AED933482255BFC +.quad 0x4576516227143300, 0x8BB89FACE9DAFDCE + +.Lk_rcon:@ rcon +.quad 0x1F8391B9AF9DEEB6, 0x702A98084D7C7D81 + +.Lk_opt:@ output transform +.quad 0xFF9F4929D6B66000, 0xF7974121DEBE6808 +.quad 0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0 +.Lk_deskew:@ deskew tables: inverts the sbox's "skew" +.quad 0x07E4A34047A4E300, 0x1DFEB95A5DBEF91A +.quad 0x5F36B5DC83EA6900, 0x2841C2ABF49D1E77 +.size _vpaes_key_consts,.-_vpaes_key_consts + +.type _vpaes_key_preheat,%function +.align 4 +_vpaes_key_preheat: + adr r11, .Lk_rcon + vmov.i8 q12, #0x5b @ .Lk_s63 + adr r10, .Lk_inv @ Must be aligned to 8 mod 16. + vmov.i8 q9, #0x0f @ .Lk_s0F + vld1.64 {q10,q11}, [r10] @ .Lk_inv + vld1.64 {q8}, [r11] @ .Lk_rcon + bx lr +.size _vpaes_key_preheat,.-_vpaes_key_preheat + +.type _vpaes_schedule_core,%function +.align 4 +_vpaes_schedule_core: + @ We only need to save lr, but ARM requires an 8-byte stack alignment, + @ so save an extra register. + stmdb sp!, {r3,lr} + + bl _vpaes_key_preheat @ load the tables + + adr r11, .Lk_ipt @ Must be aligned to 8 mod 16. + vld1.64 {q0}, [r0]! @ vmovdqu (%rdi), %xmm0 # load key (unaligned) + + @ input transform + @ Use q4 here rather than q3 so .Lschedule_am_decrypting does not + @ overlap table and destination. + vmov q4, q0 @ vmovdqa %xmm0, %xmm3 + bl _vpaes_schedule_transform + adr r10, .Lk_sr @ Must be aligned to 8 mod 16. + vmov q7, q0 @ vmovdqa %xmm0, %xmm7 + + add r8, r8, r10 + tst r3, r3 + bne .Lschedule_am_decrypting + + @ encrypting, output zeroth round key after transform + vst1.64 {q0}, [r2] @ vmovdqu %xmm0, (%rdx) + b .Lschedule_go + +.Lschedule_am_decrypting: + @ decrypting, output zeroth round key after shiftrows + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10), %xmm1 + vtbl.8 d6, {q4}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q4}, d3 + vst1.64 {q3}, [r2] @ vmovdqu %xmm3, (%rdx) + eor r8, r8, #0x30 @ xor $0x30, %r8 + +.Lschedule_go: + cmp r1, #192 @ cmp $192, %esi + bhi .Lschedule_256 + beq .Lschedule_192 + @ 128: fall though + +@@ +@@ .schedule_128 +@@ +@@ 128-bit specific part of key schedule. +@@ +@@ This schedule is really simple, because all its parts +@@ are accomplished by the subroutines. +@@ +.Lschedule_128: + mov r0, #10 @ mov $10, %esi + +.Loop_schedule_128: + bl _vpaes_schedule_round + subs r0, r0, #1 @ dec %esi + beq .Lschedule_mangle_last + bl _vpaes_schedule_mangle @ write output + b .Loop_schedule_128 + +@@ +@@ .aes_schedule_192 +@@ +@@ 192-bit specific part of key schedule. +@@ +@@ The main body of this schedule is the same as the 128-bit +@@ schedule, but with more smearing. The long, high side is +@@ stored in q7 as before, and the short, low side is in +@@ the high bits of q6. +@@ +@@ This schedule is somewhat nastier, however, because each +@@ round produces 192 bits of key material, or 1.5 round keys. +@@ Therefore, on each cycle we do 2 rounds and produce 3 round +@@ keys. +@@ +.align 4 +.Lschedule_192: + sub r0, r0, #8 + vld1.64 {q0}, [r0] @ vmovdqu 8(%rdi),%xmm0 # load key part 2 (very unaligned) + bl _vpaes_schedule_transform @ input transform + vmov q6, q0 @ vmovdqa %xmm0, %xmm6 # save short part + vmov.i8 d12, #0 @ vpxor %xmm4, %xmm4, %xmm4 # clear 4 + @ vmovhlps %xmm4, %xmm6, %xmm6 # clobber low side with zeros + mov r0, #4 @ mov $4, %esi + +.Loop_schedule_192: + bl _vpaes_schedule_round + vext.8 q0, q6, q0, #8 @ vpalignr $8,%xmm6,%xmm0,%xmm0 + bl _vpaes_schedule_mangle @ save key n + bl _vpaes_schedule_192_smear + bl _vpaes_schedule_mangle @ save key n+1 + bl _vpaes_schedule_round + subs r0, r0, #1 @ dec %esi + beq .Lschedule_mangle_last + bl _vpaes_schedule_mangle @ save key n+2 + bl _vpaes_schedule_192_smear + b .Loop_schedule_192 + +@@ +@@ .aes_schedule_256 +@@ +@@ 256-bit specific part of key schedule. +@@ +@@ The structure here is very similar to the 128-bit +@@ schedule, but with an additional "low side" in +@@ q6. The low side's rounds are the same as the +@@ high side's, except no rcon and no rotation. +@@ +.align 4 +.Lschedule_256: + vld1.64 {q0}, [r0] @ vmovdqu 16(%rdi),%xmm0 # load key part 2 (unaligned) + bl _vpaes_schedule_transform @ input transform + mov r0, #7 @ mov $7, %esi + +.Loop_schedule_256: + bl _vpaes_schedule_mangle @ output low result + vmov q6, q0 @ vmovdqa %xmm0, %xmm6 # save cur_lo in xmm6 + + @ high round + bl _vpaes_schedule_round + subs r0, r0, #1 @ dec %esi + beq .Lschedule_mangle_last + bl _vpaes_schedule_mangle + + @ low round. swap xmm7 and xmm6 + vdup.32 q0, d1[1] @ vpshufd $0xFF, %xmm0, %xmm0 + vmov.i8 q4, #0 + vmov q5, q7 @ vmovdqa %xmm7, %xmm5 + vmov q7, q6 @ vmovdqa %xmm6, %xmm7 + bl _vpaes_schedule_low_round + vmov q7, q5 @ vmovdqa %xmm5, %xmm7 + + b .Loop_schedule_256 + +@@ +@@ .aes_schedule_mangle_last +@@ +@@ Mangler for last round of key schedule +@@ Mangles q0 +@@ when encrypting, outputs out(q0) ^ 63 +@@ when decrypting, outputs unskew(q0) +@@ +@@ Always called right before return... jumps to cleanup and exits +@@ +.align 4 +.Lschedule_mangle_last: + @ schedule last round key from xmm0 + adr r11, .Lk_deskew @ lea .Lk_deskew(%rip),%r11 # prepare to deskew + tst r3, r3 + bne .Lschedule_mangle_last_dec + + @ encrypting + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10),%xmm1 + adr r11, .Lk_opt @ lea .Lk_opt(%rip), %r11 # prepare to output transform + add r2, r2, #32 @ add $32, %rdx + vmov q2, q0 + vtbl.8 d0, {q2}, d2 @ vpshufb %xmm1, %xmm0, %xmm0 # output permute + vtbl.8 d1, {q2}, d3 + +.Lschedule_mangle_last_dec: + sub r2, r2, #16 @ add $-16, %rdx + veor q0, q0, q12 @ vpxor .Lk_s63(%rip), %xmm0, %xmm0 + bl _vpaes_schedule_transform @ output transform + vst1.64 {q0}, [r2] @ vmovdqu %xmm0, (%rdx) # save last key + + @ cleanup + veor q0, q0, q0 @ vpxor %xmm0, %xmm0, %xmm0 + veor q1, q1, q1 @ vpxor %xmm1, %xmm1, %xmm1 + veor q2, q2, q2 @ vpxor %xmm2, %xmm2, %xmm2 + veor q3, q3, q3 @ vpxor %xmm3, %xmm3, %xmm3 + veor q4, q4, q4 @ vpxor %xmm4, %xmm4, %xmm4 + veor q5, q5, q5 @ vpxor %xmm5, %xmm5, %xmm5 + veor q6, q6, q6 @ vpxor %xmm6, %xmm6, %xmm6 + veor q7, q7, q7 @ vpxor %xmm7, %xmm7, %xmm7 + ldmia sp!, {r3,pc} @ return +.size _vpaes_schedule_core,.-_vpaes_schedule_core + +@@ +@@ .aes_schedule_192_smear +@@ +@@ Smear the short, low side in the 192-bit key schedule. +@@ +@@ Inputs: +@@ q7: high side, b a x y +@@ q6: low side, d c 0 0 +@@ +@@ Outputs: +@@ q6: b+c+d b+c 0 0 +@@ q0: b+c+d b+c b a +@@ +.type _vpaes_schedule_192_smear,%function +.align 4 +_vpaes_schedule_192_smear: + vmov.i8 q1, #0 + vdup.32 q0, d15[1] + vshl.i64 q1, q6, #32 @ vpshufd $0x80, %xmm6, %xmm1 # d c 0 0 -> c 0 0 0 + vmov d0, d15 @ vpshufd $0xFE, %xmm7, %xmm0 # b a _ _ -> b b b a + veor q6, q6, q1 @ vpxor %xmm1, %xmm6, %xmm6 # -> c+d c 0 0 + veor q1, q1, q1 @ vpxor %xmm1, %xmm1, %xmm1 + veor q6, q6, q0 @ vpxor %xmm0, %xmm6, %xmm6 # -> b+c+d b+c b a + vmov q0, q6 @ vmovdqa %xmm6, %xmm0 + vmov d12, d2 @ vmovhlps %xmm1, %xmm6, %xmm6 # clobber low side with zeros + bx lr +.size _vpaes_schedule_192_smear,.-_vpaes_schedule_192_smear + +@@ +@@ .aes_schedule_round +@@ +@@ Runs one main round of the key schedule on q0, q7 +@@ +@@ Specifically, runs subbytes on the high dword of q0 +@@ then rotates it by one byte and xors into the low dword of +@@ q7. +@@ +@@ Adds rcon from low byte of q8, then rotates q8 for +@@ next rcon. +@@ +@@ Smears the dwords of q7 by xoring the low into the +@@ second low, result into third, result into highest. +@@ +@@ Returns results in q7 = q0. +@@ Clobbers q1-q4, r11. +@@ +.type _vpaes_schedule_round,%function +.align 4 +_vpaes_schedule_round: + @ extract rcon from xmm8 + vmov.i8 q4, #0 @ vpxor %xmm4, %xmm4, %xmm4 + vext.8 q1, q8, q4, #15 @ vpalignr $15, %xmm8, %xmm4, %xmm1 + vext.8 q8, q8, q8, #15 @ vpalignr $15, %xmm8, %xmm8, %xmm8 + veor q7, q7, q1 @ vpxor %xmm1, %xmm7, %xmm7 + + @ rotate + vdup.32 q0, d1[1] @ vpshufd $0xFF, %xmm0, %xmm0 + vext.8 q0, q0, q0, #1 @ vpalignr $1, %xmm0, %xmm0, %xmm0 + + @ fall through... + + @ low round: same as high round, but no rotation and no rcon. +_vpaes_schedule_low_round: + @ The x86_64 version pins .Lk_sb1 in %xmm13 and .Lk_sb1+16 in %xmm12. + @ We pin other values in _vpaes_key_preheat, so load them now. + adr r11, .Lk_sb1 + vld1.64 {q14,q15}, [r11] + + @ smear xmm7 + vext.8 q1, q4, q7, #12 @ vpslldq $4, %xmm7, %xmm1 + veor q7, q7, q1 @ vpxor %xmm1, %xmm7, %xmm7 + vext.8 q4, q4, q7, #8 @ vpslldq $8, %xmm7, %xmm4 + + @ subbytes + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 # 0 = k + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 # 1 = i + veor q7, q7, q4 @ vpxor %xmm4, %xmm7, %xmm7 + vtbl.8 d4, {q11}, d2 @ vpshufb %xmm1, %xmm11, %xmm2 # 2 = a/k + vtbl.8 d5, {q11}, d3 + veor q1, q1, q0 @ vpxor %xmm0, %xmm1, %xmm1 # 0 = j + vtbl.8 d6, {q10}, d0 @ vpshufb %xmm0, %xmm10, %xmm3 # 3 = 1/i + vtbl.8 d7, {q10}, d1 + veor q3, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 # 3 = iak = 1/i + a/k + vtbl.8 d8, {q10}, d2 @ vpshufb %xmm1, %xmm10, %xmm4 # 4 = 1/j + vtbl.8 d9, {q10}, d3 + veor q7, q7, q12 @ vpxor .Lk_s63(%rip), %xmm7, %xmm7 + vtbl.8 d6, {q10}, d6 @ vpshufb %xmm3, %xmm10, %xmm3 # 2 = 1/iak + vtbl.8 d7, {q10}, d7 + veor q4, q4, q2 @ vpxor %xmm2, %xmm4, %xmm4 # 4 = jak = 1/j + a/k + vtbl.8 d4, {q10}, d8 @ vpshufb %xmm4, %xmm10, %xmm2 # 3 = 1/jak + vtbl.8 d5, {q10}, d9 + veor q3, q3, q1 @ vpxor %xmm1, %xmm3, %xmm3 # 2 = io + veor q2, q2, q0 @ vpxor %xmm0, %xmm2, %xmm2 # 3 = jo + vtbl.8 d8, {q15}, d6 @ vpshufb %xmm3, %xmm13, %xmm4 # 4 = sbou + vtbl.8 d9, {q15}, d7 + vtbl.8 d2, {q14}, d4 @ vpshufb %xmm2, %xmm12, %xmm1 # 0 = sb1t + vtbl.8 d3, {q14}, d5 + veor q1, q1, q4 @ vpxor %xmm4, %xmm1, %xmm1 # 0 = sbox output + + @ add in smeared stuff + veor q0, q1, q7 @ vpxor %xmm7, %xmm1, %xmm0 + veor q7, q1, q7 @ vmovdqa %xmm0, %xmm7 + bx lr +.size _vpaes_schedule_round,.-_vpaes_schedule_round + +@@ +@@ .aes_schedule_transform +@@ +@@ Linear-transform q0 according to tables at [r11] +@@ +@@ Requires that q9 = 0x0F0F... as in preheat +@@ Output in q0 +@@ Clobbers q1, q2, q14, q15 +@@ +.type _vpaes_schedule_transform,%function +.align 4 +_vpaes_schedule_transform: + vld1.64 {q14,q15}, [r11] @ vmovdqa (%r11), %xmm2 # lo + @ vmovdqa 16(%r11), %xmm1 # hi + vand q1, q0, q9 @ vpand %xmm9, %xmm0, %xmm1 + vshr.u8 q0, q0, #4 @ vpsrlb $4, %xmm0, %xmm0 + vtbl.8 d4, {q14}, d2 @ vpshufb %xmm1, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d3 + vtbl.8 d0, {q15}, d0 @ vpshufb %xmm0, %xmm1, %xmm0 + vtbl.8 d1, {q15}, d1 + veor q0, q0, q2 @ vpxor %xmm2, %xmm0, %xmm0 + bx lr +.size _vpaes_schedule_transform,.-_vpaes_schedule_transform + +@@ +@@ .aes_schedule_mangle +@@ +@@ Mangles q0 from (basis-transformed) standard version +@@ to our version. +@@ +@@ On encrypt, +@@ xor with 0x63 +@@ multiply by circulant 0,1,1,1 +@@ apply shiftrows transform +@@ +@@ On decrypt, +@@ xor with 0x63 +@@ multiply by "inverse mixcolumns" circulant E,B,D,9 +@@ deskew +@@ apply shiftrows transform +@@ +@@ +@@ Writes out to [r2], and increments or decrements it +@@ Keeps track of round number mod 4 in r8 +@@ Preserves q0 +@@ Clobbers q1-q5 +@@ +.type _vpaes_schedule_mangle,%function +.align 4 +_vpaes_schedule_mangle: + tst r3, r3 + vmov q4, q0 @ vmovdqa %xmm0, %xmm4 # save xmm0 for later + adr r11, .Lk_mc_forward @ Must be aligned to 8 mod 16. + vld1.64 {q5}, [r11] @ vmovdqa .Lk_mc_forward(%rip),%xmm5 + bne .Lschedule_mangle_dec + + @ encrypting + @ Write to q2 so we do not overlap table and destination below. + veor q2, q0, q12 @ vpxor .Lk_s63(%rip), %xmm0, %xmm4 + add r2, r2, #16 @ add $16, %rdx + vtbl.8 d8, {q2}, d10 @ vpshufb %xmm5, %xmm4, %xmm4 + vtbl.8 d9, {q2}, d11 + vtbl.8 d2, {q4}, d10 @ vpshufb %xmm5, %xmm4, %xmm1 + vtbl.8 d3, {q4}, d11 + vtbl.8 d6, {q1}, d10 @ vpshufb %xmm5, %xmm1, %xmm3 + vtbl.8 d7, {q1}, d11 + veor q4, q4, q1 @ vpxor %xmm1, %xmm4, %xmm4 + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10), %xmm1 + veor q3, q3, q4 @ vpxor %xmm4, %xmm3, %xmm3 + + b .Lschedule_mangle_both +.align 4 +.Lschedule_mangle_dec: + @ inverse mix columns + adr r11, .Lk_dksd @ lea .Lk_dksd(%rip),%r11 + vshr.u8 q1, q4, #4 @ vpsrlb $4, %xmm4, %xmm1 # 1 = hi + vand q4, q4, q9 @ vpand %xmm9, %xmm4, %xmm4 # 4 = lo + + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x00(%r11), %xmm2 + @ vmovdqa 0x10(%r11), %xmm3 + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + vtbl.8 d6, {q15}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q15}, d3 + @ Load .Lk_dksb ahead of time. + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x20(%r11), %xmm2 + @ vmovdqa 0x30(%r11), %xmm3 + @ Write to q13 so we do not overlap table and destination. + veor q13, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 + vtbl.8 d6, {q13}, d10 @ vpshufb %xmm5, %xmm3, %xmm3 + vtbl.8 d7, {q13}, d11 + + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + veor q2, q2, q3 @ vpxor %xmm3, %xmm2, %xmm2 + vtbl.8 d6, {q15}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q15}, d3 + @ Load .Lk_dkse ahead of time. + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x40(%r11), %xmm2 + @ vmovdqa 0x50(%r11), %xmm3 + @ Write to q13 so we do not overlap table and destination. + veor q13, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 + vtbl.8 d6, {q13}, d10 @ vpshufb %xmm5, %xmm3, %xmm3 + vtbl.8 d7, {q13}, d11 + + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + veor q2, q2, q3 @ vpxor %xmm3, %xmm2, %xmm2 + vtbl.8 d6, {q15}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d7, {q15}, d3 + @ Load .Lk_dkse ahead of time. + vld1.64 {q14,q15}, [r11]! @ vmovdqa 0x60(%r11), %xmm2 + @ vmovdqa 0x70(%r11), %xmm4 + @ Write to q13 so we do not overlap table and destination. + veor q13, q3, q2 @ vpxor %xmm2, %xmm3, %xmm3 + + vtbl.8 d4, {q14}, d8 @ vpshufb %xmm4, %xmm2, %xmm2 + vtbl.8 d5, {q14}, d9 + vtbl.8 d6, {q13}, d10 @ vpshufb %xmm5, %xmm3, %xmm3 + vtbl.8 d7, {q13}, d11 + vtbl.8 d8, {q15}, d2 @ vpshufb %xmm1, %xmm4, %xmm4 + vtbl.8 d9, {q15}, d3 + vld1.64 {q1}, [r8] @ vmovdqa (%r8,%r10), %xmm1 + veor q2, q2, q3 @ vpxor %xmm3, %xmm2, %xmm2 + veor q3, q4, q2 @ vpxor %xmm2, %xmm4, %xmm3 + + sub r2, r2, #16 @ add $-16, %rdx + +.Lschedule_mangle_both: + @ Write to q2 so table and destination do not overlap. + vtbl.8 d4, {q3}, d2 @ vpshufb %xmm1, %xmm3, %xmm3 + vtbl.8 d5, {q3}, d3 + add r8, r8, #64-16 @ add $-16, %r8 + and r8, r8, #~(1<<6) @ and $0x30, %r8 + vst1.64 {q2}, [r2] @ vmovdqu %xmm3, (%rdx) + bx lr +.size _vpaes_schedule_mangle,.-_vpaes_schedule_mangle + +.globl vpaes_set_encrypt_key +.hidden vpaes_set_encrypt_key +.type vpaes_set_encrypt_key,%function +.align 4 +vpaes_set_encrypt_key: + stmdb sp!, {r7,r8,r9,r10,r11, lr} + vstmdb sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + + lsr r9, r1, #5 @ shr $5,%eax + add r9, r9, #5 @ $5,%eax + str r9, [r2,#240] @ mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5; + + mov r3, #0 @ mov $0,%ecx + mov r8, #0x30 @ mov $0x30,%r8d + bl _vpaes_schedule_core + eor r0, r0, r0 + + vldmia sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return +.size vpaes_set_encrypt_key,.-vpaes_set_encrypt_key + +.globl vpaes_set_decrypt_key +.hidden vpaes_set_decrypt_key +.type vpaes_set_decrypt_key,%function +.align 4 +vpaes_set_decrypt_key: + stmdb sp!, {r7,r8,r9,r10,r11, lr} + vstmdb sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + + lsr r9, r1, #5 @ shr $5,%eax + add r9, r9, #5 @ $5,%eax + str r9, [r2,#240] @ mov %eax,240(%rdx) # AES_KEY->rounds = nbits/32+5; + lsl r9, r9, #4 @ shl $4,%eax + add r2, r2, #16 @ lea 16(%rdx,%rax),%rdx + add r2, r2, r9 + + mov r3, #1 @ mov $1,%ecx + lsr r8, r1, #1 @ shr $1,%r8d + and r8, r8, #32 @ and $32,%r8d + eor r8, r8, #32 @ xor $32,%r8d # nbits==192?0:32 + bl _vpaes_schedule_core + + vldmia sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return +.size vpaes_set_decrypt_key,.-vpaes_set_decrypt_key + +@ Additional constants for converting to bsaes. +.type _vpaes_convert_consts,%object +.align 4 +_vpaes_convert_consts: +@ .Lk_opt_then_skew applies skew(opt(x)) XOR 0x63, where skew is the linear +@ transform in the AES S-box. 0x63 is incorporated into the low half of the +@ table. This was computed with the following script: +@ +@ def u64s_to_u128(x, y): +@ return x | (y << 64) +@ def u128_to_u64s(w): +@ return w & ((1<<64)-1), w >> 64 +@ def get_byte(w, i): +@ return (w >> (i*8)) & 0xff +@ def apply_table(table, b): +@ lo = b & 0xf +@ hi = b >> 4 +@ return get_byte(table[0], lo) ^ get_byte(table[1], hi) +@ def opt(b): +@ table = [ +@ u64s_to_u128(0xFF9F4929D6B66000, 0xF7974121DEBE6808), +@ u64s_to_u128(0x01EDBD5150BCEC00, 0xE10D5DB1B05C0CE0), +@ ] +@ return apply_table(table, b) +@ def rot_byte(b, n): +@ return 0xff & ((b << n) | (b >> (8-n))) +@ def skew(x): +@ return (x ^ rot_byte(x, 1) ^ rot_byte(x, 2) ^ rot_byte(x, 3) ^ +@ rot_byte(x, 4)) +@ table = [0, 0] +@ for i in range(16): +@ table[0] |= (skew(opt(i)) ^ 0x63) << (i*8) +@ table[1] |= skew(opt(i<<4)) << (i*8) +@ print(" .quad 0x%016x, 0x%016x" % u128_to_u64s(table[0])) +@ print(" .quad 0x%016x, 0x%016x" % u128_to_u64s(table[1])) +.Lk_opt_then_skew: +.quad 0x9cb8436798bc4763, 0x6440bb9f6044bf9b +.quad 0x1f30062936192f00, 0xb49bad829db284ab + +@ .Lk_decrypt_transform is a permutation which performs an 8-bit left-rotation +@ followed by a byte-swap on each 32-bit word of a vector. E.g., 0x11223344 +@ becomes 0x22334411 and then 0x11443322. +.Lk_decrypt_transform: +.quad 0x0704050603000102, 0x0f0c0d0e0b08090a +.size _vpaes_convert_consts,.-_vpaes_convert_consts + +@ void vpaes_encrypt_key_to_bsaes(AES_KEY *bsaes, const AES_KEY *vpaes); +.globl vpaes_encrypt_key_to_bsaes +.hidden vpaes_encrypt_key_to_bsaes +.type vpaes_encrypt_key_to_bsaes,%function +.align 4 +vpaes_encrypt_key_to_bsaes: + stmdb sp!, {r11, lr} + + @ See _vpaes_schedule_core for the key schedule logic. In particular, + @ _vpaes_schedule_transform(.Lk_ipt) (section 2.2 of the paper), + @ _vpaes_schedule_mangle (section 4.3), and .Lschedule_mangle_last + @ contain the transformations not in the bsaes representation. This + @ function inverts those transforms. + @ + @ Note also that bsaes-armv7.pl expects aes-armv4.pl's key + @ representation, which does not match the other aes_nohw_* + @ implementations. The ARM aes_nohw_* stores each 32-bit word + @ byteswapped, as a convenience for (unsupported) big-endian ARM, at the + @ cost of extra REV and VREV32 operations in little-endian ARM. + + vmov.i8 q9, #0x0f @ Required by _vpaes_schedule_transform + adr r2, .Lk_mc_forward @ Must be aligned to 8 mod 16. + add r3, r2, 0x90 @ .Lk_sr+0x10-.Lk_mc_forward = 0x90 (Apple's toolchain doesn't support the expression) + + vld1.64 {q12}, [r2] + vmov.i8 q10, #0x5b @ .Lk_s63 from vpaes-x86_64 + adr r11, .Lk_opt @ Must be aligned to 8 mod 16. + vmov.i8 q11, #0x63 @ .LK_s63 without .Lk_ipt applied + + @ vpaes stores one fewer round count than bsaes, but the number of keys + @ is the same. + ldr r2, [r1,#240] + add r2, r2, #1 + str r2, [r0,#240] + + @ The first key is transformed with _vpaes_schedule_transform(.Lk_ipt). + @ Invert this with .Lk_opt. + vld1.64 {q0}, [r1]! + bl _vpaes_schedule_transform + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + + @ The middle keys have _vpaes_schedule_transform(.Lk_ipt) applied, + @ followed by _vpaes_schedule_mangle. _vpaes_schedule_mangle XORs 0x63, + @ multiplies by the circulant 0,1,1,1, then applies ShiftRows. +.Loop_enc_key_to_bsaes: + vld1.64 {q0}, [r1]! + + @ Invert the ShiftRows step (see .Lschedule_mangle_both). Note we cycle + @ r3 in the opposite direction and start at .Lk_sr+0x10 instead of 0x30. + @ We use r3 rather than r8 to avoid a callee-saved register. + vld1.64 {q1}, [r3] + vtbl.8 d4, {q0}, d2 + vtbl.8 d5, {q0}, d3 + add r3, r3, #16 + and r3, r3, #~(1<<6) + vmov q0, q2 + + @ Handle the last key differently. + subs r2, r2, #1 + beq .Loop_enc_key_to_bsaes_last + + @ Multiply by the circulant. This is its own inverse. + vtbl.8 d2, {q0}, d24 + vtbl.8 d3, {q0}, d25 + vmov q0, q1 + vtbl.8 d4, {q1}, d24 + vtbl.8 d5, {q1}, d25 + veor q0, q0, q2 + vtbl.8 d2, {q2}, d24 + vtbl.8 d3, {q2}, d25 + veor q0, q0, q1 + + @ XOR and finish. + veor q0, q0, q10 + bl _vpaes_schedule_transform + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + b .Loop_enc_key_to_bsaes + +.Loop_enc_key_to_bsaes_last: + @ The final key does not have a basis transform (note + @ .Lschedule_mangle_last inverts the original transform). It only XORs + @ 0x63 and applies ShiftRows. The latter was already inverted in the + @ loop. Note that, because we act on the original representation, we use + @ q11, not q10. + veor q0, q0, q11 + vrev32.8 q0, q0 + vst1.64 {q0}, [r0] + + @ Wipe registers which contained key material. + veor q0, q0, q0 + veor q1, q1, q1 + veor q2, q2, q2 + + ldmia sp!, {r11, pc} @ return +.size vpaes_encrypt_key_to_bsaes,.-vpaes_encrypt_key_to_bsaes + +@ void vpaes_decrypt_key_to_bsaes(AES_KEY *vpaes, const AES_KEY *bsaes); +.globl vpaes_decrypt_key_to_bsaes +.hidden vpaes_decrypt_key_to_bsaes +.type vpaes_decrypt_key_to_bsaes,%function +.align 4 +vpaes_decrypt_key_to_bsaes: + stmdb sp!, {r11, lr} + + @ See _vpaes_schedule_core for the key schedule logic. Note vpaes + @ computes the decryption key schedule in reverse. Additionally, + @ aes-x86_64.pl shares some transformations, so we must only partially + @ invert vpaes's transformations. In general, vpaes computes in a + @ different basis (.Lk_ipt and .Lk_opt) and applies the inverses of + @ MixColumns, ShiftRows, and the affine part of the AES S-box (which is + @ split into a linear skew and XOR of 0x63). We undo all but MixColumns. + @ + @ Note also that bsaes-armv7.pl expects aes-armv4.pl's key + @ representation, which does not match the other aes_nohw_* + @ implementations. The ARM aes_nohw_* stores each 32-bit word + @ byteswapped, as a convenience for (unsupported) big-endian ARM, at the + @ cost of extra REV and VREV32 operations in little-endian ARM. + + adr r2, .Lk_decrypt_transform + adr r3, .Lk_sr+0x30 + adr r11, .Lk_opt_then_skew @ Input to _vpaes_schedule_transform. + vld1.64 {q12}, [r2] @ Reuse q12 from encryption. + vmov.i8 q9, #0x0f @ Required by _vpaes_schedule_transform + + @ vpaes stores one fewer round count than bsaes, but the number of keys + @ is the same. + ldr r2, [r1,#240] + add r2, r2, #1 + str r2, [r0,#240] + + @ Undo the basis change and reapply the S-box affine transform. See + @ .Lschedule_mangle_last. + vld1.64 {q0}, [r1]! + bl _vpaes_schedule_transform + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + + @ See _vpaes_schedule_mangle for the transform on the middle keys. Note + @ it simultaneously inverts MixColumns and the S-box affine transform. + @ See .Lk_dksd through .Lk_dks9. +.Loop_dec_key_to_bsaes: + vld1.64 {q0}, [r1]! + + @ Invert the ShiftRows step (see .Lschedule_mangle_both). Note going + @ forwards cancels inverting for which direction we cycle r3. We use r3 + @ rather than r8 to avoid a callee-saved register. + vld1.64 {q1}, [r3] + vtbl.8 d4, {q0}, d2 + vtbl.8 d5, {q0}, d3 + add r3, r3, #64-16 + and r3, r3, #~(1<<6) + vmov q0, q2 + + @ Handle the last key differently. + subs r2, r2, #1 + beq .Loop_dec_key_to_bsaes_last + + @ Undo the basis change and reapply the S-box affine transform. + bl _vpaes_schedule_transform + + @ Rotate each word by 8 bytes (cycle the rows) and then byte-swap. We + @ combine the two operations in .Lk_decrypt_transform. + @ + @ TODO(davidben): Where does the rotation come from? + vtbl.8 d2, {q0}, d24 + vtbl.8 d3, {q0}, d25 + + vst1.64 {q1}, [r0]! + b .Loop_dec_key_to_bsaes + +.Loop_dec_key_to_bsaes_last: + @ The final key only inverts ShiftRows (already done in the loop). See + @ .Lschedule_am_decrypting. Its basis is not transformed. + vrev32.8 q0, q0 + vst1.64 {q0}, [r0]! + + @ Wipe registers which contained key material. + veor q0, q0, q0 + veor q1, q1, q1 + veor q2, q2, q2 + + ldmia sp!, {r11, pc} @ return +.size vpaes_decrypt_key_to_bsaes,.-vpaes_decrypt_key_to_bsaes +.globl vpaes_ctr32_encrypt_blocks +.hidden vpaes_ctr32_encrypt_blocks +.type vpaes_ctr32_encrypt_blocks,%function +.align 4 +vpaes_ctr32_encrypt_blocks: + mov ip, sp + stmdb sp!, {r7,r8,r9,r10,r11, lr} + @ This function uses q4-q7 (d8-d15), which are callee-saved. + vstmdb sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + + cmp r2, #0 + @ r8 is passed on the stack. + ldr r8, [ip] + beq .Lctr32_done + + @ _vpaes_encrypt_core expects the key in r2, so swap r2 and r3. + mov r9, r3 + mov r3, r2 + mov r2, r9 + + @ Load the IV and counter portion. + ldr r7, [r8, #12] + vld1.8 {q7}, [r8] + + bl _vpaes_preheat + rev r7, r7 @ The counter is big-endian. + +.Lctr32_loop: + vmov q0, q7 + vld1.8 {q6}, [r0]! @ .Load input ahead of time + bl _vpaes_encrypt_core + veor q0, q0, q6 @ XOR input and result + vst1.8 {q0}, [r1]! + subs r3, r3, #1 + @ Update the counter. + add r7, r7, #1 + rev r9, r7 + vmov.32 d15[1], r9 + bne .Lctr32_loop + +.Lctr32_done: + vldmia sp!, {d8,d9,d10,d11,d12,d13,d14,d15} + ldmia sp!, {r7,r8,r9,r10,r11, pc} @ return +.size vpaes_ctr32_encrypt_blocks,.-vpaes_ctr32_encrypt_blocks +#endif +#endif // !OPENSSL_NO_ASM +#endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.ios.aarch64.S index d1aa56f1..e7672868 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.ios.aarch64.S @@ -1214,3 +1214,7 @@ Lctr32_done: #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.linux.aarch64.S index 4a4ec01c..869275fe 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-armv8.linux.aarch64.S @@ -1216,3 +1216,7 @@ vpaes_ctr32_encrypt_blocks: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86.linux.x86.S index 16b0a690..d58b51f4 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86.linux.x86.S @@ -8,7 +8,7 @@ #include #endif .text -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST #endif .align 64 .L_vpaes_consts: @@ -487,7 +487,7 @@ vpaes_set_encrypt_key: pushl %ebx pushl %esi pushl %edi -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST pushl %ebx pushl %edx call .L016pic @@ -572,7 +572,7 @@ vpaes_encrypt: pushl %ebx pushl %esi pushl %edi -#ifndef NDEBUG +#ifdef BORINGSSL_DISPATCH_TEST pushl %ebx pushl %edx call .L019pic @@ -708,3 +708,7 @@ vpaes_cbc_encrypt: .size vpaes_cbc_encrypt,.-.L_vpaes_cbc_encrypt_begin #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S index 01d9155c..c741009b 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S @@ -814,12 +814,10 @@ _vpaes_schedule_mangle: .align 16 vpaes_set_encrypt_key: .cfi_startproc -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST .extern BORINGSSL_function_hit .hidden BORINGSSL_function_hit movb $1,BORINGSSL_function_hit+5(%rip) -#endif #endif movl %esi,%eax @@ -865,12 +863,10 @@ vpaes_set_decrypt_key: .align 16 vpaes_encrypt: .cfi_startproc -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST .extern BORINGSSL_function_hit .hidden BORINGSSL_function_hit movb $1,BORINGSSL_function_hit+4(%rip) -#endif #endif movdqu (%rdi),%xmm0 call _vpaes_preheat @@ -1137,3 +1133,7 @@ _vpaes_consts: .size _vpaes_consts,.-_vpaes_consts #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S index f3b7d038..a69b5fb3 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S @@ -814,11 +814,9 @@ L$schedule_mangle_both: .p2align 4 _vpaes_set_encrypt_key: -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+5(%rip) -#endif #endif movl %esi,%eax @@ -864,11 +862,9 @@ _vpaes_set_decrypt_key: .p2align 4 _vpaes_encrypt: -#ifndef NDEBUG -#ifndef BORINGSSL_FIPS +#ifdef BORINGSSL_DISPATCH_TEST movb $1,_BORINGSSL_function_hit+4(%rip) -#endif #endif movdqu (%rdi),%xmm0 call _vpaes_preheat @@ -1135,3 +1131,7 @@ L$ctr_add_two: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86-mont.linux.x86.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86-mont.linux.x86.S index 8e5e751a..84b44423 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86-mont.linux.x86.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86-mont.linux.x86.S @@ -484,3 +484,7 @@ bn_mul_mont: .byte 111,114,103,62,0 #endif #endif // defined(__i386__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.linux.x86_64.S index e3394f1a..8876bb9d 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.linux.x86_64.S @@ -1260,3 +1260,7 @@ bn_mulx4x_mont: .align 16 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.mac.x86_64.S index 8892d2e1..330ef959 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont.mac.x86_64.S @@ -1257,3 +1257,7 @@ L$mulx4x_epilogue: .p2align 4 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.linux.x86_64.S index 4257d50e..d86374d4 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.linux.x86_64.S @@ -3790,3 +3790,7 @@ bn_gather5: .byte 77,111,110,116,103,111,109,101,114,121,32,77,117,108,116,105,112,108,105,99,97,116,105,111,110,32,119,105,116,104,32,115,99,97,116,116,101,114,47,103,97,116,104,101,114,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.mac.x86_64.S index 9451d869..14221ddd 100644 --- a/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/fipsmodule/x86_64-mont5.mac.x86_64.S @@ -3789,3 +3789,7 @@ L$inc: .byte 77,111,110,116,103,111,109,101,114,121,32,77,117,108,116,105,112,108,105,99,97,116,105,111,110,32,119,105,116,104,32,115,99,97,116,116,101,114,47,103,97,116,104,101,114,32,102,111,114,32,120,56,54,95,54,52,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0 #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/hrss/asm/poly_rq_mul.S b/Sources/CNIOBoringSSL/crypto/hrss/asm/poly_rq_mul.S index 32257d53..1d4a2dc9 100644 --- a/Sources/CNIOBoringSSL/crypto/hrss/asm/poly_rq_mul.S +++ b/Sources/CNIOBoringSSL/crypto/hrss/asm/poly_rq_mul.S @@ -8464,3 +8464,7 @@ ret #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/internal.h b/Sources/CNIOBoringSSL/crypto/internal.h index 82feb032..6c51cd4e 100644 --- a/Sources/CNIOBoringSSL/crypto/internal.h +++ b/Sources/CNIOBoringSSL/crypto/internal.h @@ -187,6 +187,15 @@ typedef __uint128_t uint128_t; #define OPENSSL_FALLTHROUGH [[gnu::fallthrough]] #elif defined(__GNUC__) && __GNUC__ >= 7 // gcc 7 #define OPENSSL_FALLTHROUGH __attribute__ ((fallthrough)) +#elif defined(__clang__) +#if __has_attribute(fallthrough) && __clang_major__ >= 5 +// Clang 3.5, at least, complains about "error: declaration does not declare +// anything", possibily because we put a semicolon after this macro in +// practice. Thus limit it to >= Clang 5, which does work. +#define OPENSSL_FALLTHROUGH __attribute__ ((fallthrough)) +#else // clang versions that do not support fallthrough. +#define OPENSSL_FALLTHROUGH +#endif #else // C++11 on gcc 6, and all other cases #define OPENSSL_FALLTHROUGH #endif @@ -789,6 +798,15 @@ static inline void *OPENSSL_memset(void *dst, int c, size_t n) { void BORINGSSL_FIPS_abort(void) __attribute__((noreturn)); #endif +// boringssl_fips_self_test runs the FIPS KAT-based self tests. It returns one +// on success and zero on error. The argument is the integrity hash of the FIPS +// module and may be used to check and write flag files to suppress duplicate +// self-tests. If |module_hash_len| is zero then no flag file will be checked +// nor written and tests will always be run. +int boringssl_fips_self_test(const uint8_t *module_hash, + size_t module_hash_len); + + #if defined(__cplusplus) } // extern C #endif diff --git a/Sources/CNIOBoringSSL/crypto/poly1305/poly1305_arm_asm.S b/Sources/CNIOBoringSSL/crypto/poly1305/poly1305_arm_asm.S index 14513c7b..9d503236 100644 --- a/Sources/CNIOBoringSSL/crypto/poly1305/poly1305_arm_asm.S +++ b/Sources/CNIOBoringSSL/crypto/poly1305/poly1305_arm_asm.S @@ -2026,3 +2026,7 @@ bx lr #endif /* __arm__ && !OPENSSL_NO_ASM && !__APPLE__ */ #endif // defined(__arm__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.ios.aarch64.S b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.ios.aarch64.S index bb3e20df..699fc608 100644 --- a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.ios.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.ios.aarch64.S @@ -997,3 +997,7 @@ _sike_mpdblsubx2_asm: ret #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.linux.aarch64.S b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.linux.aarch64.S index 939143a5..7b0268c4 100644 --- a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.linux.aarch64.S +++ b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-armv8.linux.aarch64.S @@ -999,3 +999,7 @@ sike_mpdblsubx2_asm: #endif #endif // !OPENSSL_NO_ASM #endif // defined(__aarch64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.linux.x86_64.S b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.linux.x86_64.S index 6255fa1c..06c8700b 100644 --- a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.linux.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.linux.x86_64.S @@ -1871,3 +1871,7 @@ sike_mpmul: .cfi_endproc #endif #endif // defined(__x86_64__) && defined(__linux__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.mac.x86_64.S b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.mac.x86_64.S index b668d7db..8e86aa53 100644 --- a/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.mac.x86_64.S +++ b/Sources/CNIOBoringSSL/crypto/third_party/sike/asm/fp-x86_64.mac.x86_64.S @@ -1870,3 +1870,7 @@ _sike_mpmul: #endif #endif // defined(__x86_64__) && defined(__APPLE__) +#if defined(__linux__) && defined(__ELF__) +.section .note.GNU-stack,"",%progbits +#endif + diff --git a/Sources/CNIOBoringSSL/crypto/x509v3/v3_alt.c b/Sources/CNIOBoringSSL/crypto/x509v3/v3_alt.c index 73ccf48d..41055fdb 100644 --- a/Sources/CNIOBoringSSL/crypto/x509v3/v3_alt.c +++ b/Sources/CNIOBoringSSL/crypto/x509v3/v3_alt.c @@ -210,15 +210,18 @@ int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) break; case GEN_EMAIL: - BIO_printf(out, "email:%s", gen->d.ia5->data); + BIO_printf(out, "email:"); + ASN1_STRING_print(out, gen->d.ia5); break; case GEN_DNS: - BIO_printf(out, "DNS:%s", gen->d.ia5->data); + BIO_printf(out, "DNS:"); + ASN1_STRING_print(out, gen->d.ia5); break; case GEN_URI: - BIO_printf(out, "URI:%s", gen->d.ia5->data); + BIO_printf(out, "URI:"); + ASN1_STRING_print(out, gen->d.ia5); break; case GEN_DIRNAME: diff --git a/Sources/CNIOBoringSSL/crypto/x509v3/v3_purp.c b/Sources/CNIOBoringSSL/crypto/x509v3/v3_purp.c index 0e21f4d4..775802c1 100644 --- a/Sources/CNIOBoringSSL/crypto/x509v3/v3_purp.c +++ b/Sources/CNIOBoringSSL/crypto/x509v3/v3_purp.c @@ -611,7 +611,7 @@ static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) { - if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC)) + if (xku_reject(x, XKU_SSL_SERVER)) return 0; if (ca) return check_ca(x); diff --git a/Sources/CNIOBoringSSL/hash.txt b/Sources/CNIOBoringSSL/hash.txt index 3f9ab567..a07e920f 100644 --- a/Sources/CNIOBoringSSL/hash.txt +++ b/Sources/CNIOBoringSSL/hash.txt @@ -1 +1 @@ -This directory is derived from BoringSSL cloned from https://boringssl.googlesource.com/boringssl at revision 9806ae005b3085683a2fcf2953e4170893e0973d +This directory is derived from BoringSSL cloned from https://boringssl.googlesource.com/boringssl at revision 6e7255c17e1a7348a2377fbc804441dd284806e2 diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_aead.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_aead.h index e706f6c7..49544338 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_aead.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_aead.h @@ -99,6 +99,19 @@ extern "C" { // parameters, only use 12-byte nonces. OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm(void); +// EVP_aead_aes_192_gcm is AES-192 in Galois Counter Mode. +// +// WARNING: AES-192 is superfluous and shouldn't exist. NIST should never have +// defined it. Use only when interop with another system requires it, never +// de novo. +// +// Note: AES-GCM should only be used with 12-byte (96-bit) nonces. Although it +// is specified to take a variable-length nonce, nonces with other lengths are +// effectively randomized, which means one must consider collisions. Unless +// implementing an existing protocol which has already specified incorrect +// parameters, only use 12-byte nonces. +OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_192_gcm(void); + // EVP_aead_aes_256_gcm is AES-256 in Galois Counter Mode. // // Note: AES-GCM should only be used with 12-byte (96-bit) nonces. Although it diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_base.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_base.h index 624c1c9d..71d6362d 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_base.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_base.h @@ -129,6 +129,11 @@ extern "C" { #if defined(__APPLE__) #define OPENSSL_APPLE +// Note |TARGET_OS_MAC| is set for all Apple OS variants. |TARGET_OS_OSX| +// targets macOS specifically. +#if defined(TARGET_OS_OSX) && TARGET_OS_OSX +#define OPENSSL_MACOS +#endif #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE #define OPENSSL_IOS #endif diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols.h index d4fe9da5..d9ecd766 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols.h @@ -433,7 +433,6 @@ #define BN_usub BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BN_usub) #define BN_value_one BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BN_value_one) #define BN_zero BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BN_zero) -#define BORINGSSL_function_hit BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BORINGSSL_function_hit) #define BORINGSSL_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BORINGSSL_self_test) #define BUF_MEM_append BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BUF_MEM_append) #define BUF_MEM_free BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, BUF_MEM_free) @@ -943,6 +942,7 @@ #define EVP_MD_CTX_type BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_CTX_type) #define EVP_MD_block_size BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_block_size) #define EVP_MD_flags BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_flags) +#define EVP_MD_meth_get_flags BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_meth_get_flags) #define EVP_MD_size BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_size) #define EVP_MD_type BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_MD_type) #define EVP_PBE_scrypt BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_PBE_scrypt) @@ -1045,6 +1045,7 @@ #define EVP_aead_aes_128_gcm_siv BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_128_gcm_siv) #define EVP_aead_aes_128_gcm_tls12 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_128_gcm_tls12) #define EVP_aead_aes_128_gcm_tls13 BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_128_gcm_tls13) +#define EVP_aead_aes_192_gcm BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_192_gcm) #define EVP_aead_aes_256_cbc_sha1_tls BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_256_cbc_sha1_tls) #define EVP_aead_aes_256_cbc_sha1_tls_implicit_iv BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_256_cbc_sha1_tls_implicit_iv) #define EVP_aead_aes_256_cbc_sha256_tls BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, EVP_aead_aes_256_cbc_sha256_tls) @@ -1527,6 +1528,7 @@ #define SSL_CIPHER_get_name BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_get_name) #define SSL_CIPHER_get_prf_nid BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_get_prf_nid) #define SSL_CIPHER_get_rfc_name BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_get_rfc_name) +#define SSL_CIPHER_get_value BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_get_value) #define SSL_CIPHER_get_version BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_get_version) #define SSL_CIPHER_is_aead BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_is_aead) #define SSL_CIPHER_is_block_cipher BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_CIPHER_is_block_cipher) @@ -1766,6 +1768,7 @@ #define SSL_enable_ocsp_stapling BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_enable_ocsp_stapling) #define SSL_enable_signed_cert_timestamps BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_enable_signed_cert_timestamps) #define SSL_enable_tls_channel_id BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_enable_tls_channel_id) +#define SSL_error_description BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_error_description) #define SSL_export_keying_material BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_export_keying_material) #define SSL_free BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_free) #define SSL_generate_key_block BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, SSL_generate_key_block) @@ -2530,6 +2533,7 @@ #define X509v3_get_ext_by_OBJ BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, X509v3_get_ext_by_OBJ) #define X509v3_get_ext_by_critical BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, X509v3_get_ext_by_critical) #define X509v3_get_ext_count BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, X509v3_get_ext_count) +#define __clang_at_available_requires_core_foundation_framework BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, __clang_at_available_requires_core_foundation_framework) #define __clang_call_terminate BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, __clang_call_terminate) #define a2i_GENERAL_NAME BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, a2i_GENERAL_NAME) #define a2i_IPADDRESS BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, a2i_IPADDRESS) @@ -2656,6 +2660,7 @@ #define bn_uadd_consttime BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, bn_uadd_consttime) #define bn_usub_consttime BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, bn_usub_consttime) #define bn_wexpand BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, bn_wexpand) +#define boringssl_fips_self_test BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, boringssl_fips_self_test) #define c2i_ASN1_BIT_STRING BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, c2i_ASN1_BIT_STRING) #define c2i_ASN1_INTEGER BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, c2i_ASN1_INTEGER) #define c2i_ASN1_OBJECT BORINGSSL_ADD_PREFIX(BORINGSSL_PREFIX, c2i_ASN1_OBJECT) diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols_asm.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols_asm.h index 7208912e..0d948324 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols_asm.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_boringssl_prefix_symbols_asm.h @@ -438,7 +438,6 @@ #define _BN_usub BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BN_usub) #define _BN_value_one BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BN_value_one) #define _BN_zero BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BN_zero) -#define _BORINGSSL_function_hit BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BORINGSSL_function_hit) #define _BORINGSSL_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BORINGSSL_self_test) #define _BUF_MEM_append BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BUF_MEM_append) #define _BUF_MEM_free BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, BUF_MEM_free) @@ -948,6 +947,7 @@ #define _EVP_MD_CTX_type BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_CTX_type) #define _EVP_MD_block_size BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_block_size) #define _EVP_MD_flags BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_flags) +#define _EVP_MD_meth_get_flags BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_meth_get_flags) #define _EVP_MD_size BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_size) #define _EVP_MD_type BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_MD_type) #define _EVP_PBE_scrypt BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_PBE_scrypt) @@ -1050,6 +1050,7 @@ #define _EVP_aead_aes_128_gcm_siv BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_128_gcm_siv) #define _EVP_aead_aes_128_gcm_tls12 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_128_gcm_tls12) #define _EVP_aead_aes_128_gcm_tls13 BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_128_gcm_tls13) +#define _EVP_aead_aes_192_gcm BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_192_gcm) #define _EVP_aead_aes_256_cbc_sha1_tls BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_256_cbc_sha1_tls) #define _EVP_aead_aes_256_cbc_sha1_tls_implicit_iv BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_256_cbc_sha1_tls_implicit_iv) #define _EVP_aead_aes_256_cbc_sha256_tls BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, EVP_aead_aes_256_cbc_sha256_tls) @@ -1532,6 +1533,7 @@ #define _SSL_CIPHER_get_name BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_get_name) #define _SSL_CIPHER_get_prf_nid BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_get_prf_nid) #define _SSL_CIPHER_get_rfc_name BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_get_rfc_name) +#define _SSL_CIPHER_get_value BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_get_value) #define _SSL_CIPHER_get_version BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_get_version) #define _SSL_CIPHER_is_aead BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_is_aead) #define _SSL_CIPHER_is_block_cipher BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_CIPHER_is_block_cipher) @@ -1771,6 +1773,7 @@ #define _SSL_enable_ocsp_stapling BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_enable_ocsp_stapling) #define _SSL_enable_signed_cert_timestamps BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_enable_signed_cert_timestamps) #define _SSL_enable_tls_channel_id BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_enable_tls_channel_id) +#define _SSL_error_description BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_error_description) #define _SSL_export_keying_material BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_export_keying_material) #define _SSL_free BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_free) #define _SSL_generate_key_block BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, SSL_generate_key_block) @@ -2535,6 +2538,7 @@ #define _X509v3_get_ext_by_OBJ BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, X509v3_get_ext_by_OBJ) #define _X509v3_get_ext_by_critical BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, X509v3_get_ext_by_critical) #define _X509v3_get_ext_count BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, X509v3_get_ext_count) +#define ___clang_at_available_requires_core_foundation_framework BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, __clang_at_available_requires_core_foundation_framework) #define ___clang_call_terminate BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, __clang_call_terminate) #define _a2i_GENERAL_NAME BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, a2i_GENERAL_NAME) #define _a2i_IPADDRESS BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, a2i_IPADDRESS) @@ -2661,6 +2665,7 @@ #define _bn_uadd_consttime BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, bn_uadd_consttime) #define _bn_usub_consttime BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, bn_usub_consttime) #define _bn_wexpand BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, bn_wexpand) +#define _boringssl_fips_self_test BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, boringssl_fips_self_test) #define _c2i_ASN1_BIT_STRING BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, c2i_ASN1_BIT_STRING) #define _c2i_ASN1_INTEGER BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, c2i_ASN1_INTEGER) #define _c2i_ASN1_OBJECT BORINGSSL_ADD_PREFIX_MAC_ASM(BORINGSSL_PREFIX, c2i_ASN1_OBJECT) diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cipher.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cipher.h index 4b0e50b2..53655aa4 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cipher.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cipher.h @@ -136,8 +136,8 @@ OPENSSL_EXPORT int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in); // EVP_CIPHER_CTX_reset calls |EVP_CIPHER_CTX_cleanup| followed by -// |EVP_CIPHER_CTX_init|. -OPENSSL_EXPORT void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx); +// |EVP_CIPHER_CTX_init| and returns one. +OPENSSL_EXPORT int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx); // Cipher context configuration. @@ -425,6 +425,9 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3_ecb(void); // EVP_aes_128_cfb128 is only available in decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb128(void); +// EVP_aes_256_cfb128 is only available in decrepit. +OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cfb128(void); + // EVP_bf_ecb is Blowfish in ECB mode and is only available in decrepit. OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_ecb(void); diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cpu.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cpu.h index 210e0b8f..5393f31d 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cpu.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_cpu.h @@ -190,20 +190,19 @@ extern unsigned long OPENSSL_ppc64le_hwcap2; #endif // OPENSSL_PPC64LE -#if !defined(NDEBUG) && !defined(BORINGSSL_FIPS) +#if defined(BORINGSSL_DISPATCH_TEST) // Runtime CPU dispatch testing support // BORINGSSL_function_hit is an array of flags. The following functions will -// set these flags in non-FIPS builds if NDEBUG is not defined. +// set these flags if BORINGSSL_DISPATCH_TEST is defined. // 0: aes_hw_ctr32_encrypt_blocks // 1: aes_hw_encrypt // 2: aesni_gcm_encrypt // 3: aes_hw_set_encrypt_key // 4: vpaes_encrypt // 5: vpaes_set_encrypt_key -// 6: bsaes_ctr32_encrypt_blocks extern uint8_t BORINGSSL_function_hit[7]; -#endif // !NDEBUG && !FIPS +#endif // BORINGSSL_DISPATCH_TEST #if defined(__cplusplus) diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_crypto.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_crypto.h index 9d54b92f..a17d2786 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_crypto.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_crypto.h @@ -16,6 +16,7 @@ #define OPENSSL_HEADER_CRYPTO_H #include "CNIOBoringSSL_base.h" +#include "CNIOBoringSSL_sha.h" // Upstream OpenSSL defines |OPENSSL_malloc|, etc., in crypto.h rather than // mem.h. @@ -58,8 +59,11 @@ OPENSSL_EXPORT int CRYPTO_has_asm(void); // which case it returns one. OPENSSL_EXPORT int FIPS_mode(void); -// BORINGSSL_self_test triggers the FIPS KAT-based self tests. It returns one -// on success and zero on error. +// BORINGSSL_self_test triggers the FIPS KAT-based self tests. It returns one on +// success and zero on error. The argument is the integrity hash of the FIPS +// module and may be used to check and write flag files to suppress duplicate +// self-tests. If it is all zeros, no flag file will be checked nor written and +// tests will always be run. OPENSSL_EXPORT int BORINGSSL_self_test(void); diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_digest.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_digest.h index 201c3416..5e96e861 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_digest.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_digest.h @@ -195,7 +195,7 @@ OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md); // EVP_MD_block_size returns the native block-size of |md|, in bytes. OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); -// EVP_MD_FLAG_PKEY_DIGEST indicates the the digest function is used with a +// EVP_MD_FLAG_PKEY_DIGEST indicates that the digest function is used with a // specific public key in order to verify signatures. (For example, // EVP_dss1.) #define EVP_MD_FLAG_PKEY_DIGEST 1 @@ -205,6 +205,11 @@ OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); // undefined rather than NULL. #define EVP_MD_FLAG_DIGALGID_ABSENT 2 +// EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function +// (XOF). This flag is defined for compatibility and will never be set in any +// |EVP_MD| in BoringSSL. +#define EVP_MD_FLAG_XOF 4 + // Digest operation accessors. @@ -274,6 +279,9 @@ OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx); OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, size_t len); +// EVP_MD_meth_get_flags calls |EVP_MD_flags|. +OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md); + struct evp_md_pctx_ops; diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ec_key.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ec_key.h index 80304ac3..1c454881 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ec_key.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ec_key.h @@ -174,8 +174,8 @@ OPENSSL_EXPORT int EC_KEY_check_fips(const EC_KEY *key); // EC_KEY_set_public_key_affine_coordinates sets the public key in |key| to // (|x|, |y|). It returns one on success and zero otherwise. OPENSSL_EXPORT int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, - BIGNUM *x, - BIGNUM *y); + const BIGNUM *x, + const BIGNUM *y); // EC_KEY_key2buf encodes the public key in |key| to an allocated octet string // and sets |*out_buf| to point to it. It returns the length of the encoded diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_evp.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_evp.h index c2843e73..b72591b4 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_evp.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_evp.h @@ -942,6 +942,15 @@ OPENSSL_EXPORT int EVP_PKEY_base_id(const EVP_PKEY *pkey); #endif +// Nodejs compatibility section (hidden). +// +// These defines exist for node.js, with the hope that we can eliminate the +// need for them over time. + +#define EVPerr(function, reason) \ + ERR_put_error(ERR_LIB_EVP, 0, reason, __FILE__, __LINE__) + + // Private structures. struct evp_pkey_st { @@ -1016,5 +1025,6 @@ BSSL_NAMESPACE_END #define EVP_R_MEMORY_LIMIT_EXCEEDED 132 #define EVP_R_INVALID_PARAMETERS 133 #define EVP_R_INVALID_PEER_KEY 134 +#define EVP_R_NOT_XOF_OR_INVALID_LENGTH 135 #endif // OPENSSL_HEADER_EVP_H diff --git a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ssl.h b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ssl.h index 7b9c01ca..6d5538f2 100644 --- a/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ssl.h +++ b/Sources/CNIOBoringSSL/include/CNIOBoringSSL_ssl.h @@ -560,6 +560,11 @@ OPENSSL_EXPORT int SSL_get_error(const SSL *ssl, int ret_code); #define SSL_ERROR_HANDOFF 17 #define SSL_ERROR_HANDBACK 18 +// SSL_error_description returns a string representation of |err|, where |err| +// is one of the |SSL_ERROR_*| constants returned by |SSL_get_error|, or NULL +// if the value is unrecognized. +OPENSSL_EXPORT const char *SSL_error_description(int err); + // SSL_set_mtu sets the |ssl|'s MTU in DTLS to |mtu|. It returns one on success // and zero on failure. OPENSSL_EXPORT int SSL_set_mtu(SSL *ssl, unsigned mtu); @@ -1242,10 +1247,14 @@ DEFINE_CONST_STACK_OF(SSL_CIPHER) // https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4. OPENSSL_EXPORT const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value); -// SSL_CIPHER_get_id returns |cipher|'s id. It may be cast to a |uint16_t| to -// get the cipher suite value. +// SSL_CIPHER_get_id returns |cipher|'s non-IANA id. This is not its +// IANA-assigned number, which is called the "value" here, although it may be +// cast to a |uint16_t| to get it. OPENSSL_EXPORT uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher); +// SSL_CIPHER_get_value returns |cipher|'s IANA-assigned number. +OPENSSL_EXPORT uint16_t SSL_CIPHER_get_value(const SSL_CIPHER *cipher); + // SSL_CIPHER_is_aead returns one if |cipher| uses an AEAD cipher. OPENSSL_EXPORT int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher); @@ -3135,6 +3144,13 @@ OPENSSL_EXPORT int SSL_delegated_credential_used(const SSL *ssl); // |SSL_process_quic_post_handshake| to process it. It is an error to call // |SSL_read| and |SSL_write| in QUIC. // +// 0-RTT behaves similarly to |TLS_method|'s usual behavior. |SSL_do_handshake| +// returns early as soon as the client (respectively, server) is allowed to send +// 0-RTT (respectively, half-RTT) data. The caller should then call +// |SSL_do_handshake| again to consume the remaining handshake messages and +// confirm the handshake. As a client, |SSL_ERROR_EARLY_DATA_REJECTED| and +// |SSL_reset_early_data_reject| behave as usual. +// // Note that secrets for an encryption level may be available to QUIC before the // level is active in TLS. Callers should use |SSL_quic_read_level| to determine // the active read level for |SSL_provide_quic_data|. |SSL_do_handshake| will @@ -3150,7 +3166,8 @@ OPENSSL_EXPORT int SSL_delegated_credential_used(const SSL *ssl); // |SSL_quic_max_handshake_flight_len| to get the maximum buffer length at each // encryption level. // -// Note: 0-RTT is not currently supported via this API. +// Note: 0-RTT support is incomplete and does not currently handle QUIC +// transport parameters and server SETTINGS frame. // ssl_encryption_level_t represents a specific QUIC encryption level used to // transmit handshake messages. @@ -3332,34 +3349,39 @@ OPENSSL_EXPORT void SSL_reset_early_data_reject(SSL *ssl); // connections which resumed a session. OPENSSL_EXPORT int32_t SSL_get_ticket_age_skew(const SSL *ssl); +// An ssl_early_data_reason_t describes why 0-RTT was accepted or rejected. +// These values are persisted to logs. Entries should not be renumbered and +// numeric values should never be reused. enum ssl_early_data_reason_t BORINGSSL_ENUM_INT { // The handshake has not progressed far enough for the 0-RTT status to be // known. - ssl_early_data_unknown, + ssl_early_data_unknown = 0, // 0-RTT is disabled for this connection. - ssl_early_data_disabled, + ssl_early_data_disabled = 1, // 0-RTT was accepted. - ssl_early_data_accepted, + ssl_early_data_accepted = 2, // The negotiated protocol version does not support 0-RTT. - ssl_early_data_protocol_version, + ssl_early_data_protocol_version = 3, // The peer declined to offer or accept 0-RTT for an unknown reason. - ssl_early_data_peer_declined, + ssl_early_data_peer_declined = 4, // The client did not offer a session. - ssl_early_data_no_session_offered, + ssl_early_data_no_session_offered = 5, // The server declined to resume the session. - ssl_early_data_session_not_resumed, + ssl_early_data_session_not_resumed = 6, // The session does not support 0-RTT. - ssl_early_data_unsupported_for_session, + ssl_early_data_unsupported_for_session = 7, // The server sent a HelloRetryRequest. - ssl_early_data_hello_retry_request, + ssl_early_data_hello_retry_request = 8, // The negotiated ALPN protocol did not match the session. - ssl_early_data_alpn_mismatch, + ssl_early_data_alpn_mismatch = 9, // The connection negotiated Channel ID, which is incompatible with 0-RTT. - ssl_early_data_channel_id, + ssl_early_data_channel_id = 10, // The connection negotiated token binding, which is incompatible with 0-RTT. - ssl_early_data_token_binding, + ssl_early_data_token_binding = 11, // The client and server ticket age were too far apart. - ssl_early_data_ticket_age_skew, + ssl_early_data_ticket_age_skew = 12, + // The value of the largest entry. + ssl_early_data_reason_max_value = ssl_early_data_ticket_age_skew, }; // SSL_get_early_data_reason returns details why 0-RTT was accepted or rejected @@ -4291,19 +4313,9 @@ OPENSSL_EXPORT const char *SSL_get_cipher_list(const SSL *ssl, int n); OPENSSL_EXPORT void SSL_CTX_set_client_cert_cb( SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey)); -#define SSL_NOTHING 1 -#define SSL_WRITING 2 -#define SSL_READING 3 -#define SSL_X509_LOOKUP 4 -#define SSL_CHANNEL_ID_LOOKUP 5 -#define SSL_PENDING_SESSION 7 -#define SSL_CERTIFICATE_SELECTION_PENDING 8 -#define SSL_PRIVATE_KEY_OPERATION 9 -#define SSL_PENDING_TICKET 10 -#define SSL_EARLY_DATA_REJECTED 11 -#define SSL_CERTIFICATE_VERIFY 12 -#define SSL_HANDOFF 13 -#define SSL_HANDBACK 14 +#define SSL_NOTHING SSL_ERROR_NONE +#define SSL_WRITING SSL_ERROR_WANT_WRITE +#define SSL_READING SSL_ERROR_WANT_READ // SSL_want returns one of the above values to determine what the most recent // operation on |ssl| was blocked on. Use |SSL_get_error| instead. diff --git a/Sources/CNIOBoringSSL/include/boringssl_prefix_symbols_nasm.inc b/Sources/CNIOBoringSSL/include/boringssl_prefix_symbols_nasm.inc index ca1f0dd3..1d5fd969 100644 --- a/Sources/CNIOBoringSSL/include/boringssl_prefix_symbols_nasm.inc +++ b/Sources/CNIOBoringSSL/include/boringssl_prefix_symbols_nasm.inc @@ -430,7 +430,6 @@ %xdefine _BN_usub _ %+ BORINGSSL_PREFIX %+ _BN_usub %xdefine _BN_value_one _ %+ BORINGSSL_PREFIX %+ _BN_value_one %xdefine _BN_zero _ %+ BORINGSSL_PREFIX %+ _BN_zero -%xdefine _BORINGSSL_function_hit _ %+ BORINGSSL_PREFIX %+ _BORINGSSL_function_hit %xdefine _BORINGSSL_self_test _ %+ BORINGSSL_PREFIX %+ _BORINGSSL_self_test %xdefine _BUF_MEM_append _ %+ BORINGSSL_PREFIX %+ _BUF_MEM_append %xdefine _BUF_MEM_free _ %+ BORINGSSL_PREFIX %+ _BUF_MEM_free @@ -940,6 +939,7 @@ %xdefine _EVP_MD_CTX_type _ %+ BORINGSSL_PREFIX %+ _EVP_MD_CTX_type %xdefine _EVP_MD_block_size _ %+ BORINGSSL_PREFIX %+ _EVP_MD_block_size %xdefine _EVP_MD_flags _ %+ BORINGSSL_PREFIX %+ _EVP_MD_flags +%xdefine _EVP_MD_meth_get_flags _ %+ BORINGSSL_PREFIX %+ _EVP_MD_meth_get_flags %xdefine _EVP_MD_size _ %+ BORINGSSL_PREFIX %+ _EVP_MD_size %xdefine _EVP_MD_type _ %+ BORINGSSL_PREFIX %+ _EVP_MD_type %xdefine _EVP_PBE_scrypt _ %+ BORINGSSL_PREFIX %+ _EVP_PBE_scrypt @@ -1042,6 +1042,7 @@ %xdefine _EVP_aead_aes_128_gcm_siv _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_128_gcm_siv %xdefine _EVP_aead_aes_128_gcm_tls12 _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_128_gcm_tls12 %xdefine _EVP_aead_aes_128_gcm_tls13 _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_128_gcm_tls13 +%xdefine _EVP_aead_aes_192_gcm _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_192_gcm %xdefine _EVP_aead_aes_256_cbc_sha1_tls _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_256_cbc_sha1_tls %xdefine _EVP_aead_aes_256_cbc_sha1_tls_implicit_iv _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_256_cbc_sha1_tls_implicit_iv %xdefine _EVP_aead_aes_256_cbc_sha256_tls _ %+ BORINGSSL_PREFIX %+ _EVP_aead_aes_256_cbc_sha256_tls @@ -1524,6 +1525,7 @@ %xdefine _SSL_CIPHER_get_name _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_get_name %xdefine _SSL_CIPHER_get_prf_nid _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_get_prf_nid %xdefine _SSL_CIPHER_get_rfc_name _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_get_rfc_name +%xdefine _SSL_CIPHER_get_value _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_get_value %xdefine _SSL_CIPHER_get_version _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_get_version %xdefine _SSL_CIPHER_is_aead _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_is_aead %xdefine _SSL_CIPHER_is_block_cipher _ %+ BORINGSSL_PREFIX %+ _SSL_CIPHER_is_block_cipher @@ -1763,6 +1765,7 @@ %xdefine _SSL_enable_ocsp_stapling _ %+ BORINGSSL_PREFIX %+ _SSL_enable_ocsp_stapling %xdefine _SSL_enable_signed_cert_timestamps _ %+ BORINGSSL_PREFIX %+ _SSL_enable_signed_cert_timestamps %xdefine _SSL_enable_tls_channel_id _ %+ BORINGSSL_PREFIX %+ _SSL_enable_tls_channel_id +%xdefine _SSL_error_description _ %+ BORINGSSL_PREFIX %+ _SSL_error_description %xdefine _SSL_export_keying_material _ %+ BORINGSSL_PREFIX %+ _SSL_export_keying_material %xdefine _SSL_free _ %+ BORINGSSL_PREFIX %+ _SSL_free %xdefine _SSL_generate_key_block _ %+ BORINGSSL_PREFIX %+ _SSL_generate_key_block @@ -2527,6 +2530,7 @@ %xdefine _X509v3_get_ext_by_OBJ _ %+ BORINGSSL_PREFIX %+ _X509v3_get_ext_by_OBJ %xdefine _X509v3_get_ext_by_critical _ %+ BORINGSSL_PREFIX %+ _X509v3_get_ext_by_critical %xdefine _X509v3_get_ext_count _ %+ BORINGSSL_PREFIX %+ _X509v3_get_ext_count +%xdefine ___clang_at_available_requires_core_foundation_framework _ %+ BORINGSSL_PREFIX %+ ___clang_at_available_requires_core_foundation_framework %xdefine ___clang_call_terminate _ %+ BORINGSSL_PREFIX %+ ___clang_call_terminate %xdefine _a2i_GENERAL_NAME _ %+ BORINGSSL_PREFIX %+ _a2i_GENERAL_NAME %xdefine _a2i_IPADDRESS _ %+ BORINGSSL_PREFIX %+ _a2i_IPADDRESS @@ -2653,6 +2657,7 @@ %xdefine _bn_uadd_consttime _ %+ BORINGSSL_PREFIX %+ _bn_uadd_consttime %xdefine _bn_usub_consttime _ %+ BORINGSSL_PREFIX %+ _bn_usub_consttime %xdefine _bn_wexpand _ %+ BORINGSSL_PREFIX %+ _bn_wexpand +%xdefine _boringssl_fips_self_test _ %+ BORINGSSL_PREFIX %+ _boringssl_fips_self_test %xdefine _c2i_ASN1_BIT_STRING _ %+ BORINGSSL_PREFIX %+ _c2i_ASN1_BIT_STRING %xdefine _c2i_ASN1_INTEGER _ %+ BORINGSSL_PREFIX %+ _c2i_ASN1_INTEGER %xdefine _c2i_ASN1_OBJECT _ %+ BORINGSSL_PREFIX %+ _c2i_ASN1_OBJECT @@ -3638,7 +3643,6 @@ %xdefine BN_usub BORINGSSL_PREFIX %+ _BN_usub %xdefine BN_value_one BORINGSSL_PREFIX %+ _BN_value_one %xdefine BN_zero BORINGSSL_PREFIX %+ _BN_zero -%xdefine BORINGSSL_function_hit BORINGSSL_PREFIX %+ _BORINGSSL_function_hit %xdefine BORINGSSL_self_test BORINGSSL_PREFIX %+ _BORINGSSL_self_test %xdefine BUF_MEM_append BORINGSSL_PREFIX %+ _BUF_MEM_append %xdefine BUF_MEM_free BORINGSSL_PREFIX %+ _BUF_MEM_free @@ -4148,6 +4152,7 @@ %xdefine EVP_MD_CTX_type BORINGSSL_PREFIX %+ _EVP_MD_CTX_type %xdefine EVP_MD_block_size BORINGSSL_PREFIX %+ _EVP_MD_block_size %xdefine EVP_MD_flags BORINGSSL_PREFIX %+ _EVP_MD_flags +%xdefine EVP_MD_meth_get_flags BORINGSSL_PREFIX %+ _EVP_MD_meth_get_flags %xdefine EVP_MD_size BORINGSSL_PREFIX %+ _EVP_MD_size %xdefine EVP_MD_type BORINGSSL_PREFIX %+ _EVP_MD_type %xdefine EVP_PBE_scrypt BORINGSSL_PREFIX %+ _EVP_PBE_scrypt @@ -4250,6 +4255,7 @@ %xdefine EVP_aead_aes_128_gcm_siv BORINGSSL_PREFIX %+ _EVP_aead_aes_128_gcm_siv %xdefine EVP_aead_aes_128_gcm_tls12 BORINGSSL_PREFIX %+ _EVP_aead_aes_128_gcm_tls12 %xdefine EVP_aead_aes_128_gcm_tls13 BORINGSSL_PREFIX %+ _EVP_aead_aes_128_gcm_tls13 +%xdefine EVP_aead_aes_192_gcm BORINGSSL_PREFIX %+ _EVP_aead_aes_192_gcm %xdefine EVP_aead_aes_256_cbc_sha1_tls BORINGSSL_PREFIX %+ _EVP_aead_aes_256_cbc_sha1_tls %xdefine EVP_aead_aes_256_cbc_sha1_tls_implicit_iv BORINGSSL_PREFIX %+ _EVP_aead_aes_256_cbc_sha1_tls_implicit_iv %xdefine EVP_aead_aes_256_cbc_sha256_tls BORINGSSL_PREFIX %+ _EVP_aead_aes_256_cbc_sha256_tls @@ -4732,6 +4738,7 @@ %xdefine SSL_CIPHER_get_name BORINGSSL_PREFIX %+ _SSL_CIPHER_get_name %xdefine SSL_CIPHER_get_prf_nid BORINGSSL_PREFIX %+ _SSL_CIPHER_get_prf_nid %xdefine SSL_CIPHER_get_rfc_name BORINGSSL_PREFIX %+ _SSL_CIPHER_get_rfc_name +%xdefine SSL_CIPHER_get_value BORINGSSL_PREFIX %+ _SSL_CIPHER_get_value %xdefine SSL_CIPHER_get_version BORINGSSL_PREFIX %+ _SSL_CIPHER_get_version %xdefine SSL_CIPHER_is_aead BORINGSSL_PREFIX %+ _SSL_CIPHER_is_aead %xdefine SSL_CIPHER_is_block_cipher BORINGSSL_PREFIX %+ _SSL_CIPHER_is_block_cipher @@ -4971,6 +4978,7 @@ %xdefine SSL_enable_ocsp_stapling BORINGSSL_PREFIX %+ _SSL_enable_ocsp_stapling %xdefine SSL_enable_signed_cert_timestamps BORINGSSL_PREFIX %+ _SSL_enable_signed_cert_timestamps %xdefine SSL_enable_tls_channel_id BORINGSSL_PREFIX %+ _SSL_enable_tls_channel_id +%xdefine SSL_error_description BORINGSSL_PREFIX %+ _SSL_error_description %xdefine SSL_export_keying_material BORINGSSL_PREFIX %+ _SSL_export_keying_material %xdefine SSL_free BORINGSSL_PREFIX %+ _SSL_free %xdefine SSL_generate_key_block BORINGSSL_PREFIX %+ _SSL_generate_key_block @@ -5735,6 +5743,7 @@ %xdefine X509v3_get_ext_by_OBJ BORINGSSL_PREFIX %+ _X509v3_get_ext_by_OBJ %xdefine X509v3_get_ext_by_critical BORINGSSL_PREFIX %+ _X509v3_get_ext_by_critical %xdefine X509v3_get_ext_count BORINGSSL_PREFIX %+ _X509v3_get_ext_count +%xdefine __clang_at_available_requires_core_foundation_framework BORINGSSL_PREFIX %+ ___clang_at_available_requires_core_foundation_framework %xdefine __clang_call_terminate BORINGSSL_PREFIX %+ ___clang_call_terminate %xdefine a2i_GENERAL_NAME BORINGSSL_PREFIX %+ _a2i_GENERAL_NAME %xdefine a2i_IPADDRESS BORINGSSL_PREFIX %+ _a2i_IPADDRESS @@ -5861,6 +5870,7 @@ %xdefine bn_uadd_consttime BORINGSSL_PREFIX %+ _bn_uadd_consttime %xdefine bn_usub_consttime BORINGSSL_PREFIX %+ _bn_usub_consttime %xdefine bn_wexpand BORINGSSL_PREFIX %+ _bn_wexpand +%xdefine boringssl_fips_self_test BORINGSSL_PREFIX %+ _boringssl_fips_self_test %xdefine c2i_ASN1_BIT_STRING BORINGSSL_PREFIX %+ _c2i_ASN1_BIT_STRING %xdefine c2i_ASN1_INTEGER BORINGSSL_PREFIX %+ _c2i_ASN1_INTEGER %xdefine c2i_ASN1_OBJECT BORINGSSL_PREFIX %+ _c2i_ASN1_OBJECT diff --git a/Sources/CNIOBoringSSL/ssl/d1_both.cc b/Sources/CNIOBoringSSL/ssl/d1_both.cc index 44f9f149..dfec55ed 100644 --- a/Sources/CNIOBoringSSL/ssl/d1_both.cc +++ b/Sources/CNIOBoringSSL/ssl/d1_both.cc @@ -799,14 +799,14 @@ static int send_flight(SSL *ssl) { // Retry this packet the next time around. ssl->d1->outgoing_written = old_written; ssl->d1->outgoing_offset = old_offset; - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; ret = bio_ret; goto err; } } if (BIO_flush(ssl->wbio.get()) <= 0) { - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; goto err; } diff --git a/Sources/CNIOBoringSSL/ssl/handoff.cc b/Sources/CNIOBoringSSL/ssl/handoff.cc index d99dafad..5ac36b9e 100644 --- a/Sources/CNIOBoringSSL/ssl/handoff.cc +++ b/Sources/CNIOBoringSSL/ssl/handoff.cc @@ -54,7 +54,7 @@ bool SSL_serialize_handoff(const SSL *ssl, CBB *out, const SSL3_STATE *const s3 = ssl->s3; if (!ssl->server || s3->hs == nullptr || - s3->rwstate != SSL_HANDOFF) { + s3->rwstate != SSL_ERROR_HANDOFF) { return false; } @@ -81,7 +81,7 @@ bool SSL_decline_handoff(SSL *ssl) { const SSL3_STATE *const s3 = ssl->s3; if (!ssl->server || s3->hs == nullptr || - s3->rwstate != SSL_HANDOFF) { + s3->rwstate != SSL_ERROR_HANDOFF) { return false; } diff --git a/Sources/CNIOBoringSSL/ssl/handshake.cc b/Sources/CNIOBoringSSL/ssl/handshake.cc index 666407fc..4f89d8fb 100644 --- a/Sources/CNIOBoringSSL/ssl/handshake.cc +++ b/Sources/CNIOBoringSSL/ssl/handshake.cc @@ -157,6 +157,13 @@ SSL_HANDSHAKE::~SSL_HANDSHAKE() { ssl->ctx->x509_method->hs_flush_cached_ca_names(this); } +void SSL_HANDSHAKE::ResizeSecrets(size_t hash_len) { + if (hash_len > SSL_MAX_MD_SIZE) { + abort(); + } + hash_len_ = hash_len; +} + UniquePtr ssl_handshake_new(SSL *ssl) { UniquePtr hs = MakeUnique(ssl); if (!hs || !hs->transcript.Init()) { @@ -479,8 +486,9 @@ bool ssl_send_finished(SSL_HANDSHAKE *hs) { } // Log the master secret, if logging is enabled. - if (!ssl_log_secret(ssl, "CLIENT_RANDOM", session->master_key, - session->master_key_length)) { + if (!ssl_log_secret( + ssl, "CLIENT_RANDOM", + MakeConstSpan(session->master_key, session->master_key_length))) { return 0; } @@ -549,7 +557,7 @@ int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return) { hs->wait = ssl_hs_ok; // The change cipher spec is omitted in QUIC. if (hs->wait != ssl_hs_read_change_cipher_spec) { - ssl->s3->rwstate = SSL_READING; + ssl->s3->rwstate = SSL_ERROR_WANT_READ; return -1; } break; @@ -603,53 +611,53 @@ int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return) { } case ssl_hs_certificate_selection_pending: - ssl->s3->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; + ssl->s3->rwstate = SSL_ERROR_PENDING_CERTIFICATE; hs->wait = ssl_hs_ok; return -1; case ssl_hs_handoff: - ssl->s3->rwstate = SSL_HANDOFF; + ssl->s3->rwstate = SSL_ERROR_HANDOFF; hs->wait = ssl_hs_ok; return -1; case ssl_hs_handback: - ssl->s3->rwstate = SSL_HANDBACK; + ssl->s3->rwstate = SSL_ERROR_HANDBACK; hs->wait = ssl_hs_handback; return -1; case ssl_hs_x509_lookup: - ssl->s3->rwstate = SSL_X509_LOOKUP; + ssl->s3->rwstate = SSL_ERROR_WANT_X509_LOOKUP; hs->wait = ssl_hs_ok; return -1; case ssl_hs_channel_id_lookup: - ssl->s3->rwstate = SSL_CHANNEL_ID_LOOKUP; + ssl->s3->rwstate = SSL_ERROR_WANT_CHANNEL_ID_LOOKUP; hs->wait = ssl_hs_ok; return -1; case ssl_hs_private_key_operation: - ssl->s3->rwstate = SSL_PRIVATE_KEY_OPERATION; + ssl->s3->rwstate = SSL_ERROR_WANT_PRIVATE_KEY_OPERATION; hs->wait = ssl_hs_ok; return -1; case ssl_hs_pending_session: - ssl->s3->rwstate = SSL_PENDING_SESSION; + ssl->s3->rwstate = SSL_ERROR_PENDING_SESSION; hs->wait = ssl_hs_ok; return -1; case ssl_hs_pending_ticket: - ssl->s3->rwstate = SSL_PENDING_TICKET; + ssl->s3->rwstate = SSL_ERROR_PENDING_TICKET; hs->wait = ssl_hs_ok; return -1; case ssl_hs_certificate_verify: - ssl->s3->rwstate = SSL_CERTIFICATE_VERIFY; + ssl->s3->rwstate = SSL_ERROR_WANT_CERTIFICATE_VERIFY; hs->wait = ssl_hs_ok; return -1; case ssl_hs_early_data_rejected: assert(ssl->s3->early_data_reason != ssl_early_data_unknown); - ssl->s3->rwstate = SSL_EARLY_DATA_REJECTED; + ssl->s3->rwstate = SSL_ERROR_EARLY_DATA_REJECTED; // Cause |SSL_write| to start failing immediately. hs->can_early_write = false; return -1; diff --git a/Sources/CNIOBoringSSL/ssl/handshake_client.cc b/Sources/CNIOBoringSSL/ssl/handshake_client.cc index a3eebe52..4f3e785e 100644 --- a/Sources/CNIOBoringSSL/ssl/handshake_client.cc +++ b/Sources/CNIOBoringSSL/ssl/handshake_client.cc @@ -456,11 +456,16 @@ static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) { return ssl_hs_error; } - if (!tls13_init_early_key_schedule(hs, ssl->session->master_key, - ssl->session->master_key_length) || - !tls13_derive_early_secrets(hs) || + if (!tls13_init_early_key_schedule( + hs, MakeConstSpan(ssl->session->master_key, + ssl->session->master_key_length)) || + !tls13_derive_early_secret(hs) || + !tls13_set_early_secret_for_quic(hs)) { + return ssl_hs_error; + } + if (ssl->quic_method == nullptr && !tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_seal, - hs->early_traffic_secret, hs->hash_len)) { + hs->early_traffic_secret())) { return ssl_hs_error; } diff --git a/Sources/CNIOBoringSSL/ssl/internal.h b/Sources/CNIOBoringSSL/ssl/internal.h index c0a0d2a3..2cc2cfef 100644 --- a/Sources/CNIOBoringSSL/ssl/internal.h +++ b/Sources/CNIOBoringSSL/ssl/internal.h @@ -1248,30 +1248,34 @@ bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs); // tls13_init_key_schedule initializes the handshake hash and key derivation // state, and incorporates the PSK. The cipher suite and PRF hash must have been // selected at this point. It returns true on success and false on error. -bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk, - size_t psk_len); +bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span psk); // tls13_init_early_key_schedule initializes the handshake hash and key // derivation state from the resumption secret and incorporates the PSK to // derive the early secrets. It returns one on success and zero on error. -bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk, - size_t psk_len); +bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, Span psk); // tls13_advance_key_schedule incorporates |in| into the key schedule with // HKDF-Extract. It returns true on success and false on error. -bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in, - size_t len); +bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span in); // tls13_set_traffic_key sets the read or write traffic keys to // |traffic_secret|. It returns true on success and false on error. bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, enum evp_aead_direction_t direction, - const uint8_t *traffic_secret, - size_t traffic_secret_len); + Span traffic_secret); -// tls13_derive_early_secrets derives the early traffic secret. It returns true -// on success and false on error. -bool tls13_derive_early_secrets(SSL_HANDSHAKE *hs); +// tls13_derive_early_secret derives the early traffic secret. It returns true +// on success and false on error. Unlike with other traffic secrets, this +// function does not pass the keys to QUIC. Call +// |tls13_set_early_secret_for_quic| to do so. This is done to due to an +// ordering complication around resolving HelloRetryRequest on the server. +bool tls13_derive_early_secret(SSL_HANDSHAKE *hs); + +// tls13_set_early_secret_for_quic passes the early traffic secrets, as +// derived by |tls13_derive_early_secret|, to QUIC. It returns true on success +// and false on error. +bool tls13_set_early_secret_for_quic(SSL_HANDSHAKE *hs); // tls13_derive_handshake_secrets derives the handshake traffic secret. It // returns true on success and false on error. @@ -1456,14 +1460,38 @@ struct SSL_HANDSHAKE { // |SSL_OP_NO_*| and |SSL_CTX_set_max_proto_version| APIs. uint16_t max_version = 0; - size_t hash_len = 0; - uint8_t secret[SSL_MAX_MD_SIZE] = {0}; - uint8_t early_traffic_secret[SSL_MAX_MD_SIZE] = {0}; - uint8_t client_handshake_secret[SSL_MAX_MD_SIZE] = {0}; - uint8_t server_handshake_secret[SSL_MAX_MD_SIZE] = {0}; - uint8_t client_traffic_secret_0[SSL_MAX_MD_SIZE] = {0}; - uint8_t server_traffic_secret_0[SSL_MAX_MD_SIZE] = {0}; - uint8_t expected_client_finished[SSL_MAX_MD_SIZE] = {0}; + private: + size_t hash_len_ = 0; + uint8_t secret_[SSL_MAX_MD_SIZE] = {0}; + uint8_t early_traffic_secret_[SSL_MAX_MD_SIZE] = {0}; + uint8_t client_handshake_secret_[SSL_MAX_MD_SIZE] = {0}; + uint8_t server_handshake_secret_[SSL_MAX_MD_SIZE] = {0}; + uint8_t client_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0}; + uint8_t server_traffic_secret_0_[SSL_MAX_MD_SIZE] = {0}; + uint8_t expected_client_finished_[SSL_MAX_MD_SIZE] = {0}; + + public: + void ResizeSecrets(size_t hash_len); + + Span secret() { return MakeSpan(secret_, hash_len_); } + Span early_traffic_secret() { + return MakeSpan(early_traffic_secret_, hash_len_); + } + Span client_handshake_secret() { + return MakeSpan(client_handshake_secret_, hash_len_); + } + Span server_handshake_secret() { + return MakeSpan(server_handshake_secret_, hash_len_); + } + Span client_traffic_secret_0() { + return MakeSpan(client_traffic_secret_0_, hash_len_); + } + Span server_traffic_secret_0() { + return MakeSpan(server_traffic_secret_0_, hash_len_); + } + Span expected_client_finished() { + return MakeSpan(expected_client_finished_, hash_len_); + } union { // sent is a bitset where the bits correspond to elements of kExtensions @@ -1643,7 +1671,8 @@ struct SSL_HANDSHAKE { // handback indicates that a server should pause the handshake after // finishing operations that require private key material, in such a way that - // |SSL_get_error| returns |SSL_HANDBACK|. It is set by |SSL_apply_handoff|. + // |SSL_get_error| returns |SSL_ERROR_HANDBACK|. It is set by + // |SSL_apply_handoff|. bool handback : 1; // cert_compression_negotiated is true iff |cert_compression_alg_id| is valid. @@ -1803,9 +1832,9 @@ bool ssl_output_cert_chain(SSL_HANDSHAKE *hs); // SSLKEYLOGFILE functions. // ssl_log_secret logs |secret| with label |label|, if logging is enabled for -// |ssl|. It returns one on success and zero on failure. -int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret, - size_t secret_len); +// |ssl|. It returns true on success and false on failure. +bool ssl_log_secret(const SSL *ssl, const char *label, + Span secret); // ClientHello functions. @@ -2181,7 +2210,7 @@ struct SSL3_STATE { // This holds a variable that indicates what we were doing when a 0 or -1 is // returned. This is needed for non-blocking IO so we know what request // needs re-doing when in SSL_accept or SSL_connect - int rwstate = SSL_NOTHING; + int rwstate = SSL_ERROR_NONE; enum ssl_encryption_level_t read_level = ssl_encryption_initial; enum ssl_encryption_level_t write_level = ssl_encryption_initial; @@ -2578,7 +2607,7 @@ struct SSL_CONFIG { // handoff indicates that a server should stop after receiving the // ClientHello and pause the handshake in such a way that |SSL_get_error| - // returns |SSL_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX| + // returns |SSL_ERROR_HANDOFF|. This is copied in |SSL_new| from the |SSL_CTX| // element of the same name and may be cleared if the handoff is declined. bool handoff : 1; @@ -3187,7 +3216,7 @@ struct ssl_ctx_st { // handoff indicates that a server should stop after receiving the // ClientHello and pause the handshake in such a way that |SSL_get_error| - // returns |SSL_HANDOFF|. + // returns |SSL_ERROR_HANDOFF|. bool handoff : 1; // If enable_early_data is true, early data can be sent and accepted. diff --git a/Sources/CNIOBoringSSL/ssl/s3_both.cc b/Sources/CNIOBoringSSL/ssl/s3_both.cc index 9d3b66c0..0523d306 100644 --- a/Sources/CNIOBoringSSL/ssl/s3_both.cc +++ b/Sources/CNIOBoringSSL/ssl/s3_both.cc @@ -317,7 +317,7 @@ int ssl3_flush_flight(SSL *ssl) { if (!ssl->s3->write_buffer.empty()) { int ret = ssl_write_buffer_flush(ssl); if (ret <= 0) { - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; return ret; } } @@ -329,7 +329,7 @@ int ssl3_flush_flight(SSL *ssl) { ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset); if (ret <= 0) { - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; return ret; } @@ -337,7 +337,7 @@ int ssl3_flush_flight(SSL *ssl) { } if (BIO_flush(ssl->wbio.get()) <= 0) { - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; return -1; } diff --git a/Sources/CNIOBoringSSL/ssl/ssl_buffer.cc b/Sources/CNIOBoringSSL/ssl/ssl_buffer.cc index 58a9c61a..28c447b1 100644 --- a/Sources/CNIOBoringSSL/ssl/ssl_buffer.cc +++ b/Sources/CNIOBoringSSL/ssl/ssl_buffer.cc @@ -116,7 +116,7 @@ static int dtls_read_buffer_next_packet(SSL *ssl) { int ret = BIO_read(ssl->rbio.get(), buf->data(), static_cast(buf->cap())); if (ret <= 0) { - ssl->s3->rwstate = SSL_READING; + ssl->s3->rwstate = SSL_ERROR_WANT_READ; return ret; } buf->DidWrite(static_cast(ret)); @@ -138,7 +138,7 @@ static int tls_read_buffer_extend_to(SSL *ssl, size_t len) { int ret = BIO_read(ssl->rbio.get(), buf->data() + buf->size(), static_cast(len - buf->size())); if (ret <= 0) { - ssl->s3->rwstate = SSL_READING; + ssl->s3->rwstate = SSL_ERROR_WANT_READ; return ret; } buf->DidWrite(static_cast(ret)); @@ -243,7 +243,7 @@ static int tls_write_buffer_flush(SSL *ssl) { while (!buf->empty()) { int ret = BIO_write(ssl->wbio.get(), buf->data(), buf->size()); if (ret <= 0) { - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; return ret; } buf->Consume(static_cast(ret)); @@ -260,7 +260,7 @@ static int dtls_write_buffer_flush(SSL *ssl) { int ret = BIO_write(ssl->wbio.get(), buf->data(), buf->size()); if (ret <= 0) { - ssl->s3->rwstate = SSL_WRITING; + ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; // If the write failed, drop the write buffer anyway. Datagram transports // can't write half a packet, so the caller is expected to retry from the // top. diff --git a/Sources/CNIOBoringSSL/ssl/ssl_cipher.cc b/Sources/CNIOBoringSSL/ssl/ssl_cipher.cc index b11f5992..80865453 100644 --- a/Sources/CNIOBoringSSL/ssl/ssl_cipher.cc +++ b/Sources/CNIOBoringSSL/ssl/ssl_cipher.cc @@ -1377,6 +1377,10 @@ const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) { uint32_t SSL_CIPHER_get_id(const SSL_CIPHER *cipher) { return cipher->id; } +uint16_t SSL_CIPHER_get_value(const SSL_CIPHER *cipher) { + return static_cast(cipher->id); +} + int SSL_CIPHER_is_aead(const SSL_CIPHER *cipher) { return (cipher->algorithm_mac & SSL_AEAD) != 0; } diff --git a/Sources/CNIOBoringSSL/ssl/ssl_lib.cc b/Sources/CNIOBoringSSL/ssl/ssl_lib.cc index e718250a..583ba3f2 100644 --- a/Sources/CNIOBoringSSL/ssl/ssl_lib.cc +++ b/Sources/CNIOBoringSSL/ssl/ssl_lib.cc @@ -201,7 +201,7 @@ bool CBBFinishArray(CBB *cbb, Array *out) { void ssl_reset_error_state(SSL *ssl) { // Functions which use |SSL_get_error| must reset I/O and error state on // entry. - ssl->s3->rwstate = SSL_NOTHING; + ssl->s3->rwstate = SSL_ERROR_NONE; ERR_clear_error(); ERR_clear_system_error(); } @@ -323,46 +323,45 @@ void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) { } } -static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) { +static bool cbb_add_hex(CBB *cbb, Span in) { static const char hextable[] = "0123456789abcdef"; uint8_t *out; - if (!CBB_add_space(cbb, &out, in_len * 2)) { - return 0; + if (!CBB_add_space(cbb, &out, in.size() * 2)) { + return false; } - for (size_t i = 0; i < in_len; i++) { - *(out++) = (uint8_t)hextable[in[i] >> 4]; - *(out++) = (uint8_t)hextable[in[i] & 0xf]; + for (uint8_t b : in) { + *(out++) = (uint8_t)hextable[b >> 4]; + *(out++) = (uint8_t)hextable[b & 0xf]; } - return 1; + return true; } -int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret, - size_t secret_len) { +bool ssl_log_secret(const SSL *ssl, const char *label, + Span secret) { if (ssl->ctx->keylog_callback == NULL) { - return 1; + return true; } ScopedCBB cbb; - uint8_t *out; - size_t out_len; + Array line; if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 + - secret_len * 2 + 1) || - !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) || - !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) || - !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || - !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) || - !cbb_add_hex(cbb.get(), secret, secret_len) || + secret.size() * 2 + 1) || + !CBB_add_bytes(cbb.get(), reinterpret_cast(label), + strlen(label)) || + !CBB_add_u8(cbb.get(), ' ') || + !cbb_add_hex(cbb.get(), ssl->s3->client_random) || + !CBB_add_u8(cbb.get(), ' ') || + !cbb_add_hex(cbb.get(), secret) || !CBB_add_u8(cbb.get(), 0 /* NUL */) || - !CBB_finish(cbb.get(), &out, &out_len)) { - return 0; + !CBBFinishArray(cbb.get(), &line)) { + return false; } - ssl->ctx->keylog_callback(ssl, (const char *)out); - OPENSSL_free(out); - return 1; + ssl->ctx->keylog_callback(ssl, reinterpret_cast(line.data())); + return true; } void ssl_do_info_callback(const SSL *ssl, int type, int value) { @@ -1344,19 +1343,19 @@ int SSL_get_error(const SSL *ssl, int ret_code) { } switch (ssl->s3->rwstate) { - case SSL_PENDING_SESSION: - return SSL_ERROR_PENDING_SESSION; + case SSL_ERROR_PENDING_SESSION: + case SSL_ERROR_PENDING_CERTIFICATE: + case SSL_ERROR_HANDOFF: + case SSL_ERROR_HANDBACK: + case SSL_ERROR_WANT_X509_LOOKUP: + case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: + case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: + case SSL_ERROR_PENDING_TICKET: + case SSL_ERROR_EARLY_DATA_REJECTED: + case SSL_ERROR_WANT_CERTIFICATE_VERIFY: + return ssl->s3->rwstate; - case SSL_CERTIFICATE_SELECTION_PENDING: - return SSL_ERROR_PENDING_CERTIFICATE; - - case SSL_HANDOFF: - return SSL_ERROR_HANDOFF; - - case SSL_HANDBACK: - return SSL_ERROR_HANDBACK; - - case SSL_READING: { + case SSL_ERROR_WANT_READ: { if (ssl->quic_method) { return SSL_ERROR_WANT_READ; } @@ -1378,7 +1377,7 @@ int SSL_get_error(const SSL *ssl, int ret_code) { break; } - case SSL_WRITING: { + case SSL_ERROR_WANT_WRITE: { BIO *bio = SSL_get_wbio(ssl); if (BIO_should_write(bio)) { return SSL_ERROR_WANT_WRITE; @@ -1396,29 +1395,54 @@ int SSL_get_error(const SSL *ssl, int ret_code) { break; } - - case SSL_X509_LOOKUP: - return SSL_ERROR_WANT_X509_LOOKUP; - - case SSL_CHANNEL_ID_LOOKUP: - return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP; - - case SSL_PRIVATE_KEY_OPERATION: - return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION; - - case SSL_PENDING_TICKET: - return SSL_ERROR_PENDING_TICKET; - - case SSL_EARLY_DATA_REJECTED: - return SSL_ERROR_EARLY_DATA_REJECTED; - - case SSL_CERTIFICATE_VERIFY: - return SSL_ERROR_WANT_CERTIFICATE_VERIFY; } return SSL_ERROR_SYSCALL; } +const char *SSL_error_description(int err) { + switch (err) { + case SSL_ERROR_NONE: + return "NONE"; + case SSL_ERROR_SSL: + return "SSL"; + case SSL_ERROR_WANT_READ: + return "WANT_READ"; + case SSL_ERROR_WANT_WRITE: + return "WANT_WRITE"; + case SSL_ERROR_WANT_X509_LOOKUP: + return "WANT_X509_LOOKUP"; + case SSL_ERROR_SYSCALL: + return "SYSCALL"; + case SSL_ERROR_ZERO_RETURN: + return "ZERO_RETURN"; + case SSL_ERROR_WANT_CONNECT: + return "WANT_CONNECT"; + case SSL_ERROR_WANT_ACCEPT: + return "WANT_ACCEPT"; + case SSL_ERROR_WANT_CHANNEL_ID_LOOKUP: + return "WANT_CHANNEL_ID_LOOKUP"; + case SSL_ERROR_PENDING_SESSION: + return "PENDING_SESSION"; + case SSL_ERROR_PENDING_CERTIFICATE: + return "PENDING_CERTIFICATE"; + case SSL_ERROR_WANT_PRIVATE_KEY_OPERATION: + return "WANT_PRIVATE_KEY_OPERATION"; + case SSL_ERROR_PENDING_TICKET: + return "PENDING_TICKET"; + case SSL_ERROR_EARLY_DATA_REJECTED: + return "EARLY_DATA_REJECTED"; + case SSL_ERROR_WANT_CERTIFICATE_VERIFY: + return "WANT_CERTIFICATE_VERIFY"; + case SSL_ERROR_HANDOFF: + return "HANDOFF"; + case SSL_ERROR_HANDBACK: + return "HANDBACK"; + default: + return nullptr; + } +} + uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) { ctx->options |= options; return ctx->options; diff --git a/Sources/CNIOBoringSSL/ssl/ssl_versions.cc b/Sources/CNIOBoringSSL/ssl/ssl_versions.cc index f6332c2d..146176a2 100644 --- a/Sources/CNIOBoringSSL/ssl/ssl_versions.cc +++ b/Sources/CNIOBoringSSL/ssl/ssl_versions.cc @@ -63,24 +63,16 @@ static const uint16_t kDTLSVersions[] = { DTLS1_VERSION, }; -static void get_method_versions(const SSL_PROTOCOL_METHOD *method, - const uint16_t **out, size_t *out_num) { - if (method->is_dtls) { - *out = kDTLSVersions; - *out_num = OPENSSL_ARRAY_SIZE(kDTLSVersions); - } else { - *out = kTLSVersions; - *out_num = OPENSSL_ARRAY_SIZE(kTLSVersions); - } +static Span get_method_versions( + const SSL_PROTOCOL_METHOD *method) { + return method->is_dtls ? Span(kDTLSVersions) + : Span(kTLSVersions); } bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method, uint16_t version) { - const uint16_t *versions; - size_t num_versions; - get_method_versions(method, &versions, &num_versions); - for (size_t i = 0; i < num_versions; i++) { - if (versions[i] == version) { + for (uint16_t supported : get_method_versions(method)) { + if (supported == version) { return true; } } @@ -282,12 +274,9 @@ bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) { } bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) { - const uint16_t *versions; - size_t num_versions; - get_method_versions(hs->ssl->method, &versions, &num_versions); - for (size_t i = 0; i < num_versions; i++) { - if (ssl_supports_version(hs, versions[i]) && - !CBB_add_u16(cbb, versions[i])) { + for (uint16_t version : get_method_versions(hs->ssl->method)) { + if (ssl_supports_version(hs, version) && + !CBB_add_u16(cbb, version)) { return false; } } @@ -296,11 +285,8 @@ bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) { bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, uint16_t *out_version, const CBS *peer_versions) { - const uint16_t *versions; - size_t num_versions; - get_method_versions(hs->ssl->method, &versions, &num_versions); - for (size_t i = 0; i < num_versions; i++) { - if (!ssl_supports_version(hs, versions[i])) { + for (uint16_t version : get_method_versions(hs->ssl->method)) { + if (!ssl_supports_version(hs, version)) { continue; } @@ -312,20 +298,20 @@ bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, // own.) // // See https://bugs.openjdk.java.net/browse/JDK-8211806. - if (versions[i] == TLS1_3_VERSION && hs->apply_jdk11_workaround) { + if (version == TLS1_3_VERSION && hs->apply_jdk11_workaround) { continue; } CBS copy = *peer_versions; while (CBS_len(©) != 0) { - uint16_t version; - if (!CBS_get_u16(©, &version)) { + uint16_t peer_version; + if (!CBS_get_u16(©, &peer_version)) { OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); *out_alert = SSL_AD_DECODE_ERROR; return false; } - if (version == versions[i]) { + if (peer_version == version) { *out_version = version; return true; } diff --git a/Sources/CNIOBoringSSL/ssl/tls13_both.cc b/Sources/CNIOBoringSSL/ssl/tls13_both.cc index e2aeb9e4..7cac55c3 100644 --- a/Sources/CNIOBoringSSL/ssl/tls13_both.cc +++ b/Sources/CNIOBoringSSL/ssl/tls13_both.cc @@ -384,21 +384,20 @@ bool tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg, bool use_saved_value) { SSL *const ssl = hs->ssl; uint8_t verify_data_buf[EVP_MAX_MD_SIZE]; - const uint8_t *verify_data; - size_t verify_data_len; + Span verify_data; if (use_saved_value) { assert(ssl->server); - verify_data = hs->expected_client_finished; - verify_data_len = hs->hash_len; + verify_data = hs->expected_client_finished(); } else { - if (!tls13_finished_mac(hs, verify_data_buf, &verify_data_len, - !ssl->server)) { + size_t len; + if (!tls13_finished_mac(hs, verify_data_buf, &len, !ssl->server)) { return false; } - verify_data = verify_data_buf; + verify_data = MakeConstSpan(verify_data_buf, len); } - bool finished_ok = CBS_mem_equal(&msg.body, verify_data, verify_data_len); + bool finished_ok = + CBS_mem_equal(&msg.body, verify_data.data(), verify_data.size()); #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) finished_ok = true; #endif diff --git a/Sources/CNIOBoringSSL/ssl/tls13_client.cc b/Sources/CNIOBoringSSL/ssl/tls13_client.cc index b73d0724..bc63062a 100644 --- a/Sources/CNIOBoringSSL/ssl/tls13_client.cc +++ b/Sources/CNIOBoringSSL/ssl/tls13_client.cc @@ -365,11 +365,12 @@ static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { // Set up the key schedule and incorporate the PSK into the running secret. if (ssl->s3->session_reused) { - if (!tls13_init_key_schedule(hs, hs->new_session->master_key, - hs->new_session->master_key_length)) { + if (!tls13_init_key_schedule( + hs, MakeConstSpan(hs->new_session->master_key, + hs->new_session->master_key_length))) { return ssl_hs_error; } - } else if (!tls13_init_key_schedule(hs, kZeroes, hash_len)) { + } else if (!tls13_init_key_schedule(hs, MakeConstSpan(kZeroes, hash_len))) { return ssl_hs_error; } @@ -389,10 +390,11 @@ static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { return ssl_hs_error; } - if (!tls13_advance_key_schedule(hs, dhe_secret.data(), dhe_secret.size()) || - !ssl_hash_message(hs, msg) || !tls13_derive_handshake_secrets(hs) || + if (!tls13_advance_key_schedule(hs, dhe_secret) || + !ssl_hash_message(hs, msg) || + !tls13_derive_handshake_secrets(hs) || !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, - hs->server_handshake_secret, hs->hash_len)) { + hs->server_handshake_secret())) { return ssl_hs_error; } @@ -400,7 +402,7 @@ static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { // If not sending early data, set client traffic keys now so that alerts are // encrypted. if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, - hs->client_handshake_secret, hs->hash_len)) { + hs->client_handshake_secret())) { return ssl_hs_error; } } @@ -615,7 +617,8 @@ static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) { !tls13_process_finished(hs, msg, false /* don't use saved value */) || !ssl_hash_message(hs, msg) || // Update the secret to the master secret and derive traffic keys. - !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) || + !tls13_advance_key_schedule( + hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) || !tls13_derive_application_secrets(hs)) { return ssl_hs_error; } @@ -630,18 +633,22 @@ static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) { if (ssl->s3->early_data_accepted) { hs->can_early_write = false; - ScopedCBB cbb; - CBB body; - if (!ssl->method->init_message(ssl, cbb.get(), &body, - SSL3_MT_END_OF_EARLY_DATA) || - !ssl_add_message_cbb(ssl, cbb.get())) { - return ssl_hs_error; + // QUIC omits the EndOfEarlyData message. See draft-ietf-quic-tls-22, + // section 8.3. + if (ssl->quic_method == nullptr) { + ScopedCBB cbb; + CBB body; + if (!ssl->method->init_message(ssl, cbb.get(), &body, + SSL3_MT_END_OF_EARLY_DATA) || + !ssl_add_message_cbb(ssl, cbb.get())) { + return ssl_hs_error; + } } } if (hs->early_data_offered) { if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, - hs->client_handshake_secret, hs->hash_len)) { + hs->client_handshake_secret())) { return ssl_hs_error; } } @@ -736,9 +743,9 @@ static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) { // Derive the final keys and enable them. if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, - hs->server_traffic_secret_0, hs->hash_len) || + hs->server_traffic_secret_0()) || !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, - hs->client_traffic_secret_0, hs->hash_len) || + hs->client_traffic_secret_0()) || !tls13_derive_resumption_secret(hs)) { return ssl_hs_error; } @@ -908,6 +915,15 @@ bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) { OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); return false; } + + // QUIC does not use the max_early_data_size parameter and always sets it to + // a fixed value. See draft-ietf-quic-tls-22, section 4.5. + if (ssl->quic_method != nullptr && + session->ticket_max_early_data != 0xffffffff) { + ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); + OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); + return false; + } } // Generate a session ID for this session. Some callers expect all sessions to diff --git a/Sources/CNIOBoringSSL/ssl/tls13_enc.cc b/Sources/CNIOBoringSSL/ssl/tls13_enc.cc index fdc6da25..caaf7a52 100644 --- a/Sources/CNIOBoringSSL/ssl/tls13_enc.cc +++ b/Sources/CNIOBoringSSL/ssl/tls13_enc.cc @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -33,117 +34,111 @@ BSSL_NAMESPACE_BEGIN static bool init_key_schedule(SSL_HANDSHAKE *hs, uint16_t version, - const SSL_CIPHER *cipher) { + const SSL_CIPHER *cipher) { if (!hs->transcript.InitHash(version, cipher)) { return false; } - assert(hs->transcript.DigestLen() <= SSL_MAX_MD_SIZE); - hs->hash_len = hs->transcript.DigestLen(); - // Initialize the secret to the zero key. - OPENSSL_memset(hs->secret, 0, hs->hash_len); + hs->ResizeSecrets(hs->transcript.DigestLen()); + OPENSSL_memset(hs->secret().data(), 0, hs->secret().size()); return true; } -bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk, - size_t psk_len) { +static bool hkdf_extract_to_secret(SSL_HANDSHAKE *hs, Span in) { + size_t len; + if (!HKDF_extract(hs->secret().data(), &len, hs->transcript.Digest(), + in.data(), in.size(), hs->secret().data(), + hs->secret().size())) { + return false; + } + assert(len == hs->secret().size()); + return true; +} + +bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span psk) { if (!init_key_schedule(hs, ssl_protocol_version(hs->ssl), hs->new_cipher)) { return false; } hs->transcript.FreeBuffer(); - return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), psk, - psk_len, hs->secret, hs->hash_len); + return hkdf_extract_to_secret(hs, psk); } -bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *psk, - size_t psk_len) { +bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, Span psk) { SSL *const ssl = hs->ssl; return init_key_schedule(hs, ssl_session_protocol_version(ssl->session.get()), ssl->session->cipher) && - HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), psk, - psk_len, hs->secret, hs->hash_len); + hkdf_extract_to_secret(hs, psk); } -static bool hkdf_expand_label(uint8_t *out, const EVP_MD *digest, - const uint8_t *secret, size_t secret_len, - const char *label, size_t label_len, - const uint8_t *hash, size_t hash_len, - size_t len) { - static const char kTLS13ProtocolLabel[] = "tls13 "; +static Span label_to_span(const char *label) { + return MakeConstSpan(label, strlen(label)); +} +static bool hkdf_expand_label(Span out, const EVP_MD *digest, + Span secret, + Span label, + Span hash) { + Span protocol_label = label_to_span("tls13 "); ScopedCBB cbb; CBB child; Array hkdf_label; - if (!CBB_init(cbb.get(), 2 + 1 + strlen(kTLS13ProtocolLabel) + label_len + 1 + - hash_len) || - !CBB_add_u16(cbb.get(), len) || + if (!CBB_init(cbb.get(), 2 + 1 + protocol_label.size() + label.size() + 1 + + hash.size()) || + !CBB_add_u16(cbb.get(), out.size()) || !CBB_add_u8_length_prefixed(cbb.get(), &child) || - !CBB_add_bytes(&child, (const uint8_t *)kTLS13ProtocolLabel, - strlen(kTLS13ProtocolLabel)) || - !CBB_add_bytes(&child, (const uint8_t *)label, label_len) || + !CBB_add_bytes(&child, + reinterpret_cast(protocol_label.data()), + protocol_label.size()) || + !CBB_add_bytes(&child, reinterpret_cast(label.data()), + label.size()) || !CBB_add_u8_length_prefixed(cbb.get(), &child) || - !CBB_add_bytes(&child, hash, hash_len) || + !CBB_add_bytes(&child, hash.data(), hash.size()) || !CBBFinishArray(cbb.get(), &hkdf_label)) { return false; } - return HKDF_expand(out, len, digest, secret, secret_len, hkdf_label.data(), - hkdf_label.size()); + return HKDF_expand(out.data(), out.size(), digest, secret.data(), + secret.size(), hkdf_label.data(), hkdf_label.size()); } static const char kTLS13LabelDerived[] = "derived"; -bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in, - size_t len) { +bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span in) { uint8_t derive_context[EVP_MAX_MD_SIZE]; unsigned derive_context_len; - if (!EVP_Digest(nullptr, 0, derive_context, &derive_context_len, - hs->transcript.Digest(), nullptr)) { - return false; - } - - if (!hkdf_expand_label(hs->secret, hs->transcript.Digest(), hs->secret, - hs->hash_len, kTLS13LabelDerived, - strlen(kTLS13LabelDerived), derive_context, - derive_context_len, hs->hash_len)) { - return false; - } - - return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), in, - len, hs->secret, hs->hash_len); + return EVP_Digest(nullptr, 0, derive_context, &derive_context_len, + hs->transcript.Digest(), nullptr) && + hkdf_expand_label(hs->secret(), hs->transcript.Digest(), hs->secret(), + label_to_span(kTLS13LabelDerived), + MakeConstSpan(derive_context, derive_context_len)) && + hkdf_extract_to_secret(hs, in); } -// derive_secret derives a secret of length |len| and writes the result in |out| -// with the given label and the current base secret and most recently-saved -// handshake context. It returns true on success and false on error. -static bool derive_secret(SSL_HANDSHAKE *hs, uint8_t *out, size_t len, - const char *label, size_t label_len) { +// derive_secret derives a secret of length |out.size()| and writes the result +// in |out| with the given label, the current base secret, and the most +// recently-saved handshake context. It returns true on success and false on +// error. +static bool derive_secret(SSL_HANDSHAKE *hs, Span out, + Span label) { uint8_t context_hash[EVP_MAX_MD_SIZE]; size_t context_hash_len; if (!hs->transcript.GetHash(context_hash, &context_hash_len)) { return false; } - return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret, - hs->hash_len, label, label_len, context_hash, - context_hash_len, len); + return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret(), label, + MakeConstSpan(context_hash, context_hash_len)); } bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, enum evp_aead_direction_t direction, - const uint8_t *traffic_secret, - size_t traffic_secret_len) { + Span traffic_secret) { const SSL_SESSION *session = SSL_get_session(ssl); uint16_t version = ssl_session_protocol_version(session); - if (traffic_secret_len > 0xff) { - OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); - return false; - } - UniquePtr traffic_aead; if (ssl->quic_method == nullptr) { // Look up cipher suite properties. @@ -158,30 +153,33 @@ bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, // Derive the key. size_t key_len = EVP_AEAD_key_length(aead); - uint8_t key[EVP_AEAD_MAX_KEY_LENGTH]; - if (!hkdf_expand_label(key, digest, traffic_secret, traffic_secret_len, - "key", 3, NULL, 0, key_len)) { + uint8_t key_buf[EVP_AEAD_MAX_KEY_LENGTH]; + auto key = MakeSpan(key_buf, key_len); + if (!hkdf_expand_label(key, digest, traffic_secret, label_to_span("key"), + {})) { return false; } // Derive the IV. size_t iv_len = EVP_AEAD_nonce_length(aead); - uint8_t iv[EVP_AEAD_MAX_NONCE_LENGTH]; - if (!hkdf_expand_label(iv, digest, traffic_secret, traffic_secret_len, "iv", - 2, NULL, 0, iv_len)) { + uint8_t iv_buf[EVP_AEAD_MAX_NONCE_LENGTH]; + auto iv = MakeSpan(iv_buf, iv_len); + if (!hkdf_expand_label(iv, digest, traffic_secret, label_to_span("iv"), + {})) { return false; } - traffic_aead = SSLAEADContext::Create( - direction, session->ssl_version, SSL_is_dtls(ssl), session->cipher, - MakeConstSpan(key, key_len), Span(), - MakeConstSpan(iv, iv_len)); + traffic_aead = SSLAEADContext::Create(direction, session->ssl_version, + SSL_is_dtls(ssl), session->cipher, + key, Span(), iv); } else { // Install a placeholder SSLAEADContext so that SSL accessors work. The // encryption itself will be handled by the SSL_QUIC_METHOD. traffic_aead = SSLAEADContext::CreatePlaceholderForQUIC(version, session->cipher); + // QUIC never installs early data keys at the TLS layer. + assert(level != ssl_encryption_early_data); } if (!traffic_aead) { @@ -199,15 +197,22 @@ bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level, } // Save the traffic secret. + if (traffic_secret.size() > + OPENSSL_ARRAY_SIZE(ssl->s3->read_traffic_secret) || + traffic_secret.size() > + OPENSSL_ARRAY_SIZE(ssl->s3->write_traffic_secret)) { + OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); + return false; + } if (direction == evp_aead_open) { - OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret, - traffic_secret_len); - ssl->s3->read_traffic_secret_len = traffic_secret_len; + OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret.data(), + traffic_secret.size()); + ssl->s3->read_traffic_secret_len = traffic_secret.size(); ssl->s3->read_level = level; } else { - OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret, - traffic_secret_len); - ssl->s3->write_traffic_secret_len = traffic_secret_len; + OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret.data(), + traffic_secret.size()); + ssl->s3->write_traffic_secret_len = traffic_secret.size(); ssl->s3->write_level = level; } @@ -223,165 +228,152 @@ static const char kTLS13LabelServerHandshakeTraffic[] = "s hs traffic"; static const char kTLS13LabelClientApplicationTraffic[] = "c ap traffic"; static const char kTLS13LabelServerApplicationTraffic[] = "s ap traffic"; -bool tls13_derive_early_secrets(SSL_HANDSHAKE *hs) { +bool tls13_derive_early_secret(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; - if (!derive_secret(hs, hs->early_traffic_secret, hs->hash_len, - kTLS13LabelClientEarlyTraffic, - strlen(kTLS13LabelClientEarlyTraffic)) || + if (!derive_secret(hs, hs->early_traffic_secret(), + label_to_span(kTLS13LabelClientEarlyTraffic)) || !ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET", - hs->early_traffic_secret, hs->hash_len)) { + hs->early_traffic_secret())) { return false; } + return true; +} - if (ssl->quic_method != nullptr) { - if (ssl->server) { - if (!ssl->quic_method->set_encryption_secrets( - ssl, ssl_encryption_early_data, nullptr, hs->early_traffic_secret, - hs->hash_len)) { - OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); - return false; - } - } else { - if (!ssl->quic_method->set_encryption_secrets( - ssl, ssl_encryption_early_data, hs->early_traffic_secret, nullptr, - hs->hash_len)) { - OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); - return false; - } +bool tls13_set_early_secret_for_quic(SSL_HANDSHAKE *hs) { + SSL *const ssl = hs->ssl; + if (ssl->quic_method == nullptr) { + return true; + } + if (ssl->server) { + if (!ssl->quic_method->set_encryption_secrets( + ssl, ssl_encryption_early_data, hs->early_traffic_secret().data(), + /*write_secret=*/nullptr, hs->early_traffic_secret().size())) { + OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); + return false; + } + } else { + if (!ssl->quic_method->set_encryption_secrets( + ssl, ssl_encryption_early_data, /*read_secret=*/nullptr, + hs->early_traffic_secret().data(), + hs->early_traffic_secret().size())) { + OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); + return false; } } - return true; } +static bool set_quic_secrets(SSL_HANDSHAKE *hs, ssl_encryption_level_t level, + Span client_write_secret, + Span server_write_secret) { + SSL *const ssl = hs->ssl; + assert(client_write_secret.size() == server_write_secret.size()); + if (ssl->quic_method == nullptr) { + return true; + } + if (!ssl->server) { + std::swap(client_write_secret, server_write_secret); + } + return ssl->quic_method->set_encryption_secrets( + ssl, level, + /*read_secret=*/client_write_secret.data(), + /*write_secret=*/server_write_secret.data(), client_write_secret.size()); +} + bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; - if (!derive_secret(hs, hs->client_handshake_secret, hs->hash_len, - kTLS13LabelClientHandshakeTraffic, - strlen(kTLS13LabelClientHandshakeTraffic)) || + if (!derive_secret(hs, hs->client_handshake_secret(), + label_to_span(kTLS13LabelClientHandshakeTraffic)) || !ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET", - hs->client_handshake_secret, hs->hash_len) || - !derive_secret(hs, hs->server_handshake_secret, hs->hash_len, - kTLS13LabelServerHandshakeTraffic, - strlen(kTLS13LabelServerHandshakeTraffic)) || + hs->client_handshake_secret()) || + !derive_secret(hs, hs->server_handshake_secret(), + label_to_span(kTLS13LabelServerHandshakeTraffic)) || !ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET", - hs->server_handshake_secret, hs->hash_len)) { + hs->server_handshake_secret()) || + !set_quic_secrets(hs, ssl_encryption_handshake, + hs->client_handshake_secret(), + hs->server_handshake_secret())) { return false; } - if (ssl->quic_method != nullptr) { - if (ssl->server) { - if (!ssl->quic_method->set_encryption_secrets( - ssl, ssl_encryption_handshake, hs->client_handshake_secret, - hs->server_handshake_secret, hs->hash_len)) { - OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); - return false; - } - } else { - if (!ssl->quic_method->set_encryption_secrets( - ssl, ssl_encryption_handshake, hs->server_handshake_secret, - hs->client_handshake_secret, hs->hash_len)) { - OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); - return false; - } - } - } - return true; } bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; - ssl->s3->exporter_secret_len = hs->hash_len; - if (!derive_secret(hs, hs->client_traffic_secret_0, hs->hash_len, - kTLS13LabelClientApplicationTraffic, - strlen(kTLS13LabelClientApplicationTraffic)) || + ssl->s3->exporter_secret_len = hs->transcript.DigestLen(); + if (!derive_secret(hs, hs->client_traffic_secret_0(), + label_to_span(kTLS13LabelClientApplicationTraffic)) || !ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0", - hs->client_traffic_secret_0, hs->hash_len) || - !derive_secret(hs, hs->server_traffic_secret_0, hs->hash_len, - kTLS13LabelServerApplicationTraffic, - strlen(kTLS13LabelServerApplicationTraffic)) || + hs->client_traffic_secret_0()) || + !derive_secret(hs, hs->server_traffic_secret_0(), + label_to_span(kTLS13LabelServerApplicationTraffic)) || !ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0", - hs->server_traffic_secret_0, hs->hash_len) || - !derive_secret(hs, ssl->s3->exporter_secret, hs->hash_len, - kTLS13LabelExporter, strlen(kTLS13LabelExporter)) || - !ssl_log_secret(ssl, "EXPORTER_SECRET", ssl->s3->exporter_secret, - hs->hash_len)) { + hs->server_traffic_secret_0()) || + !derive_secret( + hs, MakeSpan(ssl->s3->exporter_secret, ssl->s3->exporter_secret_len), + label_to_span(kTLS13LabelExporter)) || + !ssl_log_secret(ssl, "EXPORTER_SECRET", + MakeConstSpan(ssl->s3->exporter_secret, + ssl->s3->exporter_secret_len)) || + !set_quic_secrets(hs, ssl_encryption_application, + hs->client_traffic_secret_0(), + hs->server_traffic_secret_0())) { return false; } - if (ssl->quic_method != nullptr) { - if (ssl->server) { - if (!ssl->quic_method->set_encryption_secrets( - ssl, ssl_encryption_application, hs->client_traffic_secret_0, - hs->server_traffic_secret_0, hs->hash_len)) { - OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); - return false; - } - } else { - if (!ssl->quic_method->set_encryption_secrets( - ssl, ssl_encryption_application, hs->server_traffic_secret_0, - hs->client_traffic_secret_0, hs->hash_len)) { - OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); - return false; - } - } - } - return true; } static const char kTLS13LabelApplicationTraffic[] = "traffic upd"; bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) { - uint8_t *secret; - size_t secret_len; + Span secret; if (direction == evp_aead_open) { - secret = ssl->s3->read_traffic_secret; - secret_len = ssl->s3->read_traffic_secret_len; + secret = MakeSpan(ssl->s3->read_traffic_secret, + ssl->s3->read_traffic_secret_len); } else { - secret = ssl->s3->write_traffic_secret; - secret_len = ssl->s3->write_traffic_secret_len; + secret = MakeSpan(ssl->s3->write_traffic_secret, + ssl->s3->write_traffic_secret_len); } const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl)); - if (!hkdf_expand_label(secret, digest, secret, secret_len, - kTLS13LabelApplicationTraffic, - strlen(kTLS13LabelApplicationTraffic), NULL, 0, - secret_len)) { - return false; - } - - return tls13_set_traffic_key(ssl, ssl_encryption_application, direction, - secret, secret_len); + return hkdf_expand_label(secret, digest, secret, + label_to_span(kTLS13LabelApplicationTraffic), {}) && + tls13_set_traffic_key(ssl, ssl_encryption_application, direction, + secret); } static const char kTLS13LabelResumption[] = "res master"; bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) { - if (hs->hash_len > SSL_MAX_MASTER_KEY_LENGTH) { + if (hs->transcript.DigestLen() > SSL_MAX_MASTER_KEY_LENGTH) { OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); return false; } - hs->new_session->master_key_length = hs->hash_len; - return derive_secret(hs, hs->new_session->master_key, - hs->new_session->master_key_length, - kTLS13LabelResumption, strlen(kTLS13LabelResumption)); + hs->new_session->master_key_length = hs->transcript.DigestLen(); + return derive_secret( + hs, + MakeSpan(hs->new_session->master_key, hs->new_session->master_key_length), + label_to_span(kTLS13LabelResumption)); } static const char kTLS13LabelFinished[] = "finished"; // tls13_verify_data sets |out| to be the HMAC of |context| using a derived -// Finished key for both Finished messages and the PSK binder. -static bool tls13_verify_data(const EVP_MD *digest, uint16_t version, - uint8_t *out, size_t *out_len, - const uint8_t *secret, size_t hash_len, - const uint8_t *context, size_t context_len) { - uint8_t key[EVP_MAX_MD_SIZE]; +// Finished key for both Finished messages and the PSK binder. |out| must have +// space available for |EVP_MAX_MD_SIZE| bytes. +static bool tls13_verify_data(uint8_t *out, size_t *out_len, + const EVP_MD *digest, uint16_t version, + Span secret, + Span context) { + uint8_t key_buf[EVP_MAX_MD_SIZE]; + auto key = MakeSpan(key_buf, EVP_MD_size(digest)); unsigned len; - if (!hkdf_expand_label(key, digest, secret, hash_len, kTLS13LabelFinished, - strlen(kTLS13LabelFinished), NULL, 0, hash_len) || - HMAC(digest, key, hash_len, context, context_len, out, &len) == NULL) { + if (!hkdf_expand_label(key, digest, secret, + label_to_span(kTLS13LabelFinished), {}) || + HMAC(digest, key.data(), key.size(), context.data(), context.size(), out, + &len) == nullptr) { return false; } *out_len = len; @@ -390,19 +382,15 @@ static bool tls13_verify_data(const EVP_MD *digest, uint16_t version, bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, bool is_server) { - const uint8_t *traffic_secret; - if (is_server) { - traffic_secret = hs->server_handshake_secret; - } else { - traffic_secret = hs->client_handshake_secret; - } + Span traffic_secret = + is_server ? hs->server_handshake_secret() : hs->client_handshake_secret(); uint8_t context_hash[EVP_MAX_MD_SIZE]; size_t context_hash_len; if (!hs->transcript.GetHash(context_hash, &context_hash_len) || - !tls13_verify_data(hs->transcript.Digest(), hs->ssl->version, out, - out_len, traffic_secret, hs->hash_len, context_hash, - context_hash_len)) { + !tls13_verify_data(out, out_len, hs->transcript.Digest(), + hs->ssl->version, traffic_secret, + MakeConstSpan(context_hash, context_hash_len))) { return 0; } return 1; @@ -412,10 +400,11 @@ static const char kTLS13LabelResumptionPSK[] = "resumption"; bool tls13_derive_session_psk(SSL_SESSION *session, Span nonce) { const EVP_MD *digest = ssl_session_get_digest(session); - return hkdf_expand_label(session->master_key, digest, session->master_key, - session->master_key_length, kTLS13LabelResumptionPSK, - strlen(kTLS13LabelResumptionPSK), nonce.data(), - nonce.size(), session->master_key_length); + // The session initially stores the resumption_master_secret, which we + // override with the PSK. + auto session_key = MakeSpan(session->master_key, session->master_key_length); + return hkdf_expand_label(session_key, digest, session_key, + label_to_span(kTLS13LabelResumptionPSK), nonce); } static const char kTLS13LabelExportKeying[] = "exporter"; @@ -432,31 +421,32 @@ bool tls13_export_keying_material(SSL *ssl, Span out, const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl)); - uint8_t hash[EVP_MAX_MD_SIZE]; - uint8_t export_context[EVP_MAX_MD_SIZE]; - uint8_t derived_secret[EVP_MAX_MD_SIZE]; + uint8_t hash_buf[EVP_MAX_MD_SIZE]; + uint8_t export_context_buf[EVP_MAX_MD_SIZE]; unsigned hash_len; unsigned export_context_len; - unsigned derived_secret_len = EVP_MD_size(digest); - return EVP_Digest(context.data(), context.size(), hash, &hash_len, digest, - nullptr) && - EVP_Digest(nullptr, 0, export_context, &export_context_len, digest, - nullptr) && - hkdf_expand_label(derived_secret, digest, secret.data(), secret.size(), - label.data(), label.size(), export_context, - export_context_len, derived_secret_len) && - hkdf_expand_label(out.data(), digest, derived_secret, - derived_secret_len, kTLS13LabelExportKeying, - strlen(kTLS13LabelExportKeying), hash, hash_len, - out.size()); + if (!EVP_Digest(context.data(), context.size(), hash_buf, &hash_len, digest, + nullptr) || + !EVP_Digest(nullptr, 0, export_context_buf, &export_context_len, digest, + nullptr)) { + return false; + } + + auto hash = MakeConstSpan(hash_buf, hash_len); + auto export_context = MakeConstSpan(export_context_buf, export_context_len); + uint8_t derived_secret_buf[EVP_MAX_MD_SIZE]; + auto derived_secret = MakeSpan(derived_secret_buf, EVP_MD_size(digest)); + return hkdf_expand_label(derived_secret, digest, secret, label, + export_context) && + hkdf_expand_label(out, digest, derived_secret, + label_to_span(kTLS13LabelExportKeying), hash); } static const char kTLS13LabelPSKBinder[] = "res binder"; -static bool tls13_psk_binder(uint8_t *out, uint16_t version, - const EVP_MD *digest, const uint8_t *psk, - size_t psk_len, const uint8_t *context, - size_t context_len, size_t hash_len) { +static bool tls13_psk_binder(uint8_t *out, size_t *out_len, uint16_t version, + const EVP_MD *digest, Span psk, + Span context) { uint8_t binder_context[EVP_MAX_MD_SIZE]; unsigned binder_context_len; if (!EVP_Digest(NULL, 0, binder_context, &binder_context_len, digest, NULL)) { @@ -465,22 +455,22 @@ static bool tls13_psk_binder(uint8_t *out, uint16_t version, uint8_t early_secret[EVP_MAX_MD_SIZE] = {0}; size_t early_secret_len; - if (!HKDF_extract(early_secret, &early_secret_len, digest, psk, psk_len, NULL, - 0)) { + if (!HKDF_extract(early_secret, &early_secret_len, digest, psk.data(), + psk.size(), NULL, 0)) { return false; } - uint8_t binder_key[EVP_MAX_MD_SIZE] = {0}; - size_t len; - if (!hkdf_expand_label(binder_key, digest, early_secret, early_secret_len, - kTLS13LabelPSKBinder, strlen(kTLS13LabelPSKBinder), - binder_context, binder_context_len, hash_len) || - !tls13_verify_data(digest, version, out, &len, binder_key, hash_len, - context, context_len)) { + uint8_t binder_key_buf[EVP_MAX_MD_SIZE] = {0}; + auto binder_key = MakeSpan(binder_key_buf, EVP_MD_size(digest)); + if (!hkdf_expand_label(binder_key, digest, + MakeConstSpan(early_secret, early_secret_len), + label_to_span(kTLS13LabelPSKBinder), + MakeConstSpan(binder_context, binder_context_len)) || + !tls13_verify_data(out, out_len, digest, version, binder_key, context)) { return false; } - assert(len == hash_len); + assert(*out_len == EVP_MD_size(digest)); return true; } @@ -513,42 +503,50 @@ bool tls13_write_psk_binder(SSL_HANDSHAKE *hs, Span msg) { ScopedEVP_MD_CTX ctx; uint8_t context[EVP_MAX_MD_SIZE]; size_t context_len; - uint8_t verify_data[EVP_MAX_MD_SIZE] = {0}; + uint8_t verify_data[EVP_MAX_MD_SIZE]; + size_t verify_data_len; if (!hash_transcript_and_truncated_client_hello( hs, context, &context_len, digest, msg, 1 /* length prefix */ + hash_len) || - !tls13_psk_binder(verify_data, ssl->session->ssl_version, digest, - ssl->session->master_key, - ssl->session->master_key_length, context, context_len, - hash_len)) { + !tls13_psk_binder(verify_data, &verify_data_len, + ssl->session->ssl_version, digest, + MakeConstSpan(ssl->session->master_key, + ssl->session->master_key_length), + MakeConstSpan(context, context_len)) || + verify_data_len != hash_len) { + OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); return false; } - OPENSSL_memcpy(msg.data() + msg.size() - hash_len, verify_data, hash_len); + OPENSSL_memcpy(msg.data() + msg.size() - verify_data_len, verify_data, + verify_data_len); return true; } bool tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session, const SSLMessage &msg, CBS *binders) { - size_t hash_len = hs->transcript.DigestLen(); uint8_t context[EVP_MAX_MD_SIZE]; size_t context_len; - uint8_t verify_data[EVP_MAX_MD_SIZE] = {0}; + uint8_t verify_data[EVP_MAX_MD_SIZE]; + size_t verify_data_len; CBS binder; if (!hash_transcript_and_truncated_client_hello(hs, context, &context_len, hs->transcript.Digest(), msg.raw, CBS_len(binders)) || - !tls13_psk_binder(verify_data, hs->ssl->version, hs->transcript.Digest(), - session->master_key, session->master_key_length, - context, context_len, hash_len) || + !tls13_psk_binder( + verify_data, &verify_data_len, hs->ssl->version, + hs->transcript.Digest(), + MakeConstSpan(session->master_key, session->master_key_length), + MakeConstSpan(context, context_len)) || // We only consider the first PSK, so compare against the first binder. !CBS_get_u8_length_prefixed(binders, &binder)) { OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); return false; } - bool binder_ok = CBS_len(&binder) == hash_len && - CRYPTO_memcmp(CBS_data(&binder), verify_data, hash_len) == 0; + bool binder_ok = + CBS_len(&binder) == verify_data_len && + CRYPTO_memcmp(CBS_data(&binder), verify_data, verify_data_len) == 0; #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) binder_ok = true; #endif diff --git a/Sources/CNIOBoringSSL/ssl/tls13_server.cc b/Sources/CNIOBoringSSL/ssl/tls13_server.cc index 6daff840..f0523537 100644 --- a/Sources/CNIOBoringSSL/ssl/tls13_server.cc +++ b/Sources/CNIOBoringSSL/ssl/tls13_server.cc @@ -87,7 +87,7 @@ static int resolve_ecdhe_secret(SSL_HANDSHAKE *hs, bool *out_need_retry, return 0; } - return tls13_advance_key_schedule(hs, dhe_secret.data(), dhe_secret.size()); + return tls13_advance_key_schedule(hs, dhe_secret); } static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs, @@ -146,7 +146,10 @@ static bool add_new_session_tickets(SSL_HANDSHAKE *hs, bool *out_sent_tickets) { } session->ticket_age_add_valid = true; if (ssl->enable_early_data) { - session->ticket_max_early_data = kMaxEarlyDataAccepted; + // QUIC does not use the max_early_data_size parameter and always sets it + // to a fixed value. See draft-ietf-quic-tls-22, section 4.5. + session->ticket_max_early_data = + ssl->quic_method != nullptr ? 0xffffffff : kMaxEarlyDataAccepted; } static_assert(kNumTickets < 256, "Too many tickets"); @@ -428,11 +431,12 @@ static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) { // Set up the key schedule and incorporate the PSK into the running secret. if (ssl->s3->session_reused) { - if (!tls13_init_key_schedule(hs, hs->new_session->master_key, - hs->new_session->master_key_length)) { + if (!tls13_init_key_schedule( + hs, MakeConstSpan(hs->new_session->master_key, + hs->new_session->master_key_length))) { return ssl_hs_error; } - } else if (!tls13_init_key_schedule(hs, kZeroes, hash_len)) { + } else if (!tls13_init_key_schedule(hs, MakeConstSpan(kZeroes, hash_len))) { return ssl_hs_error; } @@ -441,7 +445,7 @@ static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) { } if (ssl->s3->early_data_accepted) { - if (!tls13_derive_early_secrets(hs)) { + if (!tls13_derive_early_secret(hs)) { return ssl_hs_error; } } else if (hs->early_data_offered) { @@ -467,6 +471,15 @@ static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) { return ssl_hs_error; } + // Note we defer releasing the early traffic secret to QUIC until after ECDHE + // is resolved. The early traffic secret should be derived before the key + // schedule incorporates ECDHE, but doing so may reject 0-RTT. To avoid + // confusing the caller, we split derivation and releasing the secret to QUIC. + if (ssl->s3->early_data_accepted && + !tls13_set_early_secret_for_quic(hs)) { + return ssl_hs_error; + } + ssl->method->next_message(ssl); hs->tls13_state = state_send_server_hello; return ssl_hs_ok; @@ -607,7 +620,7 @@ static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { // Derive and enable the handshake traffic secrets. if (!tls13_derive_handshake_secrets(hs) || !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, - hs->server_handshake_secret, hs->hash_len)) { + hs->server_handshake_secret())) { return ssl_hs_error; } @@ -715,10 +728,11 @@ static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; if (!tls13_add_finished(hs) || // Update the secret to the master secret and derive traffic keys. - !tls13_advance_key_schedule(hs, kZeroes, hs->hash_len) || + !tls13_advance_key_schedule( + hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) || !tls13_derive_application_secrets(hs) || !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, - hs->server_traffic_secret_0, hs->hash_len)) { + hs->server_traffic_secret_0())) { return ssl_hs_error; } @@ -729,18 +743,19 @@ static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { // Finished early. See RFC 8446, section 4.6.1. static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0, 0, 0}; - if (!hs->transcript.Update(kEndOfEarlyData)) { + if (ssl->quic_method == nullptr && + !hs->transcript.Update(kEndOfEarlyData)) { OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); return ssl_hs_error; } size_t finished_len; - if (!tls13_finished_mac(hs, hs->expected_client_finished, &finished_len, - false /* client */)) { + if (!tls13_finished_mac(hs, hs->expected_client_finished().data(), + &finished_len, false /* client */)) { return ssl_hs_error; } - if (finished_len != hs->hash_len) { + if (finished_len != hs->expected_client_finished().size()) { OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); return ssl_hs_error; } @@ -750,13 +765,13 @@ static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { // // TODO(davidben): This will need to be updated for DTLS 1.3. assert(!SSL_is_dtls(hs->ssl)); - assert(hs->hash_len <= 0xff); - uint8_t header[4] = {SSL3_MT_FINISHED, 0, 0, - static_cast(hs->hash_len)}; + assert(hs->expected_client_finished().size() <= 0xff); + uint8_t header[4] = { + SSL3_MT_FINISHED, 0, 0, + static_cast(hs->expected_client_finished().size())}; bool unused_sent_tickets; if (!hs->transcript.Update(header) || - !hs->transcript.Update( - MakeConstSpan(hs->expected_client_finished, hs->hash_len)) || + !hs->transcript.Update(hs->expected_client_finished()) || !tls13_derive_resumption_secret(hs) || !add_new_session_tickets(hs, &unused_sent_tickets)) { return ssl_hs_error; @@ -770,14 +785,29 @@ static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; if (ssl->s3->early_data_accepted) { - if (!tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open, - hs->early_traffic_secret, hs->hash_len)) { + // QUIC never receives handshake messages under 0-RTT keys. + if (ssl->quic_method == nullptr && + !tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open, + hs->early_traffic_secret())) { return ssl_hs_error; } hs->can_early_write = true; hs->can_early_read = true; hs->in_early_data = true; } + + // QUIC doesn't use an EndOfEarlyData message (draft-ietf-quic-tls-22, + // section 8.3), so we switch to client_handshake_secret before the early + // return. + if (ssl->quic_method != nullptr) { + if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, + hs->client_handshake_secret())) { + return ssl_hs_error; + } + hs->tls13_state = state_read_client_certificate; + return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok; + } + hs->tls13_state = state_process_end_of_early_data; return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data : ssl_hs_ok; @@ -785,42 +815,42 @@ static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) { static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; - if (hs->early_data_offered) { - // If early data was not accepted, the EndOfEarlyData and ChangeCipherSpec - // message will be in the discarded early data. - if (hs->ssl->s3->early_data_accepted) { - SSLMessage msg; - if (!ssl->method->get_message(ssl, &msg)) { - return ssl_hs_read_message; - } - - if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) { - return ssl_hs_error; - } - if (CBS_len(&msg.body) != 0) { - ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); - OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); - return ssl_hs_error; - } - ssl->method->next_message(ssl); + // If early data was not accepted, the EndOfEarlyData will be in the discarded + // early data. + if (hs->ssl->s3->early_data_accepted) { + SSLMessage msg; + if (!ssl->method->get_message(ssl, &msg)) { + return ssl_hs_read_message; } + if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) { + return ssl_hs_error; + } + if (CBS_len(&msg.body) != 0) { + ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); + OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); + return ssl_hs_error; + } + ssl->method->next_message(ssl); } if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, - hs->client_handshake_secret, hs->hash_len)) { + hs->client_handshake_secret())) { return ssl_hs_error; } - hs->tls13_state = ssl->s3->early_data_accepted - ? state_read_client_finished - : state_read_client_certificate; + hs->tls13_state = state_read_client_certificate; return ssl_hs_ok; } static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { SSL *const ssl = hs->ssl; if (!hs->cert_request) { - // OpenSSL returns X509_V_OK when no certificates are requested. This is - // classed by them as a bug, but it's assumed by at least NGINX. - hs->new_session->verify_result = X509_V_OK; + if (!ssl->s3->session_reused) { + // OpenSSL returns X509_V_OK when no certificates are requested. This is + // classed by them as a bug, but it's assumed by at least NGINX. (Only do + // this in full handshakes as resumptions should carry over the previous + // |verify_result|, though this is a no-op because servers do not + // implement the client's odd soft-fail mode.) + hs->new_session->verify_result = X509_V_OK; + } // Skip this state. hs->tls13_state = state_read_channel_id; @@ -913,7 +943,7 @@ static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) || // evp_aead_seal keys have already been switched. !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, - hs->client_traffic_secret_0, hs->hash_len)) { + hs->client_traffic_secret_0())) { return ssl_hs_error; } diff --git a/scripts/vendor-boringssl.sh b/scripts/vendor-boringssl.sh index ec394450..c870fdec 100755 --- a/scripts/vendor-boringssl.sh +++ b/scripts/vendor-boringssl.sh @@ -229,6 +229,13 @@ echo "PATCHING BoringSSL" git apply "${HERE}/scripts/patch-1-inttypes.patch" git apply "${HERE}/scripts/patch-2-arm-arch.patch" +# We need to avoid having the stack be executable. BoringSSL does this in its build system, but we can't. +echo "PROTECTING against executable stacks" +( + cd "$DSTROOT" + find . -name "*.S" | xargs $sed -i '$ a #if defined(__linux__) && defined(__ELF__)\n.section .note.GNU-stack,"",%progbits\n#endif\n' +) + # We need BoringSSL to be modularised echo "MODULARISING BoringSSL" cat << EOF > "$DSTROOT/include/CNIOBoringSSL.h"