]>
Commit | Line | Data |
---|---|---|
f1d5d876 TB |
1 | /* |
2 | * Copyright (C) 2009 Martin Willi | |
3 | * 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 | */ | |
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 | |
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; | |
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 | ||
36 | static void usage() | |
37 | { | |
f1d5d876 | 38 | printf("usage: pubkey_speed plugins rsa|ecdsa rounds < key\n"); |
a4caeac7 MW |
39 | exit(1); |
40 | } | |
41 | ||
a4caeac7 MW |
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; | |
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]); |
7daf5226 | 59 | |
a4caeac7 MW |
60 | if (streq(argv[2], "rsa")) |
61 | { | |
62 | type = KEY_RSA; | |
63 | scheme = SIGN_RSA_EMSA_PKCS1_SHA1; | |
64 | } | |
65 | else if (streq(argv[2], "ecdsa")) | |
66 | { | |
67 | type = KEY_ECDSA; | |
68 | } | |
69 | else | |
70 | { | |
71 | usage(); | |
72 | } | |
7daf5226 | 73 | |
34d3bfcf | 74 | library_init(NULL, "pubkey_speed"); |
b18a5317 | 75 | lib->plugins->load(lib->plugins, argv[1]); |
a4caeac7 | 76 | atexit(library_deinit); |
7daf5226 | 77 | |
a4caeac7 MW |
78 | keydata = chunk_create(buf, 0); |
79 | while ((read = fread(pos, 1, sizeof(buf) - (pos - buf), stdin))) | |
80 | { | |
81 | pos += read; | |
82 | keydata.len += read; | |
83 | } | |
7daf5226 | 84 | |
a4caeac7 | 85 | private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type, |
2b7e085d | 86 | BUILD_BLOB_PEM, keydata, BUILD_END); |
a4caeac7 MW |
87 | if (!private) |
88 | { | |
89 | printf("parsing private key failed.\n"); | |
90 | exit(1); | |
91 | } | |
92 | if (type == KEY_ECDSA) | |
93 | { | |
94 | switch (private->get_keysize(private)) | |
95 | { | |
a944d209 | 96 | case 256: |
7daf5226 | 97 | scheme = SIGN_ECDSA_256; |
a4caeac7 | 98 | break; |
a944d209 | 99 | case 384: |
a4caeac7 MW |
100 | scheme = SIGN_ECDSA_384; |
101 | break; | |
a944d209 | 102 | case 521: |
a4caeac7 MW |
103 | scheme = SIGN_ECDSA_521; |
104 | break; | |
105 | default: | |
106 | printf("%d bit ECDSA private key size not supported", | |
a944d209 | 107 | private->get_keysize(private)); |
a4caeac7 MW |
108 | exit(1); |
109 | } | |
110 | } | |
7daf5226 | 111 | |
a944d209 | 112 | printf("%4d bit %N: ", private->get_keysize(private), |
a4caeac7 | 113 | key_type_names, type); |
7daf5226 | 114 | |
a4caeac7 | 115 | sigs = malloc(sizeof(chunk_t) * rounds); |
7daf5226 | 116 | |
3b878dae | 117 | data = chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07); |
a4caeac7 MW |
118 | start_timing(&timing); |
119 | for (round = 0; round < rounds; round++) | |
120 | { | |
121 | if (!private->sign(private, scheme, data, &sigs[round])) | |
122 | { | |
123 | printf("creating signature failed\n"); | |
124 | exit(1); | |
125 | } | |
126 | }; | |
127 | printf("sign()/s: %8.1f ", rounds / end_timing(&timing)); | |
7daf5226 | 128 | |
a4caeac7 MW |
129 | public = private->get_public_key(private); |
130 | if (!public) | |
131 | { | |
132 | printf("extracting public key failed\n"); | |
133 | exit(1); | |
134 | } | |
135 | start_timing(&timing); | |
136 | for (round = 0; round < rounds; round++) | |
137 | { | |
138 | if (!public->verify(public, scheme, data, sigs[round])) | |
139 | { | |
140 | printf("signature verification failed\n"); | |
141 | exit(1); | |
142 | } | |
143 | } | |
144 | printf("verify()/s: %8.1f\n", rounds / end_timing(&timing)); | |
145 | public->destroy(public); | |
146 | private->destroy(private); | |
7daf5226 | 147 | |
a4caeac7 MW |
148 | for (round = 0; round < rounds; round++) |
149 | { | |
150 | free(sigs[round].ptr); | |
151 | } | |
152 | free(sigs); | |
153 | return 0; | |
154 | } |