summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2025-05-18 07:26:09 +0000
committerjsing <>2025-05-18 07:26:09 +0000
commit3309fb148483f475d601ff252f5597f60caa434b (patch)
tree4923bc5f954fbefad174d25020db079051115017 /src
parentbc1afd923fa7ed3bad6afe202345ff452445115b (diff)
downloadopenbsd-3309fb148483f475d601ff252f5597f60caa434b.tar.gz
openbsd-3309fb148483f475d601ff252f5597f60caa434b.tar.bz2
openbsd-3309fb148483f475d601ff252f5597f60caa434b.zip
Remove contortions with the rem_4bit table.
Instead of using size_t and a PACK macro, store the entries as uint16_t and then uncondtionally left shift 48 bits. This gives a small performance gain on some architectures and has the advantage of reducing the size of the table from 1024 bits to 256 bits. ok beck@ joshua@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/modes/gcm128.c37
1 files changed, 9 insertions, 28 deletions
diff --git a/src/lib/libcrypto/modes/gcm128.c b/src/lib/libcrypto/modes/gcm128.c
index 334e592aa5..ed7373d56e 100644
--- a/src/lib/libcrypto/modes/gcm128.c
+++ b/src/lib/libcrypto/modes/gcm128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gcm128.c,v 1.38 2025/05/18 07:13:48 jsing Exp $ */ 1/* $OpenBSD: gcm128.c,v 1.39 2025/05/18 07:26:09 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -55,8 +55,6 @@
55#include "crypto_internal.h" 55#include "crypto_internal.h"
56#include "modes_local.h" 56#include "modes_local.h"
57 57
58#define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
59
60static void 58static void
61gcm_init_4bit(u128 Htable[16], u64 H[2]) 59gcm_init_4bit(u128 Htable[16], u64 H[2])
62{ 60{
@@ -109,11 +107,10 @@ gcm_init_4bit(u128 Htable[16], u64 H[2])
109} 107}
110 108
111#ifndef GHASH_ASM 109#ifndef GHASH_ASM
112static const size_t rem_4bit[16] = { 110static const uint16_t rem_4bit[16] = {
113 PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460), 111 0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
114 PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0), 112 0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0,
115 PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560), 113};
116 PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0) };
117 114
118static void 115static void
119gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]) 116gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
@@ -133,11 +130,7 @@ gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
133 rem = (size_t)Z.lo & 0xf; 130 rem = (size_t)Z.lo & 0xf;
134 Z.lo = (Z.hi << 60)|(Z.lo >> 4); 131 Z.lo = (Z.hi << 60)|(Z.lo >> 4);
135 Z.hi = (Z.hi >> 4); 132 Z.hi = (Z.hi >> 4);
136#if SIZE_MAX == 0xffffffffffffffff 133 Z.hi ^= (u64)rem_4bit[rem] << 48;
137 Z.hi ^= rem_4bit[rem];
138#else
139 Z.hi ^= (u64)rem_4bit[rem] << 32;
140#endif
141 Z.hi ^= Htable[nhi].hi; 134 Z.hi ^= Htable[nhi].hi;
142 Z.lo ^= Htable[nhi].lo; 135 Z.lo ^= Htable[nhi].lo;
143 136
@@ -151,11 +144,7 @@ gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
151 rem = (size_t)Z.lo & 0xf; 144 rem = (size_t)Z.lo & 0xf;
152 Z.lo = (Z.hi << 60)|(Z.lo >> 4); 145 Z.lo = (Z.hi << 60)|(Z.lo >> 4);
153 Z.hi = (Z.hi >> 4); 146 Z.hi = (Z.hi >> 4);
154#if SIZE_MAX == 0xffffffffffffffff 147 Z.hi ^= (u64)rem_4bit[rem] << 48;
155 Z.hi ^= rem_4bit[rem];
156#else
157 Z.hi ^= (u64)rem_4bit[rem] << 32;
158#endif
159 Z.hi ^= Htable[nlo].hi; 148 Z.hi ^= Htable[nlo].hi;
160 Z.lo ^= Htable[nlo].lo; 149 Z.lo ^= Htable[nlo].lo;
161 } 150 }
@@ -194,11 +183,7 @@ gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
194 rem = (size_t)Z.lo & 0xf; 183 rem = (size_t)Z.lo & 0xf;
195 Z.lo = (Z.hi << 60)|(Z.lo >> 4); 184 Z.lo = (Z.hi << 60)|(Z.lo >> 4);
196 Z.hi = (Z.hi >> 4); 185 Z.hi = (Z.hi >> 4);
197#if SIZE_MAX == 0xffffffffffffffff 186 Z.hi ^= (u64)rem_4bit[rem] << 48;
198 Z.hi ^= rem_4bit[rem];
199#else
200 Z.hi ^= (u64)rem_4bit[rem] << 32;
201#endif
202 Z.hi ^= Htable[nhi].hi; 187 Z.hi ^= Htable[nhi].hi;
203 Z.lo ^= Htable[nhi].lo; 188 Z.lo ^= Htable[nhi].lo;
204 189
@@ -213,11 +198,7 @@ gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
213 rem = (size_t)Z.lo & 0xf; 198 rem = (size_t)Z.lo & 0xf;
214 Z.lo = (Z.hi << 60)|(Z.lo >> 4); 199 Z.lo = (Z.hi << 60)|(Z.lo >> 4);
215 Z.hi = (Z.hi >> 4); 200 Z.hi = (Z.hi >> 4);
216#if SIZE_MAX == 0xffffffffffffffff 201 Z.hi ^= (u64)rem_4bit[rem] << 48;
217 Z.hi ^= rem_4bit[rem];
218#else
219 Z.hi ^= (u64)rem_4bit[rem] << 32;
220#endif
221 Z.hi ^= Htable[nlo].hi; 202 Z.hi ^= Htable[nlo].hi;
222 Z.lo ^= Htable[nlo].lo; 203 Z.lo ^= Htable[nlo].lo;
223 } 204 }