def get_signature(self, req_certificate): ''' Create a signature for the next lower CA Input: req_certificates ECUCertificate the next lower certificate that will be signed by this CA Output: signature EncryptedMessage signed version of the passed certificate ctf_alg AsymAuthMechEnum encryption algorithm used by this CA to sign this certificate ctf_hash HashMechEnum hashing mechanism used by this CA to sign this certificate ''' signature = encryption_tools.asy_encrypt(HashedMessage(req_certificate, self.ctf_hash), self.priv_key) return signature, self.ctf_alg, self.ctf_hash
def get_signature(self, req_certificate): ''' Create a signature for the next lower CA Input: req_certificates ECUCertificate the next lower certificate that will be signed by this CA Output: signature EncryptedMessage signed version of the passed certificate ctf_alg AsymAuthMechEnum encryption algorithm used by this CA to sign this certificate ctf_hash HashMechEnum hashing mechanism used by this CA to sign this certificate ''' signature = encryption_tools.asy_encrypt( HashedMessage(req_certificate, self.ctf_hash), self.priv_key) return signature, self.ctf_alg, self.ctf_hash
def _create_signiture(self, priv_key_owner): ''' signature = hashed and encrypted message create a signature signed with the priv_key_owner of the certificate owner Input: priv_key_owner AsymetricKey private key of the owner of the certificate Output: signature EncryptedMessage signature ''' try: signature = asy_encrypt(HashedMessage(self, self.signature_hash), priv_key_owner) return signature except: return None
def send_msg(self, sender_id, message_id, message): # Sender information print("\n\nSENDER - \nTime: "+str(self.sim_env.now)+"--Communication Layer: \nI am ECU " + sender_id + "\nSending message:\n - ID: " + str(message_id)+"\n - Content: " + message.get()) # Message to be send print("\nSize of the message we want to send: "+ str(message.padded_size)) print("\nContent of the message: "+ str(message.get())) # Assume it takes 0.2 seconds to e.g. encrypt this message uid = uuid.uuid4() # BEFORE PROCESSING print("\nECU "+ str(self._ecu_id) +"Time before message sent: "+ str(self.sim_env.now)) G().mon(self.monitor_list, MonitorInput([], "AUTH_SEND_TIME_BEFORE_ENCRYPTION", self._ecu_id, self.sim_env.now, 123, message_id, message.get(), message.padded_size, 432, uid)) ''' Perform symmetric encryption (and key generation): send the message encrypted with a created key which e.g. takes 0.01 second''' algorithm = SymAuthMechEnum.AES key_length = AuKeyLengthEnum.bit_128 algorithm_mode = SymAuthMechEnum.CBC sym_key = encryption_tools.sym_get_key(algorithm, key_length, algorithm_mode) # encrypt the message with the symmetric key we just created clear_message = message.get() cipher = encryption_tools.sym_encrypt(clear_message, sym_key) sym_cipher_message = [sym_key, cipher] yield self.sim_env.timeout(0.01) ''' Hash the message we want to send and send the hash with the message, e.g. takes 0.01 second ''' hashed_message = HashedMessage(str(sym_cipher_message), HashMechEnum.MD5) yield self.sim_env.timeout(0.01) ''' Perform asymmetric encryption: Encrypt my private key which takes e.g. 0.2 seconds''' timestamp = self.sim_env.now encrypted_size = 50 # byte - usualy calculated from the size of the original message and the encryption algorithm clear_text_of_message = [timestamp, hashed_message, sym_cipher_message] cipher_message = encryption_tools.asy_encrypt(clear_text_of_message, self.priv_key) cipher_message.valid_till = timestamp + 5 # add optional validity (e.g. 5 seconds) wrapped_cipher_message = SegData(cipher_message, encrypted_size) yield self.sim_env.timeout(0.2) # AFTER PROCESSING G().mon(self.monitor_list, MonitorInput([], "AUTH_SEND_TIME_AFTER_ENCRYPTION", self._ecu_id, self.sim_env.now, 123, message_id, message.get(), message.padded_size, 432, uid)) print("\nECU "+ str(self._ecu_id) +"Time after message sent: "+ str(self.sim_env.now)) # Send message - here send your message with your message_id yield self.sim_env.process(self.transp_lay.send_msg(sender_id, message_id, wrapped_cipher_message))
def send_msg(self, sender_id, message_id, message): # Sender information # INITIALLY SEND MY CERTIFICATE TO THE OTHER ECU if not self._have_session_key: print("Sending Certificate and my private key : ") # Encrypt message with the public key encrypted_msg = encryption_tools.asy_encrypt(message, self.my_certificate.pub_key_user) # Send the certificate and message and private key message_to_send = SegData([encrypted_msg, self.my_certificate, self.certificate_private_key], 50) sec_message_id = 901 print("\n\nSENDER - \nTime: " + str( self.sim_env.now) + "--Communication Layer: \nI am ECU " + sender_id + "\nSending message:\n - ID: " + str( sec_message_id) + "\n - Content: " + str(message_to_send.get())) yield self.sim_env.process(self.transp_lay.send_msg(sender_id, sec_message_id, message_to_send)) else: # IF THE RECEIVE MSG FUNCTION STORED A SESSION KEY THIS KEY IS NOW USED TO ENCRYPT A MESSAGE # AND TO SEND THE ENCRYPTED MESSAGE # Encrypt message encrypted_msg = sym_encrypt(message, self._session_key) print("\n\nSENDER - \nTime: "+str(self.sim_env.now)+"--Communication Layer: \nI am ECU " + sender_id + "\nSending message:\n - ID: " + str(message_id)+"\n - Content: " + message.get()) # Sending message now with the session key I have print("\nEncrypted the message") # Send message - here send your message with your message_id print("%s: Time before encryption %s" % (str(self._ecu_id), str(self.sim_env.now))) encryption_time, encrypted_size = self._get_time_for_session_encryption(message) yield self.sim_env.timeout(encryption_time) # apply time for encryption print("%s: Time after encryption %s" % (str(self._ecu_id), str(self.sim_env.now))) yield self.sim_env.process(self.transp_lay.send_msg(sender_id, message_id, SegData(encrypted_msg, encrypted_size)))
def send_msg(self, sender_id, message_id, message): # Sender information print("\n\nSENDER - \nTime: " + str(self.sim_env.now) + "--Communication Layer: \nI am ECU " + sender_id + "\nSending message:\n - ID: " + str(message_id) + "\n - Content: " + message.get()) # Message to be send print("\nSize of the message we want to send: " + str(message.padded_size)) print("\nContent of the message: " + str(message.get())) # Assume it takes 0.2 seconds to e.g. encrypt this message uid = uuid.uuid4() # BEFORE PROCESSING print("\nECU " + str(self._ecu_id) + "Time before message sent: " + str(self.sim_env.now)) G().mon( self.monitor_list, MonitorInput([], "AUTH_SEND_TIME_BEFORE_ENCRYPTION", self._ecu_id, self.sim_env.now, 123, message_id, message.get(), message.padded_size, 432, uid)) ''' Perform symmetric encryption (and key generation): send the message encrypted with a created key which e.g. takes 0.01 second''' algorithm = SymAuthMechEnum.AES key_length = AuKeyLengthEnum.bit_128 algorithm_mode = SymAuthMechEnum.CBC sym_key = encryption_tools.sym_get_key(algorithm, key_length, algorithm_mode) # encrypt the message with the symmetric key we just created clear_message = message.get() cipher = encryption_tools.sym_encrypt(clear_message, sym_key) sym_cipher_message = [sym_key, cipher] yield self.sim_env.timeout(0.01) ''' Hash the message we want to send and send the hash with the message, e.g. takes 0.01 second ''' hashed_message = HashedMessage(str(sym_cipher_message), HashMechEnum.MD5) yield self.sim_env.timeout(0.01) ''' Perform asymmetric encryption: Encrypt my private key which takes e.g. 0.2 seconds''' timestamp = self.sim_env.now encrypted_size = 50 # byte - usualy calculated from the size of the original message and the encryption algorithm clear_text_of_message = [timestamp, hashed_message, sym_cipher_message] cipher_message = encryption_tools.asy_encrypt(clear_text_of_message, self.priv_key) cipher_message.valid_till = timestamp + 5 # add optional validity (e.g. 5 seconds) wrapped_cipher_message = SegData(cipher_message, encrypted_size) yield self.sim_env.timeout(0.2) # AFTER PROCESSING G().mon( self.monitor_list, MonitorInput([], "AUTH_SEND_TIME_AFTER_ENCRYPTION", self._ecu_id, self.sim_env.now, 123, message_id, message.get(), message.padded_size, 432, uid)) print("\nECU " + str(self._ecu_id) + "Time after message sent: " + str(self.sim_env.now)) # Send message - here send your message with your message_id yield self.sim_env.process( self.transp_lay.send_msg(sender_id, message_id, wrapped_cipher_message))