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.
 
 
 
 
 
 

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