Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: various utilities |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 9 | # |
| 10 | # This program is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 2 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # This program is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | # |
| 23 | |
| 24 | |
| 25 | def h2b(s): |
| 26 | return ''.join([chr((int(x,16)<<4)+int(y,16)) for x,y in zip(s[0::2], s[1::2])]) |
| 27 | |
| 28 | def b2h(s): |
| 29 | return ''.join(['%02x'%ord(x) for x in s]) |
| 30 | |
| 31 | def h2i(s): |
| 32 | return [(int(x,16)<<4)+int(y,16) for x,y in zip(s[0::2], s[1::2])] |
| 33 | |
| 34 | def i2h(s): |
| 35 | return ''.join(['%02x'%(x) for x in s]) |
| 36 | |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 37 | def h2s(s): |
| 38 | return ''.join([chr((int(x,16)<<4)+int(y,16)) for x,y in zip(s[0::2], s[1::2]) if not (x == 'f' and y == 'f') ]) |
| 39 | |
| 40 | def s2h(s): |
| 41 | return b2h(s) |
| 42 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 43 | def swap_nibbles(s): |
| 44 | return ''.join([x+y for x,y in zip(s[1::2], s[0::2])]) |
| 45 | |
| 46 | def rpad(s, l, c='f'): |
| 47 | return s + c * (l - len(s)) |
| 48 | |
| 49 | def lpad(s, l, c='f'): |
| 50 | return c * (l - len(s)) + s |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 51 | |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 52 | def enc_imsi(imsi): |
| 53 | """Converts a string imsi into the value of the EF""" |
| 54 | l = (len(imsi) + 1) // 2 # Required bytes |
| 55 | oe = len(imsi) & 1 # Odd (1) / Even (0) |
| 56 | ei = '%02x' % l + swap_nibbles(lpad('%01x%s' % ((oe<<3)|1, imsi), 16)) |
| 57 | return ei |
| 58 | |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 59 | def dec_imsi(ef): |
| 60 | """Converts an EF value to the imsi string representation""" |
| 61 | if len(ef) < 4: |
| 62 | return None |
Pau Espin Pedrol | 665bd22 | 2017-12-29 20:30:35 +0100 | [diff] [blame] | 63 | l = int(ef[0:2], 16) * 2 # Length of the IMSI string |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 64 | swapped = swap_nibbles(ef[2:]) |
| 65 | oe = (int(swapped[0])>>3) & 1 # Odd (1) / Even (0) |
| 66 | if oe: |
| 67 | l = l-1 |
| 68 | if l+1 > len(swapped): |
| 69 | return None |
| 70 | imsi = swapped[1:l+2] |
| 71 | return imsi |
| 72 | |
| 73 | def dec_iccid(ef): |
| 74 | return swap_nibbles(ef).strip('f') |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 75 | |
| 76 | def enc_iccid(iccid): |
| 77 | return swap_nibbles(rpad(iccid, 20)) |
| 78 | |
| 79 | def enc_plmn(mcc, mnc): |
Alexander Chemeris | dddbf52 | 2017-07-18 16:49:59 +0300 | [diff] [blame] | 80 | """Converts integer MCC/MNC into 3 bytes for EF""" |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 81 | return swap_nibbles(lpad('%d' % mcc, 3) + lpad('%d' % mnc, 3)) |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 82 | |
| 83 | def dec_spn(ef): |
| 84 | byte1 = int(ef[0:2]) |
| 85 | hplmn_disp = (byte1&0x01 == 0x01) |
| 86 | oplmn_disp = (byte1&0x02 == 0x02) |
| 87 | name = h2s(ef[2:]) |
| 88 | return (name, hplmn_disp, oplmn_disp) |
| 89 | |
| 90 | def enc_spn(name, hplmn_disp=False, oplmn_disp=False): |
| 91 | byte1 = 0x00 |
| 92 | if hplmn_disp: byte1 = byte1|0x01 |
| 93 | if oplmn_disp: byte1 = byte1|0x02 |
| 94 | return i2h([byte1])+s2h(name) |
Alexander Chemeris | 19fffa1 | 2018-01-11 13:06:43 +0900 | [diff] [blame] | 95 | |
| 96 | def derive_milenage_opc(ki_hex, op_hex): |
| 97 | """ |
| 98 | Run the milenage algorithm to calculate OPC from Ki and OP |
| 99 | """ |
| 100 | from Crypto.Cipher import AES |
| 101 | from Crypto.Util.strxor import strxor |
| 102 | from pySim.utils import b2h |
| 103 | |
| 104 | # We pass in hex string and now need to work on bytes |
| 105 | aes = AES.new(h2b(ki_hex)) |
| 106 | opc_bytes = aes.encrypt(h2b(op_hex)) |
| 107 | return b2h(strxor(opc_bytes, h2b(op_hex))) |
| 108 | |
| 109 | def calculate_luhn(cc): |
| 110 | """ |
| 111 | Calculate Luhn checksum used in e.g. ICCID and IMEI |
| 112 | """ |
| 113 | num = map(int, str(cc)) |
| 114 | check_digit = 10 - sum(num[-2::-2] + [sum(divmod(d * 2, 10)) for d in num[::-2]]) % 10 |
| 115 | return 0 if check_digit == 10 else check_digit |