Update copyright headers for dual licensing.
[gd/nettle] / salsa20-core-internal.c
1 /* salsa20-core-internal.c
2
3    Internal interface to the Salsa20 core function.
4
5    Copyright (C) 2012 Simon Josefsson, Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
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.
15
16    or
17
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.
21
22    or both in parallel, as here.
23
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.
28
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/.
32 */
33
34 /* Based on:
35    salsa20-ref.c version 20051118
36    D. J. Bernstein
37    Public domain.
38 */
39
40 #if HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <assert.h>
45 #include <string.h>
46
47 #include "salsa20.h"
48
49 #include "macros.h"
50
51 #ifndef SALSA20_DEBUG
52 # define SALSA20_DEBUG 0
53 #endif
54
55 #if SALSA20_DEBUG
56 # include <stdio.h>
57 # define DEBUG(i) do {                          \
58     unsigned debug_j;                           \
59     for (debug_j = 0; debug_j < 16; debug_j++)  \
60       {                                         \
61         if (debug_j == 0)                       \
62           fprintf(stderr, "%2d:", (i));         \
63         else if (debug_j % 4 == 0)              \
64           fprintf(stderr, "\n   ");             \
65         fprintf(stderr, " %8x", x[debug_j]);    \
66       }                                         \
67     fprintf(stderr, "\n");                      \
68   } while (0)
69 #else
70 # define DEBUG(i)
71 #endif
72
73 #ifdef WORDS_BIGENDIAN
74 #define LE_SWAP32(v)                            \
75   ((ROTL32(8,  v) & 0x00FF00FFUL) |             \
76    (ROTL32(24, v) & 0xFF00FF00UL))
77 #else
78 #define LE_SWAP32(v) (v)
79 #endif
80
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);        \
86   } while(0)
87
88 void
89 _salsa20_core(uint32_t *dst, const uint32_t *src, unsigned rounds)
90 {
91   uint32_t x[_SALSA20_INPUT_LENGTH];
92   unsigned i;
93
94   assert ( (rounds & 1) == 0);
95
96   memcpy (x, src, sizeof(x));
97   for (i = 0; i < rounds;i += 2)
98     {
99       DEBUG (i);
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]);
104
105       DEBUG (i+1);
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]);
110     }
111   DEBUG (i);
112
113   for (i = 0; i < _SALSA20_INPUT_LENGTH; i++)
114     {
115       uint32_t t = x[i] + src[i];
116       dst[i] = LE_SWAP32 (t);
117     }
118 }