def main(): message = 'This is my benchmarking message; it should really be longer' e1 = time.time() #Time key generation: EC eck1 = ec.generate_private_key(ec.SECP384R1(), default_backend()) eck2 = ec.generate_private_key(ec.SECP384R1(), default_backend()) e2 = time.time() #Time key generation: RSA rsak = rsa.generate_private_key(public_exponent=65537, key_size=4096, backend=default_backend()) e3 = time.time() #Time Encryption: EC ecct = transport_security.construct_message(message, srcprivkey=eck1, destpubkey=eck2.public_key()) e4 = time.time() #Time Encryption: RSA rsact = transport_security.get_raw_encrypted(message, pubkey=rsak.public_key()) e5 = time.time() #Time Decryption: EC ecpt = transport_security.deconstruct_message(message_dict=ecct, destprivkey=eck2, srcpubkey=eck1.public_key()) e6 = time.time() if ecpt == message: print("EC Decryption successful") else: print("EC Decryption failed") #Time Decryption: RSA rsapt = transport_security.get_raw_decrypted(ciphertext=rsact, privkey=rsak) e7 = time.time() if ecpt == message: print("RSA Decryption successful") else: print("RSA Decryption failed") with open('timing.out', 'a') as f: record = "{}\t{}\t{}\t{}\t{}\t{}\n".format(e2-e1, e3-e2, e4-e3, e5-e4, e6-e5, e7-e6) f.write(record)
def main(): #priv1 = ec.generate_private_key(ec.SECP384R1(), default_backend()) priv2 = ec.generate_private_key(ec.SECP384R1(), default_backend()) priv1 = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()) #priv2 = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()) print(isinstance(priv1, rsa.RSAPrivateKey)) print(isinstance(priv2, ec.EllipticCurvePrivateKey)) #priv1 parts message = "Hello cryptography!" tosend = transport_security.get_raw_encrypted(message, pubkey=priv1.public_key()) print(tosend) #priv2 parts message_recvd = transport_security.get_raw_decrypted(tosend, privkey=priv1) print(message_recvd)
def signup(self, password, key_pass, extras=None): """ Registraion, only if the configuration file did not contain login info or was missing We first request a new ID from th auth server Then we confirm that ID by sending in our authentication info """ self._logger.info("Initiating signup protocol") # Requesting a new ID # XXX: Change to https is servers are using TLS url = "http://{}:{}/auth/signup/new".format(self._authaddr, self._authport) self._logger.info("Requesting new id at: %s", url) try: resp = requests.get(url=url) except requests.exceptions.ConnectionError: self._logger.error("Error while connecting to Auth Server") return False except requests.exceptions.Timeout: self._logger.error("Timeout while connecting to Auth Server") return False if resp.status_code != 200: # This should never happen unless the auth server ran out of IDs self._logger.error("Somehow the Auth Server replied with an error: %s", resp.status_code) return False # Check response status, response should contain the new ID and the RSA public key payload = resp.json() if not all(k in payload for k in ["id", "rsapublickey"]): self._logger.error("Server response is incomplete") return False # Build the RSA public key rsakey = self._build_rsa_server_key(payload["rsapublickey"]) self._userid = payload["id"] self._logger.info("Successfully got an ID from the server") ### Now finalize the ID ### self._logger.info("Trying to finalize the signup process") self._privatekey = self._generate_keypair() # Generate a new key pair pubkeystr = self._get_publickey_str() # Get the public part as a string to send # Is the user supplied extras, add them if extras: user_info = {"password": password, "publickey": pubkeystr, "extra": json.dumps(extras)} else: user_info = {"password": password, "publickey": pubkeystr} self._logger.info("Sending payload: %s", user_info) # RSA encrypt it payload = transport_security.get_raw_encrypted(plaintext=json.dumps(user_info), pubkey=rsakey) # Send it in a JSON enc_payload = {"rsaencryptedpayload": payload} # Now send it to the Auth server # XXX: Change to https if server is using TLS url = "http://{}:{}/auth/signup/{}".format(self._authaddr, self._authport, self._userid) self._logger.info("Finalizing new id at: %s", url) try: resp = requests.put(url=url, json=enc_payload) except requests.exceptions.ConnectionError: self._logger.error("Error while connecting to Auth Server") return False except requests.exceptions.Timeout: self._logger.error("Timeout while connecting to Auth Server") return False # Check response status if resp.status_code != 200: self._logger.error("The Auth Server replied with an error: %s", resp.status_code) return False self._logger.info("Server responded positively") # Get response data, it should contain all the following payload = resp.json() if not all(k in payload for k in ["authpublickey", "agspublickey", "extras"]): self._logger.error("Server response is malformed") return False # Build the server keys from the response self._authpubkey = self._build_server_key(keystr=payload["authpublickey"]) self._agspubkey = self._build_server_key(keystr=payload["agspublickey"]) if not (self._authpubkey and self._agspubkey): self._logger.error("Server keys in response are malformed") return False # Store the server keys self._logger.info("Server keys successfully loaded") self._save_server_key(server_name="auth", pubkey=self._authpubkey) self._save_server_key(server_name="ags", pubkey=self._agspubkey) # Update our config to reflect our new id self._config["Client"]["UserID"] = self._userid self._config["Client"]["PrivateKeyPassword"] = key_pass self._save_config() # Save the confign to file self._logger.info("Updated configuration") # Update the password before saving our private key self._key_pass = key_pass self._save_keypair() # Save the generated key self._logger.info("Signup process complete. Server saved extras? %s", payload["extras"]) return True