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