xref: /aosp_15_r20/external/vboot_reference/utility/signature_digest_utility.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2011 The ChromiumOS Authors
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 "2common.h"
15 #include "2sysincludes.h"
16 #include "host_common.h"
17 #include "host_signature21.h"
18 #include "signature_digest.h"
19 
main(int argc,char * argv[])20 int 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;
26 
27 	if (argc != 3) {
28 		fprintf(stderr, "Usage: %s <alg_id> <file>", argv[0]);
29 		goto cleanup;
30 	}
31 
32 	int algorithm = atoi(argv[1]);
33 	if (algorithm < 0 || algorithm >= VB2_ALG_COUNT) {
34 		fprintf(stderr, "Invalid Algorithm!\n");
35 		goto cleanup;
36 	}
37 
38 	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 	}
42 
43 	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 
57 cleanup:
58 	free(signature_digest);
59 	free(buf);
60 	return error_code;
61 }
62