Skip to content

Commit b69482f

Browse files
authored
Merge pull request #7569 from SparkiDev/riscv_aes_asm
AES RISC-V 64-bit ASM: ECB/CBC/CTR/GCM/CCM
2 parents 7ce9ebd + acd604d commit b69482f

8 files changed

Lines changed: 9788 additions & 71 deletions

File tree

configure.ac

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,76 @@ if test "$ENABLED_ARMASM_INLINE" = "yes"; then
28662866
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ARMASM_INLINE"
28672867
fi
28682868

2869+
# RISC-V Assembly
2870+
AC_ARG_ENABLE([riscv-asm],
2871+
[AS_HELP_STRING([--enable-riscv-asm],[Enable wolfSSL RISC-V ASM support (default: disabled).])],
2872+
[ ENABLED_RISCV_ASM=$enableval ],
2873+
[ ENABLED_RISCV_ASM=no ]
2874+
)
2875+
if test "$ENABLED_RISCV_ASM" != "no" && test "$ENABLED_ASM" = "yes"
2876+
then
2877+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_ASM"
2878+
ENABLED_AESGCM_STREAM=no # not yet implemented
2879+
AC_MSG_NOTICE([64bit RISC-V assembly for AES])
2880+
fi
2881+
2882+
ENABLED_RISCV_ASM_OPTS=$ENABLED_RISCV_ASM
2883+
for v in `echo $ENABLED_RISCV_ASM_OPTS | tr "," " "`
2884+
do
2885+
case $v in
2886+
yes)
2887+
;;
2888+
no)
2889+
;;
2890+
zbkb)
2891+
# PACK, REV8
2892+
ENABLED_RISCV_ASM=yes
2893+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_BIT_MANIPULATION"
2894+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_BASE_BIT_MANIPULATION"
2895+
;;
2896+
zbb)
2897+
# REV8
2898+
ENABLED_RISCV_ASM=yes
2899+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_BASE_BIT_MANIPULATION"
2900+
;;
2901+
zbc|zbkc)
2902+
# CLMUL, CLMULH
2903+
ENABLED_RISCV_ASM=yes
2904+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_CARRYLESS"
2905+
;;
2906+
zkn|zkned)
2907+
# AES encrypt/decrpyt
2908+
ENABLED_RISCV_ASM=yes
2909+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_SCALAR_CRYPTO_ASM"
2910+
;;
2911+
zvkg)
2912+
# VGMUL, VHHSH
2913+
ENABLED_RISCV_ASM=yes
2914+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_VECTOR_GCM"
2915+
;;
2916+
zvbc)
2917+
# VCLMUL, VCLMULH
2918+
ENABLED_RISCV_ASM=yes
2919+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_VECTOR_CARRYLESS"
2920+
;;
2921+
zvbb|zvkb)
2922+
# VBREV8
2923+
ENABLED_RISCV_ASM=yes
2924+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_VECTOR_BASE_BIT_MANIPULATION"
2925+
;;
2926+
zvkned)
2927+
# Vector AES
2928+
ENABLED_RISCV_ASM=yes
2929+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_RISCV_VECTOR_CRYPTO_ASM"
2930+
;;
2931+
*)
2932+
AC_MSG_ERROR([Invalid RISC-V option [yes,zbkb,zbb,zbc,zbkc,zkn,zkned,zvkg,zvbc,zvbb,zvkb,zvkned]: $ENABLED_RISCV_ASM.])
2933+
break
2934+
;;
2935+
esac
2936+
done
2937+
2938+
28692939
# Xilinx hardened crypto
28702940
AC_ARG_ENABLE([xilinx],
28712941
[AS_HELP_STRING([--enable-xilinx],[Enable wolfSSL support for Xilinx hardened crypto(default: disabled)])],
@@ -8388,7 +8458,7 @@ if test "$ENABLED_LINUXKM_LKCAPI_REGISTER" != "none"
83888458
then
83898459
AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER"
83908460
8391-
if test "$ENABLED_AESGCM" != "no" && test "$ENABLED_AESGCM_STREAM" = "no" && test "$ENABLED_ARMASM" = "no" && test "$ENABLED_FIPS" = "no"; then
8461+
if test "$ENABLED_AESGCM" != "no" && test "$ENABLED_AESGCM_STREAM" = "no" && test "$ENABLED_ARMASM" = "no" && test "$ENABLED_RISCV_ASM" = "no" && test "$ENABLED_FIPS" = "no"; then
83928462
ENABLED_AESGCM_STREAM=yes
83938463
fi
83948464
@@ -9389,6 +9459,7 @@ AM_CONDITIONAL([BUILD_ARMASM],[test "x$ENABLED_ARMASM" = "xyes"])
93899459
AM_CONDITIONAL([BUILD_ARMASM_INLINE],[test "x$ENABLED_ARMASM_INLINE" = "xyes"])
93909460
AM_CONDITIONAL([BUILD_ARMASM_CRYPTO],[test "x$ENABLED_ARMASM_CRYPTO" = "xyes"])
93919461
AM_CONDITIONAL([BUILD_ARMASM_NEON],[test "x$ENABLED_ARMASM_NEON" = "xyes"])
9462+
AM_CONDITIONAL([BUILD_RISCV_ASM],[test "x$ENABLED_RISCV_ASM" = "xyes"])
93929463
AM_CONDITIONAL([BUILD_XILINX],[test "x$ENABLED_XILINX" = "xyes"])
93939464
AM_CONDITIONAL([BUILD_AESNI],[test "x$ENABLED_AESNI" = "xyes"])
93949465
AM_CONDITIONAL([BUILD_INTELASM],[test "x$ENABLED_INTELASM" = "xyes"])
@@ -10024,6 +10095,7 @@ fi
1002410095
echo " * ARM ASM: $ENABLED_ARMASM"
1002510096
echo " * ARM ASM SHA512/SHA3 Crypto $ENABLED_ARMASM_SHA3"
1002610097
echo " * ARM ASM SM3/SM4 Crypto $ENABLED_ARMASM_CRYPTO_SM4"
10098+
echo " * RISC-V ASM $ENABLED_RISCV_ASM"
1002710099
echo " * Write duplicate: $ENABLED_WRITEDUP"
1002810100
echo " * Xilinx Hardware Acc.: $ENABLED_XILINX"
1002910101
echo " * Inline Code: $ENABLED_INLINE"

src/include.am

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/arm/thumb2-aes-asm.S
182182
endif !BUILD_ARMASM_INLINE
183183
endif BUILD_ARMASM
184184
endif !BUILD_ARMASM_NEON
185-
endif BUILD_AES
186185

187186
if BUILD_AESNI
188187
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/aes_asm.S
@@ -194,6 +193,11 @@ src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/aes_xts_asm.S
194193
endif
195194
endif
196195

196+
if BUILD_RISCV_ASM
197+
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/riscv/riscv-64-aes.c
198+
endif BUILD_RISCV_ASM
199+
endif BUILD_AES
200+
197201
if BUILD_SHA
198202
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/sha.c
199203
endif
@@ -672,9 +676,14 @@ src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/arm/thumb2-aes-asm.S
672676
endif !BUILD_ARMASM_INLINE
673677
endif BUILD_ARMASM
674678
endif !BUILD_ARMASM_NEON
679+
675680
if BUILD_AFALG
676681
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/af_alg/afalg_aes.c
677682
endif BUILD_AFALG
683+
684+
if BUILD_RISCV_ASM
685+
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/riscv/riscv-64-aes.c
686+
endif BUILD_RISCV_ASM
678687
endif BUILD_AES
679688
endif !BUILD_FIPS_CURRENT
680689

wolfcrypt/benchmark/benchmark.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,9 @@ static word32 bench_size = BENCH_SIZE;
19721972
static int base2 = 1;
19731973
static int digest_stream = 1;
19741974
static int encrypt_only = 0;
1975+
#ifdef HAVE_AES_CBC
1976+
static int cipher_same_buffer = 0;
1977+
#endif
19751978

19761979
#ifdef MULTI_VALUE_STATISTICS
19771980
static int minimum_runs = 0;
@@ -4138,6 +4141,8 @@ static void bench_aescbc_internal(int useDeviceID,
41384141
const byte* iv, const char* encLabel,
41394142
const char* decLabel)
41404143
{
4144+
const byte* in = bench_cipher;
4145+
byte* out = bench_plain;
41414146
int ret = 0, i, count = 0, times, pending = 0;
41424147
WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
41434148
sizeof(Aes), HEAP_HINT);
@@ -4162,6 +4167,10 @@ static void bench_aescbc_internal(int useDeviceID,
41624167
}
41634168
}
41644169

4170+
if (cipher_same_buffer) {
4171+
in = bench_plain;
4172+
}
4173+
41654174
bench_stats_start(&count, &start);
41664175
do {
41674176
for (times = 0; times < numBlocks || pending > 0; ) {
@@ -4171,8 +4180,7 @@ static void bench_aescbc_internal(int useDeviceID,
41714180
for (i = 0; i < BENCH_MAX_PENDING; i++) {
41724181
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
41734182
&times, numBlocks, &pending)) {
4174-
ret = wc_AesCbcEncrypt(enc[i], bench_plain, bench_cipher,
4175-
bench_size);
4183+
ret = wc_AesCbcEncrypt(enc[i], out, in, bench_size);
41764184

41774185
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
41784186
0, &times, &pending)) {
@@ -4221,8 +4229,7 @@ static void bench_aescbc_internal(int useDeviceID,
42214229
for (i = 0; i < BENCH_MAX_PENDING; i++) {
42224230
if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
42234231
&times, numBlocks, &pending)) {
4224-
ret = wc_AesCbcDecrypt(enc[i], bench_cipher, bench_plain,
4225-
bench_size);
4232+
ret = wc_AesCbcDecrypt(enc[i], out, in, bench_size);
42264233

42274234
if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
42284235
0, &times, &pending)) {
@@ -4860,6 +4867,8 @@ static void bench_aesecb_internal(int useDeviceID,
48604867

48614868
#endif /* HAVE_AES_DECRYPT */
48624869

4870+
(void)decLabel;
4871+
48634872
exit:
48644873

48654874
if (WC_ARRAY_OK(enc)) {

wolfcrypt/src/aes.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
9595
#include <wolfcrypt/src/misc.c>
9696
#endif
9797

98-
#ifndef WOLFSSL_ARMASM
98+
#if !defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_RISCV_ASM)
9999

100100
#ifdef WOLFSSL_IMX6_CAAM_BLOB
101101
/* case of possibly not using hardware acceleration for AES but using key
@@ -967,6 +967,9 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
967967
#elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_AES)
968968
/* implemented in wolfcrypt/src/port/psa/psa_aes.c */
969969

970+
#elif defined(WOLFSSL_RISCV_ASM)
971+
/* implemented in wolfcrypt/src/port/risc-v/riscv-64-aes.c */
972+
970973
#else
971974

972975
/* using wolfCrypt software implementation */
@@ -4317,6 +4320,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
43174320

43184321
#endif /* NEED_AES_TABLES */
43194322

4323+
#ifndef WOLFSSL_RISCV_ASM
43204324
/* Software AES - SetKey */
43214325
static WARN_UNUSED_RESULT int wc_AesSetKeyLocal(
43224326
Aes* aes, const byte* userKey, word32 keylen, const byte* iv, int dir,
@@ -4630,6 +4634,7 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
46304634
return wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir, 1);
46314635

46324636
} /* wc_AesSetKey() */
4637+
#endif
46334638

46344639
#if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
46354640
/* AES-CTR and AES-DIRECT need to use this for key setup */
@@ -6171,7 +6176,7 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
61716176
#endif /* NEED_AES_CTR_SOFT */
61726177

61736178
#endif /* WOLFSSL_AES_COUNTER */
6174-
#endif /* !WOLFSSL_ARMASM */
6179+
#endif /* !WOLFSSL_ARMASM && ! WOLFSSL_RISCV_ASM */
61756180

61766181

61776182
/*
@@ -6221,6 +6226,9 @@ static WC_INLINE void IncCtr(byte* ctr, word32 ctrSz)
62216226
#ifdef WOLFSSL_ARMASM
62226227
/* implementation is located in wolfcrypt/src/port/arm/armv8-aes.c */
62236228

6229+
#elif defined(WOLFSSL_RISCV_ASM)
6230+
/* implemented in wolfcrypt/src/port/risc-v/riscv-64-aes.c */
6231+
62246232
#elif defined(WOLFSSL_AFALG)
62256233
/* implemented in wolfcrypt/src/port/afalg/afalg_aes.c */
62266234

@@ -10478,6 +10486,9 @@ int wc_AesCcmCheckTagSize(int sz)
1047810486
#ifdef WOLFSSL_ARMASM
1047910487
/* implementation located in wolfcrypt/src/port/arm/armv8-aes.c */
1048010488

10489+
#elif defined(WOLFSSL_RISCV_ASM)
10490+
/* implementation located in wolfcrypt/src/port/risc-v/riscv-64-aes.c */
10491+
1048110492
#elif defined(HAVE_COLDFIRE_SEC)
1048210493
#error "Coldfire SEC doesn't currently support AES-CCM mode"
1048310494

@@ -11375,6 +11386,9 @@ int wc_AesGetKeySize(Aes* aes, word32* keySize)
1137511386
#elif defined(WOLFSSL_DEVCRYPTO_AES)
1137611387
/* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
1137711388

11389+
#elif defined(WOLFSSL_RISCV_ASM)
11390+
/* implemented in wolfcrypt/src/port/riscv/riscv-64-aes.c */
11391+
1137811392
#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
1137911393

1138011394
/* Software AES - ECB */
@@ -12613,12 +12627,21 @@ static WARN_UNUSED_RESULT int _AesXtsHelper(
1261312627
}
1261412628

1261512629
xorbuf(out, in, totalSz);
12630+
#ifndef WOLFSSL_RISCV_ASM
1261612631
if (dir == AES_ENCRYPTION) {
1261712632
return _AesEcbEncrypt(aes, out, out, totalSz);
1261812633
}
1261912634
else {
1262012635
return _AesEcbDecrypt(aes, out, out, totalSz);
1262112636
}
12637+
#else
12638+
if (dir == AES_ENCRYPTION) {
12639+
return wc_AesEcbEncrypt(aes, out, out, totalSz);
12640+
}
12641+
else {
12642+
return wc_AesEcbDecrypt(aes, out, out, totalSz);
12643+
}
12644+
#endif
1262212645
}
1262312646
#endif /* HAVE_AES_ECB */
1262412647

0 commit comments

Comments
 (0)