-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
264 lines (185 loc) · 7.1 KB
/
index.js
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
const crypto = require('crypto');
module.exports = function Milenage(params) {
function op_c() {
const cipher = crypto.createCipheriv('aes-128-ecb', params.key, Buffer.alloc(0));
return params.op_c || computeOpc(cipher);
}
/*-------------------------------------------------------------------
* Algorithm f1
*-------------------------------------------------------------------
*
* Computes network authentication code MAC-A from key K, random
* challenge RAND, sequence number SQN and authentication management
* field AMF.
*
*-----------------------------------------------------------------*/
function f1(rand, sqn, amf) {
const cipher = crypto.createCipheriv('aes-128-ecb', params.key, Buffer.alloc(0));
const op_c = params.op_c || computeOpc(cipher);
const rijndaelInput = new Uint8Array(16);
for (let i = 0; i < 16; i++) // @todo unroll?
rijndaelInput[i] = rand[i] ^ op_c[i];
const temp = Uint8Array.from(cipher.update(rijndaelInput));
const in1 = new Uint8Array(16);
for (let i = 0; i < 6; i++) { // @todo unroll?
in1[i] = sqn[i];
in1[i + 8] = sqn[i];
}
for (let i = 0; i < 2; i++) { // @todo unroll?
in1[i + 6] = amf[i];
in1[i + 14] = amf[i];
}
/* XOR op_c and in1, rotate by r1=64, and XOR *
* on the constant c1 (which is all zeroes) */
for (let i = 0; i < 16; i++)
rijndaelInput[(i + 8) % 16] = in1[i] ^ op_c[i];
/* XOR on the value temp computed before */
for (let i = 0; i < 16; i++)
rijndaelInput[i] ^= temp[i];
const out1 = Uint8Array.from(cipher.update(rijndaelInput));
for (let i = 0; i < 16; i++)
out1[i] ^= op_c[i];
const mac_a = new Uint8Array(8);
for (let i = 0; i < 8; i++)
mac_a[i] = out1[i];
return { op_c, mac_a };
}
/*-------------------------------------------------------------------
* Algorithms f2-f5
*-------------------------------------------------------------------
*
* Takes key K and random challenge RAND, and returns response RES,
* confidentiality key CK, integrity key IK and anonymity key AK.
*
*-----------------------------------------------------------------*/
function f2345(rand) {
const cipher = crypto.createCipheriv('aes-128-ecb', params.key, Buffer.alloc(0));
const op_c = params.op_c || computeOpc(cipher);
const rijndaelInput = new Uint8Array(16);
for (let i = 0; i < 16; i++)
rijndaelInput[i] = rand[i] ^ op_c[i];
const temp = Uint8Array.from(cipher.update(rijndaelInput));
/* To obtain output block OUT2: XOR OPc and TEMP, *
* rotate by r2=0, and XOR on the constant c2 (which *
* is all zeroes except that the last bit is 1). */
for (let i = 0; i < 16; i++)
rijndaelInput[i] = temp[i] ^ op_c[i];
rijndaelInput[15] ^= 1;
let out = Uint8Array.from(cipher.update(rijndaelInput));
for (let i = 0; i < 16; i++)
out[i] ^= op_c[i];
const res = new Uint8Array(8);
for (let i = 0; i < 8; i++)
res[i] = out[i + 8];
const ak = new Uint8Array(6);
for (let i = 0; i < 6; i++)
ak[i] = out[i];
/* To obtain output block OUT3: XOR OPc and TEMP, *
* rotate by r3=32, and XOR on the constant c3 (which *
* is all zeroes except that the next to last bit is 1). */
for (let i = 0; i < 16; i++)
rijndaelInput[(i + 12) % 16] = temp[i] ^ op_c[i];
rijndaelInput[15] ^= 2;
out = Uint8Array.from(cipher.update(rijndaelInput));
for (let i = 0; i < 16; i++)
out[i] ^= op_c[i];
const ck = new Uint8Array(16);
for (let i = 0; i < 16; i++)
ck[i] = out[i];
/* To obtain output block OUT4: XOR OPc and TEMP, *
* rotate by r4=64, and XOR on the constant c4 (which *
* is all zeroes except that the 2nd from last bit is 1). */
for (let i = 0; i < 16; i++)
rijndaelInput[(i + 8) % 16] = temp[i] ^ op_c[i];
rijndaelInput[15] ^= 4;
out = Uint8Array.from(cipher.update(rijndaelInput));
for (let i = 0; i < 16; i++)
out[i] ^= op_c[i];
const ik = new Uint8Array(16);
for (let i = 0; i < 16; i++)
ik[i] = out[i];
return { op_c, res, ck, ik, ak };
}
/*-------------------------------------------------------------------
* Algorithm f1*
*-------------------------------------------------------------------
*
* Computes resynch authentication code MAC-S from key K, random
* challenge RAND, sequence number SQN and authentication management
* field AMF.
*
*-----------------------------------------------------------------*/
function f1star(rand, sqn, amf) {
const cipher = crypto.createCipheriv('aes-128-ecb', params.key, Buffer.alloc(0));
const op_c = params.op_c || computeOpc(cipher);
const rijndaelInput = new Uint8Array(16);
for (let i = 0; i < 16; i++)
rijndaelInput[i] = rand[i] ^ op_c[i];
const temp = Uint8Array.from(cipher.update(rijndaelInput));
const in1 = new Uint8Array(16);
for (let i = 0; i < 6; i++) {
in1[i] = sqn[i];
in1[i + 8] = sqn[i];
}
for (let i = 0; i < 2; i++) {
in1[i + 6] = amf[i];
in1[i + 14] = amf[i];
}
/* XOR op_c and in1, rotate by r1=64, and XOR *
* on the constant c1 (which is all zeroes) */
for (let i = 0; i < 16; i++)
rijndaelInput[(i + 8) % 16] = in1[i] ^ op_c[i];
/* XOR on the value temp computed before */
for (let i = 0; i < 16; i++)
rijndaelInput[i] ^= temp[i];
const out1 = Uint8Array.from(cipher.update(rijndaelInput));
for (let i = 0; i < 16; i++)
out1[i] ^= op_c[i];
const mac_s = new Uint8Array(8);
for (let i = 0; i < 8; i++)
mac_s[i] = out1[i + 8];
return { op_c, mac_s };
}
/*-------------------------------------------------------------------
* Algorithm f5*
*-------------------------------------------------------------------
*
* Takes key K and random challenge RAND, and returns resynch
* anonymity key AK.
*
*-----------------------------------------------------------------*/
function f5star(rand) {
const cipher = crypto.createCipheriv('aes-128-ecb', params.key, Buffer.alloc(0));
const op_c = params.op_c || computeOpc(cipher);
const rijndaelInput = new Uint8Array(16);
for (let i = 0; i < 16; i++)
rijndaelInput[i] = rand[i] ^ op_c[i];
const temp = Uint8Array.from(cipher.update(rijndaelInput));
/* To obtain output block OUT5: XOR OPc and TEMP, *
* rotate by r5=96, and XOR on the constant c5 (which *
* is all zeroes except that the 3rd from last bit is 1). */
for (let i = 0; i < 16; i++)
rijndaelInput[(i + 4) % 16] = temp[i] ^ op_c[i];
rijndaelInput[15] ^= 8;
const out = Uint8Array.from(cipher.update(rijndaelInput));
for (let i = 0; i < 16; i++)
out[i] ^= op_c[i];
const ak_s = new Uint8Array(8);
for (let i = 0; i < 6; i++)
ak_s[i] = out[i];
return { op_c, ak_s };
}
function computeOpc(cipher) {
const op_c = Uint8Array.from(cipher.update(params.op));
for (let i = 0; i < 16; i++)
op_c[i] ^= params.op[i];
return op_c;
}
return {
op_c,
f1,
f2345,
f1star,
f5star
};
};