Debian Codegraph @

cipher/scrypt.c

raw
1 /* scrypt.c - Scrypt password-based key derivation function.
2 * Copyright (C) 2012 Simon Josefsson
3 * Copyright (C) 2013 Christian Grothoff
4 * Copyright (C) 2013 g10 Code GmbH
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /* Adapted from the nettle, low-level cryptographics library for
23 * libgcrypt by Christian Grothoff; original license:
24 *
25 * Copyright (C) 2012 Simon Josefsson
26 *
27 * The nettle library is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU Lesser General Public License as published by
29 * the Free Software Foundation; either version 2.1 of the License, or (at your
30 * option) any later version.
31 *
32 * The nettle library is distributed in the hope that it will be useful, but
33 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
34 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
35 * License for more details.
36 *
37 * You should have received a copy of the GNU Lesser General Public License
38 * along with the nettle library; see the file COPYING.LIB. If not, see
39 * <http://www.gnu.org/licenses/>.
40 */
41
42 #include <config.h>
43 #include <assert.h>
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "g10lib.h"
48 #include "kdf-internal.h"
49 #include "bufhelp.h"
50
51 /* We really need a 64 bit type for this code. */
52 #define SALSA20_INPUT_LENGTH 16
53
54 #define ROTL32(n,x) (((x)<<(n)) | ((x)>>(32-(n))))
55
56
57 /* Reads a 64-bit integer, in network, big-endian, byte order */
58 #define READ_UINT64(p) buf_get_be64(p)
59
60
61 /* And the other, little-endian, byteorder */
62 #define LE_READ_UINT64(p) buf_get_le64(p)
63
64 #define LE_SWAP32(v) le_bswap32(v)
65
66
67 #define QROUND(x0, x1, x2, x3) do { \
68 x1 ^= ROTL32(7, x0 + x3); \
69 x2 ^= ROTL32(9, x1 + x0); \
70 x3 ^= ROTL32(13, x2 + x1); \
71 x0 ^= ROTL32(18, x3 + x2); \
72 } while(0)
73
74
75 static void
76 salsa20_core (u32 *dst, const u32 *src, unsigned int rounds)
77 {
78 u32 x[SALSA20_INPUT_LENGTH];
79 unsigned i;
80
81 assert ( (rounds & 1) == 0);
82
83 for (i = 0; i < SALSA20_INPUT_LENGTH; i++)
84 x[i] = LE_SWAP32(src[i]);
85
86 for (i = 0; i < rounds;i += 2)
87 {
88 QROUND(x[0], x[4], x[8], x[12]);
89 QROUND(x[5], x[9], x[13], x[1]);
90 QROUND(x[10], x[14], x[2], x[6]);
91 QROUND(x[15], x[3], x[7], x[11]);
92
93 QROUND(x[0], x[1], x[2], x[3]);
94 QROUND(x[5], x[6], x[7], x[4]);
95 QROUND(x[10], x[11], x[8], x[9]);
96 QROUND(x[15], x[12], x[13], x[14]);
97 }
98
99 for (i = 0; i < SALSA20_INPUT_LENGTH; i++)
100 {
101 u32 t = x[i] + LE_SWAP32(src[i]);
102 dst[i] = LE_SWAP32(t);
103 }
104 }
105
106
107 static void
108 scrypt_block_mix (u32 r, unsigned char *B, unsigned char *tmp2)
109 {
110 u64 i;
111 unsigned char *X = tmp2;
112 unsigned char *Y = tmp2 + 64;
113
114 #if 0
115 if (r == 1)
116 {
117 for (i = 0; i < 2 * r; i++)
118 {
119 size_t j;
120 printf ("B[%d] = ", (int)i);
121 for (j = 0; j < 64; j++)
122 {
123 if (j && !(j % 16))
124 printf ("\n ");
125 printf (" %02x", B[i * 64 + j]);
126 }
127 putchar ('\n');
128 }
129 }
130 #endif
131
132 /* X = B[2 * r - 1] */
133 memcpy (X, &B[(2 * r - 1) * 64], 64);
134
135 /* for i = 0 to 2 * r - 1 do */
136 for (i = 0; i <= 2 * r - 1; i++)
137 {
138 /* T = X xor B[i] */
139 buf_xor(X, X, &B[i * 64], 64);
140
141 /* X = Salsa (T) */
142 salsa20_core ((u32*)(void*)X, (u32*)(void*)X, 8);
143
144 /* Y[i] = X */
145 memcpy (&Y[i * 64], X, 64);
146 }
147
148 for (i = 0; i < r; i++)
149 {
150 memcpy (&B[i * 64], &Y[2 * i * 64], 64);
151 memcpy (&B[(r + i) * 64], &Y[(2 * i + 1) * 64], 64);
152 }
153
154 #if 0
155 if (r==1)
156 {
157 for (i = 0; i < 2 * r; i++)
158 {
159 size_t j;
160 printf ("B'[%d] =", (int)i);
161 for (j = 0; j < 64; j++)
162 {
163 if (j && !(j % 16))
164 printf ("\n ");
165 printf (" %02x", B[i * 64 + j]);
166 }
167 putchar ('\n');
168 }
169 }
170 #endif
171 }
172
173
174 static void
175 scrypt_ro_mix (u32 r, unsigned char *B, u64 N,
176 unsigned char *tmp1, unsigned char *tmp2)
177 {
178 unsigned char *X = B, *T = B;
179 u64 i;
180
181 #if 0
182 if (r == 1)
183 {
184 printf ("B = ");
185 for (i = 0; i < 128 * r; i++)
186 {
187 if (i && !(i % 16))
188 printf ("\n ");
189 printf (" %02x", B[i]);
190 }
191 putchar ('\n');
192 }
193 #endif
194
195 /* for i = 0 to N - 1 do */
196 for (i = 0; i <= N - 1; i++)
197 {
198 /* V[i] = X */
199 memcpy (&tmp1[i * 128 * r], X, 128 * r);
200
201 /* X = ScryptBlockMix (X) */
202 scrypt_block_mix (r, X, tmp2);
203 }
204
205 /* for i = 0 to N - 1 do */
206 for (i = 0; i <= N - 1; i++)
207 {
208 u64 j;
209
210 /* j = Integerify (X) mod N */
211 j = LE_READ_UINT64 (&X[128 * r - 64]) % N;
212
213 /* T = X xor V[j] */
214 buf_xor (T, T, &tmp1[j * 128 * r], 128 * r);
215
216 /* X = scryptBlockMix (T) */
217 scrypt_block_mix (r, T, tmp2);
218 }
219
220 #if 0
221 if (r == 1)
222 {
223 printf ("B' =");
224 for (i = 0; i < 128 * r; i++)
225 {
226 if (i && !(i % 16))
227 printf ("\n ");
228 printf (" %02x", B[i]);
229 }
230 putchar ('\n');
231 }
232 #endif
233 }
234
235
236 /*
237 *
238 */
239 gcry_err_code_t
240 _gcry_kdf_scrypt (const unsigned char *passwd, size_t passwdlen,
241 int algo, int subalgo,
242 const unsigned char *salt, size_t saltlen,
243 unsigned long iterations,
244 size_t dkLen, unsigned char *DK)
245 {
246 u64 N = subalgo; /* CPU/memory cost parameter. */
247 u32 r; /* Block size. */
248 u32 p = iterations; /* Parallelization parameter. */
249
250 gpg_err_code_t ec;
251 u32 i;
252 unsigned char *B = NULL;
253 unsigned char *tmp1 = NULL;
254 unsigned char *tmp2 = NULL;
255 size_t r128;
256 size_t nbytes;
257
258 if (subalgo < 1 || !iterations)
259 return GPG_ERR_INV_VALUE;
260
261 if (algo == GCRY_KDF_SCRYPT)
262 r = 8;
263 else if (algo == 41) /* Hack to allow the use of all test vectors. */
264 r = 1;
265 else
266 return GPG_ERR_UNKNOWN_ALGORITHM;
267
268 r128 = r * 128;
269 if (r128 / 128 != r)
270 return GPG_ERR_ENOMEM;
271
272 nbytes = p * r128;
273 if (r128 && nbytes / r128 != p)
274 return GPG_ERR_ENOMEM;
275
276 nbytes = N * r128;
277 if (r128 && nbytes / r128 != N)
278 return GPG_ERR_ENOMEM;
279
280 nbytes = 64 + r128;
281 if (nbytes < r128)
282 return GPG_ERR_ENOMEM;
283
284 B = xtrymalloc (p * r128);
285 if (!B)
286 {
287 ec = gpg_err_code_from_syserror ();
288 goto leave;
289 }
290
291 tmp1 = xtrymalloc (N * r128);
292 if (!tmp1)
293 {
294 ec = gpg_err_code_from_syserror ();
295 goto leave;
296 }
297
298 tmp2 = xtrymalloc (64 + r128);
299 if (!tmp2)
300 {
301 ec = gpg_err_code_from_syserror ();
302 goto leave;
303 }
304
305 ec = _gcry_kdf_pkdf2 (passwd, passwdlen, GCRY_MD_SHA256, salt, saltlen,
306 1 /* iterations */, p * r128, B);
307
308 for (i = 0; !ec && i < p; i++)
309 scrypt_ro_mix (r, &B[i * r128], N, tmp1, tmp2);
310
311 if (!ec)
312 ec = _gcry_kdf_pkdf2 (passwd, passwdlen, GCRY_MD_SHA256, B, p * r128,
313 1 /* iterations */, dkLen, DK);
314
315 leave:
316 xfree (tmp2);
317 xfree (tmp1);
318 xfree (B);
319
320 return ec;
321 }