1 // Copyright (C) 2011 - Will Glozer. All rights reserved.
3 package com.lambdaworks.crypto;
5 import static java.lang.Integer.*;
6 import static java.lang.System.*;
8 import java.security.GeneralSecurityException;
10 import javax.crypto.Mac;
11 import javax.crypto.spec.SecretKeySpec;
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
26 private static final boolean native_library_loaded;
29 // do not load native library
30 native_library_loaded = false;
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}.
46 * Memory cost parameter.
48 * Parallelization parameter.
50 * Intended length of the derived key.
51 * @return The derived key.
52 * @throws GeneralSecurityException
53 * when HMAC_SHA256 is not available.
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);
60 * Native C implementation of the <a
61 * href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt KDF</a> using the
63 * href="http://www.tarsnap.com/scrypt.html">http://www.tarsnap.
73 * Memory cost parameter.
75 * Parallelization parameter.
77 * Intended length of the derived key.
78 * @return The derived key.
80 public static native byte[] scryptN(byte[] passwd, byte[] salt, int N, int r, int p, int dkLen);
83 * Pure Java implementation of the <a
84 * href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt KDF</a>.
93 * Memory cost parameter.
95 * Parallelization parameter.
97 * Intended length of the derived key.
98 * @return The derived key.
99 * @throws GeneralSecurityException
100 * when HMAC_SHA256 is not available.
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");
107 if (N > MAX_VALUE / 128 / r) {
108 throw new IllegalArgumentException("Parameter N is too large");
110 if (r > MAX_VALUE / 128 / p) {
111 throw new IllegalArgumentException("Parameter r is too large");
114 Mac mac = Mac.getInstance("HmacSHA256");
115 mac.init(new SecretKeySpec(passwd, "HmacSHA256"));
117 byte[] DK = new byte[dkLen];
119 byte[] B = new byte[128 * r * p];
120 byte[] XY = new byte[256 * r];
121 byte[] V = new byte[128 * r * N];
124 PBKDF.pbkdf2(mac, salt, 1, B, p * 128 * r);
126 for (i = 0; i < p; i++) {
127 smix(B, i * 128 * r, r, N, V, XY);
130 PBKDF.pbkdf2(mac, B, 1, DK, dkLen);
135 public static void smix(byte[] B, int Bi, int r, int N, byte[] V, byte[] XY) {
140 arraycopy(B, Bi, XY, Xi, 128 * r);
142 for (i = 0; i < N; i++) {
143 arraycopy(XY, Xi, V, i * (128 * r), 128 * r);
144 blockmix_salsa8(XY, Xi, Yi, r);
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);
153 arraycopy(XY, Xi, B, Bi, 128 * r);
156 public static void blockmix_salsa8(byte[] BY, int Bi, int Yi, int r) {
157 byte[] X = new byte[64];
160 arraycopy(BY, Bi + (2 * r - 1) * 64, X, 0, 64);
162 for (i = 0; i < 2 * r; i++) {
163 blockxor(BY, i * 64, X, 0, 64);
165 arraycopy(X, 0, BY, Yi + (i * 64), 64);
168 for (i = 0; i < r; i++) {
169 arraycopy(BY, Yi + (i * 2) * 64, BY, Bi + (i * 64), 64);
172 for (i = 0; i < r; i++) {
173 arraycopy(BY, Yi + (i * 2 + 1) * 64, BY, Bi + (i + r) * 64, 64);
177 public static int R(int a, int b) {
178 return (a << b) | (a >>> (32 - b));
181 public static void salsa20_8(byte[] B) {
182 int[] B32 = new int[16];
183 int[] x = new int[16];
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;
193 arraycopy(B32, 0, x, 0, 16);
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);
230 for (i = 0; i < 16; ++i) {
231 B32[i] = x[i] + B32[i];
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);
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];
248 public static int integerify(byte[] B, int Bi, int r) {
251 Bi += (2 * r - 1) * 64;
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;