1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8const {
9  constants,
10  generateKeyPair,
11} = require('crypto');
12const {
13  testEncryptDecrypt,
14  testSignVerify,
15} = require('../common/crypto');
16
17// Test RSA-PSS.
18{
19  generateKeyPair('rsa-pss', {
20    modulusLength: 512,
21    saltLength: 16,
22    hashAlgorithm: 'sha256',
23    mgf1HashAlgorithm: 'sha256'
24  }, common.mustSucceed((publicKey, privateKey) => {
25    assert.strictEqual(publicKey.type, 'public');
26    assert.strictEqual(publicKey.asymmetricKeyType, 'rsa-pss');
27    assert.deepStrictEqual(publicKey.asymmetricKeyDetails, {
28      modulusLength: 512,
29      publicExponent: 65537n,
30      hashAlgorithm: 'sha256',
31      mgf1HashAlgorithm: 'sha256',
32      saltLength: 16
33    });
34
35    assert.strictEqual(privateKey.type, 'private');
36    assert.strictEqual(privateKey.asymmetricKeyType, 'rsa-pss');
37    assert.deepStrictEqual(privateKey.asymmetricKeyDetails, {
38      modulusLength: 512,
39      publicExponent: 65537n,
40      hashAlgorithm: 'sha256',
41      mgf1HashAlgorithm: 'sha256',
42      saltLength: 16
43    });
44
45    // Unlike RSA, RSA-PSS does not allow encryption.
46    assert.throws(() => {
47      testEncryptDecrypt(publicKey, privateKey);
48    }, /operation not supported for this keytype/);
49
50    // RSA-PSS also does not permit signing with PKCS1 padding.
51    assert.throws(() => {
52      testSignVerify({
53        key: publicKey,
54        padding: constants.RSA_PKCS1_PADDING
55      }, {
56        key: privateKey,
57        padding: constants.RSA_PKCS1_PADDING
58      });
59    }, /illegal or unsupported padding mode/);
60
61    // The padding should correctly default to RSA_PKCS1_PSS_PADDING now.
62    testSignVerify(publicKey, privateKey);
63  }));
64}
65