blob: 0f0dc026891c9edd0db394c7d32f6d294eb8f5be [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 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 Shah290e0782010-02-05 14:37:30 -080026 RSAPublicKey* key = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080027
28 if ((key_fd = open(input_file, O_RDONLY)) == -1) {
29 fprintf(stderr, "Couldn't open pre-processed key file\n");
30 return NULL;
31 }
32
Gaurav Shahe178fd92010-02-05 11:44:58 -080033 if (-1 == fstat(key_fd, &stat_fd)) {
34 fprintf(stderr, "Couldn't stat key file\n");
35 return NULL;
36 }
37 buf_len = stat_fd.st_size;
38
39 /* Read entire key binary blob into a buffer. */
40 buf = (uint8_t*) malloc(buf_len);
41 if (!buf)
Gaurav Shah322536d2010-01-28 15:01:23 -080042 return NULL;
43
Gaurav Shahe178fd92010-02-05 11:44:58 -080044 if (buf_len != read(key_fd, buf, buf_len)) {
45 fprintf(stderr, "Couldn't read key into a buffer.\n");
46 return NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080047 }
Gaurav Shah322536d2010-01-28 15:01:23 -080048
49 close(key_fd);
Gaurav Shahe178fd92010-02-05 11:44:58 -080050 return RSAPublicKeyFromBuf(buf, buf_len);
Gaurav Shah322536d2010-01-28 15:01:23 -080051}
52
Gaurav Shahe178fd92010-02-05 11:44:58 -080053uint8_t* read_signature(char* input_file, int len) {
Gaurav Shah322536d2010-01-28 15:01:23 -080054 int i, sigfd;
Gaurav Shahe178fd92010-02-05 11:44:58 -080055 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080056 if ((sigfd = open(input_file, O_RDONLY)) == -1) {
57 fprintf(stderr, "Couldn't open signature file\n");
58 return NULL;
59 }
60
61 /* Read the signature into a buffer*/
62 signature = (uint8_t*) malloc(len);
63 if (!signature)
64 return NULL;
65
66 if( (i = read(sigfd, signature, len)) != len ) {
67 fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n",
68 len, i);
69 close(sigfd);
70 return NULL;
71 }
72
73 close(sigfd);
74 return signature;
75}
76
77
78int main(int argc, char* argv[]) {
79 int i, algorithm, sig_len;
Gaurav Shahe178fd92010-02-05 11:44:58 -080080 uint8_t* digest = NULL;
81 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080082 RSAPublicKey* key = NULL;
83
84 if (argc!=5) {
85 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>"
86 " <input file>\n\n", argv[0]);
87 fprintf(stderr, "where <algorithm> depends on the signature algorithm"
88 " used:\n");
89 for(i = 0; i<kNumAlgorithms; i++)
90 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]);
91 return -1;
92 }
93
94 algorithm = atoi(argv[1]);
95 if (algorithm >= kNumAlgorithms) {
96 fprintf(stderr, "Invalid Algorithm!\n");
97 return 0;
98 }
99 /* Length of the RSA Signature/RSA Key */
100 sig_len = siglen_map[algorithm] * sizeof(uint32_t);
101
102 if (!(key = read_RSAkey(argv[2], sig_len)))
103 goto failure;
104 if (!(signature = read_signature(argv[3], sig_len)))
105 goto failure;
Gaurav Shah290e0782010-02-05 14:37:30 -0800106 if (!(digest = DigestFile(argv[4], algorithm)))
Gaurav Shah322536d2010-01-28 15:01:23 -0800107 goto failure;
108 if(RSA_verify(key, signature, sig_len, algorithm, digest))
109 fprintf(stderr, "Signature Verification SUCCEEDED.\n");
110 else
111 fprintf(stderr, "Signature Verification FAILED!\n");
112
113failure:
114 free(key);
115 free(signature);
116 free(digest);
117
118 return 0;
119}