-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShared.fs
217 lines (192 loc) · 5.82 KB
/
Shared.fs
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
module Srp.Shared
open Models
open Extensions
open Utils
open System.Numerics
open System
type HArg =
| BInt of BigInteger
| Buff of byte[]
/// **Description**
/// One way hash function
/// * Appends bytes array into one array
/// * Hashes resulting bytes array
/// **Parameters**
/// * `param` - hash algorithm `SRPParameter`
/// * `args` - arguements to concat and hash `HArg list`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let H (param : SrpParameter) (args : HArg list) =
args
|> List.map (fun hArg -> match hArg with
| BInt bigInt -> bigInt |> bigIntToBytes
| Buff bytes -> bytes)
|> List.reduce Array.append
|> bytesToHash param.hashAlg
|> bytesToBigInt
/// **Description**
/// Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6).
/// * Pad __N__ and __g__ with zeros to the byte size of N
/// * Concatenate the two and apple one-way hash function __H__
/// * Converts byte array to big integer
/// **Parameters**
/// * `param` - srp parameter `SRPParameter`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let k param =
(param ,[Buff (param.N |> bigIntPaddedToNBytes param.bits);
Buff (param.g |> bigIntPaddedToNBytes param.bits)])
||> H
/// **Description**
/// Private Key
/// x = H(salt || H(username || ":" || password))
/// * Combine identity (username) and password and convert to UTF8 bytes
/// * Hash byte array with __param__'s hash algorithm
/// * Combines salt buffer and identity-password hashed buffer to array
/// * Applys resulting array to one-way hash function __H__
///
/// **Parameters**
/// * `param` - srp hash algorithm `SRPParameter`
/// * `_s` - salt `byte []`
/// * `_I` - user identity `string`
/// * `_p` - user password `string`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let x param _s (_I : string) (_p : string)=
let ipBytes = (sprintf "%s:%s" _I _p)
|> String.getBytesUTF8
|> bytesToHash param.hashAlg
(param, [Buff _s; Buff ipBytes ])
||> H
/// **Description**
/// Computes Verifier
/// v = g^x % N
/// * Mods g (modulous) by private key (__x__) and takes it to exponential of N
///
/// **Parameters**
/// * `param` - srp parameter `SRPParameter`
/// * `_g` - srp N modulo `BigInteger`
/// * `_x` - user computed private key `BigInteger`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let v param _x =
param.g |> BigInteger.modPow _x param.N
/// **Description**
/// Client Public ephemeral value
/// A = g^a % N
/// * Mods g (modulous) by private key (__a__) and takes it to exponential of N
///
/// **Parameters**
/// * `param` - srp parameter `SRPParameter`
/// * `_a` - client secret generated ephemeral value `BigInteger`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
let A param (_a : BigInteger) =
let aBitLength = float((_a |> bigIntToBytes).Length) * 8.0
if (Math.Ceiling( aBitLength) < 256.0)
then printfn "A: client key length %f is less than the recommended 256" aBitLength
param.g |> BigInteger.modPow _a param.N
|> bigIntPaddedToNBytes param.bits
|> bytesToBigInt
/// **Description**
/// B = kv + g^b
/// Server Public ephemeral value
/// **Parameters**
/// * `param` - srp parameter `SRPParameter`
/// * `_k` - srp multiplier `BigInteger`
/// * `_b` - server secret generated ephemeral value `BigInteger`
/// * `_v` - server stored verifier `BigInteger`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let B param _k _b _v =
(_k * _v + BigInteger.ModPow(param.g, _b, param.N)) % param.N
|> bigIntPaddedToNBytes param.bits
|> bytesToBigInt
/// **Description**
/// Random scrambling parameter
/// u = SHA-1(A || B)
/// * Convert ___A__ and ___B__ big integers to byte array
/// * Concatenate byte arrays and send to one way hash __H__
/// * Convert byte array to big integer
/// **Parameters**
/// * `hashAlg` - srp hash algorithm `HashAlg`
/// * `_A` - client puplic ephemeral value `bigint`
/// * `_B` - server puplic ephemeral value `bigint`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let u hashAlg _A _B =
(hashAlg, [BInt _A; BInt _B])
||> H
/// **Description**
/// Proof variable
/// K = H(S)
/// **Parameters**
/// * `param` - srp parameter `SRPParameter`
/// * `_S` - session key `bigint`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let K param (_S : BigInteger) =
H param [(BInt _S)]
/// **Description**
/// M1 challenge is used in place of the password as proof of identity (K)
/// * Take the one way hash of the client and sever ephemeral values
/// and the session key
/// **Parameters**
/// * `param` - srp parameters `SRPParameter`
/// * `_A` - client puplic ephemeral value `bigint`
/// * `_B` - server puplic ephemeral value `bigint`
/// * `_S_buf` - session key `bigint`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let M1 param _A _B _S =
H param [BInt _A; BInt _B ; BInt _S ]
/// **Description**
/// M2 challenge is used to prove authenticity of server
/// **Parameters**
/// * `param` - srp parameter `SRPParameter`
/// * `_A` - client puplic ephemeral value `BigInteger`
/// * `_M1` - used to prove idetity `BigInteger`
/// * `_K` - shared hashed session key `BigInteger`
///
/// **Output Type**
/// * `BigInteger`
///
/// **Exceptions**
///
let M2 param _A _M1 _K =
H param [BInt _A;BInt _M1; BInt _K]