21e4d2c1ef8d6f8c51cb6bc9f9270d42fa42b044
[strongswan.git] / scripts / pubkey_speed.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * HSR Hochschule fuer Technik Rapperswil
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 */
15
16 #include <stdio.h>
17 #include <time.h>
18 #include <library.h>
19 #include <utils/debug.h>
20 #include <credentials/keys/private_key.h>
21
22 void start_timing(struct timespec *start)
23 {
24 clock_gettime(CLOCK_THREAD_CPUTIME_ID, start);
25 }
26
27 double end_timing(struct timespec *start)
28 {
29 struct timespec end;
30
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
36 static void usage()
37 {
38 printf("usage: pubkey_speed plugins rsa|ecdsa rounds < key\n");
39 exit(1);
40 }
41
42 int 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;
49 key_type_t type = KEY_ANY;
50 signature_scheme_t scheme = SIGN_UNKNOWN;
51 chunk_t keydata, *sigs, data;
52
53 if (argc < 4)
54 {
55 usage();
56 }
57
58 rounds = atoi(argv[3]);
59 if (rounds < 0 || rounds > (2^26))
60 { /* arbitrary limit to the number of chunk_t/sigs that fit into 1 GiB */
61 usage();
62 }
63
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 }
77
78 library_init(NULL, "pubkey_speed");
79 lib->plugins->load(lib->plugins, argv[1]);
80 atexit(library_deinit);
81
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 }
88
89 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
90 BUILD_BLOB_PEM, keydata, BUILD_END);
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 {
100 case 256:
101 scheme = SIGN_ECDSA_256;
102 break;
103 case 384:
104 scheme = SIGN_ECDSA_384;
105 break;
106 case 521:
107 scheme = SIGN_ECDSA_521;
108 break;
109 default:
110 printf("%d bit ECDSA private key size not supported",
111 private->get_keysize(private));
112 exit(1);
113 }
114 }
115
116 printf("%4d bit %N: ", private->get_keysize(private),
117 key_type_names, type);
118
119 sigs = malloc(sizeof(chunk_t) * rounds);
120
121 data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07);
122 start_timing(&timing);
123 for (round = 0; round < rounds; round++)
124 {
125 if (!private->sign(private, scheme, NULL, data, &sigs[round]))
126 {
127 printf("creating signature failed\n");
128 exit(1);
129 }
130 };
131 printf("sign()/s: %8.1f ", rounds / end_timing(&timing));
132
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 {
142 if (!public->verify(public, scheme, NULL, data, sigs[round]))
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);
151
152 for (round = 0; round < rounds; round++)
153 {
154 free(sigs[round].ptr);
155 }
156 free(sigs);
157 return 0;
158 }