Home | History | Annotate | Download | only in utility
      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 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"
     15 #include "host_common.h"
     16 #include "padding.h"
     17 #include "signature_digest.h"
     18 
     19 
     20 int 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;
     25   uint64_t len;
     26   uint32_t signature_digest_len;
     27 
     28   if (argc != 3) {
     29     fprintf(stderr, "Usage: %s <alg_id> <file>", argv[0]);
     30     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) {
     40     fprintf(stderr, "Could not read file: %s\n", argv[2]);
     41     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;
     52   free(signature_digest);
     53   free(buf);
     54   return error_code;
     55 }
     56