-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcryptography.cc
executable file
·294 lines (217 loc) · 7.38 KB
/
cryptography.cc
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// cryptography.cc
/*******************************************************************************
CRYPTOGRAPHY -- Key-pair handling with OpenSSL
*******************************************************************************/
#include "cryptography.h"
#include "conversion.h"
#include "definitions.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/opensslv.h>
#include <random>
////////////////////////////////////////////////////////////////////////////////
// Generate secret key from seed
////////////////////////////////////////////////////////////////////////////////
int GenerateSecKey(
const char* in,
const int len,
uint8_t* sk,
char* skstr
)
{
ctx_t ctx;
uint64_t aux[32];
//====================================================================//
// Initialize context
//====================================================================//
memset(ctx.b, 0, 128);
B2B_IV(ctx.h);
ctx.h[0] ^= 0x01010000 ^ NUM_SIZE_8;
memset(ctx.t, 0, 16);
ctx.c = 0;
//====================================================================//
// Hash message
//====================================================================//
for (int i = 0; i < len; ++i)
{
if (ctx.c == 128) { HOST_B2B_H(&ctx, aux); }
ctx.b[ctx.c++] = (uint8_t)(in[i]);
}
HOST_B2B_H_LAST(&ctx, aux);
for (int i = 0; i < NUM_SIZE_8; ++i)
{
sk[NUM_SIZE_8 - i - 1] = (ctx.h[i >> 3] >> ((i & 7) << 3)) & 0xFF;
}
//====================================================================//
// Mod Q
//====================================================================//
uint8_t borrow[2];
borrow[0] = ((uint64_t*)sk)[0] < Q0;
aux[0] = ((uint64_t*)sk)[0] - Q0;
borrow[1] = ((uint64_t*)sk)[1] < Q1 + borrow[0];
aux[1] = ((uint64_t*)sk)[1] - Q1 - borrow[0];
borrow[0] = ((uint64_t*)sk)[2] < Q2 + borrow[1];
aux[2] = ((uint64_t*)sk)[2] - Q2 - borrow[1];
borrow[1] = ((uint64_t*)sk)[3] < Q3 + borrow[0];
aux[3] = ((uint64_t*)sk)[3] - Q3 - borrow[0];
if (!(borrow[1] || borrow[0])) { memcpy(sk, aux, NUM_SIZE_8); }
// convert secret key to hex string
LittleEndianToHexStr(sk, NUM_SIZE_8, skstr);
return EXIT_SUCCESS;
}
int GenerateSecKeyNew(
const char* in,
const int len,
uint8_t* sk,
char* skstr,
char* passphrase
)
{
unsigned char digest[NUM_SIZE_4];
char salt[1024] = "mnemonic";
strcat(salt, passphrase);
PKCS5_PBKDF2_HMAC(in, len, (unsigned char*)salt, strlen(salt), 2048, EVP_sha512(), NUM_SIZE_4, digest);
uint_t hmaclen = NUM_SIZE_4;
char key[] = "Bitcoin seed";
unsigned char result[NUM_SIZE_4];
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha512(), NULL);
HMAC_Update(&ctx, digest, NUM_SIZE_4);
HMAC_Final(&ctx, result, &hmaclen);
HMAC_CTX_cleanup(&ctx);
memcpy(sk, result, sizeof(uint8_t) * NUM_SIZE_8);
LittleEndianToHexStr(sk, NUM_SIZE_8, skstr);
HexStrToBigEndian(skstr, NUM_SIZE_4, sk, NUM_SIZE_8);
LittleEndianToHexStr(sk, NUM_SIZE_8, skstr);
#else
HMAC_CTX* ctx = HMAC_CTX_new();
HMAC_Init_ex(ctx, key, strlen(key), EVP_sha512(), NULL);
HMAC_Update(ctx, digest, NUM_SIZE_4);
HMAC_Final(ctx, result, &hmaclen);
memcpy(sk, result, sizeof(uint8_t) * NUM_SIZE_8);
HMAC_CTX_free(ctx);
LittleEndianToHexStr(sk, NUM_SIZE_8, skstr);
HexStrToBigEndian(skstr, NUM_SIZE_4, sk, NUM_SIZE_8);
LittleEndianToHexStr(sk, NUM_SIZE_8, skstr);
#endif
return EXIT_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////////
// Generate random key pair
////////////////////////////////////////////////////////////////////////////////
int GenerateKeyPair(uint8_t* sk, uint8_t* pk)
{
EC_KEY* eck = NULL;
EVP_PKEY* evpk = NULL;
FUNCTION_CALL(eck, EC_KEY_new_by_curve_name(NID_secp256k1), ERROR_OPENSSL);
// OPENSSL_EC_NAMED_CURVE flag for cert signing
EC_KEY_set_asn1_flag(eck, OPENSSL_EC_NAMED_CURVE);
// create public/private EC key pair
CALL(EC_KEY_generate_key(eck), ERROR_OPENSSL);
// convert EC key into PKEY structure
evpk = EVP_PKEY_new();
CALL(EVP_PKEY_assign_EC_KEY(evpk, eck), ERROR_OPENSSL);
// extract EC-specifics from the key
FUNCTION_CALL(eck, EVP_PKEY_get1_EC_KEY(evpk), ERROR_OPENSSL);
//====================================================================//
// Public key extraction
//====================================================================//
const EC_GROUP* group = EC_KEY_get0_group(eck);
const EC_POINT* ecp = EC_KEY_get0_public_key(eck);
CALL(group, ERROR_OPENSSL);
CALL(ecp, ERROR_OPENSSL);
char* str;
FUNCTION_CALL(
str, EC_POINT_point2hex(group, ecp, POINT_CONVERSION_COMPRESSED, NULL),
ERROR_OPENSSL
);
int len = 0;
for (; str[len] != '\0'; ++len) {}
HexStrToBigEndian(str, len, pk, PK_SIZE_8);
OPENSSL_free(str);
str = NULL;
//====================================================================//
// Secret key extraction
//====================================================================//
const BIGNUM* bn = EC_KEY_get0_private_key(eck);
CALL(bn, ERROR_OPENSSL);
FUNCTION_CALL(str, BN_bn2hex(bn), ERROR_OPENSSL);
len = 0;
for (; str[len] != '\0'; ++len) {}
HexStrToLittleEndian(str, len, sk, NUM_SIZE_8);
OPENSSL_free(str);
//====================================================================//
// Deallocation
//====================================================================//
EVP_PKEY_free(evpk);
EC_KEY_free(eck);
return EXIT_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////////
// Generate public key from secret key
////////////////////////////////////////////////////////////////////////////////
int GeneratePublicKey(
const char* skstr,
char* pkstr,
uint8_t* pk
)
{
EC_KEY* eck = NULL;
EC_POINT* sec = NULL;
BIGNUM* res;
BN_CTX* ctx;
FUNCTION_CALL(ctx, BN_CTX_new(), ERROR_OPENSSL);
res = BN_new();
CALL(BN_hex2bn(&res, skstr), ERROR_OPENSSL);
FUNCTION_CALL(eck, EC_KEY_new_by_curve_name(NID_secp256k1), ERROR_OPENSSL);
const EC_GROUP* group = EC_KEY_get0_group(eck);
CALL(group, ERROR_OPENSSL);
FUNCTION_CALL(sec, EC_POINT_new(group), ERROR_OPENSSL);
CALL(EC_KEY_set_private_key(eck, res), ERROR_OPENSSL);
CALL(EC_POINT_mul(group, sec, res, NULL, NULL, ctx), ERROR_OPENSSL);
CALL(EC_KEY_set_public_key(eck, sec), ERROR_OPENSSL);
//====================================================================//
// Public key extraction
//====================================================================//
const EC_POINT* pub = EC_KEY_get0_public_key(eck);
CALL(pub, ERROR_OPENSSL);
char* str;
FUNCTION_CALL(
str, EC_POINT_point2hex(group, pub, POINT_CONVERSION_COMPRESSED, NULL),
ERROR_OPENSSL
);
strcpy(pkstr, str);
int len = 0;
for (; str[len] != '\0'; ++len) {}
HexStrToBigEndian(str, len, pk, PK_SIZE_8);
//====================================================================//
// Deallocation
//====================================================================//
OPENSSL_free(str);
BN_CTX_free(ctx);
BN_free(res);
EC_KEY_free(eck);
return EXIT_SUCCESS;
}
//-----------------------
//--check std::random_device for different results
//---------
int checkRandomDevice()
{
std::random_device rd1;
std::random_device rd2;
if (rd1() == rd2()) return EXIT_FAILURE;
if (rd1() == rd2()) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
// cryptography.cc