1 /* salsa20-core-internal.c
3 Internal interface to the Salsa20 core function.
5 Copyright (C) 2012 Simon Josefsson, 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/.
35 salsa20-ref.c version 20051118
52 # define SALSA20_DEBUG 0
57 # define DEBUG(i) do { \
59 for (debug_j = 0; debug_j < 16; debug_j++) \
62 fprintf(stderr, "%2d:", (i)); \
63 else if (debug_j % 4 == 0) \
64 fprintf(stderr, "\n "); \
65 fprintf(stderr, " %8x", x[debug_j]); \
67 fprintf(stderr, "\n"); \
73 #ifdef WORDS_BIGENDIAN
74 #define LE_SWAP32(v) \
75 ((ROTL32(8, v) & 0x00FF00FFUL) | \
76 (ROTL32(24, v) & 0xFF00FF00UL))
78 #define LE_SWAP32(v) (v)
81 #define QROUND(x0, x1, x2, x3) do { \
82 x1 ^= ROTL32(7, x0 + x3); \
83 x2 ^= ROTL32(9, x1 + x0); \
84 x3 ^= ROTL32(13, x2 + x1); \
85 x0 ^= ROTL32(18, x3 + x2); \
89 _salsa20_core(uint32_t *dst, const uint32_t *src, unsigned rounds)
91 uint32_t x[_SALSA20_INPUT_LENGTH];
94 assert ( (rounds & 1) == 0);
96 memcpy (x, src, sizeof(x));
97 for (i = 0; i < rounds;i += 2)
100 QROUND(x[0], x[4], x[8], x[12]);
101 QROUND(x[5], x[9], x[13], x[1]);
102 QROUND(x[10], x[14], x[2], x[6]);
103 QROUND(x[15], x[3], x[7], x[11]);
106 QROUND(x[0], x[1], x[2], x[3]);
107 QROUND(x[5], x[6], x[7], x[4]);
108 QROUND(x[10], x[11], x[8], x[9]);
109 QROUND(x[15], x[12], x[13], x[14]);
113 for (i = 0; i < _SALSA20_INPUT_LENGTH; i++)
115 uint32_t t = x[i] + src[i];
116 dst[i] = LE_SWAP32 (t);