forked from hap-java/HAP-Java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClientEvidenceRoutineImpl.java
59 lines (48 loc) · 1.76 KB
/
ClientEvidenceRoutineImpl.java
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
package io.github.hapjava.server.impl.pairing;
import com.nimbusds.srp6.*;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class ClientEvidenceRoutineImpl implements ClientEvidenceRoutine {
public ClientEvidenceRoutineImpl() {}
/**
* Calculates M1 according to the following formula:
*
* <p>M1 = H(H(N) xor H(g) || H(username) || s || A || B || H(S))
*/
@Override
public BigInteger computeClientEvidence(
SRP6CryptoParams cryptoParams, SRP6ClientEvidenceContext ctx) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(cryptoParams.H);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Could not locate requested algorithm", e);
}
digest.update(BigIntegerUtils.bigIntegerToBytes(cryptoParams.N));
byte[] hN = digest.digest();
digest.update(BigIntegerUtils.bigIntegerToBytes(cryptoParams.g));
byte[] hg = digest.digest();
byte[] hNhg = xor(hN, hg);
digest.update(ctx.userID.getBytes(StandardCharsets.UTF_8));
byte[] hu = digest.digest();
digest.update(BigIntegerUtils.bigIntegerToBytes(ctx.S));
byte[] hS = digest.digest();
digest.update(hNhg);
digest.update(hu);
digest.update(BigIntegerUtils.bigIntegerToBytes(ctx.s));
digest.update(BigIntegerUtils.bigIntegerToBytes(ctx.A));
digest.update(BigIntegerUtils.bigIntegerToBytes(ctx.B));
digest.update(hS);
BigInteger ret = new BigInteger(1, digest.digest());
return ret;
}
private static byte[] xor(byte[] b1, byte[] b2) {
byte[] result = new byte[b1.length];
for (int i = 0; i < b1.length; i++) {
result[i] = (byte) (b1[i] ^ b2[i]);
}
return result;
}
}