diff options
author | jsing <> | 2024-11-23 15:38:12 +0000 |
---|---|---|
committer | jsing <> | 2024-11-23 15:38:12 +0000 |
commit | ee65f9388e2e3a70bf26a70fa1747948f88269dc (patch) | |
tree | f8f8e8ce27814c367d48b98920c756126feb9c42 | |
parent | f69cab5db16bb210dd0d4ae2f7e6e80ea8b29503 (diff) | |
download | openbsd-ee65f9388e2e3a70bf26a70fa1747948f88269dc.tar.gz openbsd-ee65f9388e2e3a70bf26a70fa1747948f88269dc.tar.bz2 openbsd-ee65f9388e2e3a70bf26a70fa1747948f88269dc.zip |
Simplify endian handling in SHA-3.
Rather than having blocks of code that are conditional on
BYTE_ORDER != LITTLE_ENDIAN, use le64toh() and htole64() unconditionally.
In the case of a little endian platform, the compiler will optimise this
away, while on a big endian platform we'll either end up with better code
or the same code than we have currently.
ok tb@
-rw-r--r-- | src/lib/libcrypto/sha/sha3.c | 31 |
1 files changed, 5 insertions, 26 deletions
diff --git a/src/lib/libcrypto/sha/sha3.c b/src/lib/libcrypto/sha/sha3.c index b070d715ca..6a7196d582 100644 --- a/src/lib/libcrypto/sha/sha3.c +++ b/src/lib/libcrypto/sha/sha3.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sha3.c,v 1.15 2023/04/16 15:32:16 jsing Exp $ */ | 1 | /* $OpenBSD: sha3.c,v 1.16 2024/11/23 15:38:12 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * The MIT License (MIT) | 3 | * The MIT License (MIT) |
4 | * | 4 | * |
@@ -57,17 +57,8 @@ sha3_keccakf(uint64_t st[25]) | |||
57 | uint64_t t, bc[5]; | 57 | uint64_t t, bc[5]; |
58 | int i, j, r; | 58 | int i, j, r; |
59 | 59 | ||
60 | #if BYTE_ORDER != LITTLE_ENDIAN | 60 | for (i = 0; i < 25; i++) |
61 | uint8_t *v; | 61 | st[i] = le64toh(st[i]); |
62 | |||
63 | for (i = 0; i < 25; i++) { | ||
64 | v = (uint8_t *) &st[i]; | ||
65 | st[i] = ((uint64_t) v[0]) | (((uint64_t) v[1]) << 8) | | ||
66 | (((uint64_t) v[2]) << 16) | (((uint64_t) v[3]) << 24) | | ||
67 | (((uint64_t) v[4]) << 32) | (((uint64_t) v[5]) << 40) | | ||
68 | (((uint64_t) v[6]) << 48) | (((uint64_t) v[7]) << 56); | ||
69 | } | ||
70 | #endif | ||
71 | 62 | ||
72 | for (r = 0; r < KECCAKF_ROUNDS; r++) { | 63 | for (r = 0; r < KECCAKF_ROUNDS; r++) { |
73 | 64 | ||
@@ -102,20 +93,8 @@ sha3_keccakf(uint64_t st[25]) | |||
102 | st[0] ^= sha3_keccakf_rndc[r]; | 93 | st[0] ^= sha3_keccakf_rndc[r]; |
103 | } | 94 | } |
104 | 95 | ||
105 | #if BYTE_ORDER != LITTLE_ENDIAN | 96 | for (i = 0; i < 25; i++) |
106 | for (i = 0; i < 25; i++) { | 97 | st[i] = htole64(st[i]); |
107 | v = (uint8_t *) &st[i]; | ||
108 | t = st[i]; | ||
109 | v[0] = t & 0xFF; | ||
110 | v[1] = (t >> 8) & 0xFF; | ||
111 | v[2] = (t >> 16) & 0xFF; | ||
112 | v[3] = (t >> 24) & 0xFF; | ||
113 | v[4] = (t >> 32) & 0xFF; | ||
114 | v[5] = (t >> 40) & 0xFF; | ||
115 | v[6] = (t >> 48) & 0xFF; | ||
116 | v[7] = (t >> 56) & 0xFF; | ||
117 | } | ||
118 | #endif | ||
119 | } | 98 | } |
120 | 99 | ||
121 | int | 100 | int |