2013-01-11 18:54:40 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2013-01-19 18:08:14 -05:00
|
|
|
|
2014-05-16 11:16:26 -04:00
|
|
|
/* #define ED25519_DLL */
|
2013-01-22 04:58:57 -05:00
|
|
|
#include "src/ed25519.h"
|
2013-01-21 16:28:34 -05:00
|
|
|
|
2013-02-04 11:56:19 -05:00
|
|
|
#include "src/ge.h"
|
|
|
|
#include "src/sc.h"
|
|
|
|
|
2013-01-11 18:54:40 -05:00
|
|
|
|
2014-05-16 11:16:26 -04:00
|
|
|
int main() {
|
2013-02-04 11:56:19 -05:00
|
|
|
unsigned char public_key[32], private_key[64], seed[32], scalar[32];
|
2013-04-11 14:20:20 -04:00
|
|
|
unsigned char other_public_key[32], other_private_key[64];
|
|
|
|
unsigned char shared_secret[32], other_shared_secret[32];
|
2013-01-22 04:23:29 -05:00
|
|
|
unsigned char signature[64];
|
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
clock_t start;
|
|
|
|
clock_t end;
|
|
|
|
int i;
|
2013-01-19 18:08:14 -05:00
|
|
|
|
2014-05-16 11:16:26 -04:00
|
|
|
const unsigned char message[] = "Hello, world!";
|
|
|
|
const int message_len = strlen((char*) message);
|
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
/* create a random seed, and a keypair out of that seed */
|
2013-01-11 18:54:40 -05:00
|
|
|
ed25519_create_seed(seed);
|
2013-02-04 07:34:01 -05:00
|
|
|
ed25519_create_keypair(public_key, private_key, seed);
|
2013-01-11 20:38:34 -05:00
|
|
|
|
2013-02-04 07:34:01 -05:00
|
|
|
/* create signature on the message with the keypair */
|
2014-05-16 11:16:26 -04:00
|
|
|
ed25519_sign(signature, message, message_len, public_key, private_key);
|
2013-01-11 20:38:34 -05:00
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
/* verify the signature */
|
2014-05-16 11:16:26 -04:00
|
|
|
if (ed25519_verify(signature, message, message_len, public_key)) {
|
2013-01-22 04:23:29 -05:00
|
|
|
printf("valid signature\n");
|
2013-02-04 07:34:01 -05:00
|
|
|
} else {
|
|
|
|
printf("invalid signature\n");
|
2013-01-11 20:38:34 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 11:56:19 -05:00
|
|
|
/* create scalar and add it to the keypair */
|
|
|
|
ed25519_create_seed(scalar);
|
|
|
|
ed25519_add_scalar(public_key, private_key, scalar);
|
|
|
|
|
|
|
|
/* create signature with the new keypair */
|
2014-05-16 11:16:26 -04:00
|
|
|
ed25519_sign(signature, message, message_len, public_key, private_key);
|
2013-02-04 11:56:19 -05:00
|
|
|
|
|
|
|
/* verify the signature with the new keypair */
|
2014-05-16 11:16:26 -04:00
|
|
|
if (ed25519_verify(signature, message, message_len, public_key)) {
|
2013-02-04 11:56:19 -05:00
|
|
|
printf("valid signature\n");
|
|
|
|
} else {
|
|
|
|
printf("invalid signature\n");
|
|
|
|
}
|
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
/* make a slight adjustment and verify again */
|
|
|
|
signature[44] ^= 0x10;
|
2014-05-16 11:16:26 -04:00
|
|
|
if (ed25519_verify(signature, message, message_len, public_key)) {
|
2013-02-04 07:34:01 -05:00
|
|
|
printf("did not detect signature change\n");
|
2013-01-11 20:38:34 -05:00
|
|
|
} else {
|
2013-02-04 07:34:01 -05:00
|
|
|
printf("correctly detected signature change\n");
|
2013-01-11 20:38:34 -05:00
|
|
|
}
|
2013-01-11 18:54:40 -05:00
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
/* generate two keypairs for testing key exchange */
|
|
|
|
ed25519_create_seed(seed);
|
|
|
|
ed25519_create_keypair(public_key, private_key, seed);
|
|
|
|
ed25519_create_seed(seed);
|
|
|
|
ed25519_create_keypair(other_public_key, other_private_key, seed);
|
2013-03-24 17:37:43 -04:00
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
/* create two shared secrets - from both perspectives - and check if they're equal */
|
|
|
|
ed25519_key_exchange(shared_secret, other_public_key, private_key);
|
|
|
|
ed25519_key_exchange(other_shared_secret, public_key, other_private_key);
|
2013-03-24 17:37:43 -04:00
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
for (i = 0; i < 32; ++i) {
|
|
|
|
if (shared_secret[i] != other_shared_secret[i]) {
|
|
|
|
printf("key exchange was incorrect\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-24 17:37:43 -04:00
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
if (i == 32) {
|
|
|
|
printf("key exchange was correct\n");
|
|
|
|
}
|
2013-03-24 17:37:43 -04:00
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
/* test performance */
|
2013-04-11 14:20:20 -04:00
|
|
|
printf("testing seed generation performance: ");
|
2013-04-11 12:53:20 -04:00
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
2013-04-11 14:20:20 -04:00
|
|
|
ed25519_create_seed(seed);
|
2013-04-11 12:53:20 -04:00
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
|
|
|
printf("%fus per seed\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
|
|
|
|
|
|
|
|
2013-02-04 07:42:59 -05:00
|
|
|
printf("testing key generation performance: ");
|
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
|
|
|
ed25519_create_keypair(public_key, private_key, seed);
|
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
2013-04-11 12:53:20 -04:00
|
|
|
printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
2013-02-04 07:42:59 -05:00
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
printf("testing sign performance: ");
|
2013-01-19 18:08:14 -05:00
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
2014-05-16 11:16:26 -04:00
|
|
|
ed25519_sign(signature, message, message_len, public_key, private_key);
|
2013-01-19 18:08:14 -05:00
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
2013-01-19 18:08:14 -05:00
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
printf("testing verify performance: ");
|
2013-01-19 18:08:14 -05:00
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
2014-05-16 11:16:26 -04:00
|
|
|
ed25519_verify(signature, message, message_len, public_key);
|
2013-01-19 18:08:14 -05:00
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
2013-01-22 04:23:29 -05:00
|
|
|
printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
2013-02-04 11:56:19 -05:00
|
|
|
|
|
|
|
|
2013-04-11 12:53:20 -04:00
|
|
|
printf("testing keypair scalar addition performance: ");
|
2013-02-04 11:56:19 -05:00
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
|
|
|
ed25519_add_scalar(public_key, private_key, scalar);
|
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
2013-03-27 13:12:58 -04:00
|
|
|
printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
2013-03-24 17:37:43 -04:00
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
printf("testing public key scalar addition performance: ");
|
2013-04-11 12:53:20 -04:00
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
|
|
|
ed25519_add_scalar(public_key, NULL, scalar);
|
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
|
|
|
printf("%fus per key\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
|
|
|
|
2013-04-11 14:20:20 -04:00
|
|
|
printf("testing key exchange performance: ");
|
2013-03-24 17:37:43 -04:00
|
|
|
start = clock();
|
|
|
|
for (i = 0; i < 10000; ++i) {
|
|
|
|
ed25519_key_exchange(shared_secret, other_public_key, private_key);
|
|
|
|
}
|
|
|
|
end = clock();
|
|
|
|
|
2013-03-27 13:12:58 -04:00
|
|
|
printf("%fus per shared secret\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
|
2013-01-19 18:08:14 -05:00
|
|
|
|
2013-01-11 18:54:40 -05:00
|
|
|
return 0;
|
2013-02-04 07:34:01 -05:00
|
|
|
}
|