verify_data.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright (c) 2010 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. /* Routines for verifying a file's signature. Useful in testing the core
  6. * RSA verification implementation.
  7. */
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <unistd.h>
  15. #include "2sysincludes.h"
  16. #include "2common.h"
  17. #include "2sha.h"
  18. #include "2rsa.h"
  19. #include "file_keys.h"
  20. #include "host_common.h"
  21. #include "vb2_common.h"
  22. /* ANSI Color coding sequences. */
  23. #define COL_GREEN "\e[1;32m"
  24. #define COL_RED "\e[0;31m"
  25. #define COL_STOP "\e[m"
  26. uint8_t* read_signature(char* input_file, int len)
  27. {
  28. int i, sigfd;
  29. uint8_t* signature = NULL;
  30. if ((sigfd = open(input_file, O_RDONLY)) == -1) {
  31. fprintf(stderr, "Couldn't open signature file\n");
  32. return NULL;
  33. }
  34. /* Read the signature into a buffer*/
  35. signature = (uint8_t*) malloc(len);
  36. if (!signature) {
  37. close(sigfd);
  38. return NULL;
  39. }
  40. if( (i = read(sigfd, signature, len)) != len ) {
  41. fprintf(stderr, "Expected signature length %d, Received %d\n",
  42. len, i);
  43. close(sigfd);
  44. free(signature);
  45. return NULL;
  46. }
  47. close(sigfd);
  48. return signature;
  49. }
  50. int main(int argc, char* argv[])
  51. {
  52. uint8_t workbuf[VB2_VERIFY_DIGEST_WORKBUF_BYTES]
  53. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  54. struct vb2_workbuf wb;
  55. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  56. int return_code = 1; /* Default to error. */
  57. uint8_t digest[VB2_MAX_DIGEST_SIZE];
  58. struct vb2_packed_key *pk = NULL;
  59. uint8_t *signature = NULL;
  60. uint32_t sig_len = 0;
  61. if (argc != 5) {
  62. int i;
  63. fprintf(stderr,
  64. "Usage: %s <algorithm> <key file> <signature file>"
  65. " <input file>\n\n", argv[0]);
  66. fprintf(stderr,
  67. "where <algorithm> depends on the signature algorithm"
  68. " used:\n");
  69. for(i = 0; i < VB2_ALG_COUNT; i++)
  70. fprintf(stderr, "\t%d for %s\n", i,
  71. vb2_get_crypto_algorithm_name(i));
  72. return -1;
  73. }
  74. int algorithm = atoi(argv[1]);
  75. if (algorithm >= VB2_ALG_COUNT) {
  76. fprintf(stderr, "Invalid algorithm %d\n", algorithm);
  77. goto error;
  78. }
  79. pk = vb2_read_packed_keyb(argv[2], algorithm, 0);
  80. if (!pk) {
  81. fprintf(stderr, "Can't read RSA public key.\n");
  82. goto error;
  83. }
  84. struct vb2_public_key k2;
  85. if (VB2_SUCCESS != vb2_unpack_key(&k2, pk)) {
  86. fprintf(stderr, "Can't unpack RSA public key.\n");
  87. goto error;
  88. }
  89. if (VB2_SUCCESS != vb2_read_file(argv[3], &signature, &sig_len)) {
  90. fprintf(stderr, "Can't read signature.\n");
  91. goto error;
  92. }
  93. uint32_t expect_sig_size =
  94. vb2_rsa_sig_size(vb2_crypto_to_signature(algorithm));
  95. if (sig_len != expect_sig_size) {
  96. fprintf(stderr, "Expected signature size %u, got %u\n",
  97. expect_sig_size, sig_len);
  98. goto error;
  99. }
  100. if (VB2_SUCCESS != DigestFile(argv[4], vb2_crypto_to_hash(algorithm),
  101. digest, sizeof(digest))) {
  102. fprintf(stderr, "Error calculating digest.\n");
  103. goto error;
  104. }
  105. if (VB2_SUCCESS == vb2_rsa_verify_digest(&k2, signature, digest, &wb)) {
  106. return_code = 0;
  107. fprintf(stderr, "Signature Verification "
  108. COL_GREEN "SUCCEEDED" COL_STOP "\n");
  109. } else {
  110. fprintf(stderr, "Signature Verification "
  111. COL_RED "FAILED" COL_STOP "\n");
  112. }
  113. error:
  114. if (pk)
  115. free(pk);
  116. if (signature)
  117. free(signature);
  118. return return_code;
  119. }