diff --git a/Cargo.toml b/Cargo.toml index ed2274e6f1f04f17472afe491f269faa98c03e69..2e2cf66ac8b6d7be6173f44632f4df8565837875 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,8 +29,8 @@ zeroize = { version = "1.1.0", features = ["zeroize_derive"] } bincode = "1.2.0" [features] -default = ["dewip", "ser"] +default = ["dewif", "ser"] aes256 = ["aes"] -dewip = ["aes256", "arrayvec"] +dewif = ["aes256", "arrayvec"] ser = ["serde"] diff --git a/src/dewip.rs b/src/dewif.rs similarity index 85% rename from src/dewip.rs rename to src/dewif.rs index 74babd59b015604f993ba8f0cdf9771cecc1c631..96c61ed546999bb480e65d920c1c4928e32deff5 100644 --- a/src/dewip.rs +++ b/src/dewif.rs @@ -13,12 +13,12 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -//! Handle [DEWIP](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) format +//! Handle [DEWIF](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) format //! //! # Write ed25519 key-pair in DEWIF file //! //! ``` -//! use dup_crypto::dewip::write_dewif_v1_content; +//! use dup_crypto::dewif::write_dewif_v1_content; //! use dup_crypto::keys::ed25519::{KeyPairFromSaltedPasswordGenerator, SaltedPassword}; //! //! // Get user credentials (from cli prompt or gui) @@ -42,18 +42,18 @@ //! # Read DEWIF file //! //! ``` -//! use dup_crypto::dewip::read_dewip_file_content; +//! use dup_crypto::dewif::read_dewif_file_content; //! use dup_crypto::keys::{KeyPair, Signator}; //! -//! // Get DEWIP file content (Usually from disk) -//! let dewip_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA="; +//! // Get DEWIF file content (Usually from disk) +//! let dewif_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA="; //! //! // Get user passphrase for DEWIF decryption (from cli prompt or gui) //! let encryption_passphrase = "toto titi tata"; //! -//! // Read DEWIP file content +//! // Read DEWIF file content //! // If the file content is correct, we get a key-pair iterator. -//! let mut key_pair_iter = read_dewip_file_content(dewip_file_content, encryption_passphrase) +//! let mut key_pair_iter = read_dewif_file_content(dewif_file_content, encryption_passphrase) //! .expect("invalid DEWIF file.") //! .into_iter(); //! @@ -85,7 +85,7 @@ mod read; mod write; -pub use read::{read_dewip_file_content, DewipReadError}; +pub use read::{read_dewif_file_content, DewifReadError}; pub use write::{write_dewif_v1_content, write_dewif_v2_content}; use crate::hashs::Hash; @@ -132,13 +132,13 @@ mod tests { use crate::seeds::Seed32; #[test] - fn dewip_v1() { + fn dewif_v1() { let written_keypair = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let dewif_content = write_dewif_v1_content(&written_keypair, "toto"); - let mut keypairs_iter = read_dewip_file_content(&dewif_content, "toto") - .expect("dewip content must be readed successfully") + let mut keypairs_iter = read_dewif_file_content(&dewif_content, "toto") + .expect("dewif content must be readed successfully") .into_iter(); let keypair_read = keypairs_iter.next().expect("Must read one keypair"); @@ -146,7 +146,7 @@ mod tests { } #[test] - fn dewip_v1_corrupted() -> Result<(), ()> { + fn dewif_v1_corrupted() -> Result<(), ()> { let written_keypair = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let mut dewif_content = write_dewif_v1_content(&written_keypair, "toto"); @@ -155,8 +155,8 @@ mod tests { let dewif_bytes_mut = unsafe { dewif_content.as_bytes_mut() }; dewif_bytes_mut[13] = 0x52; - if let Err(DewipReadError::CorruptedContent) = - read_dewip_file_content(&dewif_content, "toto") + if let Err(DewifReadError::CorruptedContent) = + read_dewif_file_content(&dewif_content, "toto") { Ok(()) } else { @@ -165,14 +165,14 @@ mod tests { } #[test] - fn dewip_v2() { + fn dewif_v2() { let written_keypair1 = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let written_keypair2 = KeyPairFromSeed32Generator::generate(Seed32::new([1u8; 32])); let dewif_content = write_dewif_v2_content(&written_keypair1, &written_keypair2, "toto"); - let mut keypairs_iter = read_dewip_file_content(&dewif_content, "toto") - .expect("dewip content must be readed successfully") + let mut keypairs_iter = read_dewif_file_content(&dewif_content, "toto") + .expect("dewif content must be readed successfully") .into_iter(); let keypair1_read = keypairs_iter.next().expect("Must read one keypair"); let keypair2_read = keypairs_iter.next().expect("Must read one keypair"); @@ -182,7 +182,7 @@ mod tests { } #[test] - fn dewip_v2_corrupted() -> Result<(), ()> { + fn dewif_v2_corrupted() -> Result<(), ()> { let written_keypair1 = KeyPairFromSeed32Generator::generate(Seed32::new([0u8; 32])); let written_keypair2 = KeyPairFromSeed32Generator::generate(Seed32::new([1u8; 32])); @@ -193,8 +193,8 @@ mod tests { let dewif_bytes_mut = unsafe { dewif_content.as_bytes_mut() }; dewif_bytes_mut[13] = 0x52; - if let Err(DewipReadError::CorruptedContent) = - read_dewip_file_content(&dewif_content, "toto") + if let Err(DewifReadError::CorruptedContent) = + read_dewif_file_content(&dewif_content, "toto") { Ok(()) } else { diff --git a/src/dewip/read.rs b/src/dewif/read.rs similarity index 79% rename from src/dewip/read.rs rename to src/dewif/read.rs index 6f190a67bad62134f351c6c1067a5f0ad903c2b2..861a52758138b8aca3218a0ebbf0fc408579d28f 100644 --- a/src/dewip/read.rs +++ b/src/dewif/read.rs @@ -13,7 +13,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -//! Read [DEWIP](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content +//! Read [DEWIF](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content use crate::keys::ed25519::{KeyPairFromSeed32Generator, PublicKey, PUBKEY_SIZE_IN_BYTES}; use crate::keys::KeyPairEnum; @@ -25,11 +25,11 @@ use thiserror::Error; const MAX_KEYPAIRS_COUNT: usize = 2; -/// Error when try to read DEWIP file content +/// Error when try to read DEWIF file content #[derive(Clone, Debug, Error)] -pub enum DewipReadError { - /// DEWIP file content is corrupted - #[error("DEWIP file content is corrupted")] +pub enum DewifReadError { + /// DEWIF file content is corrupted + #[error("DEWIF file content is corrupted")] CorruptedContent, /// Invalid base 64 string #[error("Invalid base 64 string: {0}")] @@ -51,15 +51,15 @@ pub enum DewipReadError { }, } -/// read dewip file content with user passphrase -pub fn read_dewip_file_content( +/// read dewif file content with user passphrase +pub fn read_dewif_file_content( file_content: &str, passphrase: &str, -) -> Result<impl IntoIterator<Item = KeyPairEnum>, DewipReadError> { - let mut bytes = base64::decode(file_content).map_err(DewipReadError::InvalidBase64Str)?; +) -> Result<impl IntoIterator<Item = KeyPairEnum>, DewifReadError> { + let mut bytes = base64::decode(file_content).map_err(DewifReadError::InvalidBase64Str)?; if bytes.len() < 4 { - return Err(DewipReadError::TooShortContent); + return Err(DewifReadError::TooShortContent); } let version = byteorder::BigEndian::read_u32(&bytes[0..4]); @@ -67,20 +67,20 @@ pub fn read_dewip_file_content( match version { 1 => Ok({ let mut array_keypairs = ArrayVec::new(); - array_keypairs.push(read_dewip_v1(&mut bytes[4..], passphrase)?); + array_keypairs.push(read_dewif_v1(&mut bytes[4..], passphrase)?); array_keypairs }), - 2 => read_dewip_v2(&mut bytes[4..], passphrase), - other_version => Err(DewipReadError::UnsupportedVersion { + 2 => read_dewif_v2(&mut bytes[4..], passphrase), + other_version => Err(DewifReadError::UnsupportedVersion { actual: other_version, }), } } -fn read_dewip_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, DewipReadError> { +fn read_dewif_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, DewifReadError> { match bytes.len() { - len if len < super::V1_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooShortContent), - len if len > super::V1_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooLongContent), + len if len < super::V1_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooShortContent), + len if len > super::V1_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooLongContent), _ => (), } @@ -92,15 +92,15 @@ fn read_dewip_v1(bytes: &mut [u8], passphrase: &str) -> Result<KeyPairEnum, Dewi bytes_to_checked_keypair(bytes) } -fn read_dewip_v2( +fn read_dewif_v2( bytes: &mut [u8], passphrase: &str, -) -> Result<ArrayVec<[KeyPairEnum; MAX_KEYPAIRS_COUNT]>, DewipReadError> { +) -> Result<ArrayVec<[KeyPairEnum; MAX_KEYPAIRS_COUNT]>, DewifReadError> { let mut array_keypairs = ArrayVec::new(); match bytes.len() { - len if len < super::V2_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooShortContent), - len if len > super::V2_ENCRYPTED_BYTES_LEN => return Err(DewipReadError::TooLongContent), + len if len < super::V2_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooShortContent), + len if len > super::V2_ENCRYPTED_BYTES_LEN => return Err(DewifReadError::TooLongContent), _ => (), } @@ -114,7 +114,7 @@ fn read_dewip_v2( Ok(array_keypairs) } -fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewipReadError> { +fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewifReadError> { // Wrap bytes into Seed32 and PublicKey let seed = Seed32::new( (&bytes[..PUBKEY_SIZE_IN_BYTES]) @@ -128,7 +128,7 @@ fn bytes_to_checked_keypair(bytes: &[u8]) -> Result<KeyPairEnum, DewipReadError> // Check pubkey if keypair.pubkey() != expected_pubkey { - Err(DewipReadError::CorruptedContent) + Err(DewifReadError::CorruptedContent) } else { Ok(KeyPairEnum::Ed25519(keypair)) } @@ -141,7 +141,7 @@ mod tests { #[test] fn read_unsupported_version() -> Result<(), ()> { - if let Err(DewipReadError::UnsupportedVersion { .. }) = read_dewip_file_content( + if let Err(DewifReadError::UnsupportedVersion { .. }) = read_dewif_file_content( "ABAAAfKjMzOFfhwgypF3mAx0QDXyozMzhX4cIMqRd5gMdEA1WZwQjCR49iZDK2QhYfdTbPz9AGB01edt4iRSzdTp3c4=", "toto" ) { @@ -153,7 +153,7 @@ mod tests { #[test] fn read_too_short_content() -> Result<(), ()> { - if let Err(DewipReadError::TooShortContent) = read_dewip_file_content("AAA", "toto") { + if let Err(DewifReadError::TooShortContent) = read_dewif_file_content("AAA", "toto") { Ok(()) } else { panic!("Read must be fail with error TooShortContent.") @@ -164,15 +164,15 @@ mod tests { fn tmp() { use crate::keys::{KeyPair, Signator}; - // Get DEWIP file content (Usually from disk) - let dewip_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA="; + // Get DEWIF file content (Usually from disk) + let dewif_file_content = "AAAAATHfJ3vTvEPcXm22NwhJtnNdGuSjikpSYIMgX96Z9xVT0y8GoIlBL1HaxaWpu0jVDfuwtCGSP9bu2pj6HGbuYVA="; // Get user passphrase for DEWIF decryption (from cli prompt or gui) let encryption_passphrase = "toto titi tata"; - // Read DEWIP file content + // Read DEWIF file content // If the file content is correct, we get a key-pair iterator. - let mut key_pair_iter = read_dewip_file_content(dewip_file_content, encryption_passphrase) + let mut key_pair_iter = read_dewif_file_content(dewif_file_content, encryption_passphrase) .expect("invalid DEWIF file.") .into_iter(); diff --git a/src/dewip/write.rs b/src/dewif/write.rs similarity index 94% rename from src/dewip/write.rs rename to src/dewif/write.rs index 4ce1408021bb602b4ea21382a50100f9ebf16ae4..f009f37e968b53d299e570d4249d178fcc5692f5 100644 --- a/src/dewip/write.rs +++ b/src/dewif/write.rs @@ -13,13 +13,13 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -//! Write [DEWIP](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content +//! Write [DEWIF](https://git.duniter.org/nodes/common/doc/blob/dewif/rfc/0013_Duniter_Encrypted_Wallet_Import_Format.md) file content use crate::keys::ed25519::Ed25519KeyPair; use arrayvec::ArrayVec; use unwrap::unwrap; -/// Write dewip v1 file content with user passphrase +/// Write dewif v1 file content with user passphrase pub fn write_dewif_v1_content(keypair: &Ed25519KeyPair, passphrase: &str) -> String { let mut bytes = ArrayVec::<[u8; super::V1_BYTES_LEN]>::new(); unwrap!(bytes.try_extend_from_slice(super::VERSION_V1)); @@ -32,7 +32,7 @@ pub fn write_dewif_v1_content(keypair: &Ed25519KeyPair, passphrase: &str) -> Str base64::encode(bytes.as_ref()) } -/// Write dewip v2 file content with user passphrase +/// Write dewif v2 file content with user passphrase pub fn write_dewif_v2_content( keypair1: &Ed25519KeyPair, keypair2: &Ed25519KeyPair, diff --git a/src/lib.rs b/src/lib.rs index f1204d24c6f172e899d5aa57d2e31e2093955d8e..0dec2f9830233fd562d04be77ceaeb17678957f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,8 +31,8 @@ #[cfg(feature = "aes256")] pub mod aes256; pub mod bases; -#[cfg(feature = "dewip")] -pub mod dewip; +#[cfg(feature = "dewif")] +pub mod dewif; pub mod hashs; pub mod keys; pub mod rand;