| 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 <openssl/evp.h> |
| 55 |
| 56 | #include <stdio.h> |
| 57 | #include <stdint.h> |
| 58 | #include <stdlib.h> |
| 59 | #include <string.h> |
| 60 |
| 61 | OPENSSL_MSVC_PRAGMA(warning(push)) |
| 62 | OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) |
| 63 |
| 64 | #include <map> |
| 65 | #include <string> |
| 66 | #include <utility> |
| 67 | #include <vector> |
| 68 |
| 69 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 70 |
| 71 | #include <gtest/gtest.h> |
| 72 |
| 73 | #include <openssl/buf.h> |
| 74 | #include <openssl/bytestring.h> |
| 75 | #include <openssl/crypto.h> |
| 76 | #include <openssl/digest.h> |
| 77 | #include <openssl/dsa.h> |
| 78 | #include <openssl/err.h> |
| 79 | #include <openssl/rsa.h> |
| 80 |
| 81 | #include "../test/file_test.h" |
| 82 | #include "../test/test_util.h" |
| 83 | #include "../test/wycheproof_util.h" |
| 84 |
| 85 |
| 86 | // evp_test dispatches between multiple test types. PrivateKey tests take a key |
| 87 | // name parameter and single block, decode it as a PEM private key, and save it |
| 88 | // under that key name. Decrypt, Sign, and Verify tests take a previously |
| 89 | // imported key name as parameter and test their respective operations. |
| 90 |
| 91 | static const EVP_MD *GetDigest(FileTest *t, const std::string &name) { |
| 92 | if (name == "MD5") { |
| 93 | return EVP_md5(); |
| 94 | } else if (name == "SHA1") { |
| 95 | return EVP_sha1(); |
| 96 | } else if (name == "SHA224") { |
| 97 | return EVP_sha224(); |
| 98 | } else if (name == "SHA256") { |
| 99 | return EVP_sha256(); |
| 100 | } else if (name == "SHA384") { |
| 101 | return EVP_sha384(); |
| 102 | } else if (name == "SHA512") { |
| 103 | return EVP_sha512(); |
| 104 | } |
| 105 | ADD_FAILURE() << "Unknown digest: " << name; |
| 106 | return nullptr; |
| 107 | } |
| 108 |
| 109 | static int GetKeyType(FileTest *t, const std::string &name) { |
| 110 | if (name == "RSA") { |
| 111 | return EVP_PKEY_RSA; |
| 112 | } |
| 113 | if (name == "EC") { |
| 114 | return EVP_PKEY_EC; |
| 115 | } |
| 116 | if (name == "DSA") { |
| 117 | return EVP_PKEY_DSA; |
| 118 | } |
| 119 | if (name == "Ed25519") { |
| 120 | return EVP_PKEY_ED25519; |
| 121 | } |
| 122 | if (name == "X25519") { |
| 123 | return EVP_PKEY_X25519; |
| 124 | } |
| 125 | ADD_FAILURE() << "Unknown key type: " << name; |
| 126 | return EVP_PKEY_NONE; |
| 127 | } |
| 128 |
| 129 | static int GetRSAPadding(FileTest *t, int *out, const std::string &name) { |
| 130 | if (name == "PKCS1") { |
| 131 | *out = RSA_PKCS1_PADDING; |
| 132 | return true; |
| 133 | } |
| 134 | if (name == "PSS") { |
| 135 | *out = RSA_PKCS1_PSS_PADDING; |
| 136 | return true; |
| 137 | } |
| 138 | if (name == "OAEP") { |
| 139 | *out = RSA_PKCS1_OAEP_PADDING; |
| 140 | return true; |
| 141 | } |
| 142 | ADD_FAILURE() << "Unknown RSA padding mode: " << name; |
| 143 | return false; |
| 144 | } |
| 145 |
| 146 | using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>; |
| 147 |
| 148 | static bool ImportKey(FileTest *t, KeyMap *key_map, |
| 149 | EVP_PKEY *(*parse_func)(CBS *cbs), |
| 150 | int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) { |
| 151 | std::vector<uint8_t> input; |
| 152 | if (!t->GetBytes(&input, "Input")) { |
| 153 | return false; |
| 154 | } |
| 155 |
| 156 | CBS cbs; |
| 157 | CBS_init(&cbs, input.data(), input.size()); |
| 158 | bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs)); |
| 159 | if (!pkey) { |
| 160 | return false; |
| 161 | } |
| 162 |
| 163 | std::string key_type; |
| 164 | if (!t->GetAttribute(&key_type, "Type")) { |
| 165 | return false; |
| 166 | } |
| 167 | EXPECT_EQ(GetKeyType(t, key_type), EVP_PKEY_id(pkey.get())); |
| 168 |
| 169 | // The key must re-encode correctly. |
| 170 | bssl::ScopedCBB cbb; |
| 171 | uint8_t *der; |
| 172 | size_t der_len; |
| 173 | if (!CBB_init(cbb.get(), 0) || |
| 174 | !marshal_func(cbb.get(), pkey.get()) || |
| 175 | !CBB_finish(cbb.get(), &der, &der_len)) { |
| 176 | return false; |
| 177 | } |
| 178 | bssl::UniquePtr<uint8_t> free_der(der); |
| 179 |
| 180 | std::vector<uint8_t> output = input; |
| 181 | if (t->HasAttribute("Output") && |
| 182 | !t->GetBytes(&output, "Output")) { |
| 183 | return false; |
| 184 | } |
| 185 | EXPECT_EQ(Bytes(output), Bytes(der, der_len)) |
| 186 | << "Re-encoding the key did not match."; |
| 187 |
| 188 | if (t->HasAttribute("ExpectNoRawPrivate")) { |
| 189 | size_t len; |
| 190 | EXPECT_FALSE(EVP_PKEY_get_raw_private_key(pkey.get(), nullptr, &len)); |
| 191 | } else if (t->HasAttribute("ExpectRawPrivate")) { |
| 192 | std::vector<uint8_t> expected; |
| 193 | if (!t->GetBytes(&expected, "ExpectRawPrivate")) { |
| 194 | return false; |
| 195 | } |
| 196 |
| 197 | std::vector<uint8_t> raw; |
| 198 | size_t len; |
| 199 | if (!EVP_PKEY_get_raw_private_key(pkey.get(), nullptr, &len)) { |
| 200 | return false; |
| 201 | } |
| 202 | raw.resize(len); |
| 203 | if (!EVP_PKEY_get_raw_private_key(pkey.get(), raw.data(), &len)) { |
| 204 | return false; |
| 205 | } |
| 206 | raw.resize(len); |
| 207 | EXPECT_EQ(Bytes(raw), Bytes(expected)); |
| 208 |
| 209 | // Short buffers should be rejected. |
| 210 | raw.resize(len - 1); |
| 211 | len = raw.size(); |
| 212 | EXPECT_FALSE(EVP_PKEY_get_raw_private_key(pkey.get(), raw.data(), &len)); |
| 213 | } |
| 214 |
| 215 | if (t->HasAttribute("ExpectNoRawPublic")) { |
| 216 | size_t len; |
| 217 | EXPECT_FALSE(EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len)); |
| 218 | } else if (t->HasAttribute("ExpectRawPublic")) { |
| 219 | std::vector<uint8_t> expected; |
| 220 | if (!t->GetBytes(&expected, "ExpectRawPublic")) { |
| 221 | return false; |
| 222 | } |
| 223 |
| 224 | std::vector<uint8_t> raw; |
| 225 | size_t len; |
| 226 | if (!EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len)) { |
| 227 | return false; |
| 228 | } |
| 229 | raw.resize(len); |
| 230 | if (!EVP_PKEY_get_raw_public_key(pkey.get(), raw.data(), &len)) { |
| 231 | return false; |
| 232 | } |
| 233 | raw.resize(len); |
| 234 | EXPECT_EQ(Bytes(raw), Bytes(expected)); |
| 235 |
| 236 | // Short buffers should be rejected. |
| 237 | raw.resize(len - 1); |
| 238 | len = raw.size(); |
| 239 | EXPECT_FALSE(EVP_PKEY_get_raw_public_key(pkey.get(), raw.data(), &len)); |
| 240 | } |
| 241 |
| 242 | // Save the key for future tests. |
| 243 | const std::string &key_name = t->GetParameter(); |
| 244 | EXPECT_EQ(0u, key_map->count(key_name)) << "Duplicate key: " << key_name; |
| 245 | (*key_map)[key_name] = std::move(pkey); |
| 246 | return true; |
| 247 | } |
| 248 |
| 249 | // SetupContext configures |ctx| based on attributes in |t|, with the exception |
| 250 | // of the signing digest which must be configured externally. |
| 251 | static bool SetupContext(FileTest *t, KeyMap *key_map, EVP_PKEY_CTX *ctx) { |
| 252 | if (t->HasAttribute("RSAPadding")) { |
| 253 | int padding; |
| 254 | if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) || |
| 255 | !EVP_PKEY_CTX_set_rsa_padding(ctx, padding)) { |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | if (t->HasAttribute("PSSSaltLength") && |
| 260 | !EVP_PKEY_CTX_set_rsa_pss_saltlen( |
| 261 | ctx, atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) { |
| 262 | return false; |
| 263 | } |
| 264 | if (t->HasAttribute("MGF1Digest")) { |
| 265 | const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest")); |
| 266 | if (digest == nullptr || !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, digest)) { |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | if (t->HasAttribute("OAEPDigest")) { |
| 271 | const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("OAEPDigest")); |
| 272 | if (digest == nullptr || !EVP_PKEY_CTX_set_rsa_oaep_md(ctx, digest)) { |
| 273 | return false; |
| 274 | } |
| 275 | } |
| 276 | if (t->HasAttribute("OAEPLabel")) { |
| 277 | std::vector<uint8_t> label; |
| 278 | if (!t->GetBytes(&label, "OAEPLabel")) { |
| 279 | return false; |
| 280 | } |
| 281 | // For historical reasons, |EVP_PKEY_CTX_set0_rsa_oaep_label| expects to be |
| 282 | // take ownership of the input. |
| 283 | bssl::UniquePtr<uint8_t> buf( |
| 284 | reinterpret_cast<uint8_t *>(BUF_memdup(label.data(), label.size()))); |
| 285 | if (!buf || |
| 286 | !EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, buf.get(), label.size())) { |
| 287 | return false; |
| 288 | } |
| 289 | buf.release(); |
| 290 | } |
| 291 | if (t->HasAttribute("DerivePeer")) { |
| 292 | std::string derive_peer = t->GetAttributeOrDie("DerivePeer"); |
| 293 | if (key_map->count(derive_peer) == 0) { |
| 294 | ADD_FAILURE() << "Could not find key " << derive_peer; |
| 295 | return false; |
| 296 | } |
| 297 | EVP_PKEY *derive_peer_key = (*key_map)[derive_peer].get(); |
| 298 | if (!EVP_PKEY_derive_set_peer(ctx, derive_peer_key)) { |
| 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | return true; |
| 303 | } |
| 304 |
| 305 | static bool TestDerive(FileTest *t, KeyMap *key_map, EVP_PKEY *key) { |
| 306 | bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 307 | if (!ctx || |
| 308 | !EVP_PKEY_derive_init(ctx.get()) || |
| 309 | !SetupContext(t, key_map, ctx.get())) { |
| 310 | return false; |
| 311 | } |
| 312 |
| 313 | bssl::UniquePtr<EVP_PKEY_CTX> copy(EVP_PKEY_CTX_dup(ctx.get())); |
| 314 | if (!copy) { |
| 315 | return false; |
| 316 | } |
| 317 |
| 318 | for (EVP_PKEY_CTX *pctx : {ctx.get(), copy.get()}) { |
| 319 | size_t len; |
| 320 | std::vector<uint8_t> actual, output; |
| 321 | if (!EVP_PKEY_derive(pctx, nullptr, &len)) { |
| 322 | return false; |
| 323 | } |
| 324 | actual.resize(len); |
| 325 | if (!EVP_PKEY_derive(pctx, actual.data(), &len)) { |
| 326 | return false; |
| 327 | } |
| 328 | actual.resize(len); |
| 329 |
| 330 | // Defer looking up the attribute so Error works properly. |
| 331 | if (!t->GetBytes(&output, "Output")) { |
| 332 | return false; |
| 333 | } |
| 334 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 335 |
| 336 | // Test when the buffer is too large. |
| 337 | actual.resize(len + 1); |
| 338 | len = actual.size(); |
| 339 | if (!EVP_PKEY_derive(pctx, actual.data(), &len)) { |
| 340 | return false; |
| 341 | } |
| 342 | actual.resize(len); |
| 343 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 344 |
| 345 | // Test when the buffer is too small. |
| 346 | actual.resize(len - 1); |
| 347 | len = actual.size(); |
| 348 | if (t->HasAttribute("SmallBufferTruncates")) { |
| 349 | if (!EVP_PKEY_derive(pctx, actual.data(), &len)) { |
| 350 | return false; |
| 351 | } |
| 352 | actual.resize(len); |
| 353 | EXPECT_EQ(Bytes(output.data(), len), Bytes(actual)); |
| 354 | } else { |
| 355 | EXPECT_FALSE(EVP_PKEY_derive(pctx, actual.data(), &len)); |
| 356 | ERR_clear_error(); |
| 357 | } |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 |
| 362 | static bool TestEVP(FileTest *t, KeyMap *key_map) { |
| 363 | if (t->GetType() == "PrivateKey") { |
| 364 | return ImportKey(t, key_map, EVP_parse_private_key, |
| 365 | EVP_marshal_private_key); |
| 366 | } |
| 367 |
| 368 | if (t->GetType() == "PublicKey") { |
| 369 | return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key); |
| 370 | } |
| 371 |
| 372 | // Load the key. |
| 373 | const std::string &key_name = t->GetParameter(); |
| 374 | if (key_map->count(key_name) == 0) { |
| 375 | ADD_FAILURE() << "Could not find key " << key_name; |
| 376 | return false; |
| 377 | } |
| 378 | EVP_PKEY *key = (*key_map)[key_name].get(); |
| 379 |
| 380 | int (*key_op_init)(EVP_PKEY_CTX *ctx) = nullptr; |
| 381 | int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, |
| 382 | const uint8_t *in, size_t in_len) = nullptr; |
| 383 | int (*md_op_init)(EVP_MD_CTX * ctx, EVP_PKEY_CTX * *pctx, const EVP_MD *type, |
| 384 | ENGINE *e, EVP_PKEY *pkey) = nullptr; |
| 385 | bool is_verify = false; |
| 386 | if (t->GetType() == "Decrypt") { |
| 387 | key_op_init = EVP_PKEY_decrypt_init; |
| 388 | key_op = EVP_PKEY_decrypt; |
| 389 | } else if (t->GetType() == "Sign") { |
| 390 | key_op_init = EVP_PKEY_sign_init; |
| 391 | key_op = EVP_PKEY_sign; |
| 392 | } else if (t->GetType() == "Verify") { |
| 393 | key_op_init = EVP_PKEY_verify_init; |
| 394 | is_verify = true; |
| 395 | } else if (t->GetType() == "SignMessage") { |
| 396 | md_op_init = EVP_DigestSignInit; |
| 397 | } else if (t->GetType() == "VerifyMessage") { |
| 398 | md_op_init = EVP_DigestVerifyInit; |
| 399 | is_verify = true; |
| 400 | } else if (t->GetType() == "Encrypt") { |
| 401 | key_op_init = EVP_PKEY_encrypt_init; |
| 402 | key_op = EVP_PKEY_encrypt; |
| 403 | } else if (t->GetType() == "Derive") { |
| 404 | return TestDerive(t, key_map, key); |
| 405 | } else { |
| 406 | ADD_FAILURE() << "Unknown test " << t->GetType(); |
| 407 | return false; |
| 408 | } |
| 409 |
| 410 | const EVP_MD *digest = nullptr; |
| 411 | if (t->HasAttribute("Digest")) { |
| 412 | digest = GetDigest(t, t->GetAttributeOrDie("Digest")); |
| 413 | if (digest == nullptr) { |
| 414 | return false; |
| 415 | } |
| 416 | } |
| 417 |
| 418 | // For verify tests, the "output" is the signature. Read it now so that, for |
| 419 | // tests which expect a failure in SetupContext, the attribute is still |
| 420 | // consumed. |
| 421 | std::vector<uint8_t> input, actual, output; |
| 422 | if (!t->GetBytes(&input, "Input") || |
| 423 | (is_verify && !t->GetBytes(&output, "Output"))) { |
| 424 | return false; |
| 425 | } |
| 426 |
| 427 | if (md_op_init) { |
| 428 | bssl::ScopedEVP_MD_CTX ctx, copy; |
| 429 | EVP_PKEY_CTX *pctx; |
| 430 | if (!md_op_init(ctx.get(), &pctx, digest, nullptr, key) || |
| 431 | !SetupContext(t, key_map, pctx) || |
| 432 | !EVP_MD_CTX_copy_ex(copy.get(), ctx.get())) { |
| 433 | return false; |
| 434 | } |
| 435 |
| 436 | if (is_verify) { |
| 437 | return EVP_DigestVerify(ctx.get(), output.data(), output.size(), |
| 438 | input.data(), input.size()) && |
| 439 | EVP_DigestVerify(copy.get(), output.data(), output.size(), |
| 440 | input.data(), input.size()); |
| 441 | } |
| 442 |
| 443 | size_t len; |
| 444 | if (!EVP_DigestSign(ctx.get(), nullptr, &len, input.data(), input.size())) { |
| 445 | return false; |
| 446 | } |
| 447 | actual.resize(len); |
| 448 | if (!EVP_DigestSign(ctx.get(), actual.data(), &len, input.data(), |
| 449 | input.size()) || |
| 450 | !t->GetBytes(&output, "Output")) { |
| 451 | return false; |
| 452 | } |
| 453 | actual.resize(len); |
| 454 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 455 |
| 456 | // Repeat the test with |copy|, to check |EVP_MD_CTX_copy_ex| duplicated |
| 457 | // everything. |
| 458 | if (!EVP_DigestSign(copy.get(), nullptr, &len, input.data(), |
| 459 | input.size())) { |
| 460 | return false; |
| 461 | } |
| 462 | actual.resize(len); |
| 463 | if (!EVP_DigestSign(copy.get(), actual.data(), &len, input.data(), |
| 464 | input.size()) || |
| 465 | !t->GetBytes(&output, "Output")) { |
| 466 | return false; |
| 467 | } |
| 468 | actual.resize(len); |
| 469 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 470 | return true; |
| 471 | } |
| 472 |
| 473 | bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 474 | if (!ctx || |
| 475 | !key_op_init(ctx.get()) || |
| 476 | (digest != nullptr && |
| 477 | !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) || |
| 478 | !SetupContext(t, key_map, ctx.get())) { |
| 479 | return false; |
| 480 | } |
| 481 |
| 482 | bssl::UniquePtr<EVP_PKEY_CTX> copy(EVP_PKEY_CTX_dup(ctx.get())); |
| 483 | if (!copy) { |
| 484 | return false; |
| 485 | } |
| 486 |
| 487 | if (is_verify) { |
| 488 | return EVP_PKEY_verify(ctx.get(), output.data(), output.size(), |
| 489 | input.data(), input.size()) && |
| 490 | EVP_PKEY_verify(copy.get(), output.data(), output.size(), |
| 491 | input.data(), input.size()); |
| 492 | } |
| 493 |
| 494 | for (EVP_PKEY_CTX *pctx : {ctx.get(), copy.get()}) { |
| 495 | size_t len; |
| 496 | if (!key_op(pctx, nullptr, &len, input.data(), input.size())) { |
| 497 | return false; |
| 498 | } |
| 499 | actual.resize(len); |
| 500 | if (!key_op(pctx, actual.data(), &len, input.data(), input.size())) { |
| 501 | return false; |
| 502 | } |
| 503 |
| 504 | if (t->HasAttribute("CheckDecrypt")) { |
| 505 | // Encryption is non-deterministic, so we check by decrypting. |
| 506 | size_t plaintext_len; |
| 507 | bssl::UniquePtr<EVP_PKEY_CTX> decrypt_ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 508 | if (!decrypt_ctx || |
| 509 | !EVP_PKEY_decrypt_init(decrypt_ctx.get()) || |
| 510 | (digest != nullptr && |
| 511 | !EVP_PKEY_CTX_set_signature_md(decrypt_ctx.get(), digest)) || |
| 512 | !SetupContext(t, key_map, decrypt_ctx.get()) || |
| 513 | !EVP_PKEY_decrypt(decrypt_ctx.get(), nullptr, &plaintext_len, |
| 514 | actual.data(), actual.size())) { |
| 515 | return false; |
| 516 | } |
| 517 | output.resize(plaintext_len); |
| 518 | if (!EVP_PKEY_decrypt(decrypt_ctx.get(), output.data(), &plaintext_len, |
| 519 | actual.data(), actual.size())) { |
| 520 | ADD_FAILURE() << "Could not decrypt result."; |
| 521 | return false; |
| 522 | } |
| 523 | output.resize(plaintext_len); |
| 524 | EXPECT_EQ(Bytes(input), Bytes(output)) << "Decrypted result mismatch."; |
| 525 | } else if (t->HasAttribute("CheckVerify")) { |
| 526 | // Some signature schemes are non-deterministic, so we check by verifying. |
| 527 | bssl::UniquePtr<EVP_PKEY_CTX> verify_ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 528 | if (!verify_ctx || |
| 529 | !EVP_PKEY_verify_init(verify_ctx.get()) || |
| 530 | (digest != nullptr && |
| 531 | !EVP_PKEY_CTX_set_signature_md(verify_ctx.get(), digest)) || |
| 532 | !SetupContext(t, key_map, verify_ctx.get())) { |
| 533 | return false; |
| 534 | } |
| 535 | if (t->HasAttribute("VerifyPSSSaltLength")) { |
| 536 | if (!EVP_PKEY_CTX_set_rsa_pss_saltlen( |
| 537 | verify_ctx.get(), |
| 538 | atoi(t->GetAttributeOrDie("VerifyPSSSaltLength").c_str()))) { |
| 539 | return false; |
| 540 | } |
| 541 | } |
| 542 | EXPECT_TRUE(EVP_PKEY_verify(verify_ctx.get(), actual.data(), |
| 543 | actual.size(), input.data(), input.size())) |
| 544 | << "Could not verify result."; |
| 545 | } else { |
| 546 | // By default, check by comparing the result against Output. |
| 547 | if (!t->GetBytes(&output, "Output")) { |
| 548 | return false; |
| 549 | } |
| 550 | actual.resize(len); |
| 551 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 552 | } |
| 553 | } |
| 554 | return true; |
| 555 | } |
| 556 |
| 557 | TEST(EVPTest, TestVectors) { |
| 558 | KeyMap key_map; |
| 559 | FileTestGTest("crypto/evp/evp_tests.txt", [&](FileTest *t) { |
| 560 | bool result = TestEVP(t, &key_map); |
| 561 | if (t->HasAttribute("Error")) { |
| 562 | ASSERT_FALSE(result) << "Operation unexpectedly succeeded."; |
| 563 | uint32_t err = ERR_peek_error(); |
| 564 | EXPECT_EQ(t->GetAttributeOrDie("Error"), ERR_reason_error_string(err)); |
| 565 | } else if (!result) { |
| 566 | ADD_FAILURE() << "Operation unexpectedly failed."; |
| 567 | } |
| 568 | }); |
| 569 | } |
| 570 |
| 571 | static void RunWycheproofTest(const char *path) { |
| 572 | SCOPED_TRACE(path); |
| 573 | FileTestGTest(path, [](FileTest *t) { |
| 574 | t->IgnoreInstruction("key.type"); |
| 575 | // Extra ECDSA fields. |
| 576 | t->IgnoreInstruction("key.curve"); |
| 577 | t->IgnoreInstruction("key.keySize"); |
| 578 | t->IgnoreInstruction("key.wx"); |
| 579 | t->IgnoreInstruction("key.wy"); |
| 580 | t->IgnoreInstruction("key.uncompressed"); |
| 581 | // Extra RSA fields. |
| 582 | t->IgnoreInstruction("e"); |
| 583 | t->IgnoreInstruction("keyAsn"); |
| 584 | t->IgnoreInstruction("keysize"); |
| 585 | t->IgnoreInstruction("n"); |
| 586 | t->IgnoreAttribute("padding"); |
| 587 | t->IgnoreInstruction("keyJwk.alg"); |
| 588 | t->IgnoreInstruction("keyJwk.e"); |
| 589 | t->IgnoreInstruction("keyJwk.kid"); |
| 590 | t->IgnoreInstruction("keyJwk.kty"); |
| 591 | t->IgnoreInstruction("keyJwk.n"); |
| 592 | // Extra EdDSA fields. |
| 593 | t->IgnoreInstruction("key.pk"); |
| 594 | t->IgnoreInstruction("key.sk"); |
| 595 | t->IgnoreInstruction("jwk.crv"); |
| 596 | t->IgnoreInstruction("jwk.d"); |
| 597 | t->IgnoreInstruction("jwk.kid"); |
| 598 | t->IgnoreInstruction("jwk.kty"); |
| 599 | t->IgnoreInstruction("jwk.x"); |
| 600 | // Extra DSA fields. |
| 601 | t->IgnoreInstruction("key.g"); |
| 602 | t->IgnoreInstruction("key.p"); |
| 603 | t->IgnoreInstruction("key.q"); |
| 604 | t->IgnoreInstruction("key.y"); |
| 605 |
| 606 | std::vector<uint8_t> der; |
| 607 | ASSERT_TRUE(t->GetInstructionBytes(&der, "keyDer")); |
| 608 | CBS cbs; |
| 609 | CBS_init(&cbs, der.data(), der.size()); |
| 610 | bssl::UniquePtr<EVP_PKEY> key(EVP_parse_public_key(&cbs)); |
| 611 | ASSERT_TRUE(key); |
| 612 |
| 613 | const EVP_MD *md = nullptr; |
| 614 | if (t->HasInstruction("sha")) { |
| 615 | md = GetWycheproofDigest(t, "sha", true); |
| 616 | ASSERT_TRUE(md); |
| 617 | } |
| 618 |
| 619 | bool is_pss = t->HasInstruction("mgf"); |
| 620 | const EVP_MD *mgf1_md = nullptr; |
| 621 | int pss_salt_len = -1; |
| 622 | if (is_pss) { |
| 623 | ASSERT_EQ("MGF1", t->GetInstructionOrDie("mgf")); |
| 624 | mgf1_md = GetWycheproofDigest(t, "mgfSha", true); |
| 625 |
| 626 | std::string s_len; |
| 627 | ASSERT_TRUE(t->GetInstruction(&s_len, "sLen")); |
| 628 | pss_salt_len = atoi(s_len.c_str()); |
| 629 | } |
| 630 |
| 631 | std::vector<uint8_t> msg; |
| 632 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 633 | std::vector<uint8_t> sig; |
| 634 | ASSERT_TRUE(t->GetBytes(&sig, "sig")); |
| 635 | WycheproofResult result; |
| 636 | ASSERT_TRUE(GetWycheproofResult(t, &result)); |
| 637 |
| 638 | if (EVP_PKEY_id(key.get()) == EVP_PKEY_DSA) { |
| 639 | // DSA is deprecated and is not usable via EVP. |
| 640 | DSA *dsa = EVP_PKEY_get0_DSA(key.get()); |
| 641 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 642 | unsigned digest_len; |
| 643 | ASSERT_TRUE( |
| 644 | EVP_Digest(msg.data(), msg.size(), digest, &digest_len, md, nullptr)); |
| 645 | int valid; |
| 646 | bool sig_ok = DSA_check_signature(&valid, digest, digest_len, sig.data(), |
| 647 | sig.size(), dsa) && |
| 648 | valid; |
| 649 | if (result == WycheproofResult::kValid) { |
| 650 | EXPECT_TRUE(sig_ok); |
| 651 | } else if (result == WycheproofResult::kInvalid) { |
| 652 | EXPECT_FALSE(sig_ok); |
| 653 | } else { |
| 654 | // this is a legacy signature, which may or may not be accepted. |
| 655 | } |
| 656 | } else { |
| 657 | bssl::ScopedEVP_MD_CTX ctx; |
| 658 | EVP_PKEY_CTX *pctx; |
| 659 | ASSERT_TRUE( |
| 660 | EVP_DigestVerifyInit(ctx.get(), &pctx, md, nullptr, key.get())); |
| 661 | if (is_pss) { |
| 662 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING)); |
| 663 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1_md)); |
| 664 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, pss_salt_len)); |
| 665 | } |
| 666 | int ret = EVP_DigestVerify(ctx.get(), sig.data(), sig.size(), msg.data(), |
| 667 | msg.size()); |
| 668 | if (result == WycheproofResult::kValid) { |
| 669 | EXPECT_EQ(1, ret); |
| 670 | } else if (result == WycheproofResult::kInvalid) { |
| 671 | EXPECT_EQ(0, ret); |
| 672 | } else { |
| 673 | // this is a legacy signature, which may or may not be accepted. |
| 674 | EXPECT_TRUE(ret == 1 || ret == 0); |
| 675 | } |
| 676 | } |
| 677 | }); |
| 678 | } |
| 679 |
| 680 | TEST(EVPTest, WycheproofDSA) { |
| 681 | RunWycheproofTest("third_party/wycheproof_testvectors/dsa_test.txt"); |
| 682 | } |
| 683 |
| 684 | TEST(EVPTest, WycheproofECDSAP224) { |
| 685 | RunWycheproofTest( |
| 686 | "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha224_test.txt"); |
| 687 | RunWycheproofTest( |
| 688 | "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha256_test.txt"); |
| 689 | RunWycheproofTest( |
| 690 | "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha512_test.txt"); |
| 691 | } |
| 692 |
| 693 | TEST(EVPTest, WycheproofECDSAP256) { |
| 694 | RunWycheproofTest( |
| 695 | "third_party/wycheproof_testvectors/ecdsa_secp256r1_sha256_test.txt"); |
| 696 | RunWycheproofTest( |
| 697 | "third_party/wycheproof_testvectors/ecdsa_secp256r1_sha512_test.txt"); |
| 698 | } |
| 699 |
| 700 | TEST(EVPTest, WycheproofECDSAP384) { |
| 701 | RunWycheproofTest( |
| 702 | "third_party/wycheproof_testvectors/ecdsa_secp384r1_sha384_test.txt"); |
| 703 | } |
| 704 |
| 705 | TEST(EVPTest, WycheproofECDSAP521) { |
| 706 | RunWycheproofTest( |
| 707 | "third_party/wycheproof_testvectors/ecdsa_secp384r1_sha512_test.txt"); |
| 708 | RunWycheproofTest( |
| 709 | "third_party/wycheproof_testvectors/ecdsa_secp521r1_sha512_test.txt"); |
| 710 | } |
| 711 |
| 712 | TEST(EVPTest, WycheproofEdDSA) { |
| 713 | RunWycheproofTest("third_party/wycheproof_testvectors/eddsa_test.txt"); |
| 714 | } |
| 715 |
| 716 | TEST(EVPTest, WycheproofRSAPKCS1) { |
| 717 | RunWycheproofTest( |
| 718 | "third_party/wycheproof_testvectors/rsa_signature_test.txt"); |
| 719 | } |
| 720 |
| 721 | TEST(EVPTest, WycheproofRSAPSS) { |
| 722 | RunWycheproofTest( |
| 723 | "third_party/wycheproof_testvectors/rsa_pss_2048_sha1_mgf1_20_test.txt"); |
| 724 | RunWycheproofTest( |
| 725 | "third_party/wycheproof_testvectors/rsa_pss_2048_sha256_mgf1_0_test.txt"); |
| 726 | RunWycheproofTest( |
| 727 | "third_party/wycheproof_testvectors/" |
| 728 | "rsa_pss_2048_sha256_mgf1_32_test.txt"); |
| 729 | RunWycheproofTest( |
| 730 | "third_party/wycheproof_testvectors/" |
| 731 | "rsa_pss_3072_sha256_mgf1_32_test.txt"); |
| 732 | RunWycheproofTest( |
| 733 | "third_party/wycheproof_testvectors/" |
| 734 | "rsa_pss_4096_sha256_mgf1_32_test.txt"); |
| 735 | RunWycheproofTest( |
| 736 | "third_party/wycheproof_testvectors/" |
| 737 | "rsa_pss_4096_sha512_mgf1_32_test.txt"); |
| 738 | RunWycheproofTest("third_party/wycheproof_testvectors/rsa_pss_misc_test.txt"); |
| 739 | } |
| 740 |