Overview
Comment: | A bit of restructuring and cleanup |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
a1a2a058e2d26a7b7e7cb27f65138a44 |
User & Date: | rkeene on 2018-07-02 06:10:20 |
Other Links: | manifest | tags |
Context
2018-07-02
| ||
06:10 | Check for more headers, and add more debugging flags check-in: bdce793196 user: rkeene tags: trunk | |
06:10 | A bit of restructuring and cleanup check-in: a1a2a058e2 user: rkeene tags: trunk | |
06:09 | If we are not generating the private key, do not write to it at all check-in: 1bb3e03251 user: rkeene tags: trunk | |
Changes
Modified nano.c from [7bf0e59047] to [19478c9f83].
|
| < > > > > > > | > | > < | < < | 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 | #include <stdint.h> #include <limits.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <tcl.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) #define TclNano_AttemptAlloc(x) ((void *) Tcl_Alloc(x)) #define TclNano_Free(x) Tcl_Free((char *) x) #if defined(HAVE_GETRANDOM) # ifdef HAVE_SYS_RANDOM_H # include <sys/random.h> # endif void randombytes(uint8_t *buffer, uint64_t length) { ssize_t gr_ret; while (length > 0) { gr_ret = getrandom(buffer, length, 0); if (gr_ret <= 0) { continue; } buffer += gr_ret; length -= gr_ret; } return; } #elif defined(HAVE_GETENTROPY) void randombytes(uint8_t *buffer, uint64_t length) { int ge_ret; int current_length; while (length > 0) { current_length = length; if (current_length > 256) { |
︙ | ︙ | |||
47 48 49 50 51 52 53 | buffer += current_length; length -= current_length; } return; } | > > > > > > > > > | > > > > > > | < < < > > > > | > | > > | | 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 | buffer += current_length; length -= current_length; } return; } #else # ifdef HAVE_SYS_TYPES_H # include <sys/types.h> # endif # ifdef HAVE_SYS_STAT_H # include <sys/stat.h> # endif # ifdef HAVE_FCNTL_H # include <fcntl.h> # endif void randombytes(uint8_t *buffer, uint64_t length) { ssize_t read_ret; int fd = -1; while (fd < 0) { fd = open("/dev/urandom", O_RDONLY); } while (length > 0) { read_ret = read(fd, buffer, length); if (read_ret <= 0) { continue; } buffer += read_ret; length -= read_ret; } close(fd); return; } #endif 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); |
︙ | ︙ | |||
130 131 132 133 134 135 136 | 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; | | | | 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 | 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; } |
︙ | ︙ | |||
170 171 172 173 174 175 176 | 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); | | | | | | | | | | | | 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 | 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, crypto_sign_BYTES)); 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); } |
︙ | ︙ | |||
235 236 237 238 239 240 241 | 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; | | > > > > > > > > > > | > | | > | 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 | 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; printf("cso sdl = %i, dl=%i, sl=%i\n", signed_data_length, data_length, signature_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; |
︙ | ︙ | |||
274 275 276 277 278 279 280 | Tcl_WrongNumArgs(interp, 1, objv, "data"); return(TCL_ERROR); } data = Tcl_GetByteArrayFromObj(objv[1], &data_length); crypto_hash(result, data, data_length); | | > > > > > > > > > > > > > > > > > > > > > > | 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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | Tcl_WrongNumArgs(interp, 1, objv, "data"); return(TCL_ERROR); } data = Tcl_GetByteArrayFromObj(objv[1], &data_length); crypto_hash(result, data, data_length); result_length = crypto_sign_BYTES; Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, result_length)); return(TCL_OK); /* NOTREACH */ clientData = clientData; } static int nano_tcl_self_test(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { if (objc != 1) { Tcl_WrongNumArgs(interp, 1, objv, ""); return(TCL_ERROR); } return(TCL_OK); /* NOTREACH */ clientData = clientData; } int Nano_Init(Tcl_Interp *interp) { 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::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); if (interp) { Tcl_Eval(interp, nanoInitScript); } return(TCL_OK); } |