blob: 9a39317aed87ba312419e8f11478cd7b1d2f2963 [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
Randall Spangler5a9f4982016-10-14 15:37:25 -070014#include "2common.h"
Joel Kitchingffd42a82019-08-29 13:58:52 +080015#include "2sysincludes.h"
Randall Spangler32a65262011-06-27 10:49:11 -070016#include "host_common.h"
Randall Spangler46a382d2016-10-18 12:00:07 -070017#include "host_signature2.h"
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080018#include "signature_digest.h"
Randall Spangler32a65262011-06-27 10:49:11 -070019
Randall Spangler46a382d2016-10-18 12:00:07 -070020int main(int argc, char* argv[])
21{
22 int error_code = -1;
23 uint8_t *buf = NULL;
24 uint8_t *signature_digest = NULL;
25 uint32_t len;
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080026
Randall Spangler46a382d2016-10-18 12:00:07 -070027 if (argc != 3) {
28 fprintf(stderr, "Usage: %s <alg_id> <file>", argv[0]);
29 goto cleanup;
30 }
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080031
Randall Spangler46a382d2016-10-18 12:00:07 -070032 int algorithm = atoi(argv[1]);
33 if (algorithm < 0 || algorithm >= VB2_ALG_COUNT) {
34 fprintf(stderr, "Invalid Algorithm!\n");
35 goto cleanup;
36 }
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080037
Randall Spangler46a382d2016-10-18 12:00:07 -070038 if (VB2_SUCCESS != vb2_read_file(argv[2], &buf, &len)) {
39 fprintf(stderr, "Could not read file: %s\n", argv[2]);
40 goto cleanup;
41 }
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080042
Randall Spangler46a382d2016-10-18 12:00:07 -070043 enum vb2_hash_algorithm hash_alg = vb2_crypto_to_hash(algorithm);
44 uint32_t digest_size = vb2_digest_size(hash_alg);
45 uint32_t digestinfo_size = 0;
46 const uint8_t *digestinfo = NULL;
47 if (VB2_SUCCESS != vb2_digest_info(hash_alg, &digestinfo,
48 &digestinfo_size))
49 goto cleanup;
50
51 uint32_t signature_digest_len = digest_size + digestinfo_size;
52 signature_digest = SignatureDigest(buf, len, algorithm);
53 if(signature_digest &&
54 fwrite(signature_digest, signature_digest_len, 1, stdout) == 1)
55 error_code = 0;
56
57cleanup:
58 free(signature_digest);
59 free(buf);
60 return error_code;
Gaurav Shahcb3d22e2010-03-04 10:22:36 -080061}