├── .gitignore ├── LICENSE ├── Pipfile ├── README.md ├── kiopcgen ├── kiopcgenerator ├── __init__.py └── lib.py ├── requirements.txt ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/pycharm,python,linux 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=pycharm,python,linux 4 | 5 | ### Linux ### 6 | *~ 7 | 8 | # temporary files which can be created if a process still has a handle open of a deleted file 9 | .fuse_hidden* 10 | 11 | # KDE directory preferences 12 | .directory 13 | 14 | # Linux trash folder which might appear on any partition or disk 15 | .Trash-* 16 | 17 | # .nfs files are created when an open file is removed but is still being accessed 18 | .nfs* 19 | 20 | ### PyCharm ### 21 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 22 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 23 | 24 | # User-specific stuff 25 | .idea/**/workspace.xml 26 | .idea/**/tasks.xml 27 | .idea/**/usage.statistics.xml 28 | .idea/**/dictionaries 29 | .idea/**/shelf 30 | 31 | # Generated files 32 | .idea/**/contentModel.xml 33 | 34 | # Sensitive or high-churn files 35 | .idea/**/dataSources/ 36 | .idea/**/dataSources.ids 37 | .idea/**/dataSources.local.xml 38 | .idea/**/sqlDataSources.xml 39 | .idea/**/dynamic.xml 40 | .idea/**/uiDesigner.xml 41 | .idea/**/dbnavigator.xml 42 | 43 | # Gradle 44 | .idea/**/gradle.xml 45 | .idea/**/libraries 46 | 47 | # Gradle and Maven with auto-import 48 | # When using Gradle or Maven with auto-import, you should exclude module files, 49 | # since they will be recreated, and may cause churn. Uncomment if using 50 | # auto-import. 51 | # .idea/artifacts 52 | # .idea/compiler.xml 53 | # .idea/jarRepositories.xml 54 | # .idea/modules.xml 55 | # .idea/*.iml 56 | # .idea/modules 57 | # *.iml 58 | # *.ipr 59 | 60 | # CMake 61 | cmake-build-*/ 62 | 63 | # Mongo Explorer plugin 64 | .idea/**/mongoSettings.xml 65 | 66 | # File-based project format 67 | *.iws 68 | 69 | # IntelliJ 70 | out/ 71 | 72 | # mpeltonen/sbt-idea plugin 73 | .idea_modules/ 74 | 75 | # JIRA plugin 76 | atlassian-ide-plugin.xml 77 | 78 | # Cursive Clojure plugin 79 | .idea/replstate.xml 80 | 81 | # Crashlytics plugin (for Android Studio and IntelliJ) 82 | com_crashlytics_export_strings.xml 83 | crashlytics.properties 84 | crashlytics-build.properties 85 | fabric.properties 86 | 87 | # Editor-based Rest Client 88 | .idea/httpRequests 89 | 90 | # Android studio 3.1+ serialized cache file 91 | .idea/caches/build_file_checksums.ser 92 | 93 | ### PyCharm Patch ### 94 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 95 | 96 | # *.iml 97 | # modules.xml 98 | # .idea/misc.xml 99 | # *.ipr 100 | 101 | # Sonarlint plugin 102 | # https://plugins.jetbrains.com/plugin/7973-sonarlint 103 | .idea/**/sonarlint/ 104 | 105 | # SonarQube Plugin 106 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin 107 | .idea/**/sonarIssues.xml 108 | 109 | # Markdown Navigator plugin 110 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced 111 | .idea/**/markdown-navigator.xml 112 | .idea/**/markdown-navigator-enh.xml 113 | .idea/**/markdown-navigator/ 114 | 115 | # Cache file creation bug 116 | # See https://youtrack.jetbrains.com/issue/JBR-2257 117 | .idea/$CACHE_FILE$ 118 | 119 | # CodeStream plugin 120 | # https://plugins.jetbrains.com/plugin/12206-codestream 121 | .idea/codestream.xml 122 | 123 | ### Python ### 124 | # Byte-compiled / optimized / DLL files 125 | __pycache__/ 126 | *.py[cod] 127 | *$py.class 128 | 129 | # C extensions 130 | *.so 131 | 132 | # Distribution / packaging 133 | .Python 134 | build/ 135 | develop-eggs/ 136 | dist/ 137 | downloads/ 138 | eggs/ 139 | .eggs/ 140 | lib/ 141 | lib64/ 142 | parts/ 143 | sdist/ 144 | var/ 145 | wheels/ 146 | pip-wheel-metadata/ 147 | share/python-wheels/ 148 | *.egg-info/ 149 | .installed.cfg 150 | *.egg 151 | MANIFEST 152 | 153 | # PyInstaller 154 | # Usually these files are written by a python script from a template 155 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 156 | *.manifest 157 | *.spec 158 | 159 | # Installer logs 160 | pip-log.txt 161 | pip-delete-this-directory.txt 162 | 163 | # Unit test / coverage reports 164 | htmlcov/ 165 | .tox/ 166 | .nox/ 167 | .coverage 168 | .coverage.* 169 | .cache 170 | nosetests.xml 171 | coverage.xml 172 | *.cover 173 | *.py,cover 174 | .hypothesis/ 175 | .pytest_cache/ 176 | pytestdebug.log 177 | 178 | # Translations 179 | *.mo 180 | *.pot 181 | 182 | # Django stuff: 183 | *.log 184 | local_settings.py 185 | db.sqlite3 186 | db.sqlite3-journal 187 | 188 | # Flask stuff: 189 | instance/ 190 | .webassets-cache 191 | 192 | # Scrapy stuff: 193 | .scrapy 194 | 195 | # Sphinx documentation 196 | docs/_build/ 197 | doc/_build/ 198 | 199 | # PyBuilder 200 | target/ 201 | 202 | # Jupyter Notebook 203 | .ipynb_checkpoints 204 | 205 | # IPython 206 | profile_default/ 207 | ipython_config.py 208 | 209 | # pyenv 210 | .python-version 211 | 212 | # pipenv 213 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 214 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 215 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 216 | # install all needed dependencies. 217 | Pipfile.lock 218 | 219 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 220 | __pypackages__/ 221 | 222 | # Celery stuff 223 | celerybeat-schedule 224 | celerybeat.pid 225 | 226 | # SageMath parsed files 227 | *.sage.py 228 | 229 | # Environments 230 | .env 231 | .venv 232 | env/ 233 | venv/ 234 | ENV/ 235 | env.bak/ 236 | venv.bak/ 237 | pythonenv* 238 | 239 | # Spyder project settings 240 | .spyderproject 241 | .spyproject 242 | 243 | # Rope project settings 244 | .ropeproject 245 | 246 | # mkdocs documentation 247 | /site 248 | 249 | # mypy 250 | .mypy_cache/ 251 | .dmypy.json 252 | dmypy.json 253 | 254 | # Pyre type checker 255 | .pyre/ 256 | 257 | # pytype static type analyzer 258 | .pytype/ 259 | 260 | # profiling data 261 | .prof 262 | 263 | # End of https://www.toptal.com/developers/gitignore/api/pycharm,python,linux 264 | 265 | *.pyc 266 | .idea/ 267 | build 268 | dist 269 | kiopcgenerator.egg-info 270 | .env 271 | .vscode 272 | .eggs 273 | 274 | /.idea/workspace.xml 275 | .idea/ 276 | /kiopcgenerator/.idea/ 277 | .idea/kiopcgenerator.iml 278 | .idea 279 | 280 | venv 281 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | url = "https://pypi.python.org/simple" 3 | verify_ssl = true 4 | name = "pypi" 5 | 6 | [packages] 7 | pycryptodome = "*" 8 | 9 | [dev-packages] 10 | 11 | [requires] 12 | python_version = "3.8" 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ki/OPc Generator 2 | 3 | Ki/OPc USIM card keys geneartion. This script will produce Ki/eKi/OPc triplets given the Op and Transport keys. 4 | 5 | OPc was the ultimate key that is generated from OP and Ki (secret Key). 6 | Generate your Ki secret keys and grab the OP and Transport keys from your carrier. 7 | 8 | **NOTE**: This package has been tested with Python3.6.9 and Python3.8 9 | 10 | ## Table of Contents 11 | 12 | - [Description](#description) 13 | - [Usage](#usage) 14 | - [Installation](#installation) 15 | - [Support](#support) 16 | - [Contributing](#contributing) 17 | 18 | ## Description 19 | 20 | OP: Operator Code : It is allotted to an operator and used in key generation algorithms of 3G and 4G. It is not shown as a part of input, because it is not specific to a user/Subscriber/SIM. It remains fix for all Subscriber/SIM of an operator that is why it is not used as an input to key generation algorithms. This OP (a 128-bits Operator Variant Algorithm Configuration Field )value is passed to an encryption algorithm ("RijndaelEncrypt") to generate OPc and OPc is used in all f1,f2,f3,f4,f5 functions internally to generate various keys. 21 | 22 | As OP value is single, same to all subscriber/SIM. If someone knows it then there can be a possibility of spoofing of all SIM, because all SIMs are using the same value of OP. So Operator come up with the solution that they shall provision OPc rather than OP in AuC or HLR/HSS. When f1,f2....f5 get the OPc they doesn't generate it from OP; received OPC is used in vector generation. There is no reverse engineering for OP from OPC. 23 | 24 | Basically OPc was the ultimate key that is generated from OP and KEy (secret Key) by using ("RijndaelEncrypt") algorithm which is specific to SIM. if some one able to theft OPc then it can spoof only single SIM not all the SIMs. 25 | 26 | OPc=Encypt-Algo(OP,Key) 27 | OPc -[128 Bits] 28 | 29 | Transport Key (64-Bits) : This key is used as a Lock to KEY (secret key) and OPc. When authentication credentials are to be provisioned at AuC or HLR/HSS; then they are provisioned in encrypted form rather then plain and this encryption is done by Transport Key. 30 | When authentication credentials are to be used in Authentication Generation then; all fields are decrypted to plain key by transport key; and now plain key is used f1,f2,f3,f4,f5 algorithms. 31 | 32 | ## Usage 33 | 34 | Use from the command line with the **kiopcgen** tool: 35 | 36 | ``` 37 | Usage: kiopcgen [options] 38 | 39 | Options: 40 | -h, --help show this help message and exit 41 | -o OP, --op=OP 32 char OP key 42 | -t TRANS, --transport=TRANS 32 char Transport key 43 | -k KI, --ki=KI Optional 32 char Ki key (to avoid random generation) 44 | 45 | $ kiopcgen -o D7DECB1F50404CC29ECBF989FE73AFC5 -t 2257CC6E9746434B89F346F0276CCAEC 46 | {'KI': '780E6AC95A2E43449C15BDCDD0450982', 47 | 'OPC': '2274B84B8043105A28AABBE53EF1D014', 48 | 'eKI': '4601138387FCF7D666ED24BBB3EE37B8'} 49 | 50 | $ kiopcgen -o D7DECB1F50404CC29ECBF989FE73AFC5 -t 2257CC6E9746434B89F346F0276CCAEC -k 8978B79E7C104F678FA5C336509DB188 51 | {'KI': '8978B79E7C104F678FA5C336509DB188', 52 | 'OPC': '6F2E82855DEE7C893CB1F7A72FD08B57', 53 | 'eKI': 'FBE8C170F6A5C6C257E5324719674818'} 54 | ``` 55 | 56 | Or import the kiopcgenerator module to use in your scripts 57 | 58 | ```python 59 | import pprint 60 | import uuid 61 | import kiopcgenerator 62 | 63 | op = "D7DECB1F50404CC29ECBF989FE73AFC5" 64 | transport = "2257CC6E9746434B89F346F0276CCAEC" 65 | ki = kiopcgenerator.gen_ki() # Generates random ki 66 | 67 | print (ki) 68 | # EBD77DF6CFF949448ACF82B8FE4E59E3 69 | print (kiopcgenerator.gen_opc(op, ki)) 70 | # 33244F04A86408A53110D1FCAFD04288 71 | print (kiopcgenerator.gen_eki(transport, ki)) 72 | # 8FAC9FE22D306EA4CB86279B3473D8CB 73 | print (kiopcgenerator.gen_opc_eki(op, transport, ki)) 74 | # {'KI': 'EBD77DF6CFF949448ACF82B8FE4E59E3', 'eKI': '8FAC9FE22D306EA4CB86279B3473D8CB', 'OPC': '33244F04A86408A53110D1FCAFD04288'} 75 | ``` 76 | 77 | ## Installation 78 | 79 | Before install this you need to setup below dependency: 80 | 81 | ``` 82 | For Python 3.x 83 | $ sudo apt-get install python3-dev 84 | 85 | For Python 3.4 86 | $ sudo apt-get install python3.4-dev 87 | 88 | For Python 3.6.x 89 | $ sudo apt-get install python3.6-dev 90 | 91 | For Python 3.7 92 | $ sudo apt-get install python3.7-dev 93 | 94 | For Python 3.8 95 | $ sudo apt-get install python3.8-dev 96 | ``` 97 | 98 | Using PyPI repository 99 | 100 | ``` 101 | $ pip install git+https://github.com/PodgroupConnectivity/kiopcgenerator#egg=kiopcgenerator 102 | ``` 103 | 104 | From source code 105 | 106 | ``` 107 | $ python setup.py install 108 | ``` 109 | 110 | NOTE: You may want to install the dependencies in advance, if you haven't yet. 111 | ``` 112 | $ pip install -r requirements.txt 113 | ``` 114 | 115 | ## Support 116 | 117 | Please [open an issue](https://github.com/PodgroupConnectivity/kiopcgenerator/issues/new) for support. 118 | 119 | ## Contributing 120 | 121 | Please contribute using [Github Flow](https://guides.github.com/introduction/flow/). Create a branch, add commits, and open a pull request. -------------------------------------------------------------------------------- /kiopcgen: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # vim: ts=4 4 | ### 5 | # 6 | # Copyright (c) 2019 Podsystem Ltd 7 | # Authors : J. Félix Ontañón 8 | 9 | import sys 10 | 11 | import pprint 12 | from optparse import OptionParser 13 | 14 | import kiopcgenerator 15 | 16 | parser = OptionParser() 17 | parser.add_option("-o", "--op", dest="op", help="32 char OP key") 18 | parser.add_option("-t", "--transport", dest="trans", help="32 char Transport key") 19 | parser.add_option("-k", "--ki", dest="ki", help="Optional 32 char Ki key (to avoid random generation)") 20 | (options, args) = parser.parse_args() 21 | 22 | if options.op and options.trans: 23 | if not options.ki: 24 | options.ki = kiopcgenerator.gen_ki() 25 | 26 | result = kiopcgenerator.gen_opc_eki(options.op, options.trans, options.ki) 27 | pprint.pprint(result) 28 | 29 | sys.exit(0) 30 | else: 31 | sys.exit(1) 32 | -------------------------------------------------------------------------------- /kiopcgenerator/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # vim: ts=4 4 | ### 5 | # 6 | # Copyright (c) 2019 Podsystem Ltd 7 | # Authors : J. Félix Ontañón 8 | 9 | from .lib import * -------------------------------------------------------------------------------- /kiopcgenerator/lib.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # vim: ts=4 4 | ### 5 | # 6 | # Copyright (c) 2019 Podsystem Ltd 7 | # Authors : J. Félix Ontañón 8 | # Modify for python3: Mahfuzur Rahman Khan 9 | 10 | """ 11 | OPc and eKi generator for python3 and upper version. 12 | More info: https://diameter-protocol.blogspot.com/2013/06/usage-of-opopc-and-transport-key.html 13 | """ 14 | import uuid 15 | import binascii 16 | from Crypto.Cipher import AES 17 | 18 | # from card.utils import stringToByte, byteToString - Removed from package as not needed. 19 | 20 | # Based on OSMO-SIM-AUTH library: https://osmocom.org/projects/osmo-sim-auth 21 | class AuChss: 22 | """ 23 | implements a simple AuC / HSS with no persistent storage 24 | set _debug = 1 to print out internal debug 25 | """ 26 | _debug = 0 27 | 28 | def __init__(self, op_hex="00000000000000000000000000000000", debug=0): 29 | self.op_bin = bytes(op_hex, 'utf-8') # Operator Key 30 | self.op = self.op_bin.decode('utf-8') 31 | self.users = [] 32 | 33 | def calc_opc_hex(self, k_hex, op_hex=None): 34 | iv = binascii.unhexlify(16 * '00') 35 | ki = binascii.unhexlify(k_hex) 36 | 37 | if not op_hex == None: 38 | op = binascii.unhexlify(op_hex) 39 | else: 40 | op = binascii.unhexlify(self.op) 41 | if self._debug: 42 | print(f'[DBG]calc_opc_hex: op({len(op)}) KI({len(ki)}) IV({len(iv)})') 43 | print(f'[DBG]calc_opc_hex: OP, {op} KI, {ki} IV, {iv}') 44 | 45 | aes_crypt = AES.new(ki, mode=AES.MODE_CBC, IV=iv) 46 | data = op 47 | o_pc = self._xor_str(data, aes_crypt.encrypt(data)) 48 | return binascii.hexlify(o_pc) 49 | 50 | def _xor_str(self, s, t): 51 | """xor two strings together""" 52 | return bytes([_a ^ _b for _a, _b in zip(s, t)]) 53 | 54 | # Using 16bit zeroes as IV for the AES algo 55 | IV = binascii.unhexlify('00000000000000000000000000000000') 56 | 57 | def aes_128_cbc_encrypt(key, text): 58 | """ 59 | implements aes 128b encryption with cbc. 60 | """ 61 | keyb = binascii.unhexlify(key) 62 | textb = binascii.unhexlify(text) 63 | encryptor = AES.new(keyb, AES.MODE_CBC, IV=IV) 64 | ciphertext = encryptor.encrypt(textb) 65 | return ciphertext.hex().upper() 66 | 67 | def gen_ki(): 68 | """ 69 | Clear ki random generator 70 | """ 71 | return str(uuid.uuid4()).replace('-', '').upper() 72 | 73 | 74 | def gen_opc(op, ki): 75 | """ 76 | generates opc based on op and ki 77 | """ 78 | hss = AuChss() 79 | return hss.calc_opc_hex(ki, op).upper() 80 | 81 | 82 | def gen_eki(transport, ki): 83 | """ 84 | generates eKI based on ki and transport key 85 | """ 86 | return aes_128_cbc_encrypt(transport, ki) 87 | 88 | 89 | def gen_opc_eki(op, transport, ki): 90 | return {"KI": ki, "OPC": gen_opc(op, ki), "eKI": gen_eki(transport, ki)} 91 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pycryptodome==3.9.9 -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | # Inside of setup.cfg 2 | [metadata] 3 | description-file = README.md 4 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # vim: ts=4 4 | ### 5 | # 6 | # Copyright (c) 2019 Pod Group Ltd 7 | # Authors : J. Félix Ontañón 8 | # Modify for python3: Mahfuzur Rahman Khan 9 | 10 | from setuptools import setup 11 | 12 | setup( 13 | name="kiopcgenerator", 14 | author="J. Félix Ontañón", 15 | author_email="felix.ontanon@podgroup.com", 16 | url="https://github.com/PodgroupConnectivity/kiopcgenerator", 17 | description="OPc USIM card keys geneartion. This script will produce Ki/eKi/OPc triplets given the Op and Transport keys.", 18 | long_description=open("README.md", "r").read(), 19 | classifiers=[ 20 | 'Development Status :: 0.1.2 - Beta', 21 | 'License :: OSI Approved :: GNU General Public License (GPL)', 22 | 'Operating System :: POSIX', 23 | 'Programming Language :: Python', 24 | 'Topic :: Utilities' 25 | ], 26 | keywords=['python', 'sim', 'simcard', 'telecoms'], 27 | version="0.1.4", 28 | license="GPLv3", 29 | setup_requies=['wheel'], 30 | install_requires=[ 31 | 'pycrypto' 32 | ], 33 | packages=['kiopcgenerator'], 34 | scripts=["kiopcgen"] 35 | ) --------------------------------------------------------------------------------