Esempio n. 1
0
 def sendGCM(self, plaintext):
     ciphertext, tag = self.GCM.encrypt(self.IV, plaintext)
     self.IV += 2 # self incrementing should ONLY happen here
     return self.conn.send(
         crypto.long_to_bytes(self.IV-2, 12) +
         ciphertext +
         crypto.long_to_bytes(tag, 16)
     )
Esempio n. 2
0
	def insert_counter(self):
		"""
		TODO.
			
		"""	
		smidr_long = bytes_to_long(self.smidr)
		smidr_long = (smidr_long & 0xFFFFFFFFFFFFFFE00000) | (self.tctr & 0x1FFFFF)
		self.smidr = long_to_bytes(smidr_long, __SMIDR_LEN)
Esempio n. 3
0
	def new_key3(self):
		"""
		TODO.
		
		:Returns: TODO
			
		"""	
		smidr_long = bytes_to_long(self.smidr[__SMIDR_OFF:])         # STRIP first 2 bytes from SMIDR value
		tmp1 = long_to_bytes(smidr_long | (self.shiftr & 0x1FFFFF), __DATALEN) # binary OR between shift register and SMIDR's right 8 bytes
		tmp2 = self.des_special(tmp1, self._fkr[self.key_ptr], True) # over ORed data is performed the OWF Key generation with currently pointed key on FKR
		bit_pos = self.search_bit(self.shiftr, __FKR_NUM, True)      # Search bit position on shift register
		self._fkr[bit_pos] = tmp2[:self.keyslen]                     # Update key in FKR in selected slot (by shift register)
		return True
Esempio n. 4
0
	def tag1(self):
		"""
		TODO.
		
		:Returns: TODO
			
		"""	
		if self.shiftr & self.tctr == 0: return                      # if AND check fails, return
		smidr_long = bytes_to_long(self.smidr[__SMIDR_OFF:])         # STRIP first 2 bytes from SMIDR value
		bitXorMask = ((self.shiftr - 1) & self.tctr) & 0x1FFFFF      # Define BitXorMask
		tmp1 = long_to_bytes(smidr_long ^ bitXorMask, __DATALEN)     # binary XOR between bitXorMask and SMIDR's right 8 bytes
		tmp2 = self.des_special(tmp1, self._fkr[self.key_ptr], True) # over XORed data perform OWF Key generation with currently pointed key on FKR
		self._fkr[self.key_ptr] = tmp2[:self.keyslen]                # Update key in FKR for current key slot
		return True
Esempio n. 5
0
def get_APbancomat_pin(pinblock, accountNumber):
        """
        Get the Ap bancomat pin associated with a pinblock adn an account number.
        
        :Parameters:
            - *pin\_block* (bytes array): The Pinblock.
            - *accountNumber* (string): The account number.
        
        :Returns: A string representing the pin.
        
        """ 
	if len(pinblock) != 8:
		return 'Wrong pinblock length!'
	if accountNumber is None or len(accountNumber) < 66:
		return 'Card data Faulty!'

	pan = accountNumber[3:21]
	# Instead of :
# 	sep = accountNumber.find('==')
# 	pan = accountNumber[sep - 18:sep]
	if accountNumber[55] == '=':
		CCS = accountNumber[58:66]
	else:
		CCS = accountNumber[61:69]
	if CCS.startswith('0000'):
		CCS = '99499973'
	# reference input values (with PAN only, PIN = "00000")
	i1 = int(pan[:4] + '00' + pan[6:9])
	i2 = int(pan[9:15] + '000')
	# divisor modules:
	m1 = int(CCS[:4]) #first 4 digit of CCS  
	m2 = int(CCS[4:]) #last 4 digit of CCS
	#expected result (from PIN Block received):
	r1 = int(pinblock[:4])
	r2 = int(pinblock[4:])
	# pin code value components:
	v1 = 0
	while v1 <= 99000:
		if (i1 + v1) % m1 == r1:
			break;
		v1 += 1000
	v2 = 0
	while v2 <= 999:
		if (i2 + v2) % m2 == r2:
			break;
		v2 += 1
	if (v1 > 99000 or v2 > 999):
		return 'PinBlock Not calculable!'
	return crypto.long_to_bytes(v1 + v2, 5)
Esempio n. 6
0
	def calculate_variants_for_3DESwithDUKPT(self, var_num):
		"""
		TODO.
		
		:Parameters:
			- *var\_num* (): TODO
		
		:Returns: TODO
			
		"""	
		# This function does not set the last byte in the mask
		# It is useful for 3DES-CBC encryption with DUKPT with mask 3 and 4
		mask = 0xff << ((__KEYSLEN_DES - var_num - 1) << 3)
		if self.keyslen == __KEYSLEN_3DES: mask = (mask << (__KEYSLEN_DES << 3)) | 0xff << ((__KEYSLEN_DES - var_num - 1) << 3)
		# Variant is calculated as XOR between current key and the PIN/MAC bitmask
		return long_to_bytes(mask ^ bytes_to_long(self._fkr[self.key_ptr]), self.keyslen)[-self.keyslen:]
Esempio n. 7
0
	def calculate_variants(self, var_num):
		"""
		TODO.
		
		:Parameters:
			- *var\_num* (): TODO
		
		:Returns: TODO
			
		"""	
		# The following two lines are necessary if it is called before other requests (i.e.:  after deriveKey) [MAYBE MUST BE A PRIVATE METHOD]
#		if not self.request_pin_entry1():
#			return None
		mask = 0xff << ((__KEYSLEN_DES - var_num - 1) << 3)
		if self.keyslen == __KEYSLEN_3DES: mask = (mask << (__KEYSLEN_DES << 3)) | 0xff << ((__KEYSLEN_DES - var_num - 1) << 3)
		elif var_num == __VAR_MAC_OFF: mask |= 0xff
		# Variant is calculated as XOR between current key and the PIN/MAC bitmask
		return long_to_bytes(mask ^ bytes_to_long(self._fkr[self.key_ptr]), self.keyslen)[-self.keyslen:]
Esempio n. 8
0
	def calculate_variants_Nordic(self, var_num, type = 'IN'):
		"""
		TODO.
		
		:Parameters:
			- *var\_num* (): TODO
			- *type* (string): TODO
		
		:Returns: TODO
			
		"""	
		mask = 0xff << ((__KEYSLEN_DES - var_num - 1) << 3)
		if type == 'OUT':
			mask |= 0xff
			if self.keyslen == __KEYSLEN_3DES: mask = (mask << (__KEYSLEN_DES << 3))
		if type == 'IN':
			if self.keyslen == __KEYSLEN_DES: mask |= 0xff
			else:
				 mask = (mask << (__KEYSLEN_DES << 3)) | 0xff << ((__KEYSLEN_DES - var_num - 1) << 3)

		# Variant is calculated as XOR between current key and the PIN/MAC bitmask
		return long_to_bytes(mask ^ bytes_to_long(self._fkr[self.key_ptr]), self.keyslen)[-self.keyslen:]