Debian Codegraph @

cipher/mac-poly1305.c

raw
1 /* mac-poly1305.c - Poly1305 based MACs
2 * Copyright (C) 2014 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 "mac-internal.h"
28 #include "poly1305-internal.h"
29
30
31 struct poly1305mac_context_s {
32 poly1305_context_t ctx;
33 gcry_cipher_hd_t hd;
34 struct {
35 unsigned int key_set:1;
36 unsigned int nonce_set:1;
37 unsigned int tag:1;
38 } marks;
39 byte tag[POLY1305_TAGLEN];
40 byte key[POLY1305_KEYLEN];
41 };
42
43
44 static gcry_err_code_t
45 poly1305mac_open (gcry_mac_hd_t h)
46 {
47 struct poly1305mac_context_s *mac_ctx;
48 int secure = (h->magic == CTX_MAC_MAGIC_SECURE);
49 unsigned int flags = (secure ? GCRY_CIPHER_SECURE : 0);
50 gcry_err_code_t err;
51 int cipher_algo;
52
53 if (secure)
54 mac_ctx = xtrycalloc_secure (1, sizeof(*mac_ctx));
55 else
56 mac_ctx = xtrycalloc (1, sizeof(*mac_ctx));
57
58 if (!mac_ctx)
59 return gpg_err_code_from_syserror ();
60
61 h->u.poly1305mac.ctx = mac_ctx;
62
63 switch (h->spec->algo)
64 {
65 default:
66 /* already checked. */
67 case GCRY_MAC_POLY1305:
68 /* plain Poly1305. */
69 cipher_algo = -1;
70 return 0;
71 case GCRY_MAC_POLY1305_AES:
72 cipher_algo = GCRY_CIPHER_AES;
73 break;
74 case GCRY_MAC_POLY1305_CAMELLIA:
75 cipher_algo = GCRY_CIPHER_CAMELLIA128;
76 break;
77 case GCRY_MAC_POLY1305_TWOFISH:
78 cipher_algo = GCRY_CIPHER_TWOFISH;
79 break;
80 case GCRY_MAC_POLY1305_SERPENT:
81 cipher_algo = GCRY_CIPHER_SERPENT128;
82 break;
83 case GCRY_MAC_POLY1305_SEED:
84 cipher_algo = GCRY_CIPHER_SEED;
85 break;
86 case GCRY_MAC_POLY1305_SM4:
87 cipher_algo = GCRY_CIPHER_SM4;
88 break;
89 case GCRY_MAC_POLY1305_ARIA:
90 cipher_algo = GCRY_CIPHER_ARIA128;
91 break;
92 }
93
94 err = _gcry_cipher_open_internal (&mac_ctx->hd, cipher_algo,
95 GCRY_CIPHER_MODE_ECB, flags);
96 if (err)
97 goto err_free;
98
99 return 0;
100
101 err_free:
102 xfree(h->u.poly1305mac.ctx);
103 return err;
104 }
105
106
107 static void
108 poly1305mac_close (gcry_mac_hd_t h)
109 {
110 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
111
112 if (h->spec->algo != GCRY_MAC_POLY1305)
113 _gcry_cipher_close (mac_ctx->hd);
114
115 xfree(mac_ctx);
116 }
117
118
119 static gcry_err_code_t
120 poly1305mac_prepare_key (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
121 {
122 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
123 size_t block_keylen = keylen - 16;
124
125 /* Need at least 16 + 1 byte key. */
126 if (keylen <= 16)
127 return GPG_ERR_INV_KEYLEN;
128
129 /* For Poly1305-AES, first part of key is passed to Poly1305 as is. */
130 memcpy (mac_ctx->key, key + block_keylen, 16);
131
132 /* Remaining part is used as key for the block cipher. */
133 return _gcry_cipher_setkey (mac_ctx->hd, key, block_keylen);
134 }
135
136
137 static gcry_err_code_t
138 poly1305mac_setkey (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
139 {
140 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
141 gcry_err_code_t err;
142
143 memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
144 memset(&mac_ctx->tag, 0, sizeof(mac_ctx->tag));
145 memset(&mac_ctx->key, 0, sizeof(mac_ctx->key));
146
147 mac_ctx->marks.key_set = 0;
148 mac_ctx->marks.nonce_set = 0;
149 mac_ctx->marks.tag = 0;
150
151 if (h->spec->algo != GCRY_MAC_POLY1305)
152 {
153 err = poly1305mac_prepare_key (h, key, keylen);
154 if (err)
155 return err;
156
157 /* Poly1305-AES/etc also need nonce. */
158 mac_ctx->marks.key_set = 1;
159 mac_ctx->marks.nonce_set = 0;
160 }
161 else
162 {
163 /* For plain Poly1305, key is the nonce and setup is complete now. */
164
165 if (keylen != POLY1305_KEYLEN)
166 return GPG_ERR_INV_KEYLEN;
167
168 memcpy (mac_ctx->key, key, keylen);
169
170 err = _gcry_poly1305_init (&mac_ctx->ctx, mac_ctx->key, POLY1305_KEYLEN);
171 if (err)
172 {
173 memset(&mac_ctx->key, 0, sizeof(mac_ctx->key));
174 return err;
175 }
176
177 mac_ctx->marks.key_set = 1;
178 mac_ctx->marks.nonce_set = 1;
179 }
180
181 return 0;
182 }
183
184
185 static gcry_err_code_t
186 poly1305mac_setiv (gcry_mac_hd_t h, const unsigned char *iv, size_t ivlen)
187 {
188 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
189 gcry_err_code_t err;
190
191 if (h->spec->algo == GCRY_MAC_POLY1305)
192 return GPG_ERR_INV_ARG;
193
194 if (ivlen != 16)
195 return GPG_ERR_INV_ARG;
196
197 if (!mac_ctx->marks.key_set)
198 return 0;
199
200 memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
201 memset(&mac_ctx->tag, 0, sizeof(mac_ctx->tag));
202 mac_ctx->marks.nonce_set = 0;
203 mac_ctx->marks.tag = 0;
204
205 /* Prepare second part of the poly1305 key. */
206
207 err = _gcry_cipher_encrypt (mac_ctx->hd, mac_ctx->key + 16, 16, iv, 16);
208 if (err)
209 return err;
210
211 err = _gcry_poly1305_init (&mac_ctx->ctx, mac_ctx->key, POLY1305_KEYLEN);
212 if (err)
213 return err;
214
215 mac_ctx->marks.nonce_set = 1;
216 return 0;
217 }
218
219
220 static gcry_err_code_t
221 poly1305mac_reset (gcry_mac_hd_t h)
222 {
223 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
224
225 if (!mac_ctx->marks.key_set || !mac_ctx->marks.nonce_set)
226 return GPG_ERR_INV_STATE;
227
228 memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
229 memset(&mac_ctx->tag, 0, sizeof(mac_ctx->tag));
230
231 mac_ctx->marks.key_set = 1;
232 mac_ctx->marks.nonce_set = 1;
233 mac_ctx->marks.tag = 0;
234
235 return _gcry_poly1305_init (&mac_ctx->ctx, mac_ctx->key, POLY1305_KEYLEN);
236 }
237
238
239 static gcry_err_code_t
240 poly1305mac_write (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
241 {
242 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
243
244 if (!mac_ctx->marks.key_set || !mac_ctx->marks.nonce_set ||
245 mac_ctx->marks.tag)
246 return GPG_ERR_INV_STATE;
247
248 _gcry_poly1305_update (&mac_ctx->ctx, buf, buflen);
249 return 0;
250 }
251
252
253 static gcry_err_code_t
254 poly1305mac_read (gcry_mac_hd_t h, unsigned char *outbuf, size_t *outlen)
255 {
256 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
257
258 if (!mac_ctx->marks.key_set || !mac_ctx->marks.nonce_set)
259 return GPG_ERR_INV_STATE;
260
261 if (!mac_ctx->marks.tag)
262 {
263 _gcry_poly1305_finish(&mac_ctx->ctx, mac_ctx->tag);
264
265 memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
266 mac_ctx->marks.tag = 1;
267 }
268
269 if (*outlen == 0)
270 return 0;
271
272 if (*outlen <= POLY1305_TAGLEN)
273 buf_cpy (outbuf, mac_ctx->tag, *outlen);
274 else
275 {
276 buf_cpy (outbuf, mac_ctx->tag, POLY1305_TAGLEN);
277 *outlen = POLY1305_TAGLEN;
278 }
279
280 return 0;
281 }
282
283
284 static gcry_err_code_t
285 poly1305mac_verify (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
286 {
287 struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
288 gcry_err_code_t err;
289 size_t outlen = 0;
290
291 /* Check and finalize tag. */
292 err = poly1305mac_read(h, NULL, &outlen);
293 if (err)
294 return err;
295
296 if (buflen > POLY1305_TAGLEN)
297 return GPG_ERR_INV_LENGTH;
298
299 return buf_eq_const (buf, mac_ctx->tag, buflen) ? 0 : GPG_ERR_CHECKSUM;
300 }
301
302
303 static unsigned int
304 poly1305mac_get_maclen (int algo)
305 {
306 (void)algo;
307
308 return POLY1305_TAGLEN;
309 }
310
311
312 static unsigned int
313 poly1305mac_get_keylen (int algo)
314 {
315 (void)algo;
316
317 return POLY1305_KEYLEN;
318 }
319
320
321 static gcry_mac_spec_ops_t poly1305mac_ops = {
322 poly1305mac_open,
323 poly1305mac_close,
324 poly1305mac_setkey,
325 poly1305mac_setiv,
326 poly1305mac_reset,
327 poly1305mac_write,
328 poly1305mac_read,
329 poly1305mac_verify,
330 poly1305mac_get_maclen,
331 poly1305mac_get_keylen,
332 NULL,
333 NULL,
334 };
335
336
337 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac = {
338 GCRY_MAC_POLY1305, {0, 0}, "POLY1305",
339 &poly1305mac_ops
340 };
341 #if USE_AES
342 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aes = {
343 GCRY_MAC_POLY1305_AES, {0, 0}, "POLY1305_AES",
344 &poly1305mac_ops
345 };
346 #endif
347 #if USE_CAMELLIA
348 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_camellia = {
349 GCRY_MAC_POLY1305_CAMELLIA, {0, 0}, "POLY1305_CAMELLIA",
350 &poly1305mac_ops
351 };
352 #endif
353 #if USE_TWOFISH
354 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_twofish = {
355 GCRY_MAC_POLY1305_TWOFISH, {0, 0}, "POLY1305_TWOFISH",
356 &poly1305mac_ops
357 };
358 #endif
359 #if USE_SERPENT
360 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_serpent = {
361 GCRY_MAC_POLY1305_SERPENT, {0, 0}, "POLY1305_SERPENT",
362 &poly1305mac_ops
363 };
364 #endif
365 #if USE_SEED
366 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_seed = {
367 GCRY_MAC_POLY1305_SEED, {0, 0}, "POLY1305_SEED",
368 &poly1305mac_ops
369 };
370 #endif
371 #if USE_SM4
372 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_sm4 = {
373 GCRY_MAC_POLY1305_SM4, {0, 0}, "POLY1305_SM4",
374 &poly1305mac_ops
375 };
376 #endif
377 #if USE_ARIA
378 const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aria = {
379 GCRY_MAC_POLY1305_ARIA, {0, 0}, "POLY1305_ARIA",
380 &poly1305mac_ops
381 };
382 #endif