Debian Codegraph @

cipher/cipher-gcm.c

raw
1 /* cipher-gcm.c - Generic Galois Counter Mode implementation
2 * Copyright (C) 2013 Dmitry Eremin-Solenikov
3 * Copyright (C) 2013, 2018-2019 Jussi Kivilinna <[email protected]>
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "g10lib.h"
28 #include "cipher.h"
29 #include "bufhelp.h"
30 #include "./cipher-internal.h"
31
32
33 static gcry_err_code_t _gcry_cipher_gcm_setiv_zero (gcry_cipher_hd_t c);
34
35 /* Helper macro to force alignment to 16 or 64 bytes. */
36 #ifdef HAVE_GCC_ATTRIBUTE_ALIGNED
37 # define ATTR_ALIGNED_64 __attribute__ ((aligned (64)))
38 #else
39 # define ATTR_ALIGNED_64
40 #endif
41
42
43 #ifdef GCM_USE_INTEL_PCLMUL
44 extern void _gcry_ghash_setup_intel_pclmul (gcry_cipher_hd_t c,
45 unsigned int hw_features);
46 #endif
47
48 #ifdef GCM_USE_ARM_PMULL
49 extern void _gcry_ghash_setup_armv8_ce_pmull (void *gcm_key, void *gcm_table);
50
51 extern unsigned int _gcry_ghash_armv8_ce_pmull (void *gcm_key, byte *result,
52 const byte *buf, size_t nblocks,
53 void *gcm_table);
54
55 extern unsigned int _gcry_polyval_armv8_ce_pmull (void *gcm_key, byte *result,
56 const byte *buf,
57 size_t nblocks,
58 void *gcm_table);
59
60 static void
61 ghash_setup_armv8_ce_pmull (gcry_cipher_hd_t c)
62 {
63 _gcry_ghash_setup_armv8_ce_pmull(c->u_mode.gcm.u_ghash_key.key,
64 c->u_mode.gcm.gcm_table);
65 }
66
67 static unsigned int
68 ghash_armv8_ce_pmull (gcry_cipher_hd_t c, byte *result, const byte *buf,
69 size_t nblocks)
70 {
71 return _gcry_ghash_armv8_ce_pmull(c->u_mode.gcm.u_ghash_key.key, result, buf,
72 nblocks, c->u_mode.gcm.gcm_table);
73 }
74
75 static unsigned int
76 polyval_armv8_ce_pmull (gcry_cipher_hd_t c, byte *result, const byte *buf,
77 size_t nblocks)
78 {
79 return _gcry_polyval_armv8_ce_pmull(c->u_mode.gcm.u_ghash_key.key, result,
80 buf, nblocks, c->u_mode.gcm.gcm_table);
81 }
82 #endif /* GCM_USE_ARM_PMULL */
83
84 #ifdef GCM_USE_ARM_NEON
85 extern void _gcry_ghash_setup_armv7_neon (void *gcm_key);
86
87 extern unsigned int _gcry_ghash_armv7_neon (void *gcm_key, byte *result,
88 const byte *buf, size_t nblocks);
89
90 static void
91 ghash_setup_armv7_neon (gcry_cipher_hd_t c)
92 {
93 _gcry_ghash_setup_armv7_neon(c->u_mode.gcm.u_ghash_key.key);
94 }
95
96 static unsigned int
97 ghash_armv7_neon (gcry_cipher_hd_t c, byte *result, const byte *buf,
98 size_t nblocks)
99 {
100 return _gcry_ghash_armv7_neon(c->u_mode.gcm.u_ghash_key.key, result, buf,
101 nblocks);
102 }
103 #endif /* GCM_USE_ARM_NEON */
104
105 #ifdef GCM_USE_RISCV_ZBB_ZBC
106 extern void _gcry_ghash_setup_riscv_zbb_zbc(gcry_cipher_hd_t c);
107
108 extern unsigned int _gcry_ghash_riscv_zbb_zbc(gcry_cipher_hd_t c, byte *result,
109 const byte *buf, size_t nblocks);
110
111 extern unsigned int _gcry_polyval_riscv_zbb_zbc(gcry_cipher_hd_t c,
112 byte *result, const byte *buf,
113 size_t nblocks);
114 #endif /* GCM_USE_RISCV_ZBB_ZBC */
115
116 #ifdef GCM_USE_RISCV_ZVKG
117 extern int _gcry_ghash_setup_riscv_zvkg(gcry_cipher_hd_t c);
118
119 extern unsigned int _gcry_ghash_riscv_zvkg(gcry_cipher_hd_t c, byte *result,
120 const byte *buf, size_t nblocks);
121
122 extern unsigned int _gcry_polyval_riscv_zvkg(gcry_cipher_hd_t c, byte *result,
123 const byte *buf, size_t nblocks);
124 #endif /* GCM_USE_RISCV_ZVKG */
125
126 #ifdef GCM_USE_AARCH64
127 extern void _gcry_ghash_setup_aarch64_simd(gcry_cipher_hd_t c);
128
129 extern unsigned int _gcry_ghash_aarch64_simd(gcry_cipher_hd_t c, byte *result,
130 const byte *buf, size_t nblocks);
131 #endif /* GCM_USE_AARCH64 */
132
133 #ifdef GCM_USE_S390X_CRYPTO
134 #include "asm-inline-s390x.h"
135
136 static unsigned int
137 ghash_s390x_kimd (gcry_cipher_hd_t c, byte *result, const byte *buf,
138 size_t nblocks)
139 {
140 u128_t params[2];
141
142 memcpy (&params[0], result, 16);
143 memcpy (&params[1], c->u_mode.gcm.u_ghash_key.key, 16);
144
145 kimd_execute (KMID_FUNCTION_GHASH, &params, buf, nblocks * 16);
146
147 memcpy (result, &params[0], 16);
148 wipememory (params, sizeof(params));
149 return 0;
150 }
151 #endif /* GCM_USE_S390X_CRYPTO*/
152
153 #ifdef GCM_USE_PPC_VPMSUM
154 extern void _gcry_ghash_setup_ppc_vpmsum (void *gcm_table, void *gcm_key);
155
156 /* result is 128-bits */
157 extern unsigned int _gcry_ghash_ppc_vpmsum (byte *result, void *gcm_table,
158 const byte *buf, size_t nblocks);
159
160 static void
161 ghash_setup_ppc_vpmsum (gcry_cipher_hd_t c)
162 {
163 _gcry_ghash_setup_ppc_vpmsum(c->u_mode.gcm.gcm_table,
164 c->u_mode.gcm.u_ghash_key.key);
165 }
166
167 static unsigned int
168 ghash_ppc_vpmsum (gcry_cipher_hd_t c, byte *result, const byte *buf,
169 size_t nblocks)
170 {
171 return _gcry_ghash_ppc_vpmsum(result, c->u_mode.gcm.gcm_table, buf,
172 nblocks);
173 }
174 #endif /* GCM_USE_PPC_VPMSUM */
175
176 #ifdef GCM_USE_TABLES
177 static struct
178 {
179 volatile u32 counter_head;
180 u32 cacheline_align[64 / 4 - 1];
181 u16 R[256];
182 volatile u32 counter_tail;
183 } gcm_table ATTR_ALIGNED_64 =
184 {
185 0,
186 { 0, },
187 {
188 0x0000, 0x01c2, 0x0384, 0x0246, 0x0708, 0x06ca, 0x048c, 0x054e,
189 0x0e10, 0x0fd2, 0x0d94, 0x0c56, 0x0918, 0x08da, 0x0a9c, 0x0b5e,
190 0x1c20, 0x1de2, 0x1fa4, 0x1e66, 0x1b28, 0x1aea, 0x18ac, 0x196e,
191 0x1230, 0x13f2, 0x11b4, 0x1076, 0x1538, 0x14fa, 0x16bc, 0x177e,
192 0x3840, 0x3982, 0x3bc4, 0x3a06, 0x3f48, 0x3e8a, 0x3ccc, 0x3d0e,
193 0x3650, 0x3792, 0x35d4, 0x3416, 0x3158, 0x309a, 0x32dc, 0x331e,
194 0x2460, 0x25a2, 0x27e4, 0x2626, 0x2368, 0x22aa, 0x20ec, 0x212e,
195 0x2a70, 0x2bb2, 0x29f4, 0x2836, 0x2d78, 0x2cba, 0x2efc, 0x2f3e,
196 0x7080, 0x7142, 0x7304, 0x72c6, 0x7788, 0x764a, 0x740c, 0x75ce,
197 0x7e90, 0x7f52, 0x7d14, 0x7cd6, 0x7998, 0x785a, 0x7a1c, 0x7bde,
198 0x6ca0, 0x6d62, 0x6f24, 0x6ee6, 0x6ba8, 0x6a6a, 0x682c, 0x69ee,
199 0x62b0, 0x6372, 0x6134, 0x60f6, 0x65b8, 0x647a, 0x663c, 0x67fe,
200 0x48c0, 0x4902, 0x4b44, 0x4a86, 0x4fc8, 0x4e0a, 0x4c4c, 0x4d8e,
201 0x46d0, 0x4712, 0x4554, 0x4496, 0x41d8, 0x401a, 0x425c, 0x439e,
202 0x54e0, 0x5522, 0x5764, 0x56a6, 0x53e8, 0x522a, 0x506c, 0x51ae,
203 0x5af0, 0x5b32, 0x5974, 0x58b6, 0x5df8, 0x5c3a, 0x5e7c, 0x5fbe,
204 0xe100, 0xe0c2, 0xe284, 0xe346, 0xe608, 0xe7ca, 0xe58c, 0xe44e,
205 0xef10, 0xeed2, 0xec94, 0xed56, 0xe818, 0xe9da, 0xeb9c, 0xea5e,
206 0xfd20, 0xfce2, 0xfea4, 0xff66, 0xfa28, 0xfbea, 0xf9ac, 0xf86e,
207 0xf330, 0xf2f2, 0xf0b4, 0xf176, 0xf438, 0xf5fa, 0xf7bc, 0xf67e,
208 0xd940, 0xd882, 0xdac4, 0xdb06, 0xde48, 0xdf8a, 0xddcc, 0xdc0e,
209 0xd750, 0xd692, 0xd4d4, 0xd516, 0xd058, 0xd19a, 0xd3dc, 0xd21e,
210 0xc560, 0xc4a2, 0xc6e4, 0xc726, 0xc268, 0xc3aa, 0xc1ec, 0xc02e,
211 0xcb70, 0xcab2, 0xc8f4, 0xc936, 0xcc78, 0xcdba, 0xcffc, 0xce3e,
212 0x9180, 0x9042, 0x9204, 0x93c6, 0x9688, 0x974a, 0x950c, 0x94ce,
213 0x9f90, 0x9e52, 0x9c14, 0x9dd6, 0x9898, 0x995a, 0x9b1c, 0x9ade,
214 0x8da0, 0x8c62, 0x8e24, 0x8fe6, 0x8aa8, 0x8b6a, 0x892c, 0x88ee,
215 0x83b0, 0x8272, 0x8034, 0x81f6, 0x84b8, 0x857a, 0x873c, 0x86fe,
216 0xa9c0, 0xa802, 0xaa44, 0xab86, 0xaec8, 0xaf0a, 0xad4c, 0xac8e,
217 0xa7d0, 0xa612, 0xa454, 0xa596, 0xa0d8, 0xa11a, 0xa35c, 0xa29e,
218 0xb5e0, 0xb422, 0xb664, 0xb7a6, 0xb2e8, 0xb32a, 0xb16c, 0xb0ae,
219 0xbbf0, 0xba32, 0xb874, 0xb9b6, 0xbcf8, 0xbd3a, 0xbf7c, 0xbebe,
220 },
221 0
222 };
223
224 #define gcmR gcm_table.R
225
226 static inline
227 void prefetch_table(const void *tab, size_t len)
228 {
229 const volatile byte *vtab = tab;
230 size_t i;
231
232 for (i = 0; len - i >= 8 * 32; i += 8 * 32)
233 {
234 (void)vtab[i + 0 * 32];
235 (void)vtab[i + 1 * 32];
236 (void)vtab[i + 2 * 32];
237 (void)vtab[i + 3 * 32];
238 (void)vtab[i + 4 * 32];
239 (void)vtab[i + 5 * 32];
240 (void)vtab[i + 6 * 32];
241 (void)vtab[i + 7 * 32];
242 }
243 for (; i < len; i += 32)
244 {
245 (void)vtab[i];
246 }
247
248 (void)vtab[len - 1];
249 }
250
251 static inline void
252 do_prefetch_tables (const void *gcmM, size_t gcmM_size)
253 {
254 /* Modify counters to trigger copy-on-write and unsharing if physical pages
255 * of look-up table are shared between processes. Modifying counters also
256 * causes checksums for pages to change and hint same-page merging algorithm
257 * that these pages are frequently changing. */
258 gcm_table.counter_head++;
259 gcm_table.counter_tail++;
260
261 /* Prefetch look-up tables to cache. */
262 prefetch_table(gcmM, gcmM_size);
263 prefetch_table(&gcm_table, sizeof(gcm_table));
264 }
265
266 #ifdef GCM_TABLES_USE_U64
267 static void
268 bshift (u64 * b0, u64 * b1)
269 {
270 u64 t[2], mask;
271
272 t[0] = *b0;
273 t[1] = *b1;
274 mask = -(t[1] & 1) & 0xe1;
275 mask <<= 56;
276
277 *b1 = (t[1] >> 1) ^ (t[0] << 63);
278 *b0 = (t[0] >> 1) ^ mask;
279 }
280
281 static void
282 do_fillM (unsigned char *h, u64 *M)
283 {
284 int i, j;
285
286 M[0 + 0] = 0;
287 M[0 + 16] = 0;
288
289 M[8 + 0] = buf_get_be64 (h + 0);
290 M[8 + 16] = buf_get_be64 (h + 8);
291
292 for (i = 4; i > 0; i /= 2)
293 {
294 M[i + 0] = M[2 * i + 0];
295 M[i + 16] = M[2 * i + 16];
296
297 bshift (&M[i], &M[i + 16]);
298 }
299
300 for (i = 2; i < 16; i *= 2)
301 for (j = 1; j < i; j++)
302 {
303 M[(i + j) + 0] = M[i + 0] ^ M[j + 0];
304 M[(i + j) + 16] = M[i + 16] ^ M[j + 16];
305 }
306
307 for (i = 0; i < 16; i++)
308 {
309 M[i + 32] = (M[i + 0] >> 4) ^ ((u64) gcmR[(M[i + 16] & 0xf) << 4] << 48);
310 M[i + 48] = (M[i + 16] >> 4) ^ (M[i + 0] << 60);
311 }
312 }
313
314 static inline unsigned int
315 do_ghash (unsigned char *result, const unsigned char *buf, const u64 *gcmM)
316 {
317 u64 V[2];
318 u64 tmp[2];
319 const u64 *M;
320 u64 T;
321 u32 A;
322 int i;
323
324 cipher_block_xor (V, result, buf, 16);
325 V[0] = be_bswap64 (V[0]);
326 V[1] = be_bswap64 (V[1]);
327
328 /* First round can be manually tweaked based on fact that 'tmp' is zero. */
329 M = &gcmM[(V[1] & 0xf) + 32];
330 V[1] >>= 4;
331 tmp[0] = M[0];
332 tmp[1] = M[16];
333 tmp[0] ^= gcmM[(V[1] & 0xf) + 0];
334 tmp[1] ^= gcmM[(V[1] & 0xf) + 16];
335 V[1] >>= 4;
336
337 i = 6;
338 while (1)
339 {
340 M = &gcmM[(V[1] & 0xf) + 32];
341 V[1] >>= 4;
342
343 A = tmp[1] & 0xff;
344 T = tmp[0];
345 tmp[0] = (T >> 8) ^ ((u64) gcmR[A] << 48) ^ gcmM[(V[1] & 0xf) + 0];
346 tmp[1] = (T << 56) ^ (tmp[1] >> 8) ^ gcmM[(V[1] & 0xf) + 16];
347
348 tmp[0] ^= M[0];
349 tmp[1] ^= M[16];
350
351 if (i == 0)
352 break;
353
354 V[1] >>= 4;
355 --i;
356 }
357
358 i = 7;
359 while (1)
360 {
361 M = &gcmM[(V[0] & 0xf) + 32];
362 V[0] >>= 4;
363
364 A = tmp[1] & 0xff;
365 T = tmp[0];
366 tmp[0] = (T >> 8) ^ ((u64) gcmR[A] << 48) ^ gcmM[(V[0] & 0xf) + 0];
367 tmp[1] = (T << 56) ^ (tmp[1] >> 8) ^ gcmM[(V[0] & 0xf) + 16];
368
369 tmp[0] ^= M[0];
370 tmp[1] ^= M[16];
371
372 if (i == 0)
373 break;
374
375 V[0] >>= 4;
376 --i;
377 }
378
379 buf_put_be64 (result + 0, tmp[0]);
380 buf_put_be64 (result + 8, tmp[1]);
381
382 return (sizeof(V) + sizeof(T) + sizeof(tmp) +
383 sizeof(int)*2 + sizeof(void*)*5);
384 }
385
386 #else /*!GCM_TABLES_USE_U64*/
387
388 static void
389 bshift (u32 * M, int i)
390 {
391 u32 t[4], mask;
392
393 t[0] = M[i * 4 + 0];
394 t[1] = M[i * 4 + 1];
395 t[2] = M[i * 4 + 2];
396 t[3] = M[i * 4 + 3];
397 mask = -(t[3] & 1) & 0xe1;
398
399 M[i * 4 + 3] = (t[3] >> 1) ^ (t[2] << 31);
400 M[i * 4 + 2] = (t[2] >> 1) ^ (t[1] << 31);
401 M[i * 4 + 1] = (t[1] >> 1) ^ (t[0] << 31);
402 M[i * 4 + 0] = (t[0] >> 1) ^ (mask << 24);
403 }
404
405 static void
406 do_fillM (unsigned char *h, u32 *M)
407 {
408 int i, j;
409
410 M[0 * 4 + 0] = 0;
411 M[0 * 4 + 1] = 0;
412 M[0 * 4 + 2] = 0;
413 M[0 * 4 + 3] = 0;
414
415 M[8 * 4 + 0] = buf_get_be32 (h + 0);
416 M[8 * 4 + 1] = buf_get_be32 (h + 4);
417 M[8 * 4 + 2] = buf_get_be32 (h + 8);
418 M[8 * 4 + 3] = buf_get_be32 (h + 12);
419
420 for (i = 4; i > 0; i /= 2)
421 {
422 M[i * 4 + 0] = M[2 * i * 4 + 0];
423 M[i * 4 + 1] = M[2 * i * 4 + 1];
424 M[i * 4 + 2] = M[2 * i * 4 + 2];
425 M[i * 4 + 3] = M[2 * i * 4 + 3];
426
427 bshift (M, i);
428 }
429
430 for (i = 2; i < 16; i *= 2)
431 for (j = 1; j < i; j++)
432 {
433 M[(i + j) * 4 + 0] = M[i * 4 + 0] ^ M[j * 4 + 0];
434 M[(i + j) * 4 + 1] = M[i * 4 + 1] ^ M[j * 4 + 1];
435 M[(i + j) * 4 + 2] = M[i * 4 + 2] ^ M[j * 4 + 2];
436 M[(i + j) * 4 + 3] = M[i * 4 + 3] ^ M[j * 4 + 3];
437 }
438
439 for (i = 0; i < 4 * 16; i += 4)
440 {
441 M[i + 0 + 64] = (M[i + 0] >> 4)
442 ^ ((u64) gcmR[(M[i + 3] << 4) & 0xf0] << 16);
443 M[i + 1 + 64] = (M[i + 1] >> 4) ^ (M[i + 0] << 28);
444 M[i + 2 + 64] = (M[i + 2] >> 4) ^ (M[i + 1] << 28);
445 M[i + 3 + 64] = (M[i + 3] >> 4) ^ (M[i + 2] << 28);
446 }
447 }
448
449 static inline unsigned int
450 do_ghash (unsigned char *result, const unsigned char *buf, const u32 *gcmM)
451 {
452 byte V[16];
453 u32 tmp[4];
454 u32 v;
455 const u32 *M, *m;
456 u32 T[3];
457 int i;
458
459 cipher_block_xor (V, result, buf, 16); /* V is big-endian */
460
461 /* First round can be manually tweaked based on fact that 'tmp' is zero. */
462 i = 15;
463
464 v = V[i];
465 M = &gcmM[(v & 0xf) * 4 + 64];
466 v = (v & 0xf0) >> 4;
467 m = &gcmM[v * 4];
468 v = V[--i];
469
470 tmp[0] = M[0] ^ m[0];
471 tmp[1] = M[1] ^ m[1];
472 tmp[2] = M[2] ^ m[2];
473 tmp[3] = M[3] ^ m[3];
474
475 while (1)
476 {
477 M = &gcmM[(v & 0xf) * 4 + 64];
478 v = (v & 0xf0) >> 4;
479 m = &gcmM[v * 4];
480
481 T[0] = tmp[0];
482 T[1] = tmp[1];
483 T[2] = tmp[2];
484 tmp[0] = (T[0] >> 8) ^ ((u32) gcmR[tmp[3] & 0xff] << 16) ^ m[0];
485 tmp[1] = (T[0] << 24) ^ (tmp[1] >> 8) ^ m[1];
486 tmp[2] = (T[1] << 24) ^ (tmp[2] >> 8) ^ m[2];
487 tmp[3] = (T[2] << 24) ^ (tmp[3] >> 8) ^ m[3];
488
489 tmp[0] ^= M[0];
490 tmp[1] ^= M[1];
491 tmp[2] ^= M[2];
492 tmp[3] ^= M[3];
493
494 if (i == 0)
495 break;
496
497 v = V[--i];
498 }
499
500 buf_put_be32 (result + 0, tmp[0]);
501 buf_put_be32 (result + 4, tmp[1]);
502 buf_put_be32 (result + 8, tmp[2]);
503 buf_put_be32 (result + 12, tmp[3]);
504
505 return (sizeof(V) + sizeof(T) + sizeof(tmp) +
506 sizeof(int)*2 + sizeof(void*)*6);
507 }
508 #endif /*!GCM_TABLES_USE_U64*/
509
510 #define fillM(c) \
511 do_fillM (c->u_mode.gcm.u_ghash_key.key, c->u_mode.gcm.gcm_table)
512 #define GHASH(c, result, buf) do_ghash (result, buf, c->u_mode.gcm.gcm_table)
513 #define prefetch_tables(c) \
514 do_prefetch_tables(c->u_mode.gcm.gcm_table, sizeof(c->u_mode.gcm.gcm_table))
515
516 #else
517
518 static unsigned long
519 bshift (unsigned long *b)
520 {
521 unsigned long c;
522 int i;
523 c = b[3] & 1;
524 for (i = 3; i > 0; i--)
525 {
526 b[i] = (b[i] >> 1) | (b[i - 1] << 31);
527 }
528 b[i] >>= 1;
529 return c;
530 }
531
532 static unsigned int
533 do_ghash (unsigned char *hsub, unsigned char *result, const unsigned char *buf)
534 {
535 unsigned long V[4];
536 int i, j;
537 byte *p;
538
539 #ifdef WORDS_BIGENDIAN
540 p = result;
541 #else
542 unsigned long T[4];
543
544 cipher_block_xor (V, result, buf, 16);
545 for (i = 0; i < 4; i++)
546 {
547 V[i] = (V[i] & 0x00ff00ff) << 8 | (V[i] & 0xff00ff00) >> 8;
548 V[i] = (V[i] & 0x0000ffff) << 16 | (V[i] & 0xffff0000) >> 16;
549 }
550 p = (byte *) T;
551 #endif
552
553 memset (p, 0, 16);
554
555 for (i = 0; i < 16; i++)
556 {
557 for (j = 0x80; j; j >>= 1)
558 {
559 if (hsub[i] & j)
560 cipher_block_xor (p, p, V, 16);
561 if (bshift (V))
562 V[0] ^= 0xe1000000;
563 }
564 }
565 #ifndef WORDS_BIGENDIAN
566 for (i = 0, p = (byte *) T; i < 16; i += 4, p += 4)
567 {
568 result[i + 0] = p[3];
569 result[i + 1] = p[2];
570 result[i + 2] = p[1];
571 result[i + 3] = p[0];
572 }
573 #endif
574
575 return (sizeof(V) + sizeof(T) + sizeof(int)*2 + sizeof(void*)*5);
576 }
577
578 #define fillM(c) do { } while (0)
579 #define GHASH(c, result, buf) do_ghash (c->u_mode.gcm.u_ghash_key.key, result, buf)
580 #define prefetch_tables(c) do {} while (0)
581
582 #endif /* !GCM_USE_TABLES */
583
584
585 static unsigned int
586 ghash_internal (gcry_cipher_hd_t c, byte *result, const byte *buf,
587 size_t nblocks)
588 {
589 const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
590 unsigned int burn = 0;
591
592 prefetch_tables (c);
593
594 while (nblocks)
595 {
596 burn = GHASH (c, result, buf);
597 buf += blocksize;
598 nblocks--;
599 }
600
601 return burn + (burn ? 5*sizeof(void*) : 0);
602 }
603
604
605 static void
606 setupM (gcry_cipher_hd_t c)
607 {
608 unsigned int features = _gcry_get_hw_features ();
609
610 c->u_mode.gcm.ghash_fn = NULL;
611 c->u_mode.gcm.polyval_fn = NULL;
612
613 if (0)
614 {
615 (void)features;
616 }
617 #ifdef GCM_USE_INTEL_PCLMUL
618 else if (features & HWF_INTEL_PCLMUL)
619 {
620 _gcry_ghash_setup_intel_pclmul (c, features);
621 }
622 #endif
623 #ifdef GCM_USE_ARM_PMULL
624 else if (features & HWF_ARM_PMULL)
625 {
626 c->u_mode.gcm.ghash_fn = ghash_armv8_ce_pmull;
627 c->u_mode.gcm.polyval_fn = polyval_armv8_ce_pmull;
628 ghash_setup_armv8_ce_pmull (c);
629 }
630 #endif
631 #ifdef GCM_USE_ARM_NEON
632 else if (features & HWF_ARM_NEON)
633 {
634 c->u_mode.gcm.ghash_fn = ghash_armv7_neon;
635 ghash_setup_armv7_neon (c);
636 }
637 #endif
638 #ifdef GCM_USE_AARCH64
639 else if (features & HWF_ARM_NEON)
640 {
641 c->u_mode.gcm.ghash_fn = _gcry_ghash_aarch64_simd;
642 _gcry_ghash_setup_aarch64_simd (c);
643 }
644 #endif
645 #ifdef GCM_USE_RISCV_ZVKG
646 else if ((features & HWF_RISCV_IMAFDC)
647 && (features & HWF_RISCV_B) /* Mandatory in RVA23U64 */
648 && (features & HWF_RISCV_V) /* Mandatory in RVA23U64 */
649 && (features & HWF_RISCV_ZVKG) /* Optional in RVA23U64 */
650 && _gcry_ghash_setup_riscv_zvkg (c))
651 {
652 c->u_mode.gcm.ghash_fn = _gcry_ghash_riscv_zvkg;
653 c->u_mode.gcm.polyval_fn = _gcry_polyval_riscv_zvkg;
654 }
655 #endif
656 #ifdef GCM_USE_RISCV_ZBB_ZBC
657 else if ((features & HWF_RISCV_IMAFDC)
658 && (features & HWF_RISCV_ZBB)
659 && (features & HWF_RISCV_ZBC))
660 {
661 c->u_mode.gcm.ghash_fn = _gcry_ghash_riscv_zbb_zbc;
662 c->u_mode.gcm.polyval_fn = _gcry_polyval_riscv_zbb_zbc;
663 _gcry_ghash_setup_riscv_zbb_zbc (c);
664 }
665 #endif
666 #ifdef GCM_USE_PPC_VPMSUM
667 else if (features & HWF_PPC_VCRYPTO)
668 {
669 c->u_mode.gcm.ghash_fn = ghash_ppc_vpmsum;
670 ghash_setup_ppc_vpmsum (c);
671 }
672 #endif
673 #ifdef GCM_USE_S390X_CRYPTO
674 else if (features & HWF_S390X_MSA)
675 {
676 if (kimd_query () & km_function_to_mask (KMID_FUNCTION_GHASH))
677 {
678 c->u_mode.gcm.ghash_fn = ghash_s390x_kimd;
679 }
680 }
681 #endif
682
683 if (c->u_mode.gcm.ghash_fn == NULL)
684 {
685 c->u_mode.gcm.ghash_fn = ghash_internal;
686 fillM (c);
687 }
688 }
689
690
691 static inline void
692 gcm_bytecounter_add (u32 ctr[2], size_t add)
693 {
694 if (sizeof(add) > sizeof(u32))
695 {
696 u32 high_add = ((add >> 31) >> 1) & 0xffffffff;
697 ctr[1] += high_add;
698 }
699
700 ctr[0] += add;
701 if (ctr[0] >= add)
702 return;
703 ++ctr[1];
704 }
705
706
707 static inline u32
708 gcm_add32_be128 (byte *ctr, unsigned int add)
709 {
710 /* 'ctr' must be aligned to four bytes. */
711 const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
712 u32 *pval = (u32 *)(void *)(ctr + blocksize - sizeof(u32));
713 u32 val;
714
715 val = be_bswap32(*pval) + add;
716 *pval = be_bswap32(val);
717
718 return val; /* return result as host-endian value */
719 }
720
721
722 static inline int
723 gcm_check_datalen (u32 ctr[2])
724 {
725 /* len(plaintext) <= 2^39-256 bits == 2^36-32 bytes == 2^32-2 blocks */
726 if (ctr[1] > 0xfU)
727 return 0;
728 if (ctr[1] < 0xfU)
729 return 1;
730
731 if (ctr[0] <= 0xffffffe0U)
732 return 1;
733
734 return 0;
735 }
736
737
738 static inline int
739 gcm_check_aadlen_or_ivlen (u32 ctr[2])
740 {
741 /* len(aad/iv) <= 2^64-1 bits ~= 2^61-1 bytes */
742 if (ctr[1] > 0x1fffffffU)
743 return 0;
744 if (ctr[1] < 0x1fffffffU)
745 return 1;
746
747 if (ctr[0] <= 0xffffffffU)
748 return 1;
749
750 return 0;
751 }
752
753
754 static void
755 do_ghash_buf(gcry_cipher_hd_t c, byte *hash, const byte *buf,
756 size_t buflen, int do_padding)
757 {
758 unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
759 unsigned int unused = c->u_mode.gcm.mac_unused;
760 ghash_fn_t ghash_fn = c->u_mode.gcm.ghash_fn;
761 size_t nblocks, n;
762 unsigned int burn = 0;
763
764 if (buflen == 0 && (unused == 0 || !do_padding))
765 return;
766
767 do
768 {
769 if (buflen > 0 && (buflen + unused < blocksize || unused > 0))
770 {
771 n = blocksize - unused;
772 n = n < buflen ? n : buflen;
773
774 buf_cpy (&c->u_mode.gcm.macbuf[unused], buf, n);
775
776 unused += n;
777 buf += n;
778 buflen -= n;
779 }
780 if (!buflen)
781 {
782 if (!do_padding && unused < blocksize)
783 {
784 break;
785 }
786
787 n = blocksize - unused;
788 if (n > 0)
789 {
790 memset (&c->u_mode.gcm.macbuf[unused], 0, n);
791 unused = blocksize;
792 }
793 }
794
795 if (unused > 0)
796 {
797 gcry_assert (unused == blocksize);
798
799 /* Process one block from macbuf. */
800 burn = ghash_fn (c, hash, c->u_mode.gcm.macbuf, 1);
801 unused = 0;
802 }
803
804 nblocks = buflen / blocksize;
805
806 if (nblocks)
807 {
808 burn = ghash_fn (c, hash, buf, nblocks);
809 buf += blocksize * nblocks;
810 buflen -= blocksize * nblocks;
811 }
812 }
813 while (buflen > 0);
814
815 c->u_mode.gcm.mac_unused = unused;
816
817 if (burn)
818 _gcry_burn_stack (burn);
819 }
820
821
822 static gcry_err_code_t
823 gcm_ctr_encrypt (gcry_cipher_hd_t c, byte *outbuf, size_t outbuflen,
824 const byte *inbuf, size_t inbuflen)
825 {
826 gcry_err_code_t err = 0;
827
828 while (inbuflen)
829 {
830 u32 nblocks_to_overflow;
831 u32 num_ctr_increments;
832 u32 curr_ctr_low;
833 size_t currlen = inbuflen;
834 byte ctr_copy[GCRY_GCM_BLOCK_LEN];
835 int fix_ctr = 0;
836
837 /* GCM CTR increments only least significant 32-bits, without carry
838 * to upper 96-bits of counter. Using generic CTR implementation
839 * directly would carry 32-bit overflow to upper 96-bit. Detect
840 * if input length is long enough to cause overflow, and limit
841 * input length so that CTR overflow happen but updated CTR value is
842 * not used to encrypt further input. After overflow, upper 96 bits
843 * of CTR are restored to cancel out modification done by generic CTR
844 * encryption. */
845
846 if (inbuflen > c->unused)
847 {
848 curr_ctr_low = gcm_add32_be128 (c->u_ctr.ctr, 0);
849
850 /* Number of CTR increments this inbuflen would cause. */
851 num_ctr_increments = (inbuflen - c->unused) / GCRY_GCM_BLOCK_LEN +
852 !!((inbuflen - c->unused) % GCRY_GCM_BLOCK_LEN);
853
854 if ((u32)(num_ctr_increments + curr_ctr_low) < curr_ctr_low)
855 {
856 nblocks_to_overflow = 0xffffffffU - curr_ctr_low + 1;
857 currlen = nblocks_to_overflow * GCRY_GCM_BLOCK_LEN + c->unused;
858 if (currlen > inbuflen)
859 {
860 currlen = inbuflen;
861 }
862
863 fix_ctr = 1;
864 cipher_block_cpy(ctr_copy, c->u_ctr.ctr, GCRY_GCM_BLOCK_LEN);
865 }
866 }
867
868 err = _gcry_cipher_ctr_encrypt(c, outbuf, outbuflen, inbuf, currlen);
869 if (err != 0)
870 return err;
871
872 if (fix_ctr)
873 {
874 /* Lower 32-bits of CTR should now be zero. */
875 gcry_assert(gcm_add32_be128 (c->u_ctr.ctr, 0) == 0);
876
877 /* Restore upper part of CTR. */
878 buf_cpy(c->u_ctr.ctr, ctr_copy, GCRY_GCM_BLOCK_LEN - sizeof(u32));
879
880 wipememory(ctr_copy, sizeof(ctr_copy));
881 }
882
883 inbuflen -= currlen;
884 inbuf += currlen;
885 outbuflen -= currlen;
886 outbuf += currlen;
887 }
888
889 return err;
890 }
891
892
893 static gcry_err_code_t
894 gcm_crypt_inner (gcry_cipher_hd_t c, byte *outbuf, size_t outbuflen,
895 const byte *inbuf, size_t inbuflen, int encrypt)
896 {
897 gcry_err_code_t err;
898
899 while (inbuflen)
900 {
901 size_t currlen = inbuflen;
902
903 /* Use a bulk method if available. */
904 if (c->bulk.gcm_crypt)
905 {
906 /* Bulk method requires that there is no cached data. */
907 if (inbuflen >= GCRY_GCM_BLOCK_LEN && c->u_mode.gcm.mac_unused == 0)
908 {
909 size_t nblks = inbuflen / GCRY_GCM_BLOCK_LEN;
910 size_t nleft;
911 size_t ndone;
912
913 nleft = c->bulk.gcm_crypt (c, outbuf, inbuf, nblks, encrypt);
914 ndone = nblks - nleft;
915
916 inbuf += ndone * GCRY_GCM_BLOCK_LEN;
917 outbuf += ndone * GCRY_GCM_BLOCK_LEN;
918 inbuflen -= ndone * GCRY_GCM_BLOCK_LEN;
919 outbuflen -= ndone * GCRY_GCM_BLOCK_LEN;
920
921 if (inbuflen == 0)
922 break;
923
924 currlen = inbuflen;
925 }
926 else if (c->u_mode.gcm.mac_unused > 0
927 && inbuflen >= GCRY_GCM_BLOCK_LEN
928 + (16 - c->u_mode.gcm.mac_unused))
929 {
930 /* Handle just enough data so that cache is depleted, and on
931 * next loop iteration use bulk method. */
932 currlen = 16 - c->u_mode.gcm.mac_unused;
933
934 gcry_assert(currlen);
935 }
936 }
937
938 /* Since checksumming is done after/before encryption/decryption,
939 * process input in 24KiB chunks to keep data loaded in L1 cache for
940 * checksumming/decryption. However only do splitting if input is
941 * large enough so that last chunks does not end up being short. */
942 if (currlen > 32 * 1024)
943 currlen = 24 * 1024;
944
945 if (!encrypt)
946 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, inbuf, currlen, 0);
947
948 err = gcm_ctr_encrypt(c, outbuf, outbuflen, inbuf, currlen);
949 if (err != 0)
950 return err;
951
952 if (encrypt)
953 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, outbuf, currlen, 0);
954
955 outbuf += currlen;
956 inbuf += currlen;
957 outbuflen -= currlen;
958 inbuflen -= currlen;
959 }
960
961 return 0;
962 }
963
964
965 gcry_err_code_t
966 _gcry_cipher_gcm_encrypt (gcry_cipher_hd_t c,
967 byte *outbuf, size_t outbuflen,
968 const byte *inbuf, size_t inbuflen)
969 {
970 if (c->spec->blocksize != GCRY_GCM_BLOCK_LEN)
971 return GPG_ERR_CIPHER_ALGO;
972 if (outbuflen < inbuflen)
973 return GPG_ERR_BUFFER_TOO_SHORT;
974 if (c->u_mode.gcm.datalen_over_limits)
975 return GPG_ERR_INV_LENGTH;
976 if (c->marks.tag
977 || c->u_mode.gcm.ghash_data_finalized
978 || !c->u_mode.gcm.ghash_fn)
979 return GPG_ERR_INV_STATE;
980
981 if (!c->marks.iv)
982 _gcry_cipher_gcm_setiv_zero (c);
983
984 if (c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode)
985 return GPG_ERR_INV_STATE;
986
987 if (!c->u_mode.gcm.ghash_aad_finalized)
988 {
989 /* Start of encryption marks end of AAD stream. */
990 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, NULL, 0, 1);
991 c->u_mode.gcm.ghash_aad_finalized = 1;
992 }
993
994 gcm_bytecounter_add(c->u_mode.gcm.datalen, inbuflen);
995 if (!gcm_check_datalen(c->u_mode.gcm.datalen))
996 {
997 c->u_mode.gcm.datalen_over_limits = 1;
998 return GPG_ERR_INV_LENGTH;
999 }
1000
1001 return gcm_crypt_inner (c, outbuf, outbuflen, inbuf, inbuflen, 1);
1002 }
1003
1004
1005 gcry_err_code_t
1006 _gcry_cipher_gcm_decrypt (gcry_cipher_hd_t c,
1007 byte *outbuf, size_t outbuflen,
1008 const byte *inbuf, size_t inbuflen)
1009 {
1010 if (c->spec->blocksize != GCRY_GCM_BLOCK_LEN)
1011 return GPG_ERR_CIPHER_ALGO;
1012 if (outbuflen < inbuflen)
1013 return GPG_ERR_BUFFER_TOO_SHORT;
1014 if (c->u_mode.gcm.datalen_over_limits)
1015 return GPG_ERR_INV_LENGTH;
1016 if (c->marks.tag
1017 || c->u_mode.gcm.ghash_data_finalized
1018 || !c->u_mode.gcm.ghash_fn)
1019 return GPG_ERR_INV_STATE;
1020
1021 if (!c->marks.iv)
1022 _gcry_cipher_gcm_setiv_zero (c);
1023
1024 if (!c->u_mode.gcm.ghash_aad_finalized)
1025 {
1026 /* Start of decryption marks end of AAD stream. */
1027 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, NULL, 0, 1);
1028 c->u_mode.gcm.ghash_aad_finalized = 1;
1029 }
1030
1031 gcm_bytecounter_add(c->u_mode.gcm.datalen, inbuflen);
1032 if (!gcm_check_datalen(c->u_mode.gcm.datalen))
1033 {
1034 c->u_mode.gcm.datalen_over_limits = 1;
1035 return GPG_ERR_INV_LENGTH;
1036 }
1037
1038 return gcm_crypt_inner (c, outbuf, outbuflen, inbuf, inbuflen, 0);
1039 }
1040
1041
1042 gcry_err_code_t
1043 _gcry_cipher_gcm_authenticate (gcry_cipher_hd_t c,
1044 const byte * aadbuf, size_t aadbuflen)
1045 {
1046 if (c->spec->blocksize != GCRY_GCM_BLOCK_LEN)
1047 return GPG_ERR_CIPHER_ALGO;
1048 if (c->u_mode.gcm.datalen_over_limits)
1049 return GPG_ERR_INV_LENGTH;
1050 if (c->marks.tag
1051 || c->u_mode.gcm.ghash_aad_finalized
1052 || c->u_mode.gcm.ghash_data_finalized
1053 || !c->u_mode.gcm.ghash_fn)
1054 return GPG_ERR_INV_STATE;
1055
1056 if (!c->marks.iv)
1057 _gcry_cipher_gcm_setiv_zero (c);
1058
1059 gcm_bytecounter_add(c->u_mode.gcm.aadlen, aadbuflen);
1060 if (!gcm_check_aadlen_or_ivlen(c->u_mode.gcm.aadlen))
1061 {
1062 c->u_mode.gcm.datalen_over_limits = 1;
1063 return GPG_ERR_INV_LENGTH;
1064 }
1065
1066 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, aadbuf, aadbuflen, 0);
1067
1068 return 0;
1069 }
1070
1071
1072 void
1073 _gcry_cipher_gcm_setupM (gcry_cipher_hd_t c)
1074 {
1075 setupM (c);
1076 }
1077
1078
1079 void
1080 _gcry_cipher_gcm_setkey (gcry_cipher_hd_t c)
1081 {
1082 memset (c->u_mode.gcm.u_ghash_key.key, 0, GCRY_GCM_BLOCK_LEN);
1083
1084 c->spec->encrypt (&c->context.c, c->u_mode.gcm.u_ghash_key.key,
1085 c->u_mode.gcm.u_ghash_key.key);
1086 setupM (c);
1087 }
1088
1089
1090 static gcry_err_code_t
1091 _gcry_cipher_gcm_initiv (gcry_cipher_hd_t c, const byte *iv, size_t ivlen)
1092 {
1093 memset (c->u_mode.gcm.aadlen, 0, sizeof(c->u_mode.gcm.aadlen));
1094 memset (c->u_mode.gcm.datalen, 0, sizeof(c->u_mode.gcm.datalen));
1095 memset (c->u_mode.gcm.u_tag.tag, 0, GCRY_GCM_BLOCK_LEN);
1096 c->u_mode.gcm.datalen_over_limits = 0;
1097 c->u_mode.gcm.ghash_data_finalized = 0;
1098 c->u_mode.gcm.ghash_aad_finalized = 0;
1099
1100 if (ivlen == 0)
1101 return GPG_ERR_INV_LENGTH;
1102
1103 if (ivlen != GCRY_GCM_BLOCK_LEN - 4)
1104 {
1105 u32 iv_bytes[2] = {0, 0};
1106 u32 bitlengths[2][2];
1107
1108 if (!c->u_mode.gcm.ghash_fn)
1109 return GPG_ERR_INV_STATE;
1110
1111 memset(c->u_ctr.ctr, 0, GCRY_GCM_BLOCK_LEN);
1112
1113 gcm_bytecounter_add(iv_bytes, ivlen);
1114 if (!gcm_check_aadlen_or_ivlen(iv_bytes))
1115 {
1116 c->u_mode.gcm.datalen_over_limits = 1;
1117 return GPG_ERR_INV_LENGTH;
1118 }
1119
1120 do_ghash_buf(c, c->u_ctr.ctr, iv, ivlen, 1);
1121
1122 /* iv length, 64-bit */
1123 bitlengths[1][1] = be_bswap32(iv_bytes[0] << 3);
1124 bitlengths[1][0] = be_bswap32((iv_bytes[0] >> 29) |
1125 (iv_bytes[1] << 3));
1126 /* zeros, 64-bit */
1127 bitlengths[0][1] = 0;
1128 bitlengths[0][0] = 0;
1129
1130 do_ghash_buf(c, c->u_ctr.ctr, (byte*)bitlengths, GCRY_GCM_BLOCK_LEN, 1);
1131
1132 wipememory (iv_bytes, sizeof iv_bytes);
1133 wipememory (bitlengths, sizeof bitlengths);
1134 }
1135 else
1136 {
1137 /* 96-bit IV is handled differently. */
1138 memcpy (c->u_ctr.ctr, iv, ivlen);
1139 c->u_ctr.ctr[12] = c->u_ctr.ctr[13] = c->u_ctr.ctr[14] = 0;
1140 c->u_ctr.ctr[15] = 1;
1141 }
1142
1143 c->spec->encrypt (&c->context.c, c->u_mode.gcm.tagiv, c->u_ctr.ctr);
1144
1145 gcm_add32_be128 (c->u_ctr.ctr, 1);
1146
1147 c->unused = 0;
1148 c->marks.iv = 1;
1149 c->marks.tag = 0;
1150
1151 return 0;
1152 }
1153
1154
1155 gcry_err_code_t
1156 _gcry_cipher_gcm_setiv (gcry_cipher_hd_t c, const byte *iv, size_t ivlen)
1157 {
1158 c->marks.iv = 0;
1159 c->marks.tag = 0;
1160
1161 return _gcry_cipher_gcm_initiv (c, iv, ivlen);
1162 }
1163
1164 static gcry_err_code_t
1165 _gcry_cipher_gcm_setiv_zero (gcry_cipher_hd_t c)
1166 {
1167 static const unsigned char zerobuf[MAX_BLOCKSIZE];
1168
1169 c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 0;
1170
1171 if (fips_mode ())
1172 {
1173 /* Direct invocation of GCM setiv in FIPS mode disables encryption. */
1174 c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 1;
1175 }
1176
1177 return _gcry_cipher_gcm_setiv (c, zerobuf, GCRY_GCM_BLOCK_LEN);
1178 }
1179
1180
1181 #if 0 && TODO
1182 void
1183 _gcry_cipher_gcm_geniv (gcry_cipher_hd_t c,
1184 byte *ivout, size_t ivoutlen, const byte *nonce,
1185 size_t noncelen)
1186 {
1187 /* nonce: user provided part (might be null) */
1188 /* noncelen: check if proper length (if nonce not null) */
1189 /* ivout: iv used to initialize gcm, output to user */
1190 /* ivoutlen: check correct size */
1191 byte iv[IVLEN];
1192
1193 if (!ivout)
1194 return GPG_ERR_INV_ARG;
1195 if (ivoutlen != IVLEN)
1196 return GPG_ERR_INV_LENGTH;
1197 if (nonce != NULL && !is_nonce_ok_len(noncelen))
1198 return GPG_ERR_INV_ARG;
1199
1200 gcm_generate_iv(iv, nonce, noncelen);
1201
1202 c->marks.iv = 0;
1203 c->marks.tag = 0;
1204 c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 0;
1205
1206 _gcry_cipher_gcm_initiv (c, iv, IVLEN);
1207
1208 buf_cpy(ivout, iv, IVLEN);
1209 wipememory(iv, sizeof(iv));
1210 }
1211 #endif
1212
1213
1214 static int
1215 is_tag_length_valid(size_t taglen)
1216 {
1217 switch (taglen)
1218 {
1219 /* Allowed tag lengths from NIST SP 800-38D. */
1220 case 128 / 8: /* GCRY_GCM_BLOCK_LEN */
1221 case 120 / 8:
1222 case 112 / 8:
1223 case 104 / 8:
1224 case 96 / 8:
1225 case 64 / 8:
1226 case 32 / 8:
1227 return 1;
1228
1229 default:
1230 return 0;
1231 }
1232 }
1233
1234 static gcry_err_code_t
1235 _gcry_cipher_gcm_tag (gcry_cipher_hd_t c,
1236 byte * outbuf, size_t outbuflen, int check)
1237 {
1238 if (!(is_tag_length_valid (outbuflen) || outbuflen >= GCRY_GCM_BLOCK_LEN))
1239 return GPG_ERR_INV_LENGTH;
1240 if (c->u_mode.gcm.datalen_over_limits)
1241 return GPG_ERR_INV_LENGTH;
1242
1243 if (!c->marks.tag)
1244 {
1245 u32 bitlengths[2][2];
1246
1247 if (!c->u_mode.gcm.ghash_fn)
1248 return GPG_ERR_INV_STATE;
1249
1250 /* aad length */
1251 bitlengths[0][1] = be_bswap32(c->u_mode.gcm.aadlen[0] << 3);
1252 bitlengths[0][0] = be_bswap32((c->u_mode.gcm.aadlen[0] >> 29) |
1253 (c->u_mode.gcm.aadlen[1] << 3));
1254 /* data length */
1255 bitlengths[1][1] = be_bswap32(c->u_mode.gcm.datalen[0] << 3);
1256 bitlengths[1][0] = be_bswap32((c->u_mode.gcm.datalen[0] >> 29) |
1257 (c->u_mode.gcm.datalen[1] << 3));
1258
1259 /* Finalize data-stream. */
1260 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, NULL, 0, 1);
1261 c->u_mode.gcm.ghash_aad_finalized = 1;
1262 c->u_mode.gcm.ghash_data_finalized = 1;
1263
1264 /* Add bitlengths to tag. */
1265 do_ghash_buf(c, c->u_mode.gcm.u_tag.tag, (byte*)bitlengths,
1266 GCRY_GCM_BLOCK_LEN, 1);
1267 cipher_block_xor (c->u_mode.gcm.u_tag.tag, c->u_mode.gcm.tagiv,
1268 c->u_mode.gcm.u_tag.tag, GCRY_GCM_BLOCK_LEN);
1269 c->marks.tag = 1;
1270
1271 wipememory (bitlengths, sizeof (bitlengths));
1272 wipememory (c->u_mode.gcm.macbuf, GCRY_GCM_BLOCK_LEN);
1273 wipememory (c->u_mode.gcm.tagiv, GCRY_GCM_BLOCK_LEN);
1274 wipememory (c->u_mode.gcm.aadlen, sizeof (c->u_mode.gcm.aadlen));
1275 wipememory (c->u_mode.gcm.datalen, sizeof (c->u_mode.gcm.datalen));
1276 }
1277
1278 if (!check)
1279 {
1280 if (outbuflen > GCRY_GCM_BLOCK_LEN)
1281 outbuflen = GCRY_GCM_BLOCK_LEN;
1282
1283 /* NB: We already checked that OUTBUF is large enough to hold
1284 * the result or has valid truncated length. */
1285 memcpy (outbuf, c->u_mode.gcm.u_tag.tag, outbuflen);
1286 }
1287 else
1288 {
1289 /* OUTBUFLEN gives the length of the user supplied tag in OUTBUF
1290 * and thus we need to compare its length first. */
1291 if (!is_tag_length_valid (outbuflen)
1292 || !buf_eq_const (outbuf, c->u_mode.gcm.u_tag.tag, outbuflen))
1293 return GPG_ERR_CHECKSUM;
1294 }
1295
1296 return 0;
1297 }
1298
1299
1300 gcry_err_code_t
1301 _gcry_cipher_gcm_get_tag (gcry_cipher_hd_t c, unsigned char *outtag,
1302 size_t taglen)
1303 {
1304 /* Outputting authentication tag is part of encryption. */
1305 if (c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode)
1306 return GPG_ERR_INV_STATE;
1307
1308 return _gcry_cipher_gcm_tag (c, outtag, taglen, 0);
1309 }
1310
1311 gcry_err_code_t
1312 _gcry_cipher_gcm_check_tag (gcry_cipher_hd_t c, const unsigned char *intag,
1313 size_t taglen)
1314 {
1315 return _gcry_cipher_gcm_tag (c, (unsigned char *) intag, taglen, 1);
1316 }