blob: de721075094493696473c1eaee50712b07b2a8e9 [file] [log] [blame]
David Benjamin5c694e32015-05-11 15:58:08 -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
David Benjamina353cdb2016-06-09 16:48:33 -040054#include <openssl/evp.h>
55
David Benjamin5c694e32015-05-11 15:58:08 -040056#include <stdio.h>
57#include <stdint.h>
58#include <stdlib.h>
59#include <string.h>
60
David Benjamina353cdb2016-06-09 16:48:33 -040061OPENSSL_MSVC_PRAGMA(warning(push))
62OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
Brian Smith906e2992015-07-21 21:46:20 -040063
David Benjamin5c694e32015-05-11 15:58:08 -040064#include <map>
65#include <string>
David Benjamin4f6acaf2015-11-21 03:00:50 -050066#include <utility>
David Benjamin5c694e32015-05-11 15:58:08 -040067#include <vector>
68
David Benjamina353cdb2016-06-09 16:48:33 -040069OPENSSL_MSVC_PRAGMA(warning(pop))
Brian Smith906e2992015-07-21 21:46:20 -040070
David Benjamin16c3f062017-06-08 16:00:10 -040071#include <gtest/gtest.h>
72
David Benjaminf0e935d2016-09-06 18:10:19 -040073#include <openssl/bytestring.h>
David Benjamin5c694e32015-05-11 15:58:08 -040074#include <openssl/crypto.h>
75#include <openssl/digest.h>
76#include <openssl/err.h>
David Benjamin4f570742016-12-14 12:34:08 -050077#include <openssl/rsa.h>
David Benjamin5c694e32015-05-11 15:58:08 -040078
79#include "../test/file_test.h"
David Benjamin16c3f062017-06-08 16:00:10 -040080#include "../test/test_util.h"
David Benjamin5c694e32015-05-11 15:58:08 -040081
82
David Benjamin65ee9b72015-06-15 15:51:19 -040083// evp_test dispatches between multiple test types. PrivateKey tests take a key
84// name parameter and single block, decode it as a PEM private key, and save it
85// under that key name. Decrypt, Sign, and Verify tests take a previously
86// imported key name as parameter and test their respective operations.
David Benjamin5c694e32015-05-11 15:58:08 -040087
88static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
89 if (name == "MD5") {
90 return EVP_md5();
91 } else if (name == "SHA1") {
92 return EVP_sha1();
93 } else if (name == "SHA224") {
94 return EVP_sha224();
95 } else if (name == "SHA256") {
96 return EVP_sha256();
97 } else if (name == "SHA384") {
98 return EVP_sha384();
99 } else if (name == "SHA512") {
100 return EVP_sha512();
101 }
David Benjamin16c3f062017-06-08 16:00:10 -0400102 ADD_FAILURE() << "Unknown digest: " << name;
David Benjamin5c694e32015-05-11 15:58:08 -0400103 return nullptr;
104}
105
David Benjamin68772b32015-12-30 21:40:40 -0500106static int GetKeyType(FileTest *t, const std::string &name) {
107 if (name == "RSA") {
108 return EVP_PKEY_RSA;
109 }
110 if (name == "EC") {
111 return EVP_PKEY_EC;
112 }
113 if (name == "DSA") {
114 return EVP_PKEY_DSA;
115 }
David Benjamin05bb1c52017-03-27 21:55:52 -0500116 if (name == "Ed25519") {
117 return EVP_PKEY_ED25519;
118 }
David Benjamin16c3f062017-06-08 16:00:10 -0400119 ADD_FAILURE() << "Unknown key type: " << name;
David Benjamin68772b32015-12-30 21:40:40 -0500120 return EVP_PKEY_NONE;
121}
122
David Benjamin4f570742016-12-14 12:34:08 -0500123static int GetRSAPadding(FileTest *t, int *out, const std::string &name) {
124 if (name == "PKCS1") {
125 *out = RSA_PKCS1_PADDING;
126 return true;
127 }
128 if (name == "PSS") {
129 *out = RSA_PKCS1_PSS_PADDING;
130 return true;
131 }
David Benjaminf53e3902017-01-01 16:03:24 -0500132 if (name == "OAEP") {
133 *out = RSA_PKCS1_OAEP_PADDING;
134 return true;
135 }
David Benjamin16c3f062017-06-08 16:00:10 -0400136 ADD_FAILURE() << "Unknown RSA padding mode: " << name;
David Benjamin4f570742016-12-14 12:34:08 -0500137 return false;
138}
139
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700140using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>;
David Benjamin5c694e32015-05-11 15:58:08 -0400141
David Benjamine30a09e2016-01-01 01:17:30 -0500142static bool ImportKey(FileTest *t, KeyMap *key_map,
143 EVP_PKEY *(*parse_func)(CBS *cbs),
144 int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) {
David Benjamin68772b32015-12-30 21:40:40 -0500145 std::vector<uint8_t> input;
146 if (!t->GetBytes(&input, "Input")) {
147 return false;
148 }
149
150 CBS cbs;
151 CBS_init(&cbs, input.data(), input.size());
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700152 bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs));
David Benjamin68772b32015-12-30 21:40:40 -0500153 if (!pkey) {
154 return false;
155 }
156
157 std::string key_type;
158 if (!t->GetAttribute(&key_type, "Type")) {
159 return false;
160 }
David Benjamin16c3f062017-06-08 16:00:10 -0400161 EXPECT_EQ(GetKeyType(t, key_type), EVP_PKEY_id(pkey.get()));
David Benjamin68772b32015-12-30 21:40:40 -0500162
David Benjamine30a09e2016-01-01 01:17:30 -0500163 // The key must re-encode correctly.
David Benjaminaac1e2d2016-12-06 22:35:41 -0500164 bssl::ScopedCBB cbb;
David Benjamine30a09e2016-01-01 01:17:30 -0500165 uint8_t *der;
166 size_t der_len;
David Benjamin68772b32015-12-30 21:40:40 -0500167 if (!CBB_init(cbb.get(), 0) ||
David Benjamine30a09e2016-01-01 01:17:30 -0500168 !marshal_func(cbb.get(), pkey.get()) ||
169 !CBB_finish(cbb.get(), &der, &der_len)) {
David Benjamin68772b32015-12-30 21:40:40 -0500170 return false;
171 }
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700172 bssl::UniquePtr<uint8_t> free_der(der);
David Benjamine30a09e2016-01-01 01:17:30 -0500173
174 std::vector<uint8_t> output = input;
175 if (t->HasAttribute("Output") &&
176 !t->GetBytes(&output, "Output")) {
177 return false;
178 }
David Benjamin16c3f062017-06-08 16:00:10 -0400179 EXPECT_EQ(Bytes(output), Bytes(der, der_len)) << "Re-encoding the key did not match.";
David Benjamin68772b32015-12-30 21:40:40 -0500180
181 // Save the key for future tests.
182 const std::string &key_name = t->GetParameter();
David Benjamin16c3f062017-06-08 16:00:10 -0400183 EXPECT_EQ(0u, key_map->count(key_name)) << "Duplicate key: " << key_name;
David Benjamin68772b32015-12-30 21:40:40 -0500184 (*key_map)[key_name] = std::move(pkey);
185 return true;
186}
187
David Benjamin19670942017-05-31 19:07:31 -0400188// SetupContext configures |ctx| based on attributes in |t|, with the exception
189// of the signing digest which must be configured externally.
190static bool SetupContext(FileTest *t, EVP_PKEY_CTX *ctx) {
191 if (t->HasAttribute("RSAPadding")) {
192 int padding;
193 if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) ||
194 !EVP_PKEY_CTX_set_rsa_padding(ctx, padding)) {
195 return false;
196 }
197 }
198 if (t->HasAttribute("PSSSaltLength") &&
199 !EVP_PKEY_CTX_set_rsa_pss_saltlen(
200 ctx, atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) {
201 return false;
202 }
203 if (t->HasAttribute("MGF1Digest")) {
204 const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest"));
205 if (digest == nullptr || !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, digest)) {
206 return false;
207 }
208 }
209 return true;
210}
211
David Benjamin16c3f062017-06-08 16:00:10 -0400212static bool TestEVP(FileTest *t, KeyMap *key_map) {
David Benjamin5c694e32015-05-11 15:58:08 -0400213 if (t->GetType() == "PrivateKey") {
David Benjamine30a09e2016-01-01 01:17:30 -0500214 return ImportKey(t, key_map, EVP_parse_private_key,
215 EVP_marshal_private_key);
David Benjamin5c694e32015-05-11 15:58:08 -0400216 }
217
David Benjamin68772b32015-12-30 21:40:40 -0500218 if (t->GetType() == "PublicKey") {
David Benjamine30a09e2016-01-01 01:17:30 -0500219 return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key);
David Benjamin68772b32015-12-30 21:40:40 -0500220 }
221
David Benjamin19670942017-05-31 19:07:31 -0400222 int (*key_op_init)(EVP_PKEY_CTX *ctx) = nullptr;
David Benjamin5c694e32015-05-11 15:58:08 -0400223 int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
David Benjamin7c83fda2017-03-28 13:54:06 -0500224 const uint8_t *in, size_t in_len) = nullptr;
David Benjamin19670942017-05-31 19:07:31 -0400225 int (*md_op_init)(EVP_MD_CTX * ctx, EVP_PKEY_CTX * *pctx, const EVP_MD *type,
226 ENGINE *e, EVP_PKEY *pkey) = nullptr;
227 bool is_verify = false;
David Benjamin5c694e32015-05-11 15:58:08 -0400228 if (t->GetType() == "Decrypt") {
229 key_op_init = EVP_PKEY_decrypt_init;
230 key_op = EVP_PKEY_decrypt;
231 } else if (t->GetType() == "Sign") {
232 key_op_init = EVP_PKEY_sign_init;
233 key_op = EVP_PKEY_sign;
234 } else if (t->GetType() == "Verify") {
235 key_op_init = EVP_PKEY_verify_init;
David Benjamin19670942017-05-31 19:07:31 -0400236 is_verify = true;
237 } else if (t->GetType() == "SignMessage") {
238 md_op_init = EVP_DigestSignInit;
David Benjamin7c83fda2017-03-28 13:54:06 -0500239 } else if (t->GetType() == "VerifyMessage") {
David Benjamin19670942017-05-31 19:07:31 -0400240 md_op_init = EVP_DigestVerifyInit;
241 is_verify = true;
David Benjamin5c694e32015-05-11 15:58:08 -0400242 } else {
David Benjamin16c3f062017-06-08 16:00:10 -0400243 ADD_FAILURE() << "Unknown test " << t->GetType();
David Benjamin5c694e32015-05-11 15:58:08 -0400244 return false;
245 }
246
247 // Load the key.
248 const std::string &key_name = t->GetParameter();
249 if (key_map->count(key_name) == 0) {
David Benjamin16c3f062017-06-08 16:00:10 -0400250 ADD_FAILURE() << "Could not find key " << key_name;
David Benjamin5c694e32015-05-11 15:58:08 -0400251 return false;
252 }
David Benjamin4f6acaf2015-11-21 03:00:50 -0500253 EVP_PKEY *key = (*key_map)[key_name].get();
David Benjamin5c694e32015-05-11 15:58:08 -0400254
David Benjamin19670942017-05-31 19:07:31 -0400255 const EVP_MD *digest = nullptr;
David Benjamin5c694e32015-05-11 15:58:08 -0400256 if (t->HasAttribute("Digest")) {
David Benjamin19670942017-05-31 19:07:31 -0400257 digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
258 if (digest == nullptr) {
David Benjamin4f570742016-12-14 12:34:08 -0500259 return false;
260 }
261 }
David Benjamin5c694e32015-05-11 15:58:08 -0400262
David Benjamin19670942017-05-31 19:07:31 -0400263 // For verify tests, the "output" is the signature. Read it now so that, for
264 // tests which expect a failure in SetupContext, the attribute is still
265 // consumed.
266 std::vector<uint8_t> input, actual, output;
267 if (!t->GetBytes(&input, "Input") ||
268 (is_verify && !t->GetBytes(&output, "Output"))) {
269 return false;
270 }
271
272 if (md_op_init) {
273 bssl::ScopedEVP_MD_CTX ctx;
274 EVP_PKEY_CTX *pctx;
275 if (!md_op_init(ctx.get(), &pctx, digest, nullptr, key) ||
276 !SetupContext(t, pctx)) {
David Benjamin5c694e32015-05-11 15:58:08 -0400277 return false;
278 }
David Benjamin19670942017-05-31 19:07:31 -0400279
280 if (is_verify) {
281 return !!EVP_DigestVerify(ctx.get(), output.data(), output.size(),
282 input.data(), input.size());
283 }
284
285 size_t len;
286 if (!EVP_DigestSign(ctx.get(), nullptr, &len, input.data(), input.size())) {
287 return false;
288 }
289 actual.resize(len);
290 if (!EVP_DigestSign(ctx.get(), actual.data(), &len, input.data(),
291 input.size()) ||
292 !t->GetBytes(&output, "Output")) {
293 return false;
294 }
295 actual.resize(len);
296 EXPECT_EQ(Bytes(output), Bytes(actual));
David Benjamin5c694e32015-05-11 15:58:08 -0400297 return true;
298 }
299
David Benjamin19670942017-05-31 19:07:31 -0400300 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr));
301 if (!ctx ||
302 !key_op_init(ctx.get()) ||
303 (digest != nullptr &&
304 !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) ||
305 !SetupContext(t, ctx.get())) {
306 return false;
307 }
308
309 if (is_verify) {
310 return !!EVP_PKEY_verify(ctx.get(), output.data(), output.size(),
311 input.data(), input.size());
312 }
313
David Benjamin5c694e32015-05-11 15:58:08 -0400314 size_t len;
David Benjaminef14b2d2015-11-11 14:01:27 -0800315 if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
David Benjamin5c694e32015-05-11 15:58:08 -0400316 return false;
317 }
318 actual.resize(len);
David Benjamin19670942017-05-31 19:07:31 -0400319 if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size()) ||
320 !t->GetBytes(&output, "Output")) {
David Benjamin5c694e32015-05-11 15:58:08 -0400321 return false;
322 }
323 actual.resize(len);
David Benjamin16c3f062017-06-08 16:00:10 -0400324 EXPECT_EQ(Bytes(output), Bytes(actual));
David Benjamin5c694e32015-05-11 15:58:08 -0400325 return true;
326}
327
David Benjamin16c3f062017-06-08 16:00:10 -0400328TEST(EVPTest, TestVectors) {
329 KeyMap key_map;
330 FileTestGTest("crypto/evp/evp_tests.txt", [&](FileTest *t) {
331 bool result = TestEVP(t, &key_map);
332 if (t->HasAttribute("Error")) {
333 ASSERT_FALSE(result) << "Operation unexpectedly succeeded.";
334 uint32_t err = ERR_peek_error();
335 EXPECT_EQ(t->GetAttributeOrDie("Error"), ERR_reason_error_string(err));
David Benjamin19670942017-05-31 19:07:31 -0400336 } else if (!result) {
337 ADD_FAILURE() << "Operation unexpectedly failed.";
338 ERR_print_errors_fp(stdout);
David Benjamin16c3f062017-06-08 16:00:10 -0400339 }
340 });
David Benjamin5c694e32015-05-11 15:58:08 -0400341}