Skip to content

Commit 86fa82e

Browse files
authored
srp: eliminate allocations in AuthError (#236)
We can easily use `&'static str` instead
1 parent 66be5d1 commit 86fa82e

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

srp/src/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
//! ```
101101
//!
102102
103-
use alloc::{borrow::ToOwned, vec::Vec};
103+
use alloc::vec::Vec;
104104
use core::marker::PhantomData;
105105
use crypto_bigint::{BoxedUint, ConcatenatingMul, Odd, Resize, modular::BoxedMontyForm};
106106
use digest::{Digest, Output};
@@ -325,7 +325,7 @@ impl<G: Group, D: Digest> Client<G, D> {
325325
let n = self.n().as_nz_ref();
326326

327327
if (b_pub.resize(n.bits_precision()) % n).is_zero().into() {
328-
return Err(AuthError::IllegalParameter("b_pub".to_owned()));
328+
return Err(AuthError::IllegalParameter { name: "b_pub" });
329329
}
330330

331331
Ok(())
@@ -363,7 +363,7 @@ impl<D: Digest> ClientVerifier<D> {
363363
if self.m2.ct_eq(reply).unwrap_u8() == 1 {
364364
Ok(())
365365
} else {
366-
Err(AuthError::BadRecordMac("server".to_owned()))
366+
Err(AuthError::BadRecordMac { peer: "server" })
367367
}
368368
}
369369
}
@@ -394,7 +394,7 @@ impl<D: Digest> ClientVerifierRfc5054<D> {
394394
if self.m2.ct_eq(reply).unwrap_u8() == 1 {
395395
Ok(self.session_key.as_slice())
396396
} else {
397-
Err(AuthError::BadRecordMac("server".to_owned()))
397+
Err(AuthError::BadRecordMac { peer: "server" })
398398
}
399399
}
400400
}

srp/src/errors.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
//! Error types.
22
3-
use alloc::string::String;
43
use core::{error, fmt};
54

65
/// SRP authentication error.
76
#[derive(Debug, Clone, Eq, PartialEq)]
87
pub enum AuthError {
9-
IllegalParameter(String),
10-
BadRecordMac(String),
8+
IllegalParameter {
9+
/// Parameter name
10+
name: &'static str,
11+
},
12+
BadRecordMac {
13+
/// Which peer's proof is invalid
14+
peer: &'static str,
15+
},
1116
}
1217

1318
impl fmt::Display for AuthError {
1419
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1520
match self {
16-
Self::IllegalParameter(param) => {
17-
write!(f, "illegal_parameter: bad '{param}' value")
21+
Self::IllegalParameter { name } => {
22+
write!(f, "illegal_parameter: bad '{name}' value")
1823
}
19-
Self::BadRecordMac(param) => {
20-
write!(f, "bad_record_mac: incorrect '{param}' proof")
24+
Self::BadRecordMac { peer } => {
25+
write!(f, "bad_record_mac: incorrect '{peer}' proof")
2126
}
2227
}
2328
}

srp/src/server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
//! send_proof(verifier.proof());
7878
//! ```
7979
//!
80-
use alloc::{borrow::ToOwned, vec::Vec};
80+
use alloc::vec::Vec;
8181
use core::marker::PhantomData;
8282

8383
use crypto_bigint::{BoxedUint, Odd, Resize, modular::BoxedMontyForm};
@@ -258,7 +258,7 @@ impl<G: Group, D: Digest> Server<G, D> {
258258
let n = self.n().as_nz_ref();
259259

260260
if (a_pub.resize(n.bits_precision()) % n).is_zero().into() {
261-
return Err(AuthError::IllegalParameter("a_pub".to_owned()));
261+
return Err(AuthError::IllegalParameter { name: "a_pub" });
262262
}
263263

264264
Ok(())
@@ -296,7 +296,7 @@ impl<D: Digest> ServerVerifier<D> {
296296
if self.m1.ct_eq(reply).unwrap_u8() == 1 {
297297
Ok(())
298298
} else {
299-
Err(AuthError::BadRecordMac("client".to_owned()))
299+
Err(AuthError::BadRecordMac { peer: "client" })
300300
}
301301
}
302302
}
@@ -327,7 +327,7 @@ impl<D: Digest> ServerVerifierRfc5054<D> {
327327
if self.m1.ct_eq(reply).unwrap_u8() == 1 {
328328
Ok(self.session_key.as_slice())
329329
} else {
330-
Err(AuthError::BadRecordMac("client".to_owned()))
330+
Err(AuthError::BadRecordMac { peer: "client" })
331331
}
332332
}
333333
}

0 commit comments

Comments
 (0)