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 18 #include "cryptolib.h" 19 #include "file_keys.h" 20 #include "verify_data.h" 21 22 /* ANSI Color coding sequences. */ 23 #define COL_GREEN "\e[1;32m" 24 #define COL_RED "\e[0;31m" 25 #define COL_STOP "\e[m" 26 27 uint8_t* read_signature(char* input_file, int len) { 28 int i, sigfd; 29 uint8_t* signature = NULL; 30 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 51 int main(int argc, char* argv[]) { 52 int i, algorithm, sig_len; 53 int return_code = 1; /* Default to error. */ 54 uint8_t* digest = NULL; 55 uint8_t* signature = NULL; 56 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 */ 74 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 } 86 } 87 else 88 return_code = -1; 89 90 free(key); 91 free(signature); 92 free(digest); 93 94 return return_code; 95 } 96