Gaurav Shah | 8bf29d8 | 2010-01-28 19:43:24 -0800 | [diff] [blame] | 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 | * Utility that outputs the message digest of the contents of a file in a |
| 6 | * format that can be used as input to OpenSSL for an RSA signature. |
| 7 | * Needed until the stable OpenSSL release supports SHA-256/512 digests for |
| 8 | * RSA signatures. |
| 9 | * Outputs DigestInfo || Digest where DigestInfo is the OID depending on the |
| 10 | * choice of the hash algorithm (see padding.c). |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "signature_digest.h" |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | |
Gaurav Shah | 8bf29d8 | 2010-01-28 19:43:24 -0800 | [diff] [blame] | 20 | #include "padding.h" |
| 21 | #include "sha.h" |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame^] | 22 | #include "sha_utility.h" |
Gaurav Shah | 8bf29d8 | 2010-01-28 19:43:24 -0800 | [diff] [blame] | 23 | |
| 24 | uint8_t* prepend_digestinfo(int algorithm, uint8_t* digest) { |
| 25 | const int digest_size = hash_size_map[algorithm]; |
| 26 | const int digestinfo_size = digestinfo_size_map[algorithm]; |
| 27 | const uint8_t* digestinfo = hash_digestinfo_map[algorithm]; |
| 28 | uint8_t* p = malloc(digestinfo_size + digest_size); |
| 29 | memcpy(p, digestinfo, digestinfo_size); |
| 30 | memcpy(p + digestinfo_size, digest, digest_size); |
| 31 | return p; |
| 32 | } |
| 33 | |
| 34 | int main(int argc, char* argv[]) { |
| 35 | int i, algorithm; |
| 36 | uint8_t* digest = NULL; |
| 37 | uint8_t* signature = NULL; |
| 38 | uint8_t* info_digest = NULL; |
| 39 | |
| 40 | if (argc != 3) { |
| 41 | fprintf(stderr, "Usage: %s <algorithm> <input file>\n\n", |
| 42 | argv[0]); |
| 43 | fprintf(stderr, "where <algorithm> is the signature algorithm to use:\n"); |
| 44 | for(i = 0; i<kNumAlgorithms; i++) |
| 45 | fprintf(stderr, "\t%d for %s\n", i, algo_strings[i]); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | algorithm = atoi(argv[1]); |
| 50 | if (algorithm >= kNumAlgorithms) { |
| 51 | fprintf(stderr, "Invalid Algorithm!\n"); |
| 52 | goto failure; |
| 53 | } |
| 54 | |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame^] | 55 | if (!(digest = DigestFile(argv[2], algorithm))) |
Gaurav Shah | 8bf29d8 | 2010-01-28 19:43:24 -0800 | [diff] [blame] | 56 | goto failure; |
| 57 | |
| 58 | info_digest = prepend_digestinfo(algorithm, digest); |
| 59 | write(1, info_digest, hash_size_map[algorithm] + |
| 60 | digestinfo_size_map[algorithm]); |
| 61 | |
| 62 | failure: |
| 63 | free(digest); |
| 64 | free(info_digest); |
| 65 | free(signature); |
| 66 | |
| 67 | return 0; |
| 68 | } |