132a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah * Use of this source code is governed by a BSD-style license that can be
352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah * found in the LICENSE file.
452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah */
552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
60c3ba249abb1dc60f5ebabccf84ff13206440b83Bill Richardson#include <stdint.h>
752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah#include <stdio.h>
852898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah#include <stdlib.h>
952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
105411c7a9f03f91bf2c1cd1cf852db9d4585a05c9Gaurav Shah#include "cryptolib.h"
1152898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah#include "file_keys.h"
1232a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler#include "host_common.h"
1352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah#include "timer_utils.h"
1452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
1552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah#define FILE_NAME_SIZE 128
1652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah#define NUM_OPERATIONS 100 /* Number of signature operations to time. */
1752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
188b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shahint SpeedTestAlgorithm(int algorithm) {
1952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  int i, key_size;
208b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah  int error_code = 0;
2152898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  double speed, msecs;
2252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  char file_name[FILE_NAME_SIZE];
2352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  uint8_t* digest = NULL;
2452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  uint8_t* signature = NULL;
25456678b0c45d9fa6ad45d5dc6769051a731207f3Gaurav Shah  uint64_t digest_len, sig_len;
2652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  RSAPublicKey* key = NULL;
2752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  ClockTimerState ct;
2852898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  char* sha_strings[] = {  /* Maps algorithm->SHA algorithm. */
29c7daf38f7017a63eb71edbd17be06dc6e740744aGaurav Shah    "sha1", "sha256", "sha512",  /* RSA-1024 */
3052898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah    "sha1", "sha256", "sha512",  /* RSA-2048 */
3152898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah    "sha1", "sha256", "sha512",  /* RSA-4096 */
3252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah    "sha1", "sha256", "sha512",  /* RSA-8192 */
3352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  };
3452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
35cae5fa613a947288262a63a2b89d1ffbb633f673Gaurav Shah  key_size = siglen_map[algorithm] * 8;  /* in bits. */
3652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  /* Get key. */
3752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  snprintf(file_name, FILE_NAME_SIZE, "testkeys/key_rsa%d.keyb", key_size);
3852898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  key = RSAPublicKeyFromFile(file_name);
3952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  if (!key) {
40abf055045802cb06c57ff2d7b187736bdcb3b138Bill Richardson    VBDEBUG(("Couldn't read RSA Public key from file: %s\n", file_name));
418b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah    error_code = 1;
4252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah    goto failure;
4352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  }
4452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
4552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  /* Get expected digest. */
4652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  snprintf(file_name, FILE_NAME_SIZE, "testcases/test_file.%s.digest",
4752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah           sha_strings[algorithm]);
4852898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  digest = BufferFromFile(file_name, &digest_len);
4952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  if (!digest) {
50abf055045802cb06c57ff2d7b187736bdcb3b138Bill Richardson    VBDEBUG(("Couldn't read digest file.\n"));
518b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah    error_code = 1;
5252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah    goto failure;
5352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  }
5452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
5552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  /* Get signature to verify against. */
5652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  snprintf(file_name, FILE_NAME_SIZE, "testcases/test_file.rsa%d_%s.sig",
5752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah           key_size, sha_strings[algorithm]);
5852898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  signature = BufferFromFile(file_name, &sig_len);
5952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  if (!signature) {
60abf055045802cb06c57ff2d7b187736bdcb3b138Bill Richardson    VBDEBUG(("Couldn't read signature file.\n"));
618b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah    error_code = 1;
6252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah    goto failure;
6352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  }
6452898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
6552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  StartTimer(&ct);
6652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  for (i = 0; i < NUM_OPERATIONS; i++) {
67f5564fa98c7e37b6644b92993894ebbe56306f4cGaurav Shah    if (!RSAVerify(key, signature, sig_len, algorithm, digest))
68abf055045802cb06c57ff2d7b187736bdcb3b138Bill Richardson      VBDEBUG(("Warning: Signature Check Failed.\n"));
6952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  }
7052898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  StopTimer(&ct);
7152898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
7252898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  msecs = (float) GetDurationMsecs(&ct) / NUM_OPERATIONS;
7352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  speed = 1000.0 / msecs ;
748b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah  fprintf(stderr, "# rsa%d/%s:\tTime taken per verification = %.02f ms,"
7552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah          " Speed = %.02f verifications/s\n", key_size, sha_strings[algorithm],
7652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah          msecs, speed);
77c7daf38f7017a63eb71edbd17be06dc6e740744aGaurav Shah  fprintf(stdout, "ms_rsa%d_%s:%.02f\n", key_size, sha_strings[algorithm],
78c7daf38f7017a63eb71edbd17be06dc6e740744aGaurav Shah          msecs);
7952898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
8052898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shahfailure:
8132a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler  free(signature);
8232a6526d25d4bf9a1c137fc3d275d1c68935d184Randall Spangler  free(digest);
83259de40640507d6b750db99db49d176ff5adac62Gaurav Shah  RSAPublicKeyFree(key);
848b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah  return error_code;
8552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah}
8652898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah
8752898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shahint main(int argc, char* argv[]) {
8852898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  int i;
898b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah  int error_code = 0;
9052898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  for (i = 0; i < kNumAlgorithms; ++i) {
918b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah    if(SpeedTestAlgorithm(i))
928b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah      error_code = 1;
9352898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah  }
948b95c703730da8a73219c50a6ddb668ea8fc414cGaurav Shah  return error_code;
9552898d3c268e1cdb695b1c9a25fb4190d84a24f8Gaurav Shah}
96