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
|
@@ -40,10 +40,11 @@ void XSalsa20Poly1305Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key,
|
|
|
40
40
|
std::memcpy(nonce_, native_iv->data(), kNonceSize);
|
|
41
41
|
|
|
42
42
|
data_buffer_.clear();
|
|
43
|
-
|
|
43
|
+
is_finalized = false;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::update(const std::shared_ptr<ArrayBuffer>& data) {
|
|
47
|
+
checkNotFinalized();
|
|
47
48
|
#ifndef BLSALLOC_SODIUM
|
|
48
49
|
throw std::runtime_error("XSalsa20Poly1305Cipher: libsodium must be enabled (BLSALLOC_SODIUM)");
|
|
49
50
|
#else
|
|
@@ -59,6 +60,7 @@ std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::update(const std::shared_pt
|
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::final() {
|
|
63
|
+
checkNotFinalized();
|
|
62
64
|
#ifndef BLSALLOC_SODIUM
|
|
63
65
|
throw std::runtime_error("XSalsa20Poly1305Cipher: libsodium must be enabled (BLSALLOC_SODIUM)");
|
|
64
66
|
#else
|
|
@@ -73,12 +75,12 @@ std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::final() {
|
|
|
73
75
|
throw std::runtime_error("XSalsa20Poly1305Cipher: encryption failed");
|
|
74
76
|
}
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
is_finalized = true;
|
|
77
79
|
size_t ct_len = data_buffer_.size();
|
|
78
80
|
return std::make_shared<NativeArrayBuffer>(ciphertext, ct_len, [=]() { delete[] ciphertext; });
|
|
79
81
|
} else {
|
|
80
82
|
if (data_buffer_.empty()) {
|
|
81
|
-
|
|
83
|
+
is_finalized = true;
|
|
82
84
|
return std::make_shared<NativeArrayBuffer>(nullptr, 0, nullptr);
|
|
83
85
|
}
|
|
84
86
|
|
|
@@ -92,7 +94,7 @@ std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::final() {
|
|
|
92
94
|
throw std::runtime_error("XSalsa20Poly1305Cipher: decryption failed - authentication tag mismatch");
|
|
93
95
|
}
|
|
94
96
|
|
|
95
|
-
|
|
97
|
+
is_finalized = true;
|
|
96
98
|
size_t pt_len = data_buffer_.size();
|
|
97
99
|
return std::make_shared<NativeArrayBuffer>(plaintext, pt_len, [=]() { delete[] plaintext; });
|
|
98
100
|
}
|
|
@@ -110,7 +112,7 @@ std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::getAuthTag() {
|
|
|
110
112
|
if (!is_cipher) {
|
|
111
113
|
throw std::runtime_error("getAuthTag can only be called during encryption");
|
|
112
114
|
}
|
|
113
|
-
if (!
|
|
115
|
+
if (!is_finalized) {
|
|
114
116
|
throw std::runtime_error("getAuthTag must be called after final()");
|
|
115
117
|
}
|
|
116
118
|
|
|
@@ -16,7 +16,7 @@ namespace margelo::nitro::crypto {
|
|
|
16
16
|
|
|
17
17
|
class XSalsa20Poly1305Cipher : public HybridCipher {
|
|
18
18
|
public:
|
|
19
|
-
XSalsa20Poly1305Cipher() : HybridObject(TAG)
|
|
19
|
+
XSalsa20Poly1305Cipher() : HybridObject(TAG) {}
|
|
20
20
|
~XSalsa20Poly1305Cipher();
|
|
21
21
|
|
|
22
22
|
void init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) override;
|
|
@@ -36,7 +36,6 @@ class XSalsa20Poly1305Cipher : public HybridCipher {
|
|
|
36
36
|
uint8_t nonce_[kNonceSize];
|
|
37
37
|
std::vector<uint8_t> data_buffer_;
|
|
38
38
|
uint8_t auth_tag_[kTagSize];
|
|
39
|
-
bool final_called_;
|
|
40
39
|
};
|
|
41
40
|
|
|
42
41
|
} // namespace margelo::nitro::crypto
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
#include "HybridDhKeyPair.hpp"
|
|
2
|
+
|
|
3
|
+
#include <NitroModules/ArrayBuffer.hpp>
|
|
4
|
+
#include <NitroModules/Promise.hpp>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <openssl/bio.h>
|
|
7
|
+
#include <openssl/bn.h>
|
|
8
|
+
#include <openssl/buffer.h>
|
|
9
|
+
#include <openssl/dh.h>
|
|
10
|
+
#include <openssl/err.h>
|
|
11
|
+
#include <openssl/evp.h>
|
|
12
|
+
#include <openssl/pem.h>
|
|
13
|
+
#include <stdexcept>
|
|
14
|
+
#include <string>
|
|
15
|
+
|
|
16
|
+
// Suppress deprecation warnings for DH_* functions
|
|
17
|
+
// Node.js ncrypto uses the same pattern — these APIs work but are deprecated in OpenSSL 3.x
|
|
18
|
+
#pragma clang diagnostic push
|
|
19
|
+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
20
|
+
|
|
21
|
+
namespace margelo::nitro::crypto {
|
|
22
|
+
|
|
23
|
+
using BN_ptr = std::unique_ptr<BIGNUM, decltype(&BN_free)>;
|
|
24
|
+
using DH_ptr = std::unique_ptr<DH, decltype(&DH_free)>;
|
|
25
|
+
using EVP_PKEY_CTX_ptr = std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)>;
|
|
26
|
+
|
|
27
|
+
void HybridDhKeyPair::setPrimeLength(double primeLength) {
|
|
28
|
+
primeLength_ = static_cast<int>(primeLength);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
void HybridDhKeyPair::setPrime(const std::shared_ptr<ArrayBuffer>& prime) {
|
|
32
|
+
prime_.assign(prime->data(), prime->data() + prime->size());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void HybridDhKeyPair::setGenerator(double generator) {
|
|
36
|
+
generator_ = static_cast<int>(generator);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
std::shared_ptr<Promise<void>> HybridDhKeyPair::generateKeyPair() {
|
|
40
|
+
return Promise<void>::async([this]() { this->generateKeyPairSync(); });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
void HybridDhKeyPair::generateKeyPairSync() {
|
|
44
|
+
pkey_.reset();
|
|
45
|
+
|
|
46
|
+
EVP_PKEY* params = nullptr;
|
|
47
|
+
|
|
48
|
+
if (!prime_.empty()) {
|
|
49
|
+
// Mode B: Custom prime provided as binary
|
|
50
|
+
DH_ptr dh(DH_new(), DH_free);
|
|
51
|
+
if (!dh) {
|
|
52
|
+
throw std::runtime_error("DH: failed to create DH structure");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
BIGNUM* p = BN_bin2bn(prime_.data(), static_cast<int>(prime_.size()), nullptr);
|
|
56
|
+
BIGNUM* g = BN_new();
|
|
57
|
+
if (!p || !g) {
|
|
58
|
+
if (p)
|
|
59
|
+
BN_free(p);
|
|
60
|
+
if (g)
|
|
61
|
+
BN_free(g);
|
|
62
|
+
throw std::runtime_error("DH: failed to create BIGNUM parameters");
|
|
63
|
+
}
|
|
64
|
+
BN_set_word(g, static_cast<unsigned long>(generator_));
|
|
65
|
+
|
|
66
|
+
if (DH_set0_pqg(dh.get(), p, nullptr, g) != 1) {
|
|
67
|
+
BN_free(p);
|
|
68
|
+
BN_free(g);
|
|
69
|
+
throw std::runtime_error("DH: failed to set DH parameters");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
EVP_PKEY* pkey_params = EVP_PKEY_new();
|
|
73
|
+
if (!pkey_params) {
|
|
74
|
+
throw std::runtime_error("DH: failed to create EVP_PKEY for parameters");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (EVP_PKEY_assign_DH(pkey_params, dh.get()) != 1) {
|
|
78
|
+
EVP_PKEY_free(pkey_params);
|
|
79
|
+
throw std::runtime_error("DH: failed to assign DH to EVP_PKEY");
|
|
80
|
+
}
|
|
81
|
+
dh.release(); // EVP_PKEY now owns it
|
|
82
|
+
|
|
83
|
+
params = pkey_params;
|
|
84
|
+
|
|
85
|
+
} else if (primeLength_ > 0) {
|
|
86
|
+
// Mode C: Generate random prime of given size
|
|
87
|
+
EVP_PKEY_CTX_ptr pctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DH, nullptr), EVP_PKEY_CTX_free);
|
|
88
|
+
if (!pctx) {
|
|
89
|
+
throw std::runtime_error("DH: failed to create parameter context");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (EVP_PKEY_paramgen_init(pctx.get()) <= 0) {
|
|
93
|
+
throw std::runtime_error("DH: failed to initialize parameter generation");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (EVP_PKEY_CTX_set_dh_paramgen_prime_len(pctx.get(), primeLength_) <= 0) {
|
|
97
|
+
throw std::runtime_error("DH: failed to set prime length");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (EVP_PKEY_CTX_set_dh_paramgen_generator(pctx.get(), generator_) <= 0) {
|
|
101
|
+
throw std::runtime_error("DH: failed to set generator");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (EVP_PKEY_paramgen(pctx.get(), ¶ms) <= 0) {
|
|
105
|
+
throw std::runtime_error("DH: failed to generate parameters");
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
throw std::runtime_error("DH: either prime or primeLength must be set");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> params_guard(params, EVP_PKEY_free);
|
|
112
|
+
|
|
113
|
+
// Generate key pair from parameters
|
|
114
|
+
EVP_PKEY_CTX_ptr kctx(EVP_PKEY_CTX_new(params, nullptr), EVP_PKEY_CTX_free);
|
|
115
|
+
if (!kctx) {
|
|
116
|
+
throw std::runtime_error("DH: failed to create keygen context");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (EVP_PKEY_keygen_init(kctx.get()) <= 0) {
|
|
120
|
+
throw std::runtime_error("DH: failed to initialize key generation");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
EVP_PKEY* raw_pkey = nullptr;
|
|
124
|
+
if (EVP_PKEY_keygen(kctx.get(), &raw_pkey) <= 0) {
|
|
125
|
+
throw std::runtime_error("DH: failed to generate key pair");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
pkey_.reset(raw_pkey);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
std::shared_ptr<ArrayBuffer> HybridDhKeyPair::getPublicKey() {
|
|
132
|
+
if (!pkey_) {
|
|
133
|
+
throw std::runtime_error("DH: no key pair generated");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
BIO* bio = BIO_new(BIO_s_mem());
|
|
137
|
+
if (!bio) {
|
|
138
|
+
throw std::runtime_error("DH: failed to create BIO for public key export");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (i2d_PUBKEY_bio(bio, pkey_.get()) != 1) {
|
|
142
|
+
BIO_free(bio);
|
|
143
|
+
throw std::runtime_error("DH: failed to export public key");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
BUF_MEM* mem;
|
|
147
|
+
BIO_get_mem_ptr(bio, &mem);
|
|
148
|
+
std::string derData(mem->data, mem->length);
|
|
149
|
+
BIO_free(bio);
|
|
150
|
+
|
|
151
|
+
return ToNativeArrayBuffer(derData);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
std::shared_ptr<ArrayBuffer> HybridDhKeyPair::getPrivateKey() {
|
|
155
|
+
if (!pkey_) {
|
|
156
|
+
throw std::runtime_error("DH: no key pair generated");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
BIO* bio = BIO_new(BIO_s_mem());
|
|
160
|
+
if (!bio) {
|
|
161
|
+
throw std::runtime_error("DH: failed to create BIO for private key export");
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (i2d_PKCS8PrivateKey_bio(bio, pkey_.get(), nullptr, nullptr, 0, nullptr, nullptr) != 1) {
|
|
165
|
+
BIO_free(bio);
|
|
166
|
+
throw std::runtime_error("DH: failed to export private key");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
BUF_MEM* mem;
|
|
170
|
+
BIO_get_mem_ptr(bio, &mem);
|
|
171
|
+
std::string derData(mem->data, mem->length);
|
|
172
|
+
BIO_free(bio);
|
|
173
|
+
|
|
174
|
+
return ToNativeArrayBuffer(derData);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#pragma clang diagnostic pop
|
|
178
|
+
|
|
179
|
+
} // namespace margelo::nitro::crypto
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <memory>
|
|
4
|
+
#include <openssl/dh.h>
|
|
5
|
+
#include <openssl/evp.h>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#include "HybridDhKeyPairSpec.hpp"
|
|
10
|
+
#include "QuickCryptoUtils.hpp"
|
|
11
|
+
|
|
12
|
+
namespace margelo::nitro::crypto {
|
|
13
|
+
|
|
14
|
+
class HybridDhKeyPair : public HybridDhKeyPairSpec {
|
|
15
|
+
public:
|
|
16
|
+
HybridDhKeyPair() : HybridObject(TAG) {}
|
|
17
|
+
~HybridDhKeyPair() override = default;
|
|
18
|
+
|
|
19
|
+
public:
|
|
20
|
+
std::shared_ptr<Promise<void>> generateKeyPair() override;
|
|
21
|
+
void generateKeyPairSync() override;
|
|
22
|
+
void setPrimeLength(double primeLength) override;
|
|
23
|
+
void setPrime(const std::shared_ptr<ArrayBuffer>& prime) override;
|
|
24
|
+
void setGenerator(double generator) override;
|
|
25
|
+
std::shared_ptr<ArrayBuffer> getPublicKey() override;
|
|
26
|
+
std::shared_ptr<ArrayBuffer> getPrivateKey() override;
|
|
27
|
+
|
|
28
|
+
private:
|
|
29
|
+
int primeLength_ = 0;
|
|
30
|
+
std::vector<uint8_t> prime_;
|
|
31
|
+
int generator_ = 2;
|
|
32
|
+
|
|
33
|
+
using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>;
|
|
34
|
+
EVP_PKEY_ptr pkey_{nullptr, EVP_PKEY_free};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
} // namespace margelo::nitro::crypto
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
#include "HybridDsaKeyPair.hpp"
|
|
2
|
+
|
|
3
|
+
#include <NitroModules/ArrayBuffer.hpp>
|
|
4
|
+
#include <NitroModules/Promise.hpp>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <openssl/bio.h>
|
|
7
|
+
#include <openssl/buffer.h>
|
|
8
|
+
#include <openssl/err.h>
|
|
9
|
+
#include <openssl/evp.h>
|
|
10
|
+
#include <openssl/pem.h>
|
|
11
|
+
#include <stdexcept>
|
|
12
|
+
#include <string>
|
|
13
|
+
|
|
14
|
+
namespace margelo::nitro::crypto {
|
|
15
|
+
|
|
16
|
+
void HybridDsaKeyPair::setModulusLength(double modulusLength) {
|
|
17
|
+
modulusLength_ = static_cast<int>(modulusLength);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
void HybridDsaKeyPair::setDivisorLength(double divisorLength) {
|
|
21
|
+
divisorLength_ = static_cast<int>(divisorLength);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
std::shared_ptr<Promise<void>> HybridDsaKeyPair::generateKeyPair() {
|
|
25
|
+
return Promise<void>::async([this]() { this->generateKeyPairSync(); });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
void HybridDsaKeyPair::generateKeyPairSync() {
|
|
29
|
+
if (modulusLength_ <= 0) {
|
|
30
|
+
throw std::runtime_error("DSA modulusLength must be set before generating key pair");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pkey_.reset();
|
|
34
|
+
|
|
35
|
+
// Step 1: Generate DSA parameters
|
|
36
|
+
std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, nullptr), EVP_PKEY_CTX_free);
|
|
37
|
+
|
|
38
|
+
if (!param_ctx) {
|
|
39
|
+
throw std::runtime_error("DSA: failed to create parameter context");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (EVP_PKEY_paramgen_init(param_ctx.get()) <= 0) {
|
|
43
|
+
throw std::runtime_error("DSA: failed to initialize parameter generation");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (EVP_PKEY_CTX_set_dsa_paramgen_bits(param_ctx.get(), modulusLength_) <= 0) {
|
|
47
|
+
throw std::runtime_error("DSA: failed to set modulus length");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (divisorLength_ >= 0) {
|
|
51
|
+
if (EVP_PKEY_CTX_set_dsa_paramgen_q_bits(param_ctx.get(), divisorLength_) <= 0) {
|
|
52
|
+
throw std::runtime_error("DSA: failed to set divisor length");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
EVP_PKEY* raw_params = nullptr;
|
|
57
|
+
if (EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) {
|
|
58
|
+
throw std::runtime_error("DSA: failed to generate parameters");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> params(raw_params, EVP_PKEY_free);
|
|
62
|
+
|
|
63
|
+
// Step 2: Generate key pair from parameters
|
|
64
|
+
std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)> key_ctx(EVP_PKEY_CTX_new(params.get(), nullptr), EVP_PKEY_CTX_free);
|
|
65
|
+
|
|
66
|
+
if (!key_ctx) {
|
|
67
|
+
throw std::runtime_error("DSA: failed to create key generation context");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (EVP_PKEY_keygen_init(key_ctx.get()) <= 0) {
|
|
71
|
+
throw std::runtime_error("DSA: failed to initialize key generation");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
EVP_PKEY* raw_pkey = nullptr;
|
|
75
|
+
if (EVP_PKEY_keygen(key_ctx.get(), &raw_pkey) <= 0) {
|
|
76
|
+
throw std::runtime_error("DSA: failed to generate key pair");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
pkey_.reset(raw_pkey);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
std::shared_ptr<ArrayBuffer> HybridDsaKeyPair::getPublicKey() {
|
|
83
|
+
if (!pkey_) {
|
|
84
|
+
throw std::runtime_error("DSA: no key pair generated");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
BIO* bio = BIO_new(BIO_s_mem());
|
|
88
|
+
if (!bio) {
|
|
89
|
+
throw std::runtime_error("DSA: failed to create BIO for public key export");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (i2d_PUBKEY_bio(bio, pkey_.get()) != 1) {
|
|
93
|
+
BIO_free(bio);
|
|
94
|
+
throw std::runtime_error("DSA: failed to export public key");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
BUF_MEM* mem;
|
|
98
|
+
BIO_get_mem_ptr(bio, &mem);
|
|
99
|
+
std::string derData(mem->data, mem->length);
|
|
100
|
+
BIO_free(bio);
|
|
101
|
+
|
|
102
|
+
return ToNativeArrayBuffer(derData);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
std::shared_ptr<ArrayBuffer> HybridDsaKeyPair::getPrivateKey() {
|
|
106
|
+
if (!pkey_) {
|
|
107
|
+
throw std::runtime_error("DSA: no key pair generated");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
BIO* bio = BIO_new(BIO_s_mem());
|
|
111
|
+
if (!bio) {
|
|
112
|
+
throw std::runtime_error("DSA: failed to create BIO for private key export");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (i2d_PKCS8PrivateKey_bio(bio, pkey_.get(), nullptr, nullptr, 0, nullptr, nullptr) != 1) {
|
|
116
|
+
BIO_free(bio);
|
|
117
|
+
throw std::runtime_error("DSA: failed to export private key");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
BUF_MEM* mem;
|
|
121
|
+
BIO_get_mem_ptr(bio, &mem);
|
|
122
|
+
std::string derData(mem->data, mem->length);
|
|
123
|
+
BIO_free(bio);
|
|
124
|
+
|
|
125
|
+
return ToNativeArrayBuffer(derData);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
} // namespace margelo::nitro::crypto
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <memory>
|
|
4
|
+
#include <openssl/evp.h>
|
|
5
|
+
|
|
6
|
+
#include "HybridDsaKeyPairSpec.hpp"
|
|
7
|
+
#include "QuickCryptoUtils.hpp"
|
|
8
|
+
|
|
9
|
+
namespace margelo::nitro::crypto {
|
|
10
|
+
|
|
11
|
+
class HybridDsaKeyPair : public HybridDsaKeyPairSpec {
|
|
12
|
+
public:
|
|
13
|
+
HybridDsaKeyPair() : HybridObject(TAG) {}
|
|
14
|
+
~HybridDsaKeyPair() override = default;
|
|
15
|
+
|
|
16
|
+
public:
|
|
17
|
+
std::shared_ptr<Promise<void>> generateKeyPair() override;
|
|
18
|
+
void generateKeyPairSync() override;
|
|
19
|
+
void setModulusLength(double modulusLength) override;
|
|
20
|
+
void setDivisorLength(double divisorLength) override;
|
|
21
|
+
std::shared_ptr<ArrayBuffer> getPublicKey() override;
|
|
22
|
+
std::shared_ptr<ArrayBuffer> getPrivateKey() override;
|
|
23
|
+
|
|
24
|
+
private:
|
|
25
|
+
int modulusLength_ = 0;
|
|
26
|
+
int divisorLength_ = -1;
|
|
27
|
+
|
|
28
|
+
using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>;
|
|
29
|
+
EVP_PKEY_ptr pkey_{nullptr, EVP_PKEY_free};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
} // namespace margelo::nitro::crypto
|