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