]> WPIA git - gigi.git/blob - lib/scrypt/com/lambdaworks/crypto/SCrypt.java
UPD: document and justify call to System.exit in /kill
[gigi.git] / lib / scrypt / com / lambdaworks / crypto / SCrypt.java
1 // Copyright (C) 2011 - Will Glozer. All rights reserved.
2
3 package com.lambdaworks.crypto;
4
5 import static java.lang.Integer.*;
6 import static java.lang.System.*;
7
8 import java.security.GeneralSecurityException;
9
10 import javax.crypto.Mac;
11 import javax.crypto.spec.SecretKeySpec;
12
13 /**
14  * An implementation of the <a
15  * href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt</a> key derivation
16  * function. This class will attempt to load a native library containing the
17  * optimized C implementation from <a
18  * href="http://www.tarsnap.com/scrypt.html">http
19  * ://www.tarsnap.com/scrypt.html<a> and fall back to the pure Java version if
20  * that fails.
21  *
22  * @author Will Glozer
23  */
24 public class SCrypt {
25
26     private static final boolean native_library_loaded;
27
28     static {
29         // do not load native library
30         native_library_loaded = false;
31     }
32
33     /**
34      * Implementation of the <a
35      * href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt KDF</a>. Calls
36      * the native implementation {@link #scryptN} when the native library was
37      * successfully loaded, otherwise calls {@link #scryptJ}.
38      *
39      * @param passwd
40      *            Password.
41      * @param salt
42      *            Salt.
43      * @param N
44      *            CPU cost parameter.
45      * @param r
46      *            Memory cost parameter.
47      * @param p
48      *            Parallelization parameter.
49      * @param dkLen
50      *            Intended length of the derived key.
51      * @return The derived key.
52      * @throws GeneralSecurityException
53      *             when HMAC_SHA256 is not available.
54      */
55     public static byte[] scrypt(byte[] passwd, byte[] salt, int N, int r, int p, int dkLen) throws GeneralSecurityException {
56         return native_library_loaded ? scryptN(passwd, salt, N, r, p, dkLen) : scryptJ(passwd, salt, N, r, p, dkLen);
57     }
58
59     /**
60      * Native C implementation of the <a
61      * href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt KDF</a> using the
62      * code from <a
63      * href="http://www.tarsnap.com/scrypt.html">http://www.tarsnap.
64      * com/scrypt.html<a>.
65      *
66      * @param passwd
67      *            Password.
68      * @param salt
69      *            Salt.
70      * @param N
71      *            CPU cost parameter.
72      * @param r
73      *            Memory cost parameter.
74      * @param p
75      *            Parallelization parameter.
76      * @param dkLen
77      *            Intended length of the derived key.
78      * @return The derived key.
79      */
80     public static native byte[] scryptN(byte[] passwd, byte[] salt, int N, int r, int p, int dkLen);
81
82     /**
83      * Pure Java implementation of the <a
84      * href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt KDF</a>.
85      *
86      * @param passwd
87      *            Password.
88      * @param salt
89      *            Salt.
90      * @param N
91      *            CPU cost parameter.
92      * @param r
93      *            Memory cost parameter.
94      * @param p
95      *            Parallelization parameter.
96      * @param dkLen
97      *            Intended length of the derived key.
98      * @return The derived key.
99      * @throws GeneralSecurityException
100      *             when HMAC_SHA256 is not available.
101      */
102     public static byte[] scryptJ(byte[] passwd, byte[] salt, int N, int r, int p, int dkLen) throws GeneralSecurityException {
103         if (N < 2 || (N & (N - 1)) != 0) {
104             throw new IllegalArgumentException("N must be a power of 2 greater than 1");
105         }
106
107         if (N > MAX_VALUE / 128 / r) {
108             throw new IllegalArgumentException("Parameter N is too large");
109         }
110         if (r > MAX_VALUE / 128 / p) {
111             throw new IllegalArgumentException("Parameter r is too large");
112         }
113
114         Mac mac = Mac.getInstance("HmacSHA256");
115         mac.init(new SecretKeySpec(passwd, "HmacSHA256"));
116
117         byte[] DK = new byte[dkLen];
118
119         byte[] B = new byte[128 * r * p];
120         byte[] XY = new byte[256 * r];
121         byte[] V = new byte[128 * r * N];
122         int i;
123
124         PBKDF.pbkdf2(mac, salt, 1, B, p * 128 * r);
125
126         for (i = 0; i < p; i++) {
127             smix(B, i * 128 * r, r, N, V, XY);
128         }
129
130         PBKDF.pbkdf2(mac, B, 1, DK, dkLen);
131
132         return DK;
133     }
134
135     public static void smix(byte[] B, int Bi, int r, int N, byte[] V, byte[] XY) {
136         int Xi = 0;
137         int Yi = 128 * r;
138         int i;
139
140         arraycopy(B, Bi, XY, Xi, 128 * r);
141
142         for (i = 0; i < N; i++) {
143             arraycopy(XY, Xi, V, i * (128 * r), 128 * r);
144             blockmix_salsa8(XY, Xi, Yi, r);
145         }
146
147         for (i = 0; i < N; i++) {
148             int j = integerify(XY, Xi, r) & (N - 1);
149             blockxor(V, j * (128 * r), XY, Xi, 128 * r);
150             blockmix_salsa8(XY, Xi, Yi, r);
151         }
152
153         arraycopy(XY, Xi, B, Bi, 128 * r);
154     }
155
156     public static void blockmix_salsa8(byte[] BY, int Bi, int Yi, int r) {
157         byte[] X = new byte[64];
158         int i;
159
160         arraycopy(BY, Bi + (2 * r - 1) * 64, X, 0, 64);
161
162         for (i = 0; i < 2 * r; i++) {
163             blockxor(BY, i * 64, X, 0, 64);
164             salsa20_8(X);
165             arraycopy(X, 0, BY, Yi + (i * 64), 64);
166         }
167
168         for (i = 0; i < r; i++) {
169             arraycopy(BY, Yi + (i * 2) * 64, BY, Bi + (i * 64), 64);
170         }
171
172         for (i = 0; i < r; i++) {
173             arraycopy(BY, Yi + (i * 2 + 1) * 64, BY, Bi + (i + r) * 64, 64);
174         }
175     }
176
177     public static int R(int a, int b) {
178         return (a << b) | (a >>> (32 - b));
179     }
180
181     public static void salsa20_8(byte[] B) {
182         int[] B32 = new int[16];
183         int[] x = new int[16];
184         int i;
185
186         for (i = 0; i < 16; i++) {
187             B32[i] = (B[i * 4 + 0] & 0xff) << 0;
188             B32[i] |= (B[i * 4 + 1] & 0xff) << 8;
189             B32[i] |= (B[i * 4 + 2] & 0xff) << 16;
190             B32[i] |= (B[i * 4 + 3] & 0xff) << 24;
191         }
192
193         arraycopy(B32, 0, x, 0, 16);
194
195         for (i = 8; i > 0; i -= 2) {
196             x[4] ^= R(x[0] + x[12], 7);
197             x[8] ^= R(x[4] + x[0], 9);
198             x[12] ^= R(x[8] + x[4], 13);
199             x[0] ^= R(x[12] + x[8], 18);
200             x[9] ^= R(x[5] + x[1], 7);
201             x[13] ^= R(x[9] + x[5], 9);
202             x[1] ^= R(x[13] + x[9], 13);
203             x[5] ^= R(x[1] + x[13], 18);
204             x[14] ^= R(x[10] + x[6], 7);
205             x[2] ^= R(x[14] + x[10], 9);
206             x[6] ^= R(x[2] + x[14], 13);
207             x[10] ^= R(x[6] + x[2], 18);
208             x[3] ^= R(x[15] + x[11], 7);
209             x[7] ^= R(x[3] + x[15], 9);
210             x[11] ^= R(x[7] + x[3], 13);
211             x[15] ^= R(x[11] + x[7], 18);
212             x[1] ^= R(x[0] + x[3], 7);
213             x[2] ^= R(x[1] + x[0], 9);
214             x[3] ^= R(x[2] + x[1], 13);
215             x[0] ^= R(x[3] + x[2], 18);
216             x[6] ^= R(x[5] + x[4], 7);
217             x[7] ^= R(x[6] + x[5], 9);
218             x[4] ^= R(x[7] + x[6], 13);
219             x[5] ^= R(x[4] + x[7], 18);
220             x[11] ^= R(x[10] + x[9], 7);
221             x[8] ^= R(x[11] + x[10], 9);
222             x[9] ^= R(x[8] + x[11], 13);
223             x[10] ^= R(x[9] + x[8], 18);
224             x[12] ^= R(x[15] + x[14], 7);
225             x[13] ^= R(x[12] + x[15], 9);
226             x[14] ^= R(x[13] + x[12], 13);
227             x[15] ^= R(x[14] + x[13], 18);
228         }
229
230         for (i = 0; i < 16; ++i) {
231             B32[i] = x[i] + B32[i];
232         }
233
234         for (i = 0; i < 16; i++) {
235             B[i * 4 + 0] = (byte) (B32[i] >> 0 & 0xff);
236             B[i * 4 + 1] = (byte) (B32[i] >> 8 & 0xff);
237             B[i * 4 + 2] = (byte) (B32[i] >> 16 & 0xff);
238             B[i * 4 + 3] = (byte) (B32[i] >> 24 & 0xff);
239         }
240     }
241
242     public static void blockxor(byte[] S, int Si, byte[] D, int Di, int len) {
243         for (int i = 0; i < len; i++) {
244             D[Di + i] ^= S[Si + i];
245         }
246     }
247
248     public static int integerify(byte[] B, int Bi, int r) {
249         int n;
250
251         Bi += (2 * r - 1) * 64;
252
253         n = (B[Bi + 0] & 0xff) << 0;
254         n |= (B[Bi + 1] & 0xff) << 8;
255         n |= (B[Bi + 2] & 0xff) << 16;
256         n |= (B[Bi + 3] & 0xff) << 24;
257
258         return n;
259     }
260 }