]> git.ipfire.org Git - thirdparty/strongswan.git/blame - scripts/pubkey_speed.c
script: Fix upper bounds
[thirdparty/strongswan.git] / scripts / pubkey_speed.c
CommitLineData
f1d5d876
TB
1/*
2 * Copyright (C) 2009 Martin Willi
1b671669 3 * HSR Hochschule fuer Technik Rapperswil
f1d5d876
TB
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
a4caeac7
MW
15
16#include <stdio.h>
17#include <time.h>
18#include <library.h>
f05b4272 19#include <utils/debug.h>
a4caeac7 20#include <credentials/keys/private_key.h>
a4caeac7
MW
21
22void start_timing(struct timespec *start)
23{
24 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
25}
26
27double end_timing(struct timespec *start)
28{
29 struct timespec end;
7daf5226 30
a4caeac7
MW
31 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
32 return (end.tv_nsec - start->tv_nsec) / 1000000000.0 +
33 (end.tv_sec - start->tv_sec) * 1.0;
34}
35
36static void usage()
37{
f1d5d876 38 printf("usage: pubkey_speed plugins rsa|ecdsa rounds < key\n");
a4caeac7
MW
39 exit(1);
40}
41
a4caeac7
MW
42int main(int argc, char *argv[])
43{
44 private_key_t *private;
45 public_key_t *public;
46 struct timespec timing;
47 int round, rounds, read;
48 char buf[8096], *pos = buf;
aab814c7
MW
49 key_type_t type = KEY_ANY;
50 signature_scheme_t scheme = SIGN_UNKNOWN;
3b878dae 51 chunk_t keydata, *sigs, data;
7daf5226 52
a4caeac7
MW
53 if (argc < 4)
54 {
55 usage();
56 }
7daf5226 57
a4caeac7 58 rounds = atoi(argv[3]);
03993149 59 if (rounds < 0 || rounds > (1 << 26))
e1cc6679
TB
60 { /* arbitrary limit to the number of chunk_t/sigs that fit into 1 GiB */
61 usage();
62 }
7daf5226 63
a4caeac7
MW
64 if (streq(argv[2], "rsa"))
65 {
66 type = KEY_RSA;
67 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
68 }
69 else if (streq(argv[2], "ecdsa"))
70 {
71 type = KEY_ECDSA;
72 }
73 else
74 {
75 usage();
76 }
7daf5226 77
34d3bfcf 78 library_init(NULL, "pubkey_speed");
b18a5317 79 lib->plugins->load(lib->plugins, argv[1]);
a4caeac7 80 atexit(library_deinit);
7daf5226 81
a4caeac7
MW
82 keydata = chunk_create(buf, 0);
83 while ((read = fread(pos, 1, sizeof(buf) - (pos - buf), stdin)))
84 {
85 pos += read;
86 keydata.len += read;
87 }
7daf5226 88
a4caeac7 89 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
2b7e085d 90 BUILD_BLOB_PEM, keydata, BUILD_END);
a4caeac7
MW
91 if (!private)
92 {
93 printf("parsing private key failed.\n");
94 exit(1);
95 }
96 if (type == KEY_ECDSA)
97 {
98 switch (private->get_keysize(private))
99 {
a944d209 100 case 256:
7daf5226 101 scheme = SIGN_ECDSA_256;
a4caeac7 102 break;
a944d209 103 case 384:
a4caeac7
MW
104 scheme = SIGN_ECDSA_384;
105 break;
a944d209 106 case 521:
a4caeac7
MW
107 scheme = SIGN_ECDSA_521;
108 break;
109 default:
110 printf("%d bit ECDSA private key size not supported",
a944d209 111 private->get_keysize(private));
a4caeac7
MW
112 exit(1);
113 }
114 }
7daf5226 115
a944d209 116 printf("%4d bit %N: ", private->get_keysize(private),
a4caeac7 117 key_type_names, type);
7daf5226 118
a4caeac7 119 sigs = malloc(sizeof(chunk_t) * rounds);
7daf5226 120
3b878dae 121 data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07);
a4caeac7
MW
122 start_timing(&timing);
123 for (round = 0; round < rounds; round++)
124 {
de280c2e 125 if (!private->sign(private, scheme, NULL, data, &sigs[round]))
a4caeac7
MW
126 {
127 printf("creating signature failed\n");
128 exit(1);
129 }
130 };
131 printf("sign()/s: %8.1f ", rounds / end_timing(&timing));
7daf5226 132
a4caeac7
MW
133 public = private->get_public_key(private);
134 if (!public)
135 {
136 printf("extracting public key failed\n");
137 exit(1);
138 }
139 start_timing(&timing);
140 for (round = 0; round < rounds; round++)
141 {
a413571f 142 if (!public->verify(public, scheme, NULL, data, sigs[round]))
a4caeac7
MW
143 {
144 printf("signature verification failed\n");
145 exit(1);
146 }
147 }
148 printf("verify()/s: %8.1f\n", rounds / end_timing(&timing));
149 public->destroy(public);
150 private->destroy(private);
7daf5226 151
a4caeac7
MW
152 for (round = 0; round < rounds; round++)
153 {
154 free(sigs[round].ptr);
155 }
156 free(sigs);
157 return 0;
158}