Skip to content

Commit 5e53ca8

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 461efa0 commit 5e53ca8

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
@@ -1299,6 +1299,15 @@ class EVPMDCtxPointer final {
12991299
std::optional<EVP_PKEY_CTX*> verifyInit(const EVPKeyPointer& key,
13001300
const EVP_MD* digest);
13011301

1302+
std::optional<EVP_PKEY_CTX*> signInitWithContext(
1303+
const EVPKeyPointer& key,
1304+
const Digest& digest,
1305+
const Buffer<const unsigned char>& context_string);
1306+
std::optional<EVP_PKEY_CTX*> verifyInitWithContext(
1307+
const EVPKeyPointer& key,
1308+
const Digest& digest,
1309+
const Buffer<const unsigned char>& context_string);
1310+
13021311
DataPointer signOneShot(const Buffer<const unsigned char>& buf) const;
13031312
DataPointer sign(const Buffer<const unsigned char>& buf) const;
13041313
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
@@ -4220,6 +4220,54 @@ std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInit(
42204220
return ctx;
42214221
}
42224222

4223+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::signInitWithContext(
4224+
const EVPKeyPointer& key,
4225+
const Digest& digest,
4226+
const Buffer<const unsigned char>& context_string) {
4227+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4228+
EVP_PKEY_CTX* ctx = nullptr;
4229+
4230+
const OSSL_PARAM params[] = {
4231+
OSSL_PARAM_construct_octet_string(
4232+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4233+
const_cast<unsigned char*>(context_string.data),
4234+
context_string.len),
4235+
OSSL_PARAM_END};
4236+
4237+
if (!EVP_DigestSignInit_ex(
4238+
ctx_.get(), &ctx, nullptr, nullptr, nullptr, key.get(), params)) {
4239+
return std::nullopt;
4240+
}
4241+
return ctx;
4242+
#else
4243+
return std::nullopt;
4244+
#endif
4245+
}
4246+
4247+
std::optional<EVP_PKEY_CTX*> EVPMDCtxPointer::verifyInitWithContext(
4248+
const EVPKeyPointer& key,
4249+
const Digest& digest,
4250+
const Buffer<const unsigned char>& context_string) {
4251+
#ifdef OSSL_SIGNATURE_PARAM_CONTEXT_STRING
4252+
EVP_PKEY_CTX* ctx = nullptr;
4253+
4254+
const OSSL_PARAM params[] = {
4255+
OSSL_PARAM_construct_octet_string(
4256+
OSSL_SIGNATURE_PARAM_CONTEXT_STRING,
4257+
const_cast<unsigned char*>(context_string.data),
4258+
context_string.len),
4259+
OSSL_PARAM_END};
4260+
4261+
if (!EVP_DigestVerifyInit_ex(
4262+
ctx_.get(), &ctx, nullptr, nullptr, nullptr, key.get(), params)) {
4263+
return std::nullopt;
4264+
}
4265+
return ctx;
4266+
#else
4267+
return std::nullopt;
4268+
#endif
4269+
}
4270+
42234271
DataPointer EVPMDCtxPointer::signOneShot(
42244272
const Buffer<const unsigned char>& buf) const {
42254273
if (!ctx_) return {};

0 commit comments

Comments
 (0)