|
1 | 1 | var assert = require('assert'); |
2 | 2 | var crypto = require('crypto'); |
| 3 | +var sinon = require('sinon'); |
3 | 4 | var oaep = require('../lib/oaep'); |
4 | 5 |
|
5 | 6 | // Throwaway 2048-bit key + ciphertext from the ESD-63620 repro. Produced by: |
@@ -148,6 +149,39 @@ describe('oaep', function () { |
148 | 149 | }); |
149 | 150 | }); |
150 | 151 |
|
| 152 | + describe('FIPS mode gate', function () { |
| 153 | + var fs = require('fs'); |
| 154 | + var pub = fs.readFileSync(__dirname + '/test-auth0_rsa.pub'); |
| 155 | + var key = fs.readFileSync(__dirname + '/test-auth0.key'); |
| 156 | + |
| 157 | + afterEach(function () { |
| 158 | + sinon.restore(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('refuses publicEncryptOaep when FIPS mode is enabled', function () { |
| 162 | + sinon.stub(crypto, 'getFips').returns(1); |
| 163 | + assert.throws(function () { |
| 164 | + oaep.publicEncryptOaep(pub, Buffer.from('test'), { oaepHash: 'sha256', mgf1Hash: 'sha1' }); |
| 165 | + }, function (e) { return e.code === 'ERR_XMLENC_FIPS_UNSUPPORTED'; }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('refuses privateDecryptOaep when FIPS mode is enabled', function () { |
| 169 | + // Encrypt outside FIPS, then assert decrypt is blocked under FIPS. |
| 170 | + var ct = oaep.publicEncryptOaep(pub, Buffer.from('test'), { oaepHash: 'sha256', mgf1Hash: 'sha1' }); |
| 171 | + sinon.stub(crypto, 'getFips').returns(1); |
| 172 | + assert.throws(function () { |
| 173 | + oaep.privateDecryptOaep(key, ct, { oaepHash: 'sha256', mgf1Hash: 'sha1' }); |
| 174 | + }, function (e) { return e.code === 'ERR_XMLENC_FIPS_UNSUPPORTED'; }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('runs normally when FIPS mode is disabled', function () { |
| 178 | + sinon.stub(crypto, 'getFips').returns(0); |
| 179 | + var ct = oaep.publicEncryptOaep(pub, Buffer.from('test'), { oaepHash: 'sha256', mgf1Hash: 'sha1' }); |
| 180 | + var pt = oaep.privateDecryptOaep(key, ct, { oaepHash: 'sha256', mgf1Hash: 'sha1' }); |
| 181 | + assert.equal(pt.toString(), 'test'); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
151 | 185 | describe('mgf1', function () { |
152 | 186 | it('matches the RFC 8017 counter construction for one block', function () { |
153 | 187 | var seed = Buffer.from('abc'); |
|
0 commit comments