kanegreen / aes_frast Goto Github PK
View Code? Open in Web Editor NEWAn easy-to-use lib for AES encryption and decryption, coded in pure safe Rust-lang.
License: Apache License 2.0
An easy-to-use lib for AES encryption and decryption, coded in pure safe Rust-lang.
License: Apache License 2.0
When building the CFB example https://github.com/KaneGreen/aes_frast/blob/master/src/aes_with_operation_mode.rs#L237 on Rust nightly-2018-09-29 it emits several warnings:
warning: unused import: `padding_128bit`
--> src/main.rs:5:56
|
5 | use aes_frast::{aes_core, aes_with_operation_mode, padding_128bit};
| ^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
warning: variable does not need to be mutable
--> src/main.rs:7:9
|
7 | let mut plain: Vec<u8> = vec![0x34, 0x63, 0xD0, 0x89, 0x1D, 0x71, 0x4A, 0xB0,
| ----^^^^^
| |
| help: remove this `mut`
|
= note: #[warn(unused_mut)] on by default
warning: variable does not need to be mutable
--> src/main.rs:17:9
|
17 | let mut o_key: Vec<u8> = vec![0x0F, 0x57, 0x9F, 0x79, 0x50, 0x99, 0x0A, 0xCE,
| ----^^^^^
| |
| help: remove this `mut`
warning: variable does not need to be mutable
--> src/main.rs:22:9
|
22 | let mut iv: Vec<u8> = vec![0x04, 0x7C, 0xF3, 0xEA, 0xE1, 0x76, 0x45, 0x85,
| ----^^
| |
| help: remove this `mut`
Finished dev [unoptimized + debuginfo] target(s) in 0.55s
Fixing the warnings is straightforward, removed the unused import and mut's:
use aes_frast::{aes_core, aes_with_operation_mode};
let length: usize = 64;
let plain: Vec<u8> = vec![0x34, 0x63, 0xD0, 0x89, 0x1D, 0x71, 0x4A, 0xB0,
0x08, 0x5D, 0x22, 0xE1, 0x8B, 0xFA, 0x77, 0xF0,
0xEB, 0xE4, 0xB8, 0x9E, 0xF0, 0x05, 0x32, 0x7D,
0x4F, 0xBD, 0x87, 0x69, 0x75, 0x76, 0x78, 0xAA,
0x3D, 0x24, 0x06, 0x0C, 0xA4, 0xA5, 0x8C, 0xA0,
0x21, 0x58, 0xB8, 0xA1, 0x86, 0xAD, 0xBB, 0x6D,
0x9E, 0x09, 0x1C, 0x47, 0x06, 0x25, 0x4B, 0x2E,
0x30, 0x53, 0x3A, 0x5F, 0xE9, 0xDF, 0x3A, 0x90];
let mut cipher = vec![0u8; length];
let mut dec_cipher = vec![0u8; length];
let o_key: Vec<u8> = vec![0x0F, 0x57, 0x9F, 0x79, 0x50, 0x99, 0x0A, 0xCE,
0x66, 0x72, 0xA8, 0x17, 0x95, 0x1F, 0xF6, 0x06,
0x24, 0x40, 0xDE, 0xF5, 0x08, 0xF1, 0x64, 0x34,
0xD6, 0xEF, 0xEF, 0xFD, 0x26, 0x23, 0x04, 0x95];
let mut w_keys: Vec<u32> = vec![0u32; 60];
let iv: Vec<u8> = vec![0x04, 0x7C, 0xF3, 0xEA, 0xE1, 0x76, 0x45, 0x85,
0x72, 0x52, 0x7B, 0xAA, 0x26, 0x0D, 0x65, 0xBB];
aes_core::setkey_enc_auto(&o_key, &mut w_keys);
aes_with_operation_mode::cfb_enc(&plain, &mut cipher, &w_keys, &iv);
let expected_encrypted = vec![0x9D, 0xF9, 0x3D, 0x71, 0xC1, 0x9E, 0x50, 0x22,
0x36, 0x35, 0xF1, 0xB6, 0xED, 0xA6, 0x86, 0x74,
0x5F, 0x34, 0xD6, 0x93, 0xA9, 0x0F, 0xFF, 0x50,
0x4C, 0xE6, 0x8E, 0xC0, 0x06, 0x3F, 0x3A, 0x62,
0x78, 0x9F, 0xAB, 0xB1, 0x06, 0x95, 0x64, 0xB6,
0xBB, 0x06, 0x92, 0x02, 0x34, 0x2D, 0x36, 0x35,
0xA4, 0x95, 0x30, 0x2E, 0x20, 0xD3, 0xE8, 0x53,
0x95, 0xA2, 0xDF, 0x83, 0x9B, 0x7B, 0x12, 0x3F];
assert_eq!(length, cipher.len(), "ERROR cipher.len()");
for i in 0..length {
assert_eq!(expected_encrypted[i], cipher[i], "ERROR in encrypt {}", i);
}
// Notice: CFB only uses block-encryption, no matter we use it as encryption or decryption.
// So, keep don't use functions which start with`setkey_dec_` and keep the next line commented out.
//aes_core::setkey_dec_auto(&o_key, &mut w_keys);
aes_with_operation_mode::cfb_dec(&cipher, &mut dec_cipher, &w_keys, &iv);
assert_eq!(length, dec_cipher.len(), "ERROR dec_cipher.len()");
for i in 0..length {
assert_eq!(plain[i], dec_cipher[i], "ERROR in decrypt {}", i);
}
Can aes_frast CFB mode be used with segment sizes other than the block size? From the code https://github.com/KaneGreen/aes_frast/blob/master/src/aes_with_operation_mode.rs#L228 it doesn't appear so:
/// The feedback size is fixed to 128 bits, which is the same as block size.
but this would be useful for streaming, effectively converting the block cipher into a stream cipher. https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Feedback_(CFB)
To use CFB to make a self-synchronizing stream cipher that will synchronize for any multiple of x bits lost, start by initializing a shift register the size of the block size with the initialization vector. This is encrypted with the block cipher, and the highest x bits of the result are XOR'ed with x bits of the plaintext to produce x bits of ciphertext. These x bits of output are shifted into the shift register, and the process (starting with encrypting the shift register with the block cipher) repeats for the next x bits of plaintext. Decryption is similar, start with the initialization vector, encrypt, and XOR the high bits of the result with x bits of the ciphertext to produce x bits of plaintext, then shift the x bits of the ciphertext into the shift register and encrypt again. This way of proceeding is known as CFB-8 or CFB-1 (according to the size of the shifting).
For comparison, openssl these are aes-128-cfb1 and aes-128-cfb8, and in Python PyCrypto, segment_size
:
from Crypto.Cipher import AES
key=b"XXXXXXXXXXXXXXXX"
iv=key
cipher=AES.new(key, AES.MODE_CFB, iv, segment_size=8);
plain = b"\x41"
print "plain =",map(ord, plain)
print "encrypted =",map(ord, cipher.encrypt(plain)) # 209
print "encrypted2 =",map(ord, cipher.encrypt(plain)) # 150
print
cipher=AES.new(key, AES.MODE_CFB, iv);
print "decrypted =",map(ord, cipher.decrypt(b"\xd1")) # 65
print "decrypted2 =",map(ord, cipher.decrypt(b"\x96")) # 65
Trying to port some code which used the Rust OpenSSL crate for CFB in 8-bit mode, first attempt using RustCrypto crates but not yet supported RustCrypto/block-ciphers#28, maybe will switch if it is implemented or to this module if possible.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.