示例#1
0
文件: otrdh.py 项目: eXcomm/yapyotr
	def encrypt_data_message(self, msg):
		#global memo
		# encrypt the message
		counter = _OT.zero_pad(_OT.int_to_bytes(self.ctr), 8)
		enc_msg = OtrCrypt.aes_ctr_crypt(self.send_aes_key, msg, self.ctr)
		
		memo.enc_msg = enc_msg
		
		flags = [0x00]
		
		#memo.flags = flags
		
		# generate T = (my_keyid, their_keyid, next_dh, ctr, AES-CTR_ek,ctr(msg))
		T = [0,2,3, flags[0]] # protocol version and msg code
		# my_keyid
		sender_keyid = _OT.zero_pad(_OT.int_to_bytes(self.my_sess_keyid), 4)
		#memo.sender_keyid = sender_keyid
		T.extend( sender_keyid )
		# their_keyid
		recipient_keyid = _OT.zero_pad(_OT.int_to_bytes(self.their_sess_keyid), 4)
		#memo.recipient_keyid = recipient_keyid
		T.extend( recipient_keyid )
		# next_dh
		next_dh = _OT.int_to_mpi(self.next_dh)
		#memo.next_dh = next_dh
		T.extend( next_dh )
		# ctr
		#memo.counter = counter
		T.extend( counter )
		# enc_msg
		T.extend( _OT.bytes_to_data(enc_msg) )
		
		#memo.T = T
		
		# compute MAC_mk(T)
		authenticator = OtrCrypt.get_sha1_hmac(self.send_mac_key, T)
		
		#memo.authenticator = authenticator
		#memo.old_mac_keys = self.old_mac_keys
		
		#memo.send_mac_key = self.send_mac_key
		#memo.recv_mac_key = self.recv_mac_key
		#memo.send_aes_key = self.send_aes_key
		#memo.recv_aes_key = self.recv_aes_key
		
		#memo.secbytes = self.secbytes
		#memo.sender_factor = self.my_public_factor_to_mpi(self.my_sess_keyid)
		
		#print "SENDER"
		#for x in sorted(self.my_public_factors.keys()):
		#	print (x, self.my_public_factor_to_mpi(x)) 
		#r = raw_input()
		
		#memo.recipient_factor = self.their_public_factor_to_mpi(self.their_sess_keyid)
		return (flags, sender_keyid, recipient_keyid, next_dh, counter,
			_OT.bytes_to_data(enc_msg), authenticator, _OT.bytes_to_data(self.old_mac_keys))
示例#2
0
文件: otrdh.py 项目: eXcomm/yapyotr
	def receive_data_message(self, msg):
		#global memo
		self.my_sess_keyid = _OT.bytes_to_int(msg.recipient_keyid)
		self.their_sess_keyid = _OT.bytes_to_int(msg.sender_keyid)
		logging.debug( "KEYIDS %d %d " % (self.my_sess_keyid, self.their_sess_keyid))
		if len(msg.next_dh) > 4:
			logging.debug( "GOT NEXT DH" )
			logging.debug( msg.next_dh )
			self.associate_their_keyid(self.their_sess_keyid+1, _OT.mpi_to_int(msg.next_dh))
		self.update_next_counter(self.my_sess_keyid, self.their_sess_keyid, _OT.bytes_to_int(msg.counter))
		logging.debug( (msg.counter, _OT.int_to_bytes(self.ctr)) )
		self.compute_c_and_m_factors(self.my_sess_keyid, self.their_sess_keyid)
		self.compute_ek_and_mk_factors(self.my_sess_keyid, self.their_sess_keyid)
	
		#assert memo.sender_keyid == msg.sender_keyid
		#assert memo.recipient_keyid == msg.recipient_keyid
		#assert memo.next_dh == msg.next_dh
		#assert memo.counter == msg.counter
		#assert memo.enc_msg == _OT.data_to_bytes(msg.enc_msg)
		
		T = [0,2,3, msg.flags[0]] # protocol version and type code 
		# my_keyid
		T.extend( msg.sender_keyid )
		# their_keyid
		T.extend( msg.recipient_keyid )
		# next_dh
		T.extend( msg.next_dh )
		# ctr
		T.extend( msg.counter )
		# enc_msg
		logging.debug(("ENC DATA: ", msg.enc_msg))
		T.extend( msg.enc_msg )
		
		#assert memo.T == T
		#print memo.sender_factor
		
		#print "RECVER"
		#for x in sorted(self.their_public_factors.keys()):
		#	print (x, self.their_public_factor_to_mpi(x)) 
		#r = raw_input()
		
		#assert memo.sender_factor == self.their_public_factor_to_mpi(self.their_sess_keyid)
		#assert memo.recipient_factor == self.my_public_factor_to_mpi(self.my_sess_keyid)
		#assert memo.secbytes == self.secbytes
		# compute MAC_mk(T)
		auth_check = OtrCrypt.get_sha1_hmac(self.recv_mac_key, T)
		
		if auth_check != msg.authenticator:
			logging.debug( ("got: ", auth_check) )
			logging.debug( ("exp: ", msg.authenticator) )
			#print self.recv_mac_key, self.send_mac_key
			#print memo.send_mac_key
			raise Exception("mac fail")
		
		return OtrCrypt.aes_ctr_crypt(self.recv_aes_key, _OT.data_to_bytes(msg.enc_msg), msg.counter)