GeezerVison is showing github.com — crypto: verify empty KMAC outputs · nodejs/node@54f9426
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_kmac.cc +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -191,9 +191,10 @@ MaybeLocal<Value> KmacTraits::EncodeOutput(Environment* env, | 191 | 191 | case SignConfiguration::Mode::Verify: | 192 | 192 | return Boolean::New( | 193 | 193 | env->isolate(), | 194 | | - out->size() > 0 && out->size() == params.signature.size() && | 195 | | - CRYPTO_memcmp( | 196 | | - out->data(), params.signature.data(), out->size()) == 0); | | 194 | + out->size() == params.signature.size() && | | 195 | + (out->size() == 0 || | | 196 | + CRYPTO_memcmp( | | 197 | + out->data(), params.signature.data(), out->size()) == 0)); | 197 | 198 | } | 198 | 199 | UNREACHABLE(); | 199 | 200 | } | |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | + | | 5 | +if (!common.hasCrypto) | | 6 | + common.skip('missing crypto'); | | 7 | + | | 8 | +const { hasFIPS, hasOpenSSL } = require('../common/crypto'); | | 9 | + | | 10 | +if (!hasOpenSSL(3)) | | 11 | + common.skip('requires OpenSSL >= 3'); | | 12 | +if (hasFIPS()) | | 13 | + common.skip('empty KMAC output is not supported in FIPS mode'); | | 14 | + | | 15 | +const assert = require('assert'); | | 16 | +const { subtle } = globalThis.crypto; | | 17 | + | | 18 | +(async () => { | | 19 | + const data = new Uint8Array([1, 2, 3]); | | 20 | + | | 21 | + for (const name of ['KMAC128', 'KMAC256']) { | | 22 | + const key = await subtle.importKey( | | 23 | + 'raw-secret', new Uint8Array(32), name, false, ['sign', 'verify']); | | 24 | + const otherKey = await subtle.importKey( | | 25 | + 'raw-secret', new Uint8Array(32).fill(1), name, false, ['verify']); | | 26 | + const algorithm = { name, outputLength: 0 }; | | 27 | + const signature = await subtle.sign(algorithm, key, data); | | 28 | + | | 29 | + assert.strictEqual(signature.byteLength, 0); | | 30 | + assert.strictEqual(await subtle.verify(algorithm, key, signature, data), true); | | 31 | + assert.strictEqual(await subtle.verify(algorithm, key, new Uint8Array([0]), data), false); | | 32 | + assert.strictEqual(await subtle.verify( | | 33 | + { name, outputLength: 256 }, key, signature, data), false); | | 34 | + | | 35 | + // Empty MACs compare equal even when the inputs differ. | | 36 | + assert.strictEqual(await subtle.verify( | | 37 | + algorithm, key, signature, new Uint8Array([4, 5, 6])), true); | | 38 | + assert.strictEqual(await subtle.verify(algorithm, otherKey, signature, data), true); | | 39 | + assert.strictEqual(await subtle.verify( | | 40 | + { ...algorithm, customization: new Uint8Array([1]) }, key, signature, data), true); | | 41 | + } | | 42 | +})().then(common.mustCall()); |
|