vb2_rsa_utility_tests.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (c) 2014 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 "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2rsa.h"
  10. #include "file_keys.h"
  11. #include "rsa_padding_test.h"
  12. #include "test_common.h"
  13. #include "utility.h"
  14. #include "vboot_api.h"
  15. /*
  16. * Internal functions from 2rsa.c that have error conditions we can't trigger
  17. * from the public APIs. These include checks for bad algorithms where the
  18. * next call level up already checks for bad algorithms, etc.
  19. *
  20. * These functions aren't in 2rsa.h because they're not part of the public
  21. * APIs.
  22. */
  23. int vb2_mont_ge(const struct vb2_public_key *key, uint32_t *a);
  24. int vb2_check_padding(const uint8_t *sig, const struct vb2_public_key *key);
  25. /**
  26. * Test RSA utility funcs
  27. */
  28. static void test_utils(void)
  29. {
  30. uint8_t sig[RSA1024NUMBYTES];
  31. struct vb2_public_key kbad = {.sig_alg = VB2_SIG_INVALID,
  32. .hash_alg = VB2_HASH_INVALID};
  33. /* Verify old and new algorithm count constants match */
  34. TEST_EQ(VB2_ALG_COUNT, VB2_ALG_COUNT, "Algorithm counts");
  35. /* Crypto algorithm to sig algorithm mapping */
  36. TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA1024_SHA1),
  37. VB2_SIG_RSA1024, "Crypto map to RSA1024");
  38. TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA2048_SHA256),
  39. VB2_SIG_RSA2048, "Crypto map to RSA2048");
  40. TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA4096_SHA256),
  41. VB2_SIG_RSA4096, "Crypto map to RSA4096");
  42. TEST_EQ(vb2_crypto_to_signature(VB2_ALG_RSA8192_SHA512),
  43. VB2_SIG_RSA8192, "Crypto map to RSA8192");
  44. TEST_EQ(vb2_crypto_to_signature(VB2_ALG_COUNT),
  45. VB2_SIG_INVALID, "Crypto map to invalid");
  46. /* Sig size */
  47. TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA1024), RSA1024NUMBYTES,
  48. "Sig size RSA1024");
  49. TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA2048), RSA2048NUMBYTES,
  50. "Sig size RSA2048");
  51. TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA4096), RSA4096NUMBYTES,
  52. "Sig size RSA4096");
  53. TEST_EQ(vb2_rsa_sig_size(VB2_SIG_RSA8192), RSA8192NUMBYTES,
  54. "Sig size RSA8192");
  55. TEST_EQ(vb2_rsa_sig_size(VB2_SIG_INVALID), 0,
  56. "Sig size invalid algorithm");
  57. TEST_EQ(vb2_rsa_sig_size(VB2_SIG_NONE), 0,
  58. "Sig size no signing algorithm");
  59. /* Packed key size */
  60. TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA1024),
  61. RSA1024NUMBYTES * 2 + sizeof(uint32_t) * 2,
  62. "Packed key size VB2_SIG_RSA1024");
  63. TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA2048),
  64. RSA2048NUMBYTES * 2 + sizeof(uint32_t) * 2,
  65. "Packed key size VB2_SIG_RSA2048");
  66. TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA4096),
  67. RSA4096NUMBYTES * 2 + sizeof(uint32_t) * 2,
  68. "Packed key size VB2_SIG_RSA4096");
  69. TEST_EQ(vb2_packed_key_size(VB2_SIG_RSA8192),
  70. RSA8192NUMBYTES * 2 + sizeof(uint32_t) * 2,
  71. "Packed key size VB2_SIG_RSA8192");
  72. TEST_EQ(vb2_packed_key_size(VB2_SIG_INVALID), 0,
  73. "Packed key size invalid algorithm");
  74. TEST_EQ(vb2_packed_key_size(VB2_SIG_NONE), 0,
  75. "Packed key size no signing algorithm");
  76. /* Test padding check with bad algorithm */
  77. memcpy(sig, signatures[0], sizeof(sig));
  78. TEST_EQ(vb2_check_padding(sig, &kbad),
  79. VB2_ERROR_RSA_PADDING_SIZE,
  80. "vb2_check_padding() bad padding algorithm/size");
  81. /* Test safe memcmp */
  82. TEST_EQ(vb2_safe_memcmp("foo", "foo", 3), 0, "vb2_safe_memcmp() good");
  83. TEST_NEQ(vb2_safe_memcmp("foo", "bar", 3), 0, "vb2_safe_memcmp() bad");
  84. TEST_EQ(vb2_safe_memcmp("foo", "bar", 0), 0, "vb2_safe_memcmp() zero");
  85. /* Test Montgomery >= */
  86. {
  87. uint32_t n[4] = {4, 4, 4, 4};
  88. uint32_t a[4] = {4, 4, 4, 4};
  89. struct vb2_public_key k = {
  90. .arrsize = 4,
  91. .n = n,
  92. };
  93. TEST_EQ(vb2_mont_ge(&k, a), 1, "mont_ge equal");
  94. a[2] = 3;
  95. TEST_EQ(vb2_mont_ge(&k, a), 0, "mont_ge less");
  96. a[1] = 5;
  97. TEST_EQ(vb2_mont_ge(&k, a), 0, "mont_ge greater");
  98. }
  99. }
  100. int main(int argc, char* argv[])
  101. {
  102. /* Run tests */
  103. test_utils();
  104. return gTestSuccess ? 0 : 255;
  105. }