forked from massalabs/massa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2710: Wallet encryption r=Eitu33 a=Eitu33 Co-authored-by: Thomas Plisson <[email protected]>
- Loading branch information
Showing
12 changed files
with
287 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "massa_cipher" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
displaydoc = "0.2" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
serde_qs = "0.8" | ||
thiserror = "1.0" | ||
aes-gcm-siv = "0.10" | ||
rand = "0.8" | ||
|
||
# custom modules | ||
massa_hash = { path = "../massa-hash" } | ||
massa_models = { path = "../massa-models" } | ||
massa_signature = { path = "../massa-signature" } |
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,6 @@ | ||
// Copyright (c) 2022 MASSA LABS <[email protected]> | ||
|
||
/// Nonce size | ||
/// | ||
/// Read `lib.rs` module documentation for more information. | ||
pub const NONCE_SIZE: usize = 12; |
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,35 @@ | ||
// Copyright (c) 2022 MASSA LABS <[email protected]> | ||
|
||
use aes_gcm_siv::aead::{Aead, NewAead}; | ||
use aes_gcm_siv::{Aes256GcmSiv, Key, Nonce}; | ||
use massa_hash::Hash; | ||
|
||
use crate::constants::NONCE_SIZE; | ||
use crate::error::CipherError; | ||
|
||
/// Decryption function using AES-GCM-SIV cipher. | ||
/// | ||
/// Read `lib.rs` module documentation for more information. | ||
pub fn decrypt(password: &str, data: &[u8]) -> Result<Vec<u8>, CipherError> { | ||
let cipher = Aes256GcmSiv::new(Key::from_slice( | ||
Hash::compute_from(password.as_bytes()).to_bytes(), | ||
)); | ||
let nonce = Nonce::from_slice(data.get(..NONCE_SIZE).ok_or_else(|| { | ||
CipherError::DecryptionError( | ||
"wallet file truncated: nonce missing or incomplete".to_string(), | ||
) | ||
})?); | ||
let decrypted_bytes = cipher | ||
.decrypt( | ||
nonce, | ||
data.get(NONCE_SIZE..).ok_or_else(|| { | ||
CipherError::DecryptionError( | ||
"wallet file truncated: encrypted data missing or incomplete".to_string(), | ||
) | ||
})?, | ||
) | ||
.map_err(|_| { | ||
CipherError::DecryptionError("wrong password or corrupted data".to_string()) | ||
})?; | ||
Ok(decrypted_bytes) | ||
} |
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,27 @@ | ||
// Copyright (c) 2022 MASSA LABS <[email protected]> | ||
|
||
use aes_gcm_siv::aead::{Aead, NewAead}; | ||
use aes_gcm_siv::{Aes256GcmSiv, Key, Nonce}; | ||
use massa_hash::Hash; | ||
use rand::{thread_rng, RngCore}; | ||
|
||
use crate::constants::NONCE_SIZE; | ||
use crate::error::CipherError; | ||
|
||
/// Encryption function using AES-GCM-SIV cipher. | ||
/// | ||
/// Read `lib.rs` module documentation for more information. | ||
pub fn encrypt(password: &str, data: &[u8]) -> Result<Vec<u8>, CipherError> { | ||
let cipher = Aes256GcmSiv::new(Key::from_slice( | ||
Hash::compute_from(password.as_bytes()).to_bytes(), | ||
)); | ||
let mut nonce_bytes = [0u8; NONCE_SIZE]; | ||
thread_rng().fill_bytes(&mut nonce_bytes); | ||
let nonce = Nonce::from_slice(&nonce_bytes); | ||
let encrypted_bytes = cipher | ||
.encrypt(nonce, data.as_ref()) | ||
.map_err(|e| CipherError::EncryptionError(e.to_string()))?; | ||
let mut content = nonce_bytes.to_vec(); | ||
content.extend(encrypted_bytes); | ||
Ok(content) | ||
} |
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,13 @@ | ||
// Copyright (c) 2022 MASSA LABS <[email protected]> | ||
|
||
use displaydoc::Display; | ||
use thiserror::Error; | ||
|
||
/// Cipher error | ||
#[derive(Display, Error, Debug)] | ||
pub enum CipherError { | ||
/// Encryption error: {0} | ||
EncryptionError(String), | ||
/// Decryption error: {0} | ||
DecryptionError(String), | ||
} |
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,24 @@ | ||
// Copyright (c) 2022 MASSA LABS <[email protected]> | ||
|
||
//! Cipher crate | ||
//! | ||
//! `massa-cipher` uses AES-GCM-SIV ([RFC 8452](https://datatracker.ietf.org/doc/html/rfc8452)). | ||
//! | ||
//! AES-GCM-SIV is a state-of-the-art high-performance Authenticated Encryption with Associated Data (AEAD) | ||
//! cipher which also provides nonce reuse misuse resistance. | ||
//! Suitable as a general purpose symmetric encryption cipher, AES-GCM-SIV also removes many of the sharp edges of AES-GCM. | ||
//! | ||
//! A nonce is a single-use value which enables securely encrypting multiple messages under the same key. | ||
//! Nonces need not be random: a counter can be used, so long as the values are never repeated under the same key. | ||
//! | ||
//! No complete security audits of the crate we use has been performed. | ||
//! But some of this crate's dependencies were audited by by NCC Group as part of an audit of the AES-GCM crate | ||
mod constants; | ||
mod decrypt; | ||
mod encrypt; | ||
mod error; | ||
|
||
pub use decrypt::decrypt; | ||
pub use encrypt::encrypt; | ||
pub use error::CipherError; |
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
Oops, something went wrong.