def pair_device(algo='assymetric', key_size=1024): com = APDUCommunicator(algo, key_size, False) pubkey = com.send_message([0x00, 0x15, 0x00, 0x00], '') keyDER = b64decode(pubkey) keyPub = RSA.importKey(keyDER) with open("public.pem", "w") as pub_file: print("{}".format(keyPub.exportKey(format='PEM').decode('UTF-8')), file=pub_file)
def update(self, observable, actions): (addedcards, removedcards) = actions for card in addedcards: try: com = APDUCommunicator('diffie-hellman', 1024, False) com.request_otp() except Exception: print( "Connection could not be established, please try again...")
def authenticate(algo='assymetric', key_size=1024): with open('public.pem', mode='r') as public_file: key_data = public_file.read() public_key = RSA.import_key(key_data) com = APDUCommunicator(algo, key_size, False) payload = com.send_message([0x00, 0x16, 0x00, 0x00], "") password = com.request_otp() h = SHA256.new(password.encode('UTF-8')) signer = PKCS1_v1_5.new(public_key) if signer.verify(h, b64decode(payload)): # Authentication successful print('Verified!') os._exit(0) else: # Authentication failure os._exit(1)
def basic_diffie(): # Load pre-generated primes from file with open('primes.json', 'r') as input_primes: primes = json.load(input_primes) # Randomly choose one pair of prime and it's primitive root modulo random_pair = random.choice(primes) n = random_pair['prime'] g = random_pair['root'] x = 6 com = APDUCommunicator() com.send_message(APDUHeader.SEND_DH_N, str(n)) com.send_message(APDUHeader.SEND_DH_G, str(g)) bob_sends = com.send_message(APDUHeader.SEND_DH_ALICE, str(Encryption.dh_alice_sends(n, g, x))) print("Bob: " + MessageConverter.byte_array_to_string(bob_sends)) aes_key = str( int(MessageConverter.byte_array_to_string(bob_sends))**x % n).encode() otp = com.send_message(APDUHeader.REQUEST_OTP_DH, "") otp = MessageConverter.byte_array_to_string(otp) print(otp) print(Encryption.aes_decrypt(otp, aes_key))
def asymmetric(count, always_new_instance=False): results = [] start = time.time() a = APDUCommunicator('asymmetric', False) for i in range(count): if always_new_instance: single_start = time.time() del a a = APDUCommunicator('asymmetric', False) a.request_otp() single_stop = time.time() results.append(single_stop - single_start) print(str(i) + "/" + str(count) + " " + str(single_stop - single_start)) # end = time.time() print("STDEV: " + str(statistics.stdev(results))) print("MEDIAN: " + str(statistics.median(results))) print("MEAN: " + str(statistics.mean(results)))
def diffie_hellman(count, always_new_instance=False): results = [] # start = time.time() a = APDUCommunicator('diffie-hellman', False) for i in range(count): # time.sleep(0.2) if always_new_instance: del a single_start = time.time() a = APDUCommunicator('diffie-hellman', False) a.request_otp() single_stop = time.time() results.append(single_stop - single_start) print(str(i) + "/" + str(count) + " " + str(single_stop - single_start)) # end = time.time() print("STDEV: " + str(statistics.stdev(results))) print("MEDIAN: " + str(statistics.median(results))) print("MEAN: " + str(statistics.mean(results))) return sum (results) / count
def basic_ssl(): try: com = APDUCommunicator() public_key = MessageConverter.byte_array_to_string( com.send_message(APDUHeader.REQUEST_PUBLIC_KEY, "")) aes_key = generate_aes_key().encode() keyDER = b64decode(public_key) keyPub = RSA.importKey(keyDER) cipher = Cipher_PKCS1_v1_5.new(keyPub) cipher_text = cipher.encrypt(aes_key) b64message = base64.b64encode(cipher_text) com.send_message(APDUHeader.SEND_AES_KEY, MessageConverter.format_hex_array(b64message.hex())) otp = com.send_message(APDUHeader.REQUEST_OTP, "") otp = MessageConverter.byte_array_to_string(otp) print('KEY: ', aes_decrypt(otp, aes_key)) except: pass