| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* Routines for verifying a file's signature. Useful in testing the core | 6 /* Routines for verifying a file's signature. Useful in testing the core |
| 7 * RSA verification implementation. | 7 * RSA verification implementation. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 #include <fcntl.h> | 10 #include <fcntl.h> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 #include <sys/stat.h> | 14 #include <sys/stat.h> |
| 15 #include <sys/types.h> | 15 #include <sys/types.h> |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 | 17 |
| 18 #include "file_keys.h" |
| 18 #include "sha_utility.h" | 19 #include "sha_utility.h" |
| 19 #include "padding.h" | 20 #include "padding.h" |
| 20 #include "rsa.h" | 21 #include "rsa.h" |
| 21 #include "rsa_utility.h" | 22 #include "rsa_utility.h" |
| 22 #include "verify_data.h" | 23 #include "verify_data.h" |
| 23 | 24 |
| 24 RSAPublicKey* read_RSAkey(char* input_file) { | |
| 25 int key_fd; | |
| 26 int buf_len; | |
| 27 struct stat stat_fd; | |
| 28 uint8_t* buf = NULL; | |
| 29 | |
| 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 | |
| 35 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) | |
| 44 return NULL; | |
| 45 | |
| 46 if (buf_len != read(key_fd, buf, buf_len)) { | |
| 47 fprintf(stderr, "Couldn't read key into a buffer.\n"); | |
| 48 return NULL; | |
| 49 } | |
| 50 | |
| 51 close(key_fd); | |
| 52 return RSAPublicKeyFromBuf(buf, buf_len); | |
| 53 } | |
| 54 | |
| 55 uint8_t* read_signature(char* input_file, int len) { | 25 uint8_t* read_signature(char* input_file, int len) { |
| 56 int i, sigfd; | 26 int i, sigfd; |
| 57 uint8_t* signature = NULL; | 27 uint8_t* signature = NULL; |
| 58 if ((sigfd = open(input_file, O_RDONLY)) == -1) { | 28 if ((sigfd = open(input_file, O_RDONLY)) == -1) { |
| 59 fprintf(stderr, "Couldn't open signature file\n"); | 29 fprintf(stderr, "Couldn't open signature file\n"); |
| 60 return NULL; | 30 return NULL; |
| 61 } | 31 } |
| 62 | 32 |
| 63 /* Read the signature into a buffer*/ | 33 /* Read the signature into a buffer*/ |
| 64 signature = (uint8_t*) malloc(len); | 34 signature = (uint8_t*) malloc(len); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 94 } | 64 } |
| 95 | 65 |
| 96 algorithm = atoi(argv[1]); | 66 algorithm = atoi(argv[1]); |
| 97 if (algorithm >= kNumAlgorithms) { | 67 if (algorithm >= kNumAlgorithms) { |
| 98 fprintf(stderr, "Invalid Algorithm!\n"); | 68 fprintf(stderr, "Invalid Algorithm!\n"); |
| 99 return 0; | 69 return 0; |
| 100 } | 70 } |
| 101 /* Length of the RSA Signature/RSA Key */ | 71 /* Length of the RSA Signature/RSA Key */ |
| 102 sig_len = siglen_map[algorithm] * sizeof(uint32_t); | 72 sig_len = siglen_map[algorithm] * sizeof(uint32_t); |
| 103 | 73 |
| 104 if (!(key = read_RSAkey(argv[2]))) | 74 if (!(key = RSAPublicKeyFromFile(argv[2]))) |
| 105 goto failure; | 75 goto failure; |
| 106 if (!(signature = read_signature(argv[3], sig_len))) | 76 if (!(signature = read_signature(argv[3], sig_len))) |
| 107 goto failure; | 77 goto failure; |
| 108 if (!(digest = DigestFile(argv[4], algorithm))) | 78 if (!(digest = DigestFile(argv[4], algorithm))) |
| 109 goto failure; | 79 goto failure; |
| 110 if(RSA_verify(key, signature, sig_len, algorithm, digest)) | 80 if(RSA_verify(key, signature, sig_len, algorithm, digest)) |
| 111 fprintf(stderr, "Signature Verification SUCCEEDED.\n"); | 81 fprintf(stderr, "Signature Verification SUCCEEDED.\n"); |
| 112 else | 82 else |
| 113 fprintf(stderr, "Signature Verification FAILED!\n"); | 83 fprintf(stderr, "Signature Verification FAILED!\n"); |
| 114 | 84 |
| 115 failure: | 85 failure: |
| 116 free(key); | 86 free(key); |
| 117 free(signature); | 87 free(signature); |
| 118 free(digest); | 88 free(digest); |
| 119 | 89 |
| 120 return 0; | 90 return 0; |
| 121 } | 91 } |
| OLD | NEW |