signature_digest_utility.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "2sysincludes.h"
  12. #include "2common.h"
  13. #include "host_common.h"
  14. #include "host_signature2.h"
  15. #include "signature_digest.h"
  16. int main(int argc, char* argv[])
  17. {
  18. int error_code = -1;
  19. uint8_t *buf = NULL;
  20. uint8_t *signature_digest = NULL;
  21. uint32_t len;
  22. if (argc != 3) {
  23. fprintf(stderr, "Usage: %s <alg_id> <file>", argv[0]);
  24. goto cleanup;
  25. }
  26. int algorithm = atoi(argv[1]);
  27. if (algorithm < 0 || algorithm >= VB2_ALG_COUNT) {
  28. fprintf(stderr, "Invalid Algorithm!\n");
  29. goto cleanup;
  30. }
  31. if (VB2_SUCCESS != vb2_read_file(argv[2], &buf, &len)) {
  32. fprintf(stderr, "Could not read file: %s\n", argv[2]);
  33. goto cleanup;
  34. }
  35. enum vb2_hash_algorithm hash_alg = vb2_crypto_to_hash(algorithm);
  36. uint32_t digest_size = vb2_digest_size(hash_alg);
  37. uint32_t digestinfo_size = 0;
  38. const uint8_t *digestinfo = NULL;
  39. if (VB2_SUCCESS != vb2_digest_info(hash_alg, &digestinfo,
  40. &digestinfo_size))
  41. goto cleanup;
  42. uint32_t signature_digest_len = digest_size + digestinfo_size;
  43. signature_digest = SignatureDigest(buf, len, algorithm);
  44. if(signature_digest &&
  45. fwrite(signature_digest, signature_digest_len, 1, stdout) == 1)
  46. error_code = 0;
  47. cleanup:
  48. free(signature_digest);
  49. free(buf);
  50. return error_code;
  51. }