blob: f4fd453fec31476570dc57915217930d9ec39dee [file] [log] [blame]
Alexander Chemeris6a38c4a2018-01-26 16:24:27 +09001#!/usr/bin/env python2
2
3#
4# Utility to run an A3/A8 algorithm on a SIM card
5#
6# Copyright (C) 2018 Alexander Chemeris <alexander.chemeris@gmail.com>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21
22import sys
23from optparse import OptionParser
24from pySim.commands import SimCardCommands
25
26def parse_options():
27
28 parser = OptionParser(usage="usage: %prog [options]",
29 description="Utility to run an A3/A8 algorithm on a SIM card. "
30 "Prints generated SRES and Kc for a given RAND number "
31 "and exits.")
32
33 parser.add_option("-d", "--device", dest="device", metavar="DEV",
34 help="Serial Device for SIM access [default: %default]",
35 default="/dev/ttyUSB0",
36 )
37 parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
38 help="Baudrate used for SIM access [default: %default]",
39 default=9600,
40 )
41 parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
42 help="Which PC/SC reader number for SIM access",
43 default=None,
44 )
45 parser.add_option("-r", "--rand", dest="rand", metavar="RAND",
46 help="16 bytes of RAND value",
47 default=None,
48 )
49
50 (options, args) = parser.parse_args()
51
52 if args:
53 parser.error("Extraneous arguments")
54
55 return options
56
57
58if __name__ == '__main__':
59
60 # Parse options
61 opts = parse_options()
62
63 if opts.rand is None:
64 print("Please specify RAND value")
65 sys.exit(1)
66 if len(opts.rand) != 32:
67 print("RAND must be 16 bytes long")
68 sys.exit(1)
69
70 # Connect to the card
71 if opts.pcsc_dev is None:
72 from pySim.transport.serial import SerialSimLink
73 sl = SerialSimLink(device=opts.device, baudrate=opts.baudrate)
74 else:
75 from pySim.transport.pcsc import PcscSimLink
76 sl = PcscSimLink(opts.pcsc_dev)
77
78 # Create command layer
79 scc = SimCardCommands(transport=sl)
80
81 # Wait for SIM card
82 sl.wait_for_card()
83
84 # Program the card
85 print("Running GSM algorithm with RAND %s" % (opts.rand,))
86
87 # Run GSM A3/A8
88 (res, sw) = scc.run_gsm(opts.rand)
89 if sw == '9000':
90 sres, kc = res
91 print("SRES = %s" % (sres,))
92 print("Kc = %s" % (kc,))
93 else:
94 print("Error %s, result data '%s'" % (sw, res))
95
96 # Done for this card and maybe for everything ?
97 print "Done !\n"