1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::marker::PhantomData;
use digest::Digest;
use generic_array::GenericArray;
use num_bigint::BigUint;
use crate::tools::powm;
use crate::types::{SrpAuthError, SrpGroup};
pub struct SrpClient<'a, D: Digest> {
params: &'a SrpGroup,
a: BigUint,
a_pub: BigUint,
d: PhantomData<D>,
}
pub struct SrpClientVerifier<D: Digest> {
proof: GenericArray<u8, D::OutputSize>,
server_proof: GenericArray<u8, D::OutputSize>,
key: GenericArray<u8, D::OutputSize>,
}
pub fn srp_private_key<D: Digest>(
username: &[u8],
password: &[u8],
salt: &[u8],
) -> GenericArray<u8, D::OutputSize> {
let p = {
let mut d = D::new();
d.input(username);
d.input(b":");
d.input(password);
d.result()
};
let mut d = D::new();
d.input(salt);
d.input(&p);
d.result()
}
impl<'a, D: Digest> SrpClient<'a, D> {
pub fn new(a: &[u8], params: &'a SrpGroup) -> Self {
let a = BigUint::from_bytes_be(a);
let a_pub = params.powm(&a);
Self {
params,
a,
a_pub,
d: Default::default(),
}
}
pub fn get_password_verifier(&self, private_key: &[u8]) -> Vec<u8> {
let x = BigUint::from_bytes_be(private_key);
let v = self.params.powm(&x);
v.to_bytes_be()
}
fn calc_key(
&self,
b_pub: &BigUint,
x: &BigUint,
u: &BigUint,
) -> GenericArray<u8, D::OutputSize> {
let n = &self.params.n;
let k = self.params.compute_k::<D>();
let interm = (k * self.params.powm(x)) % n;
let v = if *b_pub > interm {
(b_pub - &interm) % n
} else {
(n + b_pub - &interm) % n
};
let s = powm(&v, &(&self.a + (u * x) % n), n);
D::digest(&s.to_bytes_be())
}
pub fn process_reply(
self,
private_key: &[u8],
b_pub: &[u8],
) -> Result<SrpClientVerifier<D>, SrpAuthError> {
let u = {
let mut d = D::new();
d.input(&self.a_pub.to_bytes_be());
d.input(b_pub);
BigUint::from_bytes_be(&d.result())
};
let b_pub = BigUint::from_bytes_be(b_pub);
if &b_pub % &self.params.n == BigUint::default() {
return Err(SrpAuthError {
description: "Malicious b_pub value",
});
}
let x = BigUint::from_bytes_be(private_key);
let key = self.calc_key(&b_pub, &x, &u);
let proof = {
let mut d = D::new();
d.input(&self.a_pub.to_bytes_be());
d.input(&b_pub.to_bytes_be());
d.input(&key);
d.result()
};
let server_proof = {
let mut d = D::new();
d.input(&self.a_pub.to_bytes_be());
d.input(&proof);
d.input(&key);
d.result()
};
Ok(SrpClientVerifier {
proof,
server_proof,
key,
})
}
pub fn get_a_pub(&self) -> Vec<u8> {
self.a_pub.to_bytes_be()
}
}
impl<D: Digest> SrpClientVerifier<D> {
pub fn get_key(self) -> GenericArray<u8, D::OutputSize> {
self.key
}
pub fn get_proof(&self) -> GenericArray<u8, D::OutputSize> {
self.proof.clone()
}
pub fn verify_server(
self,
reply: &[u8],
) -> Result<GenericArray<u8, D::OutputSize>, SrpAuthError> {
if self.server_proof.as_slice() != reply {
Err(SrpAuthError {
description: "Incorrect server proof",
})
} else {
Ok(self.key)
}
}
}