From ad3241a0dade1b0202c3cd3c25508c12090441f7 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 14 Nov 2022 12:43:42 +0100 Subject: [PATCH] Add __vectorcall to please MSVC --- .../crypto_aead/aes256gcm/aead_aes256gcm.c | 2 +- .../aes256gcm/aesni/aead_aes256gcm_aesni.c | 49 +++++++++---------- .../armcrypto/aead_aes256gcm_armcrypto.c | 46 +++++++++-------- 3 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/libsodium/crypto_aead/aes256gcm/aead_aes256gcm.c b/src/libsodium/crypto_aead/aes256gcm/aead_aes256gcm.c index 1f03271e..2946ba87 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aead_aes256gcm.c +++ b/src/libsodium/crypto_aead/aes256gcm/aead_aes256gcm.c @@ -48,7 +48,7 @@ crypto_aead_aes256gcm_keygen(unsigned char k[crypto_aead_aes256gcm_KEYBYTES]) } #if !((defined(HAVE_ARMCRYPTO) && defined(__clang__) && defined(NATIVE_LITTLE_ENDIAN)) || \ - (defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H) && defined(__GNUC__))) + (defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H))) #ifndef ENOSYS #define ENOSYS ENXIO diff --git a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c index 7c82a472..43bfc83e 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -13,13 +13,18 @@ #include "runtime.h" #include "utils.h" -#if defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H) && defined(__GNUC__) +#if defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H) #ifdef __GNUC__ #pragma GCC target("ssse3") #pragma GCC target("aes") #pragma GCC target("pclmul") #endif + +#ifndef _MSC_VER +#define __vectorcall +#endif + #include #include @@ -70,8 +75,7 @@ typedef struct I256 { BlockVec mid; } I256; -static inline I256 -XOR256(const I256 a, const I256 b) +static inline I256 __vectorcall XOR256(const I256 a, const I256 b) { return (I256) { SODIUM_C99(.hi =) XOR128(a.hi, b.hi), @@ -91,8 +95,7 @@ typedef struct State { Precomp hx[PC_COUNT]; } State; -static void -expand256(const unsigned char key[KEYBYTES], BlockVec rkeys[1 + ROUNDS]) +static void __vectorcall expand256(const unsigned char key[KEYBYTES], BlockVec rkeys[1 + ROUNDS]) { BlockVec t1, t2, s; size_t i = 0; @@ -150,9 +153,9 @@ encrypt(const State *st, unsigned char dst[16], const unsigned char src[16]) /* Encrypt and add a single AES block */ -static inline void -encrypt_xor_block(const State *st, unsigned char dst[16], const unsigned char src[16], - const BlockVec counter) +static inline void __vectorcall encrypt_xor_block(const State *st, unsigned char dst[16], + const unsigned char src[16], + const BlockVec counter) { BlockVec ts; size_t i; @@ -168,10 +171,10 @@ encrypt_xor_block(const State *st, unsigned char dst[16], const unsigned char sr /* Encrypt and add PARALLEL_BLOCKS AES blocks */ -static inline void -encrypt_xor_wide(const State *st, unsigned char dst[16 * PARALLEL_BLOCKS], - const unsigned char src[16 * PARALLEL_BLOCKS], - const BlockVec counters[PARALLEL_BLOCKS]) +static inline void __vectorcall encrypt_xor_wide(const State *st, + unsigned char dst[16 * PARALLEL_BLOCKS], + const unsigned char src[16 * PARALLEL_BLOCKS], + const BlockVec counters[PARALLEL_BLOCKS]) { BlockVec ts[PARALLEL_BLOCKS]; size_t i, j; @@ -195,8 +198,7 @@ encrypt_xor_wide(const State *st, unsigned char dst[16 * PARALLEL_BLOCKS], /* Square a field element */ -static inline I256 -clsq128(const BlockVec x) +static inline I256 __vectorcall clsq128(const BlockVec x) { const BlockVec x_hi = BYTESHR128(x, 8); const BlockVec mid = XOR128(x, x_hi); @@ -214,8 +216,7 @@ clsq128(const BlockVec x) /* Multiply two field elements -- Textbook multiplication is faster than Karatsuba on some recent * CPUs */ -static inline I256 -clmul128(const BlockVec x, const BlockVec y) +static inline I256 __vectorcall clmul128(const BlockVec x, const BlockVec y) { #ifdef USE_KARATSUBA_MULTIPLICATION const BlockVec x_hi = BYTESHR128(x, 8); @@ -244,8 +245,7 @@ clmul128(const BlockVec x, const BlockVec y) /* Merge the middle word and reduce a field element */ -static inline BlockVec -gcm_reduce(const I256 x) +static inline BlockVec __vectorcall gcm_reduce(const I256 x) { const BlockVec hi = XOR128(x.hi, BYTESHR128(x.mid, 8)); const BlockVec lo = XOR128(x.lo, BYTESHL128(x.mid, 8)); @@ -261,8 +261,7 @@ gcm_reduce(const I256 x) /* Precompute powers of H from `from` to `to` */ -static inline void -precomp(Precomp hx[PC_COUNT], const size_t from, const size_t to) +static inline void __vectorcall precomp(Precomp hx[PC_COUNT], const size_t from, const size_t to) { const Precomp h = hx[0]; size_t i; @@ -275,9 +274,9 @@ precomp(Precomp hx[PC_COUNT], const size_t from, const size_t to) /* Precompute powers of H given a key and a block count */ -static void -precomp_for_block_count(Precomp hx[PC_COUNT], const unsigned char gh_key[16], - const size_t block_count) +static void __vectorcall precomp_for_block_count(Precomp hx[PC_COUNT], + const unsigned char gh_key[16], + const size_t block_count) { const BlockVec h0 = REV128(LOAD128(gh_key)); BlockVec carry = SET64x2(0xc200000000000000, 1); @@ -377,8 +376,8 @@ gh_ad_blocks(const State *st, GHash *sth, const unsigned char *ad, size_t ad_len /* Increment counters */ -static inline BlockVec -incr_counters(BlockVec rev_counters[], BlockVec counter, const size_t n) +static inline BlockVec __vectorcall incr_counters(BlockVec rev_counters[], BlockVec counter, + const size_t n) { size_t i; diff --git a/src/libsodium/crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c b/src/libsodium/crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c index 46600097..774d5d6c 100644 --- a/src/libsodium/crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c +++ b/src/libsodium/crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c @@ -15,6 +15,10 @@ #if defined(HAVE_ARMCRYPTO) && defined(__clang__) && defined(NATIVE_LITTLE_ENDIAN) +#ifndef _MSC_VER +#define __vectorcall +#endif + #include #define ABYTES crypto_aead_aes256gcm_ABYTES @@ -85,8 +89,7 @@ typedef struct I256 { BlockVec mid; } I256; -static inline I256 -XOR256(const I256 a, const I256 b) +static inline I256 __vectorcall XOR256(const I256 a, const I256 b) { return (I256) { SODIUM_C99(.hi =) XOR128(a.hi, b.hi), @@ -106,8 +109,7 @@ typedef struct State { Precomp hx[PC_COUNT]; } State; -static void -expand256(const unsigned char key[KEYBYTES], BlockVec rkeys[1 + ROUNDS]) +static void __vectorcall expand256(const unsigned char key[KEYBYTES], BlockVec rkeys[1 + ROUNDS]) { BlockVec t1, t2, s; size_t i = 0; @@ -165,9 +167,9 @@ encrypt(const State *st, unsigned char dst[16], const unsigned char src[16]) /* Encrypt and add a single AES block */ -static inline void -encrypt_xor_block(const State *st, unsigned char dst[16], const unsigned char src[16], - const BlockVec counter) +static inline void __vectorcall encrypt_xor_block(const State *st, unsigned char dst[16], + const unsigned char src[16], + const BlockVec counter) { BlockVec ts; size_t i; @@ -183,10 +185,10 @@ encrypt_xor_block(const State *st, unsigned char dst[16], const unsigned char sr /* Encrypt and add PARALLEL_BLOCKS AES blocks */ -static inline void -encrypt_xor_wide(const State *st, unsigned char dst[16 * PARALLEL_BLOCKS], - const unsigned char src[16 * PARALLEL_BLOCKS], - const BlockVec counters[PARALLEL_BLOCKS]) +static inline void __vectorcall encrypt_xor_wide(const State *st, + unsigned char dst[16 * PARALLEL_BLOCKS], + const unsigned char src[16 * PARALLEL_BLOCKS], + const BlockVec counters[PARALLEL_BLOCKS]) { BlockVec ts[PARALLEL_BLOCKS]; size_t i, j; @@ -210,8 +212,7 @@ encrypt_xor_wide(const State *st, unsigned char dst[16 * PARALLEL_BLOCKS], /* Square a field element */ -static inline I256 -clsq128(const BlockVec x) +static inline I256 __vectorcall clsq128(const BlockVec x) { const BlockVec x_hi = BYTESHR128(x, 8); const BlockVec mid = XOR128(x, x_hi); @@ -229,8 +230,7 @@ clsq128(const BlockVec x) /* Multiply two field elements -- Textbook multiplication is faster than Karatsuba on some recent * CPUs */ -static inline I256 -clmul128(const BlockVec x, const BlockVec y) +static inline I256 __vectorcall clmul128(const BlockVec x, const BlockVec y) { #ifdef USE_KARATSUBA_MULTIPLICATION const BlockVec x_hi = BYTESHR128(x, 8); @@ -259,8 +259,7 @@ clmul128(const BlockVec x, const BlockVec y) /* Merge the middle word and reduce a field element */ -static inline BlockVec -gcm_reduce(const I256 x) +static inline BlockVec __vectorcall gcm_reduce(const I256 x) { const BlockVec hi = XOR128(x.hi, BYTESHR128(x.mid, 8)); const BlockVec lo = XOR128(x.lo, BYTESHL128(x.mid, 8)); @@ -276,8 +275,7 @@ gcm_reduce(const I256 x) /* Precompute powers of H from `from` to `to` */ -static inline void -precomp(Precomp hx[PC_COUNT], const size_t from, const size_t to) +static inline void __vectorcall precomp(Precomp hx[PC_COUNT], const size_t from, const size_t to) { const Precomp h = hx[0]; size_t i; @@ -290,9 +288,9 @@ precomp(Precomp hx[PC_COUNT], const size_t from, const size_t to) /* Precompute powers of H given a key and a block count */ -static void -precomp_for_block_count(Precomp hx[PC_COUNT], const unsigned char gh_key[16], - const size_t block_count) +static void __vectorcall precomp_for_block_count(Precomp hx[PC_COUNT], + const unsigned char gh_key[16], + const size_t block_count) { const BlockVec h0 = REV128(LOAD128(gh_key)); BlockVec carry = SET64x2(0xc200000000000000, 1); @@ -392,8 +390,8 @@ gh_ad_blocks(const State *st, GHash *sth, const unsigned char *ad, size_t ad_len /* Increment counters */ -static inline BlockVec -incr_counters(BlockVec rev_counters[], BlockVec counter, const size_t n) +static inline BlockVec __vectorcall incr_counters(BlockVec rev_counters[], BlockVec counter, + const size_t n) { size_t i;