1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include "cryptolib.h"
11 #include "file_keys.h"
12 #include "host_common.h"
13 #include "timer_utils.h"
14 
15 #define FILE_NAME_SIZE 128
16 #define NUM_OPERATIONS 100 /* Number of signature operations to time. */
17 
SpeedTestAlgorithm(int algorithm)18 int SpeedTestAlgorithm(int algorithm) {
19   int i, key_size;
20   int error_code = 0;
21   double speed, msecs;
22   char file_name[FILE_NAME_SIZE];
23   uint8_t* digest = NULL;
24   uint8_t* signature = NULL;
25   uint64_t digest_len, sig_len;
26   RSAPublicKey* key = NULL;
27   ClockTimerState ct;
28   char* sha_strings[] = {  /* Maps algorithm->SHA algorithm. */
29     "sha1", "sha256", "sha512",  /* RSA-1024 */
30     "sha1", "sha256", "sha512",  /* RSA-2048 */
31     "sha1", "sha256", "sha512",  /* RSA-4096 */
32     "sha1", "sha256", "sha512",  /* RSA-8192 */
33   };
34 
35   key_size = siglen_map[algorithm] * 8;  /* in bits. */
36   /* Get key. */
37   snprintf(file_name, FILE_NAME_SIZE, "testkeys/key_rsa%d.keyb", key_size);
38   key = RSAPublicKeyFromFile(file_name);
39   if (!key) {
40     VBDEBUG(("Couldn't read RSA Public key from file: %s\n", file_name));
41     error_code = 1;
42     goto failure;
43   }
44 
45   /* Get expected digest. */
46   snprintf(file_name, FILE_NAME_SIZE, "testcases/test_file.%s.digest",
47            sha_strings[algorithm]);
48   digest = BufferFromFile(file_name, &digest_len);
49   if (!digest) {
50     VBDEBUG(("Couldn't read digest file.\n"));
51     error_code = 1;
52     goto failure;
53   }
54 
55   /* Get signature to verify against. */
56   snprintf(file_name, FILE_NAME_SIZE, "testcases/test_file.rsa%d_%s.sig",
57            key_size, sha_strings[algorithm]);
58   signature = BufferFromFile(file_name, &sig_len);
59   if (!signature) {
60     VBDEBUG(("Couldn't read signature file.\n"));
61     error_code = 1;
62     goto failure;
63   }
64 
65   StartTimer(&ct);
66   for (i = 0; i < NUM_OPERATIONS; i++) {
67     if (!RSAVerify(key, signature, sig_len, algorithm, digest))
68       VBDEBUG(("Warning: Signature Check Failed.\n"));
69   }
70   StopTimer(&ct);
71 
72   msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS;
73   speed = 1000.0 / msecs ;
74   fprintf(stderr, "# rsa%d/%s:\tTime taken per verification = %.02f ms,"
75           " Speed = %.02f verifications/s\n", key_size, sha_strings[algorithm],
76           msecs, speed);
77   fprintf(stdout, "ms_rsa%d_%s:%.02f\n", key_size, sha_strings[algorithm],
78           msecs);
79 
80 failure:
81   free(signature);
82   free(digest);
83   RSAPublicKeyFree(key);
84   return error_code;
85 }
86 
main(int argc,char * argv[])87 int main(int argc, char* argv[]) {
88   int i;
89   int error_code = 0;
90   for (i = 0; i < kNumAlgorithms; ++i) {
91     if(SpeedTestAlgorithm(i))
92       error_code = 1;
93   }
94   return error_code;
95 }
96