salsa20.c 4.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* salsa20.c
 *
 * The Salsa20 stream cipher.
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2012 Simon Josefsson
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

/* Based on:
   salsa20-ref.c version 20051118
   D. J. Bernstein
   Public domain.
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

#include "salsa20.h"

Niels Möller's avatar
Niels Möller committed
40
#include "macros.h"
41
42

#define SWAP32(v)				\
Niels Möller's avatar
Niels Möller committed
43
44
  ((ROTL32(8,  v) & 0x00FF00FFUL) |		\
   (ROTL32(24, v) & 0xFF00FF00UL))
45
46
47
48
49
50
51
52
53

#ifdef WORDS_BIGENDIAN
#define U32TO32_LITTLE(v) SWAP32(v)
#else
#define U32TO32_LITTLE(v) (v)
#endif

#define U32TO8_LITTLE(p, v) (((uint32_t*)(p))[0] = U32TO32_LITTLE(v))

Niels Möller's avatar
Niels Möller committed
54
static void salsa20_wordtobyte(uint8_t output[SALSA20_BLOCK_SIZE],const uint32_t input[_SALSA20_INPUT_LENGTH])
55
{
Niels Möller's avatar
Niels Möller committed
56
  uint32_t x[_SALSA20_INPUT_LENGTH];
57
58
  int i;

Niels Möller's avatar
Niels Möller committed
59
  for (i = 0;i < _SALSA20_INPUT_LENGTH;++i) x[i] = input[i];
60
  for (i = 20;i > 0;i -= 2) {
Niels Möller's avatar
Niels Möller committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    x[ 4] ^= ROTL32( 7, x[ 0] + x[12]);
    x[ 8] ^= ROTL32( 9, x[ 4] + x[ 0]);
    x[12] ^= ROTL32(13, x[ 8] + x[ 4]);
    x[ 0] ^= ROTL32(18, x[12] + x[ 8]);
    x[ 9] ^= ROTL32( 7, x[ 5] + x[ 1]);
    x[13] ^= ROTL32( 9, x[ 9] + x[ 5]);
    x[ 1] ^= ROTL32(13, x[13] + x[ 9]);
    x[ 5] ^= ROTL32(18, x[ 1] + x[13]);
    x[14] ^= ROTL32( 7, x[10] + x[ 6]);
    x[ 2] ^= ROTL32( 9, x[14] + x[10]);
    x[ 6] ^= ROTL32(13, x[ 2] + x[14]);
    x[10] ^= ROTL32(18, x[ 6] + x[ 2]);
    x[ 3] ^= ROTL32( 7, x[15] + x[11]);
    x[ 7] ^= ROTL32( 9, x[ 3] + x[15]);
    x[11] ^= ROTL32(13, x[ 7] + x[ 3]);
    x[15] ^= ROTL32(18, x[11] + x[ 7]);
    x[ 1] ^= ROTL32( 7, x[ 0] + x[ 3]);
    x[ 2] ^= ROTL32( 9, x[ 1] + x[ 0]);
    x[ 3] ^= ROTL32(13, x[ 2] + x[ 1]);
    x[ 0] ^= ROTL32(18, x[ 3] + x[ 2]);
    x[ 6] ^= ROTL32( 7, x[ 5] + x[ 4]);
    x[ 7] ^= ROTL32( 9, x[ 6] + x[ 5]);
    x[ 4] ^= ROTL32(13, x[ 7] + x[ 6]);
    x[ 5] ^= ROTL32(18, x[ 4] + x[ 7]);
    x[11] ^= ROTL32( 7, x[10] + x[ 9]);
    x[ 8] ^= ROTL32( 9, x[11] + x[10]);
    x[ 9] ^= ROTL32(13, x[ 8] + x[11]);
    x[10] ^= ROTL32(18, x[ 9] + x[ 8]);
    x[12] ^= ROTL32( 7, x[15] + x[14]);
    x[13] ^= ROTL32( 9, x[12] + x[15]);
    x[14] ^= ROTL32(13, x[13] + x[12]);
    x[15] ^= ROTL32(18, x[14] + x[13]);
93
  }
Niels Möller's avatar
Niels Möller committed
94
95
  for (i = 0;i < _SALSA20_INPUT_LENGTH;++i) x[i] = x[i] + input[i];
  for (i = 0;i < _SALSA20_INPUT_LENGTH;++i) U32TO8_LITTLE(output + 4 * i,x[i]);
96
97
}

Niels Möller's avatar
Niels Möller committed
98
99
static const char sigma[_SALSA20_INPUT_LENGTH] = "expand 32-byte k";
static const char tau[_SALSA20_INPUT_LENGTH] = "expand 16-byte k";
100
101
102
103
104
105
106
107
108

void
salsa20_set_key(struct salsa20_ctx *ctx,
		unsigned length, const uint8_t *key)
{
  const char *constants;

  assert (length == SALSA20_MIN_KEY_SIZE || length == SALSA20_MAX_KEY_SIZE);

Niels Möller's avatar
Niels Möller committed
109
110
111
112
  ctx->input[1] = LE_READ_UINT32(key + 0);
  ctx->input[2] = LE_READ_UINT32(key + 4);
  ctx->input[3] = LE_READ_UINT32(key + 8);
  ctx->input[4] = LE_READ_UINT32(key + 12);
113
114
115
116
117
118
  if (length == SALSA20_MAX_KEY_SIZE) { /* recommended */
    key += 16;
    constants = sigma;
  } else { /* kbits == 128 */
    constants = tau;
  }
Niels Möller's avatar
Niels Möller committed
119
120
121
122
123
124
125
126
  ctx->input[11] = LE_READ_UINT32(key + 0);
  ctx->input[12] = LE_READ_UINT32(key + 4);
  ctx->input[13] = LE_READ_UINT32(key + 8);
  ctx->input[14] = LE_READ_UINT32(key + 12);
  ctx->input[0]  = LE_READ_UINT32(constants + 0);
  ctx->input[5]  = LE_READ_UINT32(constants + 4);
  ctx->input[10] = LE_READ_UINT32(constants + 8);
  ctx->input[15] = LE_READ_UINT32(constants + 12);
127
128
129
}

void
130
salsa20_set_iv(struct salsa20_ctx *ctx, const uint8_t *iv)
131
{
Niels Möller's avatar
Niels Möller committed
132
133
  ctx->input[6] = LE_READ_UINT32(iv + 0);
  ctx->input[7] = LE_READ_UINT32(iv + 4);
134
135
136
137
138
139
140
141
142
143
  ctx->input[8] = 0;
  ctx->input[9] = 0;
}

void
salsa20_crypt(struct salsa20_ctx *ctx,
	      unsigned length,
	      uint8_t *c,
	      const uint8_t *m)
{
Niels Möller's avatar
Niels Möller committed
144
  uint8_t output[SALSA20_BLOCK_SIZE];
145
146
147
148
149
  unsigned i;

  if (!length) return;
  for (;;) {
    salsa20_wordtobyte(output,ctx->input);
Niels Möller's avatar
Niels Möller committed
150
    ctx->input[8]++;
151
    if (!ctx->input[8]) {
Niels Möller's avatar
Niels Möller committed
152
      ctx->input[9]++;
153
154
      /* stopping at 2^70 length per nonce is user's responsibility */
    }
Niels Möller's avatar
Niels Möller committed
155
    if (length <= SALSA20_BLOCK_SIZE) {
156
157
158
      for (i = 0;i < length;++i) c[i] = m[i] ^ output[i];
      return;
    }
Niels Möller's avatar
Niels Möller committed
159
160
161
162
    for (i = 0;i < SALSA20_BLOCK_SIZE;++i) c[i] = m[i] ^ output[i];
    length -= SALSA20_BLOCK_SIZE;
    c += SALSA20_BLOCK_SIZE;
    m += SALSA20_BLOCK_SIZE;
163
164
  }
}