forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 97
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
feat(crypto): mnemonic generation/encryption/decryption/storage #2014
Merged
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
03c0e59
wip: Implement mnemonic generation, encryption, decryption, and stora…
shamardy 714e72d
Wasm encryption/decryption and wallets mnemonic files storage
shamardy d0e3f44
rollback some Cargo.lock changes that were done due to the use of ope…
shamardy 2e50871
rollback some adex-cli Cargo.lock changes to be the same as cargo.loc…
shamardy eb04b12
Fix review notes: use consts where applicable, refactor MnemonicsTabl…
shamardy 825ecdb
Fix review notes: add EncryptionAlgorithm enum
shamardy 8365567
Fix review notes: fix on_upgrade_needed fn for mnemonics table, make…
shamardy 69117b8
Fix review notes: fix doc comments of read_encrypted_passphrase
shamardy 3bdb2d5
allow importing of an encrypted passphrase
shamardy 2df7834
refactor handle_passphrase_logic
shamardy 839beb6
make global wallets db part of ctx
shamardy 4b7c014
fix wasm clippy
shamardy 5a65a48
add get_mnemonic rpc
shamardy 6d5e271
add SLIP-0021 keys derivation
shamardy c37316e
add encrypt.rs for encryption of general data regardless of key deriv…
shamardy 37f16fb
add decrypt.rs for decryption of general data regardless of key deriv…
shamardy 9ad3358
Introduce SLIP-0021 encryption and decryption in slip21.rs
shamardy 66085b0
Merge remote-tracking branch 'origin/dev' into feat-seed-gen
shamardy 4f666c1
Review Fixes:
shamardy ba0fb59
Review fixes:
shamardy eb26362
Review fixes: Update base64 crate to 0.21.2 and replace deprecated fu…
shamardy 47cdc19
Merge remote-tracking branch 'origin/dev' into feat-seed-gen
shamardy 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,61 @@ | ||
use crate::EncryptedData; | ||
use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, KeyIvInit}; | ||
use aes::Aes256; | ||
use derive_more::Display; | ||
use hmac::{Hmac, Mac}; | ||
use mm2_err_handle::prelude::*; | ||
use sha2::Sha256; | ||
|
||
type Aes256CbcDec = cbc::Decryptor<Aes256>; | ||
|
||
#[derive(Debug, Display, PartialEq)] | ||
pub enum DecryptionError { | ||
#[display(fmt = "AES cipher error: {}", _0)] | ||
AESCipherError(String), | ||
#[display(fmt = "Error decoding string: {}", _0)] | ||
DecodeError(String), | ||
#[display(fmt = "HMAC error: {}", _0)] | ||
HMACError(String), | ||
Internal(String), | ||
} | ||
|
||
impl From<base64::DecodeError> for DecryptionError { | ||
fn from(e: base64::DecodeError) -> Self { DecryptionError::DecodeError(e.to_string()) } | ||
} | ||
|
||
/// Decrypts the provided encrypted data using AES-256-CBC decryption and HMAC for integrity check. | ||
/// | ||
/// This function performs several operations: | ||
/// - It decodes the Base64-encoded values of the IV, ciphertext, and HMAC tag from the `EncryptedData`. | ||
/// - It verifies the HMAC tag before decrypting to ensure the integrity of the data. | ||
/// - It creates an AES-256-CBC cipher instance and decrypts the ciphertext with the provided key and the decoded IV. | ||
/// | ||
/// # Returns | ||
/// `MmResult<Vec<u8>, DecryptionError>` - The result is either a byte vector containing the decrypted data, | ||
/// or a [`DecryptionError`] in case of failure. | ||
/// | ||
/// # Errors | ||
/// This function can return various errors related to Base64 decoding, HMAC verification, and AES decryption. | ||
pub fn decrypt_data( | ||
encrypted_data: &EncryptedData, | ||
key_aes: &[u8; 32], | ||
key_hmac: &[u8; 32], | ||
) -> MmResult<Vec<u8>, DecryptionError> { | ||
// Decode the Base64-encoded values | ||
let iv = base64::decode(&encrypted_data.iv)?; | ||
let mut ciphertext = base64::decode(&encrypted_data.ciphertext)?; | ||
let tag = base64::decode(&encrypted_data.tag)?; | ||
|
||
// Verify HMAC tag before decrypting | ||
let mut mac = Hmac::<Sha256>::new_from_slice(key_hmac).map_to_mm(|e| DecryptionError::Internal(e.to_string()))?; | ||
mac.update(&ciphertext); | ||
mac.update(&iv); | ||
mac.verify_slice(&tag) | ||
.map_to_mm(|e| DecryptionError::HMACError(e.to_string()))?; | ||
|
||
// Decrypt the ciphertext and return the result | ||
Aes256CbcDec::new(key_aes.into(), iv.as_slice().into()) | ||
.decrypt_padded_mut::<Pkcs7>(&mut ciphertext) | ||
.map_to_mm(|e| DecryptionError::AESCipherError(e.to_string())) | ||
.map(|plaintext| plaintext.to_vec()) | ||
} |
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.
Q: Can we use version
0.7.5
instead of duplicating this dependency?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.
I wanted to use the latest version for such an important encryption, after this #1957 is merged I can try to update
0.7.5
to0.8.3
instead inlibrustzcash