Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 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 | /* Routines for verifying a file's signature. Useful in testing the core |
| 7 | * RSA verification implementation. |
| 8 | */ |
| 9 | |
| 10 | #include <fcntl.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <unistd.h> |
| 17 | |
Gaurav Shah | 321f310 | 2010-01-28 16:59:42 -0800 | [diff] [blame] | 18 | #include "digest_utility.h" |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 19 | #include "padding.h" |
| 20 | #include "rsa.h" |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 21 | #include "verify_data.h" |
| 22 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 23 | RSAPublicKey* read_RSAkey(char *input_file, int len) { |
| 24 | int key_fd; |
| 25 | RSAPublicKey *key = NULL; |
| 26 | |
| 27 | if ((key_fd = open(input_file, O_RDONLY)) == -1) { |
| 28 | fprintf(stderr, "Couldn't open pre-processed key file\n"); |
| 29 | return NULL; |
| 30 | } |
| 31 | |
| 32 | key = (RSAPublicKey *) malloc(sizeof(RSAPublicKey)); |
| 33 | if (!key) |
| 34 | return NULL; |
| 35 | |
| 36 | /* Read the pre-processed RSA key into a RSAPublicKey structure */ |
| 37 | /* TODO(gauravsh): Add error checking here? */ |
| 38 | |
| 39 | read(key_fd, &key->len, sizeof(key->len)); |
| 40 | read(key_fd, &key->n0inv, sizeof(key->n0inv)); |
| 41 | |
| 42 | #ifndef NDEBUG |
| 43 | fprintf(stderr, "%d\n", key->len); |
| 44 | fprintf(stderr, "%d\n", key->n0inv); |
| 45 | #endif |
| 46 | |
| 47 | key->n = (uint32_t *) malloc(len); |
| 48 | read(key_fd, key->n, len); |
| 49 | |
| 50 | key->rr = (uint32_t *) malloc(len); |
| 51 | read(key_fd, key->rr, len); |
| 52 | |
| 53 | #ifndef NDEBUG |
| 54 | { |
| 55 | int i; |
| 56 | for(i=0; i<key->len; i++) { |
| 57 | fprintf(stderr, "%d,", key->n[i]); |
| 58 | } |
| 59 | fprintf(stderr, "\n"); |
| 60 | |
| 61 | for(i=0; i<key->len; i++) { |
| 62 | fprintf(stderr, "%d,", key->rr[i]); |
| 63 | } |
| 64 | fprintf(stderr, "\n"); |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | close(key_fd); |
| 69 | return key; |
| 70 | } |
| 71 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 72 | uint8_t* read_signature(char *input_file, int len) { |
| 73 | int i, sigfd; |
| 74 | uint8_t *signature = NULL; |
| 75 | if ((sigfd = open(input_file, O_RDONLY)) == -1) { |
| 76 | fprintf(stderr, "Couldn't open signature file\n"); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | /* Read the signature into a buffer*/ |
| 81 | signature = (uint8_t*) malloc(len); |
| 82 | if (!signature) |
| 83 | return NULL; |
| 84 | |
| 85 | if( (i = read(sigfd, signature, len)) != len ) { |
| 86 | fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n", |
| 87 | len, i); |
| 88 | close(sigfd); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | close(sigfd); |
| 93 | return signature; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | int main(int argc, char* argv[]) { |
| 98 | int i, algorithm, sig_len; |
| 99 | uint8_t *digest = NULL, *signature = NULL; |
| 100 | RSAPublicKey* key = NULL; |
| 101 | |
| 102 | if (argc!=5) { |
| 103 | fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>" |
| 104 | " <input file>\n\n", argv[0]); |
| 105 | fprintf(stderr, "where <algorithm> depends on the signature algorithm" |
| 106 | " used:\n"); |
| 107 | for(i = 0; i<kNumAlgorithms; i++) |
| 108 | fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | algorithm = atoi(argv[1]); |
| 113 | if (algorithm >= kNumAlgorithms) { |
| 114 | fprintf(stderr, "Invalid Algorithm!\n"); |
| 115 | return 0; |
| 116 | } |
| 117 | /* Length of the RSA Signature/RSA Key */ |
| 118 | sig_len = siglen_map[algorithm] * sizeof(uint32_t); |
| 119 | |
| 120 | if (!(key = read_RSAkey(argv[2], sig_len))) |
| 121 | goto failure; |
| 122 | if (!(signature = read_signature(argv[3], sig_len))) |
| 123 | goto failure; |
| 124 | if (!(digest = calculate_digest(argv[4], algorithm))) |
| 125 | goto failure; |
| 126 | if(RSA_verify(key, signature, sig_len, algorithm, digest)) |
| 127 | fprintf(stderr, "Signature Verification SUCCEEDED.\n"); |
| 128 | else |
| 129 | fprintf(stderr, "Signature Verification FAILED!\n"); |
| 130 | |
| 131 | failure: |
| 132 | free(key); |
| 133 | free(signature); |
| 134 | free(digest); |
| 135 | |
| 136 | return 0; |
| 137 | } |