From eea820f3e239a4c11d618741fd5d00a6bc877347 Mon Sep 17 00:00:00 2001 From: "Hongren (Zenithal) Zheng" Date: Wed, 11 May 2022 17:18:27 +0800 Subject: [PATCH] Add ROTATE inline asm support for SM3 And move ROTATE inline asm to header. Now this benefits SM3, SHA (when with Zbb only and no Zknh) and other hash functions Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/18287) --- crypto/sm3/sm3_local.h | 16 ++++++++-------- include/crypto/md32_common.h | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/crypto/sm3/sm3_local.h b/crypto/sm3/sm3_local.h index 48ec9ae90b..cb5a187a12 100644 --- a/crypto/sm3/sm3_local.h +++ b/crypto/sm3/sm3_local.h @@ -57,14 +57,14 @@ void ossl_sm3_transform(SM3_CTX *c, const unsigned char *data); # if defined(__GNUC__) && __GNUC__>=2 && \ !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) # if defined(__riscv_zksh) -# define P0(x) ({ MD32_REG_T ret; \ - asm ("sm3p0 %0, %1" \ - : "=r"(ret) \ - : "r"(x)); ret; }) -# define P1(x) ({ MD32_REG_T ret; \ - asm ("sm3p1 %0, %1" \ - : "=r"(ret) \ - : "r"(x)); ret; }) +# define P0(x) ({ MD32_REG_T ret; \ + asm ("sm3p0 %0, %1" \ + : "=r"(ret) \ + : "r"(x)); ret; }) +# define P1(x) ({ MD32_REG_T ret; \ + asm ("sm3p1 %0, %1" \ + : "=r"(ret) \ + : "r"(x)); ret; }) # endif # endif #endif diff --git a/include/crypto/md32_common.h b/include/crypto/md32_common.h index 262dc6503f..46214f3237 100644 --- a/include/crypto/md32_common.h +++ b/include/crypto/md32_common.h @@ -99,6 +99,28 @@ # define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) +#ifndef PEDANTIC +# if defined(__GNUC__) && __GNUC__>=2 && \ + !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) +# if defined(__riscv_zbb) || defined(__riscv_zbkb) +# if __riscv_xlen == 64 +# undef ROTATE +# define ROTATE(x, n) ({ MD32_REG_T ret; \ + asm ("roriw %0, %1, %2" \ + : "=r"(ret) \ + : "r"(x), "i"(32 - (n))); ret;}) +# endif +# if __riscv_xlen == 32 +# undef ROTATE +# define ROTATE(x, n) ({ MD32_REG_T ret; \ + asm ("rori %0, %1, %2" \ + : "=r"(ret) \ + : "r"(x), "i"(32 - (n))); ret;}) +# endif +# endif +# endif +#endif + # if defined(DATA_ORDER_IS_BIG_ENDIAN) # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \