1 /* Copyright (c) 2011 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 for to generate a padded hash suitable for generating
6  * PKCS#1.5 signatures.
7  */
8 
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 #include "file_keys.h"
14 #include "host_common.h"
15 #include "padding.h"
16 #include "signature_digest.h"
17 
18 
main(int argc,char * argv[])19 int main(int argc, char* argv[]) {
20   int algorithm = -1;
21   int error_code = 0;
22   uint8_t* digest = NULL;
23   uint8_t* padded_digest = NULL;
24   uint64_t len;
25   uint32_t padded_digest_len;
26 
27   if (argc != 3) {
28     fprintf(stderr, "Usage: %s <alg_id> <digest_file>", argv[0]);
29     return -1;
30   }
31   algorithm = atoi(argv[1]);
32   if (algorithm < 0 || algorithm >= kNumAlgorithms) {
33     fprintf(stderr, "Invalid Algorithm!\n");
34     return -1;
35   }
36 
37   digest = BufferFromFile(argv[2], &len);
38   if (!digest) {
39     fprintf(stderr, "Could not read file: %s\n", argv[2]);
40     return -1;
41   }
42 
43   padded_digest = PrependDigestInfo(algorithm, digest);
44   padded_digest_len = (hash_size_map[algorithm] +
45                        digestinfo_size_map[algorithm]);
46 
47   if (!padded_digest)
48     error_code = -1;
49   if(padded_digest &&
50      1 != fwrite(padded_digest, padded_digest_len, 1, stdout))
51     error_code = -1;
52   free(padded_digest);
53   free(digest);
54   return error_code;
55 }
56