|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +constcommon=require('../common'); |
| 4 | +if(!common.hasCrypto) |
| 5 | +common.skip('missing crypto'); |
| 6 | + |
| 7 | +constassert=require('assert'); |
| 8 | +constcrypto=require('crypto'); |
| 9 | +const{ hasOpenSSL }=require('../common/crypto'); |
| 10 | + |
| 11 | +functiongetOutcome(fn){ |
| 12 | +try{ |
| 13 | +return{result: fn()}; |
| 14 | +}catch(err){ |
| 15 | +return{ err }; |
| 16 | +} |
| 17 | +} |
| 18 | + |
| 19 | +functionassertSameOutcome(actual,expected){ |
| 20 | +if(expected.err!==undefined){ |
| 21 | +assert(actual.errinstanceofError); |
| 22 | +assert.strictEqual(actual.err.name,expected.err.name); |
| 23 | +assert.strictEqual(actual.err.code,expected.err.code); |
| 24 | +assert.strictEqual(actual.err.message,expected.err.message); |
| 25 | +}else{ |
| 26 | +assert.deepStrictEqual(actual.result,expected.result); |
| 27 | +} |
| 28 | +} |
| 29 | + |
| 30 | +functionassertSameErrorOrSuccess(actual,expected){ |
| 31 | +if(expected.err!==undefined){ |
| 32 | +assert(actual.errinstanceofError); |
| 33 | +assert.strictEqual(actual.err.name,expected.err.name); |
| 34 | +assert.strictEqual(actual.err.code,expected.err.code); |
| 35 | +assert.strictEqual(actual.err.message,expected.err.message); |
| 36 | +}else{ |
| 37 | +assert.strictEqual(actual.err,undefined); |
| 38 | +} |
| 39 | +} |
| 40 | + |
| 41 | +{ |
| 42 | +constexpected=getOutcome(()=> |
| 43 | +crypto.hkdfSync('sha256','key','salt','info',0), |
| 44 | +); |
| 45 | +assertSameOutcome( |
| 46 | +getOutcome(()=>crypto.hkdfSync('sha256','key','salt','info',-0)), |
| 47 | +expected, |
| 48 | +); |
| 49 | +crypto.hkdf('sha256','key','salt','info',-0, |
| 50 | +common.mustCall((err,result)=>{ |
| 51 | +assertSameOutcome({ err, result },expected); |
| 52 | +})); |
| 53 | +} |
| 54 | + |
| 55 | +{ |
| 56 | +assert.strictEqual( |
| 57 | +crypto.checkPrimeSync(Buffer.from([3]),{checks: -0}), |
| 58 | +true, |
| 59 | +); |
| 60 | +crypto.checkPrime(Buffer.from([3]),{checks: -0}, |
| 61 | +common.mustSucceed((result)=>{ |
| 62 | +assert.strictEqual(result,true); |
| 63 | +})); |
| 64 | +} |
| 65 | + |
| 66 | +{ |
| 67 | +assert.throws(()=>crypto.createDiffieHellman(-0,2),{ |
| 68 | +name: 'Error', |
| 69 | +}); |
| 70 | +} |
| 71 | + |
| 72 | +{ |
| 73 | +for(const[type,getOptions]of[ |
| 74 | +['rsa',(zero)=>({modulusLength: zero})], |
| 75 | +['rsa',(zero)=>({modulusLength: 512,publicExponent: zero})], |
| 76 | +['rsa-pss',(zero)=>({ |
| 77 | +modulusLength: 512, |
| 78 | +publicExponent: 65537, |
| 79 | +saltLength: zero, |
| 80 | +})], |
| 81 | +['dsa',(zero)=>({modulusLength: zero})], |
| 82 | +['dh',(zero)=>({primeLength: zero})], |
| 83 | +['dh',(zero)=>({primeLength: 2,generator: zero})], |
| 84 | +]){ |
| 85 | +assertSameErrorOrSuccess( |
| 86 | +getOutcome(()=>crypto.generateKeyPairSync(type,getOptions(-0))), |
| 87 | +getOutcome(()=>crypto.generateKeyPairSync(type,getOptions(0))), |
| 88 | +); |
| 89 | +} |
| 90 | + |
| 91 | +if(!hasOpenSSL(3)){ |
| 92 | +common.printSkipMessage( |
| 93 | +'Skipping DSA divisorLength 0 key generation on OpenSSL 1.1.1'); |
| 94 | +}else{ |
| 95 | +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 | +})), |
| 104 | +); |
| 105 | +} |
| 106 | + |
| 107 | +crypto.generateKeyPair('rsa',{modulusLength: -0}, |
| 108 | +common.mustCall((err)=>{ |
| 109 | +assert(errinstanceofError); |
| 110 | +})); |
| 111 | +} |
| 112 | + |
| 113 | +if(!process.features.openssl_is_boringssl){ |
| 114 | +assert.strictEqual( |
| 115 | +crypto.createHash('shake128',{outputLength: -0}).digest('hex'), |
| 116 | +'', |
| 117 | +); |
| 118 | +assert.strictEqual( |
| 119 | +crypto.createHash('shake128',{outputLength: 5}) |
| 120 | +.copy({outputLength: -0}) |
| 121 | +.digest('hex'), |
| 122 | +'', |
| 123 | +); |
| 124 | +assert.strictEqual( |
| 125 | +crypto.hash('shake128','data',{outputLength: -0}), |
| 126 | +'', |
| 127 | +); |
| 128 | +} |
| 129 | + |
| 130 | +{ |
| 131 | +constkey=Buffer.alloc(16); |
| 132 | +constiv=Buffer.alloc(12); |
| 133 | + |
| 134 | +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})), |
| 139 | +); |
| 140 | +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 | +)), |
| 151 | +); |
| 152 | +assert.strictEqual( |
| 153 | +crypto.getCipherInfo('aes-128-cbc',{keyLength: -0}), |
| 154 | +undefined, |
| 155 | +); |
| 156 | +assert.strictEqual( |
| 157 | +crypto.getCipherInfo('aes-128-cbc',{ivLength: -0}), |
| 158 | +undefined, |
| 159 | +); |
| 160 | +} |
0 commit comments