| 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 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 32 | #include <mLib/bits.h> |
| 33 | #include <mLib/buf.h> |
| 34 | |
| 35 | #include "gaead.h" |
| 36 | #include "keysz.h" |
| 37 | #include "latinpoly-def.h" |
| 38 | |
| 39 | #include "poly1305.h" |
| 40 | #include "salsa20.h" |
| 41 | |
| 42 | /*----- Common definitions ------------------------------------------------*/ |
| 43 | |
| 44 | const octet |
| 45 | latinpoly_noncesz[] = { KSZ_SET, SALSA20_NONCESZ, SALSA20_IETF_NONCESZ, |
| 46 | XSALSA20_NONCESZ, 0 }, |
| 47 | latinpoly_tagsz[] = { KSZ_SET, POLY1305_TAGSZ, 0 }; |
| 48 | |
| 49 | /* AAD handling. */ |
| 50 | |
| 51 | void latinpoly_aadhash_poly1305(gaead_aad *a, const void *h, size_t hsz) |
| 52 | { |
| 53 | latinpoly_aad *aad = (latinpoly_aad *)a; |
| 54 | poly1305_hash(&aad->poly, h, hsz); |
| 55 | } |
| 56 | |
| 57 | void latinpoly_aadhash_naclbox(gaead_aad *a, const void *h, size_t hsz) |
| 58 | { assert(!hsz); } |
| 59 | |
| 60 | void latinpoly_aaddestroy(gaead_aad *a) { ; } |
| 61 | |
| 62 | /* --- @latinpoly_tag@ --- * |
| 63 | * |
| 64 | * Arguments: @const poly1305_ctx *aad@ = Poly1305 context hashing AAD |
| 65 | * @poly1305_ctx *ct@ = Poly1305 context hashing ciphertext |
| 66 | * @void *tag@ = where to write the tag |
| 67 | * |
| 68 | * Returns: --- |
| 69 | * |
| 70 | * Use: Completes a Latin-dance-Poly1305 tag, combining the AAD and |
| 71 | * ciphertext hashes, appending their lengths, and writing the |
| 72 | * final masked hash to @tag@. The @ct@ context is clobbered. |
| 73 | */ |
| 74 | |
| 75 | /* Write the length of data pushed through Poly1305 as a 64-bit integer. */ |
| 76 | static void putlen(octet *p, const poly1305_ctx *poly) |
| 77 | { |
| 78 | uint32 lo = U32((poly->count << 4) | poly->nbuf), |
| 79 | hi = U32(poly->count >> 28); |
| 80 | STORE32_L(p + 0, lo); STORE32_L(p + 4, hi); |
| 81 | } |
| 82 | |
| 83 | void latinpoly_tag(const poly1305_ctx *aad, poly1305_ctx *ct, void *tag) |
| 84 | { |
| 85 | octet b[16]; |
| 86 | poly1305_ctx t; |
| 87 | |
| 88 | putlen(b + 8, ct); poly1305_flushzero(ct); |
| 89 | if (!aad) memset(b, 0, 8); |
| 90 | else { |
| 91 | putlen(b + 0, aad); |
| 92 | t = *aad; poly1305_flushzero(&t); poly1305_concat(ct, &t, ct); |
| 93 | } |
| 94 | poly1305_hash(ct, b, 16); poly1305_done(ct, tag); |
| 95 | } |
| 96 | |
| 97 | /*----- That's all, folks -------------------------------------------------*/ |