deno_node_crypto 0.26.0

Node crypto compatibility for Deno
Documentation
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
// Copyright 2018-2026 the Deno authors. MIT license.

use std::borrow::Cow;
use std::cell::RefCell;
use std::mem::MaybeUninit;
use std::rc::Rc;

use aes::cipher::BlockDecryptMut;
use aes::cipher::BlockEncryptMut;
use aes::cipher::KeyIvInit;
use aes::cipher::KeySizeUser;
use aes::cipher::StreamCipher;
use aes::cipher::block_padding::Pkcs7;
use deno_core::Resource;
use deno_error::JsErrorClass;
use digest::KeyInit;
use digest::generic_array::GenericArray;
use subtle::ConstantTimeEq;

type Tag = Option<Vec<u8>>;

#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum AesWrapError {
  #[class(range)]
  #[error("Invalid key length")]
  InvalidKeyLength,
  #[class(type)]
  #[error("Invalid initialization vector")]
  InvalidIv,
  #[class(range)]
  #[error("Invalid input length")]
  InvalidInputLength,
  #[class(type)]
  #[error("AES wrap failed")]
  WrapFailed,
  #[class(type)]
  #[error("AES unwrap failed")]
  UnwrapFailed,
}

/// AES Key Wrap (RFC 3394) with optional custom IV.
///
/// For standard wrap (`aes128-wrap`, `aes192-wrap`, `aes256-wrap`):
///   - iv: 8-byte IV (NULL → default 0xA6A6A6A6A6A6A6A6)
///   - input must be a multiple of 8 and at least 16 bytes
///   - output = input_len + 8
///
/// For padded wrap (`id-aes128-wrap-pad`, `id-aes192-wrap-pad`, `id-aes256-wrap-pad`):
///   - iv: 4-byte constant (prepended; padded AIV = iv || MLI)
///   - input can be any length >= 1
///   - output = ceil(input_len / 8) * 8 + 8
pub fn aes_wrap_key(
  algorithm: &str,
  key: &[u8],
  iv: &[u8],
  data: &[u8],
) -> Result<Vec<u8>, AesWrapError> {
  let bits = match key.len() {
    16 => 128,
    24 => 192,
    32 => 256,
    _ => return Err(AesWrapError::InvalidKeyLength),
  };

  // SAFETY: AES_KEY is an opaque type; MaybeUninit lets us avoid zeroing.
  let mut aes_key = MaybeUninit::<aws_lc_sys::AES_KEY>::uninit();
  // SAFETY: key slice is valid and bits matches key.len().
  let ret = unsafe {
    aws_lc_sys::AES_set_encrypt_key(key.as_ptr(), bits, aes_key.as_mut_ptr())
  };
  if ret != 0 {
    return Err(AesWrapError::InvalidKeyLength);
  }
  // SAFETY: AES_set_encrypt_key succeeded so aes_key is fully initialised.
  let aes_key = unsafe { aes_key.assume_init() };

  let is_pad = algorithm.ends_with("-pad");

  if is_pad {
    // Padded wrap (RFC 5649). iv must be 4 bytes (the constant part of AIV).
    if iv.len() != 4 {
      return Err(AesWrapError::InvalidIv);
    }
    if data.is_empty() {
      return Err(AesWrapError::InvalidInputLength);
    }
    let mli = data.len() as u32;

    // AIV = iv_constant || MLI (big-endian)
    let mut aiv = [0u8; 8];
    aiv[..4].copy_from_slice(iv);
    aiv[4..].copy_from_slice(&mli.to_be_bytes());

    if data.len() <= 8 {
      // Single-block case: wrap = AES_encrypt(AIV || data_padded).
      // AES_wrap_key requires padded_len >= 16, so handle this explicitly.
      let mut block = [0u8; 16];
      block[..8].copy_from_slice(&aiv);
      block[8..8 + data.len()].copy_from_slice(data);
      let mut out = vec![0u8; 16];
      // SAFETY: aes_key is initialised; block and out are 16 bytes.
      unsafe {
        aws_lc_sys::AES_encrypt(block.as_ptr(), out.as_mut_ptr(), &aes_key);
      }
      return Ok(out);
    }

    let padded_len = data.len().next_multiple_of(8);
    let mut padded = vec![0u8; padded_len];
    padded[..data.len()].copy_from_slice(data);

    let out_len = padded_len + 8;
    let mut out = vec![0u8; out_len];
    // SAFETY: aes_key is initialised; aiv, out, padded are valid slices.
    let ret = unsafe {
      aws_lc_sys::AES_wrap_key(
        &aes_key,
        aiv.as_ptr(),
        out.as_mut_ptr(),
        padded.as_ptr(),
        padded_len,
      )
    };
    if ret < 0 {
      return Err(AesWrapError::WrapFailed);
    }
    Ok(out)
  } else {
    // Standard wrap (RFC 3394). iv must be 8 bytes or empty (→ default IV).
    if !iv.is_empty() && iv.len() != 8 {
      return Err(AesWrapError::InvalidIv);
    }
    if data.len() < 16 || !data.len().is_multiple_of(8) {
      return Err(AesWrapError::InvalidInputLength);
    }
    let iv_ptr = if iv.is_empty() {
      std::ptr::null()
    } else {
      iv.as_ptr()
    };
    let out_len = data.len() + 8;
    let mut out = vec![0u8; out_len];
    // SAFETY: aes_key is initialised; pointers and lengths are valid.
    let ret = unsafe {
      aws_lc_sys::AES_wrap_key(
        &aes_key,
        iv_ptr,
        out.as_mut_ptr(),
        data.as_ptr(),
        data.len(),
      )
    };
    if ret < 0 {
      return Err(AesWrapError::WrapFailed);
    }
    Ok(out)
  }
}

// RFC 3394 / 5649 inverse step: unwrap n+1 blocks → n plaintext blocks
// and the recovered AIV. Mirrors aws-lc's static `aes_unwrap_key_inner`
// (crypto/fipsmodule/aes/key_wrap.c) so we can validate the AIV ourselves
// in constant time — needed because `AES_unwrap_key` does the IV check
// internally and rejects any non-default AIV.
fn aes_unwrap_inner(
  key: &aws_lc_sys::AES_KEY,
  data: &[u8],
) -> (Vec<u8>, [u8; 8]) {
  debug_assert!(data.len().is_multiple_of(8) && data.len() >= 24);
  let n = data.len() / 8 - 1;
  let mut out = vec![0u8; data.len() - 8];
  let mut a = [0u8; 8];
  a.copy_from_slice(&data[..8]);
  out.copy_from_slice(&data[8..]);

  let mut block = [0u8; 16];
  for j in (0..6u32).rev() {
    for i in (1..=n).rev() {
      let t = (n as u32) * j + i as u32;
      a[7] ^= (t & 0xff) as u8;
      a[6] ^= ((t >> 8) & 0xff) as u8;
      a[5] ^= ((t >> 16) & 0xff) as u8;
      a[4] ^= ((t >> 24) & 0xff) as u8;
      block[..8].copy_from_slice(&a);
      block[8..].copy_from_slice(&out[(i - 1) * 8..i * 8]);
      // SAFETY: key is initialised; block is a valid 16-byte buffer.
      unsafe {
        aws_lc_sys::AES_decrypt(block.as_ptr(), block.as_mut_ptr(), key);
      }
      a.copy_from_slice(&block[..8]);
      out[(i - 1) * 8..i * 8].copy_from_slice(&block[8..]);
    }
  }
  (out, a)
}

/// AES Key Unwrap (RFC 3394 / RFC 5649) with optional custom IV.
pub fn aes_unwrap_key(
  algorithm: &str,
  key: &[u8],
  iv: &[u8],
  data: &[u8],
) -> Result<Vec<u8>, AesWrapError> {
  let bits = match key.len() {
    16 => 128,
    24 => 192,
    32 => 256,
    _ => return Err(AesWrapError::InvalidKeyLength),
  };

  // SAFETY: AES_KEY is an opaque type; MaybeUninit avoids zeroing.
  let mut aes_key = MaybeUninit::<aws_lc_sys::AES_KEY>::uninit();
  // SAFETY: key slice and bits are valid.
  let ret = unsafe {
    aws_lc_sys::AES_set_decrypt_key(key.as_ptr(), bits, aes_key.as_mut_ptr())
  };
  if ret != 0 {
    return Err(AesWrapError::InvalidKeyLength);
  }
  // SAFETY: AES_set_decrypt_key succeeded so aes_key is initialised.
  let aes_key = unsafe { aes_key.assume_init() };

  let is_pad = algorithm.ends_with("-pad");

  if is_pad {
    // Padded unwrap (RFC 5649). iv is the 4-byte AIV constant.
    if iv.len() != 4 {
      return Err(AesWrapError::InvalidIv);
    }
    if data.len() < 16 || !data.len().is_multiple_of(8) {
      return Err(AesWrapError::InvalidInputLength);
    }

    // Decrypt once, recover AIV. Two cases per RFC 5649:
    //   - len == 16: single-block (MLI ≤ 8); decrypt one block, AIV is high
    //     half, plaintext (zero-padded to 8 bytes) is low half.
    //   - len  > 16: standard unwrap inverse, AIV is the recovered A.
    let (mut out, recovered_aiv) = if data.len() == 16 {
      let mut block = [0u8; 16];
      // SAFETY: aes_key is initialised; data and block are 16 bytes.
      unsafe {
        aws_lc_sys::AES_decrypt(data.as_ptr(), block.as_mut_ptr(), &aes_key);
      }
      let mut aiv = [0u8; 8];
      aiv.copy_from_slice(&block[..8]);
      let mut pt = vec![0u8; 8];
      pt.copy_from_slice(&block[8..]);
      (pt, aiv)
    } else {
      aes_unwrap_inner(&aes_key, data)
    };

    // Validate AIV constant in constant time.
    let constant_ok = recovered_aiv[..4].ct_eq(iv).unwrap_u8();
    let mli = u32::from_be_bytes([
      recovered_aiv[4],
      recovered_aiv[5],
      recovered_aiv[6],
      recovered_aiv[7],
    ]) as usize;

    // MLI must satisfy: (n-1)*8 < MLI <= n*8, where n = ceil(MLI/8) blocks.
    // i.e. ceil(MLI/8) == out.len()/8, and MLI >= 1.
    let n_blocks = out.len() / 8;
    let mli_blocks = mli.div_ceil(8);
    let len_ok = (mli >= 1 && mli_blocks == n_blocks) as u8;

    // Padding bytes (out[mli..]) must all be zero.
    let pad_ok = if mli <= out.len() {
      let mut acc = 0u8;
      for &b in &out[mli.min(out.len())..] {
        acc |= b;
      }
      (acc == 0) as u8
    } else {
      0
    };

    if (constant_ok & len_ok & pad_ok) != 1 {
      return Err(AesWrapError::UnwrapFailed);
    }
    out.truncate(mli);
    Ok(out)
  } else {
    // Standard unwrap (RFC 3394). iv must be 8 bytes or empty (→ default IV).
    if !iv.is_empty() && iv.len() != 8 {
      return Err(AesWrapError::InvalidIv);
    }
    if data.len() < 24 || !data.len().is_multiple_of(8) {
      return Err(AesWrapError::InvalidInputLength);
    }
    let (out, recovered_aiv) = aes_unwrap_inner(&aes_key, data);
    let expected: &[u8] = if iv.is_empty() { &[0xa6; 8] } else { iv };
    if recovered_aiv.ct_eq(expected).unwrap_u8() != 1 {
      return Err(AesWrapError::UnwrapFailed);
    }
    Ok(out)
  }
}

type Aes128Gcm = aead_gcm_stream::AesGcm<aes::Aes128>;
type Aes256Gcm = aead_gcm_stream::AesGcm<aes::Aes256>;

enum CipherInitError {
  ContextAllocation,
  InitFailed,
}

/// ChaCha20-Poly1305 cipher backed by aws-lc-sys (BoringSSL).
///
/// Uses the streaming EVP_CIPHER API for hardware-accelerated performance
/// on all platforms (NEON on aarch64, AVX2/SSE on x86_64).
struct ChaCha20Poly1305Cipher {
  ctx: *mut aws_lc_sys::EVP_CIPHER_CTX,
  aad_buf: Vec<u8>,
  aad_flushed: bool,
  auth_tag_length: usize,
}

// SAFETY: ChaCha20Poly1305Cipher is only accessed from a single thread
// (via RefCell in CipherContext/DecipherContext). The EVP_CIPHER_CTX
// pointer is exclusively owned by this struct.
unsafe impl Send for ChaCha20Poly1305Cipher {}

impl ChaCha20Poly1305Cipher {
  fn new(
    key: &[u8],
    iv: &[u8],
    auth_tag_length: usize,
    encrypting: bool,
  ) -> Result<Self, CipherInitError> {
    // SAFETY: We allocate a new EVP_CIPHER_CTX and initialize it with
    // validated key/iv. The ctx is exclusively owned by this struct and
    // freed in Drop.
    unsafe {
      let ctx = aws_lc_sys::EVP_CIPHER_CTX_new();
      if ctx.is_null() {
        return Err(CipherInitError::ContextAllocation);
      }

      let cipher = aws_lc_sys::EVP_chacha20_poly1305();
      let enc = if encrypting { 1 } else { 0 };
      let ret = aws_lc_sys::EVP_CipherInit_ex(
        ctx,
        cipher,
        std::ptr::null_mut(),
        key.as_ptr(),
        iv.as_ptr(),
        enc,
      );
      if ret != 1 {
        aws_lc_sys::EVP_CIPHER_CTX_free(ctx);
        return Err(CipherInitError::InitFailed);
      }

      Ok(ChaCha20Poly1305Cipher {
        ctx,
        aad_buf: Vec::new(),
        aad_flushed: false,
        auth_tag_length,
      })
    }
  }

  fn set_aad(&mut self, aad: &[u8]) {
    self.aad_buf.extend_from_slice(aad);
  }

  /// Flush buffered AAD to EVP context. Called lazily before the first
  /// encrypt/decrypt so that multiple setAAD() calls are concatenated.
  fn flush_aad(&mut self) {
    if !self.aad_flushed {
      self.aad_flushed = true;
      if !self.aad_buf.is_empty() {
        // SAFETY: ctx is valid, aad_buf is a valid slice. Passing NULL
        // output tells EVP this is AAD, not plaintext/ciphertext.
        // Length is validated to fit in i32 before casting.
        unsafe {
          let aad_len: i32 = self
            .aad_buf
            .len()
            .try_into()
            .expect("AAD length exceeds i32::MAX");
          let mut outl: i32 = 0;
          let ret = aws_lc_sys::EVP_CipherUpdate(
            self.ctx,
            std::ptr::null_mut(),
            &mut outl,
            self.aad_buf.as_ptr(),
            aad_len,
          );
          assert_eq!(ret, 1, "EVP_CipherUpdate for AAD failed");
        }
      }
    }
  }

  fn encrypt(&mut self, input: &[u8], output: &mut [u8]) {
    assert!(output.len() >= input.len());
    self.flush_aad();
    // SAFETY: ctx is valid and initialized for encryption. output is
    // caller-provided with at least input.len() bytes. EVP_CipherUpdate
    // writes at most input.len() bytes for a stream cipher.
    // Length is validated to fit in i32 before casting.
    unsafe {
      let input_len: i32 = input
        .len()
        .try_into()
        .expect("input length exceeds i32::MAX");
      let mut outl: i32 = 0;
      let ret = aws_lc_sys::EVP_CipherUpdate(
        self.ctx,
        output.as_mut_ptr(),
        &mut outl,
        input.as_ptr(),
        input_len,
      );
      assert_eq!(ret, 1, "EVP_CipherUpdate for encryption failed");
    }
  }

  fn decrypt(&mut self, input: &[u8], output: &mut [u8]) {
    assert!(output.len() >= input.len());
    self.flush_aad();
    // SAFETY: ctx is valid and initialized for decryption. output is
    // caller-provided with at least input.len() bytes.
    // Length is validated to fit in i32 before casting.
    unsafe {
      let input_len: i32 = input
        .len()
        .try_into()
        .expect("input length exceeds i32::MAX");
      let mut outl: i32 = 0;
      let ret = aws_lc_sys::EVP_CipherUpdate(
        self.ctx,
        output.as_mut_ptr(),
        &mut outl,
        input.as_ptr(),
        input_len,
      );
      assert_eq!(ret, 1, "EVP_CipherUpdate for decryption failed");
    }
  }

  fn compute_tag(mut self) -> Vec<u8> {
    self.flush_aad();
    // SAFETY: ctx is valid. CipherFinal_ex finalizes the AEAD operation,
    // then CTRL_AEAD_GET_TAG retrieves the computed authentication tag.
    // The tag buffer is freshly allocated with the correct length
    // (validated to 1..=16 at construction).
    unsafe {
      let mut outl: i32 = 0;
      let ret = aws_lc_sys::EVP_CipherFinal_ex(
        self.ctx,
        std::ptr::null_mut(),
        &mut outl,
      );
      assert_eq!(ret, 1, "EVP_CipherFinal_ex failed");

      let mut tag = vec![0u8; self.auth_tag_length];
      let ret = aws_lc_sys::EVP_CIPHER_CTX_ctrl(
        self.ctx,
        aws_lc_sys::EVP_CTRL_AEAD_GET_TAG,
        self.auth_tag_length as i32,
        tag.as_mut_ptr() as *mut std::ffi::c_void,
      );
      assert_eq!(ret, 1, "EVP_CTRL_AEAD_GET_TAG failed");

      tag
    }
  }

  fn verify_tag(mut self, auth_tag: &[u8]) -> bool {
    self.flush_aad();
    // SAFETY: ctx is valid and initialized for decryption. We set the
    // expected tag via CTRL_AEAD_SET_TAG, then CipherFinal_ex performs
    // constant-time tag comparison internally, returning 0 on mismatch.
    unsafe {
      let ret = aws_lc_sys::EVP_CIPHER_CTX_ctrl(
        self.ctx,
        aws_lc_sys::EVP_CTRL_AEAD_SET_TAG,
        auth_tag.len() as i32,
        auth_tag.as_ptr() as *mut std::ffi::c_void,
      );
      if ret != 1 {
        return false;
      }

      let mut outl: i32 = 0;
      let ret = aws_lc_sys::EVP_CipherFinal_ex(
        self.ctx,
        std::ptr::null_mut(),
        &mut outl,
      );
      ret == 1
    }
  }
}

impl Drop for ChaCha20Poly1305Cipher {
  fn drop(&mut self) {
    // SAFETY: ctx was allocated by EVP_CIPHER_CTX_new and is exclusively
    // owned by this struct. This is the only place it is freed.
    unsafe {
      aws_lc_sys::EVP_CIPHER_CTX_free(self.ctx);
    }
  }
}

/// Raw ChaCha20 stream cipher backed by aws-lc-sys (BoringSSL).
///
/// Matches OpenSSL's `EVP_chacha20` semantics used by Node.js: the 16-byte
/// IV consists of a 32-bit little-endian block counter followed by a
/// 96-bit nonce.
struct ChaCha20Cipher {
  key: [u8; 32],
  nonce: [u8; 12],
  counter: u32,
  /// Number of keystream bytes consumed so far.
  pos: u64,
  /// Keystream of the 64-byte block containing `pos`. Only meaningful
  /// while `pos` is mid-block (`pos % 64 != 0`); the bytes from
  /// `pos % 64` on are not yet consumed.
  keystream_block: [u8; 64],
}

impl ChaCha20Cipher {
  fn new(key: &[u8], iv: &[u8]) -> Self {
    debug_assert_eq!(key.len(), 32);
    debug_assert_eq!(iv.len(), 16);
    let mut key_arr = [0u8; 32];
    key_arr.copy_from_slice(key);
    let mut nonce = [0u8; 12];
    nonce.copy_from_slice(&iv[4..16]);
    let counter = u32::from_le_bytes([iv[0], iv[1], iv[2], iv[3]]);
    Self {
      key: key_arr,
      nonce,
      counter,
      pos: 0,
      keystream_block: [0u8; 64],
    }
  }

  /// Fills `keystream_block` with the keystream of the block containing
  /// `pos` by encrypting 64 zero bytes.
  fn refill_keystream_block(&mut self) {
    debug_assert_eq!(self.pos % 64, 0);
    let counter = self.counter.wrapping_add((self.pos / 64) as u32);
    self.keystream_block = [0u8; 64];
    // SAFETY: keystream_block is a valid 64-byte buffer for in-place
    // encryption (in == out is allowed); key and nonce have the exact
    // sizes CRYPTO_chacha_20 requires (32 and 12 bytes).
    unsafe {
      aws_lc_sys::CRYPTO_chacha_20(
        self.keystream_block.as_mut_ptr(),
        self.keystream_block.as_ptr(),
        self.keystream_block.len(),
        self.key.as_ptr(),
        self.nonce.as_ptr(),
        counter,
      );
    }
  }

  /// XORs the keystream into `input`, writing to `output`. First consumes
  /// any keystream left over in `keystream_block` from a previous
  /// mid-block call, then processes the remaining whole blocks in one
  /// pass, and finally caches the keystream of a trailing partial block
  /// for the next call — no per-call allocation.
  fn apply_keystream(&mut self, input: &[u8], output: &mut [u8]) {
    assert!(output.len() >= input.len());

    // Use up the cached keystream of the current partial block.
    let offset = (self.pos % 64) as usize;
    let mut consumed = 0;
    if offset != 0 {
      consumed = input.len().min(64 - offset);
      for ((out, inp), key) in output
        .iter_mut()
        .zip(input)
        .zip(&self.keystream_block[offset..])
      {
        *out = inp ^ key;
      }
      self.pos += consumed as u64;
    }
    let input = &input[consumed..];
    let output = &mut output[consumed..];

    // Process all remaining whole blocks directly input -> output.
    let whole = input.len() - input.len() % 64;
    if whole != 0 {
      let counter = self.counter.wrapping_add((self.pos / 64) as u32);
      // SAFETY: input and output are valid for `whole` bytes (output
      // length asserted above); key and nonce have the exact sizes
      // CRYPTO_chacha_20 requires (32 and 12 bytes).
      unsafe {
        aws_lc_sys::CRYPTO_chacha_20(
          output.as_mut_ptr(),
          input.as_ptr(),
          whole,
          self.key.as_ptr(),
          self.nonce.as_ptr(),
          counter,
        );
      }
      self.pos += whole as u64;
    }

    // A trailing partial block: cache its keystream and XOR from it.
    let tail = &input[whole..];
    if !tail.is_empty() {
      self.refill_keystream_block();
      for ((out, inp), key) in output[whole..]
        .iter_mut()
        .zip(tail)
        .zip(&self.keystream_block)
      {
        *out = inp ^ key;
      }
      self.pos += tail.len() as u64;
    }
  }
}

enum Cipher {
  Aes128Cbc(Box<cbc::Encryptor<aes::Aes128>>),
  Aes128Ecb(Box<ecb::Encryptor<aes::Aes128>>),
  Aes192Ecb(Box<ecb::Encryptor<aes::Aes192>>),
  Aes256Ecb(Box<ecb::Encryptor<aes::Aes256>>),
  Aes128Gcm(Box<Aes128Gcm>, Option<usize>),
  Aes256Gcm(Box<Aes256Gcm>, Option<usize>),
  Aes256Cbc(Box<cbc::Encryptor<aes::Aes256>>),
  Aes128Ctr(Box<ctr::Ctr128BE<aes::Aes128>>),
  Aes192Ctr(Box<ctr::Ctr128BE<aes::Aes192>>),
  Aes256Ctr(Box<ctr::Ctr128BE<aes::Aes256>>),
  DesEde3Cbc(Box<cbc::Encryptor<des::TdesEde3>>),
  ChaCha20(Box<ChaCha20Cipher>),
  ChaCha20Poly1305(Box<ChaCha20Poly1305Cipher>),
  // TODO(kt3k): add more algorithms Aes192Cbc, etc.
}

enum Decipher {
  Aes128Cbc(Box<cbc::Decryptor<aes::Aes128>>),
  Aes128Ecb(Box<ecb::Decryptor<aes::Aes128>>),
  Aes192Ecb(Box<ecb::Decryptor<aes::Aes192>>),
  Aes256Ecb(Box<ecb::Decryptor<aes::Aes256>>),
  Aes128Gcm(Box<Aes128Gcm>, Option<usize>),
  Aes256Gcm(Box<Aes256Gcm>, Option<usize>),
  Aes256Cbc(Box<cbc::Decryptor<aes::Aes256>>),
  Aes128Ctr(Box<ctr::Ctr128BE<aes::Aes128>>),
  Aes192Ctr(Box<ctr::Ctr128BE<aes::Aes192>>),
  Aes256Ctr(Box<ctr::Ctr128BE<aes::Aes256>>),
  DesEde3Cbc(Box<cbc::Decryptor<des::TdesEde3>>),
  ChaCha20(Box<ChaCha20Cipher>),
  ChaCha20Poly1305(Box<ChaCha20Poly1305Cipher>, Option<usize>),
  // TODO(kt3k): add more algorithms Aes192Cbc, Aes128GCM, etc.
}

pub struct CipherContext {
  cipher: Rc<RefCell<Cipher>>,
}

pub struct DecipherContext {
  decipher: Rc<RefCell<Decipher>>,
}

#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum CipherContextError {
  #[class(type)]
  #[error("Cipher context is already in use")]
  ContextInUse,
  #[class(inherit)]
  #[error("{0}")]
  Resource(#[from] deno_core::error::ResourceError),
  #[class(inherit)]
  #[error(transparent)]
  Cipher(#[from] CipherError),
}

impl CipherContext {
  pub fn new(
    algorithm: &str,
    key: &[u8],
    iv: &[u8],
    auth_tag_length: Option<usize>,
  ) -> Result<Self, CipherContextError> {
    Ok(Self {
      cipher: Rc::new(RefCell::new(Cipher::new(
        algorithm,
        key,
        iv,
        auth_tag_length,
      )?)),
    })
  }

  pub fn set_aad(&self, aad: &[u8]) {
    self.cipher.borrow_mut().set_aad(aad);
  }

  pub fn encrypt(&self, input: &[u8], output: &mut [u8]) {
    self.cipher.borrow_mut().encrypt(input, output);
  }

  pub fn take_tag(self) -> Tag {
    Rc::try_unwrap(self.cipher).ok()?.into_inner().take_tag()
  }

  pub fn r#final(
    self,
    auto_pad: bool,
    input: &[u8],
    output: &mut [u8],
  ) -> Result<Tag, CipherContextError> {
    Rc::try_unwrap(self.cipher)
      .map_err(|_| CipherContextError::ContextInUse)?
      .into_inner()
      .r#final(auto_pad, input, output)
      .map_err(Into::into)
  }
}

#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum DecipherContextError {
  #[class(type)]
  #[error("Decipher context is already in use")]
  ContextInUse,
  #[class(inherit)]
  #[error("{0}")]
  Resource(#[from] deno_core::error::ResourceError),
  #[class(inherit)]
  #[error(transparent)]
  Decipher(#[from] DecipherError),
}

impl DecipherContext {
  pub fn new(
    algorithm: &str,
    key: &[u8],
    iv: &[u8],
    auth_tag_length: Option<usize>,
  ) -> Result<Self, DecipherContextError> {
    Ok(Self {
      decipher: Rc::new(RefCell::new(Decipher::new(
        algorithm,
        key,
        iv,
        auth_tag_length,
      )?)),
    })
  }

  pub fn validate_auth_tag(
    &self,
    length: usize,
  ) -> Result<(), DecipherContextError> {
    self.decipher.borrow().validate_auth_tag(length)?;

    Ok(())
  }

  pub fn set_aad(&self, aad: &[u8]) {
    self.decipher.borrow_mut().set_aad(aad);
  }

  pub fn decrypt(&self, input: &[u8], output: &mut [u8]) {
    self.decipher.borrow_mut().decrypt(input, output);
  }

  pub fn r#final(
    self,
    auto_pad: bool,
    input: &[u8],
    output: &mut [u8],
    auth_tag: &[u8],
  ) -> Result<(), DecipherContextError> {
    Rc::try_unwrap(self.decipher)
      .map_err(|_| DecipherContextError::ContextInUse)?
      .into_inner()
      .r#final(auto_pad, input, output, auth_tag)
      .map_err(Into::into)
  }
}

impl Resource for CipherContext {
  fn name(&self) -> Cow<'_, str> {
    "cryptoCipher".into()
  }
}

impl Resource for DecipherContext {
  fn name(&self) -> Cow<'_, str> {
    "cryptoDecipher".into()
  }
}

#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum CipherError {
  #[class(type)]
  #[error("IV length must be 12 bytes")]
  InvalidIvLength,
  #[class(range)]
  #[error("Invalid key length")]
  InvalidKeyLength,
  #[class(type)]
  #[error("Invalid initialization vector")]
  InvalidInitializationVector,
  #[class(type)]
  #[error("bad decrypt")]
  CannotPadInputData,
  #[class(type)]
  #[error("Unknown cipher {0}")]
  UnknownCipher(String),
  #[class(type)]
  #[error("Invalid authentication tag length: {0}")]
  InvalidAuthTag(usize),
}

fn is_valid_chacha20_poly1305_tag_length(tag_len: usize) -> bool {
  (1..=16).contains(&tag_len)
}

impl Cipher {
  fn new(
    algorithm_name: &str,
    key: &[u8],
    iv: &[u8],
    auth_tag_length: Option<usize>,
  ) -> Result<Self, CipherError> {
    use Cipher::*;
    Ok(match algorithm_name {
      "aes128" | "aes-128-cbc" => {
        if key.len() != 16 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes128Cbc(Box::new(cbc::Encryptor::new(key.into(), iv.into())))
      }
      "aes-128-ecb" => {
        if key.len() != 16 {
          return Err(CipherError::InvalidKeyLength);
        }
        if !iv.is_empty() {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes128Ecb(Box::new(ecb::Encryptor::new(key.into())))
      }
      "aes-192-ecb" => {
        if key.len() != 24 {
          return Err(CipherError::InvalidKeyLength);
        }
        if !iv.is_empty() {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes192Ecb(Box::new(ecb::Encryptor::new(key.into())))
      }
      "aes-256-ecb" => {
        if key.len() != 32 {
          return Err(CipherError::InvalidKeyLength);
        }
        if !iv.is_empty() {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes256Ecb(Box::new(ecb::Encryptor::new(key.into())))
      }
      "aes-128-gcm" => {
        if key.len() != aes::Aes128::key_size() {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.is_empty() {
          return Err(CipherError::InvalidInitializationVector);
        }

        if let Some(tag_len) = auth_tag_length
          && !is_valid_gcm_tag_length(tag_len)
        {
          return Err(CipherError::InvalidAuthTag(tag_len));
        }

        let cipher =
          aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into(), iv);

        Aes128Gcm(Box::new(cipher), auth_tag_length)
      }
      "aes-256-gcm" => {
        if key.len() != aes::Aes256::key_size() {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.is_empty() {
          return Err(CipherError::InvalidInitializationVector);
        }

        if let Some(tag_len) = auth_tag_length
          && !is_valid_gcm_tag_length(tag_len)
        {
          return Err(CipherError::InvalidAuthTag(tag_len));
        }

        let cipher =
          aead_gcm_stream::AesGcm::<aes::Aes256>::new(key.into(), iv);

        Aes256Gcm(Box::new(cipher), auth_tag_length)
      }
      "aes256" | "aes-256-cbc" => {
        if key.len() != 32 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(CipherError::InvalidInitializationVector);
        }

        Aes256Cbc(Box::new(cbc::Encryptor::new(key.into(), iv.into())))
      }
      "aes-256-ctr" => {
        if key.len() != 32 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes256Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
      }
      "aes-192-ctr" => {
        if key.len() != 24 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes192Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
      }
      "aes-128-ctr" => {
        if key.len() != 16 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(CipherError::InvalidInitializationVector);
        }
        Aes128Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
      }
      "des-ede3-cbc" => {
        if key.len() != 24 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 8 {
          return Err(CipherError::InvalidInitializationVector);
        }
        DesEde3Cbc(Box::new(cbc::Encryptor::new(key.into(), iv.into())))
      }
      "chacha20" => {
        if key.len() != 32 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(CipherError::InvalidInitializationVector);
        }
        ChaCha20(Box::new(ChaCha20Cipher::new(key, iv)))
      }
      "chacha20-poly1305" => {
        if key.len() != 32 {
          return Err(CipherError::InvalidKeyLength);
        }
        if iv.len() != 12 {
          return Err(CipherError::InvalidInitializationVector);
        }
        let tag_len = auth_tag_length.unwrap_or(16);
        if !is_valid_chacha20_poly1305_tag_length(tag_len) {
          return Err(CipherError::InvalidAuthTag(tag_len));
        }
        ChaCha20Poly1305(Box::new(
          ChaCha20Poly1305Cipher::new(key, iv, tag_len, true).map_err(|e| {
            match e {
              CipherInitError::ContextAllocation => {
                panic!("Failed to allocate EVP_CIPHER_CTX")
              }
              CipherInitError::InitFailed => CipherError::InvalidKeyLength,
            }
          })?,
        ))
      }
      _ => return Err(CipherError::UnknownCipher(algorithm_name.to_string())),
    })
  }

  fn set_aad(&mut self, aad: &[u8]) {
    use Cipher::*;
    match self {
      Aes128Gcm(cipher, _) => {
        cipher.set_aad(aad);
      }
      Aes256Gcm(cipher, _) => {
        cipher.set_aad(aad);
      }
      ChaCha20Poly1305(cipher) => {
        cipher.set_aad(aad);
      }
      _ => {}
    }
  }

  /// encrypt encrypts the data in the middle of the input.
  fn encrypt(&mut self, input: &[u8], output: &mut [u8]) {
    use Cipher::*;
    match self {
      Aes128Cbc(encryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          encryptor.encrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes128Ecb(encryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          encryptor.encrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes192Ecb(encryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          encryptor.encrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes256Ecb(encryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          encryptor.encrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes128Gcm(cipher, _) => {
        output[..input.len()].copy_from_slice(input);
        cipher.encrypt(output);
      }
      Aes256Gcm(cipher, _) => {
        output[..input.len()].copy_from_slice(input);
        cipher.encrypt(output);
      }
      Aes256Cbc(encryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          encryptor.encrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes256Ctr(encryptor) => {
        encryptor.apply_keystream_b2b(input, output).unwrap();
      }
      Aes192Ctr(encryptor) => {
        encryptor.apply_keystream_b2b(input, output).unwrap();
      }
      Aes128Ctr(encryptor) => {
        encryptor.apply_keystream_b2b(input, output).unwrap();
      }
      DesEde3Cbc(encryptor) => {
        assert!(input.len().is_multiple_of(8));
        for (input, output) in input.chunks(8).zip(output.chunks_mut(8)) {
          encryptor.encrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      ChaCha20(cipher) => {
        cipher.apply_keystream(input, output);
      }
      ChaCha20Poly1305(cipher) => {
        cipher.encrypt(input, output);
      }
    }
  }

  /// r#final encrypts the last block of the input data.
  fn r#final(
    self,
    auto_pad: bool,
    input: &[u8],
    output: &mut [u8],
  ) -> Result<Tag, CipherError> {
    use Cipher::*;
    match (self, auto_pad) {
      (Aes128Cbc(encryptor), true) => {
        let _ = (*encryptor)
          .encrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| CipherError::CannotPadInputData)?;
        Ok(None)
      }
      (Aes128Cbc(mut encryptor), false) => {
        encryptor.encrypt_block_b2b_mut(
          GenericArray::from_slice(input),
          GenericArray::from_mut_slice(output),
        );
        Ok(None)
      }
      (Aes128Ecb(encryptor), true) => {
        let _ = (*encryptor)
          .encrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| CipherError::CannotPadInputData)?;
        Ok(None)
      }
      (Aes128Ecb(mut encryptor), false) => {
        encryptor.encrypt_block_b2b_mut(
          GenericArray::from_slice(input),
          GenericArray::from_mut_slice(output),
        );
        Ok(None)
      }
      (Aes192Ecb(encryptor), true) => {
        let _ = (*encryptor)
          .encrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| CipherError::CannotPadInputData)?;
        Ok(None)
      }
      (Aes192Ecb(mut encryptor), false) => {
        encryptor.encrypt_block_b2b_mut(
          GenericArray::from_slice(input),
          GenericArray::from_mut_slice(output),
        );
        Ok(None)
      }
      (Aes256Ecb(encryptor), true) => {
        let _ = (*encryptor)
          .encrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| CipherError::CannotPadInputData)?;
        Ok(None)
      }
      (Aes256Ecb(mut encryptor), false) => {
        encryptor.encrypt_block_b2b_mut(
          GenericArray::from_slice(input),
          GenericArray::from_mut_slice(output),
        );
        Ok(None)
      }
      (Aes128Gcm(cipher, auth_tag_length), _) => {
        let mut tag = cipher.finish().to_vec();
        if let Some(tag_len) = auth_tag_length {
          tag.truncate(tag_len);
        }
        Ok(Some(tag))
      }
      (Aes256Gcm(cipher, auth_tag_length), _) => {
        let mut tag = cipher.finish().to_vec();
        if let Some(tag_len) = auth_tag_length {
          tag.truncate(tag_len);
        }
        Ok(Some(tag))
      }
      (Aes256Cbc(encryptor), true) => {
        let _ = (*encryptor)
          .encrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| CipherError::CannotPadInputData)?;
        Ok(None)
      }
      (Aes256Cbc(mut encryptor), false) => {
        encryptor.encrypt_block_b2b_mut(
          GenericArray::from_slice(input),
          GenericArray::from_mut_slice(output),
        );
        Ok(None)
      }
      (Aes256Ctr(_) | Aes128Ctr(_) | Aes192Ctr(_) | ChaCha20(_), _) => Ok(None),
      (ChaCha20Poly1305(cipher), _) => {
        let tag = cipher.compute_tag();
        Ok(Some(tag))
      }
      (DesEde3Cbc(encryptor), true) => {
        let _ = (*encryptor)
          .encrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| CipherError::CannotPadInputData)?;
        Ok(None)
      }
      (DesEde3Cbc(mut encryptor), false) => {
        encryptor.encrypt_block_b2b_mut(
          GenericArray::from_slice(input),
          GenericArray::from_mut_slice(output),
        );
        Ok(None)
      }
    }
  }

  fn take_tag(self) -> Tag {
    use Cipher::*;
    match self {
      Aes128Gcm(cipher, auth_tag_length) => {
        let mut tag = cipher.finish().to_vec();
        if let Some(tag_len) = auth_tag_length {
          tag.truncate(tag_len);
        }
        Some(tag)
      }
      Aes256Gcm(cipher, auth_tag_length) => {
        let mut tag = cipher.finish().to_vec();
        if let Some(tag_len) = auth_tag_length {
          tag.truncate(tag_len);
        }
        Some(tag)
      }
      ChaCha20Poly1305(cipher) => {
        let tag = cipher.compute_tag();
        Some(tag)
      }
      _ => None,
    }
  }
}

#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[property("library" = "Provider routines")]
#[property("reason" = self.reason())]
#[property("code" = self.code())]
pub enum DecipherError {
  #[class(type)]
  #[error("IV length must be 12 bytes")]
  InvalidIvLength,
  #[class(range)]
  #[error("Invalid key length")]
  InvalidKeyLength,
  #[class(type)]
  #[error("Invalid authentication tag length: {0}")]
  InvalidAuthTag(usize),
  #[class(range)]
  #[error("error:1C80006B:Provider routines::wrong final block length")]
  InvalidFinalBlockLength,
  #[class(type)]
  #[error("Invalid initialization vector")]
  InvalidInitializationVector,
  #[class(type)]
  #[error("bad decrypt")]
  CannotUnpadInputData,
  #[class(type)]
  #[error("Unsupported state or unable to authenticate data")]
  DataAuthenticationFailed,
  #[class(type)]
  #[error("Unknown cipher {0}")]
  UnknownCipher(String),
}

impl DecipherError {
  fn code(&self) -> deno_error::PropertyValue {
    match self {
      Self::InvalidIvLength => {
        deno_error::PropertyValue::String("ERR_CRYPTO_INVALID_IV_LENGTH".into())
      }
      Self::InvalidKeyLength => deno_error::PropertyValue::String(
        "ERR_CRYPTO_INVALID_KEY_LENGTH".into(),
      ),
      Self::InvalidAuthTag(_) => {
        deno_error::PropertyValue::String("ERR_CRYPTO_INVALID_AUTH_TAG".into())
      }
      Self::InvalidFinalBlockLength => deno_error::PropertyValue::String(
        "ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH".into(),
      ),
      Self::CannotUnpadInputData => {
        deno_error::PropertyValue::String("ERR_OSSL_EVP_BAD_DECRYPT".into())
      }
      _ => deno_error::PropertyValue::String("ERR_CRYPTO_DECIPHER".into()),
    }
  }

  fn reason(&self) -> deno_error::PropertyValue {
    match self {
      Self::InvalidFinalBlockLength => {
        deno_error::PropertyValue::String("wrong final block length".into())
      }
      _ => deno_error::PropertyValue::String(self.get_message()),
    }
  }
}

macro_rules! assert_block_len {
  ($input:expr, $len:expr) => {
    if $input != $len {
      return Err(DecipherError::InvalidFinalBlockLength);
    }
  };
}

fn is_valid_gcm_tag_length(tag_len: usize) -> bool {
  tag_len == 4 || tag_len == 8 || (12..=16).contains(&tag_len)
}

impl Decipher {
  fn new(
    algorithm_name: &str,
    key: &[u8],
    iv: &[u8],
    auth_tag_length: Option<usize>,
  ) -> Result<Self, DecipherError> {
    use Decipher::*;
    Ok(match algorithm_name {
      "aes-128-cbc" => {
        if key.len() != 16 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes128Cbc(Box::new(cbc::Decryptor::new(key.into(), iv.into())))
      }
      "aes-128-ecb" => {
        if key.len() != 16 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if !iv.is_empty() {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes128Ecb(Box::new(ecb::Decryptor::new(key.into())))
      }
      "aes-192-ecb" => {
        if key.len() != 24 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if !iv.is_empty() {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes192Ecb(Box::new(ecb::Decryptor::new(key.into())))
      }
      "aes-256-ecb" => {
        if key.len() != 32 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if !iv.is_empty() {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes256Ecb(Box::new(ecb::Decryptor::new(key.into())))
      }
      "aes-128-gcm" => {
        if key.len() != aes::Aes128::key_size() {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.is_empty() {
          return Err(DecipherError::InvalidInitializationVector);
        }

        if let Some(tag_len) = auth_tag_length
          && !is_valid_gcm_tag_length(tag_len)
        {
          return Err(DecipherError::InvalidAuthTag(tag_len));
        }

        let decipher =
          aead_gcm_stream::AesGcm::<aes::Aes128>::new(key.into(), iv);

        Aes128Gcm(Box::new(decipher), auth_tag_length)
      }
      "aes-256-gcm" => {
        if key.len() != aes::Aes256::key_size() {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.is_empty() {
          return Err(DecipherError::InvalidInitializationVector);
        }

        if let Some(tag_len) = auth_tag_length
          && !is_valid_gcm_tag_length(tag_len)
        {
          return Err(DecipherError::InvalidAuthTag(tag_len));
        }

        let decipher =
          aead_gcm_stream::AesGcm::<aes::Aes256>::new(key.into(), iv);

        Aes256Gcm(Box::new(decipher), auth_tag_length)
      }
      "aes256" | "aes-256-cbc" => {
        if key.len() != 32 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(DecipherError::InvalidInitializationVector);
        }

        Aes256Cbc(Box::new(cbc::Decryptor::new(key.into(), iv.into())))
      }
      "aes-256-ctr" => {
        if key.len() != 32 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes256Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
      }
      "aes-192-ctr" => {
        if key.len() != 24 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes192Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
      }
      "aes-128-ctr" => {
        if key.len() != 16 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        Aes128Ctr(Box::new(ctr::Ctr128BE::new(key.into(), iv.into())))
      }
      "des-ede3-cbc" => {
        if key.len() != 24 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 8 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        DesEde3Cbc(Box::new(cbc::Decryptor::new(key.into(), iv.into())))
      }
      "chacha20" => {
        if key.len() != 32 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 16 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        ChaCha20(Box::new(ChaCha20Cipher::new(key, iv)))
      }
      "chacha20-poly1305" => {
        if key.len() != 32 {
          return Err(DecipherError::InvalidKeyLength);
        }
        if iv.len() != 12 {
          return Err(DecipherError::InvalidInitializationVector);
        }
        let tag_len = auth_tag_length.unwrap_or(16);
        if !is_valid_chacha20_poly1305_tag_length(tag_len) {
          return Err(DecipherError::InvalidAuthTag(tag_len));
        }
        ChaCha20Poly1305(
          Box::new(
            ChaCha20Poly1305Cipher::new(key, iv, tag_len, false).map_err(
              |e| match e {
                CipherInitError::ContextAllocation => {
                  panic!("Failed to allocate EVP_CIPHER_CTX")
                }
                CipherInitError::InitFailed => DecipherError::InvalidKeyLength,
              },
            )?,
          ),
          auth_tag_length,
        )
      }
      _ => {
        return Err(DecipherError::UnknownCipher(algorithm_name.to_string()));
      }
    })
  }

  fn validate_auth_tag(&self, length: usize) -> Result<(), DecipherError> {
    match self {
      Decipher::Aes128Gcm(_, Some(tag_len))
      | Decipher::Aes256Gcm(_, Some(tag_len))
        if *tag_len != length =>
      {
        return Err(DecipherError::InvalidAuthTag(length));
      }
      Decipher::Aes128Gcm(_, None) | Decipher::Aes256Gcm(_, None)
        if !is_valid_gcm_tag_length(length) =>
      {
        return Err(DecipherError::InvalidAuthTag(length));
      }
      Decipher::ChaCha20Poly1305(_, Some(tag_len)) if *tag_len != length => {
        return Err(DecipherError::InvalidAuthTag(length));
      }
      Decipher::ChaCha20Poly1305(_, None) if length != 16 => {
        // Default tag length is 16; reject anything else
        return Err(DecipherError::InvalidAuthTag(length));
      }
      _ => {}
    }
    Ok(())
  }

  fn set_aad(&mut self, aad: &[u8]) {
    use Decipher::*;
    match self {
      Aes128Gcm(decipher, _) => {
        decipher.set_aad(aad);
      }
      Aes256Gcm(decipher, _) => {
        decipher.set_aad(aad);
      }
      ChaCha20Poly1305(decipher, _) => {
        decipher.set_aad(aad);
      }
      _ => {}
    }
  }

  /// decrypt decrypts the data in the middle of the input.
  fn decrypt(&mut self, input: &[u8], output: &mut [u8]) {
    use Decipher::*;
    match self {
      Aes128Cbc(decryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          decryptor.decrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes128Ecb(decryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          decryptor.decrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes192Ecb(decryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          decryptor.decrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes256Ecb(decryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          decryptor.decrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes128Gcm(decipher, _) => {
        output[..input.len()].copy_from_slice(input);
        decipher.decrypt(output);
      }
      Aes256Gcm(decipher, _) => {
        output[..input.len()].copy_from_slice(input);
        decipher.decrypt(output);
      }
      Aes256Cbc(decryptor) => {
        assert!(input.len().is_multiple_of(16));
        for (input, output) in input.chunks(16).zip(output.chunks_mut(16)) {
          decryptor.decrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      Aes256Ctr(decryptor) => {
        decryptor.apply_keystream_b2b(input, output).unwrap();
      }
      Aes192Ctr(decryptor) => {
        decryptor.apply_keystream_b2b(input, output).unwrap();
      }
      Aes128Ctr(decryptor) => {
        decryptor.apply_keystream_b2b(input, output).unwrap();
      }
      DesEde3Cbc(decryptor) => {
        assert!(input.len().is_multiple_of(8));
        for (input, output) in input.chunks(8).zip(output.chunks_mut(8)) {
          decryptor.decrypt_block_b2b_mut(input.into(), output.into());
        }
      }
      ChaCha20(decipher) => {
        decipher.apply_keystream(input, output);
      }
      ChaCha20Poly1305(decipher, _) => {
        decipher.decrypt(input, output);
      }
    }
  }

  /// r#final decrypts the last block of the input data.
  fn r#final(
    self,
    auto_pad: bool,
    input: &[u8],
    output: &mut [u8],
    auth_tag: &[u8],
  ) -> Result<(), DecipherError> {
    use Decipher::*;

    if input.is_empty()
      && !matches!(
        self,
        Aes128Ecb(..)
          | Aes192Ecb(..)
          | Aes256Ecb(..)
          | Aes128Gcm(..)
          | Aes256Gcm(..)
          | ChaCha20Poly1305(..)
      )
    {
      return Ok(());
    }

    match (self, auto_pad) {
      (Aes128Cbc(decryptor), true) => {
        assert_block_len!(input.len(), 16);
        let _ = (*decryptor)
          .decrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| DecipherError::CannotUnpadInputData)?;
        Ok(())
      }
      (Aes128Cbc(mut decryptor), false) => {
        if !input.is_empty() {
          assert_block_len!(input.len(), 16);
          decryptor.decrypt_block_b2b_mut(
            GenericArray::from_slice(input),
            GenericArray::from_mut_slice(output),
          );
        }
        Ok(())
      }
      (Aes128Ecb(decryptor), true) => {
        assert_block_len!(input.len(), 16);
        let _ = (*decryptor)
          .decrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| DecipherError::CannotUnpadInputData)?;
        Ok(())
      }
      (Aes128Ecb(mut decryptor), false) => {
        if !input.is_empty() {
          assert_block_len!(input.len(), 16);
          decryptor.decrypt_block_b2b_mut(
            GenericArray::from_slice(input),
            GenericArray::from_mut_slice(output),
          );
        }
        Ok(())
      }
      (Aes192Ecb(decryptor), true) => {
        assert_block_len!(input.len(), 16);
        let _ = (*decryptor)
          .decrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| DecipherError::CannotUnpadInputData)?;
        Ok(())
      }
      (Aes192Ecb(mut decryptor), false) => {
        if !input.is_empty() {
          assert_block_len!(input.len(), 16);
          decryptor.decrypt_block_b2b_mut(
            GenericArray::from_slice(input),
            GenericArray::from_mut_slice(output),
          );
        }
        Ok(())
      }
      (Aes256Ecb(decryptor), true) => {
        assert_block_len!(input.len(), 16);
        let _ = (*decryptor)
          .decrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| DecipherError::CannotUnpadInputData)?;
        Ok(())
      }
      (Aes256Ecb(mut decryptor), false) => {
        if !input.is_empty() {
          assert_block_len!(input.len(), 16);
          decryptor.decrypt_block_b2b_mut(
            GenericArray::from_slice(input),
            GenericArray::from_mut_slice(output),
          );
        }
        Ok(())
      }
      (Aes128Gcm(decipher, auth_tag_length), _) => {
        let tag = decipher.finish();
        let tag_slice = tag.as_slice();
        let truncated_tag = if let Some(len) = auth_tag_length {
          &tag_slice[..len]
        } else {
          tag_slice
        };
        if truncated_tag.ct_eq(auth_tag).into() {
          Ok(())
        } else {
          Err(DecipherError::DataAuthenticationFailed)
        }
      }
      (Aes256Gcm(decipher, auth_tag_length), _) => {
        let tag = decipher.finish();
        let tag_slice = tag.as_slice();
        let truncated_tag = if let Some(len) = auth_tag_length {
          &tag_slice[..len]
        } else {
          tag_slice
        };
        if truncated_tag.ct_eq(auth_tag).into() {
          Ok(())
        } else {
          Err(DecipherError::DataAuthenticationFailed)
        }
      }
      (ChaCha20Poly1305(decipher, _), _) => {
        if auth_tag.is_empty() {
          return Err(DecipherError::DataAuthenticationFailed);
        }
        if decipher.verify_tag(auth_tag) {
          Ok(())
        } else {
          Err(DecipherError::DataAuthenticationFailed)
        }
      }
      (Aes256Cbc(decryptor), true) => {
        assert_block_len!(input.len(), 16);
        let _ = (*decryptor)
          .decrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| DecipherError::CannotUnpadInputData)?;
        Ok(())
      }
      (Aes256Cbc(mut decryptor), false) => {
        if !input.is_empty() {
          assert_block_len!(input.len(), 16);
          decryptor.decrypt_block_b2b_mut(
            GenericArray::from_slice(input),
            GenericArray::from_mut_slice(output),
          );
        }
        Ok(())
      }
      (Aes256Ctr(mut decryptor), _) => {
        decryptor.apply_keystream_b2b(input, output).unwrap();
        Ok(())
      }
      (Aes192Ctr(mut decryptor), _) => {
        decryptor.apply_keystream_b2b(input, output).unwrap();
        Ok(())
      }
      (Aes128Ctr(mut decryptor), _) => {
        decryptor.apply_keystream_b2b(input, output).unwrap();
        Ok(())
      }
      (ChaCha20(mut decipher), _) => {
        decipher.apply_keystream(input, output);
        Ok(())
      }
      (DesEde3Cbc(decryptor), true) => {
        assert_block_len!(input.len(), 8);
        let _ = (*decryptor)
          .decrypt_padded_b2b_mut::<Pkcs7>(input, output)
          .map_err(|_| DecipherError::CannotUnpadInputData)?;
        Ok(())
      }
      (DesEde3Cbc(mut decryptor), false) => {
        if !input.is_empty() {
          assert_block_len!(input.len(), 8);
          decryptor.decrypt_block_b2b_mut(
            GenericArray::from_slice(input),
            GenericArray::from_mut_slice(output),
          );
        }
        Ok(())
      }
    }
  }
}