symm/latinpoly-def.h, etc.: Refactor in preparation for a related scheme.
[catacomb] / symm / latinpoly-def.h
1 /* -*-c-*-
2 *
3 * AEAD schemes based on Salsa20/ChaCha and Poly1305
4 *
5 * (c) 2018 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU Library General Public License as published
14 * by the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 * USA.
26 */
27
28 #ifndef CATACOMB_LATINPOLY_DEF_H
29 #define CATACOMB_LATINPOLY_DEF_H
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <mLib/bits.h>
38 #include <mLib/buf.h>
39 #include <mLib/sub.h>
40
41 #ifndef CATACOMB_ARENA_H
42 # include "arena.h"
43 #endif
44
45 #ifndef CATACOMB_CT_H
46 # include "ct.h"
47 #endif
48
49 #ifndef CATACOMB_GAEAD_H
50 # include "gaead.h"
51 #endif
52
53 #ifndef CATACOMB_KEYSZ_H
54 # include "keysz.h"
55 #endif
56
57 #ifndef CATACOMB_LATINPOLY_H
58 # include "latinpoly.h"
59 #endif
60
61 #ifndef CATACOMB_PARANOIA_H
62 # include "paranoia.h"
63 #endif
64
65 #ifndef CATACOMB_POLY1305_H
66 # include "poly1305.h"
67 #endif
68
69 #ifndef CATACOMB_SALSA20_H
70 # include "salsa20.h"
71 #endif
72
73 /*----- Data structures ---------------------------------------------------*/
74
75 typedef struct latinpoly_aad {
76 gaead_aad a;
77 poly1305_ctx poly;
78 } latinpoly_aad;
79
80 typedef struct latinpoly_key {
81 gaead_key k;
82 octet key[SALSA20_KEYSZ];
83 size_t ksz;
84 } latinpoly_key;
85
86 /*----- Common definitions ------------------------------------------------*/
87
88 /* Tables. */
89 extern const octet latinpoly_noncesz[], latinpoly_tagsz[];
90
91 /* AAD methods. */
92 extern void latinpoly_aadhash_poly1305(gaead_aad */*a*/,
93 const void */*h*/, size_t /*hsz*/);
94 extern void latinpoly_aaddestroy(gaead_aad */*a*/);
95
96 /* Variants. */
97 enum { LPVAR_POLY1305 };
98
99 /* --- @latinpoly_tag@ --- *
100 *
101 * Arguments: @const poly1305_ctx *aad@ = Poly1305 context hashing AAD
102 * @poly1305_ctx *ct@ = Poly1305 context hashing ciphertext
103 * @void *tag@ = where to write the tag
104 *
105 * Returns: ---
106 *
107 * Use: Completes a Latin-dance-Poly1305 tag, combining the AAD and
108 * ciphertext hashes, appending their lengths, and writing the
109 * final masked hash to @tag@. The @ct@ context is clobbered.
110 */
111
112 extern void latinpoly_tag(const poly1305_ctx */*aad*/,
113 poly1305_ctx */*ct*/, void */*tag*/);
114
115 /*----- Macros ------------------------------------------------------------*/
116
117 #define LATINPOLY_DEF(latin, base, name) \
118 \
119 /* Utilities. */ \
120 \
121 /* Reinitialize the stream cipher and hash state given a new nonce. */ \
122 static int reinit_##latin(x##latin##_ctx *ctx, int var, \
123 poly1305_ctx *aadpoly, poly1305_ctx *ctpoly, \
124 const void *n, size_t nsz) \
125 { \
126 poly1305_key pk; \
127 octet b[POLY1305_KEYSZ + POLY1305_MASKSZ]; \
128 \
129 switch (nsz) { \
130 case SALSA20_NONCESZ: \
131 memcpy(ctx->s.a, ctx->k, sizeof(ctx->k)); \
132 base##_setnonce(&ctx->s, n); \
133 break; \
134 case SALSA20_IETF_NONCESZ: \
135 memcpy(ctx->s.a, ctx->k, sizeof(ctx->k)); \
136 base##_setnonce_ietf(&ctx->s, n); \
137 break; \
138 case XSALSA20_NONCESZ: \
139 x##latin##_setnonce(ctx, n); \
140 break; \
141 default: \
142 return (-1); \
143 } \
144 \
145 latin##_encrypt(&ctx->s, 0, b, sizeof(b)); \
146 poly1305_keyinit(&pk, b, POLY1305_KEYSZ); \
147 poly1305_macinit(ctpoly, &pk, b + POLY1305_KEYSZ); \
148 switch (var) { \
149 case LPVAR_POLY1305: \
150 poly1305_macinit(aadpoly, &pk, b + POLY1305_KEYSZ); \
151 latin##_encrypt(&ctx->s, 0, 0, SALSA20_OUTSZ - sizeof(b)); \
152 break; \
153 default: \
154 assert(0); \
155 } \
156 return (0); \
157 } \
158 \
159 /* AAD operations. */ \
160 \
161 static const gaead_aadops gaops_##latin##_poly1305 = \
162 { &latin##_poly1305, 0, latinpoly_aadhash_poly1305, latinpoly_aaddestroy }; \
163 \
164 \
165 /* Encryption operations. */ \
166 \
167 typedef struct gectx_##latin { \
168 gaead_enc e; \
169 latinpoly_aad aad; \
170 x##latin##_ctx ctx; \
171 poly1305_ctx poly; \
172 } gectx_##latin; \
173 \
174 static gaead_aad *geaad_##latin(gaead_enc *e) \
175 { gectx_##latin *enc = (gectx_##latin *)e; return (&enc->aad.a); } \
176 \
177 static int gereinit_##latin##_poly1305(gaead_enc *e, \
178 const void *n, size_t nsz, \
179 size_t hsz, size_t msz, size_t tsz) \
180 { \
181 gectx_##latin *enc = (gectx_##latin *)e; \
182 return (reinit_##latin(&enc->ctx, LPVAR_POLY1305, \
183 &enc->aad.poly, &enc->poly, n, nsz)); \
184 } \
185 \
186 static int geenc_##latin(gaead_enc *e, \
187 const void *m, size_t msz, buf *b) \
188 { \
189 gectx_##latin *enc = (gectx_##latin *)e; \
190 void *q; \
191 \
192 if (msz) { q = buf_get(b, msz); if (!q) return (-1); } \
193 else q = 0; \
194 latin##_encrypt(&enc->ctx.s, m, q, msz); \
195 poly1305_hash(&enc->poly, q, msz); \
196 return (0); \
197 } \
198 \
199 static int gedone_##latin##_common(gectx_##latin *enc, \
200 const latinpoly_aad *aad, \
201 buf *b, size_t tsz) \
202 { \
203 if (tsz != POLY1305_TAGSZ) return (-1); \
204 assert((!enc->aad.poly.count && !enc->aad.poly.nbuf && !aad) || \
205 aad == &enc->aad); \
206 if (!BOK(b)) return (-1); \
207 return (0); \
208 } \
209 \
210 static int gedone_##latin##_poly1305(gaead_enc *e, const gaead_aad *a, \
211 buf *b, void *t, size_t tsz) \
212 { \
213 gectx_##latin *enc = (gectx_##latin *)e; \
214 const latinpoly_aad *aad = (const latinpoly_aad *)a; \
215 \
216 if (gedone_##latin##_common(enc, aad, b, tsz)) return (-1); \
217 latinpoly_tag(aad ? &aad->poly : 0, &enc->poly, t); \
218 return (0); \
219 } \
220 \
221 static void gedestroy_##latin(gaead_enc *e) \
222 { gectx_##latin *enc = (gectx_##latin *)e; BURN(*enc); S_DESTROY(enc); } \
223 \
224 static gaead_encops geops_##latin##_poly1305 = \
225 { &latin##_poly1305, geaad_##latin, gereinit_##latin##_poly1305, \
226 geenc_##latin, gedone_##latin##_poly1305, gedestroy_##latin }; \
227 \
228 /* Decryption operations. */ \
229 \
230 typedef struct gdctx_##latin { \
231 gaead_dec d; \
232 latinpoly_aad aad; \
233 x##latin##_ctx ctx; \
234 poly1305_ctx poly; \
235 } gdctx_##latin; \
236 \
237 static gaead_aad *gdaad_##latin(gaead_dec *d) \
238 { gdctx_##latin *dec = (gdctx_##latin *)d; return (&dec->aad.a); } \
239 \
240 static int gdreinit_##latin##_poly1305(gaead_dec *d, \
241 const void *n, size_t nsz, \
242 size_t hsz, size_t msz, size_t tsz) \
243 { \
244 gdctx_##latin *dec = (gdctx_##latin *)d; \
245 return (reinit_##latin(&dec->ctx, LPVAR_POLY1305, \
246 &dec->aad.poly, &dec->poly, n, nsz)); \
247 } \
248 \
249 static int gddec_##latin(gaead_dec *d, \
250 const void *c, size_t csz, buf *b) \
251 { \
252 gdctx_##latin *dec = (gdctx_##latin *)d; \
253 void *q; \
254 \
255 if (csz) { q = buf_get(b, csz); if (!q) return (-1); } \
256 else q = 0; \
257 poly1305_hash(&dec->poly, c, csz); \
258 latin##_encrypt(&dec->ctx.s, c, q, csz); \
259 return (0); \
260 } \
261 \
262 static int gddone_##latin##_common(gdctx_##latin *dec, \
263 const latinpoly_aad *aad, \
264 buf *b, size_t tsz) \
265 { \
266 if (tsz != POLY1305_TAGSZ) return (-1); \
267 assert((!dec->aad.poly.count && !dec->aad.poly.nbuf && !aad) || \
268 aad == &dec->aad); \
269 if (!BOK(b)) return (-1); \
270 return (0); \
271 } \
272 \
273 static int gddone_##latin##_poly1305(gaead_dec *d, const gaead_aad *a, \
274 buf *b, const void *t, size_t tsz) \
275 { \
276 gdctx_##latin *dec = (gdctx_##latin *)d; \
277 const latinpoly_aad *aad = (const latinpoly_aad *)a; \
278 octet u[POLY1305_TAGSZ]; \
279 \
280 if (gddone_##latin##_common(dec, aad, b, tsz)) return (-1); \
281 latinpoly_tag(aad ? &aad->poly : 0, &dec->poly, u); \
282 if (ct_memeq(t, u, POLY1305_TAGSZ)) return (+1); \
283 else return (0); \
284 } \
285 \
286 static void gddestroy_##latin(gaead_dec *d) \
287 { gdctx_##latin *dec = (gdctx_##latin *)d; BURN(*dec); S_DESTROY(dec); } \
288 \
289 static gaead_decops gdops_##latin##_poly1305 = \
290 { &latin##_poly1305, gdaad_##latin, gdreinit_##latin##_poly1305, \
291 gddec_##latin, gddone_##latin##_poly1305, gddestroy_##latin }; \
292 \
293 /* Key operations. */ \
294 \
295 static gaead_enc *gkenc_##latin##_poly1305(const gaead_key *k, \
296 const void *n, size_t nsz, \
297 size_t hsz, size_t msz, \
298 size_t tsz) \
299 { \
300 latinpoly_key *key = (latinpoly_key *)k; \
301 gectx_##latin *enc = S_CREATE(gectx_##latin); \
302 \
303 enc->e.ops = &geops_##latin##_poly1305; \
304 enc->aad.a.ops = &gaops_##latin##_poly1305; \
305 x##latin##_init(&enc->ctx, key->key, key->ksz, 0); \
306 if (reinit_##latin(&enc->ctx, LPVAR_POLY1305, \
307 &enc->aad.poly, &enc->poly, n, nsz)) \
308 { gedestroy_##latin(&enc->e); return (0); } \
309 return (&enc->e); \
310 } \
311 \
312 static gaead_dec *gkdec_##latin##_poly1305(const gaead_key *k, \
313 const void *n, size_t nsz, \
314 size_t hsz, size_t msz, \
315 size_t tsz) \
316 { \
317 latinpoly_key *key = (latinpoly_key *)k; \
318 gdctx_##latin *dec = S_CREATE(gdctx_##latin); \
319 \
320 dec->d.ops = &gdops_##latin##_poly1305; \
321 dec->aad.a.ops = &gaops_##latin##_poly1305; \
322 x##latin##_init(&dec->ctx, key->key, key->ksz, 0); \
323 if (reinit_##latin(&dec->ctx, LPVAR_POLY1305, \
324 &dec->aad.poly, &dec->poly, n, nsz)) \
325 { gddestroy_##latin(&dec->d); return (0); } \
326 return (&dec->d); \
327 } \
328 \
329 static void gkdestroy_##latin(gaead_key *k) \
330 { latinpoly_key *key = (latinpoly_key *)k; BURN(*key); S_DESTROY(key); } \
331 \
332 static const gaead_keyops gkops_##latin##_poly1305 = \
333 { &latin##_poly1305, 0, \
334 gkenc_##latin##_poly1305, gkdec_##latin##_poly1305, \
335 gkdestroy_##latin }; \
336 \
337 /* Class definition. */ \
338 \
339 static gaead_key *gkey_##latin##_common(const gaead_keyops *ops, \
340 const void *k, size_t ksz) \
341 { \
342 latinpoly_key *key = S_CREATE(latinpoly_key); \
343 \
344 key->k.ops = ops; \
345 KSZ_ASSERT(latin, ksz); memcpy(key->key, k, ksz); key->ksz = ksz; \
346 return (&key->k); \
347 } \
348 \
349 static gaead_key *gkey_##latin##_poly1305(const void *k, size_t ksz) \
350 { return (gkey_##latin##_common(&gkops_##latin##_poly1305, k, ksz)); } \
351 \
352 const gcaead latin##_poly1305 = { \
353 name "-poly1305", latin##_keysz, latinpoly_noncesz, latinpoly_tagsz, \
354 64, 0, 0, AEADF_AADNDEP, \
355 gkey_##latin##_poly1305 \
356 };
357
358 /*----- That's all, folks -------------------------------------------------*/
359
360 #ifdef __cplusplus
361 }
362 #endif
363
364 #endif