Skip to content

Commit

Permalink
Migrate to cipher v0.5.0-pre.7
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 7, 2024
1 parent daac7ea commit 3e28e7d
Show file tree
Hide file tree
Showing 27 changed files with 1,538 additions and 1,096 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
cipher = { git = "https://github.com/RustCrypto/traits", branch = "block_backends" }
69 changes: 69 additions & 0 deletions aria/src/aria128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{
consts::{C1, C2, C3},
utils::{a, fe, fo},
Aria128,
};
use cipher::{consts::U16, AlgorithmName, Key, KeyInit, KeySizeUser};
use core::fmt;

impl KeySizeUser for Aria128 {
type KeySize = U16;
}

impl KeyInit for Aria128 {
fn new(key: &Key<Self>) -> Self {
let kl = u128::from_be_bytes(key[0..16].try_into().unwrap());
let kr = u128::default();

let w0 = kl;
let w1 = fo(w0 ^ C1) ^ kr;
let w2 = fe(w1 ^ C2) ^ w0;
let w3 = fo(w2 ^ C3) ^ w1;

let ek = [
w0 ^ w1.rotate_right(19),
w1 ^ w2.rotate_right(19),
w2 ^ w3.rotate_right(19),
w3 ^ w0.rotate_right(19),
w0 ^ w1.rotate_right(31),
w1 ^ w2.rotate_right(31),
w2 ^ w3.rotate_right(31),
w3 ^ w0.rotate_right(31),
w0 ^ w1.rotate_left(61),
w1 ^ w2.rotate_left(61),
w2 ^ w3.rotate_left(61),
w3 ^ w0.rotate_left(61),
w0 ^ w1.rotate_left(31),
];

let dk = [
ek[12],
a(ek[11]),
a(ek[10]),
a(ek[9]),
a(ek[8]),
a(ek[7]),
a(ek[6]),
a(ek[5]),
a(ek[4]),
a(ek[3]),
a(ek[2]),
a(ek[1]),
ek[0],
];

Self { ek, dk }
}
}

impl fmt::Debug for Aria128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Aria128 { ... }")
}
}

impl AlgorithmName for Aria128 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Aria128")
}
}
74 changes: 74 additions & 0 deletions aria/src/aria192.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::{
consts::{C1, C2, C3},
utils::{a, fe, fo},
Aria192,
};
use cipher::{consts::U24, AlgorithmName, Key, KeyInit, KeySizeUser};
use core::fmt;

impl KeySizeUser for Aria192 {
type KeySize = U24;
}

impl KeyInit for Aria192 {
fn new(key: &Key<Self>) -> Self {
let kl = u128::from_be_bytes(key[0..16].try_into().unwrap());
let kr = u64::from_be_bytes(key[16..24].try_into().unwrap());
let kr = (kr as u128) << 64;

let w0 = kl;
let w1 = fo(w0 ^ C2) ^ kr;
let w2 = fe(w1 ^ C3) ^ w0;
let w3 = fo(w2 ^ C1) ^ w1;

let ek = [
w0 ^ w1.rotate_right(19),
w1 ^ w2.rotate_right(19),
w2 ^ w3.rotate_right(19),
w3 ^ w0.rotate_right(19),
w0 ^ w1.rotate_right(31),
w1 ^ w2.rotate_right(31),
w2 ^ w3.rotate_right(31),
w3 ^ w0.rotate_right(31),
w0 ^ w1.rotate_left(61),
w1 ^ w2.rotate_left(61),
w2 ^ w3.rotate_left(61),
w3 ^ w0.rotate_left(61),
w0 ^ w1.rotate_left(31),
w1 ^ w2.rotate_left(31),
w2 ^ w3.rotate_left(31),
];

let dk = [
ek[14],
a(ek[13]),
a(ek[12]),
a(ek[11]),
a(ek[10]),
a(ek[9]),
a(ek[8]),
a(ek[7]),
a(ek[6]),
a(ek[5]),
a(ek[4]),
a(ek[3]),
a(ek[2]),
a(ek[1]),
ek[0],
];

Self { ek, dk }
}
}

impl fmt::Debug for Aria192 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Aria192 { ... }")
}
}

impl AlgorithmName for Aria192 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Aria192")
}
}
77 changes: 77 additions & 0 deletions aria/src/aria256.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::{
consts::{C1, C2, C3},
utils::{a, fe, fo},
Aria256,
};
use cipher::{consts::U32, AlgorithmName, Key, KeyInit, KeySizeUser};
use core::fmt;

impl KeySizeUser for Aria256 {
type KeySize = U32;
}

impl KeyInit for Aria256 {
fn new(key: &Key<Self>) -> Self {
let kl = u128::from_be_bytes(key[0..16].try_into().unwrap());
let kr = u128::from_be_bytes(key[16..32].try_into().unwrap());

let w0 = kl;
let w1 = fo(w0 ^ C3) ^ kr;
let w2 = fe(w1 ^ C1) ^ w0;
let w3 = fo(w2 ^ C2) ^ w1;

let ek = [
w0 ^ w1.rotate_right(19),
w1 ^ w2.rotate_right(19),
w2 ^ w3.rotate_right(19),
w3 ^ w0.rotate_right(19),
w0 ^ w1.rotate_right(31),
w1 ^ w2.rotate_right(31),
w2 ^ w3.rotate_right(31),
w3 ^ w0.rotate_right(31),
w0 ^ w1.rotate_left(61),
w1 ^ w2.rotate_left(61),
w2 ^ w3.rotate_left(61),
w3 ^ w0.rotate_left(61),
w0 ^ w1.rotate_left(31),
w1 ^ w2.rotate_left(31),
w2 ^ w3.rotate_left(31),
w3 ^ w0.rotate_left(31),
w0 ^ w1.rotate_left(19),
];

let dk = [
ek[16],
a(ek[15]),
a(ek[14]),
a(ek[13]),
a(ek[12]),
a(ek[11]),
a(ek[10]),
a(ek[9]),
a(ek[8]),
a(ek[7]),
a(ek[6]),
a(ek[5]),
a(ek[4]),
a(ek[3]),
a(ek[2]),
a(ek[1]),
ek[0],
];

Self { ek, dk }
}
}

impl fmt::Debug for Aria256 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Aria256 { ... }")
}
}

impl AlgorithmName for Aria256 {
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Aria256")
}
}
Loading

0 comments on commit 3e28e7d

Please sign in to comment.