Use stock libscrypt

This commit is contained in:
mikolajp 2018-02-25 23:16:36 +08:00
parent 71b83dc8cd
commit e4d467ff24
2 changed files with 26 additions and 83 deletions

View File

@ -5,8 +5,16 @@
#include <stdint.h>
#include <errno.h>
#include <crypto_scrypt.h>
#include <libscrypt.h>
/**
* 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. The value dkLen must be at most 32 * (2^32 - 1).
*/
void libscrypt_PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
uint64_t, uint8_t *, size_t);
static int _crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t,
uint64_t, uint32_t, uint32_t, uint8_t *, size_t);
@ -17,8 +25,8 @@ static int _crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t,
u3qes_hsl(u3_atom p, u3_atom pl,
u3_atom s, u3_atom sl,
u3_atom n,
u3_atom r,
u3_atom z,
u3_atom r,
u3_atom z,
u3_atom d)
{
// asserting that n is power of 2 in _crypto_scrypt
@ -30,7 +38,7 @@ static int _crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t,
(((c3_d)r * 128 * ((c3_d)n + z - 1)) <= (1 << 30))))
return u3m_bail(c3__exit);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(sl + 1);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(sl + 1);
u3r_bytes(0, pl, b_p, p); u3r_bytes(0, sl, b_s, s);
b_p[pl] = 0; b_s[sl]=0;
c3_y* buf = u3a_malloc(d);
@ -73,7 +81,7 @@ static int _crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t,
return u3m_bail(c3__exit);
c3_w pl = u3r_met(3, p); c3_w sl = u3r_met(3, s);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(sl + 1);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(sl + 1);
u3r_bytes(0, pl, b_p, p); u3r_bytes(0, sl, b_s, s);
b_p[pl] = 0; b_s[sl]=0;
c3_y* buf = u3a_malloc(d);
@ -112,12 +120,12 @@ static int _crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t,
(c != 0)))
return u3m_bail(c3__exit);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(pl + 1);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(pl + 1);
u3r_bytes(0, pl, b_p, p); u3r_bytes(0, sl, b_s, s);
b_p[pl] = 0; b_s[sl]=0;
c3_y* buf = u3a_malloc(d);
PBKDF2_SHA256(b_p, pl, b_s, sl, c, buf, d);
libscrypt_PBKDF2_SHA256(b_p, pl, b_s, sl, c, buf, d);
u3_noun res = u3i_bytes(d, buf);
u3a_free(b_p); u3a_free(b_s); u3a_free(buf);
@ -147,12 +155,12 @@ static int _crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t,
return u3m_bail(c3__exit);
c3_w pl = u3r_met(3, p); c3_w sl = u3r_met(3, s);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(pl + 1);
c3_y* b_p = u3a_malloc(pl + 1); c3_y* b_s= u3a_malloc(pl + 1);
u3r_bytes(0, pl, b_p, p); u3r_bytes(0, sl, b_s, s);
b_p[pl] = 0; b_s[sl]=0;
c3_y* buf = u3a_malloc(d);
PBKDF2_SHA256(b_p, pl, b_s, sl, c, buf, d);
libscrypt_PBKDF2_SHA256(b_p, pl, b_s, sl, c, buf, d);
u3_noun res = u3i_bytes(d, buf);
u3a_free(b_p); u3a_free(b_s); u3a_free(buf);
@ -213,77 +221,5 @@ _crypto_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)
{
void * B0, * V0, * XY0;
uint8_t * B;
uint32_t * V;
uint32_t * XY;
uint32_t i;
if (((N & (N-1)) != 0) || N == 0)
goto err0;
/* Sanity-check parameters. */
#if SIZE_MAX > UINT32_MAX
if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
errno = EFBIG;
goto err0;
}
#endif
if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
errno = EFBIG;
goto err0;
}
if (((N & (N - 1)) != 0) || (N == 0)) {
errno = EINVAL;
goto err0;
}
int test_size_max = (r > SIZE_MAX / 128 / p) || (N > SIZE_MAX / 128 / r);
#if SIZE_MAX / 256 <= UINT32_MAX
test_size_max = (r > (SIZE_MAX - 64) / 256) || test_size_max;
#endif
if(test_size_max) {
errno = ENOMEM;
goto err0;
}
/* Allocate memory. */
if ((B0 = u3a_malloc(128 * r * p + 63)) == NULL)
goto err0;
B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
if ((XY0 = u3a_malloc(256 * r + 64 + 63)) == NULL)
goto err1;
XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
if ((V0 = u3a_malloc(128 * r * N + 63)) == NULL)
goto err2;
V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
/* 2: for i = 0 to p - 1 do */
for (i = 0; i < p; i++) {
/* 3: B_i <-- MF(B_i, N) */
smix(&B[i * 128 * r], r, N, V, XY);
}
/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
/* Free memory. */
u3a_free(V0);
u3a_free(XY0);
u3a_free(B0);
/* Success! */
return (0);
err2:
u3a_free(XY0);
err1:
u3a_free(B0);
err0:
/* Failure! */
return (-1);
return libscrypt_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen);
}

View File

@ -267,7 +267,14 @@ sigsegv_dep = meson.get_compiler('c').find_library('sigsegv')
# For these libs we provide fallback bundle
cmark_dep = dependency('libcmark', version: '0.12.0', fallback: ['commonmark-legacy', 'cmark_dep'])
urbitscrypt_dep = dependency('libscrypt', fallback: ['urbit-scrypt', 'urbit_scrypt_dep'])
compiler_scrypt = meson.get_compiler('c').find_library('libscrypt')
if compiler_scrypt.found()
urbitscrypt_dep = compiler_scrypt
else
urbitscrypt_dep = dependency('libscrypt', fallback: ['urbit-scrypt', 'urbit_scrypt_dep'])
endif
ed25519_dep = dependency('ed25519', fallback: ['ed25519', 'ed25519_dep'])
murmur3_dep = dependency('murmur3', fallback: ['murmur3', 'murmur3_dep'])
http_parser_dep = dependency('http-parser', version: '0.1.0', fallback: ['http-parser-legacy', 'http_parser_dep'])