Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/modes/gcm128.c

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