1414#include " argon2.h"
1515#include " defs.h"
1616
17- argon2::argon2 (argon2_blocks_filler_ptr filler, void *seed_memory, void *user_data) {
17+ argon2::argon2 (argon2_blocks_prehash prehash, argon2_blocks_filler_ptr filler, argon2_blocks_posthash posthash, void *seed_memory, void *user_data) {
18+ __prehash = prehash;
1819 __filler = filler;
20+ __posthash = posthash;
1921 __threads = 1 ;
2022 __output_memory = __seed_memory = (uint8_t *)seed_memory;
2123 __seed_memory_offset = argon2profile_default->memsize ;
@@ -31,52 +33,92 @@ vector<hash_data> argon2::generate_hashes(const argon2profile &profile, hash_dat
3133 __inputs.push_back (input);
3234 }
3335
34- initialize_seeds (profile);
35- fill_blocks (profile);
36- encode_hashes (profile);
36+ if (initialize_seeds (profile)) {
37+ if (fill_blocks (profile)) {
38+ if (encode_hashes (profile)) {
39+ return __inputs;
40+ }
41+ }
42+ }
3743
38- return __inputs ;
44+ return vector<hash_data>() ;
3945}
4046
41- void argon2::initialize_seeds (const argon2profile &profile) {
42- uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
47+ bool argon2::initialize_seeds (const argon2profile &profile) {
4348 unsigned char base[256 ];
4449 unsigned char salt[256 ];
4550 size_t base_sz = 0 ;
4651 size_t salt_sz = 0 ;
4752
48- for (int i=0 ;i<__threads;i++) {
49- base_sz = hex::decode (__inputs[i].base .c_str (), base, 256 );
50- salt_sz = hex::decode (__inputs[i].nonce .c_str (), salt, 256 );
53+ if (__prehash != NULL ) {
54+ for (int i = 0 ; i < __threads; i++) {
55+ base_sz = hex::decode (__inputs[i].base .c_str (), base, 256 );
56+ salt_sz = hex::decode (__inputs[i].nonce .c_str (), salt, 256 );
57+
58+ memcpy (__seed_memory + i * IXIAN_SEED_SIZE, base, base_sz);
59+ memcpy (__seed_memory + i * IXIAN_SEED_SIZE + base_sz, salt, salt_sz);
60+ }
61+
62+ return (*__prehash)(__seed_memory, __threads, (argon2profile*)&profile, __user_data);
63+ }
64+ else {
65+ uint8_t blockhash[ARGON2_PREHASH_SEED_LENGTH];
66+
67+ for (int i = 0 ; i < __threads; i++) {
68+ base_sz = hex::decode (__inputs[i].base .c_str (), base, 256 );
69+ salt_sz = hex::decode (__inputs[i].nonce .c_str (), salt, 256 );
70+
71+ __initial_hash (profile, blockhash, (char *) base, base_sz, (char *) salt, salt_sz);
5172
52- __initial_hash (profile, blockhash, (char *)base, base_sz, (char *)salt, salt_sz);
73+ memset (blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0 ,
74+ ARGON2_PREHASH_SEED_LENGTH -
75+ ARGON2_PREHASH_DIGEST_LENGTH);
5376
54- memset (blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0 ,
55- ARGON2_PREHASH_SEED_LENGTH -
56- ARGON2_PREHASH_DIGEST_LENGTH);
77+ __fill_first_blocks (profile, blockhash, i);
78+ }
5779
58- __fill_first_blocks (profile, blockhash, i) ;
80+ return true ;
5981 }
6082}
6183
62- void argon2::fill_blocks (const argon2profile &profile) {
84+ bool argon2::fill_blocks (const argon2profile &profile) {
6385 __output_memory = (uint8_t *)(*__filler) (__seed_memory, __threads, (argon2profile*)&profile, __user_data);
86+ return __output_memory != NULL ;
6487}
6588
66- void argon2::encode_hashes (const argon2profile &profile) {
67- unsigned char raw_hash[ARGON2_RAW_LENGTH];
89+ bool argon2::encode_hashes (const argon2profile &profile) {
6890 char encoded_hash[ARGON2_RAW_LENGTH * 2 + 1 ];
6991
70- if (__output_memory != NULL ) {
71- for (int i = 0 ; i < __threads; i++) {
72- blake2b_long ((void *) raw_hash, ARGON2_RAW_LENGTH,
73- (void *) (__output_memory + i * __seed_memory_offset), ARGON2_BLOCK_SIZE);
92+ if (__posthash != NULL ) {
93+ if ((*__posthash)(__seed_memory, __threads, (argon2profile*)&profile, __user_data)) {
94+
95+ for (int i = 0 ; i < __threads; i++) {
96+ hex::encode (__seed_memory + i * ARGON2_RAW_LENGTH, ARGON2_RAW_LENGTH, encoded_hash);
97+
98+ __inputs[i].hash = encoded_hash;
99+ }
100+
101+ return true ;
102+ }
103+ return false ;
104+ }
105+ else {
106+ unsigned char raw_hash[ARGON2_RAW_LENGTH];
107+
108+ if (__output_memory != NULL ) {
109+ for (int i = 0 ; i < __threads; i++) {
110+ blake2b_long ((void *) raw_hash, ARGON2_RAW_LENGTH,
111+ (void *) (__output_memory + i * __seed_memory_offset), ARGON2_BLOCK_SIZE);
74112
75113
76- hex::encode (raw_hash, ARGON2_RAW_LENGTH, encoded_hash);
114+ hex::encode (raw_hash, ARGON2_RAW_LENGTH, encoded_hash);
77115
78- __inputs[i].hash = encoded_hash;
116+ __inputs[i].hash = encoded_hash;
117+ }
118+ return true ;
79119 }
120+ else
121+ return false ;
80122 }
81123}
82124
0 commit comments