3 * AEAD schemes based on Salsa20/ChaCha and Poly1305
5 * (c) 2018 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_LATINPOLY_DEF_H
29 #define CATACOMB_LATINPOLY_DEF_H
35 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
41 #ifndef CATACOMB_ARENA_H
49 #ifndef CATACOMB_GAEAD_H
53 #ifndef CATACOMB_KEYSZ_H
57 #ifndef CATACOMB_LATINPOLY_H
58 # include "latinpoly.h"
61 #ifndef CATACOMB_PARANOIA_H
62 # include "paranoia.h"
65 #ifndef CATACOMB_POLY1305_H
66 # include "poly1305.h"
69 #ifndef CATACOMB_SALSA20_H
73 /*----- Data structures ---------------------------------------------------*/
75 typedef struct latinpoly_aad
{
80 typedef struct latinpoly_key
{
82 octet key
[SALSA20_KEYSZ
];
86 /*----- Common definitions ------------------------------------------------*/
89 extern const octet latinpoly_noncesz
[], latinpoly_tagsz
[];
92 extern void latinpoly_aadhash_poly1305(gaead_aad */
*a*/
,
93 const void */
*h*/
, size_t /*hsz*/);
94 extern void latinpoly_aaddestroy(gaead_aad */
*a*/
);
97 enum { LPVAR_POLY1305
};
99 /* --- @latinpoly_tag@ --- *
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
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.
112 extern void latinpoly_tag(const poly1305_ctx */
*aad*/
,
113 poly1305_ctx */
*ct*/
, void */
*tag*/
);
115 /*----- Macros ------------------------------------------------------------*/
117 #define LATINPOLY_DEF(latin, base, name) \
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) \
127 octet b[POLY1305_KEYSZ + POLY1305_MASKSZ]; \
130 case SALSA20_NONCESZ: \
131 memcpy(ctx->s.a, ctx->k, sizeof(ctx->k)); \
132 base##_setnonce(&ctx->s, n); \
134 case SALSA20_IETF_NONCESZ: \
135 memcpy(ctx->s.a, ctx->k, sizeof(ctx->k)); \
136 base##_setnonce_ietf(&ctx->s, n); \
138 case XSALSA20_NONCESZ: \
139 x##latin##_setnonce(ctx, n); \
145 latin##_encrypt(&ctx->s, 0, b, sizeof(b)); \
146 poly1305_keyinit(&pk, b, POLY1305_KEYSZ); \
147 poly1305_macinit(ctpoly, &pk, b + POLY1305_KEYSZ); \
149 case LPVAR_POLY1305: \
150 poly1305_macinit(aadpoly, &pk, b + POLY1305_KEYSZ); \
151 latin##_encrypt(&ctx->s, 0, 0, SALSA20_OUTSZ - sizeof(b)); \
159 /* AAD operations. */ \
161 static const gaead_aadops gaops_##latin##_poly1305 = \
162 { &latin##_poly1305, 0, latinpoly_aadhash_poly1305, latinpoly_aaddestroy }; \
165 /* Encryption operations. */ \
167 typedef struct gectx_##latin { \
170 x##latin##_ctx ctx; \
174 static gaead_aad *geaad_##latin(gaead_enc *e) \
175 { gectx_##latin *enc = (gectx_##latin *)e; return (&enc->aad.a); } \
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) \
181 gectx_##latin *enc = (gectx_##latin *)e; \
182 return (reinit_##latin(&enc->ctx, LPVAR_POLY1305, \
183 &enc->aad.poly, &enc->poly, n, nsz)); \
186 static int geenc_##latin(gaead_enc *e, \
187 const void *m, size_t msz, buf *b) \
189 gectx_##latin *enc = (gectx_##latin *)e; \
192 if (msz) { q = buf_get(b, msz); if (!q) return (-1); } \
194 latin##_encrypt(&enc->ctx.s, m, q, msz); \
195 poly1305_hash(&enc->poly, q, msz); \
199 static int gedone_##latin##_common(gectx_##latin *enc, \
200 const latinpoly_aad *aad, \
201 buf *b, size_t tsz) \
203 if (tsz != POLY1305_TAGSZ) return (-1); \
204 assert((!enc->aad.poly.count && !enc->aad.poly.nbuf && !aad) || \
206 if (!BOK(b)) return (-1); \
210 static int gedone_##latin##_poly1305(gaead_enc *e, const gaead_aad *a, \
211 buf *b, void *t, size_t tsz) \
213 gectx_##latin *enc = (gectx_##latin *)e; \
214 const latinpoly_aad *aad = (const latinpoly_aad *)a; \
216 if (gedone_##latin##_common(enc, aad, b, tsz)) return (-1); \
217 latinpoly_tag(aad ? &aad->poly : 0, &enc->poly, t); \
221 static void gedestroy_##latin(gaead_enc *e) \
222 { gectx_##latin *enc = (gectx_##latin *)e; BURN(*enc); S_DESTROY(enc); } \
224 static gaead_encops geops_##latin##_poly1305 = \
225 { &latin##_poly1305, geaad_##latin, gereinit_##latin##_poly1305, \
226 geenc_##latin, gedone_##latin##_poly1305, gedestroy_##latin }; \
228 /* Decryption operations. */ \
230 typedef struct gdctx_##latin { \
233 x##latin##_ctx ctx; \
237 static gaead_aad *gdaad_##latin(gaead_dec *d) \
238 { gdctx_##latin *dec = (gdctx_##latin *)d; return (&dec->aad.a); } \
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) \
244 gdctx_##latin *dec = (gdctx_##latin *)d; \
245 return (reinit_##latin(&dec->ctx, LPVAR_POLY1305, \
246 &dec->aad.poly, &dec->poly, n, nsz)); \
249 static int gddec_##latin(gaead_dec *d, \
250 const void *c, size_t csz, buf *b) \
252 gdctx_##latin *dec = (gdctx_##latin *)d; \
255 if (csz) { q = buf_get(b, csz); if (!q) return (-1); } \
257 poly1305_hash(&dec->poly, c, csz); \
258 latin##_encrypt(&dec->ctx.s, c, q, csz); \
262 static int gddone_##latin##_common(gdctx_##latin *dec, \
263 const latinpoly_aad *aad, \
264 buf *b, size_t tsz) \
266 if (tsz != POLY1305_TAGSZ) return (-1); \
267 assert((!dec->aad.poly.count && !dec->aad.poly.nbuf && !aad) || \
269 if (!BOK(b)) return (-1); \
273 static int gddone_##latin##_poly1305(gaead_dec *d, const gaead_aad *a, \
274 buf *b, const void *t, size_t tsz) \
276 gdctx_##latin *dec = (gdctx_##latin *)d; \
277 const latinpoly_aad *aad = (const latinpoly_aad *)a; \
278 octet u[POLY1305_TAGSZ]; \
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); \
286 static void gddestroy_##latin(gaead_dec *d) \
287 { gdctx_##latin *dec = (gdctx_##latin *)d; BURN(*dec); S_DESTROY(dec); } \
289 static gaead_decops gdops_##latin##_poly1305 = \
290 { &latin##_poly1305, gdaad_##latin, gdreinit_##latin##_poly1305, \
291 gddec_##latin, gddone_##latin##_poly1305, gddestroy_##latin }; \
293 /* Key operations. */ \
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, \
300 latinpoly_key *key = (latinpoly_key *)k; \
301 gectx_##latin *enc = S_CREATE(gectx_##latin); \
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); } \
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, \
317 latinpoly_key *key = (latinpoly_key *)k; \
318 gdctx_##latin *dec = S_CREATE(gdctx_##latin); \
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); } \
329 static void gkdestroy_##latin(gaead_key *k) \
330 { latinpoly_key *key = (latinpoly_key *)k; BURN(*key); S_DESTROY(key); } \
332 static const gaead_keyops gkops_##latin##_poly1305 = \
333 { &latin##_poly1305, 0, \
334 gkenc_##latin##_poly1305, gkdec_##latin##_poly1305, \
335 gkdestroy_##latin }; \
337 /* Class definition. */ \
339 static gaead_key *gkey_##latin##_common(const gaead_keyops *ops, \
340 const void *k, size_t ksz) \
342 latinpoly_key *key = S_CREATE(latinpoly_key); \
345 KSZ_ASSERT(latin, ksz); memcpy(key->key, k, ksz); key->ksz = ksz; \
349 static gaead_key *gkey_##latin##_poly1305(const void *k, size_t ksz) \
350 { return (gkey_##latin##_common(&gkops_##latin##_poly1305, k, ksz)); } \
352 const gcaead latin##_poly1305 = { \
353 name "-poly1305", latin##_keysz, latinpoly_noncesz, latinpoly_tagsz, \
354 64, 0, 0, AEADF_AADNDEP, \
355 gkey_##latin##_poly1305 \
358 /*----- That's all, folks -------------------------------------------------*/