Skip to content

Commit

Permalink
The core and stream operations are moved into separate modules (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheroz committed Aug 15, 2023
1 parent a6200f2 commit d72e6e6
Show file tree
Hide file tree
Showing 29 changed files with 1,275 additions and 1,226 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## 0.8.0 (2023-08-15)

### Breaking Change

- The core and stream operations are moved into separate modules

## 0.7.1 (2023-08-05)

- Updated documentation
Expand Down
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ let decrypted = magma.decrypt(encrypted);
println!("Decrypted:\n{:x}", decrypted);

assert_eq!(decrypted, source);

```

### Text encryption sample: [encrypt_text.rs](https://github.com/sheroz/magma/tree/main/magma_samples/src/samples/encrypt_text.rs)

```rust
use cipher_magma::{CipherMode, CipherOperation, Magma};

let cipher_mode = CipherMode::CFB;
use cipher_magma::{CipherMode, MagmaStream};

let key = [0xab; 32];
println!("Key:\n{:x?}\n", key);
let mut magma = Magma::with_key(key);
let mut magma = MagmaStream::new(key, CipherMode::CFB);

let source = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
Aenean ac sem leo. Morbi pretium neque eget felis finibus convallis. \
Expand All @@ -78,12 +75,12 @@ let source = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \

println!("Source:\n{}\n", String::from_utf8(source.to_vec()).unwrap());

let encrypted = magma.cipher(source, &CipherOperation::Encrypt, &cipher_mode);
let encrypted = magma.encrypt(source);
println!("Encrypted:\n{:02x?}\n", encrypted);

let mut decrypted = magma.cipher(&encrypted, &CipherOperation::Decrypt, &cipher_mode);
let mut decrypted = magma.decrypt(&encrypted);

if cipher_mode.has_padding() {
if magma.get_mode().has_padding() {
// remove padding bytes
decrypted.truncate(source.len());
}
Expand All @@ -95,15 +92,14 @@ println!("Decrypted:\n{}\n", String::from_utf8(decrypted).unwrap());
### Message Authentication Code (MAC) sample: [calculate_mac.rs](https://github.com/sheroz/magma/tree/main/magma_samples/src/samples/calculate_mac.rs)

```rust
use cipher_magma::{mac, Magma};
use cipher_magma::{mac, CipherMode, MagmaStream};

let key: [u8; 32] = [
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
0x00, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
0xfe, 0xff,
];
println!("Key:\n{:x?}\n", key);
let mut magma = Magma::with_key(key);

let message = [
0x92, 0xde, 0xf0, 0x6b, 0x3c, 0x13, 0x0a, 0x59, 0xdb, 0x54, 0xc7, 0x04, 0xf8, 0x18, 0x9d,
Expand All @@ -112,6 +108,8 @@ let message = [
];
println!("Message:\n{:02x?}\n", message);

let mut magma = MagmaStream::new(key, CipherMode::MAC);

// update the context
for chunk in message.chunks(8) {
mac::update(&mut magma, &chunk);
Expand All @@ -127,29 +125,29 @@ assert_eq!(mac, 0x154e7210);
### File encryption sample: [encrypt_file.rs](https://github.com/sheroz/magma/tree/main/magma_samples/src/samples/encrypt_file.rs)

```rust
use cipher_magma::{CipherMode, CipherOperation, Magma};
use cipher_magma::{CipherMode, MagmaStream};
use std::env;
use std::fs::File;
use std::io::{Read, Seek, Write};

let cipher_mode = CipherMode::CBC;

let key = [0xab; 32];
let mut magma = Magma::with_key(key);
let mut magma = MagmaStream::new(key, CipherMode::CBC);

// opening file
// opening source file
let source_filename = "README.md";
println!("Opening source file: {}", source_filename);

let mut source_file = std::fs::File::open(source_filename).expect("Could not open file.");
let mut source_file = File::open(source_filename).expect("Could not open file.");
let source_len = source_file.metadata().unwrap().len();

let temp_dir = std::env::temp_dir();
let temp_dir = env::temp_dir();

// creating file for encrypted data
let encrypted_filename = format!("{}.encrypted", source_filename);
let encrypted_filepath = temp_dir.join(encrypted_filename);
println!("Creating encrypted file: {:?}", encrypted_filepath);

let mut encrypted_file = std::fs::File::options()
let mut encrypted_file = File::options()
.write(true)
.read(true)
.create(true)
Expand All @@ -170,12 +168,13 @@ loop {
break;
}

let ciphertext = magma.cipher(&buf[0..read_count], &CipherOperation::Encrypt, &cipher_mode);
let ciphertext = magma.encrypt(&buf[0..read_count]);

encrypted_file
.write_all(&ciphertext)
.expect("Could not write into encrypted file");
}

encrypted_file
.flush()
.expect("Could not flush the encrypted file");
Expand All @@ -185,10 +184,10 @@ println!("Encryption completed.");
let decrypted_filename = format!("{}.decrypted", source_filename);
let decrypted_filepath = temp_dir.join(decrypted_filename);

println!("Creating decrypted file: {:?}", decrypted_filepath);
println!("Creating file for decrypted data: {:?}", decrypted_filepath);

let mut decrypted_file =
std::fs::File::create(decrypted_filepath).expect("Could not create decrypted file.");
File::create(decrypted_filepath).expect("Could not create decrypted file.");

println!("Decrypting ...");

Expand All @@ -206,17 +205,18 @@ loop {
break;
}

let plaintext = magma.cipher(&buf[0..read_count], &CipherOperation::Decrypt, &cipher_mode);
let plaintext = magma.decrypt(&buf[0..read_count]);

decrypted_file
.write_all(&plaintext)
.expect("Could not write into decrypted file");
}

decrypted_file
.flush()
.expect("Could not flush the decrypted file");

if cipher_mode.has_padding() {
if magma.get_mode().has_padding() {
// remove padding bytes
decrypted_file
.set_len(source_len)
Expand Down
6 changes: 6 additions & 0 deletions cipher_magma/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## 0.8.0 (2023-08-15)

### Breaking Change

- The core and stream operations are moved into separate modules

## 0.7.1 (2023-08-05)

- Updated documentation
Expand Down
2 changes: 1 addition & 1 deletion cipher_magma/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cipher_magma"
version = "0.7.1"
version = "0.8.0"
edition = "2021"
authors = ["Sheroz Khaydarov"]
description = "Block Cipher Magma (GOST R 34.12-2015, former GOST 28147-89)"
Expand Down
46 changes: 23 additions & 23 deletions cipher_magma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ let decrypted = magma.decrypt(encrypted);
println!("Decrypted:\n{:x}", decrypted);

assert_eq!(decrypted, source);

```

### Text encryption sample: [encrypt_text.rs](https://github.com/sheroz/magma/tree/main/magma_samples/src/samples/encrypt_text.rs)

```rust
use cipher_magma::{CipherMode, CipherOperation, Magma};

let cipher_mode = CipherMode::CFB;
use cipher_magma::{CipherMode, MagmaStream};

let key = [0xab; 32];
println!("Key:\n{:x?}\n", key);
let mut magma = Magma::with_key(key);
let mut magma = MagmaStream::new(key, CipherMode::CFB);

let source = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
Aenean ac sem leo. Morbi pretium neque eget felis finibus convallis. \
Expand All @@ -78,12 +75,12 @@ let source = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. \

println!("Source:\n{}\n", String::from_utf8(source.to_vec()).unwrap());

let encrypted = magma.cipher(source, &CipherOperation::Encrypt, &cipher_mode);
let encrypted = magma.encrypt(source);
println!("Encrypted:\n{:02x?}\n", encrypted);

let mut decrypted = magma.cipher(&encrypted, &CipherOperation::Decrypt, &cipher_mode);
let mut decrypted = magma.decrypt(&encrypted);

if cipher_mode.has_padding() {
if magma.get_mode().has_padding() {
// remove padding bytes
decrypted.truncate(source.len());
}
Expand All @@ -95,15 +92,14 @@ println!("Decrypted:\n{}\n", String::from_utf8(decrypted).unwrap());
### Message Authentication Code (MAC) sample: [calculate_mac.rs](https://github.com/sheroz/magma/tree/main/magma_samples/src/samples/calculate_mac.rs)

```rust
use cipher_magma::{mac, Magma};
use cipher_magma::{mac, CipherMode, MagmaStream};

let key: [u8; 32] = [
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
0x00, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
0xfe, 0xff,
];
println!("Key:\n{:x?}\n", key);
let mut magma = Magma::with_key(key);

let message = [
0x92, 0xde, 0xf0, 0x6b, 0x3c, 0x13, 0x0a, 0x59, 0xdb, 0x54, 0xc7, 0x04, 0xf8, 0x18, 0x9d,
Expand All @@ -112,6 +108,8 @@ let message = [
];
println!("Message:\n{:02x?}\n", message);

let mut magma = MagmaStream::new(key, CipherMode::MAC);

// update the context
for chunk in message.chunks(8) {
mac::update(&mut magma, &chunk);
Expand All @@ -127,29 +125,29 @@ assert_eq!(mac, 0x154e7210);
### File encryption sample: [encrypt_file.rs](https://github.com/sheroz/magma/tree/main/magma_samples/src/samples/encrypt_file.rs)

```rust
use cipher_magma::{CipherMode, CipherOperation, Magma};
use cipher_magma::{CipherMode, MagmaStream};
use std::env;
use std::fs::File;
use std::io::{Read, Seek, Write};

let cipher_mode = CipherMode::CBC;

let key = [0xab; 32];
let mut magma = Magma::with_key(key);
let mut magma = MagmaStream::new(key, CipherMode::CBC);

// opening file
// opening source file
let source_filename = "README.md";
println!("Opening source file: {}", source_filename);

let mut source_file = std::fs::File::open(source_filename).expect("Could not open file.");
let mut source_file = File::open(source_filename).expect("Could not open file.");
let source_len = source_file.metadata().unwrap().len();

let temp_dir = std::env::temp_dir();
let temp_dir = env::temp_dir();

// creating file for encrypted data
let encrypted_filename = format!("{}.encrypted", source_filename);
let encrypted_filepath = temp_dir.join(encrypted_filename);
println!("Creating encrypted file: {:?}", encrypted_filepath);

let mut encrypted_file = std::fs::File::options()
let mut encrypted_file = File::options()
.write(true)
.read(true)
.create(true)
Expand All @@ -170,12 +168,13 @@ loop {
break;
}

let ciphertext = magma.cipher(&buf[0..read_count], &CipherOperation::Encrypt, &cipher_mode);
let ciphertext = magma.encrypt(&buf[0..read_count]);

encrypted_file
.write_all(&ciphertext)
.expect("Could not write into encrypted file");
}

encrypted_file
.flush()
.expect("Could not flush the encrypted file");
Expand All @@ -185,10 +184,10 @@ println!("Encryption completed.");
let decrypted_filename = format!("{}.decrypted", source_filename);
let decrypted_filepath = temp_dir.join(decrypted_filename);

println!("Creating decrypted file: {:?}", decrypted_filepath);
println!("Creating file for decrypted data: {:?}", decrypted_filepath);

let mut decrypted_file =
std::fs::File::create(decrypted_filepath).expect("Could not create decrypted file.");
File::create(decrypted_filepath).expect("Could not create decrypted file.");

println!("Decrypting ...");

Expand All @@ -206,17 +205,18 @@ loop {
break;
}

let plaintext = magma.cipher(&buf[0..read_count], &CipherOperation::Decrypt, &cipher_mode);
let plaintext = magma.decrypt(&buf[0..read_count]);

decrypted_file
.write_all(&plaintext)
.expect("Could not write into decrypted file");
}

decrypted_file
.flush()
.expect("Could not flush the decrypted file");

if cipher_mode.has_padding() {
if magma.get_mode().has_padding() {
// remove padding bytes
decrypted_file
.set_len(source_len)
Expand Down
19 changes: 19 additions & 0 deletions cipher_magma/src/core/cipher_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Passing cipher keys in a polymorphic way
pub enum CipherKey {
ArrayU8([u8; 32]),
ArrayU32([u32; 8]),
}

impl From<[u8; 32]> for CipherKey {
/// builds key from '[u8;32]' array
fn from(array_u8: [u8; 32]) -> Self {
Self::ArrayU8(array_u8)
}
}

impl From<[u32; 8]> for CipherKey {
/// builds key from '[u32;8]' array
fn from(array_u32: [u32; 8]) -> Self {
Self::ArrayU32(array_u32)
}
}

0 comments on commit d72e6e6

Please sign in to comment.