diff options
author | jsing <> | 2023-04-12 04:54:16 +0000 |
---|---|---|
committer | jsing <> | 2023-04-12 04:54:16 +0000 |
commit | 0639a12b364c61132014c0052e54345f2de59568 (patch) | |
tree | 71be9c2306d6ac3d5d004e512e05bf07782c26a6 /src/lib | |
parent | a9c434936ce2a17263afcfb92d37ece5fd9b1220 (diff) | |
download | openbsd-0639a12b364c61132014c0052e54345f2de59568.tar.gz openbsd-0639a12b364c61132014c0052e54345f2de59568.tar.bz2 openbsd-0639a12b364c61132014c0052e54345f2de59568.zip |
Provide and use crypto_ro{l,r}_u{32,64}().
Various code in libcrypto needs bitwise rotation - rather than defining
different versions across the code base, provide a common set that can
be reused. Any sensible compiler optimises these to a single instruction
where the architecture supports it, which means we can ditch the inline
assembly.
On the chance that we need to provide a platform specific versions, this
follows the approach used in BN where a MD crypto_arch.h header could be
added in the future, which would then provide more specific versions of
these functions.
ok tb@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/crypto_internal.h | 34 | ||||
-rw-r--r-- | src/lib/libcrypto/md32_common.h | 14 | ||||
-rw-r--r-- | src/lib/libcrypto/sha/sha512.c | 16 |
3 files changed, 39 insertions, 25 deletions
diff --git a/src/lib/libcrypto/crypto_internal.h b/src/lib/libcrypto/crypto_internal.h index af2a87216e..fa1dc504f7 100644 --- a/src/lib/libcrypto/crypto_internal.h +++ b/src/lib/libcrypto/crypto_internal.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: crypto_internal.h,v 1.1 2023/04/12 04:40:39 jsing Exp $ */ | 1 | /* $OpenBSD: crypto_internal.h,v 1.2 2023/04/12 04:54:15 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -31,4 +31,36 @@ crypto_store_htobe64(uint8_t *dst, uint64_t v) | |||
31 | } | 31 | } |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #ifndef HAVE_CRYPTO_ROL_U32 | ||
35 | static inline uint32_t | ||
36 | crypto_rol_u32(uint32_t v, size_t shift) | ||
37 | { | ||
38 | return (v << shift) | (v >> (32 - shift)); | ||
39 | } | ||
40 | #endif | ||
41 | |||
42 | #ifndef HAVE_CRYPTO_ROR_U32 | ||
43 | static inline uint32_t | ||
44 | crypto_ror_u32(uint32_t v, size_t shift) | ||
45 | { | ||
46 | return (v << (32 - shift)) | (v >> shift); | ||
47 | } | ||
48 | #endif | ||
49 | |||
50 | #ifndef HAVE_CRYPTO_ROL_U64 | ||
51 | static inline uint64_t | ||
52 | crypto_rol_u64(uint64_t v, size_t shift) | ||
53 | { | ||
54 | return (v << shift) | (v >> (64 - shift)); | ||
55 | } | ||
56 | #endif | ||
57 | |||
58 | #ifndef HAVE_CRYPTO_ROR_U64 | ||
59 | static inline uint64_t | ||
60 | crypto_ror_u64(uint64_t v, size_t shift) | ||
61 | { | ||
62 | return (v << (64 - shift)) | (v >> shift); | ||
63 | } | ||
64 | #endif | ||
65 | |||
34 | #endif | 66 | #endif |
diff --git a/src/lib/libcrypto/md32_common.h b/src/lib/libcrypto/md32_common.h index a8b0d9ab74..cce4cfb0f7 100644 --- a/src/lib/libcrypto/md32_common.h +++ b/src/lib/libcrypto/md32_common.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: md32_common.h,v 1.23 2022/12/26 07:18:50 jmc Exp $ */ | 1 | /* $OpenBSD: md32_common.h,v 1.24 2023/04/12 04:54:15 jsing Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -111,6 +111,8 @@ | |||
111 | 111 | ||
112 | #include <openssl/opensslconf.h> | 112 | #include <openssl/opensslconf.h> |
113 | 113 | ||
114 | #include "crypto_internal.h" | ||
115 | |||
114 | #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) | 116 | #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
115 | #error "DATA_ORDER must be defined!" | 117 | #error "DATA_ORDER must be defined!" |
116 | #endif | 118 | #endif |
@@ -139,15 +141,7 @@ | |||
139 | #error "HASH_BLOCK_DATA_ORDER must be defined!" | 141 | #error "HASH_BLOCK_DATA_ORDER must be defined!" |
140 | #endif | 142 | #endif |
141 | 143 | ||
142 | /* | 144 | #define ROTATE(a, n) crypto_rol_u32(a, n) |
143 | * This common idiom is recognized by the compiler and turned into a | ||
144 | * CPU-specific intrinsic as appropriate. | ||
145 | * e.g. GCC optimizes to roll on amd64 at -O0 | ||
146 | */ | ||
147 | static inline uint32_t ROTATE(uint32_t a, uint32_t n) | ||
148 | { | ||
149 | return (a<<n)|(a>>(32-n)); | ||
150 | } | ||
151 | 145 | ||
152 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 146 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
153 | 147 | ||
diff --git a/src/lib/libcrypto/sha/sha512.c b/src/lib/libcrypto/sha/sha512.c index 14c4cbd4f3..ff9ca889e0 100644 --- a/src/lib/libcrypto/sha/sha512.c +++ b/src/lib/libcrypto/sha/sha512.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sha512.c,v 1.31 2023/04/12 04:40:39 jsing Exp $ */ | 1 | /* $OpenBSD: sha512.c,v 1.32 2023/04/12 04:54:16 jsing Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -119,11 +119,6 @@ static const SHA_LONG64 K512[80] = { | |||
119 | 119 | ||
120 | #if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) | 120 | #if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) |
121 | # if defined(__x86_64) || defined(__x86_64__) | 121 | # if defined(__x86_64) || defined(__x86_64__) |
122 | # define ROTR(a, n) ({ SHA_LONG64 ret; \ | ||
123 | asm ("rorq %1,%0" \ | ||
124 | : "=r"(ret) \ | ||
125 | : "J"(n),"0"(a) \ | ||
126 | : "cc"); ret; }) | ||
127 | # define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x))); \ | 122 | # define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x))); \ |
128 | asm ("bswapq %0" \ | 123 | asm ("bswapq %0" \ |
129 | : "=r"(ret) \ | 124 | : "=r"(ret) \ |
@@ -135,11 +130,6 @@ static const SHA_LONG64 K512[80] = { | |||
135 | : "=r"(lo),"=r"(hi) \ | 130 | : "=r"(lo),"=r"(hi) \ |
136 | : "0"(lo),"1"(hi)); \ | 131 | : "0"(lo),"1"(hi)); \ |
137 | ((SHA_LONG64)hi)<<32|lo; }) | 132 | ((SHA_LONG64)hi)<<32|lo; }) |
138 | # elif (defined(_ARCH_PPC) && defined(__64BIT__)) || defined(_ARCH_PPC64) | ||
139 | # define ROTR(a, n) ({ SHA_LONG64 ret; \ | ||
140 | asm ("rotrdi %0,%1,%2" \ | ||
141 | : "=r"(ret) \ | ||
142 | : "r"(a),"K"(n)); ret; }) | ||
143 | # endif | 133 | # endif |
144 | #endif | 134 | #endif |
145 | 135 | ||
@@ -152,9 +142,7 @@ static const SHA_LONG64 K512[80] = { | |||
152 | #endif | 142 | #endif |
153 | #endif | 143 | #endif |
154 | 144 | ||
155 | #ifndef ROTR | 145 | #define ROTR(x, s) crypto_ror_u64(x, s) |
156 | #define ROTR(x, s) (((x)>>s) | (x)<<(64-s)) | ||
157 | #endif | ||
158 | 146 | ||
159 | #define Sigma0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39)) | 147 | #define Sigma0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39)) |
160 | #define Sigma1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41)) | 148 | #define Sigma1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41)) |