blob: c9da240148e1a089561c61b5b397acd9b76d0be7 [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 Shah5411c7a2010-03-31 10:56:49 -070018#include "cryptolib.h"
Gaurav Shah431b9882010-02-12 15:54:37 -080019#include "file_keys.h"
Gaurav Shah322536d2010-01-28 15:01:23 -080020#include "verify_data.h"
21
Gaurav Shah08df9b82010-02-23 16:16:23 -080022/* ANSI Color coding sequences. */
23#define COL_GREEN "\e[1;32m"
Gaurav Shahfc70d722010-03-31 13:26:55 -070024#define COL_RED "\e[0;31m"
Gaurav Shah08df9b82010-02-23 16:16:23 -080025#define COL_STOP "\e[m"
26
Gaurav Shahe178fd92010-02-05 11:44:58 -080027uint8_t* read_signature(char* input_file, int len) {
Gaurav Shah322536d2010-01-28 15:01:23 -080028 int i, sigfd;
Gaurav Shahe178fd92010-02-05 11:44:58 -080029 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080030 if ((sigfd = open(input_file, O_RDONLY)) == -1) {
31 fprintf(stderr, "Couldn't open signature file\n");
32 return NULL;
33 }
34
35 /* Read the signature into a buffer*/
36 signature = (uint8_t*) malloc(len);
37 if (!signature)
38 return NULL;
39
40 if( (i = read(sigfd, signature, len)) != len ) {
41 fprintf(stderr, "Wrong signature length - Expected = %d, Received = %d\n",
42 len, i);
43 close(sigfd);
44 return NULL;
45 }
46
47 close(sigfd);
48 return signature;
49}
50
Gaurav Shah322536d2010-01-28 15:01:23 -080051int main(int argc, char* argv[]) {
52 int i, algorithm, sig_len;
Gaurav Shah6f555392010-02-11 21:04:49 -080053 int return_code = 1; /* Default to error. */
Gaurav Shahe178fd92010-02-05 11:44:58 -080054 uint8_t* digest = NULL;
55 uint8_t* signature = NULL;
Gaurav Shah322536d2010-01-28 15:01:23 -080056 RSAPublicKey* key = NULL;
57
58 if (argc!=5) {
59 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>"
60 " <input file>\n\n", argv[0]);
61 fprintf(stderr, "where <algorithm> depends on the signature algorithm"
62 " used:\n");
63 for(i = 0; i<kNumAlgorithms; i++)
64 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]);
65 return -1;
66 }
67
68 algorithm = atoi(argv[1]);
69 if (algorithm >= kNumAlgorithms) {
70 fprintf(stderr, "Invalid Algorithm!\n");
71 return 0;
72 }
73 /* Length of the RSA Signature/RSA Key */
Gaurav Shahf5564fa2010-03-02 15:40:01 -080074 sig_len = siglen_map[algorithm];
75 if ((key = RSAPublicKeyFromFile(argv[2])) &&
76 (signature = read_signature(argv[3], sig_len)) &&
77 (digest = DigestFile(argv[4], algorithm))) {
78 if (RSAVerify(key, signature, sig_len, algorithm, digest)) {
79 return_code = 0;
80 fprintf(stderr, "Signature Verification "
81 COL_GREEN "SUCCEEDED" COL_STOP "\n");
82 } else {
83 fprintf(stderr, "Signature Verification "
84 COL_RED "FAILED" COL_STOP "\n");
85 }
Gaurav Shah6f555392010-02-11 21:04:49 -080086 }
Gaurav Shahf5564fa2010-03-02 15:40:01 -080087 else
88 return_code = -1;
Gaurav Shah322536d2010-01-28 15:01:23 -080089
Gaurav Shah322536d2010-01-28 15:01:23 -080090 free(key);
91 free(signature);
92 free(digest);
93
Gaurav Shah6f555392010-02-11 21:04:49 -080094 return return_code;
Gaurav Shah322536d2010-01-28 15:01:23 -080095}