Skip to content

Commit 2580833

Browse files
panvanpaun
authored andcommitted
crypto: support Ed448 and ML-DSA context parameter in Web Cryptography
PR-URL: nodejs/node#59570 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e2b9db7 commit 2580833

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

include/ncrypto.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,15 @@ class EVPMDCtxPointer final {
14291429
std::optional<EVP_PKEY_CTX*> verifyInit(const EVPKeyPointer& key,
14301430
const Digest& digest);
14311431

1432+
std::optional<EVP_PKEY_CTX*> signInitWithContext(
1433+
const EVPKeyPointer& key,
1434+
const Digest& digest,
1435+
const Buffer<const unsigned char>& context_string);
1436+
std::optional<EVP_PKEY_CTX*> verifyInitWithContext(
1437+
const EVPKeyPointer& key,
1438+
const Digest& digest,
1439+
const Buffer<const unsigned char>& context_string);
1440+
14321441
DataPointer signOneShot(const Buffer<const unsigned char>& buf) const;
14331442
DataPointer sign(const Buffer<const unsigned char>& buf) const;
14341443
bool verify(const Buffer<const unsigned char>& buf,

src/ncrypto.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,6 +4481,54 @@ std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInit(
44814481
return ctx;
44824482
}
44834483

4484+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::signInitWithContext(
4485+
const EVPKeyPointer& key,
4486+
const Digest& digest,
4487+
const Buffer<const unsigned char>& context_string) {
4488+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4489+
EVP_PKEY_CTX* ctx = nullptr;
4490+
4491+
const OSSL_PARAM params[] = {
4492+
OSSL_PARAM_construct_octet_string(
4493+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4494+
const_cast<unsigned char*>(context_string.data),
4495+
context_string.len),
4496+
OSSL_PARAM_END};
4497+
4498+
if (!EVP_DigestSignInit_ex(
4499+
ctx_.get(), &ctx, nullptr, nullptr, nullptr, key.get(), params)) {
4500+
return std::nullopt;
4501+
}
4502+
return ctx;
4503+
#else
4504+
return std::nullopt;
4505+
#endif
4506+
}
4507+
4508+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInitWithContext(
4509+
const EVPKeyPointer& key,
4510+
const Digest& digest,
4511+
const Buffer<const unsigned char>& context_string) {
4512+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4513+
EVP_PKEY_CTX* ctx = nullptr;
4514+
4515+
const OSSL_PARAM params[] = {
4516+
OSSL_PARAM_construct_octet_string(
4517+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4518+
const_cast<unsigned char*>(context_string.data),
4519+
context_string.len),
4520+
OSSL_PARAM_END};
4521+
4522+
if (!EVP_DigestVerifyInit_ex(
4523+
ctx_.get(), &ctx, nullptr, nullptr, nullptr, key.get(), params)) {
4524+
return std::nullopt;
4525+
}
4526+
return ctx;
4527+
#else
4528+
return std::nullopt;
4529+
#endif
4530+
}
4531+
44844532
DataPointer EVPMDCtxPointer::signOneShot(
44854533
const Buffer<const unsigned char>& buf) const {
44864534
if (!ctx_) return {};

0 commit comments

Comments
 (0)