react-native-quick-crypto 0.7.0-rc.9 → 0.7.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.
Files changed (155) hide show
  1. package/android/CMakeLists.txt +2 -0
  2. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +51 -14
  3. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +25 -9
  4. package/cpp/Cipher/MGLRsa.cpp +192 -15
  5. package/cpp/Cipher/MGLRsa.h +36 -2
  6. package/cpp/JSIUtils/MGLJSIUtils.h +17 -0
  7. package/cpp/MGLKeys.cpp +210 -187
  8. package/cpp/MGLKeys.h +25 -13
  9. package/cpp/MGLQuickCryptoHostObject.cpp +6 -6
  10. package/cpp/Sig/MGLSignHostObjects.cpp +284 -421
  11. package/cpp/Sig/MGLSignHostObjects.h +40 -0
  12. package/cpp/Utils/MGLUtils.cpp +71 -42
  13. package/cpp/Utils/MGLUtils.h +81 -6
  14. package/cpp/webcrypto/MGLWebCrypto.cpp +97 -35
  15. package/cpp/webcrypto/MGLWebCrypto.h +5 -7
  16. package/cpp/webcrypto/crypto_aes.cpp +516 -0
  17. package/cpp/webcrypto/crypto_aes.h +79 -0
  18. package/cpp/webcrypto/crypto_ec.cpp +110 -20
  19. package/cpp/webcrypto/crypto_ec.h +18 -5
  20. package/cpp/webcrypto/crypto_keygen.cpp +86 -0
  21. package/cpp/webcrypto/crypto_keygen.h +38 -0
  22. package/lib/commonjs/Cipher.js +140 -95
  23. package/lib/commonjs/Cipher.js.map +1 -1
  24. package/lib/commonjs/Hashnames.js +20 -8
  25. package/lib/commonjs/Hashnames.js.map +1 -1
  26. package/lib/commonjs/NativeQuickCrypto/Cipher.js +23 -8
  27. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  28. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  29. package/lib/commonjs/NativeQuickCrypto/aes.js +6 -0
  30. package/lib/commonjs/NativeQuickCrypto/aes.js.map +1 -0
  31. package/lib/commonjs/NativeQuickCrypto/keygen.js +6 -0
  32. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +1 -0
  33. package/lib/commonjs/NativeQuickCrypto/rsa.js +6 -0
  34. package/lib/commonjs/NativeQuickCrypto/rsa.js.map +1 -0
  35. package/lib/commonjs/NativeQuickCrypto/sig.js +17 -0
  36. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  37. package/lib/commonjs/Utils.js +45 -7
  38. package/lib/commonjs/Utils.js.map +1 -1
  39. package/lib/commonjs/aes.js +184 -227
  40. package/lib/commonjs/aes.js.map +1 -1
  41. package/lib/commonjs/ec.js +79 -91
  42. package/lib/commonjs/ec.js.map +1 -1
  43. package/lib/commonjs/index.js +12 -2
  44. package/lib/commonjs/index.js.map +1 -1
  45. package/lib/commonjs/keygen.js +56 -0
  46. package/lib/commonjs/keygen.js.map +1 -0
  47. package/lib/commonjs/keys.js +84 -29
  48. package/lib/commonjs/keys.js.map +1 -1
  49. package/lib/commonjs/random.js +6 -0
  50. package/lib/commonjs/random.js.map +1 -1
  51. package/lib/commonjs/rsa.js +115 -196
  52. package/lib/commonjs/rsa.js.map +1 -1
  53. package/lib/commonjs/sig.js.map +1 -1
  54. package/lib/commonjs/subtle.js +205 -29
  55. package/lib/commonjs/subtle.js.map +1 -1
  56. package/lib/commonjs/webcrypto.js +14 -0
  57. package/lib/commonjs/webcrypto.js.map +1 -0
  58. package/lib/module/Cipher.js +138 -93
  59. package/lib/module/Cipher.js.map +1 -1
  60. package/lib/module/Hashnames.js +20 -8
  61. package/lib/module/Hashnames.js.map +1 -1
  62. package/lib/module/NativeQuickCrypto/Cipher.js +22 -7
  63. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  64. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  65. package/lib/module/NativeQuickCrypto/aes.js +2 -0
  66. package/lib/module/NativeQuickCrypto/aes.js.map +1 -0
  67. package/lib/module/NativeQuickCrypto/keygen.js +2 -0
  68. package/lib/module/NativeQuickCrypto/keygen.js.map +1 -0
  69. package/lib/module/NativeQuickCrypto/rsa.js +2 -0
  70. package/lib/module/NativeQuickCrypto/rsa.js.map +1 -0
  71. package/lib/module/NativeQuickCrypto/sig.js +13 -0
  72. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  73. package/lib/module/Utils.js +38 -5
  74. package/lib/module/Utils.js.map +1 -1
  75. package/lib/module/aes.js +183 -228
  76. package/lib/module/aes.js.map +1 -1
  77. package/lib/module/ec.js +76 -93
  78. package/lib/module/ec.js.map +1 -1
  79. package/lib/module/index.js +11 -2
  80. package/lib/module/index.js.map +1 -1
  81. package/lib/module/keygen.js +47 -0
  82. package/lib/module/keygen.js.map +1 -0
  83. package/lib/module/keys.js +76 -28
  84. package/lib/module/keys.js.map +1 -1
  85. package/lib/module/random.js +6 -0
  86. package/lib/module/random.js.map +1 -1
  87. package/lib/module/rsa.js +115 -198
  88. package/lib/module/rsa.js.map +1 -1
  89. package/lib/module/sig.js.map +1 -1
  90. package/lib/module/subtle.js +209 -34
  91. package/lib/module/subtle.js.map +1 -1
  92. package/lib/module/webcrypto.js +8 -0
  93. package/lib/module/webcrypto.js.map +1 -0
  94. package/lib/typescript/Cipher.d.ts +23 -14
  95. package/lib/typescript/Cipher.d.ts.map +1 -1
  96. package/lib/typescript/Hash.d.ts.map +1 -1
  97. package/lib/typescript/Hashnames.d.ts +2 -2
  98. package/lib/typescript/Hashnames.d.ts.map +1 -1
  99. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +16 -6
  100. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
  101. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +4 -1
  102. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -1
  103. package/lib/typescript/NativeQuickCrypto/aes.d.ts +5 -0
  104. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +1 -0
  105. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +4 -0
  106. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +1 -0
  107. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +5 -0
  108. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +1 -0
  109. package/lib/typescript/NativeQuickCrypto/sig.d.ts +10 -0
  110. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +1 -1
  111. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +14 -2
  112. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
  113. package/lib/typescript/Utils.d.ts +5 -4
  114. package/lib/typescript/Utils.d.ts.map +1 -1
  115. package/lib/typescript/aes.d.ts +18 -1
  116. package/lib/typescript/aes.d.ts.map +1 -1
  117. package/lib/typescript/ec.d.ts +3 -1
  118. package/lib/typescript/ec.d.ts.map +1 -1
  119. package/lib/typescript/index.d.ts +30 -24
  120. package/lib/typescript/index.d.ts.map +1 -1
  121. package/lib/typescript/keygen.d.ts +6 -0
  122. package/lib/typescript/keygen.d.ts.map +1 -0
  123. package/lib/typescript/keys.d.ts +67 -18
  124. package/lib/typescript/keys.d.ts.map +1 -1
  125. package/lib/typescript/random.d.ts +2 -1
  126. package/lib/typescript/random.d.ts.map +1 -1
  127. package/lib/typescript/rsa.d.ts +9 -1
  128. package/lib/typescript/rsa.d.ts.map +1 -1
  129. package/lib/typescript/sig.d.ts +3 -17
  130. package/lib/typescript/sig.d.ts.map +1 -1
  131. package/lib/typescript/subtle.d.ts +8 -4
  132. package/lib/typescript/subtle.d.ts.map +1 -1
  133. package/lib/typescript/webcrypto.d.ts +9 -0
  134. package/lib/typescript/webcrypto.d.ts.map +1 -0
  135. package/package.json +2 -2
  136. package/src/Cipher.ts +139 -75
  137. package/src/Hashnames.ts +23 -21
  138. package/src/NativeQuickCrypto/Cipher.ts +46 -14
  139. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +6 -0
  140. package/src/NativeQuickCrypto/aes.ts +14 -0
  141. package/src/NativeQuickCrypto/keygen.ts +7 -0
  142. package/src/NativeQuickCrypto/rsa.ts +12 -0
  143. package/src/NativeQuickCrypto/sig.ts +27 -0
  144. package/src/NativeQuickCrypto/webcrypto.ts +28 -2
  145. package/src/Utils.ts +49 -8
  146. package/src/aes.ts +259 -222
  147. package/src/ec.ts +114 -90
  148. package/src/index.ts +10 -1
  149. package/src/keygen.ts +80 -0
  150. package/src/keys.ts +165 -61
  151. package/src/random.ts +12 -1
  152. package/src/rsa.ts +161 -187
  153. package/src/sig.ts +7 -23
  154. package/src/subtle.ts +309 -35
  155. package/src/webcrypto.ts +8 -0
package/src/Cipher.ts CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  validateInt32,
15
15
  type BinaryLikeNode,
16
16
  } from './Utils';
17
- import { type InternalCipher, RSAKeyVariant } from './NativeQuickCrypto/Cipher';
17
+ import { type InternalCipher, KeyVariant } from './NativeQuickCrypto/Cipher';
18
18
  import type {
19
19
  CipherCCMOptions,
20
20
  CipherCCMTypes,
@@ -34,11 +34,21 @@ import { Buffer } from '@craftzdog/react-native-buffer';
34
34
  import { Buffer as SBuffer } from 'safe-buffer';
35
35
  import { constants } from './constants';
36
36
  import {
37
+ CryptoKey,
37
38
  parsePrivateKeyEncoding,
38
39
  parsePublicKeyEncoding,
39
40
  preparePrivateKey,
40
41
  preparePublicOrPrivateKey,
42
+ type CryptoKeyPair,
43
+ type KeyPairType,
44
+ type NamedCurve,
41
45
  } from './keys';
46
+ import type { KeyObjectHandle } from './NativeQuickCrypto/webcrypto';
47
+
48
+ export enum ECCurve {
49
+ OPENSSL_EC_EXPLICIT_CURVE,
50
+ OPENSSL_EC_NAMED_CURVE,
51
+ }
42
52
 
43
53
  // make sure that nextTick is there
44
54
  global.process.nextTick = setImmediate;
@@ -428,8 +438,8 @@ export const privateDecrypt = rsaFunctionFor(
428
438
  // \__, |\___|_| |_|\___|_| \__,_|\__\___|_|\_\___|\__, |_| \__,_|_|_|
429
439
  // __/ | __/ |
430
440
  // |___/ |___/
431
- type GenerateKeyPairOptions = {
432
- modulusLength: number; // Key size in bits (RSA, DSA).
441
+ export type GenerateKeyPairOptions = {
442
+ modulusLength?: number; // Key size in bits (RSA, DSA).
433
443
  publicExponent?: number; // Public exponent (RSA). Default: 0x10001.
434
444
  hashAlgorithm?: string; // Name of the message digest (RSA-PSS).
435
445
  mgf1HashAlgorithm?: string; // string Name of the message digest used by MGF1 (RSA-PSS).
@@ -446,11 +456,27 @@ type GenerateKeyPairOptions = {
446
456
  hash?: any;
447
457
  mgf1Hash?: any;
448
458
  };
449
- type GenerateKeyPairCallback = (
450
- error: unknown | null,
451
- publicKey?: Buffer,
452
- privateKey?: Buffer
453
- ) => void;
459
+
460
+ export type KeyPairKey = Buffer | KeyObjectHandle | CryptoKey | undefined;
461
+
462
+ export type GenerateKeyPairReturn = [
463
+ error?: Error,
464
+ privateKey?: KeyPairKey,
465
+ publicKey?: KeyPairKey,
466
+ ];
467
+
468
+ export type GenerateKeyPairCallback = (
469
+ error?: Error,
470
+ publicKey?: KeyPairKey,
471
+ privateKey?: KeyPairKey
472
+ ) => GenerateKeyPairReturn | void;
473
+
474
+ export type KeyPair = {
475
+ publicKey?: KeyPairKey;
476
+ privateKey?: KeyPairKey;
477
+ };
478
+
479
+ export type GenerateKeyPairPromiseReturn = [error?: Error, keypair?: KeyPair];
454
480
 
455
481
  function parseKeyEncoding(
456
482
  keyType: string,
@@ -505,14 +531,15 @@ function parseKeyEncoding(
505
531
  ];
506
532
  }
507
533
 
534
+ /** On node a very complex "job" chain is created, we are going for a far simpler approach and calling
535
+ * an internal function that basically executes the same byte shuffling on the native side
536
+ */
508
537
  function internalGenerateKeyPair(
509
538
  isAsync: boolean,
510
- type: string,
539
+ type: KeyPairType,
511
540
  options: GenerateKeyPairOptions | undefined,
512
- callback: GenerateKeyPairCallback | undefined
513
- ) {
514
- // On node a very complex "job" chain is created, we are going for a far simpler approach and calling
515
- // an internal function that basically executes the same byte shuffling on the native side
541
+ callback?: GenerateKeyPairCallback
542
+ ): GenerateKeyPairReturn | void {
516
543
  const encoding = parseKeyEncoding(type, options);
517
544
 
518
545
  // if (options !== undefined)
@@ -520,11 +547,11 @@ function internalGenerateKeyPair(
520
547
 
521
548
  switch (type) {
522
549
  case 'rsa-pss':
523
- case 'rsa': {
550
+ // fallthrough
551
+ case 'rsa':
524
552
  validateObject<GenerateKeyPairOptions>(options, 'options');
525
553
  const { modulusLength } = options!;
526
- validateUint32(modulusLength, 'options.modulusLength');
527
-
554
+ validateUint32(modulusLength as number, 'options.modulusLength');
528
555
  let { publicExponent } = options!;
529
556
  if (publicExponent == null) {
530
557
  publicExponent = 0x10001;
@@ -535,37 +562,36 @@ function internalGenerateKeyPair(
535
562
  if (type === 'rsa') {
536
563
  if (isAsync) {
537
564
  NativeQuickCrypto.generateKeyPair(
538
- RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5,
539
- modulusLength,
565
+ KeyVariant.RSA_SSA_PKCS1_v1_5, // Used also for RSA-OAEP
566
+ modulusLength as number,
540
567
  publicExponent,
541
568
  ...encoding
542
569
  )
543
570
  .then(([err, publicKey, privateKey]) => {
544
- if (typeof publicKey === 'object') {
571
+ if (publicKey instanceof Buffer) {
545
572
  publicKey = Buffer.from(publicKey);
546
573
  }
547
- if (typeof privateKey === 'object') {
574
+ if (privateKey instanceof Buffer) {
548
575
  privateKey = Buffer.from(privateKey);
549
576
  }
550
- callback?.(err, publicKey, privateKey);
577
+ callback!(err, publicKey, privateKey);
551
578
  })
552
579
  .catch((err) => {
553
- callback?.(err, undefined, undefined);
580
+ callback!(err, undefined, undefined);
554
581
  });
555
- return;
556
582
  } else {
557
583
  let [err, publicKey, privateKey] =
558
584
  NativeQuickCrypto.generateKeyPairSync(
559
- RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5,
560
- modulusLength,
585
+ KeyVariant.RSA_SSA_PKCS1_v1_5,
586
+ modulusLength as number,
561
587
  publicExponent,
562
588
  ...encoding
563
589
  );
564
590
 
565
- if (typeof publicKey === 'object') {
591
+ if (publicKey instanceof Buffer) {
566
592
  publicKey = Buffer.from(publicKey);
567
593
  }
568
- if (typeof privateKey === 'object') {
594
+ if (privateKey instanceof Buffer) {
569
595
  privateKey = Buffer.from(privateKey);
570
596
  }
571
597
 
@@ -609,15 +635,15 @@ function internalGenerateKeyPair(
609
635
  }
610
636
 
611
637
  return NativeQuickCrypto.generateKeyPairSync(
612
- RSAKeyVariant.kKeyVariantRSA_PSS,
613
- modulusLength,
638
+ KeyVariant.RSA_PSS,
639
+ modulusLength as number,
614
640
  publicExponent,
615
641
  hashAlgorithm || hash,
616
642
  mgf1HashAlgorithm || mgf1Hash,
617
643
  saltLength,
618
644
  ...encoding
619
645
  );
620
- }
646
+
621
647
  // case 'dsa': {
622
648
  // validateObject(options, 'options');
623
649
  // const { modulusLength } = options!;
@@ -634,21 +660,57 @@ function internalGenerateKeyPair(
634
660
  // // divisorLength,
635
661
  // // ...encoding);
636
662
  // }
637
- // case 'ec': {
638
- // validateObject(options, 'options');
639
- // const { namedCurve } = options!;
640
- // validateString(namedCurve, 'options.namedCurve');
641
- // let { paramEncoding } = options!;
642
- // if (paramEncoding == null || paramEncoding === 'named')
643
- // paramEncoding = OPENSSL_EC_NAMED_CURVE;
644
- // else if (paramEncoding === 'explicit')
645
- // paramEncoding = OPENSSL_EC_EXPLICIT_CURVE;
646
- // else
647
- // throw new Error(`Invalid Argument options.paramEncoding ${paramEncoding}`);
648
- // // throw new ERR_INVALID_ARG_VALUE('options.paramEncoding', paramEncoding);
649
-
650
- // // return new EcKeyPairGenJob(mode, namedCurve, paramEncoding, ...encoding);
651
- // }
663
+
664
+ case 'ec':
665
+ validateObject<GenerateKeyPairOptions>(options, 'options');
666
+ const { namedCurve } = options!;
667
+ validateString(namedCurve, 'options.namedCurve');
668
+ let paramEncodingFlag = ECCurve.OPENSSL_EC_NAMED_CURVE;
669
+ const { paramEncoding } = options!;
670
+ if (paramEncoding == null || paramEncoding === 'named')
671
+ paramEncodingFlag = ECCurve.OPENSSL_EC_NAMED_CURVE;
672
+ else if (paramEncoding === 'explicit')
673
+ paramEncodingFlag = ECCurve.OPENSSL_EC_EXPLICIT_CURVE;
674
+ else
675
+ throw new Error(
676
+ `Invalid Argument options.paramEncoding ${paramEncoding}`
677
+ );
678
+
679
+ if (isAsync) {
680
+ NativeQuickCrypto.generateKeyPair(
681
+ KeyVariant.EC,
682
+ namedCurve as NamedCurve,
683
+ paramEncodingFlag,
684
+ ...encoding
685
+ )
686
+ .then(([err, publicKey, privateKey]) => {
687
+ if (publicKey instanceof Buffer) {
688
+ publicKey = Buffer.from(publicKey);
689
+ }
690
+ if (privateKey instanceof Buffer) {
691
+ privateKey = Buffer.from(privateKey);
692
+ }
693
+ callback?.(err, publicKey, privateKey);
694
+ })
695
+ .catch((err) => {
696
+ callback?.(err, undefined, undefined);
697
+ });
698
+ }
699
+
700
+ let [err, publicKey, privateKey] = NativeQuickCrypto.generateKeyPairSync(
701
+ KeyVariant.EC,
702
+ namedCurve as NamedCurve,
703
+ paramEncodingFlag,
704
+ ...encoding
705
+ );
706
+ if (publicKey instanceof Buffer) {
707
+ publicKey = Buffer.from(publicKey);
708
+ }
709
+ if (privateKey instanceof Buffer) {
710
+ privateKey = Buffer.from(privateKey);
711
+ }
712
+ return [err, publicKey, privateKey];
713
+
652
714
  // case 'ed25519':
653
715
  // case 'ed448':
654
716
  // case 'x25519':
@@ -712,48 +774,50 @@ function internalGenerateKeyPair(
712
774
  default:
713
775
  // Fall through
714
776
  }
715
- throw new Error(
716
- `Invalid Argument options: ${type} scheme not supported. Currently not all encryption methods are supported in quick-crypto!`
717
- );
777
+ const err = new Error(`
778
+ Invalid Argument options: '${type}' scheme not supported for generateKey().
779
+ Currently not all encryption methods are supported in quick-crypto. Check
780
+ implementation_coverage.md for status.
781
+ `);
782
+ return [err, undefined, undefined];
718
783
  }
719
784
 
720
- // TODO(osp) put correct types (e.g. type -> 'rsa', etc..)
721
- export function generateKeyPair(
722
- type: string,
723
- callback: GenerateKeyPairCallback
724
- ): void;
725
- export function generateKeyPair(
726
- type: string,
785
+ export const generateKeyPair = (
786
+ type: KeyPairType,
727
787
  options: GenerateKeyPairOptions,
728
788
  callback: GenerateKeyPairCallback
729
- ): void;
730
- export function generateKeyPair(
731
- type: string,
732
- options?: GenerateKeyPairCallback | GenerateKeyPairOptions,
733
- callback?: GenerateKeyPairCallback
734
- ) {
735
- if (typeof options === 'function') {
736
- callback = options;
737
- options = undefined;
738
- }
739
-
789
+ ): void => {
740
790
  validateFunction(callback);
741
-
742
791
  internalGenerateKeyPair(true, type, options, callback);
743
- }
792
+ };
744
793
 
745
- export function generateKeyPairSync(type: string): {
746
- publicKey: any;
747
- privateKey: any;
794
+ // Promisify generateKeyPair
795
+ // (attempted to use util.promisify, to no avail)
796
+ export const generateKeyPairPromise = (
797
+ type: KeyPairType,
798
+ options: GenerateKeyPairOptions
799
+ ): Promise<GenerateKeyPairPromiseReturn> => {
800
+ return new Promise((resolve, reject) => {
801
+ generateKeyPair(type, options, (err, publicKey, privateKey) => {
802
+ if (err) {
803
+ reject([err, undefined]);
804
+ } else {
805
+ resolve([undefined, { publicKey, privateKey }]);
806
+ }
807
+ });
808
+ });
748
809
  };
810
+
811
+ // generateKeyPairSync
812
+ export function generateKeyPairSync(type: KeyPairType): CryptoKeyPair;
749
813
  export function generateKeyPairSync(
750
- type: string,
814
+ type: KeyPairType,
751
815
  options: GenerateKeyPairOptions
752
- ): { publicKey: any; privateKey: any };
816
+ ): CryptoKeyPair;
753
817
  export function generateKeyPairSync(
754
- type: string,
818
+ type: KeyPairType,
755
819
  options?: GenerateKeyPairOptions
756
- ): { publicKey: any; privateKey: any } {
820
+ ): CryptoKeyPair {
757
821
  const [_, publicKey, privateKey] = internalGenerateKeyPair(
758
822
  false,
759
823
  type,
package/src/Hashnames.ts CHANGED
@@ -1,9 +1,4 @@
1
- import type {
2
- HashAlgorithm,
3
- KeyPairAlgorithm,
4
- SecretKeyAlgorithm,
5
- SubtleAlgorithm,
6
- } from './keys';
1
+ import type { HashAlgorithm } from './keys';
7
2
 
8
3
  export enum HashContext {
9
4
  Node,
@@ -32,6 +27,14 @@ const kHashNames: HashNames = {
32
27
  [HashContext.JwkRsaOaep]: 'RSA-OAEP',
33
28
  [HashContext.JwkHmac]: 'HS1',
34
29
  },
30
+ sha224: {
31
+ [HashContext.Node]: 'sha224',
32
+ [HashContext.WebCrypto]: 'SHA-224',
33
+ [HashContext.JwkRsa]: 'RS224',
34
+ [HashContext.JwkRsaPss]: 'PS224',
35
+ [HashContext.JwkRsaOaep]: 'RSA-OAEP-224',
36
+ [HashContext.JwkHmac]: 'HS224',
37
+ },
35
38
  sha256: {
36
39
  [HashContext.Node]: 'sha256',
37
40
  [HashContext.WebCrypto]: 'SHA-256',
@@ -56,6 +59,10 @@ const kHashNames: HashNames = {
56
59
  [HashContext.JwkRsaOaep]: 'RSA-OAEP-512',
57
60
  [HashContext.JwkHmac]: 'HS512',
58
61
  },
62
+ ripemd160: {
63
+ [HashContext.Node]: 'ripemd160',
64
+ [HashContext.WebCrypto]: 'RIPEMD-160',
65
+ },
59
66
  };
60
67
 
61
68
  {
@@ -72,20 +79,15 @@ const kHashNames: HashNames = {
72
79
  }
73
80
 
74
81
  export function normalizeHashName(
75
- algo:
76
- | SubtleAlgorithm
77
- | HashAlgorithm
78
- | KeyPairAlgorithm
79
- | SecretKeyAlgorithm
80
- | undefined,
82
+ algo: string | HashAlgorithm | undefined,
81
83
  context: HashContext = HashContext.Node
82
- ): string {
83
- if (typeof algo === 'undefined') return 'unknown';
84
- if (typeof algo !== 'string') return algo.name;
85
- const normAlgo = algo.toString().toLowerCase();
86
- try {
87
- const alias = kHashNames[normAlgo]![context];
88
- return alias || algo;
89
- } catch (_e) {}
90
- return algo;
84
+ ): HashAlgorithm {
85
+ if (typeof algo !== 'undefined') {
86
+ const normAlgo = algo.toString().toLowerCase();
87
+ try {
88
+ const alias = kHashNames[normAlgo]![context] as HashAlgorithm;
89
+ if (alias) return alias;
90
+ } catch (_e) {}
91
+ }
92
+ throw new Error(`Invalid Hash Algorithm: ${algo}`);
91
93
  }
@@ -1,14 +1,37 @@
1
+ import type { GenerateKeyPairReturn } from '../Cipher';
1
2
  import type { BinaryLike } from '../Utils';
2
3
  import type { Buffer } from '@craftzdog/react-native-buffer';
4
+ import type {
5
+ EncodingOptions,
6
+ PrivateKeyObject,
7
+ PublicKeyObject,
8
+ SecretKeyObject,
9
+ } from '../keys';
3
10
 
4
- // TODO(osp) on node this is defined on the native side
5
- // Need to do the same so that values are always in sync
6
- export enum RSAKeyVariant {
7
- kKeyVariantRSA_SSA_PKCS1_v1_5,
8
- kKeyVariantRSA_PSS,
9
- kKeyVariantRSA_OAEP,
11
+ // TODO: until shared, keep in sync with C++ side (cpp/Utils/MGLUtils.h)
12
+ export enum KeyVariant {
13
+ RSA_SSA_PKCS1_v1_5,
14
+ RSA_PSS,
15
+ RSA_OAEP,
16
+ DSA,
17
+ EC,
18
+ NID,
19
+ DH,
10
20
  }
11
21
 
22
+ export const KeyVariantLookup: Record<string, KeyVariant> = {
23
+ 'RSASSA-PKCS1-v1_5': KeyVariant.RSA_SSA_PKCS1_v1_5,
24
+ 'RSA-PSS': KeyVariant.RSA_PSS,
25
+ 'RSA-OAEP': KeyVariant.RSA_OAEP,
26
+ 'ECDSA': KeyVariant.DSA,
27
+ 'ECDH': KeyVariant.EC,
28
+ 'Ed25519': KeyVariant.NID,
29
+ 'Ed448': KeyVariant.NID,
30
+ 'X25519': KeyVariant.NID,
31
+ 'X448': KeyVariant.NID,
32
+ 'DH': KeyVariant.DH,
33
+ };
34
+
12
35
  export type InternalCipher = {
13
36
  update: (data: BinaryLike | ArrayBufferView) => ArrayBuffer;
14
37
  final: () => ArrayBuffer;
@@ -56,15 +79,24 @@ export type PrivateDecryptMethod = (
56
79
  ) => Buffer;
57
80
 
58
81
  export type GenerateKeyPairMethod = (
59
- keyVariant: RSAKeyVariant,
60
- modulusLength: number,
61
- publicExponent: number,
82
+ keyVariant: KeyVariant,
62
83
  ...rest: any[]
63
- ) => Promise<[error: unknown, publicBuffer: any, privateBuffer: any]>;
84
+ ) => Promise<GenerateKeyPairReturn>;
64
85
 
65
86
  export type GenerateKeyPairSyncMethod = (
66
- keyVariant: RSAKeyVariant,
67
- modulusLength: number,
68
- publicExponent: number,
87
+ keyVariant: KeyVariant,
69
88
  ...rest: any[]
70
- ) => [error: unknown, publicBuffer: any, privateBuffer: any];
89
+ ) => GenerateKeyPairReturn;
90
+
91
+ export type CreatePublicKeyMethod = (
92
+ key: BinaryLike | EncodingOptions
93
+ ) => PublicKeyObject;
94
+
95
+ export type CreatePrivateKeyMethod = (
96
+ key: BinaryLike | EncodingOptions
97
+ ) => PrivateKeyObject;
98
+
99
+ export type CreateSecretKeyMethod = (
100
+ key: BinaryLike | EncodingOptions,
101
+ encoding?: string
102
+ ) => SecretKeyObject;
@@ -10,6 +10,9 @@ import type {
10
10
  PrivateDecryptMethod,
11
11
  GenerateKeyPairMethod,
12
12
  GenerateKeyPairSyncMethod,
13
+ CreatePublicKeyMethod,
14
+ CreatePrivateKeyMethod,
15
+ CreateSecretKeyMethod,
13
16
  } from './Cipher';
14
17
  import type { CreateSignMethod, CreateVerifyMethod } from './sig';
15
18
  import type { webcrypto } from './webcrypto';
@@ -21,6 +24,9 @@ interface NativeQuickCryptoSpec {
21
24
  createHash: CreateHashMethod;
22
25
  createCipher: CreateCipherMethod;
23
26
  createDecipher: CreateDecipherMethod;
27
+ createPublicKey: CreatePublicKeyMethod;
28
+ createPrivateKey: CreatePrivateKeyMethod;
29
+ createSecretKey: CreateSecretKeyMethod;
24
30
  publicEncrypt: PublicEncryptMethod;
25
31
  publicDecrypt: PublicEncryptMethod;
26
32
  privateDecrypt: PrivateDecryptMethod;
@@ -0,0 +1,14 @@
1
+ import type { AESKeyVariant } from '../aes';
2
+ import type { CipherOrWrapMode } from '../keys';
3
+ import type { KeyObjectHandle } from './webcrypto';
4
+
5
+ export type AESCipher = (
6
+ mode: CipherOrWrapMode,
7
+ handle: KeyObjectHandle,
8
+ data: ArrayBuffer,
9
+ variant: AESKeyVariant,
10
+ iv_or_counter?: ArrayBuffer,
11
+ length?: number,
12
+ authTag?: ArrayBuffer,
13
+ additionalData?: ArrayBuffer
14
+ ) => Promise<ArrayBuffer>;
@@ -0,0 +1,7 @@
1
+ import type { KeyObjectHandle } from './webcrypto';
2
+
3
+ export type GenerateSecretKeyMethod = (
4
+ length: number
5
+ ) => Promise<KeyObjectHandle>;
6
+
7
+ export type GenerateSecretKeySyncMethod = (length: number) => KeyObjectHandle;
@@ -0,0 +1,12 @@
1
+ import type { CipherOrWrapMode, DigestAlgorithm } from '../keys';
2
+ import type { RSAKeyVariant } from '../rsa';
3
+ import type { KeyObjectHandle } from './webcrypto';
4
+
5
+ export type RSACipher = (
6
+ mode: CipherOrWrapMode,
7
+ handle: KeyObjectHandle,
8
+ data: ArrayBuffer,
9
+ variant: RSAKeyVariant,
10
+ hash: DigestAlgorithm,
11
+ label?: ArrayBuffer
12
+ ) => Promise<ArrayBuffer>;
@@ -1,4 +1,7 @@
1
1
  // TODO Add real types to sign/verify, the problem is that because of encryption schemes
2
+
3
+ import type { KeyObjectHandle } from './webcrypto';
4
+
2
5
  // they will have variable amount of parameters
3
6
  export type InternalSign = {
4
7
  init: (algorithm: string) => void;
@@ -15,3 +18,27 @@ export type InternalVerify = {
15
18
  export type CreateSignMethod = () => InternalSign;
16
19
 
17
20
  export type CreateVerifyMethod = () => InternalVerify;
21
+
22
+ export enum DSASigEnc {
23
+ kSigEncDER,
24
+ kSigEncP1363,
25
+ }
26
+
27
+ export enum SignMode {
28
+ kSignJobModeSign,
29
+ kSignJobModeVerify,
30
+ }
31
+
32
+ export type SignVerify = (
33
+ mode: SignMode,
34
+ handle: KeyObjectHandle,
35
+ unused1: undefined,
36
+ unused2: undefined,
37
+ unused3: undefined,
38
+ data: ArrayBuffer,
39
+ digest: string | undefined,
40
+ salt_length: number | undefined,
41
+ padding: number | undefined,
42
+ dsa_encoding: DSASigEnc | undefined,
43
+ signature: ArrayBuffer | undefined
44
+ ) => ArrayBuffer | boolean;
@@ -1,3 +1,4 @@
1
+ import type { AESCipher } from './aes';
1
2
  import type {
2
3
  AsymmetricKeyType,
3
4
  JWK,
@@ -7,6 +8,13 @@ import type {
7
8
  KWebCryptoKeyFormat,
8
9
  NamedCurve,
9
10
  } from '../keys';
11
+ import type { SignVerify } from './sig';
12
+ import type {
13
+ GenerateSecretKeyMethod,
14
+ GenerateSecretKeySyncMethod,
15
+ } from './keygen';
16
+ import type { KeyVariant } from './Cipher';
17
+ import type { RSACipher } from './rsa';
10
18
 
11
19
  type KeyDetail = {
12
20
  length?: number;
@@ -23,6 +31,12 @@ type ECExportKey = (
23
31
  handle: KeyObjectHandle
24
32
  ) => ArrayBuffer;
25
33
 
34
+ type RSAExportKey = (
35
+ format: KWebCryptoKeyFormat,
36
+ handle: KeyObjectHandle,
37
+ variant: KeyVariant
38
+ ) => ArrayBuffer;
39
+
26
40
  export type KeyObjectHandle = {
27
41
  export(
28
42
  format?: KFormatType,
@@ -32,7 +46,13 @@ export type KeyObjectHandle = {
32
46
  ): ArrayBuffer;
33
47
  exportJwk(key: JWK, handleRsaPss: boolean): JWK;
34
48
  getAsymmetricKeyType(): AsymmetricKeyType;
35
- init(keyType: KeyType, key: any): boolean;
49
+ init(
50
+ keyType: KeyType,
51
+ key: any,
52
+ format?: KFormatType,
53
+ type?: KeyEncoding,
54
+ passphrase?: string | ArrayBuffer
55
+ ): boolean;
36
56
  initECRaw(curveName: string, keyData: ArrayBuffer): boolean;
37
57
  initJwk(keyData: JWK, namedCurve?: NamedCurve): KeyType | undefined;
38
58
  keyDetail(): KeyDetail;
@@ -41,6 +61,12 @@ export type KeyObjectHandle = {
41
61
  type CreateKeyObjectHandle = () => KeyObjectHandle;
42
62
 
43
63
  export type webcrypto = {
44
- ecExportKey: ECExportKey;
64
+ aesCipher: AESCipher;
45
65
  createKeyObjectHandle: CreateKeyObjectHandle;
66
+ ecExportKey: ECExportKey;
67
+ generateSecretKey: GenerateSecretKeyMethod;
68
+ generateSecretKeySync: GenerateSecretKeySyncMethod;
69
+ rsaCipher: RSACipher;
70
+ rsaExportKey: RSAExportKey;
71
+ signVerify: SignVerify;
46
72
  };