Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Utility that outputs the cryptographic digest of a contents of a |
| 6 | * file in a format that can be directly used to generate PKCS#1 v1.5 |
| 7 | * signatures via the "openssl" command line utility. |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #include "file_keys.h" |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 15 | #include "host_common.h" |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 16 | #include "padding.h" |
| 17 | #include "signature_digest.h" |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 18 | |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 19 | |
| 20 | int main(int argc, char* argv[]) { |
| 21 | int algorithm = -1; |
| 22 | int error_code = 0; |
| 23 | uint8_t* buf = NULL; |
| 24 | uint8_t* signature_digest = NULL; |
Gaurav Shah | 456678b | 2010-03-10 18:38:45 -0800 | [diff] [blame] | 25 | uint64_t len; |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 26 | uint32_t signature_digest_len; |
| 27 | |
| 28 | if (argc != 3) { |
Gaurav Shah | 5ab5a37 | 2011-02-07 11:12:39 -0800 | [diff] [blame] | 29 | fprintf(stderr, "Usage: %s <alg_id> <file>", argv[0]); |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 30 | return -1; |
| 31 | } |
| 32 | algorithm = atoi(argv[1]); |
| 33 | if (algorithm < 0 || algorithm >= kNumAlgorithms) { |
| 34 | fprintf(stderr, "Invalid Algorithm!\n"); |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | buf = BufferFromFile(argv[2], &len); |
| 39 | if (!buf) { |
Gaurav Shah | 5ab5a37 | 2011-02-07 11:12:39 -0800 | [diff] [blame] | 40 | fprintf(stderr, "Could not read file: %s\n", argv[2]); |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | signature_digest = SignatureDigest(buf, len, algorithm); |
| 45 | signature_digest_len = (hash_size_map[algorithm] + |
| 46 | digestinfo_size_map[algorithm]); |
| 47 | if (!signature_digest) |
| 48 | error_code = -1; |
| 49 | if(signature_digest && |
| 50 | 1 != fwrite(signature_digest, signature_digest_len, 1, stdout)) |
| 51 | error_code = -1; |
Randall Spangler | 32a6526 | 2011-06-27 10:49:11 -0700 | [diff] [blame] | 52 | free(signature_digest); |
| 53 | free(buf); |
Gaurav Shah | cb3d22e | 2010-03-04 10:22:36 -0800 | [diff] [blame] | 54 | return error_code; |
| 55 | } |