Skip to content

Commit 7f87a94

Browse files
committed
revert
1 parent e2ab217 commit 7f87a94

2 files changed

Lines changed: 92 additions & 92 deletions

File tree

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
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
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
#pragma once
2-
3-
#if BLSALLOC_SODIUM
4-
#include "sodium.h"
5-
#else
6-
// Define XSalsa20 constants when sodium is disabled (for compilation purposes)
7-
#define crypto_stream_KEYBYTES 32 // XSalsa20 key size (32 bytes)
8-
#define crypto_stream_NONCEBYTES 24 // XSalsa20 nonce size (24 bytes)
9-
#endif
10-
11-
#include "HybridCipher.hpp"
12-
#include "NitroModules/ArrayBuffer.hpp"
13-
14-
namespace margelo::nitro::crypto {
15-
16-
class XSalsa20Cipher : public HybridCipher {
17-
public:
18-
XSalsa20Cipher() : HybridObject(TAG) {}
19-
~XSalsa20Cipher() {
20-
// Let parent destructor free the context
21-
ctx = nullptr;
22-
}
23-
24-
void init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) override;
25-
std::shared_ptr<ArrayBuffer> update(const std::shared_ptr<ArrayBuffer>& data) override;
26-
std::shared_ptr<ArrayBuffer> final() override;
27-
28-
private:
29-
uint8_t key[crypto_stream_KEYBYTES];
30-
uint8_t nonce[crypto_stream_NONCEBYTES];
31-
};
32-
1+
#pragma once
2+
3+
#if BLSALLOC_SODIUM
4+
#include "sodium.h"
5+
#else
6+
// Define XSalsa20 constants when sodium is disabled (for compilation purposes)
7+
#define crypto_stream_KEYBYTES 32 // XSalsa20 key size (32 bytes)
8+
#define crypto_stream_NONCEBYTES 24 // XSalsa20 nonce size (24 bytes)
9+
#endif
10+
11+
#include "HybridCipher.hpp"
12+
#include "NitroModules/ArrayBuffer.hpp"
13+
14+
namespace margelo::nitro::crypto {
15+
16+
class XSalsa20Cipher : public HybridCipher {
17+
public:
18+
XSalsa20Cipher() : HybridObject(TAG) {}
19+
~XSalsa20Cipher() {
20+
// Let parent destructor free the context
21+
ctx = nullptr;
22+
}
23+
24+
void init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) override;
25+
std::shared_ptr<ArrayBuffer> update(const std::shared_ptr<ArrayBuffer>& data) override;
26+
std::shared_ptr<ArrayBuffer> final() override;
27+
28+
private:
29+
uint8_t key[crypto_stream_KEYBYTES];
30+
uint8_t nonce[crypto_stream_NONCEBYTES];
31+
};
32+
3333
} // namespace margelo::nitro::crypto

0 commit comments

Comments
 (0)