]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/modes/gcm128.c
Copyright year updates
[thirdparty/openssl.git] / crypto / modes / gcm128.c
CommitLineData
4f22f405 1/*
da1c088f 2 * Copyright 2010-2023 The OpenSSL Project Authors. All Rights Reserved.
e7f5b1cd 3 *
81cae8ce 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
e7f5b1cd
AP
8 */
9
e7f5b1cd 10#include <string.h>
459b15d4 11#include <openssl/crypto.h>
24fd8541 12#include "internal/cryptlib.h"
e23d850f 13#include "internal/endian.h"
25f2138b 14#include "crypto/modes.h"
e7f5b1cd 15
77286fe3
BE
16#if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
17typedef size_t size_t_aX __attribute((__aligned__(1)));
18#else
19typedef size_t size_t_aX;
20#endif
21
f472ec8c
AP
22#if defined(BSWAP4) && defined(STRICT_ALIGNMENT)
23/* redefine, because alignment is ensured */
0f113f3e
MC
24# undef GETU32
25# define GETU32(p) BSWAP4(*(const u32 *)(p))
26# undef PUTU32
27# define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
28#endif
29
f3fed0d5 30/* RISC-V uses C implementation as a fallback. */
999376dc
HB
31#if defined(__riscv)
32# define INCLUDE_C_GMULT_4BIT
f3fed0d5 33# define INCLUDE_C_GHASH_4BIT
999376dc
HB
34#endif
35
0f113f3e
MC
36#define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
37#define REDUCE1BIT(V) do { \
38 if (sizeof(size_t)==8) { \
39 u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \
40 V.lo = (V.hi<<63)|(V.lo>>1); \
41 V.hi = (V.hi>>1 )^T; \
42 } \
43 else { \
44 u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \
45 V.lo = (V.hi<<63)|(V.lo>>1); \
46 V.hi = (V.hi>>1 )^((u64)T<<32); \
47 } \
c1f092d1
AP
48} while(0)
49
1d97c843 50/*-
7b6e19fc 51 *
eb4129e1 52 * NOTE: TABLE_BITS and all non-4bit implementations have been removed in 3.1.
7b6e19fc 53 *
d8d95832
AP
54 * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
55 * never be set to 8. 8 is effectively reserved for testing purposes.
56 * TABLE_BITS>1 are lookup-table-driven implementations referred to as
57 * "Shoup's" in GCM specification. In other words OpenSSL does not cover
58 * whole spectrum of possible table driven implementations. Why? In
59 * non-"Shoup's" case memory access pattern is segmented in such manner,
60 * that it's trivial to see that cache timing information can reveal
61 * fair portion of intermediate hash value. Given that ciphertext is
62 * always available to attacker, it's possible for him to attempt to
63 * deduce secret parameter H and if successful, tamper with messages
64 * [which is nothing but trivial in CTR mode]. In "Shoup's" case it's
65 * not as trivial, but there is no reason to believe that it's resistant
66 * to cache-timing attack. And the thing about "8-bit" implementation is
67 * that it consumes 16 (sixteen) times more memory, 4KB per individual
68 * key + 1KB shared. Well, on pros side it should be twice as fast as
69 * "4-bit" version. And for gcc-generated x86[_64] code, "8-bit" version
70 * was observed to run ~75% faster, closer to 100% for commercial
71 * compilers... Yet "4-bit" procedure is preferred, because it's
72 * believed to provide better security-performance balance and adequate
73 * all-round performance. "All-round" refers to things like:
74 *
75 * - shorter setup time effectively improves overall timing for
76 * handling short messages;
77 * - larger table allocation can become unbearable because of VM
78 * subsystem penalties (for example on Windows large enough free
79 * results in VM working set trimming, meaning that consequent
80 * malloc would immediately incur working set expansion);
81 * - larger table has larger cache footprint, which can affect
82 * performance of other code paths (not necessarily even from same
83 * thread in Hyper-Threading world);
84 *
85 * Value of 1 is not appropriate for performance reasons.
86 */
a595baff 87
92c9086e 88static void gcm_init_4bit(u128 Htable[16], const u64 H[2])
e7f5b1cd 89{
0f113f3e
MC
90 u128 V;
91# if defined(OPENSSL_SMALL_FOOTPRINT)
92 int i;
93# endif
e7f5b1cd 94
0f113f3e
MC
95 Htable[0].hi = 0;
96 Htable[0].lo = 0;
97 V.hi = H[0];
98 V.lo = H[1];
99
100# if defined(OPENSSL_SMALL_FOOTPRINT)
101 for (Htable[8] = V, i = 4; i > 0; i >>= 1) {
102 REDUCE1BIT(V);
103 Htable[i] = V;
104 }
105
106 for (i = 2; i < 16; i <<= 1) {
107 u128 *Hi = Htable + i;
108 int j;
109 for (V = *Hi, j = 1; j < i; ++j) {
110 Hi[j].hi = V.hi ^ Htable[j].hi;
111 Hi[j].lo = V.lo ^ Htable[j].lo;
112 }
113 }
114# else
115 Htable[8] = V;
116 REDUCE1BIT(V);
117 Htable[4] = V;
118 REDUCE1BIT(V);
119 Htable[2] = V;
120 REDUCE1BIT(V);
121 Htable[1] = V;
122 Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
123 V = Htable[4];
124 Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
125 Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
126 Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
127 V = Htable[8];
128 Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
129 Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
130 Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
131 Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
132 Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
133 Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
134 Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
135# endif
136# if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm))
137 /*
138 * ARM assembler expects specific dword order in Htable.
139 */
140 {
141 int j;
e23d850f 142 DECLARE_IS_ENDIAN;
0f113f3e 143
e23d850f 144 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
145 for (j = 0; j < 16; ++j) {
146 V = Htable[j];
147 Htable[j].hi = V.lo;
148 Htable[j].lo = V.hi;
149 } else
150 for (j = 0; j < 16; ++j) {
151 V = Htable[j];
152 Htable[j].hi = V.lo << 32 | V.lo >> 32;
153 Htable[j].lo = V.hi << 32 | V.hi >> 32;
154 }
155 }
156# endif
e7f5b1cd
AP
157}
158
999376dc 159# if !defined(GHASH_ASM) || defined(INCLUDE_C_GMULT_4BIT)
2262beef 160static const size_t rem_4bit[16] = {
0f113f3e
MC
161 PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
162 PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
163 PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
164 PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)
165};
2262beef 166
4f39edbf 167static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
e7f5b1cd 168{
0f113f3e
MC
169 u128 Z;
170 int cnt = 15;
171 size_t rem, nlo, nhi;
e23d850f 172 DECLARE_IS_ENDIAN;
0f113f3e
MC
173
174 nlo = ((const u8 *)Xi)[15];
175 nhi = nlo >> 4;
176 nlo &= 0xf;
177
178 Z.hi = Htable[nlo].hi;
179 Z.lo = Htable[nlo].lo;
180
181 while (1) {
182 rem = (size_t)Z.lo & 0xf;
183 Z.lo = (Z.hi << 60) | (Z.lo >> 4);
184 Z.hi = (Z.hi >> 4);
185 if (sizeof(size_t) == 8)
186 Z.hi ^= rem_4bit[rem];
187 else
188 Z.hi ^= (u64)rem_4bit[rem] << 32;
189
190 Z.hi ^= Htable[nhi].hi;
191 Z.lo ^= Htable[nhi].lo;
192
193 if (--cnt < 0)
194 break;
195
196 nlo = ((const u8 *)Xi)[cnt];
197 nhi = nlo >> 4;
198 nlo &= 0xf;
199
200 rem = (size_t)Z.lo & 0xf;
201 Z.lo = (Z.hi << 60) | (Z.lo >> 4);
202 Z.hi = (Z.hi >> 4);
203 if (sizeof(size_t) == 8)
204 Z.hi ^= rem_4bit[rem];
205 else
206 Z.hi ^= (u64)rem_4bit[rem] << 32;
207
208 Z.hi ^= Htable[nlo].hi;
209 Z.lo ^= Htable[nlo].lo;
210 }
211
e23d850f 212 if (IS_LITTLE_ENDIAN) {
0f113f3e
MC
213# ifdef BSWAP8
214 Xi[0] = BSWAP8(Z.hi);
215 Xi[1] = BSWAP8(Z.lo);
216# else
217 u8 *p = (u8 *)Xi;
218 u32 v;
219 v = (u32)(Z.hi >> 32);
220 PUTU32(p, v);
221 v = (u32)(Z.hi);
222 PUTU32(p + 4, v);
223 v = (u32)(Z.lo >> 32);
224 PUTU32(p + 8, v);
225 v = (u32)(Z.lo);
226 PUTU32(p + 12, v);
227# endif
228 } else {
229 Xi[0] = Z.hi;
230 Xi[1] = Z.lo;
231 }
2262beef
AP
232}
233
999376dc
HB
234# endif
235
f3fed0d5 236# if !defined(GHASH_ASM) || defined(INCLUDE_C_GHASH_4BIT)
0f113f3e 237# if !defined(OPENSSL_SMALL_FOOTPRINT)
2262beef
AP
238/*
239 * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
a595baff
AP
240 * details... Compiler-generated code doesn't seem to give any
241 * performance improvement, at least not on x86[_64]. It's here
242 * mostly as reference and a placeholder for possible future
243 * non-trivial optimization[s]...
2262beef 244 */
0f113f3e
MC
245static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
246 const u8 *inp, size_t len)
2262beef
AP
247{
248 u128 Z;
249 int cnt;
250 size_t rem, nlo, nhi;
e23d850f 251 DECLARE_IS_ENDIAN;
0f113f3e 252
2262beef 253 do {
0f113f3e
MC
254 cnt = 15;
255 nlo = ((const u8 *)Xi)[15];
256 nlo ^= inp[15];
257 nhi = nlo >> 4;
258 nlo &= 0xf;
259
260 Z.hi = Htable[nlo].hi;
261 Z.lo = Htable[nlo].lo;
262
263 while (1) {
264 rem = (size_t)Z.lo & 0xf;
265 Z.lo = (Z.hi << 60) | (Z.lo >> 4);
266 Z.hi = (Z.hi >> 4);
267 if (sizeof(size_t) == 8)
268 Z.hi ^= rem_4bit[rem];
269 else
270 Z.hi ^= (u64)rem_4bit[rem] << 32;
271
272 Z.hi ^= Htable[nhi].hi;
273 Z.lo ^= Htable[nhi].lo;
274
275 if (--cnt < 0)
276 break;
277
278 nlo = ((const u8 *)Xi)[cnt];
279 nlo ^= inp[cnt];
280 nhi = nlo >> 4;
281 nlo &= 0xf;
282
283 rem = (size_t)Z.lo & 0xf;
284 Z.lo = (Z.hi << 60) | (Z.lo >> 4);
285 Z.hi = (Z.hi >> 4);
286 if (sizeof(size_t) == 8)
287 Z.hi ^= rem_4bit[rem];
288 else
289 Z.hi ^= (u64)rem_4bit[rem] << 32;
290
291 Z.hi ^= Htable[nlo].hi;
292 Z.lo ^= Htable[nlo].lo;
293 }
e7f5b1cd 294
e23d850f 295 if (IS_LITTLE_ENDIAN) {
0f113f3e
MC
296# ifdef BSWAP8
297 Xi[0] = BSWAP8(Z.hi);
298 Xi[1] = BSWAP8(Z.lo);
299# else
300 u8 *p = (u8 *)Xi;
301 u32 v;
302 v = (u32)(Z.hi >> 32);
303 PUTU32(p, v);
304 v = (u32)(Z.hi);
305 PUTU32(p + 4, v);
306 v = (u32)(Z.lo >> 32);
307 PUTU32(p + 8, v);
308 v = (u32)(Z.lo);
309 PUTU32(p + 12, v);
310# endif
311 } else {
312 Xi[0] = Z.hi;
313 Xi[1] = Z.lo;
314 }
36c269c3
DF
315
316 inp += 16;
317 /* Block size is 128 bits so len is a multiple of 16 */
318 len -= 16;
319 } while (len > 0);
e7f5b1cd 320}
0f113f3e
MC
321# endif
322# else
323void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]);
324void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
325 size_t len);
326# endif
2262beef 327
d50e0934 328# define GCM_MUL(ctx) ctx->funcs.gmult(ctx->Xi.u,ctx->Htable)
0f113f3e 329# if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT)
95201ef4 330# define GHASH(ctx,in,len) ctx->funcs.ghash((ctx)->Xi.u,(ctx)->Htable,in,len)
0f113f3e
MC
331/*
332 * GHASH_CHUNK is "stride parameter" missioned to mitigate cache trashing
333 * effect. In other words idea is to hash data while it's still in L1 cache
334 * after encryption pass...
335 */
336# define GHASH_CHUNK (3*1024)
337# endif
2262beef 338
7b6e19fc 339#if (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ))
0f113f3e
MC
340# if !defined(I386_ONLY) && \
341 (defined(__i386) || defined(__i386__) || \
342 defined(__x86_64) || defined(__x86_64__) || \
343 defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
1e863180 344# define GHASH_ASM_X86_OR_64
c1f092d1 345
0f113f3e
MC
346void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]);
347void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]);
348void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp,
349 size_t len);
c1f092d1 350
0f113f3e
MC
351# if defined(__i386) || defined(__i386__) || defined(_M_IX86)
352# define gcm_init_avx gcm_init_clmul
353# define gcm_gmult_avx gcm_gmult_clmul
354# define gcm_ghash_avx gcm_ghash_clmul
355# else
356void gcm_init_avx(u128 Htable[16], const u64 Xi[2]);
357void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]);
358void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
359 size_t len);
360# endif
1da5d302 361
0f113f3e 362# if defined(__i386) || defined(__i386__) || defined(_M_IX86)
1e863180 363# define GHASH_ASM_X86
0f113f3e
MC
364void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]);
365void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
366 size_t len);
c1f092d1 367
0f113f3e
MC
368void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]);
369void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp,
370 size_t len);
1e863180 371# endif
82741e9c 372# elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
1e863180 373# include "arm_arch.h"
c1669e1c 374# if __ARM_MAX_ARCH__>=7
1e863180 375# define GHASH_ASM_ARM
0f113f3e 376# define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL)
82741e9c 377# if defined(__arm__) || defined(__arm)
0f113f3e 378# define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
82741e9c 379# endif
0f113f3e
MC
380void gcm_init_neon(u128 Htable[16], const u64 Xi[2]);
381void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]);
382void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp,
383 size_t len);
384void gcm_init_v8(u128 Htable[16], const u64 Xi[2]);
385void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]);
386void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
387 size_t len);
1e863180 388# endif
23328d4b 389# elif defined(__sparc__) || defined(__sparc)
52f7e44e 390# include "crypto/sparc_arch.h"
23328d4b 391# define GHASH_ASM_SPARC
0f113f3e
MC
392void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]);
393void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]);
394void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp,
395 size_t len);
396# elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
3d178db7 397# include "crypto/ppc_arch.h"
0e716d92 398# define GHASH_ASM_PPC
0f113f3e
MC
399void gcm_init_p8(u128 Htable[16], const u64 Xi[2]);
400void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]);
401void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
402 size_t len);
999376dc
HB
403# elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
404# include "crypto/riscv_arch.h"
f3fed0d5 405# define GHASH_ASM_RV64I
b2468436
CM
406/* Zbc/Zbkc (scalar crypto with clmul) based routines. */
407void gcm_init_rv64i_zbc(u128 Htable[16], const u64 Xi[2]);
408void gcm_init_rv64i_zbc__zbb(u128 Htable[16], const u64 Xi[2]);
409void gcm_init_rv64i_zbc__zbkb(u128 Htable[16], const u64 Xi[2]);
410void gcm_gmult_rv64i_zbc(u64 Xi[2], const u128 Htable[16]);
411void gcm_gmult_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16]);
f3fed0d5
CM
412void gcm_ghash_rv64i_zbc(u64 Xi[2], const u128 Htable[16],
413 const u8 *inp, size_t len);
414void gcm_ghash_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16],
415 const u8 *inp, size_t len);
c1f092d1 416# endif
c1f092d1
AP
417#endif
418
92c9086e 419static void gcm_get_funcs(struct gcm_funcs_st *ctx)
e7f5b1cd 420{
92c9086e
TS
421 /* set defaults -- overridden below as needed */
422 ctx->ginit = gcm_init_4bit;
f3fed0d5 423#if !defined(GHASH_ASM)
92c9086e 424 ctx->gmult = gcm_gmult_4bit;
e7f5b1cd 425#else
92c9086e 426 ctx->gmult = NULL;
e7f5b1cd 427#endif
92c9086e
TS
428#if !defined(GHASH_ASM) && !defined(OPENSSL_SMALL_FOOTPRINT)
429 ctx->ghash = gcm_ghash_4bit;
7b6e19fc 430#else
92c9086e 431 ctx->ghash = NULL;
7b6e19fc 432#endif
92c9086e
TS
433
434#if defined(GHASH_ASM_X86_OR_64)
435# if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
436 /* x86_64 */
6e5a853b 437 if (OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */
0f113f3e 438 if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
92c9086e 439 ctx->ginit = gcm_init_avx;
0f113f3e 440 ctx->gmult = gcm_gmult_avx;
92c9086e 441 ctx->ghash = gcm_ghash_avx;
0f113f3e 442 } else {
92c9086e 443 ctx->ginit = gcm_init_clmul;
0f113f3e 444 ctx->gmult = gcm_gmult_clmul;
92c9086e 445 ctx->ghash = gcm_ghash_clmul;
0f113f3e
MC
446 }
447 return;
448 }
7b6e19fc 449# endif
92c9086e
TS
450# if defined(GHASH_ASM_X86)
451 /* x86 only */
452# if defined(OPENSSL_IA32_SSE2)
0f113f3e 453 if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
92c9086e
TS
454 ctx->gmult = gcm_gmult_4bit_mmx;
455 ctx->ghash = gcm_ghash_4bit_mmx;
456 return;
457 }
7b6e19fc 458# else
0f113f3e 459 if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */
0f113f3e 460 ctx->gmult = gcm_gmult_4bit_mmx;
92c9086e
TS
461 ctx->ghash = gcm_ghash_4bit_mmx;
462 return;
0f113f3e 463 }
92c9086e
TS
464# endif
465 ctx->gmult = gcm_gmult_4bit_x86;
466 ctx->ghash = gcm_ghash_4bit_x86;
467 return;
be0161ff
TM
468# else
469 /* x86_64 fallback defaults */
470 ctx->gmult = gcm_gmult_4bit;
471 ctx->ghash = gcm_ghash_4bit;
472 return;
7b6e19fc 473# endif
92c9086e 474#elif defined(GHASH_ASM_ARM)
186be8ed
TM
475 /* ARM defaults */
476 ctx->gmult = gcm_gmult_4bit;
477 ctx->ghash = gcm_ghash_4bit;
7b6e19fc 478# ifdef PMULL_CAPABLE
0f113f3e 479 if (PMULL_CAPABLE) {
92c9086e 480 ctx->ginit = (gcm_init_fn)gcm_init_v8;
0f113f3e 481 ctx->gmult = gcm_gmult_v8;
92c9086e
TS
482 ctx->ghash = gcm_ghash_v8;
483 }
484# elif defined(NEON_CAPABLE)
0f113f3e 485 if (NEON_CAPABLE) {
92c9086e 486 ctx->ginit = gcm_init_neon;
0f113f3e 487 ctx->gmult = gcm_gmult_neon;
92c9086e 488 ctx->ghash = gcm_ghash_neon;
0f113f3e 489 }
92c9086e
TS
490# endif
491 return;
492#elif defined(GHASH_ASM_SPARC)
186be8ed
TM
493 /* SPARC defaults */
494 ctx->gmult = gcm_gmult_4bit;
495 ctx->ghash = gcm_ghash_4bit;
0f113f3e 496 if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) {
92c9086e 497 ctx->ginit = gcm_init_vis3;
0f113f3e 498 ctx->gmult = gcm_gmult_vis3;
92c9086e 499 ctx->ghash = gcm_ghash_vis3;
0f113f3e 500 }
92c9086e
TS
501 return;
502#elif defined(GHASH_ASM_PPC)
186be8ed 503 /* PowerPC does not define GHASH_ASM; defaults set above */
0f113f3e 504 if (OPENSSL_ppccap_P & PPC_CRYPTO207) {
92c9086e 505 ctx->ginit = gcm_init_p8;
0f113f3e 506 ctx->gmult = gcm_gmult_p8;
92c9086e 507 ctx->ghash = gcm_ghash_p8;
0f113f3e 508 }
92c9086e 509 return;
f3fed0d5
CM
510#elif defined(GHASH_ASM_RV64I)
511 /* RISCV defaults */
512 ctx->gmult = gcm_gmult_4bit;
513 ctx->ghash = gcm_ghash_4bit;
514
b2468436
CM
515 if (RISCV_HAS_ZBC()) {
516 if (RISCV_HAS_ZBKB()) {
517 ctx->ginit = gcm_init_rv64i_zbc__zbkb;
518 ctx->gmult = gcm_gmult_rv64i_zbc__zbkb;
f3fed0d5 519 ctx->ghash = gcm_ghash_rv64i_zbc__zbkb;
b2468436
CM
520 } else if (RISCV_HAS_ZBB()) {
521 ctx->ginit = gcm_init_rv64i_zbc__zbb;
522 ctx->gmult = gcm_gmult_rv64i_zbc;
f3fed0d5 523 ctx->ghash = gcm_ghash_rv64i_zbc;
b2468436
CM
524 } else {
525 ctx->ginit = gcm_init_rv64i_zbc;
526 ctx->gmult = gcm_gmult_rv64i_zbc;
f3fed0d5 527 ctx->ghash = gcm_ghash_rv64i_zbc;
b2468436 528 }
999376dc 529 }
92c9086e 530 return;
186be8ed
TM
531#elif defined(GHASH_ASM)
532 /* all other architectures use the generic names */
48e35b99
JC
533 ctx->gmult = gcm_gmult_4bit;
534 ctx->ghash = gcm_ghash_4bit;
535 return;
536#endif
92c9086e
TS
537}
538
0113ec84
TS
539void ossl_gcm_init_4bit(u128 Htable[16], const u64 H[2])
540{
541 struct gcm_funcs_st funcs;
542
543 gcm_get_funcs(&funcs);
544 funcs.ginit(Htable, H);
545}
546
547void ossl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
548{
549 struct gcm_funcs_st funcs;
550
551 gcm_get_funcs(&funcs);
552 funcs.gmult(Xi, Htable);
553}
554
555void ossl_gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
556 const u8 *inp, size_t len)
557{
558 struct gcm_funcs_st funcs;
559 u64 tmp[2];
560 size_t i;
561
562 gcm_get_funcs(&funcs);
563 if (funcs.ghash != NULL) {
564 funcs.ghash(Xi, Htable, inp, len);
565 } else {
566 /* Emulate ghash if needed */
567 for (i = 0; i < len; i += 16) {
568 memcpy(tmp, &inp[i], sizeof(tmp));
569 Xi[0] ^= tmp[0];
570 Xi[1] ^= tmp[1];
571 funcs.gmult(Xi, Htable);
572 }
573 }
574}
575
92c9086e
TS
576void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
577{
578 DECLARE_IS_ENDIAN;
579
580 memset(ctx, 0, sizeof(*ctx));
581 ctx->block = block;
582 ctx->key = key;
583
584 (*block) (ctx->H.c, ctx->H.c, key);
585
586 if (IS_LITTLE_ENDIAN) {
587 /* H is stored in host byte order */
588#ifdef BSWAP8
589 ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
590 ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
7b6e19fc 591#else
92c9086e
TS
592 u8 *p = ctx->H.c;
593 u64 hi, lo;
594 hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
595 lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
596 ctx->H.u[0] = hi;
597 ctx->H.u[1] = lo;
a595baff 598#endif
92c9086e
TS
599 }
600
601 gcm_get_funcs(&ctx->funcs);
602 ctx->funcs.ginit(ctx->Htable, ctx->H.u);
e7f5b1cd
AP
603}
604
0f113f3e
MC
605void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
606 size_t len)
e7f5b1cd 607{
e23d850f 608 DECLARE_IS_ENDIAN;
0f113f3e 609 unsigned int ctr;
0f113f3e 610
0f113f3e
MC
611 ctx->len.u[0] = 0; /* AAD length */
612 ctx->len.u[1] = 0; /* message length */
613 ctx->ares = 0;
614 ctx->mres = 0;
615
616 if (len == 12) {
617 memcpy(ctx->Yi.c, iv, 12);
f5791af3
AP
618 ctx->Yi.c[12] = 0;
619 ctx->Yi.c[13] = 0;
620 ctx->Yi.c[14] = 0;
0f113f3e
MC
621 ctx->Yi.c[15] = 1;
622 ctr = 1;
623 } else {
624 size_t i;
625 u64 len0 = len;
626
f5791af3
AP
627 /* Borrow ctx->Xi to calculate initial Yi */
628 ctx->Xi.u[0] = 0;
629 ctx->Xi.u[1] = 0;
630
0f113f3e
MC
631 while (len >= 16) {
632 for (i = 0; i < 16; ++i)
f5791af3
AP
633 ctx->Xi.c[i] ^= iv[i];
634 GCM_MUL(ctx);
0f113f3e
MC
635 iv += 16;
636 len -= 16;
637 }
638 if (len) {
639 for (i = 0; i < len; ++i)
f5791af3
AP
640 ctx->Xi.c[i] ^= iv[i];
641 GCM_MUL(ctx);
0f113f3e
MC
642 }
643 len0 <<= 3;
e23d850f 644 if (IS_LITTLE_ENDIAN) {
e7f5b1cd 645#ifdef BSWAP8
f5791af3 646 ctx->Xi.u[1] ^= BSWAP8(len0);
e7f5b1cd 647#else
f5791af3
AP
648 ctx->Xi.c[8] ^= (u8)(len0 >> 56);
649 ctx->Xi.c[9] ^= (u8)(len0 >> 48);
650 ctx->Xi.c[10] ^= (u8)(len0 >> 40);
651 ctx->Xi.c[11] ^= (u8)(len0 >> 32);
652 ctx->Xi.c[12] ^= (u8)(len0 >> 24);
653 ctx->Xi.c[13] ^= (u8)(len0 >> 16);
654 ctx->Xi.c[14] ^= (u8)(len0 >> 8);
655 ctx->Xi.c[15] ^= (u8)(len0);
e7f5b1cd 656#endif
f5791af3
AP
657 } else {
658 ctx->Xi.u[1] ^= len0;
659 }
e7f5b1cd 660
f5791af3 661 GCM_MUL(ctx);
e7f5b1cd 662
e23d850f 663 if (IS_LITTLE_ENDIAN)
997d1aac 664#ifdef BSWAP4
f5791af3 665 ctr = BSWAP4(ctx->Xi.d[3]);
997d1aac 666#else
f5791af3 667 ctr = GETU32(ctx->Xi.c + 12);
997d1aac 668#endif
0f113f3e 669 else
f5791af3
AP
670 ctr = ctx->Xi.d[3];
671
672 /* Copy borrowed Xi to Yi */
673 ctx->Yi.u[0] = ctx->Xi.u[0];
674 ctx->Yi.u[1] = ctx->Xi.u[1];
0f113f3e 675 }
e7f5b1cd 676
f5791af3
AP
677 ctx->Xi.u[0] = 0;
678 ctx->Xi.u[1] = 0;
679
0f113f3e
MC
680 (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
681 ++ctr;
e23d850f 682 if (IS_LITTLE_ENDIAN)
997d1aac 683#ifdef BSWAP4
0f113f3e 684 ctx->Yi.d[3] = BSWAP4(ctr);
997d1aac 685#else
0f113f3e 686 PUTU32(ctx->Yi.c + 12, ctr);
997d1aac 687#endif
0f113f3e
MC
688 else
689 ctx->Yi.d[3] = ctr;
e7f5b1cd
AP
690}
691
0f113f3e
MC
692int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
693 size_t len)
e7f5b1cd 694{
0f113f3e
MC
695 size_t i;
696 unsigned int n;
697 u64 alen = ctx->len.u[0];
e7f5b1cd 698
0f113f3e
MC
699 if (ctx->len.u[1])
700 return -2;
701
702 alen += len;
703 if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
704 return -1;
705 ctx->len.u[0] = alen;
706
707 n = ctx->ares;
708 if (n) {
709 while (n && len) {
710 ctx->Xi.c[n] ^= *(aad++);
711 --len;
712 n = (n + 1) % 16;
713 }
714 if (n == 0)
f5791af3 715 GCM_MUL(ctx);
0f113f3e
MC
716 else {
717 ctx->ares = n;
718 return 0;
719 }
720 }
2262beef 721#ifdef GHASH
0f113f3e
MC
722 if ((i = (len & (size_t)-16))) {
723 GHASH(ctx, aad, i);
724 aad += i;
725 len -= i;
726 }
2262beef 727#else
0f113f3e
MC
728 while (len >= 16) {
729 for (i = 0; i < 16; ++i)
730 ctx->Xi.c[i] ^= aad[i];
f5791af3 731 GCM_MUL(ctx);
0f113f3e
MC
732 aad += 16;
733 len -= 16;
734 }
2262beef 735#endif
0f113f3e
MC
736 if (len) {
737 n = (unsigned int)len;
738 for (i = 0; i < len; ++i)
739 ctx->Xi.c[i] ^= aad[i];
740 }
b68c1315 741
0f113f3e
MC
742 ctx->ares = n;
743 return 0;
e7f5b1cd
AP
744}
745
1f2502eb 746int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
0f113f3e
MC
747 const unsigned char *in, unsigned char *out,
748 size_t len)
e7f5b1cd 749{
e23d850f 750 DECLARE_IS_ENDIAN;
c1b2569d 751 unsigned int n, ctr, mres;
0f113f3e
MC
752 size_t i;
753 u64 mlen = ctx->len.u[1];
754 block128_f block = ctx->block;
755 void *key = ctx->key;
1f2502eb 756
0f113f3e
MC
757 mlen += len;
758 if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
759 return -1;
760 ctx->len.u[1] = mlen;
e7f5b1cd 761
c1b2569d
AP
762 mres = ctx->mres;
763
0f113f3e
MC
764 if (ctx->ares) {
765 /* First call to encrypt finalizes GHASH(AAD) */
c1b2569d
AP
766#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
767 if (len == 0) {
768 GCM_MUL(ctx);
769 ctx->ares = 0;
770 return 0;
771 }
772 memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
773 ctx->Xi.u[0] = 0;
774 ctx->Xi.u[1] = 0;
775 mres = sizeof(ctx->Xi);
776#else
f5791af3 777 GCM_MUL(ctx);
c1b2569d 778#endif
0f113f3e
MC
779 ctx->ares = 0;
780 }
96a4cf8c 781
e23d850f 782 if (IS_LITTLE_ENDIAN)
997d1aac 783#ifdef BSWAP4
0f113f3e 784 ctr = BSWAP4(ctx->Yi.d[3]);
997d1aac 785#else
0f113f3e 786 ctr = GETU32(ctx->Yi.c + 12);
997d1aac 787#endif
0f113f3e
MC
788 else
789 ctr = ctx->Yi.d[3];
96a4cf8c 790
c1b2569d 791 n = mres % 16;
0f113f3e
MC
792#if !defined(OPENSSL_SMALL_FOOTPRINT)
793 if (16 % sizeof(size_t) == 0) { /* always true actually */
794 do {
795 if (n) {
c1b2569d
AP
796# if defined(GHASH)
797 while (n && len) {
798 ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
799 --len;
800 n = (n + 1) % 16;
801 }
802 if (n == 0) {
803 GHASH(ctx, ctx->Xn, mres);
804 mres = 0;
805 } else {
806 ctx->mres = mres;
807 return 0;
808 }
809# else
0f113f3e
MC
810 while (n && len) {
811 ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
812 --len;
813 n = (n + 1) % 16;
814 }
c1b2569d 815 if (n == 0) {
f5791af3 816 GCM_MUL(ctx);
c1b2569d
AP
817 mres = 0;
818 } else {
0f113f3e
MC
819 ctx->mres = n;
820 return 0;
821 }
c1b2569d 822# endif
0f113f3e
MC
823 }
824# if defined(STRICT_ALIGNMENT)
825 if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
826 break;
827# endif
2e635aa8 828# if defined(GHASH)
c1b2569d
AP
829 if (len >= 16 && mres) {
830 GHASH(ctx, ctx->Xn, mres);
831 mres = 0;
832 }
2e635aa8 833# if defined(GHASH_CHUNK)
0f113f3e
MC
834 while (len >= GHASH_CHUNK) {
835 size_t j = GHASH_CHUNK;
836
837 while (j) {
77286fe3
BE
838 size_t_aX *out_t = (size_t_aX *)out;
839 const size_t_aX *in_t = (const size_t_aX *)in;
0f113f3e
MC
840
841 (*block) (ctx->Yi.c, ctx->EKi.c, key);
842 ++ctr;
e23d850f 843 if (IS_LITTLE_ENDIAN)
2e635aa8 844# ifdef BSWAP4
0f113f3e 845 ctx->Yi.d[3] = BSWAP4(ctr);
2e635aa8 846# else
0f113f3e 847 PUTU32(ctx->Yi.c + 12, ctr);
2e635aa8 848# endif
0f113f3e
MC
849 else
850 ctx->Yi.d[3] = ctr;
851 for (i = 0; i < 16 / sizeof(size_t); ++i)
852 out_t[i] = in_t[i] ^ ctx->EKi.t[i];
853 out += 16;
854 in += 16;
855 j -= 16;
856 }
857 GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
858 len -= GHASH_CHUNK;
859 }
2e635aa8 860# endif
0f113f3e
MC
861 if ((i = (len & (size_t)-16))) {
862 size_t j = i;
863
864 while (len >= 16) {
77286fe3
BE
865 size_t_aX *out_t = (size_t_aX *)out;
866 const size_t_aX *in_t = (const size_t_aX *)in;
0f113f3e
MC
867
868 (*block) (ctx->Yi.c, ctx->EKi.c, key);
869 ++ctr;
e23d850f 870 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
871# ifdef BSWAP4
872 ctx->Yi.d[3] = BSWAP4(ctr);
873# else
874 PUTU32(ctx->Yi.c + 12, ctr);
875# endif
876 else
877 ctx->Yi.d[3] = ctr;
878 for (i = 0; i < 16 / sizeof(size_t); ++i)
879 out_t[i] = in_t[i] ^ ctx->EKi.t[i];
880 out += 16;
881 in += 16;
882 len -= 16;
883 }
884 GHASH(ctx, out - j, j);
885 }
886# else
887 while (len >= 16) {
888 size_t *out_t = (size_t *)out;
889 const size_t *in_t = (const size_t *)in;
890
891 (*block) (ctx->Yi.c, ctx->EKi.c, key);
892 ++ctr;
e23d850f 893 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
894# ifdef BSWAP4
895 ctx->Yi.d[3] = BSWAP4(ctr);
896# else
897 PUTU32(ctx->Yi.c + 12, ctr);
898# endif
899 else
900 ctx->Yi.d[3] = ctr;
901 for (i = 0; i < 16 / sizeof(size_t); ++i)
902 ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
f5791af3 903 GCM_MUL(ctx);
0f113f3e
MC
904 out += 16;
905 in += 16;
906 len -= 16;
907 }
908# endif
909 if (len) {
910 (*block) (ctx->Yi.c, ctx->EKi.c, key);
911 ++ctr;
e23d850f 912 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
913# ifdef BSWAP4
914 ctx->Yi.d[3] = BSWAP4(ctr);
915# else
916 PUTU32(ctx->Yi.c + 12, ctr);
917# endif
918 else
919 ctx->Yi.d[3] = ctr;
c1b2569d
AP
920# if defined(GHASH)
921 while (len--) {
922 ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
923 ++n;
924 }
925# else
0f113f3e
MC
926 while (len--) {
927 ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
928 ++n;
929 }
c1b2569d
AP
930 mres = n;
931# endif
0f113f3e
MC
932 }
933
c1b2569d 934 ctx->mres = mres;
0f113f3e
MC
935 return 0;
936 } while (0);
937 }
e7f5b1cd 938#endif
0f113f3e
MC
939 for (i = 0; i < len; ++i) {
940 if (n == 0) {
941 (*block) (ctx->Yi.c, ctx->EKi.c, key);
942 ++ctr;
e23d850f 943 if (IS_LITTLE_ENDIAN)
997d1aac 944#ifdef BSWAP4
0f113f3e 945 ctx->Yi.d[3] = BSWAP4(ctr);
997d1aac 946#else
0f113f3e
MC
947 PUTU32(ctx->Yi.c + 12, ctr);
948#endif
949 else
950 ctx->Yi.d[3] = ctr;
951 }
c1b2569d
AP
952#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
953 ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
0f113f3e 954 n = (n + 1) % 16;
c1b2569d
AP
955 if (mres == sizeof(ctx->Xn)) {
956 GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
957 mres = 0;
958 }
959#else
960 ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
961 mres = n = (n + 1) % 16;
0f113f3e 962 if (n == 0)
f5791af3 963 GCM_MUL(ctx);
c1b2569d 964#endif
0f113f3e
MC
965 }
966
c1b2569d 967 ctx->mres = mres;
0f113f3e 968 return 0;
e7f5b1cd
AP
969}
970
1f2502eb 971int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
0f113f3e
MC
972 const unsigned char *in, unsigned char *out,
973 size_t len)
e7f5b1cd 974{
e23d850f 975 DECLARE_IS_ENDIAN;
c1b2569d 976 unsigned int n, ctr, mres;
0f113f3e
MC
977 size_t i;
978 u64 mlen = ctx->len.u[1];
979 block128_f block = ctx->block;
980 void *key = ctx->key;
1f2502eb 981
0f113f3e
MC
982 mlen += len;
983 if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
984 return -1;
985 ctx->len.u[1] = mlen;
e7f5b1cd 986
c1b2569d
AP
987 mres = ctx->mres;
988
0f113f3e
MC
989 if (ctx->ares) {
990 /* First call to decrypt finalizes GHASH(AAD) */
c1b2569d
AP
991#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
992 if (len == 0) {
993 GCM_MUL(ctx);
994 ctx->ares = 0;
995 return 0;
996 }
997 memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
998 ctx->Xi.u[0] = 0;
999 ctx->Xi.u[1] = 0;
1000 mres = sizeof(ctx->Xi);
1001#else
f5791af3 1002 GCM_MUL(ctx);
c1b2569d 1003#endif
0f113f3e
MC
1004 ctx->ares = 0;
1005 }
b68c1315 1006
e23d850f 1007 if (IS_LITTLE_ENDIAN)
997d1aac 1008#ifdef BSWAP4
0f113f3e 1009 ctr = BSWAP4(ctx->Yi.d[3]);
997d1aac 1010#else
0f113f3e 1011 ctr = GETU32(ctx->Yi.c + 12);
997d1aac 1012#endif
0f113f3e
MC
1013 else
1014 ctr = ctx->Yi.d[3];
e7f5b1cd 1015
c1b2569d 1016 n = mres % 16;
e7f5b1cd 1017#if !defined(OPENSSL_SMALL_FOOTPRINT)
0f113f3e
MC
1018 if (16 % sizeof(size_t) == 0) { /* always true actually */
1019 do {
1020 if (n) {
c1b2569d
AP
1021# if defined(GHASH)
1022 while (n && len) {
1023 *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
1024 --len;
1025 n = (n + 1) % 16;
1026 }
1027 if (n == 0) {
1028 GHASH(ctx, ctx->Xn, mres);
1029 mres = 0;
1030 } else {
1031 ctx->mres = mres;
1032 return 0;
1033 }
1034# else
0f113f3e
MC
1035 while (n && len) {
1036 u8 c = *(in++);
1037 *(out++) = c ^ ctx->EKi.c[n];
1038 ctx->Xi.c[n] ^= c;
1039 --len;
1040 n = (n + 1) % 16;
1041 }
c1b2569d 1042 if (n == 0) {
f5791af3 1043 GCM_MUL(ctx);
c1b2569d
AP
1044 mres = 0;
1045 } else {
0f113f3e
MC
1046 ctx->mres = n;
1047 return 0;
1048 }
c1b2569d 1049# endif
0f113f3e
MC
1050 }
1051# if defined(STRICT_ALIGNMENT)
1052 if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
1053 break;
1054# endif
2e635aa8 1055# if defined(GHASH)
c1b2569d
AP
1056 if (len >= 16 && mres) {
1057 GHASH(ctx, ctx->Xn, mres);
1058 mres = 0;
1059 }
2e635aa8 1060# if defined(GHASH_CHUNK)
0f113f3e
MC
1061 while (len >= GHASH_CHUNK) {
1062 size_t j = GHASH_CHUNK;
1063
1064 GHASH(ctx, in, GHASH_CHUNK);
1065 while (j) {
77286fe3
BE
1066 size_t_aX *out_t = (size_t_aX *)out;
1067 const size_t_aX *in_t = (const size_t_aX *)in;
0f113f3e
MC
1068
1069 (*block) (ctx->Yi.c, ctx->EKi.c, key);
1070 ++ctr;
e23d850f 1071 if (IS_LITTLE_ENDIAN)
2e635aa8 1072# ifdef BSWAP4
0f113f3e 1073 ctx->Yi.d[3] = BSWAP4(ctr);
2e635aa8 1074# else
0f113f3e 1075 PUTU32(ctx->Yi.c + 12, ctr);
2e635aa8 1076# endif
0f113f3e
MC
1077 else
1078 ctx->Yi.d[3] = ctr;
1079 for (i = 0; i < 16 / sizeof(size_t); ++i)
1080 out_t[i] = in_t[i] ^ ctx->EKi.t[i];
1081 out += 16;
1082 in += 16;
1083 j -= 16;
1084 }
1085 len -= GHASH_CHUNK;
1086 }
2e635aa8 1087# endif
0f113f3e
MC
1088 if ((i = (len & (size_t)-16))) {
1089 GHASH(ctx, in, i);
1090 while (len >= 16) {
77286fe3
BE
1091 size_t_aX *out_t = (size_t_aX *)out;
1092 const size_t_aX *in_t = (const size_t_aX *)in;
0f113f3e
MC
1093
1094 (*block) (ctx->Yi.c, ctx->EKi.c, key);
1095 ++ctr;
e23d850f 1096 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
1097# ifdef BSWAP4
1098 ctx->Yi.d[3] = BSWAP4(ctr);
1099# else
1100 PUTU32(ctx->Yi.c + 12, ctr);
1101# endif
1102 else
1103 ctx->Yi.d[3] = ctr;
1104 for (i = 0; i < 16 / sizeof(size_t); ++i)
1105 out_t[i] = in_t[i] ^ ctx->EKi.t[i];
1106 out += 16;
1107 in += 16;
1108 len -= 16;
1109 }
1110 }
1111# else
1112 while (len >= 16) {
1113 size_t *out_t = (size_t *)out;
1114 const size_t *in_t = (const size_t *)in;
1115
1116 (*block) (ctx->Yi.c, ctx->EKi.c, key);
1117 ++ctr;
e23d850f 1118 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
1119# ifdef BSWAP4
1120 ctx->Yi.d[3] = BSWAP4(ctr);
1121# else
1122 PUTU32(ctx->Yi.c + 12, ctr);
1123# endif
1124 else
1125 ctx->Yi.d[3] = ctr;
1126 for (i = 0; i < 16 / sizeof(size_t); ++i) {
1d724b5e
ZJ
1127 size_t c = in_t[i];
1128 out_t[i] = c ^ ctx->EKi.t[i];
0f113f3e
MC
1129 ctx->Xi.t[i] ^= c;
1130 }
f5791af3 1131 GCM_MUL(ctx);
0f113f3e
MC
1132 out += 16;
1133 in += 16;
1134 len -= 16;
1135 }
1136# endif
1137 if (len) {
1138 (*block) (ctx->Yi.c, ctx->EKi.c, key);
1139 ++ctr;
e23d850f 1140 if (IS_LITTLE_ENDIAN)
0f113f3e
MC
1141# ifdef BSWAP4
1142 ctx->Yi.d[3] = BSWAP4(ctr);
1143# else
1144 PUTU32(ctx->Yi.c + 12, ctr);
1145# endif
1146 else
1147 ctx->Yi.d[3] = ctr;
c1b2569d
AP
1148# if defined(GHASH)
1149 while (len--) {
1150 out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
1151 ++n;
1152 }
1153# else
0f113f3e
MC
1154 while (len--) {
1155 u8 c = in[n];
1156 ctx->Xi.c[n] ^= c;
1157 out[n] = c ^ ctx->EKi.c[n];
1158 ++n;
1159 }
c1b2569d
AP
1160 mres = n;
1161# endif
0f113f3e
MC
1162 }
1163
c1b2569d 1164 ctx->mres = mres;
0f113f3e
MC
1165 return 0;
1166 } while (0);
1167 }
997d1aac 1168#endif
0f113f3e
MC
1169 for (i = 0; i < len; ++i) {
1170 u8 c;
1171 if (n == 0) {
1172 (*block) (ctx->Yi.c, ctx->EKi.c, key);
1173 ++ctr;
e23d850f 1174 if (IS_LITTLE_ENDIAN)
997d1aac 1175#ifdef BSWAP4
0f113f3e 1176 ctx->Yi.d[3] = BSWAP4(ctr);
997d1aac 1177#else
0f113f3e
MC
1178 PUTU32(ctx->Yi.c + 12, ctr);
1179#endif
1180 else
1181 ctx->Yi.d[3] = ctr;
1182 }
c1b2569d
AP
1183#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1184 out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
1185 n = (n + 1) % 16;
1186 if (mres == sizeof(ctx->Xn)) {
1187 GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
1188 mres = 0;
1189 }
1190#else
0f113f3e
MC
1191 c = in[i];
1192 out[i] = c ^ ctx->EKi.c[n];
1193 ctx->Xi.c[n] ^= c;
c1b2569d 1194 mres = n = (n + 1) % 16;
0f113f3e 1195 if (n == 0)
f5791af3 1196 GCM_MUL(ctx);
c1b2569d 1197#endif
0f113f3e 1198 }
96a4cf8c 1199
c1b2569d 1200 ctx->mres = mres;
0f113f3e 1201 return 0;
e7f5b1cd
AP
1202}
1203
1f2502eb 1204int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
0f113f3e
MC
1205 const unsigned char *in, unsigned char *out,
1206 size_t len, ctr128_f stream)
f71c6ace 1207{
2e635aa8
AP
1208#if defined(OPENSSL_SMALL_FOOTPRINT)
1209 return CRYPTO_gcm128_encrypt(ctx, in, out, len);
1210#else
e23d850f 1211 DECLARE_IS_ENDIAN;
c1b2569d 1212 unsigned int n, ctr, mres;
0f113f3e
MC
1213 size_t i;
1214 u64 mlen = ctx->len.u[1];
1215 void *key = ctx->key;
1f2502eb 1216
0f113f3e
MC
1217 mlen += len;
1218 if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1219 return -1;
1220 ctx->len.u[1] = mlen;
f71c6ace 1221
c1b2569d
AP
1222 mres = ctx->mres;
1223
0f113f3e
MC
1224 if (ctx->ares) {
1225 /* First call to encrypt finalizes GHASH(AAD) */
c1b2569d
AP
1226#if defined(GHASH)
1227 if (len == 0) {
1228 GCM_MUL(ctx);
1229 ctx->ares = 0;
1230 return 0;
1231 }
1232 memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
1233 ctx->Xi.u[0] = 0;
1234 ctx->Xi.u[1] = 0;
1235 mres = sizeof(ctx->Xi);
1236#else
f5791af3 1237 GCM_MUL(ctx);
c1b2569d 1238#endif
0f113f3e
MC
1239 ctx->ares = 0;
1240 }
b68c1315 1241
e23d850f 1242 if (IS_LITTLE_ENDIAN)
2e635aa8 1243# ifdef BSWAP4
0f113f3e 1244 ctr = BSWAP4(ctx->Yi.d[3]);
2e635aa8 1245# else
0f113f3e 1246 ctr = GETU32(ctx->Yi.c + 12);
2e635aa8 1247# endif
0f113f3e
MC
1248 else
1249 ctr = ctx->Yi.d[3];
1250
c1b2569d 1251 n = mres % 16;
0f113f3e 1252 if (n) {
c1b2569d
AP
1253# if defined(GHASH)
1254 while (n && len) {
1255 ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
1256 --len;
1257 n = (n + 1) % 16;
1258 }
1259 if (n == 0) {
1260 GHASH(ctx, ctx->Xn, mres);
1261 mres = 0;
1262 } else {
1263 ctx->mres = mres;
1264 return 0;
1265 }
1266# else
0f113f3e
MC
1267 while (n && len) {
1268 ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
1269 --len;
1270 n = (n + 1) % 16;
1271 }
c1b2569d 1272 if (n == 0) {
f5791af3 1273 GCM_MUL(ctx);
c1b2569d
AP
1274 mres = 0;
1275 } else {
0f113f3e
MC
1276 ctx->mres = n;
1277 return 0;
1278 }
c1b2569d 1279# endif
0f113f3e 1280 }
c1b2569d
AP
1281# if defined(GHASH)
1282 if (len >= 16 && mres) {
1283 GHASH(ctx, ctx->Xn, mres);
1284 mres = 0;
1285 }
1286# if defined(GHASH_CHUNK)
0f113f3e
MC
1287 while (len >= GHASH_CHUNK) {
1288 (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
1289 ctr += GHASH_CHUNK / 16;
e23d850f 1290 if (IS_LITTLE_ENDIAN)
c1b2569d 1291# ifdef BSWAP4
0f113f3e 1292 ctx->Yi.d[3] = BSWAP4(ctr);
c1b2569d 1293# else
0f113f3e 1294 PUTU32(ctx->Yi.c + 12, ctr);
c1b2569d 1295# endif
0f113f3e
MC
1296 else
1297 ctx->Yi.d[3] = ctr;
1298 GHASH(ctx, out, GHASH_CHUNK);
1299 out += GHASH_CHUNK;
1300 in += GHASH_CHUNK;
1301 len -= GHASH_CHUNK;
1302 }
c1b2569d 1303# endif
2e635aa8 1304# endif
0f113f3e
MC
1305 if ((i = (len & (size_t)-16))) {
1306 size_t j = i / 16;
f71c6ace 1307
0f113f3e
MC
1308 (*stream) (in, out, j, key, ctx->Yi.c);
1309 ctr += (unsigned int)j;
e23d850f 1310 if (IS_LITTLE_ENDIAN)
2e635aa8 1311# ifdef BSWAP4
0f113f3e 1312 ctx->Yi.d[3] = BSWAP4(ctr);
2e635aa8 1313# else
0f113f3e 1314 PUTU32(ctx->Yi.c + 12, ctr);
2e635aa8 1315# endif
0f113f3e
MC
1316 else
1317 ctx->Yi.d[3] = ctr;
1318 in += i;
1319 len -= i;
2e635aa8 1320# if defined(GHASH)
0f113f3e
MC
1321 GHASH(ctx, out, i);
1322 out += i;
2e635aa8 1323# else
0f113f3e
MC
1324 while (j--) {
1325 for (i = 0; i < 16; ++i)
1326 ctx->Xi.c[i] ^= out[i];
f5791af3 1327 GCM_MUL(ctx);
0f113f3e
MC
1328 out += 16;
1329 }
2e635aa8 1330# endif
0f113f3e
MC
1331 }
1332 if (len) {
1333 (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
1334 ++ctr;
e23d850f 1335 if (IS_LITTLE_ENDIAN)
2e635aa8 1336# ifdef BSWAP4
0f113f3e 1337 ctx->Yi.d[3] = BSWAP4(ctr);
2e635aa8 1338# else
0f113f3e 1339 PUTU32(ctx->Yi.c + 12, ctr);
2e635aa8 1340# endif
0f113f3e
MC
1341 else
1342 ctx->Yi.d[3] = ctr;
1343 while (len--) {
c1b2569d
AP
1344# if defined(GHASH)
1345 ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
1346# else
1347 ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n];
1348# endif
0f113f3e
MC
1349 ++n;
1350 }
1351 }
1352
c1b2569d 1353 ctx->mres = mres;
0f113f3e 1354 return 0;
2e635aa8 1355#endif
f71c6ace
AP
1356}
1357
1f2502eb 1358int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
0f113f3e
MC
1359 const unsigned char *in, unsigned char *out,
1360 size_t len, ctr128_f stream)
f71c6ace 1361{
2e635aa8
AP
1362#if defined(OPENSSL_SMALL_FOOTPRINT)
1363 return CRYPTO_gcm128_decrypt(ctx, in, out, len);
1364#else
e23d850f 1365 DECLARE_IS_ENDIAN;
c1b2569d 1366 unsigned int n, ctr, mres;
0f113f3e
MC
1367 size_t i;
1368 u64 mlen = ctx->len.u[1];
1369 void *key = ctx->key;
1f2502eb 1370
0f113f3e
MC
1371 mlen += len;
1372 if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
1373 return -1;
1374 ctx->len.u[1] = mlen;
f71c6ace 1375
c1b2569d
AP
1376 mres = ctx->mres;
1377
0f113f3e
MC
1378 if (ctx->ares) {
1379 /* First call to decrypt finalizes GHASH(AAD) */
c1b2569d
AP
1380# if defined(GHASH)
1381 if (len == 0) {
1382 GCM_MUL(ctx);
1383 ctx->ares = 0;
1384 return 0;
1385 }
1386 memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
1387 ctx->Xi.u[0] = 0;
1388 ctx->Xi.u[1] = 0;
1389 mres = sizeof(ctx->Xi);
1390# else
f5791af3 1391 GCM_MUL(ctx);
c1b2569d 1392# endif
0f113f3e
MC
1393 ctx->ares = 0;
1394 }
b68c1315 1395
e23d850f 1396 if (IS_LITTLE_ENDIAN)
2e635aa8 1397# ifdef BSWAP4
0f113f3e 1398 ctr = BSWAP4(ctx->Yi.d[3]);
2e635aa8 1399# else
0f113f3e 1400 ctr = GETU32(ctx->Yi.c + 12);
2e635aa8 1401# endif
0f113f3e
MC
1402 else
1403 ctr = ctx->Yi.d[3];
1404
c1b2569d 1405 n = mres % 16;
0f113f3e 1406 if (n) {
c1b2569d
AP
1407# if defined(GHASH)
1408 while (n && len) {
1409 *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
1410 --len;
1411 n = (n + 1) % 16;
1412 }
1413 if (n == 0) {
1414 GHASH(ctx, ctx->Xn, mres);
1415 mres = 0;
1416 } else {
1417 ctx->mres = mres;
1418 return 0;
1419 }
1420# else
0f113f3e
MC
1421 while (n && len) {
1422 u8 c = *(in++);
1423 *(out++) = c ^ ctx->EKi.c[n];
1424 ctx->Xi.c[n] ^= c;
1425 --len;
1426 n = (n + 1) % 16;
1427 }
c1b2569d 1428 if (n == 0) {
f5791af3 1429 GCM_MUL(ctx);
c1b2569d
AP
1430 mres = 0;
1431 } else {
0f113f3e
MC
1432 ctx->mres = n;
1433 return 0;
1434 }
c1b2569d 1435# endif
0f113f3e 1436 }
c1b2569d
AP
1437# if defined(GHASH)
1438 if (len >= 16 && mres) {
1439 GHASH(ctx, ctx->Xn, mres);
1440 mres = 0;
1441 }
1442# if defined(GHASH_CHUNK)
0f113f3e
MC
1443 while (len >= GHASH_CHUNK) {
1444 GHASH(ctx, in, GHASH_CHUNK);
1445 (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
1446 ctr += GHASH_CHUNK / 16;
e23d850f 1447 if (IS_LITTLE_ENDIAN)
c1b2569d 1448# ifdef BSWAP4
0f113f3e 1449 ctx->Yi.d[3] = BSWAP4(ctr);
c1b2569d 1450# else
0f113f3e 1451 PUTU32(ctx->Yi.c + 12, ctr);
c1b2569d 1452# endif
0f113f3e
MC
1453 else
1454 ctx->Yi.d[3] = ctr;
1455 out += GHASH_CHUNK;
1456 in += GHASH_CHUNK;
1457 len -= GHASH_CHUNK;
1458 }
c1b2569d 1459# endif
2e635aa8 1460# endif
0f113f3e
MC
1461 if ((i = (len & (size_t)-16))) {
1462 size_t j = i / 16;
f71c6ace 1463
2e635aa8 1464# if defined(GHASH)
0f113f3e 1465 GHASH(ctx, in, i);
2e635aa8 1466# else
0f113f3e
MC
1467 while (j--) {
1468 size_t k;
1469 for (k = 0; k < 16; ++k)
1470 ctx->Xi.c[k] ^= in[k];
f5791af3 1471 GCM_MUL(ctx);
0f113f3e
MC
1472 in += 16;
1473 }
1474 j = i / 16;
1475 in -= i;
2e635aa8 1476# endif
0f113f3e
MC
1477 (*stream) (in, out, j, key, ctx->Yi.c);
1478 ctr += (unsigned int)j;
e23d850f 1479 if (IS_LITTLE_ENDIAN)
2e635aa8 1480# ifdef BSWAP4
0f113f3e 1481 ctx->Yi.d[3] = BSWAP4(ctr);
2e635aa8 1482# else
0f113f3e 1483 PUTU32(ctx->Yi.c + 12, ctr);
2e635aa8 1484# endif
0f113f3e
MC
1485 else
1486 ctx->Yi.d[3] = ctr;
1487 out += i;
1488 in += i;
1489 len -= i;
1490 }
1491 if (len) {
1492 (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
1493 ++ctr;
e23d850f 1494 if (IS_LITTLE_ENDIAN)
2e635aa8 1495# ifdef BSWAP4
0f113f3e 1496 ctx->Yi.d[3] = BSWAP4(ctr);
2e635aa8 1497# else
0f113f3e 1498 PUTU32(ctx->Yi.c + 12, ctr);
2e635aa8 1499# endif
0f113f3e
MC
1500 else
1501 ctx->Yi.d[3] = ctr;
1502 while (len--) {
c1b2569d
AP
1503# if defined(GHASH)
1504 out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
1505# else
0f113f3e 1506 u8 c = in[n];
c1b2569d 1507 ctx->Xi.c[mres++] ^= c;
0f113f3e 1508 out[n] = c ^ ctx->EKi.c[n];
c1b2569d 1509# endif
0f113f3e
MC
1510 ++n;
1511 }
1512 }
1513
c1b2569d 1514 ctx->mres = mres;
0f113f3e 1515 return 0;
2e635aa8 1516#endif
f71c6ace
AP
1517}
1518
0f113f3e
MC
1519int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
1520 size_t len)
e7f5b1cd 1521{
e23d850f 1522 DECLARE_IS_ENDIAN;
0f113f3e
MC
1523 u64 alen = ctx->len.u[0] << 3;
1524 u64 clen = ctx->len.u[1] << 3;
e7f5b1cd 1525
c1b2569d
AP
1526#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1527 u128 bitlen;
1528 unsigned int mres = ctx->mres;
1529
1530 if (mres) {
1531 unsigned blocks = (mres + 15) & -16;
1532
1533 memset(ctx->Xn + mres, 0, blocks - mres);
1534 mres = blocks;
1535 if (mres == sizeof(ctx->Xn)) {
1536 GHASH(ctx, ctx->Xn, mres);
1537 mres = 0;
1538 }
1539 } else if (ctx->ares) {
1540 GCM_MUL(ctx);
1541 }
1542#else
0f113f3e 1543 if (ctx->mres || ctx->ares)
f5791af3 1544 GCM_MUL(ctx);
c1b2569d 1545#endif
e7f5b1cd 1546
e23d850f 1547 if (IS_LITTLE_ENDIAN) {
e7f5b1cd 1548#ifdef BSWAP8
0f113f3e
MC
1549 alen = BSWAP8(alen);
1550 clen = BSWAP8(clen);
e7f5b1cd 1551#else
0f113f3e 1552 u8 *p = ctx->len.c;
e7f5b1cd 1553
0f113f3e
MC
1554 ctx->len.u[0] = alen;
1555 ctx->len.u[1] = clen;
e7f5b1cd 1556
0f113f3e
MC
1557 alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
1558 clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
e7f5b1cd 1559#endif
0f113f3e 1560 }
e7f5b1cd 1561
c1b2569d
AP
1562#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
1563 bitlen.hi = alen;
1564 bitlen.lo = clen;
1565 memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen));
1566 mres += sizeof(bitlen);
1567 GHASH(ctx, ctx->Xn, mres);
1568#else
0f113f3e
MC
1569 ctx->Xi.u[0] ^= alen;
1570 ctx->Xi.u[1] ^= clen;
f5791af3 1571 GCM_MUL(ctx);
c1b2569d 1572#endif
e7f5b1cd 1573
0f113f3e
MC
1574 ctx->Xi.u[0] ^= ctx->EK0.u[0];
1575 ctx->Xi.u[1] ^= ctx->EK0.u[1];
6acb4ff3 1576
0f113f3e 1577 if (tag && len <= sizeof(ctx->Xi))
1e4a355d 1578 return CRYPTO_memcmp(ctx->Xi.c, tag, len);
0f113f3e
MC
1579 else
1580 return -1;
6acb4ff3
AP
1581}
1582
fd3dbc1d
DSH
1583void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
1584{
0f113f3e
MC
1585 CRYPTO_gcm128_finish(ctx, NULL, 0);
1586 memcpy(tag, ctx->Xi.c,
1587 len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
fd3dbc1d
DSH
1588}
1589
6acb4ff3
AP
1590GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
1591{
0f113f3e 1592 GCM128_CONTEXT *ret;
6acb4ff3 1593
90945fa3 1594 if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
0f113f3e 1595 CRYPTO_gcm128_init(ret, key, block);
6acb4ff3 1596
0f113f3e 1597 return ret;
6acb4ff3
AP
1598}
1599
1600void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
1601{
4b45c6e5 1602 OPENSSL_clear_free(ctx, sizeof(*ctx));
e7f5b1cd 1603}