Review at  https://gerrit.osmocom.org/5745

Make derive_milenage_opc and calculate_luhn publicly available through utils.py

Change-Id: I2effc85fd55da0981de0ada74dcb28b7e8e56a01
---
M pySim-prog.py
M pySim/utils.py
2 files changed, 22 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/45/5745/1

diff --git a/pySim-prog.py b/pySim-prog.py
index 44ca1fd..c08f43b 100755
--- a/pySim-prog.py
+++ b/pySim-prog.py
@@ -39,7 +39,7 @@
 
 from pySim.commands import SimCardCommands
 from pySim.cards import _cards_classes
-from pySim.utils import h2b, swap_nibbles, rpad
+from pySim.utils import h2b, swap_nibbles, rpad, derive_milenage_opc, 
calculate_luhn
 
 
 def parse_options():
@@ -233,24 +233,6 @@
                        out.append(chr(x))
 
        return ''.join(out)
-
-def calculate_luhn(cc):
-       num = map(int, str(cc))
-       check_digit = 10 - sum(num[-2::-2] + [sum(divmod(d * 2, 10)) for d in 
num[::-2]]) % 10
-       return 0 if check_digit == 10 else check_digit
-
-def derive_milenage_opc(ki_hex, op_hex):
-       """
-       Run the milenage algorithm.
-       """
-       from Crypto.Cipher import AES
-       from Crypto.Util.strxor import strxor
-       from pySim.utils import b2h
-
-       # We pass in hex string and now need to work on bytes
-       aes = AES.new(h2b(ki_hex))
-       opc_bytes = aes.encrypt(h2b(op_hex))
-       return b2h(strxor(opc_bytes, h2b(op_hex)))
 
 def gen_parameters(opts):
        """Generates Name, ICCID, MCC, MNC, IMSI, SMSP, Ki, PIN-ADM from the
diff --git a/pySim/utils.py b/pySim/utils.py
index 43d52dd..17dc693 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -92,3 +92,24 @@
        if hplmn_disp: byte1 = byte1|0x01
        if oplmn_disp: byte1 = byte1|0x02
        return i2h([byte1])+s2h(name)
+
+def derive_milenage_opc(ki_hex, op_hex):
+       """
+       Run the milenage algorithm to calculate OPC from Ki and OP
+       """
+       from Crypto.Cipher import AES
+       from Crypto.Util.strxor import strxor
+       from pySim.utils import b2h
+
+       # We pass in hex string and now need to work on bytes
+       aes = AES.new(h2b(ki_hex))
+       opc_bytes = aes.encrypt(h2b(op_hex))
+       return b2h(strxor(opc_bytes, h2b(op_hex)))
+
+def calculate_luhn(cc):
+       """
+       Calculate Luhn checksum used in e.g. ICCID and IMEI
+       """
+       num = map(int, str(cc))
+       check_digit = 10 - sum(num[-2::-2] + [sum(divmod(d * 2, 10)) for d in 
num[::-2]]) % 10
+       return 0 if check_digit == 10 else check_digit

-- 
To view, visit https://gerrit.osmocom.org/5745
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2effc85fd55da0981de0ada74dcb28b7e8e56a01
Gerrit-PatchSet: 1
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Owner: Alexander Chemeris <alexander.cheme...@gmail.com>

Reply via email to