6 #include <credentials/keys/private_key.h>
8 void start_timing(struct timespec
*start
)
10 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, start
);
13 double end_timing(struct timespec
*start
)
17 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, &end
);
18 return (end
.tv_nsec
- start
->tv_nsec
) / 1000000000.0 +
19 (end
.tv_sec
- start
->tv_sec
) * 1.0;
24 printf("usage: pubkey_speed plugins rsa|ecdsa rounds\n");
28 static char data_buf
[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07};
30 int main(int argc
, char *argv
[])
32 private_key_t
*private;
34 struct timespec timing
;
35 int round
, rounds
, read
;
36 char buf
[8096], *pos
= buf
;
37 key_type_t type
= KEY_ANY
;
38 signature_scheme_t scheme
= SIGN_UNKNOWN
;
39 chunk_t keydata
, *sigs
, data
= chunk_from_buf(data_buf
);
46 rounds
= atoi(argv
[3]);
48 if (streq(argv
[2], "rsa"))
51 scheme
= SIGN_RSA_EMSA_PKCS1_SHA1
;
53 else if (streq(argv
[2], "ecdsa"))
62 library_init(STRONGSWAN_CONF
);
63 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
, argv
[1]);
64 atexit(library_deinit
);
66 keydata
= chunk_create(buf
, 0);
67 while ((read
= fread(pos
, 1, sizeof(buf
) - (pos
- buf
), stdin
)))
73 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
74 BUILD_BLOB_PEM
, keydata
, BUILD_END
);
77 printf("parsing private key failed.\n");
80 if (type
== KEY_ECDSA
)
82 switch (private->get_keysize(private))
85 scheme
= SIGN_ECDSA_256
;
88 scheme
= SIGN_ECDSA_384
;
91 scheme
= SIGN_ECDSA_521
;
94 printf("%d bit ECDSA private key size not supported",
95 private->get_keysize(private) * 8);
100 printf("%4d bit %N: ", private->get_keysize(private)*8,
101 key_type_names
, type
);
103 sigs
= malloc(sizeof(chunk_t
) * rounds
);
105 start_timing(&timing
);
106 for (round
= 0; round
< rounds
; round
++)
108 if (!private->sign(private, scheme
, data
, &sigs
[round
]))
110 printf("creating signature failed\n");
114 printf("sign()/s: %8.1f ", rounds
/ end_timing(&timing
));
116 public = private->get_public_key(private);
119 printf("extracting public key failed\n");
122 start_timing(&timing
);
123 for (round
= 0; round
< rounds
; round
++)
125 if (!public->verify(public, scheme
, data
, sigs
[round
]))
127 printf("signature verification failed\n");
131 printf("verify()/s: %8.1f\n", rounds
/ end_timing(&timing
));
132 public->destroy(public);
133 private->destroy(private);
135 for (round
= 0; round
< rounds
; round
++)
137 free(sigs
[round
].ptr
);