Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

263 rindas
8.9 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. const std::vector<uint8_t> &key,
  101. const std::vector<uint8_t> &iv,
  102. const std::vector<uint8_t> &plaintext,
  103. const std::vector<uint8_t> &ciphertext,
  104. const std::vector<uint8_t> &aad,
  105. const std::vector<uint8_t> &tag) {
  106. const std::vector<uint8_t> *in, *out;
  107. if (encrypt) {
  108. in = &plaintext;
  109. out = &ciphertext;
  110. } else {
  111. in = &ciphertext;
  112. out = &plaintext;
  113. }
  114. bool is_aead = EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE;
  115. ScopedEVP_CIPHER_CTX ctx;
  116. if (!EVP_CipherInit_ex(ctx.get(), cipher, nullptr, nullptr, nullptr,
  117. encrypt ? 1 : 0)) {
  118. return false;
  119. }
  120. if (t->HasAttribute("IV")) {
  121. if (is_aead) {
  122. if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN,
  123. iv.size(), 0)) {
  124. return false;
  125. }
  126. } else if (iv.size() != (size_t)EVP_CIPHER_CTX_iv_length(ctx.get())) {
  127. t->PrintLine("Bad IV length.");
  128. return false;
  129. }
  130. }
  131. if (is_aead && !encrypt &&
  132. !EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, tag.size(),
  133. const_cast<uint8_t*>(bssl::vector_data(&tag)))) {
  134. return false;
  135. }
  136. // The ciphers are run with no padding. For each of the ciphers we test, the
  137. // output size matches the input size.
  138. std::vector<uint8_t> result(in->size());
  139. if (in->size() != out->size()) {
  140. t->PrintLine("Input/output size mismatch (%u vs %u).", (unsigned)in->size(),
  141. (unsigned)out->size());
  142. return false;
  143. }
  144. // Note: the deprecated |EVP_CIPHER|-based AES-GCM API is sensitive to whether
  145. // parameters are NULL, so it is important to skip the |in| and |aad|
  146. // |EVP_CipherUpdate| calls when empty.
  147. int unused, result_len1 = 0, result_len2;
  148. if (!EVP_CIPHER_CTX_set_key_length(ctx.get(), key.size()) ||
  149. !EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, bssl::vector_data(&key),
  150. bssl::vector_data(&iv), -1) ||
  151. (!aad.empty() &&
  152. !EVP_CipherUpdate(ctx.get(), nullptr, &unused, bssl::vector_data(&aad),
  153. aad.size())) ||
  154. !EVP_CIPHER_CTX_set_padding(ctx.get(), 0) ||
  155. (!in->empty() &&
  156. !EVP_CipherUpdate(ctx.get(), bssl::vector_data(&result), &result_len1,
  157. bssl::vector_data(in), in->size())) ||
  158. !EVP_CipherFinal_ex(ctx.get(), bssl::vector_data(&result) + result_len1,
  159. &result_len2)) {
  160. t->PrintLine("Operation failed.");
  161. return false;
  162. }
  163. result.resize(result_len1 + result_len2);
  164. if (!t->ExpectBytesEqual(bssl::vector_data(out), out->size(),
  165. bssl::vector_data(&result), result.size())) {
  166. return false;
  167. }
  168. if (encrypt && is_aead) {
  169. uint8_t rtag[16];
  170. if (tag.size() > sizeof(rtag)) {
  171. t->PrintLine("Bad tag length.");
  172. return false;
  173. }
  174. if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, tag.size(),
  175. rtag) ||
  176. !t->ExpectBytesEqual(bssl::vector_data(&tag), tag.size(), rtag,
  177. tag.size())) {
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. static bool TestCipher(FileTest *t, void *arg) {
  184. std::string cipher_str;
  185. if (!t->GetAttribute(&cipher_str, "Cipher")) {
  186. return false;
  187. }
  188. const EVP_CIPHER *cipher = GetCipher(cipher_str);
  189. if (cipher == nullptr) {
  190. t->PrintLine("Unknown cipher: '%s'.", cipher_str.c_str());
  191. return false;
  192. }
  193. std::vector<uint8_t> key, iv, plaintext, ciphertext, aad, tag;
  194. if (!t->GetBytes(&key, "Key") ||
  195. !t->GetBytes(&plaintext, "Plaintext") ||
  196. !t->GetBytes(&ciphertext, "Ciphertext")) {
  197. return false;
  198. }
  199. if (EVP_CIPHER_iv_length(cipher) > 0 &&
  200. !t->GetBytes(&iv, "IV")) {
  201. return false;
  202. }
  203. if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) {
  204. if (!t->GetBytes(&aad, "AAD") ||
  205. !t->GetBytes(&tag, "Tag")) {
  206. return false;
  207. }
  208. }
  209. enum {
  210. kEncrypt,
  211. kDecrypt,
  212. kBoth,
  213. } operation = kBoth;
  214. if (t->HasAttribute("Operation")) {
  215. const std::string &str = t->GetAttributeOrDie("Operation");
  216. if (str == "ENCRYPT") {
  217. operation = kEncrypt;
  218. } else if (str == "DECRYPT") {
  219. operation = kDecrypt;
  220. } else {
  221. t->PrintLine("Unknown operation: '%s'.", str.c_str());
  222. return false;
  223. }
  224. }
  225. // By default, both directions are run, unless overridden by the operation.
  226. if (operation != kDecrypt &&
  227. !TestOperation(t, cipher, true /* encrypt */, key, iv, plaintext,
  228. ciphertext, aad, tag)) {
  229. return false;
  230. }
  231. if (operation != kEncrypt &&
  232. !TestOperation(t, cipher, false /* decrypt */, key, iv, plaintext,
  233. ciphertext, aad, tag)) {
  234. return false;
  235. }
  236. return true;
  237. }
  238. int main(int argc, char **argv) {
  239. CRYPTO_library_init();
  240. if (argc != 2) {
  241. fprintf(stderr, "%s <test file>\n", argv[0]);
  242. return 1;
  243. }
  244. return FileTestMain(TestCipher, nullptr, argv[1]);
  245. }