react-native-quick-crypto 0.6.0 → 0.7.0-rc.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.
Files changed (196) hide show
  1. package/README.md +70 -18
  2. package/android/CMakeLists.txt +58 -61
  3. package/android/build.gradle +105 -53
  4. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  5. package/android/gradle.properties +5 -5
  6. package/android/src/main/{AndroidManifest.xml → AndroidManifestNew.xml} +1 -2
  7. package/cpp/Cipher/MGLCipherHostObject.cpp +31 -5
  8. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +21 -31
  9. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +4 -17
  10. package/cpp/Cipher/MGLPublicCipher.h +1 -1
  11. package/cpp/Cipher/MGLPublicCipherInstaller.h +6 -6
  12. package/cpp/Cipher/MGLRsa.cpp +224 -11
  13. package/cpp/Cipher/MGLRsa.h +13 -3
  14. package/cpp/Hash/MGLHashHostObject.cpp +1 -1
  15. package/cpp/Hash/MGLHashInstaller.cpp +2 -4
  16. package/cpp/JSIUtils/MGLJSIMacros.h +10 -0
  17. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
  18. package/cpp/MGLKeys.cpp +415 -471
  19. package/cpp/MGLKeys.h +70 -2
  20. package/cpp/MGLQuickCryptoHostObject.cpp +9 -0
  21. package/cpp/Random/MGLRandomHostObject.cpp +9 -2
  22. package/cpp/Sig/MGLSignHostObjects.cpp +1 -1
  23. package/cpp/Utils/MGLUtils.cpp +104 -32
  24. package/cpp/Utils/MGLUtils.h +172 -143
  25. package/cpp/Utils/node.h +13 -0
  26. package/cpp/webcrypto/MGLWebCrypto.cpp +63 -0
  27. package/cpp/webcrypto/MGLWebCrypto.h +34 -0
  28. package/cpp/webcrypto/crypto_ec.cpp +334 -0
  29. package/cpp/webcrypto/crypto_ec.h +65 -0
  30. package/ios/QuickCrypto.xcodeproj/project.pbxproj +4 -4
  31. package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -1
  32. package/lib/commonjs/Cipher.js +53 -194
  33. package/lib/commonjs/Cipher.js.map +1 -1
  34. package/lib/commonjs/Hash.js +29 -29
  35. package/lib/commonjs/Hash.js.map +1 -1
  36. package/lib/commonjs/Hashnames.js +75 -0
  37. package/lib/commonjs/Hashnames.js.map +1 -0
  38. package/lib/commonjs/Hmac.js +6 -29
  39. package/lib/commonjs/Hmac.js.map +1 -1
  40. package/lib/commonjs/NativeQuickCrypto/Cipher.js +3 -5
  41. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  42. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -20
  43. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  44. package/lib/commonjs/NativeQuickCrypto/hash.js.map +1 -1
  45. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +1 -1
  46. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +1 -1
  47. package/lib/commonjs/NativeQuickCrypto/random.js.map +1 -1
  48. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  49. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +6 -0
  50. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +1 -0
  51. package/lib/commonjs/QuickCrypto.js +5 -14
  52. package/lib/commonjs/QuickCrypto.js.map +1 -1
  53. package/lib/commonjs/Utils.js +360 -48
  54. package/lib/commonjs/Utils.js.map +1 -1
  55. package/lib/commonjs/aes.js +324 -0
  56. package/lib/commonjs/aes.js.map +1 -0
  57. package/lib/commonjs/constants.js +1 -2
  58. package/lib/commonjs/constants.js.map +1 -1
  59. package/lib/commonjs/ec.js +288 -0
  60. package/lib/commonjs/ec.js.map +1 -0
  61. package/lib/commonjs/index.js +6 -10
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/commonjs/keys.js +280 -48
  64. package/lib/commonjs/keys.js.map +1 -1
  65. package/lib/commonjs/pbkdf2.js +44 -18
  66. package/lib/commonjs/pbkdf2.js.map +1 -1
  67. package/lib/commonjs/random.js +49 -68
  68. package/lib/commonjs/random.js.map +1 -1
  69. package/lib/commonjs/rsa.js +329 -0
  70. package/lib/commonjs/rsa.js.map +1 -0
  71. package/lib/commonjs/sig.js +13 -54
  72. package/lib/commonjs/sig.js.map +1 -1
  73. package/lib/commonjs/subtle.js +271 -0
  74. package/lib/commonjs/subtle.js.map +1 -0
  75. package/lib/module/@types/crypto-browserify.d.js.map +1 -1
  76. package/lib/module/Cipher.js +53 -188
  77. package/lib/module/Cipher.js.map +1 -1
  78. package/lib/module/Hash.js +27 -21
  79. package/lib/module/Hash.js.map +1 -1
  80. package/lib/module/Hashnames.js +71 -0
  81. package/lib/module/Hashnames.js.map +1 -0
  82. package/lib/module/Hmac.js +4 -21
  83. package/lib/module/Hmac.js.map +1 -1
  84. package/lib/module/NativeQuickCrypto/Cipher.js +3 -4
  85. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  86. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +11 -17
  87. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  88. package/lib/module/NativeQuickCrypto/hash.js.map +1 -1
  89. package/lib/module/NativeQuickCrypto/hmac.js.map +1 -1
  90. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +1 -1
  91. package/lib/module/NativeQuickCrypto/random.js.map +1 -1
  92. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  93. package/lib/module/NativeQuickCrypto/webcrypto.js +2 -0
  94. package/lib/module/NativeQuickCrypto/webcrypto.js.map +1 -0
  95. package/lib/module/QuickCrypto.js +2 -0
  96. package/lib/module/QuickCrypto.js.map +1 -1
  97. package/lib/module/Utils.js +319 -33
  98. package/lib/module/Utils.js.map +1 -1
  99. package/lib/module/aes.js +317 -0
  100. package/lib/module/aes.js.map +1 -0
  101. package/lib/module/constants.js.map +1 -1
  102. package/lib/module/ec.js +282 -0
  103. package/lib/module/ec.js.map +1 -0
  104. package/lib/module/index.js +7 -4
  105. package/lib/module/index.js.map +1 -1
  106. package/lib/module/keys.js +279 -43
  107. package/lib/module/keys.js.map +1 -1
  108. package/lib/module/pbkdf2.js +44 -13
  109. package/lib/module/pbkdf2.js.map +1 -1
  110. package/lib/module/random.js +46 -54
  111. package/lib/module/random.js.map +1 -1
  112. package/lib/module/rsa.js +323 -0
  113. package/lib/module/rsa.js.map +1 -0
  114. package/lib/module/sig.js +13 -46
  115. package/lib/module/sig.js.map +1 -1
  116. package/lib/module/subtle.js +265 -0
  117. package/lib/module/subtle.js.map +1 -0
  118. package/lib/typescript/src/Cipher.d.ts +72 -0
  119. package/lib/typescript/src/Cipher.d.ts.map +1 -0
  120. package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +8 -7
  121. package/lib/typescript/src/Hash.d.ts.map +1 -0
  122. package/lib/typescript/src/Hashnames.d.ts +11 -0
  123. package/lib/typescript/src/Hashnames.d.ts.map +1 -0
  124. package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +3 -3
  125. package/lib/typescript/src/Hmac.d.ts.map +1 -0
  126. package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts +34 -0
  127. package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
  128. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +3 -0
  129. package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
  130. package/lib/typescript/src/NativeQuickCrypto/hash.d.ts +7 -0
  131. package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
  132. package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts +6 -0
  133. package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
  134. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +2 -1
  135. package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
  136. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +2 -1
  137. package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
  138. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +5 -4
  139. package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
  140. package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts +27 -0
  141. package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
  142. package/lib/typescript/{QuickCrypto.d.ts → src/QuickCrypto.d.ts} +16 -8
  143. package/lib/typescript/src/QuickCrypto.d.ts.map +1 -0
  144. package/lib/typescript/src/Utils.d.ts +44 -0
  145. package/lib/typescript/src/Utils.d.ts.map +1 -0
  146. package/lib/typescript/src/aes.d.ts +5 -0
  147. package/lib/typescript/src/aes.d.ts.map +1 -0
  148. package/lib/typescript/{constants.d.ts → src/constants.d.ts} +1 -0
  149. package/lib/typescript/src/constants.d.ts.map +1 -0
  150. package/lib/typescript/src/ec.d.ts +5 -0
  151. package/lib/typescript/src/ec.d.ts.map +1 -0
  152. package/lib/typescript/{index.d.ts → src/index.d.ts} +33 -27
  153. package/lib/typescript/src/index.d.ts.map +1 -0
  154. package/lib/typescript/src/keys.d.ts +154 -0
  155. package/lib/typescript/src/keys.d.ts.map +1 -0
  156. package/lib/typescript/src/pbkdf2.d.ts +12 -0
  157. package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
  158. package/lib/typescript/{random.d.ts → src/random.d.ts} +7 -6
  159. package/lib/typescript/src/random.d.ts.map +1 -0
  160. package/lib/typescript/src/rsa.d.ts +4 -0
  161. package/lib/typescript/src/rsa.d.ts.map +1 -0
  162. package/lib/typescript/{sig.d.ts → src/sig.d.ts} +3 -3
  163. package/lib/typescript/src/sig.d.ts.map +1 -0
  164. package/lib/typescript/src/subtle.d.ts +11 -0
  165. package/lib/typescript/src/subtle.d.ts.map +1 -0
  166. package/package.json +35 -30
  167. package/react-native-quick-crypto.podspec +5 -4
  168. package/src/Cipher.ts +103 -100
  169. package/src/Hash.ts +42 -6
  170. package/src/Hashnames.ts +91 -0
  171. package/src/Hmac.ts +3 -3
  172. package/src/NativeQuickCrypto/Cipher.ts +1 -0
  173. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +2 -0
  174. package/src/NativeQuickCrypto/webcrypto.ts +46 -0
  175. package/src/QuickCrypto.ts +2 -0
  176. package/src/Utils.ts +409 -5
  177. package/src/aes.ts +365 -0
  178. package/src/ec.ts +351 -0
  179. package/src/keys.ts +428 -54
  180. package/src/pbkdf2.ts +84 -11
  181. package/src/random.ts +37 -24
  182. package/src/rsa.ts +396 -0
  183. package/src/sig.ts +3 -2
  184. package/src/subtle.ts +358 -0
  185. package/lib/commonjs/@types/stream-browserify.d.js +0 -2
  186. package/lib/commonjs/@types/stream-browserify.d.js.map +0 -1
  187. package/lib/module/@types/stream-browserify.d.js +0 -2
  188. package/lib/module/@types/stream-browserify.d.js.map +0 -1
  189. package/lib/typescript/Cipher.d.ts +0 -87
  190. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -32
  191. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -6
  192. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -5
  193. package/lib/typescript/Utils.d.ts +0 -23
  194. package/lib/typescript/keys.d.ts +0 -60
  195. package/lib/typescript/pbkdf2.d.ts +0 -9
  196. package/src/@types/stream-browserify.d.ts +0 -4
package/src/subtle.ts ADDED
@@ -0,0 +1,358 @@
1
+ import {
2
+ type ImportFormat,
3
+ type SubtleAlgorithm,
4
+ type KeyUsage,
5
+ CryptoKey,
6
+ KWebCryptoKeyFormat,
7
+ createSecretKey,
8
+ type AnyAlgorithm,
9
+ type JWK,
10
+ } from './keys';
11
+ import {
12
+ hasAnyNotIn,
13
+ type BufferLike,
14
+ type BinaryLike,
15
+ normalizeAlgorithm,
16
+ lazyDOMException,
17
+ normalizeHashName,
18
+ HashContext,
19
+ } from './Utils';
20
+ import { ecImportKey, ecExportKey } from './ec';
21
+ import { pbkdf2DeriveBits } from './pbkdf2';
22
+ import { asyncDigest } from './Hash';
23
+ import { aesImportKey, getAlgorithmName } from './aes';
24
+ import { rsaImportKey } from './rsa';
25
+
26
+ const exportKeySpki = async (key: CryptoKey): Promise<ArrayBuffer | any> => {
27
+ switch (key.algorithm.name) {
28
+ // case 'RSASSA-PKCS1-v1_5':
29
+ // // Fall through
30
+ // case 'RSA-PSS':
31
+ // // Fall through
32
+ // case 'RSA-OAEP':
33
+ // if (key.type === 'public') {
34
+ // return require('internal/crypto/rsa').rsaExportKey(
35
+ // key,
36
+ // kWebCryptoKeyFormatSPKI
37
+ // );
38
+ // }
39
+ // break;
40
+ case 'ECDSA':
41
+ // Fall through
42
+ case 'ECDH':
43
+ if (key.type === 'public') {
44
+ return ecExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatSPKI);
45
+ }
46
+ break;
47
+ // case 'Ed25519':
48
+ // // Fall through
49
+ // case 'Ed448':
50
+ // // Fall through
51
+ // case 'X25519':
52
+ // // Fall through
53
+ // case 'X448':
54
+ // if (key.type === 'public') {
55
+ // return require('internal/crypto/cfrg').cfrgExportKey(
56
+ // key,
57
+ // kWebCryptoKeyFormatSPKI
58
+ // );
59
+ // }
60
+ // break;
61
+ }
62
+
63
+ throw new Error(
64
+ `Unable to export a raw ${key.algorithm.name} ${key.type} key`
65
+ );
66
+ };
67
+
68
+ const exportKeyRaw = (key: CryptoKey): ArrayBuffer | any => {
69
+ switch (key.algorithm.name) {
70
+ case 'ECDSA':
71
+ // Fall through
72
+ case 'ECDH':
73
+ if (key.type === 'public') {
74
+ return ecExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatRaw);
75
+ }
76
+ break;
77
+ // case 'Ed25519':
78
+ // // Fall through
79
+ // case 'Ed448':
80
+ // // Fall through
81
+ // case 'X25519':
82
+ // // Fall through
83
+ // case 'X448':
84
+ // if (key.type === 'public') {
85
+ // return require('internal/crypto/cfrg')
86
+ // .cfrgExportKey(key, kWebCryptoKeyFormatRaw);
87
+ // }
88
+ // break;
89
+ case 'AES-CTR':
90
+ // Fall through
91
+ case 'AES-CBC':
92
+ // Fall through
93
+ case 'AES-GCM':
94
+ // Fall through
95
+ case 'AES-KW':
96
+ // Fall through
97
+ case 'HMAC':
98
+ return key.keyObject.export();
99
+ }
100
+
101
+ throw lazyDOMException(
102
+ `Unable to export a raw ${key.algorithm.name} ${key.type} key`,
103
+ 'InvalidAccessError'
104
+ );
105
+ };
106
+
107
+ const exportKeyJWK = (key: CryptoKey): ArrayBuffer | any => {
108
+ const jwk = key.keyObject.handle.exportJwk(
109
+ {
110
+ key_ops: key.usages,
111
+ ext: key.extractable,
112
+ },
113
+ true
114
+ );
115
+ switch (key.algorithm.name) {
116
+ case 'RSASSA-PKCS1-v1_5':
117
+ jwk.alg = normalizeHashName(key.algorithm.hash, HashContext.JwkRsa);
118
+ return jwk;
119
+ case 'RSA-PSS':
120
+ jwk.alg = normalizeHashName(key.algorithm.hash, HashContext.JwkRsaPss);
121
+ return jwk;
122
+ case 'RSA-OAEP':
123
+ jwk.alg = normalizeHashName(key.algorithm.hash, HashContext.JwkRsaOaep);
124
+ return jwk;
125
+ case 'ECDSA':
126
+ // Fall through
127
+ case 'ECDH':
128
+ jwk.crv ||= key.algorithm.namedCurve;
129
+ return jwk;
130
+ // case 'X25519':
131
+ // // Fall through
132
+ // case 'X448':
133
+ // jwk.crv ||= key.algorithm.name;
134
+ // return jwk;
135
+ // case 'Ed25519':
136
+ // // Fall through
137
+ // case 'Ed448':
138
+ // jwk.crv ||= key.algorithm.name;
139
+ // return jwk;
140
+ case 'AES-CTR':
141
+ // Fall through
142
+ case 'AES-CBC':
143
+ // Fall through
144
+ case 'AES-GCM':
145
+ // Fall through
146
+ case 'AES-KW':
147
+ jwk.alg = getAlgorithmName(key.algorithm.name, key.algorithm.length);
148
+ return jwk;
149
+ // case 'HMAC':
150
+ // jwk.alg = normalizeHashName(
151
+ // key.algorithm.hash.name,
152
+ // normalizeHashName.kContextJwkHmac);
153
+ // return jwk;
154
+ default:
155
+ // Fall through
156
+ }
157
+
158
+ throw lazyDOMException(
159
+ `JWK export not yet supported: ${key.algorithm.name}`,
160
+ 'NotSupportedError'
161
+ );
162
+ };
163
+
164
+ const importGenericSecretKey = async (
165
+ { name, length }: SubtleAlgorithm,
166
+ format: ImportFormat,
167
+ keyData: BufferLike | BinaryLike,
168
+ extractable: boolean,
169
+ keyUsages: KeyUsage[]
170
+ ): Promise<CryptoKey> => {
171
+ if (extractable) {
172
+ throw new Error(`${name} keys are not extractable`);
173
+ }
174
+ if (hasAnyNotIn(keyUsages, ['deriveKey', 'deriveBits'])) {
175
+ throw new Error(`Unsupported key usage for a ${name} key`);
176
+ }
177
+
178
+ switch (format) {
179
+ case 'raw': {
180
+ if (hasAnyNotIn(keyUsages, ['deriveKey', 'deriveBits'])) {
181
+ throw new Error(`Unsupported key usage for a ${name} key`);
182
+ }
183
+
184
+ const checkLength =
185
+ typeof keyData === 'string'
186
+ ? keyData.length * 8
187
+ : keyData.byteLength * 8;
188
+
189
+ // The Web Crypto spec allows for key lengths that are not multiples of
190
+ // 8. We don't. Our check here is stricter than that defined by the spec
191
+ // in that we require that algorithm.length match keyData.length * 8 if
192
+ // algorithm.length is specified.
193
+ if (length !== undefined && length !== checkLength) {
194
+ throw new Error('Invalid key length');
195
+ }
196
+
197
+ const keyObject = createSecretKey(keyData);
198
+ return new CryptoKey(keyObject, { name }, keyUsages, false);
199
+ }
200
+ }
201
+
202
+ throw new Error(`Unable to import ${name} key with format ${format}`);
203
+ };
204
+
205
+ class Subtle {
206
+ async digest(
207
+ algorithm: SubtleAlgorithm | AnyAlgorithm,
208
+ data: BufferLike
209
+ ): Promise<ArrayBuffer> {
210
+ const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'digest');
211
+ return asyncDigest(normalizedAlgorithm, data);
212
+ }
213
+
214
+ async deriveBits(
215
+ algorithm: SubtleAlgorithm,
216
+ baseKey: CryptoKey,
217
+ length: number
218
+ ): Promise<ArrayBuffer> {
219
+ if (!baseKey.keyUsages.includes('deriveBits')) {
220
+ throw new Error('baseKey does not have deriveBits usage');
221
+ }
222
+ if (baseKey.algorithm.name !== algorithm.name)
223
+ throw new Error('Key algorithm mismatch');
224
+ switch (algorithm.name) {
225
+ // case 'X25519':
226
+ // // Fall through
227
+ // case 'X448':
228
+ // // Fall through
229
+ // case 'ECDH':
230
+ // return require('internal/crypto/diffiehellman')
231
+ // .ecdhDeriveBits(algorithm, baseKey, length);
232
+ // case 'HKDF':
233
+ // return require('internal/crypto/hkdf')
234
+ // .hkdfDeriveBits(algorithm, baseKey, length);
235
+ case 'PBKDF2':
236
+ return pbkdf2DeriveBits(algorithm, baseKey, length);
237
+ }
238
+ throw new Error(
239
+ `'subtle.deriveBits()' for ${algorithm.name} is not implemented.`
240
+ );
241
+ }
242
+
243
+ async importKey(
244
+ format: ImportFormat,
245
+ data: BufferLike | BinaryLike | JWK,
246
+ algorithm: SubtleAlgorithm,
247
+ extractable: boolean,
248
+ keyUsages: KeyUsage[]
249
+ ): Promise<CryptoKey> {
250
+ let result: CryptoKey;
251
+ switch (algorithm.name) {
252
+ case 'RSASSA-PKCS1-v1_5':
253
+ // Fall through
254
+ case 'RSA-PSS':
255
+ // Fall through
256
+ case 'RSA-OAEP':
257
+ result = rsaImportKey(
258
+ format,
259
+ data as BufferLike | JWK,
260
+ algorithm,
261
+ extractable,
262
+ keyUsages
263
+ );
264
+ break;
265
+ case 'ECDSA':
266
+ // Fall through
267
+ case 'ECDH':
268
+ result = ecImportKey(format, data, algorithm, extractable, keyUsages);
269
+ break;
270
+ // case 'Ed25519':
271
+ // // Fall through
272
+ // case 'Ed448':
273
+ // // Fall through
274
+ // case 'X25519':
275
+ // // Fall through
276
+ // case 'X448':
277
+ // result = await require('internal/crypto/cfrg').cfrgImportKey(
278
+ // format,
279
+ // keyData,
280
+ // algorithm,
281
+ // extractable,
282
+ // keyUsages
283
+ // );
284
+ // break;
285
+ // case 'HMAC':
286
+ // result = await require('internal/crypto/mac').hmacImportKey(
287
+ // format,
288
+ // keyData,
289
+ // algorithm,
290
+ // extractable,
291
+ // keyUsages
292
+ // );
293
+ // break;
294
+ case 'AES-CTR':
295
+ // Fall through
296
+ case 'AES-CBC':
297
+ // Fall through
298
+ case 'AES-GCM':
299
+ // Fall through
300
+ case 'AES-KW':
301
+ result = await aesImportKey(
302
+ algorithm,
303
+ format,
304
+ data as BufferLike | JWK,
305
+ extractable,
306
+ keyUsages
307
+ );
308
+ break;
309
+ // case 'HKDF':
310
+ // // Fall through
311
+ case 'PBKDF2':
312
+ result = await importGenericSecretKey(
313
+ algorithm,
314
+ format,
315
+ data as BufferLike | BinaryLike,
316
+ extractable,
317
+ keyUsages
318
+ );
319
+ break;
320
+ default:
321
+ throw new Error(
322
+ `"subtle.importKey()" is not implemented for ${algorithm.name}`
323
+ );
324
+ }
325
+
326
+ if (
327
+ (result.type === 'secret' || result.type === 'private') &&
328
+ result.usages.length === 0
329
+ ) {
330
+ throw new Error(
331
+ `Usages cannot be empty when importing a ${result.type} key.`
332
+ );
333
+ }
334
+
335
+ return result;
336
+ }
337
+
338
+ async exportKey(
339
+ format: ImportFormat,
340
+ key: CryptoKey
341
+ ): Promise<ArrayBuffer | any> {
342
+ if (!key.extractable) throw new Error('key is not extractable');
343
+
344
+ switch (format) {
345
+ case 'spki':
346
+ return await exportKeySpki(key);
347
+ // case 'pkcs8':
348
+ // return await exportKeyPkcs8(key);
349
+ case 'jwk':
350
+ return exportKeyJWK(key);
351
+ case 'raw':
352
+ return exportKeyRaw(key);
353
+ }
354
+ throw new Error(`'subtle.exportKey()' is not implemented for ${format}`);
355
+ }
356
+ }
357
+
358
+ export const subtle = new Subtle();
@@ -1,2 +0,0 @@
1
- "use strict";
2
- //# sourceMappingURL=stream-browserify.d.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
@@ -1,2 +0,0 @@
1
-
2
- //# sourceMappingURL=stream-browserify.d.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
@@ -1,87 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import Stream from 'stream-browserify';
4
- import { BinaryLike, CipherEncoding, Encoding } from './Utils';
5
- import { Buffer } from '@craftzdog/react-native-buffer';
6
- declare class CipherCommon extends Stream.Transform {
7
- private internal;
8
- private decoder;
9
- constructor(cipherType: string, cipherKey: BinaryLike, isCipher: boolean, options?: Record<string, any>, iv?: BinaryLike | null);
10
- update(data: BinaryLike, inputEncoding?: CipherEncoding, outputEncoding?: CipherEncoding): ArrayBuffer | string;
11
- final(): ArrayBuffer;
12
- final(outputEncoding: BufferEncoding | 'buffer'): string;
13
- _transform(chunk: BinaryLike, encoding: Encoding, callback: () => void): void;
14
- _flush(callback: () => void): void;
15
- setAutoPadding(autoPadding?: boolean): this;
16
- setAAD(buffer: Buffer, options?: {
17
- plaintextLength: number;
18
- }): this;
19
- setAuthTag(tag: Buffer): this;
20
- }
21
- declare class Cipher extends CipherCommon {
22
- constructor(cipherType: string, cipherKey: BinaryLike, options?: Record<string, any>, iv?: BinaryLike | null);
23
- }
24
- declare class Decipher extends CipherCommon {
25
- constructor(cipherType: string, cipherKey: BinaryLike, options?: Record<string, any>, iv?: BinaryLike | null);
26
- }
27
- export declare function createDecipher(algorithm: string, password: BinaryLike, options?: Stream.TransformOptions): Decipher;
28
- export declare function createDecipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike | null, options?: Stream.TransformOptions): Decipher;
29
- export declare function createCipher(algorithm: string, password: BinaryLike, options?: Stream.TransformOptions): Cipher;
30
- export declare function createCipheriv(algorithm: string, key: BinaryLike, iv: BinaryLike | null, options?: Stream.TransformOptions): Cipher;
31
- export declare const publicEncrypt: (options: {
32
- key: any;
33
- encoding?: string;
34
- format?: any;
35
- padding?: any;
36
- oaepHash?: any;
37
- oaepLabel?: any;
38
- passphrase?: string;
39
- }, buffer: BinaryLike) => Buffer;
40
- export declare const publicDecrypt: (options: {
41
- key: any;
42
- encoding?: string;
43
- format?: any;
44
- padding?: any;
45
- oaepHash?: any;
46
- oaepLabel?: any;
47
- passphrase?: string;
48
- }, buffer: BinaryLike) => Buffer;
49
- export declare const privateDecrypt: (options: {
50
- key: any;
51
- encoding?: string;
52
- format?: any;
53
- padding?: any;
54
- oaepHash?: any;
55
- oaepLabel?: any;
56
- passphrase?: string;
57
- }, buffer: BinaryLike) => Buffer;
58
- declare type GenerateKeyPairOptions = {
59
- modulusLength: number;
60
- publicExponent?: number;
61
- hashAlgorithm?: string;
62
- mgf1HashAlgorithm?: string;
63
- saltLength?: number;
64
- divisorLength?: number;
65
- namedCurve?: string;
66
- prime?: Buffer;
67
- primeLength?: number;
68
- generator?: number;
69
- groupName?: string;
70
- publicKeyEncoding?: any;
71
- privateKeyEncoding?: any;
72
- paramEncoding?: string;
73
- hash?: any;
74
- mgf1Hash?: any;
75
- };
76
- declare type GenerateKeyPairCallback = (error: unknown | null, publicKey?: Buffer, privateKey?: Buffer) => void;
77
- export declare function generateKeyPair(type: string, callback: GenerateKeyPairCallback): void;
78
- export declare function generateKeyPair(type: string, options: GenerateKeyPairOptions, callback: GenerateKeyPairCallback): void;
79
- export declare function generateKeyPairSync(type: string): {
80
- publicKey: any;
81
- privateKey: any;
82
- };
83
- export declare function generateKeyPairSync(type: string, options: GenerateKeyPairOptions): {
84
- publicKey: any;
85
- privateKey: any;
86
- };
87
- export {};
@@ -1,32 +0,0 @@
1
- import type { BinaryLike } from '../Utils';
2
- import type { Buffer } from '@craftzdog/react-native-buffer';
3
- export declare enum RSAKeyVariant {
4
- kKeyVariantRSA_SSA_PKCS1_v1_5 = 0,
5
- kKeyVariantRSA_PSS = 1,
6
- kKeyVariantRSA_OAEP = 2
7
- }
8
- export declare type InternalCipher = {
9
- update: (data: BinaryLike | ArrayBufferView) => ArrayBuffer;
10
- final: () => ArrayBuffer;
11
- copy: () => void;
12
- setAAD: (args: {
13
- data: BinaryLike;
14
- plaintextLength?: number;
15
- }) => InternalCipher;
16
- setAutoPadding: (autoPad: boolean) => boolean;
17
- setAuthTag: (tag: ArrayBuffer) => boolean;
18
- };
19
- export declare type CreateCipherMethod = (params: {
20
- cipher_type: string;
21
- cipher_key: ArrayBuffer;
22
- auth_tag_len: number;
23
- }) => InternalCipher;
24
- export declare type CreateDecipherMethod = (params: {
25
- cipher_type: string;
26
- cipher_key: ArrayBuffer;
27
- auth_tag_len: number;
28
- }) => InternalCipher;
29
- export declare type PublicEncryptMethod = (data: ArrayBuffer, format: number, type: any, passphrase: any, buffer: ArrayBuffer, padding: number, oaepHash: any, oaepLabel: any) => Buffer;
30
- export declare type PrivateDecryptMethod = (data: ArrayBuffer, format: number, type: any, passphrase: any, buffer: ArrayBuffer, padding: number, oaepHash: any, oaepLabel: any) => Buffer;
31
- export declare type GenerateKeyPairMethod = (keyVariant: RSAKeyVariant, modulusLength: number, publicExponent: number, ...rest: any[]) => Promise<[error: unknown, publicBuffer: any, privateBuffer: any]>;
32
- export declare type GenerateKeyPairSyncMethod = (keyVariant: RSAKeyVariant, modulusLength: number, publicExponent: number, ...rest: any[]) => [error: unknown, publicBuffer: any, privateBuffer: any];
@@ -1,6 +0,0 @@
1
- export declare type InternalHash = {
2
- update: (data: ArrayBuffer) => InternalHash;
3
- digest: () => ArrayBuffer;
4
- copy: (len?: number) => InternalHash;
5
- };
6
- export declare type CreateHashMethod = (algorithm: string, outputLength?: number) => InternalHash;
@@ -1,5 +0,0 @@
1
- export declare type InternalHmac = {
2
- update: (data: ArrayBuffer) => InternalHmac;
3
- digest: () => ArrayBuffer;
4
- };
5
- export declare type CreateHmacMethod = (algorithm: string, key?: ArrayBuffer) => InternalHmac;
@@ -1,23 +0,0 @@
1
- import { Buffer } from '@craftzdog/react-native-buffer';
2
- export declare type BinaryLike = string | ArrayBuffer | Buffer;
3
- export declare type BinaryToTextEncoding = 'base64' | 'base64url' | 'hex' | 'binary';
4
- export declare type CharacterEncoding = 'utf8' | 'utf-8' | 'utf16le' | 'latin1';
5
- export declare type LegacyCharacterEncoding = 'ascii' | 'binary' | 'ucs2' | 'ucs-2';
6
- export declare type Encoding = BinaryToTextEncoding | CharacterEncoding | LegacyCharacterEncoding;
7
- export declare type CipherEncoding = Encoding | 'buffer';
8
- export declare function setDefaultEncoding(encoding: CipherEncoding): void;
9
- export declare function getDefaultEncoding(): CipherEncoding;
10
- export declare const kEmptyObject: any;
11
- export declare function toArrayBuffer(buf: Buffer): ArrayBuffer;
12
- export declare function binaryLikeToArrayBuffer(input: BinaryLike, encoding?: string): ArrayBuffer;
13
- export declare function ab2str(buf: ArrayBuffer, encoding?: string): string;
14
- export declare function validateString(str: any, name?: string): str is string;
15
- export declare function validateFunction(f: any): f is Function;
16
- export declare function isStringOrBuffer(val: any): val is string | ArrayBuffer;
17
- export declare function validateObject<T>(value: any, name: string, options?: {
18
- allowArray: boolean;
19
- allowFunction: boolean;
20
- nullable: boolean;
21
- } | null): value is T;
22
- export declare function validateInt32(value: any, name: string, min?: number, max?: number): void;
23
- export declare function validateUint32(value: number, name: string, positive?: boolean): void;
@@ -1,60 +0,0 @@
1
- import { BinaryLike } from './Utils';
2
- declare enum KFormatType {
3
- kKeyFormatDER = 0,
4
- kKeyFormatPEM = 1,
5
- kKeyFormatJWK = 2
6
- }
7
- declare enum KeyEncoding {
8
- kKeyEncodingPKCS1 = 0,
9
- kKeyEncodingPKCS8 = 1,
10
- kKeyEncodingSPKI = 2,
11
- kKeyEncodingSEC1 = 3
12
- }
13
- export declare function preparePrivateKey(key: BinaryLike | {
14
- key: any;
15
- encoding?: string;
16
- format?: any;
17
- padding?: number;
18
- passphrase?: string;
19
- }): {
20
- format: KFormatType;
21
- data: ArrayBuffer;
22
- type?: any;
23
- passphrase?: any;
24
- };
25
- export declare function preparePublicOrPrivateKey(key: BinaryLike | {
26
- key: any;
27
- encoding?: string;
28
- format?: any;
29
- padding?: number;
30
- }): {
31
- format: KFormatType;
32
- data: ArrayBuffer;
33
- type?: any;
34
- passphrase?: any;
35
- };
36
- export declare function parsePublicKeyEncoding(enc: {
37
- key: any;
38
- encoding?: string;
39
- format?: string;
40
- cipher?: string;
41
- passphrase?: string;
42
- }, keyType: string | undefined, objName?: string): {
43
- format: KFormatType;
44
- type: KeyEncoding | undefined;
45
- cipher: string | undefined;
46
- passphrase: ArrayBuffer | undefined;
47
- };
48
- export declare function parsePrivateKeyEncoding(enc: {
49
- key: any;
50
- encoding?: string;
51
- format?: string;
52
- cipher?: string;
53
- passphrase?: string;
54
- }, keyType: string | undefined, objName?: string): {
55
- format: KFormatType;
56
- type: KeyEncoding | undefined;
57
- cipher: string | undefined;
58
- passphrase: ArrayBuffer | undefined;
59
- };
60
- export {};
@@ -1,9 +0,0 @@
1
- import { Buffer } from '@craftzdog/react-native-buffer';
2
- import { BinaryLike } from './Utils';
3
- declare type Password = BinaryLike;
4
- declare type Salt = BinaryLike;
5
- declare type Pbkdf2Callback = (err: Error | null, derivedKey?: Buffer) => void;
6
- export declare function pbkdf2(password: Password, salt: Salt, iterations: number, keylen: number, digest: string, callback: Pbkdf2Callback): void;
7
- export declare function pbkdf2(password: Password, salt: Salt, iterations: number, keylen: number, callback: Pbkdf2Callback): void;
8
- export declare function pbkdf2Sync(password: Password, salt: Salt, iterations: number, keylen: number, digest?: string): Buffer;
9
- export {};
@@ -1,4 +0,0 @@
1
- declare module 'stream-browserify' {
2
- import Stream from 'stream';
3
- export = Stream;
4
- }