選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cipher_test.cc 9.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <string>
  56. #include <vector>
  57. #include <gtest/gtest.h>
  58. #include <openssl/cipher.h>
  59. #include <openssl/err.h>
  60. #include "../test/file_test.h"
  61. #include "../test/test_util.h"
  62. static const EVP_CIPHER *GetCipher(const std::string &name) {
  63. if (name == "DES-CBC") {
  64. return EVP_des_cbc();
  65. } else if (name == "DES-ECB") {
  66. return EVP_des_ecb();
  67. } else if (name == "DES-EDE") {
  68. return EVP_des_ede();
  69. } else if (name == "DES-EDE3") {
  70. return EVP_des_ede3();
  71. } else if (name == "DES-EDE-CBC") {
  72. return EVP_des_ede_cbc();
  73. } else if (name == "DES-EDE3-CBC") {
  74. return EVP_des_ede3_cbc();
  75. } else if (name == "RC4") {
  76. return EVP_rc4();
  77. } else if (name == "AES-128-ECB") {
  78. return EVP_aes_128_ecb();
  79. } else if (name == "AES-256-ECB") {
  80. return EVP_aes_256_ecb();
  81. } else if (name == "AES-128-CBC") {
  82. return EVP_aes_128_cbc();
  83. } else if (name == "AES-128-GCM") {
  84. return EVP_aes_128_gcm();
  85. } else if (name == "AES-128-OFB") {
  86. return EVP_aes_128_ofb();
  87. } else if (name == "AES-192-CBC") {
  88. return EVP_aes_192_cbc();
  89. } else if (name == "AES-192-CTR") {
  90. return EVP_aes_192_ctr();
  91. } else if (name == "AES-192-ECB") {
  92. return EVP_aes_192_ecb();
  93. } else if (name == "AES-256-CBC") {
  94. return EVP_aes_256_cbc();
  95. } else if (name == "AES-128-CTR") {
  96. return EVP_aes_128_ctr();
  97. } else if (name == "AES-256-CTR") {
  98. return EVP_aes_256_ctr();
  99. } else if (name == "AES-256-GCM") {
  100. return EVP_aes_256_gcm();
  101. } else if (name == "AES-256-OFB") {
  102. return EVP_aes_256_ofb();
  103. }
  104. return nullptr;
  105. }
  106. static void TestOperation(FileTest *t, const EVP_CIPHER *cipher, bool encrypt,
  107. size_t chunk_size, const std::vector<uint8_t> &key,
  108. const std::vector<uint8_t> &iv,
  109. const std::vector<uint8_t> &plaintext,
  110. const std::vector<uint8_t> &ciphertext,
  111. const std::vector<uint8_t> &aad,
  112. const std::vector<uint8_t> &tag) {
  113. const std::vector<uint8_t> *in, *out;
  114. if (encrypt) {
  115. in = &plaintext;
  116. out = &ciphertext;
  117. } else {
  118. in = &ciphertext;
  119. out = &plaintext;
  120. }
  121. bool is_aead = EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE;
  122. bssl::ScopedEVP_CIPHER_CTX ctx;
  123. ASSERT_TRUE(EVP_CipherInit_ex(ctx.get(), cipher, nullptr, nullptr, nullptr,
  124. encrypt ? 1 : 0));
  125. if (t->HasAttribute("IV")) {
  126. if (is_aead) {
  127. ASSERT_TRUE(
  128. EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, iv.size(), 0));
  129. } else {
  130. ASSERT_EQ(iv.size(), EVP_CIPHER_CTX_iv_length(ctx.get()));
  131. }
  132. }
  133. if (is_aead && !encrypt) {
  134. ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, tag.size(),
  135. const_cast<uint8_t *>(tag.data())));
  136. }
  137. // The ciphers are run with no padding. For each of the ciphers we test, the
  138. // output size matches the input size.
  139. std::vector<uint8_t> result(in->size());
  140. ASSERT_EQ(in->size(), out->size());
  141. int unused, result_len1 = 0, result_len2;
  142. ASSERT_TRUE(EVP_CIPHER_CTX_set_key_length(ctx.get(), key.size()));
  143. ASSERT_TRUE(EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key.data(),
  144. iv.data(), -1));
  145. // Note: the deprecated |EVP_CIPHER|-based AES-GCM API is sensitive to whether
  146. // parameters are NULL, so it is important to skip the |in| and |aad|
  147. // |EVP_CipherUpdate| calls when empty.
  148. if (!aad.empty()) {
  149. ASSERT_TRUE(
  150. EVP_CipherUpdate(ctx.get(), nullptr, &unused, aad.data(), aad.size()));
  151. }
  152. ASSERT_TRUE(EVP_CIPHER_CTX_set_padding(ctx.get(), 0));
  153. if (chunk_size != 0) {
  154. for (size_t i = 0; i < in->size();) {
  155. size_t todo = chunk_size;
  156. if (i + todo > in->size()) {
  157. todo = in->size() - i;
  158. }
  159. int len;
  160. ASSERT_TRUE(EVP_CipherUpdate(ctx.get(), result.data() + result_len1, &len,
  161. in->data() + i, todo));
  162. result_len1 += len;
  163. i += todo;
  164. }
  165. } else if (!in->empty()) {
  166. ASSERT_TRUE(EVP_CipherUpdate(ctx.get(), result.data(), &result_len1,
  167. in->data(), in->size()));
  168. }
  169. ASSERT_TRUE(
  170. EVP_CipherFinal_ex(ctx.get(), result.data() + result_len1, &result_len2));
  171. result.resize(result_len1 + result_len2);
  172. EXPECT_EQ(Bytes(*out), Bytes(result));
  173. if (encrypt && is_aead) {
  174. uint8_t rtag[16];
  175. ASSERT_LE(tag.size(), sizeof(rtag));
  176. ASSERT_TRUE(
  177. EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, tag.size(), rtag));
  178. EXPECT_EQ(Bytes(tag), Bytes(rtag, tag.size()));
  179. }
  180. }
  181. static void TestCipher(FileTest *t) {
  182. std::string cipher_str;
  183. ASSERT_TRUE(t->GetAttribute(&cipher_str, "Cipher"));
  184. const EVP_CIPHER *cipher = GetCipher(cipher_str);
  185. ASSERT_TRUE(cipher);
  186. std::vector<uint8_t> key, iv, plaintext, ciphertext, aad, tag;
  187. ASSERT_TRUE(t->GetBytes(&key, "Key"));
  188. ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext"));
  189. ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext"));
  190. if (EVP_CIPHER_iv_length(cipher) > 0) {
  191. ASSERT_TRUE(t->GetBytes(&iv, "IV"));
  192. }
  193. if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) {
  194. ASSERT_TRUE(t->GetBytes(&aad, "AAD"));
  195. ASSERT_TRUE(t->GetBytes(&tag, "Tag"));
  196. }
  197. enum {
  198. kEncrypt,
  199. kDecrypt,
  200. kBoth,
  201. } operation = kBoth;
  202. if (t->HasAttribute("Operation")) {
  203. const std::string &str = t->GetAttributeOrDie("Operation");
  204. if (str == "ENCRYPT") {
  205. operation = kEncrypt;
  206. } else if (str == "DECRYPT") {
  207. operation = kDecrypt;
  208. } else {
  209. FAIL() << "Unknown operation: " << str;
  210. }
  211. }
  212. const std::vector<size_t> chunk_sizes = {0, 1, 2, 5, 7, 8, 9, 15, 16,
  213. 17, 31, 32, 33, 63, 64, 65, 512};
  214. for (size_t chunk_size : chunk_sizes) {
  215. SCOPED_TRACE(chunk_size);
  216. // By default, both directions are run, unless overridden by the operation.
  217. if (operation != kDecrypt) {
  218. SCOPED_TRACE("encrypt");
  219. TestOperation(t, cipher, true /* encrypt */, chunk_size, key, iv,
  220. plaintext, ciphertext, aad, tag);
  221. }
  222. if (operation != kEncrypt) {
  223. SCOPED_TRACE("decrypt");
  224. TestOperation(t, cipher, false /* decrypt */, chunk_size, key, iv,
  225. plaintext, ciphertext, aad, tag);
  226. }
  227. }
  228. }
  229. TEST(CipherTest, TestVectors) {
  230. FileTestGTest("crypto/cipher_extra/test/cipher_tests.txt", TestCipher);
  231. }
  232. TEST(CipherTest, CAVP_AES_128_CBC) {
  233. FileTestGTest("crypto/cipher_extra/test/nist_cavp/aes_128_cbc.txt",
  234. TestCipher);
  235. }
  236. TEST(CipherTest, CAVP_AES_128_CTR) {
  237. FileTestGTest("crypto/cipher_extra/test/nist_cavp/aes_128_ctr.txt",
  238. TestCipher);
  239. }
  240. TEST(CipherTest, CAVP_AES_192_CBC) {
  241. FileTestGTest("crypto/cipher_extra/test/nist_cavp/aes_192_cbc.txt",
  242. TestCipher);
  243. }
  244. TEST(CipherTest, CAVP_AES_192_CTR) {
  245. FileTestGTest("crypto/cipher_extra/test/nist_cavp/aes_192_ctr.txt",
  246. TestCipher);
  247. }
  248. TEST(CipherTest, CAVP_AES_256_CBC) {
  249. FileTestGTest("crypto/cipher_extra/test/nist_cavp/aes_256_cbc.txt",
  250. TestCipher);
  251. }
  252. TEST(CipherTest, CAVP_AES_256_CTR) {
  253. FileTestGTest("crypto/cipher_extra/test/nist_cavp/aes_256_ctr.txt",
  254. TestCipher);
  255. }
  256. TEST(CipherTest, CAVP_TDES_CBC) {
  257. FileTestGTest("crypto/cipher_extra/test/nist_cavp/tdes_cbc.txt", TestCipher);
  258. }
  259. TEST(CipherTest, CAVP_TDES_ECB) {
  260. FileTestGTest("crypto/cipher_extra/test/nist_cavp/tdes_ecb.txt", TestCipher);
  261. }