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
48 #include "salsa20-internal.h"
53 #if HAVE_NATIVE_salsa20_core
55 _nettle_salsa20_core_c(uint32_t *dst, const uint32_t *src, unsigned rounds);
56 #define _nettle_salsa20_core _nettle_salsa20_core_c
60 # define SALSA20_DEBUG 0
65 # define DEBUG(i) do { \
67 for (debug_j = 0; debug_j < 16; debug_j++) \
70 fprintf(stderr, "%2d:", (i)); \
71 else if (debug_j % 4 == 0) \
72 fprintf(stderr, "\n "); \
73 fprintf(stderr, " %8x", x[debug_j]); \
75 fprintf(stderr, "\n"); \
81 #ifdef WORDS_BIGENDIAN
82 #define LE_SWAP32(v) \
83 ((ROTL32(8, v) & 0x00FF00FFUL) | \
84 (ROTL32(24, v) & 0xFF00FF00UL))
86 #define LE_SWAP32(v) (v)
89 #define QROUND(x0, x1, x2, x3) do { \
90 x1 ^= ROTL32(7, x0 + x3); \
91 x2 ^= ROTL32(9, x1 + x0); \
92 x3 ^= ROTL32(13, x2 + x1); \
93 x0 ^= ROTL32(18, x3 + x2); \
97 _salsa20_core(uint32_t *dst, const uint32_t *src, unsigned rounds)
99 uint32_t x[_SALSA20_INPUT_LENGTH];
102 assert ( (rounds & 1) == 0);
104 memcpy (x, src, sizeof(x));
105 for (i = 0; i < rounds;i += 2)
108 QROUND(x[0], x[4], x[8], x[12]);
109 QROUND(x[5], x[9], x[13], x[1]);
110 QROUND(x[10], x[14], x[2], x[6]);
111 QROUND(x[15], x[3], x[7], x[11]);
114 QROUND(x[0], x[1], x[2], x[3]);
115 QROUND(x[5], x[6], x[7], x[4]);
116 QROUND(x[10], x[11], x[8], x[9]);
117 QROUND(x[15], x[12], x[13], x[14]);
121 for (i = 0; i < _SALSA20_INPUT_LENGTH; i++)
123 uint32_t t = x[i] + src[i];
124 dst[i] = LE_SWAP32 (t);