|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const { hasOpenSSL } = require('../common/crypto'); |
| 8 | + |
| 9 | +if (!hasOpenSSL(3, 5)) |
| 10 | + common.skip('requires OpenSSL >= 3.5'); |
| 11 | + |
| 12 | +const assert = require('assert'); |
| 13 | +const { |
| 14 | + randomBytes, |
| 15 | + sign, |
| 16 | + verify, |
| 17 | +} = require('crypto'); |
| 18 | + |
| 19 | +const fixtures = require('../common/fixtures'); |
| 20 | + |
| 21 | +function getKeyFileName(type, suffix) { |
| 22 | + return `${type.replaceAll('-', '_')}_${suffix}.pem`; |
| 23 | +} |
| 24 | + |
| 25 | +for (const [asymmetricKeyType, sigLen] of [ |
| 26 | + ['ml-dsa-44', 2420], ['ml-dsa-65', 3309], ['ml-dsa-87', 4627], |
| 27 | +]) { |
| 28 | + const keys = { |
| 29 | + public: fixtures.readKey(getKeyFileName(asymmetricKeyType, 'public'), 'ascii'), |
| 30 | + private: fixtures.readKey(getKeyFileName(asymmetricKeyType, 'private'), 'ascii'), |
| 31 | + private_seed_only: fixtures.readKey(getKeyFileName(asymmetricKeyType, 'private_seed_only'), 'ascii'), |
| 32 | + private_priv_only: fixtures.readKey(getKeyFileName(asymmetricKeyType, 'private_priv_only'), 'ascii'), |
| 33 | + }; |
| 34 | + |
| 35 | + for (const privateKey of [keys.private, keys.private_seed_only, keys.private_priv_only]) { |
| 36 | + for (const data of [randomBytes(0), randomBytes(1), randomBytes(32), randomBytes(128), randomBytes(1024)]) { |
| 37 | + // sync |
| 38 | + { |
| 39 | + const signature = sign(undefined, data, privateKey); |
| 40 | + assert.strictEqual(signature.byteLength, sigLen); |
| 41 | + assert.strictEqual(verify(undefined, randomBytes(32), keys.public, signature), false); |
| 42 | + assert.strictEqual(verify(undefined, data, keys.public, Buffer.alloc(sigLen)), false); |
| 43 | + assert.strictEqual(verify(undefined, data, keys.public, signature), true); |
| 44 | + assert.strictEqual(verify(undefined, data, privateKey, signature), true); |
| 45 | + assert.throws(() => sign('sha256', data, privateKey), { code: 'ERR_OSSL_INVALID_DIGEST' }); |
| 46 | + assert.throws( |
| 47 | + () => verify('sha256', data, keys.public, Buffer.alloc(sigLen)), |
| 48 | + { code: 'ERR_OSSL_INVALID_DIGEST' }); |
| 49 | + } |
| 50 | + |
| 51 | + // async |
| 52 | + { |
| 53 | + sign(undefined, data, privateKey, common.mustSucceed((signature) => { |
| 54 | + assert.strictEqual(signature.byteLength, sigLen); |
| 55 | + verify(undefined, data, privateKey, signature, common.mustSucceed((valid) => { |
| 56 | + assert.strictEqual(valid, true); |
| 57 | + })); |
| 58 | + verify(undefined, data, privateKey, Buffer.alloc(sigLen), common.mustSucceed((valid) => { |
| 59 | + assert.strictEqual(valid, false); |
| 60 | + })); |
| 61 | + })); |
| 62 | + |
| 63 | + sign('sha256', data, privateKey, common.expectsError(/invalid digest/)); |
| 64 | + verify('sha256', data, keys.public, Buffer.alloc(sigLen), common.expectsError(/invalid digest/)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments