Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

164 lignes
6.7 KiB

  1. // Copyright (c) 2019, Google Inc.
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. #include <openssl/cipher.h>
  15. #include <gtest/gtest.h>
  16. #include "../../crypto/internal.h"
  17. #include "../../crypto/test/test_util.h"
  18. struct TestCase {
  19. uint8_t key[16];
  20. uint8_t plaintext[16];
  21. uint8_t iv[8];
  22. uint8_t ecb_ciphertext[16];
  23. uint8_t cbc_ciphertext[24];
  24. uint8_t cfb_ciphertext[16];
  25. };
  26. static const TestCase kTests[] = {
  27. // Randomly generated test cases. Checked against vanilla OpenSSL.
  28. {
  29. {0xbb, 0x56, 0xb1, 0x27, 0x7c, 0x4c, 0xdd, 0x5a, 0x99, 0x90, 0x1e, 0x6f,
  30. 0xeb, 0x36, 0x6c, 0xf3},
  31. {0xa6, 0x5b, 0xe0, 0x99, 0xad, 0x5d, 0x91, 0x98, 0x37, 0xc1, 0xa4, 0x7f,
  32. 0x01, 0x24, 0x9a, 0x6b},
  33. {0xd5, 0x8a, 0x5c, 0x29, 0xeb, 0xee, 0xed, 0x76},
  34. {0xda, 0x6e, 0x18, 0x9c, 0x03, 0x59, 0x12, 0x61, 0xfa, 0x20, 0xd9, 0xce,
  35. 0xee, 0x43, 0x9d, 0x05},
  36. {0x4f, 0x8b, 0x3e, 0x17, 0xa5, 0x35, 0x9b, 0xcb,
  37. 0xdf, 0x3c, 0x52, 0xfb, 0xe5, 0x20, 0xdd, 0x53,
  38. 0xd5, 0xf8, 0x1a, 0x6c, 0xf0, 0x99, 0x34, 0x94},
  39. {0xfd, 0x65, 0xc5, 0xa6, 0x07, 0x07, 0xb5, 0xf3, 0x2e, 0xfb, 0x12, 0xc3,
  40. 0x7f, 0x45, 0x37, 0x54},
  41. },
  42. {
  43. {0x5d, 0x98, 0xa9, 0xd2, 0x27, 0x5d, 0xc8, 0x8c, 0x8c, 0xee, 0x23, 0x7f,
  44. 0x8e, 0x2b, 0xd4, 0x8d},
  45. {0x60, 0xec, 0x31, 0xda, 0x25, 0x07, 0x02, 0x14, 0x84, 0x44, 0x96, 0xa6,
  46. 0x04, 0x81, 0xca, 0x4e},
  47. {0x96, 0x4c, 0xa4, 0x07, 0xee, 0x1c, 0xd1, 0xfb},
  48. {0x83, 0x8a, 0xef, 0x18, 0x53, 0x96, 0xec, 0xf3, 0xf4, 0xd9, 0xe8, 0x4b,
  49. 0x2c, 0x3f, 0xe7, 0x27},
  50. {0xad, 0x78, 0x70, 0x06, 0x2e, 0x5e, 0xa5, 0x21,
  51. 0xdd, 0xe8, 0xa0, 0xb9, 0xdb, 0x0c, 0x81, 0x1d,
  52. 0x0a, 0xd3, 0xa9, 0x63, 0x78, 0xac, 0x82, 0x64},
  53. {0x43, 0x2f, 0xf3, 0x23, 0xf4, 0x5c, 0xbf, 0x05, 0x53, 0x3c, 0x9e, 0xd6,
  54. 0xd3, 0xd2, 0xc0, 0xf0},
  55. },
  56. };
  57. TEST(Blowfish, ECB) {
  58. unsigned test_num = 0;
  59. for (const auto &test : kTests) {
  60. test_num++;
  61. SCOPED_TRACE(test_num);
  62. uint8_t out[sizeof(test.ecb_ciphertext)];
  63. int out_bytes, final_bytes;
  64. bssl::ScopedEVP_CIPHER_CTX ctx;
  65. ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_bf_ecb(), nullptr, test.key,
  66. nullptr));
  67. ASSERT_TRUE(EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding */));
  68. ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
  69. sizeof(test.plaintext)));
  70. ASSERT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
  71. EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
  72. sizeof(test.plaintext));
  73. EXPECT_EQ(Bytes(test.ecb_ciphertext), Bytes(out));
  74. bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
  75. ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_bf_ecb(), nullptr,
  76. test.key, nullptr));
  77. ASSERT_TRUE(
  78. EVP_CIPHER_CTX_set_padding(decrypt_ctx.get(), 0 /* no padding */));
  79. ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
  80. test.ecb_ciphertext,
  81. sizeof(test.ecb_ciphertext)));
  82. ASSERT_TRUE(
  83. EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
  84. EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
  85. sizeof(test.plaintext));
  86. EXPECT_EQ(Bytes(test.plaintext), Bytes(out));
  87. }
  88. }
  89. TEST(Blowfish, CBC) {
  90. unsigned test_num = 0;
  91. for (const auto &test : kTests) {
  92. test_num++;
  93. SCOPED_TRACE(test_num);
  94. uint8_t out[sizeof(test.cbc_ciphertext)];
  95. int out_bytes, final_bytes;
  96. bssl::ScopedEVP_CIPHER_CTX ctx;
  97. ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_bf_cbc(), nullptr, test.key,
  98. test.iv));
  99. ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
  100. sizeof(test.plaintext)));
  101. EXPECT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
  102. EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
  103. sizeof(test.cbc_ciphertext));
  104. EXPECT_EQ(Bytes(test.cbc_ciphertext), Bytes(out));
  105. bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
  106. ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_bf_cbc(), nullptr,
  107. test.key, test.iv));
  108. ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
  109. test.cbc_ciphertext,
  110. sizeof(test.cbc_ciphertext)));
  111. EXPECT_TRUE(
  112. EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
  113. EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
  114. sizeof(test.plaintext));
  115. EXPECT_EQ(Bytes(test.plaintext), Bytes(out, out_bytes + final_bytes));
  116. }
  117. }
  118. TEST(Blowfish, CFB) {
  119. unsigned test_num = 0;
  120. for (const auto &test : kTests) {
  121. test_num++;
  122. SCOPED_TRACE(test_num);
  123. uint8_t out[sizeof(test.cfb_ciphertext)];
  124. int out_bytes, final_bytes;
  125. bssl::ScopedEVP_CIPHER_CTX ctx;
  126. ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_bf_cfb(), nullptr, test.key,
  127. test.iv));
  128. ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
  129. sizeof(test.plaintext)));
  130. ASSERT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
  131. EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
  132. sizeof(test.plaintext));
  133. EXPECT_EQ(Bytes(test.cfb_ciphertext), Bytes(out));
  134. bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
  135. ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_bf_cfb(), nullptr,
  136. test.key, test.iv));
  137. ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
  138. test.cfb_ciphertext,
  139. sizeof(test.cfb_ciphertext)));
  140. ASSERT_TRUE(
  141. EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
  142. EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
  143. sizeof(test.plaintext));
  144. EXPECT_EQ(Bytes(test.plaintext), Bytes(out));
  145. }
  146. }