diff --git a/core/libs/prx/libSceLibreSsl3/ParentExports.cpp b/core/libs/prx/libSceLibreSsl3/ParentExports.cpp index 895e66fe7107..f908746a42c0 100644 --- a/core/libs/prx/libSceLibreSsl3/ParentExports.cpp +++ b/core/libs/prx/libSceLibreSsl3/ParentExports.cpp @@ -114,9 +114,6 @@ APS5_EXPORT("6LrAQY0Ka4c", libreSsl3Forward_EVP_DigestUpdate_nid_no_patch); __asm__(APS5_ASM_FUNCTION("libreSsl3Forward_CMAC_Final_nid_no_patch") "jmp *__imp_CMAC_Final_nid_postfix(%rip)\n"); APS5_EXPORT("6MM7ng9P+18", libreSsl3Forward_CMAC_Final_nid_no_patch); -__asm__(APS5_ASM_FUNCTION("libreSsl3Forward_EVP_DigestVerifyFinal_nid_no_patch") "jmp *__imp_EVP_DigestVerifyFinal_nid_postfix(%rip)\n"); -APS5_EXPORT("6QfhFhAfeIo", libreSsl3Forward_EVP_DigestVerifyFinal_nid_no_patch); - __asm__(APS5_ASM_FUNCTION("libreSsl3Forward_ECDSA_sign_nid_no_patch") "jmp *__imp_ECDSA_sign_nid_postfix(%rip)\n"); APS5_EXPORT("6qGhMiG+4-0", libreSsl3Forward_ECDSA_sign_nid_no_patch); @@ -552,9 +549,6 @@ APS5_EXPORT("XpAJ1Ckg-q4", libreSsl3Forward_CMAC_Update_nid_no_patch); __asm__(APS5_ASM_FUNCTION("libreSsl3Forward_OCSP_resp_count_nid_no_patch") "jmp *__imp_OCSP_resp_count_nid_postfix(%rip)\n"); APS5_EXPORT("Y4NRPm9miAc", libreSsl3Forward_OCSP_resp_count_nid_no_patch); -__asm__(APS5_ASM_FUNCTION("libreSsl3Forward_EVP_DigestVerifyInit_nid_no_patch") "jmp *__imp_EVP_DigestVerifyInit_nid_postfix(%rip)\n"); -APS5_EXPORT("Y5ChdsxEESU", libreSsl3Forward_EVP_DigestVerifyInit_nid_no_patch); - __asm__(APS5_ASM_FUNCTION("libreSsl3Forward_ASN1_STRING_data_nid_no_patch") "jmp *__imp_ASN1_STRING_data_nid_postfix(%rip)\n"); APS5_EXPORT("YcJlolMfV0k", libreSsl3Forward_ASN1_STRING_data_nid_no_patch); @@ -948,9 +942,6 @@ APS5_EXPORT("ptYKwIWRtmg", libreSsl3Forward_SSL_get_wbio_nid_no_patch); __asm__(APS5_ASM_FUNCTION("libreSsl3Forward_X509_STORE_new_nid_no_patch") "jmp *__imp_X509_STORE_new_nid_postfix(%rip)\n"); APS5_EXPORT("q6-H-qFeRJk", libreSsl3Forward_X509_STORE_new_nid_no_patch); -__asm__(APS5_ASM_FUNCTION("libreSsl3Forward_EVP_PKEY_get1_EC_KEY_nid_no_patch") "jmp *__imp_EVP_PKEY_get1_EC_KEY_nid_postfix(%rip)\n"); -APS5_EXPORT("qB+V9Qvf9qg", libreSsl3Forward_EVP_PKEY_get1_EC_KEY_nid_no_patch); - __asm__(APS5_ASM_FUNCTION("libreSsl3Forward_EC_KEY_free_nid_no_patch") "jmp *__imp_EC_KEY_free_nid_postfix(%rip)\n"); APS5_EXPORT("qBVx0vmleoc", libreSsl3Forward_EC_KEY_free_nid_no_patch); diff --git a/core/libs/prx/libSceLibreSsl3/include/Api.hpp b/core/libs/prx/libSceLibreSsl3/include/Api.hpp index 4c9f13994222..32c4d29f79cc 100644 --- a/core/libs/prx/libSceLibreSsl3/include/Api.hpp +++ b/core/libs/prx/libSceLibreSsl3/include/Api.hpp @@ -250,6 +250,11 @@ int APS5_VABI EVP_DigestSignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, uns int APS5_VABI EVP_DigestSignInit_nid_postfix(LibreSsl3::DigestContext* ctx, LibreSsl3PkeyContext** pctx, const LibreSsl3::DigestMethod* type, void* engine, LibreSsl3EvpPkey* pkey); +int APS5_VABI EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(LibreSsl3::DigestContext* ctx, + const unsigned char* sig, std::size_t siglen); +int APS5_VABI EVP_DigestVerifyInit_nid_disambig1_nid_postfix(LibreSsl3::DigestContext* ctx, + LibreSsl3PkeyContext** pctx, const LibreSsl3::DigestMethod* type, void* engine, + LibreSsl3EvpPkey* pkey); int APS5_VABI EVP_EncryptInit_nid_postfix(LibreSslEvpCipherCtx* context, const LibreSslEvpCipher* cipher, const unsigned char* key, const unsigned char* iv); int APS5_VABI EVP_EncryptUpdate_nid_disambig1_nid_postfix(LibreSslEvpCipherCtx* context, unsigned char* out, int* written, const unsigned char* in, int length); int APS5_VABI EVP_DecryptUpdate_nid_disambig1_nid_postfix(LibreSslEvpCipherCtx* context, unsigned char* out, int* written, const unsigned char* in, int length); @@ -274,6 +279,7 @@ void* APS5_VABI EVP_PKEY_get0_DH_nid_postfix(LibreSsl3EvpPkey* key); void* APS5_VABI EVP_PKEY_get0_DSA_nid_postfix(LibreSsl3EvpPkey* key); LibreSsl3Rsa* APS5_VABI EVP_PKEY_get0_RSA_nid_postfix(LibreSsl3EvpPkey* key); LibreSsl3Rsa* APS5_VABI EVP_PKEY_get1_RSA_nid_disambig1_nid_postfix(LibreSsl3EvpPkey* key); +LibreSsl3EcKey* APS5_VABI EVP_PKEY_get1_EC_KEY_nid_disambig1_nid_postfix(LibreSsl3EvpPkey* key); LibreSsl3EvpPkey* APS5_VABI EVP_PKEY_new_nid_disambig1_nid_postfix(void); void APS5_VABI EVP_PKEY_free_nid_disambig1_nid_postfix(LibreSsl3EvpPkey* key); int APS5_VABI EVP_PKEY_set1_RSA_nid_disambig1_nid_postfix(LibreSsl3EvpPkey* key, LibreSsl3Rsa* rsa); diff --git a/core/libs/prx/libSceLibreSsl3/src/Key.cpp b/core/libs/prx/libSceLibreSsl3/src/Key.cpp index 865b48c62946..561852f1e1f5 100644 --- a/core/libs/prx/libSceLibreSsl3/src/Key.cpp +++ b/core/libs/prx/libSceLibreSsl3/src/Key.cpp @@ -273,6 +273,14 @@ LibreSsl3EcKey* APS5_VABI EVP_PKEY_get0_EC_KEY_nid_postfix(LibreSsl3EvpPkey* key return static_cast(guestKey.key); } +LibreSsl3EcKey* APS5_VABI EVP_PKEY_get1_EC_KEY_nid_disambig1_nid_postfix(LibreSsl3EvpPkey* key) { + LibreSsl3EcKey* const ecKey = EVP_PKEY_get0_EC_KEY_nid_postfix(key); + if (!ecKey) + return nullptr; + EC_KEY_up_ref_nid_postfix(ecKey); + return ecKey; +} + int APS5_VABI EC_KEY_up_ref_nid_postfix(LibreSsl3EcKey* key) { if (!key) throw std::invalid_argument("EC_KEY_up_ref requires a key"); diff --git a/core/libs/prx/libSceLibreSsl3/src/PkeyContext.cpp b/core/libs/prx/libSceLibreSsl3/src/PkeyContext.cpp index 891cdceae4b4..c02833636598 100644 --- a/core/libs/prx/libSceLibreSsl3/src/PkeyContext.cpp +++ b/core/libs/prx/libSceLibreSsl3/src/PkeyContext.cpp @@ -530,6 +530,7 @@ #include #include #include +#include #include #include #include @@ -581,6 +582,10 @@ constexpr int ReasonSaltLengthTooSmall = 164; constexpr int ReasonInvalidDigestLength = 143; constexpr int ReasonInputNotInitialized = 111; constexpr int ReasonNoDefaultDigest = 158; +constexpr int ReasonInvalidDigestType = 138; +constexpr int EcdsaWithSha1Type = 416; + +bool IsLibreSslOnlyPkeyMethod(int keyType); constexpr int Asn1MethodFlags = 4; constexpr int DefaultDigestNid = 672; @@ -1156,15 +1161,103 @@ const LibreSsl3PkeyMethod RsaPssMethod { nullptr, }; +struct EcKeyContext { + const DigestMethod* digest; +}; + +EcKeyContext* EnsureEcContextData(LibreSsl3PkeyContext* context) { + if (context->data) return static_cast(context->data); + auto* ecContext = static_cast(calloc_nid_postfix(1, sizeof(EcKeyContext))); + if (!ecContext) { + LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, AllocationFailure); + return nullptr; + } + context->data = ecContext; + return ecContext; +} + +bool IsAllowedEcDigest(int type) { + switch (type) { + case Sha1Type: + case EcdsaWithSha1Type: + case Sha224Type: + case Sha256Type: + case Sha384Type: + case Sha512Type: + case Sha3_224Type: + case Sha3_256Type: + case Sha3_384Type: + case Sha3_512Type: + return true; + default: + return false; + } +} + int APS5_VABI EcContextInitialize(LibreSsl3PkeyContext*) { return 1; } -void APS5_VABI EcContextClear(LibreSsl3PkeyContext*) {} +void APS5_VABI EcContextClear(LibreSsl3PkeyContext* context) { + if (!context->data) return; + free_nid_postfix(context->data); + context->data = nullptr; +} -int APS5_VABI EcControl(LibreSsl3PkeyContext*, int command, int, void*) { - if (command == LibreSsl3PkeyControlPeerKey) return 1; - return -2; +int APS5_VABI EcContextCopy(LibreSsl3PkeyContext* destination, LibreSsl3PkeyContext* source) { + if (!source->data) { + EcContextClear(destination); + return 1; + } + auto* ecContext = EnsureEcContextData(destination); + if (!ecContext) return 0; + ecContext->digest = static_cast(source->data)->digest; + return 1; +} + +int APS5_VABI EcVerify(LibreSsl3PkeyContext* context, const unsigned char* signature, std::size_t signatureLength, + const unsigned char* content, std::size_t contentLength) { + const auto* ecContext = static_cast(context->data); + const int type = ecContext && ecContext->digest ? ecContext->digest->type : Sha1Type; + const auto* key = static_cast(context->key); + auto* ecKey = key ? static_cast(key->key) : nullptr; + if (!ecKey) NotImplemented_nid_no_patch("EVP_PKEY_CTX verify without a native EC key"); + if (signatureLength > static_cast(std::numeric_limits::max()) || + contentLength > static_cast(std::numeric_limits::max())) { + Reject("EVP_PKEY_verify EC input length exceeds int range"); + } + return ECDSA_verify_nid_postfix(type, content, static_cast(contentLength), signature, + static_cast(signatureLength), ecKey); +} + +int APS5_VABI EcControl(LibreSsl3PkeyContext* context, int command, int, void* second) { + switch (command) { + case LibreSsl3PkeyControlMessageDigest: { + const auto* digest = static_cast(second); + if (!digest) Reject("EVP_PKEY_CTX_ctrl set EC digest without a digest"); + if (!IsAllowedEcDigest(digest->type)) { + LibreSslPutError_nid_no_patch(EcLibrary, AnyFunction, ReasonInvalidDigestType); + return 0; + } + auto* ecContext = EnsureEcContextData(context); + if (!ecContext) return 0; + ecContext->digest = digest; + return 1; + } + case LibreSsl3PkeyControlGetMessageDigest: { + if (!second) Reject("EVP_PKEY_CTX_ctrl get digest without an output"); + const auto* ecContext = static_cast(context->data); + *static_cast(second) = ecContext ? ecContext->digest : nullptr; + return 1; + } + case LibreSsl3PkeyControlPeerKey: + case LibreSsl3PkeyControlDigestInit: + case LibreSsl3PkeyControlPkcs7Sign: + case LibreSsl3PkeyControlCmsSign: + return 1; + default: + return -2; + } } int APS5_VABI EcDerive(LibreSsl3PkeyContext* context, unsigned char* output, std::size_t* outputLength) { @@ -1214,14 +1307,14 @@ const LibreSsl3PkeyMethod EcMethod { LibreSsl3PkeyEc, 0, &EcContextInitialize, - nullptr, + &EcContextCopy, &EcContextClear, nullptr, nullptr, nullptr, nullptr, nullptr, - nullptr, + &EcVerify, nullptr, nullptr, nullptr, @@ -1392,46 +1485,69 @@ int DigestInitialize(LibreSsl3::DigestContext* context, const DigestMethod* type return type->init(context); } -int DigestSignInitialize(LibreSsl3::DigestContext* ctx, - LibreSsl3PkeyContext** pctx, const DigestMethod* type, LibreSsl3EvpPkey* pkey) { - if (!ctx->keyContext) +const DigestMethod* DefaultDigestForKey(LibreSsl3EvpPkey* pkey, const char* function) { + if (!pkey) Reject(std::string(function) + " requires a key when digest is null"); + int nid = 0; + if (pkey->method) { + const auto* keyAsn1 = static_cast(pkey->method); + if (!keyAsn1->keyControl || + keyAsn1->keyControl(pkey, LibreSsl3Asn1PkeyControlDefaultDigestNid, 0, &nid) <= 0) { + QueueEvpError(ReasonNoDefaultDigest); + return nullptr; + } + } else if (pkey->type == LibreSsl3PkeyEc) { + nid = Sha1Type; + } else { + QueueEvpError(ReasonNoDefaultDigest); + return nullptr; + } + if (nid == Sha256Type) return reinterpret_cast(EVP_sha256_nid_postfix()); + if (nid == Sha1Type) return reinterpret_cast(EVP_sha1_nid_postfix()); + NotImplemented_nid_no_patch((std::string(function) + " default digest lookup for non-SHA1/SHA256").c_str()); +} + +int DoSigVerInitialize(LibreSsl3::DigestContext* ctx, LibreSsl3PkeyContext** pctx, + const DigestMethod* type, LibreSsl3EvpPkey* pkey, bool verify) { + const char* const function = verify ? "EVP_DigestVerifyInit" : "EVP_DigestSignInit"; + if (!ctx) Reject(std::string(function) + " requires a digest context"); + if (!ctx->keyContext) { + if (verify && pkey && IsLibreSslOnlyPkeyMethod(pkey->type)) + NotImplemented_nid_no_patch("EVP_DigestVerifyInit for non-RSA/EC key type"); ctx->keyContext = EVP_PKEY_CTX_new_nid_postfix(pkey, nullptr); + } if (!ctx->keyContext) return 0; - const auto* method = MethodOf(static_cast(ctx->keyContext)); - const bool hasSignCtxCustom = (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) != 0; - if (!hasSignCtxCustom) { - if (type == nullptr) { - int nid = 0; - const auto* keyAsn1 = static_cast(pkey->method); - if (!keyAsn1 || keyAsn1->keyControl(pkey, LibreSsl3Asn1PkeyControlDefaultDigestNid, 0, - &nid) <= 0) { - QueueEvpError(ReasonNoDefaultDigest); - return 0; - } - if (nid == DefaultDigestNid) { - type = reinterpret_cast(EVP_sha256_nid_postfix()); - } else { - NotImplemented_nid_no_patch("EVP_DigestSignInit default digest lookup for non-SHA256"); - } - } + auto* keyContext = static_cast(ctx->keyContext); + const auto* method = MethodOf(keyContext); + if (!method || FindNativePkeyMethod_nid_no_patch(method->keyType) != method) + NotImplemented_nid_no_patch((std::string(function) + " with a foreign EVP_PKEY_CTX").c_str()); + + const bool hasSigCtxCustom = (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) != 0; + if (!hasSigCtxCustom && type == nullptr) { + type = DefaultDigestForKey(pkey, function); + if (!type) return 0; } - if (method->signatureContextInitialize) NotImplemented_nid_no_patch("EVP_DigestSignInit with a signctx_init method"); - if (method->digestSignature) NotImplemented_nid_no_patch("EVP_DigestSignInit with a digestsign method"); - if (EVP_PKEY_sign_init_nid_postfix(static_cast(ctx->keyContext)) <= 0) - return 0; + if (verify) { + if (method->digestVerification) + NotImplemented_nid_no_patch("EVP_DigestVerifyInit with a digestverify method"); + if (EVP_PKEY_verify_init_nid_postfix(keyContext) <= 0) return 0; + } else { + if (method->signatureContextInitialize) + NotImplemented_nid_no_patch("EVP_DigestSignInit with a signctx_init method"); + if (method->digestSignature) + NotImplemented_nid_no_patch("EVP_DigestSignInit with a digestsign method"); + if (EVP_PKEY_sign_init_nid_postfix(keyContext) <= 0) return 0; + } - if (EVP_PKEY_CTX_ctrl_nid_postfix(static_cast(ctx->keyContext), -1, - LibreSsl3PkeyOperationTypeSignature, LibreSsl3PkeyControlMessageDigest, 0, + if (EVP_PKEY_CTX_ctrl_nid_postfix(keyContext, -1, LibreSsl3PkeyOperationTypeSignature, + LibreSsl3PkeyControlMessageDigest, 0, static_cast(const_cast(type))) <= 0) return 0; - if (pctx) *pctx = static_cast(ctx->keyContext); + if (pctx) *pctx = keyContext; - if (!hasSignCtxCustom) { - if (!DigestInitialize(ctx, type)) return 0; - } + if (!hasSigCtxCustom && !DigestInitialize(ctx, type)) return 0; return 1; } @@ -1502,6 +1618,18 @@ int DigestFinal(LibreSsl3::DigestContext* ctx, unsigned char* md, unsigned int* return ret; } +int FinalizeDigestCopy(LibreSsl3::DigestContext* ctx, unsigned char* digest, unsigned int& digestLength, + int copyFailureReturn) { + LibreSsl3::DigestContext copy = {}; + if (!DigestCopy(©, ctx)) { + if (copy.data) free_nid_postfix(copy.data); + return copyFailureReturn; + } + const int finalized = DigestFinal(©, digest, &digestLength); + if (copy.data) free_nid_postfix(copy.data); + return finalized; +} + } // namespace const LibreSsl3PkeyMethod* FindNativePkeyMethod_nid_no_patch(std::int32_t keyType) { @@ -1803,7 +1931,7 @@ int APS5_VABI EVP_DigestSignInit_nid_postfix(LibreSsl3::DigestContext* ctx, LibreSsl3PkeyContext** pctx, const LibreSsl3::DigestMethod* type, void* engine, LibreSsl3EvpPkey* pkey) { if (engine) NotImplemented_nid_no_patch("EVP_DigestSignInit with engine"); - return DigestSignInitialize(ctx, pctx, type, pkey); + return DoSigVerInitialize(ctx, pctx, type, pkey, false); } int APS5_VABI EVP_DigestSignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, @@ -1819,13 +1947,40 @@ int APS5_VABI EVP_DigestSignFinal_nid_postfix(LibreSsl3::DigestContext* ctx, if (size < 0) return 0; return EVP_PKEY_sign_nid_postfix(pctx, nullptr, siglen, nullptr, static_cast(size)) > 0; } - LibreSsl3::DigestContext copy = {}; - unsigned char digest[LibreSsl3MaximumDigestSize]; + unsigned char digest[LibreSsl3MaximumDigestSize] {}; unsigned int digestLength = 0; auto result = 0; - if (DigestCopy(©, ctx) && DigestFinal(©, digest, &digestLength)) + if (FinalizeDigestCopy(ctx, digest, digestLength, 0) > 0) result = EVP_PKEY_sign_nid_postfix(pctx, sigret, siglen, digest, digestLength) > 0; - if (copy.data) free_nid_postfix(copy.data); + for (unsigned int i = 0; i < digestLength; ++i) digest[i] = 0; + return result; +} + +int APS5_VABI EVP_DigestVerifyInit_nid_disambig1_nid_postfix(LibreSsl3::DigestContext* ctx, + LibreSsl3PkeyContext** pctx, const LibreSsl3::DigestMethod* type, void* engine, + LibreSsl3EvpPkey* pkey) { + if (engine) NotImplemented_nid_no_patch("EVP_DigestVerifyInit with engine"); + return DoSigVerInitialize(ctx, pctx, type, pkey, true); +} + +int APS5_VABI EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(LibreSsl3::DigestContext* ctx, + const unsigned char* sig, std::size_t siglen) { + if (!ctx) Reject("EVP_DigestVerifyFinal requires a digest context"); + auto* pctx = static_cast(ctx->keyContext); + if (!pctx) NotImplemented_nid_no_patch("EVP_DigestVerifyFinal without an EVP_PKEY_CTX"); + const auto* method = MethodOf(pctx); + if (!method || FindNativePkeyMethod_nid_no_patch(method->keyType) != method) + NotImplemented_nid_no_patch("EVP_DigestVerifyFinal with a foreign EVP_PKEY_CTX"); + if (method->flags & LibreSsl3PkeyMethodFlagSignatureContextCustom) + NotImplemented_nid_no_patch("EVP_DigestVerifyFinal with a custom signature context"); + if (method->digestVerification) + NotImplemented_nid_no_patch("EVP_DigestVerifyFinal with a digestverify method"); + unsigned char digest[LibreSsl3MaximumDigestSize] {}; + unsigned int digestLength = 0; + const int status = FinalizeDigestCopy(ctx, digest, digestLength, -1); + int result = status; + if (status > 0) + result = EVP_PKEY_verify_nid_postfix(pctx, sig, siglen, digest, digestLength); for (unsigned int i = 0; i < digestLength; ++i) digest[i] = 0; return result; } diff --git a/core/libs/prx/libSceLibreSsl3/tests/PkeyContext.cpp b/core/libs/prx/libSceLibreSsl3/tests/PkeyContext.cpp index 1fde2d973842..ac965db813b0 100644 --- a/core/libs/prx/libSceLibreSsl3/tests/PkeyContext.cpp +++ b/core/libs/prx/libSceLibreSsl3/tests/PkeyContext.cpp @@ -1841,6 +1841,549 @@ void GuestOutputLengths() { ReleaseKey(key); } +constexpr unsigned char OracleRsaPkcs1Sha256Sig[256] { + 0xb4, 0x44, 0x92, 0xa1, 0x5b, 0xa5, 0xa0, 0xe8, 0x93, 0x85, 0x96, 0x04, 0x43, 0x22, 0xc9, 0xa8, + 0x37, 0x2c, 0x38, 0x8d, 0xf9, 0xf1, 0x56, 0x53, 0x63, 0x30, 0x8c, 0x56, 0x8d, 0x80, 0x18, 0xcb, + 0xdb, 0x6a, 0x8c, 0xd8, 0xca, 0xe8, 0xbe, 0x8b, 0x17, 0x59, 0xd6, 0xfc, 0x7c, 0xf0, 0x5d, 0x67, + 0x55, 0x77, 0xb1, 0x5c, 0x2c, 0xc4, 0xc6, 0x09, 0x61, 0xe2, 0x2a, 0x03, 0x7a, 0x67, 0x68, 0x75, + 0x6d, 0x58, 0xda, 0xe6, 0x74, 0x5d, 0x19, 0xe9, 0x8c, 0xde, 0x13, 0x47, 0x38, 0xa3, 0x64, 0x3e, + 0x86, 0x6a, 0xb0, 0x9d, 0xe5, 0x7b, 0x79, 0x50, 0xf0, 0x20, 0x0f, 0xf6, 0xe7, 0x47, 0x5b, 0x2b, + 0x60, 0xce, 0x8d, 0x7b, 0x36, 0xf1, 0xf6, 0xb6, 0xd1, 0xab, 0x70, 0xff, 0xe5, 0xa0, 0x1e, 0x86, + 0x84, 0xe4, 0xb7, 0x14, 0x88, 0xe9, 0x51, 0x35, 0xf4, 0x91, 0x2f, 0x72, 0xd1, 0x55, 0xff, 0x6f, + 0x5e, 0xfd, 0x9b, 0x4f, 0x56, 0xcf, 0x56, 0xab, 0x98, 0x6f, 0x93, 0x1d, 0x0c, 0xc0, 0xd8, 0x97, + 0xfd, 0x95, 0xb8, 0x6b, 0x56, 0x53, 0x2b, 0x36, 0x39, 0xa4, 0xcd, 0xd2, 0x26, 0x84, 0x89, 0x92, + 0x06, 0x68, 0x80, 0xac, 0x2c, 0x16, 0x49, 0x1b, 0xc9, 0x20, 0x36, 0x10, 0x8c, 0xf0, 0x80, 0x31, + 0x76, 0x02, 0xc1, 0x9f, 0x15, 0x68, 0xbc, 0x54, 0x80, 0x35, 0x92, 0x35, 0xa0, 0x95, 0x0c, 0x5a, + 0xd3, 0xc4, 0xc3, 0x8f, 0xcc, 0x41, 0xfe, 0x73, 0x15, 0xf0, 0x2d, 0x13, 0x8e, 0xd8, 0x92, 0x82, + 0x6b, 0x9a, 0x3d, 0x66, 0xf1, 0x40, 0x15, 0x67, 0xc0, 0xe4, 0x2a, 0xc8, 0x15, 0xad, 0xab, 0x47, + 0xc1, 0x44, 0xc5, 0x45, 0x89, 0x43, 0x84, 0xd0, 0x77, 0xc6, 0x1b, 0x52, 0x0c, 0xac, 0xcd, 0x05, + 0xec, 0x01, 0x65, 0x45, 0xbc, 0xd4, 0x47, 0x1f, 0x07, 0x77, 0x49, 0xaf, 0x4b, 0x02, 0xb2, 0xf2, +}; + +constexpr unsigned char OracleRsaPssSha256Salt20Sig[256] { + 0xa4, 0x34, 0x86, 0x18, 0x5b, 0xcb, 0x2f, 0x5e, 0x4b, 0xfd, 0xc0, 0x4d, 0x1a, 0x46, 0xea, 0xbf, + 0x84, 0x31, 0xae, 0x7b, 0x6e, 0x38, 0xea, 0xf4, 0x60, 0xf1, 0x38, 0xbb, 0x5e, 0xca, 0x15, 0x3b, + 0x14, 0x95, 0x67, 0xae, 0x1d, 0x3c, 0x17, 0x40, 0xc6, 0x82, 0x0f, 0xb1, 0x10, 0x74, 0x74, 0x8e, + 0xf0, 0xe1, 0xa9, 0xdb, 0xa7, 0xc2, 0xb8, 0x07, 0x62, 0xc4, 0x4f, 0x43, 0xef, 0x84, 0x02, 0x3b, + 0xc9, 0x37, 0xd3, 0x28, 0xe3, 0xfb, 0xf2, 0xe3, 0xc5, 0x09, 0xf6, 0x04, 0xd1, 0x1e, 0x70, 0x95, + 0xd0, 0x60, 0xd5, 0xc9, 0xab, 0x4a, 0x17, 0xf1, 0x32, 0xba, 0x70, 0xd4, 0x97, 0xa1, 0x72, 0xac, + 0x06, 0xd2, 0xa7, 0xa9, 0x80, 0xaa, 0x77, 0xe6, 0x04, 0x37, 0x2b, 0xae, 0x8c, 0x87, 0x6d, 0xc3, + 0x1e, 0x19, 0xeb, 0x4b, 0xa0, 0xbe, 0x7b, 0xb2, 0x02, 0x13, 0x96, 0x7d, 0x82, 0xbe, 0x05, 0x4f, + 0x23, 0x36, 0x93, 0x2a, 0x72, 0xc7, 0x5d, 0x37, 0x32, 0xf6, 0x1f, 0x05, 0x36, 0xaf, 0x78, 0x75, + 0xe9, 0xe9, 0x54, 0xbe, 0x0a, 0x5c, 0x29, 0x6f, 0xdd, 0x34, 0x1f, 0x51, 0x58, 0x26, 0x91, 0xb6, + 0x94, 0x88, 0xeb, 0x16, 0xc8, 0x15, 0xea, 0x9e, 0xf2, 0x38, 0x55, 0x22, 0x10, 0xaf, 0x0c, 0x40, + 0xa4, 0x2d, 0xda, 0xd0, 0x30, 0x7e, 0x5d, 0xbb, 0x59, 0x4d, 0x05, 0x4b, 0x04, 0x34, 0x9a, 0x61, + 0x74, 0x35, 0xdf, 0xd2, 0x77, 0xb9, 0x60, 0x7c, 0xeb, 0x13, 0xad, 0x99, 0x46, 0xb1, 0x7e, 0x7d, + 0xa4, 0xa2, 0xfb, 0x8a, 0x29, 0x48, 0x12, 0x97, 0x46, 0x8f, 0xa3, 0x77, 0xbc, 0x53, 0xfd, 0x3c, + 0xa5, 0x8b, 0x76, 0x22, 0x1a, 0x2e, 0xc8, 0x4f, 0x12, 0x6a, 0x45, 0xe4, 0x55, 0xf3, 0xaf, 0x14, + 0x77, 0xc6, 0x7c, 0xa7, 0x67, 0xfe, 0xe0, 0x1c, 0xaf, 0xfd, 0x1b, 0x8c, 0x95, 0x9c, 0xc8, 0x28, +}; + +constexpr unsigned char OracleRsaPssSha512_256Mgf1Sha1Salt16Sig[256] { + 0xac, 0x18, 0x92, 0x42, 0xd7, 0x7b, 0x7c, 0xca, 0xdd, 0xfa, 0x70, 0x29, 0x68, 0x57, 0x1a, 0x9d, + 0x71, 0xbe, 0xc0, 0xe7, 0x69, 0x52, 0x01, 0x36, 0x1a, 0x89, 0x5d, 0x8f, 0xf5, 0x77, 0x0f, 0x6b, + 0x2f, 0xe2, 0xb2, 0x64, 0xc5, 0x05, 0xc2, 0xa6, 0xbc, 0xca, 0x5b, 0x0a, 0x01, 0x31, 0xc7, 0x7b, + 0x06, 0x46, 0x14, 0xab, 0xa1, 0x56, 0x50, 0xf1, 0xa7, 0x4d, 0xac, 0x97, 0x86, 0x9b, 0xef, 0xef, + 0x3d, 0x9c, 0xa9, 0x04, 0xd7, 0x6c, 0x30, 0x42, 0xf3, 0x32, 0xe0, 0x14, 0xfb, 0x2a, 0x46, 0x4f, + 0x57, 0x25, 0x8c, 0xfa, 0x44, 0xcf, 0x71, 0x97, 0xe9, 0xcb, 0x87, 0x84, 0x36, 0xc2, 0x30, 0x40, + 0xd2, 0x82, 0x3e, 0x29, 0x76, 0x1f, 0xd4, 0x62, 0x59, 0x70, 0x1d, 0x8b, 0x13, 0x26, 0x19, 0xda, + 0xca, 0xaf, 0x34, 0x05, 0x7c, 0xcd, 0xcb, 0x03, 0x29, 0x5e, 0x8a, 0xe6, 0xd4, 0x04, 0x46, 0x31, + 0x18, 0x96, 0x7d, 0x18, 0xf6, 0xca, 0x53, 0x6f, 0x4b, 0x69, 0x3b, 0x4c, 0x0f, 0xdb, 0xa0, 0x3b, + 0x1f, 0xf0, 0xcb, 0xf0, 0x33, 0xad, 0x0a, 0x23, 0x2b, 0x87, 0x95, 0x89, 0x4f, 0x11, 0x68, 0xf8, + 0x26, 0x26, 0x82, 0x60, 0x97, 0x47, 0xa2, 0x66, 0xfc, 0x3d, 0x26, 0x9d, 0x26, 0x90, 0xa4, 0x3d, + 0xba, 0x83, 0xfb, 0x15, 0x49, 0x7d, 0x5d, 0x45, 0x93, 0x00, 0xe0, 0xc4, 0x03, 0x8f, 0xcf, 0x49, + 0xd5, 0xa4, 0x00, 0xdf, 0x60, 0x5e, 0x7e, 0x5f, 0xb6, 0x8a, 0x04, 0x75, 0xd8, 0xb4, 0xb4, 0x97, + 0x1a, 0x7f, 0xa0, 0xaf, 0xb3, 0xdb, 0x24, 0x66, 0x65, 0x75, 0x5e, 0x3d, 0xf5, 0x4e, 0xdb, 0x71, + 0xb9, 0x11, 0x3a, 0x46, 0xbd, 0x11, 0x1e, 0x75, 0x3d, 0x93, 0x11, 0xdf, 0xb5, 0x51, 0xbf, 0x29, + 0xe8, 0xed, 0xd4, 0xac, 0xed, 0x82, 0x91, 0x0f, 0xbb, 0xf3, 0xd0, 0xae, 0xcc, 0x4b, 0x1d, 0xa0, +}; + +constexpr unsigned char OracleEcP256Sha256Sig[71] { + 0x30, 0x45, 0x02, 0x20, 0x4c, 0xfe, 0x09, 0x5a, 0xd3, 0xdc, 0x2d, 0x82, 0xf7, 0xaf, 0x7c, 0xef, + 0xed, 0x15, 0xb4, 0x46, 0x38, 0xe1, 0x1e, 0x29, 0x7c, 0x91, 0xcf, 0x2e, 0x4a, 0x89, 0xc9, 0x6c, + 0x24, 0x64, 0xb2, 0x37, 0x02, 0x21, 0x00, 0xfa, 0x68, 0xa3, 0x84, 0x60, 0xc9, 0xa0, 0x6f, 0x9f, + 0xc1, 0xe1, 0x80, 0xa8, 0x4d, 0x7a, 0xf1, 0x5f, 0xdb, 0xca, 0x93, 0x13, 0x67, 0x0e, 0x17, 0xaa, + 0x50, 0x6a, 0x18, 0x78, 0xf6, 0x62, 0xe6, +}; + +constexpr unsigned char OracleEcP256DefaultSha1Sig[72] { + 0x30, 0x46, 0x02, 0x21, 0x00, 0x8e, 0x13, 0x49, 0x9c, 0xa3, 0x62, 0xac, 0x57, 0x32, 0x07, 0xc7, + 0x35, 0x99, 0x4f, 0xce, 0xd3, 0xf3, 0x36, 0x40, 0xd3, 0x7b, 0x84, 0x7b, 0x94, 0xdc, 0x87, 0xe9, + 0x8f, 0x17, 0x9a, 0x15, 0x2c, 0x02, 0x21, 0x00, 0xa5, 0x1c, 0x5f, 0x63, 0xb3, 0x25, 0xe1, 0x78, + 0xbb, 0xc6, 0xef, 0x18, 0x9e, 0x80, 0xb6, 0x5b, 0x5a, 0x12, 0xe2, 0x3f, 0x63, 0x21, 0xe5, 0x6f, + 0x07, 0xdd, 0xcf, 0x7b, 0x68, 0x35, 0x62, 0xde, +}; + +constexpr unsigned char OracleEcP384Sha384Sig[102] { + 0x30, 0x64, 0x02, 0x30, 0x78, 0x90, 0x41, 0x48, 0x17, 0x8b, 0x99, 0xd4, 0xb7, 0x6e, 0x23, 0x48, + 0x25, 0x44, 0x43, 0xf2, 0x3e, 0xc4, 0x9f, 0x04, 0x8a, 0x2d, 0xc4, 0xd5, 0x47, 0x19, 0x71, 0x9b, + 0xfc, 0xe1, 0xe2, 0xf5, 0x57, 0xc2, 0x9c, 0x8d, 0x7d, 0x9b, 0xb8, 0x5c, 0x2b, 0xbe, 0xe1, 0xf3, + 0x9e, 0xd8, 0xbb, 0x63, 0x02, 0x30, 0x31, 0x43, 0xf4, 0x36, 0x14, 0x13, 0x75, 0x64, 0x46, 0x7e, + 0xd8, 0x93, 0x42, 0xa4, 0xae, 0xe6, 0xaa, 0xbb, 0x12, 0x81, 0x51, 0x79, 0x78, 0x98, 0x35, 0x1e, + 0xa1, 0xea, 0xda, 0xcb, 0xdd, 0x5b, 0x0c, 0xef, 0xb2, 0x94, 0x4e, 0x5d, 0x4f, 0xf6, 0x17, 0x8c, + 0xa7, 0xd2, 0x0a, 0x9e, 0x9d, 0x6d, +}; + +const LibreSsl3::DigestMethod* AsDigestMethod(const void* md) { + return static_cast(md); +} + +void DigestVerifyRsa() { + auto* key = NewNativeKey(LibreSsl3PkeyRsa); + auto* md = EVP_MD_CTX_new_nid_postfix(); + Require(md != nullptr); + LibreSsl3PkeyContext* pctx = nullptr; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, key) == 1); + Require(pctx != nullptr && md->keyContext == pctx && Pointer(md->digest) == Pointer(EVP_sha256_nid_postfix())); + Require(md->update(md, MessageBytes(), 7) == 1); + Require(md->update(md, MessageBytes() + 7, MessageLength - 7) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + + auto flippedSig = Flipped(OracleRsaPkcs1Sha256Sig, 10); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, flippedSig.data(), flippedSig.size()) == 0); + ExpectErrors({RsaCode(106), RsaCode(114)}); + + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig) - 1) == 0); + ExpectErrors({RsaCode(119)}); + + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, nullptr, nullptr, key) == 1); + Require(Pointer(md->digest) == Pointer(EVP_sha256_nid_postfix())); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + + std::array tampered {}; + std::memcpy(tampered.data(), MessageBytes(), MessageLength); + tampered[0] ^= 1; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, key) == 1); + Require(md->update(md, tampered.data(), tampered.size()) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == 0); + ExpectErrors({RsaCode(104)}); + + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, Sha512_256Method(), nullptr, key) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == 0); + ExpectErrors({RsaCode(104)}); + + EVP_MD_CTX_free_nid_postfix(md); + ReleaseKey(key); +} + +void DigestVerifyPss() { + auto* rsaKey = NewNativeKey(LibreSsl3PkeyRsa); + auto* md = EVP_MD_CTX_new_nid_postfix(); + Require(md != nullptr); + LibreSsl3PkeyContext* pctx = nullptr; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, rsaKey) == 1); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaPadding, + LibreSsl3RsaPaddingPss, nullptr) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPssSha256Salt20Sig, + sizeof(OracleRsaPssSha256Salt20Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaPssSaltLength, + 20, nullptr) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPssSha256Salt20Sig, + sizeof(OracleRsaPssSha256Salt20Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaPssSaltLength, + LibreSsl3RsaPssSaltLengthDigest, nullptr) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPssSha256Salt20Sig, + sizeof(OracleRsaPssSha256Salt20Sig)) == 0); + ExpectErrors({RsaCode(136)}); + EVP_MD_CTX_free_nid_postfix(md); + + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, Sha512_256Method(), nullptr, rsaKey) == 1); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaPadding, + LibreSsl3RsaPaddingPss, nullptr) == 1); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaPssSaltLength, + 16, nullptr) == 1); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaMaskDigest, + 0, Pointer(EVP_sha1_nid_postfix())) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPssSha512_256Mgf1Sha1Salt16Sig, + sizeof(OracleRsaPssSha512_256Mgf1Sha1Salt16Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyRsa, -1, LibreSsl3PkeyControlRsaMaskDigest, + 0, Pointer(Sha512_256Method())) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPssSha512_256Mgf1Sha1Salt16Sig, + sizeof(OracleRsaPssSha512_256Mgf1Sha1Salt16Sig)) == 0); + ExpectErrors({RsaCode(135)}); + EVP_MD_CTX_free_nid_postfix(md); + ReleaseKey(rsaKey); + + auto* pssKey = NewNativeKey(LibreSsl3PkeyRsaPss); + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, nullptr, nullptr, pssKey) == 1); + Require(Pointer(md->digest) == Pointer(EVP_sha256_nid_postfix())); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPssSha256Salt20Sig, + sizeof(OracleRsaPssSha256Salt20Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + EVP_MD_CTX_free_nid_postfix(md); + ReleaseKey(pssKey); +} + +void DigestVerifyEcdsa() { + constexpr int P256 = 415; + constexpr int P384 = 715; + constexpr char PrivateA[] = "C88F01F510D9AC3F70A292DAA2316DE544E9AAB8AFE84049C62A9C57862D1433"; + constexpr char PrivateB[] = "C6EF9C5D78AE012A011164ACB397CE2088685D8F06BF9BE0B283AB46476BEE53"; + constexpr char Private384[] = "0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F30"; + + auto* ec256A = NewEcPkey(NewEcKey(PrivateA, P256)); + auto* ec256B = NewEcPkey(NewEcKey(PrivateB, P256)); + auto* ec384 = NewEcPkey(NewEcKey(Private384, P384)); + + auto* md = EVP_MD_CTX_new_nid_postfix(); + LibreSsl3PkeyContext* pctx = nullptr; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, ec256A) == 1); + Require(pctx != nullptr && md->keyContext == pctx); + const LibreSsl3::DigestMethod* selectedMd = nullptr; + Require(EVP_PKEY_CTX_ctrl_nid_postfix(pctx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlGetMessageDigest, 0, + &selectedMd) == 1); + Require(Pointer(selectedMd) == Pointer(EVP_sha256_nid_postfix())); + Require(md->update(md, MessageBytes(), 5) == 1); + Require(md->update(md, MessageBytes() + 5, MessageLength - 5) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + + auto tamperedSig = OracleEcP256Sha256Sig; + std::array badSig {}; + std::memcpy(badSig.data(), tamperedSig, badSig.size()); + badSig.back() ^= 1; + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, badSig.data(), badSig.size()) == 0); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig) - 1) == -1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, 0) == -1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == -1); + + std::array tamperedMsg {}; + std::memcpy(tamperedMsg.data(), MessageBytes(), MessageLength); + tamperedMsg[0] ^= 1; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, ec256A) == 1); + Require(md->update(md, tamperedMsg.data(), tamperedMsg.size()) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)) == 0); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha384_nid_postfix()), + nullptr, ec256A) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)) == 0); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, nullptr, nullptr, ec256A) == 1); + Require(Pointer(md->digest) == Pointer(EVP_sha1_nid_postfix())); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256DefaultSha1Sig, + sizeof(OracleEcP256DefaultSha1Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + EVP_MD_CTX_free_nid_postfix(md); + + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, ec256B) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)) == 0); + Require(ERR_get_error_nid_postfix() == 0); + EVP_MD_CTX_free_nid_postfix(md); + + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha384_nid_postfix()), + nullptr, ec384) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP384Sha384Sig, + sizeof(OracleEcP384Sha384Sig)) == 1); + Require(ERR_get_error_nid_postfix() == 0); + EVP_MD_CTX_free_nid_postfix(md); + + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha224_nid_postfix()), + nullptr, ec256A) == 1); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha512_nid_postfix()), + nullptr, ec256A) == 1); + EVP_MD_CTX_free_nid_postfix(md); + + auto* rejectedMd = EVP_MD_CTX_new_nid_postfix(); + LibreSsl3PkeyContext* rejectedPctx = nullptr; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(rejectedMd, &rejectedPctx, + AsDigestMethod(EVP_md5_nid_postfix()), nullptr, ec256A) == 0); + ExpectErrors({EcCode(138)}); + Require(rejectedPctx == nullptr && rejectedMd->digest == nullptr); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(rejectedMd, &rejectedPctx, Sha512_256Method(), + nullptr, ec256A) == 0); + ExpectErrors({EcCode(138)}); + Require(rejectedPctx == nullptr && rejectedMd->digest == nullptr); + EVP_MD_CTX_free_nid_postfix(rejectedMd); + + auto* rsaKey = NewNativeKey(LibreSsl3PkeyRsa); + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, rsaKey) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)) == 0); + ExpectErrors({RsaCode(119)}); + EVP_MD_CTX_free_nid_postfix(md); + ReleaseKey(rsaKey); + + EVP_PKEY_free_nid_disambig1_nid_postfix(ec256A); + EVP_PKEY_free_nid_disambig1_nid_postfix(ec256B); + EVP_PKEY_free_nid_disambig1_nid_postfix(ec384); +} + +int APS5_VABI ZeroInitDigest(LibreSsl3::DigestContext*) { + return 0; +} + +int APS5_VABI GoodInitDigest(LibreSsl3::DigestContext*) { + return 1; +} + +int APS5_VABI ZeroFinalDigest(LibreSsl3::DigestContext*, unsigned char*) { + return 0; +} + +void DigestVerifyErrors() { + constexpr int EcCurveP256 = 415; + auto* rsaKey = NewNativeKey(LibreSsl3PkeyRsa); + auto* md = EVP_MD_CTX_new_nid_postfix(); + LibreSsl3PkeyContext* untouched = reinterpret_cast(std::uintptr_t {0x1234}); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &untouched, + AsDigestMethod(EVP_sha256_nid_postfix()), nullptr, nullptr) == 0); + Require(untouched == reinterpret_cast(std::uintptr_t {0x1234})); + Require(md->keyContext == nullptr); + Require(ERR_get_error_nid_postfix() == 0); + + auto* undefKey = EVP_PKEY_new_nid_disambig1_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &untouched, + AsDigestMethod(EVP_sha256_nid_postfix()), nullptr, undefKey) == 0); + Require(untouched == reinterpret_cast(std::uintptr_t {0x1234})); + Require(md->keyContext == nullptr); + Require(ERR_get_error_nid_postfix() == 0); + EVP_PKEY_free_nid_disambig1_nid_postfix(undefKey); + + Require(Throws([&] { + EVP_DigestVerifyInit_nid_disambig1_nid_postfix(nullptr, nullptr, + AsDigestMethod(EVP_sha256_nid_postfix()), nullptr, rsaKey); + })); + int engineMarker = 0; + Require(Throws([&] { + EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, + AsDigestMethod(EVP_sha256_nid_postfix()), &engineMarker, rsaKey); + })); + Require(Throws([&] { EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(nullptr, nullptr, 0); })); + Require(Throws([&] { EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, nullptr, 0); })); + + auto* hmacKey = EVP_PKEY_new_mac_key_nid_postfix(LibreSsl3PkeyHmac, nullptr, + reinterpret_cast("secret"), 6); + Require(hmacKey != nullptr); + Require(Throws([&] { + EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, + AsDigestMethod(EVP_sha256_nid_postfix()), nullptr, hmacKey); + })); + Require(md->keyContext == nullptr && ERR_get_error_nid_postfix() == 0); + EVP_PKEY_free_nid_disambig1_nid_postfix(hmacKey); + + LibreSsl3PkeyMethod foreignMethod {}; + foreignMethod.keyType = LibreSsl3PkeyRsa; + LibreSsl3PkeyContext foreignCtx {}; + foreignCtx.method = &foreignMethod; + md->keyContext = &foreignCtx; + Require(Throws([&] { + EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, + AsDigestMethod(EVP_sha256_nid_postfix()), nullptr, rsaKey); + })); + Require(Throws([&] { + EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)); + })); + md->keyContext = nullptr; + + LibreSsl3PkeyContext* pctx = nullptr; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, rsaKey) == 1); + const auto* savedDigest = md->digest; + md->digest = nullptr; + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == -1); + ExpectErrors({EvpCode(111)}); + md->digest = savedDigest; + + LibreSsl3EvpPkey rawRsaPkey {}; + rawRsaPkey.type = LibreSsl3PkeyRsa; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, nullptr, nullptr, &rawRsaPkey) == 0); + ExpectErrors({EvpCode(158)}); + + LibreSsl3PkeyAsn1Method noCtrlAsn1 = *static_cast(rsaKey->method); + noCtrlAsn1.keyControl = nullptr; + LibreSsl3EvpPkey noCtrlKey {}; + noCtrlKey.type = LibreSsl3PkeyRsa; + noCtrlKey.method = &noCtrlAsn1; + noCtrlKey.key = rsaKey->key; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, nullptr, nullptr, &noCtrlKey) == 0); + ExpectErrors({EvpCode(158)}); + + LibreSsl3::DigestMethod failInitMethod {}; + failInitMethod.type = 672; + failInitMethod.digestSize = 32; + failInitMethod.init = &ZeroInitDigest; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, &failInitMethod, nullptr, rsaKey) == 0); + + LibreSsl3::DigestMethod failFinalMethod {}; + failFinalMethod.type = 672; + failFinalMethod.digestSize = 32; + failFinalMethod.init = &GoodInitDigest; + failFinalMethod.finalize = &ZeroFinalDigest; + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, &failFinalMethod, nullptr, rsaKey) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == 0); + Require(ERR_get_error_nid_postfix() == 0); + + Require(EVP_DigestSignInit_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, rsaKey) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleRsaPkcs1Sha256Sig, + sizeof(OracleRsaPkcs1Sha256Sig)) == -1); + ExpectErrors({EvpCode(151)}); + + Require(Throws([&] { + EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, nullptr, nullptr, nullptr, nullptr); + })); + EVP_MD_CTX_free_nid_postfix(md); + + auto* emptyEc = EVP_PKEY_new_nid_disambig1_nid_postfix(); + Require(EVP_PKEY_assign_nid_postfix(emptyEc, LibreSsl3PkeyEc, nullptr) == 0); + emptyEc->type = LibreSsl3PkeyEc; + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, emptyEc) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + Require(Throws([&] { + EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, OracleEcP256Sha256Sig, + sizeof(OracleEcP256Sha256Sig)); + })); + Require(ERR_get_error_nid_postfix() == 0); + EVP_MD_CTX_free_nid_postfix(md); + EVP_PKEY_free_nid_disambig1_nid_postfix(emptyEc); + + auto* ecNoGroup = NewEcPkey(EC_KEY_new_nid_postfix()); + md = EVP_MD_CTX_new_nid_postfix(); + Require(EVP_DigestVerifyInit_nid_disambig1_nid_postfix(md, &pctx, AsDigestMethod(EVP_sha256_nid_postfix()), + nullptr, ecNoGroup) == 1); + Require(md->update(md, MessageBytes(), MessageLength) == 1); + constexpr unsigned char ValidDerSig[] {0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01}; + Require(EVP_DigestVerifyFinal_nid_disambig1_nid_postfix(md, ValidDerSig, sizeof(ValidDerSig)) == -1); + Require(ERR_get_error_nid_postfix() != 0); + ERR_clear_error_nid_postfix(); + EVP_MD_CTX_free_nid_postfix(md); + EVP_PKEY_free_nid_disambig1_nid_postfix(ecNoGroup); + + auto* ecKey = NewEcPkey(NewEcKey("C88F01F510D9AC3F70A292DAA2316DE544E9AAB8AFE84049C62A9C57862D1433", EcCurveP256)); + auto* ecCtx = EVP_PKEY_CTX_new_nid_postfix(ecKey, nullptr); + Require(EVP_PKEY_verify_init_nid_postfix(ecCtx) == 1); + const LibreSsl3::DigestMethod* gotMd = AsDigestMethod(EVP_sha256_nid_postfix()); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(ecCtx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlGetMessageDigest, 0, + &gotMd) == 1); + Require(gotMd == nullptr); + Require(Throws([&] { + EVP_PKEY_CTX_ctrl_nid_postfix(ecCtx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlMessageDigest, 0, nullptr); + })); + Require(Throws([&] { + EVP_PKEY_CTX_ctrl_nid_postfix(ecCtx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlGetMessageDigest, 0, nullptr); + })); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(ecCtx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlCmsSign, 0, nullptr) == 1); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(ecCtx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlRsaPadding, + LibreSsl3RsaPaddingPkcs1, nullptr) == -2); + ExpectErrors({EvpCode(147)}); + Require(Throws([&] { + EVP_PKEY_verify_nid_postfix(ecCtx, OracleEcP256Sha256Sig, + static_cast(std::numeric_limits::max()) + 1, DigestSha1, sizeof(DigestSha1)); + })); + Require(Throws([&] { + EVP_PKEY_verify_nid_postfix(ecCtx, OracleEcP256Sha256Sig, sizeof(OracleEcP256Sha256Sig), DigestSha1, + static_cast(std::numeric_limits::max()) + 1); + })); + + const auto* ecMethod = FindNativePkeyMethod_nid_no_patch(LibreSsl3PkeyEc); + Require(ecMethod != nullptr && ecMethod->copy != nullptr); + auto* ecCtxCopy = EVP_PKEY_CTX_new_nid_postfix(ecKey, nullptr); + Require(EVP_PKEY_verify_init_nid_postfix(ecCtxCopy) == 1); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(ecCtx, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlMessageDigest, 0, + Pointer(EVP_sha256_nid_postfix())) == 1); + Require(ecMethod->copy(ecCtxCopy, ecCtx) == 1); + gotMd = nullptr; + Require(EVP_PKEY_CTX_ctrl_nid_postfix(ecCtxCopy, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlGetMessageDigest, 0, + &gotMd) == 1); + Require(Pointer(gotMd) == Pointer(EVP_sha256_nid_postfix())); + auto* emptyEcCtx = EVP_PKEY_CTX_new_nid_postfix(ecKey, nullptr); + Require(EVP_PKEY_verify_init_nid_postfix(emptyEcCtx) == 1); + Require(ecMethod->copy(ecCtxCopy, emptyEcCtx) == 1); + gotMd = AsDigestMethod(EVP_sha256_nid_postfix()); + Require(EVP_PKEY_CTX_ctrl_nid_postfix(ecCtxCopy, LibreSsl3PkeyEc, -1, LibreSsl3PkeyControlGetMessageDigest, 0, + &gotMd) == 1); + Require(gotMd == nullptr); + EVP_PKEY_CTX_free_nid_postfix(emptyEcCtx); + EVP_PKEY_CTX_free_nid_postfix(ecCtxCopy); + + EVP_PKEY_CTX_free_nid_postfix(ecCtx); + EVP_PKEY_free_nid_disambig1_nid_postfix(ecKey); + + ReleaseKey(rsaKey); +} + +void Get1EcKey() { + constexpr int EcCurveP256 = 415; + auto* rawEc = NewEcKey("C88F01F510D9AC3F70A292DAA2316DE544E9AAB8AFE84049C62A9C57862D1433", EcCurveP256); + auto* ecPkey = NewEcPkey(rawEc); + Require(rawEc->references == 1); + auto* got = EVP_PKEY_get1_EC_KEY_nid_disambig1_nid_postfix(ecPkey); + Require(reinterpret_cast(got) == rawEc && rawEc->references == 2); + Require(ERR_get_error_nid_postfix() == 0); + EC_KEY_free_nid_postfix(reinterpret_cast(got)); + Require(rawEc->references == 1); + EVP_PKEY_free_nid_disambig1_nid_postfix(ecPkey); + + auto* emptyEc = EVP_PKEY_new_nid_disambig1_nid_postfix(); + emptyEc->type = LibreSsl3PkeyEc; + Require(EVP_PKEY_get1_EC_KEY_nid_disambig1_nid_postfix(emptyEc) == nullptr); + Require(ERR_get_error_nid_postfix() == 0); + EVP_PKEY_free_nid_disambig1_nid_postfix(emptyEc); + + auto* rsaKey = NewNativeKey(LibreSsl3PkeyRsa); + Require(EVP_PKEY_get1_EC_KEY_nid_disambig1_nid_postfix(rsaKey) == nullptr); + ExpectErrors({EvpCode(142)}); + ReleaseKey(rsaKey); + + Require(Throws([] { EVP_PKEY_get1_EC_KEY_nid_disambig1_nid_postfix(nullptr); })); +} + int main() { std::array api{reinterpret_cast(Allocate_nid_no_patch), reinterpret_cast(Free_nid_no_patch), reinterpret_cast(Calloc_nid_no_patch), reinterpret_cast(Reallocate_nid_no_patch), @@ -1876,6 +2419,11 @@ int main() { DigestSignInitWithoutDigest(); EcdhDerivation(); GuestOutputLengths(); + DigestVerifyRsa(); + DigestVerifyPss(); + DigestVerifyEcdsa(); + DigestVerifyErrors(); + Get1EcKey(); Require(allocations.empty()); std::puts("LibreSSL3 EVP_PKEY_CTX RSA encryption and signature tests passed"); } diff --git a/docs/dev/debt/changes/feat-libressl3-native-digest-verify.md b/docs/dev/debt/changes/feat-libressl3-native-digest-verify.md new file mode 100644 index 000000000000..f49ebe290fad --- /dev/null +++ b/docs/dev/debt/changes/feat-libressl3-native-digest-verify.md @@ -0,0 +1,6 @@ +# libSceLibreSsl3 + +## add Functional + +- [EVP_DigestVerifyInit and EVP_DigestVerifyFinal](../../../core/libs/prx/libSceLibreSsl3/src/PkeyContext.cpp) (libSceLibreSsl3) - unverified on console; follow LibreSSL 4.0.0 [`m_sigver.c`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/m_sigver.c), `rsa_pmeth.c`, `ec_pmeth.c` and `ec_ameth.c` for RSA PKCS#1 v1.5, RSA-PSS and ECDSA (EC defaults to SHA-1). `ECDSA_verify` delegation returns `-1` without ASN.1 DER errors on bad DER, queues `ERR_LIB_ECDSA` (`42`) `100`/`103` instead of `EC_R_BAD_SIGNATURE` (`16`, `166`)/`EC_R_MISSING_PARAMETERS` (`16`, `124`) for out-of-range `r`/`s` or missing group/pubkey, and throws on null inner `EC_KEY`. Non-RSA/EC `IsLibreSslOnlyPkeyMethod` keys, engines, `digestverify`/`SIGCTX_CUSTOM`, foreign contexts and restricted PSS keys throw. Guest pointers are unchecked (`#22457`). +- [EVP_PKEY_get1_EC_KEY](../../../core/libs/prx/libSceLibreSsl3/src/Key.cpp) (libSceLibreSsl3) - not checked on a console. Follows LibreSSL 4.0.0 [`p_lib.c`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/p_lib.c#L715-L727) on the 32-byte `EVP_PKEY` and 64-byte `EC_KEY` layouts, sharing `EVP_PKEY_get0_EC_KEY` and `EC_KEY_up_ref`. A null key throws `invalid_argument`, a wrong key type queues `EVP_R_EXPECTING_A_EC_KEY` (`142`) and returns null, and an EC key with a null inner pointer returns null without altering references. Closes when: compared with the console. diff --git a/docs/dev/debt/changes/libressl3-20171-inventory.md b/docs/dev/debt/changes/libressl3-20171-inventory.md index fb8945521ba3..36e34d0268a7 100644 --- a/docs/dev/debt/changes/libressl3-20171-inventory.md +++ b/docs/dev/debt/changes/libressl3-20171-inventory.md @@ -16,8 +16,8 @@ ## change [Native PKEY forwarding gaps] -- [Native PKEY forwarding gaps](../../../core/libs/prx/libSceLibreSsl3/ParentExports.cpp) - `X509_get_pubkey` still forwards to a legacy throwing stub. `SSL_get_privatekey` forwards to the parent library and returns its legacy PKEY. The consumers `EVP_PKEY_get1_EC_KEY`, `EVP_DigestVerifyInit`, `SSL_CTX_use_PrivateKey`, `SSL_CTX_use_PrivateKey_ASN1`, `SSL_CTX_use_PrivateKey_file` and `SSL_use_PrivateKey_ASN1` do too. Each needs a native wrapper before its legacy implementation can run, to prevent mixing the incompatible PKEY layouts. +- [Native PKEY forwarding gaps](../../../core/libs/prx/libSceLibreSsl3/ParentExports.cpp) - `X509_get_pubkey` still forwards to a legacy throwing stub. `SSL_get_privatekey` forwards to the parent library and returns its legacy PKEY. The consumers `SSL_CTX_use_PrivateKey`, `SSL_CTX_use_PrivateKey_ASN1`, `SSL_CTX_use_PrivateKey_file` and `SSL_use_PrivateKey_ASN1` do too. Each needs a native wrapper before its legacy implementation can run, to prevent mixing the incompatible PKEY layouts. ## change [Parent export forwarding] -- [Parent export forwarding](../../../core/libs/prx/libSceLibreSsl3/ParentExports.cpp) exposes 382 parent-library exports on Windows through IAT tail jumps without changing their ABI. Non-Windows hosts use their existing dependency lookup. Static COFF inspection verifies names, parent imports and tail jumps; Windows loading and runtime behavior remain unverified. Parent functions retain their existing implementation limitations. +- [Parent export forwarding](../../../core/libs/prx/libSceLibreSsl3/ParentExports.cpp) exposes 368 parent-library exports on Windows through IAT tail jumps without changing their ABI. Non-Windows hosts use their existing dependency lookup. Static COFF inspection verifies names, parent imports and tail jumps; Windows loading and runtime behavior remain unverified. Parent functions retain their existing implementation limitations.