Skip to content

Commit 61738f7

Browse files
committed
clippy: Fix warnings
Fix all warnings reported by `cargo xtask clippy`, including try_from usage, redundant match arms, and unnecessary mut refs. Signed-off-by: Steven Lee <steven_lee@aspeedtech.com>
1 parent 8e084a4 commit 61738f7

File tree

13 files changed

+181
-84
lines changed

13 files changed

+181
-84
lines changed

src/ecdsa.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl Error for AspeedEcdsaError {
217217
match self {
218218
Self::InvalidSignature => ErrorKind::InvalidSignature,
219219
Self::Busy => ErrorKind::Busy,
220-
_ => ErrorKind::Other,
220+
Self::BadInput => ErrorKind::Other,
221221
}
222222
}
223223
}
@@ -246,26 +246,22 @@ impl<'a, D: DelayNs> AspeedEcdsa<'a, D> {
246246
}
247247
}
248248

249-
#[inline(always)]
250249
fn sec_rd(&self, offset: usize) -> u32 {
251250
unsafe { read_volatile(self.ecdsa_base.as_ptr().add(offset / 4)) }
252251
}
253252

254-
#[inline(always)]
255253
fn sec_wr(&self, offset: usize, val: u32) {
256254
unsafe {
257255
write_volatile(self.ecdsa_base.as_ptr().add(offset / 4), val);
258256
}
259257
}
260258

261-
#[inline(always)]
262259
fn sram_wr_u32(&self, offset: usize, val: u32) {
263260
unsafe {
264261
write_volatile(self.sram_base.as_ptr().add(offset / 4), val);
265262
}
266263
}
267264

268-
#[inline(always)]
269265
fn sram_wr(&self, offset: usize, data: &[u8; Scalar48::LEN]) {
270266
for i in (0..Scalar48::LEN).step_by(4) {
271267
let val = u32::from_le_bytes([data[i], data[i + 1], data[i + 2], data[i + 3]]);

src/hace_controller.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pub struct AspeedSg {
151151
}
152152

153153
impl AspeedSg {
154+
#[must_use]
154155
pub const fn new() -> Self {
155156
Self { len: 0, addr: 0 }
156157
}
@@ -193,6 +194,7 @@ impl Default for AspeedHashContext {
193194
}
194195

195196
impl AspeedHashContext {
197+
#[must_use]
196198
pub const fn new() -> Self {
197199
Self {
198200
sg: [AspeedSg::new(), AspeedSg::new()],
@@ -244,6 +246,7 @@ pub enum HashAlgo {
244246
}
245247

246248
impl HashAlgo {
249+
#[must_use]
247250
pub const fn digest_size(&self) -> usize {
248251
match self {
249252
HashAlgo::SHA1 => 20,
@@ -254,6 +257,7 @@ impl HashAlgo {
254257
}
255258
}
256259

260+
#[must_use]
257261
pub const fn block_size(&self) -> usize {
258262
match self {
259263
HashAlgo::SHA1 | HashAlgo::SHA224 | HashAlgo::SHA256 => 64,
@@ -263,6 +267,7 @@ impl HashAlgo {
263267
}
264268
}
265269

270+
#[must_use]
266271
pub const fn bitmask(&self) -> u32 {
267272
match self {
268273
HashAlgo::SHA1 => HACE_ALGO_SHA1,
@@ -275,6 +280,7 @@ impl HashAlgo {
275280
}
276281
}
277282

283+
#[must_use]
278284
pub const fn iv(&self) -> &'static [u32] {
279285
match self {
280286
HashAlgo::SHA1 => &SHA1_IV,
@@ -287,6 +293,7 @@ impl HashAlgo {
287293
}
288294
}
289295

296+
#[must_use]
290297
pub const fn iv_size(&self) -> usize {
291298
match self {
292299
HashAlgo::SHA1 => SHA1_IV.len(),
@@ -299,6 +306,7 @@ impl HashAlgo {
299306
}
300307
}
301308

309+
#[must_use]
302310
pub fn hash_cmd(&self) -> u32 {
303311
const COMMON_FLAGS: u32 = HACE_CMD_ACC_MODE | HACE_SHA_BE_EN | HACE_SG_EN;
304312
COMMON_FLAGS | self.bitmask()
@@ -312,6 +320,7 @@ pub struct HaceController<'ctrl> {
312320
}
313321

314322
impl<'ctrl> HaceController<'ctrl> {
323+
#[must_use]
315324
pub fn new(hace: &'ctrl Hace) -> Self {
316325
Self {
317326
hace,
@@ -370,7 +379,7 @@ impl HaceController<'_> {
370379
pub fn copy_iv_to_digest(&mut self) {
371380
let iv = self.algo.iv();
372381
let iv_bytes =
373-
unsafe { core::slice::from_raw_parts(iv.as_ptr() as *const u8, iv.len() * 4) };
382+
unsafe { core::slice::from_raw_parts(iv.as_ptr().cast::<u8>(), iv.len() * 4) };
374383

375384
self.ctx_mut().digest[..iv_bytes.len()].copy_from_slice(iv_bytes);
376385
}
@@ -381,7 +390,7 @@ impl HaceController<'_> {
381390
let digest_len = self.algo.digest_size();
382391

383392
self.ctx_mut().digcnt[0] = key_len as u64;
384-
self.ctx_mut().bufcnt = key_len as u32;
393+
self.ctx_mut().bufcnt = u32::try_from(key_len).expect("key_len too large to fit in u32");
385394
self.ctx_mut().buffer[..key_len].copy_from_slice(key_bytes);
386395
self.ctx_mut().method &= !HACE_SG_EN; // Disable SG mode for key hashing
387396
self.copy_iv_to_digest();
@@ -395,11 +404,12 @@ impl HaceController<'_> {
395404
self.ctx_mut().key[..digest_len].copy_from_slice(slice);
396405
self.ctx_mut().ipad[..digest_len].copy_from_slice(slice);
397406
self.ctx_mut().opad[..digest_len].copy_from_slice(slice);
398-
self.ctx_mut().key_len = digest_len as u32;
407+
self.ctx_mut().key_len =
408+
u32::try_from(digest_len).expect("digest_len too large to fit in u32");
399409
}
400410

401411
pub fn fill_padding(&mut self, remaining: usize) {
402-
let ctx = &mut self.ctx_mut();
412+
let ctx = self.ctx_mut();
403413
let block_size = ctx.block_size as usize;
404414
let bufcnt = ctx.bufcnt as usize;
405415

@@ -422,15 +432,15 @@ impl HaceController<'_> {
422432
if block_size == 64 {
423433
let bits = (ctx.digcnt[0] << 3).to_be_bytes();
424434
ctx.buffer[bufcnt + padlen..bufcnt + padlen + 8].copy_from_slice(&bits);
425-
ctx.bufcnt += (padlen + 8) as u32;
435+
ctx.bufcnt += u32::try_from(padlen + 8).expect("padlen + 8 too large to fit in u32");
426436
} else {
427437
let low = (ctx.digcnt[0] << 3).to_be_bytes();
428438
let high = ((ctx.digcnt[1] << 3) | (ctx.digcnt[0] >> 61)).to_be_bytes();
429439

430440
ctx.buffer[bufcnt + padlen..bufcnt + padlen + 8].copy_from_slice(&high);
431441
ctx.buffer[bufcnt + padlen + 8..bufcnt + padlen + 16].copy_from_slice(&low);
432442

433-
ctx.bufcnt += (padlen + 16) as u32;
443+
ctx.bufcnt += u32::try_from(padlen + 16).expect("padlen + 16 too large to fit in u32");
434444
}
435445
}
436446
}

src/hash.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::hace_controller::{ContextCleanup, HaceController, HashAlgo, HACE_SG_LAST};
2-
use core::convert::Infallible;
3-
use proposed_traits::digest::*;
2+
use proposed_traits::digest::{DigestAlgorithm, DigestInit, DigestOp, Error, ErrorKind, ErrorType};
43

54
// DigestAlgorithm implementation for HashAlgo
65
impl DigestAlgorithm for HashAlgo {
@@ -132,7 +131,7 @@ where
132131
self.algo = A::to_hash_algo();
133132
self.ctx_mut().method = self.algo.hash_cmd();
134133
self.copy_iv_to_digest();
135-
self.ctx_mut().block_size = self.algo.block_size() as u32;
134+
self.ctx_mut().block_size = u32::try_from(self.algo.block_size()).unwrap();
136135
self.ctx_mut().bufcnt = 0;
137136
self.ctx_mut().digcnt = [0; 2];
138137

@@ -148,11 +147,26 @@ pub struct OpContextImpl<'a, 'ctrl, A: DigestAlgorithm + IntoHashAlgo> {
148147
_phantom: core::marker::PhantomData<A>,
149148
}
150149

151-
impl<A> proposed_traits::digest::ErrorType for OpContextImpl<'_, '_, A>
150+
#[derive(Debug)]
151+
pub struct HashError(pub ErrorKind);
152+
153+
impl Error for HashError {
154+
fn kind(&self) -> ErrorKind {
155+
self.0
156+
}
157+
}
158+
159+
impl From<ErrorKind> for HashError {
160+
fn from(kind: ErrorKind) -> Self {
161+
HashError(kind)
162+
}
163+
}
164+
165+
impl<A> ErrorType for OpContextImpl<'_, '_, A>
152166
where
153167
A: DigestAlgorithm + IntoHashAlgo,
154168
{
155-
type Error = Infallible;
169+
type Error = HashError;
156170
}
157171

158172
impl<A> DigestOp for OpContextImpl<'_, '_, A>
@@ -162,10 +176,11 @@ where
162176
{
163177
type Output = A::DigestOutput;
164178

165-
fn update(&mut self, _input: &[u8]) -> Result<(), Self::Error> {
166-
let input_len = _input.len() as u32;
179+
fn update(&mut self, input: &[u8]) -> Result<(), Self::Error> {
180+
let input_len = u32::try_from(input.len()).map_err(|_| ErrorKind::InvalidInputLength)?;
181+
167182
let (new_len, carry) =
168-
self.controller.ctx_mut().digcnt[0].overflowing_add(input_len as u64);
183+
self.controller.ctx_mut().digcnt[0].overflowing_add(u64::from(input_len));
169184

170185
self.controller.ctx_mut().digcnt[0] = new_len;
171186
if carry {
@@ -175,7 +190,7 @@ where
175190
let start = self.controller.ctx_mut().bufcnt as usize;
176191
let end = start + input_len as usize;
177192
if self.controller.ctx_mut().bufcnt + input_len < self.controller.ctx_mut().block_size {
178-
self.controller.ctx_mut().buffer[start..end].copy_from_slice(_input);
193+
self.controller.ctx_mut().buffer[start..end].copy_from_slice(input);
179194
self.controller.ctx_mut().bufcnt += input_len;
180195
return Ok(());
181196
}
@@ -189,14 +204,14 @@ where
189204
self.controller.ctx_mut().sg[0].addr = self.controller.ctx_mut().buffer.as_ptr() as u32;
190205
self.controller.ctx_mut().sg[0].len = self.controller.ctx_mut().bufcnt;
191206
if total_len == self.controller.ctx_mut().bufcnt {
192-
self.controller.ctx_mut().sg[0].addr = _input.as_ptr() as u32;
207+
self.controller.ctx_mut().sg[0].addr = input.as_ptr() as u32;
193208
self.controller.ctx_mut().sg[0].len |= HACE_SG_LAST;
194209
}
195210
i += 1;
196211
}
197212

198213
if total_len != self.controller.ctx_mut().bufcnt {
199-
self.controller.ctx_mut().sg[i].addr = _input.as_ptr() as u32;
214+
self.controller.ctx_mut().sg[i].addr = input.as_ptr() as u32;
200215
self.controller.ctx_mut().sg[i].len =
201216
(total_len - self.controller.ctx_mut().bufcnt) | HACE_SG_LAST;
202217
}
@@ -208,7 +223,7 @@ where
208223
let src_end = src_start + remaining as usize;
209224

210225
self.controller.ctx_mut().buffer[..(remaining as usize)]
211-
.copy_from_slice(&_input[src_start..src_end]);
226+
.copy_from_slice(&input[src_start..src_end]);
212227
self.controller.ctx_mut().bufcnt = remaining;
213228
}
214229
Ok(())

src/hmac.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::hace_controller::{ContextCleanup, HaceController, HashAlgo, HACE_SG_EN};
2-
use core::convert::Infallible;
3-
use proposed_traits::mac::*;
2+
use proposed_traits::mac::{Error, ErrorKind, ErrorType, MacAlgorithm, MacInit, MacOp};
43

54
// MacAlgorithm implementation for HashAlgo
65
impl MacAlgorithm for HashAlgo {
@@ -139,7 +138,7 @@ where
139138
self.algo = A::to_hash_algo();
140139
self.ctx_mut().method = self.algo.hash_cmd();
141140
self.copy_iv_to_digest();
142-
self.ctx_mut().block_size = self.algo.block_size() as u32;
141+
self.ctx_mut().block_size = u32::try_from(self.algo.block_size()).unwrap();
143142
self.ctx_mut().bufcnt = 0;
144143
self.ctx_mut().digcnt = [0; 2];
145144
self.ctx_mut().buffer.fill(0);
@@ -155,7 +154,7 @@ where
155154
self.ctx_mut().key[..key.as_ref().len()].copy_from_slice(key.as_ref());
156155
self.ctx_mut().ipad[..key.as_ref().len()].copy_from_slice(key.as_ref());
157156
self.ctx_mut().opad[..key.as_ref().len()].copy_from_slice(key.as_ref());
158-
self.ctx_mut().key_len = key.as_ref().len() as u32;
157+
self.ctx_mut().key_len = u32::try_from(key.as_ref().len()).unwrap();
159158
}
160159

161160
for i in 0..self.ctx_mut().block_size as usize {
@@ -175,11 +174,26 @@ pub struct OpContextImpl<'a, 'ctrl, A: MacAlgorithm + IntoHashAlgo> {
175174
_phantom: core::marker::PhantomData<A>,
176175
}
177176

177+
#[derive(Debug)]
178+
pub struct MacError(pub ErrorKind);
179+
180+
impl Error for MacError {
181+
fn kind(&self) -> ErrorKind {
182+
self.0
183+
}
184+
}
185+
186+
impl From<ErrorKind> for MacError {
187+
fn from(kind: ErrorKind) -> Self {
188+
MacError(kind)
189+
}
190+
}
191+
178192
impl<A> ErrorType for OpContextImpl<'_, '_, A>
179193
where
180194
A: MacAlgorithm + IntoHashAlgo,
181195
{
182-
type Error = Infallible;
196+
type Error = MacError;
183197
}
184198

185199
impl<A> MacOp for OpContextImpl<'_, '_, A>
@@ -190,7 +204,7 @@ where
190204
type Output = A::MacOutput;
191205

192206
fn update(&mut self, input: &[u8]) -> Result<(), Self::Error> {
193-
let ctrl = &mut self.controller;
207+
let ctrl: &mut HaceController = self.controller;
194208
let algo = ctrl.algo;
195209
let block_size = algo.block_size();
196210
let digest_size = algo.digest_size();
@@ -199,14 +213,16 @@ where
199213
{
200214
let ctx = ctrl.ctx_mut();
201215
ctx.digcnt[0] = block_size as u64;
202-
ctx.bufcnt = block_size as u32;
216+
ctx.bufcnt =
217+
u32::try_from(block_size).map_err(|_| MacError(ErrorKind::InvalidInputLength))?;
203218

204219
// H(ipad + input)
205220
let ipad = &ctx.ipad[..block_size];
206221
ctx.buffer[..algo.block_size()].copy_from_slice(ipad);
207222
ctx.buffer[algo.block_size()..(algo.block_size() + input.len())].copy_from_slice(input);
208223
ctx.digcnt[0] += input.len() as u64;
209-
ctx.bufcnt += input.len() as u32;
224+
ctx.bufcnt +=
225+
u32::try_from(input.len()).map_err(|_| MacError(ErrorKind::InvalidInputLength))?;
210226
ctx.method &= !HACE_SG_EN; // Disable SG mode for key hashing
211227
}
212228

@@ -221,7 +237,8 @@ where
221237
{
222238
let ctx = ctrl.ctx_mut();
223239
ctx.digcnt[0] = block_size as u64 + digest_size as u64;
224-
ctx.bufcnt = block_size as u32 + digest_size as u32;
240+
ctx.bufcnt = u32::try_from(block_size + digest_size)
241+
.map_err(|_| MacError(ErrorKind::UpdateError))?;
225242
ctx.buffer[..block_size].copy_from_slice(&ctx.opad[..block_size]);
226243
ctx.buffer[block_size..(block_size + digest_size)].copy_from_slice(slice);
227244
}

0 commit comments

Comments
 (0)