vb20_rsa_padding_tests.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <stdint.h>
  6. #include <stdio.h>
  7. #include "file_keys.h"
  8. #include "rsa_padding_test.h"
  9. #include "test_common.h"
  10. #include "utility.h"
  11. #include "2sysincludes.h"
  12. #include "2rsa.h"
  13. #include "host_key.h"
  14. #include "vb2_common.h"
  15. /**
  16. * Test valid and invalid signatures.
  17. */
  18. static void test_signatures(const struct vb2_public_key *key)
  19. {
  20. uint8_t workbuf[VB2_VERIFY_DIGEST_WORKBUF_BYTES]
  21. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  22. uint8_t sig[RSA1024NUMBYTES];
  23. struct vb2_workbuf wb;
  24. int unexpected_success;
  25. int i;
  26. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  27. /* The first test signature is valid. */
  28. memcpy(sig, signatures[0], sizeof(sig));
  29. TEST_SUCC(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  30. "RSA Padding Test valid sig");
  31. /* All other signatures should fail verification. */
  32. unexpected_success = 0;
  33. for (i = 1; i < sizeof(signatures) / sizeof(signatures[0]); i++) {
  34. memcpy(sig, signatures[i], sizeof(sig));
  35. if (!vb2_rsa_verify_digest(key, sig,
  36. test_message_sha1_hash, &wb)) {
  37. fprintf(stderr,
  38. "RSA Padding Test vector %d FAILED!\n", i);
  39. unexpected_success++;
  40. }
  41. }
  42. TEST_EQ(unexpected_success, 0, "RSA Padding Test invalid sigs");
  43. }
  44. /**
  45. * Test other error conditions in vb2_rsa_verify_digest().
  46. */
  47. static void test_verify_digest(struct vb2_public_key *key) {
  48. uint8_t workbuf[VB2_VERIFY_DIGEST_WORKBUF_BYTES]
  49. __attribute__ ((aligned (VB2_WORKBUF_ALIGN)));
  50. uint8_t sig[RSA1024NUMBYTES];
  51. struct vb2_workbuf wb;
  52. enum vb2_signature_algorithm orig_key_alg = key->sig_alg;
  53. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  54. memcpy(sig, signatures[0], sizeof(sig));
  55. TEST_SUCC(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  56. "vb2_rsa_verify_digest() good");
  57. memcpy(sig, signatures[0], sizeof(sig));
  58. vb2_workbuf_init(&wb, workbuf, sizeof(sig) * 3 - 1);
  59. TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  60. VB2_ERROR_RSA_VERIFY_WORKBUF,
  61. "vb2_rsa_verify_digest() small workbuf");
  62. vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
  63. key->sig_alg = VB2_SIG_INVALID;
  64. memcpy(sig, signatures[0], sizeof(sig));
  65. TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  66. VB2_ERROR_RSA_VERIFY_ALGORITHM,
  67. "vb2_rsa_verify_digest() bad key alg");
  68. key->sig_alg = orig_key_alg;
  69. key->arrsize *= 2;
  70. memcpy(sig, signatures[0], sizeof(sig));
  71. TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  72. VB2_ERROR_RSA_VERIFY_SIG_LEN,
  73. "vb2_rsa_verify_digest() bad sig len");
  74. key->arrsize /= 2;
  75. /* Corrupt the signature near start and end */
  76. memcpy(sig, signatures[0], sizeof(sig));
  77. sig[3] ^= 0x42;
  78. TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  79. VB2_ERROR_RSA_PADDING, "vb2_rsa_verify_digest() bad sig");
  80. memcpy(sig, signatures[0], sizeof(sig));
  81. sig[RSA1024NUMBYTES - 3] ^= 0x56;
  82. TEST_EQ(vb2_rsa_verify_digest(key, sig, test_message_sha1_hash, &wb),
  83. VB2_ERROR_RSA_PADDING, "vb2_rsa_verify_digest() bad sig end");
  84. }
  85. int main(int argc, char *argv[])
  86. {
  87. struct vb2_public_key k2;
  88. struct vb2_packed_key *pk;
  89. /* Read test key */
  90. if (argc != 2) {
  91. fprintf(stderr, "Usage: %s <test public key>\n", argv[0]);
  92. return 1;
  93. }
  94. pk = vb2_read_packed_keyb(argv[1], VB2_ALG_RSA1024_SHA1, 0);
  95. if (!pk) {
  96. fprintf(stderr, "Couldn't read RSA public key for the test.\n");
  97. return 1;
  98. }
  99. if (VB2_SUCCESS != vb2_unpack_key(&k2, pk)) {
  100. fprintf(stderr, "Couldn't unpack RSA public key.\n");
  101. free(pk);
  102. return 1;
  103. }
  104. /* Run tests */
  105. test_signatures(&k2);
  106. test_verify_digest(&k2);
  107. /* Clean up and exit */
  108. free(pk);
  109. return gTestSuccess ? 0 : 255;
  110. }