blob: 8f8b76bc028ce015453272580ad3df8f07cda7c6 [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 Shah290e0782010-02-05 14:37:30 -080018#include "sha_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 Shah355031b2010-02-05 15:00:23 -080024RSAPublicKey* read_RSAkey(char* input_file) {
Gaurav Shah322536d2010-01-28 15:01:23 -080025 int key_fd;
Gaurav Shah355031b2010-02-05 15:00:23 -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 Shah6f555392010-02-11 21:04:49 -080082 int return_code = 1; /* Default to error. */
Gaurav Shahe178fd92010-02-05 11:44:58 -080083 uint8_t* digest = NULL;
84 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080085 RSAPublicKey* key = NULL;
86
87 if (argc!=5) {
88 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>"
89 " <input file>\n\n", argv[0]);
90 fprintf(stderr, "where <algorithm> depends on the signature algorithm"
91 " used:\n");
92 for(i = 0; i<kNumAlgorithms; i++)
93 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]);
94 return -1;
95 }
96
97 algorithm = atoi(argv[1]);
98 if (algorithm >= kNumAlgorithms) {
99 fprintf(stderr, "Invalid Algorithm!\n");
100 return 0;
101 }
102 /* Length of the RSA Signature/RSA Key */
103 sig_len = siglen_map[algorithm] * sizeof(uint32_t);
104
Gaurav Shah355031b2010-02-05 15:00:23 -0800105 if (!(key = read_RSAkey(argv[2])))
Gaurav Shah322536d2010-01-28 15:01:23 -0800106 goto failure;
107 if (!(signature = read_signature(argv[3], sig_len)))
108 goto failure;
Gaurav Shah290e0782010-02-05 14:37:30 -0800109 if (!(digest = DigestFile(argv[4], algorithm)))
Gaurav Shah322536d2010-01-28 15:01:23 -0800110 goto failure;
Gaurav Shah6f555392010-02-11 21:04:49 -0800111 if(RSA_verify(key, signature, sig_len, algorithm, digest)) {
112 return_code = 0;
Gaurav Shah322536d2010-01-28 15:01:23 -0800113 fprintf(stderr, "Signature Verification SUCCEEDED.\n");
Gaurav Shah6f555392010-02-11 21:04:49 -0800114 }
115 else {
Gaurav Shah322536d2010-01-28 15:01:23 -0800116 fprintf(stderr, "Signature Verification FAILED!\n");
Gaurav Shah6f555392010-02-11 21:04:49 -0800117 }
Gaurav Shah322536d2010-01-28 15:01:23 -0800118
119failure:
120 free(key);
121 free(signature);
122 free(digest);
123
Gaurav Shah6f555392010-02-11 21:04:49 -0800124 return return_code;
Gaurav Shah322536d2010-01-28 15:01:23 -0800125}