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