Skip to content
Snippets Groups Projects
Commit 4c7468ff authored by Éloïs's avatar Éloïs Committed by Éloïs
Browse files

[feat] add aes256 encryption and decryption

parent 48348b42
No related branches found
No related tags found
1 merge request!1Resolve "Impl DEWIF format"
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "aes"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54eb1d8fe354e5fc611daf4f2ea97dd45a765f4f1e4512306ec183ae2e8f20c9"
dependencies = [
"aes-soft",
"aesni",
"block-cipher-trait",
]
[[package]]
name = "aes-soft"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfd7e7ae3f9a1fb5c03b389fc6bb9a51400d0c13053f0dca698c832bfd893a0d"
dependencies = [
"block-cipher-trait",
"byteorder",
"opaque-debug",
]
[[package]]
name = "aesni"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f70a6b5f971e473091ab7cfb5ffac6cde81666c4556751d8d5620ead8abf100"
dependencies = [
"block-cipher-trait",
"opaque-debug",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.26" version = "1.0.26"
...@@ -34,6 +66,15 @@ dependencies = [ ...@@ -34,6 +66,15 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "block-cipher-trait"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c924d49bd09e7c06003acda26cd9742e796e34282ec6c1189404dee0c1f4774"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "block-padding" name = "block-padding"
version = "0.1.5" version = "0.1.5"
...@@ -102,6 +143,7 @@ dependencies = [ ...@@ -102,6 +143,7 @@ dependencies = [
name = "dup-crypto" name = "dup-crypto"
version = "0.8.0" version = "0.8.0"
dependencies = [ dependencies = [
"aes",
"base64", "base64",
"bincode", "bincode",
"bs58", "bs58",
......
...@@ -13,6 +13,7 @@ edition = "2018" ...@@ -13,6 +13,7 @@ edition = "2018"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
aes = { version = "0.3.2", optional = true }
base64 = "0.11.0" base64 = "0.11.0"
bs58 = "0.3.0" bs58 = "0.3.0"
byteorder = "1.3.2" byteorder = "1.3.2"
...@@ -28,4 +29,5 @@ bincode = "1.2.0" ...@@ -28,4 +29,5 @@ bincode = "1.2.0"
[features] [features]
default = ["ser"] default = ["ser"]
aes256 = ["aes"]
ser = ["serde"] ser = ["serde"]
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// 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/>.
//! Aes256 encryption/decryption
pub(crate) mod decrypt;
pub(crate) mod encrypt;
pub use aes::Aes256;
pub use decrypt::decrypt_bytes;
pub use encrypt::encrypt_bytes;
use crate::seeds::Seed32;
use aes::block_cipher_trait::generic_array::GenericArray;
use aes::block_cipher_trait::BlockCipher;
type Block = GenericArray<u8, <Aes256 as BlockCipher>::BlockSize>;
type ParBlocks = <Aes256 as BlockCipher>::ParBlocks;
/// Create cipher from seed of 32 bytes
pub fn new_cipher(seed: Seed32) -> Aes256 {
Aes256::new(GenericArray::from_slice(seed.as_ref()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_and_decrypt_128_bytes() {
let cipher = new_cipher(Seed32::default());
let bytes = [3u8; 128];
let mut encrypted_bytes = bytes;
encrypt_bytes(&cipher, &mut encrypted_bytes);
decrypt_bytes(&cipher, &mut encrypted_bytes);
for i in 0..128 {
assert_eq!(bytes[i], encrypted_bytes[i]);
}
}
}
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// 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/>.
//! Aes256 decryption
use super::{Aes256, Block, ParBlocks};
use aes::block_cipher_trait::generic_array::GenericArray;
use aes::block_cipher_trait::BlockCipher;
/// Decrypt bytes.
/// The length of the bytes slice must be a multiple of 16 !
/// Panics if the length of the bytes slice is not a multiple of 16.
pub fn decrypt_bytes(cipher: &Aes256, bytes: &mut [u8]) {
assert!(bytes.len() % 16 == 0);
let mut remaining_len = bytes.len();
let par_len = bytes.len() / 128;
if par_len > 0 {
decrypt_par_n_blocks(cipher, &mut bytes[..par_len], par_len / 8);
remaining_len -= par_len;
}
if remaining_len > 0 {
decrypt_n_blocks(cipher, &mut bytes[par_len..], remaining_len / 16);
}
}
fn decrypt_par_n_blocks(cipher: &Aes256, bytes: &mut [u8], n: usize) {
for i in (0..n).step_by(8) {
decrypt_8_blocks(cipher, &mut bytes[i..i + 128]);
}
}
pub(crate) fn decrypt_8_blocks(cipher: &Aes256, bytes: &mut [u8]) {
let mut blocks: GenericArray<Block, ParBlocks> = (0..8)
.map(|i| {
let begin = i * 16;
let end = begin + 16;
GenericArray::clone_from_slice(&bytes[begin..end])
})
.collect();
cipher.decrypt_blocks(&mut blocks);
for (i, block) in blocks.into_iter().enumerate() {
let begin = i * 16;
let end = (i + 1) * 16;
bytes[begin..end].copy_from_slice(block.as_slice());
}
}
pub(crate) fn decrypt_n_blocks(cipher: &Aes256, bytes: &mut [u8], n: usize) {
for i in 0..n {
let begin = i * 16;
let end = (i + 1) * 16;
let mut block = GenericArray::from_mut_slice(&mut bytes[begin..end]);
cipher.decrypt_block(&mut block);
}
}
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// 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/>.
//! Aes256 encryption
use super::{Aes256, Block, ParBlocks};
use aes::block_cipher_trait::generic_array::GenericArray;
use aes::block_cipher_trait::BlockCipher;
/// Encrypt bytes.
/// The length of the bytes slice must be a multiple of 16 !
/// Panics if the length of the bytes slice is not a multiple of 16.
pub fn encrypt_bytes(cipher: &Aes256, bytes: &mut [u8]) {
assert!(bytes.len() % 16 == 0);
let mut remaining_len = bytes.len();
let par_len = bytes.len() / 128;
if par_len > 0 {
encrypt_par_n_blocks(cipher, &mut bytes[..par_len], par_len / 8);
remaining_len -= par_len;
}
if remaining_len > 0 {
encrypt_n_blocks(cipher, &mut bytes[par_len..], remaining_len / 16);
}
}
fn encrypt_par_n_blocks(cipher: &Aes256, bytes: &mut [u8], n: usize) {
for i in (0..n).step_by(8) {
encrypt_8_blocks(cipher, &mut bytes[i..i + 128]);
}
}
pub(crate) fn encrypt_8_blocks(cipher: &Aes256, bytes: &mut [u8]) {
let mut blocks: GenericArray<Block, ParBlocks> = (0..8)
.map(|i| {
let begin = i * 16;
let end = begin + 16;
GenericArray::clone_from_slice(&bytes[begin..end])
})
.collect();
cipher.encrypt_blocks(&mut blocks);
for (i, block) in blocks.into_iter().enumerate() {
let begin = i * 16;
let end = (i + 1) * 16;
bytes[begin..end].copy_from_slice(block.as_slice());
}
}
pub(crate) fn encrypt_n_blocks(cipher: &Aes256, bytes: &mut [u8], n: usize) {
for i in 0..n {
let begin = i * 16;
let end = (i + 1) * 16;
let mut block = GenericArray::from_mut_slice(&mut bytes[begin..end]);
cipher.encrypt_block(&mut block);
}
}
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
)] )]
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#[cfg(feature = "aes256")]
pub mod aes256;
pub mod bases; pub mod bases;
pub mod hashs; pub mod hashs;
pub mod keys; pub mod keys;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment