Skip to content

Commit 95d3e32

Browse files
committed
fixup! crypto: add TurboSHAKE and KangarooTwelve Web Cryptography algorithms
1 parent 4408757 commit 95d3e32

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

lib/internal/crypto/webidl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ converters.KangarooTwelveParams = createDictionaryConverter(
903903
converter: (V, opts) =>
904904
converters['unsigned long'](V, { ...opts, enforceRange: true }),
905905
validator: (V, opts) => {
906-
if (V % 8)
907-
throw lazyDOMException('Unsupported KangarooTwelveParams outputLength', 'NotSupportedError');
906+
if (V === 0 || V % 8)
907+
throw lazyDOMException('Invalid KangarooTwelveParams outputLength', 'OperationError');
908908
},
909909
required: true,
910910
},
@@ -922,8 +922,8 @@ converters.TurboShakeParams = createDictionaryConverter(
922922
converter: (V, opts) =>
923923
converters['unsigned long'](V, { ...opts, enforceRange: true }),
924924
validator: (V, opts) => {
925-
if (V % 8)
926-
throw lazyDOMException('Unsupported TurboShakeParams outputLength', 'NotSupportedError');
925+
if (V === 0 || V % 8)
926+
throw lazyDOMException('Invalid TurboShakeParams outputLength', 'OperationError');
927927
},
928928
required: true,
929929
},

src/crypto/crypto_turboshake.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ Maybe<void> TurboShakeTraits::AdditionalConfig(
449449
CHECK(args[offset + 1]->IsUint32());
450450
params->domain_separation =
451451
static_cast<uint8_t>(args[offset + 1].As<Uint32>()->Value());
452+
CHECK_GE(params->domain_separation, 0x01);
453+
CHECK_LE(params->domain_separation, 0x7F);
452454

453455
// args[offset + 2] = output length in bytes (uint32)
454456
CHECK(args[offset + 2]->IsUint32());
@@ -469,11 +471,7 @@ bool TurboShakeTraits::DeriveBits(Environment* env,
469471
const TurboShakeConfig& params,
470472
ByteSource* out,
471473
CryptoJobMode mode) {
472-
if (params.output_length == 0) {
473-
*out = ByteSource();
474-
return true;
475-
}
476-
474+
CHECK_GT(params.output_length, 0);
477475
char* buf = MallocOpenSSL<char>(params.output_length);
478476

479477
const uint8_t* input = reinterpret_cast<const uint8_t*>(params.data.data());
@@ -585,11 +583,7 @@ bool KangarooTwelveTraits::DeriveBits(Environment* env,
585583
const KangarooTwelveConfig& params,
586584
ByteSource* out,
587585
CryptoJobMode mode) {
588-
if (params.output_length == 0) {
589-
*out = ByteSource();
590-
return true;
591-
}
592-
586+
CHECK_GT(params.output_length, 0);
593587
char* buf = MallocOpenSSL<char>(params.output_length);
594588

595589
const uint8_t* input = reinterpret_cast<const uint8_t*>(params.data.data());

test/fixtures/webcrypto/supports-modern-algorithms.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ export const vectors = {
3131
[false, 'TurboSHAKE128'],
3232
[true, { name: 'TurboSHAKE128', outputLength: 128 }],
3333
[true, { name: 'TurboSHAKE128', outputLength: 128, domainSeparation: 0x07 }],
34+
[false, { name: 'TurboSHAKE128', outputLength: 0 }],
3435
[false, { name: 'TurboSHAKE128', outputLength: 127 }],
3536
[false, { name: 'TurboSHAKE128', outputLength: 128, domainSeparation: 0x00 }],
3637
[false, { name: 'TurboSHAKE128', outputLength: 128, domainSeparation: 0x80 }],
3738
[false, 'TurboSHAKE256'],
3839
[true, { name: 'TurboSHAKE256', outputLength: 256 }],
3940
[true, { name: 'TurboSHAKE256', outputLength: 256, domainSeparation: 0x07 }],
41+
[false, { name: 'TurboSHAKE256', outputLength: 0 }],
4042
[false, { name: 'TurboSHAKE256', outputLength: 255 }],
4143
[false, { name: 'TurboSHAKE256', outputLength: 256, domainSeparation: 0x00 }],
4244
[false, { name: 'TurboSHAKE256', outputLength: 256, domainSeparation: 0x80 }],
4345
[false, 'KT128'],
4446
[true, { name: 'KT128', outputLength: 128 }],
4547
[true, { name: 'KT128', outputLength: 128, customization: Buffer.alloc(0) }],
48+
[false, { name: 'KT128', outputLength: 0 }],
4649
[false, { name: 'KT128', outputLength: 127 }],
4750
[false, 'KT256'],
4851
[true, { name: 'KT256', outputLength: 256 }],
4952
[true, { name: 'KT256', outputLength: 256, customization: Buffer.alloc(0) }],
53+
[false, { name: 'KT256', outputLength: 0 }],
5054
[false, { name: 'KT256', outputLength: 255 }],
5155
],
5256
'sign': [

test/parallel/test-webcrypto-digest-turboshake.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,37 @@ async function testDigest(size, alg) {
9898
await Promise.all(variations);
9999
})().then(common.mustCall());
100100

101-
// Edge cases: zero-length output
101+
// Edge cases: zero-length output rejects
102102
(async () => {
103-
assert.deepStrictEqual(
104-
new Uint8Array(await subtle.digest(
105-
{ name: 'TurboSHAKE128', outputLength: 0 },
106-
Buffer.alloc(1))),
107-
new Uint8Array(0),
108-
);
109-
110-
assert.deepStrictEqual(
111-
new Uint8Array(await subtle.digest(
112-
{ name: 'KT128', outputLength: 0 },
113-
Buffer.alloc(1))),
114-
new Uint8Array(0),
115-
);
103+
await assert.rejects(
104+
subtle.digest({ name: 'TurboSHAKE128', outputLength: 0 }, Buffer.alloc(1)),
105+
{
106+
name: 'OperationError',
107+
message: 'Invalid TurboShakeParams outputLength',
108+
});
109+
110+
await assert.rejects(
111+
subtle.digest({ name: 'KT128', outputLength: 0 }, Buffer.alloc(1)),
112+
{
113+
name: 'OperationError',
114+
message: 'Invalid KangarooTwelveParams outputLength',
115+
});
116116
})().then(common.mustCall());
117117

118118
// Edge case: non-byte-aligned outputLength rejects
119119
(async () => {
120120
await assert.rejects(
121121
subtle.digest({ name: 'TurboSHAKE128', outputLength: 7 }, Buffer.alloc(1)),
122122
{
123-
name: 'NotSupportedError',
124-
message: 'Unsupported TurboShakeParams outputLength',
123+
name: 'OperationError',
124+
message: 'Invalid TurboShakeParams outputLength',
125125
});
126126

127127
await assert.rejects(
128128
subtle.digest({ name: 'KT128', outputLength: 7 }, Buffer.alloc(1)),
129129
{
130-
name: 'NotSupportedError',
131-
message: 'Unsupported KangarooTwelveParams outputLength',
130+
name: 'OperationError',
131+
message: 'Invalid KangarooTwelveParams outputLength',
132132
});
133133
})().then(common.mustCall());
134134

0 commit comments

Comments
 (0)