Skip to content
Permalink
Browse files

crypto: simplify DSA validation in FIPS mode

PR-URL: #29195
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information...
tniessen authored and BridgeAR committed Aug 18, 2019
1 parent 219c195 commit e0537e6978d45617ba618651c0585a13e1c10eaf
Showing with 23 additions and 49 deletions.
  1. +23 −49 src/node_crypto.cc
@@ -4877,15 +4877,7 @@ static AllocatedBuffer Node_SignFinal(Environment* env,
return AllocatedBuffer();
}

Sign::SignResult Sign::SignFinal(
const ManagedEVPPKey& pkey,
int padding,
const Maybe<int>& salt_len) {
if (!mdctx_)
return SignResult(kSignNotInitialised);

EVPMDPointer mdctx = std::move(mdctx_);

static inline bool ValidateDSAParameters(EVP_PKEY* key) {
#ifdef NODE_FIPS_MODE
/* Validate DSA2 parameters from FIPS 186-4 */
if (FIPS_mode() && EVP_PKEY_DSA == EVP_PKEY_base_id(pkey.get())) {
@@ -4896,23 +4888,29 @@ Sign::SignResult Sign::SignFinal(
const BIGNUM* q;
DSA_get0_pqg(dsa, nullptr, &q, nullptr);
size_t N = BN_num_bits(q);
bool result = false;

if (L == 1024 && N == 160)
result = true;
else if (L == 2048 && N == 224)
result = true;
else if (L == 2048 && N == 256)
result = true;
else if (L == 3072 && N == 256)
result = true;

if (!result) {
return SignResult(kSignPrivateKey);
}

return (L == 1024 && N == 160) ||
(L == 2048 && N == 224) ||
(L == 2048 && N == 256) ||
(L == 3072 && N == 256)
}
#endif // NODE_FIPS_MODE

return true;
}

Sign::SignResult Sign::SignFinal(
const ManagedEVPPKey& pkey,
int padding,
const Maybe<int>& salt_len) {
if (!mdctx_)
return SignResult(kSignNotInitialised);

EVPMDPointer mdctx = std::move(mdctx_);

if (!ValidateDSAParameters(pkey.get()))
return SignResult(kSignPrivateKey);

AllocatedBuffer buffer =
Node_SignFinal(env(), std::move(mdctx), pkey, padding, salt_len);
Error error = buffer.data() == nullptr ? kSignPrivateKey : kSignOk;
@@ -4963,32 +4961,8 @@ void SignOneShot(const FunctionCallbackInfo<Value>& args) {
if (!key)
return;

#ifdef NODE_FIPS_MODE
/* Validate DSA2 parameters from FIPS 186-4 */
if (FIPS_mode() && EVP_PKEY_DSA == EVP_PKEY_base_id(key.get())) {
DSA* dsa = EVP_PKEY_get0_DSA(key.get());
const BIGNUM* p;
DSA_get0_pqg(dsa, &p, nullptr, nullptr);
size_t L = BN_num_bits(p);
const BIGNUM* q;
DSA_get0_pqg(dsa, nullptr, &q, nullptr);
size_t N = BN_num_bits(q);
bool result = false;

if (L == 1024 && N == 160)
result = true;
else if (L == 2048 && N == 224)
result = true;
else if (L == 2048 && N == 256)
result = true;
else if (L == 3072 && N == 256)
result = true;

if (!result) {
return CheckThrow(env, SignBase::Error::kSignPrivateKey);
}
}
#endif // NODE_FIPS_MODE
if (!ValidateDSAParameters(key.get()))
return CheckThrow(env, SignBase::Error::kSignPrivateKey);

ArrayBufferViewContents<char> data(args[offset]);

0 comments on commit e0537e6

Please sign in to comment.
You can’t perform that action at this time.