|
4 | 4 | //! groups. Additionally, it is not recommended to use `G_1024` and `G_1536`, |
5 | 5 | //! they are provided only for compatibility with the legacy software. |
6 | 6 |
|
7 | | -use crate::types::SrpGroup; |
8 | | -use crypto_bigint::BoxedUint; |
| 7 | +use crypto_bigint::{ |
| 8 | + BoxedUint, Odd, Resize, |
| 9 | + modular::{BoxedMontyForm, BoxedMontyParams}, |
| 10 | +}; |
9 | 11 | use once_cell::sync::Lazy; |
10 | 12 |
|
| 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 | + |
11 | 31 | pub static G_1024: Lazy<SrpGroup> = Lazy::new(|| { |
12 | 32 | SrpGroup::new( |
13 | 33 | BoxedUint::from_be_slice_vartime(include_bytes!("groups/1024.bin")), |
@@ -43,16 +63,15 @@ pub static G_4096: Lazy<SrpGroup> = Lazy::new(|| { |
43 | 63 | ) |
44 | 64 | }); |
45 | 65 |
|
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; |
52 | 71 |
|
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 | +} |
0 commit comments