| 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 |
#include "common.h" |
| 29 |
#include "endianess.h" |
| 30 |
|
| 31 |
FAKE_INIT(Salsa20) |
| 32 |
|
| 33 |
#define ROUNDS 20 |
| 34 |
#define MAX_KEY_SIZE 32 |
| 35 |
|
| 36 |
static const uint8_t sigma[16] = "expand 32-byte k"; |
| 37 |
static const uint8_t tau[16] = "expand 16-byte k"; |
| 38 |
|
| 39 |
#define ROTL32(v, c) (((v) << (c)) | ((v) >> (32 - (c)))) |
| 40 |
#define XOR(v,w) ((v) ^ (w)) |
| 41 |
|
| 42 |
typedef struct { |
| 43 |
uint32_t input[16]; |
| 44 |
uint8_t block[64]; |
| 45 |
uint8_t blockindex; |
| 46 |
} stream_state; |
| 47 |
|
| 48 |
static void _salsa20_block(unsigned rounds, uint32_t *input, uint8_t *output) |
| 49 |
{ |
| 50 |
unsigned x0, x1, x2, x3, x4, x5, x6, x7; |
| 51 |
unsigned x8, x9, x10, x11, x12, x13, x14, x15; |
| 52 |
unsigned i; |
| 53 |
|
| 54 |
x0 = input[0]; |
| 55 |
x1 = input[1]; |
| 56 |
x2 = input[2]; |
| 57 |
x3 = input[3]; |
| 58 |
x4 = input[4]; |
| 59 |
x5 = input[5]; |
| 60 |
x6 = input[6]; |
| 61 |
x7 = input[7]; |
| 62 |
x8 = input[8]; |
| 63 |
x9 = input[9]; |
| 64 |
x10 = input[10]; |
| 65 |
x11 = input[11]; |
| 66 |
x12 = input[12]; |
| 67 |
x13 = input[13]; |
| 68 |
x14 = input[14]; |
| 69 |
x15 = input[15]; |
| 70 |
|
| 71 |
for (i = rounds; i > 0; i -= 2) { |
| 72 |
|
| 73 |
x4 = XOR( x4, ROTL32( x0 + x12, 7)); |
| 74 |
x8 = XOR( x8, ROTL32( x4 + x0, 9)); |
| 75 |
x12 = XOR(x12, ROTL32( x8 + x4, 13)); |
| 76 |
x0 = XOR( x0, ROTL32(x12 + x8, 18)); |
| 77 |
x9 = XOR( x9, ROTL32( x5 + x1, 7)); |
| 78 |
x13 = XOR(x13, ROTL32( x9 + x5, 9)); |
| 79 |
x1 = XOR( x1, ROTL32(x13 + x9, 13)); |
| 80 |
x5 = XOR( x5, ROTL32( x1 + x13, 18)); |
| 81 |
x14 = XOR(x14, ROTL32(x10 + x6, 7)); |
| 82 |
x2 = XOR( x2, ROTL32(x14 + x10, 9)); |
| 83 |
x6 = XOR( x6, ROTL32( x2 + x14, 13)); |
| 84 |
x10 = XOR(x10, ROTL32( x6 + x2, 18)); |
| 85 |
x3 = XOR( x3, ROTL32(x15 + x11, 7)); |
| 86 |
x7 = XOR( x7, ROTL32( x3 + x15, 9)); |
| 87 |
x11 = XOR(x11, ROTL32( x7 + x3, 13)); |
| 88 |
x15 = XOR(x15, ROTL32(x11 + x7, 18)); |
| 89 |
|
| 90 |
|
| 91 |
x1 = XOR( x1, ROTL32( x0 + x3, 7)); |
| 92 |
x2 = XOR( x2, ROTL32( x1 + x0, 9)); |
| 93 |
x3 = XOR( x3, ROTL32( x2 + x1, 13)); |
| 94 |
x0 = XOR( x0, ROTL32( x3 + x2, 18)); |
| 95 |
x6 = XOR( x6, ROTL32( x5 + x4, 7)); |
| 96 |
x7 = XOR( x7, ROTL32( x6 + x5, 9)); |
| 97 |
x4 = XOR( x4, ROTL32( x7 + x6, 13)); |
| 98 |
x5 = XOR( x5, ROTL32( x4 + x7, 18)); |
| 99 |
x11 = XOR(x11, ROTL32(x10 + x9, 7)); |
| 100 |
x8 = XOR( x8, ROTL32(x11 + x10, 9)); |
| 101 |
x9 = XOR( x9, ROTL32( x8 + x11, 13)); |
| 102 |
x10 = XOR(x10, ROTL32( x9 + x8, 18)); |
| 103 |
x12 = XOR(x12, ROTL32(x15 + x14, 7)); |
| 104 |
x13 = XOR(x13, ROTL32(x12 + x15, 9)); |
| 105 |
x14 = XOR(x14, ROTL32(x13 + x12, 13)); |
| 106 |
x15 = XOR(x15, ROTL32(x14 + x13, 18)); |
| 107 |
} |
| 108 |
|
| 109 |
x0 = x0 + input[0]; |
| 110 |
x1 = x1 + input[1]; |
| 111 |
x2 = x2 + input[2]; |
| 112 |
x3 = x3 + input[3]; |
| 113 |
x4 = x4 + input[4]; |
| 114 |
x5 = x5 + input[5]; |
| 115 |
x6 = x6 + input[6]; |
| 116 |
x7 = x7 + input[7]; |
| 117 |
x8 = x8 + input[8]; |
| 118 |
x9 = x9 + input[9]; |
| 119 |
x10 = x10 + input[10]; |
| 120 |
x11 = x11 + input[11]; |
| 121 |
x12 = x12 + input[12]; |
| 122 |
x13 = x13 + input[13]; |
| 123 |
x14 = x14 + input[14]; |
| 124 |
x15 = x15 + input[15]; |
| 125 |
|
| 126 |
STORE_U32_LITTLE (output + 0, x0); |
| 127 |
STORE_U32_LITTLE (output + 4, x1); |
| 128 |
STORE_U32_LITTLE (output + 8, x2); |
| 129 |
STORE_U32_LITTLE (output + 12, x3); |
| 130 |
STORE_U32_LITTLE (output + 16, x4); |
| 131 |
STORE_U32_LITTLE (output + 20, x5); |
| 132 |
STORE_U32_LITTLE (output + 24, x6); |
| 133 |
STORE_U32_LITTLE (output + 28, x7); |
| 134 |
STORE_U32_LITTLE (output + 32, x8); |
| 135 |
STORE_U32_LITTLE (output + 36, x9); |
| 136 |
STORE_U32_LITTLE (output + 40, x10); |
| 137 |
STORE_U32_LITTLE (output + 44, x11); |
| 138 |
STORE_U32_LITTLE (output + 48, x12); |
| 139 |
STORE_U32_LITTLE (output + 52, x13); |
| 140 |
STORE_U32_LITTLE (output + 56, x14); |
| 141 |
STORE_U32_LITTLE (output + 60, x15); |
| 142 |
|
| 143 |
|
| 144 |
input[8] = input[8] + 1; |
| 145 |
if (!input[8]) { |
| 146 |
input[9] = input[9] + 1; |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
EXPORT_SYM int Salsa20_8_core(const uint8_t *x, const uint8_t *y, uint8_t *out) |
| 159 |
{ |
| 160 |
uint32_t input_32[16]; |
| 161 |
int i; |
| 162 |
|
| 163 |
if (NULL==x || NULL==y || NULL==out) |
| 164 |
return ERR_NULL; |
| 165 |
|
| 166 |
for (i=0; i<16; i++) { |
| 167 |
uint32_t tmp; |
| 168 |
|
| 169 |
tmp = LOAD_U32_LITTLE(&x[i*4]); |
| 170 |
input_32[i] = LOAD_U32_LITTLE(&y[i*4]); |
| 171 |
input_32[i] ^= tmp; |
| 172 |
} |
| 173 |
|
| 174 |
_salsa20_block(8, input_32, out); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
EXPORT_SYM int Salsa20_stream_init(uint8_t *key, size_t keylen, |
| 179 |
uint8_t *nonce, size_t nonce_len, |
| 180 |
stream_state **pSalsaState) |
| 181 |
{ |
| 182 |
const uint8_t *constants; |
| 183 |
uint32_t *input; |
| 184 |
stream_state *salsaState; |
| 185 |
unsigned i; |
| 186 |
|
| 187 |
if (NULL == pSalsaState || NULL == key || NULL == nonce) |
| 188 |
return ERR_NULL; |
| 189 |
|
| 190 |
if (keylen != 16 && keylen != 32) |
| 191 |
return ERR_KEY_SIZE; |
| 192 |
constants = keylen == 32 ? sigma : tau; |
| 193 |
|
| 194 |
if (nonce_len != 8) |
| 195 |
return ERR_NONCE_SIZE; |
| 196 |
|
| 197 |
*pSalsaState = salsaState = calloc(1, sizeof(stream_state)); |
| 198 |
if (NULL == salsaState) |
| 199 |
return ERR_MEMORY; |
| 200 |
|
| 201 |
input = salsaState->input; |
| 202 |
|
| 203 |
input[0] = LOAD_U32_LITTLE(constants + 0); |
| 204 |
|
| 205 |
for (i=0; i<4; i++) |
| 206 |
input[i+1] = LOAD_U32_LITTLE(key + 4*i); |
| 207 |
input[5] = LOAD_U32_LITTLE(constants + 4); |
| 208 |
input[6] = LOAD_U32_LITTLE(nonce + 0); |
| 209 |
input[7] = LOAD_U32_LITTLE(nonce + 4); |
| 210 |
|
| 211 |
input[8] = 0; |
| 212 |
input[9] = 0; |
| 213 |
input[10] = LOAD_U32_LITTLE(constants + 8); |
| 214 |
|
| 215 |
if (keylen == 32) { |
| 216 |
key += 16; |
| 217 |
} |
| 218 |
for (i=0; i<4; i++) |
| 219 |
input[i+11] = LOAD_U32_LITTLE(key + 4*i); |
| 220 |
input[15] = LOAD_U32_LITTLE(constants + 12); |
| 221 |
|
| 222 |
salsaState->blockindex = 64; |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
EXPORT_SYM int Salsa20_stream_destroy(stream_state *salsaState) |
| 227 |
{ |
| 228 |
free(salsaState); |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
EXPORT_SYM int Salsa20_stream_encrypt(stream_state *salsaState, const uint8_t in[], |
| 233 |
uint8_t out[], size_t len) |
| 234 |
{ |
| 235 |
unsigned i; |
| 236 |
for (i = 0; i < len; ++i) { |
| 237 |
if (salsaState->blockindex == 64) { |
| 238 |
salsaState->blockindex = 0; |
| 239 |
_salsa20_block(ROUNDS, salsaState->input, salsaState->block); |
| 240 |
} |
| 241 |
out[i] = in[i] ^ salsaState->block[salsaState->blockindex]; |
| 242 |
salsaState->blockindex++; |
| 243 |
} |
| 244 |
return 0; |
| 245 |
} |