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
|
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <tcl.h>
#ifdef NANO_TCL_HAVE_OPENMP
# include <omp.h>
#endif
#include "randombytes.h"
#include "tweetnacl.h"
#include "blake2.h"
#define NANO_SECRET_KEY_LENGTH (crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES)
#define NANO_PUBLIC_KEY_LENGTH (crypto_sign_PUBLICKEYBYTES)
#define NANO_BLOCK_HASH_LENGTH 32
#define NANO_BLOCK_SIGNATURE_LENGTH crypto_sign_BYTES
#define NANO_WORK_VALUE_LENGTH 8
#define NANO_WORK_HASH_LENGTH 8
#define NANO_WORK_DEFAULT_MIN 0xffffffc000000000LLU
#define TclNano_AttemptAlloc(x) ((void *) Tcl_AttemptAlloc(x))
#define TclNano_Free(x) Tcl_Free((char *) x)
#define TclNano_SetIntVar(interp, name, intValue) \
tclobj_ret = Tcl_SetVar2Ex(interp, name, NULL, Tcl_NewIntObj(intValue), TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
|
>
>
>
>
|
|
|
|
|
>
>
>
|
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
|
/* XXX:TODO: OpenMP support is currently incomplete */
#undef NANO_TCL_HAVE_OPENMP
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <tcl.h>
#ifdef NANO_TCL_HAVE_OPENMP
# include <omp.h>
#endif
#include "randombytes.h"
#include "monocypher.h"
#include "argon2.h"
#include "aes.h"
#define NANO_SECRET_KEY_LENGTH 32
#define NANO_PUBLIC_KEY_LENGTH 32
#define NANO_BLOCK_HASH_LENGTH 32
#define NANO_BLOCK_SIGNATURE_LENGTH 64
#define NANO_WORK_VALUE_LENGTH 8
#define NANO_WORK_HASH_LENGTH 8
#define NANO_WORK_DEFAULT_MIN 0xffffffc000000000LLU
#define NANO_KDF_ARGON2_MEMORY 64 * 1024
#define NANO_KDF_ARGON2_TIMING 1
#define NANO_KDF_ARGON2_THREADS 1
#define TclNano_AttemptAlloc(x) ((void *) Tcl_AttemptAlloc(x))
#define TclNano_Free(x) Tcl_Free((char *) x)
#define TclNano_SetIntVar(interp, name, intValue) \
tclobj_ret = Tcl_SetVar2Ex(interp, name, NULL, Tcl_NewIntObj(intValue), TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
|
︙ | | | ︙ | |
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
|
#define TclNano_PkgProvide(interp, name, version) \
tclcmd_ret = Tcl_PkgProvide(interp, name, version); \
if (tclcmd_ret != TCL_OK) { \
return(tclcmd_ret); \
}
static unsigned char *nano_parse_secret_key(Tcl_Obj *secret_key_only_obj, int *out_key_length) {
unsigned char *secret_key, *public_key, *secret_key_only;
int secret_key_length, secret_key_only_length;
secret_key_only = Tcl_GetByteArrayFromObj(secret_key_only_obj, &secret_key_only_length);
if (secret_key_only_length != NANO_SECRET_KEY_LENGTH) {
return(NULL);
}
if ((NANO_SECRET_KEY_LENGTH + NANO_PUBLIC_KEY_LENGTH) != crypto_sign_SECRETKEYBYTES) {
return(NULL);
}
secret_key_length = crypto_sign_SECRETKEYBYTES;
secret_key = TclNano_AttemptAlloc(secret_key_length);
if (!secret_key) {
return(NULL);
}
memcpy(secret_key, secret_key_only, secret_key_only_length);
public_key = secret_key + secret_key_only_length;
crypto_sign_keypair(public_key, secret_key, 0);
*out_key_length = secret_key_length;
return(secret_key);
}
static int nano_tcl_generate_keypair(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char secret_key[crypto_sign_SECRETKEYBYTES], public_key[crypto_sign_PUBLICKEYBYTES];
unsigned char *seed, *buffer, buffer_s[NANO_SECRET_KEY_LENGTH + 4];
long seed_index;
int seed_length, buffer_length;
int csk_ret, tglfo_ret;
if (objc != 1 && objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "?seed index?");
return(TCL_ERROR);
}
if (objc == 1) {
csk_ret = crypto_sign_keypair(public_key, secret_key, 1);
if (csk_ret != 0) {
Tcl_SetResult(interp, "Internal error", NULL);
return(TCL_ERROR);
}
} else {
seed = Tcl_GetByteArrayFromObj(objv[1], &seed_length);
if (seed_length != NANO_SECRET_KEY_LENGTH) {
Tcl_SetResult(interp, "Seed is not the right size", NULL);
return(TCL_ERROR);
}
|
|
|
|
|
<
<
<
<
<
<
<
<
|
<
|
|
|
|
<
<
|
<
<
|
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
|
#define TclNano_PkgProvide(interp, name, version) \
tclcmd_ret = Tcl_PkgProvide(interp, name, version); \
if (tclcmd_ret != TCL_OK) { \
return(tclcmd_ret); \
}
static unsigned char *nano_parse_secret_key(Tcl_Obj *secret_key_only_obj, unsigned char *public_key, int public_key_length) {
unsigned char *secret_key_only;
int secret_key_only_length;
secret_key_only = Tcl_GetByteArrayFromObj(secret_key_only_obj, &secret_key_only_length);
if (secret_key_only_length != NANO_SECRET_KEY_LENGTH) {
return(NULL);
}
if (public_key_length != NANO_PUBLIC_KEY_LENGTH) {
return(NULL);
}
crypto_sign_public_key(public_key, secret_key_only);
return(public_key);
}
static int nano_tcl_generate_keypair(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char secret_key[NANO_SECRET_KEY_LENGTH], public_key[NANO_PUBLIC_KEY_LENGTH];
unsigned char *seed, *buffer, buffer_s[NANO_SECRET_KEY_LENGTH + 4];
long seed_index;
int seed_length, buffer_length;
int tglfo_ret;
if (objc != 1 && objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "?seed index?");
return(TCL_ERROR);
}
if (objc == 1) {
randombytes(secret_key, NANO_SECRET_KEY_LENGTH);
crypto_sign_public_key(public_key, secret_key);
} else {
seed = Tcl_GetByteArrayFromObj(objv[1], &seed_length);
if (seed_length != NANO_SECRET_KEY_LENGTH) {
Tcl_SetResult(interp, "Seed is not the right size", NULL);
return(TCL_ERROR);
}
|
︙ | | | ︙ | |
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
buffer += seed_length;
buffer[0] = (seed_index >> 24) & 0xff;
buffer[1] = (seed_index >> 16) & 0xff;
buffer[2] = (seed_index >> 8) & 0xff;
buffer[3] = seed_index & 0xff;
buffer -= seed_length;
blake2b(secret_key, NANO_SECRET_KEY_LENGTH, buffer, buffer_length, NULL, 0);
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(secret_key, NANO_SECRET_KEY_LENGTH));
return(TCL_OK);
/* NOTREACH */
|
|
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
buffer += seed_length;
buffer[0] = (seed_index >> 24) & 0xff;
buffer[1] = (seed_index >> 16) & 0xff;
buffer[2] = (seed_index >> 8) & 0xff;
buffer[3] = seed_index & 0xff;
buffer -= seed_length;
crypto_blake2b_general(secret_key, NANO_SECRET_KEY_LENGTH, NULL, 0, buffer, buffer_length);
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(secret_key, NANO_SECRET_KEY_LENGTH));
return(TCL_OK);
/* NOTREACH */
|
︙ | | | ︙ | |
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
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_secret_key_to_public_key(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char *secret_key, *public_key;
int secret_key_length, public_key_length;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "secretKey");
return(TCL_ERROR);
}
secret_key = Tcl_GetByteArrayFromObj(objv[1], &secret_key_length);
if (secret_key_length != NANO_SECRET_KEY_LENGTH) {
Tcl_SetResult(interp, "Secret key is not the right size", NULL);
return(TCL_ERROR);
}
public_key_length = NANO_PUBLIC_KEY_LENGTH;
public_key = TclNano_AttemptAlloc(public_key_length);
if (!public_key) {
Tcl_SetResult(interp, "Internal error", NULL);
return(TCL_ERROR);
}
crypto_sign_keypair(public_key, secret_key, 0);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(public_key, public_key_length));
TclNano_Free(public_key);
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_sign_detached(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
int cs_ret;
unsigned char *signature, *data, *secret_key;
unsigned long long signature_length;
int data_length, secret_key_length;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "data secretKey");
return(TCL_ERROR);
}
data = Tcl_GetByteArrayFromObj(objv[1], &data_length);
signature_length = data_length + NANO_BLOCK_SIGNATURE_LENGTH;
if (signature_length >= UINT_MAX) {
Tcl_SetResult(interp, "Input message too long", NULL);
return(TCL_ERROR);
}
secret_key = nano_parse_secret_key(objv[2], &secret_key_length);
if (!secret_key) {
Tcl_SetResult(interp, "Secret key is not the right size", NULL);
return(TCL_ERROR);
}
signature = TclNano_AttemptAlloc(signature_length);
if (!signature) {
TclNano_Free(secret_key);
Tcl_SetResult(interp, "Unable to allocate memory", NULL);
return(TCL_ERROR);
}
cs_ret = crypto_sign(signature, &signature_length, data, data_length, secret_key);
if (cs_ret != 0) {
TclNano_Free(secret_key);
TclNano_Free(signature);
Tcl_SetResult(interp, "crypto_sign failed", NULL);
return(TCL_ERROR);
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(signature, NANO_BLOCK_SIGNATURE_LENGTH));
TclNano_Free(signature);
TclNano_Free(secret_key);
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_verify_detached(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
int cso_ret;
unsigned char *signature, *data, *signed_data, *verify_data, *public_key;
int signature_length, data_length, signed_data_length, verify_data_length, public_key_length;
unsigned long long verify_data_length_nacl;
int result;
if (objc != 4) {
Tcl_WrongNumArgs(interp, 1, objv, "data signature publicKey");
return(TCL_ERROR);
}
|
|
>
|
|
<
<
<
<
<
|
<
<
<
|
<
<
<
<
<
<
|
|
|
|
<
<
<
<
<
<
|
|
|
|
<
|
|
<
<
<
<
|
<
<
<
<
<
<
<
|
|
|
<
|
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
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_secret_key_to_public_key(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
Tcl_Obj *secret_key;
unsigned char *public_key, public_key_buffer[NANO_PUBLIC_KEY_LENGTH];
int public_key_length;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "secretKey");
return(TCL_ERROR);
}
secret_key = objv[1];
public_key_length = sizeof(public_key_buffer);
public_key = nano_parse_secret_key(secret_key, public_key_buffer, public_key_length);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(public_key, public_key_length));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_sign_detached(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char signature[NANO_BLOCK_SIGNATURE_LENGTH];
unsigned char public_key_buffer[NANO_PUBLIC_KEY_LENGTH];
unsigned char *data, *secret_key, *public_key;
int data_length, public_key_length, secret_key_length;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "data secretKey");
return(TCL_ERROR);
}
data = Tcl_GetByteArrayFromObj(objv[1], &data_length);
secret_key = Tcl_GetByteArrayFromObj(objv[2], &secret_key_length);
if (secret_key_length != NANO_SECRET_KEY_LENGTH) {
Tcl_SetResult(interp, "Secret key is not the right size", NULL);
return(TCL_ERROR);
}
public_key_length = sizeof(public_key_buffer);
public_key = nano_parse_secret_key(objv[2], public_key_buffer, public_key_length);
if (!public_key) {
Tcl_SetResult(interp, "Error converting secret key to public key", NULL);
return(TCL_ERROR);
}
crypto_sign(signature, secret_key, public_key, data, data_length);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(signature, NANO_BLOCK_SIGNATURE_LENGTH));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_verify_detached(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
int cc_ret;
unsigned char *signature, *data, *public_key;
int signature_length, data_length, public_key_length;
int result;
if (objc != 4) {
Tcl_WrongNumArgs(interp, 1, objv, "data signature publicKey");
return(TCL_ERROR);
}
|
︙ | | | ︙ | |
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
public_key = Tcl_GetByteArrayFromObj(objv[3], &public_key_length);
if (public_key_length != NANO_PUBLIC_KEY_LENGTH) {
Tcl_SetResult(interp, "Public key is not the right size", NULL);
return(TCL_ERROR);
}
signed_data_length = data_length + signature_length;
signed_data = TclNano_AttemptAlloc(signed_data_length);
if (!signed_data) {
Tcl_SetResult(interp, "Internal error", NULL);
return(TCL_ERROR);
}
memcpy(signed_data, signature, signature_length);
memcpy(signed_data + signature_length, data, data_length);
verify_data_length = signed_data_length;
verify_data = TclNano_AttemptAlloc(verify_data_length);
if (!verify_data) {
TclNano_Free(verify_data);
Tcl_SetResult(interp, "Internal error", NULL);
return(TCL_ERROR);
}
verify_data_length_nacl = verify_data_length;
cso_ret = crypto_sign_open(verify_data, &verify_data_length_nacl, signed_data, signed_data_length, public_key);
result = 0;
if (cso_ret == 0) {
result = 1;
}
TclNano_Free(signed_data);
TclNano_Free(verify_data);
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(result));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_hash_data(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
>
|
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
public_key = Tcl_GetByteArrayFromObj(objv[3], &public_key_length);
if (public_key_length != NANO_PUBLIC_KEY_LENGTH) {
Tcl_SetResult(interp, "Public key is not the right size", NULL);
return(TCL_ERROR);
}
cc_ret = crypto_check(signature, public_key, data, data_length);
result = 0;
if (!cc_ret) {
result = 1;
}
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(result));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_derive_key_from_password(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
void *password, *salt;
int password_length, salt_length;
unsigned char result[32];
int hash_ret;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "password salt");
return(TCL_ERROR);
}
password = Tcl_GetByteArrayFromObj(objv[1], &password_length);
salt = Tcl_GetByteArrayFromObj(objv[2], &salt_length);
hash_ret = argon2_hash(NANO_KDF_ARGON2_TIMING, NANO_KDF_ARGON2_MEMORY, NANO_KDF_ARGON2_THREADS,
password, password_length,
salt, salt_length,
result, sizeof(result),
NULL, 0, Argon2_d, 0x10);
if (hash_ret != ARGON2_OK) {
Tcl_SetResult(interp, (char *) argon2_error_message(hash_ret), NULL);
return(TCL_ERROR);
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, sizeof(result)));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_aes256_ctr(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
struct AES_ctx aes_handle;
void *key, *iv, *data;
int key_length, iv_length, data_length;
unsigned char result[128];
if (objc != 4) {
Tcl_WrongNumArgs(interp, 1, objv, "key iv data");
return(TCL_ERROR);
}
key = Tcl_GetByteArrayFromObj(objv[1], &key_length);
iv = Tcl_GetByteArrayFromObj(objv[2], &iv_length);
data = Tcl_GetByteArrayFromObj(objv[3], &data_length);
if (key_length != AES_KEYLEN) {
Tcl_SetResult(interp, "Key is not the right size", NULL);
return(TCL_ERROR);
}
if (iv_length != AES_BLOCKLEN) {
Tcl_SetResult(interp, "IV is not the right size", NULL);
return(TCL_ERROR);
}
if (data_length > sizeof(result)) {
Tcl_SetResult(interp, "Data exceeds maximum size", NULL);
return(TCL_ERROR);
}
memcpy(result, data, data_length);
AES_init_ctx_iv(&aes_handle, key, iv);
AES_CTR_xcrypt_buffer(&aes_handle, result, data_length);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, AES_KEYLEN));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_hash_data(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
|
︙ | | | ︙ | |
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
if (result_length > sizeof(result)) {
Tcl_SetResult(interp, "Hash length too large", NULL);
return(TCL_ERROR);
}
blake2b(result, result_length, data, data_length, NULL, 0);
} else {
/*
* Default to the same as the cryptographic primitive
*/
crypto_hash(result, data, data_length);
result_length = NANO_BLOCK_SIGNATURE_LENGTH;
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, result_length));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_validate_work(const unsigned char *blockhash, const unsigned char *work, uint64_t workMin) {
unsigned char workReversed[NANO_WORK_VALUE_LENGTH], workCheck[NANO_WORK_HASH_LENGTH];
unsigned int idxIn, idxOut;
blake2b_state workhash_state;
uint64_t workValue;
int blake2_ret;
idxIn = sizeof(workReversed) - 1;
idxOut = 0;
while (idxOut < sizeof(workReversed)) {
workReversed[idxOut] = work[idxIn];
idxOut++;
idxIn--;
}
blake2_ret = blake2b_init(&workhash_state, sizeof(workCheck));
if (blake2_ret != 0) {
return(0);
}
blake2_ret = blake2b_update(&workhash_state, workReversed, sizeof(workReversed));
if (blake2_ret != 0) {
return(0);
}
blake2_ret = blake2b_update(&workhash_state, blockhash, NANO_BLOCK_HASH_LENGTH);
if (blake2_ret != 0) {
return(0);
}
blake2_ret = blake2b_final(&workhash_state, workCheck, sizeof(workCheck));
if (blake2_ret != 0) {
return(0);
}
workValue = 0;
for (idxIn = sizeof(workCheck); idxIn > 0; idxIn--) {
workValue <<= 8;
workValue |= workCheck[idxIn - 1];
}
|
|
|
|
<
|
<
<
<
<
|
<
<
<
<
|
<
<
<
<
|
<
<
<
|
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
if (result_length > sizeof(result)) {
Tcl_SetResult(interp, "Hash length too large", NULL);
return(TCL_ERROR);
}
crypto_blake2b_general(result, result_length, NULL, 0, data, data_length);
} else {
/*
* Default to the same as the cryptographic primitive
*/
crypto_blake2b(result, data, data_length);
result_length = NANO_BLOCK_SIGNATURE_LENGTH;
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, result_length));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_validate_work(const unsigned char *blockhash, const unsigned char *work, uint64_t workMin) {
unsigned char workReversed[NANO_WORK_VALUE_LENGTH], workCheck[NANO_WORK_HASH_LENGTH];
unsigned int idxIn, idxOut;
crypto_blake2b_ctx workhash_state;
uint64_t workValue;
idxIn = sizeof(workReversed) - 1;
idxOut = 0;
while (idxOut < sizeof(workReversed)) {
workReversed[idxOut] = work[idxIn];
idxOut++;
idxIn--;
}
crypto_blake2b_general_init(&workhash_state, sizeof(workCheck), NULL, 0);
crypto_blake2b_update(&workhash_state, workReversed, sizeof(workReversed));
crypto_blake2b_update(&workhash_state, blockhash, NANO_BLOCK_HASH_LENGTH);
crypto_blake2b_final(&workhash_state, workCheck);
workValue = 0;
for (idxIn = sizeof(workCheck); idxIn > 0; idxIn--) {
workValue <<= 8;
workValue |= workCheck[idxIn - 1];
}
|
︙ | | | ︙ | |
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
static void nano_generate_work(const unsigned char *blockhash, unsigned char *workOut, uint64_t workMin) {
unsigned char work[NANO_WORK_VALUE_LENGTH];
unsigned int offset;
int work_valid;
memcpy(work, blockhash, sizeof(work));
#pragma omp target map(tofrom:work)
while (1) {
work_valid = nano_validate_work(blockhash, work, workMin);
if (work_valid) {
break;
}
offset = 0;
|
|
|
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
static void nano_generate_work(const unsigned char *blockhash, unsigned char *workOut, uint64_t workMin) {
unsigned char work[NANO_WORK_VALUE_LENGTH];
unsigned int offset;
int work_valid;
memcpy(work, blockhash, sizeof(work));
/* XXX:TODO: INCOMPLETE OpenMP support #pragma omp target map(tofrom:work) */
while (1) {
work_valid = nano_validate_work(blockhash, work, workMin);
if (work_valid) {
break;
}
offset = 0;
|
︙ | | | ︙ | |
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_random_bytes(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char *buffer;
int number_of_bytes;
int tgifo_ret;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "numberOfBytes");
return(TCL_ERROR);
}
tgifo_ret = Tcl_GetIntFromObj(interp, objv[1], &number_of_bytes);
if (tgifo_ret != TCL_OK) {
return(tgifo_ret);
}
if (number_of_bytes > 128) {
Tcl_SetResult(interp, "May only request 128 bytes of random data at once", NULL);
return(TCL_ERROR);
}
buffer = TclNano_AttemptAlloc(number_of_bytes);
if (!buffer) {
Tcl_SetResult(interp, "memory allocation failure", NULL);
return(TCL_ERROR);
}
randombytes(buffer, number_of_bytes);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(buffer, number_of_bytes));
TclNano_Free(buffer);
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_self_test(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
|
|
<
<
<
<
<
<
<
<
<
|
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_random_bytes(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char buffer[128];
int number_of_bytes;
int tgifo_ret;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "numberOfBytes");
return(TCL_ERROR);
}
tgifo_ret = Tcl_GetIntFromObj(interp, objv[1], &number_of_bytes);
if (tgifo_ret != TCL_OK) {
return(tgifo_ret);
}
if (number_of_bytes > 128) {
Tcl_SetResult(interp, "May only request 128 bytes of random data at once", NULL);
return(TCL_ERROR);
}
randombytes(buffer, number_of_bytes);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(buffer, number_of_bytes));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_self_test(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
|
︙ | | | ︙ | |
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
TclNano_CreateObjCommand(interp, "::nano::internal::selfTest", nano_tcl_self_test);
TclNano_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair);
TclNano_CreateObjCommand(interp, "::nano::internal::generateSeed", nano_tcl_generate_seed);
TclNano_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key);
TclNano_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached);
TclNano_CreateObjCommand(interp, "::nano::internal::verifyDetached", nano_tcl_verify_detached);
TclNano_CreateObjCommand(interp, "::nano::internal::hashData", nano_tcl_hash_data);
TclNano_CreateObjCommand(interp, "::nano::internal::validateWork", nano_tcl_validate_work);
TclNano_CreateObjCommand(interp, "::nano::internal::generateWork", nano_tcl_generate_work);
TclNano_CreateObjCommand(interp, "::nano::internal::randomBytes", nano_tcl_random_bytes);
TclNano_Eval(interp, nanoInitScript);
TclNano_PkgProvide(interp, "nano", PACKAGE_VERSION);
return(TCL_OK);
}
|
>
>
|
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
TclNano_CreateObjCommand(interp, "::nano::internal::selfTest", nano_tcl_self_test);
TclNano_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair);
TclNano_CreateObjCommand(interp, "::nano::internal::generateSeed", nano_tcl_generate_seed);
TclNano_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key);
TclNano_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached);
TclNano_CreateObjCommand(interp, "::nano::internal::verifyDetached", nano_tcl_verify_detached);
TclNano_CreateObjCommand(interp, "::nano::internal::hashData", nano_tcl_hash_data);
TclNano_CreateObjCommand(interp, "::nano::internal::deriveKeyFromPassword", nano_tcl_derive_key_from_password);
TclNano_CreateObjCommand(interp, "::nano::internal::AES256-CTR", nano_tcl_aes256_ctr);
TclNano_CreateObjCommand(interp, "::nano::internal::validateWork", nano_tcl_validate_work);
TclNano_CreateObjCommand(interp, "::nano::internal::generateWork", nano_tcl_generate_work);
TclNano_CreateObjCommand(interp, "::nano::internal::randomBytes", nano_tcl_random_bytes);
TclNano_Eval(interp, nanoInitScript);
TclNano_PkgProvide(interp, "nano", PACKAGE_VERSION);
return(TCL_OK);
}
|