blob: 85ba0c9bac5cef67f4774cb43a3201a7187784f8 [file] [log] [blame]
Randall Spangler32a65262011-06-27 10:49:11 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shahcb3d22e2010-03-04 10:22:36 -08002 * 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 Spangler32a65262011-06-27 10:49:11 -070015#include "host_common.h"
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080016#include "padding.h"
17#include "signature_digest.h"
Randall Spangler32a65262011-06-27 10:49:11 -070018
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080019
20int 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 Shah456678b2010-03-10 18:38:45 -080025 uint64_t len;
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080026 uint32_t signature_digest_len;
27
28 if (argc != 3) {
Gaurav Shah5ab5a372011-02-07 11:12:39 -080029 fprintf(stderr, "Usage: %s <alg_id> <file>", argv[0]);
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080030 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 Shah5ab5a372011-02-07 11:12:39 -080040 fprintf(stderr, "Could not read file: %s\n", argv[2]);
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080041 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 Spangler32a65262011-06-27 10:49:11 -070052 free(signature_digest);
53 free(buf);
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080054 return error_code;
55}