-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyUtils.cpp
172 lines (158 loc) · 5.37 KB
/
KeyUtils.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <cstdint>
#include <vector>
#include "KeyUtils.h"
#include "K12AndKeyUtil.h"
#include "logger.h"
#include <stdint.h>
#ifdef _WIN32
#define VOID_FUNC_DECL extern "C" void __declspec(dllexport)
#define BOOL_FUNC_DECL extern "C" bool __declspec(dllexport)
#else
#define VOID_FUNC_DECL extern "C" void __attribute__((visibility("default")))
#define BOOL_FUNC_DECL extern "C" bool __attribute__((visibility("default")))
#endif
BOOL_FUNC_DECL getSubseedFromSeed(const uint8_t* seed, uint8_t* subseed)
{
uint8_t seedBytes[55];
for (int i = 0; i < 55; i++)
{
if (seed[i] < 'a' || seed[i] > 'z')
{
return false;
}
seedBytes[i] = seed[i] - 'a';
}
KangarooTwelve(seedBytes, sizeof(seedBytes), subseed, 32);
return true;
}
VOID_FUNC_DECL getPrivateKeyFromSubSeed(const uint8_t* seed, uint8_t* privateKey)
{
KangarooTwelve(seed, 32, privateKey, 32);
}
VOID_FUNC_DECL getPublicKeyFromPrivateKey(const uint8_t* privateKey, uint8_t* publicKey)
{
point_t P;
ecc_mul_fixed((unsigned long long*)privateKey, P); // Compute public key
encode(P, publicKey);
}
VOID_FUNC_DECL getIdentityFromPublicKey(const uint8_t* pubkey, char* dstIdentity, bool isLowerCase)
{
uint8_t publicKey[32] ;
memcpy(publicKey, pubkey, 32);
uint16_t identity[61] = {0};
for (int i = 0; i < 4; i++)
{
unsigned long long publicKeyFragment = *((unsigned long long*)&publicKey[i << 3]);
for (int j = 0; j < 14; j++)
{
identity[i * 14 + j] = publicKeyFragment % 26 + (isLowerCase ? L'a' : L'A');
publicKeyFragment /= 26;
}
}
unsigned int identityBytesChecksum;
KangarooTwelve(publicKey, 32, (uint8_t*)&identityBytesChecksum, 3);
identityBytesChecksum &= 0x3FFFF;
for (int i = 0; i < 4; i++)
{
identity[56 + i] = identityBytesChecksum % 26 + (isLowerCase ? L'a' : L'A');
identityBytesChecksum /= 26;
}
identity[60] = 0;
for (int i = 0; i < 60; i++) dstIdentity[i] = identity[i];
}
VOID_FUNC_DECL getTxHashFromDigest(const uint8_t* digest, char* txHash)
{
bool isLowerCase = true;
getIdentityFromPublicKey(digest, txHash, isLowerCase);
}
VOID_FUNC_DECL getPublicKeyFromIdentity(const char* identity, uint8_t* publicKey)
{
unsigned char publicKeyBuffer[32];
for (int i = 0; i < 4; i++)
{
*((unsigned long long*)&publicKeyBuffer[i << 3]) = 0;
for (int j = 14; j-- > 0; )
{
if (identity[i * 14 + j] < 'A' || identity[i * 14 + j] > 'Z')
{
return;
}
*((unsigned long long*)&publicKeyBuffer[i << 3]) = *((unsigned long long*)&publicKeyBuffer[i << 3]) * 26 + (identity[i * 14 + j] - 'A');
}
}
memcpy(publicKey, publicKeyBuffer, 32);
}
BOOL_FUNC_DECL checkSumIdentity(char* identity)
{
unsigned char publicKeyBuffer[32];
for (int i = 0; i < 4; i++)
{
*((unsigned long long*) & publicKeyBuffer[i << 3]) = 0;
for (int j = 14; j-- > 0; )
{
if (identity[i * 14 + j] < 'A' || identity[i * 14 + j] > 'Z')
{
return false;
}
*((unsigned long long*) & publicKeyBuffer[i << 3]) = *((unsigned long long*) & publicKeyBuffer[i << 3]) * 26 + (identity[i * 14 + j] - 'A');
}
}
unsigned int identityBytesChecksum;
KangarooTwelve(publicKeyBuffer, 32, (unsigned char*)&identityBytesChecksum, 3);
identityBytesChecksum &= 0x3FFFF;
for (int i = 0; i < 4; i++)
{
if (identityBytesChecksum % 26 + 'A' != identity[56 + i])
{
return false;
}
identityBytesChecksum /= 26;
}
return true;
}
template <unsigned int hashByteLen>
void getDigestFromSiblings(
unsigned int depth,
const uint8_t *input,
unsigned int inputByteLen,
unsigned int inputIndex,
const uint8_t (*siblings)[hashByteLen],
uint8_t *output)
{
const uint32_t hash_byte_lenx2 = (hashByteLen << 1);
std::vector<uint8_t> pair_digests(hash_byte_lenx2);
std::vector<uint8_t> digest(hashByteLen);
// Hash the input
KangarooTwelve(input, inputByteLen, digest.data(), hashByteLen);
// Loop through the siblings hash and go to the root
unsigned int digest_index = inputIndex;
for (unsigned int i = 0 ; i < depth; i++)
{
uint8_t const* fisrt_digest = digest.data();
uint8_t const* second_digest = siblings[i];
// Depend on the odd or even of the index, the sibling is left or right node of the tree
// odd - sibling is the left node
// even - sibling is the right node
if (digest_index % 2 == 1)
{
fisrt_digest = siblings[i];
second_digest = digest.data();
}
// Concatenate pair of hashes
memcpy(pair_digests.data(), fisrt_digest, hashByteLen);
memcpy(pair_digests.data() + hashByteLen, second_digest, hashByteLen);
// Calculate the new hash for next level
KangarooTwelve(pair_digests.data(), hash_byte_lenx2, digest.data(), hashByteLen);
// Index of next level
digest_index = (digest_index >> 1);
}
memcpy(output, digest.data(), hashByteLen);
}
template
void getDigestFromSiblings<32>(
unsigned int depth,
const uint8_t *input,
unsigned int inputByteLen,
unsigned int inputIndex,
const uint8_t (*siblings)[32],
uint8_t *output);