blob: baf41fe9e635937c838738a51a9a1b5fa4a02534 [file] [log] [blame]
Adam Langleye9ada862015-05-11 17:20:37 -07001/*
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 Benjamin6e899c72016-06-09 18:02:18 -040054#include <openssl/evp.h>
55
Adam Langleye9ada862015-05-11 17:20:37 -070056#include <stdio.h>
57#include <stdint.h>
58#include <stdlib.h>
59#include <string.h>
60
David Benjamin6e899c72016-06-09 18:02:18 -040061OPENSSL_MSVC_PRAGMA(warning(push))
62OPENSSL_MSVC_PRAGMA(warning(disable: 4702))
Kenny Rootb8494592015-09-25 02:29:14 +000063
Adam Langleye9ada862015-05-11 17:20:37 -070064#include <map>
65#include <string>
David Benjamin4969cc92016-04-22 15:02:23 -040066#include <utility>
Adam Langleye9ada862015-05-11 17:20:37 -070067#include <vector>
68
David Benjamin6e899c72016-06-09 18:02:18 -040069OPENSSL_MSVC_PRAGMA(warning(pop))
Kenny Rootb8494592015-09-25 02:29:14 +000070
David Benjaminf0c4a6c2016-08-11 13:26:41 -040071#include <openssl/bytestring.h>
Adam Langleye9ada862015-05-11 17:20:37 -070072#include <openssl/crypto.h>
73#include <openssl/digest.h>
74#include <openssl/err.h>
Robert Sloan69939df2017-01-09 10:53:07 -080075#include <openssl/rsa.h>
Adam Langleye9ada862015-05-11 17:20:37 -070076
77#include "../test/file_test.h"
Adam Langleye9ada862015-05-11 17:20:37 -070078
79
Kenny Rootb8494592015-09-25 02:29:14 +000080// evp_test dispatches between multiple test types. PrivateKey tests take a key
81// name parameter and single block, decode it as a PEM private key, and save it
82// under that key name. Decrypt, Sign, and Verify tests take a previously
83// imported key name as parameter and test their respective operations.
Adam Langleye9ada862015-05-11 17:20:37 -070084
85static const EVP_MD *GetDigest(FileTest *t, const std::string &name) {
86 if (name == "MD5") {
87 return EVP_md5();
88 } else if (name == "SHA1") {
89 return EVP_sha1();
90 } else if (name == "SHA224") {
91 return EVP_sha224();
92 } else if (name == "SHA256") {
93 return EVP_sha256();
94 } else if (name == "SHA384") {
95 return EVP_sha384();
96 } else if (name == "SHA512") {
97 return EVP_sha512();
98 }
99 t->PrintLine("Unknown digest: '%s'", name.c_str());
100 return nullptr;
101}
102
David Benjamin4969cc92016-04-22 15:02:23 -0400103static int GetKeyType(FileTest *t, const std::string &name) {
104 if (name == "RSA") {
105 return EVP_PKEY_RSA;
106 }
107 if (name == "EC") {
108 return EVP_PKEY_EC;
109 }
110 if (name == "DSA") {
111 return EVP_PKEY_DSA;
112 }
Robert Sloan572a4e22017-04-17 10:52:19 -0700113 if (name == "Ed25519") {
114 return EVP_PKEY_ED25519;
115 }
David Benjamin4969cc92016-04-22 15:02:23 -0400116 t->PrintLine("Unknown key type: '%s'", name.c_str());
117 return EVP_PKEY_NONE;
118}
Adam Langleye9ada862015-05-11 17:20:37 -0700119
Robert Sloan69939df2017-01-09 10:53:07 -0800120static int GetRSAPadding(FileTest *t, int *out, const std::string &name) {
121 if (name == "PKCS1") {
122 *out = RSA_PKCS1_PADDING;
123 return true;
124 }
125 if (name == "PSS") {
126 *out = RSA_PKCS1_PSS_PADDING;
127 return true;
128 }
129 if (name == "OAEP") {
130 *out = RSA_PKCS1_OAEP_PADDING;
131 return true;
132 }
133 t->PrintLine("Unknown RSA padding mode: '%s'", name.c_str());
134 return false;
135}
136
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400137using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>;
David Benjamin4969cc92016-04-22 15:02:23 -0400138
139static bool ImportKey(FileTest *t, KeyMap *key_map,
140 EVP_PKEY *(*parse_func)(CBS *cbs),
141 int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) {
142 std::vector<uint8_t> input;
143 if (!t->GetBytes(&input, "Input")) {
144 return false;
145 }
146
147 CBS cbs;
148 CBS_init(&cbs, input.data(), input.size());
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400149 bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs));
David Benjamin4969cc92016-04-22 15:02:23 -0400150 if (!pkey) {
151 return false;
152 }
153
154 std::string key_type;
155 if (!t->GetAttribute(&key_type, "Type")) {
156 return false;
157 }
158 if (EVP_PKEY_id(pkey.get()) != GetKeyType(t, key_type)) {
159 t->PrintLine("Bad key type.");
160 return false;
161 }
162
163 // The key must re-encode correctly.
David Benjamin1b249672016-12-06 18:25:50 -0500164 bssl::ScopedCBB cbb;
David Benjamin4969cc92016-04-22 15:02:23 -0400165 uint8_t *der;
166 size_t der_len;
167 if (!CBB_init(cbb.get(), 0) ||
168 !marshal_func(cbb.get(), pkey.get()) ||
169 !CBB_finish(cbb.get(), &der, &der_len)) {
170 return false;
171 }
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400172 bssl::UniquePtr<uint8_t> free_der(der);
David Benjamin4969cc92016-04-22 15:02:23 -0400173
174 std::vector<uint8_t> output = input;
175 if (t->HasAttribute("Output") &&
176 !t->GetBytes(&output, "Output")) {
177 return false;
178 }
179 if (!t->ExpectBytesEqual(output.data(), output.size(), der, der_len)) {
180 t->PrintLine("Re-encoding the key did not match.");
181 return false;
182 }
183
184 // Save the key for future tests.
Adam Langleye9ada862015-05-11 17:20:37 -0700185 const std::string &key_name = t->GetParameter();
186 if (key_map->count(key_name) > 0) {
187 t->PrintLine("Duplicate key '%s'.", key_name.c_str());
188 return false;
189 }
David Benjamin4969cc92016-04-22 15:02:23 -0400190 (*key_map)[key_name] = std::move(pkey);
Adam Langleye9ada862015-05-11 17:20:37 -0700191 return true;
192}
193
Adam Langleye9ada862015-05-11 17:20:37 -0700194static bool TestEVP(FileTest *t, void *arg) {
195 KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
196 if (t->GetType() == "PrivateKey") {
David Benjamin4969cc92016-04-22 15:02:23 -0400197 return ImportKey(t, key_map, EVP_parse_private_key,
198 EVP_marshal_private_key);
199 }
200
201 if (t->GetType() == "PublicKey") {
202 return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key);
Adam Langleye9ada862015-05-11 17:20:37 -0700203 }
204
205 int (*key_op_init)(EVP_PKEY_CTX *ctx);
206 int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len,
Robert Sloan572a4e22017-04-17 10:52:19 -0700207 const uint8_t *in, size_t in_len) = nullptr;
208 int (*verify_op)(EVP_PKEY_CTX * ctx, const uint8_t *sig, size_t sig_len,
209 const uint8_t *in, size_t in_len) = nullptr;
Adam Langleye9ada862015-05-11 17:20:37 -0700210 if (t->GetType() == "Decrypt") {
211 key_op_init = EVP_PKEY_decrypt_init;
212 key_op = EVP_PKEY_decrypt;
213 } else if (t->GetType() == "Sign") {
214 key_op_init = EVP_PKEY_sign_init;
215 key_op = EVP_PKEY_sign;
Robert Sloan572a4e22017-04-17 10:52:19 -0700216 } else if (t->GetType() == "SignMessage") {
217 key_op_init = EVP_PKEY_sign_init;
218 key_op = EVP_PKEY_sign_message;
Adam Langleye9ada862015-05-11 17:20:37 -0700219 } else if (t->GetType() == "Verify") {
220 key_op_init = EVP_PKEY_verify_init;
Robert Sloan572a4e22017-04-17 10:52:19 -0700221 verify_op = EVP_PKEY_verify;
222 } else if (t->GetType() == "VerifyMessage") {
223 key_op_init = EVP_PKEY_verify_init;
224 verify_op = EVP_PKEY_verify_message;
Adam Langleye9ada862015-05-11 17:20:37 -0700225 } else {
226 t->PrintLine("Unknown test '%s'", t->GetType().c_str());
227 return false;
228 }
229
230 // Load the key.
231 const std::string &key_name = t->GetParameter();
232 if (key_map->count(key_name) == 0) {
233 t->PrintLine("Could not find key '%s'.", key_name.c_str());
234 return false;
235 }
David Benjamin4969cc92016-04-22 15:02:23 -0400236 EVP_PKEY *key = (*key_map)[key_name].get();
Adam Langleye9ada862015-05-11 17:20:37 -0700237
Robert Sloan8ecb7cd2017-03-21 09:39:01 -0700238 std::vector<uint8_t> input;
239 if (!t->GetBytes(&input, "Input")) {
Adam Langleye9ada862015-05-11 17:20:37 -0700240 return false;
241 }
242
243 // Set up the EVP_PKEY_CTX.
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400244 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr));
Adam Langleye9ada862015-05-11 17:20:37 -0700245 if (!ctx || !key_op_init(ctx.get())) {
246 return false;
247 }
248 if (t->HasAttribute("Digest")) {
249 const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("Digest"));
250 if (digest == nullptr ||
251 !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) {
252 return false;
253 }
254 }
Robert Sloan69939df2017-01-09 10:53:07 -0800255 if (t->HasAttribute("RSAPadding")) {
256 int padding;
257 if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) ||
258 !EVP_PKEY_CTX_set_rsa_padding(ctx.get(), padding)) {
259 return false;
260 }
261 }
262 if (t->HasAttribute("PSSSaltLength") &&
263 !EVP_PKEY_CTX_set_rsa_pss_saltlen(
264 ctx.get(), atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) {
265 return false;
266 }
267 if (t->HasAttribute("MGF1Digest")) {
268 const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest"));
269 if (digest == nullptr ||
270 !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), digest)) {
271 return false;
272 }
273 }
Adam Langleye9ada862015-05-11 17:20:37 -0700274
Robert Sloan572a4e22017-04-17 10:52:19 -0700275 if (verify_op != nullptr) {
Robert Sloan8ecb7cd2017-03-21 09:39:01 -0700276 std::vector<uint8_t> output;
277 if (!t->GetBytes(&output, "Output") ||
Robert Sloan572a4e22017-04-17 10:52:19 -0700278 !verify_op(ctx.get(), output.data(), output.size(), input.data(),
279 input.size())) {
Adam Langleye9ada862015-05-11 17:20:37 -0700280 // ECDSA sometimes doesn't push an error code. Push one on the error queue
281 // so it's distinguishable from other errors.
Kenny Rootb8494592015-09-25 02:29:14 +0000282 OPENSSL_PUT_ERROR(USER, ERR_R_EVP_LIB);
Adam Langleye9ada862015-05-11 17:20:37 -0700283 return false;
284 }
285 return true;
286 }
287
288 size_t len;
Robert Sloan8ecb7cd2017-03-21 09:39:01 -0700289 std::vector<uint8_t> actual, output;
Adam Langley4139edb2016-01-13 15:00:54 -0800290 if (!key_op(ctx.get(), nullptr, &len, input.data(), input.size())) {
Adam Langleye9ada862015-05-11 17:20:37 -0700291 return false;
292 }
293 actual.resize(len);
Adam Langley4139edb2016-01-13 15:00:54 -0800294 if (!key_op(ctx.get(), actual.data(), &len, input.data(), input.size())) {
Adam Langleye9ada862015-05-11 17:20:37 -0700295 return false;
296 }
297 actual.resize(len);
Robert Sloan8ecb7cd2017-03-21 09:39:01 -0700298 if (!t->GetBytes(&output, "Output") ||
299 !t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700300 return false;
301 }
302 return true;
303}
304
David Benjamin1b249672016-12-06 18:25:50 -0500305int main(int argc, char *argv[]) {
Adam Langleye9ada862015-05-11 17:20:37 -0700306 CRYPTO_library_init();
307 if (argc != 2) {
308 fprintf(stderr, "%s <test file.txt>\n", argv[0]);
309 return 1;
310 }
311
312 KeyMap map;
David Benjamin4969cc92016-04-22 15:02:23 -0400313 return FileTestMain(TestEVP, &map, argv[1]);
Adam Langleye9ada862015-05-11 17:20:37 -0700314}