summaryrefslogtreecommitdiffstats
path: root/tinySIP/src/authentication/tsip_milenage.c
blob: 0c95e4ad61c5141fd92d4feba85deb46538b71a6 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Partial Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tsip_milenage.c
* @brief 3GPP authentication and key agreement functions f1, f1*, f2, f3, f4, f5 and f5*.
*
* @section DESCRIPTION
*
* @sa 3G Security
* <a href="http://www.3gpp.org/ftp/Specs/html-info/35205.htm"> 3GPP TS 35.205 </a>
* <a href="http://www.3gpp.org/ftp/Specs/html-info/35206.htm"> 3GPP TS 35.206 </a>
* <a href="http://www.3gpp.org/ftp/Specs/html-info/35207.htm"> 3GPP TS 35.207 </a>
* <a href="http://www.3gpp.org/ftp/Specs/html-info/35208.htm"> 3GPP TS 35.208 </a>
* <a href="http://www.3gpp.org/ftp/Specs/html-info/35909.htm"> 3GPP TS 35.909 </a>
*-------------------------------------------------------------------
*          Example algorithms f1, f1*, f2, f3, f4, f5, f5*
*-------------------------------------------------------------------
*
*  A sample implementation of the example 3GPP authentication and
*  key agreement functions f1, f1*, f2, f3, f4, f5 and f5*.  This is
*  a byte-oriented implementation of the functions, and of the block
*  cipher kernel function Rijndael.
*
*  This has been coded for clarity, not necessarily for efficiency.
*
*  The functions f2, f3, f4 and f5 share the same inputs and have
*  been coded together as a single function.  f1, f1* and f5* are
*  all coded separately.
*
*-----------------------------------------------------------------
*
*/

#include "tinysip/authentication/tsip_milenage.h"
#include "tinysip/authentication/tsip_rijndael.h"

/*--------- Operator Variant Algorithm Configuration Field --------*/

/*------- Insert your value of OP here -------*/
//uint8_t OP[16] = {0x63, 0xbf, 0xa5, 0x0e, 0xe6, 0x52, 0x33, 0x65,
//             0xff, 0x14, 0xc1, 0xf4, 0x5f, 0x88, 0x73, 0x7d};
/*------- Insert your value of OP here -------*/
uint8_t OP[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                 };

/*-------------------------------------------------------------------
*                            Algorithm f1
*-------------------------------------------------------------------
*
*  Computes network authentication code MAC-A from key K, random
*  challenge RAND, sequence number SQN and authentication management
*  field AMF.
*
*-----------------------------------------------------------------*/

void f1    ( uint8_t k[16], uint8_t rand[16], uint8_t sqn[6], uint8_t amf[2],
             uint8_t mac_a[8] )
{
    uint8_t op_c[16];
    uint8_t temp[16];
    uint8_t in1[16];
    uint8_t out1[16];
    uint8_t rijndaelInput[16];
    uint8_t i;

    RijndaelKeySchedule( k );

    ComputeOPc( op_c );

    for (i=0; i<16; i++) {
        rijndaelInput[i] = rand[i] ^ op_c[i];
    }
    RijndaelEncrypt( rijndaelInput, temp );

    for (i=0; i<6; i++) {
        in1[i]    = sqn[i];
        in1[i+8]  = sqn[i];
    }

    for (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 (i=0; i<16; i++) {
        rijndaelInput[(i+8) % 16] = in1[i] ^ op_c[i];
    }

    /* XOR on the value temp computed before */

    for (i=0; i<16; i++) {
        rijndaelInput[i] ^= temp[i];
    }

    RijndaelEncrypt( rijndaelInput, out1 );
    for (i=0; i<16; i++) {
        out1[i] ^= op_c[i];
    }

    for (i=0; i<8; i++) {
        mac_a[i] = out1[i];
    }

    return;
} /* end of function f1 */



/*-------------------------------------------------------------------
*                            Algorithms f2-f5
*-------------------------------------------------------------------
*
*  Takes key K and random challenge RAND, and returns response RES,
*  confidentiality key CK, integrity key IK and anonymity key AK.
*
*-----------------------------------------------------------------*/

void f2345 ( uint8_t k[16], uint8_t rand[16],
             uint8_t res[8], uint8_t ck[16], uint8_t ik[16], uint8_t ak[6] )
{
    uint8_t op_c[16];
    uint8_t temp[16];
    uint8_t out[16];
    uint8_t rijndaelInput[16];
    uint8_t i;

    RijndaelKeySchedule( k );

    ComputeOPc( op_c );

    for (i=0; i<16; i++) {
        rijndaelInput[i] = rand[i] ^ op_c[i];
    }
    RijndaelEncrypt( rijndaelInput, temp );

    /* 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 (i=0; i<16; i++) {
        rijndaelInput[i] = temp[i] ^ op_c[i];
    }
    rijndaelInput[15] ^= 1;

    RijndaelEncrypt( rijndaelInput, out );
    for (i=0; i<16; i++) {
        out[i] ^= op_c[i];
    }

    for (i=0; i<8; i++) {
        res[i] = out[i+8];
    }
    for (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 (i=0; i<16; i++) {
        rijndaelInput[(i+12) % 16] = temp[i] ^ op_c[i];
    }
    rijndaelInput[15] ^= 2;

    RijndaelEncrypt( rijndaelInput, out );
    for (i=0; i<16; i++) {
        out[i] ^= op_c[i];
    }

    for (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 (i=0; i<16; i++) {
        rijndaelInput[(i+8) % 16] = temp[i] ^ op_c[i];
    }
    rijndaelInput[15] ^= 4;

    RijndaelEncrypt( rijndaelInput, out );
    for (i=0; i<16; i++) {
        out[i] ^= op_c[i];
    }

    for (i=0; i<16; i++) {
        ik[i] = out[i];
    }

    return;
} /* end of function f2345 */


/*-------------------------------------------------------------------
*                            Algorithm f1*
*-------------------------------------------------------------------
*
*  Computes resynch authentication code MAC-S from key K, random
*  challenge RAND, sequence number SQN and authentication management
*  field AMF.
*
*-----------------------------------------------------------------*/

void f1star( uint8_t k[16], uint8_t rand[16], uint8_t sqn[6], uint8_t amf[2],
             uint8_t mac_s[8] )
{
    uint8_t op_c[16];
    uint8_t temp[16];
    uint8_t in1[16];
    uint8_t out1[16];
    uint8_t rijndaelInput[16];
    uint8_t i;

    RijndaelKeySchedule( k );

    ComputeOPc( op_c );

    for (i=0; i<16; i++) {
        rijndaelInput[i] = rand[i] ^ op_c[i];
    }
    RijndaelEncrypt( rijndaelInput, temp );

    for (i=0; i<6; i++) {
        in1[i]    = sqn[i];
        in1[i+8]  = sqn[i];
    }
    for (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 (i=0; i<16; i++) {
        rijndaelInput[(i+8) % 16] = in1[i] ^ op_c[i];
    }

    /* XOR on the value temp computed before */

    for (i=0; i<16; i++) {
        rijndaelInput[i] ^= temp[i];
    }

    RijndaelEncrypt( rijndaelInput, out1 );
    for (i=0; i<16; i++) {
        out1[i] ^= op_c[i];
    }

    for (i=0; i<8; i++) {
        mac_s[i] = out1[i+8];
    }

    return;
} /* end of function f1star */


/*-------------------------------------------------------------------
*                            Algorithm f5*
*-------------------------------------------------------------------
*
*  Takes key K and random challenge RAND, and returns resynch
*  anonymity key AK.
*
*-----------------------------------------------------------------*/

void f5star( uint8_t k[16], uint8_t rand[16],
             uint8_t ak[6] )
{
    uint8_t op_c[16];
    uint8_t temp[16];
    uint8_t out[16];
    uint8_t rijndaelInput[16];
    uint8_t i;

    RijndaelKeySchedule( k );

    ComputeOPc( op_c );

    for (i=0; i<16; i++) {
        rijndaelInput[i] = rand[i] ^ op_c[i];
    }
    RijndaelEncrypt( rijndaelInput, temp );

    /* 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 (i=0; i<16; i++) {
        rijndaelInput[(i+4) % 16] = temp[i] ^ op_c[i];
    }
    rijndaelInput[15] ^= 8;

    RijndaelEncrypt( rijndaelInput, out );
    for (i=0; i<16; i++) {
        out[i] ^= op_c[i];
    }

    for (i=0; i<6; i++) {
        ak[i] = out[i];
    }

    return;
} /* end of function f5star */


/*-------------------------------------------------------------------
*  Function to compute OPc from OP and K.  Assumes key schedule has
already been performed.
*-----------------------------------------------------------------*/

void ComputeOPc( uint8_t op_c[16] )
{
    uint8_t i;

    RijndaelEncrypt( OP, op_c );
    for (i=0; i<16; i++) {
        op_c[i] ^= OP[i];
    }

    return;
} /* end of function ComputeOPc */

void ComputeOP( uint8_t op[16] )
{
    int i;
    for(i=0; i<16; i++) {
        OP[i]=op[i];
    }
}
OpenPOWER on IntegriCloud