Skip to content

Commit 550357c

Browse files
authored
Merge pull request #2 from bogdanadnan/dev
Dev
2 parents f12cb76 + 5e34f64 commit 550357c

File tree

14 files changed

+530
-78
lines changed

14 files changed

+530
-78
lines changed

common/common.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ string format_seconds(uint64_t seconds) {
5252
ss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2) << std::setfill('0') << reminder;
5353
return ss.str();
5454
}
55+
56+
string format_hashrate(int hashrate) {
57+
double khs = (double)hashrate / 1000.0;
58+
char buff[20];
59+
sprintf(buff, "%.1fk", khs);
60+
return string(buff);
61+
}

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ DLLEXPORT vector<string> get_files(string folder);
5858
DLLEXPORT bool is_number(const string &s);
5959
DLLEXPORT string generate_uid(size_t length);
6060
DLLEXPORT string format_seconds(uint64_t seconds);
61+
DLLEXPORT string format_hashrate(int hashrate);
6162

6263
#endif //IXIMINER_COMMON_H

crypt/hex.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77
#include "hex.h"
88

99
void hex::encode(const unsigned char *input, int input_size, char *output) {
10-
for(int i=0; i<input_size; i++) {
11-
sprintf(output, "%02x", input[i]);
12-
output+=2;
10+
for ( int i=0; i<input_size; i++ ) {
11+
char b1= *input >> 4; // hi nybble
12+
char b2= *input & 0x0f; // lo nybble
13+
b1+='0'; if (b1>'9') b1 += 7; // gap between '9' and 'A'
14+
b2+='0'; if (b2>'9') b2 += 7;
15+
*(output++)= b1;
16+
*(output++) = b2;
17+
input++;
1318
}
19+
*output = 0;
1420
}
1521

1622
int hex::decode(const char *input, unsigned char *output, int output_size) {
17-
char buff[3];
18-
size_t input_size = strlen(input);
19-
if(output_size < input_size / 2)
20-
return -1;
21-
22-
for(int i=0; i<input_size; i+=2) {
23-
strncpy(buff, &input[i], 2);
24-
output[i/2] = strtoul(buff, NULL, 16);
23+
size_t in_len = strlen(input);
24+
for ( int i=0; i<in_len; i+=2 ) {
25+
unsigned char b1= input[i] -'0'; if (b1>9) b1 -= 7;
26+
unsigned char b2= input[i+1] -'0'; if (b2>9) b2 -= 7;
27+
*(output++) = (b1<<4) + b2; // <<4 multiplies by 16
2528
}
26-
27-
return input_size / 2;
29+
return in_len / 2;
2830
}

hash/argon2/argon2.cpp

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
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

hash/argon2/argon2.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
#include "defs.h"
99
#include "../hasher.h"
1010

11+
typedef bool (*argon2_blocks_prehash)(void *, int, argon2profile *, void *);
1112
typedef void *(*argon2_blocks_filler_ptr)(void *, int, argon2profile *, void *);
13+
typedef bool (*argon2_blocks_posthash)(void *, int, argon2profile *, void *);
1214

1315
class DLLEXPORT argon2 {
1416
public:
15-
argon2(argon2_blocks_filler_ptr filler, void *seed_memory, void *user_data);
17+
argon2(argon2_blocks_prehash prehash, argon2_blocks_filler_ptr filler, argon2_blocks_posthash posthash, void *seed_memory, void *user_data);
1618

17-
void initialize_seeds(const argon2profile &profile);
18-
void fill_blocks(const argon2profile &profile);
19-
void encode_hashes(const argon2profile &profile);
19+
bool initialize_seeds(const argon2profile &profile);
20+
bool fill_blocks(const argon2profile &profile);
21+
bool encode_hashes(const argon2profile &profile);
2022

2123
vector<hash_data> generate_hashes(const argon2profile &profile, hash_data &input);
2224

@@ -30,7 +32,10 @@ class DLLEXPORT argon2 {
3032
void __initial_hash(const argon2profile &profile, uint8_t *blockhash, const char *base, size_t base_sz, const char *salt, size_t salt_sz);
3133
void __fill_first_blocks(const argon2profile &profile, uint8_t *blockhash, int thread);
3234

33-
argon2_blocks_filler_ptr __filler;
35+
argon2_blocks_prehash __prehash;
36+
argon2_blocks_filler_ptr __filler;
37+
argon2_blocks_posthash __posthash;
38+
3439
int __threads;
3540

3641
uint8_t *__seed_memory;

hash/argon2/defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#define ARGON2_RAW_LENGTH 32
99
#define ARGON2_TYPE_VALUE 2
1010
#define ARGON2_VERSION 0x13
11+
#define IXIAN_SEED_SIZE 156
1112

1213
#define ARGON2_BLOCK_SIZE 1024
14+
#define ARGON2_DWORDS_IN_BLOCK ARGON2_BLOCK_SIZE / 4
1315
#define ARGON2_QWORDS_IN_BLOCK ARGON2_BLOCK_SIZE / 8
1416
#define ARGON2_OWORDS_IN_BLOCK ARGON2_BLOCK_SIZE / 16
1517
#define ARGON2_HWORDS_IN_BLOCK ARGON2_BLOCK_SIZE / 32

hash/cpu/cpu_hasher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void cpu_hasher::__run() {
186186
return;
187187
}
188188

189-
argon2 hash_factory(__argon2_blocks_filler_ptr, mem, NULL);
189+
argon2 hash_factory(NULL, __argon2_blocks_filler_ptr, NULL, mem, NULL);
190190

191191
bool should_realloc = false;
192192

hash/gpu/amdgcn/amdgcn_hasher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ void amdgcn_hasher::__run(amdgcn_device_info *device, int thread_id) {
770770
thread_data.device = device;
771771
thread_data.thread_id = thread_id;
772772

773-
argon2 hash_factory(amdgcn_kernel_filler, memory, &thread_data);
773+
argon2 hash_factory(NULL, amdgcn_kernel_filler, NULL, memory, &thread_data);
774774
hash_factory.set_lane_length(2);
775775

776776
while(__running) {

0 commit comments

Comments
 (0)