2013-09-04 07:08:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Martin Willi
|
2018-05-23 14:04:50 +00:00
|
|
|
* HSR Hochschule fuer Technik Rapperswil
|
2013-09-04 07:08:46 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
2009-06-10 14:10:46 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <library.h>
|
2012-10-16 14:03:21 +00:00
|
|
|
#include <utils/debug.h>
|
2009-06-10 14:10:46 +00:00
|
|
|
#include <credentials/keys/private_key.h>
|
|
|
|
|
|
|
|
void start_timing(struct timespec *start)
|
|
|
|
{
|
|
|
|
clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
double end_timing(struct timespec *start)
|
|
|
|
{
|
|
|
|
struct timespec end;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
|
|
|
|
return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
|
|
|
|
(end.tv_sec - start->tv_sec) * 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage()
|
|
|
|
{
|
2013-09-04 07:08:46 +00:00
|
|
|
printf("usage: pubkey_speed plugins rsa|ecdsa rounds < key\n");
|
2009-06-10 14:10:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
private_key_t *private;
|
|
|
|
public_key_t *public;
|
|
|
|
struct timespec timing;
|
|
|
|
int round, rounds, read;
|
|
|
|
char buf[8096], *pos = buf;
|
2009-06-19 08:05:27 +00:00
|
|
|
key_type_t type = KEY_ANY;
|
|
|
|
signature_scheme_t scheme = SIGN_UNKNOWN;
|
2009-09-11 13:35:10 +00:00
|
|
|
chunk_t keydata, *sigs, data;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
if (argc < 4)
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
rounds = atoi(argv[3]);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
if (streq(argv[2], "rsa"))
|
|
|
|
{
|
|
|
|
type = KEY_RSA;
|
|
|
|
scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
|
|
|
|
}
|
|
|
|
else if (streq(argv[2], "ecdsa"))
|
|
|
|
{
|
|
|
|
type = KEY_ECDSA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2014-01-22 10:50:39 +00:00
|
|
|
library_init(NULL, "pubkey_speed");
|
2013-06-28 08:44:15 +00:00
|
|
|
lib->plugins->load(lib->plugins, argv[1]);
|
2009-06-10 14:10:46 +00:00
|
|
|
atexit(library_deinit);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
keydata = chunk_create(buf, 0);
|
|
|
|
while ((read = fread(pos, 1, sizeof(buf) - (pos - buf), stdin)))
|
|
|
|
{
|
|
|
|
pos += read;
|
|
|
|
keydata.len += read;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
|
2009-08-13 13:39:29 +00:00
|
|
|
BUILD_BLOB_PEM, keydata, BUILD_END);
|
2009-06-10 14:10:46 +00:00
|
|
|
if (!private)
|
|
|
|
{
|
|
|
|
printf("parsing private key failed.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (type == KEY_ECDSA)
|
|
|
|
{
|
|
|
|
switch (private->get_keysize(private))
|
|
|
|
{
|
2010-08-10 13:56:10 +00:00
|
|
|
case 256:
|
2009-09-04 11:46:09 +00:00
|
|
|
scheme = SIGN_ECDSA_256;
|
2009-06-10 14:10:46 +00:00
|
|
|
break;
|
2010-08-10 13:56:10 +00:00
|
|
|
case 384:
|
2009-06-10 14:10:46 +00:00
|
|
|
scheme = SIGN_ECDSA_384;
|
|
|
|
break;
|
2010-08-10 13:56:10 +00:00
|
|
|
case 521:
|
2009-06-10 14:10:46 +00:00
|
|
|
scheme = SIGN_ECDSA_521;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("%d bit ECDSA private key size not supported",
|
2010-08-10 13:56:10 +00:00
|
|
|
private->get_keysize(private));
|
2009-06-10 14:10:46 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-08-10 13:56:10 +00:00
|
|
|
printf("%4d bit %N: ", private->get_keysize(private),
|
2009-06-10 14:10:46 +00:00
|
|
|
key_type_names, type);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
sigs = malloc(sizeof(chunk_t) * rounds);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-09-11 13:35:10 +00:00
|
|
|
data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07);
|
2009-06-10 14:10:46 +00:00
|
|
|
start_timing(&timing);
|
|
|
|
for (round = 0; round < rounds; round++)
|
|
|
|
{
|
2017-09-19 15:26:58 +00:00
|
|
|
if (!private->sign(private, scheme, NULL, data, &sigs[round]))
|
2009-06-10 14:10:46 +00:00
|
|
|
{
|
|
|
|
printf("creating signature failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
printf("sign()/s: %8.1f ", rounds / end_timing(&timing));
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
public = private->get_public_key(private);
|
|
|
|
if (!public)
|
|
|
|
{
|
|
|
|
printf("extracting public key failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
start_timing(&timing);
|
|
|
|
for (round = 0; round < rounds; round++)
|
|
|
|
{
|
2017-09-19 15:15:18 +00:00
|
|
|
if (!public->verify(public, scheme, NULL, data, sigs[round]))
|
2009-06-10 14:10:46 +00:00
|
|
|
{
|
|
|
|
printf("signature verification failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("verify()/s: %8.1f\n", rounds / end_timing(&timing));
|
|
|
|
public->destroy(public);
|
|
|
|
private->destroy(private);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2009-06-10 14:10:46 +00:00
|
|
|
for (round = 0; round < rounds; round++)
|
|
|
|
{
|
|
|
|
free(sigs[round].ptr);
|
|
|
|
}
|
|
|
|
free(sigs);
|
|
|
|
return 0;
|
|
|
|
}
|