1/*-
2 * Copyright 2009 Colin Percival
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * This file was originally written by Colin Percival as part of the Tarsnap
27 * online backup system.
28 */
29#include <stdio.h>
30#include <stdint.h>
31#include <string.h>
32#include <ByteOrder.h>
33#include "pbkdf2.h"
34#include "crypto_scrypt_smix.h"
35
36static void blkcpy(void *, const void *, size_t);
37static void blkxor(void *, const void *, size_t);
38static void salsa20_8(uint32_t[16]);
39static void blockmix_salsa8(const uint32_t *, uint32_t *, uint32_t *, size_t);
40static uint64_t integerify(const void *, size_t);
41
42static void
43blkcpy(void * dest, const void * src, size_t len)
44{
45	size_t * D = (size_t *)dest;
46	const size_t * S = (const size_t *)src;
47	size_t L = len / sizeof(size_t);
48	size_t i;
49
50	for (i = 0; i < L; i++)
51		D[i] = S[i];
52}
53
54static void
55blkxor(void * dest, const void * src, size_t len)
56{
57	size_t * D = (size_t *)dest;
58	const size_t * S = (const size_t *)src;
59	size_t L = len / sizeof(size_t);
60	size_t i;
61
62	for (i = 0; i < L; i++)
63		D[i] ^= S[i];
64}
65
66/**
67 * salsa20_8(B):
68 * Apply the salsa20/8 core to the provided block.
69 */
70static void
71salsa20_8(uint32_t B[16])
72{
73	uint32_t x[16];
74	size_t i;
75
76	blkcpy(x, B, 64);
77	for (i = 0; i < 8; i += 2) {
78#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
79		/* Operate on columns. */
80		x[ 4] ^= R(x[ 0]+x[12], 7);  x[ 8] ^= R(x[ 4]+x[ 0], 9);
81		x[12] ^= R(x[ 8]+x[ 4],13);  x[ 0] ^= R(x[12]+x[ 8],18);
82
83		x[ 9] ^= R(x[ 5]+x[ 1], 7);  x[13] ^= R(x[ 9]+x[ 5], 9);
84		x[ 1] ^= R(x[13]+x[ 9],13);  x[ 5] ^= R(x[ 1]+x[13],18);
85
86		x[14] ^= R(x[10]+x[ 6], 7);  x[ 2] ^= R(x[14]+x[10], 9);
87		x[ 6] ^= R(x[ 2]+x[14],13);  x[10] ^= R(x[ 6]+x[ 2],18);
88
89		x[ 3] ^= R(x[15]+x[11], 7);  x[ 7] ^= R(x[ 3]+x[15], 9);
90		x[11] ^= R(x[ 7]+x[ 3],13);  x[15] ^= R(x[11]+x[ 7],18);
91
92		/* Operate on rows. */
93		x[ 1] ^= R(x[ 0]+x[ 3], 7);  x[ 2] ^= R(x[ 1]+x[ 0], 9);
94		x[ 3] ^= R(x[ 2]+x[ 1],13);  x[ 0] ^= R(x[ 3]+x[ 2],18);
95
96		x[ 6] ^= R(x[ 5]+x[ 4], 7);  x[ 7] ^= R(x[ 6]+x[ 5], 9);
97		x[ 4] ^= R(x[ 7]+x[ 6],13);  x[ 5] ^= R(x[ 4]+x[ 7],18);
98
99		x[11] ^= R(x[10]+x[ 9], 7);  x[ 8] ^= R(x[11]+x[10], 9);
100		x[ 9] ^= R(x[ 8]+x[11],13);  x[10] ^= R(x[ 9]+x[ 8],18);
101
102		x[12] ^= R(x[15]+x[14], 7);  x[13] ^= R(x[12]+x[15], 9);
103		x[14] ^= R(x[13]+x[12],13);  x[15] ^= R(x[14]+x[13],18);
104#undef R
105	}
106	for (i = 0; i < 16; i++)
107		B[i] += x[i];
108}
109
110/**
111 * blockmix_salsa8(Bin, Bout, X, r):
112 * Compute Bout = BlockMix_{salsa20/8, r}(Bin).  The input Bin must be 128r
113 * bytes in length; the output Bout must also be the same size.  The
114 * temporary space X must be 64 bytes.
115 */
116static void
117blockmix_salsa8(const uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
118{
119	size_t i;
120
121	/* 1: X <-- B_{2r - 1} */
122	blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
123
124	/* 2: for i = 0 to 2r - 1 do */
125	for (i = 0; i < 2 * r; i += 2) {
126		/* 3: X <-- H(X \xor B_i) */
127		blkxor(X, &Bin[i * 16], 64);
128		salsa20_8(X);
129
130		/* 4: Y_i <-- X */
131		/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
132		blkcpy(&Bout[i * 8], X, 64);
133
134		/* 3: X <-- H(X \xor B_i) */
135		blkxor(X, &Bin[i * 16 + 16], 64);
136		salsa20_8(X);
137
138		/* 4: Y_i <-- X */
139		/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
140		blkcpy(&Bout[i * 8 + r * 16], X, 64);
141	}
142}
143
144/**
145 * integerify(B, r):
146 * Return the result of parsing B_{2r-1} as a little-endian integer.
147 */
148static uint64_t
149integerify(const void * B, size_t r)
150{
151	const uint32_t * X = (const uint32_t *)((uintptr_t)(B) + (2 * r - 1) * 64);
152
153	return (((uint64_t)(X[1]) << 32) + X[0]);
154}
155
156/**
157 * crypto_scrypt_smix(B, r, N, V, XY):
158 * Compute B = SMix_r(B, N).  The input B must be 128r bytes in length;
159 * the temporary storage V must be 128rN bytes in length; the temporary
160 * storage XY must be 256r + 64 bytes in length.  The value N must be a
161 * power of 2 greater than 1.  The arrays B, V, and XY must be aligned to a
162 * multiple of 64 bytes.
163 */
164void
165crypto_scrypt_smix(uint8_t * B, size_t r, uint64_t N, void * _V, void * XY)
166{
167	uint32_t * X = (uint32_t *)XY;
168	uint32_t * Y = (uint32_t *)((uint8_t *)(XY) + 128 * r);
169	uint32_t * Z = (uint32_t *)((uint8_t *)(XY) + 256 * r);
170	uint32_t * V = (uint32_t *)_V;
171	uint64_t i;
172	uint64_t j;
173	size_t k;
174
175	/* 1: X <-- B */
176	for (k = 0; k < 32 * r; k++) {
177		X[k] = B_LENDIAN_TO_HOST_INT32(((uint32_t*)B)[k]);
178	}
179
180	/* 2: for i = 0 to N - 1 do */
181	for (i = 0; i < N; i += 2) {
182		/* 3: V_i <-- X */
183		blkcpy(&V[i * (32 * r)], X, 128 * r);
184
185		/* 4: X <-- H(X) */
186		blockmix_salsa8(X, Y, Z, r);
187
188		/* 3: V_i <-- X */
189		blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
190
191		/* 4: X <-- H(X) */
192		blockmix_salsa8(Y, X, Z, r);
193	}
194
195	/* 6: for i = 0 to N - 1 do */
196	for (i = 0; i < N; i += 2) {
197		/* 7: j <-- Integerify(X) mod N */
198		j = integerify(X, r) & (N - 1);
199
200		/* 8: X <-- H(X \xor V_j) */
201		blkxor(X, &V[j * (32 * r)], 128 * r);
202		blockmix_salsa8(X, Y, Z, r);
203
204		/* 7: j <-- Integerify(X) mod N */
205		j = integerify(Y, r) & (N - 1);
206
207		/* 8: X <-- H(X \xor V_j) */
208		blkxor(Y, &V[j * (32 * r)], 128 * r);
209		blockmix_salsa8(Y, X, Z, r);
210	}
211
212	/* 10: B' <-- X */
213	for (k = 0; k < 32 * r; k++) {
214		uint32_t* B32 = &(reinterpret_cast<uint32_t*>(B)[k]);
215		*B32 = B_HOST_TO_LENDIAN_INT32(X[k]);
216	}
217}
218