pad_digest_utility.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "2sysincludes.h"
  9. #include "2common.h"
  10. #include "2sha.h"
  11. #include "host_common.h"
  12. #include "host_signature2.h"
  13. #include "signature_digest.h"
  14. static void usage(char* argv[]) {
  15. fprintf(stderr,
  16. "Usage: %s <alg_id> <digest_file>\n"
  17. "\n"
  18. "Generate a padded hash suitable for generating PKCS#1.5 "
  19. "signatures.\n",
  20. basename(argv[0]));
  21. }
  22. int main(int argc, char* argv[])
  23. {
  24. int algorithm = -1;
  25. int error_code = -1;
  26. uint8_t* digest = NULL;
  27. uint8_t* padded_digest = NULL;
  28. uint32_t len;
  29. if (argc != 3) {
  30. usage(argv);
  31. goto cleanup;
  32. }
  33. algorithm = atoi(argv[1]);
  34. if (algorithm < 0 || algorithm >= VB2_ALG_COUNT) {
  35. fprintf(stderr, "Invalid Algorithm!\n");
  36. goto cleanup;
  37. }
  38. enum vb2_hash_algorithm hash_alg = vb2_crypto_to_hash(algorithm);
  39. uint32_t digest_size = vb2_digest_size(hash_alg);
  40. uint32_t digestinfo_size = 0;
  41. const uint8_t* digestinfo = NULL;
  42. if (VB2_SUCCESS != vb2_digest_info(hash_alg, &digestinfo,
  43. &digestinfo_size)) {
  44. fprintf(stderr, "SignatureBuf(): Couldn't get digest info\n");
  45. goto cleanup;
  46. }
  47. uint32_t padded_digest_len = digest_size + digestinfo_size;
  48. if (VB2_SUCCESS != vb2_read_file(argv[2], &digest, &len)) {
  49. fprintf(stderr, "Could not read file: %s\n", argv[2]);
  50. goto cleanup;
  51. }
  52. padded_digest = PrependDigestInfo(hash_alg, digest);
  53. if(padded_digest &&
  54. fwrite(padded_digest, padded_digest_len, 1, stdout) == 1)
  55. error_code = 0;
  56. cleanup:
  57. free(padded_digest);
  58. free(digest);
  59. return error_code;
  60. }