react-native-quick-crypto 0.2.0 → 0.3.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 +23 -6
- package/cpp/Cipher/MGLCipherHostObject.cpp +64 -48
- package/cpp/Cipher/MGLCipherKeys.cpp +1469 -0
- package/cpp/Cipher/MGLCipherKeys.h +124 -0
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +56 -53
- package/cpp/Cipher/MGLCreateCipherInstaller.h +5 -0
- package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +56 -53
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +107 -0
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +32 -0
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +60 -0
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +35 -0
- package/cpp/Cipher/MGLPublicCipher.h +120 -0
- package/cpp/Cipher/MGLPublicCipherInstaller.h +113 -0
- package/cpp/Cipher/MGLRsa.cpp +188 -0
- package/cpp/Cipher/MGLRsa.h +61 -0
- package/cpp/JSIUtils/MGLJSIUtils.h +24 -0
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
- package/cpp/MGLQuickCryptoHostObject.cpp +42 -3
- package/cpp/Utils/MGLUtils.cpp +156 -0
- package/cpp/Utils/MGLUtils.h +254 -0
- package/lib/commonjs/Cipher.js +307 -0
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -0
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/QuickCrypto.js +8 -0
- package/lib/commonjs/QuickCrypto.js.map +1 -1
- package/lib/commonjs/Utils.js +82 -1
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/constants.js +86 -0
- package/lib/commonjs/constants.js.map +1 -0
- package/lib/commonjs/index.js +5 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys.js +207 -0
- package/lib/commonjs/keys.js.map +1 -0
- package/lib/module/Cipher.js +296 -3
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +9 -1
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/QuickCrypto.js +8 -1
- package/lib/module/QuickCrypto.js.map +1 -1
- package/lib/module/Utils.js +67 -1
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/constants.js +79 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/index.js +2 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys.js +193 -0
- package/lib/module/keys.js.map +1 -0
- package/lib/typescript/Cipher.d.ts +58 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +10 -0
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +6 -1
- package/lib/typescript/QuickCrypto.d.ts +105 -1
- package/lib/typescript/Utils.d.ts +11 -0
- package/lib/typescript/constants.d.ts +75 -0
- package/lib/typescript/index.d.ts +2 -0
- package/lib/typescript/keys.d.ts +60 -0
- package/package.json +5 -5
- package/react-native-quick-crypto.podspec +1 -1
- package/src/.DS_Store +0 -0
- package/src/Cipher.ts +444 -3
- package/src/NativeQuickCrypto/Cipher.ts +44 -0
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +13 -1
- package/src/QuickCrypto.ts +12 -0
- package/src/Utils.ts +91 -0
- package/src/constants.ts +79 -0
- package/src/index.ts +4 -0
- package/src/keys.ts +297 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export declare const constants: {
|
|
2
|
+
OPENSSL_VERSION_NUMBER: number;
|
|
3
|
+
SSL_OP_ALL: number;
|
|
4
|
+
SSL_OP_ALLOW_NO_DHE_KEX: number;
|
|
5
|
+
SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION: number;
|
|
6
|
+
SSL_OP_CIPHER_SERVER_PREFERENCE: number;
|
|
7
|
+
SSL_OP_CISCO_ANYCONNECT: number;
|
|
8
|
+
SSL_OP_COOKIE_EXCHANGE: number;
|
|
9
|
+
SSL_OP_CRYPTOPRO_TLSEXT_BUG: number;
|
|
10
|
+
SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS: number;
|
|
11
|
+
SSL_OP_EPHEMERAL_RSA: number;
|
|
12
|
+
SSL_OP_LEGACY_SERVER_CONNECT: number;
|
|
13
|
+
SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER: number;
|
|
14
|
+
SSL_OP_MICROSOFT_SESS_ID_BUG: number;
|
|
15
|
+
SSL_OP_MSIE_SSLV2_RSA_PADDING: number;
|
|
16
|
+
SSL_OP_NETSCAPE_CA_DN_BUG: number;
|
|
17
|
+
SSL_OP_NETSCAPE_CHALLENGE_BUG: number;
|
|
18
|
+
SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG: number;
|
|
19
|
+
SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG: number;
|
|
20
|
+
SSL_OP_NO_COMPRESSION: number;
|
|
21
|
+
SSL_OP_NO_ENCRYPT_THEN_MAC: number;
|
|
22
|
+
SSL_OP_NO_QUERY_MTU: number;
|
|
23
|
+
SSL_OP_NO_RENEGOTIATION: number;
|
|
24
|
+
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION: number;
|
|
25
|
+
SSL_OP_NO_SSLv2: number;
|
|
26
|
+
SSL_OP_NO_SSLv3: number;
|
|
27
|
+
SSL_OP_NO_TICKET: number;
|
|
28
|
+
SSL_OP_NO_TLSv1: number;
|
|
29
|
+
SSL_OP_NO_TLSv1_1: number;
|
|
30
|
+
SSL_OP_NO_TLSv1_2: number;
|
|
31
|
+
SSL_OP_NO_TLSv1_3: number;
|
|
32
|
+
SSL_OP_PKCS1_CHECK_1: number;
|
|
33
|
+
SSL_OP_PKCS1_CHECK_2: number;
|
|
34
|
+
SSL_OP_PRIORITIZE_CHACHA: number;
|
|
35
|
+
SSL_OP_SINGLE_DH_USE: number;
|
|
36
|
+
SSL_OP_SINGLE_ECDH_USE: number;
|
|
37
|
+
SSL_OP_SSLEAY_080_CLIENT_DH_BUG: number;
|
|
38
|
+
SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG: number;
|
|
39
|
+
SSL_OP_TLS_BLOCK_PADDING_BUG: number;
|
|
40
|
+
SSL_OP_TLS_D5_BUG: number;
|
|
41
|
+
SSL_OP_TLS_ROLLBACK_BUG: number;
|
|
42
|
+
ENGINE_METHOD_RSA: number;
|
|
43
|
+
ENGINE_METHOD_DSA: number;
|
|
44
|
+
ENGINE_METHOD_DH: number;
|
|
45
|
+
ENGINE_METHOD_RAND: number;
|
|
46
|
+
ENGINE_METHOD_EC: number;
|
|
47
|
+
ENGINE_METHOD_CIPHERS: number;
|
|
48
|
+
ENGINE_METHOD_DIGESTS: number;
|
|
49
|
+
ENGINE_METHOD_PKEY_METHS: number;
|
|
50
|
+
ENGINE_METHOD_PKEY_ASN1_METHS: number;
|
|
51
|
+
ENGINE_METHOD_ALL: number;
|
|
52
|
+
ENGINE_METHOD_NONE: number;
|
|
53
|
+
DH_CHECK_P_NOT_SAFE_PRIME: number;
|
|
54
|
+
DH_CHECK_P_NOT_PRIME: number;
|
|
55
|
+
DH_UNABLE_TO_CHECK_GENERATOR: number;
|
|
56
|
+
DH_NOT_SUITABLE_GENERATOR: number;
|
|
57
|
+
ALPN_ENABLED: number;
|
|
58
|
+
RSA_PKCS1_PADDING: number;
|
|
59
|
+
RSA_SSLV23_PADDING: number;
|
|
60
|
+
RSA_NO_PADDING: number;
|
|
61
|
+
RSA_PKCS1_OAEP_PADDING: number;
|
|
62
|
+
RSA_X931_PADDING: number;
|
|
63
|
+
RSA_PKCS1_PSS_PADDING: number;
|
|
64
|
+
RSA_PSS_SALTLEN_DIGEST: number;
|
|
65
|
+
RSA_PSS_SALTLEN_MAX_SIGN: number;
|
|
66
|
+
RSA_PSS_SALTLEN_AUTO: number;
|
|
67
|
+
defaultCoreCipherList: string;
|
|
68
|
+
TLS1_VERSION: number;
|
|
69
|
+
TLS1_1_VERSION: number;
|
|
70
|
+
TLS1_2_VERSION: number;
|
|
71
|
+
TLS1_3_VERSION: number;
|
|
72
|
+
POINT_CONVERSION_COMPRESSED: number;
|
|
73
|
+
POINT_CONVERSION_UNCOMPRESSED: number;
|
|
74
|
+
POINT_CONVERSION_HYBRID: number;
|
|
75
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { BinaryLike } from './Utils';
|
|
2
|
+
declare enum KFormatType {
|
|
3
|
+
kKeyFormatDER = 0,
|
|
4
|
+
kKeyFormatPEM = 1,
|
|
5
|
+
kKeyFormatJWK = 2
|
|
6
|
+
}
|
|
7
|
+
declare enum KeyEncoding {
|
|
8
|
+
kKeyEncodingPKCS1 = 0,
|
|
9
|
+
kKeyEncodingPKCS8 = 1,
|
|
10
|
+
kKeyEncodingSPKI = 2,
|
|
11
|
+
kKeyEncodingSEC1 = 3
|
|
12
|
+
}
|
|
13
|
+
export declare function preparePrivateKey(key: BinaryLike | {
|
|
14
|
+
key: any;
|
|
15
|
+
encoding?: string;
|
|
16
|
+
format?: any;
|
|
17
|
+
padding?: number;
|
|
18
|
+
passphrase?: string;
|
|
19
|
+
}): {
|
|
20
|
+
format: KFormatType;
|
|
21
|
+
data: ArrayBuffer;
|
|
22
|
+
type?: any;
|
|
23
|
+
passphrase?: any;
|
|
24
|
+
};
|
|
25
|
+
export declare function preparePublicOrPrivateKey(key: BinaryLike | {
|
|
26
|
+
key: any;
|
|
27
|
+
encoding?: string;
|
|
28
|
+
format?: any;
|
|
29
|
+
padding?: number;
|
|
30
|
+
}): {
|
|
31
|
+
format: KFormatType;
|
|
32
|
+
data: ArrayBuffer;
|
|
33
|
+
type?: any;
|
|
34
|
+
passphrase?: any;
|
|
35
|
+
};
|
|
36
|
+
export declare function parsePublicKeyEncoding(enc: {
|
|
37
|
+
key: any;
|
|
38
|
+
encoding?: string;
|
|
39
|
+
format?: string;
|
|
40
|
+
cipher?: string;
|
|
41
|
+
passphrase?: string;
|
|
42
|
+
}, keyType: string | undefined, objName?: string): {
|
|
43
|
+
format: KFormatType;
|
|
44
|
+
type: KeyEncoding | undefined;
|
|
45
|
+
cipher: string | undefined;
|
|
46
|
+
passphrase: ArrayBuffer | undefined;
|
|
47
|
+
};
|
|
48
|
+
export declare function parsePrivateKeyEncoding(enc: {
|
|
49
|
+
key: any;
|
|
50
|
+
encoding?: string;
|
|
51
|
+
format?: string;
|
|
52
|
+
cipher?: string;
|
|
53
|
+
passphrase?: string;
|
|
54
|
+
}, keyType: string | undefined, objName?: string): {
|
|
55
|
+
format: KFormatType;
|
|
56
|
+
type: KeyEncoding | undefined;
|
|
57
|
+
cipher: string | undefined;
|
|
58
|
+
passphrase: ArrayBuffer | undefined;
|
|
59
|
+
};
|
|
60
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-quick-crypto",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
27
27
|
"prepare": "bob build",
|
|
28
28
|
"release": "release-it",
|
|
29
|
-
"pods": "cd example && pod-install --quiet"
|
|
29
|
+
"pods": "cd example/ios && pod-install --quiet"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
32
|
"react-native",
|
|
@@ -55,14 +55,14 @@
|
|
|
55
55
|
"@release-it/conventional-changelog": "^3.3.0",
|
|
56
56
|
"@types/react": "^17.0.21",
|
|
57
57
|
"@types/react-native": "^0.65.5",
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
59
|
-
"eslint": "^
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.28.0",
|
|
59
|
+
"eslint": "^8.17.0",
|
|
60
60
|
"prettier": "^2.4.0",
|
|
61
61
|
"react": "^17.0.2",
|
|
62
62
|
"react-native": "^0.66.0",
|
|
63
63
|
"react-native-builder-bob": "^0.18.1",
|
|
64
64
|
"release-it": "^14.11.5",
|
|
65
|
-
"typescript": "^4.4
|
|
65
|
+
"typescript": "^4.7.4"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"react": "*",
|
|
@@ -33,7 +33,7 @@ Pod::Spec.new do |s|
|
|
|
33
33
|
"HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_TARGET_SRCROOT)\" \"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/boost-for-react-native\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/Headers/Private/React-Core\" "
|
|
34
34
|
}
|
|
35
35
|
s.xcconfig = {
|
|
36
|
-
"CLANG_CXX_LANGUAGE_STANDARD" => "c++
|
|
36
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
|
|
37
37
|
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/boost-for-react-native\" \"$(PODS_ROOT)/glog\" \"${PODS_ROOT}/Headers/Public/React-hermes\" \"${PODS_ROOT}/Headers/Public/hermes-engine\""
|
|
38
38
|
}
|
|
39
39
|
|
package/src/.DS_Store
ADDED
|
Binary file
|
package/src/Cipher.ts
CHANGED
|
@@ -7,8 +7,14 @@ import {
|
|
|
7
7
|
CipherEncoding,
|
|
8
8
|
Encoding,
|
|
9
9
|
getDefaultEncoding,
|
|
10
|
+
kEmptyObject,
|
|
11
|
+
validateFunction,
|
|
12
|
+
validateObject,
|
|
13
|
+
validateString,
|
|
14
|
+
validateUint32,
|
|
15
|
+
validateInt32,
|
|
10
16
|
} from './Utils';
|
|
11
|
-
import
|
|
17
|
+
import { InternalCipher, RSAKeyVariant } from './NativeQuickCrypto/Cipher';
|
|
12
18
|
// TODO(osp) re-enable type specific constructors
|
|
13
19
|
// They are nice to have but not absolutely necessary
|
|
14
20
|
// import type {
|
|
@@ -18,16 +24,29 @@ import type { InternalCipher } from './NativeQuickCrypto/Cipher';
|
|
|
18
24
|
// CipherGCMOptions,
|
|
19
25
|
// // CipherKey,
|
|
20
26
|
// // KeyObject,
|
|
21
|
-
// // TODO
|
|
27
|
+
// // TODO(Szymon) This types seem to be missing? Where did you get this definitions from?
|
|
22
28
|
// // CipherOCBTypes,
|
|
23
29
|
// // CipherOCBOptions,
|
|
24
30
|
// } from 'crypto'; // Node crypto typings
|
|
25
31
|
import { StringDecoder } from 'string_decoder';
|
|
26
|
-
import
|
|
32
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
27
33
|
import { Buffer as SBuffer } from 'safe-buffer';
|
|
34
|
+
import { constants } from './constants';
|
|
35
|
+
import {
|
|
36
|
+
parsePrivateKeyEncoding,
|
|
37
|
+
parsePublicKeyEncoding,
|
|
38
|
+
preparePrivateKey,
|
|
39
|
+
preparePublicOrPrivateKey,
|
|
40
|
+
} from './keys';
|
|
41
|
+
|
|
42
|
+
// make sure that nextTick is there
|
|
43
|
+
global.process.nextTick = setImmediate;
|
|
28
44
|
|
|
29
45
|
const createInternalCipher = NativeQuickCrypto.createCipher;
|
|
30
46
|
const createInternalDecipher = NativeQuickCrypto.createDecipher;
|
|
47
|
+
const _publicEncrypt = NativeQuickCrypto.publicEncrypt;
|
|
48
|
+
const _publicDecrypt = NativeQuickCrypto.publicDecrypt;
|
|
49
|
+
const _privateDecrypt = NativeQuickCrypto.privateDecrypt;
|
|
31
50
|
|
|
32
51
|
function getUIntOption(options: Record<string, any>, key: string) {
|
|
33
52
|
let value;
|
|
@@ -320,3 +339,425 @@ export function createCipheriv(
|
|
|
320
339
|
): Cipher {
|
|
321
340
|
return new Cipher(algorithm, key, options, iv);
|
|
322
341
|
}
|
|
342
|
+
|
|
343
|
+
// RSA Functions
|
|
344
|
+
// Follows closely the model implemented in node
|
|
345
|
+
|
|
346
|
+
// TODO(osp) types...
|
|
347
|
+
function rsaFunctionFor(
|
|
348
|
+
method: (
|
|
349
|
+
data: ArrayBuffer,
|
|
350
|
+
format: number,
|
|
351
|
+
type: any,
|
|
352
|
+
passphrase: any,
|
|
353
|
+
buffer: ArrayBuffer,
|
|
354
|
+
padding: number,
|
|
355
|
+
oaepHash: any,
|
|
356
|
+
oaepLabel: any
|
|
357
|
+
) => Buffer,
|
|
358
|
+
defaultPadding: number,
|
|
359
|
+
keyType: 'public' | 'private'
|
|
360
|
+
) {
|
|
361
|
+
return (
|
|
362
|
+
options: {
|
|
363
|
+
key: any;
|
|
364
|
+
encoding?: string;
|
|
365
|
+
format?: any;
|
|
366
|
+
padding?: any;
|
|
367
|
+
oaepHash?: any;
|
|
368
|
+
oaepLabel?: any;
|
|
369
|
+
passphrase?: string;
|
|
370
|
+
},
|
|
371
|
+
buffer: BinaryLike
|
|
372
|
+
) => {
|
|
373
|
+
const { format, type, data, passphrase } =
|
|
374
|
+
keyType === 'private'
|
|
375
|
+
? preparePrivateKey(options)
|
|
376
|
+
: preparePublicOrPrivateKey(options);
|
|
377
|
+
const padding = options.padding || defaultPadding;
|
|
378
|
+
const { oaepHash, encoding } = options;
|
|
379
|
+
let { oaepLabel } = options;
|
|
380
|
+
if (oaepHash !== undefined) validateString(oaepHash, 'key.oaepHash');
|
|
381
|
+
if (oaepLabel !== undefined)
|
|
382
|
+
oaepLabel = binaryLikeToArrayBuffer(oaepLabel, encoding);
|
|
383
|
+
buffer = binaryLikeToArrayBuffer(buffer, encoding);
|
|
384
|
+
|
|
385
|
+
const rawRes = method(
|
|
386
|
+
data,
|
|
387
|
+
format,
|
|
388
|
+
type,
|
|
389
|
+
passphrase,
|
|
390
|
+
buffer,
|
|
391
|
+
padding,
|
|
392
|
+
oaepHash,
|
|
393
|
+
oaepLabel
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
return Buffer.from(rawRes);
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
export const publicEncrypt = rsaFunctionFor(
|
|
401
|
+
_publicEncrypt,
|
|
402
|
+
constants.RSA_PKCS1_OAEP_PADDING,
|
|
403
|
+
'public'
|
|
404
|
+
);
|
|
405
|
+
export const publicDecrypt = rsaFunctionFor(
|
|
406
|
+
_publicDecrypt,
|
|
407
|
+
constants.RSA_PKCS1_PADDING,
|
|
408
|
+
'public'
|
|
409
|
+
);
|
|
410
|
+
// const privateEncrypt = rsaFunctionFor(_privateEncrypt, constants.RSA_PKCS1_PADDING,
|
|
411
|
+
// 'private');
|
|
412
|
+
export const privateDecrypt = rsaFunctionFor(
|
|
413
|
+
_privateDecrypt,
|
|
414
|
+
constants.RSA_PKCS1_OAEP_PADDING,
|
|
415
|
+
'private'
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
// _ _ __ _____ _
|
|
419
|
+
// | | | |/ / | __ \ (_)
|
|
420
|
+
// __ _ ___ _ __ ___ _ __ __ _| |_ ___| ' / ___ _ _| |__) |_ _ _ _ __
|
|
421
|
+
// / _` |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \ < / _ \ | | | ___/ _` | | '__|
|
|
422
|
+
// | (_| | __/ | | | __/ | | (_| | || __/ . \ __/ |_| | | | (_| | | |
|
|
423
|
+
// \__, |\___|_| |_|\___|_| \__,_|\__\___|_|\_\___|\__, |_| \__,_|_|_|
|
|
424
|
+
// __/ | __/ |
|
|
425
|
+
// |___/ |___/
|
|
426
|
+
type GenerateKeyPairOptions = {
|
|
427
|
+
modulusLength: number; // Key size in bits (RSA, DSA).
|
|
428
|
+
publicExponent?: number; // Public exponent (RSA). Default: 0x10001.
|
|
429
|
+
hashAlgorithm?: string; // Name of the message digest (RSA-PSS).
|
|
430
|
+
mgf1HashAlgorithm?: string; // string Name of the message digest used by MGF1 (RSA-PSS).
|
|
431
|
+
saltLength?: number; // Minimal salt length in bytes (RSA-PSS).
|
|
432
|
+
divisorLength?: number; // Size of q in bits (DSA).
|
|
433
|
+
namedCurve?: string; // Name of the curve to use (EC).
|
|
434
|
+
prime?: Buffer; // The prime parameter (DH).
|
|
435
|
+
primeLength?: number; // Prime length in bits (DH).
|
|
436
|
+
generator?: number; // Custom generator (DH). Default: 2.
|
|
437
|
+
groupName?: string; // Diffie-Hellman group name (DH). See crypto.getDiffieHellman().
|
|
438
|
+
publicKeyEncoding?: any; // See keyObject.export().
|
|
439
|
+
privateKeyEncoding?: any; // See keyObject.export().
|
|
440
|
+
paramEncoding?: string;
|
|
441
|
+
hash?: any;
|
|
442
|
+
mgf1Hash?: any;
|
|
443
|
+
};
|
|
444
|
+
type GenerateKeyPairCallback = (
|
|
445
|
+
error: unknown | null,
|
|
446
|
+
publicKey?: Buffer,
|
|
447
|
+
privateKey?: Buffer
|
|
448
|
+
) => void;
|
|
449
|
+
|
|
450
|
+
function parseKeyEncoding(
|
|
451
|
+
keyType: string,
|
|
452
|
+
options: GenerateKeyPairOptions = kEmptyObject
|
|
453
|
+
) {
|
|
454
|
+
const { publicKeyEncoding, privateKeyEncoding } = options;
|
|
455
|
+
|
|
456
|
+
let publicFormat, publicType;
|
|
457
|
+
if (publicKeyEncoding == null) {
|
|
458
|
+
publicFormat = publicType = undefined;
|
|
459
|
+
} else if (typeof publicKeyEncoding === 'object') {
|
|
460
|
+
({ format: publicFormat, type: publicType } = parsePublicKeyEncoding(
|
|
461
|
+
publicKeyEncoding,
|
|
462
|
+
keyType,
|
|
463
|
+
'publicKeyEncoding'
|
|
464
|
+
));
|
|
465
|
+
} else {
|
|
466
|
+
throw new Error(
|
|
467
|
+
'Invalid argument options.publicKeyEncoding',
|
|
468
|
+
publicKeyEncoding
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
let privateFormat, privateType, cipher, passphrase;
|
|
473
|
+
if (privateKeyEncoding == null) {
|
|
474
|
+
privateFormat = privateType = undefined;
|
|
475
|
+
} else if (typeof privateKeyEncoding === 'object') {
|
|
476
|
+
({
|
|
477
|
+
format: privateFormat,
|
|
478
|
+
type: privateType,
|
|
479
|
+
cipher,
|
|
480
|
+
passphrase,
|
|
481
|
+
} = parsePrivateKeyEncoding(
|
|
482
|
+
privateKeyEncoding,
|
|
483
|
+
keyType,
|
|
484
|
+
'privateKeyEncoding'
|
|
485
|
+
));
|
|
486
|
+
} else {
|
|
487
|
+
throw new Error(
|
|
488
|
+
'Invalid argument options.privateKeyEncoding',
|
|
489
|
+
publicKeyEncoding
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return [
|
|
494
|
+
publicFormat,
|
|
495
|
+
publicType,
|
|
496
|
+
privateFormat,
|
|
497
|
+
privateType,
|
|
498
|
+
cipher,
|
|
499
|
+
passphrase,
|
|
500
|
+
];
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function internalGenerateKeyPair(
|
|
504
|
+
isAsync: boolean,
|
|
505
|
+
type: string,
|
|
506
|
+
options: GenerateKeyPairOptions | undefined,
|
|
507
|
+
callback: GenerateKeyPairCallback | undefined
|
|
508
|
+
) {
|
|
509
|
+
// On node a very complex "job" chain is created, we are going for a far simpler approach and calling
|
|
510
|
+
// an internal function that basically executes the same byte shuffling on the native side
|
|
511
|
+
const encoding = parseKeyEncoding(type, options);
|
|
512
|
+
|
|
513
|
+
// if (options !== undefined)
|
|
514
|
+
// validateObject(options, 'options');
|
|
515
|
+
|
|
516
|
+
switch (type) {
|
|
517
|
+
case 'rsa-pss':
|
|
518
|
+
case 'rsa': {
|
|
519
|
+
validateObject<GenerateKeyPairOptions>(options, 'options');
|
|
520
|
+
const { modulusLength } = options!;
|
|
521
|
+
validateUint32(modulusLength, 'options.modulusLength');
|
|
522
|
+
|
|
523
|
+
let { publicExponent } = options!;
|
|
524
|
+
if (publicExponent == null) {
|
|
525
|
+
publicExponent = 0x10001;
|
|
526
|
+
} else {
|
|
527
|
+
validateUint32(publicExponent, 'options.publicExponent');
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (type === 'rsa') {
|
|
531
|
+
if (isAsync) {
|
|
532
|
+
NativeQuickCrypto.generateKeyPair(
|
|
533
|
+
RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5,
|
|
534
|
+
modulusLength,
|
|
535
|
+
publicExponent,
|
|
536
|
+
...encoding
|
|
537
|
+
)
|
|
538
|
+
.then(([err, publicKey, privateKey]) => {
|
|
539
|
+
if (typeof publicKey === 'object') {
|
|
540
|
+
publicKey = Buffer.from(publicKey);
|
|
541
|
+
}
|
|
542
|
+
if (typeof privateKey === 'object') {
|
|
543
|
+
privateKey = Buffer.from(privateKey);
|
|
544
|
+
}
|
|
545
|
+
callback?.(err, publicKey, privateKey);
|
|
546
|
+
})
|
|
547
|
+
.catch((err) => {
|
|
548
|
+
callback?.(err, undefined, undefined);
|
|
549
|
+
});
|
|
550
|
+
return;
|
|
551
|
+
} else {
|
|
552
|
+
let [err, publicKey, privateKey] =
|
|
553
|
+
NativeQuickCrypto.generateKeyPairSync(
|
|
554
|
+
RSAKeyVariant.kKeyVariantRSA_SSA_PKCS1_v1_5,
|
|
555
|
+
modulusLength,
|
|
556
|
+
publicExponent,
|
|
557
|
+
...encoding
|
|
558
|
+
);
|
|
559
|
+
|
|
560
|
+
if (typeof publicKey === 'object') {
|
|
561
|
+
publicKey = Buffer.from(publicKey);
|
|
562
|
+
}
|
|
563
|
+
if (typeof privateKey === 'object') {
|
|
564
|
+
privateKey = Buffer.from(privateKey);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return [err, publicKey, privateKey];
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const { hash, mgf1Hash, hashAlgorithm, mgf1HashAlgorithm, saltLength } =
|
|
572
|
+
options!;
|
|
573
|
+
|
|
574
|
+
// // We don't have a process object on RN
|
|
575
|
+
// // const pendingDeprecation = getOptionValue('--pending-deprecation');
|
|
576
|
+
|
|
577
|
+
if (saltLength !== undefined)
|
|
578
|
+
validateInt32(saltLength, 'options.saltLength', 0);
|
|
579
|
+
if (hashAlgorithm !== undefined)
|
|
580
|
+
validateString(hashAlgorithm, 'options.hashAlgorithm');
|
|
581
|
+
if (mgf1HashAlgorithm !== undefined)
|
|
582
|
+
validateString(mgf1HashAlgorithm, 'options.mgf1HashAlgorithm');
|
|
583
|
+
if (hash !== undefined) {
|
|
584
|
+
// pendingDeprecation && process.emitWarning(
|
|
585
|
+
// '"options.hash" is deprecated, ' +
|
|
586
|
+
// 'use "options.hashAlgorithm" instead.',
|
|
587
|
+
// 'DeprecationWarning',
|
|
588
|
+
// 'DEP0154');
|
|
589
|
+
validateString(hash, 'options.hash');
|
|
590
|
+
if (hashAlgorithm && hash !== hashAlgorithm) {
|
|
591
|
+
throw new Error(`Invalid Argument options.hash ${hash}`);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
if (mgf1Hash !== undefined) {
|
|
595
|
+
// pendingDeprecation && process.emitWarning(
|
|
596
|
+
// '"options.mgf1Hash" is deprecated, ' +
|
|
597
|
+
// 'use "options.mgf1HashAlgorithm" instead.',
|
|
598
|
+
// 'DeprecationWarning',
|
|
599
|
+
// 'DEP0154');
|
|
600
|
+
validateString(mgf1Hash, 'options.mgf1Hash');
|
|
601
|
+
if (mgf1HashAlgorithm && mgf1Hash !== mgf1HashAlgorithm) {
|
|
602
|
+
throw new Error(`Invalid Argument options.mgf1Hash ${mgf1Hash}`);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
return NativeQuickCrypto.generateKeyPairSync(
|
|
607
|
+
RSAKeyVariant.kKeyVariantRSA_PSS,
|
|
608
|
+
modulusLength,
|
|
609
|
+
publicExponent,
|
|
610
|
+
hashAlgorithm || hash,
|
|
611
|
+
mgf1HashAlgorithm || mgf1Hash,
|
|
612
|
+
saltLength,
|
|
613
|
+
...encoding
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
// case 'dsa': {
|
|
617
|
+
// validateObject(options, 'options');
|
|
618
|
+
// const { modulusLength } = options!;
|
|
619
|
+
// validateUint32(modulusLength, 'options.modulusLength');
|
|
620
|
+
|
|
621
|
+
// let { divisorLength } = options!;
|
|
622
|
+
// if (divisorLength == null) {
|
|
623
|
+
// divisorLength = -1;
|
|
624
|
+
// } else validateInt32(divisorLength, 'options.divisorLength', 0);
|
|
625
|
+
|
|
626
|
+
// // return new DsaKeyPairGenJob(
|
|
627
|
+
// // mode,
|
|
628
|
+
// // modulusLength,
|
|
629
|
+
// // divisorLength,
|
|
630
|
+
// // ...encoding);
|
|
631
|
+
// }
|
|
632
|
+
// case 'ec': {
|
|
633
|
+
// validateObject(options, 'options');
|
|
634
|
+
// const { namedCurve } = options!;
|
|
635
|
+
// validateString(namedCurve, 'options.namedCurve');
|
|
636
|
+
// let { paramEncoding } = options!;
|
|
637
|
+
// if (paramEncoding == null || paramEncoding === 'named')
|
|
638
|
+
// paramEncoding = OPENSSL_EC_NAMED_CURVE;
|
|
639
|
+
// else if (paramEncoding === 'explicit')
|
|
640
|
+
// paramEncoding = OPENSSL_EC_EXPLICIT_CURVE;
|
|
641
|
+
// else
|
|
642
|
+
// throw new Error(`Invalid Argument options.paramEncoding ${paramEncoding}`);
|
|
643
|
+
// // throw new ERR_INVALID_ARG_VALUE('options.paramEncoding', paramEncoding);
|
|
644
|
+
|
|
645
|
+
// // return new EcKeyPairGenJob(mode, namedCurve, paramEncoding, ...encoding);
|
|
646
|
+
// }
|
|
647
|
+
// case 'ed25519':
|
|
648
|
+
// case 'ed448':
|
|
649
|
+
// case 'x25519':
|
|
650
|
+
// case 'x448': {
|
|
651
|
+
// let id;
|
|
652
|
+
// switch (type) {
|
|
653
|
+
// case 'ed25519':
|
|
654
|
+
// id = EVP_PKEY_ED25519;
|
|
655
|
+
// break;
|
|
656
|
+
// case 'ed448':
|
|
657
|
+
// id = EVP_PKEY_ED448;
|
|
658
|
+
// break;
|
|
659
|
+
// case 'x25519':
|
|
660
|
+
// id = EVP_PKEY_X25519;
|
|
661
|
+
// break;
|
|
662
|
+
// case 'x448':
|
|
663
|
+
// id = EVP_PKEY_X448;
|
|
664
|
+
// break;
|
|
665
|
+
// }
|
|
666
|
+
// return new NidKeyPairGenJob(mode, id, ...encoding);
|
|
667
|
+
// }
|
|
668
|
+
// case 'dh': {
|
|
669
|
+
// validateObject(options, 'options');
|
|
670
|
+
// const { group, primeLength, prime, generator } = options;
|
|
671
|
+
// if (group != null) {
|
|
672
|
+
// if (prime != null)
|
|
673
|
+
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'prime');
|
|
674
|
+
// if (primeLength != null)
|
|
675
|
+
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'primeLength');
|
|
676
|
+
// if (generator != null)
|
|
677
|
+
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('group', 'generator');
|
|
678
|
+
|
|
679
|
+
// validateString(group, 'options.group');
|
|
680
|
+
|
|
681
|
+
// return new DhKeyPairGenJob(mode, group, ...encoding);
|
|
682
|
+
// }
|
|
683
|
+
|
|
684
|
+
// if (prime != null) {
|
|
685
|
+
// if (primeLength != null)
|
|
686
|
+
// throw new ERR_INCOMPATIBLE_OPTION_PAIR('prime', 'primeLength');
|
|
687
|
+
|
|
688
|
+
// validateBuffer(prime, 'options.prime');
|
|
689
|
+
// } else if (primeLength != null) {
|
|
690
|
+
// validateInt32(primeLength, 'options.primeLength', 0);
|
|
691
|
+
// } else {
|
|
692
|
+
// throw new ERR_MISSING_OPTION(
|
|
693
|
+
// 'At least one of the group, prime, or primeLength options'
|
|
694
|
+
// );
|
|
695
|
+
// }
|
|
696
|
+
|
|
697
|
+
// if (generator != null) {
|
|
698
|
+
// validateInt32(generator, 'options.generator', 0);
|
|
699
|
+
// }
|
|
700
|
+
// return new DhKeyPairGenJob(
|
|
701
|
+
// mode,
|
|
702
|
+
// prime != null ? prime : primeLength,
|
|
703
|
+
// generator == null ? 2 : generator,
|
|
704
|
+
// ...encoding
|
|
705
|
+
// );
|
|
706
|
+
// }
|
|
707
|
+
default:
|
|
708
|
+
// Fall through
|
|
709
|
+
}
|
|
710
|
+
throw new Error(
|
|
711
|
+
`Invalid Argument options: ${type} scheme not supported. Currently not all encryption methods are supported in quick-crypto!`
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// TODO(osp) put correct types (e.g. type -> 'rsa', etc..)
|
|
716
|
+
export function generateKeyPair(
|
|
717
|
+
type: string,
|
|
718
|
+
callback: GenerateKeyPairCallback
|
|
719
|
+
): void;
|
|
720
|
+
export function generateKeyPair(
|
|
721
|
+
type: string,
|
|
722
|
+
options: GenerateKeyPairOptions,
|
|
723
|
+
callback: GenerateKeyPairCallback
|
|
724
|
+
): void;
|
|
725
|
+
export function generateKeyPair(
|
|
726
|
+
type: string,
|
|
727
|
+
options?: GenerateKeyPairCallback | GenerateKeyPairOptions,
|
|
728
|
+
callback?: GenerateKeyPairCallback
|
|
729
|
+
) {
|
|
730
|
+
if (typeof options === 'function') {
|
|
731
|
+
callback = options;
|
|
732
|
+
options = undefined;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
validateFunction(callback);
|
|
736
|
+
|
|
737
|
+
internalGenerateKeyPair(true, type, options, callback);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
export function generateKeyPairSync(type: string): {
|
|
741
|
+
publicKey: any;
|
|
742
|
+
privateKey: any;
|
|
743
|
+
};
|
|
744
|
+
export function generateKeyPairSync(
|
|
745
|
+
type: string,
|
|
746
|
+
options: GenerateKeyPairOptions
|
|
747
|
+
): { publicKey: any; privateKey: any };
|
|
748
|
+
export function generateKeyPairSync(
|
|
749
|
+
type: string,
|
|
750
|
+
options?: GenerateKeyPairOptions
|
|
751
|
+
): { publicKey: any; privateKey: any } {
|
|
752
|
+
const [_, publicKey, privateKey] = internalGenerateKeyPair(
|
|
753
|
+
false,
|
|
754
|
+
type,
|
|
755
|
+
options,
|
|
756
|
+
undefined
|
|
757
|
+
)!;
|
|
758
|
+
|
|
759
|
+
return {
|
|
760
|
+
publicKey,
|
|
761
|
+
privateKey,
|
|
762
|
+
};
|
|
763
|
+
}
|