-
Notifications
You must be signed in to change notification settings - Fork 0
/
handshake.spec.ts
86 lines (82 loc) · 2.58 KB
/
handshake.spec.ts
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
import { expect, test } from "vitest";
import {
deriveVerifier,
generateClientEphemeral,
deriveSessionKey as deriveSessionKey_client,
} from "../src/srp/client";
import {
generateServerEphemeral,
deriveSessionKey as deriveSessionKey_server,
} from "../src/srp/server";
import { SRP_PARAMETERS_RFC5054_4096 } from "../src/constants";
import { byteArrayToHexString } from "../src/utils";
test("it should produce correct shared secret", async () => {
// 1. Client init
const username = "alice";
const password = "test@password";
const { salt, verifier } = await deriveVerifier({ username, password });
const { clientPublicEphemeral, clientPrivateEphemeral } =
generateClientEphemeral();
// 2. Server init
const { serverPublicEphemeral, serverPrivateEphemeral } =
await generateServerEphemeral({ verifier });
// 3. Client-side shared session key
const clientSharedKey = await deriveSessionKey_client({
salt,
username,
password,
clientPublicEphemeral,
serverPublicEphemeral,
clientPrivateEphemeral,
});
// 4. Server-side shared session key
const serverSharedKey = await deriveSessionKey_server({
verifier,
clientPublicEphemeral,
serverPublicEphemeral,
serverPrivateEphemeral,
});
// Handshake done
expect(byteArrayToHexString(clientSharedKey)).toBe(
byteArrayToHexString(serverSharedKey)
);
});
test("it should produce correct shared secret with RFC5054 4096-bit parameters", async () => {
// 1. Client init
const username = "alice";
const password = "test@password";
const { salt, verifier } = await deriveVerifier(
{ username, password },
{ parameters: SRP_PARAMETERS_RFC5054_4096 }
);
const { clientPublicEphemeral, clientPrivateEphemeral } =
generateClientEphemeral(SRP_PARAMETERS_RFC5054_4096);
// 2. Server init
const { serverPublicEphemeral, serverPrivateEphemeral } =
await generateServerEphemeral({
verifier,
parameters: SRP_PARAMETERS_RFC5054_4096,
});
// 3. Client-side shared session key
const clientSharedKey = await deriveSessionKey_client({
salt,
username,
password,
serverPublicEphemeral,
clientPublicEphemeral,
clientPrivateEphemeral,
parameters: SRP_PARAMETERS_RFC5054_4096,
});
// 4. Server-side shared session key
const serverSharedKey = await deriveSessionKey_server({
verifier,
clientPublicEphemeral,
serverPublicEphemeral,
serverPrivateEphemeral,
parameters: SRP_PARAMETERS_RFC5054_4096,
});
// Handshake done
expect(byteArrayToHexString(clientSharedKey)).toBe(
byteArrayToHexString(serverSharedKey)
);
});