react-native-quick-crypto 1.1.1 → 1.1.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 +1 -0
- package/android/CMakeLists.txt +4 -0
- package/cpp/cipher/CCMCipher.cpp +7 -11
- package/cpp/cipher/ChaCha20Cipher.cpp +6 -10
- package/cpp/cipher/ChaCha20Poly1305Cipher.cpp +10 -16
- package/cpp/cipher/GCMCipher.cpp +3 -5
- package/cpp/cipher/HybridCipher.cpp +7 -13
- package/cpp/cipher/HybridRsaCipher.cpp +19 -27
- package/cpp/cipher/OCBCipher.cpp +2 -3
- package/cpp/cipher/XChaCha20Poly1305Cipher.cpp +13 -19
- package/cpp/cipher/XSalsa20Cipher.cpp +8 -12
- package/cpp/cipher/XSalsa20Poly1305Cipher.cpp +11 -16
- package/cpp/keys/HybridKeyObjectHandle.cpp +630 -2
- package/cpp/keys/HybridKeyObjectHandle.hpp +21 -1
- package/cpp/sign/HybridSignHandle.cpp +26 -8
- package/cpp/sign/HybridVerifyHandle.cpp +28 -11
- package/cpp/slhdsa/HybridSlhDsaKeyPair.cpp +245 -0
- package/cpp/slhdsa/HybridSlhDsaKeyPair.hpp +48 -0
- package/cpp/turboshake/HybridTurboShake.cpp +379 -0
- package/cpp/turboshake/HybridTurboShake.hpp +28 -0
- package/cpp/utils/HybridUtils.cpp +26 -14
- package/deps/blake3/README.md +6 -7
- package/deps/blake3/c/blake3.c +3 -2
- package/deps/blake3/c/blake3.h +2 -2
- package/deps/blake3/c/blake3_dispatch.c +2 -2
- package/deps/blake3/c/blake3_impl.h +1 -1
- package/deps/blake3/c/blake3_neon.c +5 -4
- package/deps/ncrypto/include/ncrypto/version.h +2 -2
- package/deps/ncrypto/include/ncrypto.h +9 -2
- package/deps/ncrypto/src/ncrypto.cpp +130 -35
- package/lib/commonjs/dhKeyPair.js +3 -0
- package/lib/commonjs/dhKeyPair.js.map +1 -1
- package/lib/commonjs/dsa.js +3 -0
- package/lib/commonjs/dsa.js.map +1 -1
- package/lib/commonjs/ec.js +37 -30
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/ed.js +60 -6
- package/lib/commonjs/ed.js.map +1 -1
- package/lib/commonjs/hash.js +52 -5
- package/lib/commonjs/hash.js.map +1 -1
- package/lib/commonjs/keys/classes.js +33 -7
- package/lib/commonjs/keys/classes.js.map +1 -1
- package/lib/commonjs/keys/generateKeyPair.js +85 -4
- package/lib/commonjs/keys/generateKeyPair.js.map +1 -1
- package/lib/commonjs/keys/index.js +50 -2
- package/lib/commonjs/keys/index.js.map +1 -1
- package/lib/commonjs/keys/signVerify.js +9 -2
- package/lib/commonjs/keys/signVerify.js.map +1 -1
- package/lib/commonjs/keys/utils.js +59 -1
- package/lib/commonjs/keys/utils.js.map +1 -1
- package/lib/commonjs/random.js +63 -9
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/rsa.js +3 -0
- package/lib/commonjs/rsa.js.map +1 -1
- package/lib/commonjs/slhdsa.js +70 -0
- package/lib/commonjs/slhdsa.js.map +1 -0
- package/lib/commonjs/specs/slhDsaKeyPair.nitro.js +6 -0
- package/lib/commonjs/specs/slhDsaKeyPair.nitro.js.map +1 -0
- package/lib/commonjs/specs/turboshake.nitro.js +6 -0
- package/lib/commonjs/specs/turboshake.nitro.js.map +1 -0
- package/lib/commonjs/subtle.js +926 -275
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/commonjs/utils/conversion.js +53 -19
- package/lib/commonjs/utils/conversion.js.map +1 -1
- package/lib/commonjs/utils/errors.js +63 -4
- package/lib/commonjs/utils/errors.js.map +1 -1
- package/lib/commonjs/utils/types.js.map +1 -1
- package/lib/commonjs/utils/validation.js +46 -0
- package/lib/commonjs/utils/validation.js.map +1 -1
- package/lib/module/dhKeyPair.js +3 -0
- package/lib/module/dhKeyPair.js.map +1 -1
- package/lib/module/dsa.js +3 -0
- package/lib/module/dsa.js.map +1 -1
- package/lib/module/ec.js +38 -31
- package/lib/module/ec.js.map +1 -1
- package/lib/module/ed.js +61 -7
- package/lib/module/ed.js.map +1 -1
- package/lib/module/hash.js +52 -5
- package/lib/module/hash.js.map +1 -1
- package/lib/module/keys/classes.js +31 -5
- package/lib/module/keys/classes.js.map +1 -1
- package/lib/module/keys/generateKeyPair.js +86 -5
- package/lib/module/keys/generateKeyPair.js.map +1 -1
- package/lib/module/keys/index.js +50 -2
- package/lib/module/keys/index.js.map +1 -1
- package/lib/module/keys/signVerify.js +9 -2
- package/lib/module/keys/signVerify.js.map +1 -1
- package/lib/module/keys/utils.js +57 -1
- package/lib/module/keys/utils.js.map +1 -1
- package/lib/module/random.js +63 -10
- package/lib/module/random.js.map +1 -1
- package/lib/module/rsa.js +3 -0
- package/lib/module/rsa.js.map +1 -1
- package/lib/module/slhdsa.js +64 -0
- package/lib/module/slhdsa.js.map +1 -0
- package/lib/module/specs/slhDsaKeyPair.nitro.js +4 -0
- package/lib/module/specs/slhDsaKeyPair.nitro.js.map +1 -0
- package/lib/module/specs/turboshake.nitro.js +4 -0
- package/lib/module/specs/turboshake.nitro.js.map +1 -0
- package/lib/module/subtle.js +927 -276
- package/lib/module/subtle.js.map +1 -1
- package/lib/module/utils/conversion.js +51 -19
- package/lib/module/utils/conversion.js.map +1 -1
- package/lib/module/utils/errors.js +61 -4
- package/lib/module/utils/errors.js.map +1 -1
- package/lib/module/utils/types.js.map +1 -1
- package/lib/module/utils/validation.js +44 -0
- package/lib/module/utils/validation.js.map +1 -1
- package/lib/typescript/dhKeyPair.d.ts.map +1 -1
- package/lib/typescript/dsa.d.ts.map +1 -1
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/ed.d.ts.map +1 -1
- package/lib/typescript/hash.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +12 -7
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys/classes.d.ts +10 -1
- package/lib/typescript/keys/classes.d.ts.map +1 -1
- package/lib/typescript/keys/generateKeyPair.d.ts +12 -1
- package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -1
- package/lib/typescript/keys/index.d.ts +3 -1
- package/lib/typescript/keys/index.d.ts.map +1 -1
- package/lib/typescript/keys/signVerify.d.ts.map +1 -1
- package/lib/typescript/keys/utils.d.ts +21 -4
- package/lib/typescript/keys/utils.d.ts.map +1 -1
- package/lib/typescript/random.d.ts +5 -1
- package/lib/typescript/random.d.ts.map +1 -1
- package/lib/typescript/rsa.d.ts.map +1 -1
- package/lib/typescript/slhdsa.d.ts +19 -0
- package/lib/typescript/slhdsa.d.ts.map +1 -0
- package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +9 -0
- package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -1
- package/lib/typescript/specs/slhDsaKeyPair.nitro.d.ts +16 -0
- package/lib/typescript/specs/slhDsaKeyPair.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/turboshake.nitro.d.ts +11 -0
- package/lib/typescript/specs/turboshake.nitro.d.ts.map +1 -0
- package/lib/typescript/subtle.d.ts +3 -2
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/lib/typescript/utils/conversion.d.ts +4 -3
- package/lib/typescript/utils/conversion.d.ts.map +1 -1
- package/lib/typescript/utils/errors.d.ts +12 -0
- package/lib/typescript/utils/errors.d.ts.map +1 -1
- package/lib/typescript/utils/types.d.ts +32 -15
- package/lib/typescript/utils/types.d.ts.map +1 -1
- package/lib/typescript/utils/validation.d.ts +3 -1
- package/lib/typescript/utils/validation.d.ts.map +1 -1
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +2 -0
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +20 -0
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +20 -0
- package/nitrogen/generated/shared/c++/AsymmetricKeyType.hpp +48 -0
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +9 -0
- package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +9 -0
- package/nitrogen/generated/shared/c++/HybridSlhDsaKeyPairSpec.cpp +29 -0
- package/nitrogen/generated/shared/c++/HybridSlhDsaKeyPairSpec.hpp +72 -0
- package/nitrogen/generated/shared/c++/HybridTurboShakeSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridTurboShakeSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/JWK.hpp +9 -1
- package/nitrogen/generated/shared/c++/JWKkty.hpp +4 -0
- package/nitrogen/generated/shared/c++/KangarooTwelveVariant.hpp +76 -0
- package/nitrogen/generated/shared/c++/TurboShakeVariant.hpp +76 -0
- package/package.json +2 -3
- package/src/dhKeyPair.ts +8 -0
- package/src/dsa.ts +8 -0
- package/src/ec.ts +52 -29
- package/src/ed.ts +95 -16
- package/src/hash.ts +108 -5
- package/src/keys/classes.ts +46 -5
- package/src/keys/generateKeyPair.ts +151 -5
- package/src/keys/index.ts +73 -3
- package/src/keys/signVerify.ts +13 -2
- package/src/keys/utils.ts +78 -5
- package/src/random.ts +93 -9
- package/src/rsa.ts +8 -0
- package/src/slhdsa.ts +146 -0
- package/src/specs/keyObjectHandle.nitro.ts +17 -0
- package/src/specs/slhDsaKeyPair.nitro.ts +29 -0
- package/src/specs/turboshake.nitro.ts +21 -0
- package/src/subtle.ts +1191 -360
- package/src/utils/conversion.ts +72 -21
- package/src/utils/errors.ts +72 -4
- package/src/utils/types.ts +80 -15
- package/src/utils/validation.ts +70 -1
package/QuickCrypto.podspec
CHANGED
|
@@ -152,6 +152,7 @@ Pod::Spec.new do |s|
|
|
|
152
152
|
"\"$(PODS_TARGET_SRCROOT)/cpp/hkdf\"",
|
|
153
153
|
"\"$(PODS_TARGET_SRCROOT)/cpp/dh\"",
|
|
154
154
|
"\"$(PODS_TARGET_SRCROOT)/cpp/ecdh\"",
|
|
155
|
+
"\"$(PODS_TARGET_SRCROOT)/cpp/turboshake\"",
|
|
155
156
|
"\"$(PODS_TARGET_SRCROOT)/nitrogen/generated/shared/c++\"",
|
|
156
157
|
"\"$(PODS_TARGET_SRCROOT)/deps/ncrypto/include\"",
|
|
157
158
|
"\"$(PODS_TARGET_SRCROOT)/deps/simdutf/include\"",
|
package/android/CMakeLists.txt
CHANGED
|
@@ -52,6 +52,7 @@ add_library(
|
|
|
52
52
|
../cpp/keys/KeyObjectData.cpp
|
|
53
53
|
../cpp/mldsa/HybridMlDsaKeyPair.cpp
|
|
54
54
|
../cpp/mlkem/HybridMlKemKeyPair.cpp
|
|
55
|
+
../cpp/slhdsa/HybridSlhDsaKeyPair.cpp
|
|
55
56
|
../cpp/pbkdf2/HybridPbkdf2.cpp
|
|
56
57
|
../cpp/prime/HybridPrime.cpp
|
|
57
58
|
../cpp/random/HybridRandom.cpp
|
|
@@ -59,6 +60,7 @@ add_library(
|
|
|
59
60
|
../cpp/scrypt/HybridScrypt.cpp
|
|
60
61
|
../cpp/sign/HybridSignHandle.cpp
|
|
61
62
|
../cpp/sign/HybridVerifyHandle.cpp
|
|
63
|
+
../cpp/turboshake/HybridTurboShake.cpp
|
|
62
64
|
../cpp/x509/HybridX509Certificate.cpp
|
|
63
65
|
../cpp/utils/HybridUtils.cpp
|
|
64
66
|
../cpp/utils/QuickCryptoUtils.cpp
|
|
@@ -92,12 +94,14 @@ include_directories(
|
|
|
92
94
|
"../cpp/keys"
|
|
93
95
|
"../cpp/mldsa"
|
|
94
96
|
"../cpp/mlkem"
|
|
97
|
+
"../cpp/slhdsa"
|
|
95
98
|
"../cpp/pbkdf2"
|
|
96
99
|
"../cpp/prime"
|
|
97
100
|
"../cpp/random"
|
|
98
101
|
"../cpp/rsa"
|
|
99
102
|
"../cpp/sign"
|
|
100
103
|
"../cpp/scrypt"
|
|
104
|
+
"../cpp/turboshake"
|
|
101
105
|
"../cpp/utils"
|
|
102
106
|
"../cpp/x509"
|
|
103
107
|
"../deps/blake3/c"
|
package/cpp/cipher/CCMCipher.cpp
CHANGED
|
@@ -18,8 +18,7 @@ void CCMCipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::s
|
|
|
18
18
|
checkCtx();
|
|
19
19
|
|
|
20
20
|
// 2. Perform CCM-specific initialization
|
|
21
|
-
|
|
22
|
-
size_t iv_len = native_iv->size();
|
|
21
|
+
size_t iv_len = iv->size();
|
|
23
22
|
|
|
24
23
|
// Set the IV length using CCM-specific control
|
|
25
24
|
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_CCM_SET_IVLEN, iv_len, nullptr) != 1) {
|
|
@@ -39,9 +38,8 @@ void CCMCipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::s
|
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
// Finally, initialize the key and IV using the parameters passed to this function.
|
|
42
|
-
|
|
43
|
-
const unsigned char*
|
|
44
|
-
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(native_iv->data());
|
|
41
|
+
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(cipher_key->data());
|
|
42
|
+
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(iv->data());
|
|
45
43
|
|
|
46
44
|
// The last argument (is_cipher) should be consistent with the initial setup call.
|
|
47
45
|
if (EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key_ptr, iv_ptr, is_cipher) != 1) {
|
|
@@ -56,8 +54,7 @@ std::shared_ptr<ArrayBuffer> CCMCipher::update(const std::shared_ptr<ArrayBuffer
|
|
|
56
54
|
checkCtx();
|
|
57
55
|
checkNotFinalized();
|
|
58
56
|
has_update_called = true;
|
|
59
|
-
|
|
60
|
-
size_t in_len = native_data->size();
|
|
57
|
+
size_t in_len = data->size();
|
|
61
58
|
if (in_len < 0 || in_len > INT_MAX) {
|
|
62
59
|
throw std::runtime_error("Invalid message length");
|
|
63
60
|
}
|
|
@@ -77,7 +74,7 @@ std::shared_ptr<ArrayBuffer> CCMCipher::update(const std::shared_ptr<ArrayBuffer
|
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
auto out_buf = std::make_unique<unsigned char[]>(out_len);
|
|
80
|
-
const uint8_t* in = reinterpret_cast<const uint8_t*>(
|
|
77
|
+
const uint8_t* in = reinterpret_cast<const uint8_t*>(data->data());
|
|
81
78
|
|
|
82
79
|
int actual_out_len = 0;
|
|
83
80
|
int ret = EVP_CipherUpdate(ctx.get(), out_buf.get(), &actual_out_len, in, in_len);
|
|
@@ -185,8 +182,7 @@ bool CCMCipher::setAAD(const std::shared_ptr<ArrayBuffer>& data, std::optional<d
|
|
|
185
182
|
int out_len = 0;
|
|
186
183
|
|
|
187
184
|
// Get AAD data and length *before* deciding whether to set total length
|
|
188
|
-
|
|
189
|
-
size_t aad_len = native_aad->size();
|
|
185
|
+
size_t aad_len = data->size();
|
|
190
186
|
|
|
191
187
|
// 1. Set the total *ciphertext* length. This seems necessary based on examples,
|
|
192
188
|
// BUT the wiki says "(only needed if AAD is passed)". Let's skip if decrypting and AAD length is 0.
|
|
@@ -203,7 +199,7 @@ bool CCMCipher::setAAD(const std::shared_ptr<ArrayBuffer>& data, std::optional<d
|
|
|
203
199
|
// 2. Process AAD Data
|
|
204
200
|
// Per OpenSSL CCM decryption examples, this MUST be called even if aad_len is 0.
|
|
205
201
|
// Pass nullptr as the output buffer, the AAD data pointer, and its length.
|
|
206
|
-
if (EVP_CipherUpdate(ctx.get(), nullptr, &out_len,
|
|
202
|
+
if (EVP_CipherUpdate(ctx.get(), nullptr, &out_len, data->data(), aad_len) != 1) {
|
|
207
203
|
unsigned long err = ERR_get_error();
|
|
208
204
|
char err_buf[256];
|
|
209
205
|
ERR_error_string_n(err, err_buf, sizeof(err_buf));
|
|
@@ -32,21 +32,18 @@ void ChaCha20Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const s
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// Set key and IV
|
|
35
|
-
auto native_key = ToNativeArrayBuffer(cipher_key);
|
|
36
|
-
auto native_iv = ToNativeArrayBuffer(iv);
|
|
37
|
-
|
|
38
35
|
// Validate key size
|
|
39
|
-
if (
|
|
36
|
+
if (cipher_key->size() != kKeySize) {
|
|
40
37
|
throw std::runtime_error("ChaCha20 key must be 32 bytes");
|
|
41
38
|
}
|
|
42
39
|
|
|
43
40
|
// Validate IV size
|
|
44
|
-
if (
|
|
41
|
+
if (iv->size() != kIVSize) {
|
|
45
42
|
throw std::runtime_error("ChaCha20 IV must be 16 bytes");
|
|
46
43
|
}
|
|
47
44
|
|
|
48
|
-
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(
|
|
49
|
-
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(
|
|
45
|
+
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(cipher_key->data());
|
|
46
|
+
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(iv->data());
|
|
50
47
|
|
|
51
48
|
if (EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key_ptr, iv_ptr, is_cipher) != 1) {
|
|
52
49
|
unsigned long err = ERR_get_error();
|
|
@@ -60,8 +57,7 @@ void ChaCha20Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const s
|
|
|
60
57
|
std::shared_ptr<ArrayBuffer> ChaCha20Cipher::update(const std::shared_ptr<ArrayBuffer>& data) {
|
|
61
58
|
checkCtx();
|
|
62
59
|
checkNotFinalized();
|
|
63
|
-
|
|
64
|
-
size_t in_len = native_data->size();
|
|
60
|
+
size_t in_len = data->size();
|
|
65
61
|
if (in_len > INT_MAX) {
|
|
66
62
|
throw std::runtime_error("Message too long");
|
|
67
63
|
}
|
|
@@ -71,7 +67,7 @@ std::shared_ptr<ArrayBuffer> ChaCha20Cipher::update(const std::shared_ptr<ArrayB
|
|
|
71
67
|
auto out_buf = std::make_unique<uint8_t[]>(out_len);
|
|
72
68
|
|
|
73
69
|
// Perform the cipher update operation
|
|
74
|
-
if (EVP_CipherUpdate(ctx.get(), out_buf.get(), &out_len,
|
|
70
|
+
if (EVP_CipherUpdate(ctx.get(), out_buf.get(), &out_len, data->data(), in_len) != 1) {
|
|
75
71
|
unsigned long err = ERR_get_error();
|
|
76
72
|
char err_buf[256];
|
|
77
73
|
ERR_error_string_n(err, err_buf, sizeof(err_buf));
|
|
@@ -32,21 +32,18 @@ void ChaCha20Poly1305Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key,
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// Set key and IV
|
|
35
|
-
auto native_key = ToNativeArrayBuffer(cipher_key);
|
|
36
|
-
auto native_iv = ToNativeArrayBuffer(iv);
|
|
37
|
-
|
|
38
35
|
// Validate key size
|
|
39
|
-
if (
|
|
36
|
+
if (cipher_key->size() != kKeySize) {
|
|
40
37
|
throw std::runtime_error("ChaCha20-Poly1305 key must be 32 bytes");
|
|
41
38
|
}
|
|
42
39
|
|
|
43
40
|
// Validate nonce size
|
|
44
|
-
if (
|
|
41
|
+
if (iv->size() != kNonceSize) {
|
|
45
42
|
throw std::runtime_error("ChaCha20-Poly1305 nonce must be 12 bytes");
|
|
46
43
|
}
|
|
47
44
|
|
|
48
|
-
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(
|
|
49
|
-
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(
|
|
45
|
+
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(cipher_key->data());
|
|
46
|
+
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(iv->data());
|
|
50
47
|
|
|
51
48
|
if (EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key_ptr, iv_ptr, is_cipher) != 1) {
|
|
52
49
|
unsigned long err = ERR_get_error();
|
|
@@ -66,8 +63,7 @@ std::shared_ptr<ArrayBuffer> ChaCha20Poly1305Cipher::update(const std::shared_pt
|
|
|
66
63
|
checkCtx();
|
|
67
64
|
checkNotFinalized();
|
|
68
65
|
has_update_called = true;
|
|
69
|
-
|
|
70
|
-
size_t in_len = native_data->size();
|
|
66
|
+
size_t in_len = data->size();
|
|
71
67
|
if (in_len > INT_MAX) {
|
|
72
68
|
throw std::runtime_error("Message too long");
|
|
73
69
|
}
|
|
@@ -77,7 +73,7 @@ std::shared_ptr<ArrayBuffer> ChaCha20Poly1305Cipher::update(const std::shared_pt
|
|
|
77
73
|
auto out_buf = std::make_unique<uint8_t[]>(out_len);
|
|
78
74
|
|
|
79
75
|
// Perform the cipher update operation
|
|
80
|
-
if (EVP_CipherUpdate(ctx.get(), out_buf.get(), &out_len,
|
|
76
|
+
if (EVP_CipherUpdate(ctx.get(), out_buf.get(), &out_len, data->data(), in_len) != 1) {
|
|
81
77
|
unsigned long err = ERR_get_error();
|
|
82
78
|
char err_buf[256];
|
|
83
79
|
ERR_error_string_n(err, err_buf, sizeof(err_buf));
|
|
@@ -121,12 +117,11 @@ std::shared_ptr<ArrayBuffer> ChaCha20Poly1305Cipher::final() {
|
|
|
121
117
|
bool ChaCha20Poly1305Cipher::setAAD(const std::shared_ptr<ArrayBuffer>& data, std::optional<double> plaintextLength) {
|
|
122
118
|
checkCtx();
|
|
123
119
|
checkAADBeforeUpdate();
|
|
124
|
-
|
|
125
|
-
size_t aad_len = native_aad->size();
|
|
120
|
+
size_t aad_len = data->size();
|
|
126
121
|
|
|
127
122
|
// Set AAD data
|
|
128
123
|
int out_len = 0;
|
|
129
|
-
if (EVP_CipherUpdate(ctx.get(), nullptr, &out_len,
|
|
124
|
+
if (EVP_CipherUpdate(ctx.get(), nullptr, &out_len, data->data(), aad_len) != 1) {
|
|
130
125
|
unsigned long err = ERR_get_error();
|
|
131
126
|
char err_buf[256];
|
|
132
127
|
ERR_error_string_n(err, err_buf, sizeof(err_buf));
|
|
@@ -163,12 +158,11 @@ bool ChaCha20Poly1305Cipher::setAuthTag(const std::shared_ptr<ArrayBuffer>& tag)
|
|
|
163
158
|
throw std::runtime_error("setAuthTag can only be called during decryption");
|
|
164
159
|
}
|
|
165
160
|
|
|
166
|
-
|
|
167
|
-
if (native_tag->size() != kTagSize) {
|
|
161
|
+
if (tag->size() != kTagSize) {
|
|
168
162
|
throw std::runtime_error("ChaCha20-Poly1305 tag must be 16 bytes");
|
|
169
163
|
}
|
|
170
164
|
|
|
171
|
-
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_TAG, kTagSize,
|
|
165
|
+
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_TAG, kTagSize, tag->data()) != 1) {
|
|
172
166
|
unsigned long err = ERR_get_error();
|
|
173
167
|
char err_buf[256];
|
|
174
168
|
ERR_error_string_n(err, err_buf, sizeof(err_buf));
|
package/cpp/cipher/GCMCipher.cpp
CHANGED
|
@@ -37,8 +37,7 @@ void GCMCipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::s
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// 4. Set IV length for non-standard IV sizes (GCM default is 96 bits/12 bytes)
|
|
40
|
-
|
|
41
|
-
size_t iv_len = native_iv->size();
|
|
40
|
+
size_t iv_len = iv->size();
|
|
42
41
|
|
|
43
42
|
if (iv_len != 12) { // Only set if not the default length
|
|
44
43
|
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, static_cast<int>(iv_len), nullptr) != 1) {
|
|
@@ -51,9 +50,8 @@ void GCMCipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::s
|
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
// 5. Now set the key and IV
|
|
54
|
-
|
|
55
|
-
const unsigned char*
|
|
56
|
-
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(native_iv->data());
|
|
53
|
+
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(cipher_key->data());
|
|
54
|
+
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(iv->data());
|
|
57
55
|
|
|
58
56
|
if (EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key_ptr, iv_ptr, is_cipher) != 1) {
|
|
59
57
|
unsigned long err = ERR_get_error();
|
|
@@ -83,10 +83,8 @@ void HybridCipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std
|
|
|
83
83
|
|
|
84
84
|
// For base hybrid cipher, set key and IV immediately.
|
|
85
85
|
// Derived classes like CCM might override init and handle this differently.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(native_key->data());
|
|
89
|
-
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(native_iv->data());
|
|
86
|
+
const unsigned char* key_ptr = reinterpret_cast<const unsigned char*>(cipher_key->data());
|
|
87
|
+
const unsigned char* iv_ptr = reinterpret_cast<const unsigned char*>(iv->data());
|
|
90
88
|
|
|
91
89
|
if (EVP_CipherInit_ex(ctx.get(), nullptr, nullptr, key_ptr, iv_ptr, is_cipher) != 1) {
|
|
92
90
|
unsigned long err = ERR_get_error();
|
|
@@ -106,11 +104,10 @@ void HybridCipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std
|
|
|
106
104
|
}
|
|
107
105
|
|
|
108
106
|
std::shared_ptr<ArrayBuffer> HybridCipher::update(const std::shared_ptr<ArrayBuffer>& data) {
|
|
109
|
-
auto native_data = ToNativeArrayBuffer(data);
|
|
110
107
|
checkCtx();
|
|
111
108
|
checkNotFinalized();
|
|
112
109
|
has_update_called = true;
|
|
113
|
-
size_t in_len =
|
|
110
|
+
size_t in_len = data->size();
|
|
114
111
|
if (in_len > INT_MAX) {
|
|
115
112
|
throw std::runtime_error("Message too long");
|
|
116
113
|
}
|
|
@@ -119,7 +116,7 @@ std::shared_ptr<ArrayBuffer> HybridCipher::update(const std::shared_ptr<ArrayBuf
|
|
|
119
116
|
auto out_buf = std::make_unique<uint8_t[]>(out_len);
|
|
120
117
|
// Perform the cipher update operation. The real size of the output is
|
|
121
118
|
// returned in out_len
|
|
122
|
-
int ret = EVP_CipherUpdate(ctx.get(), out_buf.get(), &out_len,
|
|
119
|
+
int ret = EVP_CipherUpdate(ctx.get(), out_buf.get(), &out_len, data->data(), in_len);
|
|
123
120
|
|
|
124
121
|
if (!ret) {
|
|
125
122
|
unsigned long err = ERR_get_error();
|
|
@@ -168,11 +165,9 @@ std::shared_ptr<ArrayBuffer> HybridCipher::final() {
|
|
|
168
165
|
bool HybridCipher::setAAD(const std::shared_ptr<ArrayBuffer>& data, std::optional<double> plaintextLength) {
|
|
169
166
|
checkCtx();
|
|
170
167
|
checkAADBeforeUpdate();
|
|
171
|
-
auto native_data = ToNativeArrayBuffer(data);
|
|
172
|
-
|
|
173
168
|
// Set the AAD
|
|
174
169
|
int out_len;
|
|
175
|
-
if (!EVP_CipherUpdate(ctx.get(), nullptr, &out_len,
|
|
170
|
+
if (!EVP_CipherUpdate(ctx.get(), nullptr, &out_len, data->data(), data->size())) {
|
|
176
171
|
return false;
|
|
177
172
|
}
|
|
178
173
|
|
|
@@ -192,9 +187,8 @@ bool HybridCipher::setAuthTag(const std::shared_ptr<ArrayBuffer>& tag) {
|
|
|
192
187
|
throw std::runtime_error("setAuthTag can only be called during decryption.");
|
|
193
188
|
}
|
|
194
189
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
uint8_t* tag_ptr = native_tag->data();
|
|
190
|
+
size_t tag_len = tag->size();
|
|
191
|
+
uint8_t* tag_ptr = tag->data();
|
|
198
192
|
|
|
199
193
|
int mode = EVP_CIPHER_CTX_mode(ctx.get());
|
|
200
194
|
|
|
@@ -102,15 +102,14 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::encrypt(const std::shared_ptr<Hybr
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
if (label.has_value() && label.value()->size() > 0) {
|
|
105
|
-
|
|
106
|
-
unsigned char* label_copy = (unsigned char*)OPENSSL_malloc(native_label->size());
|
|
105
|
+
unsigned char* label_copy = (unsigned char*)OPENSSL_malloc(label.value()->size());
|
|
107
106
|
if (!label_copy) {
|
|
108
107
|
EVP_PKEY_CTX_free(ctx);
|
|
109
108
|
throw std::runtime_error("Failed to allocate memory for label");
|
|
110
109
|
}
|
|
111
|
-
std::memcpy(label_copy,
|
|
110
|
+
std::memcpy(label_copy, label.value()->data(), label.value()->size());
|
|
112
111
|
|
|
113
|
-
if (EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy,
|
|
112
|
+
if (EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy, label.value()->size()) <= 0) {
|
|
114
113
|
OPENSSL_free(label_copy);
|
|
115
114
|
EVP_PKEY_CTX_free(ctx);
|
|
116
115
|
throw std::runtime_error("Failed to set OAEP label");
|
|
@@ -118,9 +117,8 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::encrypt(const std::shared_ptr<Hybr
|
|
|
118
117
|
}
|
|
119
118
|
}
|
|
120
119
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
size_t inlen = native_data->size();
|
|
120
|
+
const unsigned char* in = data->data();
|
|
121
|
+
size_t inlen = data->size();
|
|
124
122
|
|
|
125
123
|
size_t outlen;
|
|
126
124
|
if (EVP_PKEY_encrypt(ctx, nullptr, &outlen, in, inlen) <= 0) {
|
|
@@ -197,15 +195,14 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::decrypt(const std::shared_ptr<Hybr
|
|
|
197
195
|
}
|
|
198
196
|
|
|
199
197
|
if (label.has_value() && label.value()->size() > 0) {
|
|
200
|
-
|
|
201
|
-
unsigned char* label_copy = (unsigned char*)OPENSSL_malloc(native_label->size());
|
|
198
|
+
unsigned char* label_copy = (unsigned char*)OPENSSL_malloc(label.value()->size());
|
|
202
199
|
if (!label_copy) {
|
|
203
200
|
EVP_PKEY_CTX_free(ctx);
|
|
204
201
|
throw std::runtime_error("Failed to allocate memory for label");
|
|
205
202
|
}
|
|
206
|
-
std::memcpy(label_copy,
|
|
203
|
+
std::memcpy(label_copy, label.value()->data(), label.value()->size());
|
|
207
204
|
|
|
208
|
-
if (EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy,
|
|
205
|
+
if (EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy, label.value()->size()) <= 0) {
|
|
209
206
|
OPENSSL_free(label_copy);
|
|
210
207
|
EVP_PKEY_CTX_free(ctx);
|
|
211
208
|
throw std::runtime_error("Failed to set OAEP label");
|
|
@@ -213,9 +210,8 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::decrypt(const std::shared_ptr<Hybr
|
|
|
213
210
|
}
|
|
214
211
|
}
|
|
215
212
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
size_t inlen = native_data->size();
|
|
213
|
+
const unsigned char* in = data->data();
|
|
214
|
+
size_t inlen = data->size();
|
|
219
215
|
|
|
220
216
|
// Both decrypt calls below operate on attacker-controlled ciphertext, so
|
|
221
217
|
// any failure must be surfaced with an opaque, content-independent message.
|
|
@@ -269,9 +265,8 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::publicDecrypt(const std::shared_pt
|
|
|
269
265
|
throw std::runtime_error("Failed to set RSA padding");
|
|
270
266
|
}
|
|
271
267
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
size_t inlen = native_data->size();
|
|
268
|
+
const unsigned char* in = data->data();
|
|
269
|
+
size_t inlen = data->size();
|
|
275
270
|
|
|
276
271
|
// verify_recover acts on attacker-controlled ciphertext too — surface only
|
|
277
272
|
// an opaque error so a remote caller cannot distinguish failure modes.
|
|
@@ -351,9 +346,8 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::privateEncrypt(const std::shared_p
|
|
|
351
346
|
throw std::runtime_error("Failed to set RSA padding");
|
|
352
347
|
}
|
|
353
348
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
size_t inlen = native_data->size();
|
|
349
|
+
const unsigned char* in = data->data();
|
|
350
|
+
size_t inlen = data->size();
|
|
357
351
|
|
|
358
352
|
size_t outlen;
|
|
359
353
|
if (EVP_PKEY_sign(ctx, nullptr, &outlen, in, inlen) <= 0) {
|
|
@@ -430,15 +424,14 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::privateDecrypt(const std::shared_p
|
|
|
430
424
|
}
|
|
431
425
|
|
|
432
426
|
if (label.has_value() && label.value()->size() > 0) {
|
|
433
|
-
|
|
434
|
-
unsigned char* label_copy = (unsigned char*)OPENSSL_malloc(native_label->size());
|
|
427
|
+
unsigned char* label_copy = (unsigned char*)OPENSSL_malloc(label.value()->size());
|
|
435
428
|
if (!label_copy) {
|
|
436
429
|
EVP_PKEY_CTX_free(ctx);
|
|
437
430
|
throw std::runtime_error("Failed to allocate memory for label");
|
|
438
431
|
}
|
|
439
|
-
std::memcpy(label_copy,
|
|
432
|
+
std::memcpy(label_copy, label.value()->data(), label.value()->size());
|
|
440
433
|
|
|
441
|
-
if (EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy,
|
|
434
|
+
if (EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, label_copy, label.value()->size()) <= 0) {
|
|
442
435
|
OPENSSL_free(label_copy);
|
|
443
436
|
EVP_PKEY_CTX_free(ctx);
|
|
444
437
|
throw std::runtime_error("Failed to set OAEP label");
|
|
@@ -446,9 +439,8 @@ std::shared_ptr<ArrayBuffer> HybridRsaCipher::privateDecrypt(const std::shared_p
|
|
|
446
439
|
}
|
|
447
440
|
}
|
|
448
441
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
size_t inlen = native_data->size();
|
|
442
|
+
const unsigned char* in = data->data();
|
|
443
|
+
size_t inlen = data->size();
|
|
452
444
|
|
|
453
445
|
// Both decrypt calls below operate on attacker-controlled ciphertext, so
|
|
454
446
|
// any failure must be surfaced with an opaque, content-independent message.
|
package/cpp/cipher/OCBCipher.cpp
CHANGED
|
@@ -40,12 +40,11 @@ bool OCBCipher::setAuthTag(const std::shared_ptr<ArrayBuffer>& tag) {
|
|
|
40
40
|
if (is_cipher) {
|
|
41
41
|
throw std::runtime_error("setAuthTag can only be called during decryption.");
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
size_t tag_len = native_tag->size();
|
|
43
|
+
size_t tag_len = tag->size();
|
|
45
44
|
if (tag_len < 8 || tag_len > 16) {
|
|
46
45
|
throw std::runtime_error("Invalid OCB tag length");
|
|
47
46
|
}
|
|
48
|
-
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_TAG, tag_len,
|
|
47
|
+
if (EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_TAG, tag_len, tag->data()) != 1) {
|
|
49
48
|
throw std::runtime_error("Failed to set OCB auth tag");
|
|
50
49
|
}
|
|
51
50
|
auth_tag_len = tag_len;
|
|
@@ -29,19 +29,16 @@ XChaCha20Poly1305Cipher::~XChaCha20Poly1305Cipher() {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
void XChaCha20Poly1305Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (native_key->size() != kKeySize) {
|
|
36
|
-
throw std::runtime_error("XChaCha20-Poly1305 key must be 32 bytes, got " + std::to_string(native_key->size()) + " bytes");
|
|
32
|
+
if (cipher_key->size() != kKeySize) {
|
|
33
|
+
throw std::runtime_error("XChaCha20-Poly1305 key must be 32 bytes, got " + std::to_string(cipher_key->size()) + " bytes");
|
|
37
34
|
}
|
|
38
35
|
|
|
39
|
-
if (
|
|
40
|
-
throw std::runtime_error("XChaCha20-Poly1305 nonce must be 24 bytes, got " + std::to_string(
|
|
36
|
+
if (iv->size() != kNonceSize) {
|
|
37
|
+
throw std::runtime_error("XChaCha20-Poly1305 nonce must be 24 bytes, got " + std::to_string(iv->size()) + " bytes");
|
|
41
38
|
}
|
|
42
39
|
|
|
43
|
-
std::memcpy(key_,
|
|
44
|
-
std::memcpy(nonce_,
|
|
40
|
+
std::memcpy(key_, cipher_key->data(), kKeySize);
|
|
41
|
+
std::memcpy(nonce_, iv->data(), kNonceSize);
|
|
45
42
|
|
|
46
43
|
data_buffer_.clear();
|
|
47
44
|
aad_.clear();
|
|
@@ -53,12 +50,11 @@ std::shared_ptr<ArrayBuffer> XChaCha20Poly1305Cipher::update(const std::shared_p
|
|
|
53
50
|
#ifndef BLSALLOC_SODIUM
|
|
54
51
|
throw std::runtime_error("XChaCha20Poly1305Cipher: libsodium must be enabled (BLSALLOC_SODIUM)");
|
|
55
52
|
#else
|
|
56
|
-
|
|
57
|
-
size_t data_len = native_data->size();
|
|
53
|
+
size_t data_len = data->size();
|
|
58
54
|
|
|
59
55
|
size_t old_size = data_buffer_.size();
|
|
60
56
|
data_buffer_.resize(old_size + data_len);
|
|
61
|
-
std::memcpy(data_buffer_.data() + old_size,
|
|
57
|
+
std::memcpy(data_buffer_.data() + old_size, data->data(), data_len);
|
|
62
58
|
|
|
63
59
|
return std::make_shared<NativeArrayBuffer>(nullptr, 0, nullptr);
|
|
64
60
|
#endif
|
|
@@ -114,9 +110,8 @@ bool XChaCha20Poly1305Cipher::setAAD(const std::shared_ptr<ArrayBuffer>& data, s
|
|
|
114
110
|
#ifndef BLSALLOC_SODIUM
|
|
115
111
|
throw std::runtime_error("XChaCha20Poly1305Cipher: libsodium must be enabled (BLSALLOC_SODIUM)");
|
|
116
112
|
#else
|
|
117
|
-
|
|
118
|
-
aad_.
|
|
119
|
-
std::memcpy(aad_.data(), native_aad->data(), native_aad->size());
|
|
113
|
+
aad_.resize(data->size());
|
|
114
|
+
std::memcpy(aad_.data(), data->data(), data->size());
|
|
120
115
|
return true;
|
|
121
116
|
#endif
|
|
122
117
|
}
|
|
@@ -147,12 +142,11 @@ bool XChaCha20Poly1305Cipher::setAuthTag(const std::shared_ptr<ArrayBuffer>& tag
|
|
|
147
142
|
throw std::runtime_error("setAuthTag can only be called during decryption");
|
|
148
143
|
}
|
|
149
144
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
throw std::runtime_error("XChaCha20-Poly1305 tag must be 16 bytes, got " + std::to_string(native_tag->size()) + " bytes");
|
|
145
|
+
if (tag->size() != kTagSize) {
|
|
146
|
+
throw std::runtime_error("XChaCha20-Poly1305 tag must be 16 bytes, got " + std::to_string(tag->size()) + " bytes");
|
|
153
147
|
}
|
|
154
148
|
|
|
155
|
-
std::memcpy(auth_tag_,
|
|
149
|
+
std::memcpy(auth_tag_, tag->data(), kTagSize);
|
|
156
150
|
return true;
|
|
157
151
|
#endif
|
|
158
152
|
}
|
|
@@ -13,23 +13,20 @@ namespace margelo::nitro::crypto {
|
|
|
13
13
|
* Initialize the cipher with a key and a nonce (using iv argument as nonce)
|
|
14
14
|
*/
|
|
15
15
|
void XSalsa20Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) {
|
|
16
|
-
auto native_key = ToNativeArrayBuffer(cipher_key);
|
|
17
|
-
auto native_iv = ToNativeArrayBuffer(iv);
|
|
18
|
-
|
|
19
16
|
// Validate key size
|
|
20
|
-
if (
|
|
17
|
+
if (cipher_key->size() < crypto_stream_KEYBYTES) {
|
|
21
18
|
throw std::runtime_error("XSalsa20 key too short: expected " + std::to_string(crypto_stream_KEYBYTES) + " bytes, got " +
|
|
22
|
-
std::to_string(
|
|
19
|
+
std::to_string(cipher_key->size()) + " bytes.");
|
|
23
20
|
}
|
|
24
21
|
// Validate nonce size
|
|
25
|
-
if (
|
|
22
|
+
if (iv->size() < crypto_stream_NONCEBYTES) {
|
|
26
23
|
throw std::runtime_error("XSalsa20 nonce too short: expected " + std::to_string(crypto_stream_NONCEBYTES) + " bytes, got " +
|
|
27
|
-
std::to_string(
|
|
24
|
+
std::to_string(iv->size()) + " bytes.");
|
|
28
25
|
}
|
|
29
26
|
|
|
30
27
|
// Copy key and nonce data
|
|
31
|
-
std::memcpy(key,
|
|
32
|
-
std::memcpy(nonce,
|
|
28
|
+
std::memcpy(key, cipher_key->data(), crypto_stream_KEYBYTES);
|
|
29
|
+
std::memcpy(nonce, iv->data(), crypto_stream_NONCEBYTES);
|
|
33
30
|
|
|
34
31
|
// Reset streaming state so a re-init'd cipher does not accidentally reuse
|
|
35
32
|
// keystream bytes from a previous session.
|
|
@@ -57,8 +54,7 @@ std::shared_ptr<ArrayBuffer> XSalsa20Cipher::update(const std::shared_ptr<ArrayB
|
|
|
57
54
|
#ifndef BLSALLOC_SODIUM
|
|
58
55
|
throw std::runtime_error("XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined).");
|
|
59
56
|
#else
|
|
60
|
-
|
|
61
|
-
const std::size_t data_size = native_data->size();
|
|
57
|
+
const std::size_t data_size = data->size();
|
|
62
58
|
|
|
63
59
|
if (data_size == 0) {
|
|
64
60
|
return std::make_shared<NativeArrayBuffer>(nullptr, 0, nullptr);
|
|
@@ -66,7 +62,7 @@ std::shared_ptr<ArrayBuffer> XSalsa20Cipher::update(const std::shared_ptr<ArrayB
|
|
|
66
62
|
|
|
67
63
|
// Owning buffer: prevents leaking `output` if we throw on the way out.
|
|
68
64
|
auto output = std::make_unique<uint8_t[]>(data_size);
|
|
69
|
-
const uint8_t* input =
|
|
65
|
+
const uint8_t* input = data->data();
|
|
70
66
|
std::size_t pos = 0;
|
|
71
67
|
|
|
72
68
|
// (1) Drain any unused keystream from the previous update()'s tail block.
|
|
@@ -20,19 +20,16 @@ XSalsa20Poly1305Cipher::~XSalsa20Poly1305Cipher() {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
void XSalsa20Poly1305Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (native_key->size() != kKeySize) {
|
|
27
|
-
throw std::runtime_error("XSalsa20-Poly1305 key must be 32 bytes, got " + std::to_string(native_key->size()) + " bytes");
|
|
23
|
+
if (cipher_key->size() != kKeySize) {
|
|
24
|
+
throw std::runtime_error("XSalsa20-Poly1305 key must be 32 bytes, got " + std::to_string(cipher_key->size()) + " bytes");
|
|
28
25
|
}
|
|
29
26
|
|
|
30
|
-
if (
|
|
31
|
-
throw std::runtime_error("XSalsa20-Poly1305 nonce must be 24 bytes, got " + std::to_string(
|
|
27
|
+
if (iv->size() != kNonceSize) {
|
|
28
|
+
throw std::runtime_error("XSalsa20-Poly1305 nonce must be 24 bytes, got " + std::to_string(iv->size()) + " bytes");
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
std::memcpy(key_,
|
|
35
|
-
std::memcpy(nonce_,
|
|
31
|
+
std::memcpy(key_, cipher_key->data(), kKeySize);
|
|
32
|
+
std::memcpy(nonce_, iv->data(), kNonceSize);
|
|
36
33
|
|
|
37
34
|
data_buffer_.clear();
|
|
38
35
|
is_finalized = false;
|
|
@@ -43,12 +40,11 @@ std::shared_ptr<ArrayBuffer> XSalsa20Poly1305Cipher::update(const std::shared_pt
|
|
|
43
40
|
#ifndef BLSALLOC_SODIUM
|
|
44
41
|
throw std::runtime_error("XSalsa20Poly1305Cipher: libsodium must be enabled (BLSALLOC_SODIUM)");
|
|
45
42
|
#else
|
|
46
|
-
|
|
47
|
-
size_t data_len = native_data->size();
|
|
43
|
+
size_t data_len = data->size();
|
|
48
44
|
|
|
49
45
|
size_t old_size = data_buffer_.size();
|
|
50
46
|
data_buffer_.resize(old_size + data_len);
|
|
51
|
-
std::memcpy(data_buffer_.data() + old_size,
|
|
47
|
+
std::memcpy(data_buffer_.data() + old_size, data->data(), data_len);
|
|
52
48
|
|
|
53
49
|
return std::make_shared<NativeArrayBuffer>(nullptr, 0, nullptr);
|
|
54
50
|
#endif
|
|
@@ -126,12 +122,11 @@ bool XSalsa20Poly1305Cipher::setAuthTag(const std::shared_ptr<ArrayBuffer>& tag)
|
|
|
126
122
|
throw std::runtime_error("setAuthTag can only be called during decryption");
|
|
127
123
|
}
|
|
128
124
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
throw std::runtime_error("XSalsa20-Poly1305 tag must be 16 bytes, got " + std::to_string(native_tag->size()) + " bytes");
|
|
125
|
+
if (tag->size() != kTagSize) {
|
|
126
|
+
throw std::runtime_error("XSalsa20-Poly1305 tag must be 16 bytes, got " + std::to_string(tag->size()) + " bytes");
|
|
132
127
|
}
|
|
133
128
|
|
|
134
|
-
std::memcpy(auth_tag_,
|
|
129
|
+
std::memcpy(auth_tag_, tag->data(), kTagSize);
|
|
135
130
|
return true;
|
|
136
131
|
#endif
|
|
137
132
|
}
|