react-native-quick-crypto 0.6.1 → 0.7.0-rc.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 +70 -18
- package/android/CMakeLists.txt +58 -61
- package/android/build.gradle +105 -53
- package/android/gradle.properties +5 -5
- package/android/src/main/{AndroidManifest.xml → AndroidManifestNew.xml} +1 -2
- package/cpp/Cipher/MGLCipherHostObject.cpp +30 -6
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +21 -31
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +4 -17
- package/cpp/Cipher/MGLPublicCipher.h +1 -1
- package/cpp/Cipher/MGLPublicCipherInstaller.h +6 -6
- package/cpp/Cipher/MGLRsa.cpp +224 -11
- package/cpp/Cipher/MGLRsa.h +13 -3
- package/cpp/Hash/MGLHashHostObject.cpp +1 -1
- package/cpp/Hash/MGLHashInstaller.cpp +2 -4
- package/cpp/JSIUtils/MGLJSIMacros.h +10 -0
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
- package/cpp/MGLKeys.cpp +415 -471
- package/cpp/MGLKeys.h +70 -2
- package/cpp/MGLQuickCryptoHostObject.cpp +9 -0
- package/cpp/Random/MGLRandomHostObject.cpp +9 -2
- package/cpp/Sig/MGLSignHostObjects.cpp +1 -1
- package/cpp/Utils/MGLUtils.cpp +104 -32
- package/cpp/Utils/MGLUtils.h +172 -143
- package/cpp/Utils/node.h +13 -0
- package/cpp/webcrypto/MGLWebCrypto.cpp +63 -0
- package/cpp/webcrypto/MGLWebCrypto.h +34 -0
- package/cpp/webcrypto/crypto_ec.cpp +334 -0
- package/cpp/webcrypto/crypto_ec.h +65 -0
- package/ios/QuickCrypto.xcodeproj/project.pbxproj +4 -4
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/ios/QuickCrypto.xcodeproj/project.xcworkspace/xcuserdata/brad.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/QuickCrypto.xcodeproj/xcuserdata/brad.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
- package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -1
- package/lib/commonjs/Cipher.js +54 -195
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/Hash.js +29 -29
- package/lib/commonjs/Hash.js.map +1 -1
- package/lib/commonjs/Hashnames.js +75 -0
- package/lib/commonjs/Hashnames.js.map +1 -0
- package/lib/commonjs/Hmac.js +6 -29
- package/lib/commonjs/Hmac.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +3 -5
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -20
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/hash.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/hmac.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/random.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js +6 -0
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +1 -0
- package/lib/commonjs/QuickCrypto.js +9 -15
- package/lib/commonjs/QuickCrypto.js.map +1 -1
- package/lib/commonjs/Utils.js +374 -48
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/aes.js +324 -0
- package/lib/commonjs/aes.js.map +1 -0
- package/lib/commonjs/constants.js +1 -2
- package/lib/commonjs/constants.js.map +1 -1
- package/lib/commonjs/ec.js +288 -0
- package/lib/commonjs/ec.js.map +1 -0
- package/lib/commonjs/index.js +3 -15
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys.js +280 -48
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/pbkdf2.js +44 -18
- package/lib/commonjs/pbkdf2.js.map +1 -1
- package/lib/commonjs/random.js +49 -68
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/rsa.js +329 -0
- package/lib/commonjs/rsa.js.map +1 -0
- package/lib/commonjs/sig.js +13 -54
- package/lib/commonjs/sig.js.map +1 -1
- package/lib/commonjs/subtle.js +271 -0
- package/lib/commonjs/subtle.js.map +1 -0
- package/lib/module/@types/crypto-browserify.d.js.map +1 -1
- package/lib/module/Cipher.js +54 -189
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/Hash.js +27 -21
- package/lib/module/Hash.js.map +1 -1
- package/lib/module/Hashnames.js +71 -0
- package/lib/module/Hashnames.js.map +1 -0
- package/lib/module/Hmac.js +4 -21
- package/lib/module/Hmac.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +3 -4
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +11 -17
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/hash.js.map +1 -1
- package/lib/module/NativeQuickCrypto/hmac.js.map +1 -1
- package/lib/module/NativeQuickCrypto/pbkdf2.js.map +1 -1
- package/lib/module/NativeQuickCrypto/random.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/module/NativeQuickCrypto/webcrypto.js +2 -0
- package/lib/module/NativeQuickCrypto/webcrypto.js.map +1 -0
- package/lib/module/QuickCrypto.js +6 -1
- package/lib/module/QuickCrypto.js.map +1 -1
- package/lib/module/Utils.js +327 -33
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/aes.js +317 -0
- package/lib/module/aes.js.map +1 -0
- package/lib/module/constants.js.map +1 -1
- package/lib/module/ec.js +282 -0
- package/lib/module/ec.js.map +1 -0
- package/lib/module/index.js +4 -8
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys.js +279 -43
- package/lib/module/keys.js.map +1 -1
- package/lib/module/pbkdf2.js +44 -13
- package/lib/module/pbkdf2.js.map +1 -1
- package/lib/module/random.js +46 -54
- package/lib/module/random.js.map +1 -1
- package/lib/module/rsa.js +323 -0
- package/lib/module/rsa.js.map +1 -0
- package/lib/module/sig.js +13 -46
- package/lib/module/sig.js.map +1 -1
- package/lib/module/subtle.js +265 -0
- package/lib/module/subtle.js.map +1 -0
- package/lib/typescript/src/Cipher.d.ts +72 -0
- package/lib/typescript/src/Cipher.d.ts.map +1 -0
- package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +8 -7
- package/lib/typescript/src/Hash.d.ts.map +1 -0
- package/lib/typescript/src/Hashnames.d.ts +11 -0
- package/lib/typescript/src/Hashnames.d.ts.map +1 -0
- package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +3 -3
- package/lib/typescript/src/Hmac.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts +34 -0
- package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +3 -0
- package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/hash.d.ts +7 -0
- package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts +6 -0
- package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +2 -1
- package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +2 -1
- package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
- package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +5 -4
- package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
- package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts +27 -0
- package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
- package/lib/typescript/{QuickCrypto.d.ts → src/QuickCrypto.d.ts} +18 -8
- package/lib/typescript/src/QuickCrypto.d.ts.map +1 -0
- package/lib/typescript/src/Utils.d.ts +46 -0
- package/lib/typescript/src/Utils.d.ts.map +1 -0
- package/lib/typescript/src/aes.d.ts +5 -0
- package/lib/typescript/src/aes.d.ts.map +1 -0
- package/lib/typescript/{constants.d.ts → src/constants.d.ts} +1 -0
- package/lib/typescript/src/constants.d.ts.map +1 -0
- package/lib/typescript/src/ec.d.ts +5 -0
- package/lib/typescript/src/ec.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/keys.d.ts +154 -0
- package/lib/typescript/src/keys.d.ts.map +1 -0
- package/lib/typescript/src/pbkdf2.d.ts +12 -0
- package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
- package/lib/typescript/{random.d.ts → src/random.d.ts} +7 -6
- package/lib/typescript/src/random.d.ts.map +1 -0
- package/lib/typescript/src/rsa.d.ts +4 -0
- package/lib/typescript/src/rsa.d.ts.map +1 -0
- package/lib/typescript/{sig.d.ts → src/sig.d.ts} +3 -3
- package/lib/typescript/src/sig.d.ts.map +1 -0
- package/lib/typescript/src/subtle.d.ts +11 -0
- package/lib/typescript/src/subtle.d.ts.map +1 -0
- package/package.json +39 -34
- package/react-native-quick-crypto.podspec +4 -5
- package/src/Cipher.ts +108 -102
- package/src/Hash.ts +42 -6
- package/src/Hashnames.ts +91 -0
- package/src/Hmac.ts +3 -3
- package/src/NativeQuickCrypto/Cipher.ts +1 -0
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +2 -0
- package/src/NativeQuickCrypto/webcrypto.ts +46 -0
- package/src/QuickCrypto.ts +5 -0
- package/src/Utils.ts +491 -5
- package/src/aes.ts +365 -0
- package/src/ec.ts +351 -0
- package/src/index.ts +3 -8
- package/src/keys.ts +428 -54
- package/src/pbkdf2.ts +84 -11
- package/src/random.ts +37 -24
- package/src/rsa.ts +396 -0
- package/src/sig.ts +3 -2
- package/src/subtle.ts +358 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +0 -5
- package/android/gradlew +0 -183
- package/android/gradlew.bat +0 -100
- package/lib/commonjs/@types/stream-browserify.d.js +0 -2
- package/lib/commonjs/@types/stream-browserify.d.js.map +0 -1
- package/lib/module/@types/stream-browserify.d.js +0 -2
- package/lib/module/@types/stream-browserify.d.js.map +0 -1
- package/lib/typescript/Cipher.d.ts +0 -87
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -32
- package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -6
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -5
- package/lib/typescript/Utils.d.ts +0 -23
- package/lib/typescript/index.d.ts +0 -207
- package/lib/typescript/keys.d.ts +0 -60
- package/lib/typescript/pbkdf2.d.ts +0 -9
- package/src/@types/stream-browserify.d.ts +0 -4
package/lib/commonjs/random.js
CHANGED
|
@@ -11,80 +11,63 @@ exports.randomFillSync = randomFillSync;
|
|
|
11
11
|
exports.randomInt = randomInt;
|
|
12
12
|
exports.randomUUID = randomUUID;
|
|
13
13
|
exports.rng = void 0;
|
|
14
|
-
|
|
15
14
|
var _NativeQuickCrypto = require("./NativeQuickCrypto/NativeQuickCrypto");
|
|
16
|
-
|
|
17
15
|
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
18
|
-
|
|
19
16
|
const random = _NativeQuickCrypto.NativeQuickCrypto.random;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (typeof (_ref = (arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1, _ref < 1 || arguments.length <= _ref ? undefined : arguments[_ref]) !== 'function') {
|
|
25
|
-
throw new Error('No callback provided to randomDill');
|
|
17
|
+
function randomFill(buffer, ...rest) {
|
|
18
|
+
if (typeof rest[rest.length - 1] !== 'function') {
|
|
19
|
+
throw new Error('No callback provided to randomFill');
|
|
26
20
|
}
|
|
27
|
-
|
|
28
|
-
const callback = (_ref2 = (arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1, _ref2 < 1 || arguments.length <= _ref2 ? undefined : arguments[_ref2]);
|
|
21
|
+
const callback = rest[rest.length - 1];
|
|
29
22
|
let offset = 0;
|
|
30
23
|
let size = buffer.byteLength;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
size = arguments.length <= 2 ? undefined : arguments[2];
|
|
24
|
+
if (typeof rest[2] === 'function') {
|
|
25
|
+
offset = rest[0];
|
|
26
|
+
size = rest[1];
|
|
35
27
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
offset = arguments.length <= 1 ? undefined : arguments[1];
|
|
28
|
+
if (typeof rest[1] === 'function') {
|
|
29
|
+
offset = rest[0];
|
|
39
30
|
}
|
|
40
|
-
|
|
41
|
-
random.randomFill(_reactNativeBuffer.Buffer.isBuffer(buffer) ? buffer.buffer : buffer, offset, size).then(() => {
|
|
31
|
+
random.randomFill(_reactNativeBuffer.Buffer.isBuffer(buffer) ? buffer.buffer : ArrayBuffer.isView(buffer) ? buffer.buffer : buffer, offset, size).then(() => {
|
|
42
32
|
callback(null, buffer);
|
|
43
33
|
}, e => {
|
|
44
34
|
callback(e);
|
|
45
35
|
});
|
|
46
36
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
let offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
50
|
-
let size = arguments.length > 2 ? arguments[2] : undefined;
|
|
51
|
-
random.randomFillSync(buffer.buffer ? buffer.buffer : buffer, offset, size !== null && size !== void 0 ? size : buffer.byteLength);
|
|
37
|
+
function randomFillSync(buffer, offset = 0, size) {
|
|
38
|
+
random.randomFillSync(buffer.buffer ? buffer.buffer : buffer, offset, size ?? buffer.byteLength);
|
|
52
39
|
return buffer;
|
|
53
40
|
}
|
|
54
|
-
|
|
55
41
|
function randomBytes(size, callback) {
|
|
56
42
|
const buf = new _reactNativeBuffer.Buffer(size);
|
|
57
|
-
|
|
58
43
|
if (callback === undefined) {
|
|
59
44
|
randomFillSync(buf.buffer, 0, size);
|
|
60
45
|
return buf;
|
|
61
46
|
}
|
|
62
|
-
|
|
63
47
|
randomFill(buf.buffer, 0, size, error => {
|
|
64
48
|
if (error) {
|
|
65
49
|
callback(error);
|
|
66
50
|
}
|
|
67
|
-
|
|
68
51
|
callback(null, buf);
|
|
69
52
|
});
|
|
70
53
|
}
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
exports.
|
|
74
|
-
const pseudoRandomBytes = randomBytes;
|
|
75
|
-
exports.pseudoRandomBytes = pseudoRandomBytes;
|
|
76
|
-
const prng = randomBytes;
|
|
77
|
-
exports.prng = prng;
|
|
54
|
+
const rng = exports.rng = randomBytes;
|
|
55
|
+
const pseudoRandomBytes = exports.pseudoRandomBytes = randomBytes;
|
|
56
|
+
const prng = exports.prng = randomBytes;
|
|
78
57
|
// The rest of the file is taken from https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js
|
|
58
|
+
|
|
79
59
|
// Largest integer we can read from a buffer.
|
|
80
60
|
// e.g.: Buffer.from("ff".repeat(6), "hex").readUIntBE(0, 6);
|
|
81
|
-
const RAND_MAX = 0xffffffffffff;
|
|
82
|
-
// divisible by 6 because each attempt to obtain a random int uses 6 bytes.
|
|
61
|
+
const RAND_MAX = 0xffffffffffff;
|
|
83
62
|
|
|
63
|
+
// Cache random data to use in randomInt. The cache size must be evenly
|
|
64
|
+
// divisible by 6 because each attempt to obtain a random int uses 6 bytes.
|
|
84
65
|
const randomCache = new _reactNativeBuffer.Buffer(6 * 1024);
|
|
85
66
|
let randomCacheOffset = randomCache.length;
|
|
86
67
|
let asyncCacheFillInProgress = false;
|
|
87
|
-
const asyncCachePendingTasks = [];
|
|
68
|
+
const asyncCachePendingTasks = [];
|
|
69
|
+
|
|
70
|
+
// Generates an integer in [min, max) range where min is inclusive and max is
|
|
88
71
|
// exclusive.
|
|
89
72
|
|
|
90
73
|
function randomInt(arg1, arg2, callback) {
|
|
@@ -94,7 +77,6 @@ function randomInt(arg1, arg2, callback) {
|
|
|
94
77
|
let max;
|
|
95
78
|
let min;
|
|
96
79
|
const minNotSpecified = typeof arg2 === 'undefined' || typeof arg2 === 'function';
|
|
97
|
-
|
|
98
80
|
if (minNotSpecified) {
|
|
99
81
|
callback = arg2;
|
|
100
82
|
max = arg1;
|
|
@@ -103,19 +85,18 @@ function randomInt(arg1, arg2, callback) {
|
|
|
103
85
|
min = arg1;
|
|
104
86
|
max = arg2;
|
|
105
87
|
}
|
|
106
|
-
|
|
88
|
+
if (typeof callback !== 'undefined' && typeof callback !== 'function') {
|
|
89
|
+
throw new TypeError('callback must be a function or undefined');
|
|
90
|
+
}
|
|
107
91
|
const isSync = typeof callback === 'undefined';
|
|
108
|
-
|
|
109
92
|
if (!Number.isSafeInteger(min)) {
|
|
110
93
|
// todo throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);
|
|
111
94
|
throw 'ERR_INVALID_ARG_TYPE';
|
|
112
95
|
}
|
|
113
|
-
|
|
114
96
|
if (!Number.isSafeInteger(max)) {
|
|
115
97
|
// todo throw new ERR_INVALID_ARG_TYPE('max', 'a safe integer', max);
|
|
116
98
|
throw 'ERR_INVALID_ARG_TYPE';
|
|
117
99
|
}
|
|
118
|
-
|
|
119
100
|
if (max <= min) {
|
|
120
101
|
/* todo throw new ERR_OUT_OF_RANGE(
|
|
121
102
|
'max',
|
|
@@ -123,11 +104,10 @@ function randomInt(arg1, arg2, callback) {
|
|
|
123
104
|
max
|
|
124
105
|
);*/
|
|
125
106
|
throw 'ERR_OUT_OF_RANGE';
|
|
126
|
-
}
|
|
127
|
-
|
|
107
|
+
}
|
|
128
108
|
|
|
109
|
+
// First we generate a random int between [0..range)
|
|
129
110
|
const range = max - min;
|
|
130
|
-
|
|
131
111
|
if (!(range <= RAND_MAX)) {
|
|
132
112
|
/* todo throw new ERR_OUT_OF_RANGE(
|
|
133
113
|
`max${minNotSpecified ? '' : ' - min'}`,
|
|
@@ -135,36 +115,35 @@ function randomInt(arg1, arg2, callback) {
|
|
|
135
115
|
range
|
|
136
116
|
);*/
|
|
137
117
|
throw 'ERR_OUT_OF_RANGE';
|
|
138
|
-
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// For (x % range) to produce an unbiased value greater than or equal to 0 and
|
|
139
121
|
// less than range, x must be drawn randomly from the set of integers greater
|
|
140
122
|
// than or equal to 0 and less than randLimit.
|
|
123
|
+
const randLimit = RAND_MAX - RAND_MAX % range;
|
|
141
124
|
|
|
142
|
-
|
|
143
|
-
const randLimit = RAND_MAX - RAND_MAX % range; // If we don't have a callback, or if there is still data in the cache, we can
|
|
125
|
+
// If we don't have a callback, or if there is still data in the cache, we can
|
|
144
126
|
// do this synchronously, which is super fast.
|
|
145
|
-
|
|
146
127
|
while (isSync || randomCacheOffset < randomCache.length) {
|
|
147
128
|
if (randomCacheOffset === randomCache.length) {
|
|
148
129
|
// This might block the thread for a bit, but we are in sync mode.
|
|
149
130
|
randomFillSync(randomCache);
|
|
150
131
|
randomCacheOffset = 0;
|
|
151
132
|
}
|
|
152
|
-
|
|
153
133
|
const x = randomCache.readUIntBE(randomCacheOffset, 6);
|
|
154
134
|
randomCacheOffset += 6;
|
|
155
|
-
|
|
156
135
|
if (x < randLimit) {
|
|
157
136
|
const n = x % range + min;
|
|
158
137
|
if (isSync) return n;
|
|
159
138
|
process.nextTick(callback, undefined, n);
|
|
160
139
|
return;
|
|
161
140
|
}
|
|
162
|
-
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// At this point, we are in async mode with no data in the cache. We cannot
|
|
163
144
|
// simply refill the cache, because another async call to randomInt might
|
|
164
145
|
// already be doing that. Instead, queue this call for when the cache has
|
|
165
146
|
// been refilled.
|
|
166
|
-
|
|
167
|
-
|
|
168
147
|
if (callback !== undefined) {
|
|
169
148
|
// it is (typescript doesn't know it)
|
|
170
149
|
asyncCachePendingTasks.push({
|
|
@@ -175,7 +154,6 @@ function randomInt(arg1, arg2, callback) {
|
|
|
175
154
|
asyncRefillRandomIntCache();
|
|
176
155
|
}
|
|
177
156
|
}
|
|
178
|
-
|
|
179
157
|
function asyncRefillRandomIntCache() {
|
|
180
158
|
if (asyncCacheFillInProgress) return;
|
|
181
159
|
asyncCacheFillInProgress = true;
|
|
@@ -183,46 +161,49 @@ function asyncRefillRandomIntCache() {
|
|
|
183
161
|
asyncCacheFillInProgress = false;
|
|
184
162
|
const tasks = asyncCachePendingTasks;
|
|
185
163
|
const errorReceiver = err && tasks.shift();
|
|
186
|
-
if (!err) randomCacheOffset = 0;
|
|
164
|
+
if (!err) randomCacheOffset = 0;
|
|
165
|
+
|
|
166
|
+
// Restart all pending tasks. If an error occurred, we only notify a single
|
|
187
167
|
// callback (errorReceiver) about it. This way, every async call to
|
|
188
168
|
// randomInt has a chance of being successful, and it avoids complex
|
|
189
169
|
// exception handling here.
|
|
190
|
-
|
|
191
170
|
tasks.splice(0).forEach(task => {
|
|
192
171
|
randomInt(task.min, task.max, task.callback);
|
|
193
|
-
});
|
|
172
|
+
});
|
|
194
173
|
|
|
174
|
+
// This is the only call that might throw, and is therefore done at the end.
|
|
195
175
|
if (errorReceiver) errorReceiver.callback(err, 0);
|
|
196
176
|
});
|
|
197
|
-
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Really just the Web Crypto API alternative
|
|
198
180
|
// to require('crypto').randomFillSync() with an
|
|
199
181
|
// additional limitation that the input buffer is
|
|
200
182
|
// not allowed to exceed 65536 bytes, and can only
|
|
201
183
|
// be an integer-type TypedArray.
|
|
202
184
|
|
|
203
|
-
|
|
204
185
|
function getRandomValues(data) {
|
|
205
186
|
if (data.byteLength > 65536) {
|
|
206
187
|
throw new Error('The requested length exceeds 65,536 bytes');
|
|
207
188
|
}
|
|
208
|
-
|
|
209
189
|
randomFillSync(data, 0);
|
|
210
190
|
return data;
|
|
211
191
|
}
|
|
212
|
-
|
|
213
192
|
const byteToHex = [];
|
|
214
|
-
|
|
215
193
|
for (let i = 0; i < 256; ++i) {
|
|
216
194
|
byteToHex.push((i + 0x100).toString(16).slice(1));
|
|
217
|
-
}
|
|
218
|
-
|
|
195
|
+
}
|
|
219
196
|
|
|
197
|
+
// Based on https://github.com/uuidjs/uuid/blob/main/src/v4.js
|
|
220
198
|
function randomUUID() {
|
|
221
199
|
const size = 16;
|
|
222
200
|
const buffer = new _reactNativeBuffer.Buffer(size);
|
|
223
|
-
randomFillSync(buffer, 0, size);
|
|
201
|
+
randomFillSync(buffer, 0, size);
|
|
224
202
|
|
|
203
|
+
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
|
|
204
|
+
// eslint-disable-next-line no-bitwise
|
|
225
205
|
buffer[6] = buffer[6] & 0x0f | 0x40;
|
|
206
|
+
// eslint-disable-next-line no-bitwise
|
|
226
207
|
buffer[8] = buffer[8] & 0x3f | 0x80;
|
|
227
208
|
return (byteToHex[buffer[0]] + byteToHex[buffer[1]] + byteToHex[buffer[2]] + byteToHex[buffer[3]] + '-' + byteToHex[buffer[4]] + byteToHex[buffer[5]] + '-' + byteToHex[buffer[6]] + byteToHex[buffer[7]] + '-' + byteToHex[buffer[8]] + byteToHex[buffer[9]] + '-' + byteToHex[buffer[10]] + byteToHex[buffer[11]] + byteToHex[buffer[12]] + byteToHex[buffer[13]] + byteToHex[buffer[14]] + byteToHex[buffer[15]]).toLowerCase();
|
|
228
209
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["random.ts"],"names":["random","NativeQuickCrypto","randomFill","buffer","Error","callback","offset","size","byteLength","Buffer","isBuffer","then","e","randomFillSync","randomBytes","buf","undefined","error","rng","pseudoRandomBytes","prng","RAND_MAX","randomCache","randomCacheOffset","length","asyncCacheFillInProgress","asyncCachePendingTasks","randomInt","arg1","arg2","max","min","minNotSpecified","isSync","Number","isSafeInteger","range","randLimit","x","readUIntBE","n","process","nextTick","push","asyncRefillRandomIntCache","err","tasks","errorReceiver","shift","splice","forEach","task","getRandomValues","data","byteToHex","i","toString","slice","randomUUID","toLowerCase"],"mappings":";;;;;;;;;;;;;;AAAA;;AACA;;AAEA,MAAMA,MAAM,GAAGC,qCAAkBD,MAAjC;;AAgCO,SAASE,UAAT,CAAoBC,MAApB,EAAuD;AAAA;;AAC5D,MAAI,eAAY,qDAAc,CAA1B,8EAAiC,UAArC,EAAiD;AAC/C,UAAM,IAAIC,KAAJ,CAAU,oCAAV,CAAN;AACD;;AAED,QAAMC,QAAQ,YAAQ,qDAAc,CAAtB,4EAAd;AAKA,MAAIC,MAAc,GAAG,CAArB;AACA,MAAIC,IAAY,GAAGJ,MAAM,CAACK,UAA1B;;AAEA,MAAI,8DAAmB,UAAvB,EAAmC;AACjCF,IAAAA,MAAM,mDAAN;AACAC,IAAAA,IAAI,mDAAJ;AACD;;AAED,MAAI,8DAAmB,UAAvB,EAAmC;AACjCD,IAAAA,MAAM,mDAAN;AACD;;AAEDN,EAAAA,MAAM,CACHE,UADH,CACcO,0BAAOC,QAAP,CAAgBP,MAAhB,IAA0BA,MAAM,CAACA,MAAjC,GAA0CA,MADxD,EACgEG,MADhE,EACwEC,IADxE,EAEGI,IAFH,CAGI,MAAM;AACJN,IAAAA,QAAQ,CAAC,IAAD,EAAOF,MAAP,CAAR;AACD,GALL,EAMKS,CAAD,IAAc;AACZP,IAAAA,QAAQ,CAACO,CAAD,CAAR;AACD,GARL;AAUD;;AAQM,SAASC,cAAT,CAAwBV,MAAxB,EAAwE;AAAA,MAAnCG,MAAmC,uEAAlB,CAAkB;AAAA,MAAfC,IA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T,GAUA,GAVA,GAWAmD,SAAS,CAACnD,MAAM,CAAC,CAAD,CAAP,CAXT,GAYAmD,SAAS,CAACnD,MAAM,CAAC,CAAD,CAAP,CAZT,GAaA,GAbA,GAcAmD,SAAS,CAACnD,MAAM,CAAC,EAAD,CAAP,CAdT,GAeAmD,SAAS,CAACnD,MAAM,CAAC,EAAD,CAAP,CAfT,GAgBAmD,SAAS,CAACnD,MAAM,CAAC,EAAD,CAAP,CAhBT,GAiBAmD,SAAS,CAACnD,MAAM,CAAC,EAAD,CAAP,CAjBT,GAkBAmD,SAAS,CAACnD,MAAM,CAAC,EAAD,CAAP,CAlBT,GAmBAmD,SAAS,CAACnD,MAAM,CAAC,EAAD,CAAP,CApBJ,EAqBLwD,WArBK,EAAP;AAsBD","sourcesContent":["import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';\nimport { Buffer } from '@craftzdog/react-native-buffer';\n\nconst random = NativeQuickCrypto.random;\n\ntype TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | Float32Array\n | Float64Array;\ntype ArrayBufferView = TypedArray | DataView | ArrayBufferLike | Buffer;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill<T extends ArrayBufferView>(\n buffer: T,\n offset: number,\n size: number,\n callback: (err: Error | null, buf: T) => void\n): void;\n\nexport function randomFill(buffer: any, ...rest: any[]): void {\n if (typeof rest[rest.length - 1] !== 'function') {\n throw new Error('No callback provided to randomDill');\n }\n\n const callback = rest[rest.length - 1] as any as (\n err: Error | null,\n buf?: ArrayBuffer\n ) => void;\n\n let offset: number = 0;\n let size: number = buffer.byteLength;\n\n if (typeof rest[2] === 'function') {\n offset = rest[0];\n size = rest[1];\n }\n\n if (typeof rest[1] === 'function') {\n offset = rest[0];\n }\n\n random\n .randomFill(Buffer.isBuffer(buffer) ? buffer.buffer : buffer, offset, size)\n .then(\n () => {\n callback(null, buffer);\n },\n (e: Error) => {\n callback(e);\n }\n );\n}\n\nexport function randomFillSync<T extends ArrayBufferView>(\n buffer: T,\n offset?: number,\n size?: number\n): T;\n\nexport function randomFillSync(buffer: any, offset: number = 0, size?: number) {\n random.randomFillSync(\n buffer.buffer ? buffer.buffer : buffer,\n offset,\n size ?? buffer.byteLength\n );\n return buffer;\n}\n\nexport function randomBytes(size: number): ArrayBuffer;\n\nexport function randomBytes(\n size: number,\n callback: (err: Error | null, buf?: ArrayBuffer) => void\n): void;\n\nexport function randomBytes(\n size: number,\n callback?: (err: Error | null, buf?: ArrayBuffer) => void\n): void | ArrayBuffer {\n const buf = new Buffer(size);\n\n if (callback === undefined) {\n randomFillSync(buf.buffer, 0, size);\n return buf;\n }\n\n randomFill(buf.buffer, 0, size, (error: Error | null) => {\n if (error) {\n callback(error);\n }\n callback(null, buf);\n });\n}\n\nexport const rng = randomBytes;\nexport const pseudoRandomBytes = randomBytes;\nexport const prng = randomBytes;\n\ntype RandomIntCallback = (err: Error | null, value: number) => void;\ntype Task = {\n min: number;\n max: number;\n callback: RandomIntCallback;\n};\n\n// The rest of the file is taken from https://github.com/nodejs/node/blob/master/lib/internal/crypto/random.js\n\n// Largest integer we can read from a buffer.\n// e.g.: Buffer.from(\"ff\".repeat(6), \"hex\").readUIntBE(0, 6);\nconst RAND_MAX = 0xffffffffffff;\n\n// Cache random data to use in randomInt. The cache size must be evenly\n// divisible by 6 because each attempt to obtain a random int uses 6 bytes.\nconst randomCache = new Buffer(6 * 1024);\nlet randomCacheOffset = randomCache.length;\nlet asyncCacheFillInProgress = false;\nconst asyncCachePendingTasks: Task[] = [];\n\n// Generates an integer in [min, max) range where min is inclusive and max is\n// exclusive.\n\nexport function randomInt(max: number, callback: RandomIntCallback): void;\nexport function randomInt(max: number): number;\nexport function randomInt(\n min: number,\n max: number,\n callback: RandomIntCallback\n): void;\nexport function randomInt(min: number, max: number): number;\nexport function randomInt(\n arg1: number,\n arg2?: number | RandomIntCallback,\n callback?: RandomIntCallback\n): void | number {\n // Detect optional min syntax\n // randomInt(max)\n // randomInt(max, callback)\n let max: number;\n let min: number;\n const minNotSpecified =\n typeof arg2 === 'undefined' || typeof arg2 === 'function';\n\n if (minNotSpecified) {\n callback = arg2 as any as undefined | RandomIntCallback;\n max = arg1;\n min = 0;\n } else {\n min = arg1;\n max = arg2 as any as number;\n }\n\n const isSync = typeof callback === 'undefined';\n if (!Number.isSafeInteger(min)) {\n // todo throw new ERR_INVALID_ARG_TYPE('min', 'a safe integer', min);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (!Number.isSafeInteger(max)) {\n // todo throw new ERR_INVALID_ARG_TYPE('max', 'a safe integer', max);\n throw 'ERR_INVALID_ARG_TYPE';\n }\n if (max <= min) {\n /* todo throw new ERR_OUT_OF_RANGE(\n 'max',\n `greater than the value of \"min\" (${min})`,\n max\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // First we generate a random int between [0..range)\n const range = max - min;\n\n if (!(range <= RAND_MAX)) {\n /* todo throw new ERR_OUT_OF_RANGE(\n `max${minNotSpecified ? '' : ' - min'}`,\n `<= ${RAND_MAX}`,\n range\n );*/\n throw 'ERR_OUT_OF_RANGE';\n }\n\n // For (x % range) to produce an unbiased value greater than or equal to 0 and\n // less than range, x must be drawn randomly from the set of integers greater\n // than or equal to 0 and less than randLimit.\n const randLimit = RAND_MAX - (RAND_MAX % range);\n\n // If we don't have a callback, or if there is still data in the cache, we can\n // do this synchronously, which is super fast.\n while (isSync || randomCacheOffset < randomCache.length) {\n if (randomCacheOffset === randomCache.length) {\n // This might block the thread for a bit, but we are in sync mode.\n randomFillSync(randomCache);\n randomCacheOffset = 0;\n }\n\n const x = randomCache.readUIntBE(randomCacheOffset, 6);\n randomCacheOffset += 6;\n\n if (x < randLimit) {\n const n = (x % range) + min;\n if (isSync) return n;\n process.nextTick(callback as Function, undefined, n);\n return;\n }\n }\n\n // At this point, we are in async mode with no data in the cache. We cannot\n // simply refill the cache, because another async call to randomInt might\n // already be doing that. Instead, queue this call for when the cache has\n // been refilled.\n if (callback !== undefined) {\n // it is (typescript doesn't know it)\n asyncCachePendingTasks.push({ min, max, callback });\n asyncRefillRandomIntCache();\n }\n}\n\nfunction asyncRefillRandomIntCache() {\n if (asyncCacheFillInProgress) return;\n\n asyncCacheFillInProgress = true;\n randomFill(randomCache, (err) => {\n asyncCacheFillInProgress = false;\n\n const tasks = asyncCachePendingTasks;\n const errorReceiver = err && tasks.shift();\n if (!err) randomCacheOffset = 0;\n\n // Restart all pending tasks. If an error occurred, we only notify a single\n // callback (errorReceiver) about it. This way, every async call to\n // randomInt has a chance of being successful, and it avoids complex\n // exception handling here.\n tasks.splice(0).forEach((task) => {\n randomInt(task.min, task.max, task.callback);\n });\n\n // This is the only call that might throw, and is therefore done at the end.\n if (errorReceiver) errorReceiver.callback(err, 0);\n });\n}\n\n// Really just the Web Crypto API alternative\n// to require('crypto').randomFillSync() with an\n// additional limitation that the input buffer is\n// not allowed to exceed 65536 bytes, and can only\n// be an integer-type TypedArray.\ntype DataType =\n | Int8Array\n | Int16Array\n | Int32Array\n | Uint8Array\n | Uint16Array\n | Uint32Array;\nexport function getRandomValues(data: DataType) {\n if (data.byteLength > 65536) {\n throw new Error('The requested length exceeds 65,536 bytes');\n }\n randomFillSync(data, 0);\n return data;\n}\n\nconst byteToHex: string[] = [];\n\nfor (let i = 0; i < 256; ++i) {\n byteToHex.push((i + 0x100).toString(16).slice(1));\n}\n\n// Based on https://github.com/uuidjs/uuid/blob/main/src/v4.js\nexport function randomUUID() {\n const size = 16;\n const buffer = new Buffer(size);\n randomFillSync(buffer, 0, size);\n\n // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`\n buffer[6] = (buffer[6] & 0x0f) | 0x40;\n buffer[8] = (buffer[8] & 0x3f) | 0x80;\n\n return (\n byteToHex[buffer[0]] +\n byteToHex[buffer[1]] +\n byteToHex[buffer[2]] +\n byteToHex[buffer[3]] +\n '-' +\n byteToHex[buffer[4]] +\n byteToHex[buffer[5]] +\n '-' +\n byteToHex[buffer[6]] +\n byteToHex[buffer[7]] +\n '-' +\n byteToHex[buffer[8]] +\n byteToHex[buffer[9]] +\n '-' +\n byteToHex[buffer[10]] +\n byteToHex[buffer[11]] +\n byteToHex[buffer[12]] +\n byteToHex[buffer[13]] +\n byteToHex[buffer[14]] +\n byteToHex[buffer[15]]\n ).toLowerCase();\n}\n"]}
|
|
1
|
+
{"version":3,"names":["_NativeQuickCrypto","require","_reactNativeBuffer","random","NativeQuickCrypto","randomFill","buffer","rest","length","Error","callback","offset","size","byteLength","Buffer","isBuffer","ArrayBuffer","isView","then","e","randomFillSync","randomBytes","buf","undefined","error","rng","exports","pseudoRandomBytes","prng","RAND_MAX","randomCache","randomCacheOffset","asyncCacheFillInProgress","asyncCachePendingTasks","randomInt","arg1","arg2","max","min","minNotSpecified","TypeError","isSync","Number","isSafeInteger","range","randLimit","x","readUIntBE","n","process","nextTick","push","asyncRefillRandomIntCache","err","tasks","errorReceiver","shift","splice","forEach","task","getRandomValues","data","byteToHex","i","toString","slice","randomUUID","toLowerCase"],"sourceRoot":"../../src","sources":["random.ts"],"mappings":";;;;;;;;;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,kBAAA,GAAAD,OAAA;AAEA,MAAME,MAAM,GAAGC,oCAAiB,CAACD,MAAM;AAgChC,SAASE,UAAUA,CAACC,MAAW,EAAE,GAAGC,IAAW,EAAQ;EAC5D,IAAI,OAAOA,IAAI,CAACA,IAAI,CAACC,MAAM,GAAG,CAAC,CAAC,KAAK,UAAU,EAAE;IAC/C,MAAM,IAAIC,KAAK,CAAC,oCAAoC,CAAC;EACvD;EAEA,MAAMC,QAAQ,GAAGH,IAAI,CAACA,IAAI,CAACC,MAAM,GAAG,CAAC,CAG5B;EAET,IAA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|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.rsaImportKey = void 0;
|
|
7
|
+
var _NativeQuickCrypto = require("./NativeQuickCrypto/NativeQuickCrypto");
|
|
8
|
+
var _Utils = require("./Utils");
|
|
9
|
+
var _keys = require("./keys");
|
|
10
|
+
// 'use strict';
|
|
11
|
+
|
|
12
|
+
// const {
|
|
13
|
+
// SafeSet,
|
|
14
|
+
// Uint8Array,
|
|
15
|
+
// } = primordials;
|
|
16
|
+
|
|
17
|
+
// const {
|
|
18
|
+
// KeyObjectHandle,
|
|
19
|
+
// RSACipherJob,
|
|
20
|
+
// RSAKeyExportJob,
|
|
21
|
+
// SignJob,
|
|
22
|
+
// kCryptoJobAsync,
|
|
23
|
+
// kSignJobModeSign,
|
|
24
|
+
// kSignJobModeVerify,
|
|
25
|
+
// kKeyVariantRSA_SSA_PKCS1_v1_5,
|
|
26
|
+
// kKeyVariantRSA_PSS,
|
|
27
|
+
// kKeyVariantRSA_OAEP,
|
|
28
|
+
// kKeyTypePrivate,
|
|
29
|
+
// kWebCryptoCipherEncrypt,
|
|
30
|
+
// RSA_PKCS1_PSS_PADDING,
|
|
31
|
+
// } = internalBinding('crypto');
|
|
32
|
+
|
|
33
|
+
// const {
|
|
34
|
+
// validateInt32,
|
|
35
|
+
// } = require('internal/validators');
|
|
36
|
+
|
|
37
|
+
// const {
|
|
38
|
+
// bigIntArrayToUnsignedInt,
|
|
39
|
+
// getUsagesUnion,
|
|
40
|
+
// hasAnyNotIn,
|
|
41
|
+
// jobPromise,
|
|
42
|
+
// normalizeHashName,
|
|
43
|
+
// validateKeyOps,
|
|
44
|
+
// validateMaxBufferLength,
|
|
45
|
+
// kHandle,
|
|
46
|
+
// kKeyObject,
|
|
47
|
+
// } = require('internal/crypto/util');
|
|
48
|
+
|
|
49
|
+
// const {
|
|
50
|
+
// lazyDOMException,
|
|
51
|
+
// promisify,
|
|
52
|
+
// } = require('internal/util');
|
|
53
|
+
|
|
54
|
+
// const {
|
|
55
|
+
// InternalCryptoKey,
|
|
56
|
+
// PrivateKeyObject,
|
|
57
|
+
// PublicKeyObject,
|
|
58
|
+
// createPublicKey,
|
|
59
|
+
// createPrivateKey,
|
|
60
|
+
// } = require('internal/crypto/keys');
|
|
61
|
+
|
|
62
|
+
// const {
|
|
63
|
+
// generateKeyPair: _generateKeyPair,
|
|
64
|
+
// } = require('internal/crypto/keygen');
|
|
65
|
+
|
|
66
|
+
// const kRsaVariants = {
|
|
67
|
+
// 'RSASSA-PKCS1-v1_5': kKeyVariantRSA_SSA_PKCS1_v1_5,
|
|
68
|
+
// 'RSA-PSS': kKeyVariantRSA_PSS,
|
|
69
|
+
// 'RSA-OAEP': kKeyVariantRSA_OAEP,
|
|
70
|
+
// };
|
|
71
|
+
// const generateKeyPair = promisify(_generateKeyPair);
|
|
72
|
+
|
|
73
|
+
function verifyAcceptableRsaKeyUse(name, isPublic, usages) {
|
|
74
|
+
let checkSet;
|
|
75
|
+
switch (name) {
|
|
76
|
+
case 'RSA-OAEP':
|
|
77
|
+
checkSet = isPublic ? ['encrypt', 'wrapKey'] : ['decrypt', 'unwrapKey'];
|
|
78
|
+
break;
|
|
79
|
+
case 'RSA-PSS':
|
|
80
|
+
// Fall through
|
|
81
|
+
case 'RSASSA-PKCS1-v1_5':
|
|
82
|
+
checkSet = isPublic ? ['verify'] : ['sign'];
|
|
83
|
+
break;
|
|
84
|
+
default:
|
|
85
|
+
throw (0, _Utils.lazyDOMException)('The algorithm is not supported', 'NotSupportedError');
|
|
86
|
+
}
|
|
87
|
+
if ((0, _Utils.hasAnyNotIn)(usages, checkSet)) {
|
|
88
|
+
throw (0, _Utils.lazyDOMException)(`Unsupported key usage for an ${name} key`, 'SyntaxError');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// function rsaOaepCipher(mode, key, data, { label }) {
|
|
93
|
+
// const type = mode === kWebCryptoCipherEncrypt ? 'public' : 'private';
|
|
94
|
+
// if (key.type !== type) {
|
|
95
|
+
// throw lazyDOMException(
|
|
96
|
+
// 'The requested operation is not valid for the provided key',
|
|
97
|
+
// 'InvalidAccessError');
|
|
98
|
+
// }
|
|
99
|
+
// if (label !== undefined) {
|
|
100
|
+
// validateMaxBufferLength(label, 'algorithm.label');
|
|
101
|
+
// }
|
|
102
|
+
|
|
103
|
+
// return jobPromise(() => new RSACipherJob(
|
|
104
|
+
// kCryptoJobAsync,
|
|
105
|
+
// mode,
|
|
106
|
+
// key[kKeyObject][kHandle],
|
|
107
|
+
// data,
|
|
108
|
+
// kKeyVariantRSA_OAEP,
|
|
109
|
+
// normalizeHashName(key.algorithm.hash.name),
|
|
110
|
+
// label));
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
// async function rsaKeyGenerate(
|
|
114
|
+
// algorithm,
|
|
115
|
+
// extractable,
|
|
116
|
+
// keyUsages) {
|
|
117
|
+
|
|
118
|
+
// const {
|
|
119
|
+
// name,
|
|
120
|
+
// modulusLength,
|
|
121
|
+
// publicExponent,
|
|
122
|
+
// hash,
|
|
123
|
+
// } = algorithm;
|
|
124
|
+
|
|
125
|
+
// const usageSet = new SafeSet(keyUsages);
|
|
126
|
+
|
|
127
|
+
// const publicExponentConverted = bigIntArrayToUnsignedInt(publicExponent);
|
|
128
|
+
// if (publicExponentConverted === undefined) {
|
|
129
|
+
// throw lazyDOMException(
|
|
130
|
+
// 'The publicExponent must be equivalent to an unsigned 32-bit value',
|
|
131
|
+
// 'OperationError');
|
|
132
|
+
// }
|
|
133
|
+
|
|
134
|
+
// switch (name) {
|
|
135
|
+
// case 'RSA-OAEP':
|
|
136
|
+
// if (hasAnyNotIn(usageSet,
|
|
137
|
+
// ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'])) {
|
|
138
|
+
// throw lazyDOMException(
|
|
139
|
+
// 'Unsupported key usage for a RSA key',
|
|
140
|
+
// 'SyntaxError');
|
|
141
|
+
// }
|
|
142
|
+
// break;
|
|
143
|
+
// default:
|
|
144
|
+
// if (hasAnyNotIn(usageSet, ['sign', 'verify'])) {
|
|
145
|
+
// throw lazyDOMException(
|
|
146
|
+
// 'Unsupported key usage for a RSA key',
|
|
147
|
+
// 'SyntaxError');
|
|
148
|
+
// }
|
|
149
|
+
// }
|
|
150
|
+
|
|
151
|
+
// const keypair = await generateKeyPair('rsa', {
|
|
152
|
+
// modulusLength,
|
|
153
|
+
// publicExponent: publicExponentConverted,
|
|
154
|
+
// }).catch((err) => {
|
|
155
|
+
// throw lazyDOMException(
|
|
156
|
+
// 'The operation failed for an operation-specific reason',
|
|
157
|
+
// { name: 'OperationError', cause: err });
|
|
158
|
+
// });
|
|
159
|
+
|
|
160
|
+
// const keyAlgorithm = {
|
|
161
|
+
// name,
|
|
162
|
+
// modulusLength,
|
|
163
|
+
// publicExponent,
|
|
164
|
+
// hash: { name: hash.name },
|
|
165
|
+
// };
|
|
166
|
+
|
|
167
|
+
// let publicUsages;
|
|
168
|
+
// let privateUsages;
|
|
169
|
+
// switch (name) {
|
|
170
|
+
// case 'RSA-OAEP': {
|
|
171
|
+
// publicUsages = getUsagesUnion(usageSet, 'encrypt', 'wrapKey');
|
|
172
|
+
// privateUsages = getUsagesUnion(usageSet, 'decrypt', 'unwrapKey');
|
|
173
|
+
// break;
|
|
174
|
+
// }
|
|
175
|
+
// default: {
|
|
176
|
+
// publicUsages = getUsagesUnion(usageSet, 'verify');
|
|
177
|
+
// privateUsages = getUsagesUnion(usageSet, 'sign');
|
|
178
|
+
// break;
|
|
179
|
+
// }
|
|
180
|
+
// }
|
|
181
|
+
|
|
182
|
+
// const publicKey =
|
|
183
|
+
// new InternalCryptoKey(
|
|
184
|
+
// keypair.publicKey,
|
|
185
|
+
// keyAlgorithm,
|
|
186
|
+
// publicUsages,
|
|
187
|
+
// true);
|
|
188
|
+
|
|
189
|
+
// const privateKey =
|
|
190
|
+
// new InternalCryptoKey(
|
|
191
|
+
// keypair.privateKey,
|
|
192
|
+
// keyAlgorithm,
|
|
193
|
+
// privateUsages,
|
|
194
|
+
// extractable);
|
|
195
|
+
|
|
196
|
+
// return { __proto__: null, publicKey, privateKey };
|
|
197
|
+
// }
|
|
198
|
+
|
|
199
|
+
// function rsaExportKey(key, format) {
|
|
200
|
+
// return jobPromise(() => new RSAKeyExportJob(
|
|
201
|
+
// kCryptoJobAsync,
|
|
202
|
+
// format,
|
|
203
|
+
// key[kKeyObject][kHandle],
|
|
204
|
+
// kRsaVariants[key.algorithm.name]));
|
|
205
|
+
// }
|
|
206
|
+
|
|
207
|
+
const rsaImportKey = (format, keyData, algorithm, extractable, keyUsages) => {
|
|
208
|
+
// const usagesSet = new SafeSet(keyUsages);
|
|
209
|
+
let keyObject;
|
|
210
|
+
switch (format) {
|
|
211
|
+
// case 'spki': {
|
|
212
|
+
// verifyAcceptableRsaKeyUse(algorithm.name, true, keyUsages);
|
|
213
|
+
// try {
|
|
214
|
+
// keyObject = createPublicKey({
|
|
215
|
+
// key: keyData,
|
|
216
|
+
// format: 'der',
|
|
217
|
+
// type: 'spki',
|
|
218
|
+
// });
|
|
219
|
+
// } catch (err) {
|
|
220
|
+
// throw lazyDOMException('Invalid keyData', {
|
|
221
|
+
// name: 'DataError',
|
|
222
|
+
// cause: err,
|
|
223
|
+
// });
|
|
224
|
+
// }
|
|
225
|
+
// break;
|
|
226
|
+
// }
|
|
227
|
+
// case 'pkcs8': {
|
|
228
|
+
// verifyAcceptableRsaKeyUse(algorithm.name, false, keyUsages);
|
|
229
|
+
// try {
|
|
230
|
+
// keyObject = createPrivateKey({
|
|
231
|
+
// key: keyData,
|
|
232
|
+
// format: 'der',
|
|
233
|
+
// type: 'pkcs8',
|
|
234
|
+
// });
|
|
235
|
+
// } catch (err) {
|
|
236
|
+
// throw lazyDOMException('Invalid keyData', {
|
|
237
|
+
// name: 'DataError',
|
|
238
|
+
// cause: err,
|
|
239
|
+
// });
|
|
240
|
+
// }
|
|
241
|
+
// break;
|
|
242
|
+
// }
|
|
243
|
+
case 'jwk':
|
|
244
|
+
{
|
|
245
|
+
const data = keyData;
|
|
246
|
+
if (!data.kty) {
|
|
247
|
+
throw (0, _Utils.lazyDOMException)('Invalid keyData', 'DataError');
|
|
248
|
+
}
|
|
249
|
+
if (data.kty !== 'RSA') throw (0, _Utils.lazyDOMException)('Invalid JWK "kty" Parameter', 'DataError');
|
|
250
|
+
verifyAcceptableRsaKeyUse(algorithm.name, data.d === undefined, keyUsages);
|
|
251
|
+
if (keyUsages.length > 0 && data.use !== undefined) {
|
|
252
|
+
const checkUse = algorithm.name === 'RSA-OAEP' ? 'enc' : 'sig';
|
|
253
|
+
if (data.use !== checkUse) throw (0, _Utils.lazyDOMException)('Invalid JWK "use" Parameter', 'DataError');
|
|
254
|
+
}
|
|
255
|
+
(0, _Utils.validateKeyOps)(data.key_ops, keyUsages);
|
|
256
|
+
if (data.ext !== undefined && data.ext === false && extractable === true) {
|
|
257
|
+
throw (0, _Utils.lazyDOMException)('JWK "ext" Parameter and extractable mismatch', 'DataError');
|
|
258
|
+
}
|
|
259
|
+
if (data.alg !== undefined) {
|
|
260
|
+
const hash = (0, _Utils.normalizeHashName)(data.alg, _Utils.HashContext.WebCrypto);
|
|
261
|
+
if (hash !== algorithm.hash) throw (0, _Utils.lazyDOMException)('JWK "alg" does not match the requested algorithm', 'DataError');
|
|
262
|
+
}
|
|
263
|
+
const handle = _NativeQuickCrypto.NativeQuickCrypto.webcrypto.createKeyObjectHandle();
|
|
264
|
+
const type = handle.initJwk(data);
|
|
265
|
+
if (type === undefined) throw (0, _Utils.lazyDOMException)('Invalid JWK', 'DataError');
|
|
266
|
+
keyObject = type === _keys.KeyType.Private ? new _keys.PrivateKeyObject(handle) : new _keys.PublicKeyObject(handle);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
default:
|
|
270
|
+
throw (0, _Utils.lazyDOMException)(`Unable to import RSA key with format ${format}`, 'NotSupportedError');
|
|
271
|
+
}
|
|
272
|
+
if (keyObject.asymmetricKeyType !== 'rsa') {
|
|
273
|
+
throw (0, _Utils.lazyDOMException)('Invalid key type', 'DataError');
|
|
274
|
+
}
|
|
275
|
+
const {
|
|
276
|
+
modulusLength,
|
|
277
|
+
publicExponent
|
|
278
|
+
} = keyObject.handle.keyDetail();
|
|
279
|
+
if (publicExponent === undefined) {
|
|
280
|
+
throw (0, _Utils.lazyDOMException)('publicExponent is undefined', 'DataError');
|
|
281
|
+
}
|
|
282
|
+
return new _keys.CryptoKey(keyObject, {
|
|
283
|
+
name: algorithm.name,
|
|
284
|
+
modulusLength,
|
|
285
|
+
publicExponent: new Uint8Array(publicExponent),
|
|
286
|
+
hash: algorithm.hash
|
|
287
|
+
}, keyUsages, extractable);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// function rsaSignVerify(key, data, { saltLength }, signature) {
|
|
291
|
+
// let padding;
|
|
292
|
+
// if (key.algorithm.name === 'RSA-PSS') {
|
|
293
|
+
// padding = RSA_PKCS1_PSS_PADDING;
|
|
294
|
+
// // TODO(@jasnell): Validate maximum size of saltLength
|
|
295
|
+
// // based on the key size:
|
|
296
|
+
// // Math.ceil((keySizeInBits - 1)/8) - digestSizeInBytes - 2
|
|
297
|
+
// validateInt32(saltLength, 'algorithm.saltLength', -2);
|
|
298
|
+
// }
|
|
299
|
+
|
|
300
|
+
// const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
|
|
301
|
+
// const type = mode === kSignJobModeSign ? 'private' : 'public';
|
|
302
|
+
|
|
303
|
+
// if (key.type !== type)
|
|
304
|
+
// throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
|
|
305
|
+
|
|
306
|
+
// return jobPromise(() => new SignJob(
|
|
307
|
+
// kCryptoJobAsync,
|
|
308
|
+
// signature === undefined ? kSignJobModeSign : kSignJobModeVerify,
|
|
309
|
+
// key[kKeyObject][kHandle],
|
|
310
|
+
// undefined,
|
|
311
|
+
// undefined,
|
|
312
|
+
// undefined,
|
|
313
|
+
// data,
|
|
314
|
+
// normalizeHashName(key.algorithm.hash.name),
|
|
315
|
+
// saltLength,
|
|
316
|
+
// padding,
|
|
317
|
+
// undefined,
|
|
318
|
+
// signature));
|
|
319
|
+
// }
|
|
320
|
+
|
|
321
|
+
// module.exports = {
|
|
322
|
+
// rsaCipher: rsaOaepCipher,
|
|
323
|
+
// rsaExportKey,
|
|
324
|
+
// rsaImportKey,
|
|
325
|
+
// rsaKeyGenerate,
|
|
326
|
+
// rsaSignVerify,
|
|
327
|
+
// };
|
|
328
|
+
exports.rsaImportKey = rsaImportKey;
|
|
329
|
+
//# sourceMappingURL=rsa.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_NativeQuickCrypto","require","_Utils","_keys","verifyAcceptableRsaKeyUse","name","isPublic","usages","checkSet","lazyDOMException","hasAnyNotIn","rsaImportKey","format","keyData","algorithm","extractable","keyUsages","keyObject","data","kty","d","undefined","length","use","checkUse","validateKeyOps","key_ops","ext","alg","hash","normalizeHashName","HashContext","WebCrypto","handle","NativeQuickCrypto","webcrypto","createKeyObjectHandle","type","initJwk","KeyType","Private","PrivateKeyObject","PublicKeyObject","asymmetricKeyType","modulusLength","publicExponent","keyDetail","CryptoKey","Uint8Array","exports"],"sourceRoot":"../../src","sources":["rsa.ts"],"mappings":";;;;;;AAEA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAQA,IAAAE,KAAA,GAAAF,OAAA;AAXA;;AAwBA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASG,yBAAyBA,CAChCC,IAAkB,EAClBC,QAAiB,EACjBC,MAAkB,EACZ;EACN,IAAIC,QAAQ;EACZ,QAAQH,IAAI;IACV,KAAK,UAAU;MACbG,QAAQ,GAAGF,QAAQ,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC;MACvE;IACF,KAAK,SAAS;IACd;IACA,KAAK,mBAAmB;MACtBE,QAAQ,GAAGF,QAAQ,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC;MAC3C;IACF;MACE,MAAM,IAAAG,uBAAgB,EACpB,gCAAgC,EAChC,mBACF,CAAC;EACL;EACA,IAAI,IAAAC,kBAAW,EAACH,MAAM,EAAEC,QAAQ,CAAC,EAAE;IACjC,MAAM,IAAAC,uBAAgB,EACnB,gCAA+BJ,IAAK,MAAK,EAC1C,aACF,CAAC;EACH;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEO,MAAMM,YAAY,GAAGA,CAC1BC,MAAoB,EACpBC,OAAyB,EACzBC,SAA0B,EAC1BC,WAAoB,EACpBC,SAAqB,KACP;EACd;EACA,IAAIC,SAAS;EACb,QAAQL,MAAM;IACZ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