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
|
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
|
-
-
-
+
+
+
-
+
-
-
-
|
#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 *public_key, *secret_key_only;
int public_key_length, secret_key_only_length;
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);
}
public_key_length = NANO_PUBLIC_KEY_LENGTH;
if (public_key_length != NANO_PUBLIC_KEY_LENGTH) {
public_key = TclNano_AttemptAlloc(public_key_length);
if (!public_key) {
return(NULL);
}
crypto_sign_public_key(public_key, secret_key_only);
*out_key_length = public_key_length;
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;
|
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
|
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
|
-
-
+
+
+
-
+
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
+
+
-
+
-
-
+
-
+
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
|
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;
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 = Tcl_GetByteArrayFromObj(objv[1], &secret_key_length);
secret_key = objv[1];
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_length = sizeof(public_key_buffer);
public_key = TclNano_AttemptAlloc(public_key_length);
if (!public_key) {
Tcl_SetResult(interp, "Internal error", NULL);
public_key = nano_parse_secret_key(secret_key, public_key_buffer, public_key_length);
return(TCL_ERROR);
}
crypto_sign_public_key(public_key, secret_key);
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[]) {
unsigned char signature[NANO_BLOCK_SIGNATURE_LENGTH];
unsigned char public_key_buffer[NANO_PUBLIC_KEY_LENGTH];
unsigned char *signature, *data, *secret_key, *public_key;
unsigned char *data, *secret_key, *public_key;
unsigned long long signature_length;
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);
signature_length = NANO_BLOCK_SIGNATURE_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_length);
public_key = nano_parse_secret_key(objv[2], public_key_buffer, public_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(public_key);
if (!public_key) {
Tcl_SetResult(interp, "Unable to allocate memory", NULL);
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));
TclNano_Free(signature);
TclNano_Free(public_key);
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_verify_detached(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
|
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
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;
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);
}
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[]) {
|