0% found this document useful (0 votes)
6 views5 pages

Mobile Computing Lab Codes

This document outlines an experiment focused on implementing GSM security algorithms A3, A5, and A8 using Python. It details the theoretical background of GSM security procedures, including user authentication and data encryption processes, as well as the specific algorithms used for these purposes. The provided code implements the A3/A8 authentication and A5/1 encryption algorithms, demonstrating how to generate authentication responses and keystreams for secure communication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Mobile Computing Lab Codes

This document outlines an experiment focused on implementing GSM security algorithms A3, A5, and A8 using Python. It details the theoretical background of GSM security procedures, including user authentication and data encryption processes, as well as the specific algorithms used for these purposes. The provided code implements the A3/A8 authentication and A5/1 encryption algorithms, demonstrating how to generate authentication responses and keystreams for secure communication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT No.

AIM: Implementation of GSM security algorithms (A3/A5/A8)


SOFTWARE REQUIREMENT: Python
THEORY:
The security procedures in GSM are aimed at protecting the network against unauthorized
access and protecting the privacy of mobile subscriber against eavesdropping, eavesdropping
on subscriber communication is prevented by ciphering the information. To protect identity
and location of the subscriber the appropriate signaling channels are ciphered and Temporary
Subscriber Identity (TMSI) instead of IMSI is used over the radio path. At the time of
initiating a service, the mobile terminal is powered on the subscriber may be required to enter
4-8 digits Password Identification Number (PIN) to validate the ownership of the SIM. At the
time of service provisioning the IMSI, the individual subscriber authentication key (Ki), the
authentication algorithm (A3), the cipher key generation algorithm (A8) and the encryption
algorithm (A5) are programmed into the SIM by GSM operator. The A3 ciphering algorithm
is used to authenticate each mobile by verifying the user password within the SIM with the
cryptographic key at the MSC. The A5 ciphering algorithm is used for encryption. It provides
scrambling for 114 coded bits sent in each TS. The A8 is used for ciphering key. The IMSI
and the secret authentication key (Ki) are specific to each mobile station, the authentication
algorithm A3 and A8 are different for different networks and operator’s encryption algorithm
A5 is unique and needs to be used across all GSM network operators. The authentication
center is responsible for all security aspects and its function is closely linked with HLR. The
secret authentication key (Ki) is not known to mobile user and is the property of service
provider, the home system of the mobile station (MS) generates the random number say Rand
which is 126-bit number. This random number is sent to MS. The MS uses A3 algorithm to
authenticate the user. The algorithm A3 uses Ki and Rand number to generate a signed

result called s_RES. MS sends s_RES to home system of MS. In the home system
authentication contains Ki and it also uses the same authentication algorithm A3 to
authenticate the valid user. The A3 algorithm use Ki and Rand generated by home system to
generate a signed result called
〖(s〗_RES). The s_RES generated by MS and authentication center are compared. If both
s_RES are identical only then the user is valid and access is granted otherwise no

Code:

def comp128(Ki, RAND):


"""
Ki : 16-byte secret key
RAND : 16-byte random challenge
Returns:
SRES (4 bytes)
Kc (8 bytes)
"""
x = [Ki[i] ^ RAND[i] for i in range(16)]

for _ in range(8):
for i in range(16):
x[i] = ((x[i] << 1) | (x[i] >> 7)) & 0xFF
x[i] ^= (i * 13)
SRES = bytes(x[:4])
Kc = bytes(x[4:12])

return SRES, Kc

class A5_1:
def __init__(self, Kc, frame):
self.R1 = [0] * 19
self.R2 = [0] * 22
self.R3 = [0] * 23

self.load_key(Kc)
self.load_frame(frame)

def majority(self, x, y, z):


return (x & y) | (x & z) | (y & z)

def clock_register(self, reg, taps):


fb = 0
for t in taps:
fb ^= reg[t]
[Link]()
[Link](0, fb)

def clock_all(self):
self.clock_register(self.R1, [13,16,17,18])
self.clock_register(self.R2, [20,21])
self.clock_register(self.R3, [7,20,21,22])

def load_key(self, Kc):


key_bits = bin(int.from_bytes(Kc, "big"))[2:].zfill(64)
for bit in key_bits:
b = int(bit)
self.R1[0] ^= b
self.R2[0] ^= b
self.R3[0] ^= b
self.clock_all()

def load_frame(self, frame):


frame_bits = bin(frame)[2:].zfill(22)
for bit in frame_bits:
b = int(bit)
self.R1[0] ^= b
self.R2[0] ^= b
self.R3[0] ^= b
self.clock_all()

def clock_majority(self):
m = [Link](self.R1[8], self.R2[10], self.R3[10])

if self.R1[8] == m:
self.clock_register(self.R1, [13,16,17,18])
if self.R2[10] == m:
self.clock_register(self.R2, [20,21])
if self.R3[10] == m:
self.clock_register(self.R3, [7,20,21,22])

def keystream(self, length=114):


ks = []
for _ in range(length):
self.clock_majority()
bit = self.R1[-1] ^ self.R2[-1] ^ self.R3[-1]
[Link](bit)
return ks

def main():
print("\n===== GSM SECURITY IMPLEMENTATION =====")

# Sample inputs
Ki = [Link]("465B5CE8B199B49FAA5F0A2EE238A6BC")
RAND = [Link]("23553CBE9637A89D218AE64DAE47BF35")
frame_number = 0x134

print("\nKi :", [Link]())


print("RAND :", [Link]())

# A3/A8
SRES, Kc = comp128(Ki, RAND)
print("\n--- A3 / A8 OUTPUT ---")
print("SRES (Authentication):", [Link]())
print("Kc (Cipher Key) :", [Link]())

# A5/1
cipher = A5_1(Kc, frame_number)
ks = [Link]()
print("\n--- A5/1 OUTPUT ---")
print("Frame Number:", frame_number)
print("Keystream :", "".join(map(str, ks)))
print("\n===== END =====")

if __name__ == "__main__":
main()

Output:

You might also like