blob: ac51dafaf1749cb10bcb65ec19e961bf73376e61 [file] [log] [blame]
Gaurav Shah322536d2010-01-28 15:01:23 -08001/* 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 Shah321f3102010-01-28 16:59:42 -080018#include "digest_utility.h"
Gaurav Shah322536d2010-01-28 15:01:23 -080019#include "padding.h"
20#include "rsa.h"
Gaurav Shahe178fd92010-02-05 11:44:58 -080021#include "rsa_utility.h"
Gaurav Shah322536d2010-01-28 15:01:23 -080022#include "verify_data.h"
23
Gaurav Shahe178fd92010-02-05 11:44:58 -080024RSAPublicKey* read_RSAkey(char* input_file, int len) {
Gaurav Shah322536d2010-01-28 15:01:23 -080025 int key_fd;
Gaurav Shahe178fd92010-02-05 11:44:58 -080026 int buf_len;
27 struct stat stat_fd;
28 uint8_t* buf = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080029
30 if ((key_fd = open(input_file, O_RDONLY)) == -1) {
31 fprintf(stderr, "Couldn't open pre-processed key file\n");
32 return NULL;
33 }
34
Gaurav Shahe178fd92010-02-05 11:44:58 -080035 if (-1 == fstat(key_fd, &stat_fd)) {
36 fprintf(stderr, "Couldn't stat key file\n");
37 return NULL;
38 }
39 buf_len = stat_fd.st_size;
40
41 /* Read entire key binary blob into a buffer. */
42 buf = (uint8_t*) malloc(buf_len);
43 if (!buf)
Gaurav Shah322536d2010-01-28 15:01:23 -080044 return NULL;
45
Gaurav Shahe178fd92010-02-05 11:44:58 -080046 if (buf_len != read(key_fd, buf, buf_len)) {
47 fprintf(stderr, "Couldn't read key into a buffer.\n");
48 return NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080049 }
Gaurav Shah322536d2010-01-28 15:01:23 -080050
51 close(key_fd);
Gaurav Shahe178fd92010-02-05 11:44:58 -080052 return RSAPublicKeyFromBuf(buf, buf_len);
Gaurav Shah322536d2010-01-28 15:01:23 -080053}
54
Gaurav Shahe178fd92010-02-05 11:44:58 -080055uint8_t* read_signature(char* input_file, int len) {
Gaurav Shah322536d2010-01-28 15:01:23 -080056 int i, sigfd;
Gaurav Shahe178fd92010-02-05 11:44:58 -080057 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080058 if ((sigfd = open(input_file, O_RDONLY)) == -1) {
59 fprintf(stderr, "Couldn't open signature file\n");
60 return NULL;
61 }
62
63 /* Read the signature into a buffer*/
64 signature = (uint8_t*) malloc(len);
65 if (!signature)
66 return NULL;
67
68 if( (i = read(sigfd, signature, len)) != len ) {
69 fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n",
70 len, i);
71 close(sigfd);
72 return NULL;
73 }
74
75 close(sigfd);
76 return signature;
77}
78
79
80int main(int argc, char* argv[]) {
81 int i, algorithm, sig_len;
Gaurav Shahe178fd92010-02-05 11:44:58 -080082 uint8_t* digest = NULL;
83 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080084 RSAPublicKey* key = NULL;
85
86 if (argc!=5) {
87 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>"
88 " <input file>\n\n", argv[0]);
89 fprintf(stderr, "where <algorithm> depends on the signature algorithm"
90 " used:\n");
91 for(i = 0; i<kNumAlgorithms; i++)
92 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]);
93 return -1;
94 }
95
96 algorithm = atoi(argv[1]);
97 if (algorithm >= kNumAlgorithms) {
98 fprintf(stderr, "Invalid Algorithm!\n");
99 return 0;
100 }
101 /* Length of the RSA Signature/RSA Key */
102 sig_len = siglen_map[algorithm] * sizeof(uint32_t);
103
104 if (!(key = read_RSAkey(argv[2], sig_len)))
105 goto failure;
106 if (!(signature = read_signature(argv[3], sig_len)))
107 goto failure;
108 if (!(digest = calculate_digest(argv[4], algorithm)))
109 goto failure;
110 if(RSA_verify(key, signature, sig_len, algorithm, digest))
111 fprintf(stderr, "Signature Verification SUCCEEDED.\n");
112 else
113 fprintf(stderr, "Signature Verification FAILED!\n");
114
115failure:
116 free(key);
117 free(signature);
118 free(digest);
119
120 return 0;
121}