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