Skip to content

Commit 52ef2be

Browse files
authored
srp: Client* and Server* type aliases (#245)
Adds type aliases (still parameterized by a generic `D`) which are already configured with the appropriate SRP groups, e.g `ClientG2048`, `ServerG2048`.
1 parent 695e390 commit 52ef2be

File tree

7 files changed

+54
-42
lines changed

7 files changed

+54
-42
lines changed

srp/src/client.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
use crate::{
2-
Group,
3-
errors::AuthError,
4-
utils::{
5-
compute_hash, compute_k, compute_m1_legacy, compute_m1_rfc5054, compute_m2, compute_u,
6-
monty_form,
7-
},
8-
};
1+
use crate::{errors::AuthError, groups::*, utils::*};
92
use alloc::vec::Vec;
103
use core::marker::PhantomData;
114
use crypto_bigint::{BoxedUint, ConcatenatingMul, Odd, Resize, modular::BoxedMontyForm};
125
use digest::{Digest, Output};
136
use subtle::ConstantTimeEq;
147

8+
/// SRP client configured with a standard 1024-bit group.
9+
pub type ClientG1024<D> = Client<G1024, D>;
10+
/// SRP client configured with a standard 1536-bit group.
11+
pub type ClientG1536<D> = Client<G1536, D>;
12+
/// SRP client configured with a standard 2048-bit group.
13+
pub type ClientG2048<D> = Client<G2048, D>;
14+
/// SRP client configured with a standard 3072-bit group.
15+
pub type ClientG3072<D> = Client<G3072, D>;
16+
/// SRP client configured with a standard 4096-bit group.
17+
pub type ClientG4096<D> = Client<G4096, D>;
18+
1519
/// SRP client implementation.
1620
///
1721
/// # Usage
@@ -22,17 +26,16 @@ use subtle::ConstantTimeEq;
2226
/// password hashing algorithm instead (e.g. PBKDF2, argon2 or scrypt).
2327
///
2428
/// ```rust
25-
/// use srp::{G2048, Client};
2629
/// use sha2::Sha256;
2730
///
28-
/// let client = Client::<G2048, Sha256>::new();
31+
/// let client = srp::ClientG2048::<Sha256>::new();
2932
/// ```
3033
///
3134
/// Next send handshake data (username and `a_pub`) to the server and receive
3235
/// `salt` and `b_pub`:
3336
///
3437
/// ```rust
35-
/// # let client = srp::Client::<srp::G2048, sha2::Sha256>::new();
38+
/// # let client = srp::ClientG2048::<sha2::Sha256>::new();
3639
/// # fn server_response()-> (Vec<u8>, Vec<u8>) { (vec![], vec![]) }
3740
///
3841
/// let mut a = [0u8; 64];
@@ -45,7 +48,7 @@ use subtle::ConstantTimeEq;
4548
/// `process_reply` can return error in case of malicious `b_pub`.
4649
///
4750
/// ```rust
48-
/// # let client = srp::Client::<srp::G2048, sha2::Sha256>::new();
51+
/// # let client = srp::ClientG2048::<sha2::Sha256>::new();
4952
/// # let a = [0u8; 64];
5053
/// # let username = b"username";
5154
/// # let password = b"password";
@@ -61,7 +64,7 @@ use subtle::ConstantTimeEq;
6164
/// `verify_server` method will return error in case of incorrect server reply.
6265
///
6366
/// ```ignore
64-
/// # let client = srp::Client::<srp::G2048, sha2::Sha256>::new();
67+
/// # let client = srp::ClientG2048::<sha2::Sha256>::new();
6568
/// # let verifier = client.process_reply(b"", b"", b"", b"", b"1").unwrap();
6669
/// # fn send_proof(_: &[u8]) -> Vec<u8> { vec![173, 202, 13, 26, 207, 73, 0, 46, 121, 238, 48, 170, 96, 146, 60, 49, 88, 76, 12, 184, 152, 76, 207, 220, 140, 205, 190, 189, 117, 6, 131, 63] }
6770
///
@@ -73,7 +76,7 @@ use subtle::ConstantTimeEq;
7376
/// `key` contains shared secret key between user and the server. You can extract shared secret
7477
/// key using `key()` method.
7578
/// ```rust
76-
/// # let client = srp::Client::<srp::G2048, sha2::Sha256>::new();
79+
/// # let client = srp::ClientG2048::<sha2::Sha256>::new();
7780
/// # let verifier = client.process_reply_legacy(b"", b"", b"", b"", b"1").unwrap();
7881
///
7982
/// verifier.key();
@@ -86,7 +89,7 @@ use subtle::ConstantTimeEq;
8689
/// key in case of correct server data.
8790
///
8891
/// ```ignore
89-
/// # let client = srp::Client::<srp::G2048, sha2::Sha256>::new();
92+
/// # let client = srp::ClientG2048::<sha2::Sha256>::new();
9093
/// # let verifier = client.process_reply_rfc5054(b"", b"", b"", b"", b"1").unwrap();
9194
/// # fn send_proof(_: &[u8]) -> Vec<u8> { vec![10, 215, 214, 40, 136, 200, 122, 121, 68, 160, 38, 32, 85, 82, 128, 30, 199, 194, 126, 222, 61, 55, 2, 28, 120, 181, 155, 102, 141, 65, 17, 64] }
9295
///
@@ -102,7 +105,7 @@ use subtle::ConstantTimeEq;
102105
/// Man-in-the-middle (MITM) attack for registration.
103106
///
104107
/// ```rust
105-
/// # let client = srp::Client::<srp::G2048, sha2::Sha256>::new();
108+
/// # let client = srp::ClientG2048::<sha2::Sha256>::new();
106109
/// # let username = b"username";
107110
/// # let password = b"password";
108111
/// # let salt = b"salt";

srp/src/groups.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Groups from [RFC 5054](https://tools.ietf.org/html/rfc5054)
1+
//! Groups from [RFC5054](https://tools.ietf.org/html/rfc5054)
22
//!
33
//! It is strongly recommended to use them instead of custom generated
4-
//! groups. Additionally, it is not recommended to use `G1024` and `G_1536`,
4+
//! groups. Additionally, it is not recommended to use `G1024` and `G1536`,
55
//! they are provided only for compatibility with the legacy software.
66
77
use core::{

srp/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
//! (`N = 2q + 1`, where `q` is prime).
3535
//!
3636
//! Additionally, `g` MUST be a generator modulo `N`. It's STRONGLY recommended to use SRP
37-
//! parameters provided by this crate as [`G1024`], [`G1536`], [`G2048`], [`G3072`] and [`G4096`].
37+
//! parameters provided by this crate under the [`groups`] module, and available as various
38+
//! type aliases e.g. [`ClientG2048`] and [`ServerG2048`].
3839
//!
3940
//! | Client | Data transfer | Server |
4041
//! |------------------------|-------------------|---------------------------------|
@@ -69,17 +70,22 @@
6970
#[macro_use]
7071
extern crate alloc;
7172

73+
pub mod groups;
74+
#[doc(hidden)]
75+
pub mod utils;
76+
7277
mod client;
7378
mod errors;
74-
mod groups;
7579
mod server;
76-
#[doc(hidden)]
77-
pub mod utils;
7880

79-
pub use client::{Client, ClientVerifier};
81+
pub use client::{
82+
Client, ClientG1024, ClientG1536, ClientG2048, ClientG3072, ClientG4096, ClientVerifier,
83+
};
8084
pub use errors::AuthError;
81-
pub use groups::*;
82-
pub use server::{Server, ServerVerifier};
85+
pub use groups::Group;
86+
pub use server::{
87+
Server, ServerG1024, ServerG1536, ServerG2048, ServerG3072, ServerG4096, ServerVerifier,
88+
};
8389

8490
#[allow(deprecated)]
8591
pub use {client::LegacyClientVerifier, server::LegacyServerVerifier};

srp/src/server.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
use crate::{
2-
Group,
3-
errors::AuthError,
4-
utils::{
5-
compute_hash, compute_k, compute_m1_legacy, compute_m1_rfc5054, compute_m2, compute_u,
6-
monty_form,
7-
},
8-
};
1+
use crate::{errors::AuthError, groups::*, utils::*};
92
use alloc::vec::Vec;
103
use core::marker::PhantomData;
114
use crypto_bigint::{BoxedUint, Odd, Resize, modular::BoxedMontyForm};
125
use digest::{Digest, Output};
136
use subtle::ConstantTimeEq;
147

8+
/// SRP server configured with a standard [`G1024`] group.
9+
pub type ServerG1024<D> = Server<G1024, D>;
10+
/// SRP server configured with a standard [`G1536`] group.
11+
pub type ServerG1536<D> = Server<G1536, D>;
12+
/// SRP server configured with a standard [`G2048`] group.
13+
pub type ServerG2048<D> = Server<G2048, D>;
14+
/// SRP server configured with a standard [`G3072`] group.
15+
pub type ServerG3072<D> = Server<G3072, D>;
16+
/// SRP server configured with a standard [`G4096`] group.
17+
pub type ServerG4096<D> = Server<G4096, D>;
18+
1519
/// SRP server implementation.
1620
///
1721
/// # Usage
@@ -20,12 +24,11 @@ use subtle::ConstantTimeEq;
2024
///
2125
///
2226
/// ```rust
23-
/// use srp::{G2048, Server};
2427
/// use sha2::Sha256;
2528
/// # fn get_client_request()-> (Vec<u8>, Vec<u8>) { (vec![], vec![])}
2629
/// # fn get_user(_: &[u8])-> (Vec<u8>, Vec<u8>) { (vec![], vec![])}
2730
///
28-
/// let server = Server::<G2048, Sha256>::new();
31+
/// let server = srp::ServerG2048::<Sha256>::new();
2932
/// let (username, a_pub) = get_client_request();
3033
/// let (salt, v) = get_user(&username);
3134
/// let mut b = [0u8; 64];
@@ -38,7 +41,7 @@ use subtle::ConstantTimeEq;
3841
/// Next process the user response:
3942
///
4043
/// ```rust
41-
/// # let server = srp::Server::<srp::G2048, sha2::Sha256>::new();
44+
/// # let server = srp::ServerG2048::<sha2::Sha256>::new();
4245
/// # fn get_client_response() -> Vec<u8> { vec![1] }
4346
/// # let b = [0u8; 64];
4447
/// # let v = b"";
@@ -52,7 +55,7 @@ use subtle::ConstantTimeEq;
5255
/// reply:
5356
///
5457
/// ```rust
55-
/// # let server = srp::Server::<srp::G2048, sha2::Sha256>::new();
58+
/// # let server = srp::ServerG2048::<sha2::Sha256>::new();
5659
/// # let verifier = server.process_reply_legacy(b"", b"", b"1").unwrap();
5760
/// # fn get_client_proof()-> Vec<u8> { vec![26, 80, 8, 243, 111, 162, 238, 171, 208, 237, 207, 46, 46, 137, 44, 213, 105, 208, 84, 224, 244, 216, 103, 145, 14, 103, 182, 56, 242, 4, 179, 57] };
5861
/// # fn send_proof(_: &[u8]) { };
@@ -66,7 +69,7 @@ use subtle::ConstantTimeEq;
6669
/// `key` contains shared secret key between user and the server. You can extract shared secret
6770
/// key using `key()` method.
6871
/// ```rust
69-
/// # let server = srp::Server::<srp::G2048, sha2::Sha256>::new();
72+
/// # let server = srp::ServerG2048::<sha2::Sha256>::new();
7073
/// # let verifier = server.process_reply_legacy(b"", b"", b"1").unwrap();
7174
///
7275
/// verifier.key();
@@ -80,7 +83,7 @@ use subtle::ConstantTimeEq;
8083
/// key in case of correct server data.
8184
///
8285
/// ```ident
83-
/// # let server = srp::Server::<srp::G2048, sha2::Sha256>::new();
86+
/// # let server = srp::ServerG2048::<sha2::Sha256>::new();
8487
/// # let verifier = server.process_reply_rfc5054(b"", b"", b"", b"", b"1").unwrap();
8588
/// # fn get_client_proof()-> Vec<u8> { vec![53, 91, 252, 129, 223, 201, 97, 145, 208, 243, 229, 232, 20, 118, 108, 126, 244, 68, 237, 38, 121, 24, 181, 53, 155, 103, 134, 44, 107, 204, 56, 50] };
8689
/// # fn send_proof(_: &[u8]) { };

srp/tests/bad_public.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crypto_bigint::BoxedUint;
22
use sha1::Sha1;
3-
use srp::{Client, G1024, Server};
3+
use srp::{Client, Server, groups::G1024};
44

55
#[test]
66
#[should_panic]

srp/tests/rfc5054.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crypto_bigint::BoxedUint;
22
use hex_literal::hex;
33
use sha1::Sha1;
44
use srp::utils::{compute_k, compute_u};
5-
use srp::{Client, G1024, Group, Server};
5+
use srp::{Client, Group, Server, groups::G1024};
66

77
#[test]
88
#[allow(clippy::many_single_char_names)]

srp/tests/srp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use getrandom::{
33
rand_core::{RngCore, TryRngCore},
44
};
55
use sha2::Sha256;
6-
use srp::{Client, G2048, Server};
6+
use srp::{Client, Server, groups::G2048};
77

88
fn auth_test_rfc5054(true_pwd: &[u8], auth_pwd: &[u8]) {
99
let mut rng = SysRng.unwrap_err();

0 commit comments

Comments
 (0)