Debian Codegraph @

cipher/cipher-gcm-siv.c

raw
1 /* cipher-gcm-siv.c - GCM-SIV implementation (RFC 8452)
2 * Copyright (C) 2021 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "g10lib.h"
27 #include "cipher.h"
28 #include "bufhelp.h"
29 #include "./cipher-internal.h"
30
31
32 #define GCM_SIV_NONCE_LENGTH (96 / 8)
33
34
35 static inline void
36 mulx_ghash (byte *a)
37 {
38 u64 t[2], mask;
39
40 t[0] = buf_get_be64(a + 0);
41 t[1] = buf_get_be64(a + 8);
42 mask = -(t[1] & 1) & 0xe1;
43 mask <<= 56;
44
45 buf_put_be64(a + 8, (t[1] >> 1) ^ (t[0] << 63));
46 buf_put_be64(a + 0, (t[0] >> 1) ^ mask);
47 }
48
49
50 static inline void
51 gcm_siv_bytecounter_add (u32 ctr[2], size_t add)
52 {
53 if (sizeof(add) > sizeof(u32))
54 {
55 u32 high_add = ((add >> 31) >> 1) & 0xffffffff;
56 ctr[1] += high_add;
57 }
58
59 ctr[0] += add;
60 if (ctr[0] >= add)
61 return;
62 ++ctr[1];
63 }
64
65
66 static inline int
67 gcm_siv_check_len (u32 ctr[2])
68 {
69 /* len(plaintext/aadlen) <= 2^39-256 bits == 2^36-32 bytes == 2^32-2 blocks */
70 if (ctr[1] > 0xfU)
71 return 0;
72 if (ctr[1] < 0xfU)
73 return 1;
74
75 if (ctr[0] <= 0xffffffe0U)
76 return 1;
77
78 return 0;
79 }
80
81
82 static void
83 polyval_set_key (gcry_cipher_hd_t c, const byte *auth_key)
84 {
85 cipher_block_bswap (c->u_mode.gcm.u_ghash_key.key, auth_key,
86 GCRY_SIV_BLOCK_LEN);
87 mulx_ghash (c->u_mode.gcm.u_ghash_key.key);
88 _gcry_cipher_gcm_setupM (c);
89 }
90
91
92 static void
93 do_polyval_buf(gcry_cipher_hd_t c, byte *hash, const byte *buf,
94 size_t buflen, int do_padding)
95 {
96 unsigned int blocksize = GCRY_SIV_BLOCK_LEN;
97 unsigned int unused = c->u_mode.gcm.mac_unused;
98 ghash_fn_t ghash_fn = c->u_mode.gcm.ghash_fn;
99 ghash_fn_t polyval_fn = c->u_mode.gcm.polyval_fn;
100 byte tmp_blocks[16][GCRY_SIV_BLOCK_LEN];
101 size_t nblocks, n;
102 unsigned int burn = 0, nburn;
103 unsigned int num_blks_used = 0;
104
105 if (buflen == 0 && (unused == 0 || !do_padding))
106 return;
107
108 do
109 {
110 if (buflen > 0 && (buflen + unused < blocksize || unused > 0))
111 {
112 n = blocksize - unused;
113 n = n < buflen ? n : buflen;
114
115 buf_cpy (&c->u_mode.gcm.macbuf[unused], buf, n);
116
117 unused += n;
118 buf += n;
119 buflen -= n;
120 }
121 if (!buflen)
122 {
123 if (!do_padding && unused < blocksize)
124 {
125 break;
126 }
127
128 n = blocksize - unused;
129 if (n > 0)
130 {
131 memset (&c->u_mode.gcm.macbuf[unused], 0, n);
132 unused = blocksize;
133 }
134 }
135
136 if (unused > 0)
137 {
138 gcry_assert (unused == blocksize);
139
140 /* Process one block from macbuf. */
141 if (polyval_fn)
142 {
143 nburn = polyval_fn (c, hash, c->u_mode.gcm.macbuf, 1);
144 }
145 else
146 {
147 cipher_block_bswap (c->u_mode.gcm.macbuf, c->u_mode.gcm.macbuf,
148 blocksize);
149 nburn = ghash_fn (c, hash, c->u_mode.gcm.macbuf, 1);
150 }
151
152 burn = nburn > burn ? nburn : burn;
153 unused = 0;
154 }
155
156 nblocks = buflen / blocksize;
157
158 while (nblocks)
159 {
160 if (polyval_fn)
161 {
162 n = nblocks;
163 nburn = polyval_fn (c, hash, buf, n);
164 }
165 else
166 {
167 for (n = 0; n < (nblocks > 16 ? 16 : nblocks); n++)
168 cipher_block_bswap (tmp_blocks[n], buf + n * blocksize,
169 blocksize);
170
171 num_blks_used = n > num_blks_used ? n : num_blks_used;
172
173 nburn = ghash_fn (c, hash, tmp_blocks[0], n);
174 }
175
176 burn = nburn > burn ? nburn : burn;
177 buf += n * blocksize;
178 buflen -= n * blocksize;
179 nblocks -= n;
180 }
181 }
182 while (buflen > 0);
183
184 c->u_mode.gcm.mac_unused = unused;
185
186 if (num_blks_used)
187 wipememory (tmp_blocks, num_blks_used * blocksize);
188 if (burn)
189 _gcry_burn_stack (burn);
190 }
191
192
193 static void
194 do_ctr_le32 (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
195 size_t inbuflen)
196 {
197 gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
198 unsigned char tmp[GCRY_SIV_BLOCK_LEN];
199 unsigned int burn = 0, nburn;
200 size_t nblocks;
201
202 if (inbuflen == 0)
203 return;
204
205 /* Use a bulk method if available. */
206 nblocks = inbuflen / GCRY_SIV_BLOCK_LEN;
207 if (nblocks && c->bulk.ctr32le_enc)
208 {
209 c->bulk.ctr32le_enc (c->context.c, c->u_ctr.ctr, outbuf, inbuf, nblocks);
210 inbuf += nblocks * GCRY_SIV_BLOCK_LEN;
211 outbuf += nblocks * GCRY_SIV_BLOCK_LEN;
212 inbuflen -= nblocks * GCRY_SIV_BLOCK_LEN;
213 }
214
215 do
216 {
217 nburn = enc_fn (c->context.c, tmp, c->u_ctr.ctr);
218 burn = nburn > burn ? nburn : burn;
219
220 buf_put_le32(c->u_ctr.ctr, buf_get_le32(c->u_ctr.ctr) + 1);
221
222 if (inbuflen < GCRY_SIV_BLOCK_LEN)
223 break;
224 cipher_block_xor(outbuf, inbuf, tmp, GCRY_SIV_BLOCK_LEN);
225
226 inbuflen -= GCRY_SIV_BLOCK_LEN;
227 outbuf += GCRY_SIV_BLOCK_LEN;
228 inbuf += GCRY_SIV_BLOCK_LEN;
229 }
230 while (inbuflen);
231
232 if (inbuflen)
233 {
234 buf_xor(outbuf, inbuf, tmp, inbuflen);
235
236 outbuf += inbuflen;
237 inbuf += inbuflen;
238 inbuflen -= inbuflen;
239 }
240
241 wipememory (tmp, sizeof(tmp));
242
243 if (burn > 0)
244 _gcry_burn_stack (burn + 4 * sizeof(void *));
245 }
246
247
248 static int
249 gcm_siv_selftest (gcry_cipher_hd_t c)
250 {
251 static const byte in1[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
252 "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
253 static const byte out1[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
254 "\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
255 static const byte in2[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
256 "\x9c\x98\xc0\x4d\xf9\x38\x7d\xed\x82\x81\x75\xa9\x2b\xa6\x52\xd8";
257 static const byte out2[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
258 "\x4e\x4c\x60\x26\xfc\x9c\x3e\xf6\xc1\x40\xba\xd4\x95\xd3\x29\x6c";
259 static const byte polyval_key[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
260 "\x25\x62\x93\x47\x58\x92\x42\x76\x1d\x31\xf8\x26\xba\x4b\x75\x7b";
261 static const byte ghash_key[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
262 "\xdc\xba\xa5\xdd\x13\x7c\x18\x8e\xbb\x21\x49\x2c\x23\xc9\xb1\x12";
263 static const byte polyval_data[GCRY_SIV_BLOCK_LEN * 2] _GCRY_GCC_ATTR_NONSTRING =
264 "\x4f\x4f\x95\x66\x8c\x83\xdf\xb6\x40\x17\x62\xbb\x2d\x01\xa2\x62"
265 "\xd1\xa2\x4d\xdd\x27\x21\xd0\x06\xbb\xe4\x5f\x20\xd3\xc9\xf3\x62";
266 static const byte polyval_tag[GCRY_SIV_BLOCK_LEN] _GCRY_GCC_ATTR_NONSTRING =
267 "\xf7\xa3\xb4\x7b\x84\x61\x19\xfa\xe5\xb7\x86\x6c\xf5\xe5\xb7\x7e";
268 byte tmp[GCRY_SIV_BLOCK_LEN];
269
270 /* Test mulx_ghash */
271 memcpy (tmp, in1, GCRY_SIV_BLOCK_LEN);
272 mulx_ghash (tmp);
273 if (memcmp (tmp, out1, GCRY_SIV_BLOCK_LEN) != 0)
274 return -1;
275
276 memcpy (tmp, in2, GCRY_SIV_BLOCK_LEN);
277 mulx_ghash (tmp);
278 if (memcmp (tmp, out2, GCRY_SIV_BLOCK_LEN) != 0)
279 return -1;
280
281 /* Test GHASH key generation */
282 memcpy (tmp, polyval_key, GCRY_SIV_BLOCK_LEN);
283 cipher_block_bswap (tmp, tmp, GCRY_SIV_BLOCK_LEN);
284 mulx_ghash (tmp);
285 if (memcmp (tmp, ghash_key, GCRY_SIV_BLOCK_LEN) != 0)
286 return -1;
287
288 /* Test POLYVAL */
289 memset (&c->u_mode.gcm, 0, sizeof(c->u_mode.gcm));
290 polyval_set_key (c, polyval_key);
291 memset (&tmp, 0, sizeof(tmp));
292 do_polyval_buf (c, tmp, polyval_data, GCRY_SIV_BLOCK_LEN * 2, 1);
293 cipher_block_bswap (tmp, tmp, GCRY_SIV_BLOCK_LEN);
294 if (memcmp (tmp, polyval_tag, GCRY_SIV_BLOCK_LEN) != 0)
295 return -1;
296
297 return 0;
298 }
299
300
301 gcry_err_code_t
302 _gcry_cipher_gcm_siv_setkey (gcry_cipher_hd_t c, unsigned int keylen)
303 {
304 static int done;
305
306 if (keylen != 16 && keylen != 32)
307 return GPG_ERR_INV_KEYLEN;
308
309 if (!done)
310 {
311 if (gcm_siv_selftest (c))
312 return GPG_ERR_SELFTEST_FAILED;
313
314 done = 1;
315 }
316
317 c->marks.iv = 0;
318 c->marks.tag = 0;
319 memset (&c->u_mode.gcm, 0, sizeof(c->u_mode.gcm));
320 c->u_mode.gcm.siv_keylen = keylen;
321 return 0;
322 }
323
324
325 gcry_err_code_t
326 _gcry_cipher_gcm_siv_set_nonce (gcry_cipher_hd_t c, const byte *iv,
327 size_t ivlen)
328 {
329 byte auth_key[GCRY_SIV_BLOCK_LEN];
330 byte tmp_in[GCRY_SIV_BLOCK_LEN];
331 byte tmp[GCRY_SIV_BLOCK_LEN];
332 byte enc_key[32];
333 gcry_err_code_t err;
334
335 if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN)
336 return GPG_ERR_CIPHER_ALGO;
337 if (ivlen != GCM_SIV_NONCE_LENGTH)
338 return GPG_ERR_INV_ARG;
339 if (c->u_mode.gcm.siv_keylen == 0)
340 return GPG_ERR_INV_STATE;
341 if (c->marks.iv)
342 {
343 /* If nonce is already set, use cipher_reset or setkey first to reset
344 * cipher state. */
345 return GPG_ERR_INV_STATE;
346 }
347
348 memset (c->u_mode.gcm.aadlen, 0, sizeof(c->u_mode.gcm.aadlen));
349 memset (c->u_mode.gcm.datalen, 0, sizeof(c->u_mode.gcm.datalen));
350 memset (c->u_mode.gcm.u_tag.tag, 0, sizeof(c->u_mode.gcm.u_tag.tag));
351 c->u_mode.gcm.datalen_over_limits = 0;
352 c->u_mode.gcm.ghash_data_finalized = 0;
353 c->u_mode.gcm.ghash_aad_finalized = 0;
354
355 memset (c->u_iv.iv, 0, GCRY_SIV_BLOCK_LEN);
356 memcpy (c->u_iv.iv, iv, ivlen);
357 memcpy (tmp_in + 4, iv, ivlen);
358
359 /* Derive message authentication key */
360 buf_put_le32(tmp_in, 0);
361 c->spec->encrypt (&c->context.c, tmp, tmp_in);
362 memcpy (auth_key + 0, tmp, 8);
363
364 buf_put_le32(tmp_in, 1);
365 c->spec->encrypt (&c->context.c, tmp, tmp_in);
366 memcpy (auth_key + 8, tmp, 8);
367
368 polyval_set_key (c, auth_key);
369 wipememory (auth_key, sizeof(auth_key));
370
371 /* Derive message encryption key */
372 buf_put_le32(tmp_in, 2);
373 c->spec->encrypt (&c->context.c, tmp, tmp_in);
374 memcpy (enc_key + 0, tmp, 8);
375
376 buf_put_le32(tmp_in, 3);
377 c->spec->encrypt (&c->context.c, tmp, tmp_in);
378 memcpy (enc_key + 8, tmp, 8);
379
380 if (c->u_mode.gcm.siv_keylen >= 24)
381 {
382 buf_put_le32(tmp_in, 4);
383 c->spec->encrypt (&c->context.c, tmp, tmp_in);
384 memcpy (enc_key + 16, tmp, 8);
385 }
386
387 if (c->u_mode.gcm.siv_keylen >= 32)
388 {
389 buf_put_le32(tmp_in, 5);
390 c->spec->encrypt (&c->context.c, tmp, tmp_in);
391 memcpy (enc_key + 24, tmp, 8);
392 }
393
394 wipememory (tmp, sizeof(tmp));
395 wipememory (tmp_in, sizeof(tmp_in));
396
397 err = c->spec->setkey (&c->context.c, enc_key, c->u_mode.gcm.siv_keylen,
398 &c->bulk);
399 wipememory (enc_key, sizeof(enc_key));
400 if (err)
401 return err;
402
403 c->marks.iv = 1;
404 return 0;
405 }
406
407
408 gcry_err_code_t
409 _gcry_cipher_gcm_siv_authenticate (gcry_cipher_hd_t c,
410 const byte *aadbuf, size_t aadbuflen)
411 {
412 if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN)
413 return GPG_ERR_CIPHER_ALGO;
414 if (c->u_mode.gcm.datalen_over_limits)
415 return GPG_ERR_INV_LENGTH;
416 if (c->marks.tag
417 || !c->marks.iv
418 || c->u_mode.gcm.ghash_aad_finalized
419 || c->u_mode.gcm.ghash_data_finalized
420 || !c->u_mode.gcm.ghash_fn)
421 return GPG_ERR_INV_STATE;
422
423 gcm_siv_bytecounter_add (c->u_mode.gcm.aadlen, aadbuflen);
424 if (!gcm_siv_check_len (c->u_mode.gcm.aadlen))
425 {
426 c->u_mode.gcm.datalen_over_limits = 1;
427 return GPG_ERR_INV_LENGTH;
428 }
429
430 do_polyval_buf (c, c->u_mode.gcm.u_tag.tag, aadbuf, aadbuflen, 0);
431
432 return 0;
433 }
434
435
436 gcry_err_code_t
437 _gcry_cipher_gcm_siv_encrypt (gcry_cipher_hd_t c,
438 byte *outbuf, size_t outbuflen,
439 const byte *inbuf, size_t inbuflen)
440 {
441 u32 bitlengths[2][2];
442
443 if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN)
444 return GPG_ERR_CIPHER_ALGO;
445 if (outbuflen < inbuflen)
446 return GPG_ERR_BUFFER_TOO_SHORT;
447 if (c->u_mode.gcm.datalen_over_limits)
448 return GPG_ERR_INV_LENGTH;
449 if (c->marks.tag
450 || !c->marks.iv
451 || c->u_mode.gcm.ghash_data_finalized
452 || !c->u_mode.gcm.ghash_fn)
453 return GPG_ERR_INV_STATE;
454
455 if (!c->u_mode.gcm.ghash_aad_finalized)
456 {
457 /* Start of encryption marks end of AAD stream. */
458 do_polyval_buf(c, c->u_mode.gcm.u_tag.tag, NULL, 0, 1);
459 c->u_mode.gcm.ghash_aad_finalized = 1;
460 }
461
462 gcm_siv_bytecounter_add (c->u_mode.gcm.datalen, inbuflen);
463 if (!gcm_siv_check_len (c->u_mode.gcm.datalen))
464 {
465 c->u_mode.gcm.datalen_over_limits = 1;
466 return GPG_ERR_INV_LENGTH;
467 }
468
469 /* Plaintext and padding to POLYVAL. */
470 do_polyval_buf (c, c->u_mode.gcm.u_tag.tag, inbuf, inbuflen, 1);
471 c->u_mode.gcm.ghash_data_finalized = 1;
472
473 /* aad length */
474 bitlengths[0][0] = le_bswap32(c->u_mode.gcm.aadlen[0] << 3);
475 bitlengths[0][1] = le_bswap32((c->u_mode.gcm.aadlen[0] >> 29) |
476 (c->u_mode.gcm.aadlen[1] << 3));
477 /* data length */
478 bitlengths[1][0] = le_bswap32(c->u_mode.gcm.datalen[0] << 3);
479 bitlengths[1][1] = le_bswap32((c->u_mode.gcm.datalen[0] >> 29) |
480 (c->u_mode.gcm.datalen[1] << 3));
481
482 /* Length block to POLYVAL. */
483 do_polyval_buf(c, c->u_mode.gcm.u_tag.tag, (byte *)bitlengths,
484 GCRY_SIV_BLOCK_LEN, 1);
485 wipememory (bitlengths, sizeof(bitlengths));
486
487 /* Prepare tag and counter. */
488 cipher_block_bswap (c->u_mode.gcm.u_tag.tag, c->u_mode.gcm.u_tag.tag,
489 GCRY_SIV_BLOCK_LEN);
490 cipher_block_xor (c->u_mode.gcm.tagiv, c->u_iv.iv, c->u_mode.gcm.u_tag.tag,
491 GCRY_SIV_BLOCK_LEN);
492 c->u_mode.gcm.tagiv[GCRY_SIV_BLOCK_LEN - 1] &= 0x7f;
493 c->spec->encrypt (&c->context.c, c->u_mode.gcm.tagiv, c->u_mode.gcm.tagiv);
494 c->marks.tag = 1;
495 memcpy (c->u_ctr.ctr, c->u_mode.gcm.tagiv, GCRY_SIV_BLOCK_LEN);
496 c->u_ctr.ctr[GCRY_SIV_BLOCK_LEN - 1] |= 0x80;
497
498 /* Encrypt data */
499 do_ctr_le32 (c, outbuf, inbuf, inbuflen);
500 return 0;
501 }
502
503
504 gcry_err_code_t
505 _gcry_cipher_gcm_siv_set_decryption_tag (gcry_cipher_hd_t c,
506 const byte *tag, size_t taglen)
507 {
508 if (taglen != GCRY_SIV_BLOCK_LEN)
509 return GPG_ERR_INV_ARG;
510 if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN)
511 return GPG_ERR_CIPHER_ALGO;
512 if (c->marks.tag)
513 return GPG_ERR_INV_STATE;
514
515 memcpy (c->u_mode.gcm.tagiv, tag, GCRY_SIV_BLOCK_LEN);
516 c->marks.tag = 1;
517
518 return 0;
519 }
520
521
522 gcry_err_code_t
523 _gcry_cipher_gcm_siv_decrypt (gcry_cipher_hd_t c,
524 byte *outbuf, size_t outbuflen,
525 const byte *inbuf, size_t inbuflen)
526 {
527 byte expected_tag[GCRY_SIV_BLOCK_LEN];
528 u32 bitlengths[2][2];
529 gcry_err_code_t rc = 0;
530
531 if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN)
532 return GPG_ERR_CIPHER_ALGO;
533 if (outbuflen < inbuflen)
534 return GPG_ERR_BUFFER_TOO_SHORT;
535 if (c->u_mode.gcm.datalen_over_limits)
536 return GPG_ERR_INV_LENGTH;
537 if (!c->marks.tag
538 || !c->marks.iv
539 || c->u_mode.gcm.ghash_data_finalized
540 || !c->u_mode.gcm.ghash_fn)
541 return GPG_ERR_INV_STATE;
542
543 if (!c->u_mode.gcm.ghash_aad_finalized)
544 {
545 /* Start of encryption marks end of AAD stream. */
546 do_polyval_buf(c, c->u_mode.gcm.u_tag.tag, NULL, 0, 1);
547 c->u_mode.gcm.ghash_aad_finalized = 1;
548 }
549
550 gcm_siv_bytecounter_add (c->u_mode.gcm.datalen, inbuflen);
551 if (!gcm_siv_check_len (c->u_mode.gcm.datalen))
552 {
553 c->u_mode.gcm.datalen_over_limits = 1;
554 return GPG_ERR_INV_LENGTH;
555 }
556
557 /* Prepare counter. */
558 memcpy (c->u_ctr.ctr, c->u_mode.gcm.tagiv, GCRY_SIV_BLOCK_LEN);
559 c->u_ctr.ctr[GCRY_SIV_BLOCK_LEN - 1] |= 0x80;
560
561 /* Decrypt data. */
562 do_ctr_le32 (c, outbuf, inbuf, inbuflen);
563
564 /* Plaintext and padding to POLYVAL. */
565 do_polyval_buf (c, c->u_mode.gcm.u_tag.tag, outbuf, inbuflen, 1);
566 c->u_mode.gcm.ghash_data_finalized = 1;
567
568 /* aad length */
569 bitlengths[0][0] = le_bswap32(c->u_mode.gcm.aadlen[0] << 3);
570 bitlengths[0][1] = le_bswap32((c->u_mode.gcm.aadlen[0] >> 29) |
571 (c->u_mode.gcm.aadlen[1] << 3));
572 /* data length */
573 bitlengths[1][0] = le_bswap32(c->u_mode.gcm.datalen[0] << 3);
574 bitlengths[1][1] = le_bswap32((c->u_mode.gcm.datalen[0] >> 29) |
575 (c->u_mode.gcm.datalen[1] << 3));
576
577 /* Length block to POLYVAL. */
578 do_polyval_buf(c, c->u_mode.gcm.u_tag.tag, (byte *)bitlengths,
579 GCRY_SIV_BLOCK_LEN, 1);
580 wipememory (bitlengths, sizeof(bitlengths));
581
582 /* Prepare tag. */
583 cipher_block_bswap (c->u_mode.gcm.u_tag.tag, c->u_mode.gcm.u_tag.tag,
584 GCRY_SIV_BLOCK_LEN);
585 cipher_block_xor (expected_tag, c->u_iv.iv, c->u_mode.gcm.u_tag.tag,
586 GCRY_SIV_BLOCK_LEN);
587 expected_tag[GCRY_SIV_BLOCK_LEN - 1] &= 0x7f;
588 c->spec->encrypt (&c->context.c, expected_tag, expected_tag);
589
590 if (!buf_eq_const(c->u_mode.gcm.tagiv, expected_tag, GCRY_SIV_BLOCK_LEN))
591 {
592 wipememory (outbuf, inbuflen);
593 rc = GPG_ERR_CHECKSUM;
594 }
595
596 wipememory (expected_tag, sizeof(expected_tag));
597 return rc;
598 }
599
600
601 static gcry_err_code_t
602 _gcry_cipher_gcm_siv_tag (gcry_cipher_hd_t c,
603 byte * outbuf, size_t outbuflen, int check)
604 {
605 gcry_err_code_t err;
606
607 if (!c->marks.tag)
608 {
609 if (!c->u_mode.gcm.ghash_fn)
610 return GPG_ERR_INV_STATE;
611
612 if (!c->marks.tag)
613 {
614 /* Finalize GCM-SIV with zero-length plaintext. */
615 err = _gcry_cipher_gcm_siv_encrypt (c, NULL, 0, NULL, 0);
616 if (err != 0)
617 return err;
618 }
619 }
620
621 if (c->u_mode.gcm.datalen_over_limits)
622 return GPG_ERR_INV_LENGTH;
623 if (!c->u_mode.gcm.ghash_data_finalized)
624 return GPG_ERR_INV_STATE;
625 if (!c->marks.tag)
626 return GPG_ERR_INV_STATE;
627
628 if (!check)
629 {
630 if (outbuflen > GCRY_SIV_BLOCK_LEN)
631 outbuflen = GCRY_SIV_BLOCK_LEN;
632
633 /* NB: We already checked that OUTBUF is large enough to hold
634 * the result or has valid truncated length. */
635 memcpy (outbuf, c->u_mode.gcm.tagiv, outbuflen);
636 }
637 else
638 {
639 /* OUTBUFLEN gives the length of the user supplied tag in OUTBUF
640 * and thus we need to compare its length first. */
641 if (outbuflen != GCRY_SIV_BLOCK_LEN
642 || !buf_eq_const (outbuf, c->u_mode.gcm.tagiv, outbuflen))
643 return GPG_ERR_CHECKSUM;
644 }
645
646 return 0;
647 }
648
649
650 gcry_err_code_t
651 _gcry_cipher_gcm_siv_get_tag (gcry_cipher_hd_t c, unsigned char *outtag,
652 size_t taglen)
653 {
654 return _gcry_cipher_gcm_siv_tag (c, outtag, taglen, 0);
655 }
656
657
658 gcry_err_code_t
659 _gcry_cipher_gcm_siv_check_tag (gcry_cipher_hd_t c,
660 const unsigned char *intag,
661 size_t taglen)
662 {
663 return _gcry_cipher_gcm_siv_tag (c, (unsigned char *)intag, taglen, 1);
664 }