Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

303 rader
9.8 KiB

  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 <openssl/cipher.h>
  58. #include <openssl/crypto.h>
  59. #include <openssl/err.h>
  60. #include "../test/file_test.h"
  61. namespace bssl {
  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-EDE-CBC") {
  70. return EVP_des_ede_cbc();
  71. } else if (name == "DES-EDE3-CBC") {
  72. return EVP_des_ede3_cbc();
  73. } else if (name == "RC4") {
  74. return EVP_rc4();
  75. } else if (name == "AES-128-ECB") {
  76. return EVP_aes_128_ecb();
  77. } else if (name == "AES-256-ECB") {
  78. return EVP_aes_256_ecb();
  79. } else if (name == "AES-128-CBC") {
  80. return EVP_aes_128_cbc();
  81. } else if (name == "AES-128-GCM") {
  82. return EVP_aes_128_gcm();
  83. } else if (name == "AES-128-OFB") {
  84. return EVP_aes_128_ofb();
  85. } else if (name == "AES-192-CBC") {
  86. return EVP_aes_192_cbc();
  87. } else if (name == "AES-192-ECB") {
  88. return EVP_aes_192_ecb();
  89. } else if (name == "AES-256-CBC") {
  90. return EVP_aes_256_cbc();
  91. } else if (name == "AES-128-CTR") {
  92. return EVP_aes_128_ctr();
  93. } else if (name == "AES-256-CTR") {
  94. return EVP_aes_256_ctr();
  95. } else if (name == "AES-256-GCM") {
  96. return EVP_aes_256_gcm();
  97. } else if (name == "AES-256-OFB") {
  98. return EVP_aes_256_ofb();
  99. }
  100. return nullptr;
  101. }
  102. static bool TestOperation(FileTest *t,
  103. const EVP_CIPHER *cipher,
  104. bool encrypt,
  105. size_t chunk_size,
  106. const std::vector<uint8_t> &key,
  107. const std::vector<uint8_t> &iv,
  108. const std::vector<uint8_t> &plaintext,
  109. const std::vector<uint8_t> &ciphertext,
  110. const std::vector<uint8_t> &aad,
  111. const std::vector<uint8_t> &tag) {
  112. const std::vector<uint8_t> *in, *out;
  113. if (encrypt) {
  114. in = &plaintext;
  115. out = &ciphertext;
  116. } else {
  117. in = &ciphertext;
  118. out = &plaintext;
  119. }
  120. bool is_aead = EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE;
  121. ScopedEVP_CIPHER_CTX ctx;
  122. if (!EVP_CipherInit_ex(ctx.get(), cipher, nullptr, nullptr, nullptr,
  123. encrypt ? 1 : 0)) {
  124. return false;
  125. }
  126. if (t->HasAttribute("IV")) {
  127. if (is_aead) {
  128. if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN,
  129. iv.size(), 0)) {
  130. return false;
  131. }
  132. } else if (iv.size() != EVP_CIPHER_CTX_iv_length(ctx.get())) {
  133. t->PrintLine("Bad IV length.");
  134. return false;
  135. }
  136. }
  137. if (is_aead && !encrypt &&
  138. !EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, tag.size(),
  139. const_cast<uint8_t*>(tag.data()))) {
  140. return false;
  141. }
  142. // The ciphers are run with no padding. For each of the ciphers we test, the
  143. // output size matches the input size.
  144. std::vector<uint8_t> result(in->size());
  145. if (in->size() != out->size()) {
  146. t->PrintLine("Input/output size mismatch (%u vs %u).", (unsigned)in->size(),
  147. (unsigned)out->size());
  148. return false;
  149. }
  150. // Note: the deprecated |EVP_CIPHER|-based AES-GCM API is sensitive to whether
  151. // parameters are NULL, so it is important to skip the |in| and |aad|
  152. // |EVP_CipherUpdate| calls when empty.
  153. int unused, result_len1 = 0, result_len2;
  154. if (!EVP_CIPHER_CTX_set_key_length(ctx.get(), key.size()) ||
  155. !EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data(),
  156. -1) ||
  157. (!aad.empty() &&
  158. !EVP_CipherUpdate(ctx.get(), nullptr, &unused, aad.data(),
  159. aad.size())) ||
  160. !EVP_CIPHER_CTX_set_padding(ctx.get(), 0)) {
  161. t->PrintLine("Operation failed.");
  162. return false;
  163. }
  164. if (chunk_size != 0) {
  165. for (size_t i = 0; i < in->size();) {
  166. size_t todo = chunk_size;
  167. if (i + todo > in->size()) {
  168. todo = in->size() - i;
  169. }
  170. int len;
  171. if (!EVP_CipherUpdate(ctx.get(), result.data() + result_len1, &len,
  172. in->data() + i, todo)) {
  173. t->PrintLine("Operation failed.");
  174. return false;
  175. }
  176. result_len1 += len;
  177. i += todo;
  178. }
  179. } else if (!in->empty() &&
  180. !EVP_CipherUpdate(ctx.get(), result.data(), &result_len1,
  181. in->data(), in->size())) {
  182. t->PrintLine("Operation failed.");
  183. return false;
  184. }
  185. if (!EVP_CipherFinal_ex(ctx.get(), result.data() + result_len1,
  186. &result_len2)) {
  187. t->PrintLine("Operation failed.");
  188. return false;
  189. }
  190. result.resize(result_len1 + result_len2);
  191. if (!t->ExpectBytesEqual(out->data(), out->size(), result.data(),
  192. result.size())) {
  193. return false;
  194. }
  195. if (encrypt && is_aead) {
  196. uint8_t rtag[16];
  197. if (tag.size() > sizeof(rtag)) {
  198. t->PrintLine("Bad tag length.");
  199. return false;
  200. }
  201. if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, tag.size(),
  202. rtag) ||
  203. !t->ExpectBytesEqual(tag.data(), tag.size(), rtag,
  204. tag.size())) {
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. static bool TestCipher(FileTest *t, void *arg) {
  211. std::string cipher_str;
  212. if (!t->GetAttribute(&cipher_str, "Cipher")) {
  213. return false;
  214. }
  215. const EVP_CIPHER *cipher = GetCipher(cipher_str);
  216. if (cipher == nullptr) {
  217. t->PrintLine("Unknown cipher: '%s'.", cipher_str.c_str());
  218. return false;
  219. }
  220. std::vector<uint8_t> key, iv, plaintext, ciphertext, aad, tag;
  221. if (!t->GetBytes(&key, "Key") ||
  222. !t->GetBytes(&plaintext, "Plaintext") ||
  223. !t->GetBytes(&ciphertext, "Ciphertext")) {
  224. return false;
  225. }
  226. if (EVP_CIPHER_iv_length(cipher) > 0 &&
  227. !t->GetBytes(&iv, "IV")) {
  228. return false;
  229. }
  230. if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) {
  231. if (!t->GetBytes(&aad, "AAD") ||
  232. !t->GetBytes(&tag, "Tag")) {
  233. return false;
  234. }
  235. }
  236. enum {
  237. kEncrypt,
  238. kDecrypt,
  239. kBoth,
  240. } operation = kBoth;
  241. if (t->HasAttribute("Operation")) {
  242. const std::string &str = t->GetAttributeOrDie("Operation");
  243. if (str == "ENCRYPT") {
  244. operation = kEncrypt;
  245. } else if (str == "DECRYPT") {
  246. operation = kDecrypt;
  247. } else {
  248. t->PrintLine("Unknown operation: '%s'.", str.c_str());
  249. return false;
  250. }
  251. }
  252. const std::vector<size_t> chunk_sizes = {0, 1, 2, 5, 7, 8, 9, 15, 16,
  253. 17, 31, 32, 33, 63, 64, 65, 512};
  254. for (size_t chunk_size : chunk_sizes) {
  255. // By default, both directions are run, unless overridden by the operation.
  256. if (operation != kDecrypt &&
  257. !TestOperation(t, cipher, true /* encrypt */, chunk_size, key, iv,
  258. plaintext, ciphertext, aad, tag)) {
  259. return false;
  260. }
  261. if (operation != kEncrypt &&
  262. !TestOperation(t, cipher, false /* decrypt */, chunk_size, key, iv,
  263. plaintext, ciphertext, aad, tag)) {
  264. return false;
  265. }
  266. }
  267. return true;
  268. }
  269. static int Main(int argc, char **argv) {
  270. CRYPTO_library_init();
  271. if (argc != 2) {
  272. fprintf(stderr, "%s <test file>\n", argv[0]);
  273. return 1;
  274. }
  275. return FileTestMain(TestCipher, nullptr, argv[1]);
  276. }
  277. } // namespace bssl
  278. int main(int argc, char **argv) {
  279. return bssl::Main(argc, argv);
  280. }