|
| 1 | +'use strict'; |
| 2 | +constcommon=require('../common'); |
| 3 | +if(!common.hasCrypto) |
| 4 | +common.skip('missing crypto'); |
| 5 | + |
| 6 | +// Tests the `mgf1Hash` option of crypto.publicEncrypt() and |
| 7 | +// crypto.privateDecrypt(), which allows the MGF1 digest of RSA-OAEP padding to |
| 8 | +// differ from the OAEP message digest (`oaepHash`). This is required for |
| 9 | +// interoperability with profiles such as XML Encryption's `rsa-oaep-mgf1p`, |
| 10 | +// where the OAEP digest may be changed but MGF1 is fixed to SHA-1. |
| 11 | + |
| 12 | +constassert=require('assert'); |
| 13 | +constcrypto=require('crypto'); |
| 14 | +constfixtures=require('../common/fixtures'); |
| 15 | +const{ hasFIPS }=require('../common/crypto'); |
| 16 | + |
| 17 | +constconstants=crypto.constants; |
| 18 | + |
| 19 | +constpublicKey=fixtures.readKey('rsa_public.pem','ascii'); |
| 20 | +constprivateKey=fixtures.readKey('rsa_private.pem','ascii'); |
| 21 | + |
| 22 | +constinput=Buffer.from('the quick brown fox jumps over the lazy dog'); |
| 23 | + |
| 24 | +// A round-trip with mismatched OAEP and MGF1 digests must succeed when both |
| 25 | +// sides agree on the digests. |
| 26 | +{ |
| 27 | +constencrypted=crypto.publicEncrypt({ |
| 28 | +key: publicKey, |
| 29 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 30 | +oaepHash: 'sha256', |
| 31 | +mgf1Hash: 'sha1', |
| 32 | +},input); |
| 33 | + |
| 34 | +constdecrypted=crypto.privateDecrypt({ |
| 35 | +key: privateKey, |
| 36 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 37 | +oaepHash: 'sha256', |
| 38 | +mgf1Hash: 'sha1', |
| 39 | +},encrypted); |
| 40 | + |
| 41 | +assert.deepStrictEqual(decrypted,input); |
| 42 | +} |
| 43 | + |
| 44 | +// mgf1Hash actually affects the padding: a ciphertext produced with |
| 45 | +// oaepHash=sha256 and mgf1Hash=sha1 must NOT decrypt when MGF1 defaults to the |
| 46 | +// OAEP digest (sha256), which is the pre-existing behavior. |
| 47 | +{ |
| 48 | +constencrypted=crypto.publicEncrypt({ |
| 49 | +key: publicKey, |
| 50 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 51 | +oaepHash: 'sha256', |
| 52 | +mgf1Hash: 'sha1', |
| 53 | +},input); |
| 54 | + |
| 55 | +assert.throws(()=>{ |
| 56 | +crypto.privateDecrypt({ |
| 57 | +key: privateKey, |
| 58 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 59 | +oaepHash: 'sha256', |
| 60 | +// No mgf1Hash: MGF1 follows oaepHash (sha256) and must fail to unpad. |
| 61 | +},encrypted); |
| 62 | +},{ |
| 63 | +code: hasFIPS(3,5) ? 'ERR_OSSL_EVP_PROVIDER_ASYM_CIPHER_FAILURE' : |
| 64 | +'ERR_OSSL_RSA_OAEP_DECODING_ERROR' |
| 65 | +}); |
| 66 | +} |
| 67 | + |
| 68 | +// Backward compatibility: omitting mgf1Hash on both sides keeps MGF1 == oaepHash |
| 69 | +// (the historical behavior), so this round-trips, and setting mgf1Hash equal to |
| 70 | +// oaepHash is equivalent to omitting it. |
| 71 | +{ |
| 72 | +constencrypted=crypto.publicEncrypt({ |
| 73 | +key: publicKey, |
| 74 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 75 | +oaepHash: 'sha256', |
| 76 | +},input); |
| 77 | + |
| 78 | +constdecrypted=crypto.privateDecrypt({ |
| 79 | +key: privateKey, |
| 80 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 81 | +oaepHash: 'sha256', |
| 82 | +mgf1Hash: 'sha256', |
| 83 | +},encrypted); |
| 84 | + |
| 85 | +assert.deepStrictEqual(decrypted,input); |
| 86 | +} |
| 87 | + |
| 88 | +// The default oaepHash is sha1, so mgf1Hash defaults to sha1 as well. A |
| 89 | +// ciphertext encrypted with all defaults must decrypt with an explicit |
| 90 | +// mgf1Hash: 'sha1'. |
| 91 | +{ |
| 92 | +constencrypted=crypto.publicEncrypt({ |
| 93 | +key: publicKey, |
| 94 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 95 | +},input); |
| 96 | + |
| 97 | +constdecrypted=crypto.privateDecrypt({ |
| 98 | +key: privateKey, |
| 99 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 100 | +mgf1Hash: 'sha1', |
| 101 | +},encrypted); |
| 102 | + |
| 103 | +assert.deepStrictEqual(decrypted,input); |
| 104 | +} |
| 105 | + |
| 106 | +// A few other digest combinations round-trip. |
| 107 | +for(const[oaepHash,mgf1Hash]of[ |
| 108 | +['sha512','sha1'], |
| 109 | +['sha384','sha256'], |
| 110 | +['sha1','sha256'], |
| 111 | +]){ |
| 112 | +constencrypted=crypto.publicEncrypt({ |
| 113 | +key: publicKey, |
| 114 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 115 | + oaepHash, |
| 116 | + mgf1Hash, |
| 117 | +},input); |
| 118 | + |
| 119 | +constdecrypted=crypto.privateDecrypt({ |
| 120 | +key: privateKey, |
| 121 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 122 | + oaepHash, |
| 123 | + mgf1Hash, |
| 124 | +},encrypted); |
| 125 | + |
| 126 | +assert.deepStrictEqual(decrypted,input); |
| 127 | +} |
| 128 | + |
| 129 | +// mgf1Hash must be a string. |
| 130 | +for(constmgf1Hashof[1,true,{},[],null]){ |
| 131 | +assert.throws(()=>{ |
| 132 | +crypto.publicEncrypt({ |
| 133 | +key: publicKey, |
| 134 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 135 | +oaepHash: 'sha256', |
| 136 | + mgf1Hash, |
| 137 | +},input); |
| 138 | +},{code: 'ERR_INVALID_ARG_TYPE'}); |
| 139 | +} |
| 140 | + |
| 141 | +// An unknown mgf1Hash digest name is rejected. |
| 142 | +assert.throws(()=>{ |
| 143 | +crypto.publicEncrypt({ |
| 144 | +key: publicKey, |
| 145 | +padding: constants.RSA_PKCS1_OAEP_PADDING, |
| 146 | +oaepHash: 'sha256', |
| 147 | +mgf1Hash: 'not-a-real-digest', |
| 148 | +},input); |
| 149 | +},{code: 'ERR_OSSL_EVP_INVALID_DIGEST'}); |
0 commit comments