3 AEAD mechanism based on chacha and poly1305.
5 Copyright (C) 2014, 2015 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
34 /* This implements chacha-poly1305 according to
35 draft-irtf-cfrg-chacha20-poly1305-08. The inputs to poly1305 are:
41 length of associated data (64-bit, little endian)
42 length of ciphertext (64-bit, little endian)
44 where the padding fields are 0-15 zero bytes, filling up to a
55 #include "chacha-internal.h"
56 #include "chacha-poly1305.h"
60 #define CHACHA_ROUNDS 20
62 /* FIXME: Also set nonce to zero, and implement nonce
65 chacha_poly1305_set_key (struct chacha_poly1305_ctx *ctx,
68 chacha_set_key (&ctx->chacha, key);
72 chacha_poly1305_set_nonce (struct chacha_poly1305_ctx *ctx,
76 uint32_t x[_CHACHA_STATE_LENGTH];
80 chacha_set_nonce96 (&ctx->chacha, nonce);
81 /* Generate authentication key */
82 _chacha_core (u.x, ctx->chacha.state, CHACHA_ROUNDS);
83 poly1305_set_key (&ctx->poly1305, u.subkey);
84 /* For final poly1305 processing */
85 memcpy (ctx->s.b, u.subkey + 16, 16);
86 /* Increment block count */
87 ctx->chacha.state[12] = 1;
89 ctx->auth_size = ctx->data_size = ctx->index = 0;
92 /* FIXME: Duplicated in poly1305-aes128.c */
93 #define COMPRESS(ctx, data) _poly1305_block(&(ctx)->poly1305, (data), 1)
96 poly1305_update (struct chacha_poly1305_ctx *ctx,
97 size_t length, const uint8_t *data)
99 MD_UPDATE (ctx, length, data, COMPRESS, (void) 0);
103 poly1305_pad (struct chacha_poly1305_ctx *ctx)
107 memset (ctx->block + ctx->index, 0,
108 POLY1305_BLOCK_SIZE - ctx->index);
109 _poly1305_block(&ctx->poly1305, ctx->block, 1);
114 chacha_poly1305_update (struct chacha_poly1305_ctx *ctx,
115 size_t length, const uint8_t *data)
117 assert (ctx->data_size == 0);
118 poly1305_update (ctx, length, data);
119 ctx->auth_size += length;
124 chacha_poly1305_encrypt (struct chacha_poly1305_ctx *ctx,
125 size_t length, uint8_t *dst, const uint8_t *src)
130 assert (ctx->data_size % CHACHA_POLY1305_BLOCK_SIZE == 0);
133 chacha_crypt (&ctx->chacha, length, dst, src);
134 poly1305_update (ctx, length, dst);
135 ctx->data_size += length;
139 chacha_poly1305_decrypt (struct chacha_poly1305_ctx *ctx,
140 size_t length, uint8_t *dst, const uint8_t *src)
145 assert (ctx->data_size % CHACHA_POLY1305_BLOCK_SIZE == 0);
148 poly1305_update (ctx, length, src);
149 chacha_crypt (&ctx->chacha, length, dst, src);
150 ctx->data_size += length;
154 chacha_poly1305_digest (struct chacha_poly1305_ctx *ctx,
155 size_t length, uint8_t *digest)
160 LE_WRITE_UINT64 (buf, ctx->auth_size);
161 LE_WRITE_UINT64 (buf + 8, ctx->data_size);
163 _poly1305_block (&ctx->poly1305, buf, 1);
165 poly1305_digest (&ctx->poly1305, &ctx->s);
166 memcpy (digest, &ctx->s.b, length);