static void generate_testvectors(const char *type) { #define TEST_OUTLEN 32 #define TEST_PWDLEN 32 #define TEST_SALTLEN 16 #define TEST_SECRETLEN 8 #define TEST_ADLEN 12 argon2_context context; unsigned char out[TEST_OUTLEN]; unsigned char pwd[TEST_PWDLEN]; unsigned char salt[TEST_SALTLEN]; unsigned char secret[TEST_SECRETLEN]; unsigned char ad[TEST_ADLEN]; const allocate_fptr myown_allocator = NULL; const deallocate_fptr myown_deallocator = NULL; unsigned t_cost = 3; unsigned m_cost = 16; unsigned lanes = 4; memset(pwd, 1, TEST_OUTLEN); memset(salt, 2, TEST_SALTLEN); memset(secret, 3, TEST_SECRETLEN); memset(ad, 4, TEST_ADLEN); context.out = out; context.outlen = TEST_OUTLEN; context.pwd = pwd; context.pwdlen = TEST_PWDLEN; context.salt = salt; context.saltlen = TEST_SALTLEN; context.secret = secret; context.secretlen = TEST_SECRETLEN; context.ad = ad; context.adlen = TEST_ADLEN; context.t_cost = t_cost; context.m_cost = m_cost; context.lanes = lanes; context.threads = lanes; context.allocate_cbk = myown_allocator; context.free_cbk = myown_deallocator; context.flags = 0; #undef TEST_OUTLEN #undef TEST_PWDLEN #undef TEST_SALTLEN #undef TEST_SECRETLEN #undef TEST_ADLEN if (!strcmp(type, "d")) { argon2d(&context); } else if (!strcmp(type, "i")) { argon2i(&context); } else fatal("wrong Argon2 type"); }
/* Runs Argon2 with certain inputs and parameters, inputs not cleared. Prints the Base64-encoded hash string @out output array with at least 32 bytes allocated @pwd NULL-terminated string, presumably from argv[] @salt salt array with at least SALTLEN_DEF bytes allocated @t_cost number of iterations @m_cost amount of requested memory in KB @lanes amount of requested parallelism @threads actual parallelism @type String, only "d" and "i" are accepted */ static void run(uint8_t *out, char *pwd, uint8_t *salt, uint32_t t_cost, uint32_t m_cost, uint32_t lanes, uint32_t threads, const char *type) { clock_t start_time, stop_time; unsigned pwd_length; argon2_context context; uint32_t i; start_time = clock(); if (!pwd) { fatal("password missing"); } if (!salt) { secure_wipe_memory(pwd, strlen(pwd)); fatal("salt missing"); } pwd_length = strlen(pwd); UNUSED_PARAMETER(threads); context.out = out; context.outlen = OUT_LEN; context.pwd = (uint8_t *)pwd; context.pwdlen = pwd_length; context.salt = salt; context.saltlen = SALT_LEN; context.secret = NULL; context.secretlen = 0; context.ad = NULL; context.adlen = 0; context.t_cost = t_cost; context.m_cost = m_cost; context.lanes = lanes; context.threads = lanes; context.allocate_cbk = NULL; context.free_cbk = NULL; context.flags = ARGON2_FLAG_CLEAR_PASSWORD; if (!strcmp(type, "d")) { int result = argon2d(&context); if (result != ARGON2_OK) fatal(error_message(result)); } else if (!strcmp(type, "i")) { int result = argon2i(&context); if (result != ARGON2_OK) fatal(error_message(result)); } else { secure_wipe_memory(pwd, strlen(pwd)); fatal("wrong Argon2 type"); } stop_time = clock(); /* add back when proper decoding */ /* char encoded[300]; encode_string(encoded, sizeof encoded, &context); printf("%s\n", encoded); */ printf("Hash:\t\t"); for (i = 0; i < context.outlen; ++i) { printf("%02x", context.out[i]); } printf("\n"); printf("%2.3f seconds\n", ((double)stop_time - start_time) / (CLOCKS_PER_SEC)); }