pk11_rsaencrypt_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include <stdint.h>
  7. #include "cpputil.h"
  8. #include "cryptohi.h"
  9. #include "gtest/gtest.h"
  10. #include "limits.h"
  11. #include "nss.h"
  12. #include "nss_scoped_ptrs.h"
  13. #include "pk11pub.h"
  14. #include "testvectors/rsa_pkcs1_2048_test-vectors.h"
  15. #include "testvectors/rsa_pkcs1_3072_test-vectors.h"
  16. #include "testvectors/rsa_pkcs1_4096_test-vectors.h"
  17. namespace nss_test {
  18. class RsaDecryptWycheproofTest
  19. : public ::testing::TestWithParam<RsaDecryptTestVector> {
  20. protected:
  21. void TestDecrypt(const RsaDecryptTestVector vec) {
  22. SECItem pkcs8_item = {siBuffer, toUcharPtr(vec.priv_key.data()),
  23. static_cast<unsigned int>(vec.priv_key.size())};
  24. ScopedPK11SlotInfo slot(PK11_GetInternalKeySlot());
  25. EXPECT_NE(nullptr, slot);
  26. SECKEYPrivateKey* key = nullptr;
  27. SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(
  28. slot.get(), &pkcs8_item, nullptr, nullptr, false, false, KU_ALL, &key,
  29. nullptr);
  30. ASSERT_EQ(SECSuccess, rv);
  31. ASSERT_NE(nullptr, key);
  32. ScopedSECKEYPrivateKey priv_key(key);
  33. // Decrypt
  34. std::vector<uint8_t> decrypted(PR_MAX(1, vec.ct.size()));
  35. unsigned int decrypted_len = 0;
  36. rv = PK11_PrivDecryptPKCS1(priv_key.get(), decrypted.data(), &decrypted_len,
  37. decrypted.size(), vec.ct.data(), vec.ct.size());
  38. // RSA_DecryptBlock returns SECFailure with an empty message.
  39. if (vec.valid && vec.msg.size()) {
  40. EXPECT_EQ(SECSuccess, rv);
  41. decrypted.resize(decrypted_len);
  42. EXPECT_EQ(vec.msg, decrypted);
  43. } else {
  44. EXPECT_EQ(SECFailure, rv);
  45. }
  46. };
  47. };
  48. TEST_P(RsaDecryptWycheproofTest, Pkcs1Decrypt) { TestDecrypt(GetParam()); }
  49. INSTANTIATE_TEST_CASE_P(WycheproofRsa2048DecryptTest, RsaDecryptWycheproofTest,
  50. ::testing::ValuesIn(kRsa2048DecryptWycheproofVectors));
  51. INSTANTIATE_TEST_CASE_P(WycheproofRsa3072DecryptTest, RsaDecryptWycheproofTest,
  52. ::testing::ValuesIn(kRsa3072DecryptWycheproofVectors));
  53. INSTANTIATE_TEST_CASE_P(WycheproofRsa4096DecryptTest, RsaDecryptWycheproofTest,
  54. ::testing::ValuesIn(kRsa4096DecryptWycheproofVectors));
  55. TEST(RsaEncryptTest, MessageLengths) {
  56. const uint8_t spki[] = {
  57. 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  58. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81,
  59. 0x89, 0x02, 0x81, 0x81, 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9,
  60. 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb,
  61. 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0,
  62. 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef,
  63. 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa,
  64. 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76,
  65. 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25,
  66. 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79,
  67. 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49,
  68. 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd,
  69. 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61,
  70. 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  71. };
  72. // Import public key (use pre-generated for performance).
  73. SECItem spki_item = {siBuffer, toUcharPtr(spki), sizeof(spki)};
  74. ScopedCERTSubjectPublicKeyInfo cert_spki(
  75. SECKEY_DecodeDERSubjectPublicKeyInfo(&spki_item));
  76. ASSERT_TRUE(cert_spki);
  77. ScopedSECKEYPublicKey pub_key(SECKEY_ExtractPublicKey(cert_spki.get()));
  78. ASSERT_TRUE(pub_key);
  79. int mod_len = SECKEY_PublicKeyStrength(pub_key.get());
  80. ASSERT_TRUE(mod_len > 0);
  81. std::vector<uint8_t> ctxt(mod_len);
  82. unsigned int ctxt_len;
  83. std::vector<uint8_t> msg(mod_len, 0xff);
  84. // Test with valid inputs
  85. SECStatus rv =
  86. PK11_PubEncrypt(pub_key.get(), CKM_RSA_PKCS, nullptr, ctxt.data(),
  87. &ctxt_len, mod_len, msg.data(), 1, nullptr);
  88. ASSERT_EQ(SECSuccess, rv);
  89. // Maximum message length is mod_len - miniumum padding (8B) - flags (3B)
  90. unsigned int max_msg_len = mod_len - 8 - 3;
  91. rv = PK11_PubEncrypt(pub_key.get(), CKM_RSA_PKCS, nullptr, ctxt.data(),
  92. &ctxt_len, mod_len, msg.data(), max_msg_len, nullptr);
  93. ASSERT_EQ(SECSuccess, rv);
  94. // Test one past maximum length
  95. rv =
  96. PK11_PubEncrypt(pub_key.get(), CKM_RSA_PKCS, nullptr, ctxt.data(),
  97. &ctxt_len, mod_len, msg.data(), max_msg_len + 1, nullptr);
  98. ASSERT_EQ(SECFailure, rv);
  99. // Make sure the the length will not overflow - i.e.
  100. // (padLen = modulusLen - (UINT_MAX + MINIMUM_PAD_LEN)) may overflow and
  101. // result in a value that appears valid.
  102. rv = PK11_PubEncrypt(pub_key.get(), CKM_RSA_PKCS, nullptr, ctxt.data(),
  103. &ctxt_len, UINT_MAX, msg.data(), UINT_MAX, nullptr);
  104. ASSERT_EQ(SECFailure, rv);
  105. }
  106. } // namespace nss_test