You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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