-
Notifications
You must be signed in to change notification settings - Fork 105
feat: add xsalsa20
cipher from libsodium
#689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4467484
feat: add xsalsa20 cipher from libsodium
boorad 9ef11ad
fix: add libsodium src temporarily
boorad 47c946a
fix: proper dependabot config?
boorad 1b6309e
fix: merge conflict resolution undo
boorad 2cf49e9
chore: cpp lint
boorad 20ec9eb
fix: use swift-sodium dep, remove src
boorad 0de1d74
fix: build libsodium pod from source, fml
boorad 504fb0c
fix: more libsodium pod build fixes
boorad 917b216
fix: xsalsa needs to use crypto_stream_xor function for encrypt/decry…
boorad fcb8545
fix: format
boorad e6c1fda
chore: fiddling with clang/IDE
boorad 4ccf959
chore: format
boorad cf069f8
feat: add xsalsa20 benchmarks
boorad 108c193
chore: format
boorad dc67e9e
fix: new values each benchmark run
boorad bd0233c
fix: android dependency, build & includes
boorad 1a3845d
fix: ugh, clang fmt
boorad 56567b9
fix: RN upgrade stragglers
boorad 875c490
fix: remaining Android build issues
boorad d30ce4f
chore: test suite result images
boorad 25f5eb4
chore: GHA
boorad 922c748
fix: cleanup commented code, bump libsodium ndkport
boorad e7d5410
fix: PR feedback
boorad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/react-native-quick-crypto/cpp/cipher/XSalsa20Cipher.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "XSalsa20Cipher.hpp" | ||
#include <cstring> // For std::memcpy | ||
#include <stdexcept> // For std::runtime_error | ||
#include <string> // For std::to_string | ||
|
||
namespace margelo::nitro::crypto { | ||
|
||
/** | ||
* Initialize the cipher with a key and a nonce (using iv argument as nonce) | ||
*/ | ||
void XSalsa20Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) { | ||
auto native_key = ToNativeArrayBuffer(cipher_key); | ||
auto native_iv = ToNativeArrayBuffer(iv); | ||
|
||
// Validate key size | ||
if (native_key->size() < crypto_stream_KEYBYTES) { | ||
throw std::runtime_error("XSalsa20 key too short: expected " + std::to_string(crypto_stream_KEYBYTES) + " bytes, got " + | ||
std::to_string(native_key->size()) + " bytes."); | ||
} | ||
// Validate nonce size | ||
if (native_iv->size() < crypto_stream_NONCEBYTES) { | ||
throw std::runtime_error("XSalsa20 nonce too short: expected " + std::to_string(crypto_stream_NONCEBYTES) + " bytes, got " + | ||
std::to_string(native_iv->size()) + " bytes."); | ||
} | ||
|
||
// Copy key and nonce data | ||
std::memcpy(key, native_key->data(), crypto_stream_KEYBYTES); | ||
std::memcpy(nonce, native_iv->data(), crypto_stream_NONCEBYTES); | ||
} | ||
|
||
/** | ||
* xsalsa20 call to sodium implementation | ||
*/ | ||
std::shared_ptr<ArrayBuffer> XSalsa20Cipher::update(const std::shared_ptr<ArrayBuffer>& data) { | ||
auto native_data = ToNativeArrayBuffer(data); | ||
int result = crypto_stream(native_data->data(), native_data->size(), nonce, key); | ||
if (result != 0) { | ||
throw std::runtime_error("XSalsa20Cipher: Failed to update"); | ||
} | ||
return std::make_shared<NativeArrayBuffer>(native_data->data(), native_data->size(), nullptr); | ||
} | ||
|
||
/** | ||
* xsalsa20 does not have a final step, returns empty buffer | ||
*/ | ||
std::shared_ptr<ArrayBuffer> XSalsa20Cipher::final() { | ||
return std::make_shared<NativeArrayBuffer>(nullptr, 0, nullptr); | ||
} | ||
|
||
} // namespace margelo::nitro::crypto |
27 changes: 27 additions & 0 deletions
27
packages/react-native-quick-crypto/cpp/cipher/XSalsa20Cipher.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include "sodium.h" | ||
|
||
#include "HybridCipher.hpp" | ||
#include "Utils.hpp" | ||
|
||
namespace margelo::nitro::crypto { | ||
|
||
class XSalsa20Cipher : public HybridCipher { | ||
public: | ||
XSalsa20Cipher() : HybridObject(TAG) {} | ||
~XSalsa20Cipher() { | ||
// Let parent destructor free the context | ||
ctx = nullptr; | ||
} | ||
|
||
void init(const std::shared_ptr<ArrayBuffer> cipher_key, const std::shared_ptr<ArrayBuffer> iv) override; | ||
std::shared_ptr<ArrayBuffer> update(const std::shared_ptr<ArrayBuffer>& data) override; | ||
std::shared_ptr<ArrayBuffer> final() override; | ||
|
||
private: | ||
uint8_t key[crypto_stream_KEYBYTES]; | ||
boorad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint8_t nonce[crypto_stream_NONCEBYTES]; | ||
boorad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
} // namespace margelo::nitro::crypto |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that deallocate fine? Not sure if this calls
delete[]
at some point but maybe it's fine.