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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
package se.leap.leapclient;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import org.jboss.security.Util;
import org.jboss.security.srp.SRPClientSession;
import org.jboss.security.srp.SRPParameters;
import org.jboss.security.srp.SRPPermission;
public class LeapSRPSession {
private SRPParameters params;
private BigInteger N;
private BigInteger g;
private BigInteger x;
private BigInteger v;
private byte[] s;
private BigInteger a;
private BigInteger A;
private byte[] K;
/** The M1 = H(H(N) xor H(g) | H(U) | s | A | B | K) hash */
private MessageDigest clientHash;
/** The M2 = H(A | M | K) hash */
private MessageDigest serverHash;
private static int A_LEN;
/** Creates a new SRP server session object from the username, password
verifier,
@param username, the user ID
@param password, the user clear text password
@param params, the SRP parameters for the session
*/
public LeapSRPSession(String username, char[] password, SRPParameters params)
{
this(username, password, params, null);
}
/** Creates a new SRP server session object from the username, password
verifier,
@param username, the user ID
@param password, the user clear text password
@param params, the SRP parameters for the session
@param abytes, the random exponent used in the A public key. This must be
8 bytes in length.
*/
public LeapSRPSession(String username, char[] password, SRPParameters params,
byte[] abytes)
{
try
{
// Initialize the secure random number and message digests
Util.init();
}
catch(NoSuchAlgorithmException e)
{
}
this.params = params;
this.g = new BigInteger(1, params.g);
byte[] N_bytes = Util.trim(params.N);
this.N = new BigInteger(1, N_bytes);
if( abytes != null ) {
A_LEN = 8*abytes.length;
/* TODO Why did they put this condition?
if( 8*abytes.length != A_LEN )
throw new IllegalArgumentException("The abytes param must be "
+(A_LEN/8)+" in length, abytes.length="+abytes.length);
*/
this.a = new BigInteger(abytes);
}
// Calculate x = H(s | H(U | ':' | password))
byte[] xb = calculatePasswordHash(username, password, params.s);
//xb = Util.trim(xb);
this.x = new BigInteger(1, xb);
// Calculate v = kg^x mod N
this.v = calculateV();
serverHash = newDigest();
clientHash = newDigest();
// H(N)
byte[] hn = newDigest().digest(N_bytes);
// H(g)
byte[] hg = newDigest().digest(params.g);
// clientHash = H(N) xor H(g)
byte[] hxg = xor(hn, hg, hg.length);
String hxg_string = new BigInteger(1, hxg).toString(16);
hxg = Util.trim(hxg);
clientHash.update(hxg); // OK
// clientHash = H(N) xor H(g) | H(U)
byte[] username_bytes = username.getBytes();
byte[] username_digest = newDigest().digest(username_bytes);
String username_digest_string = new BigInteger(1, username_digest).toString(16);
clientHash.update(username_digest); // OK
// clientHash = H(N) xor H(g) | H(U) | s
byte[] salt_bytes = params.s;
String salt_string = new BigInteger(1, salt_bytes).toString(16);
clientHash.update(salt_bytes); // OK
K = null;
}
private BigInteger calculateV() {
BigInteger k = new BigInteger("bf66c44a428916cad64aa7c679f3fd897ad4c375e9bbb4cbf2f5de241d618ef0", 16);
return k.multiply(g.modPow(x, N)); // g^x % N
}
/**
* @returns The exponential residue (parameter A) to be sent to the server.
*/
public byte[] exponential() {
byte[] Abytes = null;
if(A == null) {
/* If the random component of A has not been specified use a random
number */
if( a == null ) {
BigInteger one = BigInteger.ONE;
do {
a = new BigInteger(A_LEN, Util.getPRNG());
} while(a.compareTo(one) <= 0);
}
A = g.modPow(a, N);
Abytes = A.toByteArray();
String Abytes_string = new BigInteger(1, Abytes).toString(16);
Abytes = Util.trim(Abytes);
// clientHash = H(N) xor H(g) | H(U) | A
clientHash.update(Abytes); // Begins with 0: second case
// serverHash = A
serverHash.update(Abytes);
}
return Abytes;
}
public byte[] response(byte[] Bbytes) throws NoSuchAlgorithmException {
// clientHash = H(N) xor H(g) | H(U) | s | A | B
Bbytes = Util.trim(Bbytes); // Begins with 0: first case, second case
String Abytes_string = new BigInteger(1, Bbytes).toString(16);
clientHash.update(Bbytes); // OK
// Calculate S = (B - kg^x) ^ (a + u * x) % N
BigInteger S = calculateS(Bbytes);
byte[] S_bytes = S.toByteArray();
S_bytes = Util.trim(S_bytes); // Begins with 0: first case
String S_bytes_string = new BigInteger(1, S_bytes).toString(16);
// K = SessionHash(S)
String hash_algorithm = params.hashAlgorithm;
MessageDigest sessionDigest = MessageDigest.getInstance(hash_algorithm);
K = sessionDigest.digest(S_bytes);
//K = Util.trim(K);
String K_bytes_string = new BigInteger(1, K).toString(16);
// clientHash = H(N) xor H(g) | H(U) | A | B | K
clientHash.update(K);
byte[] M1 = clientHash.digest();
return M1;
}
private BigInteger calculateS(byte[] Bbytes) {
byte[] ub = getU(A.toByteArray(), Bbytes);
BigInteger B = new BigInteger(1, Bbytes);
BigInteger u = new BigInteger(1, ub);
BigInteger B_v = B.subtract(v);
BigInteger a_ux = a.add(u.multiply(x));
BigInteger S = B_v.modPow(a_ux, N);
return S;
}
public byte[] getU(byte[] Abytes, byte[] Bbytes) {
MessageDigest u_digest = newDigest();
u_digest.update(Util.trim(Abytes));
u_digest.update(Util.trim(Bbytes));
return new BigInteger(1, u_digest.digest()).toByteArray();
}
/**
* @param M2 The server's response to the client's challenge
* @returns True if and only if the server's response was correct.
*/
public boolean verify(byte[] M2)
{
// M2 = H(A | M1 | K)
byte[] myM2 = serverHash.digest();
boolean valid = Arrays.equals(M2, myM2);
return valid;
}
/** Returns the negotiated session K, K = SHA_Interleave(S)
@return the private session K byte[]
@throws SecurityException - if the current thread does not have an
getSessionKey SRPPermission.
*/
public byte[] getSessionKey() throws SecurityException
{
SecurityManager sm = System.getSecurityManager();
if( sm != null )
{
SRPPermission p = new SRPPermission("getSessionKey");
sm.checkPermission(p);
}
return K;
}
public MessageDigest newDigest()
{
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return md;
}
public byte[] calculatePasswordHash(String username, char[] password, byte[] salt)
{
// Calculate x = H(s | H(U | ':' | password))
MessageDigest xd = newDigest();
// Try to convert the username to a byte[] using UTF-8
byte[] user = null;
byte[] colon = {};
try {
user = username.getBytes("UTF-8");
colon = ":".getBytes("UTF-8");
}
catch(UnsupportedEncodingException e) {
// Use the default platform encoding
user = username.getBytes();
colon = ":".getBytes();
}
byte[] passBytes = new byte[2*password.length];
int passBytesLength = 0;
for(int p = 0; p < password.length; p ++) {
int c = (password[p] & 0x00FFFF);
// The low byte of the char
byte b0 = (byte) (c & 0x0000FF);
// The high byte of the char
byte b1 = (byte) ((c & 0x00FF00) >> 8);
passBytes[passBytesLength ++] = b0;
// Only encode the high byte if c is a multi-byte char
if( c > 255 )
passBytes[passBytesLength ++] = b1;
}
// Build the hash
xd.update(user);
xd.update(colon);
xd.update(passBytes, 0, passBytesLength);
byte[] h = xd.digest();
xd.reset();
xd.update(salt);
xd.update(h);
byte[] xb = xd.digest();
return xb;
}
public byte[] xor(byte[] b1, byte[] b2, int length)
{
//TODO Check if length matters in the order, when b2 is smaller than b1 or viceversa
return new BigInteger(1, b1).xor(new BigInteger(1, b2)).toByteArray();
}
}
|