8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#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_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)
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) {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
#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); \
}
#define TclNano_CreateNamespace(interp, name) \
tclobj_ret = Tcl_CreateNamespace(interp, name, NULL, NULL); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
}
#define TclNano_CreateObjCommand(interp, name, functionName) \
tclobj_ret = Tcl_CreateObjCommand(interp, name, functionName, NULL, NULL); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
}
#define TclNano_Eval(interp, script) \
tclcmd_ret = Tcl_Eval(interp, script); \
if (tclcmd_ret != TCL_OK) { \
return(tclcmd_ret); \
}
#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) {
|
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "data secretKey");
return(TCL_ERROR);
}
data = Tcl_GetByteArrayFromObj(objv[1], &data_length);
signature_length = data_length + crypto_sign_BYTES;
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);
|
|
|
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
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);
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
TclNano_Free(signature);
Tcl_SetResult(interp, "crypto_sign failed", NULL);
return(TCL_ERROR);
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(signature, crypto_sign_BYTES));
TclNano_Free(signature);
TclNano_Free(secret_key);
return(TCL_OK);
/* NOTREACH */
|
|
|
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
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 */
|
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
Tcl_WrongNumArgs(interp, 1, objv, "data signature publicKey");
return(TCL_ERROR);
}
data = Tcl_GetByteArrayFromObj(objv[1], &data_length);
signature = Tcl_GetByteArrayFromObj(objv[2], &signature_length);
if (signature_length != crypto_sign_BYTES) {
Tcl_SetResult(interp, "Signature is not the right size", NULL);
return(TCL_ERROR);
}
public_key = Tcl_GetByteArrayFromObj(objv[3], &public_key_length);
if (public_key_length != NANO_PUBLIC_KEY_LENGTH) {
|
|
|
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
Tcl_WrongNumArgs(interp, 1, objv, "data signature publicKey");
return(TCL_ERROR);
}
data = Tcl_GetByteArrayFromObj(objv[1], &data_length);
signature = Tcl_GetByteArrayFromObj(objv[2], &signature_length);
if (signature_length != NANO_BLOCK_SIGNATURE_LENGTH) {
Tcl_SetResult(interp, "Signature is not the right size", NULL);
return(TCL_ERROR);
}
public_key = Tcl_GetByteArrayFromObj(objv[3], &public_key_length);
if (public_key_length != NANO_PUBLIC_KEY_LENGTH) {
|
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_hash_data(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char *data, result[crypto_sign_BYTES];
int tgifo_ret;
int data_length, result_length;
if (objc < 2 || objc > 3) {
Tcl_WrongNumArgs(interp, 1, objv, "data ?hashLength?");
return(TCL_ERROR);
|
|
|
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_hash_data(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char *data, result[NANO_BLOCK_SIGNATURE_LENGTH];
int tgifo_ret;
int data_length, result_length;
if (objc < 2 || objc > 3) {
Tcl_WrongNumArgs(interp, 1, objv, "data ?hashLength?");
return(TCL_ERROR);
|
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
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 = crypto_sign_BYTES;
}
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, result_length));
return(TCL_OK);
/* NOTREACH */
|
|
|
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
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 */
|
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
int Nano_Init(Tcl_Interp *interp) {
int te_ret, tpp_ret;
const char nanoInitScript[] = {
#include "nano.tcl.h"
0x00
};
#ifdef USE_TCL_STUBS
const char *tclInitStubs_ret;
/* Initialize Stubs */
tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
if (!tclInitStubs_ret) {
return(TCL_ERROR);
}
#endif
Tcl_CreateObjCommand(interp, "::nano::internal::selfTest", nano_tcl_self_test, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::generateSeed", nano_tcl_generate_seed, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::verifyDetached", nano_tcl_verify_detached, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::hashData", nano_tcl_hash_data, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::validateWork", nano_tcl_validate_work, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::generateWork", nano_tcl_generate_work, NULL, NULL);
if (interp) {
te_ret = Tcl_Eval(interp, nanoInitScript);
if (te_ret != TCL_OK) {
return(te_ret);
}
}
tpp_ret = Tcl_PkgProvide(interp, "nano", PACKAGE_VERSION);
if (tpp_ret != TCL_OK) {
return(tpp_ret);
}
return(TCL_OK);
}
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
<
|
<
<
|
<
<
|
<
<
|
<
|
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
578
579
580
581
582
583
584
585
586
587
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
int Nano_Init(Tcl_Interp *interp) {
void *tclobj_ret;
int tclcmd_ret;
const char nanoInitScript[] = {
#include "nano.tcl.h"
0x00
};
#ifdef USE_TCL_STUBS
const char *tclInitStubs_ret;
/* Initialize Stubs */
tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
if (!tclInitStubs_ret) {
return(TCL_ERROR);
}
#endif
if (!interp) {
return(TCL_OK);
}
TclNano_CreateNamespace(interp, "::nano");
TclNano_CreateNamespace(interp, "::nano::block");
TclNano_CreateNamespace(interp, "::nano::key");
TclNano_CreateNamespace(interp, "::nano::work");
TclNano_SetIntVar(interp, "::nano::block::hashLength", NANO_BLOCK_HASH_LENGTH);
TclNano_SetIntVar(interp, "::nano::block::signatureLength", NANO_BLOCK_SIGNATURE_LENGTH);
TclNano_SetIntVar(interp, "::nano::key::publicKeyLength", NANO_PUBLIC_KEY_LENGTH);
TclNano_SetIntVar(interp, "::nano::key::privateKeyLength", NANO_SECRET_KEY_LENGTH);
TclNano_SetIntVar(interp, "::nano::key::seedLength", NANO_SECRET_KEY_LENGTH);
TclNano_SetIntVar(interp, "::nano::work::workValueLength", NANO_WORK_VALUE_LENGTH);
TclNano_SetIntVar(interp, "::nano::work::workHashLength", NANO_WORK_HASH_LENGTH);
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_Eval(interp, nanoInitScript);
TclNano_PkgProvide(interp, "nano", PACKAGE_VERSION);
return(TCL_OK);
}
|