-
Notifications
You must be signed in to change notification settings - Fork 0
/
psi_hashing.c
executable file
·85 lines (70 loc) · 2.47 KB
/
psi_hashing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "psi_hashing.h"
#include "psi_misc.h"
void psi_get_64bit_sha256_with_seed(uint8_t * seed, uint8_t * element, uint64_t * ret_buffer, uint n) {
uint8_t tmp[16];
for (uint8_t i = 0; i < 16; i++)
tmp[i] = element[i]^seed[i];
psi_get_64bit_sha256(tmp, ret_buffer, n);
}
void psi_get_64bit_sha256(uint8_t * element, uint64_t * ret_buffer, uint n) {
uint8_t hash_buffer[SHA256_DIGEST_LENGTH];
uint64_t reducer[4];
get_sha256(element, hash_buffer, n);
for (uint8_t i = 0; i < 4; i++)
reducer[i] = 0;
for (uint8_t i = 0; i < 8; i++) {
reducer[0] += hash_buffer[i];
reducer[1] += hash_buffer[8 + i];
reducer[2] += hash_buffer[16 + i];
reducer[3] += hash_buffer[24 + i];
if (i != 7) {
reducer[0] <<= 8;
reducer[1] <<= 8;
reducer[2] <<= 8;
reducer[3] <<= 8;
}
}
reducer[0] ^= reducer[2];
reducer[1] ^= reducer[3];
*ret_buffer = reducer[0] ^ reducer[1];
}
void get_sha256(uint8_t * element, uint8_t digest[SHA256_DIGEST_LENGTH], uint n) {
SHA256_CTX c;
SHA256_Init(&c);
SHA256_Update(&c, element, n);
SHA256_Final(digest, &c);
}
void bytes_to_chars(uint8_t * byte_array, char * buffer, uint length) {
if (length % 8 == 0 && length < 256) {
for (uint8_t i = 0; i < length - 1; i += 8) {
sprintf(buffer + i * 2, "%02x%02x%02x%02x%02x%02x%02x%02x",
byte_array[i], byte_array[i + 1], byte_array[i + 2],
byte_array[i + 3], byte_array[i + 4], byte_array[i + 5],
byte_array[i + 6], byte_array[i + 7]);
}
} else
for (size_t i = 0; i < length; i++)
sprintf(buffer + i * 2, "%02x", byte_array[i]);
buffer[length * 2] = '\0';
}
void print_hash(uint8_t hash[32]) {
for (uint8_t i = 0; i < 32; i++)
printf("%02x", hash[i]);
printf("\n");
}
void get_16_byte_sha256(uint8_t * elem, uint8_t * hash, uint n) {
uint8_t buf[SHA256_DIGEST_LENGTH];
get_sha256(elem, buf, n);
for (int i = 0; i < 16; i++)
hash[i] = buf[i]^buf[i + 16];
}
void get_10_byte_sha256(uint8_t * elem, uint8_t * hash, uint n) {
uint8_t tmp[SHA256_DIGEST_LENGTH];
get_16_byte_sha256(elem, tmp, n);
xor10_elem(tmp, hash);
}
void get_10_byte_sha256_with_seed(uint8_t * seed, uint8_t * elem, uint8_t * hash, uint n) {
for (size_t i = 0; i < 16; i++)
elem[i] ^= seed[i];
get_10_byte_sha256(elem, hash, n);
}