3 The crypt function in the ChaCha stream cipher.
4 Heavily based on the Salsa20 implementation in Nettle.
6 Copyright (C) 2014 Niels Möller
7 Copyright (C) 2013 Joachim Strömbergson
8 Copyright (C) 2012 Simon Josefsson
10 This file is part of GNU Nettle.
12 GNU Nettle is free software: you can redistribute it and/or
13 modify it under the terms of either:
15 * the GNU Lesser General Public License as published by the Free
16 Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
21 * the GNU General Public License as published by the Free
22 Software Foundation; either version 2 of the License, or (at your
23 option) any later version.
25 or both in parallel, as here.
27 GNU Nettle is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received copies of the GNU General Public License and
33 the GNU Lesser General Public License along with this program. If
34 not, see http://www.gnu.org/licenses/.
38 chacha-ref.c version 2008.01.20.
54 #define CHACHA_ROUNDS 20
57 chacha_crypt(struct chacha_ctx *ctx,
67 uint32_t x[_CHACHA_STATE_LENGTH];
69 _chacha_core (x, ctx->state, CHACHA_ROUNDS);
71 ctx->state[13] += (++ctx->state[12] == 0);
73 /* stopping at 2^70 length per nonce is user's responsibility */
75 if (length <= CHACHA_BLOCK_SIZE)
77 memxor3 (c, m, x, length);
80 memxor3 (c, m, x, CHACHA_BLOCK_SIZE);
82 length -= CHACHA_BLOCK_SIZE;
83 c += CHACHA_BLOCK_SIZE;
84 m += CHACHA_BLOCK_SIZE;