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 int main(int argc
, char *argv
[])
30 private_key_t
*private;
32 struct timespec timing
;
33 int round
, rounds
, read
;
34 char buf
[8096], *pos
= buf
;
35 key_type_t type
= KEY_ANY
;
36 signature_scheme_t scheme
= SIGN_UNKNOWN
;
37 chunk_t keydata
, *sigs
, data
;
44 rounds
= atoi(argv
[3]);
46 if (streq(argv
[2], "rsa"))
49 scheme
= SIGN_RSA_EMSA_PKCS1_SHA1
;
51 else if (streq(argv
[2], "ecdsa"))
61 lib
->plugins
->load(lib
->plugins
, NULL
, argv
[1]);
62 atexit(library_deinit
);
64 keydata
= chunk_create(buf
, 0);
65 while ((read
= fread(pos
, 1, sizeof(buf
) - (pos
- buf
), stdin
)))
71 private = lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, type
,
72 BUILD_BLOB_PEM
, keydata
, BUILD_END
);
75 printf("parsing private key failed.\n");
78 if (type
== KEY_ECDSA
)
80 switch (private->get_keysize(private))
83 scheme
= SIGN_ECDSA_256
;
86 scheme
= SIGN_ECDSA_384
;
89 scheme
= SIGN_ECDSA_521
;
92 printf("%d bit ECDSA private key size not supported",
93 private->get_keysize(private));
98 printf("%4d bit %N: ", private->get_keysize(private),
99 key_type_names
, type
);
101 sigs
= malloc(sizeof(chunk_t
) * rounds
);
103 data
= chunk_from_chars(0x01,0x02,0x03,0x04,0x05,0x06,0x07);
104 start_timing(&timing
);
105 for (round
= 0; round
< rounds
; round
++)
107 if (!private->sign(private, scheme
, data
, &sigs
[round
]))
109 printf("creating signature failed\n");
113 printf("sign()/s: %8.1f ", rounds
/ end_timing(&timing
));
115 public = private->get_public_key(private);
118 printf("extracting public key failed\n");
121 start_timing(&timing
);
122 for (round
= 0; round
< rounds
; round
++)
124 if (!public->verify(public, scheme
, data
, sigs
[round
]))
126 printf("signature verification failed\n");
130 printf("verify()/s: %8.1f\n", rounds
/ end_timing(&timing
));
131 public->destroy(public);
132 private->destroy(private);
134 for (round
= 0; round
< rounds
; round
++)
136 free(sigs
[round
].ptr
);