Skip to content

Commit

Permalink
Merge pull request #3 from spacemeshos/allow-for-reusing-allocs
Browse files Browse the repository at this point in the history
Added scrypt variant allowing to reuse allocations
  • Loading branch information
kenorb authored Feb 17, 2024
2 parents 54dcdbd + 2c3a6d8 commit 81f8dca
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
65 changes: 59 additions & 6 deletions scrypt-jane.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ scrypt_power_on_self_test(void) {
scrypt((uint8_t *)t->pw, strlen(t->pw), (uint8_t *)t->salt, strlen(t->salt), t->Nfactor, t->rfactor, t->pfactor, test_digest, sizeof(test_digest));
scrypt_valid &= scrypt_verify(post_vectors[i], test_digest, sizeof(test_digest));
}

if (!scrypt_valid) {
#if !defined(SCRYPT_TEST)
scrypt_fatal_error("scrypt: scrypt power-on-self-test failed");
Expand All @@ -79,9 +79,7 @@ scrypt_power_on_self_test(void) {
return res;
}

typedef struct scrypt_aligned_alloc_t {
uint8_t *mem, *ptr;
} scrypt_aligned_alloc;


#if defined(SCRYPT_TEST_SPEED)
static uint8_t *mem_base = (uint8_t *)0;
Expand Down Expand Up @@ -124,7 +122,7 @@ scrypt_alloc(uint64_t size) {
}

static void
scrypt_free(scrypt_aligned_alloc *aa) {
scrypt_free(const scrypt_aligned_alloc *aa) {
free(aa->mem);
}
#endif
Expand All @@ -141,7 +139,7 @@ scrypt(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t
scrypt_ROMixfn scrypt_ROMix = scrypt_getROMix();
#endif

#if !defined(SCRYPT_TEST)
#if defined(SCRYPT_TEST)
static int power_on_self_test = 0;
if (!power_on_self_test) {
power_on_self_test = 1;
Expand Down Expand Up @@ -191,3 +189,58 @@ scrypt(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t

//scrypt_ensure_zero(YX.ptr, (p + 1) * chunk_bytes);
}

const scrypt_instance* new_instance(unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor) {
if (Nfactor > scrypt_maxNfactor)
scrypt_fatal_error("scrypt: N out of range");
if (rfactor > scrypt_maxrfactor)
scrypt_fatal_error("scrypt: r out of range");
if (pfactor > scrypt_maxpfactor)
scrypt_fatal_error("scrypt: p out of range");

scrypt_instance* instance = (scrypt_instance *)malloc((size_t)sizeof(scrypt_instance));

instance->N = (1 << (Nfactor + 1));
instance->r = (1 << rfactor);
instance->p = (1 << pfactor);

uint32_t chunk_bytes = SCRYPT_BLOCK_BYTES * instance->r * 2;
instance->V = scrypt_alloc((uint64_t)instance->N * chunk_bytes);
instance->YX = scrypt_alloc((instance->p + 1) * chunk_bytes);
return instance;
}

void free_instance(const scrypt_instance* instance) {
scrypt_free(&instance->V);
scrypt_free(&instance->YX);
free((void*)instance);
}


void scrypt_preallocated(const scrypt_instance* const instance, const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char *out, size_t bytes) {
#if !defined(SCRYPT_CHOOSE_COMPILETIME)
scrypt_ROMixfn scrypt_ROMix = scrypt_getROMix();
#endif

#if defined(SCRYPT_TEST)
static int power_on_self_test = 0;
if (!power_on_self_test) {
power_on_self_test = 1;
if (!scrypt_power_on_self_test())
scrypt_fatal_error("scrypt: power on self test failed");
}
#endif
uint32_t chunk_bytes = SCRYPT_BLOCK_BYTES * instance->r * 2;
/* 1: X = PBKDF2(password, salt) */
uint8_t* Y = instance->YX.ptr;
uint8_t* X = Y + chunk_bytes;
scrypt_pbkdf2(password, password_len, salt, salt_len, 1, X, chunk_bytes * instance->p);

/* 2: X = ROMix(X) */
for (uint32_t i = 0; i < instance->p; i++) {
scrypt_ROMix((scrypt_mix_word_t *)(X + (chunk_bytes * i)), (scrypt_mix_word_t *)Y, (scrypt_mix_word_t *)instance->V.ptr, instance->N, instance->r);
}

/* 3: Out = PBKDF2(password, X) */
scrypt_pbkdf2(password, password_len, X, chunk_bytes * instance->p, 1, out, bytes);
}
17 changes: 17 additions & 0 deletions scrypt-jane.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@
*/

#include <stdlib.h>
#include <stdint.h>

typedef struct scrypt_aligned_alloc_t {
uint8_t *mem, *ptr;
} scrypt_aligned_alloc;

typedef struct scrypt_instance_t {
scrypt_aligned_alloc V;
scrypt_aligned_alloc YX;
uint32_t N, r, p;
} scrypt_instance;

typedef void (*scrypt_fatal_errorfn)(const char *msg);
void scrypt_set_fatal_error(scrypt_fatal_errorfn fn);

void scrypt(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor, unsigned char *out, size_t bytes);

void scrypt_preallocated(const scrypt_instance* const instance, const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char *out, size_t bytes);

const scrypt_instance* new_instance(unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor);

void free_instance(const scrypt_instance* instance);

#endif /* SCRYPT_JANE_H */

0 comments on commit 81f8dca

Please sign in to comment.