react-native-quick-crypto 0.3.2 → 0.4.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 (52) hide show
  1. package/README.md +10 -6
  2. package/android/CMakeLists.txt +10 -2
  3. package/android/gradle.properties +1 -1
  4. package/android/src/main/cpp/cpp-adapter.cpp +1 -1
  5. package/cpp/Cipher/MGLCipherHostObject.cpp +4 -5
  6. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +1 -3
  7. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +6 -3
  8. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +5 -3
  9. package/cpp/Cipher/MGLPublicCipher.h +1 -1
  10. package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
  11. package/cpp/Cipher/MGLRsa.h +5 -1
  12. package/cpp/JSIUtils/MGLJSIMacros.h +69 -6
  13. package/cpp/{Cipher/MGLCipherKeys.cpp → MGLKeys.cpp} +47 -49
  14. package/cpp/{Cipher/MGLCipherKeys.h → MGLKeys.h} +29 -30
  15. package/cpp/MGLQuickCryptoHostObject.cpp +12 -0
  16. package/cpp/Sig/MGLSignHostObjects.cpp +889 -0
  17. package/cpp/Sig/MGLSignHostObjects.h +88 -0
  18. package/cpp/Sig/MGLSignInstaller.cpp +24 -0
  19. package/cpp/Sig/MGLSignInstaller.h +29 -0
  20. package/cpp/Sig/MGLVerifyInstaller.cpp +24 -0
  21. package/cpp/Sig/MGLVerifyInstaller.h +22 -0
  22. package/cpp/Utils/MGLUtils.cpp +67 -29
  23. package/cpp/Utils/MGLUtils.h +17 -17
  24. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  25. package/lib/commonjs/NativeQuickCrypto/sig.js +2 -0
  26. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -0
  27. package/lib/commonjs/QuickCrypto.js +4 -0
  28. package/lib/commonjs/QuickCrypto.js.map +1 -1
  29. package/lib/commonjs/keys.js +1 -4
  30. package/lib/commonjs/keys.js.map +1 -1
  31. package/lib/commonjs/sig.js +170 -0
  32. package/lib/commonjs/sig.js.map +1 -0
  33. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  34. package/lib/module/NativeQuickCrypto/sig.js +2 -0
  35. package/lib/module/NativeQuickCrypto/sig.js.map +1 -0
  36. package/lib/module/QuickCrypto.js +3 -0
  37. package/lib/module/QuickCrypto.js.map +1 -1
  38. package/lib/module/keys.js +1 -4
  39. package/lib/module/keys.js.map +1 -1
  40. package/lib/module/sig.js +155 -0
  41. package/lib/module/sig.js.map +1 -0
  42. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +3 -0
  43. package/lib/typescript/NativeQuickCrypto/sig.d.ts +12 -0
  44. package/lib/typescript/QuickCrypto.d.ts +3 -0
  45. package/lib/typescript/index.d.ts +2 -3
  46. package/lib/typescript/sig.d.ts +35 -0
  47. package/package.json +3 -3
  48. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +3 -0
  49. package/src/NativeQuickCrypto/sig.ts +17 -0
  50. package/src/QuickCrypto.ts +3 -0
  51. package/src/keys.ts +18 -13
  52. package/src/sig.ts +179 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-quick-crypto",
3
- "version": "0.3.2",
3
+ "version": "0.4.2",
4
4
  "description": "A fast implementation of Node's `crypto` module written in C/C++ JSI",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -128,7 +128,8 @@
128
128
  ]
129
129
  },
130
130
  "globals": {
131
- "BufferEncoding": true
131
+ "BufferEncoding": true,
132
+ "Buffer": true
132
133
  }
133
134
  },
134
135
  "eslintIgnore": [
@@ -161,7 +162,6 @@
161
162
  "@types/node": "^17.0.31",
162
163
  "events": "^3.3.0",
163
164
  "react-native-quick-base64": "^2.0.2",
164
- "stream": "^0.0.2",
165
165
  "stream-browserify": "^3.0.0",
166
166
  "string_decoder": "^1.3.0",
167
167
  "crypto-browserify": "^3.12.0"
@@ -11,6 +11,7 @@ import type {
11
11
  GenerateKeyPairMethod,
12
12
  GenerateKeyPairSyncMethod,
13
13
  } from './Cipher';
14
+ import type { CreateSignMethod, CreateVerifyMethod } from './sig';
14
15
 
15
16
  interface NativeQuickCryptoSpec {
16
17
  createHmac: CreateHmacMethod;
@@ -24,6 +25,8 @@ interface NativeQuickCryptoSpec {
24
25
  privateDecrypt: PrivateDecryptMethod;
25
26
  generateKeyPair: GenerateKeyPairMethod;
26
27
  generateKeyPairSync: GenerateKeyPairSyncMethod;
28
+ createSign: CreateSignMethod;
29
+ createVerify: CreateVerifyMethod;
27
30
  }
28
31
 
29
32
  // global func declaration for JSI functions
@@ -0,0 +1,17 @@
1
+ // TODO Add real types to sign/verify, the problem is that because of encryption schemes
2
+ // they will have variable amount of parameters
3
+ export type InternalSign = {
4
+ init: (algorithm: string) => void;
5
+ update: (data: ArrayBuffer) => void;
6
+ sign: (...args: any) => Uint8Array; // returns raw bytes
7
+ };
8
+
9
+ export type InternalVerify = {
10
+ init: (algorithm: string) => void;
11
+ update: (data: ArrayBuffer) => void;
12
+ verify: (...args: any) => boolean;
13
+ };
14
+
15
+ export type CreateSignMethod = () => InternalSign;
16
+
17
+ export type CreateVerifyMethod = () => InternalVerify;
@@ -11,6 +11,7 @@ import {
11
11
  generateKeyPair,
12
12
  generateKeyPairSync,
13
13
  } from './Cipher';
14
+ import { createSign, createVerify } from './sig';
14
15
  import { createHmac } from './Hmac';
15
16
  import { createHash } from './Hash';
16
17
  import { constants } from './constants';
@@ -29,6 +30,8 @@ export const QuickCrypto = {
29
30
  privateDecrypt,
30
31
  generateKeyPair,
31
32
  generateKeyPairSync,
33
+ createSign,
34
+ createVerify,
32
35
  constants,
33
36
  ...pbkdf2,
34
37
  ...random,
package/src/keys.ts CHANGED
@@ -36,7 +36,7 @@ function option(name: string, objName: string | undefined) {
36
36
  }
37
37
 
38
38
  function parseKeyFormat(
39
- formatStr: string,
39
+ formatStr: string | undefined,
40
40
  defaultFormat: KFormatType | undefined,
41
41
  optionName?: string
42
42
  ) {
@@ -50,10 +50,10 @@ function parseKeyFormat(
50
50
  }
51
51
 
52
52
  function parseKeyType(
53
- typeStr: string,
53
+ typeStr: string | undefined,
54
54
  required: boolean,
55
- keyType: string,
56
- isPublic: boolean,
55
+ keyType: string | undefined,
56
+ isPublic: boolean | undefined,
57
57
  optionName: string
58
58
  ) {
59
59
  if (typeStr === undefined && !required) {
@@ -63,10 +63,6 @@ function parseKeyType(
63
63
  throw new Error(
64
64
  `Crypto incompatible key options: ${typeStr} can only be used for RSA keys`
65
65
  );
66
- // throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(
67
- // typeStr,
68
- // 'can only be used for RSA keys'
69
- // );
70
66
  }
71
67
  return KeyEncoding.kKeyEncodingPKCS1;
72
68
  } else if (typeStr === 'spki' && isPublic !== false) {
@@ -86,10 +82,17 @@ function parseKeyType(
86
82
  }
87
83
 
88
84
  function parseKeyFormatAndType(
89
- enc: any,
90
- keyType: any,
91
- isPublic: any,
92
- objName: any
85
+ enc: {
86
+ key: any;
87
+ type?: string;
88
+ encoding?: string;
89
+ format?: string;
90
+ cipher?: string;
91
+ passphrase?: string;
92
+ },
93
+ keyType: string | undefined,
94
+ isPublic: boolean | undefined,
95
+ objName: string | undefined
93
96
  ) {
94
97
  const { format: formatStr, type: typeStr } = enc;
95
98
 
@@ -103,6 +106,7 @@ function parseKeyFormatAndType(
103
106
  const isRequired =
104
107
  (!isInput || format === KFormatType.kKeyFormatDER) &&
105
108
  format !== KFormatType.kKeyFormatJWK;
109
+
106
110
  const type = parseKeyType(
107
111
  typeStr,
108
112
  isRequired,
@@ -116,6 +120,7 @@ function parseKeyFormatAndType(
116
120
  function parseKeyEncoding(
117
121
  enc: {
118
122
  key: any;
123
+ type?: string;
119
124
  encoding?: string;
120
125
  format?: string;
121
126
  cipher?: string;
@@ -123,7 +128,7 @@ function parseKeyEncoding(
123
128
  },
124
129
  keyType: string | undefined,
125
130
  isPublic: boolean | undefined,
126
- objName?: string
131
+ objName?: string | undefined
127
132
  ) {
128
133
  // validateObject(enc, 'options');
129
134
 
package/src/sig.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
2
+ import type { InternalSign, InternalVerify } from './NativeQuickCrypto/sig';
3
+ import Stream from 'stream-browserify';
4
+
5
+ // TODO(osp) same as publicCipher on node this are defined on C++ and exposed to node
6
+ // Do the same here
7
+ enum DSASigEnc {
8
+ kSigEncDER,
9
+ kSigEncP1363,
10
+ }
11
+
12
+ import {
13
+ BinaryLike,
14
+ binaryLikeToArrayBuffer,
15
+ getDefaultEncoding,
16
+ } from './Utils';
17
+ import { preparePrivateKey, preparePublicOrPrivateKey } from './keys';
18
+
19
+ const createInternalSign = NativeQuickCrypto.createSign;
20
+ const createInternalVerify = NativeQuickCrypto.createVerify;
21
+
22
+ function getPadding(options: any) {
23
+ return getIntOption('padding', options);
24
+ }
25
+
26
+ function getSaltLength(options: any) {
27
+ return getIntOption('saltLength', options);
28
+ }
29
+
30
+ function getDSASignatureEncoding(options: any) {
31
+ if (typeof options === 'object') {
32
+ const { dsaEncoding = 'der' } = options;
33
+ if (dsaEncoding === 'der') return DSASigEnc.kSigEncDER;
34
+ else if (dsaEncoding === 'ieee-p1363') return DSASigEnc.kSigEncP1363;
35
+ throw new Error(`options.dsaEncoding: ${dsaEncoding} not a valid encoding`);
36
+ }
37
+
38
+ return DSASigEnc.kSigEncDER;
39
+ }
40
+
41
+ function getIntOption(name: string, options: any) {
42
+ const value = options[name];
43
+ if (value !== undefined) {
44
+ if (value === value >> 0) {
45
+ return value;
46
+ }
47
+ throw new Error(`options.${name}: ${value} not a valid int value`);
48
+ }
49
+ return undefined;
50
+ }
51
+
52
+ class Verify extends Stream.Writable {
53
+ private internal: InternalVerify;
54
+ constructor(algorithm: string, options: Stream.WritableOptions) {
55
+ super(options);
56
+ this.internal = createInternalVerify();
57
+ this.internal.init(algorithm);
58
+ }
59
+
60
+ _write(chunk: BinaryLike, encoding: string, callback: () => void) {
61
+ this.update(chunk, encoding);
62
+ callback();
63
+ }
64
+
65
+ update(data: BinaryLike, encoding?: string) {
66
+ encoding = encoding ?? getDefaultEncoding();
67
+ data = binaryLikeToArrayBuffer(data, encoding);
68
+ this.internal.update(data);
69
+ return this;
70
+ }
71
+
72
+ verify(
73
+ options: {
74
+ key: string | Buffer;
75
+ format?: string;
76
+ type?: string;
77
+ passphrase?: string;
78
+ padding?: number;
79
+ saltLength?: number;
80
+ },
81
+ signature: BinaryLike
82
+ ): boolean {
83
+ if (!options) {
84
+ throw new Error('Crypto sign key required');
85
+ }
86
+
87
+ const { data, format, type, passphrase } =
88
+ preparePublicOrPrivateKey(options);
89
+
90
+ const rsaPadding = getPadding(options);
91
+ const pssSaltLength = getSaltLength(options);
92
+
93
+ // Options specific to (EC)DSA
94
+ const dsaSigEnc = getDSASignatureEncoding(options);
95
+
96
+ const ret = this.internal.verify(
97
+ data,
98
+ format,
99
+ type,
100
+ passphrase,
101
+ binaryLikeToArrayBuffer(signature),
102
+ rsaPadding,
103
+ pssSaltLength,
104
+ dsaSigEnc
105
+ );
106
+
107
+ return ret;
108
+ }
109
+ }
110
+
111
+ class Sign extends Stream.Writable {
112
+ private internal: InternalSign;
113
+ constructor(algorithm: string, options: Stream.WritableOptions) {
114
+ super(options);
115
+ this.internal = createInternalSign();
116
+ this.internal.init(algorithm);
117
+ }
118
+
119
+ _write(chunk: BinaryLike, encoding: string, callback: () => void) {
120
+ this.update(chunk, encoding);
121
+ callback();
122
+ }
123
+
124
+ update(data: BinaryLike, encoding?: string) {
125
+ encoding = encoding ?? getDefaultEncoding();
126
+ data = binaryLikeToArrayBuffer(data, encoding);
127
+ this.internal.update(data);
128
+ return this;
129
+ }
130
+
131
+ sign(
132
+ options: {
133
+ key: string | Buffer;
134
+ format?: string;
135
+ type?: string;
136
+ passphrase?: string;
137
+ padding?: number;
138
+ saltLength?: number;
139
+ },
140
+ encoding?: string
141
+ ) {
142
+ if (!options) {
143
+ throw new Error('Crypto sign key required');
144
+ }
145
+
146
+ const { data, format, type, passphrase } = preparePrivateKey(options);
147
+
148
+ const rsaPadding = getPadding(options);
149
+ const pssSaltLength = getSaltLength(options);
150
+
151
+ // Options specific to (EC)DSA
152
+ const dsaSigEnc = getDSASignatureEncoding(options);
153
+
154
+ const ret = this.internal.sign(
155
+ data,
156
+ format,
157
+ type,
158
+ passphrase,
159
+ rsaPadding,
160
+ pssSaltLength,
161
+ dsaSigEnc
162
+ );
163
+
164
+ encoding = encoding || getDefaultEncoding();
165
+ if (encoding && encoding !== 'buffer') {
166
+ return Buffer.from(ret).toString(encoding as any);
167
+ }
168
+
169
+ return Buffer.from(ret);
170
+ }
171
+ }
172
+
173
+ export function createSign(algorithm: string, options?: any) {
174
+ return new Sign(algorithm, options);
175
+ }
176
+
177
+ export function createVerify(algorithm: string, options?: any) {
178
+ return new Verify(algorithm, options);
179
+ }