@substrate-system/keys 0.3.3 → 0.5.1
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 +35 -3
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +4 -4
- package/dist/aes/index.cjs +14 -7
- package/dist/aes/index.cjs.map +2 -2
- package/dist/aes/index.d.ts +11 -3
- package/dist/aes/index.d.ts.map +1 -1
- package/dist/aes/index.js +14 -7
- package/dist/aes/index.js.map +2 -2
- package/dist/aes/index.min.cjs +1 -1
- package/dist/aes/index.min.cjs.map +3 -3
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +3 -3
- package/dist/browser/index.js +341 -116
- package/dist/browser/index.min.js +2 -2
- package/dist/constants.cjs +4 -0
- package/dist/constants.cjs.map +2 -2
- package/dist/constants.d.ts +6 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +4 -0
- package/dist/constants.js.map +2 -2
- package/dist/constants.min.cjs +1 -1
- package/dist/constants.min.cjs.map +3 -3
- package/dist/constants.min.js +1 -1
- package/dist/constants.min.js.map +3 -3
- package/dist/crypto.cjs +9 -7
- package/dist/crypto.cjs.map +2 -2
- package/dist/crypto.d.ts +6 -0
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +9 -8
- package/dist/crypto.js.map +2 -2
- package/dist/crypto.min.cjs +2 -2
- package/dist/crypto.min.cjs.map +2 -2
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +4 -4
- package/dist/ecc/index.cjs +176 -60
- package/dist/ecc/index.cjs.map +2 -2
- package/dist/ecc/index.d.ts +47 -13
- package/dist/ecc/index.d.ts.map +1 -1
- package/dist/ecc/index.js +179 -61
- package/dist/ecc/index.js.map +2 -2
- package/dist/ecc/index.min.cjs +1 -1
- package/dist/ecc/index.min.cjs.map +3 -3
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +4 -4
- package/dist/errors.min.js +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +4 -4
- package/dist/meta.json +33 -26
- package/dist/rsa/index.cjs +15 -4
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts +6 -1
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +15 -5
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs +1 -1
- package/dist/rsa/index.min.cjs.map +3 -3
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +4 -4
- package/dist/types.cjs.map +2 -2
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +2 -2
- package/dist/types.min.cjs +1 -1
- package/dist/types.min.cjs.map +2 -2
- package/dist/types.min.js +1 -1
- package/dist/types.min.js.map +2 -2
- package/dist/util.cjs +6 -4
- package/dist/util.cjs.map +2 -2
- package/dist/util.d.ts +5 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +6 -4
- package/dist/util.js.map +2 -2
- package/dist/util.min.cjs +1 -1
- package/dist/util.min.cjs.map +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +3 -3
package/dist/browser/index.js
CHANGED
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
var rsa_exports = {};
|
|
56
56
|
__export(rsa_exports, {
|
|
57
57
|
AES_GCM: () => AES_GCM,
|
|
58
|
+
AES_GCM_TAG_LENGTH: () => AES_GCM_TAG_LENGTH,
|
|
58
59
|
BASE58_DID_PREFIX: () => BASE58_DID_PREFIX,
|
|
59
60
|
BLS_DID_PREFIX: () => BLS_DID_PREFIX,
|
|
60
61
|
DEFAULT_CHAR_SIZE: () => DEFAULT_CHAR_SIZE,
|
|
@@ -79,6 +80,7 @@
|
|
|
79
80
|
RSA_SIGN_ALGORITHM: () => RSA_SIGN_ALGORITHM,
|
|
80
81
|
RsaKeys: () => RsaKeys,
|
|
81
82
|
SALT_LENGTH: () => SALT_LENGTH,
|
|
83
|
+
WRAP_FORMAT_VERSION: () => WRAP_FORMAT_VERSION,
|
|
82
84
|
X25519_PUBLIC_KEY_LENGTH: () => X25519_PUBLIC_KEY_LENGTH,
|
|
83
85
|
encrypt: () => encrypt2,
|
|
84
86
|
encryptKeyTo: () => encryptKeyTo2,
|
|
@@ -96,8 +98,16 @@
|
|
|
96
98
|
__name(allocUnsafe, "allocUnsafe");
|
|
97
99
|
|
|
98
100
|
// node_modules/uint8arrays/dist/src/util/as-uint8array.js
|
|
101
|
+
function isByteArrayWithArrayBuffer(b) {
|
|
102
|
+
return b?.buffer instanceof ArrayBuffer;
|
|
103
|
+
}
|
|
104
|
+
__name(isByteArrayWithArrayBuffer, "isByteArrayWithArrayBuffer");
|
|
99
105
|
function asUint8Array(buf) {
|
|
100
|
-
|
|
106
|
+
if (isByteArrayWithArrayBuffer(buf)) {
|
|
107
|
+
return buf;
|
|
108
|
+
}
|
|
109
|
+
const b = buf.slice();
|
|
110
|
+
return new Uint8Array(b.buffer, 0, b.byteLength);
|
|
101
111
|
}
|
|
102
112
|
__name(asUint8Array, "asUint8Array");
|
|
103
113
|
|
|
@@ -141,28 +151,57 @@
|
|
|
141
151
|
__name(equals, "equals");
|
|
142
152
|
function coerce(o) {
|
|
143
153
|
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") {
|
|
144
|
-
return o;
|
|
154
|
+
return toArrayBufferBackedArray(o);
|
|
145
155
|
}
|
|
146
156
|
if (o instanceof ArrayBuffer) {
|
|
147
157
|
return new Uint8Array(o);
|
|
148
158
|
}
|
|
149
159
|
if (ArrayBuffer.isView(o)) {
|
|
150
|
-
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
|
|
160
|
+
return toArrayBufferBackedArray(new Uint8Array(o.buffer, o.byteOffset, o.byteLength));
|
|
151
161
|
}
|
|
152
162
|
throw new Error("Unknown type, must be binary type");
|
|
153
163
|
}
|
|
154
164
|
__name(coerce, "coerce");
|
|
155
165
|
function fromString(str) {
|
|
156
|
-
|
|
166
|
+
const output = new Uint8Array(str.length);
|
|
167
|
+
for (let i = 0; i < str.length; i++) {
|
|
168
|
+
output[i] = str.charCodeAt(i);
|
|
169
|
+
}
|
|
170
|
+
return output;
|
|
157
171
|
}
|
|
158
172
|
__name(fromString, "fromString");
|
|
173
|
+
var MAX_ARGUMENTS_LENGTH = 4096;
|
|
159
174
|
function toString(b) {
|
|
160
|
-
|
|
175
|
+
const len = b.length;
|
|
176
|
+
if (len <= MAX_ARGUMENTS_LENGTH) {
|
|
177
|
+
return String.fromCharCode.apply(String, b);
|
|
178
|
+
}
|
|
179
|
+
let res = "";
|
|
180
|
+
let i = 0;
|
|
181
|
+
while (i < len) {
|
|
182
|
+
res += String.fromCharCode.apply(
|
|
183
|
+
String,
|
|
184
|
+
// @ts-expect-error cannot ordinarily apply a Uint8Array
|
|
185
|
+
b.subarray(i, i += MAX_ARGUMENTS_LENGTH)
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return res;
|
|
161
189
|
}
|
|
162
190
|
__name(toString, "toString");
|
|
191
|
+
function isByteArrayWithArrayBuffer2(b) {
|
|
192
|
+
return b?.buffer instanceof ArrayBuffer;
|
|
193
|
+
}
|
|
194
|
+
__name(isByteArrayWithArrayBuffer2, "isByteArrayWithArrayBuffer");
|
|
195
|
+
function toArrayBufferBackedArray(b) {
|
|
196
|
+
if (isByteArrayWithArrayBuffer2(b)) {
|
|
197
|
+
return b;
|
|
198
|
+
}
|
|
199
|
+
return b.slice();
|
|
200
|
+
}
|
|
201
|
+
__name(toArrayBufferBackedArray, "toArrayBufferBackedArray");
|
|
163
202
|
|
|
164
203
|
// node_modules/multiformats/dist/src/vendor/base-x.js
|
|
165
|
-
function base(ALPHABET, name2) {
|
|
204
|
+
function base(ALPHABET, name2, caseInsensitive) {
|
|
166
205
|
if (ALPHABET.length >= 255) {
|
|
167
206
|
throw new TypeError("Alphabet too long");
|
|
168
207
|
}
|
|
@@ -177,6 +216,16 @@
|
|
|
177
216
|
throw new TypeError(x + " is ambiguous");
|
|
178
217
|
}
|
|
179
218
|
BASE_MAP[xc] = i;
|
|
219
|
+
if (caseInsensitive) {
|
|
220
|
+
var xl = x.toLowerCase().charCodeAt(0);
|
|
221
|
+
var xu = x.toUpperCase().charCodeAt(0);
|
|
222
|
+
if (xl !== xc) {
|
|
223
|
+
BASE_MAP[xl] = i;
|
|
224
|
+
}
|
|
225
|
+
if (xu !== xc) {
|
|
226
|
+
BASE_MAP[xu] = i;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
180
229
|
}
|
|
181
230
|
var BASE = ALPHABET.length;
|
|
182
231
|
var LEADER = ALPHABET.charAt(0);
|
|
@@ -251,7 +300,11 @@
|
|
|
251
300
|
var size = (source.length - psz) * FACTOR + 1 >>> 0;
|
|
252
301
|
var b256 = new Uint8Array(size);
|
|
253
302
|
while (source[psz]) {
|
|
254
|
-
var
|
|
303
|
+
var charCode = source.charCodeAt(psz);
|
|
304
|
+
if (charCode > 255) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
var carry = BASE_MAP[charCode];
|
|
255
308
|
if (carry === 255) {
|
|
256
309
|
return;
|
|
257
310
|
}
|
|
@@ -411,8 +464,8 @@
|
|
|
411
464
|
return new Codec(name2, prefix, encode5, decode6);
|
|
412
465
|
}
|
|
413
466
|
__name(from, "from");
|
|
414
|
-
function baseX({ name: name2, prefix, alphabet: alphabet2 }) {
|
|
415
|
-
const { encode: encode5, decode: decode6 } = base_x_default(alphabet2, name2);
|
|
467
|
+
function baseX({ name: name2, prefix, alphabet: alphabet2, caseInsensitive = false }) {
|
|
468
|
+
const { encode: encode5, decode: decode6 } = base_x_default(alphabet2, name2, caseInsensitive);
|
|
416
469
|
return from({
|
|
417
470
|
prefix,
|
|
418
471
|
name: name2,
|
|
@@ -473,16 +526,29 @@
|
|
|
473
526
|
return out;
|
|
474
527
|
}
|
|
475
528
|
__name(encode, "encode");
|
|
476
|
-
function createAlphabetIdx(alphabet2) {
|
|
529
|
+
function createAlphabetIdx(alphabet2, caseInsensitive) {
|
|
477
530
|
const alphabetIdx = {};
|
|
478
531
|
for (let i = 0; i < alphabet2.length; ++i) {
|
|
532
|
+
if (alphabet2[i] === "=") {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
479
535
|
alphabetIdx[alphabet2[i]] = i;
|
|
536
|
+
if (caseInsensitive) {
|
|
537
|
+
const lower = alphabet2[i].toLowerCase();
|
|
538
|
+
const upper = alphabet2[i].toUpperCase();
|
|
539
|
+
if (lower !== alphabet2[i]) {
|
|
540
|
+
alphabetIdx[lower] = i;
|
|
541
|
+
}
|
|
542
|
+
if (upper !== alphabet2[i]) {
|
|
543
|
+
alphabetIdx[upper] = i;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
480
546
|
}
|
|
481
547
|
return alphabetIdx;
|
|
482
548
|
}
|
|
483
549
|
__name(createAlphabetIdx, "createAlphabetIdx");
|
|
484
|
-
function rfc4648({ name: name2, prefix, bitsPerChar, alphabet: alphabet2 }) {
|
|
485
|
-
const alphabetIdx = createAlphabetIdx(alphabet2);
|
|
550
|
+
function rfc4648({ name: name2, prefix, bitsPerChar, alphabet: alphabet2, caseInsensitive = false }) {
|
|
551
|
+
const alphabetIdx = createAlphabetIdx(alphabet2, caseInsensitive);
|
|
486
552
|
return from({
|
|
487
553
|
prefix,
|
|
488
554
|
name: name2,
|
|
@@ -513,13 +579,15 @@
|
|
|
513
579
|
prefix: "f",
|
|
514
580
|
name: "base16",
|
|
515
581
|
alphabet: "0123456789abcdef",
|
|
516
|
-
bitsPerChar: 4
|
|
582
|
+
bitsPerChar: 4,
|
|
583
|
+
caseInsensitive: true
|
|
517
584
|
});
|
|
518
585
|
var base16upper = rfc4648({
|
|
519
586
|
prefix: "F",
|
|
520
587
|
name: "base16upper",
|
|
521
588
|
alphabet: "0123456789ABCDEF",
|
|
522
|
-
bitsPerChar: 4
|
|
589
|
+
bitsPerChar: 4,
|
|
590
|
+
caseInsensitive: true
|
|
523
591
|
});
|
|
524
592
|
|
|
525
593
|
// node_modules/multiformats/dist/src/bases/base2.js
|
|
@@ -599,49 +667,57 @@
|
|
|
599
667
|
prefix: "b",
|
|
600
668
|
name: "base32",
|
|
601
669
|
alphabet: "abcdefghijklmnopqrstuvwxyz234567",
|
|
602
|
-
bitsPerChar: 5
|
|
670
|
+
bitsPerChar: 5,
|
|
671
|
+
caseInsensitive: true
|
|
603
672
|
});
|
|
604
673
|
var base32upper = rfc4648({
|
|
605
674
|
prefix: "B",
|
|
606
675
|
name: "base32upper",
|
|
607
676
|
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
|
|
608
|
-
bitsPerChar: 5
|
|
677
|
+
bitsPerChar: 5,
|
|
678
|
+
caseInsensitive: true
|
|
609
679
|
});
|
|
610
680
|
var base32pad = rfc4648({
|
|
611
681
|
prefix: "c",
|
|
612
682
|
name: "base32pad",
|
|
613
683
|
alphabet: "abcdefghijklmnopqrstuvwxyz234567=",
|
|
614
|
-
bitsPerChar: 5
|
|
684
|
+
bitsPerChar: 5,
|
|
685
|
+
caseInsensitive: true
|
|
615
686
|
});
|
|
616
687
|
var base32padupper = rfc4648({
|
|
617
688
|
prefix: "C",
|
|
618
689
|
name: "base32padupper",
|
|
619
690
|
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
|
|
620
|
-
bitsPerChar: 5
|
|
691
|
+
bitsPerChar: 5,
|
|
692
|
+
caseInsensitive: true
|
|
621
693
|
});
|
|
622
694
|
var base32hex = rfc4648({
|
|
623
695
|
prefix: "v",
|
|
624
696
|
name: "base32hex",
|
|
625
697
|
alphabet: "0123456789abcdefghijklmnopqrstuv",
|
|
626
|
-
bitsPerChar: 5
|
|
698
|
+
bitsPerChar: 5,
|
|
699
|
+
caseInsensitive: true
|
|
627
700
|
});
|
|
628
701
|
var base32hexupper = rfc4648({
|
|
629
702
|
prefix: "V",
|
|
630
703
|
name: "base32hexupper",
|
|
631
704
|
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV",
|
|
632
|
-
bitsPerChar: 5
|
|
705
|
+
bitsPerChar: 5,
|
|
706
|
+
caseInsensitive: true
|
|
633
707
|
});
|
|
634
708
|
var base32hexpad = rfc4648({
|
|
635
709
|
prefix: "t",
|
|
636
710
|
name: "base32hexpad",
|
|
637
711
|
alphabet: "0123456789abcdefghijklmnopqrstuv=",
|
|
638
|
-
bitsPerChar: 5
|
|
712
|
+
bitsPerChar: 5,
|
|
713
|
+
caseInsensitive: true
|
|
639
714
|
});
|
|
640
715
|
var base32hexpadupper = rfc4648({
|
|
641
716
|
prefix: "T",
|
|
642
717
|
name: "base32hexpadupper",
|
|
643
718
|
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV=",
|
|
644
|
-
bitsPerChar: 5
|
|
719
|
+
bitsPerChar: 5,
|
|
720
|
+
caseInsensitive: true
|
|
645
721
|
});
|
|
646
722
|
var base32z = rfc4648({
|
|
647
723
|
prefix: "h",
|
|
@@ -659,12 +735,14 @@
|
|
|
659
735
|
var base36 = baseX({
|
|
660
736
|
prefix: "k",
|
|
661
737
|
name: "base36",
|
|
662
|
-
alphabet: "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
738
|
+
alphabet: "0123456789abcdefghijklmnopqrstuvwxyz",
|
|
739
|
+
caseInsensitive: true
|
|
663
740
|
});
|
|
664
741
|
var base36upper = baseX({
|
|
665
742
|
prefix: "K",
|
|
666
743
|
name: "base36upper",
|
|
667
|
-
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
744
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
745
|
+
caseInsensitive: true
|
|
668
746
|
});
|
|
669
747
|
|
|
670
748
|
// node_modules/multiformats/dist/src/bases/base58.js
|
|
@@ -737,8 +815,8 @@
|
|
|
737
815
|
var identity = from({
|
|
738
816
|
prefix: "\0",
|
|
739
817
|
name: "identity",
|
|
740
|
-
encode: /* @__PURE__ */ __name((buf) => toString(buf), "encode"),
|
|
741
|
-
decode: /* @__PURE__ */ __name((str) => fromString(str), "decode")
|
|
818
|
+
encode: /* @__PURE__ */ __name((buf) => toString(coerce(buf)), "encode"),
|
|
819
|
+
decode: /* @__PURE__ */ __name((str) => coerce(fromString(str)), "decode")
|
|
742
820
|
});
|
|
743
821
|
|
|
744
822
|
// node_modules/multiformats/dist/src/codecs/json.js
|
|
@@ -815,7 +893,14 @@
|
|
|
815
893
|
// node_modules/multiformats/dist/src/varint.js
|
|
816
894
|
function decode4(data, offset = 0) {
|
|
817
895
|
const code2 = varint_default.decode(data, offset);
|
|
818
|
-
|
|
896
|
+
const length2 = varint_default.decode.bytes;
|
|
897
|
+
if (length2 > 9) {
|
|
898
|
+
throw new RangeError("Invalid varint: too long");
|
|
899
|
+
}
|
|
900
|
+
if (length2 > 1 && data[offset + length2 - 1] === 0) {
|
|
901
|
+
throw new RangeError("Invalid varint: not minimally encoded");
|
|
902
|
+
}
|
|
903
|
+
return [code2, length2];
|
|
819
904
|
}
|
|
820
905
|
__name(decode4, "decode");
|
|
821
906
|
function encodeTo(int, target, offset = 0) {
|
|
@@ -874,8 +959,8 @@
|
|
|
874
959
|
constructor(code2, size, digest2, bytes) {
|
|
875
960
|
this.code = code2;
|
|
876
961
|
this.size = size;
|
|
877
|
-
this.digest = digest2;
|
|
878
|
-
this.bytes = bytes;
|
|
962
|
+
this.digest = toArrayBufferBackedArray(digest2);
|
|
963
|
+
this.bytes = toArrayBufferBackedArray(bytes);
|
|
879
964
|
}
|
|
880
965
|
};
|
|
881
966
|
|
|
@@ -1011,8 +1096,8 @@
|
|
|
1011
1096
|
this.code = code2;
|
|
1012
1097
|
this.version = version;
|
|
1013
1098
|
this.multihash = multihash;
|
|
1014
|
-
this.bytes = bytes;
|
|
1015
|
-
this["/"] = bytes;
|
|
1099
|
+
this.bytes = toArrayBufferBackedArray(bytes);
|
|
1100
|
+
this["/"] = this.bytes;
|
|
1016
1101
|
}
|
|
1017
1102
|
/**
|
|
1018
1103
|
* Signalling `cid.asCID === cid` has been replaced with `cid['/'] === cid.bytes`
|
|
@@ -1215,10 +1300,11 @@
|
|
|
1215
1300
|
if (version === 18) {
|
|
1216
1301
|
version = 0;
|
|
1217
1302
|
offset = 0;
|
|
1218
|
-
} else {
|
|
1303
|
+
} else if (version === 1) {
|
|
1219
1304
|
codec = next();
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1305
|
+
} else if (version === 0) {
|
|
1306
|
+
throw new RangeError("Invalid CID version, v0 is always implicit");
|
|
1307
|
+
} else {
|
|
1222
1308
|
throw new RangeError(`Invalid CID version ${version}`);
|
|
1223
1309
|
}
|
|
1224
1310
|
const prefixSize = offset;
|
|
@@ -1417,6 +1503,7 @@
|
|
|
1417
1503
|
var DEFAULT_SYMM_ALGORITHM = AES_GCM;
|
|
1418
1504
|
var DEFAULT_SYMM_LENGTH = 256;
|
|
1419
1505
|
var IV_LENGTH = 12;
|
|
1506
|
+
var AES_GCM_TAG_LENGTH = 16;
|
|
1420
1507
|
var BLS_DID_PREFIX = new Uint8Array([234, 1]);
|
|
1421
1508
|
var EDWARDS_DID_PREFIX = new Uint8Array([237, 1]);
|
|
1422
1509
|
var KEY_TYPE = {
|
|
@@ -1429,6 +1516,7 @@
|
|
|
1429
1516
|
var DEFAULT_ECC_EXCHANGE = "ecc-exchange";
|
|
1430
1517
|
var DEFAULT_ECC_WRITE = "ecc-write";
|
|
1431
1518
|
var SALT_LENGTH = 16;
|
|
1519
|
+
var WRAP_FORMAT_VERSION = 1;
|
|
1432
1520
|
|
|
1433
1521
|
// src/util.ts
|
|
1434
1522
|
var webcrypto = globalThis.crypto;
|
|
@@ -1439,9 +1527,11 @@
|
|
|
1439
1527
|
}
|
|
1440
1528
|
__name(toArrayBuffer, "toArrayBuffer");
|
|
1441
1529
|
async function sha2562(bytes) {
|
|
1442
|
-
|
|
1443
|
-
|
|
1530
|
+
const buf = bytes.buffer.slice(
|
|
1531
|
+
bytes.byteOffset,
|
|
1532
|
+
bytes.byteOffset + bytes.byteLength
|
|
1444
1533
|
);
|
|
1534
|
+
return new Uint8Array(await webcrypto.subtle.digest("sha-256", buf));
|
|
1445
1535
|
}
|
|
1446
1536
|
__name(sha2562, "sha256");
|
|
1447
1537
|
function base64ToArrBuf(string2) {
|
|
@@ -1464,7 +1554,7 @@
|
|
|
1464
1554
|
}
|
|
1465
1555
|
__name(normalizeBase64ToBuf, "normalizeBase64ToBuf");
|
|
1466
1556
|
var normalizeUtf8ToBuf = /* @__PURE__ */ __name((msg) => {
|
|
1467
|
-
return normalizeToBuf(msg, (str) =>
|
|
1557
|
+
return normalizeToBuf(msg, (str) => toArrayBuffer(new TextEncoder().encode(str)));
|
|
1468
1558
|
}, "normalizeUtf8ToBuf");
|
|
1469
1559
|
function strToArrBuf(str, charSize) {
|
|
1470
1560
|
const view = charSize === 8 ? new Uint8Array(str.length) : new Uint16Array(str.length);
|
|
@@ -1552,7 +1642,7 @@
|
|
|
1552
1642
|
name: opts?.alg || DEFAULT_SYMM_ALGORITHM,
|
|
1553
1643
|
length: opts?.length || DEFAULT_SYMM_LENGTH
|
|
1554
1644
|
},
|
|
1555
|
-
true,
|
|
1645
|
+
opts?.extractable ?? true,
|
|
1556
1646
|
["encrypt", "decrypt"]
|
|
1557
1647
|
);
|
|
1558
1648
|
}
|
|
@@ -1719,8 +1809,7 @@
|
|
|
1719
1809
|
verify: /* @__PURE__ */ __name(async function rsaVerify2(msg, sig, publicKey, charSize = DEFAULT_CHAR_SIZE, hashAlg = DEFAULT_HASH_ALGORITHM) {
|
|
1720
1810
|
return webcrypto2.subtle.verify(
|
|
1721
1811
|
{
|
|
1722
|
-
name: RSA_SIGN_ALGORITHM
|
|
1723
|
-
saltLength: SALT_LENGTH
|
|
1812
|
+
name: RSA_SIGN_ALGORITHM
|
|
1724
1813
|
},
|
|
1725
1814
|
typeof publicKey === "string" ? await rsaImportPublicKey(publicKey, hashAlg, "signing" /* Sign */) : publicKey,
|
|
1726
1815
|
normalizeBase64ToBuf(sig),
|
|
@@ -1729,7 +1818,7 @@
|
|
|
1729
1818
|
}, "rsaVerify"),
|
|
1730
1819
|
sign: /* @__PURE__ */ __name(async function sign(msg, privateKey, charSize = DEFAULT_CHAR_SIZE) {
|
|
1731
1820
|
return webcrypto2.subtle.sign(
|
|
1732
|
-
{ name: RSA_SIGN_ALGORITHM
|
|
1821
|
+
{ name: RSA_SIGN_ALGORITHM },
|
|
1733
1822
|
privateKey,
|
|
1734
1823
|
normalizeUnicodeToBuf(msg, charSize)
|
|
1735
1824
|
);
|
|
@@ -1801,6 +1890,9 @@
|
|
|
1801
1890
|
}
|
|
1802
1891
|
__name(stripKeyHeader, "stripKeyHeader");
|
|
1803
1892
|
function isCryptoKey(val) {
|
|
1893
|
+
if (typeof CryptoKey !== "undefined") {
|
|
1894
|
+
return val instanceof CryptoKey;
|
|
1895
|
+
}
|
|
1804
1896
|
return hasProp(val, "algorithm") && hasProp(val, "extractable") && hasProp(val, "type");
|
|
1805
1897
|
}
|
|
1806
1898
|
__name(isCryptoKey, "isCryptoKey");
|
|
@@ -1841,8 +1933,8 @@
|
|
|
1841
1933
|
const magicalBuf = fromString2(didWithoutPrefix, "base58btc");
|
|
1842
1934
|
const result = Object.entries(did.keyTypes).find(
|
|
1843
1935
|
([_key, attr]) => hasPrefix(
|
|
1844
|
-
magicalBuf
|
|
1845
|
-
attr.magicBytes
|
|
1936
|
+
toArrayBuffer(magicalBuf),
|
|
1937
|
+
toArrayBuffer(attr.magicBytes)
|
|
1846
1938
|
)
|
|
1847
1939
|
);
|
|
1848
1940
|
if (!result) throw new Error("Invalid key type");
|
|
@@ -1877,8 +1969,8 @@
|
|
|
1877
1969
|
const magicalBuf = fromString2(didWithoutPrefix, "base58btc");
|
|
1878
1970
|
const result = Object.entries(did.keyTypes).find(
|
|
1879
1971
|
([_key, attr]) => hasPrefix(
|
|
1880
|
-
magicalBuf
|
|
1881
|
-
attr.magicBytes
|
|
1972
|
+
toArrayBuffer(magicalBuf),
|
|
1973
|
+
toArrayBuffer(attr.magicBytes)
|
|
1882
1974
|
)
|
|
1883
1975
|
);
|
|
1884
1976
|
if (!result) throw new Error("Unsupported key algorithm.");
|
|
@@ -1902,7 +1994,9 @@
|
|
|
1902
1994
|
/**
|
|
1903
1995
|
* Factory function -- create a new AES key. Defaults are `AES-GCM`
|
|
1904
1996
|
* algorithm and 12 bytes `iv` size.
|
|
1905
|
-
* @param {{ alg?:string,
|
|
1997
|
+
* @param {{ alg?:string, length?:number, extractable?:boolean }} opts
|
|
1998
|
+
* Algorithm, key size, and whether the key can be exported with
|
|
1999
|
+
* `crypto.subtle.exportKey`. `extractable` defaults to `true`.
|
|
1906
2000
|
* @returns {Promise<CryptoKey>}
|
|
1907
2001
|
*/
|
|
1908
2002
|
create(opts = {
|
|
@@ -1911,10 +2005,11 @@
|
|
|
1911
2005
|
}) {
|
|
1912
2006
|
const length2 = opts.length || DEFAULT_SYMM_LENGTH;
|
|
1913
2007
|
const alg = opts.alg || DEFAULT_SYMM_ALGORITHM;
|
|
2008
|
+
const extractable = opts.extractable ?? true;
|
|
1914
2009
|
return webcrypto3.subtle.generateKey({
|
|
1915
2010
|
name: alg,
|
|
1916
2011
|
length: length2
|
|
1917
|
-
},
|
|
2012
|
+
}, extractable, ["encrypt", "decrypt"]);
|
|
1918
2013
|
},
|
|
1919
2014
|
/**
|
|
1920
2015
|
* key.export -> return a Uint8Array of the key
|
|
@@ -1932,8 +2027,12 @@
|
|
|
1932
2027
|
}, "asString")
|
|
1933
2028
|
}
|
|
1934
2029
|
),
|
|
1935
|
-
import(key) {
|
|
1936
|
-
return importAesKey(
|
|
2030
|
+
import(key, extractable) {
|
|
2031
|
+
return importAesKey(
|
|
2032
|
+
typeof key === "string" ? base64ToArrBuf(key) : key,
|
|
2033
|
+
void 0,
|
|
2034
|
+
extractable
|
|
2035
|
+
);
|
|
1937
2036
|
},
|
|
1938
2037
|
async exportAsString(key) {
|
|
1939
2038
|
const raw = await AES.export(key);
|
|
@@ -1948,12 +2047,12 @@
|
|
|
1948
2047
|
iv: toArrayBuffer(iv)
|
|
1949
2048
|
},
|
|
1950
2049
|
key,
|
|
1951
|
-
typeof encryptedData === "string" ? toArrayBuffer(fromString2(encryptedData)) : encryptedData instanceof Uint8Array ? toArrayBuffer(encryptedData) : encryptedData
|
|
2050
|
+
typeof encryptedData === "string" ? toArrayBuffer(fromString2(encryptedData, "base64pad")) : encryptedData instanceof Uint8Array ? toArrayBuffer(encryptedData) : encryptedData
|
|
1952
2051
|
) : await decryptBytes(encryptedData, key);
|
|
1953
2052
|
return new Uint8Array(decrypted);
|
|
1954
2053
|
}
|
|
1955
2054
|
};
|
|
1956
|
-
function importAesKey(key, length2) {
|
|
2055
|
+
function importAesKey(key, length2, extractable = true) {
|
|
1957
2056
|
return webcrypto3.subtle.importKey(
|
|
1958
2057
|
"raw",
|
|
1959
2058
|
key instanceof Uint8Array ? toArrayBuffer(key) : key,
|
|
@@ -1961,7 +2060,7 @@
|
|
|
1961
2060
|
name: AES_GCM,
|
|
1962
2061
|
length: length2 || 256 /* B256 */
|
|
1963
2062
|
},
|
|
1964
|
-
|
|
2063
|
+
extractable,
|
|
1965
2064
|
["encrypt", "decrypt"]
|
|
1966
2065
|
);
|
|
1967
2066
|
}
|
|
@@ -2403,6 +2502,10 @@
|
|
|
2403
2502
|
return new Uint8Array(data).buffer;
|
|
2404
2503
|
}
|
|
2405
2504
|
__name(toArrayBuffer2, "toArrayBuffer");
|
|
2505
|
+
function rsaCiphertextByteLength(rsaKey) {
|
|
2506
|
+
return rsaKey.algorithm.modulusLength / 8;
|
|
2507
|
+
}
|
|
2508
|
+
__name(rsaCiphertextByteLength, "rsaCiphertextByteLength");
|
|
2406
2509
|
var RsaKeys = class extends AbstractKeys {
|
|
2407
2510
|
static {
|
|
2408
2511
|
__name(this, "RsaKeys");
|
|
@@ -2515,12 +2618,19 @@
|
|
|
2515
2618
|
}
|
|
2516
2619
|
/**
|
|
2517
2620
|
* Decrypt the given message. Expects encrypted AES key + encrypted content.
|
|
2621
|
+
*
|
|
2622
|
+
* The RSA-encrypted AES key is always exactly one RSA-modulus-sized
|
|
2623
|
+
* block, so the split point between it and the AES-encrypted content
|
|
2624
|
+
* is derived from this instance's RSA modulus length -- not from the
|
|
2625
|
+
* (unrelated) AES key size.
|
|
2518
2626
|
*/
|
|
2519
|
-
async decrypt(msg,
|
|
2520
|
-
const keysizeValue = (typeof keysize === "number" ? keysize : void 0) || DEFAULT_SYMM_LENGTH;
|
|
2627
|
+
async decrypt(msg, _keysize, _aesAlgorithm) {
|
|
2521
2628
|
const cipherText = normalizeToBuf(msg, base64ToArrBuf);
|
|
2522
|
-
const
|
|
2523
|
-
|
|
2629
|
+
const rsaCipherLength = rsaCiphertextByteLength(
|
|
2630
|
+
this.exchangeKey.privateKey
|
|
2631
|
+
);
|
|
2632
|
+
const key = cipherText.slice(0, rsaCipherLength);
|
|
2633
|
+
const data = cipherText.slice(rsaCipherLength);
|
|
2524
2634
|
const decryptedKey = await this.decryptKey(key);
|
|
2525
2635
|
const decryptedContent = await AES.decrypt(data, decryptedKey);
|
|
2526
2636
|
return decryptedContent;
|
|
@@ -2775,89 +2885,167 @@
|
|
|
2775
2885
|
* 4. AES-GCM encrypt the content
|
|
2776
2886
|
* 5. Return the ephemeral public key (encapsulation) + wrapped key
|
|
2777
2887
|
*
|
|
2888
|
+
* The result is signed with our Ed25519 write key, so the recipient
|
|
2889
|
+
* can verify who created the wrap. `unwrap` requires our DID and
|
|
2890
|
+
* rejects the wrap unless the signature matches it.
|
|
2891
|
+
*
|
|
2778
2892
|
* @param {SymmKey|Uint8Array|string} contentKey The existing AES key used
|
|
2779
2893
|
* to encrypt the content. Can be a base64 encoded string.
|
|
2780
2894
|
* @param {CryptoKey} newPublicKey The new device's public X25519 key.
|
|
2781
2895
|
* @param {string} [info] Optional info parameter for HKDF.
|
|
2782
2896
|
* Defaults to 'key-wrap'.
|
|
2783
|
-
* @returns {Promise<WrappedKey>} The ephemeral public key (base64)
|
|
2784
|
-
* the wrapped AES key (base64)
|
|
2897
|
+
* @returns {Promise<WrappedKey>} The ephemeral public key (base64),
|
|
2898
|
+
* the wrapped AES key (base64), and a signature (base64) by
|
|
2899
|
+
* our write key.
|
|
2785
2900
|
*/
|
|
2786
2901
|
async wrap(contentKey, newPublicKey, info) {
|
|
2787
|
-
const ephemeralKeypair = await _EccKeys._createExchangeKeys(
|
|
2788
|
-
const
|
|
2902
|
+
const ephemeralKeypair = await _EccKeys._createExchangeKeys(false);
|
|
2903
|
+
const ephemeralPubKeyBuffer = await exportPublicKey(ephemeralKeypair);
|
|
2904
|
+
const ephemeralPubKeyBytes = new Uint8Array(ephemeralPubKeyBuffer);
|
|
2905
|
+
const recipientPubKey = new Uint8Array(
|
|
2906
|
+
await webcrypto5.subtle.exportKey("raw", newPublicKey)
|
|
2907
|
+
);
|
|
2908
|
+
const baseInfo = info || "key-wrap";
|
|
2909
|
+
const salt = webcrypto5.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
2789
2910
|
const kek = await deriveKey(
|
|
2790
2911
|
ephemeralKeypair.privateKey,
|
|
2791
2912
|
newPublicKey,
|
|
2792
2913
|
salt,
|
|
2793
|
-
|
|
2914
|
+
wrapKekInfo(ephemeralPubKeyBytes, recipientPubKey, baseInfo)
|
|
2794
2915
|
);
|
|
2795
2916
|
let contentKeyBytes;
|
|
2917
|
+
let ownsContentKeyBytes = false;
|
|
2796
2918
|
if (contentKey instanceof CryptoKey) {
|
|
2797
|
-
const exported = await
|
|
2919
|
+
const exported = await webcrypto5.subtle.exportKey("raw", contentKey);
|
|
2798
2920
|
contentKeyBytes = new Uint8Array(exported);
|
|
2921
|
+
ownsContentKeyBytes = true;
|
|
2799
2922
|
} else if (typeof contentKey === "string") {
|
|
2800
2923
|
contentKeyBytes = fromString2(contentKey, "base64pad");
|
|
2924
|
+
ownsContentKeyBytes = true;
|
|
2801
2925
|
} else {
|
|
2802
2926
|
contentKeyBytes = contentKey;
|
|
2803
2927
|
}
|
|
2804
|
-
const iv =
|
|
2805
|
-
const wrappedKeyBuffer = await
|
|
2928
|
+
const iv = webcrypto5.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
2929
|
+
const wrappedKeyBuffer = await webcrypto5.subtle.encrypt(
|
|
2806
2930
|
{ name: DEFAULT_SYMM_ALGORITHM, iv },
|
|
2807
2931
|
kek,
|
|
2808
2932
|
toArrayBuffer3(contentKeyBytes)
|
|
2809
2933
|
);
|
|
2810
|
-
|
|
2811
|
-
const saltBuf = new Uint8Array(salt);
|
|
2934
|
+
if (ownsContentKeyBytes) contentKeyBytes.fill(0);
|
|
2812
2935
|
const ivBuf = new Uint8Array(iv);
|
|
2813
2936
|
const wrappedBuf = new Uint8Array(wrappedKeyBuffer);
|
|
2814
|
-
const combinedSize =
|
|
2937
|
+
const combinedSize = 1 + SALT_LENGTH + ivBuf.length + wrappedBuf.length;
|
|
2815
2938
|
const combined = new Uint8Array(combinedSize);
|
|
2816
|
-
combined
|
|
2817
|
-
combined.set(
|
|
2818
|
-
combined.set(
|
|
2939
|
+
combined[0] = WRAP_FORMAT_VERSION;
|
|
2940
|
+
combined.set(salt, 1);
|
|
2941
|
+
combined.set(ivBuf, 1 + SALT_LENGTH);
|
|
2942
|
+
combined.set(wrappedBuf, 1 + SALT_LENGTH + ivBuf.length);
|
|
2943
|
+
const signData = wrapSignData(
|
|
2944
|
+
ephemeralPubKeyBytes,
|
|
2945
|
+
recipientPubKey,
|
|
2946
|
+
combined,
|
|
2947
|
+
baseInfo
|
|
2948
|
+
);
|
|
2949
|
+
const signature = await this.sign(signData);
|
|
2819
2950
|
return {
|
|
2820
|
-
enc: toBase64(
|
|
2821
|
-
wrappedKey: toBase64(combined)
|
|
2951
|
+
enc: toBase64(ephemeralPubKeyBytes),
|
|
2952
|
+
wrappedKey: toBase64(combined),
|
|
2953
|
+
signature: toString2(signature, "base64pad")
|
|
2822
2954
|
};
|
|
2823
2955
|
}
|
|
2824
2956
|
/**
|
|
2825
|
-
* Unwrap a content key that
|
|
2957
|
+
* Unwrap a content key that another device wrapped for this device
|
|
2958
|
+
* with `wrap`.
|
|
2826
2959
|
*
|
|
2827
2960
|
* The recipient device uses its private key + the ephemeral public key
|
|
2828
2961
|
* to rederive the KEK and decrypt the wrapped content key.
|
|
2829
2962
|
*
|
|
2830
|
-
*
|
|
2831
|
-
*
|
|
2832
|
-
*
|
|
2963
|
+
* The wrap's signature is always checked against `signer` before
|
|
2964
|
+
* anything is decrypted, and this throws if the signature is missing
|
|
2965
|
+
* or invalid. `signer` must be the DID of the device you expect the
|
|
2966
|
+
* key from, obtained over a channel an attacker cannot tamper with.
|
|
2967
|
+
* A DID that travels alongside the wrapped key proves nothing, since
|
|
2968
|
+
* an attacker who can swap the wrapped key can swap the DID too.
|
|
2969
|
+
*
|
|
2970
|
+
* @param wrapped The `{ enc, wrappedKey, signature }` object returned
|
|
2971
|
+
* by `wrap`.
|
|
2972
|
+
* @param signer The DID of the expected sender.
|
|
2973
|
+
* @param opts.info The HKDF info string. Must match what was passed to
|
|
2974
|
+
* `wrap`. Defaults to 'key-wrap'.
|
|
2975
|
+
* @param opts.extractable Can the returned key be exported with
|
|
2976
|
+
* `crypto.subtle.exportKey`? Default `true`.
|
|
2833
2977
|
* @returns {Promise<CryptoKey>} The unwrapped AES content key.
|
|
2834
2978
|
*/
|
|
2835
|
-
async unwrap(
|
|
2979
|
+
async unwrap(wrapped, signer, opts = {}) {
|
|
2980
|
+
const { enc, wrappedKey, signature } = wrapped;
|
|
2981
|
+
const info = opts.info;
|
|
2982
|
+
const extractable = opts.extractable ?? true;
|
|
2983
|
+
if (!signer) {
|
|
2984
|
+
throw new Error("`unwrap` requires the DID of the sender");
|
|
2985
|
+
}
|
|
2986
|
+
if (!signature) {
|
|
2987
|
+
throw new Error("The wrapped key is not signed");
|
|
2988
|
+
}
|
|
2989
|
+
const encBytes = fromString2(enc, "base64pad");
|
|
2990
|
+
if (encBytes.length !== X25519_PUBLIC_KEY_LENGTH) {
|
|
2991
|
+
throw new Error(
|
|
2992
|
+
`\`enc\` must be a ${X25519_PUBLIC_KEY_LENGTH}-byte X25519 public key, got ${encBytes.length} bytes`
|
|
2993
|
+
);
|
|
2994
|
+
}
|
|
2995
|
+
const wrappedBuf = new Uint8Array(base64ToArrBuf(wrappedKey));
|
|
2996
|
+
const baseInfo = info || "key-wrap";
|
|
2997
|
+
const minWrappedLength = 1 + SALT_LENGTH + IV_LENGTH + AES_GCM_TAG_LENGTH;
|
|
2998
|
+
if (wrappedBuf.length < minWrappedLength) {
|
|
2999
|
+
throw new Error(
|
|
3000
|
+
`Wrapped key must be at least ${minWrappedLength} bytes, got ${wrappedBuf.length} bytes`
|
|
3001
|
+
);
|
|
3002
|
+
}
|
|
3003
|
+
const version = wrappedBuf[0];
|
|
3004
|
+
if (version !== WRAP_FORMAT_VERSION) {
|
|
3005
|
+
throw new Error(
|
|
3006
|
+
`Unsupported wrapped key format version: ${version}`
|
|
3007
|
+
);
|
|
3008
|
+
}
|
|
3009
|
+
const ourPubKeyBytes = new Uint8Array(
|
|
3010
|
+
await exportPublicKey(this.exchangeKey)
|
|
3011
|
+
);
|
|
3012
|
+
const signData = wrapSignData(
|
|
3013
|
+
encBytes,
|
|
3014
|
+
ourPubKeyBytes,
|
|
3015
|
+
wrappedBuf,
|
|
3016
|
+
baseInfo
|
|
3017
|
+
);
|
|
3018
|
+
const isValid = await verify3(signData, signature, signer);
|
|
3019
|
+
if (!isValid) {
|
|
3020
|
+
throw new Error("Invalid signature for the wrapped key");
|
|
3021
|
+
}
|
|
2836
3022
|
const ephemeralPubKey = await importPublicKey(
|
|
2837
3023
|
enc,
|
|
2838
3024
|
"X25519" /* X25519 */,
|
|
2839
3025
|
"exchange" /* Exchange */
|
|
2840
3026
|
);
|
|
2841
|
-
const
|
|
2842
|
-
const
|
|
2843
|
-
|
|
2844
|
-
|
|
3027
|
+
const salt = wrappedBuf.slice(1, 1 + SALT_LENGTH);
|
|
3028
|
+
const iv = wrappedBuf.slice(
|
|
3029
|
+
1 + SALT_LENGTH,
|
|
3030
|
+
1 + SALT_LENGTH + IV_LENGTH
|
|
3031
|
+
);
|
|
3032
|
+
const ciphertext = wrappedBuf.slice(1 + SALT_LENGTH + IV_LENGTH);
|
|
2845
3033
|
const kek = await deriveKey(
|
|
2846
3034
|
this.exchangeKey.privateKey,
|
|
2847
3035
|
ephemeralPubKey,
|
|
2848
3036
|
salt,
|
|
2849
|
-
|
|
3037
|
+
wrapKekInfo(encBytes, ourPubKeyBytes, baseInfo)
|
|
2850
3038
|
);
|
|
2851
|
-
const contentKeyBytes = await
|
|
3039
|
+
const contentKeyBytes = await webcrypto5.subtle.decrypt(
|
|
2852
3040
|
{ name: DEFAULT_SYMM_ALGORITHM, iv },
|
|
2853
3041
|
kek,
|
|
2854
3042
|
toArrayBuffer3(ciphertext)
|
|
2855
3043
|
);
|
|
2856
|
-
const contentKey = await
|
|
3044
|
+
const contentKey = await webcrypto5.subtle.importKey(
|
|
2857
3045
|
"raw",
|
|
2858
3046
|
contentKeyBytes,
|
|
2859
3047
|
{ name: DEFAULT_SYMM_ALGORITHM },
|
|
2860
|
-
|
|
3048
|
+
extractable,
|
|
2861
3049
|
["encrypt", "decrypt"]
|
|
2862
3050
|
);
|
|
2863
3051
|
return contentKey;
|
|
@@ -2875,14 +3063,15 @@
|
|
|
2875
3063
|
* will use our own public key if not passed in. Can be a CryptoKey
|
|
2876
3064
|
* or a base64 encoded string.
|
|
2877
3065
|
* @param {string} [info] info tag for HKDF. Default is the class property.
|
|
2878
|
-
* @param {SymmKey|Uint8Array|string} [aesKey]
|
|
2879
|
-
*
|
|
3066
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] Use this exact AES key
|
|
3067
|
+
* instead of deriving one from ECDH. Not relevant for most use
|
|
3068
|
+
* cases. Pass the same key to `decrypt` to read it back.
|
|
2880
3069
|
* @param {SymmKeyLength} [keysize] Default is 256
|
|
2881
3070
|
* @returns {Promise<ArrayBuffer>} Buffer of ephemeral-public-key + salt +
|
|
2882
3071
|
* iv + cipher text
|
|
2883
3072
|
*/
|
|
2884
3073
|
async encrypt(content, recipient, info, aesKey, keysize) {
|
|
2885
|
-
const salt =
|
|
3074
|
+
const salt = webcrypto5.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
2886
3075
|
const _publicKey = recipient || this.exchangeKey.publicKey;
|
|
2887
3076
|
let publicKey;
|
|
2888
3077
|
if (typeof _publicKey === "string") {
|
|
@@ -2894,8 +3083,8 @@
|
|
|
2894
3083
|
} else {
|
|
2895
3084
|
publicKey = _publicKey;
|
|
2896
3085
|
}
|
|
2897
|
-
const ephemeralKeypair = await _EccKeys._createExchangeKeys(
|
|
2898
|
-
|
|
3086
|
+
const ephemeralKeypair = await _EccKeys._createExchangeKeys(false);
|
|
3087
|
+
const key = aesKey ? aesKey instanceof CryptoKey ? aesKey : await AES.import(aesKey) : await deriveKey(
|
|
2899
3088
|
ephemeralKeypair.privateKey,
|
|
2900
3089
|
// use ephemeral private key
|
|
2901
3090
|
publicKey,
|
|
@@ -2903,18 +3092,9 @@
|
|
|
2903
3092
|
info || _EccKeys.INFO,
|
|
2904
3093
|
keysize
|
|
2905
3094
|
);
|
|
2906
|
-
|
|
2907
|
-
key = await deriveKey(
|
|
2908
|
-
ephemeralKeypair.privateKey,
|
|
2909
|
-
// use ephemeral private key
|
|
2910
|
-
publicKey,
|
|
2911
|
-
salt,
|
|
2912
|
-
info || _EccKeys.INFO
|
|
2913
|
-
);
|
|
2914
|
-
}
|
|
2915
|
-
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
3095
|
+
const iv = webcrypto5.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
2916
3096
|
const plaintext = typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
2917
|
-
const ciphertext = await
|
|
3097
|
+
const ciphertext = await webcrypto5.subtle.encrypt(
|
|
2918
3098
|
{ name: DEFAULT_SYMM_ALGORITHM, iv },
|
|
2919
3099
|
key,
|
|
2920
3100
|
toArrayBuffer3(plaintext)
|
|
@@ -2956,9 +3136,12 @@
|
|
|
2956
3136
|
* @param msg The encrypted content
|
|
2957
3137
|
* @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
|
|
2958
3138
|
* @param {string} info Custom "info" parameter
|
|
3139
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] Use this exact AES key
|
|
3140
|
+
* instead of deriving one from ECDH. Must match the `aesKey`
|
|
3141
|
+
* passed to the corresponding `encrypt` call.
|
|
2959
3142
|
* @returns {Promise<ArrayBuffer>} The decrypted content.
|
|
2960
3143
|
*/
|
|
2961
|
-
async decrypt(msg, aesAlgorithm, info) {
|
|
3144
|
+
async decrypt(msg, aesAlgorithm, info, aesKey) {
|
|
2962
3145
|
const encrypted = normalizeToBuf(msg, base64ToArrBuf);
|
|
2963
3146
|
const ephemeralPubKeyBuf = encrypted.slice(0, X25519_PUBLIC_KEY_LENGTH);
|
|
2964
3147
|
const ephemeralPublicKey = await webcrypto5.subtle.importKey(
|
|
@@ -2980,13 +3163,13 @@
|
|
|
2980
3163
|
X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
|
|
2981
3164
|
);
|
|
2982
3165
|
const { privateKey } = this.exchangeKey;
|
|
2983
|
-
const key = await deriveKey(
|
|
3166
|
+
const key = aesKey ? aesKey instanceof CryptoKey ? aesKey : await AES.import(aesKey) : await deriveKey(
|
|
2984
3167
|
privateKey,
|
|
2985
3168
|
ephemeralPublicKey,
|
|
2986
3169
|
salt,
|
|
2987
3170
|
info || _EccKeys.INFO
|
|
2988
3171
|
);
|
|
2989
|
-
const decrypted = await
|
|
3172
|
+
const decrypted = await webcrypto5.subtle.decrypt(
|
|
2990
3173
|
{
|
|
2991
3174
|
name: aesAlgorithm || DEFAULT_SYMM_ALGORITHM,
|
|
2992
3175
|
iv
|
|
@@ -3008,20 +3191,28 @@
|
|
|
3008
3191
|
* @param {string} info Custom "info" parameter
|
|
3009
3192
|
* @returns {Promise<string>} The decrypted content as a string.
|
|
3010
3193
|
*/
|
|
3011
|
-
async decryptAsString(msg, aesAlgorithm, info) {
|
|
3012
|
-
const dec = await this.decrypt(msg, aesAlgorithm, info);
|
|
3194
|
+
async decryptAsString(msg, aesAlgorithm, info, aesKey) {
|
|
3195
|
+
const dec = await this.decrypt(msg, aesAlgorithm, info, aesKey);
|
|
3013
3196
|
return toString2(new Uint8Array(dec));
|
|
3014
3197
|
}
|
|
3015
3198
|
/**
|
|
3016
3199
|
* Do DHKE, create a new AES-GCM key.
|
|
3017
3200
|
*
|
|
3201
|
+
* The HKDF salt is either supplied by the caller (so both sides can
|
|
3202
|
+
* re-derive the exact same key later) or generated at random, in
|
|
3203
|
+
* which case it is returned as a `.salt` (base64) property on the
|
|
3204
|
+
* key so the caller can persist it for later re-derivation.
|
|
3205
|
+
*
|
|
3018
3206
|
* @param {CryptoKey|string|null} [publicKey] Public key to use in DHKE.
|
|
3019
3207
|
* Will use our public key if it is not passed in.
|
|
3020
3208
|
* @param {string} info The info parameter for DHKE. Will use the class
|
|
3021
3209
|
* property `INFO` if it is not passed in.
|
|
3022
|
-
* @
|
|
3210
|
+
* @param {Uint8Array|ArrayBuffer|string|null} [salt] Reuse this salt
|
|
3211
|
+
* instead of generating a random one -- pass the same salt on
|
|
3212
|
+
* both sides to re-derive the same key.
|
|
3213
|
+
* @returns {CryptoKey} New AES key, with a `.salt` (base64) property
|
|
3023
3214
|
*/
|
|
3024
|
-
async getAesKey(publicKey, info) {
|
|
3215
|
+
async getAesKey(publicKey, info, salt) {
|
|
3025
3216
|
if (!publicKey) {
|
|
3026
3217
|
publicKey = this.publicExchangeKey;
|
|
3027
3218
|
}
|
|
@@ -3035,14 +3226,16 @@
|
|
|
3035
3226
|
} else {
|
|
3036
3227
|
pub = publicKey;
|
|
3037
3228
|
}
|
|
3038
|
-
const
|
|
3229
|
+
const saltBytes = salt ? new Uint8Array(
|
|
3230
|
+
typeof salt === "string" ? base64ToArrBuf(salt) : salt
|
|
3231
|
+
) : webcrypto5.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
3039
3232
|
const aesKey = await deriveKey(
|
|
3040
3233
|
this.exchangeKey.privateKey,
|
|
3041
3234
|
pub,
|
|
3042
|
-
|
|
3235
|
+
saltBytes,
|
|
3043
3236
|
info || _EccKeys.INFO
|
|
3044
3237
|
);
|
|
3045
|
-
return aesKey;
|
|
3238
|
+
return Object.assign(aesKey, { salt: toBase64(saltBytes) });
|
|
3046
3239
|
}
|
|
3047
3240
|
/**
|
|
3048
3241
|
* Sign the given content using our private write key.
|
|
@@ -3079,7 +3272,7 @@
|
|
|
3079
3272
|
}, "asString")
|
|
3080
3273
|
});
|
|
3081
3274
|
async function exportPublicKey(keypair, format2 = "raw") {
|
|
3082
|
-
return await
|
|
3275
|
+
return await webcrypto5.subtle.exportKey(format2, keypair.publicKey);
|
|
3083
3276
|
}
|
|
3084
3277
|
__name(exportPublicKey, "exportPublicKey");
|
|
3085
3278
|
async function importPublicKey(base64Key, curve, use) {
|
|
@@ -3157,26 +3350,58 @@
|
|
|
3157
3350
|
}
|
|
3158
3351
|
}
|
|
3159
3352
|
__name(verify3, "verify");
|
|
3353
|
+
function wrapKekInfo(ephemeralPubKey, recipientPubKey, info) {
|
|
3354
|
+
const infoBytes = new TextEncoder().encode(info);
|
|
3355
|
+
const data = new Uint8Array(
|
|
3356
|
+
ephemeralPubKey.length + recipientPubKey.length + infoBytes.length
|
|
3357
|
+
);
|
|
3358
|
+
data.set(ephemeralPubKey, 0);
|
|
3359
|
+
data.set(recipientPubKey, ephemeralPubKey.length);
|
|
3360
|
+
data.set(infoBytes, ephemeralPubKey.length + recipientPubKey.length);
|
|
3361
|
+
return data;
|
|
3362
|
+
}
|
|
3363
|
+
__name(wrapKekInfo, "wrapKekInfo");
|
|
3364
|
+
function wrapSignData(ephemeralPubKey, recipientPubKey, wrappedBlob, info) {
|
|
3365
|
+
const infoBytes = new TextEncoder().encode(info);
|
|
3366
|
+
const blobLength = new Uint8Array(4);
|
|
3367
|
+
new DataView(blobLength.buffer).setUint32(0, wrappedBlob.length);
|
|
3368
|
+
const data = new Uint8Array(
|
|
3369
|
+
ephemeralPubKey.length + recipientPubKey.length + blobLength.length + wrappedBlob.length + infoBytes.length
|
|
3370
|
+
);
|
|
3371
|
+
let offset = 0;
|
|
3372
|
+
data.set(ephemeralPubKey, offset);
|
|
3373
|
+
offset += ephemeralPubKey.length;
|
|
3374
|
+
data.set(recipientPubKey, offset);
|
|
3375
|
+
offset += recipientPubKey.length;
|
|
3376
|
+
data.set(blobLength, offset);
|
|
3377
|
+
offset += blobLength.length;
|
|
3378
|
+
data.set(wrappedBlob, offset);
|
|
3379
|
+
offset += wrappedBlob.length;
|
|
3380
|
+
data.set(infoBytes, offset);
|
|
3381
|
+
return data;
|
|
3382
|
+
}
|
|
3383
|
+
__name(wrapSignData, "wrapSignData");
|
|
3160
3384
|
async function deriveKey(privateKey, publicKey, salt, info, keysize) {
|
|
3161
3385
|
const encoder = new TextEncoder();
|
|
3162
|
-
const
|
|
3386
|
+
const infoBytes = typeof info === "string" ? encoder.encode(info) : info;
|
|
3387
|
+
const sharedSecret = await webcrypto5.subtle.deriveBits(
|
|
3163
3388
|
{ name: "X25519", public: publicKey },
|
|
3164
3389
|
privateKey,
|
|
3165
3390
|
256
|
|
3166
3391
|
);
|
|
3167
|
-
const hkdfBaseKey = await
|
|
3392
|
+
const hkdfBaseKey = await webcrypto5.subtle.importKey(
|
|
3168
3393
|
"raw",
|
|
3169
3394
|
sharedSecret,
|
|
3170
3395
|
"HKDF",
|
|
3171
3396
|
false,
|
|
3172
3397
|
["deriveKey"]
|
|
3173
3398
|
);
|
|
3174
|
-
return
|
|
3399
|
+
return webcrypto5.subtle.deriveKey(
|
|
3175
3400
|
{
|
|
3176
3401
|
name: "HKDF",
|
|
3177
3402
|
hash: "SHA-256",
|
|
3178
3403
|
salt: salt instanceof Uint8Array ? toArrayBuffer3(salt) : salt,
|
|
3179
|
-
info:
|
|
3404
|
+
info: toArrayBuffer3(infoBytes)
|
|
3180
3405
|
},
|
|
3181
3406
|
hkdfBaseKey,
|
|
3182
3407
|
{ name: "AES-GCM", length: keysize || DEFAULT_SYMM_LENGTH },
|