30 #include <sys/types.h> 44 static void blkcpy(
void *,
void *,
size_t);
45 static void blkxor(
void *,
void *,
size_t);
46 static void salsa20_8(uint32_t[16]);
47 static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *,
size_t);
48 static uint64_t integerify(
void *,
size_t);
49 static void smix(uint8_t *,
size_t, uint64_t, uint32_t *, uint32_t *);
52 blkcpy(
void * dest,
void * src,
size_t len)
56 size_t L = len /
sizeof(size_t);
59 for (i = 0; i < L; i++)
64 blkxor(
void * dest,
void * src,
size_t len)
68 size_t L = len /
sizeof(size_t);
71 for (i = 0; i < L; i++)
80 salsa20_8(uint32_t B[16])
86 for (i = 0; i < 8; i += 2) {
87 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) 89 x[ 4] ^=
R(x[ 0]+x[12], 7); x[ 8] ^=
R(x[ 4]+x[ 0], 9);
90 x[12] ^=
R(x[ 8]+x[ 4],13); x[ 0] ^=
R(x[12]+x[ 8],18);
92 x[ 9] ^=
R(x[ 5]+x[ 1], 7); x[13] ^=
R(x[ 9]+x[ 5], 9);
93 x[ 1] ^=
R(x[13]+x[ 9],13); x[ 5] ^=
R(x[ 1]+x[13],18);
95 x[14] ^=
R(x[10]+x[ 6], 7); x[ 2] ^=
R(x[14]+x[10], 9);
96 x[ 6] ^=
R(x[ 2]+x[14],13); x[10] ^=
R(x[ 6]+x[ 2],18);
98 x[ 3] ^=
R(x[15]+x[11], 7); x[ 7] ^=
R(x[ 3]+x[15], 9);
99 x[11] ^=
R(x[ 7]+x[ 3],13); x[15] ^=
R(x[11]+x[ 7],18);
102 x[ 1] ^=
R(x[ 0]+x[ 3], 7); x[ 2] ^=
R(x[ 1]+x[ 0], 9);
103 x[ 3] ^=
R(x[ 2]+x[ 1],13); x[ 0] ^=
R(x[ 3]+x[ 2],18);
105 x[ 6] ^=
R(x[ 5]+x[ 4], 7); x[ 7] ^=
R(x[ 6]+x[ 5], 9);
106 x[ 4] ^=
R(x[ 7]+x[ 6],13); x[ 5] ^=
R(x[ 4]+x[ 7],18);
108 x[11] ^=
R(x[10]+x[ 9], 7); x[ 8] ^=
R(x[11]+x[10], 9);
109 x[ 9] ^=
R(x[ 8]+x[11],13); x[10] ^=
R(x[ 9]+x[ 8],18);
111 x[12] ^=
R(x[15]+x[14], 7); x[13] ^=
R(x[12]+x[15], 9);
112 x[14] ^=
R(x[13]+x[12],13); x[15] ^=
R(x[14]+x[13],18);
115 for (i = 0; i < 16; i++)
126 blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t *
X,
size_t r)
131 blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
134 for (i = 0; i < 2 * r; i += 2) {
136 blkxor(X, &Bin[i * 16], 64);
141 blkcpy(&Bout[i * 8], X, 64);
144 blkxor(X, &Bin[i * 16 + 16], 64);
149 blkcpy(&Bout[i * 8 + r * 16], X, 64);
158 integerify(
void * B,
size_t r)
160 uint32_t * X = (
void *)((uintptr_t)(B) + (2 * r - 1) * 64);
162 return (((uint64_t)(X[1]) << 32) + X[0]);
174 smix(uint8_t * B,
size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
177 uint32_t * Y = &XY[32 * r];
178 uint32_t * Z = &XY[64 * r];
184 for (k = 0; k < 32 * r; k++)
185 X[k] = le32dec(&B[4 * k]);
188 for (i = 0; i < N; i += 2) {
190 blkcpy(&V[i * (32 * r)], X, 128 * r);
193 blockmix_salsa8(X, Y, Z, r);
196 blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
199 blockmix_salsa8(Y, X, Z, r);
203 for (i = 0; i < N; i += 2) {
205 j = integerify(X, r) & (N - 1);
208 blkxor(X, &V[j * (32 * r)], 128 * r);
209 blockmix_salsa8(X, Y, Z, r);
212 j = integerify(Y, r) & (N - 1);
215 blkxor(Y, &V[j * (32 * r)], 128 * r);
216 blockmix_salsa8(Y, X, Z, r);
220 for (k = 0; k < 32 * r; k++)
221 le32enc(&B[4 * k], X[k]);
235 const uint8_t * salt,
size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
236 uint8_t * buf,
size_t buflen)
238 void *
B0, *
V0, * XY0;
245 #if SIZE_MAX > UINT32_MAX 246 if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
251 if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
255 if (r == 0 || p == 0) {
259 if (((N & (N - 1)) != 0) || (N < 2)) {
273 #ifdef HAVE_POSIX_MEMALIGN 274 if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
277 if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
279 XY = (uint32_t *)(XY0);
281 if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
283 V = (uint32_t *)(V0);
286 if ((B0 = malloc(128 * r * p + 63)) == NULL)
288 B = (uint8_t *)(((uintptr_t)(
B0) + 63) & ~ (uintptr_t)(63));
289 if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
291 XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
293 if ((V0 = malloc(128 * r * N + 63)) == NULL)
295 V = (uint32_t *)(((uintptr_t)(
V0) + 63) & ~ (uintptr_t)(63));
299 if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
301 MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
303 MAP_ANON | MAP_PRIVATE,
305 -1, 0)) == MAP_FAILED)
307 V = (uint32_t *)(V0);
314 for (i = 0; i < p; i++) {
316 smix(&B[i * 128 * r], r, N, V, XY);
324 if (munmap(V0, 128 * r * N))
int libscrypt_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p, uint8_t *buf, size_t buflen)
crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): Compute scrypt(passwd[0 ...
void libscrypt_PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and write the output to buf.