react-native-quick-crypto 0.7.0 → 0.7.2

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 (133) hide show
  1. package/README.md +11 -63
  2. package/android/CMakeLists.txt +2 -0
  3. package/cpp/Cipher/MGLRsa.cpp +179 -3
  4. package/cpp/Cipher/MGLRsa.h +40 -0
  5. package/cpp/JSIUtils/MGLJSIUtils.h +8 -0
  6. package/cpp/MGLKeys.cpp +41 -43
  7. package/cpp/MGLKeys.h +9 -2
  8. package/cpp/MGLQuickCryptoHostObject.cpp +6 -6
  9. package/cpp/Sig/MGLSignHostObjects.cpp +22 -15
  10. package/cpp/Utils/MGLUtils.cpp +71 -1
  11. package/cpp/Utils/MGLUtils.h +55 -1
  12. package/cpp/webcrypto/MGLWebCrypto.cpp +89 -37
  13. package/cpp/webcrypto/MGLWebCrypto.h +5 -7
  14. package/cpp/webcrypto/crypto_aes.cpp +516 -0
  15. package/cpp/webcrypto/crypto_aes.h +79 -0
  16. package/cpp/webcrypto/crypto_ec.cpp +4 -20
  17. package/cpp/webcrypto/crypto_ec.h +0 -5
  18. package/cpp/webcrypto/crypto_keygen.cpp +86 -0
  19. package/cpp/webcrypto/crypto_keygen.h +38 -0
  20. package/lib/commonjs/Cipher.js +3 -1
  21. package/lib/commonjs/Cipher.js.map +1 -1
  22. package/lib/commonjs/Hashnames.js +20 -8
  23. package/lib/commonjs/Hashnames.js.map +1 -1
  24. package/lib/commonjs/NativeQuickCrypto/Cipher.js +13 -1
  25. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  26. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  27. package/lib/commonjs/NativeQuickCrypto/aes.js +6 -0
  28. package/lib/commonjs/NativeQuickCrypto/aes.js.map +1 -0
  29. package/lib/commonjs/NativeQuickCrypto/keygen.js +6 -0
  30. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +1 -0
  31. package/lib/commonjs/NativeQuickCrypto/rsa.js +6 -0
  32. package/lib/commonjs/NativeQuickCrypto/rsa.js.map +1 -0
  33. package/lib/commonjs/Utils.js +30 -6
  34. package/lib/commonjs/Utils.js.map +1 -1
  35. package/lib/commonjs/aes.js +184 -227
  36. package/lib/commonjs/aes.js.map +1 -1
  37. package/lib/commonjs/index.js +12 -2
  38. package/lib/commonjs/index.js.map +1 -1
  39. package/lib/commonjs/keygen.js +56 -0
  40. package/lib/commonjs/keygen.js.map +1 -0
  41. package/lib/commonjs/keys.js +74 -5
  42. package/lib/commonjs/keys.js.map +1 -1
  43. package/lib/commonjs/rsa.js +115 -196
  44. package/lib/commonjs/rsa.js.map +1 -1
  45. package/lib/commonjs/sig.js.map +1 -1
  46. package/lib/commonjs/subtle.js +140 -78
  47. package/lib/commonjs/subtle.js.map +1 -1
  48. package/lib/commonjs/webcrypto.js +14 -0
  49. package/lib/commonjs/webcrypto.js.map +1 -0
  50. package/lib/module/Cipher.js +3 -1
  51. package/lib/module/Cipher.js.map +1 -1
  52. package/lib/module/Hashnames.js +20 -8
  53. package/lib/module/Hashnames.js.map +1 -1
  54. package/lib/module/NativeQuickCrypto/Cipher.js +12 -0
  55. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  56. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  57. package/lib/module/NativeQuickCrypto/aes.js +2 -0
  58. package/lib/module/NativeQuickCrypto/aes.js.map +1 -0
  59. package/lib/module/NativeQuickCrypto/keygen.js +2 -0
  60. package/lib/module/NativeQuickCrypto/keygen.js.map +1 -0
  61. package/lib/module/NativeQuickCrypto/rsa.js +2 -0
  62. package/lib/module/NativeQuickCrypto/rsa.js.map +1 -0
  63. package/lib/module/Utils.js +26 -5
  64. package/lib/module/Utils.js.map +1 -1
  65. package/lib/module/aes.js +183 -228
  66. package/lib/module/aes.js.map +1 -1
  67. package/lib/module/index.js +11 -2
  68. package/lib/module/index.js.map +1 -1
  69. package/lib/module/keygen.js +47 -0
  70. package/lib/module/keygen.js.map +1 -0
  71. package/lib/module/keys.js +68 -4
  72. package/lib/module/keys.js.map +1 -1
  73. package/lib/module/rsa.js +115 -198
  74. package/lib/module/rsa.js.map +1 -1
  75. package/lib/module/sig.js.map +1 -1
  76. package/lib/module/subtle.js +143 -82
  77. package/lib/module/subtle.js.map +1 -1
  78. package/lib/module/webcrypto.js +8 -0
  79. package/lib/module/webcrypto.js.map +1 -0
  80. package/lib/typescript/Cipher.d.ts +0 -1
  81. package/lib/typescript/Cipher.d.ts.map +1 -1
  82. package/lib/typescript/Hash.d.ts.map +1 -1
  83. package/lib/typescript/Hashnames.d.ts +2 -2
  84. package/lib/typescript/Hashnames.d.ts.map +1 -1
  85. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +5 -0
  86. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
  87. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +4 -1
  88. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -1
  89. package/lib/typescript/NativeQuickCrypto/aes.d.ts +5 -0
  90. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +1 -0
  91. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +4 -0
  92. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +1 -0
  93. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +5 -0
  94. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +1 -0
  95. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +12 -2
  96. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
  97. package/lib/typescript/Utils.d.ts +4 -4
  98. package/lib/typescript/Utils.d.ts.map +1 -1
  99. package/lib/typescript/aes.d.ts +18 -1
  100. package/lib/typescript/aes.d.ts.map +1 -1
  101. package/lib/typescript/ec.d.ts.map +1 -1
  102. package/lib/typescript/index.d.ts +27 -24
  103. package/lib/typescript/index.d.ts.map +1 -1
  104. package/lib/typescript/keygen.d.ts +6 -0
  105. package/lib/typescript/keygen.d.ts.map +1 -0
  106. package/lib/typescript/keys.d.ts +58 -17
  107. package/lib/typescript/keys.d.ts.map +1 -1
  108. package/lib/typescript/rsa.d.ts +9 -1
  109. package/lib/typescript/rsa.d.ts.map +1 -1
  110. package/lib/typescript/sig.d.ts +3 -17
  111. package/lib/typescript/sig.d.ts.map +1 -1
  112. package/lib/typescript/subtle.d.ts +6 -5
  113. package/lib/typescript/subtle.d.ts.map +1 -1
  114. package/lib/typescript/webcrypto.d.ts +9 -0
  115. package/lib/typescript/webcrypto.d.ts.map +1 -0
  116. package/package.json +12 -12
  117. package/src/Cipher.ts +1 -1
  118. package/src/Hashnames.ts +23 -21
  119. package/src/NativeQuickCrypto/Cipher.ts +32 -0
  120. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +6 -0
  121. package/src/NativeQuickCrypto/aes.ts +14 -0
  122. package/src/NativeQuickCrypto/keygen.ts +7 -0
  123. package/src/NativeQuickCrypto/rsa.ts +12 -0
  124. package/src/NativeQuickCrypto/webcrypto.ts +26 -2
  125. package/src/Utils.ts +37 -8
  126. package/src/aes.ts +259 -222
  127. package/src/index.ts +10 -1
  128. package/src/keygen.ts +80 -0
  129. package/src/keys.ts +143 -30
  130. package/src/rsa.ts +161 -187
  131. package/src/sig.ts +7 -23
  132. package/src/subtle.ts +211 -93
  133. package/src/webcrypto.ts +8 -0
package/src/subtle.ts CHANGED
@@ -8,6 +8,9 @@ import {
8
8
  type AnyAlgorithm,
9
9
  type JWK,
10
10
  type CryptoKeyPair,
11
+ CipherOrWrapMode,
12
+ type EncryptDecryptParams,
13
+ type AesKeyGenParams,
11
14
  } from './keys';
12
15
  import {
13
16
  hasAnyNotIn,
@@ -18,32 +21,72 @@ import {
18
21
  normalizeHashName,
19
22
  HashContext,
20
23
  type Operation,
24
+ validateMaxBufferLength,
25
+ bufferLikeToArrayBuffer,
21
26
  } from './Utils';
22
27
  import { ecImportKey, ecExportKey, ecGenerateKey, ecdsaSignVerify } from './ec';
23
28
  import { pbkdf2DeriveBits } from './pbkdf2';
24
29
  import { asyncDigest } from './Hash';
25
- import { aesImportKey, getAlgorithmName } from './aes';
26
- import { rsaImportKey } from './rsa';
30
+ import {
31
+ aesCipher,
32
+ aesGenerateKey,
33
+ aesImportKey,
34
+ getAlgorithmName,
35
+ } from './aes';
36
+ import { rsaCipher, rsaExportKey, rsaImportKey, rsaKeyGenerate } from './rsa';
27
37
 
28
38
  const exportKeySpki = async (key: CryptoKey): Promise<ArrayBuffer | any> => {
29
39
  switch (key.algorithm.name) {
30
- // case 'RSASSA-PKCS1-v1_5':
40
+ case 'RSASSA-PKCS1-v1_5':
41
+ // Fall through
42
+ case 'RSA-PSS':
43
+ // Fall through
44
+ case 'RSA-OAEP':
45
+ if (key.type === 'public') {
46
+ return rsaExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatSPKI);
47
+ }
48
+ break;
49
+ case 'ECDSA':
50
+ // Fall through
51
+ case 'ECDH':
52
+ if (key.type === 'public') {
53
+ return ecExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatSPKI);
54
+ }
55
+ break;
56
+ // case 'Ed25519':
31
57
  // // Fall through
32
- // case 'RSA-PSS':
58
+ // case 'Ed448':
33
59
  // // Fall through
34
- // case 'RSA-OAEP':
60
+ // case 'X25519':
61
+ // // Fall through
62
+ // case 'X448':
35
63
  // if (key.type === 'public') {
36
- // return require('internal/crypto/rsa').rsaExportKey(
37
- // key,
38
- // kWebCryptoKeyFormatSPKI
39
- // );
64
+ // return cfrgExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatSPKI);
40
65
  // }
41
66
  // break;
67
+ }
68
+
69
+ throw new Error(
70
+ `Unable to export a spki ${key.algorithm.name} ${key.type} key`
71
+ );
72
+ };
73
+
74
+ const exportKeyPkcs8 = async (key: CryptoKey): Promise<ArrayBuffer | any> => {
75
+ switch (key.algorithm.name) {
76
+ case 'RSASSA-PKCS1-v1_5':
77
+ // Fall through
78
+ case 'RSA-PSS':
79
+ // Fall through
80
+ case 'RSA-OAEP':
81
+ if (key.type === 'private') {
82
+ return rsaExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatPKCS8);
83
+ }
84
+ break;
42
85
  case 'ECDSA':
43
86
  // Fall through
44
87
  case 'ECDH':
45
- if (key.type === 'public') {
46
- return ecExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatSPKI);
88
+ if (key.type === 'private') {
89
+ return ecExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatPKCS8);
47
90
  }
48
91
  break;
49
92
  // case 'Ed25519':
@@ -53,17 +96,14 @@ const exportKeySpki = async (key: CryptoKey): Promise<ArrayBuffer | any> => {
53
96
  // case 'X25519':
54
97
  // // Fall through
55
98
  // case 'X448':
56
- // if (key.type === 'public') {
57
- // return require('internal/crypto/cfrg').cfrgExportKey(
58
- // key,
59
- // kWebCryptoKeyFormatSPKI
60
- // );
99
+ // if (key.type === 'private') {
100
+ // return cfrgExportKey(key, KWebCryptoKeyFormat.kWebCryptoKeyFormatPKCS8);
61
101
  // }
62
102
  // break;
63
103
  }
64
104
 
65
105
  throw new Error(
66
- `Unable to export a raw ${key.algorithm.name} ${key.type} key`
106
+ `Unable to export a pkcs8 ${key.algorithm.name} ${key.type} key`
67
107
  );
68
108
  };
69
109
 
@@ -285,7 +325,70 @@ const signVerify = (
285
325
  );
286
326
  };
287
327
 
288
- class Subtle {
328
+ const cipherOrWrap = async (
329
+ mode: CipherOrWrapMode,
330
+ algorithm: EncryptDecryptParams, // | WrapUnwrapParams,
331
+ key: CryptoKey,
332
+ data: ArrayBuffer,
333
+ op: Operation
334
+ ): Promise<ArrayBuffer> => {
335
+ // We use a Node.js style error here instead of a DOMException because
336
+ // the WebCrypto spec is not specific what kind of error is to be thrown
337
+ // in this case. Both Firefox and Chrome throw simple TypeErrors here.
338
+ // The key algorithm and cipher algorithm must match, and the
339
+ // key must have the proper usage.
340
+ if (
341
+ key.algorithm.name !== algorithm.name ||
342
+ !key.usages.includes(op as KeyUsage)
343
+ ) {
344
+ throw lazyDOMException(
345
+ 'The requested operation is not valid for the provided key',
346
+ 'InvalidAccessError'
347
+ );
348
+ }
349
+
350
+ // While WebCrypto allows for larger input buffer sizes, we limit
351
+ // those to sizes that can fit within uint32_t because of limitations
352
+ // in the OpenSSL API.
353
+ validateMaxBufferLength(data, 'data');
354
+
355
+ switch (algorithm.name) {
356
+ case 'RSA-OAEP':
357
+ return rsaCipher(mode, key, data, algorithm);
358
+ case 'AES-CTR':
359
+ // Fall through
360
+ case 'AES-CBC':
361
+ // Fall through
362
+ case 'AES-GCM':
363
+ return aesCipher(mode, key, data, algorithm);
364
+ // case 'AES-KW':
365
+ // if (op === 'wrapKey' || op === 'unwrapKey') {
366
+ // return aesCipher(mode, key, data, algorithm);
367
+ // }
368
+ }
369
+ // @ts-ignore
370
+ throw lazyDOMException(
371
+ `Unrecognized algorithm name '${algorithm}' for '${op}'`,
372
+ 'NotSupportedError'
373
+ );
374
+ };
375
+
376
+ export class Subtle {
377
+ async decrypt(
378
+ algorithm: EncryptDecryptParams,
379
+ key: CryptoKey,
380
+ data: BufferLike
381
+ ): Promise<ArrayBuffer> {
382
+ const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'decrypt');
383
+ return cipherOrWrap(
384
+ CipherOrWrapMode.kWebCryptoCipherDecrypt,
385
+ normalizedAlgorithm as EncryptDecryptParams,
386
+ key,
387
+ bufferLikeToArrayBuffer(data),
388
+ 'decrypt'
389
+ );
390
+ }
391
+
289
392
  async digest(
290
393
  algorithm: SubtleAlgorithm | AnyAlgorithm,
291
394
  data: BufferLike
@@ -323,6 +426,96 @@ class Subtle {
323
426
  );
324
427
  }
325
428
 
429
+ async encrypt(
430
+ algorithm: EncryptDecryptParams,
431
+ key: CryptoKey,
432
+ data: BufferLike
433
+ ): Promise<ArrayBuffer> {
434
+ const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'encrypt');
435
+ return cipherOrWrap(
436
+ CipherOrWrapMode.kWebCryptoCipherEncrypt,
437
+ normalizedAlgorithm as EncryptDecryptParams,
438
+ key,
439
+ bufferLikeToArrayBuffer(data),
440
+ 'encrypt'
441
+ );
442
+ }
443
+
444
+ async exportKey(
445
+ format: ImportFormat,
446
+ key: CryptoKey
447
+ ): Promise<ArrayBuffer | JWK> {
448
+ if (!key.extractable) throw new Error('key is not extractable');
449
+
450
+ switch (format) {
451
+ case 'spki':
452
+ return await exportKeySpki(key);
453
+ case 'pkcs8':
454
+ return await exportKeyPkcs8(key);
455
+ case 'jwk':
456
+ return exportKeyJWK(key);
457
+ case 'raw':
458
+ return exportKeyRaw(key);
459
+ }
460
+ }
461
+
462
+ async generateKey(
463
+ algorithm: SubtleAlgorithm,
464
+ extractable: boolean,
465
+ keyUsages: KeyUsage[]
466
+ ): Promise<CryptoKey | CryptoKeyPair> {
467
+ algorithm = normalizeAlgorithm(algorithm, 'generateKey');
468
+ let result: CryptoKey | CryptoKeyPair;
469
+ switch (algorithm.name) {
470
+ case 'RSASSA-PKCS1-v1_5':
471
+ // Fall through
472
+ case 'RSA-PSS':
473
+ // Fall through
474
+ case 'RSA-OAEP':
475
+ result = await rsaKeyGenerate(algorithm, extractable, keyUsages);
476
+ break;
477
+ // case 'Ed25519':
478
+ // // Fall through
479
+ // case 'Ed448':
480
+ // // Fall through
481
+ // case 'X25519':
482
+ // // Fall through
483
+ // case 'X448':
484
+ // resultType = 'CryptoKeyPair';
485
+ // result = await cfrgGenerateKey(algorithm, extractable, keyUsages);
486
+ // break;
487
+ case 'ECDSA':
488
+ // Fall through
489
+ case 'ECDH':
490
+ result = await ecGenerateKey(algorithm, extractable, keyUsages);
491
+ checkCryptoKeyPairUsages(result);
492
+ break;
493
+ // case 'HMAC':
494
+ // result = await hmacGenerateKey(algorithm, extractable, keyUsages);
495
+ // break;
496
+ case 'AES-CTR':
497
+ // Fall through
498
+ case 'AES-CBC':
499
+ // Fall through
500
+ case 'AES-GCM':
501
+ // Fall through
502
+ case 'AES-KW':
503
+ result = await aesGenerateKey(
504
+ algorithm as AesKeyGenParams,
505
+ extractable,
506
+ keyUsages
507
+ );
508
+ break;
509
+ default:
510
+ throw new Error(
511
+ `'subtle.generateKey()' is not implemented for ${algorithm.name}.
512
+ Unrecognized algorithm name`
513
+ );
514
+ }
515
+
516
+ return result;
517
+ }
518
+
326
519
  async importKey(
327
520
  format: ImportFormat,
328
521
  data: BufferLike | BinaryLike | JWK,
@@ -425,81 +618,6 @@ class Subtle {
425
618
  return result;
426
619
  }
427
620
 
428
- async exportKey(
429
- format: ImportFormat,
430
- key: CryptoKey
431
- ): Promise<ArrayBuffer | any> {
432
- if (!key.extractable) throw new Error('key is not extractable');
433
-
434
- switch (format) {
435
- case 'spki':
436
- return await exportKeySpki(key);
437
- // case 'pkcs8':
438
- // return await exportKeyPkcs8(key);
439
- case 'jwk':
440
- return exportKeyJWK(key);
441
- case 'raw':
442
- return exportKeyRaw(key);
443
- }
444
- throw new Error(`'subtle.exportKey()' is not implemented for ${format}`);
445
- }
446
-
447
- async generateKey(
448
- algorithm: SubtleAlgorithm,
449
- extractable: boolean,
450
- keyUsages: KeyUsage[]
451
- ): Promise<CryptoKey | CryptoKeyPair> {
452
- algorithm = normalizeAlgorithm(algorithm, 'generateKey');
453
- let result: CryptoKey | CryptoKeyPair;
454
- switch (algorithm.name) {
455
- // case 'RSASSA-PKCS1-v1_5':
456
- // // Fall through
457
- // case 'RSA-PSS':
458
- // // Fall through
459
- // case 'RSA-OAEP':
460
- // resultType = 'CryptoKeyPair';
461
- // result = await rsaKeyGenerate(algorithm, extractable, keyUsages);
462
- // break;
463
- // case 'Ed25519':
464
- // // Fall through
465
- // case 'Ed448':
466
- // // Fall through
467
- // case 'X25519':
468
- // // Fall through
469
- // case 'X448':
470
- // resultType = 'CryptoKeyPair';
471
- // result = await cfrgGenerateKey(algorithm, extractable, keyUsages);
472
- // break;
473
- case 'ECDSA':
474
- // Fall through
475
- case 'ECDH':
476
- result = await ecGenerateKey(algorithm, extractable, keyUsages);
477
- checkCryptoKeyPairUsages(result);
478
- break;
479
- // case 'HMAC':
480
- // resultType = 'CryptoKey';
481
- // result = await hmacGenerateKey(algorithm, extractable, keyUsages);
482
- // break;
483
- // case 'AES-CTR':
484
- // // Fall through
485
- // case 'AES-CBC':
486
- // // Fall through
487
- // case 'AES-GCM':
488
- // // Fall through
489
- // case 'AES-KW':
490
- // resultType = 'CryptoKey';
491
- // result = await aesGenerateKey(algorithm, extractable, keyUsages);
492
- // break;
493
- default:
494
- throw new Error(
495
- `'subtle.generateKey()' is not implemented for ${algorithm.name}.
496
- Unrecognized algorithm name`
497
- );
498
- }
499
-
500
- return result;
501
- }
502
-
503
621
  async sign(
504
622
  algorithm: SubtleAlgorithm,
505
623
  key: CryptoKey,
@@ -0,0 +1,8 @@
1
+ import { subtle, Subtle } from './subtle';
2
+ import { CryptoKey } from './keys';
3
+
4
+ export default {
5
+ subtle,
6
+ SubtleCrypto: Subtle,
7
+ CryptoKey,
8
+ };