ed25519_key *crypto_ed25519_new() { ed25519_key *key = malloc(sizeof(ed25519_key)); int r = wc_ed25519_init(key); if (r) { free(key); return NULL; } return key; }
bool uc_ecc_create_key(uc_ed25519_key *key) { if (!uc_init()) return false; wc_ed25519_init(key); if (wc_ed25519_make_key(&uc_random, ED25519_KEY_SIZE, key)) return false; // UCDUMP("ECCPRV", key->k, ED25519_PRV_KEY_SIZE); UCDUMP("ECCPUB", key->p, ED25519_PUB_KEY_SIZE); return true; }
bool uc_import_ecc_pub_key(uc_ed25519_key *key, const unsigned char *in, size_t inlen) { if(inlen != ED25519_PUB_KEY_SIZE) return false; wc_ed25519_init(key); const int status = wc_ed25519_import_public(in, inlen, key); if(status < 0) { UCERROR("import ecc pub", status); return false; } UCDUMP("ECCPUB", key->p, ED25519_PUB_KEY_SIZE); return true; }
bool uc_import_ecc_key(uc_ed25519_key *key, const unsigned char *in, size_t inlen) { if (inlen != ED25519_PRV_KEY_SIZE) return false; wc_ed25519_init(key); const int status = wc_ed25519_import_private_key(in + 32, ED25519_KEY_SIZE, in, ED25519_PUB_KEY_SIZE, key); if (status < 0) { UCERROR("import ecc key", status); return false; } // UCDUMP("ECCPRV", key->k, ED25519_PRV_KEY_SIZE); UCDUMP("ECCPUB", key->p, ED25519_PUB_KEY_SIZE); return true; }
void bench_ed25519KeyGen(void) { ed25519_key genKey; double start, total, milliEach; int i; /* 256 bit */ start = current_time(1); for (i = 0; i < genTimes; i++) { wc_ed25519_init(&genKey); wc_ed25519_make_key(&rng, 32, &genKey); wc_ed25519_free(&genKey); } total = current_time(0) - start; milliEach = total / genTimes / 1000; printf("\r\n"); printf("ED25519 key generation %6.3f milliseconds, avg over %d" " iterations\r\n", milliEach, genTimes); }
void bench_ed25519KeySign(void) { int ret; ed25519_key genKey; #ifdef HAVE_ED25519_SIGN double start, total, milliEach; int i; byte sig[ED25519_SIG_SIZE]; byte msg[512]; word32 x = 0; #endif wc_ed25519_init(&genKey); ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &genKey); if (ret != 0) { printf("ed25519_make_key failed\r\n"); return; } #ifdef HAVE_ED25519_SIGN /* make dummy msg */ for (i = 0; i < (int) sizeof(msg); i++) msg[i] = (byte) i; start = current_time(1); for (i = 0; i < agreeTimes; i++) { x = sizeof(sig); ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey); if (ret != 0) { printf("ed25519_sign_msg failed\r\n"); return; } } total = current_time(0) - start; milliEach = total / agreeTimes / 1000; /* per second */ printf("ED25519 sign time %6.3f milliseconds, avg over %d" " iterations\r\n", milliEach, agreeTimes); #ifdef HAVE_ED25519_VERIFY start = current_time(1); for (i = 0; i < agreeTimes; i++) { int verify = 0; ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify, &genKey); if (ret != 0 || verify != 1) { printf("ed25519_verify_msg failed\r\n"); return; } } total = current_time(0) - start; milliEach = total / agreeTimes / 1000; printf("ED25519 verify time %6.3f milliseconds, avg over %d" " iterations\r\n", milliEach, agreeTimes); #endif /* HAVE_ED25519_VERIFY */ #endif /* HAVE_ED25519_SIGN */ wc_ed25519_free(&genKey); }