blob: 7b27f9e88e0a81f08816ed0534d964a9f89acac5 [file] [log] [blame]
Randall Spangler32a65262011-06-27 10:49:11 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shah52898d32010-02-17 16:37:33 -08002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
Bill Richardson0c3ba242013-03-29 11:09:30 -07006#include <stdint.h>
Gaurav Shah52898d32010-02-17 16:37:33 -08007#include <stdio.h>
8#include <stdlib.h>
9
Gaurav Shah5411c7a2010-03-31 10:56:49 -070010#include "cryptolib.h"
Gaurav Shah52898d32010-02-17 16:37:33 -080011#include "file_keys.h"
Randall Spangler32a65262011-06-27 10:49:11 -070012#include "host_common.h"
Gaurav Shah52898d32010-02-17 16:37:33 -080013#include "timer_utils.h"
Gaurav Shah52898d32010-02-17 16:37:33 -080014
15#define FILE_NAME_SIZE 128
16#define NUM_OPERATIONS 100 /* Number of signature operations to time. */
17
Gaurav Shah8b95c702010-02-28 12:50:29 -080018int SpeedTestAlgorithm(int algorithm) {
Gaurav Shah52898d32010-02-17 16:37:33 -080019 int i, key_size;
Gaurav Shah8b95c702010-02-28 12:50:29 -080020 int error_code = 0;
Gaurav Shah52898d32010-02-17 16:37:33 -080021 double speed, msecs;
22 char file_name[FILE_NAME_SIZE];
23 uint8_t* digest = NULL;
24 uint8_t* signature = NULL;
Gaurav Shah456678b2010-03-10 18:38:45 -080025 uint64_t digest_len, sig_len;
Gaurav Shah52898d32010-02-17 16:37:33 -080026 RSAPublicKey* key = NULL;
27 ClockTimerState ct;
28 char* sha_strings[] = { /* Maps algorithm->SHA algorithm. */
Gaurav Shahc7daf382010-03-01 20:24:37 -080029 "sha1", "sha256", "sha512", /* RSA-1024 */
Gaurav Shah52898d32010-02-17 16:37:33 -080030 "sha1", "sha256", "sha512", /* RSA-2048 */
31 "sha1", "sha256", "sha512", /* RSA-4096 */
32 "sha1", "sha256", "sha512", /* RSA-8192 */
33 };
34
Gaurav Shahcae5fa62010-02-28 20:02:29 -080035 key_size = siglen_map[algorithm] * 8; /* in bits. */
Gaurav Shah52898d32010-02-17 16:37:33 -080036 /* Get key. */
37 snprintf(file_name, FILE_NAME_SIZE, "testkeys/key_rsa%d.keyb", key_size);
38 key = RSAPublicKeyFromFile(file_name);
39 if (!key) {
Bill Richardsonabf05502010-07-01 10:22:06 -070040 VBDEBUG(("Couldn't read RSA Public key from file: %s\n", file_name));
Gaurav Shah8b95c702010-02-28 12:50:29 -080041 error_code = 1;
Gaurav Shah52898d32010-02-17 16:37:33 -080042 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) {
Bill Richardsonabf05502010-07-01 10:22:06 -070050 VBDEBUG(("Couldn't read digest file.\n"));
Gaurav Shah8b95c702010-02-28 12:50:29 -080051 error_code = 1;
Gaurav Shah52898d32010-02-17 16:37:33 -080052 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) {
Bill Richardsonabf05502010-07-01 10:22:06 -070060 VBDEBUG(("Couldn't read signature file.\n"));
Gaurav Shah8b95c702010-02-28 12:50:29 -080061 error_code = 1;
Gaurav Shah52898d32010-02-17 16:37:33 -080062 goto failure;
63 }
64
65 StartTimer(&ct);
66 for (i = 0; i < NUM_OPERATIONS; i++) {
Gaurav Shahf5564fa2010-03-02 15:40:01 -080067 if (!RSAVerify(key, signature, sig_len, algorithm, digest))
Bill Richardsonabf05502010-07-01 10:22:06 -070068 VBDEBUG(("Warning: Signature Check Failed.\n"));
Gaurav Shah52898d32010-02-17 16:37:33 -080069 }
70 StopTimer(&ct);
71
72 msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS;
73 speed = 1000.0 / msecs ;
Gaurav Shah8b95c702010-02-28 12:50:29 -080074 fprintf(stderr, "# rsa%d/%s:\tTime taken per verification = %.02f ms,"
Gaurav Shah52898d32010-02-17 16:37:33 -080075 " Speed = %.02f verifications/s\n", key_size, sha_strings[algorithm],
76 msecs, speed);
Gaurav Shahc7daf382010-03-01 20:24:37 -080077 fprintf(stdout, "ms_rsa%d_%s:%.02f\n", key_size, sha_strings[algorithm],
78 msecs);
Gaurav Shah52898d32010-02-17 16:37:33 -080079
80failure:
Randall Spangler32a65262011-06-27 10:49:11 -070081 free(signature);
82 free(digest);
Gaurav Shah259de402010-03-12 17:42:03 -080083 RSAPublicKeyFree(key);
Gaurav Shah8b95c702010-02-28 12:50:29 -080084 return error_code;
Gaurav Shah52898d32010-02-17 16:37:33 -080085}
86
87int main(int argc, char* argv[]) {
88 int i;
Gaurav Shah8b95c702010-02-28 12:50:29 -080089 int error_code = 0;
Gaurav Shah52898d32010-02-17 16:37:33 -080090 for (i = 0; i < kNumAlgorithms; ++i) {
Gaurav Shah8b95c702010-02-28 12:50:29 -080091 if(SpeedTestAlgorithm(i))
92 error_code = 1;
Gaurav Shah52898d32010-02-17 16:37:33 -080093 }
Gaurav Shah8b95c702010-02-28 12:50:29 -080094 return error_code;
Gaurav Shah52898d32010-02-17 16:37:33 -080095}