@jscrypto/classic 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -3
- package/dist/ciphers/aes.js +1 -1
- package/dist/ciphers/aes.js.map +1 -1
- package/dist/ciphers/rc4.js +2 -2
- package/dist/ciphers/rc4.js.map +1 -1
- package/dist/ciphers/triple-des.js +1 -1
- package/dist/ciphers/triple-des.js.map +1 -1
- package/dist/index.cjs +330 -49
- package/dist/index.cjs.map +3 -3
- package/dist/index.mjs +312 -27
- package/dist/index.mjs.map +3 -3
- package/dist/jscrypto-classic.iife.js +415 -84
- package/dist/jscrypto-classic.iife.js.map +3 -3
- package/dist/jscrypto-classic.iife.min.js +2 -2
- package/dist/jscrypto-classic.iife.min.js.map +3 -3
- package/dist/jscrypto-classic.umd.js +415 -84
- package/dist/jscrypto-classic.umd.js.map +3 -3
- package/dist/jscrypto-classic.umd.min.js +2 -2
- package/dist/jscrypto-classic.umd.min.js.map +3 -3
- package/dist/kdfs/evpkdf.js +3 -2
- package/dist/kdfs/evpkdf.js.map +1 -1
- package/dist/kdfs/pbkdf2.js +1 -1
- package/dist/kdfs/pbkdf2.js.map +1 -1
- package/dist/modes/cbc.js +4 -4
- package/dist/modes/cbc.js.map +1 -1
- package/dist/modes/cfb.js +2 -2
- package/dist/modes/cfb.js.map +1 -1
- package/dist/modes/ctr.js +2 -2
- package/dist/modes/ctr.js.map +1 -1
- package/dist/modes/ecb.js +4 -4
- package/dist/modes/ecb.js.map +1 -1
- package/dist/modes/gcm.d.ts +1 -1
- package/dist/modes/gcm.d.ts.map +1 -1
- package/dist/modes/gcm.js +289 -4
- package/dist/modes/gcm.js.map +1 -1
- package/dist/modes/ofb.js +2 -2
- package/dist/modes/ofb.js.map +1 -1
- package/dist/paddings/iso10126.js +2 -1
- package/dist/paddings/iso10126.js.map +1 -1
- package/package.json +11 -2
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @jscrypto/classic v0.
|
|
2
|
+
* @jscrypto/classic v0.2.0
|
|
3
3
|
* Copyright 2026 Chen, Yi-Cyuan
|
|
4
4
|
* Released under the MIT license
|
|
5
5
|
*/
|
|
@@ -135,7 +135,7 @@ var aes = {
|
|
|
135
135
|
}
|
|
136
136
|
};
|
|
137
137
|
function createAesCipher(key) {
|
|
138
|
-
if (
|
|
138
|
+
if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
|
|
139
139
|
throw new Error("AES key must be 128, 192, or 256 bits.");
|
|
140
140
|
}
|
|
141
141
|
return createCryptoJsBlockCipher({
|
|
@@ -212,10 +212,10 @@ function createRc4Transform(key, drop = 0) {
|
|
|
212
212
|
assertNotFinalized(finalized);
|
|
213
213
|
return processInput(input);
|
|
214
214
|
},
|
|
215
|
-
finalize(input = new Uint8Array()) {
|
|
215
|
+
finalize(input = new Uint8Array(0)) {
|
|
216
216
|
assertNotFinalized(finalized);
|
|
217
217
|
finalized = true;
|
|
218
|
-
return input.length === 0 ? new Uint8Array() : processInput(input);
|
|
218
|
+
return input.length === 0 ? new Uint8Array(0) : processInput(input);
|
|
219
219
|
}
|
|
220
220
|
};
|
|
221
221
|
}
|
|
@@ -280,7 +280,7 @@ var tripleDes = {
|
|
|
280
280
|
}
|
|
281
281
|
};
|
|
282
282
|
function createTripleDesCipher(key) {
|
|
283
|
-
if (
|
|
283
|
+
if (key.length !== 16 && key.length !== 24) {
|
|
284
284
|
throw new Error("Triple DES key must be 128 or 192 bits.");
|
|
285
285
|
}
|
|
286
286
|
return createCryptoJsBlockCipher({
|
|
@@ -342,13 +342,14 @@ var evpKdf = {
|
|
|
342
342
|
}
|
|
343
343
|
};
|
|
344
344
|
function deriveEvpKdf(params) {
|
|
345
|
+
var _a;
|
|
345
346
|
assertPositiveInteger(params.length, "EvpKDF length");
|
|
346
347
|
if (params.iterations !== void 0) {
|
|
347
348
|
assertPositiveInteger(params.iterations, "EvpKDF iterations");
|
|
348
349
|
}
|
|
349
350
|
const options = {
|
|
350
351
|
keySize: params.length / 4,
|
|
351
|
-
iterations: params.iterations
|
|
352
|
+
iterations: (_a = params.iterations) != null ? _a : 1
|
|
352
353
|
};
|
|
353
354
|
const hasher = getHasher(params.hash);
|
|
354
355
|
if (hasher) {
|
|
@@ -371,7 +372,7 @@ function getHasher(hash) {
|
|
|
371
372
|
return hasher;
|
|
372
373
|
}
|
|
373
374
|
function normalizeHashName(hash) {
|
|
374
|
-
return hash.
|
|
375
|
+
return hash.replace(/-/g, "").toUpperCase();
|
|
375
376
|
}
|
|
376
377
|
function assertPositiveInteger(value, label) {
|
|
377
378
|
if (!Number.isInteger(value) || value <= 0) {
|
|
@@ -415,7 +416,7 @@ function getHasher2(hash) {
|
|
|
415
416
|
return hasher;
|
|
416
417
|
}
|
|
417
418
|
function normalizeHashName2(hash) {
|
|
418
|
-
return hash.
|
|
419
|
+
return hash.replace(/-/g, "").toUpperCase();
|
|
419
420
|
}
|
|
420
421
|
function assertPositiveInteger2(value, label) {
|
|
421
422
|
if (!Number.isInteger(value) || value <= 0) {
|
|
@@ -452,8 +453,8 @@ function createCbcEncryptor(cipher, iv) {
|
|
|
452
453
|
}
|
|
453
454
|
return output;
|
|
454
455
|
},
|
|
455
|
-
finalize(input = new Uint8Array()) {
|
|
456
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
456
|
+
finalize(input = new Uint8Array(0)) {
|
|
457
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
457
458
|
}
|
|
458
459
|
};
|
|
459
460
|
}
|
|
@@ -471,8 +472,8 @@ function createCbcDecryptor(cipher, iv) {
|
|
|
471
472
|
}
|
|
472
473
|
return output;
|
|
473
474
|
},
|
|
474
|
-
finalize(input = new Uint8Array()) {
|
|
475
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
475
|
+
finalize(input = new Uint8Array(0)) {
|
|
476
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
476
477
|
}
|
|
477
478
|
};
|
|
478
479
|
}
|
|
@@ -526,8 +527,8 @@ function createCfbTransform(cipher, iv, encrypting) {
|
|
|
526
527
|
}
|
|
527
528
|
return output;
|
|
528
529
|
},
|
|
529
|
-
finalize(input = new Uint8Array()) {
|
|
530
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
530
|
+
finalize(input = new Uint8Array(0)) {
|
|
531
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
531
532
|
}
|
|
532
533
|
};
|
|
533
534
|
}
|
|
@@ -565,8 +566,8 @@ function createCtrTransform(cipher, iv) {
|
|
|
565
566
|
}
|
|
566
567
|
return output;
|
|
567
568
|
},
|
|
568
|
-
finalize(input = new Uint8Array()) {
|
|
569
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
569
|
+
finalize(input = new Uint8Array(0)) {
|
|
570
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
570
571
|
}
|
|
571
572
|
};
|
|
572
573
|
}
|
|
@@ -602,8 +603,8 @@ function createEcbEncryptor(cipher) {
|
|
|
602
603
|
}
|
|
603
604
|
return output;
|
|
604
605
|
},
|
|
605
|
-
finalize(input = new Uint8Array()) {
|
|
606
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
606
|
+
finalize(input = new Uint8Array(0)) {
|
|
607
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
607
608
|
}
|
|
608
609
|
};
|
|
609
610
|
}
|
|
@@ -617,8 +618,8 @@ function createEcbDecryptor(cipher) {
|
|
|
617
618
|
}
|
|
618
619
|
return output;
|
|
619
620
|
},
|
|
620
|
-
finalize(input = new Uint8Array()) {
|
|
621
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
621
|
+
finalize(input = new Uint8Array(0)) {
|
|
622
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
622
623
|
}
|
|
623
624
|
};
|
|
624
625
|
}
|
|
@@ -627,32 +628,311 @@ function assertBlockMultiple2(blockSize, input) {
|
|
|
627
628
|
}
|
|
628
629
|
|
|
629
630
|
// packages/classic/src/modes/gcm.ts
|
|
631
|
+
var import_core6 = require("@jscrypto/core");
|
|
630
632
|
var gcm = {
|
|
631
633
|
kind: "mode",
|
|
632
634
|
name: "GCM",
|
|
633
635
|
aead: true,
|
|
634
636
|
requiredBlockSize: 16,
|
|
635
637
|
requiresPadding: false,
|
|
636
|
-
createEncryptor() {
|
|
637
|
-
|
|
638
|
+
createEncryptor({ cipher, iv, options }) {
|
|
639
|
+
return createGcmEncryptor(cipher, getNonce(iv, options), getAad(options), getTagLength(options));
|
|
638
640
|
},
|
|
639
|
-
createDecryptor() {
|
|
640
|
-
|
|
641
|
+
createDecryptor({ cipher, iv, options }) {
|
|
642
|
+
return createGcmDecryptor(cipher, getNonce(iv, options), getAad(options), getTagLength(options), getTag(options));
|
|
641
643
|
}
|
|
642
644
|
};
|
|
645
|
+
var BLOCK_SIZE = 16;
|
|
646
|
+
var DEFAULT_TAG_LENGTH = 16;
|
|
647
|
+
var R_WORD = 3774873600;
|
|
648
|
+
function createGcmEncryptor(cipher, nonce, aad, tagLength) {
|
|
649
|
+
assertGcmCipher(cipher);
|
|
650
|
+
const auth = createGhash(cipher.encryptBlock(new Uint8Array(BLOCK_SIZE)));
|
|
651
|
+
auth.update(aad);
|
|
652
|
+
auth.pad();
|
|
653
|
+
const j0 = createInitialCounter(auth.h, nonce);
|
|
654
|
+
const tagMask = cipher.encryptBlock(j0);
|
|
655
|
+
const counter = j0.slice();
|
|
656
|
+
incrementCounter2(counter);
|
|
657
|
+
const xor = createCounterXor(cipher, counter);
|
|
658
|
+
let ciphertextLength = 0;
|
|
659
|
+
return {
|
|
660
|
+
process(input) {
|
|
661
|
+
const ciphertext = xor(input);
|
|
662
|
+
auth.update(ciphertext);
|
|
663
|
+
ciphertextLength += ciphertext.length;
|
|
664
|
+
return ciphertext;
|
|
665
|
+
},
|
|
666
|
+
finalize(input = new Uint8Array(0)) {
|
|
667
|
+
const ciphertext = input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
668
|
+
const tag = createTag(auth, tagMask, aad.length, ciphertextLength, tagLength);
|
|
669
|
+
return (0, import_core6.concatBytes)(ciphertext, tag);
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
function createGcmDecryptor(cipher, nonce, aad, tagLength, detachedTag) {
|
|
674
|
+
assertGcmCipher(cipher);
|
|
675
|
+
const h = cipher.encryptBlock(new Uint8Array(BLOCK_SIZE));
|
|
676
|
+
const j0 = createInitialCounter(h, nonce);
|
|
677
|
+
const tagMask = cipher.encryptBlock(j0);
|
|
678
|
+
let pending = new Uint8Array(0);
|
|
679
|
+
return {
|
|
680
|
+
process(input) {
|
|
681
|
+
pending = (0, import_core6.concatBytes)(pending, input);
|
|
682
|
+
return new Uint8Array(0);
|
|
683
|
+
},
|
|
684
|
+
finalize(input = new Uint8Array(0)) {
|
|
685
|
+
if (input.length !== 0) {
|
|
686
|
+
this.process(input);
|
|
687
|
+
}
|
|
688
|
+
let ciphertext = pending;
|
|
689
|
+
let tag = detachedTag;
|
|
690
|
+
if (!tag) {
|
|
691
|
+
if (pending.length < tagLength) {
|
|
692
|
+
throw new Error("GCM ciphertext must include an authentication tag.");
|
|
693
|
+
}
|
|
694
|
+
ciphertext = new Uint8Array(pending.subarray(0, pending.length - tagLength));
|
|
695
|
+
tag = new Uint8Array(pending.subarray(pending.length - tagLength));
|
|
696
|
+
}
|
|
697
|
+
const auth = createGhash(h);
|
|
698
|
+
auth.update(aad);
|
|
699
|
+
auth.pad();
|
|
700
|
+
auth.update(ciphertext);
|
|
701
|
+
const expectedTag = createTag(auth, tagMask, aad.length, ciphertext.length, tagLength);
|
|
702
|
+
if (!(0, import_core6.equalBytes)(expectedTag, tag)) {
|
|
703
|
+
throw new Error("GCM authentication failed.");
|
|
704
|
+
}
|
|
705
|
+
const counter = j0.slice();
|
|
706
|
+
incrementCounter2(counter);
|
|
707
|
+
const plaintext = createCounterXor(cipher, counter)(ciphertext);
|
|
708
|
+
pending = new Uint8Array(0);
|
|
709
|
+
return plaintext;
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
function createGhash(h) {
|
|
714
|
+
const hValue = blockToWords(h);
|
|
715
|
+
let state = createZeroWords();
|
|
716
|
+
let pending = new Uint8Array(0);
|
|
717
|
+
function updateBlock(block) {
|
|
718
|
+
state = multiplyGf128(xorWords(state, blockToWords(padBlock(block))), hValue);
|
|
719
|
+
}
|
|
720
|
+
return {
|
|
721
|
+
h,
|
|
722
|
+
update(input) {
|
|
723
|
+
const data = (0, import_core6.concatBytes)(pending, input);
|
|
724
|
+
const processLength = data.length - data.length % BLOCK_SIZE;
|
|
725
|
+
for (let offset = 0; offset < processLength; offset += BLOCK_SIZE) {
|
|
726
|
+
updateBlock(data.subarray(offset, offset + BLOCK_SIZE));
|
|
727
|
+
}
|
|
728
|
+
pending = data.slice(processLength);
|
|
729
|
+
},
|
|
730
|
+
pad() {
|
|
731
|
+
if (pending.length !== 0) {
|
|
732
|
+
updateBlock(pending);
|
|
733
|
+
pending = new Uint8Array(0);
|
|
734
|
+
}
|
|
735
|
+
},
|
|
736
|
+
digest(aadLength, ciphertextLength) {
|
|
737
|
+
this.pad();
|
|
738
|
+
updateBlock(createLengthBlock(aadLength, ciphertextLength));
|
|
739
|
+
return wordsToBlock(state);
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
function createInitialCounter(h, nonce) {
|
|
744
|
+
if (nonce.length === 0) {
|
|
745
|
+
throw new Error("GCM mode requires a nonce.");
|
|
746
|
+
}
|
|
747
|
+
if (nonce.length === 12) {
|
|
748
|
+
const j0 = new Uint8Array(BLOCK_SIZE);
|
|
749
|
+
j0.set(nonce);
|
|
750
|
+
j0[15] = 1;
|
|
751
|
+
return j0;
|
|
752
|
+
}
|
|
753
|
+
const auth = createGhash(h);
|
|
754
|
+
auth.update(nonce);
|
|
755
|
+
return auth.digest(0, nonce.length);
|
|
756
|
+
}
|
|
757
|
+
function createCounterXor(cipher, counter) {
|
|
758
|
+
let keystream = new Uint8Array(0);
|
|
759
|
+
let position = BLOCK_SIZE;
|
|
760
|
+
return (input) => {
|
|
761
|
+
const output = new Uint8Array(input.length);
|
|
762
|
+
for (let i = 0; i < input.length; i++) {
|
|
763
|
+
if (position === BLOCK_SIZE) {
|
|
764
|
+
keystream = cipher.encryptBlock(counter);
|
|
765
|
+
incrementCounter2(counter);
|
|
766
|
+
position = 0;
|
|
767
|
+
}
|
|
768
|
+
output[i] = input[i] ^ keystream[position];
|
|
769
|
+
position++;
|
|
770
|
+
}
|
|
771
|
+
return output;
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
function createTag(auth, tagMask, aadLength, ciphertextLength, tagLength) {
|
|
775
|
+
const tag = auth.digest(aadLength, ciphertextLength);
|
|
776
|
+
for (let i = 0; i < tag.length; i++) {
|
|
777
|
+
tag[i] ^= tagMask[i];
|
|
778
|
+
}
|
|
779
|
+
return tag.subarray(0, tagLength);
|
|
780
|
+
}
|
|
781
|
+
function createZeroWords() {
|
|
782
|
+
return [0, 0, 0, 0];
|
|
783
|
+
}
|
|
784
|
+
function multiplyGf128(x, y) {
|
|
785
|
+
const z = createZeroWords();
|
|
786
|
+
const v = [y[0], y[1], y[2], y[3]];
|
|
787
|
+
for (let i = 0; i < 128; i++) {
|
|
788
|
+
if ((x[i >>> 5] & 2147483648 >>> (i & 31)) !== 0) {
|
|
789
|
+
xorWordsInPlace(z, v);
|
|
790
|
+
}
|
|
791
|
+
shiftRightAndReduce(v);
|
|
792
|
+
}
|
|
793
|
+
return z;
|
|
794
|
+
}
|
|
795
|
+
function shiftRightAndReduce(words) {
|
|
796
|
+
const lsb = words[3] & 1;
|
|
797
|
+
words[3] = (words[3] >>> 1 | (words[2] & 1) << 31) >>> 0;
|
|
798
|
+
words[2] = (words[2] >>> 1 | (words[1] & 1) << 31) >>> 0;
|
|
799
|
+
words[1] = (words[1] >>> 1 | (words[0] & 1) << 31) >>> 0;
|
|
800
|
+
words[0] >>>= 1;
|
|
801
|
+
if (lsb) {
|
|
802
|
+
words[0] = (words[0] ^ R_WORD) >>> 0;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
function xorWords(left, right) {
|
|
806
|
+
return [
|
|
807
|
+
(left[0] ^ right[0]) >>> 0,
|
|
808
|
+
(left[1] ^ right[1]) >>> 0,
|
|
809
|
+
(left[2] ^ right[2]) >>> 0,
|
|
810
|
+
(left[3] ^ right[3]) >>> 0
|
|
811
|
+
];
|
|
812
|
+
}
|
|
813
|
+
function xorWordsInPlace(left, right) {
|
|
814
|
+
left[0] = (left[0] ^ right[0]) >>> 0;
|
|
815
|
+
left[1] = (left[1] ^ right[1]) >>> 0;
|
|
816
|
+
left[2] = (left[2] ^ right[2]) >>> 0;
|
|
817
|
+
left[3] = (left[3] ^ right[3]) >>> 0;
|
|
818
|
+
}
|
|
819
|
+
function incrementCounter2(counter) {
|
|
820
|
+
const value = (counter[12] << 24 >>> 0 | counter[13] << 16 | counter[14] << 8 | counter[15]) + 1 >>> 0;
|
|
821
|
+
counter[12] = value >>> 24;
|
|
822
|
+
counter[13] = value >>> 16;
|
|
823
|
+
counter[14] = value >>> 8;
|
|
824
|
+
counter[15] = value;
|
|
825
|
+
}
|
|
826
|
+
function padBlock(input) {
|
|
827
|
+
if (input.length === BLOCK_SIZE) {
|
|
828
|
+
return input;
|
|
829
|
+
}
|
|
830
|
+
const output = new Uint8Array(BLOCK_SIZE);
|
|
831
|
+
output.set(input);
|
|
832
|
+
return output;
|
|
833
|
+
}
|
|
834
|
+
function createLengthBlock(aadLength, ciphertextLength) {
|
|
835
|
+
const output = new Uint8Array(BLOCK_SIZE);
|
|
836
|
+
writeBitLength64BE(aadLength, output, 0);
|
|
837
|
+
writeBitLength64BE(ciphertextLength, output, 8);
|
|
838
|
+
return output;
|
|
839
|
+
}
|
|
840
|
+
function writeBitLength64BE(byteLength, output, offset) {
|
|
841
|
+
const high = Math.floor(byteLength / 536870912);
|
|
842
|
+
const low = byteLength % 536870912 * 8;
|
|
843
|
+
writeUint32BE(high, output, offset);
|
|
844
|
+
writeUint32BE(low, output, offset + 4);
|
|
845
|
+
}
|
|
846
|
+
function blockToWords(input) {
|
|
847
|
+
return [
|
|
848
|
+
readUint32BE(input, 0),
|
|
849
|
+
readUint32BE(input, 4),
|
|
850
|
+
readUint32BE(input, 8),
|
|
851
|
+
readUint32BE(input, 12)
|
|
852
|
+
];
|
|
853
|
+
}
|
|
854
|
+
function wordsToBlock(words) {
|
|
855
|
+
const output = new Uint8Array(BLOCK_SIZE);
|
|
856
|
+
writeUint32BE(words[0], output, 0);
|
|
857
|
+
writeUint32BE(words[1], output, 4);
|
|
858
|
+
writeUint32BE(words[2], output, 8);
|
|
859
|
+
writeUint32BE(words[3], output, 12);
|
|
860
|
+
return output;
|
|
861
|
+
}
|
|
862
|
+
function readUint32BE(input, offset) {
|
|
863
|
+
return (input[offset] << 24 >>> 0 | input[offset + 1] << 16 | input[offset + 2] << 8 | input[offset + 3]) >>> 0;
|
|
864
|
+
}
|
|
865
|
+
function writeUint32BE(value, output, offset) {
|
|
866
|
+
output[offset] = value >>> 24;
|
|
867
|
+
output[offset + 1] = value >>> 16;
|
|
868
|
+
output[offset + 2] = value >>> 8;
|
|
869
|
+
output[offset + 3] = value;
|
|
870
|
+
}
|
|
871
|
+
function getNonce(iv, options) {
|
|
872
|
+
const nonce = getOptions(options).nonce;
|
|
873
|
+
if (nonce !== void 0) {
|
|
874
|
+
(0, import_core6.assertBytes)(nonce, "GCM nonce");
|
|
875
|
+
if (iv !== void 0) {
|
|
876
|
+
throw new Error("GCM mode accepts either iv or nonce, not both.");
|
|
877
|
+
}
|
|
878
|
+
return nonce;
|
|
879
|
+
}
|
|
880
|
+
if (!iv) {
|
|
881
|
+
throw new Error("GCM mode requires an IV/nonce.");
|
|
882
|
+
}
|
|
883
|
+
return iv;
|
|
884
|
+
}
|
|
885
|
+
function getAad(options) {
|
|
886
|
+
const aad = getOptions(options).aad;
|
|
887
|
+
if (aad === void 0) {
|
|
888
|
+
return new Uint8Array(0);
|
|
889
|
+
}
|
|
890
|
+
(0, import_core6.assertBytes)(aad, "GCM aad");
|
|
891
|
+
return aad;
|
|
892
|
+
}
|
|
893
|
+
function getTag(options) {
|
|
894
|
+
const tag = getOptions(options).tag;
|
|
895
|
+
if (tag === void 0) {
|
|
896
|
+
return void 0;
|
|
897
|
+
}
|
|
898
|
+
(0, import_core6.assertBytes)(tag, "GCM tag");
|
|
899
|
+
return tag;
|
|
900
|
+
}
|
|
901
|
+
function getTagLength(options) {
|
|
902
|
+
const tag = getTag(options);
|
|
903
|
+
if (tag) {
|
|
904
|
+
return tag.length;
|
|
905
|
+
}
|
|
906
|
+
const tagLength = getOptions(options).tagLength;
|
|
907
|
+
if (tagLength === void 0) {
|
|
908
|
+
return DEFAULT_TAG_LENGTH;
|
|
909
|
+
}
|
|
910
|
+
if (typeof tagLength !== "number" || !Number.isInteger(tagLength) || tagLength < 4 || tagLength > BLOCK_SIZE) {
|
|
911
|
+
throw new RangeError("GCM tagLength must be an integer between 4 and 16 bytes.");
|
|
912
|
+
}
|
|
913
|
+
return tagLength;
|
|
914
|
+
}
|
|
915
|
+
function getOptions(options) {
|
|
916
|
+
return typeof options === "object" && options !== null ? options : {};
|
|
917
|
+
}
|
|
918
|
+
function assertGcmCipher(cipher) {
|
|
919
|
+
if (cipher.blockSize !== BLOCK_SIZE) {
|
|
920
|
+
throw new Error("GCM mode requires a 128-bit block cipher.");
|
|
921
|
+
}
|
|
922
|
+
}
|
|
643
923
|
|
|
644
924
|
// packages/classic/src/modes/ofb.ts
|
|
645
|
-
var
|
|
925
|
+
var import_core7 = require("@jscrypto/core");
|
|
646
926
|
var ofb = {
|
|
647
927
|
kind: "mode",
|
|
648
928
|
name: "OFB",
|
|
649
929
|
requiresPadding: false,
|
|
650
930
|
createEncryptor({ cipher, iv }) {
|
|
651
|
-
(0,
|
|
931
|
+
(0, import_core7.assertIv)(cipher.blockSize, iv, "OFB");
|
|
652
932
|
return createOfbTransform(cipher, iv);
|
|
653
933
|
},
|
|
654
934
|
createDecryptor({ cipher, iv }) {
|
|
655
|
-
(0,
|
|
935
|
+
(0, import_core7.assertIv)(cipher.blockSize, iv, "OFB");
|
|
656
936
|
return createOfbTransform(cipher, iv);
|
|
657
937
|
}
|
|
658
938
|
};
|
|
@@ -674,14 +954,14 @@ function createOfbTransform(cipher, iv) {
|
|
|
674
954
|
}
|
|
675
955
|
return output;
|
|
676
956
|
},
|
|
677
|
-
finalize(input = new Uint8Array()) {
|
|
678
|
-
return input.length === 0 ? new Uint8Array() : this.process(input);
|
|
957
|
+
finalize(input = new Uint8Array(0)) {
|
|
958
|
+
return input.length === 0 ? new Uint8Array(0) : this.process(input);
|
|
679
959
|
}
|
|
680
960
|
};
|
|
681
961
|
}
|
|
682
962
|
|
|
683
963
|
// packages/classic/src/paddings/ansi-x923.ts
|
|
684
|
-
var
|
|
964
|
+
var import_core8 = require("@jscrypto/core");
|
|
685
965
|
var ansiX923 = {
|
|
686
966
|
kind: "padding",
|
|
687
967
|
name: "AnsiX923",
|
|
@@ -708,17 +988,17 @@ var ansiX923 = {
|
|
|
708
988
|
}
|
|
709
989
|
};
|
|
710
990
|
function assertBlockSize(blockSize) {
|
|
711
|
-
(0,
|
|
991
|
+
(0, import_core8.assertBlockSize)(blockSize, { max: 255 });
|
|
712
992
|
}
|
|
713
993
|
function assertPaddedInput(input, blockSize) {
|
|
714
|
-
(0,
|
|
994
|
+
(0, import_core8.assertPaddedInput)(input, blockSize, "ANSI X9.23", { max: 255 });
|
|
715
995
|
}
|
|
716
996
|
function getPaddingLength(inputLength, blockSize) {
|
|
717
|
-
return (0,
|
|
997
|
+
return (0, import_core8.getBlockPaddingLength)(inputLength, blockSize);
|
|
718
998
|
}
|
|
719
999
|
|
|
720
1000
|
// packages/classic/src/paddings/iso10126.ts
|
|
721
|
-
var
|
|
1001
|
+
var import_core9 = require("@jscrypto/core");
|
|
722
1002
|
var iso10126 = {
|
|
723
1003
|
kind: "padding",
|
|
724
1004
|
name: "Iso10126",
|
|
@@ -741,20 +1021,21 @@ var iso10126 = {
|
|
|
741
1021
|
}
|
|
742
1022
|
};
|
|
743
1023
|
function assertBlockSize2(blockSize) {
|
|
744
|
-
(0,
|
|
1024
|
+
(0, import_core9.assertBlockSize)(blockSize, { max: 255 });
|
|
745
1025
|
}
|
|
746
1026
|
function assertPaddedInput2(input, blockSize) {
|
|
747
|
-
(0,
|
|
1027
|
+
(0, import_core9.assertPaddedInput)(input, blockSize, "ISO 10126", { max: 255 });
|
|
748
1028
|
}
|
|
749
1029
|
function getPaddingLength2(inputLength, blockSize) {
|
|
750
|
-
return (0,
|
|
1030
|
+
return (0, import_core9.getBlockPaddingLength)(inputLength, blockSize);
|
|
751
1031
|
}
|
|
752
1032
|
function fillRandom(bytes) {
|
|
1033
|
+
var _a;
|
|
753
1034
|
if (bytes.length === 0) {
|
|
754
1035
|
return;
|
|
755
1036
|
}
|
|
756
1037
|
const crypto = globalThis;
|
|
757
|
-
crypto.crypto
|
|
1038
|
+
(_a = crypto.crypto) == null ? void 0 : _a.getRandomValues(bytes);
|
|
758
1039
|
if (bytes.some((byte) => byte !== 0)) {
|
|
759
1040
|
return;
|
|
760
1041
|
}
|
|
@@ -764,7 +1045,7 @@ function fillRandom(bytes) {
|
|
|
764
1045
|
}
|
|
765
1046
|
|
|
766
1047
|
// packages/classic/src/paddings/iso97971.ts
|
|
767
|
-
var
|
|
1048
|
+
var import_core10 = require("@jscrypto/core");
|
|
768
1049
|
var iso97971 = {
|
|
769
1050
|
kind: "padding",
|
|
770
1051
|
name: "Iso97971",
|
|
@@ -789,10 +1070,10 @@ var iso97971 = {
|
|
|
789
1070
|
}
|
|
790
1071
|
};
|
|
791
1072
|
function assertBlockSize3(blockSize) {
|
|
792
|
-
(0,
|
|
1073
|
+
(0, import_core10.assertBlockSize)(blockSize);
|
|
793
1074
|
}
|
|
794
1075
|
function assertPaddedInput3(input, blockSize) {
|
|
795
|
-
(0,
|
|
1076
|
+
(0, import_core10.assertPaddedInput)(input, blockSize, "ISO/IEC 9797-1");
|
|
796
1077
|
}
|
|
797
1078
|
function getPaddingLength3(inputLength, blockSize) {
|
|
798
1079
|
const remainder = (inputLength + 1) % blockSize;
|
|
@@ -800,7 +1081,7 @@ function getPaddingLength3(inputLength, blockSize) {
|
|
|
800
1081
|
}
|
|
801
1082
|
|
|
802
1083
|
// packages/classic/src/paddings/none.ts
|
|
803
|
-
var
|
|
1084
|
+
var import_core11 = require("@jscrypto/core");
|
|
804
1085
|
var noPadding = {
|
|
805
1086
|
kind: "padding",
|
|
806
1087
|
name: "NoPadding",
|
|
@@ -814,24 +1095,24 @@ var noPadding = {
|
|
|
814
1095
|
}
|
|
815
1096
|
};
|
|
816
1097
|
function assertMultiple(input, blockSize) {
|
|
817
|
-
(0,
|
|
1098
|
+
(0, import_core11.assertBlockMultiple)(input, blockSize, "NoPadding");
|
|
818
1099
|
}
|
|
819
1100
|
|
|
820
1101
|
// packages/classic/src/paddings/pkcs7.ts
|
|
821
|
-
var
|
|
1102
|
+
var import_core12 = require("@jscrypto/core");
|
|
822
1103
|
var pkcs7 = {
|
|
823
1104
|
kind: "padding",
|
|
824
1105
|
name: "Pkcs7",
|
|
825
1106
|
pad(input, blockSize) {
|
|
826
|
-
(0,
|
|
827
|
-
const paddingLength = (0,
|
|
1107
|
+
(0, import_core12.assertBlockSize)(blockSize, { max: 255 });
|
|
1108
|
+
const paddingLength = (0, import_core12.getBlockPaddingLength)(input.length, blockSize);
|
|
828
1109
|
const output = new Uint8Array(input.length + paddingLength);
|
|
829
1110
|
output.set(input);
|
|
830
1111
|
output.fill(paddingLength, input.length);
|
|
831
1112
|
return output;
|
|
832
1113
|
},
|
|
833
1114
|
unpad(input, blockSize) {
|
|
834
|
-
(0,
|
|
1115
|
+
(0, import_core12.assertPaddedInput)(input, blockSize, "PKCS#7", { max: 255 });
|
|
835
1116
|
const paddingLength = input[input.length - 1];
|
|
836
1117
|
if (paddingLength === 0 || paddingLength > blockSize || paddingLength > input.length) {
|
|
837
1118
|
throw new Error("Invalid PKCS#7 padding.");
|
|
@@ -871,7 +1152,7 @@ var zeroPadding = {
|
|
|
871
1152
|
};
|
|
872
1153
|
|
|
873
1154
|
// packages/classic/src/preset.ts
|
|
874
|
-
var
|
|
1155
|
+
var import_core13 = require("@jscrypto/core");
|
|
875
1156
|
function classicPreset() {
|
|
876
1157
|
return {
|
|
877
1158
|
kind: "preset",
|
|
@@ -903,7 +1184,7 @@ function classicPreset() {
|
|
|
903
1184
|
};
|
|
904
1185
|
}
|
|
905
1186
|
function createClassicRegistry() {
|
|
906
|
-
return (0,
|
|
1187
|
+
return (0, import_core13.createRegistry)().use(classicPreset());
|
|
907
1188
|
}
|
|
908
1189
|
var registry = createClassicRegistry();
|
|
909
1190
|
// Annotate the CommonJS export names for ESM import in node:
|