Debian Codegraph @

cipher/cipher-eax.c

raw
1 /* cipher-eax.c - EAX implementation
2 * Copyright (C) 2018 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 gcry_err_code_t
33 _gcry_cipher_eax_encrypt (gcry_cipher_hd_t c,
34 byte *outbuf, size_t outbuflen,
35 const byte *inbuf, size_t inbuflen)
36 {
37 gcry_err_code_t err;
38
39 if (outbuflen < inbuflen)
40 return GPG_ERR_BUFFER_TOO_SHORT;
41 if (c->marks.tag)
42 return GPG_ERR_INV_STATE;
43
44 if (!c->marks.iv)
45 {
46 err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
47 if (err != 0)
48 return err;
49 }
50
51 while (inbuflen)
52 {
53 size_t currlen = inbuflen;
54
55 /* Since checksumming is done after encryption, process input in 24KiB
56 * chunks to keep data loaded in L1 cache for checksumming. However
57 * only do splitting if input is large enough so that last chunks does
58 * not end up being short.*/
59 if (currlen > 32 * 1024)
60 currlen = 24 * 1024;
61
62 err = _gcry_cipher_ctr_encrypt (c, outbuf, outbuflen, inbuf, currlen);
63 if (err != 0)
64 return err;
65
66 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, outbuf,
67 currlen);
68 if (err != 0)
69 return err;
70
71 outbuf += currlen;
72 inbuf += currlen;
73 outbuflen -= currlen;
74 inbuflen -= currlen;
75 }
76
77 return 0;
78 }
79
80
81 gcry_err_code_t
82 _gcry_cipher_eax_decrypt (gcry_cipher_hd_t c,
83 byte *outbuf, size_t outbuflen,
84 const byte *inbuf, size_t inbuflen)
85 {
86 gcry_err_code_t err;
87
88 if (outbuflen < inbuflen)
89 return GPG_ERR_BUFFER_TOO_SHORT;
90 if (c->marks.tag)
91 return GPG_ERR_INV_STATE;
92
93 if (!c->marks.iv)
94 {
95 err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
96 if (err != 0)
97 return err;
98 }
99
100 while (inbuflen)
101 {
102 size_t currlen = inbuflen;
103
104 /* Since checksumming is done before decryption, process input in 24KiB
105 * chunks to keep data loaded in L1 cache for decryption. However only
106 * do splitting if input is large enough so that last chunks does not
107 * end up being short. */
108 if (currlen > 32 * 1024)
109 currlen = 24 * 1024;
110
111 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, inbuf,
112 currlen);
113 if (err != 0)
114 return err;
115
116 err = _gcry_cipher_ctr_encrypt (c, outbuf, outbuflen, inbuf, currlen);
117 if (err != 0)
118 return err;
119
120 outbuf += currlen;
121 inbuf += currlen;
122 outbuflen -= currlen;
123 inbuflen -= currlen;
124 }
125
126 return 0;
127 }
128
129
130 gcry_err_code_t
131 _gcry_cipher_eax_authenticate (gcry_cipher_hd_t c,
132 const byte * aadbuf, size_t aadbuflen)
133 {
134 gcry_err_code_t err;
135
136 if (c->marks.tag)
137 return GPG_ERR_INV_STATE;
138
139 if (!c->marks.iv)
140 {
141 err = _gcry_cipher_eax_set_nonce (c, NULL, 0);
142 if (err != 0)
143 return err;
144 }
145
146 return _gcry_cmac_write (c, &c->u_mode.eax.cmac_header, aadbuf, aadbuflen);
147 }
148
149
150 gcry_err_code_t
151 _gcry_cipher_eax_setkey (gcry_cipher_hd_t c)
152 {
153 gcry_err_code_t err;
154
155 err = _gcry_cmac_generate_subkeys (c, &c->u_mode.eax.cmac_header);
156 if (err != 0)
157 return err;
158
159 buf_cpy (c->u_mode.eax.cmac_ciphertext.subkeys,
160 c->u_mode.eax.cmac_header.subkeys,
161 sizeof(c->u_mode.eax.cmac_header.subkeys));
162
163 return 0;
164 }
165
166
167 gcry_err_code_t
168 _gcry_cipher_eax_set_nonce (gcry_cipher_hd_t c, const byte *nonce,
169 size_t noncelen)
170 {
171 gcry_cmac_context_t nonce_cmac;
172 unsigned char initbuf[MAX_BLOCKSIZE];
173 gcry_err_code_t err;
174
175 c->marks.iv = 0;
176 c->marks.tag = 0;
177
178 _gcry_cmac_reset (&c->u_mode.eax.cmac_header);
179 _gcry_cmac_reset (&c->u_mode.eax.cmac_ciphertext);
180
181 /* Calculate nonce CMAC */
182
183 memset(&nonce_cmac, 0, sizeof(nonce_cmac));
184 memset(&initbuf, 0, sizeof(initbuf));
185
186 buf_cpy (&nonce_cmac.subkeys, c->u_mode.eax.cmac_header.subkeys,
187 sizeof(c->u_mode.eax.cmac_header.subkeys));
188
189 err = _gcry_cmac_write (c, &nonce_cmac, initbuf, c->spec->blocksize);
190 if (err != 0)
191 return err;
192
193 if (noncelen != 0)
194 {
195 err = _gcry_cmac_write (c, &nonce_cmac, nonce, noncelen);
196 if (err != 0)
197 return err;
198 }
199
200 err = _gcry_cmac_final (c, &nonce_cmac);
201 if (err != 0)
202 return err;
203
204 cipher_block_cpy (c->u_iv.iv, nonce_cmac.u_iv.iv, MAX_BLOCKSIZE);
205 cipher_block_cpy (c->u_ctr.ctr, nonce_cmac.u_iv.iv, MAX_BLOCKSIZE);
206
207 wipememory (&nonce_cmac, sizeof(nonce_cmac));
208
209 /* Prepare header CMAC */
210
211 initbuf[c->spec->blocksize - 1] = 1;
212 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_header, initbuf,
213 c->spec->blocksize);
214 if (err != 0)
215 return err;
216
217 /* Prepare ciphertext CMAC */
218
219 initbuf[c->spec->blocksize - 1] = 2;
220 err = _gcry_cmac_write (c, &c->u_mode.eax.cmac_ciphertext, initbuf,
221 c->spec->blocksize);
222 if (err != 0)
223 return err;
224
225 c->marks.iv = 1;
226 c->marks.tag = 0;
227
228 return 0;
229 }
230
231
232 static gcry_err_code_t
233 _gcry_cipher_eax_tag (gcry_cipher_hd_t c,
234 byte *outbuf, size_t outbuflen, int check)
235 {
236 gcry_err_code_t err;
237
238 if (!c->marks.tag)
239 {
240 err = _gcry_cmac_final (c, &c->u_mode.eax.cmac_header);
241 if (err != 0)
242 return err;
243
244 err = _gcry_cmac_final (c, &c->u_mode.eax.cmac_ciphertext);
245 if (err != 0)
246 return err;
247
248 cipher_block_xor_1 (c->u_iv.iv, c->u_mode.eax.cmac_header.u_iv.iv,
249 MAX_BLOCKSIZE);
250 cipher_block_xor_1 (c->u_iv.iv, c->u_mode.eax.cmac_ciphertext.u_iv.iv,
251 MAX_BLOCKSIZE);
252
253 _gcry_cmac_reset (&c->u_mode.eax.cmac_header);
254 _gcry_cmac_reset (&c->u_mode.eax.cmac_ciphertext);
255
256 c->marks.tag = 1;
257 }
258
259 if (!check)
260 {
261 if (outbuflen > c->spec->blocksize)
262 outbuflen = c->spec->blocksize;
263
264 /* NB: We already checked that OUTBUF is large enough to hold
265 * the result or has valid truncated length. */
266 memcpy (outbuf, c->u_iv.iv, outbuflen);
267 }
268 else
269 {
270 /* OUTBUFLEN gives the length of the user supplied tag in OUTBUF
271 * and thus we need to compare its length first. */
272 if (!(outbuflen <= c->spec->blocksize)
273 || !buf_eq_const (outbuf, c->u_iv.iv, outbuflen))
274 return GPG_ERR_CHECKSUM;
275 }
276
277 return 0;
278 }
279
280
281 gcry_err_code_t
282 _gcry_cipher_eax_get_tag (gcry_cipher_hd_t c, unsigned char *outtag,
283 size_t taglen)
284 {
285 return _gcry_cipher_eax_tag (c, outtag, taglen, 0);
286 }
287
288 gcry_err_code_t
289 _gcry_cipher_eax_check_tag (gcry_cipher_hd_t c, const unsigned char *intag,
290 size_t taglen)
291 {
292 return _gcry_cipher_eax_tag (c, (unsigned char *) intag, taglen, 1);
293 }