1 /* cipher-eax.c - EAX implementation
2 * Copyright (C) 2018 Jussi Kivilinna <jussi.kivilinna@iki.fi>
4 * This file is part of Libgcrypt.
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.
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.
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/>.
29 #include "./cipher-internal.h"
33 _gcry_cipher_eax_encrypt (gcry_cipher_hd_t c,
34 byte *outbuf, size_t outbuflen,
35 const byte *inbuf, size_t inbuflen)
39 if (outbuflen < inbuflen)
40 return GPG_ERR_BUFFER_TOO_SHORT;
42 return GPG_ERR_INV_STATE;
46 err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
53 size_t currlen = inbuflen;
55 /* Since checksumming is done after encryption, process input in 24KiB
56 * chunks to keep data loaded in L1 cache for checksumming. */
57 if (currlen > 24 * 1024)
60 err = _gcry_cipher_ctr_encrypt (c, outbuf, outbuflen, inbuf, currlen);
64 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, outbuf,
80 _gcry_cipher_eax_decrypt (gcry_cipher_hd_t c,
81 byte *outbuf, size_t outbuflen,
82 const byte *inbuf, size_t inbuflen)
86 if (outbuflen < inbuflen)
87 return GPG_ERR_BUFFER_TOO_SHORT;
89 return GPG_ERR_INV_STATE;
93 err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
100 size_t currlen = inbuflen;
102 /* Since checksumming is done before decryption, process input in 24KiB
103 * chunks to keep data loaded in L1 cache for decryption. */
104 if (currlen > 24 * 1024)
107 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, inbuf,
112 err = _gcry_cipher_ctr_encrypt (c, outbuf, outbuflen, inbuf, currlen);
118 outbuflen -= currlen;
127 _gcry_cipher_eax_authenticate (gcry_cipher_hd_t c,
128 const byte * aadbuf, size_t aadbuflen)
133 return GPG_ERR_INV_STATE;
137 err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
142 return _gcry_cmac_write (c, &c->u_mode.eax.cmac_header, aadbuf, aadbuflen);
147 _gcry_cipher_eax_setkey (gcry_cipher_hd_t c)
151 err = _gcry_cmac_generate_subkeys (c, &c->u_mode.eax.cmac_header);
155 buf_cpy (c->u_mode.eax.cmac_ciphertext.subkeys,
156 c->u_mode.eax.cmac_header.subkeys,
157 sizeof(c->u_mode.eax.cmac_header.subkeys));
164 _gcry_cipher_eax_set_nonce (gcry_cipher_hd_t c, const byte *nonce,
167 gcry_cmac_context_t nonce_cmac;
168 unsigned char initbuf[MAX_BLOCKSIZE];
174 _gcry_cmac_reset (&c->u_mode.eax.cmac_header);
175 _gcry_cmac_reset (&c->u_mode.eax.cmac_ciphertext);
177 /* Calculate nonce CMAC */
179 memset(&nonce_cmac, 0, sizeof(nonce_cmac));
180 memset(&initbuf, 0, sizeof(initbuf));
182 buf_cpy (&nonce_cmac.subkeys, c->u_mode.eax.cmac_header.subkeys,
183 sizeof(c->u_mode.eax.cmac_header.subkeys));
185 err = _gcry_cmac_write (c, &nonce_cmac, initbuf, c->spec->blocksize);
191 err = _gcry_cmac_write (c, &nonce_cmac, nonce, noncelen);
196 err = _gcry_cmac_final (c, &nonce_cmac);
200 cipher_block_cpy (c->u_iv.iv, nonce_cmac.u_iv.iv, MAX_BLOCKSIZE);
201 cipher_block_cpy (c->u_ctr.ctr, nonce_cmac.u_iv.iv, MAX_BLOCKSIZE);
203 wipememory (&nonce_cmac, sizeof(nonce_cmac));
205 /* Prepare header CMAC */
207 initbuf[c->spec->blocksize - 1] = 1;
208 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_header, initbuf,
213 /* Prepare ciphertext CMAC */
215 initbuf[c->spec->blocksize - 1] = 2;
216 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, initbuf,
228 static gcry_err_code_t
229 _gcry_cipher_eax_tag (gcry_cipher_hd_t c,
230 byte *outbuf, size_t outbuflen, int check)
236 err = _gcry_cmac_final (c, &c->u_mode.eax.cmac_header);
240 err = _gcry_cmac_final (c, &c->u_mode.eax.cmac_ciphertext);
244 cipher_block_xor_1 (c->u_iv.iv, c->u_mode.eax.cmac_header.u_iv.iv,
246 cipher_block_xor_1 (c->u_iv.iv, c->u_mode.eax.cmac_ciphertext.u_iv.iv,
249 _gcry_cmac_reset (&c->u_mode.eax.cmac_header);
250 _gcry_cmac_reset (&c->u_mode.eax.cmac_ciphertext);
257 if (outbuflen > c->spec->blocksize)
258 outbuflen = c->spec->blocksize;
260 /* NB: We already checked that OUTBUF is large enough to hold
261 * the result or has valid truncated length. */
262 memcpy (outbuf, c->u_iv.iv, outbuflen);
266 /* OUTBUFLEN gives the length of the user supplied tag in OUTBUF
267 * and thus we need to compare its length first. */
268 if (!(outbuflen <= c->spec->blocksize)
269 || !buf_eq_const (outbuf, c->u_iv.iv, outbuflen))
270 return GPG_ERR_CHECKSUM;
278 _gcry_cipher_eax_get_tag (gcry_cipher_hd_t c, unsigned char *outtag,
281 return _gcry_cipher_eax_tag (c, outtag, taglen, 0);
285 _gcry_cipher_eax_check_tag (gcry_cipher_hd_t c, const unsigned char *intag,
288 return _gcry_cipher_eax_tag (c, (unsigned char *) intag, taglen, 1);