deno_crypto 0.271.0

Web Cryptography API implementation for Deno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Copyright 2018-2026 the Deno authors. MIT license.

//! `SubtleCrypto.digest()` — algorithm parsing, BufferSource coercion and
//! the dispatch onto the `aws_lc_rs` / `sha3` backends. All of the logic
//! that used to live in the `digest(algorithm, data)` method body in
//! `ext/crypto/00_crypto.js` is now here.

use std::borrow::Cow;

use aws_lc_rs::digest;
use deno_core::v8;
use deno_core::webidl::ContextFn;
use deno_core::webidl::WebIdlConverter;
use deno_core::webidl::WebIdlError;
use deno_core::webidl::WebIdlErrorKind;
use deno_error::JsErrorBox;
use serde::Deserialize;

use crate::CryptoError;
use crate::key::CryptoHash;

/// Parameter dictionary for the SHA-3-family extendable-output digest
/// algorithms (cSHAKE / TurboSHAKE) used by [`DigestAlgorithm::Xof`].
/// Carries the `outputLength`-and-optional-trim parameters validated in
/// [`run_xof`] rather than at converter time so the eventual error is a
/// `DOMExceptionOperationError`, not a `WebIdlError`.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", tag = "name")]
pub enum SubtleDigestXof {
  #[serde(rename = "cSHAKE128", rename_all = "camelCase")]
  CShake128 {
    output_length: u32,
    #[serde(with = "serde_bytes", default)]
    function_name: Option<Vec<u8>>,
    #[serde(with = "serde_bytes", default)]
    customization: Option<Vec<u8>>,
  },
  #[serde(rename = "cSHAKE256", rename_all = "camelCase")]
  CShake256 {
    output_length: u32,
    #[serde(with = "serde_bytes", default)]
    function_name: Option<Vec<u8>>,
    #[serde(with = "serde_bytes", default)]
    customization: Option<Vec<u8>>,
  },
  #[serde(rename = "TurboSHAKE128", rename_all = "camelCase")]
  TurboShake128 {
    output_length: u32,
    domain_separation: Option<u8>,
  },
  #[serde(rename = "TurboSHAKE256", rename_all = "camelCase")]
  TurboShake256 {
    output_length: u32,
    domain_separation: Option<u8>,
  },
  #[serde(rename = "KT128", rename_all = "camelCase")]
  Kt128 {
    output_length: u32,
    #[serde(with = "serde_bytes", default)]
    customization: Option<Vec<u8>>,
  },
  #[serde(rename = "KT256", rename_all = "camelCase")]
  Kt256 {
    output_length: u32,
    #[serde(with = "serde_bytes", default)]
    customization: Option<Vec<u8>>,
  },
  #[serde(rename = "KangarooTwelve", rename_all = "camelCase")]
  KangarooTwelve {
    output_length: u32,
    #[serde(with = "serde_bytes", default)]
    customization: Option<Vec<u8>>,
  },
}

/// The `WebIdlConverter` for `AlgorithmIdentifier`-restricted-to-digest,
/// canonicalized into the variant the dispatch code needs. Mirrors what
/// `normalizeAlgorithm(algorithm, "digest")` produced in JS.
///
/// Unrecognized algorithm names are kept as [`DigestAlgorithm::Unknown`] so
/// the dispatch in [`run`] can throw the WebCrypto-spec-mandated
/// `NotSupportedError` `DOMException` (not the WebIDL `TypeError` that a
/// converter-level error would produce). The WPT `digest.https.any.html`
/// "AES-GCM/RSA-OAEP/PBKDF2/AES-KW with empty/short/medium/long" subtests
/// hardcode that error name.
pub enum DigestAlgorithm {
  Sha(CryptoHash),
  /// cSHAKE / TurboSHAKE — variable-length output with extra dictionary
  /// parameters. The validation that the JS body performed against the
  /// raw dictionary (multiple-of-8 outputLength, non-zero TurboSHAKE
  /// outputLength, domainSeparation range) is deferred until [`run`] so
  /// the `WebIdlError` path stays clean.
  Xof(SubtleDigestXof),
  /// Algorithm identifier whose `name` resolved (it was a string or had a
  /// `.name` DOMString) but isn't in the digest registry. Holds the input
  /// spelling so the eventual `NotSupportedError` carries it.
  Unknown(String),
}

impl<'a> WebIdlConverter<'a> for DigestAlgorithm {
  type Options = ();

  fn convert<'b>(
    scope: &mut v8::PinScope<'a, '_>,
    value: v8::Local<'a, v8::Value>,
    prefix: Cow<'static, str>,
    context: ContextFn<'b>,
    _options: &Self::Options,
  ) -> Result<Self, WebIdlError> {
    // 1. Resolve the AlgorithmIdentifier union: a `DOMString` is treated
    //    as `{ name: <string> }`; otherwise the input must be an object
    //    carrying a `.name` field. The original dictionary (if any) is
    //    kept around so the XOF arms can pluck `outputLength`,
    //    `functionName`, `customization` and `domainSeparation` off it.
    //
    //    A missing `name` member -> `TypeError` (WebIDL required-member
    //    semantics), exercised by the WPT `digest({}, ...)` "empty
    //    algorithm object" subtest.
    let (name_str, maybe_obj) =
      extract_name_and_obj(scope, value, prefix.clone(), context.borrowed())?;

    // 2. Canonical (case-insensitive) name lookup -- the WebCrypto
    //    registry treats algorithm identifiers as case-insensitive but
    //    every other call site relies on the canonical spelling. If the
    //    name isn't in the digest registry, defer the `NotSupportedError`
    //    to [`run`] so the right error class is thrown.
    let Some(canonical) = canonical_digest_name(&name_str) else {
      return Ok(Self::Unknown(name_str));
    };

    // 3. Build the dispatch variant. SHA / SHA3 do not consult the dict;
    //    XOF variants pull their extra parameters out.
    match canonical {
      "SHA-1" => Ok(Self::Sha(CryptoHash::Sha1)),
      "SHA-256" => Ok(Self::Sha(CryptoHash::Sha256)),
      "SHA-384" => Ok(Self::Sha(CryptoHash::Sha384)),
      "SHA-512" => Ok(Self::Sha(CryptoHash::Sha512)),
      "SHA3-256" => Ok(Self::Sha(CryptoHash::Sha3_256)),
      "SHA3-384" => Ok(Self::Sha(CryptoHash::Sha3_384)),
      "SHA3-512" => Ok(Self::Sha(CryptoHash::Sha3_512)),
      "cSHAKE128" | "cSHAKE256" | "TurboSHAKE128" | "TurboSHAKE256"
      | "KT128" | "KT256" | "KangarooTwelve" => {
        let obj = maybe_obj.ok_or_else(|| {
          WebIdlError::other(
            prefix.clone(),
            context.borrowed(),
            JsErrorBox::type_error(format!(
              "'{canonical}' requires a parameter dictionary"
            )),
          )
        })?;
        let xof = parse_xof_dict(
          scope,
          obj,
          canonical,
          prefix.clone(),
          context.borrowed(),
        )?;
        Ok(Self::Xof(xof))
      }
      _ => unreachable!("canonical_digest_name returned an unknown variant"),
    }
  }
}

fn canonical_digest_name(name: &str) -> Option<&'static str> {
  const NAMES: &[&str] = &[
    "SHA-1",
    "SHA-256",
    "SHA-384",
    "SHA-512",
    "SHA3-256",
    "SHA3-384",
    "SHA3-512",
    "cSHAKE128",
    "cSHAKE256",
    "TurboSHAKE128",
    "TurboSHAKE256",
    "KT128",
    "KT256",
    "KangarooTwelve",
  ];
  NAMES
    .iter()
    .copied()
    .find(|canon| canon.eq_ignore_ascii_case(name))
}

fn extract_name_and_obj<'a, 'b>(
  scope: &mut v8::PinScope<'a, '_>,
  value: v8::Local<'a, v8::Value>,
  prefix: Cow<'static, str>,
  context: ContextFn<'b>,
) -> Result<(String, Option<v8::Local<'a, v8::Object>>), WebIdlError> {
  if value.is_string() {
    let s = value.to_rust_string_lossy(scope);
    return Ok((s, None));
  }
  if let Ok(obj) = v8::Local::<v8::Object>::try_from(value) {
    let name_key = v8_str(scope, "name");
    let name_val = obj
      .get(scope, name_key.into())
      .unwrap_or_else(|| v8::undefined(scope).into());
    // WebIDL `Algorithm` dictionary requires `name` -- WPT
    // `digest({}, ...)` "empty algorithm object" checks the resulting
    // error name is `TypeError`, not `NotSupportedError`.
    if name_val.is_undefined() {
      return Err(WebIdlError::other(
        prefix,
        context,
        JsErrorBox::type_error("required member 'name' is undefined"),
      ));
    }
    let s = name_val
      .to_string(scope)
      .ok_or_else(|| {
        WebIdlError::other(
          prefix.clone(),
          context.borrowed(),
          JsErrorBox::type_error(
            "algorithm.name is not convertible to DOMString",
          ),
        )
      })?
      .to_rust_string_lossy(scope);
    return Ok((s, Some(obj)));
  }
  Err(WebIdlError::new(
    prefix,
    context,
    WebIdlErrorKind::ConvertToConverterType("AlgorithmIdentifier"),
  ))
}

fn parse_xof_dict<'a, 'b>(
  scope: &mut v8::PinScope<'a, '_>,
  obj: v8::Local<'a, v8::Object>,
  canonical: &'static str,
  prefix: Cow<'static, str>,
  context: ContextFn<'b>,
) -> Result<SubtleDigestXof, WebIdlError> {
  let output_length =
    read_required_u32(scope, obj, "outputLength", prefix.clone(), &context)?;
  match canonical {
    "cSHAKE128" => Ok(SubtleDigestXof::CShake128 {
      output_length,
      function_name: read_optional_buffer(
        scope,
        obj,
        "functionName",
        prefix.clone(),
        &context,
      )?,
      customization: read_optional_buffer(
        scope,
        obj,
        "customization",
        prefix.clone(),
        &context,
      )?,
    }),
    "cSHAKE256" => Ok(SubtleDigestXof::CShake256 {
      output_length,
      function_name: read_optional_buffer(
        scope,
        obj,
        "functionName",
        prefix.clone(),
        &context,
      )?,
      customization: read_optional_buffer(
        scope,
        obj,
        "customization",
        prefix.clone(),
        &context,
      )?,
    }),
    "TurboSHAKE128" => Ok(SubtleDigestXof::TurboShake128 {
      output_length,
      domain_separation: read_optional_u8(
        scope,
        obj,
        "domainSeparation",
        prefix.clone(),
        &context,
      )?,
    }),
    "TurboSHAKE256" => Ok(SubtleDigestXof::TurboShake256 {
      output_length,
      domain_separation: read_optional_u8(
        scope,
        obj,
        "domainSeparation",
        prefix.clone(),
        &context,
      )?,
    }),
    "KT128" => Ok(SubtleDigestXof::Kt128 {
      output_length,
      customization: read_optional_buffer(
        scope,
        obj,
        "customization",
        prefix.clone(),
        &context,
      )?,
    }),
    "KT256" => Ok(SubtleDigestXof::Kt256 {
      output_length,
      customization: read_optional_buffer(
        scope,
        obj,
        "customization",
        prefix.clone(),
        &context,
      )?,
    }),
    "KangarooTwelve" => Ok(SubtleDigestXof::KangarooTwelve {
      output_length,
      customization: read_optional_buffer(
        scope,
        obj,
        "customization",
        prefix.clone(),
        &context,
      )?,
    }),
    _ => unreachable!(),
  }
}

fn read_required_u32<'a, 'b>(
  scope: &mut v8::PinScope<'a, '_>,
  obj: v8::Local<'a, v8::Object>,
  key: &'static str,
  prefix: Cow<'static, str>,
  context: &ContextFn<'b>,
) -> Result<u32, WebIdlError> {
  let key_v8 = v8_str(scope, key);
  let val = obj
    .get(scope, key_v8.into())
    .unwrap_or_else(|| v8::undefined(scope).into());
  if val.is_undefined() {
    return Err(WebIdlError::other(
      prefix,
      context.borrowed(),
      JsErrorBox::type_error(format!("required dictionary member '{key}'")),
    ));
  }
  val.uint32_value(scope).ok_or_else(|| {
    WebIdlError::other(
      prefix,
      context.borrowed(),
      JsErrorBox::type_error(format!("'{key}' must be convertible to u32")),
    )
  })
}

fn read_optional_u8<'a, 'b>(
  scope: &mut v8::PinScope<'a, '_>,
  obj: v8::Local<'a, v8::Object>,
  key: &'static str,
  prefix: Cow<'static, str>,
  context: &ContextFn<'b>,
) -> Result<Option<u8>, WebIdlError> {
  let key_v8 = v8_str(scope, key);
  let val = obj
    .get(scope, key_v8.into())
    .unwrap_or_else(|| v8::undefined(scope).into());
  if val.is_undefined() || val.is_null() {
    return Ok(None);
  }
  // Read the full u32 and reject values outside `[0, 0xFF]` so a stray
  // `0x101` does not wrap to `0x01` and slip past the caller's
  // [1, 0x7F] domain-separation range check (TurboSHAKE edge).
  let u = val.uint32_value(scope).unwrap_or(0);
  if u > u8::MAX as u32 {
    return Err(WebIdlError::other(
      prefix,
      context.borrowed(),
      JsErrorBox::type_error(format!("'{key}' must be in the range [0, 0xFF]")),
    ));
  }
  Ok(Some(u as u8))
}

fn read_optional_buffer<'a, 'b>(
  scope: &mut v8::PinScope<'a, '_>,
  obj: v8::Local<'a, v8::Object>,
  key: &'static str,
  prefix: Cow<'static, str>,
  context: &ContextFn<'b>,
) -> Result<Option<Vec<u8>>, WebIdlError> {
  let key_v8 = v8_str(scope, key);
  let val = obj
    .get(scope, key_v8.into())
    .unwrap_or_else(|| v8::undefined(scope).into());
  if val.is_undefined() || val.is_null() {
    return Ok(None);
  }
  // SAB and SAB-backed views are rejected to match the JS
  // `webidl.converters.BufferSource` contract (cSHAKE
  // `functionName`/`customization`), and non-BufferSource values raise
  // `TypeError` instead of silently materializing as an empty `Vec`.
  if let Ok(view) = v8::Local::<v8::ArrayBufferView>::try_from(val) {
    if let Some(ab) = view.buffer(scope) {
      let ab_val: v8::Local<v8::Value> = ab.into();
      if ab_val.is_shared_array_buffer() {
        return Err(WebIdlError::other(
          prefix,
          context.borrowed(),
          JsErrorBox::type_error(format!(
            "'{key}' is a view on a SharedArrayBuffer, which is not allowed"
          )),
        ));
      }
    }
    return Ok(Some(value_to_byte_vec(scope, val)));
  }
  if val.is_shared_array_buffer() {
    return Err(WebIdlError::other(
      prefix,
      context.borrowed(),
      JsErrorBox::type_error(format!(
        "'{key}' is a SharedArrayBuffer, which is not allowed"
      )),
    ));
  }
  if v8::Local::<v8::ArrayBuffer>::try_from(val).is_ok() {
    return Ok(Some(value_to_byte_vec(scope, val)));
  }
  Err(WebIdlError::other(
    prefix,
    context.borrowed(),
    JsErrorBox::type_error(format!("'{key}' is not a BufferSource")),
  ))
}

fn value_to_byte_vec<'a>(
  scope: &mut v8::PinScope<'a, '_>,
  value: v8::Local<'a, v8::Value>,
) -> Vec<u8> {
  if let Ok(view) = v8::Local::<v8::ArrayBufferView>::try_from(value) {
    let byte_offset = view.byte_offset();
    let byte_length = view.byte_length();
    if byte_length == 0 {
      return Vec::new();
    }
    let ab = view.buffer(scope).unwrap();
    // SAFETY: V8 guarantees byte_offset + byte_length is within the
    // backing store, and a non-detached buffer has a non-null data ptr.
    unsafe {
      let base = ab.data().unwrap().as_ptr() as *const u8;
      std::slice::from_raw_parts(base.add(byte_offset), byte_length).to_vec()
    }
  } else if let Ok(ab) = v8::Local::<v8::ArrayBuffer>::try_from(value) {
    let byte_length = ab.byte_length();
    if byte_length == 0 {
      return Vec::new();
    }
    // SAFETY: as above.
    unsafe {
      let base = ab.data().unwrap().as_ptr() as *const u8;
      std::slice::from_raw_parts(base, byte_length).to_vec()
    }
  } else {
    Vec::new()
  }
}

/// `WebIdlConverter` matching the WebCrypto `BufferSource` union
/// (`ArrayBufferView` or `ArrayBuffer`). Always materializes the bytes
/// into an owned `Vec<u8>` so the data is safe to hold across `.await`.
///
/// Rejects `SharedArrayBuffer` and `ArrayBufferView`s whose backing buffer is
/// a `SharedArrayBuffer`, mirroring the WebIDL `BufferSource` converter --
/// the WebCrypto spec uses `BufferSource` without `[AllowShared]`, so the
/// SAB-backed `subtle.digest('SHA-256', new Uint8Array(new SharedArrayBuffer))`
/// call required by the `crypto-subtle-cross-realm` node compat test must
/// reject with a `TypeError`.
pub struct BufferSource(pub Vec<u8>);

impl<'a> WebIdlConverter<'a> for BufferSource {
  type Options = ();

  fn convert<'b>(
    scope: &mut v8::PinScope<'a, '_>,
    value: v8::Local<'a, v8::Value>,
    prefix: Cow<'static, str>,
    context: ContextFn<'b>,
    _options: &Self::Options,
  ) -> Result<Self, WebIdlError> {
    if let Ok(view) = v8::Local::<v8::ArrayBufferView>::try_from(value) {
      if let Some(ab) = view.buffer(scope) {
        let ab_val: v8::Local<v8::Value> = ab.into();
        if ab_val.is_shared_array_buffer() {
          return Err(WebIdlError::other(
            prefix,
            context,
            JsErrorBox::type_error(
              "is a view on a SharedArrayBuffer, which is not allowed",
            ),
          ));
        }
      }
      return Ok(BufferSource(value_to_byte_vec(scope, value)));
    }
    if value.is_shared_array_buffer() {
      return Err(WebIdlError::other(
        prefix,
        context,
        JsErrorBox::type_error("is not an ArrayBuffer or a view on one"),
      ));
    }
    if v8::Local::<v8::ArrayBuffer>::try_from(value).is_ok() {
      return Ok(BufferSource(value_to_byte_vec(scope, value)));
    }
    Err(WebIdlError::new(
      prefix,
      context,
      WebIdlErrorKind::ConvertToConverterType("BufferSource"),
    ))
  }
}

fn v8_str<'s>(
  scope: &mut v8::PinScope<'s, '_>,
  s: &str,
) -> v8::Local<'s, v8::String> {
  v8::String::new_from_one_byte(scope, s.as_bytes(), v8::NewStringType::Normal)
    .unwrap()
}

/// Execute the `SubtleCrypto.digest()` body for the already-normalized
/// algorithm + copied data. Mirrors the JS dispatch in
/// `00_crypto.js: SubtleCrypto.prototype.digest`.
pub fn run(
  algorithm: DigestAlgorithm,
  data: Vec<u8>,
) -> Result<Vec<u8>, CryptoError> {
  match algorithm {
    DigestAlgorithm::Sha(hash) => {
      Ok(digest::digest(hash.into(), &data).as_ref().to_vec())
    }
    DigestAlgorithm::Xof(xof) => run_xof(xof, &data),
    DigestAlgorithm::Unknown(name) => {
      Err(CryptoError::UnsupportedDigestAlgorithm(name))
    }
  }
}

fn run_xof(
  algorithm: SubtleDigestXof,
  data: &[u8],
) -> Result<Vec<u8>, CryptoError> {
  use sha3::digest::ExtendableOutput;
  use sha3::digest::Update;
  use sha3::digest::XofReader;
  use sha3::digest::core_api::CoreWrapper;

  // Validate `outputLength` and `domainSeparation` per the JS body. The
  // JS code threw `OperationError` DOMExceptions for these; the
  // `InvalidXofParameters` variant maps to the same class.
  let output_length = match &algorithm {
    SubtleDigestXof::CShake128 { output_length, .. }
    | SubtleDigestXof::CShake256 { output_length, .. }
    | SubtleDigestXof::TurboShake128 { output_length, .. }
    | SubtleDigestXof::TurboShake256 { output_length, .. }
    | SubtleDigestXof::Kt128 { output_length, .. }
    | SubtleDigestXof::Kt256 { output_length, .. }
    | SubtleDigestXof::KangarooTwelve { output_length, .. } => *output_length,
  };
  if !output_length.is_multiple_of(8) {
    return Err(CryptoError::InvalidXofParameters);
  }
  let is_turbo = matches!(
    algorithm,
    SubtleDigestXof::TurboShake128 { .. }
      | SubtleDigestXof::TurboShake256 { .. }
  );
  let is_kangaroo = matches!(
    algorithm,
    SubtleDigestXof::Kt128 { .. }
      | SubtleDigestXof::Kt256 { .. }
      | SubtleDigestXof::KangarooTwelve { .. }
  );
  if (is_turbo || is_kangaroo) && output_length == 0 {
    return Err(CryptoError::InvalidXofParameters);
  }
  if let SubtleDigestXof::TurboShake128 {
    domain_separation, ..
  }
  | SubtleDigestXof::TurboShake256 {
    domain_separation, ..
  } = &algorithm
    && let Some(d) = domain_separation
    && !(0x01..=0x7F).contains(d)
  {
    return Err(CryptoError::InvalidXofParameters);
  }

  let out_len = (output_length / 8) as usize;
  let mut out = vec![0u8; out_len];

  match algorithm {
    SubtleDigestXof::CShake128 {
      function_name,
      customization,
      ..
    } => {
      let core = sha3::CShake128Core::new_with_function_name(
        function_name.as_deref().unwrap_or(&[]),
        customization.as_deref().unwrap_or(&[]),
      );
      let mut h: sha3::CShake128 = CoreWrapper::from_core(core);
      h.update(data);
      h.finalize_xof().read(&mut out);
    }
    SubtleDigestXof::CShake256 {
      function_name,
      customization,
      ..
    } => {
      let core = sha3::CShake256Core::new_with_function_name(
        function_name.as_deref().unwrap_or(&[]),
        customization.as_deref().unwrap_or(&[]),
      );
      let mut h: sha3::CShake256 = CoreWrapper::from_core(core);
      h.update(data);
      h.finalize_xof().read(&mut out);
    }
    SubtleDigestXof::TurboShake128 {
      domain_separation, ..
    } => {
      let d = domain_separation.unwrap_or(0x1F);
      let core = sha3::TurboShake128Core::new(d);
      let mut h: sha3::TurboShake128 = CoreWrapper::from_core(core);
      h.update(data);
      h.finalize_xof().read(&mut out);
    }
    SubtleDigestXof::TurboShake256 {
      domain_separation, ..
    } => {
      let d = domain_separation.unwrap_or(0x1F);
      let core = sha3::TurboShake256Core::new(d);
      let mut h: sha3::TurboShake256 = CoreWrapper::from_core(core);
      h.update(data);
      h.finalize_xof().read(&mut out);
    }
    SubtleDigestXof::Kt128 { customization, .. }
    | SubtleDigestXof::KangarooTwelve { customization, .. } => {
      kangaroo_twelve_128(
        data,
        customization.as_deref().unwrap_or(&[]),
        &mut out,
      );
    }
    SubtleDigestXof::Kt256 { customization, .. } => {
      kangaroo_twelve_256(
        data,
        customization.as_deref().unwrap_or(&[]),
        &mut out,
      );
    }
  }

  Ok(out)
}

const KT_CHUNK_SIZE: usize = 8192;

fn encode_len(mut len: usize) -> Vec<u8> {
  if len == 0 {
    return vec![0];
  }
  let mut bytes = Vec::new();
  while len > 0 {
    bytes.push((len & 0xff) as u8);
    len >>= 8;
  }
  bytes.reverse();
  bytes.push(bytes.len() as u8);
  bytes
}

fn kangaroo_twelve_128(data: &[u8], customization: &[u8], out: &mut [u8]) {
  use tiny_keccak::Hasher;

  let mut h = tiny_keccak::KangarooTwelve::new(customization);
  h.update(data);
  h.finalize(out);
}

fn kangaroo_twelve_256(data: &[u8], customization: &[u8], out: &mut [u8]) {
  kangaroo_twelve_turbo::<136>(data, customization, out);
}

fn kangaroo_twelve_turbo<const RATE: usize>(
  data: &[u8],
  customization: &[u8],
  out: &mut [u8],
) {
  let mut input = Vec::with_capacity(data.len() + customization.len() + 9);
  input.extend_from_slice(data);
  input.extend_from_slice(customization);
  input.extend_from_slice(&encode_len(customization.len()));

  if input.len() <= KT_CHUNK_SIZE {
    let mut h = TurboShakeNode::<RATE>::new(0x07);
    h.update(&input);
    h.squeeze(out);
    return;
  }

  let cv_len = if RATE == 168 { 32 } else { 64 };
  let mut h = TurboShakeNode::<RATE>::new(0x06);
  h.update(&input[..KT_CHUNK_SIZE]);
  h.update(&[0x03, 0, 0, 0, 0, 0, 0, 0]);

  let mut chunks = 0usize;
  let mut tail = &input[KT_CHUNK_SIZE..];
  while !tail.is_empty() {
    let take = tail.len().min(KT_CHUNK_SIZE);
    let mut inner = TurboShakeNode::<RATE>::new(0x0b);
    inner.update(&tail[..take]);
    let mut cv = vec![0u8; cv_len];
    inner.squeeze(&mut cv);
    h.update(&cv);
    chunks += 1;
    tail = &tail[take..];
  }

  h.update(&encode_len(chunks));
  h.update(&[0xff, 0xff]);
  h.squeeze(out);
}

struct TurboShakeNode<const RATE: usize> {
  state: [u64; 25],
  offset: usize,
  delim: u8,
  squeezing: bool,
}

impl<const RATE: usize> TurboShakeNode<RATE> {
  fn new(delim: u8) -> Self {
    Self {
      state: [0; 25],
      offset: 0,
      delim,
      squeezing: false,
    }
  }

  fn update(&mut self, mut input: &[u8]) {
    debug_assert!(!self.squeezing);
    while !input.is_empty() {
      let take = input.len().min(RATE - self.offset);
      xor_into_state(&mut self.state, self.offset, &input[..take]);
      self.offset += take;
      input = &input[take..];
      if self.offset == RATE {
        tiny_keccak::keccakp(&mut self.state);
        self.offset = 0;
      }
    }
  }

  fn squeeze(&mut self, mut out: &mut [u8]) {
    if !self.squeezing {
      xor_into_state(&mut self.state, self.offset, &[self.delim]);
      xor_into_state(&mut self.state, RATE - 1, &[0x80]);
      tiny_keccak::keccakp(&mut self.state);
      self.offset = 0;
      self.squeezing = true;
    }

    while !out.is_empty() {
      if self.offset == RATE {
        tiny_keccak::keccakp(&mut self.state);
        self.offset = 0;
      }
      let take = out.len().min(RATE - self.offset);
      copy_from_state(&self.state, self.offset, &mut out[..take]);
      self.offset += take;
      out = &mut out[take..];
    }
  }
}

fn xor_into_state(state: &mut [u64; 25], offset: usize, bytes: &[u8]) {
  for (i, byte) in bytes.iter().copied().enumerate() {
    let pos = offset + i;
    state[pos / 8] ^= (byte as u64) << ((pos % 8) * 8);
  }
}

fn copy_from_state(state: &[u64; 25], offset: usize, out: &mut [u8]) {
  for (i, byte) in out.iter_mut().enumerate() {
    let pos = offset + i;
    *byte = (state[pos / 8] >> ((pos % 8) * 8)) as u8;
  }
}