Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit fd7e904

Browse files
panvaaduh95
authored andcommitted
test: update tests to run with OpenSSL >= 3.0 FIPS mode
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #64960Fixes: #48379 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 219495f commit fd7e904

130 files changed

Lines changed: 2869 additions & 1014 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Žlib/internal/crypto/webcrypto.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,7 @@ class SubtleCrypto {
16171617
}
16181618

16191619
// Implements https://wicg.github.io/webcrypto-modern-algos/#SubtleCrypto-method-supports
1620+
// TODO(panva): Make supports() account for the active FIPS state.
16201621
staticsupports(operation,algorithm,lengthOrAdditionalAlgorithm=null){
16211622
emitExperimentalWarning('The supports Web Crypto API method');
16221623
if(this!==SubtleCrypto)thrownewERR_INVALID_THIS('SubtleCrypto constructor');

β€Žtest/common/crypto.jsβ€Ž

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ function assertApproximateSize(key, expectedSize) {
5050
functiontestEncryptDecrypt(publicKey,privateKey){
5151
constmessage='Hello Node.js world!';
5252
constplaintext=Buffer.from(message,'utf8');
53+
constwithOaepHash=(key)=>{
54+
if(!hasFIPS(3))returnkey;
55+
if(key?.key!==undefined)return{ ...key,oaepHash: 'sha256'};
56+
return{ key,oaepHash: 'sha256'};
57+
};
5358
for(constkeyof[publicKey,privateKey]){
54-
constciphertext=publicEncrypt(key,plaintext);
55-
constreceived=privateDecrypt(privateKey,ciphertext);
59+
constciphertext=publicEncrypt(withOaepHash(key),plaintext);
60+
constreceived=privateDecrypt(withOaepHash(privateKey),ciphertext);
5661
assert.strictEqual(received.toString('utf8'),message);
5762
}
5863
}
@@ -118,6 +123,10 @@ const hasOpenSSL = (major = 0, minor = 0, patch = 0) => {
118123
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119124
};
120125

126+
consthasFIPS=(major=0,minor=0,patch=0)=>{
127+
returncrypto.getFips()===1&&hasOpenSSL(major,minor,patch);
128+
};
129+
121130
letopensslCli=null;
122131

123132
module.exports={
@@ -134,6 +143,7 @@ module.exports = {
134143
sec1Exp,
135144
sec1EncExp,
136145
hasOpenSSL,
146+
hasFIPS,
137147
gethasOpenSSL3(){
138148
returnhasOpenSSL(3);
139149
},

β€Žtest/fixtures/keys/Makefileβ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all: \
88
ca5-cert.pem \
99
ca6-cert.pem \
1010
agent1-cert.pem \
11+
agent1-fips.pfx \
1112
agent1.pfx \
1213
agent2-cert.pem \
1314
agent3-cert.pem \
@@ -39,6 +40,7 @@ all: \
3940
dsa_private_encrypted_1025.pem \
4041
dsa_public_1025.pem \
4142
ec-cert.pem \
43+
ec-fips.pfx \
4244
ec.pfx \
4345
fake-cnnic-root-cert.pem \
4446
intermediate-ca-cert.pem \
@@ -444,6 +446,20 @@ agent1.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
444446
-out agent1.pfx \
445447
-password pass:sample
446448

449+
# PKCS12KDF is unavailable under FIPS properties. Use PBMAC1 with PBKDF2
450+
# instead, alongside AES-256/PBKDF2 key protection.
451+
agent1-fips.pfx: agent1-cert.pem agent1-key.pem ca1-cert.pem
452+
openssl pkcs12 -export \
453+
-keypbe AES-256-CBC \
454+
-certpbe AES-256-CBC \
455+
-iter 2048 \
456+
-pbmac1_pbkdf2 \
457+
-in agent1-cert.pem \
458+
-inkey agent1-key.pem \
459+
-certfile ca1-cert.pem \
460+
-out agent1-fips.pfx \
461+
-password pass:password
462+
447463
agent1-verify: agent1-cert.pem ca1-cert.pem
448464
openssl verify -CAfile ca1-cert.pem agent1-cert.pem
449465

@@ -787,6 +803,18 @@ ec.pfx: ec-cert.pem ec-key.pem
787803
-out ec.pfx \
788804
-password pass:
789805

806+
# See agent1-fips.pfx for why the FIPS fixture uses PBMAC1.
807+
ec-fips.pfx: ec-cert.pem ec-key.pem
808+
openssl pkcs12 -export \
809+
-keypbe AES-256-CBC \
810+
-certpbe AES-256-CBC \
811+
-iter 2048 \
812+
-pbmac1_pbkdf2 \
813+
-in ec-cert.pem \
814+
-inkey ec-key.pem \
815+
-out ec-fips.pfx \
816+
-password pass:password
817+
790818
dh512.pem:
791819
openssl dhparam -out dh512.pem 512
792820

3.72 KB
Binary file not shown.
1.23 KB
Binary file not shown.

β€Žtest/parallel/test-crypto-argon2-job.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const common = require('../common');
44
if(!common.hasCrypto)
55
common.skip('missing crypto');
66

7-
const{ hasOpenSSL }=require('../common/crypto');
7+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
88

99
if(!hasOpenSSL(3,2))
1010
common.skip('requires OpenSSL >= 3.2');
11+
if(hasFIPS(3))
12+
common.skip('Argon2 is not available in FIPS mode');
1113

1214
// Exercises the native Argon2 job directly via internalBinding, bypassing
1315
// the JS validators, to ensure that if invalid parameters ever reach the

β€Žtest/parallel/test-crypto-argon2.jsβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{ hasOpenSSL }=require('../common/crypto');
6+
const{hasFIPS,hasOpenSSL }=require('../common/crypto');
77

88
if(!hasOpenSSL(3,2))
99
common.skip('requires OpenSSL >= 3.2');
@@ -28,6 +28,17 @@ const secret = Buffer.alloc(8, 0x03);
2828
constassociatedData=Buffer.alloc(12,0x04);
2929
constdefaults={ message, nonce,parallelism: 1,tagLength: 64,memory: 8,passes: 3};
3030

31+
if(hasFIPS(3)){
32+
assert.throws(()=>crypto.argon2Sync('argon2id',defaults),{
33+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
34+
});
35+
crypto.argon2('argon2id',defaults,common.mustCall((err,result)=>{
36+
assert.strictEqual(err?.code,'ERR_OSSL_EVP_UNSUPPORTED');
37+
assert.strictEqual(result,undefined);
38+
}));
39+
return;
40+
}
41+
3142
constgood=[
3243
// Test vectors from RFC 9106 https://www.rfc-editor.org/rfc/rfc9106.html#name-test-vectors
3344
// and OpenSSL 3.2 https://github.com/openssl/openssl/blob/6dfa998f7ea150f9c6d4e4727cf6d5c82a68a8da/test/recipes/30-test_evp_data/evpkdf_argon2.txt

β€Žtest/parallel/test-crypto-async-sign-verify.jsβ€Ž

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const common = require('../common');
33
if(!common.hasCrypto)
44
common.skip('missing crypto');
55

6-
const{hasOpenSSL3}=require('../common/crypto');
6+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
77
constassert=require('assert');
88
constutil=require('util');
99
constcrypto=require('crypto');
1010
constfixtures=require('../common/fixtures');
1111

12+
constfips3=hasFIPS(3);
13+
1214
functiontest(
1315
publicFixture,
1416
privateFixture,
@@ -65,6 +67,15 @@ function test(
6567
}
6668
}
6769

70+
functiontestSignFailure(privateFixture,algorithm,options,code){
71+
constkey={key: fixtures.readKey(privateFixture), ...options};
72+
constdata=Buffer.from('Hello world');
73+
assert.throws(()=>crypto.sign(algorithm,data,key),{ code });
74+
crypto.sign(algorithm,data,key,common.mustCall((err)=>{
75+
assert.strictEqual(err?.code,code);
76+
}));
77+
}
78+
6879
// RSA w/ default padding
6980
test('rsa_public.pem','rsa_private.pem','sha256',true);
7081
test('rsa_public.pem','rsa_private.pem','sha256',true,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105
test('ed448_public.pem','ed448_private.pem',undefined,true);
95106

96107
// ECDSA w/ der signature encoding
97-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
98-
false);
99-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
100-
false,{dsaEncoding: 'der'});
101-
102-
// ECDSA w/ ieee-p1363 signature encoding
103-
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
104-
{dsaEncoding: 'ieee-p1363'});
108+
if(fips3){
109+
testSignFailure('ec_secp256k1_private.pem','sha384',{},
110+
'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE');
111+
}else{
112+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
113+
false);
114+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',
115+
false,{dsaEncoding: 'der'});
116+
117+
// ECDSA w/ ieee-p1363 signature encoding
118+
test('ec_secp256k1_public.pem','ec_secp256k1_private.pem','sha384',false,
119+
{dsaEncoding: 'ieee-p1363'});
120+
}
105121

106122
// DSA w/ der signature encoding
107123
test('dsa_public.pem','dsa_private.pem','sha256',
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173

158174
letexpected=/nodefaultdigest/;
159175
letexpectedCode='ERR_OSSL_EVP_NO_DEFAULT_DIGEST';
160-
if(hasOpenSSL3||process.features.openssl_is_boringssl){
176+
if(hasOpenSSL(3)||process.features.openssl_is_boringssl){
161177
expected=/operation[\s_]not[\s_]supported[\s_]for[\s_]this[\s_]keytype/i;
162178
expectedCode='ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE';
163179
}
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186
}
171187

172188
{
173-
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
174-
modulusLength: 512
175-
});
176-
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
177-
assert.ok(err);
178-
assert.match(err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
179-
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
180-
}));
189+
if(fips3){
190+
crypto.generateKeyPair('rsa',{modulusLength: 512},
191+
common.mustCall((err)=>{
192+
assert.strictEqual(
193+
err?.code,'ERR_OSSL_RSA_INVALID_MODULUS');
194+
}));
195+
}else{
196+
const{ privateKey }=crypto.generateKeyPairSync('rsa',{
197+
modulusLength: 512
198+
});
199+
crypto.sign('sha512','message',privateKey,common.mustCall((err)=>{
200+
assert.ok(err);
201+
assert.match(
202+
err.message,/digest[\s_]too[\s_]big[\s_]for[\s_]rsa[\s_]key/i);
203+
assert.match(err.code,/^ERR_OSSL_.*DIGEST_TOO_BIG_FOR_RSA_KEY$/);
204+
}));
205+
}
181206
}

β€Žtest/parallel/test-crypto-authenticated-stream.jsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if (!common.hasCrypto)
66

77
constassert=require('assert');
88
constcrypto=require('crypto');
9+
const{ hasFIPS }=require('../common/crypto');
910
constfs=require('fs');
1011
conststream=require('stream');
1112
consttmpdir=require('../common/tmpdir');
@@ -120,6 +121,16 @@ function test(config) {
120121
return;
121122
}
122123

124+
if(hasFIPS(3)){
125+
assert.throws(()=>crypto.createDecipheriv(
126+
config.cipher,config.key,config.iv,{
127+
authTagLength: config.authTagLength,
128+
}),{
129+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
130+
});
131+
return;
132+
}
133+
123134
direct(config);
124135
mstream(config);
125136
fstream(config);

β€Žtest/parallel/test-crypto-authenticated.jsβ€Ž

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const assert = require('assert');
2929
constcrypto=require('crypto');
3030
const{ inspect }=require('util');
3131
constfixtures=require('../common/fixtures');
32-
const{hasOpenSSL3}=require('../common/crypto');
32+
const{hasOpenSSL, hasFIPS}=require('../common/crypto');
3333

34-
constisFipsEnabled=crypto.getFips();
34+
constisFipsEnabled=crypto.getFips()===1;
35+
constfips3=hasFIPS(3);
3536

3637
//
3738
// Test authenticated encryption modes.
@@ -559,6 +560,14 @@ for (const test of TEST_CASES) {
559560
constciphertext=Buffer.concat([cipher.update(plain),cipher.final()]);
560561
consttag=cipher.getAuthTag();
561562

563+
if(fips3&&mode==='ccm'){
564+
assert.throws(()=>crypto.createDecipheriv(
565+
`aes-128-${mode}`,key,iv,opts),{
566+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
567+
});
568+
continue;
569+
}
570+
562571
constdecipher=crypto.createDecipheriv(`aes-128-${mode}`,key,iv,opts);
563572
decipher.setAuthTag(tag);
564573
assert.throws(()=>{
@@ -636,15 +645,22 @@ for (const test of TEST_CASES) {
636645
constcipher=crypto.createCipheriv('aes-128-ccm',key,iv,opts);
637646
assert.throws(()=>{
638647
cipher.final();
639-
},hasOpenSSL3 ? {
648+
},hasOpenSSL(3) ? {
640649
code: 'ERR_OSSL_TAG_NOT_SET'
641650
} : {
642651
message: /Unsupportedstate/
643652
});
644653
}
645654
}
646655

647-
if(!process.features.openssl_is_boringssl){
656+
if(fips3){
657+
assert.throws(()=>crypto.createCipheriv(
658+
'chacha20-poly1305',Buffer.alloc(32),Buffer.alloc(12),{
659+
authTagLength: 16,
660+
}),{
661+
code: 'ERR_OSSL_EVP_UNSUPPORTED',
662+
});
663+
}elseif(!process.features.openssl_is_boringssl){
648664
constkey=Buffer.alloc(32);
649665
constiv=Buffer.alloc(12);
650666

@@ -662,7 +678,7 @@ if (!process.features.openssl_is_boringssl) {
662678

663679
// ChaCha20-Poly1305 should respect the authTagLength option and should not
664680
// require the authentication tag before calls to update() during decryption.
665-
if(!process.features.openssl_is_boringssl){
681+
if(!fips3&&!process.features.openssl_is_boringssl){
666682
constkey=Buffer.alloc(32);
667683
constiv=Buffer.alloc(12);
668684

@@ -713,7 +729,7 @@ if (!process.features.openssl_is_boringssl) {
713729
// shorter tags as long as their length was valid according to NIST SP 800-38D.
714730
// For ChaCha20-Poly1305, we intentionally deviate from that because there are
715731
// no recommended or approved authentication tag lengths below 16 bytes.
716-
if(!process.features.openssl_is_boringssl){
732+
if(!fips3&&!process.features.openssl_is_boringssl){
717733
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
718734
returnalgo==='chacha20-poly1305'&&tampered===false;
719735
});
@@ -752,7 +768,7 @@ if (!process.features.openssl_is_boringssl) {
752768
}
753769

754770
// https://github.com/nodejs/node/issues/45874
755-
if(!process.features.openssl_is_boringssl){
771+
if(!fips3&&!process.features.openssl_is_boringssl){
756772
constrfcTestCases=TEST_CASES.filter(({ algo, tampered })=>{
757773
returnalgo==='chacha20-poly1305'&&tampered===false;
758774
});
@@ -798,13 +814,20 @@ if (ciphers.includes('aes-128-ccm')) {
798814
consttag=cipher.getAuthTag();
799815
assert.strictEqual(tag.length,16);
800816

801-
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
802-
authTagLength: 16,
803-
});
804-
decipher.setAuthTag(tag);
805-
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
806-
decipher.update(newDataView(newArrayBuffer(0)));
807-
decipher.final();
817+
if(fips3){
818+
assert.throws(()=>crypto.createDecipheriv(
819+
'aes-128-ccm',key,nonce,{authTagLength: 16}),{
820+
code: 'ERR_CRYPTO_UNSUPPORTED_OPERATION',
821+
});
822+
}else{
823+
constdecipher=crypto.createDecipheriv('aes-128-ccm',key,nonce,{
824+
authTagLength: 16,
825+
});
826+
decipher.setAuthTag(tag);
827+
decipher.setAAD(Buffer.alloc(0),{plaintextLength: 0});
828+
decipher.update(newDataView(newArrayBuffer(0)));
829+
decipher.final();
830+
}
808831
}else{
809832
common.printSkipMessage('Skipping unsupported aes-128-ccm test');
810833
}

0 commit comments

Comments
Β (0)