react-native-quick-crypto 1.0.10 → 1.0.12
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/android/CMakeLists.txt +16 -0
- package/cpp/argon2/HybridArgon2.cpp +103 -0
- package/cpp/argon2/HybridArgon2.hpp +32 -0
- package/cpp/certificate/HybridCertificate.cpp +42 -0
- package/cpp/certificate/HybridCertificate.hpp +16 -0
- package/cpp/cipher/CCMCipher.cpp +4 -1
- package/cpp/cipher/ChaCha20Cipher.cpp +3 -1
- package/cpp/cipher/ChaCha20Poly1305Cipher.cpp +5 -5
- package/cpp/cipher/ChaCha20Poly1305Cipher.hpp +1 -2
- package/cpp/cipher/HybridCipher.cpp +68 -1
- package/cpp/cipher/HybridCipher.hpp +6 -0
- package/cpp/cipher/HybridRsaCipher.cpp +0 -13
- package/cpp/cipher/XChaCha20Poly1305Cipher.cpp +7 -5
- package/cpp/cipher/XChaCha20Poly1305Cipher.hpp +1 -2
- package/cpp/cipher/XSalsa20Cipher.cpp +4 -0
- package/cpp/cipher/XSalsa20Poly1305Cipher.cpp +7 -5
- package/cpp/cipher/XSalsa20Poly1305Cipher.hpp +1 -2
- package/cpp/dh/HybridDhKeyPair.cpp +179 -0
- package/cpp/dh/HybridDhKeyPair.hpp +37 -0
- package/cpp/dsa/HybridDsaKeyPair.cpp +128 -0
- package/cpp/dsa/HybridDsaKeyPair.hpp +32 -0
- package/cpp/ecdh/HybridECDH.cpp +42 -120
- package/cpp/ecdh/HybridECDH.hpp +1 -0
- package/cpp/keys/HybridKeyObjectHandle.cpp +150 -128
- package/cpp/keys/HybridKeyObjectHandle.hpp +6 -3
- package/cpp/keys/KeyObjectData.hpp +2 -0
- package/cpp/kmac/HybridKmac.cpp +83 -0
- package/cpp/kmac/HybridKmac.hpp +31 -0
- package/cpp/mldsa/HybridMlDsaKeyPair.cpp +11 -20
- package/cpp/mldsa/HybridMlDsaKeyPair.hpp +4 -2
- package/cpp/mlkem/HybridMlKemKeyPair.cpp +319 -0
- package/cpp/mlkem/HybridMlKemKeyPair.hpp +48 -0
- package/cpp/prime/HybridPrime.cpp +81 -0
- package/cpp/prime/HybridPrime.hpp +20 -0
- package/cpp/sign/SignUtils.hpp +9 -26
- package/cpp/utils/QuickCryptoUtils.cpp +44 -0
- package/cpp/utils/QuickCryptoUtils.hpp +39 -0
- package/cpp/x509/HybridX509Certificate.cpp +174 -0
- package/cpp/x509/HybridX509Certificate.hpp +51 -0
- package/lib/commonjs/argon2.js +39 -0
- package/lib/commonjs/argon2.js.map +1 -0
- package/lib/commonjs/certificate.js +35 -0
- package/lib/commonjs/certificate.js.map +1 -0
- package/lib/commonjs/cipher.js +23 -2
- package/lib/commonjs/cipher.js.map +1 -1
- package/lib/commonjs/dhKeyPair.js +109 -0
- package/lib/commonjs/dhKeyPair.js.map +1 -0
- package/lib/commonjs/dsa.js +92 -0
- package/lib/commonjs/dsa.js.map +1 -0
- package/lib/commonjs/ec.js +18 -18
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/ecdh.js +37 -0
- package/lib/commonjs/ecdh.js.map +1 -1
- package/lib/commonjs/ed.js +9 -9
- package/lib/commonjs/ed.js.map +1 -1
- package/lib/commonjs/hash.js +17 -12
- package/lib/commonjs/hash.js.map +1 -1
- package/lib/commonjs/hkdf.js.map +1 -1
- package/lib/commonjs/index.js +57 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys/classes.js +11 -9
- package/lib/commonjs/keys/classes.js.map +1 -1
- package/lib/commonjs/keys/generateKeyPair.js +11 -0
- package/lib/commonjs/keys/generateKeyPair.js.map +1 -1
- package/lib/commonjs/keys/index.js +24 -0
- package/lib/commonjs/keys/index.js.map +1 -1
- package/lib/commonjs/keys/signVerify.js +0 -2
- package/lib/commonjs/keys/signVerify.js.map +1 -1
- package/lib/commonjs/mlkem.js +219 -0
- package/lib/commonjs/mlkem.js.map +1 -0
- package/lib/commonjs/pbkdf2.js +18 -1
- package/lib/commonjs/pbkdf2.js.map +1 -1
- package/lib/commonjs/prime.js +84 -0
- package/lib/commonjs/prime.js.map +1 -0
- package/lib/commonjs/rsa.js +7 -7
- package/lib/commonjs/rsa.js.map +1 -1
- package/lib/commonjs/specs/argon2.nitro.js +6 -0
- package/lib/commonjs/specs/argon2.nitro.js.map +1 -0
- package/lib/commonjs/specs/certificate.nitro.js +6 -0
- package/lib/commonjs/specs/certificate.nitro.js.map +1 -0
- package/lib/commonjs/specs/dhKeyPair.nitro.js +6 -0
- package/lib/commonjs/specs/dhKeyPair.nitro.js.map +1 -0
- package/lib/commonjs/specs/dsaKeyPair.nitro.js +6 -0
- package/lib/commonjs/specs/dsaKeyPair.nitro.js.map +1 -0
- package/lib/commonjs/specs/kmac.nitro.js +6 -0
- package/lib/commonjs/specs/kmac.nitro.js.map +1 -0
- package/lib/commonjs/specs/mlKemKeyPair.nitro.js +6 -0
- package/lib/commonjs/specs/mlKemKeyPair.nitro.js.map +1 -0
- package/lib/commonjs/specs/prime.nitro.js +6 -0
- package/lib/commonjs/specs/prime.nitro.js.map +1 -0
- package/lib/commonjs/specs/x509certificate.nitro.js +6 -0
- package/lib/commonjs/specs/x509certificate.nitro.js.map +1 -0
- package/lib/commonjs/subtle.js +385 -114
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/commonjs/utils/conversion.js +3 -3
- package/lib/commonjs/utils/conversion.js.map +1 -1
- package/lib/commonjs/utils/hashnames.js +31 -0
- package/lib/commonjs/utils/hashnames.js.map +1 -1
- package/lib/commonjs/utils/types.js.map +1 -1
- package/lib/commonjs/x509certificate.js +189 -0
- package/lib/commonjs/x509certificate.js.map +1 -0
- package/lib/module/argon2.js +34 -0
- package/lib/module/argon2.js.map +1 -0
- package/lib/module/certificate.js +30 -0
- package/lib/module/certificate.js.map +1 -0
- package/lib/module/cipher.js +23 -3
- package/lib/module/cipher.js.map +1 -1
- package/lib/module/dhKeyPair.js +102 -0
- package/lib/module/dhKeyPair.js.map +1 -0
- package/lib/module/dsa.js +85 -0
- package/lib/module/dsa.js.map +1 -0
- package/lib/module/ec.js +6 -6
- package/lib/module/ec.js.map +1 -1
- package/lib/module/ecdh.js +37 -0
- package/lib/module/ecdh.js.map +1 -1
- package/lib/module/ed.js +1 -1
- package/lib/module/ed.js.map +1 -1
- package/lib/module/hash.js +17 -12
- package/lib/module/hash.js.map +1 -1
- package/lib/module/hkdf.js.map +1 -1
- package/lib/module/index.js +15 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys/classes.js +11 -9
- package/lib/module/keys/classes.js.map +1 -1
- package/lib/module/keys/generateKeyPair.js +11 -0
- package/lib/module/keys/generateKeyPair.js.map +1 -1
- package/lib/module/keys/index.js +25 -1
- package/lib/module/keys/index.js.map +1 -1
- package/lib/module/keys/signVerify.js +0 -2
- package/lib/module/keys/signVerify.js.map +1 -1
- package/lib/module/mlkem.js +211 -0
- package/lib/module/mlkem.js.map +1 -0
- package/lib/module/pbkdf2.js +18 -1
- package/lib/module/pbkdf2.js.map +1 -1
- package/lib/module/prime.js +77 -0
- package/lib/module/prime.js.map +1 -0
- package/lib/module/rsa.js +1 -1
- package/lib/module/rsa.js.map +1 -1
- package/lib/module/specs/argon2.nitro.js +4 -0
- package/lib/module/specs/argon2.nitro.js.map +1 -0
- package/lib/module/specs/certificate.nitro.js +4 -0
- package/lib/module/specs/certificate.nitro.js.map +1 -0
- package/lib/module/specs/dhKeyPair.nitro.js +4 -0
- package/lib/module/specs/dhKeyPair.nitro.js.map +1 -0
- package/lib/module/specs/dsaKeyPair.nitro.js +4 -0
- package/lib/module/specs/dsaKeyPair.nitro.js.map +1 -0
- package/lib/module/specs/kmac.nitro.js +4 -0
- package/lib/module/specs/kmac.nitro.js.map +1 -0
- package/lib/module/specs/mlKemKeyPair.nitro.js +4 -0
- package/lib/module/specs/mlKemKeyPair.nitro.js.map +1 -0
- package/lib/module/specs/prime.nitro.js +4 -0
- package/lib/module/specs/prime.nitro.js.map +1 -0
- package/lib/module/specs/x509certificate.nitro.js +4 -0
- package/lib/module/specs/x509certificate.nitro.js.map +1 -0
- package/lib/module/subtle.js +386 -116
- package/lib/module/subtle.js.map +1 -1
- package/lib/module/utils/conversion.js +3 -4
- package/lib/module/utils/conversion.js.map +1 -1
- package/lib/module/utils/hashnames.js +31 -0
- package/lib/module/utils/hashnames.js.map +1 -1
- package/lib/module/utils/types.js.map +1 -1
- package/lib/module/x509certificate.js +184 -0
- package/lib/module/x509certificate.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/argon2.d.ts +16 -0
- package/lib/typescript/argon2.d.ts.map +1 -0
- package/lib/typescript/certificate.d.ts +8 -0
- package/lib/typescript/certificate.d.ts.map +1 -0
- package/lib/typescript/cipher.d.ts +15 -0
- package/lib/typescript/cipher.d.ts.map +1 -1
- package/lib/typescript/dhKeyPair.d.ts +19 -0
- package/lib/typescript/dhKeyPair.d.ts.map +1 -0
- package/lib/typescript/dsa.d.ts +19 -0
- package/lib/typescript/dsa.d.ts.map +1 -0
- package/lib/typescript/ec.d.ts +1 -1
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/ecdh.d.ts +3 -0
- package/lib/typescript/ecdh.d.ts.map +1 -1
- package/lib/typescript/ed.d.ts +1 -1
- package/lib/typescript/ed.d.ts.map +1 -1
- package/lib/typescript/hash.d.ts.map +1 -1
- package/lib/typescript/hkdf.d.ts +2 -6
- package/lib/typescript/hkdf.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +32 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys/classes.d.ts +7 -5
- package/lib/typescript/keys/classes.d.ts.map +1 -1
- package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -1
- package/lib/typescript/keys/index.d.ts +2 -2
- package/lib/typescript/keys/index.d.ts.map +1 -1
- package/lib/typescript/keys/signVerify.d.ts.map +1 -1
- package/lib/typescript/mlkem.d.ts +30 -0
- package/lib/typescript/mlkem.d.ts.map +1 -0
- package/lib/typescript/pbkdf2.d.ts +2 -2
- package/lib/typescript/pbkdf2.d.ts.map +1 -1
- package/lib/typescript/prime.d.ts +19 -0
- package/lib/typescript/prime.d.ts.map +1 -0
- package/lib/typescript/rsa.d.ts +1 -1
- package/lib/typescript/rsa.d.ts.map +1 -1
- package/lib/typescript/specs/argon2.nitro.d.ts +9 -0
- package/lib/typescript/specs/argon2.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/certificate.nitro.d.ts +10 -0
- package/lib/typescript/specs/certificate.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/cipher.nitro.d.ts +9 -0
- package/lib/typescript/specs/cipher.nitro.d.ts.map +1 -1
- package/lib/typescript/specs/dhKeyPair.nitro.d.ts +14 -0
- package/lib/typescript/specs/dhKeyPair.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/dsaKeyPair.nitro.d.ts +13 -0
- package/lib/typescript/specs/dsaKeyPair.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/ecdh.nitro.d.ts +1 -0
- package/lib/typescript/specs/ecdh.nitro.d.ts.map +1 -1
- package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +1 -0
- package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -1
- package/lib/typescript/specs/kmac.nitro.d.ts +10 -0
- package/lib/typescript/specs/kmac.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/mlKemKeyPair.nitro.d.ts +18 -0
- package/lib/typescript/specs/mlKemKeyPair.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/prime.nitro.d.ts +11 -0
- package/lib/typescript/specs/prime.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/x509certificate.nitro.d.ts +34 -0
- package/lib/typescript/specs/x509certificate.nitro.d.ts.map +1 -0
- package/lib/typescript/subtle.d.ts +12 -0
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/lib/typescript/utils/conversion.d.ts.map +1 -1
- package/lib/typescript/utils/hashnames.d.ts +1 -1
- package/lib/typescript/utils/hashnames.d.ts.map +1 -1
- package/lib/typescript/utils/types.d.ts +25 -9
- package/lib/typescript/utils/types.d.ts.map +1 -1
- package/lib/typescript/x509certificate.d.ts +64 -0
- package/lib/typescript/x509certificate.d.ts.map +1 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +8 -0
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +80 -0
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +80 -0
- package/nitrogen/generated/shared/c++/AsymmetricKeyType.hpp +12 -0
- package/nitrogen/generated/shared/c++/CipherInfo.hpp +104 -0
- package/nitrogen/generated/shared/c++/HybridArgon2Spec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridArgon2Spec.hpp +66 -0
- package/nitrogen/generated/shared/c++/HybridCertificateSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridCertificateSpec.hpp +64 -0
- package/nitrogen/generated/shared/c++/HybridCipherSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridCipherSpec.hpp +4 -0
- package/nitrogen/generated/shared/c++/HybridDhKeyPairSpec.cpp +27 -0
- package/nitrogen/generated/shared/c++/HybridDhKeyPairSpec.hpp +69 -0
- package/nitrogen/generated/shared/c++/HybridDsaKeyPairSpec.cpp +26 -0
- package/nitrogen/generated/shared/c++/HybridDsaKeyPairSpec.hpp +68 -0
- package/nitrogen/generated/shared/c++/HybridECDHSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridECDHSpec.hpp +1 -0
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +1 -0
- package/nitrogen/generated/shared/c++/HybridKmacSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridKmacSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/HybridMlKemKeyPairSpec.cpp +31 -0
- package/nitrogen/generated/shared/c++/HybridMlKemKeyPairSpec.hpp +74 -0
- package/nitrogen/generated/shared/c++/HybridPrimeSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridPrimeSpec.hpp +67 -0
- package/nitrogen/generated/shared/c++/HybridX509CertificateHandleSpec.cpp +46 -0
- package/nitrogen/generated/shared/c++/HybridX509CertificateHandleSpec.hpp +96 -0
- package/package.json +4 -1
- package/src/argon2.ts +83 -0
- package/src/certificate.ts +41 -0
- package/src/cipher.ts +41 -3
- package/src/dhKeyPair.ts +156 -0
- package/src/dsa.ts +129 -0
- package/src/ec.ts +9 -9
- package/src/ecdh.ts +59 -0
- package/src/ed.ts +2 -2
- package/src/hash.ts +34 -11
- package/src/hkdf.ts +2 -7
- package/src/index.ts +16 -0
- package/src/keys/classes.ts +26 -14
- package/src/keys/generateKeyPair.ts +14 -0
- package/src/keys/index.ts +37 -2
- package/src/keys/signVerify.ts +0 -5
- package/src/mlkem.ts +350 -0
- package/src/pbkdf2.ts +34 -5
- package/src/prime.ts +134 -0
- package/src/rsa.ts +1 -1
- package/src/specs/argon2.nitro.ts +29 -0
- package/src/specs/certificate.nitro.ts +8 -0
- package/src/specs/cipher.nitro.ts +14 -0
- package/src/specs/dhKeyPair.nitro.ts +14 -0
- package/src/specs/dsaKeyPair.nitro.ts +13 -0
- package/src/specs/ecdh.nitro.ts +1 -0
- package/src/specs/keyObjectHandle.nitro.ts +5 -0
- package/src/specs/kmac.nitro.ts +12 -0
- package/src/specs/mlKemKeyPair.nitro.ts +32 -0
- package/src/specs/prime.nitro.ts +18 -0
- package/src/specs/x509certificate.nitro.ts +38 -0
- package/src/subtle.ts +821 -136
- package/src/utils/conversion.ts +10 -4
- package/src/utils/hashnames.ts +33 -2
- package/src/utils/types.ts +64 -8
- package/src/x509certificate.ts +277 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-quick-crypto",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
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",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"prepare": "bun clean && bun tsc && bob build",
|
|
20
20
|
"release": "release-it",
|
|
21
21
|
"specs": "nitrogen",
|
|
22
|
+
"circular": "dpdm --circular --no-tree --no-warning --exit-code circular:1 --transform src/index.ts",
|
|
22
23
|
"test": "jest"
|
|
23
24
|
},
|
|
24
25
|
"files": [
|
|
@@ -77,6 +78,7 @@
|
|
|
77
78
|
"events": "3.3.0",
|
|
78
79
|
"readable-stream": "4.5.2",
|
|
79
80
|
"safe-buffer": "^5.2.1",
|
|
81
|
+
"string_decoder": "^1.3.0",
|
|
80
82
|
"util": "0.12.5"
|
|
81
83
|
},
|
|
82
84
|
"devDependencies": {
|
|
@@ -85,6 +87,7 @@
|
|
|
85
87
|
"@types/react": "18.3.3",
|
|
86
88
|
"@types/readable-stream": "4.0.18",
|
|
87
89
|
"del-cli": "7.0.0",
|
|
90
|
+
"dpdm": "^4.0.1",
|
|
88
91
|
"expo": "^54.0.25",
|
|
89
92
|
"expo-build-properties": "^1.0.0",
|
|
90
93
|
"jest": "29.7.0",
|
package/src/argon2.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
2
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
3
|
+
import type { Argon2 as NativeArgon2 } from './specs/argon2.nitro';
|
|
4
|
+
import { binaryLikeToArrayBuffer } from './utils';
|
|
5
|
+
import type { BinaryLike } from './utils';
|
|
6
|
+
|
|
7
|
+
let native: NativeArgon2;
|
|
8
|
+
function getNative(): NativeArgon2 {
|
|
9
|
+
if (native == null) {
|
|
10
|
+
native = NitroModules.createHybridObject<NativeArgon2>('Argon2');
|
|
11
|
+
}
|
|
12
|
+
return native;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Argon2Params {
|
|
16
|
+
message: BinaryLike;
|
|
17
|
+
nonce: BinaryLike;
|
|
18
|
+
parallelism: number;
|
|
19
|
+
tagLength: number;
|
|
20
|
+
memory: number;
|
|
21
|
+
passes: number;
|
|
22
|
+
secret?: BinaryLike;
|
|
23
|
+
associatedData?: BinaryLike;
|
|
24
|
+
version?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const ARGON2_VERSION = 0x13; // v1.3
|
|
28
|
+
|
|
29
|
+
function validateAlgorithm(algorithm: string): void {
|
|
30
|
+
if (
|
|
31
|
+
algorithm !== 'argon2d' &&
|
|
32
|
+
algorithm !== 'argon2i' &&
|
|
33
|
+
algorithm !== 'argon2id'
|
|
34
|
+
) {
|
|
35
|
+
throw new TypeError(`Unknown argon2 algorithm: ${algorithm}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function toAB(value: BinaryLike): ArrayBuffer {
|
|
40
|
+
return binaryLikeToArrayBuffer(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function argon2Sync(algorithm: string, params: Argon2Params): Buffer {
|
|
44
|
+
validateAlgorithm(algorithm);
|
|
45
|
+
const version = params.version ?? ARGON2_VERSION;
|
|
46
|
+
const result = getNative().hashSync(
|
|
47
|
+
algorithm,
|
|
48
|
+
toAB(params.message),
|
|
49
|
+
toAB(params.nonce),
|
|
50
|
+
params.parallelism,
|
|
51
|
+
params.tagLength,
|
|
52
|
+
params.memory,
|
|
53
|
+
params.passes,
|
|
54
|
+
version,
|
|
55
|
+
params.secret ? toAB(params.secret) : undefined,
|
|
56
|
+
params.associatedData ? toAB(params.associatedData) : undefined,
|
|
57
|
+
);
|
|
58
|
+
return Buffer.from(result);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function argon2(
|
|
62
|
+
algorithm: string,
|
|
63
|
+
params: Argon2Params,
|
|
64
|
+
callback: (err: Error | null, result: Buffer) => void,
|
|
65
|
+
): void {
|
|
66
|
+
validateAlgorithm(algorithm);
|
|
67
|
+
const version = params.version ?? ARGON2_VERSION;
|
|
68
|
+
getNative()
|
|
69
|
+
.hash(
|
|
70
|
+
algorithm,
|
|
71
|
+
toAB(params.message),
|
|
72
|
+
toAB(params.nonce),
|
|
73
|
+
params.parallelism,
|
|
74
|
+
params.tagLength,
|
|
75
|
+
params.memory,
|
|
76
|
+
params.passes,
|
|
77
|
+
version,
|
|
78
|
+
params.secret ? toAB(params.secret) : undefined,
|
|
79
|
+
params.associatedData ? toAB(params.associatedData) : undefined,
|
|
80
|
+
)
|
|
81
|
+
.then(ab => callback(null, Buffer.from(ab)))
|
|
82
|
+
.catch((err: Error) => callback(err, Buffer.alloc(0)));
|
|
83
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
3
|
+
import type { Certificate as NativeCertificate } from './specs/certificate.nitro';
|
|
4
|
+
import type { BinaryLike } from './utils';
|
|
5
|
+
import { binaryLikeToArrayBuffer } from './utils';
|
|
6
|
+
|
|
7
|
+
let native: NativeCertificate;
|
|
8
|
+
function getNative(): NativeCertificate {
|
|
9
|
+
if (native == null) {
|
|
10
|
+
native = NitroModules.createHybridObject<NativeCertificate>('Certificate');
|
|
11
|
+
}
|
|
12
|
+
return native;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function toArrayBuffer(
|
|
16
|
+
spkac: BinaryLike,
|
|
17
|
+
encoding?: BufferEncoding,
|
|
18
|
+
): ArrayBuffer {
|
|
19
|
+
if (typeof spkac === 'string') {
|
|
20
|
+
return binaryLikeToArrayBuffer(spkac, encoding || 'utf8');
|
|
21
|
+
}
|
|
22
|
+
return binaryLikeToArrayBuffer(spkac);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class Certificate {
|
|
26
|
+
static exportChallenge(spkac: BinaryLike, encoding?: BufferEncoding): Buffer {
|
|
27
|
+
return Buffer.from(
|
|
28
|
+
getNative().exportChallenge(toArrayBuffer(spkac, encoding)),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static exportPublicKey(spkac: BinaryLike, encoding?: BufferEncoding): Buffer {
|
|
33
|
+
return Buffer.from(
|
|
34
|
+
getNative().exportPublicKey(toArrayBuffer(spkac, encoding)),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static verifySpkac(spkac: BinaryLike, encoding?: BufferEncoding): boolean {
|
|
39
|
+
return getNative().verifySpkac(toArrayBuffer(spkac, encoding));
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/cipher.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
2
|
import Stream, { type TransformOptions } from 'readable-stream';
|
|
3
|
+
import { StringDecoder } from 'string_decoder';
|
|
3
4
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
4
5
|
import type { BinaryLike, BinaryLikeNode, Encoding } from './utils';
|
|
5
6
|
import type {
|
|
@@ -14,7 +15,7 @@ import type {
|
|
|
14
15
|
Cipher as NativeCipher,
|
|
15
16
|
CipherFactory,
|
|
16
17
|
} from './specs/cipher.nitro';
|
|
17
|
-
import {
|
|
18
|
+
import { binaryLikeToArrayBuffer } from './utils';
|
|
18
19
|
import {
|
|
19
20
|
getDefaultEncoding,
|
|
20
21
|
getUIntOption,
|
|
@@ -28,18 +29,42 @@ export type CipherOptions =
|
|
|
28
29
|
| CipherGCMOptions
|
|
29
30
|
| TransformOptions;
|
|
30
31
|
|
|
32
|
+
export interface CipherInfoResult {
|
|
33
|
+
name: string;
|
|
34
|
+
nid: number;
|
|
35
|
+
mode: string;
|
|
36
|
+
keyLength: number;
|
|
37
|
+
blockSize?: number;
|
|
38
|
+
ivLength?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
class CipherUtils {
|
|
32
42
|
private static native =
|
|
33
43
|
NitroModules.createHybridObject<NativeCipher>('Cipher');
|
|
34
44
|
public static getSupportedCiphers(): string[] {
|
|
35
45
|
return this.native.getSupportedCiphers();
|
|
36
46
|
}
|
|
47
|
+
public static getCipherInfo(
|
|
48
|
+
name: string,
|
|
49
|
+
keyLength?: number,
|
|
50
|
+
ivLength?: number,
|
|
51
|
+
): CipherInfoResult | undefined {
|
|
52
|
+
return this.native.getCipherInfo(name, keyLength, ivLength);
|
|
53
|
+
}
|
|
37
54
|
}
|
|
38
55
|
|
|
39
56
|
export function getCiphers(): string[] {
|
|
40
57
|
return CipherUtils.getSupportedCiphers();
|
|
41
58
|
}
|
|
42
59
|
|
|
60
|
+
export function getCipherInfo(
|
|
61
|
+
name: string,
|
|
62
|
+
options?: { keyLength?: number; ivLength?: number },
|
|
63
|
+
): CipherInfoResult | undefined {
|
|
64
|
+
if (typeof name !== 'string' || name.length === 0) return undefined;
|
|
65
|
+
return CipherUtils.getCipherInfo(name, options?.keyLength, options?.ivLength);
|
|
66
|
+
}
|
|
67
|
+
|
|
43
68
|
interface CipherArgs {
|
|
44
69
|
isCipher: boolean;
|
|
45
70
|
cipherType: string;
|
|
@@ -50,6 +75,8 @@ interface CipherArgs {
|
|
|
50
75
|
|
|
51
76
|
class CipherCommon extends Stream.Transform {
|
|
52
77
|
private native: NativeCipher;
|
|
78
|
+
private _decoder: StringDecoder | null = null;
|
|
79
|
+
private _decoderEncoding: string | undefined = undefined;
|
|
53
80
|
|
|
54
81
|
constructor({ isCipher, cipherType, cipherKey, iv, options }: CipherArgs) {
|
|
55
82
|
// Explicitly create TransformOptions for super()
|
|
@@ -96,6 +123,17 @@ class CipherCommon extends Stream.Transform {
|
|
|
96
123
|
});
|
|
97
124
|
}
|
|
98
125
|
|
|
126
|
+
private getDecoder(encoding: string): StringDecoder {
|
|
127
|
+
const normalized = normalizeEncoding(encoding);
|
|
128
|
+
if (!this._decoder) {
|
|
129
|
+
this._decoder = new StringDecoder(encoding as BufferEncoding);
|
|
130
|
+
this._decoderEncoding = normalized;
|
|
131
|
+
} else if (this._decoderEncoding !== normalized) {
|
|
132
|
+
throw new Error('Cannot change encoding');
|
|
133
|
+
}
|
|
134
|
+
return this._decoder;
|
|
135
|
+
}
|
|
136
|
+
|
|
99
137
|
update(data: Buffer): Buffer;
|
|
100
138
|
update(data: BinaryLike, inputEncoding?: Encoding): Buffer;
|
|
101
139
|
update(
|
|
@@ -123,7 +161,7 @@ class CipherCommon extends Stream.Transform {
|
|
|
123
161
|
);
|
|
124
162
|
|
|
125
163
|
if (outputEncoding && outputEncoding !== 'buffer') {
|
|
126
|
-
return
|
|
164
|
+
return this.getDecoder(outputEncoding).write(Buffer.from(ret));
|
|
127
165
|
}
|
|
128
166
|
|
|
129
167
|
return Buffer.from(ret);
|
|
@@ -135,7 +173,7 @@ class CipherCommon extends Stream.Transform {
|
|
|
135
173
|
const ret = this.native.final();
|
|
136
174
|
|
|
137
175
|
if (outputEncoding && outputEncoding !== 'buffer') {
|
|
138
|
-
return
|
|
176
|
+
return this.getDecoder(outputEncoding).end(Buffer.from(ret));
|
|
139
177
|
}
|
|
140
178
|
|
|
141
179
|
return Buffer.from(ret);
|
package/src/dhKeyPair.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
3
|
+
import { KeyObject, PublicKeyObject, PrivateKeyObject } from './keys/classes';
|
|
4
|
+
import type { DhKeyPair } from './specs/dhKeyPair.nitro';
|
|
5
|
+
import type { GenerateKeyPairOptions, KeyPairGenConfig } from './utils/types';
|
|
6
|
+
import { KFormatType, KeyEncoding } from './utils';
|
|
7
|
+
import { DH_GROUPS } from './dh-groups';
|
|
8
|
+
|
|
9
|
+
export class DhKeyPairGen {
|
|
10
|
+
native: DhKeyPair;
|
|
11
|
+
|
|
12
|
+
constructor(options: GenerateKeyPairOptions) {
|
|
13
|
+
this.native = NitroModules.createHybridObject<DhKeyPair>('DhKeyPair');
|
|
14
|
+
|
|
15
|
+
const { groupName, prime, primeLength, generator } = options;
|
|
16
|
+
|
|
17
|
+
if (groupName) {
|
|
18
|
+
// Resolve named group to prime + generator
|
|
19
|
+
const group = DH_GROUPS[groupName];
|
|
20
|
+
if (!group) {
|
|
21
|
+
throw new Error(`Unknown DH group: ${groupName}`);
|
|
22
|
+
}
|
|
23
|
+
const primeBuf = Buffer.from(group.prime, 'hex');
|
|
24
|
+
this.native.setPrime(
|
|
25
|
+
primeBuf.buffer.slice(
|
|
26
|
+
primeBuf.byteOffset,
|
|
27
|
+
primeBuf.byteOffset + primeBuf.byteLength,
|
|
28
|
+
) as ArrayBuffer,
|
|
29
|
+
);
|
|
30
|
+
const gen = parseInt(group.generator, 16);
|
|
31
|
+
this.native.setGenerator(gen);
|
|
32
|
+
} else if (prime) {
|
|
33
|
+
// Custom prime as Buffer
|
|
34
|
+
const primeBuf = Buffer.from(prime);
|
|
35
|
+
this.native.setPrime(
|
|
36
|
+
primeBuf.buffer.slice(
|
|
37
|
+
primeBuf.byteOffset,
|
|
38
|
+
primeBuf.byteOffset + primeBuf.byteLength,
|
|
39
|
+
) as ArrayBuffer,
|
|
40
|
+
);
|
|
41
|
+
this.native.setGenerator(generator ?? 2);
|
|
42
|
+
} else if (primeLength) {
|
|
43
|
+
this.native.setPrimeLength(primeLength);
|
|
44
|
+
this.native.setGenerator(generator ?? 2);
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'DH key generation requires one of: groupName, prime, or primeLength',
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async generateKeyPair(): Promise<void> {
|
|
53
|
+
await this.native.generateKeyPair();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
generateKeyPairSync(): void {
|
|
57
|
+
this.native.generateKeyPairSync();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function dh_prepareKeyGenParams(
|
|
62
|
+
options: GenerateKeyPairOptions | undefined,
|
|
63
|
+
): DhKeyPairGen {
|
|
64
|
+
if (!options) {
|
|
65
|
+
throw new Error('Options are required for DH key generation');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return new DhKeyPairGen(options);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function dh_formatKeyPairOutput(
|
|
72
|
+
dh: DhKeyPairGen,
|
|
73
|
+
encoding: KeyPairGenConfig,
|
|
74
|
+
): {
|
|
75
|
+
publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
76
|
+
privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
77
|
+
} {
|
|
78
|
+
const { publicFormat, privateFormat, cipher, passphrase } = encoding;
|
|
79
|
+
|
|
80
|
+
const publicKeyData = dh.native.getPublicKey();
|
|
81
|
+
const privateKeyData = dh.native.getPrivateKey();
|
|
82
|
+
|
|
83
|
+
const pub = KeyObject.createKeyObject(
|
|
84
|
+
'public',
|
|
85
|
+
publicKeyData,
|
|
86
|
+
KFormatType.DER,
|
|
87
|
+
KeyEncoding.SPKI,
|
|
88
|
+
) as PublicKeyObject;
|
|
89
|
+
|
|
90
|
+
const priv = KeyObject.createKeyObject(
|
|
91
|
+
'private',
|
|
92
|
+
privateKeyData,
|
|
93
|
+
KFormatType.DER,
|
|
94
|
+
KeyEncoding.PKCS8,
|
|
95
|
+
) as PrivateKeyObject;
|
|
96
|
+
|
|
97
|
+
let publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
98
|
+
let privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
99
|
+
|
|
100
|
+
if (publicFormat === -1) {
|
|
101
|
+
publicKey = pub;
|
|
102
|
+
} else {
|
|
103
|
+
const format =
|
|
104
|
+
publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
|
|
105
|
+
const exported = pub.handle.exportKey(format, KeyEncoding.SPKI);
|
|
106
|
+
if (format === KFormatType.PEM) {
|
|
107
|
+
publicKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
|
|
108
|
+
} else {
|
|
109
|
+
publicKey = exported;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (privateFormat === -1) {
|
|
114
|
+
privateKey = priv;
|
|
115
|
+
} else {
|
|
116
|
+
const format =
|
|
117
|
+
privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
|
|
118
|
+
const exported = priv.handle.exportKey(
|
|
119
|
+
format,
|
|
120
|
+
KeyEncoding.PKCS8,
|
|
121
|
+
cipher,
|
|
122
|
+
passphrase,
|
|
123
|
+
);
|
|
124
|
+
if (format === KFormatType.PEM) {
|
|
125
|
+
privateKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
|
|
126
|
+
} else {
|
|
127
|
+
privateKey = exported;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { publicKey, privateKey };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function dh_generateKeyPairNode(
|
|
135
|
+
options: GenerateKeyPairOptions | undefined,
|
|
136
|
+
encoding: KeyPairGenConfig,
|
|
137
|
+
): Promise<{
|
|
138
|
+
publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
139
|
+
privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
140
|
+
}> {
|
|
141
|
+
const dh = dh_prepareKeyGenParams(options);
|
|
142
|
+
await dh.generateKeyPair();
|
|
143
|
+
return dh_formatKeyPairOutput(dh, encoding);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function dh_generateKeyPairNodeSync(
|
|
147
|
+
options: GenerateKeyPairOptions | undefined,
|
|
148
|
+
encoding: KeyPairGenConfig,
|
|
149
|
+
): {
|
|
150
|
+
publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
151
|
+
privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
152
|
+
} {
|
|
153
|
+
const dh = dh_prepareKeyGenParams(options);
|
|
154
|
+
dh.generateKeyPairSync();
|
|
155
|
+
return dh_formatKeyPairOutput(dh, encoding);
|
|
156
|
+
}
|
package/src/dsa.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
|
+
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
3
|
+
import { KeyObject, PublicKeyObject, PrivateKeyObject } from './keys/classes';
|
|
4
|
+
import type { DsaKeyPair } from './specs/dsaKeyPair.nitro';
|
|
5
|
+
import type { GenerateKeyPairOptions, KeyPairGenConfig } from './utils/types';
|
|
6
|
+
import { KFormatType, KeyEncoding } from './utils';
|
|
7
|
+
|
|
8
|
+
export class Dsa {
|
|
9
|
+
native: DsaKeyPair;
|
|
10
|
+
|
|
11
|
+
constructor(modulusLength: number, divisorLength?: number) {
|
|
12
|
+
this.native = NitroModules.createHybridObject<DsaKeyPair>('DsaKeyPair');
|
|
13
|
+
this.native.setModulusLength(modulusLength);
|
|
14
|
+
if (divisorLength !== undefined && divisorLength >= 0) {
|
|
15
|
+
this.native.setDivisorLength(divisorLength);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async generateKeyPair(): Promise<void> {
|
|
20
|
+
await this.native.generateKeyPair();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
generateKeyPairSync(): void {
|
|
24
|
+
this.native.generateKeyPairSync();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function dsa_prepareKeyGenParams(
|
|
29
|
+
options: GenerateKeyPairOptions | undefined,
|
|
30
|
+
): Dsa {
|
|
31
|
+
if (!options) {
|
|
32
|
+
throw new Error('Options are required for DSA key generation');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { modulusLength, divisorLength } = options;
|
|
36
|
+
|
|
37
|
+
if (!modulusLength || modulusLength <= 0) {
|
|
38
|
+
throw new Error('Invalid or missing modulusLength for DSA key generation');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return new Dsa(modulusLength, divisorLength);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function dsa_formatKeyPairOutput(
|
|
45
|
+
dsa: Dsa,
|
|
46
|
+
encoding: KeyPairGenConfig,
|
|
47
|
+
): {
|
|
48
|
+
publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
49
|
+
privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
50
|
+
} {
|
|
51
|
+
const { publicFormat, privateFormat, cipher, passphrase } = encoding;
|
|
52
|
+
|
|
53
|
+
const publicKeyData = dsa.native.getPublicKey();
|
|
54
|
+
const privateKeyData = dsa.native.getPrivateKey();
|
|
55
|
+
|
|
56
|
+
const pub = KeyObject.createKeyObject(
|
|
57
|
+
'public',
|
|
58
|
+
publicKeyData,
|
|
59
|
+
KFormatType.DER,
|
|
60
|
+
KeyEncoding.SPKI,
|
|
61
|
+
) as PublicKeyObject;
|
|
62
|
+
|
|
63
|
+
const priv = KeyObject.createKeyObject(
|
|
64
|
+
'private',
|
|
65
|
+
privateKeyData,
|
|
66
|
+
KFormatType.DER,
|
|
67
|
+
KeyEncoding.PKCS8,
|
|
68
|
+
) as PrivateKeyObject;
|
|
69
|
+
|
|
70
|
+
let publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
71
|
+
let privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
72
|
+
|
|
73
|
+
if (publicFormat === -1) {
|
|
74
|
+
publicKey = pub;
|
|
75
|
+
} else {
|
|
76
|
+
const format =
|
|
77
|
+
publicFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
|
|
78
|
+
const exported = pub.handle.exportKey(format, KeyEncoding.SPKI);
|
|
79
|
+
if (format === KFormatType.PEM) {
|
|
80
|
+
publicKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
|
|
81
|
+
} else {
|
|
82
|
+
publicKey = exported;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (privateFormat === -1) {
|
|
87
|
+
privateKey = priv;
|
|
88
|
+
} else {
|
|
89
|
+
const format =
|
|
90
|
+
privateFormat === KFormatType.PEM ? KFormatType.PEM : KFormatType.DER;
|
|
91
|
+
const exported = priv.handle.exportKey(
|
|
92
|
+
format,
|
|
93
|
+
KeyEncoding.PKCS8,
|
|
94
|
+
cipher,
|
|
95
|
+
passphrase,
|
|
96
|
+
);
|
|
97
|
+
if (format === KFormatType.PEM) {
|
|
98
|
+
privateKey = Buffer.from(new Uint8Array(exported)).toString('utf-8');
|
|
99
|
+
} else {
|
|
100
|
+
privateKey = exported;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { publicKey, privateKey };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function dsa_generateKeyPairNode(
|
|
108
|
+
options: GenerateKeyPairOptions | undefined,
|
|
109
|
+
encoding: KeyPairGenConfig,
|
|
110
|
+
): Promise<{
|
|
111
|
+
publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
112
|
+
privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
113
|
+
}> {
|
|
114
|
+
const dsa = dsa_prepareKeyGenParams(options);
|
|
115
|
+
await dsa.generateKeyPair();
|
|
116
|
+
return dsa_formatKeyPairOutput(dsa, encoding);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function dsa_generateKeyPairNodeSync(
|
|
120
|
+
options: GenerateKeyPairOptions | undefined,
|
|
121
|
+
encoding: KeyPairGenConfig,
|
|
122
|
+
): {
|
|
123
|
+
publicKey: PublicKeyObject | Buffer | string | ArrayBuffer;
|
|
124
|
+
privateKey: PrivateKeyObject | Buffer | string | ArrayBuffer;
|
|
125
|
+
} {
|
|
126
|
+
const dsa = dsa_prepareKeyGenParams(options);
|
|
127
|
+
dsa.generateKeyPairSync();
|
|
128
|
+
return dsa_formatKeyPairOutput(dsa, encoding);
|
|
129
|
+
}
|
package/src/ec.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
KeyObject,
|
|
7
7
|
PublicKeyObject,
|
|
8
8
|
PrivateKeyObject,
|
|
9
|
-
} from './keys';
|
|
9
|
+
} from './keys/classes';
|
|
10
10
|
import type {
|
|
11
11
|
CryptoKeyPair,
|
|
12
12
|
KeyPairOptions,
|
|
@@ -76,7 +76,7 @@ export class Ec {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
//
|
|
79
|
+
// WebCrypto API - only P-256, P-384, P-521 allowed per spec
|
|
80
80
|
export function ecImportKey(
|
|
81
81
|
format: ImportFormat,
|
|
82
82
|
keyData: BufferLike | BinaryLike | JWK,
|
|
@@ -289,7 +289,7 @@ export const ecdsaSignVerify = (
|
|
|
289
289
|
}
|
|
290
290
|
};
|
|
291
291
|
|
|
292
|
-
//
|
|
292
|
+
// WebCrypto API - only P-256, P-384, P-521 allowed per spec
|
|
293
293
|
|
|
294
294
|
export async function ec_generateKeyPair(
|
|
295
295
|
name: string,
|
|
@@ -388,11 +388,8 @@ function ec_prepareKeyGenParams(
|
|
|
388
388
|
|
|
389
389
|
const { namedCurve } = options as { namedCurve?: string };
|
|
390
390
|
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
!kNamedCurveAliases[namedCurve as keyof typeof kNamedCurveAliases]
|
|
394
|
-
) {
|
|
395
|
-
throw new Error(`Invalid or unsupported named curve: ${namedCurve}`);
|
|
391
|
+
if (!namedCurve) {
|
|
392
|
+
throw new Error('namedCurve is required for EC key generation');
|
|
396
393
|
}
|
|
397
394
|
|
|
398
395
|
return new Ec(namedCurve);
|
|
@@ -550,7 +547,10 @@ export function ecDeriveBits(
|
|
|
550
547
|
// If length is specified, truncate
|
|
551
548
|
const byteLength = Math.ceil(length / 8);
|
|
552
549
|
if (secretBuf.byteLength >= byteLength) {
|
|
553
|
-
return secretBuf.
|
|
550
|
+
return secretBuf.buffer.slice(
|
|
551
|
+
secretBuf.byteOffset,
|
|
552
|
+
secretBuf.byteOffset + byteLength,
|
|
553
|
+
) as ArrayBuffer;
|
|
554
554
|
}
|
|
555
555
|
|
|
556
556
|
throw new Error('Derived key is shorter than requested length');
|
package/src/ecdh.ts
CHANGED
|
@@ -2,7 +2,20 @@ import { NitroModules } from 'react-native-nitro-modules';
|
|
|
2
2
|
import type { ECDH as ECDHInterface } from './specs/ecdh.nitro';
|
|
3
3
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
4
4
|
|
|
5
|
+
const POINT_CONVERSION_COMPRESSED = 2;
|
|
6
|
+
const POINT_CONVERSION_UNCOMPRESSED = 4;
|
|
7
|
+
const POINT_CONVERSION_HYBRID = 6;
|
|
8
|
+
|
|
5
9
|
export class ECDH {
|
|
10
|
+
private static _convertKeyHybrid: ECDHInterface | undefined;
|
|
11
|
+
private static get convertKeyHybrid(): ECDHInterface {
|
|
12
|
+
if (!this._convertKeyHybrid) {
|
|
13
|
+
this._convertKeyHybrid =
|
|
14
|
+
NitroModules.createHybridObject<ECDHInterface>('ECDH');
|
|
15
|
+
}
|
|
16
|
+
return this._convertKeyHybrid;
|
|
17
|
+
}
|
|
18
|
+
|
|
6
19
|
private _hybrid: ECDHInterface;
|
|
7
20
|
|
|
8
21
|
constructor(curveName: string) {
|
|
@@ -69,6 +82,52 @@ export class ECDH {
|
|
|
69
82
|
}
|
|
70
83
|
this._hybrid.setPublicKey(keyBuf.buffer as ArrayBuffer);
|
|
71
84
|
}
|
|
85
|
+
|
|
86
|
+
static convertKey(
|
|
87
|
+
key: Buffer | string,
|
|
88
|
+
curve: string,
|
|
89
|
+
inputEncoding?: BufferEncoding,
|
|
90
|
+
outputEncoding?: BufferEncoding,
|
|
91
|
+
format?: 'uncompressed' | 'compressed' | 'hybrid',
|
|
92
|
+
): Buffer | string {
|
|
93
|
+
let keyBuf: Buffer;
|
|
94
|
+
if (Buffer.isBuffer(key)) {
|
|
95
|
+
keyBuf = key;
|
|
96
|
+
} else {
|
|
97
|
+
keyBuf = Buffer.from(key, inputEncoding);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let formatNum: number;
|
|
101
|
+
switch (format) {
|
|
102
|
+
case 'compressed':
|
|
103
|
+
formatNum = POINT_CONVERSION_COMPRESSED;
|
|
104
|
+
break;
|
|
105
|
+
case 'hybrid':
|
|
106
|
+
formatNum = POINT_CONVERSION_HYBRID;
|
|
107
|
+
break;
|
|
108
|
+
case 'uncompressed':
|
|
109
|
+
case undefined:
|
|
110
|
+
formatNum = POINT_CONVERSION_UNCOMPRESSED;
|
|
111
|
+
break;
|
|
112
|
+
default:
|
|
113
|
+
throw new TypeError(
|
|
114
|
+
`Invalid point conversion format: ${format as string}`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const result = Buffer.from(
|
|
119
|
+
ECDH.convertKeyHybrid.convertKey(
|
|
120
|
+
keyBuf.buffer as ArrayBuffer,
|
|
121
|
+
curve,
|
|
122
|
+
formatNum,
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
if (outputEncoding) {
|
|
127
|
+
return result.toString(outputEncoding);
|
|
128
|
+
}
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
72
131
|
}
|
|
73
132
|
|
|
74
133
|
export function createECDH(curveName: string): ECDH {
|
package/src/ed.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
2
|
import { Buffer } from '@craftzdog/react-native-buffer';
|
|
3
|
-
import type { AsymmetricKeyObject, PrivateKeyObject } from './keys';
|
|
3
|
+
import type { AsymmetricKeyObject, PrivateKeyObject } from './keys/classes';
|
|
4
4
|
import {
|
|
5
5
|
CryptoKey,
|
|
6
6
|
KeyObject,
|
|
7
7
|
PublicKeyObject,
|
|
8
8
|
PrivateKeyObject as PrivateKeyObjectClass,
|
|
9
|
-
} from './keys';
|
|
9
|
+
} from './keys/classes';
|
|
10
10
|
import type { EdKeyPair } from './specs/edKeyPair.nitro';
|
|
11
11
|
import type {
|
|
12
12
|
BinaryLike,
|