react-native-quick-crypto 0.3.2 → 0.4.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.
- package/README.md +10 -6
- package/android/CMakeLists.txt +10 -2
- package/android/gradle.properties +1 -1
- package/cpp/Cipher/MGLCipherHostObject.cpp +4 -5
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +1 -3
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +6 -3
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +5 -3
- package/cpp/Cipher/MGLPublicCipher.h +1 -1
- package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
- package/cpp/Cipher/MGLRsa.h +5 -1
- package/cpp/JSIUtils/MGLJSIMacros.h +69 -6
- package/cpp/{Cipher/MGLCipherKeys.cpp → MGLKeys.cpp} +47 -49
- package/cpp/{Cipher/MGLCipherKeys.h → MGLKeys.h} +29 -30
- package/cpp/MGLQuickCryptoHostObject.cpp +12 -0
- package/cpp/Sig/MGLSignHostObjects.cpp +889 -0
- package/cpp/Sig/MGLSignHostObjects.h +88 -0
- package/cpp/Sig/MGLSignInstaller.cpp +24 -0
- package/cpp/Sig/MGLSignInstaller.h +29 -0
- package/cpp/Sig/MGLVerifyInstaller.cpp +24 -0
- package/cpp/Sig/MGLVerifyInstaller.h +22 -0
- package/cpp/Utils/MGLUtils.cpp +67 -29
- package/cpp/Utils/MGLUtils.h +17 -17
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +2 -0
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -0
- package/lib/commonjs/QuickCrypto.js +4 -0
- package/lib/commonjs/QuickCrypto.js.map +1 -1
- package/lib/commonjs/keys.js +1 -4
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/sig.js +170 -0
- package/lib/commonjs/sig.js.map +1 -0
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js +2 -0
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -0
- package/lib/module/QuickCrypto.js +3 -0
- package/lib/module/QuickCrypto.js.map +1 -1
- package/lib/module/keys.js +1 -4
- package/lib/module/keys.js.map +1 -1
- package/lib/module/sig.js +155 -0
- package/lib/module/sig.js.map +1 -0
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +3 -0
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +12 -0
- package/lib/typescript/QuickCrypto.d.ts +3 -0
- package/lib/typescript/index.d.ts +2 -3
- package/lib/typescript/sig.d.ts +35 -0
- package/package.json +3 -2
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +3 -0
- package/src/NativeQuickCrypto/sig.ts +17 -0
- package/src/QuickCrypto.ts +3 -0
- package/src/keys.ts +18 -13
- 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
|
+
"version": "0.4.0",
|
|
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": [
|
|
@@ -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;
|
package/src/QuickCrypto.ts
CHANGED
|
@@ -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:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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';
|
|
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
|
+
}
|