| 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> |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 return NULL; | 72 return NULL; |
| 73 } | 73 } |
| 74 | 74 |
| 75 close(sigfd); | 75 close(sigfd); |
| 76 return signature; | 76 return signature; |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 int main(int argc, char* argv[]) { | 80 int main(int argc, char* argv[]) { |
| 81 int i, algorithm, sig_len; | 81 int i, algorithm, sig_len; |
| 82 int return_code = 1; /* Default to error. */ |
| 82 uint8_t* digest = NULL; | 83 uint8_t* digest = NULL; |
| 83 uint8_t* signature = NULL; | 84 uint8_t* signature = NULL; |
| 84 RSAPublicKey* key = NULL; | 85 RSAPublicKey* key = NULL; |
| 85 | 86 |
| 86 if (argc!=5) { | 87 if (argc!=5) { |
| 87 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>" | 88 fprintf(stderr, "Usage: %s <algorithm> <key file> <signature file>" |
| 88 " <input file>\n\n", argv[0]); | 89 " <input file>\n\n", argv[0]); |
| 89 fprintf(stderr, "where <algorithm> depends on the signature algorithm" | 90 fprintf(stderr, "where <algorithm> depends on the signature algorithm" |
| 90 " used:\n"); | 91 " used:\n"); |
| 91 for(i = 0; i<kNumAlgorithms; i++) | 92 for(i = 0; i<kNumAlgorithms; i++) |
| 92 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); | 93 fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); |
| 93 return -1; | 94 return -1; |
| 94 } | 95 } |
| 95 | 96 |
| 96 algorithm = atoi(argv[1]); | 97 algorithm = atoi(argv[1]); |
| 97 if (algorithm >= kNumAlgorithms) { | 98 if (algorithm >= kNumAlgorithms) { |
| 98 fprintf(stderr, "Invalid Algorithm!\n"); | 99 fprintf(stderr, "Invalid Algorithm!\n"); |
| 99 return 0; | 100 return 0; |
| 100 } | 101 } |
| 101 /* Length of the RSA Signature/RSA Key */ | 102 /* Length of the RSA Signature/RSA Key */ |
| 102 sig_len = siglen_map[algorithm] * sizeof(uint32_t); | 103 sig_len = siglen_map[algorithm] * sizeof(uint32_t); |
| 103 | 104 |
| 104 if (!(key = read_RSAkey(argv[2]))) | 105 if (!(key = read_RSAkey(argv[2]))) |
| 105 goto failure; | 106 goto failure; |
| 106 if (!(signature = read_signature(argv[3], sig_len))) | 107 if (!(signature = read_signature(argv[3], sig_len))) |
| 107 goto failure; | 108 goto failure; |
| 108 if (!(digest = DigestFile(argv[4], algorithm))) | 109 if (!(digest = DigestFile(argv[4], algorithm))) |
| 109 goto failure; | 110 goto failure; |
| 110 if(RSA_verify(key, signature, sig_len, algorithm, digest)) | 111 if(RSA_verify(key, signature, sig_len, algorithm, digest)) { |
| 112 return_code = 0; |
| 111 fprintf(stderr, "Signature Verification SUCCEEDED.\n"); | 113 fprintf(stderr, "Signature Verification SUCCEEDED.\n"); |
| 112 else | 114 } |
| 115 else { |
| 113 fprintf(stderr, "Signature Verification FAILED!\n"); | 116 fprintf(stderr, "Signature Verification FAILED!\n"); |
| 117 } |
| 114 | 118 |
| 115 failure: | 119 failure: |
| 116 free(key); | 120 free(key); |
| 117 free(signature); | 121 free(signature); |
| 118 free(digest); | 122 free(digest); |
| 119 | 123 |
| 120 return 0; | 124 return return_code; |
| 121 } | 125 } |
| OLD | NEW |