def _derive_pubkey(self): # derive EC public key instance from self._key if self._key == None: return False membuff = BIO.MemoryBuffer() self._key.save_pub_key_bio(membuff) #(filename) self._pubkey = EC.load_pub_key_bio(membuff) #filename)
def _constructEcFromRawKey(self, rawPublicKey): assert(len(rawPublicKey) == 64) bytes1 = a2b_hex("3059301306072a8648ce3d020106082a8648ce3d03010703420004") asn1KeyBytes = bytes1 + rawPublicKey pemPubKeyBytes = PEMEncoder(asn1KeyBytes).getEncoded("PUBLIC KEY") return EC.load_pub_key_bio(BIO.MemoryBuffer(pemPubKeyBytes))
def _constructEcFromRawKey(self, rawPublicKey): assert (len(rawPublicKey) == 64) bytes1 = a2b_hex( "3059301306072a8648ce3d020106082a8648ce3d03010703420004") asn1KeyBytes = bytes1 + rawPublicKey pemPubKeyBytes = PEMEncoder(asn1KeyBytes).getEncoded("PUBLIC KEY") return EC.load_pub_key_bio(BIO.MemoryBuffer(pemPubKeyBytes))
def __init__(self, mai): """Initialize the structure with a MetaAppInstance. Arguments: * ``mai``: the MetaAppInstance whose public key is used to verify signatures """ self.mai = mai self._ecs = [] bio = BIO.MemoryBuffer(str(self.mai.pubkey_ec)) self._ecs.append(EC.load_pub_key_bio(bio))
def DSKE(connection, sid, participants, phase=None): """ Generate ECDH Keypair and perform Key Exchange with all other participants @param connection: Current CRYPTO connection @type connection: CRYPTOConnection instance @param sid: Current SessionID @type sid: Integer @param phase: Phase of the DSKE routine @type phase: integer in range (0,2) """ newlist = [] # Create public/private keypairs and add them to keytable # Send keys out to participants and wait for responses if phase == 0: connection.keypair = EC_GenerateKey(744) connection.private_pem = EC_Private(connection.keypair) connection.public_pem = EC_Public(connection.keypair) z = EC.load_pub_key_bio(BIO.MemoryBuffer(connection.public_pem)) public_bin = ec_to_public_bin(connection.keypair) connection.keytable.update({broadcast.getPrefs():public_bin.encode("HEX")}) broadcast.Broadcast(participants, SendKey(public_bin.encode("HEX"))) return None # Once all public keys are received, sort them and take the hash # Broadcast to all users to confirm agreement elif phase == 1: pubkeys = [y for x,y in connection.keytable.items()] pubkeys.sort() pubkeys = "".join(pubkeys) hash_object = hashlib.sha256(pubkeys) hex_dig = hash_object.hexdigest() connection.keyhash = hex_dig connection.associationtable.update({broadcast.getPrefs():connection.keyhash}) broadcast.Broadcast(connection.users, SendAssociation(hex_dig)) return None # else: i = 0 hashes = [y for x,y in connection.associationtable.items()] for y in hashes: if not y == connection.keyhash: i = i+1 if i == 0: return 1
try: if type(ciphertext) == str: j = serializer.loads(ciphertext) else: j = ciphertext if j['type'] != 'EC_Encrypted': raise Exception("Input may not be the intending ciphertext.") publickey = j['public_key'] ciphertext= j['ciphertext'] except Exception,e: raise Exception("Bad EC ciphertext format.") try: # Read the temp. key. First write to a file. membuff = BIO.MemoryBuffer() membuff.write(publickey) tempkey = EC.load_pub_key_bio(membuff) # Combine this temp. key with our private key, and get Shared Secret. sharedsecret = self._key.compute_dh_key(tempkey) except Exception,e: raise Exception("Unable to load public key. Error is [%s]." % e) return decryptor(sharedsecret,ciphertext) def load_publickey(self,publickey): # Try parse the public key info. try: if type(publickey) == str: j = serializer.loads(publickey) else: j = publickey if j['type'] != 'EC_Public_Key': raise Exception("This is not a public key thus cannot be loaded.") if self._curves_id.has_key(j['curve']):
def ec_from_public_pem(pem): "Get the EC from a public PEM." return EC.load_pub_key_bio(BIO.MemoryBuffer(pem))
def PubKey_from_PEM(self, pemkey): m = BIO.MemoryBuffer(pemkey) self._key = EC.load_pub_key_bio(m)