6 #include <credentials/keys/private_key.h>
9 void start_timing(struct timespec
*start
)
11 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, start
);
14 double end_timing(struct timespec
*start
)
18 clock_gettime(CLOCK_THREAD_CPUTIME_ID
, &end
);
19 return (end
.tv_nsec
- start
->tv_nsec
) / 1000000000.0 +
20 (end
.tv_sec
- start
->tv_sec
) * 1.0;
25 printf("usage: pubkey_speed plugins rsa|ecdsa rounds\n");
29 static char data_buf
[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07};
31 int main(int argc
, char *argv
[])
33 private_key_t
*private;
35 struct timespec timing
;
36 int round
, rounds
, read
;
37 char buf
[8096], *pos
= buf
;
39 signature_scheme_t scheme
;
40 chunk_t keydata
, *sigs
, data
= chunk_from_buf(data_buf
);
47 rounds
= atoi(argv
[3]);
49 if (streq(argv
[2], "rsa"))
52 scheme
= SIGN_RSA_EMSA_PKCS1_SHA1
;
54 else if (streq(argv
[2], "ecdsa"))
63 library_init(STRONGSWAN_CONF
);
64 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
, argv
[1]);
65 atexit(library_deinit
);
67 keydata
= chunk_create(buf
, 0);
68 while ((read
= fread(pos
, 1, sizeof(buf
) - (pos
- buf
), stdin
)))
73 if (pem_to_bin(&keydata
, chunk_empty
, NULL
) != SUCCESS
)
75 printf("converting PEM private key failed.\n");
79 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
80 BUILD_BLOB_ASN1_DER
, keydata
, BUILD_END
);
83 printf("parsing private key failed.\n");
86 if (type
== KEY_ECDSA
)
88 switch (private->get_keysize(private))
91 scheme
= SIGN_ECDSA_256
;
94 scheme
= SIGN_ECDSA_384
;
97 scheme
= SIGN_ECDSA_521
;
100 printf("%d bit ECDSA private key size not supported",
101 private->get_keysize(private) * 8);
106 printf("%4d bit %N: ", private->get_keysize(private)*8,
107 key_type_names
, type
);
109 sigs
= malloc(sizeof(chunk_t
) * rounds
);
111 start_timing(&timing
);
112 for (round
= 0; round
< rounds
; round
++)
114 if (!private->sign(private, scheme
, data
, &sigs
[round
]))
116 printf("creating signature failed\n");
120 printf("sign()/s: %8.1f ", rounds
/ end_timing(&timing
));
122 public = private->get_public_key(private);
125 printf("extracting public key failed\n");
128 start_timing(&timing
);
129 for (round
= 0; round
< rounds
; round
++)
131 if (!public->verify(public, scheme
, data
, sigs
[round
]))
133 printf("signature verification failed\n");
137 printf("verify()/s: %8.1f\n", rounds
/ end_timing(&timing
));
138 public->destroy(public);
139 private->destroy(private);
141 for (round
= 0; round
< rounds
; round
++)
143 free(sigs
[round
].ptr
);