Check-in [28166744a1]
Overview
Comment:Start of support for key generation
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 28166744a158150e8e24cbf1a0da0de5d2ee2e57eac6d940d029af94b46e6c09
User & Date: rkeene on 2018-07-01 22:12:46
Other Links: manifest | tags
Context
2018-07-02
01:01
Only export the symbols we actually want to export check-in: a05b5da951 user: rkeene tags: trunk
2018-07-01
22:12
Start of support for key generation check-in: 28166744a1 user: rkeene tags: trunk
22:04
Added support for hashing arbitrary data check-in: 54a51a61b6 user: rkeene tags: trunk
Changes

Modified build/test/test.tcl from [aa86c2ee6b] to [e7f0ffae87].

64
65
66
67
68
69
70





71
72
73
74

75
76
77
78
79
80
81
		puts "\[FAIL\] Exp: $hash_expected"

		return false
	}

	return true
}






set tests {
	signatures
	hashing

}

foreach test $tests {
	if {![test_$test]} {
		puts "FAILED test $test"
		exit 1
	}







>
>
>
>
>




>







64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		puts "\[FAIL\] Exp: $hash_expected"

		return false
	}

	return true
}

proc test_keygeneration {} {
	set key [::nano::internal::generateKey]
	puts "key=$key"
}

set tests {
	signatures
	hashing
	keygeneration
}

foreach test $tests {
	if {![test_$test]} {
		puts "FAILED test $test"
		exit 1
	}

Modified nano.c from [df3e692dc7] to [7bf0e59047].

1
2
3
4
5
6
7



8
9
10
11
12
13
14
#include <tcl.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include "tweetnacl.h"
#include "blake2-supercop.h"




#if 0
#include <sys/random.h>

void randombytes(uint8_t *buffer, uint64_t length) {
	ssize_t gr_ret;

	while (length > 0) {







>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <tcl.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include "tweetnacl.h"
#include "blake2-supercop.h"

#define NANO_SECRET_KEY_LENGTH (crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES)
#define NANO_PUBLIC_KEY_LENGTH (crypto_sign_PUBLICKEYBYTES)

#if 0
#include <sys/random.h>

void randombytes(uint8_t *buffer, uint64_t length) {
	ssize_t gr_ret;

	while (length > 0) {
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
		buffer[length - 1] = (length % 256);
		length--;
	}

	return;
}

#define NANO_SECRET_KEY_LENGTH (crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES)
#define NANO_PUBLIC_KEY_LENGTH (crypto_sign_PUBLICKEYBYTES)

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);







<
<
<







61
62
63
64
65
66
67



68
69
70
71
72
73
74
		buffer[length - 1] = (length % 256);
		length--;
	}

	return;
}




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);
88
89
90
91
92
93
94
























95
96
97
98
99
100
101
	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_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");








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
	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[]) {
	int csk_ret;
	unsigned char secret_key[crypto_sign_SECRETKEYBYTES], public_key[crypto_sign_PUBLICKEYBYTES];

	if (objc != 1) {
		Tcl_WrongNumArgs(interp, 1, objv, "");

		return(TCL_ERROR);
	}

	csk_ret = crypto_sign_keypair(public_key, secret_key, 1);
	if (csk_ret != 0) {
		Tcl_SetResult(interp, "Internal error", NULL);

		return(TCL_ERROR);
	}

	Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(secret_key, NANO_SECRET_KEY_LENGTH));

	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");

270
271
272
273
274
275
276

277
278
279
280
281
282
283
	/* Initialize Stubs */
	tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
	if (!tclInitStubs_ret) {
		return(TCL_ERROR);
	}
#endif


	Tcl_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key, 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);

	return(TCL_OK);
}







>







294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	/* Initialize Stubs */
	tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
	if (!tclInitStubs_ret) {
		return(TCL_ERROR);
	}
#endif

	Tcl_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached, NULL, NULL);
	Tcl_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key, 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);

	return(TCL_OK);
}