1- #include < cstring> // For std::memcpy
2- #include < stdexcept> // For std::runtime_error
3-
4- #include " NitroModules/ArrayBuffer.hpp"
5- #include " Utils.hpp"
6- #include " XSalsa20Cipher.hpp"
7-
8- namespace margelo ::nitro::crypto {
9-
10- /* *
11- * Initialize the cipher with a key and a nonce (using iv argument as nonce)
12- */
13- void XSalsa20Cipher::init (const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) {
14- auto native_key = ToNativeArrayBuffer (cipher_key);
15- auto native_iv = ToNativeArrayBuffer (iv);
16-
17- // Validate key size
18- if (native_key->size () < crypto_stream_KEYBYTES) {
19- throw std::runtime_error (" XSalsa20 key too short: expected " + std::to_string (crypto_stream_KEYBYTES) + " bytes, got " +
20- std::to_string (native_key->size ()) + " bytes." );
21- }
22- // Validate nonce size
23- if (native_iv->size () < crypto_stream_NONCEBYTES) {
24- throw std::runtime_error (" XSalsa20 nonce too short: expected " + std::to_string (crypto_stream_NONCEBYTES) + " bytes, got " +
25- std::to_string (native_iv->size ()) + " bytes." );
26- }
27-
28- // Copy key and nonce data
29- std::memcpy (key, native_key->data (), crypto_stream_KEYBYTES);
30- std::memcpy (nonce, native_iv->data (), crypto_stream_NONCEBYTES);
31- }
32-
33- /* *
34- * xsalsa20 call to sodium implementation
35- */
36- std::shared_ptr<ArrayBuffer> XSalsa20Cipher::update (const std::shared_ptr<ArrayBuffer>& data) {
37- #ifndef BLSALLOC_SODIUM
38- throw std::runtime_error (" XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined)." );
39- #else
40- auto native_data = ToNativeArrayBuffer (data);
41- auto output = new uint8_t [native_data->size ()];
42- int result = crypto_stream_xor (output, native_data->data (), native_data->size (), nonce, key);
43- if (result != 0 ) {
44- throw std::runtime_error (" XSalsa20Cipher: Failed to update" );
45- }
46- return std::make_shared<NativeArrayBuffer>(output, native_data->size (), [=]() { delete[] output; });
47- #endif
48- }
49-
50- /* *
51- * xsalsa20 does not have a final step, returns empty buffer
52- */
53- std::shared_ptr<ArrayBuffer> XSalsa20Cipher::final () {
54- #ifndef BLSALLOC_SODIUM
55- throw std::runtime_error (" XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined)." );
56- #else
57- return std::make_shared<NativeArrayBuffer>(nullptr , 0 , nullptr );
58- #endif
59- }
60-
1+ #include < cstring> // For std::memcpy
2+ #include < stdexcept> // For std::runtime_error
3+
4+ #include " NitroModules/ArrayBuffer.hpp"
5+ #include " Utils.hpp"
6+ #include " XSalsa20Cipher.hpp"
7+
8+ namespace margelo ::nitro::crypto {
9+
10+ /* *
11+ * Initialize the cipher with a key and a nonce (using iv argument as nonce)
12+ */
13+ void XSalsa20Cipher::init (const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) {
14+ auto native_key = ToNativeArrayBuffer (cipher_key);
15+ auto native_iv = ToNativeArrayBuffer (iv);
16+
17+ // Validate key size
18+ if (native_key->size () < crypto_stream_KEYBYTES) {
19+ throw std::runtime_error (" XSalsa20 key too short: expected " + std::to_string (crypto_stream_KEYBYTES) + " bytes, got " +
20+ std::to_string (native_key->size ()) + " bytes." );
21+ }
22+ // Validate nonce size
23+ if (native_iv->size () < crypto_stream_NONCEBYTES) {
24+ throw std::runtime_error (" XSalsa20 nonce too short: expected " + std::to_string (crypto_stream_NONCEBYTES) + " bytes, got " +
25+ std::to_string (native_iv->size ()) + " bytes." );
26+ }
27+
28+ // Copy key and nonce data
29+ std::memcpy (key, native_key->data (), crypto_stream_KEYBYTES);
30+ std::memcpy (nonce, native_iv->data (), crypto_stream_NONCEBYTES);
31+ }
32+
33+ /* *
34+ * xsalsa20 call to sodium implementation
35+ */
36+ std::shared_ptr<ArrayBuffer> XSalsa20Cipher::update (const std::shared_ptr<ArrayBuffer>& data) {
37+ #ifndef BLSALLOC_SODIUM
38+ throw std::runtime_error (" XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined)." );
39+ #else
40+ auto native_data = ToNativeArrayBuffer (data);
41+ auto output = new uint8_t [native_data->size ()];
42+ int result = crypto_stream_xor (output, native_data->data (), native_data->size (), nonce, key);
43+ if (result != 0 ) {
44+ throw std::runtime_error (" XSalsa20Cipher: Failed to update" );
45+ }
46+ return std::make_shared<NativeArrayBuffer>(output, native_data->size (), [=]() { delete[] output; });
47+ #endif
48+ }
49+
50+ /* *
51+ * xsalsa20 does not have a final step, returns empty buffer
52+ */
53+ std::shared_ptr<ArrayBuffer> XSalsa20Cipher::final () {
54+ #ifndef BLSALLOC_SODIUM
55+ throw std::runtime_error (" XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined)." );
56+ #else
57+ return std::make_shared<NativeArrayBuffer>(nullptr , 0 , nullptr );
58+ #endif
59+ }
60+
6161} // namespace margelo::nitro::crypto
0 commit comments