@@ -16,6 +16,26 @@ function getOutcome(fn) {
1616 }
1717}
1818
19+ let traceStep = 0 ;
20+ function trace ( message ) {
21+ process . _rawDebug ( `[test-crypto-negative-zero] ${ ++ traceStep } ${ message } ` ) ;
22+ }
23+
24+ function formatOutcome ( outcome ) {
25+ if ( outcome . err != null ) {
26+ const { code, message, name } = outcome . err ;
27+ return `error name=${ name } code=${ code } message=${ message } ` ;
28+ }
29+ return 'success' ;
30+ }
31+
32+ function getTracedOutcome ( label , fn ) {
33+ trace ( `${ label } : start` ) ;
34+ const outcome = getOutcome ( fn ) ;
35+ trace ( `${ label } : ${ formatOutcome ( outcome ) } ` ) ;
36+ return outcome ;
37+ }
38+
1939function assertSameOutcome ( actual , expected ) {
2040 if ( expected . err !== undefined ) {
2141 assert ( actual . err instanceof Error ) ;
@@ -38,123 +58,169 @@ function assertSameErrorOrSuccess(actual, expected) {
3858 }
3959}
4060
61+ trace (
62+ `start openssl=${ process . versions . openssl } ` +
63+ `fips=${ crypto . getFips ( ) } ` +
64+ `boringssl=${ process . features . openssl_is_boringssl } ` ,
65+ ) ;
66+
4167{
42- const expected = getOutcome ( ( ) =>
68+ const expected = getTracedOutcome ( 'hkdfSync 0' , ( ) =>
4369 crypto . hkdfSync ( 'sha256' , 'key' , 'salt' , 'info' , 0 ) ,
4470 ) ;
4571 assertSameOutcome (
46- getOutcome ( ( ) => crypto . hkdfSync ( 'sha256' , 'key' , 'salt' , 'info' , - 0 ) ) ,
72+ getTracedOutcome ( 'hkdfSync -0' , ( ) =>
73+ crypto . hkdfSync ( 'sha256' , 'key' , 'salt' , 'info' , - 0 ) ) ,
4774 expected ,
4875 ) ;
76+ trace ( 'hkdf -0: start' ) ;
4977 crypto . hkdf ( 'sha256' , 'key' , 'salt' , 'info' , - 0 ,
5078 common . mustCall ( ( err , result ) => {
79+ trace ( `hkdf -0 callback: ${ formatOutcome ( { err, result } ) } ` ) ;
5180 assertSameOutcome ( { err, result } , expected ) ;
5281 } ) ) ;
5382}
5483
5584{
85+ trace ( 'checkPrimeSync -0: start' ) ;
5686 assert . strictEqual (
5787 crypto . checkPrimeSync ( Buffer . from ( [ 3 ] ) , { checks : - 0 } ) ,
5888 true ,
5989 ) ;
90+ trace ( 'checkPrimeSync -0: success' ) ;
91+ trace ( 'checkPrime -0: start' ) ;
6092 crypto . checkPrime ( Buffer . from ( [ 3 ] ) , { checks : - 0 } ,
6193 common . mustSucceed ( ( result ) => {
94+ trace ( 'checkPrime -0 callback: success' ) ;
6295 assert . strictEqual ( result , true ) ;
6396 } ) ) ;
6497}
6598
6699{
100+ trace ( 'createDiffieHellman -0: start' ) ;
67101 assert . throws ( ( ) => crypto . createDiffieHellman ( - 0 , 2 ) , {
68102 name : 'Error' ,
69103 } ) ;
104+ trace ( 'createDiffieHellman -0: success' ) ;
70105}
71106
72107{
73- for ( const [ type , getOptions ] of [
74- [ 'rsa' , ( zero ) => ( { modulusLength : zero } ) ] ,
75- [ 'rsa' , ( zero ) => ( { modulusLength : 512 , publicExponent : zero } ) ] ,
76- [ 'rsa-pss' , ( zero ) => ( {
108+ for ( const [ label , type , getOptions ] of [
109+ [ 'rsa modulusLength' , 'rsa' , ( zero ) => ( { modulusLength : zero } ) ] ,
110+ [ 'rsa publicExponent' , 'rsa' ,
111+ ( zero ) => ( { modulusLength : 512 , publicExponent : zero } ) ] ,
112+ [ 'rsa-pss saltLength' , 'rsa-pss' , ( zero ) => ( {
77113 modulusLength : 512 ,
78114 publicExponent : 65537 ,
79115 saltLength : zero ,
80116 } ) ] ,
81- [ 'dsa' , ( zero ) => ( { modulusLength : zero } ) ] ,
82- [ 'dh' , ( zero ) => ( { primeLength : zero } ) ] ,
83- [ 'dh' , ( zero ) => ( { primeLength : 2 , generator : zero } ) ] ,
117+ [ 'dsa modulusLength' , 'dsa ', ( zero ) => ( { modulusLength : zero } ) ] ,
118+ [ 'dh primeLength' , 'dh ', ( zero ) => ( { primeLength : zero } ) ] ,
119+ [ 'dh generator' , 'dh ', ( zero ) => ( { primeLength : 2 , generator : zero } ) ] ,
84120 ] ) {
85121 assertSameErrorOrSuccess (
86- getOutcome ( ( ) => crypto . generateKeyPairSync ( type , getOptions ( - 0 ) ) ) ,
87- getOutcome ( ( ) => crypto . generateKeyPairSync ( type , getOptions ( 0 ) ) ) ,
122+ getTracedOutcome ( `generateKeyPairSync ${ label } -0` ,
123+ ( ) => crypto . generateKeyPairSync (
124+ type , getOptions ( - 0 ) ) ) ,
125+ getTracedOutcome ( `generateKeyPairSync ${ label } 0` ,
126+ ( ) => crypto . generateKeyPairSync (
127+ type , getOptions ( 0 ) ) ) ,
88128 ) ;
89129 }
90130
91131 if ( ! hasOpenSSL ( 3 ) && crypto . getFips ( ) ) {
132+ trace ( 'generateKeyPairSync dsa divisorLength: skipped' ) ;
92133 common . printSkipMessage (
93134 'Skipping DSA divisorLength 0 key generation on OpenSSL 1.1.1' ) ;
94135 } else {
95136 assertSameErrorOrSuccess (
96- getOutcome ( ( ) => crypto . generateKeyPairSync ( 'dsa' , {
97- modulusLength : 512 ,
98- divisorLength : - 0 ,
99- } ) ) ,
100- getOutcome ( ( ) => crypto . generateKeyPairSync ( 'dsa' , {
101- modulusLength : 512 ,
102- divisorLength : 0 ,
103- } ) ) ,
137+ getTracedOutcome ( 'generateKeyPairSync dsa divisorLength -0' , ( ) => {
138+ return crypto . generateKeyPairSync ( 'dsa' , {
139+ modulusLength : 512 ,
140+ divisorLength : - 0 ,
141+ } ) ;
142+ } ) ,
143+ getTracedOutcome ( 'generateKeyPairSync dsa divisorLength 0' , ( ) => {
144+ return crypto . generateKeyPairSync ( 'dsa' , {
145+ modulusLength : 512 ,
146+ divisorLength : 0 ,
147+ } ) ;
148+ } ) ,
104149 ) ;
105150 }
106151
152+ trace ( 'generateKeyPair rsa modulusLength -0: start' ) ;
107153 crypto . generateKeyPair ( 'rsa' , { modulusLength : - 0 } ,
108154 common . mustCall ( ( err ) => {
155+ trace (
156+ 'generateKeyPair rsa modulusLength -0 ' +
157+ `callback: ${ formatOutcome ( { err } ) } ` ,
158+ ) ;
109159 assert ( err instanceof Error ) ;
110160 } ) ) ;
111161}
112162
113163if ( ! process . features . openssl_is_boringssl ) {
164+ trace ( 'createHash shake128 outputLength -0: start' ) ;
114165 assert . strictEqual (
115166 crypto . createHash ( 'shake128' , { outputLength : - 0 } ) . digest ( 'hex' ) ,
116167 '' ,
117168 ) ;
169+ trace ( 'createHash shake128 outputLength -0: success' ) ;
170+ trace ( 'copy shake128 outputLength -0: start' ) ;
118171 assert . strictEqual (
119172 crypto . createHash ( 'shake128' , { outputLength : 5 } )
120173 . copy ( { outputLength : - 0 } )
121174 . digest ( 'hex' ) ,
122175 '' ,
123176 ) ;
177+ trace ( 'copy shake128 outputLength -0: success' ) ;
178+ trace ( 'crypto.hash shake128 outputLength -0: start' ) ;
124179 assert . strictEqual (
125180 crypto . hash ( 'shake128' , 'data' , { outputLength : - 0 } ) ,
126181 '' ,
127182 ) ;
183+ trace ( 'crypto.hash shake128 outputLength -0: success' ) ;
128184}
129185
130186{
131187 const key = Buffer . alloc ( 16 ) ;
132188 const iv = Buffer . alloc ( 12 ) ;
133189
134190 assertSameErrorOrSuccess (
135- getOutcome ( ( ) => crypto . createCipheriv (
136- 'aes-128-gcm' , key , iv , { authTagLength : - 0 } ) ) ,
137- getOutcome ( ( ) => crypto . createCipheriv (
138- 'aes-128-gcm' , key , iv , { authTagLength : 0 } ) ) ,
191+ getTracedOutcome ( 'createCipheriv authTagLength -0' , ( ) => {
192+ return crypto . createCipheriv (
193+ 'aes-128-gcm' , key , iv , { authTagLength : - 0 } ) ;
194+ } ) ,
195+ getTracedOutcome ( 'createCipheriv authTagLength 0' , ( ) => {
196+ return crypto . createCipheriv (
197+ 'aes-128-gcm' , key , iv , { authTagLength : 0 } ) ;
198+ } ) ,
139199 ) ;
140200 assertSameErrorOrSuccess (
141- getOutcome ( ( ) => crypto . createCipheriv (
142- 'aes-128-gcm' , key , iv ) . setAAD (
143- Buffer . alloc ( 0 ) ,
144- { plaintextLength : - 0 } ,
145- ) ) ,
146- getOutcome ( ( ) => crypto . createCipheriv (
147- 'aes-128-gcm' , key , iv ) . setAAD (
148- Buffer . alloc ( 0 ) ,
149- { plaintextLength : 0 } ,
150- ) ) ,
201+ getTracedOutcome ( 'setAAD plaintextLength -0' , ( ) => {
202+ return crypto . createCipheriv ( 'aes-128-gcm' , key , iv ) . setAAD (
203+ Buffer . alloc ( 0 ) ,
204+ { plaintextLength : - 0 } ,
205+ ) ;
206+ } ) ,
207+ getTracedOutcome ( 'setAAD plaintextLength 0' , ( ) => {
208+ return crypto . createCipheriv ( 'aes-128-gcm' , key , iv ) . setAAD (
209+ Buffer . alloc ( 0 ) ,
210+ { plaintextLength : 0 } ,
211+ ) ;
212+ } ) ,
151213 ) ;
214+ trace ( 'getCipherInfo keyLength -0: start' ) ;
152215 assert . strictEqual (
153216 crypto . getCipherInfo ( 'aes-128-cbc' , { keyLength : - 0 } ) ,
154217 undefined ,
155218 ) ;
219+ trace ( 'getCipherInfo keyLength -0: success' ) ;
220+ trace ( 'getCipherInfo ivLength -0: start' ) ;
156221 assert . strictEqual (
157222 crypto . getCipherInfo ( 'aes-128-cbc' , { ivLength : - 0 } ) ,
158223 undefined ,
159224 ) ;
225+ trace ( 'getCipherInfo ivLength -0: success' ) ;
160226}
0 commit comments