blob: 5f04178fc7c3dab27e032ece5c191270a450a741 [file] [log] [blame]
David Benjamin4690bb52015-05-10 03:10:07 -04001/*
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
69static const EVP_CIPHER *GetCipher(const std::string &name) {
70 if (name == "DES-CBC") {
71 return EVP_des_cbc();
Matt Braithwaite98d2f1f2015-08-18 20:27:03 -070072 } else if (name == "DES-ECB") {
73 return EVP_des_ecb();
Matt Braithwaited82a7b22015-08-19 14:25:32 -070074 } else if (name == "DES-EDE") {
75 return EVP_des_ede();
Matt Braithwaite8c413a22015-08-11 17:19:35 -070076 } else if (name == "DES-EDE-CBC") {
77 return EVP_des_ede_cbc();
David Benjamin4690bb52015-05-10 03:10:07 -040078 } 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();
94 } else if (name == "AES-192-ECB") {
95 return EVP_aes_192_ecb();
96 } else if (name == "AES-256-CBC") {
97 return EVP_aes_256_cbc();
98 } else if (name == "AES-128-CTR") {
99 return EVP_aes_128_ctr();
100 } else if (name == "AES-256-CTR") {
101 return EVP_aes_256_ctr();
102 } else if (name == "AES-256-GCM") {
103 return EVP_aes_256_gcm();
104 } else if (name == "AES-256-OFB") {
105 return EVP_aes_256_ofb();
106 }
107 return nullptr;
108}
109
110static bool TestOperation(FileTest *t,
111 const EVP_CIPHER *cipher,
112 bool encrypt,
David Benjaminf0786392015-06-30 10:28:40 -0400113 bool streaming,
David Benjamin4690bb52015-05-10 03:10:07 -0400114 const std::vector<uint8_t> &key,
115 const std::vector<uint8_t> &iv,
116 const std::vector<uint8_t> &plaintext,
117 const std::vector<uint8_t> &ciphertext,
118 const std::vector<uint8_t> &aad,
119 const std::vector<uint8_t> &tag) {
120 const std::vector<uint8_t> *in, *out;
121 if (encrypt) {
122 in = &plaintext;
123 out = &ciphertext;
124 } else {
125 in = &ciphertext;
126 out = &plaintext;
127 }
128
129 bool is_aead = EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE;
130
131 ScopedEVP_CIPHER_CTX ctx;
132 if (!EVP_CipherInit_ex(ctx.get(), cipher, nullptr, nullptr, nullptr,
133 encrypt ? 1 : 0)) {
134 return false;
135 }
136 if (t->HasAttribute("IV")) {
137 if (is_aead) {
138 if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN,
139 iv.size(), 0)) {
140 return false;
141 }
142 } else if (iv.size() != (size_t)EVP_CIPHER_CTX_iv_length(ctx.get())) {
143 t->PrintLine("Bad IV length.");
144 return false;
145 }
146 }
147 if (is_aead && !encrypt &&
148 !EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, tag.size(),
149 const_cast<uint8_t*>(bssl::vector_data(&tag)))) {
150 return false;
151 }
152 // The ciphers are run with no padding. For each of the ciphers we test, the
153 // output size matches the input size.
154 std::vector<uint8_t> result(in->size());
155 if (in->size() != out->size()) {
156 t->PrintLine("Input/output size mismatch (%u vs %u).", (unsigned)in->size(),
157 (unsigned)out->size());
158 return false;
159 }
160 // Note: the deprecated |EVP_CIPHER|-based AES-GCM API is sensitive to whether
161 // parameters are NULL, so it is important to skip the |in| and |aad|
162 // |EVP_CipherUpdate| calls when empty.
163 int unused, result_len1 = 0, result_len2;
164 if (!EVP_CIPHER_CTX_set_key_length(ctx.get(), key.size()) ||
165 !EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, bssl::vector_data(&key),
166 bssl::vector_data(&iv), -1) ||
167 (!aad.empty() &&
168 !EVP_CipherUpdate(ctx.get(), nullptr, &unused, bssl::vector_data(&aad),
169 aad.size())) ||
David Benjaminf0786392015-06-30 10:28:40 -0400170 !EVP_CIPHER_CTX_set_padding(ctx.get(), 0)) {
171 t->PrintLine("Operation failed.");
172 return false;
173 }
174 if (streaming) {
175 for (size_t i = 0; i < in->size(); i++) {
176 uint8_t c = (*in)[i];
177 int len;
178 if (!EVP_CipherUpdate(ctx.get(), bssl::vector_data(&result) + result_len1,
179 &len, &c, 1)) {
180 t->PrintLine("Operation failed.");
181 return false;
182 }
183 result_len1 += len;
184 }
185 } else if (!in->empty() &&
186 !EVP_CipherUpdate(ctx.get(), bssl::vector_data(&result),
187 &result_len1, bssl::vector_data(in),
188 in->size())) {
189 t->PrintLine("Operation failed.");
190 return false;
191 }
192 if (!EVP_CipherFinal_ex(ctx.get(), bssl::vector_data(&result) + result_len1,
David Benjamin4690bb52015-05-10 03:10:07 -0400193 &result_len2)) {
194 t->PrintLine("Operation failed.");
195 return false;
196 }
197 result.resize(result_len1 + result_len2);
198 if (!t->ExpectBytesEqual(bssl::vector_data(out), out->size(),
199 bssl::vector_data(&result), result.size())) {
200 return false;
201 }
202 if (encrypt && is_aead) {
203 uint8_t rtag[16];
204 if (tag.size() > sizeof(rtag)) {
205 t->PrintLine("Bad tag length.");
206 return false;
207 }
208 if (!EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, tag.size(),
209 rtag) ||
210 !t->ExpectBytesEqual(bssl::vector_data(&tag), tag.size(), rtag,
211 tag.size())) {
212 return false;
213 }
214 }
215 return true;
216}
217
218static bool TestCipher(FileTest *t, void *arg) {
219 std::string cipher_str;
220 if (!t->GetAttribute(&cipher_str, "Cipher")) {
221 return false;
222 }
223 const EVP_CIPHER *cipher = GetCipher(cipher_str);
224 if (cipher == nullptr) {
225 t->PrintLine("Unknown cipher: '%s'.", cipher_str.c_str());
226 return false;
227 }
228
229 std::vector<uint8_t> key, iv, plaintext, ciphertext, aad, tag;
230 if (!t->GetBytes(&key, "Key") ||
231 !t->GetBytes(&plaintext, "Plaintext") ||
232 !t->GetBytes(&ciphertext, "Ciphertext")) {
233 return false;
234 }
235 if (EVP_CIPHER_iv_length(cipher) > 0 &&
236 !t->GetBytes(&iv, "IV")) {
237 return false;
238 }
239 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) {
240 if (!t->GetBytes(&aad, "AAD") ||
241 !t->GetBytes(&tag, "Tag")) {
242 return false;
243 }
244 }
245
246 enum {
247 kEncrypt,
248 kDecrypt,
249 kBoth,
250 } operation = kBoth;
251 if (t->HasAttribute("Operation")) {
252 const std::string &str = t->GetAttributeOrDie("Operation");
253 if (str == "ENCRYPT") {
254 operation = kEncrypt;
255 } else if (str == "DECRYPT") {
256 operation = kDecrypt;
257 } else {
258 t->PrintLine("Unknown operation: '%s'.", str.c_str());
259 return false;
260 }
261 }
262
263 // By default, both directions are run, unless overridden by the operation.
David Benjaminf0786392015-06-30 10:28:40 -0400264 if (operation != kDecrypt) {
265 if (!TestOperation(t, cipher, true /* encrypt */, false /* single-shot */,
266 key, iv, plaintext, ciphertext, aad, tag) ||
267 !TestOperation(t, cipher, true /* encrypt */, true /* streaming */, key,
268 iv, plaintext, ciphertext, aad, tag)) {
269 return false;
270 }
David Benjamin4690bb52015-05-10 03:10:07 -0400271 }
David Benjaminf0786392015-06-30 10:28:40 -0400272 if (operation != kEncrypt) {
273 if (!TestOperation(t, cipher, false /* decrypt */, false /* single-shot */,
274 key, iv, plaintext, ciphertext, aad, tag) ||
275 !TestOperation(t, cipher, false /* decrypt */, true /* streaming */,
276 key, iv, plaintext, ciphertext, aad, tag)) {
277 return false;
278 }
David Benjamin4690bb52015-05-10 03:10:07 -0400279 }
280
281 return true;
282}
283
284int main(int argc, char **argv) {
285 CRYPTO_library_init();
286
287 if (argc != 2) {
288 fprintf(stderr, "%s <test file>\n", argv[0]);
289 return 1;
290 }
291
292 return FileTestMain(TestCipher, nullptr, argv[1]);
293}