react-native-quick-crypto 0.7.3 → 1.0.0-beta.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.
- package/QuickCrypto.podspec +34 -0
- package/README.md +13 -46
- package/android/CMakeLists.txt +32 -64
- package/android/build.gradle +52 -125
- package/android/gradle.properties +4 -4
- package/android/src/main/cpp/cpp-adapter.cpp +9 -54
- package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
- package/cpp/random/HybridRandom.cpp +34 -0
- package/cpp/random/HybridRandom.hpp +44 -0
- package/cpp/utils/Utils.hpp +9 -0
- package/ios/QuickCryptoOnLoad.mm +19 -0
- package/lib/commonjs/index.js +39 -39
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/random.js +26 -12
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
- package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
- package/lib/commonjs/utils/conversion.js +22 -0
- package/lib/commonjs/utils/conversion.js.map +1 -0
- package/lib/commonjs/utils/index.js +12 -0
- package/lib/commonjs/utils/index.js.map +1 -0
- package/lib/commonjs/utils/types.js +2 -0
- package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
- package/lib/module/index.js +38 -38
- package/lib/module/index.js.map +1 -1
- package/lib/module/random.js +26 -12
- package/lib/module/random.js.map +1 -1
- package/lib/module/specs/random.nitro.js +4 -0
- package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
- package/lib/module/utils/conversion.js +16 -0
- package/lib/module/utils/conversion.js.map +1 -0
- package/lib/module/utils/index.js +8 -0
- package/lib/module/utils/index.js.map +1 -0
- package/lib/module/utils/types.js +2 -0
- package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/typescript/index.d.ts +10 -141
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/random.d.ts +4 -5
- package/lib/typescript/random.d.ts.map +1 -1
- package/lib/typescript/specs/random.nitro.d.ts +9 -0
- package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
- package/lib/typescript/utils/conversion.d.ts +4 -0
- package/lib/typescript/utils/conversion.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +6 -0
- package/lib/typescript/utils/index.d.ts.map +1 -0
- package/lib/typescript/utils/types.d.ts +5 -0
- package/lib/typescript/utils/types.d.ts.map +1 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +45 -0
- package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +26 -0
- package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +57 -0
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +9 -0
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +25 -0
- package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +29 -0
- package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +62 -0
- package/package.json +50 -106
- package/src/index.ts +38 -48
- package/src/random.ts +51 -48
- package/src/specs/random.nitro.ts +14 -0
- package/src/utils/conversion.ts +16 -0
- package/src/utils/index.ts +6 -0
- package/src/utils/types.ts +15 -0
- package/LICENSE +0 -27
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
- package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
- package/cpp/Cipher/MGLCipherHostObject.h +0 -90
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
- package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
- package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
- package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
- package/cpp/Cipher/MGLPublicCipher.h +0 -124
- package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
- package/cpp/Cipher/MGLRsa.cpp +0 -573
- package/cpp/Cipher/MGLRsa.h +0 -109
- package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
- package/cpp/HMAC/MGLHmacHostObject.h +0 -39
- package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
- package/cpp/HMAC/MGLHmacInstaller.h +0 -20
- package/cpp/Hash/MGLHashHostObject.cpp +0 -147
- package/cpp/Hash/MGLHashHostObject.h +0 -53
- package/cpp/Hash/MGLHashInstaller.cpp +0 -44
- package/cpp/Hash/MGLHashInstaller.h +0 -20
- package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
- package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
- package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
- package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
- package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
- package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
- package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
- package/cpp/JSIUtils/MGLTypedArray.h +0 -160
- package/cpp/MGLKeys.cpp +0 -1426
- package/cpp/MGLKeys.h +0 -203
- package/cpp/MGLQuickCryptoHostObject.cpp +0 -127
- package/cpp/MGLQuickCryptoHostObject.h +0 -30
- package/cpp/Random/MGLRandomHostObject.cpp +0 -96
- package/cpp/Random/MGLRandomHostObject.h +0 -27
- package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
- package/cpp/Sig/MGLSignHostObjects.h +0 -128
- package/cpp/Sig/MGLSignInstaller.cpp +0 -24
- package/cpp/Sig/MGLSignInstaller.h +0 -29
- package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
- package/cpp/Sig/MGLVerifyInstaller.h +0 -22
- package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
- package/cpp/Utils/MGLDispatchQueue.h +0 -55
- package/cpp/Utils/MGLUtils.cpp +0 -295
- package/cpp/Utils/MGLUtils.h +0 -358
- package/cpp/Utils/base64.h +0 -320
- package/cpp/Utils/logs.h +0 -38
- package/cpp/Utils/node.h +0 -13
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
- package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
- package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
- package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
- package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
- package/cpp/webcrypto/MGLWebCrypto.h +0 -32
- package/cpp/webcrypto/crypto_aes.cpp +0 -516
- package/cpp/webcrypto/crypto_aes.h +0 -79
- package/cpp/webcrypto/crypto_ec.cpp +0 -424
- package/cpp/webcrypto/crypto_ec.h +0 -78
- package/cpp/webcrypto/crypto_keygen.cpp +0 -86
- package/cpp/webcrypto/crypto_keygen.h +0 -38
- package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
- package/ios/QuickCryptoModule.h +0 -5
- package/ios/QuickCryptoModule.mm +0 -42
- package/lib/commonjs/Cipher.js +0 -502
- package/lib/commonjs/Cipher.js.map +0 -1
- package/lib/commonjs/Hash.js +0 -99
- package/lib/commonjs/Hash.js.map +0 -1
- package/lib/commonjs/Hashnames.js +0 -87
- package/lib/commonjs/Hashnames.js.map +0 -1
- package/lib/commonjs/Hmac.js +0 -79
- package/lib/commonjs/Hmac.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
- package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
- package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
- package/lib/commonjs/Utils.js +0 -608
- package/lib/commonjs/Utils.js.map +0 -1
- package/lib/commonjs/aes.js +0 -281
- package/lib/commonjs/aes.js.map +0 -1
- package/lib/commonjs/constants.js +0 -85
- package/lib/commonjs/constants.js.map +0 -1
- package/lib/commonjs/ec.js +0 -276
- package/lib/commonjs/ec.js.map +0 -1
- package/lib/commonjs/keygen.js +0 -56
- package/lib/commonjs/keygen.js.map +0 -1
- package/lib/commonjs/keys.js +0 -492
- package/lib/commonjs/keys.js.map +0 -1
- package/lib/commonjs/pbkdf2.js +0 -90
- package/lib/commonjs/pbkdf2.js.map +0 -1
- package/lib/commonjs/rsa.js +0 -248
- package/lib/commonjs/rsa.js.map +0 -1
- package/lib/commonjs/sig.js +0 -129
- package/lib/commonjs/sig.js.map +0 -1
- package/lib/commonjs/subtle.js +0 -448
- package/lib/commonjs/subtle.js.map +0 -1
- package/lib/commonjs/webcrypto.js +0 -14
- package/lib/commonjs/webcrypto.js.map +0 -1
- package/lib/module/Cipher.js +0 -491
- package/lib/module/Cipher.js.map +0 -1
- package/lib/module/Hash.js +0 -93
- package/lib/module/Hash.js.map +0 -1
- package/lib/module/Hashnames.js +0 -85
- package/lib/module/Hashnames.js.map +0 -1
- package/lib/module/Hmac.js +0 -74
- package/lib/module/Hmac.js.map +0 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +0 -26
- package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -49
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
- package/lib/module/NativeQuickCrypto/aes.js +0 -4
- package/lib/module/NativeQuickCrypto/hash.js +0 -2
- package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
- package/lib/module/NativeQuickCrypto/hmac.js +0 -2
- package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
- package/lib/module/NativeQuickCrypto/keygen.js +0 -4
- package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
- package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
- package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
- package/lib/module/NativeQuickCrypto/random.js +0 -2
- package/lib/module/NativeQuickCrypto/random.js.map +0 -1
- package/lib/module/NativeQuickCrypto/rsa.js +0 -4
- package/lib/module/NativeQuickCrypto/sig.js +0 -17
- package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
- package/lib/module/NativeQuickCrypto/webcrypto.js +0 -4
- package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
- package/lib/module/Utils.js +0 -539
- package/lib/module/Utils.js.map +0 -1
- package/lib/module/aes.js +0 -274
- package/lib/module/aes.js.map +0 -1
- package/lib/module/constants.js +0 -81
- package/lib/module/constants.js.map +0 -1
- package/lib/module/ec.js +0 -267
- package/lib/module/ec.js.map +0 -1
- package/lib/module/keygen.js +0 -49
- package/lib/module/keygen.js.map +0 -1
- package/lib/module/keys.js +0 -477
- package/lib/module/keys.js.map +0 -1
- package/lib/module/pbkdf2.js +0 -84
- package/lib/module/pbkdf2.js.map +0 -1
- package/lib/module/rsa.js +0 -242
- package/lib/module/rsa.js.map +0 -1
- package/lib/module/sig.js +0 -124
- package/lib/module/sig.js.map +0 -1
- package/lib/module/subtle.js +0 -443
- package/lib/module/subtle.js.map +0 -1
- package/lib/module/webcrypto.js +0 -10
- package/lib/module/webcrypto.js.map +0 -1
- package/lib/typescript/Cipher.d.ts +0 -81
- package/lib/typescript/Cipher.d.ts.map +0 -1
- package/lib/typescript/Hash.d.ts +0 -44
- package/lib/typescript/Hash.d.ts.map +0 -1
- package/lib/typescript/Hashnames.d.ts +0 -11
- package/lib/typescript/Hashnames.d.ts.map +0 -1
- package/lib/typescript/Hmac.d.ts +0 -37
- package/lib/typescript/Hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
- package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
- package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
- package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
- package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
- package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
- package/lib/typescript/Utils.d.ts +0 -48
- package/lib/typescript/Utils.d.ts.map +0 -1
- package/lib/typescript/aes.d.ts +0 -22
- package/lib/typescript/aes.d.ts.map +0 -1
- package/lib/typescript/constants.d.ts +0 -76
- package/lib/typescript/constants.d.ts.map +0 -1
- package/lib/typescript/ec.d.ts +0 -7
- package/lib/typescript/ec.d.ts.map +0 -1
- package/lib/typescript/keygen.d.ts +0 -6
- package/lib/typescript/keygen.d.ts.map +0 -1
- package/lib/typescript/keys.d.ts +0 -206
- package/lib/typescript/keys.d.ts.map +0 -1
- package/lib/typescript/pbkdf2.d.ts +0 -12
- package/lib/typescript/pbkdf2.d.ts.map +0 -1
- package/lib/typescript/rsa.d.ts +0 -12
- package/lib/typescript/rsa.d.ts.map +0 -1
- package/lib/typescript/sig.d.ts +0 -21
- package/lib/typescript/sig.d.ts.map +0 -1
- package/lib/typescript/subtle.d.ts +0 -15
- package/lib/typescript/subtle.d.ts.map +0 -1
- package/lib/typescript/webcrypto.d.ts +0 -9
- package/lib/typescript/webcrypto.d.ts.map +0 -1
- package/react-native-quick-crypto.podspec +0 -40
- package/src/Cipher.ts +0 -832
- package/src/Hash.ts +0 -132
- package/src/Hashnames.ts +0 -93
- package/src/Hmac.ts +0 -106
- package/src/NativeQuickCrypto/Cipher.ts +0 -102
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
- package/src/NativeQuickCrypto/aes.ts +0 -14
- package/src/NativeQuickCrypto/hash.ts +0 -10
- package/src/NativeQuickCrypto/hmac.ts +0 -9
- package/src/NativeQuickCrypto/keygen.ts +0 -7
- package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
- package/src/NativeQuickCrypto/random.ts +0 -12
- package/src/NativeQuickCrypto/rsa.ts +0 -12
- package/src/NativeQuickCrypto/sig.ts +0 -44
- package/src/NativeQuickCrypto/webcrypto.ts +0 -72
- package/src/Utils.ts +0 -777
- package/src/aes.ts +0 -402
- package/src/constants.ts +0 -79
- package/src/ec.ts +0 -375
- package/src/keygen.ts +0 -80
- package/src/keys.ts +0 -787
- package/src/pbkdf2.ts +0 -169
- package/src/rsa.ts +0 -370
- package/src/sig.ts +0 -164
- package/src/subtle.ts +0 -639
- package/src/webcrypto.ts +0 -8
- /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
#ifndef crypto_aes_h
|
|
2
|
-
#define crypto_aes_h
|
|
3
|
-
|
|
4
|
-
#include <jsi/jsi.h>
|
|
5
|
-
|
|
6
|
-
#include "MGLKeys.h"
|
|
7
|
-
#ifdef ANDROID
|
|
8
|
-
#include "Utils/MGLUtils.h"
|
|
9
|
-
#else
|
|
10
|
-
#include "MGLUtils.h"
|
|
11
|
-
#endif
|
|
12
|
-
|
|
13
|
-
namespace margelo {
|
|
14
|
-
|
|
15
|
-
namespace jsi = facebook::jsi;
|
|
16
|
-
|
|
17
|
-
constexpr size_t kAesBlockSize = 16;
|
|
18
|
-
constexpr unsigned kNoAuthTagLength = static_cast<unsigned>(-1);
|
|
19
|
-
constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
|
|
20
|
-
|
|
21
|
-
#define VARIANTS(V) \
|
|
22
|
-
V(CTR_128, AES_CTR_Cipher) \
|
|
23
|
-
V(CTR_192, AES_CTR_Cipher) \
|
|
24
|
-
V(CTR_256, AES_CTR_Cipher) \
|
|
25
|
-
V(CBC_128, AES_Cipher) \
|
|
26
|
-
V(CBC_192, AES_Cipher) \
|
|
27
|
-
V(CBC_256, AES_Cipher) \
|
|
28
|
-
V(GCM_128, AES_Cipher) \
|
|
29
|
-
V(GCM_192, AES_Cipher) \
|
|
30
|
-
V(GCM_256, AES_Cipher) \
|
|
31
|
-
V(KW_128, AES_Cipher) \
|
|
32
|
-
V(KW_192, AES_Cipher) \
|
|
33
|
-
V(KW_256, AES_Cipher)
|
|
34
|
-
|
|
35
|
-
enum AESKeyVariant {
|
|
36
|
-
#define V(name, _) kKeyVariantAES_ ## name,
|
|
37
|
-
VARIANTS(V)
|
|
38
|
-
#undef V
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
struct AESCipherConfig final {
|
|
42
|
-
enum Mode {
|
|
43
|
-
kEncrypt,
|
|
44
|
-
kDecrypt,
|
|
45
|
-
// kWrapKey,
|
|
46
|
-
// kUnwrapKey,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
Mode mode;
|
|
50
|
-
AESKeyVariant variant;
|
|
51
|
-
std::shared_ptr<KeyObjectData> key;
|
|
52
|
-
ByteSource data;
|
|
53
|
-
const EVP_CIPHER* cipher;
|
|
54
|
-
ByteSource iv; // Used for both iv or counter
|
|
55
|
-
size_t length;
|
|
56
|
-
ByteSource tag; // Used only for authenticated modes (GCM)
|
|
57
|
-
ByteSource additional_data;
|
|
58
|
-
|
|
59
|
-
AESCipherConfig() = default;
|
|
60
|
-
|
|
61
|
-
// AESCipherConfig(AESCipherConfig&& other) noexcept;
|
|
62
|
-
|
|
63
|
-
// AESCipherConfig& operator=(AESCipherConfig&& other) noexcept;
|
|
64
|
-
|
|
65
|
-
// void MemoryInfo(MemoryTracker* tracker) const override;
|
|
66
|
-
// SET_MEMORY_INFO_NAME(AESCipherConfig)
|
|
67
|
-
// SET_SELF_SIZE(AESCipherConfig)
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
class AESCipher {
|
|
71
|
-
public:
|
|
72
|
-
AESCipher() {}
|
|
73
|
-
AESCipherConfig GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args);
|
|
74
|
-
WebCryptoCipherStatus DoCipher(const AESCipherConfig ¶ms, ByteSource *out);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
} // namespace margelo
|
|
78
|
-
|
|
79
|
-
#endif // crypto_aes_h
|
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// crypto_ec.cpp
|
|
3
|
-
// BEMCheckBox
|
|
4
|
-
//
|
|
5
|
-
// Created by Oscar Franco on 30/11/23.
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
#include "crypto_ec.h"
|
|
9
|
-
#include <iostream>
|
|
10
|
-
#include <openssl/ec.h>
|
|
11
|
-
#include <string>
|
|
12
|
-
#include <utility>
|
|
13
|
-
|
|
14
|
-
namespace margelo {
|
|
15
|
-
namespace jsi = facebook::jsi;
|
|
16
|
-
|
|
17
|
-
int GetCurveFromName(const char* name) {
|
|
18
|
-
int nid = EC_curve_nist2nid(name);
|
|
19
|
-
if (nid == NID_undef)
|
|
20
|
-
nid = OBJ_sn2nid(name);
|
|
21
|
-
return nid;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
ECPointPointer ECDH::BufferToPoint(jsi::Runtime &rt,
|
|
25
|
-
const EC_GROUP* group,
|
|
26
|
-
jsi::ArrayBuffer &buf) {
|
|
27
|
-
int r;
|
|
28
|
-
|
|
29
|
-
ECPointPointer pub(EC_POINT_new(group));
|
|
30
|
-
if (!pub) {
|
|
31
|
-
throw std::runtime_error(
|
|
32
|
-
"Failed to allocate EC_POINT for a public key");
|
|
33
|
-
return pub;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// TODO(osp) re-insert this check
|
|
37
|
-
// if (UNLIKELY(!input.CheckSizeInt32())) {
|
|
38
|
-
// THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
|
|
39
|
-
// return ECPointPointer();
|
|
40
|
-
// }
|
|
41
|
-
r = EC_POINT_oct2point(
|
|
42
|
-
group,
|
|
43
|
-
pub.get(),
|
|
44
|
-
buf.data(rt),
|
|
45
|
-
buf.size(rt),
|
|
46
|
-
nullptr);
|
|
47
|
-
|
|
48
|
-
if (!r) {
|
|
49
|
-
return ECPointPointer();
|
|
50
|
-
}
|
|
51
|
-
return pub;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
WebCryptoKeyExportStatus ECDH::doExport(jsi::Runtime &rt,
|
|
55
|
-
std::shared_ptr<KeyObjectData> key_data,
|
|
56
|
-
WebCryptoKeyFormat format,
|
|
57
|
-
const ECKeyExportConfig& params,
|
|
58
|
-
ByteSource* out) {
|
|
59
|
-
CHECK_NE(key_data->GetKeyType(), kKeyTypeSecret);
|
|
60
|
-
|
|
61
|
-
switch (format) {
|
|
62
|
-
case kWebCryptoKeyFormatRaw:
|
|
63
|
-
return EC_Raw_Export(key_data.get(), params, out);
|
|
64
|
-
case kWebCryptoKeyFormatPKCS8:
|
|
65
|
-
if (key_data->GetKeyType() != kKeyTypePrivate)
|
|
66
|
-
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
|
|
67
|
-
return PKEY_PKCS8_Export(key_data.get(), out);
|
|
68
|
-
case kWebCryptoKeyFormatSPKI: {
|
|
69
|
-
if (key_data->GetKeyType() != kKeyTypePublic)
|
|
70
|
-
throw std::runtime_error("Invalid type public to be exported");
|
|
71
|
-
|
|
72
|
-
ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey();
|
|
73
|
-
if (EVP_PKEY_id(m_pkey.get()) != EVP_PKEY_EC) {
|
|
74
|
-
return PKEY_SPKI_Export(key_data.get(), out);
|
|
75
|
-
} else {
|
|
76
|
-
// Ensure exported key is in uncompressed point format.
|
|
77
|
-
// The temporary EC key is so we can have i2d_PUBKEY_bio() write out
|
|
78
|
-
// the header but it is a somewhat silly hoop to jump through because
|
|
79
|
-
// the header is for all practical purposes a static 26 byte sequence
|
|
80
|
-
// where only the second byte changes.
|
|
81
|
-
|
|
82
|
-
const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(m_pkey.get());
|
|
83
|
-
const EC_GROUP* group = EC_KEY_get0_group(ec_key);
|
|
84
|
-
const EC_POINT* point = EC_KEY_get0_public_key(ec_key);
|
|
85
|
-
const point_conversion_form_t form =
|
|
86
|
-
POINT_CONVERSION_UNCOMPRESSED;
|
|
87
|
-
const size_t need =
|
|
88
|
-
EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
|
|
89
|
-
if (need == 0) {
|
|
90
|
-
throw std::runtime_error("Failed to export EC key");
|
|
91
|
-
}
|
|
92
|
-
ByteSource::Builder data(need);
|
|
93
|
-
const size_t have = EC_POINT_point2oct(group,
|
|
94
|
-
point, form, data.data<unsigned char>(), need, nullptr);
|
|
95
|
-
if (have == 0) {
|
|
96
|
-
throw std::runtime_error("Failed to export EC key");
|
|
97
|
-
}
|
|
98
|
-
ECKeyPointer ec(EC_KEY_new());
|
|
99
|
-
CHECK_EQ(1, EC_KEY_set_group(ec.get(), group));
|
|
100
|
-
ECPointPointer uncompressed(EC_POINT_new(group));
|
|
101
|
-
CHECK_EQ(1,
|
|
102
|
-
EC_POINT_oct2point(group,
|
|
103
|
-
uncompressed.get(),
|
|
104
|
-
data.data<unsigned char>(),
|
|
105
|
-
data.size(),
|
|
106
|
-
nullptr));
|
|
107
|
-
CHECK_EQ(1, EC_KEY_set_public_key(ec.get(),
|
|
108
|
-
uncompressed.get()));
|
|
109
|
-
EVPKeyPointer pkey(EVP_PKEY_new());
|
|
110
|
-
CHECK_EQ(1, EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get()));
|
|
111
|
-
BIOPointer bio(BIO_new(BIO_s_mem()));
|
|
112
|
-
CHECK(bio);
|
|
113
|
-
if (!i2d_PUBKEY_bio(bio.get(), pkey.get())) {
|
|
114
|
-
throw std::runtime_error("Failed to export EC key");
|
|
115
|
-
}
|
|
116
|
-
*out = ByteSource::FromBIO(bio);
|
|
117
|
-
return WebCryptoKeyExportStatus::OK;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
default:
|
|
121
|
-
throw std::runtime_error("Un-reachable export code");
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
WebCryptoKeyExportStatus EC_Raw_Export(KeyObjectData* key_data,
|
|
126
|
-
const ECKeyExportConfig& params,
|
|
127
|
-
ByteSource* out) {
|
|
128
|
-
ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey();
|
|
129
|
-
CHECK(m_pkey);
|
|
130
|
-
// std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
|
|
131
|
-
|
|
132
|
-
const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(m_pkey.get());
|
|
133
|
-
|
|
134
|
-
size_t len = 0;
|
|
135
|
-
|
|
136
|
-
if (ec_key == nullptr) {
|
|
137
|
-
typedef int (*export_fn)(const EVP_PKEY*, unsigned char*, size_t* len);
|
|
138
|
-
export_fn fn = nullptr;
|
|
139
|
-
switch (key_data->GetKeyType()) {
|
|
140
|
-
case kKeyTypePrivate:
|
|
141
|
-
fn = EVP_PKEY_get_raw_private_key;
|
|
142
|
-
break;
|
|
143
|
-
case kKeyTypePublic:
|
|
144
|
-
fn = EVP_PKEY_get_raw_public_key;
|
|
145
|
-
break;
|
|
146
|
-
case kKeyTypeSecret:
|
|
147
|
-
throw std::runtime_error("unreachable code in EC_Raw_Export");
|
|
148
|
-
}
|
|
149
|
-
CHECK_NOT_NULL(fn);
|
|
150
|
-
// Get the size of the raw key data
|
|
151
|
-
if (fn(m_pkey.get(), nullptr, &len) == 0)
|
|
152
|
-
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
|
|
153
|
-
ByteSource::Builder data(len);
|
|
154
|
-
if (fn(m_pkey.get(), data.data<unsigned char>(), &len) == 0)
|
|
155
|
-
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
|
|
156
|
-
*out = std::move(data).release(len);
|
|
157
|
-
} else {
|
|
158
|
-
if (key_data->GetKeyType() != kKeyTypePublic)
|
|
159
|
-
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
|
|
160
|
-
const EC_GROUP* group = EC_KEY_get0_group(ec_key);
|
|
161
|
-
const EC_POINT* point = EC_KEY_get0_public_key(ec_key);
|
|
162
|
-
point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
|
|
163
|
-
|
|
164
|
-
// Get the allocated data size...
|
|
165
|
-
len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
|
|
166
|
-
if (len == 0)
|
|
167
|
-
return WebCryptoKeyExportStatus::FAILED;
|
|
168
|
-
ByteSource::Builder data(len);
|
|
169
|
-
size_t check_len = EC_POINT_point2oct(
|
|
170
|
-
group, point, form, data.data<unsigned char>(), len, nullptr);
|
|
171
|
-
if (check_len == 0)
|
|
172
|
-
return WebCryptoKeyExportStatus::FAILED;
|
|
173
|
-
|
|
174
|
-
CHECK_EQ(len, check_len);
|
|
175
|
-
*out = std::move(data).release();
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return WebCryptoKeyExportStatus::OK;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
jsi::Value ExportJWKEcKey(jsi::Runtime &rt,
|
|
182
|
-
std::shared_ptr<KeyObjectData> key,
|
|
183
|
-
jsi::Object &target) {
|
|
184
|
-
ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
|
|
185
|
-
// std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
|
|
186
|
-
CHECK_EQ(EVP_PKEY_id(m_pkey.get()), EVP_PKEY_EC);
|
|
187
|
-
|
|
188
|
-
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(m_pkey.get());
|
|
189
|
-
CHECK_NOT_NULL(ec);
|
|
190
|
-
|
|
191
|
-
const EC_POINT* pub = EC_KEY_get0_public_key(ec);
|
|
192
|
-
const EC_GROUP* group = EC_KEY_get0_group(ec);
|
|
193
|
-
|
|
194
|
-
int degree_bits = EC_GROUP_get_degree(group);
|
|
195
|
-
int degree_bytes =
|
|
196
|
-
(degree_bits / CHAR_BIT) + (7 + (degree_bits % CHAR_BIT)) / 8;
|
|
197
|
-
|
|
198
|
-
BignumPointer x(BN_new());
|
|
199
|
-
BignumPointer y(BN_new());
|
|
200
|
-
|
|
201
|
-
if (!EC_POINT_get_affine_coordinates(group, pub, x.get(), y.get(), nullptr)) {
|
|
202
|
-
throw jsi::JSError(rt, "Failed to get elliptic-curve point coordinates");
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
target.setProperty(rt, "kty", "EC");
|
|
206
|
-
target.setProperty(rt, "x", EncodeBignum(x.get(), degree_bytes, true));
|
|
207
|
-
target.setProperty(rt, "y", EncodeBignum(y.get(), degree_bytes, true));
|
|
208
|
-
|
|
209
|
-
std::string crv_name;
|
|
210
|
-
const int nid = EC_GROUP_get_curve_name(group);
|
|
211
|
-
switch (nid) {
|
|
212
|
-
case NID_X9_62_prime256v1:
|
|
213
|
-
crv_name = "P-256";
|
|
214
|
-
break;
|
|
215
|
-
case NID_secp256k1:
|
|
216
|
-
crv_name = "secp256k1";
|
|
217
|
-
break;
|
|
218
|
-
case NID_secp384r1:
|
|
219
|
-
crv_name = "P-384";
|
|
220
|
-
break;
|
|
221
|
-
case NID_secp521r1:
|
|
222
|
-
crv_name = "P-521";
|
|
223
|
-
break;
|
|
224
|
-
default: {
|
|
225
|
-
throw jsi::JSError(rt, "Unsupported JWK EC curve: %s.", OBJ_nid2sn(nid));
|
|
226
|
-
return jsi::Value::undefined();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
target.setProperty(rt, "crv", crv_name);
|
|
230
|
-
|
|
231
|
-
if (key->GetKeyType() == kKeyTypePrivate) {
|
|
232
|
-
const BIGNUM* pvt = EC_KEY_get0_private_key(ec);
|
|
233
|
-
target.setProperty(rt, "d", EncodeBignum(pvt, degree_bytes, true));
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return std::move(target);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
std::shared_ptr<KeyObjectData> ImportJWKEcKey(jsi::Runtime &rt,
|
|
240
|
-
jsi::Object &jwk,
|
|
241
|
-
jsi::Value &namedCurve) {
|
|
242
|
-
// curve name
|
|
243
|
-
if (namedCurve.isUndefined()) {
|
|
244
|
-
throw jsi::JSError(rt, "Invalid Named Curve");
|
|
245
|
-
return std::shared_ptr<KeyObjectData>();
|
|
246
|
-
}
|
|
247
|
-
std::string curve = namedCurve.asString(rt).utf8(rt);
|
|
248
|
-
|
|
249
|
-
int nid = GetCurveFromName(curve.c_str());
|
|
250
|
-
if (nid == NID_undef) { // Unknown curve
|
|
251
|
-
throw jsi::JSError(rt, "Invalid Named Curve: " + curve);
|
|
252
|
-
return std::shared_ptr<KeyObjectData>();
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
jsi::Value x_value = jwk.getProperty(rt, "x");
|
|
256
|
-
jsi::Value y_value = jwk.getProperty(rt, "y");
|
|
257
|
-
jsi::Value d_value = jwk.getProperty(rt, "d");
|
|
258
|
-
|
|
259
|
-
if (!x_value.isString() ||
|
|
260
|
-
!y_value.isString() ||
|
|
261
|
-
(!d_value.isUndefined() && !d_value.isString())) {
|
|
262
|
-
throw jsi::JSError(rt, "Invalid JWK EC key 0");
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
KeyType type = d_value.isString() ? kKeyTypePrivate : kKeyTypePublic;
|
|
266
|
-
|
|
267
|
-
ECKeyPointer ec(EC_KEY_new_by_curve_name(nid));
|
|
268
|
-
if (!ec) {
|
|
269
|
-
throw jsi::JSError(rt, "Invalid JWK EC key 1");
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
ByteSource x = ByteSource::FromEncodedString(rt,
|
|
273
|
-
x_value.asString(rt).utf8(rt),
|
|
274
|
-
encoding::BASE64URL);
|
|
275
|
-
ByteSource y = ByteSource::FromEncodedString(rt,
|
|
276
|
-
y_value.asString(rt).utf8(rt),
|
|
277
|
-
encoding::BASE64URL);
|
|
278
|
-
|
|
279
|
-
int r = EC_KEY_set_public_key_affine_coordinates(ec.get(),
|
|
280
|
-
x.ToBN().get(),
|
|
281
|
-
y.ToBN().get());
|
|
282
|
-
if (!r) {
|
|
283
|
-
throw jsi::JSError(rt, "Invalid JWK EC key 2");
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (type == kKeyTypePrivate) {
|
|
287
|
-
ByteSource d = ByteSource::FromEncodedString(rt, d_value.asString(rt).utf8(rt));
|
|
288
|
-
if (!EC_KEY_set_private_key(ec.get(), d.ToBN().get())) {
|
|
289
|
-
throw jsi::JSError(rt, "Invalid JWK EC key 3");
|
|
290
|
-
return std::shared_ptr<KeyObjectData>();
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
EVPKeyPointer pkey(EVP_PKEY_new());
|
|
295
|
-
CHECK_EQ(EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get()), 1);
|
|
296
|
-
|
|
297
|
-
return KeyObjectData::CreateAsymmetric(type, ManagedEVPPKey(std::move(pkey)));
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
jsi::Value GetEcKeyDetail(jsi::Runtime &rt,
|
|
301
|
-
std::shared_ptr<KeyObjectData> key) {
|
|
302
|
-
jsi::Object target = jsi::Object(rt);
|
|
303
|
-
ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
|
|
304
|
-
// std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
|
|
305
|
-
CHECK_EQ(EVP_PKEY_id(m_pkey.get()), EVP_PKEY_EC);
|
|
306
|
-
|
|
307
|
-
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(m_pkey.get());
|
|
308
|
-
CHECK_NOT_NULL(ec);
|
|
309
|
-
|
|
310
|
-
const EC_GROUP* group = EC_KEY_get0_group(ec);
|
|
311
|
-
int nid = EC_GROUP_get_curve_name(group);
|
|
312
|
-
|
|
313
|
-
jsi::String value = jsi::String::createFromUtf8(rt, OBJ_nid2sn(nid));
|
|
314
|
-
target.setProperty(rt, "namedCurve", value);
|
|
315
|
-
return target;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
EcKeyPairGenConfig prepareEcKeyGenConfig(jsi::Runtime &rt,
|
|
319
|
-
const jsi::Value *args)
|
|
320
|
-
{
|
|
321
|
-
EcKeyPairGenConfig config = EcKeyPairGenConfig();
|
|
322
|
-
|
|
323
|
-
// curve name
|
|
324
|
-
std::string curveName = args[1].asString(rt).utf8(rt);
|
|
325
|
-
config.curve_nid = GetCurveFromName(curveName.c_str());
|
|
326
|
-
|
|
327
|
-
// encoding
|
|
328
|
-
if (CheckIsInt32(args[2].asNumber())) {
|
|
329
|
-
int encoding = static_cast<int>(args[2].asNumber());
|
|
330
|
-
if (encoding != OPENSSL_EC_NAMED_CURVE &&
|
|
331
|
-
encoding != OPENSSL_EC_EXPLICIT_CURVE) {
|
|
332
|
-
throw jsi::JSError(rt, "Invalid param_encoding specified");
|
|
333
|
-
} else {
|
|
334
|
-
config.param_encoding = encoding;
|
|
335
|
-
}
|
|
336
|
-
} else {
|
|
337
|
-
throw jsi::JSError(rt, "Invalid param_encoding specified (not int)");
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// rest of args for encoding
|
|
341
|
-
unsigned int offset = 3;
|
|
342
|
-
|
|
343
|
-
config.public_key_encoding = ManagedEVPPKey::GetPublicKeyEncodingFromJs(
|
|
344
|
-
rt, args, &offset, kKeyContextGenerate);
|
|
345
|
-
|
|
346
|
-
auto private_key_encoding = ManagedEVPPKey::GetPrivateKeyEncodingFromJs(
|
|
347
|
-
rt, args, &offset, kKeyContextGenerate);
|
|
348
|
-
|
|
349
|
-
if (!private_key_encoding.IsEmpty()) {
|
|
350
|
-
config.private_key_encoding = private_key_encoding.Release();
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return config;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
EVPKeyCtxPointer setup(std::shared_ptr<EcKeyPairGenConfig> config) {
|
|
357
|
-
EVPKeyCtxPointer key_ctx;
|
|
358
|
-
switch (config->curve_nid) {
|
|
359
|
-
case EVP_PKEY_ED25519:
|
|
360
|
-
// Fall through
|
|
361
|
-
case EVP_PKEY_ED448:
|
|
362
|
-
// Fall through
|
|
363
|
-
case EVP_PKEY_X25519:
|
|
364
|
-
// Fall through
|
|
365
|
-
case EVP_PKEY_X448:
|
|
366
|
-
key_ctx.reset(EVP_PKEY_CTX_new_id(config->curve_nid, nullptr));
|
|
367
|
-
break;
|
|
368
|
-
default: {
|
|
369
|
-
EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr));
|
|
370
|
-
EVP_PKEY* raw_params = nullptr;
|
|
371
|
-
if (!param_ctx ||
|
|
372
|
-
EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 ||
|
|
373
|
-
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(
|
|
374
|
-
param_ctx.get(), config->curve_nid) <= 0 ||
|
|
375
|
-
EVP_PKEY_CTX_set_ec_param_enc(
|
|
376
|
-
param_ctx.get(), config->param_encoding) <= 0 ||
|
|
377
|
-
EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) {
|
|
378
|
-
return EVPKeyCtxPointer();
|
|
379
|
-
}
|
|
380
|
-
EVPKeyPointer key_params(raw_params);
|
|
381
|
-
key_ctx.reset(EVP_PKEY_CTX_new(key_params.get(), nullptr));
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
if (key_ctx && EVP_PKEY_keygen_init(key_ctx.get()) <= 0)
|
|
386
|
-
key_ctx.reset();
|
|
387
|
-
|
|
388
|
-
return key_ctx;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
std::pair<jsi::Value, jsi::Value> generateEcKeyPair(jsi::Runtime& runtime,
|
|
392
|
-
std::shared_ptr<EcKeyPairGenConfig> config)
|
|
393
|
-
{
|
|
394
|
-
// TODO: this is all copied from MGLRsa.cpp - template it up like Node?
|
|
395
|
-
|
|
396
|
-
EVPKeyCtxPointer ctx = setup(config);
|
|
397
|
-
|
|
398
|
-
if (!ctx) {
|
|
399
|
-
throw jsi::JSError(runtime, "Error on key generation job");
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
// Generate the key
|
|
403
|
-
EVP_PKEY* pkey = nullptr;
|
|
404
|
-
if (!EVP_PKEY_keygen(ctx.get(), &pkey)) {
|
|
405
|
-
throw jsi::JSError(runtime, "Error generating key");
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
config->key = ManagedEVPPKey(EVPKeyPointer(pkey));
|
|
409
|
-
|
|
410
|
-
jsi::Value publicBuffer =
|
|
411
|
-
ManagedEVPPKey::ToEncodedPublicKey(runtime, std::move(config->key),
|
|
412
|
-
config->public_key_encoding);
|
|
413
|
-
jsi::Value privateBuffer =
|
|
414
|
-
ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
|
|
415
|
-
config->private_key_encoding);
|
|
416
|
-
|
|
417
|
-
if (publicBuffer.isUndefined() || privateBuffer.isUndefined()) {
|
|
418
|
-
throw jsi::JSError(runtime, "Failed to encode public and/or private key (EC)");
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
return {std::move(publicBuffer), std::move(privateBuffer)};
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
} // namespace margelo
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// crypto_ec.hpp
|
|
3
|
-
// BEMCheckBox
|
|
4
|
-
//
|
|
5
|
-
// Created by Oscar Franco on 30/11/23.
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
#ifndef crypto_ec_h
|
|
9
|
-
#define crypto_ec_h
|
|
10
|
-
|
|
11
|
-
#include <jsi/jsi.h>
|
|
12
|
-
#include <openssl/ec.h>
|
|
13
|
-
#include <memory>
|
|
14
|
-
#include "MGLKeys.h"
|
|
15
|
-
#ifdef ANDROID
|
|
16
|
-
#include "Utils/MGLUtils.h"
|
|
17
|
-
#include "JSIUtils/MGLJSIUtils.h"
|
|
18
|
-
#else
|
|
19
|
-
#include "MGLUtils.h"
|
|
20
|
-
#include "MGLJSIUtils.h"
|
|
21
|
-
#endif
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
namespace margelo {
|
|
25
|
-
namespace jsi = facebook::jsi;
|
|
26
|
-
|
|
27
|
-
// There is currently no additional information that the
|
|
28
|
-
// ECKeyExport needs to collect, but we need to provide
|
|
29
|
-
// the base struct anyway.
|
|
30
|
-
struct ECKeyExportConfig final {};
|
|
31
|
-
|
|
32
|
-
class ECDH final {
|
|
33
|
-
public:
|
|
34
|
-
static ECPointPointer BufferToPoint(jsi::Runtime &rt,
|
|
35
|
-
const EC_GROUP* group,
|
|
36
|
-
jsi::ArrayBuffer &buf);
|
|
37
|
-
|
|
38
|
-
static WebCryptoKeyExportStatus doExport(jsi::Runtime &rt,
|
|
39
|
-
std::shared_ptr<KeyObjectData> key_data,
|
|
40
|
-
WebCryptoKeyFormat format,
|
|
41
|
-
const ECKeyExportConfig ¶ms,
|
|
42
|
-
ByteSource* out);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
WebCryptoKeyExportStatus EC_Raw_Export(KeyObjectData* key_data,
|
|
46
|
-
const ECKeyExportConfig ¶ms,
|
|
47
|
-
ByteSource* out);
|
|
48
|
-
|
|
49
|
-
jsi::Value ExportJWKEcKey(jsi::Runtime &rt,
|
|
50
|
-
std::shared_ptr<KeyObjectData> key,
|
|
51
|
-
jsi::Object &target);
|
|
52
|
-
|
|
53
|
-
std::shared_ptr<KeyObjectData> ImportJWKEcKey(jsi::Runtime &rt,
|
|
54
|
-
jsi::Object &jwk,
|
|
55
|
-
jsi::Value &namedCurve);
|
|
56
|
-
|
|
57
|
-
jsi::Value GetEcKeyDetail(jsi::Runtime &rt,
|
|
58
|
-
std::shared_ptr<KeyObjectData> key);
|
|
59
|
-
|
|
60
|
-
struct EcKeyPairGenConfig {
|
|
61
|
-
PublicKeyEncodingConfig public_key_encoding;
|
|
62
|
-
PrivateKeyEncodingConfig private_key_encoding;
|
|
63
|
-
ManagedEVPPKey key;
|
|
64
|
-
|
|
65
|
-
int curve_nid;
|
|
66
|
-
int param_encoding;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
EcKeyPairGenConfig prepareEcKeyGenConfig(jsi::Runtime& runtime,
|
|
70
|
-
const jsi::Value* arguments);
|
|
71
|
-
|
|
72
|
-
std::pair<jsi::Value, jsi::Value> generateEcKeyPair(jsi::Runtime& runtime,
|
|
73
|
-
std::shared_ptr<EcKeyPairGenConfig> config);
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
} // namespace margelo
|
|
77
|
-
|
|
78
|
-
#endif /* crypto_ec_hpp */
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
#include "crypto_keygen.h"
|
|
2
|
-
|
|
3
|
-
#ifdef ANDROID
|
|
4
|
-
#include "JSIUtils/MGLJSIMacros.h"
|
|
5
|
-
#include "JSIUtils/MGLJSIUtils.h"
|
|
6
|
-
#include "Utils/MGLUtils.h"
|
|
7
|
-
#else
|
|
8
|
-
#include "MGLJSIMacros.h"
|
|
9
|
-
#include "MGLJSIUtils.h"
|
|
10
|
-
#include "MGLUtils.h"
|
|
11
|
-
#endif
|
|
12
|
-
|
|
13
|
-
namespace margelo {
|
|
14
|
-
|
|
15
|
-
FieldDefinition GenerateSecretKeyFieldDefinition(
|
|
16
|
-
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
17
|
-
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
|
|
18
|
-
return buildPair(
|
|
19
|
-
"generateSecretKey", JSIF([=]) {
|
|
20
|
-
auto skg = new SecretKeyGen();
|
|
21
|
-
CHECK(skg->GetParamsFromJS(runtime, arguments));
|
|
22
|
-
// make and return a promise
|
|
23
|
-
auto promiseConstructor = runtime.global().getPropertyAsFunction(runtime, "Promise");
|
|
24
|
-
auto promise = promiseConstructor.callAsConstructor(
|
|
25
|
-
runtime,
|
|
26
|
-
jsi::Function::createFromHostFunction(
|
|
27
|
-
runtime,
|
|
28
|
-
jsi::PropNameID::forAscii(runtime, "executor"),
|
|
29
|
-
2,
|
|
30
|
-
[&jsCallInvoker, skg](
|
|
31
|
-
jsi::Runtime &runtime, const jsi::Value &,
|
|
32
|
-
const jsi::Value *promiseArgs, size_t) -> jsi::Value {
|
|
33
|
-
auto resolve = std::make_shared<jsi::Value>(runtime, promiseArgs[0]);
|
|
34
|
-
auto reject = std::make_shared<jsi::Value>(runtime, promiseArgs[1]);
|
|
35
|
-
try {
|
|
36
|
-
jsCallInvoker->invokeAsync([&runtime, resolve, skg]() {
|
|
37
|
-
if (skg->DoKeyGen()) {
|
|
38
|
-
auto res = jsi::Object::createFromHostObject(runtime, skg->GetHandle());
|
|
39
|
-
resolve->asObject(runtime).asFunction(runtime).call(runtime, std::move(res));
|
|
40
|
-
} else {
|
|
41
|
-
throw std::runtime_error("Error generating key");
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
} catch (std::exception e) {
|
|
45
|
-
jsCallInvoker->invokeAsync([&runtime, reject, e]() {
|
|
46
|
-
auto res = jsi::String::createFromUtf8(runtime, e.what());
|
|
47
|
-
reject->asObject(runtime).asFunction(runtime).call(runtime, std::move(res));
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return {};
|
|
51
|
-
}
|
|
52
|
-
)
|
|
53
|
-
);
|
|
54
|
-
return promise;
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
bool SecretKeyGen::GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args) {
|
|
59
|
-
SecretKeyGenConfig params;
|
|
60
|
-
unsigned int offset = 0;
|
|
61
|
-
|
|
62
|
-
// length
|
|
63
|
-
CHECK(CheckIsUint32(args[offset]));
|
|
64
|
-
uint32_t bits = (uint32_t)args[offset].asNumber();
|
|
65
|
-
params.length = bits / CHAR_BIT;
|
|
66
|
-
|
|
67
|
-
this->params_ = std::move(params);
|
|
68
|
-
return true;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
bool SecretKeyGen::DoKeyGen() {
|
|
72
|
-
// TODO: threading / async here, as we don't have jsi::Runtime
|
|
73
|
-
ByteSource::Builder bytes(this->params_.length);
|
|
74
|
-
if (CSPRNG(bytes.data<unsigned char>(), this->params_.length).is_err())
|
|
75
|
-
return false;
|
|
76
|
-
auto key_data = std::move(bytes).release();
|
|
77
|
-
this->key_ = KeyObjectData::CreateSecret(std::move(key_data));
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
std::shared_ptr<KeyObjectHandle> SecretKeyGen::GetHandle() {
|
|
82
|
-
auto handle = KeyObjectHandle::Create(this->key_);
|
|
83
|
-
return handle;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
} // namespace margelo
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
#ifndef crypto_keygen_h
|
|
2
|
-
#define crypto_keygen_h
|
|
3
|
-
|
|
4
|
-
#include <jsi/jsi.h>
|
|
5
|
-
|
|
6
|
-
#include "MGLKeys.h"
|
|
7
|
-
#ifdef ANDROID
|
|
8
|
-
#include "Utils/MGLUtils.h"
|
|
9
|
-
#else
|
|
10
|
-
#include "MGLUtils.h"
|
|
11
|
-
#endif
|
|
12
|
-
|
|
13
|
-
namespace margelo {
|
|
14
|
-
|
|
15
|
-
namespace jsi = facebook::jsi;
|
|
16
|
-
|
|
17
|
-
FieldDefinition GenerateSecretKeyFieldDefinition(
|
|
18
|
-
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
19
|
-
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
20
|
-
|
|
21
|
-
struct SecretKeyGenConfig {
|
|
22
|
-
size_t length; // in bytes
|
|
23
|
-
SecretKeyGenConfig() = default;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
class SecretKeyGen {
|
|
27
|
-
public:
|
|
28
|
-
bool GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args);
|
|
29
|
-
bool DoKeyGen();
|
|
30
|
-
std::shared_ptr<KeyObjectHandle> GetHandle();
|
|
31
|
-
private:
|
|
32
|
-
SecretKeyGenConfig params_;
|
|
33
|
-
std::shared_ptr<KeyObjectData> key_;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
} // namespace margelo
|
|
37
|
-
|
|
38
|
-
#endif // crypto_keygen_h
|