| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <stdio.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 |
| 22 | #include <map> |
| 23 | #include <string> |
| 24 | #include <utility> |
| 25 | #include <vector> |
| 26 |
| 27 | #include <gtest/gtest.h> |
| 28 |
| 29 | #include <openssl/bn.h> |
| 30 | #include <openssl/bytestring.h> |
| 31 | #include <openssl/crypto.h> |
| 32 | #include <openssl/digest.h> |
| 33 | #include <openssl/dh.h> |
| 34 | #include <openssl/dsa.h> |
| 35 | #include <openssl/err.h> |
| 36 | #include <openssl/rsa.h> |
| 37 |
| 38 | #include "../test/file_test.h" |
| 39 | #include "../test/test_util.h" |
| 40 | #include "../test/wycheproof_util.h" |
| 41 |
| 42 |
| 43 | // evp_test dispatches between multiple test types. PrivateKey tests take a key |
| 44 | // name parameter and single block, decode it as a PEM private key, and save it |
| 45 | // under that key name. Decrypt, Sign, and Verify tests take a previously |
| 46 | // imported key name as parameter and test their respective operations. |
| 47 |
| 48 | static const EVP_MD *GetDigest(FileTest *t, const std::string &name) { |
| 49 | if (name == "MD5") { |
| 50 | return EVP_md5(); |
| 51 | } else if (name == "SHA1") { |
| 52 | return EVP_sha1(); |
| 53 | } else if (name == "SHA224") { |
| 54 | return EVP_sha224(); |
| 55 | } else if (name == "SHA256") { |
| 56 | return EVP_sha256(); |
| 57 | } else if (name == "SHA384") { |
| 58 | return EVP_sha384(); |
| 59 | } else if (name == "SHA512") { |
| 60 | return EVP_sha512(); |
| 61 | } |
| 62 | ADD_FAILURE() << "Unknown digest: " << name; |
| 63 | return nullptr; |
| 64 | } |
| 65 |
| 66 | static int GetKeyType(FileTest *t, const std::string &name) { |
| 67 | if (name == "RSA") { |
| 68 | return EVP_PKEY_RSA; |
| 69 | } |
| 70 | if (name == "EC") { |
| 71 | return EVP_PKEY_EC; |
| 72 | } |
| 73 | if (name == "DSA") { |
| 74 | return EVP_PKEY_DSA; |
| 75 | } |
| 76 | if (name == "Ed25519") { |
| 77 | return EVP_PKEY_ED25519; |
| 78 | } |
| 79 | if (name == "X25519") { |
| 80 | return EVP_PKEY_X25519; |
| 81 | } |
| 82 | ADD_FAILURE() << "Unknown key type: " << name; |
| 83 | return EVP_PKEY_NONE; |
| 84 | } |
| 85 |
| 86 | static bool GetRSAPadding(FileTest *t, int *out, const std::string &name) { |
| 87 | if (name == "PKCS1") { |
| 88 | *out = RSA_PKCS1_PADDING; |
| 89 | return true; |
| 90 | } |
| 91 | if (name == "PSS") { |
| 92 | *out = RSA_PKCS1_PSS_PADDING; |
| 93 | return true; |
| 94 | } |
| 95 | if (name == "OAEP") { |
| 96 | *out = RSA_PKCS1_OAEP_PADDING; |
| 97 | return true; |
| 98 | } |
| 99 | if (name == "None") { |
| 100 | *out = RSA_NO_PADDING; |
| 101 | return true; |
| 102 | } |
| 103 | ADD_FAILURE() << "Unknown RSA padding mode: " << name; |
| 104 | return false; |
| 105 | } |
| 106 |
| 107 | using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>; |
| 108 |
| 109 | static bool ImportKey(FileTest *t, KeyMap *key_map, |
| 110 | EVP_PKEY *(*parse_func)(CBS *cbs), |
| 111 | int (*marshal_func)(CBB *cbb, const EVP_PKEY *key)) { |
| 112 | std::vector<uint8_t> input; |
| 113 | if (!t->GetBytes(&input, "Input")) { |
| 114 | return false; |
| 115 | } |
| 116 |
| 117 | CBS cbs; |
| 118 | CBS_init(&cbs, input.data(), input.size()); |
| 119 | bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs)); |
| 120 | if (!pkey) { |
| 121 | return false; |
| 122 | } |
| 123 |
| 124 | std::string key_type; |
| 125 | if (!t->GetAttribute(&key_type, "Type")) { |
| 126 | return false; |
| 127 | } |
| 128 | EXPECT_EQ(GetKeyType(t, key_type), EVP_PKEY_id(pkey.get())); |
| 129 |
| 130 | // The key must re-encode correctly. |
| 131 | bssl::ScopedCBB cbb; |
| 132 | uint8_t *der; |
| 133 | size_t der_len; |
| 134 | if (!CBB_init(cbb.get(), 0) || |
| 135 | !marshal_func(cbb.get(), pkey.get()) || |
| 136 | !CBB_finish(cbb.get(), &der, &der_len)) { |
| 137 | return false; |
| 138 | } |
| 139 | bssl::UniquePtr<uint8_t> free_der(der); |
| 140 |
| 141 | std::vector<uint8_t> output = input; |
| 142 | if (t->HasAttribute("Output") && |
| 143 | !t->GetBytes(&output, "Output")) { |
| 144 | return false; |
| 145 | } |
| 146 | EXPECT_EQ(Bytes(output), Bytes(der, der_len)) |
| 147 | << "Re-encoding the key did not match."; |
| 148 |
| 149 | if (t->HasAttribute("ExpectNoRawPrivate")) { |
| 150 | size_t len; |
| 151 | EXPECT_FALSE(EVP_PKEY_get_raw_private_key(pkey.get(), nullptr, &len)); |
| 152 | } else if (t->HasAttribute("ExpectRawPrivate")) { |
| 153 | std::vector<uint8_t> expected; |
| 154 | if (!t->GetBytes(&expected, "ExpectRawPrivate")) { |
| 155 | return false; |
| 156 | } |
| 157 |
| 158 | std::vector<uint8_t> raw; |
| 159 | size_t len; |
| 160 | if (!EVP_PKEY_get_raw_private_key(pkey.get(), nullptr, &len)) { |
| 161 | return false; |
| 162 | } |
| 163 | raw.resize(len); |
| 164 | if (!EVP_PKEY_get_raw_private_key(pkey.get(), raw.data(), &len)) { |
| 165 | return false; |
| 166 | } |
| 167 | raw.resize(len); |
| 168 | EXPECT_EQ(Bytes(raw), Bytes(expected)); |
| 169 |
| 170 | // Short buffers should be rejected. |
| 171 | raw.resize(len - 1); |
| 172 | len = raw.size(); |
| 173 | EXPECT_FALSE(EVP_PKEY_get_raw_private_key(pkey.get(), raw.data(), &len)); |
| 174 | } |
| 175 |
| 176 | if (t->HasAttribute("ExpectNoRawPublic")) { |
| 177 | size_t len; |
| 178 | EXPECT_FALSE(EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len)); |
| 179 | } else if (t->HasAttribute("ExpectRawPublic")) { |
| 180 | std::vector<uint8_t> expected; |
| 181 | if (!t->GetBytes(&expected, "ExpectRawPublic")) { |
| 182 | return false; |
| 183 | } |
| 184 |
| 185 | std::vector<uint8_t> raw; |
| 186 | size_t len; |
| 187 | if (!EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len)) { |
| 188 | return false; |
| 189 | } |
| 190 | raw.resize(len); |
| 191 | if (!EVP_PKEY_get_raw_public_key(pkey.get(), raw.data(), &len)) { |
| 192 | return false; |
| 193 | } |
| 194 | raw.resize(len); |
| 195 | EXPECT_EQ(Bytes(raw), Bytes(expected)); |
| 196 |
| 197 | // Short buffers should be rejected. |
| 198 | raw.resize(len - 1); |
| 199 | len = raw.size(); |
| 200 | EXPECT_FALSE(EVP_PKEY_get_raw_public_key(pkey.get(), raw.data(), &len)); |
| 201 | } |
| 202 |
| 203 | // Save the key for future tests. |
| 204 | const std::string &key_name = t->GetParameter(); |
| 205 | EXPECT_EQ(0u, key_map->count(key_name)) << "Duplicate key: " << key_name; |
| 206 | (*key_map)[key_name] = std::move(pkey); |
| 207 | return true; |
| 208 | } |
| 209 |
| 210 | static bool GetOptionalBignum(FileTest *t, bssl::UniquePtr<BIGNUM> *out, |
| 211 | const std::string &key) { |
| 212 | if (!t->HasAttribute(key)) { |
| 213 | *out = nullptr; |
| 214 | return true; |
| 215 | } |
| 216 |
| 217 | std::vector<uint8_t> bytes; |
| 218 | if (!t->GetBytes(&bytes, key)) { |
| 219 | return false; |
| 220 | } |
| 221 |
| 222 | out->reset(BN_bin2bn(bytes.data(), bytes.size(), nullptr)); |
| 223 | return *out != nullptr; |
| 224 | } |
| 225 |
| 226 | static bool ImportDHKey(FileTest *t, KeyMap *key_map) { |
| 227 | bssl::UniquePtr<BIGNUM> p, q, g, pub_key, priv_key; |
| 228 | if (!GetOptionalBignum(t, &p, "P") || // |
| 229 | !GetOptionalBignum(t, &q, "Q") || // |
| 230 | !GetOptionalBignum(t, &g, "G") || |
| 231 | !GetOptionalBignum(t, &pub_key, "Public") || |
| 232 | !GetOptionalBignum(t, &priv_key, "Private")) { |
| 233 | return false; |
| 234 | } |
| 235 |
| 236 | bssl::UniquePtr<DH> dh(DH_new()); |
| 237 | if (dh == nullptr || !DH_set0_pqg(dh.get(), p.get(), q.get(), g.get())) { |
| 238 | return false; |
| 239 | } |
| 240 | // |DH_set0_pqg| takes ownership on success. |
| 241 | p.release(); |
| 242 | q.release(); |
| 243 | g.release(); |
| 244 |
| 245 | if (!DH_set0_key(dh.get(), pub_key.get(), priv_key.get())) { |
| 246 | return false; |
| 247 | } |
| 248 | // |DH_set0_key| takes ownership on success. |
| 249 | pub_key.release(); |
| 250 | priv_key.release(); |
| 251 |
| 252 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 253 | if (pkey == nullptr || !EVP_PKEY_set1_DH(pkey.get(), dh.get())) { |
| 254 | return false; |
| 255 | } |
| 256 |
| 257 | // Save the key for future tests. |
| 258 | const std::string &key_name = t->GetParameter(); |
| 259 | EXPECT_EQ(0u, key_map->count(key_name)) << "Duplicate key: " << key_name; |
| 260 | (*key_map)[key_name] = std::move(pkey); |
| 261 | return true; |
| 262 | } |
| 263 |
| 264 | // SetupContext configures |ctx| based on attributes in |t|, with the exception |
| 265 | // of the signing digest which must be configured externally. |
| 266 | static bool SetupContext(FileTest *t, KeyMap *key_map, EVP_PKEY_CTX *ctx) { |
| 267 | if (t->HasAttribute("RSAPadding")) { |
| 268 | int padding; |
| 269 | if (!GetRSAPadding(t, &padding, t->GetAttributeOrDie("RSAPadding")) || |
| 270 | !EVP_PKEY_CTX_set_rsa_padding(ctx, padding)) { |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | if (t->HasAttribute("PSSSaltLength") && |
| 275 | !EVP_PKEY_CTX_set_rsa_pss_saltlen( |
| 276 | ctx, atoi(t->GetAttributeOrDie("PSSSaltLength").c_str()))) { |
| 277 | return false; |
| 278 | } |
| 279 | if (t->HasAttribute("MGF1Digest")) { |
| 280 | const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("MGF1Digest")); |
| 281 | if (digest == nullptr || !EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, digest)) { |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | if (t->HasAttribute("OAEPDigest")) { |
| 286 | const EVP_MD *digest = GetDigest(t, t->GetAttributeOrDie("OAEPDigest")); |
| 287 | if (digest == nullptr || !EVP_PKEY_CTX_set_rsa_oaep_md(ctx, digest)) { |
| 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | if (t->HasAttribute("OAEPLabel")) { |
| 292 | std::vector<uint8_t> label; |
| 293 | if (!t->GetBytes(&label, "OAEPLabel")) { |
| 294 | return false; |
| 295 | } |
| 296 | // For historical reasons, |EVP_PKEY_CTX_set0_rsa_oaep_label| expects to be |
| 297 | // take ownership of the input. |
| 298 | bssl::UniquePtr<uint8_t> buf(reinterpret_cast<uint8_t *>( |
| 299 | OPENSSL_memdup(label.data(), label.size()))); |
| 300 | if (!buf || |
| 301 | !EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, buf.get(), label.size())) { |
| 302 | return false; |
| 303 | } |
| 304 | buf.release(); |
| 305 | } |
| 306 | if (t->HasAttribute("DerivePeer")) { |
| 307 | std::string derive_peer = t->GetAttributeOrDie("DerivePeer"); |
| 308 | if (key_map->count(derive_peer) == 0) { |
| 309 | ADD_FAILURE() << "Could not find key " << derive_peer; |
| 310 | return false; |
| 311 | } |
| 312 | EVP_PKEY *derive_peer_key = (*key_map)[derive_peer].get(); |
| 313 | if (!EVP_PKEY_derive_set_peer(ctx, derive_peer_key)) { |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | if (t->HasAttribute("DiffieHellmanPad") && !EVP_PKEY_CTX_set_dh_pad(ctx, 1)) { |
| 318 | return false; |
| 319 | } |
| 320 | return true; |
| 321 | } |
| 322 |
| 323 | static bool TestDerive(FileTest *t, KeyMap *key_map, EVP_PKEY *key) { |
| 324 | bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 325 | if (!ctx || |
| 326 | !EVP_PKEY_derive_init(ctx.get()) || |
| 327 | !SetupContext(t, key_map, ctx.get())) { |
| 328 | return false; |
| 329 | } |
| 330 |
| 331 | bssl::UniquePtr<EVP_PKEY_CTX> copy(EVP_PKEY_CTX_dup(ctx.get())); |
| 332 | if (!copy) { |
| 333 | return false; |
| 334 | } |
| 335 |
| 336 | for (EVP_PKEY_CTX *pctx : {ctx.get(), copy.get()}) { |
| 337 | size_t len; |
| 338 | std::vector<uint8_t> actual, output; |
| 339 | if (!EVP_PKEY_derive(pctx, nullptr, &len)) { |
| 340 | return false; |
| 341 | } |
| 342 | actual.resize(len); |
| 343 | if (!EVP_PKEY_derive(pctx, actual.data(), &len)) { |
| 344 | return false; |
| 345 | } |
| 346 | actual.resize(len); |
| 347 |
| 348 | // Defer looking up the attribute so Error works properly. |
| 349 | if (!t->GetBytes(&output, "Output")) { |
| 350 | return false; |
| 351 | } |
| 352 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 353 |
| 354 | // Test when the buffer is too large. |
| 355 | actual.resize(len + 1); |
| 356 | len = actual.size(); |
| 357 | if (!EVP_PKEY_derive(pctx, actual.data(), &len)) { |
| 358 | return false; |
| 359 | } |
| 360 | actual.resize(len); |
| 361 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 362 |
| 363 | // Test when the buffer is too small. |
| 364 | actual.resize(len - 1); |
| 365 | len = actual.size(); |
| 366 | if (t->HasAttribute("SmallBufferTruncates")) { |
| 367 | if (!EVP_PKEY_derive(pctx, actual.data(), &len)) { |
| 368 | return false; |
| 369 | } |
| 370 | actual.resize(len); |
| 371 | EXPECT_EQ(Bytes(output.data(), len), Bytes(actual)); |
| 372 | } else { |
| 373 | EXPECT_FALSE(EVP_PKEY_derive(pctx, actual.data(), &len)); |
| 374 | ERR_clear_error(); |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 |
| 380 | static bool TestEVP(FileTest *t, KeyMap *key_map) { |
| 381 | if (t->GetType() == "PrivateKey") { |
| 382 | return ImportKey(t, key_map, EVP_parse_private_key, |
| 383 | EVP_marshal_private_key); |
| 384 | } |
| 385 |
| 386 | if (t->GetType() == "PublicKey") { |
| 387 | return ImportKey(t, key_map, EVP_parse_public_key, EVP_marshal_public_key); |
| 388 | } |
| 389 |
| 390 | if (t->GetType() == "DHKey") { |
| 391 | return ImportDHKey(t, key_map); |
| 392 | } |
| 393 |
| 394 | // Load the key. |
| 395 | const std::string &key_name = t->GetParameter(); |
| 396 | if (key_map->count(key_name) == 0) { |
| 397 | ADD_FAILURE() << "Could not find key " << key_name; |
| 398 | return false; |
| 399 | } |
| 400 | EVP_PKEY *key = (*key_map)[key_name].get(); |
| 401 |
| 402 | int (*key_op_init)(EVP_PKEY_CTX *ctx) = nullptr; |
| 403 | int (*key_op)(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, |
| 404 | const uint8_t *in, size_t in_len) = nullptr; |
| 405 | int (*md_op_init)(EVP_MD_CTX * ctx, EVP_PKEY_CTX * *pctx, const EVP_MD *type, |
| 406 | ENGINE *e, EVP_PKEY *pkey) = nullptr; |
| 407 | bool is_verify = false; |
| 408 | if (t->GetType() == "Decrypt") { |
| 409 | key_op_init = EVP_PKEY_decrypt_init; |
| 410 | key_op = EVP_PKEY_decrypt; |
| 411 | } else if (t->GetType() == "Sign") { |
| 412 | key_op_init = EVP_PKEY_sign_init; |
| 413 | key_op = EVP_PKEY_sign; |
| 414 | } else if (t->GetType() == "Verify") { |
| 415 | key_op_init = EVP_PKEY_verify_init; |
| 416 | is_verify = true; |
| 417 | } else if (t->GetType() == "SignMessage") { |
| 418 | md_op_init = EVP_DigestSignInit; |
| 419 | } else if (t->GetType() == "VerifyMessage") { |
| 420 | md_op_init = EVP_DigestVerifyInit; |
| 421 | is_verify = true; |
| 422 | } else if (t->GetType() == "Encrypt") { |
| 423 | key_op_init = EVP_PKEY_encrypt_init; |
| 424 | key_op = EVP_PKEY_encrypt; |
| 425 | } else if (t->GetType() == "Derive") { |
| 426 | return TestDerive(t, key_map, key); |
| 427 | } else { |
| 428 | ADD_FAILURE() << "Unknown test " << t->GetType(); |
| 429 | return false; |
| 430 | } |
| 431 |
| 432 | const EVP_MD *digest = nullptr; |
| 433 | if (t->HasAttribute("Digest")) { |
| 434 | digest = GetDigest(t, t->GetAttributeOrDie("Digest")); |
| 435 | if (digest == nullptr) { |
| 436 | return false; |
| 437 | } |
| 438 | } |
| 439 |
| 440 | // For verify tests, the "output" is the signature. Read it now so that, for |
| 441 | // tests which expect a failure in SetupContext, the attribute is still |
| 442 | // consumed. |
| 443 | std::vector<uint8_t> input, actual, output; |
| 444 | if (!t->GetBytes(&input, "Input") || |
| 445 | (is_verify && !t->GetBytes(&output, "Output"))) { |
| 446 | return false; |
| 447 | } |
| 448 |
| 449 | if (md_op_init) { |
| 450 | bssl::ScopedEVP_MD_CTX ctx, copy; |
| 451 | EVP_PKEY_CTX *pctx; |
| 452 | if (!md_op_init(ctx.get(), &pctx, digest, nullptr, key) || |
| 453 | !SetupContext(t, key_map, pctx) || |
| 454 | !EVP_MD_CTX_copy_ex(copy.get(), ctx.get())) { |
| 455 | return false; |
| 456 | } |
| 457 |
| 458 | if (is_verify) { |
| 459 | return EVP_DigestVerify(ctx.get(), output.data(), output.size(), |
| 460 | input.data(), input.size()) && |
| 461 | EVP_DigestVerify(copy.get(), output.data(), output.size(), |
| 462 | input.data(), input.size()); |
| 463 | } |
| 464 |
| 465 | size_t len; |
| 466 | if (!EVP_DigestSign(ctx.get(), nullptr, &len, input.data(), input.size())) { |
| 467 | return false; |
| 468 | } |
| 469 | actual.resize(len); |
| 470 | if (!EVP_DigestSign(ctx.get(), actual.data(), &len, input.data(), |
| 471 | input.size()) || |
| 472 | !t->GetBytes(&output, "Output")) { |
| 473 | return false; |
| 474 | } |
| 475 | actual.resize(len); |
| 476 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 477 |
| 478 | // Repeat the test with |copy|, to check |EVP_MD_CTX_copy_ex| duplicated |
| 479 | // everything. |
| 480 | if (!EVP_DigestSign(copy.get(), nullptr, &len, input.data(), |
| 481 | input.size())) { |
| 482 | return false; |
| 483 | } |
| 484 | actual.resize(len); |
| 485 | if (!EVP_DigestSign(copy.get(), actual.data(), &len, input.data(), |
| 486 | input.size()) || |
| 487 | !t->GetBytes(&output, "Output")) { |
| 488 | return false; |
| 489 | } |
| 490 | actual.resize(len); |
| 491 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 492 | return true; |
| 493 | } |
| 494 |
| 495 | bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 496 | if (!ctx || |
| 497 | !key_op_init(ctx.get()) || |
| 498 | (digest != nullptr && |
| 499 | !EVP_PKEY_CTX_set_signature_md(ctx.get(), digest)) || |
| 500 | !SetupContext(t, key_map, ctx.get())) { |
| 501 | return false; |
| 502 | } |
| 503 |
| 504 | bssl::UniquePtr<EVP_PKEY_CTX> copy(EVP_PKEY_CTX_dup(ctx.get())); |
| 505 | if (!copy) { |
| 506 | return false; |
| 507 | } |
| 508 |
| 509 | if (is_verify) { |
| 510 | return EVP_PKEY_verify(ctx.get(), output.data(), output.size(), |
| 511 | input.data(), input.size()) && |
| 512 | EVP_PKEY_verify(copy.get(), output.data(), output.size(), |
| 513 | input.data(), input.size()); |
| 514 | } |
| 515 |
| 516 | for (EVP_PKEY_CTX *pctx : {ctx.get(), copy.get()}) { |
| 517 | size_t len; |
| 518 | if (!key_op(pctx, nullptr, &len, input.data(), input.size())) { |
| 519 | return false; |
| 520 | } |
| 521 | actual.resize(len); |
| 522 | if (!key_op(pctx, actual.data(), &len, input.data(), input.size())) { |
| 523 | return false; |
| 524 | } |
| 525 |
| 526 | if (t->HasAttribute("CheckDecrypt")) { |
| 527 | // Encryption is non-deterministic, so we check by decrypting. |
| 528 | size_t plaintext_len; |
| 529 | bssl::UniquePtr<EVP_PKEY_CTX> decrypt_ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 530 | if (!decrypt_ctx || |
| 531 | !EVP_PKEY_decrypt_init(decrypt_ctx.get()) || |
| 532 | (digest != nullptr && |
| 533 | !EVP_PKEY_CTX_set_signature_md(decrypt_ctx.get(), digest)) || |
| 534 | !SetupContext(t, key_map, decrypt_ctx.get()) || |
| 535 | !EVP_PKEY_decrypt(decrypt_ctx.get(), nullptr, &plaintext_len, |
| 536 | actual.data(), actual.size())) { |
| 537 | return false; |
| 538 | } |
| 539 | output.resize(plaintext_len); |
| 540 | if (!EVP_PKEY_decrypt(decrypt_ctx.get(), output.data(), &plaintext_len, |
| 541 | actual.data(), actual.size())) { |
| 542 | ADD_FAILURE() << "Could not decrypt result."; |
| 543 | return false; |
| 544 | } |
| 545 | output.resize(plaintext_len); |
| 546 | EXPECT_EQ(Bytes(input), Bytes(output)) << "Decrypted result mismatch."; |
| 547 | } else if (t->HasAttribute("CheckVerify")) { |
| 548 | // Some signature schemes are non-deterministic, so we check by verifying. |
| 549 | bssl::UniquePtr<EVP_PKEY_CTX> verify_ctx(EVP_PKEY_CTX_new(key, nullptr)); |
| 550 | if (!verify_ctx || |
| 551 | !EVP_PKEY_verify_init(verify_ctx.get()) || |
| 552 | (digest != nullptr && |
| 553 | !EVP_PKEY_CTX_set_signature_md(verify_ctx.get(), digest)) || |
| 554 | !SetupContext(t, key_map, verify_ctx.get())) { |
| 555 | return false; |
| 556 | } |
| 557 | if (t->HasAttribute("VerifyPSSSaltLength")) { |
| 558 | if (!EVP_PKEY_CTX_set_rsa_pss_saltlen( |
| 559 | verify_ctx.get(), |
| 560 | atoi(t->GetAttributeOrDie("VerifyPSSSaltLength").c_str()))) { |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | EXPECT_TRUE(EVP_PKEY_verify(verify_ctx.get(), actual.data(), |
| 565 | actual.size(), input.data(), input.size())) |
| 566 | << "Could not verify result."; |
| 567 | } else { |
| 568 | // By default, check by comparing the result against Output. |
| 569 | if (!t->GetBytes(&output, "Output")) { |
| 570 | return false; |
| 571 | } |
| 572 | actual.resize(len); |
| 573 | EXPECT_EQ(Bytes(output), Bytes(actual)); |
| 574 | } |
| 575 | } |
| 576 | return true; |
| 577 | } |
| 578 |
| 579 | TEST(EVPTest, TestVectors) { |
| 580 | KeyMap key_map; |
| 581 | FileTestGTest("crypto/evp/evp_tests.txt", [&](FileTest *t) { |
| 582 | bool result = TestEVP(t, &key_map); |
| 583 | if (t->HasAttribute("Error")) { |
| 584 | ASSERT_FALSE(result) << "Operation unexpectedly succeeded."; |
| 585 | uint32_t err = ERR_peek_error(); |
| 586 | EXPECT_EQ(t->GetAttributeOrDie("Error"), ERR_reason_error_string(err)); |
| 587 | } else if (!result) { |
| 588 | ADD_FAILURE() << "Operation unexpectedly failed."; |
| 589 | } |
| 590 | }); |
| 591 | } |
| 592 |
| 593 | static void RunWycheproofVerifyTest(const char *path) { |
| 594 | SCOPED_TRACE(path); |
| 595 | FileTestGTest(path, [](FileTest *t) { |
| 596 | t->IgnoreAllUnusedInstructions(); |
| 597 |
| 598 | std::vector<uint8_t> der; |
| 599 | ASSERT_TRUE(t->GetInstructionBytes(&der, "keyDer")); |
| 600 | CBS cbs; |
| 601 | CBS_init(&cbs, der.data(), der.size()); |
| 602 | bssl::UniquePtr<EVP_PKEY> key(EVP_parse_public_key(&cbs)); |
| 603 | ASSERT_TRUE(key); |
| 604 |
| 605 | const EVP_MD *md = nullptr; |
| 606 | if (t->HasInstruction("sha")) { |
| 607 | md = GetWycheproofDigest(t, "sha", true); |
| 608 | ASSERT_TRUE(md); |
| 609 | } |
| 610 |
| 611 | bool is_pss = t->HasInstruction("mgf"); |
| 612 | const EVP_MD *mgf1_md = nullptr; |
| 613 | int pss_salt_len = -1; |
| 614 | if (is_pss) { |
| 615 | ASSERT_EQ("MGF1", t->GetInstructionOrDie("mgf")); |
| 616 | mgf1_md = GetWycheproofDigest(t, "mgfSha", true); |
| 617 |
| 618 | std::string s_len; |
| 619 | ASSERT_TRUE(t->GetInstruction(&s_len, "sLen")); |
| 620 | pss_salt_len = atoi(s_len.c_str()); |
| 621 | } |
| 622 |
| 623 | std::vector<uint8_t> msg; |
| 624 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 625 | std::vector<uint8_t> sig; |
| 626 | ASSERT_TRUE(t->GetBytes(&sig, "sig")); |
| 627 | WycheproofResult result; |
| 628 | ASSERT_TRUE(GetWycheproofResult(t, &result)); |
| 629 |
| 630 | if (EVP_PKEY_id(key.get()) == EVP_PKEY_DSA) { |
| 631 | // DSA is deprecated and is not usable via EVP. |
| 632 | DSA *dsa = EVP_PKEY_get0_DSA(key.get()); |
| 633 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 634 | unsigned digest_len; |
| 635 | ASSERT_TRUE( |
| 636 | EVP_Digest(msg.data(), msg.size(), digest, &digest_len, md, nullptr)); |
| 637 | int valid; |
| 638 | bool sig_ok = DSA_check_signature(&valid, digest, digest_len, sig.data(), |
| 639 | sig.size(), dsa) && |
| 640 | valid; |
| 641 | EXPECT_EQ(sig_ok, result.IsValid()); |
| 642 | } else { |
| 643 | bssl::ScopedEVP_MD_CTX ctx; |
| 644 | EVP_PKEY_CTX *pctx; |
| 645 | ASSERT_TRUE( |
| 646 | EVP_DigestVerifyInit(ctx.get(), &pctx, md, nullptr, key.get())); |
| 647 | if (is_pss) { |
| 648 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING)); |
| 649 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_mgf1_md(pctx, mgf1_md)); |
| 650 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, pss_salt_len)); |
| 651 | } |
| 652 | int ret = EVP_DigestVerify(ctx.get(), sig.data(), sig.size(), msg.data(), |
| 653 | msg.size()); |
| 654 | // BoringSSL does not enforce policies on weak keys and leaves it to the |
| 655 | // caller. |
| 656 | EXPECT_EQ(ret, |
| 657 | result.IsValid({"SmallModulus", "SmallPublicKey", "WeakHash"}) |
| 658 | ? 1 |
| 659 | : 0); |
| 660 | } |
| 661 | }); |
| 662 | } |
| 663 |
| 664 | TEST(EVPTest, WycheproofDSA) { |
| 665 | RunWycheproofVerifyTest("third_party/wycheproof_testvectors/dsa_test.txt"); |
| 666 | } |
| 667 |
| 668 | TEST(EVPTest, WycheproofECDSAP224) { |
| 669 | RunWycheproofVerifyTest( |
| 670 | "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha224_test.txt"); |
| 671 | RunWycheproofVerifyTest( |
| 672 | "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha256_test.txt"); |
| 673 | RunWycheproofVerifyTest( |
| 674 | "third_party/wycheproof_testvectors/ecdsa_secp224r1_sha512_test.txt"); |
| 675 | } |
| 676 |
| 677 | TEST(EVPTest, WycheproofECDSAP256) { |
| 678 | RunWycheproofVerifyTest( |
| 679 | "third_party/wycheproof_testvectors/ecdsa_secp256r1_sha256_test.txt"); |
| 680 | RunWycheproofVerifyTest( |
| 681 | "third_party/wycheproof_testvectors/ecdsa_secp256r1_sha512_test.txt"); |
| 682 | } |
| 683 |
| 684 | TEST(EVPTest, WycheproofECDSAP384) { |
| 685 | RunWycheproofVerifyTest( |
| 686 | "third_party/wycheproof_testvectors/ecdsa_secp384r1_sha384_test.txt"); |
| 687 | } |
| 688 |
| 689 | TEST(EVPTest, WycheproofECDSAP521) { |
| 690 | RunWycheproofVerifyTest( |
| 691 | "third_party/wycheproof_testvectors/ecdsa_secp384r1_sha512_test.txt"); |
| 692 | RunWycheproofVerifyTest( |
| 693 | "third_party/wycheproof_testvectors/ecdsa_secp521r1_sha512_test.txt"); |
| 694 | } |
| 695 |
| 696 | TEST(EVPTest, WycheproofEdDSA) { |
| 697 | RunWycheproofVerifyTest("third_party/wycheproof_testvectors/eddsa_test.txt"); |
| 698 | } |
| 699 |
| 700 | TEST(EVPTest, WycheproofRSAPKCS1) { |
| 701 | RunWycheproofVerifyTest( |
| 702 | "third_party/wycheproof_testvectors/rsa_signature_2048_sha224_test.txt"); |
| 703 | RunWycheproofVerifyTest( |
| 704 | "third_party/wycheproof_testvectors/rsa_signature_2048_sha256_test.txt"); |
| 705 | RunWycheproofVerifyTest( |
| 706 | "third_party/wycheproof_testvectors/rsa_signature_2048_sha384_test.txt"); |
| 707 | RunWycheproofVerifyTest( |
| 708 | "third_party/wycheproof_testvectors/rsa_signature_2048_sha512_test.txt"); |
| 709 | RunWycheproofVerifyTest( |
| 710 | "third_party/wycheproof_testvectors/rsa_signature_3072_sha256_test.txt"); |
| 711 | RunWycheproofVerifyTest( |
| 712 | "third_party/wycheproof_testvectors/rsa_signature_3072_sha384_test.txt"); |
| 713 | RunWycheproofVerifyTest( |
| 714 | "third_party/wycheproof_testvectors/rsa_signature_3072_sha512_test.txt"); |
| 715 | RunWycheproofVerifyTest( |
| 716 | "third_party/wycheproof_testvectors/rsa_signature_4096_sha384_test.txt"); |
| 717 | RunWycheproofVerifyTest( |
| 718 | "third_party/wycheproof_testvectors/rsa_signature_4096_sha512_test.txt"); |
| 719 | // TODO(davidben): Is this file redundant with the tests above? |
| 720 | RunWycheproofVerifyTest( |
| 721 | "third_party/wycheproof_testvectors/rsa_signature_test.txt"); |
| 722 | } |
| 723 |
| 724 | TEST(EVPTest, WycheproofRSAPKCS1Sign) { |
| 725 | FileTestGTest( |
| 726 | "third_party/wycheproof_testvectors/rsa_sig_gen_misc_test.txt", |
| 727 | [](FileTest *t) { |
| 728 | t->IgnoreAllUnusedInstructions(); |
| 729 |
| 730 | std::vector<uint8_t> pkcs8; |
| 731 | ASSERT_TRUE(t->GetInstructionBytes(&pkcs8, "privateKeyPkcs8")); |
| 732 | CBS cbs; |
| 733 | CBS_init(&cbs, pkcs8.data(), pkcs8.size()); |
| 734 | bssl::UniquePtr<EVP_PKEY> key(EVP_parse_private_key(&cbs)); |
| 735 | ASSERT_TRUE(key); |
| 736 |
| 737 | const EVP_MD *md = GetWycheproofDigest(t, "sha", true); |
| 738 | ASSERT_TRUE(md); |
| 739 |
| 740 | std::vector<uint8_t> msg, sig; |
| 741 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 742 | ASSERT_TRUE(t->GetBytes(&sig, "sig")); |
| 743 | WycheproofResult result; |
| 744 | ASSERT_TRUE(GetWycheproofResult(t, &result)); |
| 745 |
| 746 | bssl::ScopedEVP_MD_CTX ctx; |
| 747 | EVP_PKEY_CTX *pctx; |
| 748 | ASSERT_TRUE( |
| 749 | EVP_DigestSignInit(ctx.get(), &pctx, md, nullptr, key.get())); |
| 750 | std::vector<uint8_t> out(EVP_PKEY_size(key.get())); |
| 751 | size_t len = out.size(); |
| 752 | int ret = |
| 753 | EVP_DigestSign(ctx.get(), out.data(), &len, msg.data(), msg.size()); |
| 754 | // BoringSSL does not enforce policies on weak keys and leaves it to the |
| 755 | // caller. |
| 756 | bool is_valid = |
| 757 | result.IsValid({"SmallModulus", "SmallPublicKey", "WeakHash"}); |
| 758 | EXPECT_EQ(ret, is_valid ? 1 : 0); |
| 759 | if (is_valid) { |
| 760 | out.resize(len); |
| 761 | EXPECT_EQ(Bytes(sig), Bytes(out)); |
| 762 | } |
| 763 | }); |
| 764 | } |
| 765 |
| 766 | TEST(EVPTest, WycheproofRSAPSS) { |
| 767 | RunWycheproofVerifyTest( |
| 768 | "third_party/wycheproof_testvectors/rsa_pss_2048_sha1_mgf1_20_test.txt"); |
| 769 | RunWycheproofVerifyTest( |
| 770 | "third_party/wycheproof_testvectors/rsa_pss_2048_sha256_mgf1_0_test.txt"); |
| 771 | RunWycheproofVerifyTest( |
| 772 | "third_party/wycheproof_testvectors/" |
| 773 | "rsa_pss_2048_sha256_mgf1_32_test.txt"); |
| 774 | RunWycheproofVerifyTest( |
| 775 | "third_party/wycheproof_testvectors/" |
| 776 | "rsa_pss_3072_sha256_mgf1_32_test.txt"); |
| 777 | RunWycheproofVerifyTest( |
| 778 | "third_party/wycheproof_testvectors/" |
| 779 | "rsa_pss_4096_sha256_mgf1_32_test.txt"); |
| 780 | RunWycheproofVerifyTest( |
| 781 | "third_party/wycheproof_testvectors/" |
| 782 | "rsa_pss_4096_sha512_mgf1_32_test.txt"); |
| 783 | RunWycheproofVerifyTest( |
| 784 | "third_party/wycheproof_testvectors/rsa_pss_misc_test.txt"); |
| 785 | } |
| 786 |
| 787 | static void RunWycheproofDecryptTest( |
| 788 | const char *path, |
| 789 | std::function<void(FileTest *, EVP_PKEY_CTX *)> setup_cb) { |
| 790 | FileTestGTest(path, [&](FileTest *t) { |
| 791 | t->IgnoreAllUnusedInstructions(); |
| 792 |
| 793 | std::vector<uint8_t> pkcs8; |
| 794 | ASSERT_TRUE(t->GetInstructionBytes(&pkcs8, "privateKeyPkcs8")); |
| 795 | CBS cbs; |
| 796 | CBS_init(&cbs, pkcs8.data(), pkcs8.size()); |
| 797 | bssl::UniquePtr<EVP_PKEY> key(EVP_parse_private_key(&cbs)); |
| 798 | ASSERT_TRUE(key); |
| 799 |
| 800 | std::vector<uint8_t> ct, msg; |
| 801 | ASSERT_TRUE(t->GetBytes(&ct, "ct")); |
| 802 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 803 | WycheproofResult result; |
| 804 | ASSERT_TRUE(GetWycheproofResult(t, &result)); |
| 805 |
| 806 | bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key.get(), nullptr)); |
| 807 | ASSERT_TRUE(ctx); |
| 808 | ASSERT_TRUE(EVP_PKEY_decrypt_init(ctx.get())); |
| 809 | ASSERT_NO_FATAL_FAILURE(setup_cb(t, ctx.get())); |
| 810 | std::vector<uint8_t> out(EVP_PKEY_size(key.get())); |
| 811 | size_t len = out.size(); |
| 812 | int ret = |
| 813 | EVP_PKEY_decrypt(ctx.get(), out.data(), &len, ct.data(), ct.size()); |
| 814 | // BoringSSL does not enforce policies on weak keys and leaves it to the |
| 815 | // caller. |
| 816 | bool is_valid = result.IsValid({"SmallModulus"}); |
| 817 | EXPECT_EQ(ret, is_valid ? 1 : 0); |
| 818 | if (is_valid) { |
| 819 | out.resize(len); |
| 820 | EXPECT_EQ(Bytes(msg), Bytes(out)); |
| 821 | } |
| 822 | }); |
| 823 | } |
| 824 |
| 825 | static void RunWycheproofOAEPTest(const char *path) { |
| 826 | RunWycheproofDecryptTest(path, [](FileTest *t, EVP_PKEY_CTX *ctx) { |
| 827 | const EVP_MD *md = GetWycheproofDigest(t, "sha", true); |
| 828 | ASSERT_TRUE(md); |
| 829 | const EVP_MD *mgf1_md = GetWycheproofDigest(t, "mgfSha", true); |
| 830 | ASSERT_TRUE(mgf1_md); |
| 831 | std::vector<uint8_t> label; |
| 832 | ASSERT_TRUE(t->GetBytes(&label, "label")); |
| 833 |
| 834 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING)); |
| 835 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md)); |
| 836 | ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, mgf1_md)); |
| 837 | bssl::UniquePtr<uint8_t> label_copy( |
| 838 | static_cast<uint8_t *>(OPENSSL_memdup(label.data(), label.size()))); |
| 839 | ASSERT_TRUE(label_copy || label.empty()); |
| 840 | ASSERT_TRUE( |
| 841 | EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy.get(), label.size())); |
| 842 | // |EVP_PKEY_CTX_set0_rsa_oaep_label| takes ownership on success. |
| 843 | label_copy.release(); |
| 844 | }); |
| 845 | } |
| 846 |
| 847 | TEST(EVPTest, WycheproofRSAOAEP2048) { |
| 848 | RunWycheproofOAEPTest( |
| 849 | "third_party/wycheproof_testvectors/" |
| 850 | "rsa_oaep_2048_sha1_mgf1sha1_test.txt"); |
| 851 | RunWycheproofOAEPTest( |
| 852 | "third_party/wycheproof_testvectors/" |
| 853 | "rsa_oaep_2048_sha224_mgf1sha1_test.txt"); |
| 854 | RunWycheproofOAEPTest( |
| 855 | "third_party/wycheproof_testvectors/" |
| 856 | "rsa_oaep_2048_sha224_mgf1sha224_test.txt"); |
| 857 | RunWycheproofOAEPTest( |
| 858 | "third_party/wycheproof_testvectors/" |
| 859 | "rsa_oaep_2048_sha256_mgf1sha1_test.txt"); |
| 860 | RunWycheproofOAEPTest( |
| 861 | "third_party/wycheproof_testvectors/" |
| 862 | "rsa_oaep_2048_sha256_mgf1sha256_test.txt"); |
| 863 | RunWycheproofOAEPTest( |
| 864 | "third_party/wycheproof_testvectors/" |
| 865 | "rsa_oaep_2048_sha384_mgf1sha1_test.txt"); |
| 866 | RunWycheproofOAEPTest( |
| 867 | "third_party/wycheproof_testvectors/" |
| 868 | "rsa_oaep_2048_sha384_mgf1sha384_test.txt"); |
| 869 | RunWycheproofOAEPTest( |
| 870 | "third_party/wycheproof_testvectors/" |
| 871 | "rsa_oaep_2048_sha512_mgf1sha1_test.txt"); |
| 872 | RunWycheproofOAEPTest( |
| 873 | "third_party/wycheproof_testvectors/" |
| 874 | "rsa_oaep_2048_sha512_mgf1sha512_test.txt"); |
| 875 | } |
| 876 |
| 877 | TEST(EVPTest, WycheproofRSAOAEP3072) { |
| 878 | RunWycheproofOAEPTest( |
| 879 | "third_party/wycheproof_testvectors/" |
| 880 | "rsa_oaep_3072_sha256_mgf1sha1_test.txt"); |
| 881 | RunWycheproofOAEPTest( |
| 882 | "third_party/wycheproof_testvectors/" |
| 883 | "rsa_oaep_3072_sha256_mgf1sha256_test.txt"); |
| 884 | RunWycheproofOAEPTest( |
| 885 | "third_party/wycheproof_testvectors/" |
| 886 | "rsa_oaep_3072_sha512_mgf1sha1_test.txt"); |
| 887 | RunWycheproofOAEPTest( |
| 888 | "third_party/wycheproof_testvectors/" |
| 889 | "rsa_oaep_3072_sha512_mgf1sha512_test.txt"); |
| 890 | } |
| 891 |
| 892 | TEST(EVPTest, WycheproofRSAOAEP4096) { |
| 893 | RunWycheproofOAEPTest( |
| 894 | "third_party/wycheproof_testvectors/" |
| 895 | "rsa_oaep_4096_sha256_mgf1sha1_test.txt"); |
| 896 | RunWycheproofOAEPTest( |
| 897 | "third_party/wycheproof_testvectors/" |
| 898 | "rsa_oaep_4096_sha256_mgf1sha256_test.txt"); |
| 899 | RunWycheproofOAEPTest( |
| 900 | "third_party/wycheproof_testvectors/" |
| 901 | "rsa_oaep_4096_sha512_mgf1sha1_test.txt"); |
| 902 | RunWycheproofOAEPTest( |
| 903 | "third_party/wycheproof_testvectors/" |
| 904 | "rsa_oaep_4096_sha512_mgf1sha512_test.txt"); |
| 905 | } |
| 906 |
| 907 | TEST(EVPTest, WycheproofRSAOAEPMisc) { |
| 908 | RunWycheproofOAEPTest( |
| 909 | "third_party/wycheproof_testvectors/rsa_oaep_misc_test.txt"); |
| 910 | } |
| 911 |
| 912 | static void RunWycheproofPKCS1DecryptTest(const char *path) { |
| 913 | RunWycheproofDecryptTest(path, [](FileTest *t, EVP_PKEY_CTX *ctx) { |
| 914 | // No setup needed. PKCS#1 is, sadly, the default. |
| 915 | }); |
| 916 | } |
| 917 |
| 918 | TEST(EVPTest, WycheproofRSAPKCS1Decrypt) { |
| 919 | RunWycheproofPKCS1DecryptTest( |
| 920 | "third_party/wycheproof_testvectors/rsa_pkcs1_2048_test.txt"); |
| 921 | RunWycheproofPKCS1DecryptTest( |
| 922 | "third_party/wycheproof_testvectors/rsa_pkcs1_3072_test.txt"); |
| 923 | RunWycheproofPKCS1DecryptTest( |
| 924 | "third_party/wycheproof_testvectors/rsa_pkcs1_4096_test.txt"); |
| 925 | } |
| 926 |