Skip to content

Commit 1d4d223

Browse files
committed
style: fix clippy warnings and apply rustfmt
- Addressed clippy lints for needless lifetimes, etc. - Ran cargo fmt for formatting consistency Signed-off-by: Steven Lee <steven_lee@aspeedtech.com>
1 parent b77c1f3 commit 1d4d223

File tree

5 files changed

+130
-123
lines changed

5 files changed

+130
-123
lines changed

src/hace_controller.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use ast1060_pac::Hace;
2+
use core::convert::Infallible;
23
use proposed_traits::digest::ErrorType as DigestErrorType;
34
use proposed_traits::mac::ErrorType as MacErrorType;
4-
use core::convert::Infallible;
55

66
#[link_section = ".ram_nc"]
77
static mut HMAC_CTX: AspeedHashContext = AspeedHashContext::new();
88

9-
109
const SHA1_IV: [u32; 8] = [
1110
0x0123_4567,
1211
0x89ab_cdef,
@@ -116,7 +115,6 @@ const SHA512_256_IV: [u32; 16] = [
116115
0xA22C_C581,
117116
];
118117

119-
120118
const HACE_SHA_BE_EN: u32 = 1 << 3;
121119
const HACE_CMD_ACC_MODE: u32 = 1 << 8;
122120
pub const HACE_SG_EN: u32 = 1 << 18;
@@ -142,7 +140,6 @@ impl AspeedSg {
142140
}
143141
}
144142

145-
146143
#[repr(C)]
147144
#[repr(align(64))]
148145
pub struct AspeedHashContext {
@@ -287,8 +284,7 @@ impl<'ctrl> HaceController<'ctrl> {
287284
}
288285
}
289286

290-
291-
impl <'a> DigestErrorType for HaceController<'_> {
287+
impl<'a> DigestErrorType for HaceController<'_> {
292288
type Error = Infallible;
293289
}
294290

@@ -348,11 +344,10 @@ impl HaceController<'_> {
348344
self.copy_iv_to_digest();
349345
self.fill_padding(0);
350346
let bufcnt = self.ctx_mut().bufcnt;
351-
self.start_hash_operation(bufcnt as u32);
347+
self.start_hash_operation(bufcnt);
352348

353-
let slice = unsafe {
354-
core::slice::from_raw_parts(self.ctx_mut().digest.as_ptr(), digest_len)
355-
};
349+
let slice =
350+
unsafe { core::slice::from_raw_parts(self.ctx_mut().digest.as_ptr(), digest_len) };
356351

357352
self.ctx_mut().key[..digest_len].copy_from_slice(slice);
358353
self.ctx_mut().ipad[..digest_len].copy_from_slice(slice);
@@ -372,12 +367,10 @@ impl HaceController<'_> {
372367
} else {
373368
64 + 56 - index
374369
}
370+
} else if index < 112 {
371+
112 - index
375372
} else {
376-
if index < 112 {
377-
112 - index
378-
} else {
379-
128 + 112 - index
380-
}
373+
128 + 112 - index
381374
};
382375

383376
ctx.buffer[bufcnt] = 0x80;

src/hash.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use proposed_traits::digest::*;
2-
use core::convert::Infallible;
31
use crate::hace_controller::{HaceController, HashAlgo, HACE_SG_LAST};
2+
use core::convert::Infallible;
3+
use proposed_traits::digest::*;
44

55
// DigestAlgorithm implementation for HashAlgo
66
impl DigestAlgorithm for HashAlgo {
@@ -83,18 +83,23 @@ impl DigestAlgorithm for Sha512 {
8383
}
8484

8585
impl Default for Sha256 {
86-
fn default() -> Self { Sha256 }
86+
fn default() -> Self {
87+
Sha256
88+
}
8789
}
8890

8991
impl Default for Sha384 {
90-
fn default() -> Self { Sha384 }
92+
fn default() -> Self {
93+
Sha384
94+
}
9195
}
9296

9397
impl Default for Sha512 {
94-
fn default() -> Self { Sha512 }
98+
fn default() -> Self {
99+
Sha512
100+
}
95101
}
96102

97-
98103
impl IntoHashAlgo for Sha256 {
99104
fn to_hash_algo() -> HashAlgo {
100105
HashAlgo::SHA256
@@ -118,17 +123,19 @@ where
118123
A: DigestAlgorithm + IntoHashAlgo,
119124
A::DigestOutput: Default + AsMut<[u8]>,
120125
{
121-
type OpContext<'a> = OpContextImpl<'a, 'ctrl, A> where Self: 'a; // Define your OpContext type here
126+
type OpContext<'a>
127+
= OpContextImpl<'a, 'ctrl, A>
128+
where
129+
Self: 'a; // Define your OpContext type here
122130

123-
fn init<'a>(&'a mut self, _algo: A) -> Result<Self::OpContext<'a>, Self::Error> {
131+
fn init(&mut self, _algo: A) -> Result<Self::OpContext<'_>, Self::Error> {
124132
self.algo = A::to_hash_algo();
125133
self.ctx_mut().method = self.algo.hash_cmd();
126134
self.copy_iv_to_digest();
127135
self.ctx_mut().block_size = self.algo.block_size() as u32;
128136
self.ctx_mut().bufcnt = 0;
129137
self.ctx_mut().digcnt = [0; 2];
130138

131-
132139
Ok(OpContextImpl {
133140
controller: self,
134141
_phantom: core::marker::PhantomData,
@@ -141,23 +148,24 @@ pub struct OpContextImpl<'a, 'ctrl, A: DigestAlgorithm + IntoHashAlgo> {
141148
_phantom: core::marker::PhantomData<A>,
142149
}
143150

144-
impl<'a, 'ctrl, A> proposed_traits::digest::ErrorType for OpContextImpl<'a, 'ctrl, A>
151+
impl<A> proposed_traits::digest::ErrorType for OpContextImpl<'_, '_, A>
145152
where
146153
A: DigestAlgorithm + IntoHashAlgo,
147154
{
148155
type Error = Infallible;
149156
}
150157

151-
impl<'a, 'ctrl, A> DigestOp for OpContextImpl<'a, 'ctrl, A>
158+
impl<A> DigestOp for OpContextImpl<'_, '_, A>
152159
where
153160
A: DigestAlgorithm + IntoHashAlgo,
154-
A::DigestOutput: Default + AsMut<[u8]>
161+
A::DigestOutput: Default + AsMut<[u8]>,
155162
{
156163
type Output = A::DigestOutput;
157164

158165
fn update(&mut self, _input: &[u8]) -> Result<(), Self::Error> {
159166
let input_len = _input.len() as u32;
160-
let (new_len, carry) = self.controller.ctx_mut().digcnt[0].overflowing_add(input_len as u64);
167+
let (new_len, carry) =
168+
self.controller.ctx_mut().digcnt[0].overflowing_add(input_len as u64);
161169

162170
self.controller.ctx_mut().digcnt[0] = new_len;
163171
if carry {
@@ -172,7 +180,8 @@ where
172180
return Ok(());
173181
}
174182

175-
let remaining = (input_len + self.controller.ctx_mut().bufcnt) % self.controller.ctx_mut().block_size;
183+
let remaining =
184+
(input_len + self.controller.ctx_mut().bufcnt) % self.controller.ctx_mut().block_size;
176185
let total_len = (input_len + self.controller.ctx_mut().bufcnt) - remaining;
177186
let mut i = 0;
178187

@@ -188,7 +197,8 @@ where
188197

189198
if total_len != self.controller.ctx_mut().bufcnt {
190199
self.controller.ctx_mut().sg[i].addr = _input.as_ptr() as u32;
191-
self.controller.ctx_mut().sg[i].len = (total_len - self.controller.ctx_mut().bufcnt) | HACE_SG_LAST;
200+
self.controller.ctx_mut().sg[i].len =
201+
(total_len - self.controller.ctx_mut().bufcnt) | HACE_SG_LAST;
192202
}
193203

194204
self.controller.start_hash_operation(total_len);
@@ -199,12 +209,11 @@ where
199209

200210
self.controller.ctx_mut().buffer[..(remaining as usize)]
201211
.copy_from_slice(&_input[src_start..src_end]);
202-
self.controller.ctx_mut().bufcnt = remaining as u32;
212+
self.controller.ctx_mut().bufcnt = remaining;
203213
}
204214
Ok(())
205215
}
206216

207-
208217
fn finalize(self) -> Result<Self::Output, Self::Error> {
209218
self.controller.fill_padding(0);
210219
let digest_len = self.controller.algo.digest_size();
@@ -220,12 +229,10 @@ where
220229

221230
self.controller.start_hash_operation(bufcnt);
222231

223-
let slice = unsafe {
224-
core::slice::from_raw_parts(digest_ptr, digest_len)
225-
};
232+
let slice = unsafe { core::slice::from_raw_parts(digest_ptr, digest_len) };
226233

227-
let mut output = A::DigestOutput::default();
228-
output.as_mut()[..digest_len].copy_from_slice(slice);
234+
let mut output = A::DigestOutput::default();
235+
output.as_mut()[..digest_len].copy_from_slice(slice);
229236

230237
let ctx = self.controller.ctx_mut();
231238
ctx.bufcnt = 0;

src/hmac.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use proposed_traits::mac::*;
2-
use core::convert::Infallible;
31
use crate::hace_controller::{HaceController, HashAlgo, HACE_SG_EN};
2+
use core::convert::Infallible;
3+
use proposed_traits::mac::*;
44

55
// MacAlgorithm implementation for HashAlgo
66
impl MacAlgorithm for HashAlgo {
@@ -52,7 +52,6 @@ impl AsMut<[u8]> for Digest64 {
5252
}
5353
}
5454

55-
5655
pub struct Sha1;
5756
pub struct Sha224;
5857
pub struct Sha256;
@@ -90,15 +89,21 @@ impl MacAlgorithm for Sha512 {
9089
}
9190

9291
impl Default for Sha256 {
93-
fn default() -> Self { Sha256 }
92+
fn default() -> Self {
93+
Sha256
94+
}
9495
}
9596

9697
impl Default for Sha384 {
97-
fn default() -> Self { Sha384 }
98+
fn default() -> Self {
99+
Sha384
100+
}
98101
}
99102

100103
impl Default for Sha512 {
101-
fn default() -> Self { Sha512 }
104+
fn default() -> Self {
105+
Sha512
106+
}
102107
}
103108

104109
impl IntoHashAlgo for Sha256 {
@@ -125,7 +130,10 @@ where
125130
A::MacOutput: Default + AsMut<[u8]>,
126131
A::Key: AsRef<[u8]>,
127132
{
128-
type OpContext<'a> = OpContextImpl<'a, 'ctrl, A> where Self: 'a; // Define your OpContext type here
133+
type OpContext<'a>
134+
= OpContextImpl<'a, 'ctrl, A>
135+
where
136+
Self: 'a; // Define your OpContext type here
129137

130138
fn init<'a>(&'a mut self, _algo: A, key: &A::Key) -> Result<Self::OpContext<'a>, Self::Error> {
131139
self.algo = A::to_hash_algo();
@@ -167,17 +175,17 @@ pub struct OpContextImpl<'a, 'ctrl, A: MacAlgorithm + IntoHashAlgo> {
167175
_phantom: core::marker::PhantomData<A>,
168176
}
169177

170-
impl<'a, 'ctrl, A> ErrorType for OpContextImpl<'a, 'ctrl, A>
178+
impl<A> ErrorType for OpContextImpl<'_, '_, A>
171179
where
172180
A: MacAlgorithm + IntoHashAlgo,
173181
{
174182
type Error = Infallible;
175183
}
176184

177-
impl<'a, 'ctrl, A> MacOp for OpContextImpl<'a, 'ctrl, A>
185+
impl<A> MacOp for OpContextImpl<'_, '_, A>
178186
where
179187
A: MacAlgorithm + IntoHashAlgo,
180-
A::MacOutput: Default + AsMut<[u8]>
188+
A::MacOutput: Default + AsMut<[u8]>,
181189
{
182190
type Output = A::MacOutput;
183191

@@ -206,18 +214,16 @@ where
206214
bufcnt = ctrl.ctx_mut().bufcnt;
207215
ctrl.copy_iv_to_digest();
208216
ctrl.start_hash_operation(bufcnt);
209-
let slice = unsafe {
210-
core::slice::from_raw_parts(ctrl.ctx_mut().digest.as_ptr(), digest_size)
211-
};
217+
let slice =
218+
unsafe { core::slice::from_raw_parts(ctrl.ctx_mut().digest.as_ptr(), digest_size) };
212219

213220
// H(opad + H(opad + hash sum))
214221
{
215222
let ctx = ctrl.ctx_mut();
216223
ctx.digcnt[0] = block_size as u64 + digest_size as u64;
217224
ctx.bufcnt = block_size as u32 + digest_size as u32;
218225
ctx.buffer[..block_size].copy_from_slice(&ctx.opad[..block_size]);
219-
ctx.buffer[block_size..(block_size + digest_size)]
220-
.copy_from_slice(slice);
226+
ctx.buffer[block_size..(block_size + digest_size)].copy_from_slice(slice);
221227
}
222228
ctrl.fill_padding(0);
223229
bufcnt = ctrl.ctx_mut().bufcnt;
@@ -231,10 +237,7 @@ where
231237
let digest_size = self.controller.algo.digest_size();
232238
let ctx = self.controller.ctx_mut();
233239

234-
235-
let slice = unsafe {
236-
core::slice::from_raw_parts(ctx.digest.as_ptr(), digest_size)
237-
};
240+
let slice = unsafe { core::slice::from_raw_parts(ctx.digest.as_ptr(), digest_size) };
238241

239242
let mut output = A::MacOutput::default();
240243
output.as_mut()[..digest_size].copy_from_slice(slice);

0 commit comments

Comments
 (0)