Skip to content

Commit a3e3e7e

Browse files
authored
srp: refactor groups and errors (#233)
Factors apart the junk drawer `types` module, moving the `SrpGroup` type into the `groups` module, and renaming the remaining `types` module into an `errors` module.
1 parent f983ac3 commit a3e3e7e

File tree

7 files changed

+67
-79
lines changed

7 files changed

+67
-79
lines changed

srp/src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ use digest::{Digest, Output};
107107
use subtle::ConstantTimeEq;
108108

109109
use crate::{
110-
types::{SrpAuthError, SrpGroup},
110+
SrpGroup,
111+
errors::SrpAuthError,
111112
utils::{compute_hash, compute_k, compute_m1, compute_m1_rfc5054, compute_m2, compute_u},
112113
};
113114

srp/src/errors.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Error types.
2+
3+
use alloc::string::String;
4+
use core::{error, fmt};
5+
6+
/// SRP authentication error.
7+
#[derive(Debug, Clone, Eq, PartialEq)]
8+
pub enum SrpAuthError {
9+
IllegalParameter(String),
10+
BadRecordMac(String),
11+
}
12+
13+
impl fmt::Display for SrpAuthError {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
match self {
16+
Self::IllegalParameter(param) => {
17+
write!(f, "illegal_parameter: bad '{param}' value")
18+
}
19+
Self::BadRecordMac(param) => {
20+
write!(f, "bad_record_mac: incorrect '{param}' proof")
21+
}
22+
}
23+
}
24+
}
25+
26+
impl error::Error for SrpAuthError {}

srp/src/groups.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,30 @@
44
//! groups. Additionally, it is not recommended to use `G_1024` and `G_1536`,
55
//! they are provided only for compatibility with the legacy software.
66
7-
use crate::types::SrpGroup;
8-
use crypto_bigint::BoxedUint;
7+
use crypto_bigint::{
8+
BoxedUint, Odd, Resize,
9+
modular::{BoxedMontyForm, BoxedMontyParams},
10+
};
911
use once_cell::sync::Lazy;
1012

13+
/// Group used for SRP computations
14+
#[derive(Debug, Clone, Eq, PartialEq)]
15+
pub struct SrpGroup {
16+
/// A large safe prime (N = 2q+1, where q is prime)
17+
pub n: BoxedMontyParams,
18+
/// A generator modulo N
19+
pub g: BoxedMontyForm,
20+
}
21+
22+
impl SrpGroup {
23+
/// Initialize a new group from the given boxed integers.
24+
pub fn new(n: BoxedUint, g: BoxedUint) -> Self {
25+
let n = BoxedMontyParams::new(Odd::new(n).expect("n should be odd"));
26+
let g = BoxedMontyForm::new(g.resize(n.bits_precision()), &n);
27+
Self { n, g }
28+
}
29+
}
30+
1131
pub static G_1024: Lazy<SrpGroup> = Lazy::new(|| {
1232
SrpGroup::new(
1333
BoxedUint::from_be_slice_vartime(include_bytes!("groups/1024.bin")),
@@ -43,16 +63,15 @@ pub static G_4096: Lazy<SrpGroup> = Lazy::new(|| {
4363
)
4464
});
4565

46-
pub static G_6144: Lazy<SrpGroup> = Lazy::new(|| {
47-
SrpGroup::new(
48-
BoxedUint::from_be_slice_vartime(include_bytes!("groups/6144.bin")),
49-
BoxedUint::from_be_slice_vartime(&[5]),
50-
)
51-
});
66+
#[cfg(test)]
67+
mod tests {
68+
use crate::groups::G_1024;
69+
use crate::utils::compute_k;
70+
use sha1::Sha1;
5271

53-
pub static G_8192: Lazy<SrpGroup> = Lazy::new(|| {
54-
SrpGroup::new(
55-
BoxedUint::from_be_slice_vartime(include_bytes!("groups/8192.bin")),
56-
BoxedUint::from_be_slice_vartime(&[19]),
57-
)
58-
});
72+
#[test]
73+
fn test_k_1024_sha1() {
74+
let k = compute_k::<Sha1>(&G_1024).to_be_bytes_trimmed_vartime();
75+
assert_eq!(&*k, include_bytes!("test/k_sha1_1024.bin"));
76+
}
77+
}

srp/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
extern crate alloc;
5959

6060
pub mod client;
61+
pub mod errors;
6162
pub mod groups;
6263
pub mod server;
63-
pub mod types;
6464
pub mod utils;
65+
66+
pub use groups::SrpGroup;

srp/src/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ use digest::{Digest, Output};
8585
use subtle::ConstantTimeEq;
8686

8787
use crate::{
88-
types::{SrpAuthError, SrpGroup},
88+
SrpGroup,
89+
errors::SrpAuthError,
8990
utils::{compute_hash, compute_k, compute_m1, compute_m1_rfc5054, compute_m2, compute_u},
9091
};
9192

srp/src/types.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

srp/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloc::vec::Vec;
22
use crypto_bigint::BoxedUint;
33
use digest::{Digest, Output};
44

5-
use crate::types::SrpGroup;
5+
use crate::groups::SrpGroup;
66

77
// u = H(PAD(A) | PAD(B))
88
#[must_use]

0 commit comments

Comments
 (0)