Skip to content

Commit

Permalink
Port additional crates
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 9, 2024
1 parent 3e28e7d commit 6d1b886
Show file tree
Hide file tree
Showing 52 changed files with 1,405 additions and 1,357 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions aes/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Copyright (c) 2018-2024 The RustCrypto Project Developers
Copyright (c) 2018 Artyom Pavlov

Permission is hereby granted, free of charge, to any
Expand Down
42 changes: 18 additions & 24 deletions aes/src/autodetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::soft;
use cipher::{
consts::{U16, U24, U32},
AlgorithmName, BlockCipher, BlockCipherDecrypt, BlockCipherEncrypt, BlockClosure,
BlockSizeUser, Key, KeyInit, KeySizeUser,
AlgorithmName, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncClosure,
BlockCipherEncrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
};
use core::fmt;
use core::mem::ManuallyDrop;
Expand Down Expand Up @@ -128,41 +128,39 @@ macro_rules! define_aes_impl {
type BlockSize = U16;
}

impl BlockCipher for $name {}

impl BlockCipherEncrypt for $name {
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = U16>) {
unsafe {
if self.token.get() {
#[target_feature(enable = "aes")]
unsafe fn inner(
state: &intrinsics::$name,
f: impl BlockClosure<BlockSize = U16>,
f: impl BlockCipherEncClosure<BlockSize = U16>,
) {
f.call(&mut state.get_enc_backend());
f.call(state.get_enc_backend());
}
inner(&self.inner.intrinsics, f);
} else {
f.call(&mut self.inner.soft.get_enc_backend());
f.call(&self.inner.soft.get_enc_backend());
}
}
}
}

impl BlockCipherDecrypt for $name {
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = U16>) {
unsafe {
if self.token.get() {
#[target_feature(enable = "aes")]
unsafe fn inner(
state: &intrinsics::$name,
f: impl BlockClosure<BlockSize = U16>,
f: impl BlockCipherDecClosure<BlockSize = U16>,
) {
f.call(&mut state.get_dec_backend());
f.call(state.get_dec_backend());
}
inner(&self.inner.intrinsics, f);
} else {
f.call(&mut self.inner.soft.get_dec_backend());
f.call(&self.inner.soft.get_dec_backend());
}
}
}
Expand Down Expand Up @@ -247,22 +245,20 @@ macro_rules! define_aes_impl {
type BlockSize = U16;
}

impl BlockCipher for $name_enc {}

impl BlockCipherEncrypt for $name_enc {
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = U16>) {
unsafe {
if self.token.get() {
#[target_feature(enable = "aes")]
unsafe fn inner(
state: &intrinsics::$name_enc,
f: impl BlockClosure<BlockSize = U16>,
f: impl BlockCipherEncClosure<BlockSize = U16>,
) {
f.call(&mut state.get_enc_backend());
f.call(state.get_enc_backend());
}
inner(&self.inner.intrinsics, f);
} else {
f.call(&mut self.inner.soft.get_enc_backend());
f.call(&self.inner.soft.get_enc_backend());
}
}
}
Expand Down Expand Up @@ -376,22 +372,20 @@ macro_rules! define_aes_impl {
type BlockSize = U16;
}

impl BlockCipher for $name_dec {}

impl BlockCipherDecrypt for $name_dec {
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = U16>) {
unsafe {
if self.token.get() {
#[target_feature(enable = "aes")]
unsafe fn inner(
state: &intrinsics::$name_dec,
f: impl BlockClosure<BlockSize = U16>,
f: impl BlockCipherDecClosure<BlockSize = U16>,
) {
f.call(&mut state.get_dec_backend());
f.call(state.get_dec_backend());
}
inner(&self.inner.intrinsics, f);
} else {
f.call(&mut self.inner.soft.get_dec_backend());
f.call(&self.inner.soft.get_dec_backend());
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions aes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@
//! # Examples
//! ```
//! use aes::Aes128;
//! use aes::cipher::{
//! BlockCipher, BlockCipherEncrypt, BlockCipherDecrypt, KeyInit,
//! array::Array,
//! };
//! use aes::cipher::{Array, BlockCipherEncrypt, BlockCipherDecrypt, KeyInit};
//!
//! let key = Array::from([0u8; 16]);
//! let mut block = Array::from([42u8; 16]);
Expand Down
30 changes: 15 additions & 15 deletions aes/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ macro_rules! impl_backends {
keys: $keys_ty,
}

impl cipher::BlockSizeUser for &$enc_name {
impl cipher::BlockSizeUser for $enc_name {
type BlockSize = cipher::consts::U16;
}

impl cipher::ParBlocksSizeUser for &$enc_name {
impl cipher::ParBlocksSizeUser for $enc_name {
type ParBlocksSize = $par_size;
}

Expand All @@ -40,15 +40,15 @@ macro_rules! impl_backends {
}
}

impl cipher::BlockBackend for &$enc_name {
impl cipher::BlockCipherEncBackend for $enc_name {
#[inline(always)]
fn proc_block(&mut self, block: cipher::inout::InOut<'_, '_, cipher::Block<Self>>) {
fn encrypt_block(&self, block: cipher::inout::InOut<'_, '_, cipher::Block<Self>>) {
unsafe { $encrypt(&self.keys, block) }
}

#[inline(always)]
fn proc_par_blocks(
&mut self,
fn encrypt_par_blocks(
&self,
blocks: cipher::inout::InOut<'_, '_, cipher::ParBlocks<Self>>,
) {
unsafe { $encrypt_par(&self.keys, blocks) }
Expand All @@ -60,11 +60,11 @@ macro_rules! impl_backends {
keys: $keys_ty,
}

impl cipher::BlockSizeUser for &$dec_name {
impl cipher::BlockSizeUser for $dec_name {
type BlockSize = cipher::consts::U16;
}

impl cipher::ParBlocksSizeUser for &$dec_name {
impl cipher::ParBlocksSizeUser for $dec_name {
type ParBlocksSize = $par_size;
}

Expand All @@ -75,27 +75,27 @@ macro_rules! impl_backends {
impl cipher::KeyInit for $dec_name {
#[inline]
fn new(key: &cipher::Key<Self>) -> Self {
From::from(&$enc_name::new(key))
From::from($enc_name::new(key))
}
}

impl From<&$enc_name> for $dec_name {
impl From<$enc_name> for $dec_name {
#[inline]
fn from(enc: &$enc_name) -> $dec_name {
fn from(enc: $enc_name) -> $dec_name {
let keys = unsafe { $inv_keys(&enc.keys) };
Self { keys }
}
}

impl cipher::BlockBackend for &$dec_name {
impl cipher::BlockCipherDecBackend for $dec_name {
#[inline(always)]
fn proc_block(&mut self, block: cipher::inout::InOut<'_, '_, cipher::Block<Self>>) {
fn decrypt_block(&self, block: cipher::inout::InOut<'_, '_, cipher::Block<Self>>) {
unsafe { $decrypt(&self.keys, block) }
}

#[inline(always)]
fn proc_par_blocks(
&mut self,
fn decrypt_par_blocks(
&self,
blocks: cipher::inout::InOut<'_, '_, cipher::ParBlocks<Self>>,
) {
unsafe { $decrypt_par(&self.keys, blocks) }
Expand Down
24 changes: 9 additions & 15 deletions aes/src/ni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use core::arch::x86_64 as arch;

use cipher::{
consts::{self, U16, U24, U32},
AlgorithmName, BlockCipher, BlockCipherDecrypt, BlockCipherEncrypt, BlockClosure,
BlockSizeUser, Key, KeyInit, KeySizeUser,
AlgorithmName, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncClosure,
BlockCipherEncrypt, BlockSizeUser, Key, KeyInit, KeySizeUser,
};
use core::fmt;

Expand Down Expand Up @@ -107,8 +107,6 @@ macro_rules! define_aes_impl {
}
}

impl BlockCipher for $name {}

impl KeySizeUser for $name {
type KeySize = $key_size;
}
Expand Down Expand Up @@ -144,13 +142,13 @@ macro_rules! define_aes_impl {
}

impl BlockCipherEncrypt for $name {
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = U16>) {
self.encrypt.encrypt_with_backend(f)
}
}

impl BlockCipherDecrypt for $name {
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = U16>) {
self.decrypt.decrypt_with_backend(f)
}
}
Expand Down Expand Up @@ -184,8 +182,6 @@ macro_rules! define_aes_impl {
}
}

impl BlockCipher for $name_enc {}

impl KeySizeUser for $name_enc {
type KeySize = $key_size;
}
Expand All @@ -204,8 +200,8 @@ macro_rules! define_aes_impl {
}

impl BlockCipherEncrypt for $name_enc {
fn encrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
f.call(&mut &self.backend)
fn encrypt_with_backend(&self, f: impl BlockCipherEncClosure<BlockSize = U16>) {
f.call(&self.backend)
}
}

Expand Down Expand Up @@ -248,8 +244,6 @@ macro_rules! define_aes_impl {
}
}

impl BlockCipher for $name_dec {}

impl KeySizeUser for $name_dec {
type KeySize = $key_size;
}
Expand All @@ -272,7 +266,7 @@ macro_rules! define_aes_impl {
#[inline]
fn from(enc: &$name_enc) -> $name_dec {
Self {
backend: (&enc.backend).into(),
backend: enc.backend.clone().into(),
}
}
}
Expand All @@ -282,8 +276,8 @@ macro_rules! define_aes_impl {
}

impl BlockCipherDecrypt for $name_dec {
fn decrypt_with_backend(&self, f: impl BlockClosure<BlockSize = U16>) {
f.call(&mut self.get_dec_backend());
fn decrypt_with_backend(&self, f: impl BlockCipherDecClosure<BlockSize = U16>) {
f.call(self.get_dec_backend());
}
}

Expand Down
Loading

0 comments on commit 6d1b886

Please sign in to comment.