From 07d28d859812933045bc9b88d007ba4b0786b380 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 20 Mar 2023 18:12:36 +0800 Subject: [PATCH 1/9] Add msvc build for aesce module Signed-off-by: Jerry Yu --- library/aesce.c | 3 ++- library/aesce.h | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/library/aesce.c b/library/aesce.c index fe056dc4c0..f0ca274df0 100644 --- a/library/aesce.c +++ b/library/aesce.c @@ -62,8 +62,9 @@ # pragma GCC push_options # pragma GCC target ("arch=armv8-a+crypto") # define MBEDTLS_POP_TARGET_PRAGMA +# elif defined(_MSC_VER) # else -# error "Only GCC and Clang supported for MBEDTLS_AESCE_C" +# error "Only MSVC, GCC and Clang supported for MBEDTLS_AESCE_C" # endif #endif /* !__ARM_FEATURE_AES || MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */ diff --git a/library/aesce.h b/library/aesce.h index 12ddc74b37..a29da34574 100644 --- a/library/aesce.h +++ b/library/aesce.h @@ -30,12 +30,19 @@ #include "mbedtls/aes.h" - +#if !defined(MBEDTLS_HAVE_ARM64) #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ - defined(__aarch64__) && !defined(MBEDTLS_HAVE_ARM64) + defined(__aarch64__) #define MBEDTLS_HAVE_ARM64 #endif +/* MSVC */ +#if defined(_M_ARM64) || defined(_M_ARM64EC) +#define MBEDTLS_HAVE_ARM64 +#endif +#endif + + #if defined(MBEDTLS_HAVE_ARM64) #ifdef __cplusplus From 9db4b1f455e54090c80da74b30447bfe9bddbdf6 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 21 Mar 2023 16:56:43 +0800 Subject: [PATCH 2/9] fix msvc type cast fail. GCC needs the `cast` due to incompatible type error Signed-off-by: Jerry Yu --- library/aesce.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/library/aesce.c b/library/aesce.c index f0ca274df0..fe52113c0c 100644 --- a/library/aesce.c +++ b/library/aesce.c @@ -296,12 +296,24 @@ static inline poly64_t vget_low_p64(poly64x2_t __a) * Older compilers miss some intrinsic functions for `poly*_t`. We use * uint8x16_t and uint8x16x3_t as input/output parameters. */ +#if defined(__GNUC__) && !defined(__clang__) +/* GCC reports incompatible type error without cast. GCC think poly64_t and + * poly64x1_t are different, that is different with MSVC and Clang. */ +#define MBEDTLS_VMULL_P64(a, b) vmull_p64((poly64_t) a, (poly64_t) b) +#else +/* MSVC reports `error C2440: 'type cast'` with cast. Clang does not report + * error with/without cast. And I think poly64_t and poly64x1_t are same, no + * cast for clang also. */ +#define MBEDTLS_VMULL_P64(a, b) vmull_p64(a, b) +#endif static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b) { + return vreinterpretq_u8_p128( - vmull_p64( - (poly64_t) vget_low_p64(vreinterpretq_p64_u8(a)), - (poly64_t) vget_low_p64(vreinterpretq_p64_u8(b)))); + MBEDTLS_VMULL_P64( + vget_low_p64(vreinterpretq_p64_u8(a)), + vget_low_p64(vreinterpretq_p64_u8(b)) + )); } static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b) From 8b6df3fd767880e5fdf74eaebb2ee80f732de5d5 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 21 Mar 2023 16:59:13 +0800 Subject: [PATCH 3/9] fix msvc fail on embed assembly code Signed-off-by: Jerry Yu --- library/aesce.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/aesce.c b/library/aesce.c index fe52113c0c..4e4ed06190 100644 --- a/library/aesce.c +++ b/library/aesce.c @@ -375,9 +375,14 @@ static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b) static inline uint8x16_t poly_mult_reduce(uint8x16x3_t input) { uint8x16_t const ZERO = vdupq_n_u8(0); - /* use 'asm' as an optimisation barrier to prevent loading MODULO from memory */ + uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87)); +#if defined(__GNUC__) + /* use 'asm' as an optimisation barrier to prevent loading MODULO from + * memory. It is for GNUC compatible compilers. + */ asm ("" : "+w" (r)); +#endif uint8x16_t const MODULO = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8)); uint8x16_t h, m, l; /* input high/middle/low 128b */ uint8x16_t c, d, e, f, g, n, o; From a7d454cec2630e9768d01708f19145dc0f768e47 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 21 Mar 2023 17:00:41 +0800 Subject: [PATCH 4/9] Remove unnecessary check Signed-off-by: Jerry Yu --- include/mbedtls/check_config.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index 266eb9ef49..c81cd1cc54 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -66,13 +66,6 @@ #error "MBEDTLS_HAVE_TIME_DATE without MBEDTLS_HAVE_TIME does not make sense" #endif -#if defined(__aarch64__) && defined(__GNUC__) -/* We don't do anything with MBEDTLS_AESCE_C on systems without ^ these two */ -#if defined(MBEDTLS_AESCE_C) && !defined(MBEDTLS_HAVE_ASM) -#error "MBEDTLS_AESCE_C defined, but not all prerequisites" -#endif -#endif - #if defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_AES_C) #error "MBEDTLS_CTR_DRBG_C defined, but not all prerequisites" #endif From 8f0e3d4c22306ffc992b97b72f85f3e5b507433c Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 25 Apr 2023 10:24:53 +0800 Subject: [PATCH 5/9] fix wrong compiler checks - Add msc version check - remove HAVE_ASM due to conflict with check_config Signed-off-by: Jerry Yu --- library/aesce.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/aesce.h b/library/aesce.h index a29da34574..55cbd7e829 100644 --- a/library/aesce.h +++ b/library/aesce.h @@ -31,13 +31,13 @@ #include "mbedtls/aes.h" #if !defined(MBEDTLS_HAVE_ARM64) -#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ - defined(__aarch64__) +#if defined(__GNUC__) && defined(__aarch64__) #define MBEDTLS_HAVE_ARM64 #endif /* MSVC */ -#if defined(_M_ARM64) || defined(_M_ARM64EC) +#if defined(_MSC_VER) && _MSC_VER >=1929 && \ + (defined(_M_ARM64) || defined(_M_ARM64EC)) #define MBEDTLS_HAVE_ARM64 #endif #endif From f015a93f9820902c0b1cbc45796a902962c11732 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 25 Apr 2023 10:38:03 +0800 Subject: [PATCH 6/9] Add msvc version document Signed-off-by: Jerry Yu --- include/mbedtls/mbedtls_config.h | 5 ++++- library/aesce.h | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/mbedtls/mbedtls_config.h b/include/mbedtls/mbedtls_config.h index 89d565971c..e2a88181c5 100644 --- a/include/mbedtls/mbedtls_config.h +++ b/include/mbedtls/mbedtls_config.h @@ -2076,12 +2076,15 @@ * Module: library/aesce.c * Caller: library/aes.c * - * Requires: MBEDTLS_HAVE_ASM, MBEDTLS_AES_C + * Requires: MBEDTLS_AES_C * * \warning Runtime detection only works on Linux. For non-Linux operating * system, Armv8-A Cryptographic Extensions must be supported by * the CPU when this option is enabled. * + * \note The minimum version of MSVC for building this module is "Visual + * Studio 2019 version 16.11.2"(`_MSC_VER >= 1929`) + * * This module adds support for the AES Armv8-A Cryptographic Extensions on Aarch64 systems. */ #define MBEDTLS_AESCE_C diff --git a/library/aesce.h b/library/aesce.h index 55cbd7e829..7916e522ce 100644 --- a/library/aesce.h +++ b/library/aesce.h @@ -35,7 +35,11 @@ #define MBEDTLS_HAVE_ARM64 #endif -/* MSVC */ +/* MSVC + * TODO: We haven't verified msvc from 1920 to 1928. If someone verified that, + * please update this and document of `MBEDTLS_AESCE_C` in + * `mbedtls_config.h` + */ #if defined(_MSC_VER) && _MSC_VER >=1929 && \ (defined(_M_ARM64) || defined(_M_ARM64EC)) #define MBEDTLS_HAVE_ARM64 From a1a039dba65d9a9e7ffcefefd6d66e991840ab12 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 26 Apr 2023 10:21:42 +0800 Subject: [PATCH 7/9] Improve minimum compiler versions document Signed-off-by: Jerry Yu --- include/mbedtls/mbedtls_config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mbedtls/mbedtls_config.h b/include/mbedtls/mbedtls_config.h index e2a88181c5..615885003d 100644 --- a/include/mbedtls/mbedtls_config.h +++ b/include/mbedtls/mbedtls_config.h @@ -2082,8 +2082,8 @@ * system, Armv8-A Cryptographic Extensions must be supported by * the CPU when this option is enabled. * - * \note The minimum version of MSVC for building this module is "Visual - * Studio 2019 version 16.11.2"(`_MSC_VER >= 1929`) + * \note Minimum compiler versions for this feature are Clang 4.0, + * GCC 6.0 or MSVC 2019 version 16.11.2. * * This module adds support for the AES Armv8-A Cryptographic Extensions on Aarch64 systems. */ From 61c4cfa2a73b3d5d906eb127ec3b49444d15d0eb Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 26 Apr 2023 11:06:51 +0800 Subject: [PATCH 8/9] Add compiler version checks. When `MBEDTLS_AESCE_C` enabled and the compiler is not expected, we should raise error to user. Signed-off-by: Jerry Yu --- library/aesce.c | 21 +++++++++++++-------- library/aesce.h | 13 +------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/library/aesce.c b/library/aesce.c index 4e4ed06190..4cbe8b0229 100644 --- a/library/aesce.c +++ b/library/aesce.c @@ -48,23 +48,28 @@ #if defined(MBEDTLS_HAVE_ARM64) +/* Compiler version checks. */ +#if defined(__clang__) && (__clang_major__ < 4) +# error "Minimum version of Clang for MBEDTLS_AESCE_C is 4.0." +#elif defined(__GNUC__) && (__GNUC__ < 6) +# error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0." +#elif defined(_MSC_VER) && (_MSC_VER < 1929) +/* TODO: We haven't verified MSVC from 1920 to 1928. If someone verified that, + * please update this and document of `MBEDTLS_AESCE_C` in + * `mbedtls_config.h`. */ +# error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2." +#endif + #if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG) # if defined(__clang__) -# if __clang_major__ < 4 -# error "A more recent Clang is required for MBEDTLS_AESCE_C" -# endif # pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function) # define MBEDTLS_POP_TARGET_PRAGMA # elif defined(__GNUC__) -# if __GNUC__ < 6 -# error "A more recent GCC is required for MBEDTLS_AESCE_C" -# endif # pragma GCC push_options # pragma GCC target ("arch=armv8-a+crypto") # define MBEDTLS_POP_TARGET_PRAGMA # elif defined(_MSC_VER) -# else -# error "Only MSVC, GCC and Clang supported for MBEDTLS_AESCE_C" +# error "Required feature(__ARM_FEATURE_AES) is not enabled." # endif #endif /* !__ARM_FEATURE_AES || MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */ diff --git a/library/aesce.h b/library/aesce.h index 7916e522ce..7048d77c56 100644 --- a/library/aesce.h +++ b/library/aesce.h @@ -31,22 +31,11 @@ #include "mbedtls/aes.h" #if !defined(MBEDTLS_HAVE_ARM64) -#if defined(__GNUC__) && defined(__aarch64__) -#define MBEDTLS_HAVE_ARM64 -#endif - -/* MSVC - * TODO: We haven't verified msvc from 1920 to 1928. If someone verified that, - * please update this and document of `MBEDTLS_AESCE_C` in - * `mbedtls_config.h` - */ -#if defined(_MSC_VER) && _MSC_VER >=1929 && \ - (defined(_M_ARM64) || defined(_M_ARM64EC)) +#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) #define MBEDTLS_HAVE_ARM64 #endif #endif - #if defined(MBEDTLS_HAVE_ARM64) #ifdef __cplusplus From db368dea8814e253d848f8e12933cdd2fda7ef4e Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 26 Apr 2023 16:55:37 +0800 Subject: [PATCH 9/9] fix clang test fail Signed-off-by: Jerry Yu --- library/aesce.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/library/aesce.c b/library/aesce.c index 4cbe8b0229..ff8c2e09cd 100644 --- a/library/aesce.c +++ b/library/aesce.c @@ -49,15 +49,21 @@ #if defined(MBEDTLS_HAVE_ARM64) /* Compiler version checks. */ -#if defined(__clang__) && (__clang_major__ < 4) -# error "Minimum version of Clang for MBEDTLS_AESCE_C is 4.0." -#elif defined(__GNUC__) && (__GNUC__ < 6) -# error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0." -#elif defined(_MSC_VER) && (_MSC_VER < 1929) +#if defined(__clang__) +# if __clang_major__ < 4 +# error "Minimum version of Clang for MBEDTLS_AESCE_C is 4.0." +# endif +#elif defined(__GNUC__) +# if __GNUC__ < 6 +# error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0." +# endif +#elif defined(_MSC_VER) /* TODO: We haven't verified MSVC from 1920 to 1928. If someone verified that, * please update this and document of `MBEDTLS_AESCE_C` in * `mbedtls_config.h`. */ -# error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2." +# if _MSC_VER < 1929 +# error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2." +# endif #endif #if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)