C4Err ECC_Export_ANSI_X963(ECC_ContextRef ctx, void *outData, size_t bufSize, size_t *datSize) { C4Err err = kC4Err_NoErr; unsigned long length = bufSize; validateECCContext(ctx); ValidateParam(ctx->isInited); err = ecc_ansi_x963_export(&ctx->key, outData, &length); CKERR; *datSize = length; done: return (err); }
/** Generates and returns a new (privkey, pubkey) ECDH key pair. * Keys are represented as Lua strings, the private one under a libtomcrypt * proprietary format, the public one under X9.63 format. */ static int lnew( lua_State *L) { prng_state prng; int prng_initialized = 0; ecc_key key; int idx = find_prng("fortuna"); if( -1 == idx) goto failure; if( CRYPT_OK != rng_make_prng( ENTROPY, idx, & prng, NULL)) goto failure; prng_initialized=1; /* Generate the 512 bits ECC key in privkey. */ if( CRYPT_OK != ecc_make_key( & prng, idx, 64, & key)) goto failure; /* Buffer will hold both the private and public key transiently, * until each of them is transformed into a Lua string. */ unsigned char buff [BUFF_SIZE]; unsigned long buff_len = BUFF_SIZE; /* Push the string representation of privkey (tomcrypt's proprietary format). */ if( CRYPT_OK != ecc_export( buff, & buff_len, PK_PRIVATE, & key)) goto failure; lua_pushlstring( L, (const char*) buff, buff_len); /* Push the string representation of pubkey (ANSI X9.63 format, which only * supports public keys. This is the format expected by the server). */ if( CRYPT_OK != ecc_ansi_x963_export( & key, buff, & buff_len)) goto failure; lua_pushlstring( L, (const char *) buff, buff_len); fortuna_done( & prng); return 2; failure: /* TODO: release resources */ if( prng_initialized) fortuna_done( & prng); lua_pushnil( L); lua_pushstring( L, "error"); return 2; }
int ecc_tests (void) { unsigned char buf[4][4096]; unsigned long x, y, z, s; int stat, stat2; ecc_key usera, userb, pubKey, privKey; DO(ecc_test ()); DO(ecc_test ()); DO(ecc_test ()); DO(ecc_test ()); DO(ecc_test ()); for (s = 0; s < (sizeof(sizes)/sizeof(sizes[0])); s++) { /* make up two keys */ DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera)); DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &userb)); /* make the shared secret */ x = sizeof(buf[0]); DO(ecc_shared_secret (&usera, &userb, buf[0], &x)); y = sizeof(buf[1]); DO(ecc_shared_secret (&userb, &usera, buf[1], &y)); if (y != x) { fprintf(stderr, "ecc Shared keys are not same size."); return 1; } if (memcmp (buf[0], buf[1], x)) { fprintf(stderr, "ecc Shared keys not same contents."); return 1; } /* now export userb */ y = sizeof(buf[0]); DO(ecc_export (buf[1], &y, PK_PUBLIC, &userb)); ecc_free (&userb); /* import and make the shared secret again */ DO(ecc_import (buf[1], y, &userb)); z = sizeof(buf[0]); DO(ecc_shared_secret (&usera, &userb, buf[2], &z)); if (z != x) { fprintf(stderr, "failed. Size don't match?"); return 1; } if (memcmp (buf[0], buf[2], x)) { fprintf(stderr, "Failed. Contents didn't match."); return 1; } /* export with ANSI X9.63 */ y = sizeof(buf[1]); DO(ecc_ansi_x963_export(&userb, buf[1], &y)); ecc_free (&userb); /* now import the ANSI key */ DO(ecc_ansi_x963_import(buf[1], y, &userb)); /* shared secret */ z = sizeof(buf[0]); DO(ecc_shared_secret (&usera, &userb, buf[2], &z)); if (z != x) { fprintf(stderr, "failed. Size don't match?"); return 1; } if (memcmp (buf[0], buf[2], x)) { fprintf(stderr, "Failed. Contents didn't match."); return 1; } ecc_free (&usera); ecc_free (&userb); /* test encrypt_key */ DO(ecc_make_key (&yarrow_prng, find_prng ("yarrow"), sizes[s], &usera)); /* export key */ x = sizeof(buf[0]); DO(ecc_export(buf[0], &x, PK_PUBLIC, &usera)); DO(ecc_import(buf[0], x, &pubKey)); x = sizeof(buf[0]); DO(ecc_export(buf[0], &x, PK_PRIVATE, &usera)); DO(ecc_import(buf[0], x, &privKey)); for (x = 0; x < 32; x++) { buf[0][x] = x; } y = sizeof (buf[1]); DO(ecc_encrypt_key (buf[0], 32, buf[1], &y, &yarrow_prng, find_prng ("yarrow"), find_hash ("sha256"), &pubKey)); zeromem (buf[0], sizeof (buf[0])); x = sizeof (buf[0]); DO(ecc_decrypt_key (buf[1], y, buf[0], &x, &privKey)); if (x != 32) { fprintf(stderr, "Failed (length)"); return 1; } for (x = 0; x < 32; x++) { if (buf[0][x] != x) { fprintf(stderr, "Failed (contents)"); return 1; } } /* test sign_hash */ for (x = 0; x < 16; x++) { buf[0][x] = x; } x = sizeof (buf[1]); DO(ecc_sign_hash (buf[0], 16, buf[1], &x, &yarrow_prng, find_prng ("yarrow"), &privKey)); DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat, &pubKey)); buf[0][0] ^= 1; DO(ecc_verify_hash (buf[1], x, buf[0], 16, &stat2, &privKey)); if (!(stat == 1 && stat2 == 0)) { fprintf(stderr, "ecc_verify_hash failed %d, %d, ", stat, stat2); return 1; } ecc_free (&usera); ecc_free (&pubKey); ecc_free (&privKey); } #ifdef LTC_ECC_SHAMIR return ecc_test_shamir(); #else return 0; #endif }