react-native-quick-crypto 0.7.10 → 0.7.12

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 (62) hide show
  1. package/android/CMakeLists.txt +59 -62
  2. package/android/build.gradle +9 -7
  3. package/lib/commonjs/Algorithms.js +224 -0
  4. package/lib/commonjs/Algorithms.js.map +1 -0
  5. package/lib/commonjs/Cipher.js +36 -8
  6. package/lib/commonjs/Cipher.js.map +1 -1
  7. package/lib/commonjs/Hash.js +1 -1
  8. package/lib/commonjs/Hash.js.map +1 -1
  9. package/lib/commonjs/Hashnames.js +1 -1
  10. package/lib/commonjs/Hashnames.js.map +1 -1
  11. package/lib/commonjs/Utils.js +27 -235
  12. package/lib/commonjs/Utils.js.map +1 -1
  13. package/lib/commonjs/keys.js.map +1 -1
  14. package/lib/commonjs/pbkdf2.js +1 -1
  15. package/lib/commonjs/pbkdf2.js.map +1 -1
  16. package/lib/commonjs/random.js.map +1 -1
  17. package/lib/commonjs/subtle.js +7 -6
  18. package/lib/commonjs/subtle.js.map +1 -1
  19. package/lib/module/Algorithms.js +219 -0
  20. package/lib/module/Algorithms.js.map +1 -0
  21. package/lib/module/Cipher.js +36 -8
  22. package/lib/module/Cipher.js.map +1 -1
  23. package/lib/module/Hash.js +1 -1
  24. package/lib/module/Hash.js.map +1 -1
  25. package/lib/module/Hashnames.js +1 -1
  26. package/lib/module/Hashnames.js.map +1 -1
  27. package/lib/module/Utils.js +25 -230
  28. package/lib/module/Utils.js.map +1 -1
  29. package/lib/module/keys.js.map +1 -1
  30. package/lib/module/pbkdf2.js +1 -1
  31. package/lib/module/pbkdf2.js.map +1 -1
  32. package/lib/module/random.js.map +1 -1
  33. package/lib/module/subtle.js +2 -1
  34. package/lib/module/subtle.js.map +1 -1
  35. package/lib/typescript/src/Algorithms.d.ts +4 -0
  36. package/lib/typescript/src/Algorithms.d.ts.map +1 -0
  37. package/lib/typescript/src/Cipher.d.ts +21 -3
  38. package/lib/typescript/src/Cipher.d.ts.map +1 -1
  39. package/lib/typescript/src/Hashnames.d.ts +2 -2
  40. package/lib/typescript/src/Hashnames.d.ts.map +1 -1
  41. package/lib/typescript/src/Utils.d.ts +20 -8
  42. package/lib/typescript/src/Utils.d.ts.map +1 -1
  43. package/lib/typescript/src/index.d.ts +7 -7
  44. package/lib/typescript/src/index.d.ts.map +1 -1
  45. package/lib/typescript/src/keys.d.ts +5 -2
  46. package/lib/typescript/src/keys.d.ts.map +1 -1
  47. package/lib/typescript/src/random.d.ts +5 -6
  48. package/lib/typescript/src/random.d.ts.map +1 -1
  49. package/lib/typescript/src/sig.d.ts +1 -1
  50. package/lib/typescript/src/subtle.d.ts.map +1 -1
  51. package/package.json +40 -52
  52. package/react-native-quick-crypto.podspec +1 -1
  53. package/src/Algorithms.ts +247 -0
  54. package/src/Cipher.ts +49 -4
  55. package/src/Hash.ts +1 -1
  56. package/src/Hashnames.ts +4 -4
  57. package/src/Utils.ts +34 -279
  58. package/src/keys.ts +5 -1
  59. package/src/pbkdf2.ts +1 -1
  60. package/src/random.ts +16 -24
  61. package/src/subtle.ts +1 -2
  62. package/LICENSE +0 -27
@@ -0,0 +1,247 @@
1
+ import type {
2
+ AnyAlgorithm,
3
+ DeriveBitsAlgorithm,
4
+ DigestAlgorithm,
5
+ EncryptDecryptAlgorithm,
6
+ EncryptDecryptParams,
7
+ KeyPairAlgorithm,
8
+ SecretKeyAlgorithm,
9
+ SignVerifyAlgorithm,
10
+ SubtleAlgorithm,
11
+ } from './keys';
12
+
13
+ type SupportedAlgorithm<Type extends string> = {
14
+ [key in Type]: string | null;
15
+ };
16
+
17
+ type SupportedAlgorithms = {
18
+ digest: SupportedAlgorithm<DigestAlgorithm>;
19
+ generateKey: SupportedAlgorithm<KeyPairAlgorithm | SecretKeyAlgorithm>;
20
+ sign: SupportedAlgorithm<SignVerifyAlgorithm>;
21
+ verify: SupportedAlgorithm<SignVerifyAlgorithm>;
22
+ importKey: SupportedAlgorithm<
23
+ KeyPairAlgorithm | 'PBKDF2' | SecretKeyAlgorithm | 'HKDF'
24
+ >;
25
+ deriveBits: SupportedAlgorithm<DeriveBitsAlgorithm>;
26
+ encrypt: SupportedAlgorithm<EncryptDecryptAlgorithm>;
27
+ decrypt: SupportedAlgorithm<EncryptDecryptAlgorithm>;
28
+ 'get key length': SupportedAlgorithm<SecretKeyAlgorithm | 'PBKDF2' | 'HKDF'>;
29
+ wrapKey: SupportedAlgorithm<'AES-KW'>;
30
+ unwrapKey: SupportedAlgorithm<'AES-KW'>;
31
+ };
32
+
33
+ export type Operation =
34
+ | 'digest'
35
+ | 'generateKey'
36
+ | 'sign'
37
+ | 'verify'
38
+ | 'importKey'
39
+ | 'deriveBits'
40
+ | 'encrypt'
41
+ | 'decrypt'
42
+ | 'get key length'
43
+ | 'wrapKey'
44
+ | 'unwrapKey';
45
+
46
+ const kSupportedAlgorithms: SupportedAlgorithms = {
47
+ digest: {
48
+ 'SHA-1': null,
49
+ 'SHA-256': null,
50
+ 'SHA-384': null,
51
+ 'SHA-512': null,
52
+ },
53
+ generateKey: {
54
+ 'RSASSA-PKCS1-v1_5': 'RsaHashedKeyGenParams',
55
+ 'RSA-PSS': 'RsaHashedKeyGenParams',
56
+ 'RSA-OAEP': 'RsaHashedKeyGenParams',
57
+ ECDSA: 'EcKeyGenParams',
58
+ ECDH: 'EcKeyGenParams',
59
+ 'AES-CTR': 'AesKeyGenParams',
60
+ 'AES-CBC': 'AesKeyGenParams',
61
+ 'AES-GCM': 'AesKeyGenParams',
62
+ 'AES-KW': 'AesKeyGenParams',
63
+ HMAC: 'HmacKeyGenParams',
64
+ X25519: null,
65
+ Ed25519: null,
66
+ X448: null,
67
+ Ed448: null,
68
+ },
69
+ sign: {
70
+ 'RSASSA-PKCS1-v1_5': null,
71
+ 'RSA-PSS': 'RsaPssParams',
72
+ ECDSA: 'EcdsaParams',
73
+ HMAC: null,
74
+ Ed25519: null,
75
+ Ed448: 'Ed448Params',
76
+ },
77
+ verify: {
78
+ 'RSASSA-PKCS1-v1_5': null,
79
+ 'RSA-PSS': 'RsaPssParams',
80
+ ECDSA: 'EcdsaParams',
81
+ HMAC: null,
82
+ Ed25519: null,
83
+ Ed448: 'Ed448Params',
84
+ },
85
+ importKey: {
86
+ 'RSASSA-PKCS1-v1_5': 'RsaHashedImportParams',
87
+ 'RSA-PSS': 'RsaHashedImportParams',
88
+ 'RSA-OAEP': 'RsaHashedImportParams',
89
+ ECDSA: 'EcKeyImportParams',
90
+ ECDH: 'EcKeyImportParams',
91
+ HMAC: 'HmacImportParams',
92
+ HKDF: null,
93
+ PBKDF2: null,
94
+ 'AES-CTR': null,
95
+ 'AES-CBC': null,
96
+ 'AES-GCM': null,
97
+ 'AES-KW': null,
98
+ Ed25519: null,
99
+ X25519: null,
100
+ Ed448: null,
101
+ X448: null,
102
+ },
103
+ deriveBits: {
104
+ HKDF: 'HkdfParams',
105
+ PBKDF2: 'Pbkdf2Params',
106
+ ECDH: 'EcdhKeyDeriveParams',
107
+ X25519: 'EcdhKeyDeriveParams',
108
+ X448: 'EcdhKeyDeriveParams',
109
+ },
110
+ encrypt: {
111
+ 'RSA-OAEP': 'RsaOaepParams',
112
+ 'AES-CBC': 'AesCbcParams',
113
+ 'AES-GCM': 'AesGcmParams',
114
+ 'AES-CTR': 'AesCtrParams',
115
+ },
116
+ decrypt: {
117
+ 'RSA-OAEP': 'RsaOaepParams',
118
+ 'AES-CBC': 'AesCbcParams',
119
+ 'AES-GCM': 'AesGcmParams',
120
+ 'AES-CTR': 'AesCtrParams',
121
+ },
122
+ 'get key length': {
123
+ 'AES-CBC': 'AesDerivedKeyParams',
124
+ 'AES-CTR': 'AesDerivedKeyParams',
125
+ 'AES-GCM': 'AesDerivedKeyParams',
126
+ 'AES-KW': 'AesDerivedKeyParams',
127
+ HMAC: 'HmacImportParams',
128
+ HKDF: null,
129
+ PBKDF2: null,
130
+ },
131
+ wrapKey: {
132
+ 'AES-KW': null,
133
+ },
134
+ unwrapKey: {
135
+ 'AES-KW': null,
136
+ },
137
+ };
138
+
139
+ type AlgorithmDictionaries = {
140
+ [key in string]: object;
141
+ };
142
+
143
+ const simpleAlgorithmDictionaries: AlgorithmDictionaries = {
144
+ AesGcmParams: { iv: 'BufferSource', additionalData: 'BufferSource' },
145
+ RsaHashedKeyGenParams: { hash: 'HashAlgorithmIdentifier' },
146
+ EcKeyGenParams: {},
147
+ HmacKeyGenParams: { hash: 'HashAlgorithmIdentifier' },
148
+ RsaPssParams: {},
149
+ EcdsaParams: { hash: 'HashAlgorithmIdentifier' },
150
+ HmacImportParams: { hash: 'HashAlgorithmIdentifier' },
151
+ HkdfParams: {
152
+ hash: 'HashAlgorithmIdentifier',
153
+ salt: 'BufferSource',
154
+ info: 'BufferSource',
155
+ },
156
+ Ed448Params: { context: 'BufferSource' },
157
+ Pbkdf2Params: { hash: 'HashAlgorithmIdentifier', salt: 'BufferSource' },
158
+ RsaOaepParams: { label: 'BufferSource' },
159
+ RsaHashedImportParams: { hash: 'HashAlgorithmIdentifier' },
160
+ EcKeyImportParams: {},
161
+ };
162
+
163
+ // https://w3c.github.io/webcrypto/#algorithm-normalization-normalize-an-algorithm
164
+ // adapted for Node.js from Deno's implementation
165
+ // https://github.com/denoland/deno/blob/v1.29.1/ext/crypto/00_crypto.js#L195
166
+ export const normalizeAlgorithm = (
167
+ algorithm: SubtleAlgorithm | EncryptDecryptParams | AnyAlgorithm,
168
+ op: Operation,
169
+ ): SubtleAlgorithm | EncryptDecryptParams => {
170
+ if (typeof algorithm === 'string') {
171
+ return normalizeAlgorithm({ name: algorithm }, op);
172
+ }
173
+
174
+ // 1.
175
+ const registeredAlgorithms = kSupportedAlgorithms[op];
176
+ // 2. 3.
177
+ // commented, because typescript takes care of this for us 🤞👀
178
+ // const initialAlg = webidl.converters.Algorithm(algorithm, {
179
+ // prefix: 'Failed to normalize algorithm',
180
+ // context: 'passed algorithm',
181
+ // });
182
+
183
+ // 4.
184
+ let algName = algorithm.name;
185
+ if (algName === undefined) return { name: 'unknown' };
186
+
187
+ // 5.
188
+ let desiredType: string | null | undefined;
189
+ for (const key in registeredAlgorithms) {
190
+ if (!Object.prototype.hasOwnProperty.call(registeredAlgorithms, key)) {
191
+ continue;
192
+ }
193
+ if (key.toUpperCase() === algName.toUpperCase()) {
194
+ algName = key as AnyAlgorithm;
195
+ desiredType = (
196
+ registeredAlgorithms as Record<string, typeof desiredType>
197
+ )[algName];
198
+ }
199
+ }
200
+ if (desiredType === undefined)
201
+ throw new Error(`Unrecognized algorithm name: ${algName}`);
202
+
203
+ // Fast path everything below if the registered dictionary is null
204
+ if (desiredType === null) return { name: algName };
205
+
206
+ // 6.
207
+ const normalizedAlgorithm = algorithm;
208
+ // TODO: implement this? Maybe via typescript?
209
+ // webidl.converters[desiredType](algorithm, {
210
+ // prefix: 'Failed to normalize algorithm',
211
+ // context: 'passed algorithm',
212
+ // });
213
+ // 7.
214
+ normalizedAlgorithm.name = algName;
215
+
216
+ // 9.
217
+ const dict = simpleAlgorithmDictionaries[desiredType];
218
+ // 10.
219
+ const dictKeys = dict ? Object.keys(dict) : [];
220
+ for (let i = 0; i < dictKeys.length; i++) {
221
+ const member = dictKeys[i] || '';
222
+ if (!Object.prototype.hasOwnProperty.call(dict, member)) continue;
223
+ // TODO: implement this? Maybe via typescript?
224
+ // const idlType = dict[member];
225
+ // const idlValue = normalizedAlgorithm[member];
226
+ // 3.
227
+ // if (idlType === 'BufferSource' && idlValue) {
228
+ // const isView = ArrayBufferIsView(idlValue);
229
+ // normalizedAlgorithm[member] = TypedArrayPrototypeSlice(
230
+ // new Uint8Array(
231
+ // isView ? getDataViewOrTypedArrayBuffer(idlValue) : idlValue,
232
+ // isView ? getDataViewOrTypedArrayByteOffset(idlValue) : 0,
233
+ // isView
234
+ // ? getDataViewOrTypedArrayByteLength(idlValue)
235
+ // : ArrayBufferPrototypeGetByteLength(idlValue)
236
+ // )
237
+ // );
238
+ // } else if (idlType === 'HashAlgorithmIdentifier') {
239
+ // normalizedAlgorithm[member] = normalizeAlgorithm(idlValue, 'digest');
240
+ // } else if (idlType === 'AlgorithmIdentifier') {
241
+ // // This extension point is not used by any supported algorithm (yet?)
242
+ // throw lazyDOMException('Not implemented.', 'NotSupportedError');
243
+ // }
244
+ }
245
+
246
+ return normalizedAlgorithm;
247
+ };
package/src/Cipher.ts CHANGED
@@ -261,21 +261,33 @@ class Decipher extends CipherCommon {
261
261
  }
262
262
  }
263
263
 
264
+ /**
265
+ * @deprecated Use createDecipheriv instead. This function will be removed in 1.0+
266
+ */
264
267
  export function createDecipher(
265
268
  algorithm: CipherCCMTypes,
266
269
  password: BinaryLikeNode,
267
270
  options: CipherCCMOptions,
268
271
  ): DecipherCCM;
272
+ /**
273
+ * @deprecated Use createDecipheriv instead. This function will be removed in 1.0+
274
+ */
269
275
  export function createDecipher(
270
276
  algorithm: CipherGCMTypes,
271
277
  password: BinaryLikeNode,
272
278
  options?: CipherGCMOptions,
273
279
  ): DecipherGCM;
280
+ /**
281
+ * @deprecated Use createDecipheriv instead. This function will be removed in 1.0+
282
+ */
274
283
  export function createDecipher(
275
284
  algorithm: CipherType,
276
285
  password: BinaryLikeNode,
277
286
  options?: Stream.TransformOptions,
278
287
  ): DecipherCCM | DecipherGCM | Decipher;
288
+ /**
289
+ * @deprecated Use createDecipheriv instead. This function will be removed in 1.0+
290
+ */
279
291
  export function createDecipher(
280
292
  algorithm: string,
281
293
  password: BinaryLikeNode,
@@ -331,21 +343,33 @@ export function createDecipheriv(
331
343
  );
332
344
  }
333
345
 
346
+ /**
347
+ * @deprecated Use createCipheriv instead. This function will be removed in 1.0+
348
+ */
334
349
  export function createCipher(
335
350
  algorithm: CipherCCMTypes,
336
351
  password: BinaryLikeNode,
337
352
  options: CipherCCMOptions,
338
353
  ): CipherCCM;
354
+ /**
355
+ * @deprecated Use createCipheriv instead. This function will be removed in 1.0+
356
+ */
339
357
  export function createCipher(
340
358
  algorithm: CipherGCMTypes,
341
359
  password: BinaryLikeNode,
342
360
  options?: CipherGCMOptions,
343
361
  ): CipherGCM;
362
+ /**
363
+ * @deprecated Use createCipheriv instead. This function will be removed in 1.0+
364
+ */
344
365
  export function createCipher(
345
366
  algorithm: CipherType,
346
367
  password: BinaryLikeNode,
347
368
  options?: Stream.TransformOptions,
348
369
  ): CipherCCM | CipherGCM | Cipher;
370
+ /**
371
+ * @deprecated Use createCipheriv instead. This function will be removed in 1.0+
372
+ */
349
373
  export function createCipher(
350
374
  algorithm: string,
351
375
  password: BinaryLikeNode,
@@ -417,14 +441,35 @@ function rsaFunctionFor(
417
441
  defaultPadding: number,
418
442
  keyType: 'public' | 'private',
419
443
  ) {
420
- return (options: EncodingOptions, buffer: BinaryLike) => {
444
+ return (options: EncodingOptions | BinaryLike, buffer: BinaryLike) => {
421
445
  const { format, type, data, passphrase } =
422
446
  keyType === 'private'
423
447
  ? preparePrivateKey(options)
424
448
  : preparePublicOrPrivateKey(options);
425
- const padding = options.padding || defaultPadding;
426
- const { oaepHash, encoding } = options;
427
- let { oaepLabel } = options;
449
+
450
+ const padding =
451
+ options &&
452
+ typeof options === 'object' &&
453
+ 'padding' in options &&
454
+ typeof options.padding === 'number'
455
+ ? options.padding
456
+ : defaultPadding;
457
+
458
+ const oaepHash =
459
+ options && typeof options === 'object' && 'oaepHash' in options
460
+ ? options.oaepHash
461
+ : undefined;
462
+
463
+ const encoding =
464
+ options && typeof options === 'object' && 'encoding' in options
465
+ ? options.encoding
466
+ : undefined;
467
+
468
+ let oaepLabel =
469
+ options && typeof options === 'object' && 'oaepLabel' in options
470
+ ? options.oaepLabel
471
+ : undefined;
472
+
428
473
  if (oaepHash !== undefined) validateString(oaepHash, 'key.oaepHash');
429
474
  if (oaepLabel !== undefined)
430
475
  oaepLabel = binaryLikeToArrayBuffer(oaepLabel, encoding);
package/src/Hash.ts CHANGED
@@ -135,5 +135,5 @@ const internalDigest = (
135
135
  const normalizedHashName = normalizeHashName(algorithm.name);
136
136
  const hash = new Hash(normalizedHashName);
137
137
  hash.update(bufferLikeToArrayBuffer(data));
138
- return hash.digest();
138
+ return toArrayBuffer(hash.digest());
139
139
  };
package/src/Hashnames.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { HashAlgorithm, SubtleAlgorithm } from './keys';
1
+ import type { HashAlgorithmIdentifier, HashAlgorithm } from './keys';
2
2
 
3
3
  export enum HashContext {
4
4
  Node,
@@ -79,7 +79,7 @@ const kHashNames: HashNames = {
79
79
  }
80
80
 
81
81
  export function normalizeHashName(
82
- algo: string | HashAlgorithm | SubtleAlgorithm | undefined,
82
+ algo: string | HashAlgorithm | HashAlgorithmIdentifier | undefined,
83
83
  context: HashContext = HashContext.Node,
84
84
  ): HashAlgorithm {
85
85
  if (typeof algo !== 'undefined') {
@@ -91,8 +91,8 @@ export function normalizeHashName(
91
91
  const alias = kHashNames[normAlgo]![context] as HashAlgorithm;
92
92
  if (alias) return alias;
93
93
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
94
- } catch (_e) {
95
- // ignore
94
+ } catch (_e: unknown) {
95
+ /* empty */
96
96
  }
97
97
  }
98
98
  throw new Error(`Invalid Hash Algorithm: ${algo}`);