-
Notifications
You must be signed in to change notification settings - Fork 14
/
milenage.py
executable file
·190 lines (148 loc) · 4.69 KB
/
milenage.py
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
#!/usr/bin/env python
#
# $Id$
#
# This program implements GSM milenage algorithm, specified
# in 3GPP TS 55.205, for generating GSM auth triplets. This
# function runs on HLR for providing auth info to SGSN/MME/
# Radius server for UE/MS authentication.
#
# Author: [email protected]
#
import sys
import binascii
from Crypto.Cipher import AES
from itertools import izip
#Our macro
__XOR__ = lambda x, y: chr(ord(x) ^ ord(y))
def LogicalXOR(str1, str2):
'''Function to XOR two strings'''
return ''.join(__XOR__(x, y) for (x,y) in izip(str1, str2))
def AESEncrypt(key, buf):
'''Encrypt buffer using AES-SHA1 128 algorithm
@key: Key to be used for encryption
@buf: Buffer to be encrypted'''
encryptor = AES.new(key, AES.MODE_CBC)
return encryptor.encrypt(buf)
def GsmMilenageGenOpc(ki, op):
'''Generate Opc using Ki and Op
@ki: 128-bit subscriber key
@op: 128-bit operator variant'''
opc = AESEncrypt(ki, op)
return LogicalXOR(opc, op)
def GsmMilenageF2345(ki, opc, rand):
'''Milenage f2, f3, f4, f5, f5* algorithms'''
i = 0
tmp1 = LogicalXOR(rand, opc)
tmp2 = AESEncrypt(ki, tmp1)
tmp1 = LogicalXOR(tmp2, opc)
tmp1 = tmp1[:15] + chr(ord(tmp1[15]) ^ 1)
tmp3 = AESEncrypt(ki, tmp1)
tmp3 = LogicalXOR(tmp3, opc)
res = tmp3[8:]
#F3 - to calculate ck
ck_map = {}
for i in range(16):
ck_map[(i+12)%16] = __XOR__(tmp2[i], opc[i])
ck_map[15] = __XOR__(ck_map[15], chr(2))
tmp1 = ''.join(val for val in ck_map.values())
ck = AESEncrypt(ki, tmp1)
ck = LogicalXOR(ck, opc)
#F4 - to calculate ik
ik_map = {}
for i in range(16):
ik_map[(i+8)%16] = __XOR__(tmp2[i], opc[i])
ik_map[15] = __XOR__(ik_map[15], chr(4))
tmp1 = ''.join(val for val in ik_map.values())
ik = AESEncrypt(ki, tmp1)
ik = LogicalXOR(ik, opc)
return res, ck, ik
def GsmMilenage(ki, opc, rand):
'''Generate GSM-Milenage (3GPP TS 55.205) auth triplet
@ki : 128-bit subscriber key
@opc : 128-bit operator variant algorithm configuration
@rand: 128-bit random challenge'''
res, ck, ik = GsmMilenageF2345(ki, opc, rand)
#Calculate sres
sres_map = {}
for idx in range(4):
sres_map[idx] = __XOR__(res[idx], res[idx+4])
sres = ''.join(val for val in sres_map.values())
#Calculate kc
kc_map = {}
for idx in range(8):
kc_map[idx] = __XOR__(__XOR__(ck[idx], ck[idx+8]),
__XOR__(ik[idx], ik[idx+8]))
kc = ''.join(val for val in kc_map.values())
return sres, kc
def GenerateAuthTriplets(keyset):
ki = binascii.unhexlify(keyset['ki'])
op = binascii.unhexlify(keyset['op'])
rand = binascii.unhexlify(keyset['rand'])
#Generate opc from ki and op
opc = GsmMilenageGenOpc(ki, op)
#Get sres, kc
sres, kc = GsmMilenage(ki, opc, rand)
#Store values now
keyset['opc'] = binascii.hexlify(opc)
keyset['kc'] = binascii.hexlify(kc)
keyset['sres'] = binascii.hexlify(sres)
return
def ReadMilenageInput(filename):
attribs = []
keyset = {}
try:
fp = open(filename)
except:
print 'Error opening file %s'%(filename)
sys.exit()
for line in fp.readlines():
if line.startswith('#'):
continue
if line.startswith('\n'):
if len(keyset):
attribs.append(keyset)
keyset = {}
continue
key, value = line.split('=')
keyset[key] = value.split('\n')[0]
#Validate input
if len(attribs) == 0:
print 'Milenage: Please provide KI/OP/RAND in input file'
sys.exit()
for keyset in attribs:
if not keyset.has_key('ki') or \
not keyset.has_key('op') or \
not keyset.has_key('rand'):
print 'Milenage: KI or OP missing in keyset'
sys.exit()
return attribs
def PrintMilenageOutput(attribs):
'''Prints input read'''
idx = 1
for keyset in attribs:
print 'Keyset # %d'%(idx)
print ' %2s: %s'%('ki', keyset['ki'])
print ' %2s: %s'%('op', keyset['op'])
print ' Auth Triplets: '
print ' %4s: %s'%('rand', keyset['rand'])
print ' %4s: %s'%('sres', keyset['sres'])
print ' %4s: %s'%('kc', keyset['kc'])
print ''
idx += 1
return
def main():
'''The main function'''
if len(sys.argv) < 2:
print 'Milenage: Please provide input file'
return
#Read input
attribs = ReadMilenageInput(sys.argv[1])
#Generate auth triplets now
for keyset in attribs:
GenerateAuthTriplets(keyset)
#Print output
PrintMilenageOutput(attribs)
return
if __name__ == '__main__':
main()