def keyexchange(n, ip, publickey_list, secretkey_list, extra_list): exchangeKey = [] for i in range(n): if i == ip: exchangeKey.append(0) else: if i > ip: comKeyint, _ = xc.crypto_kx_client_session_keys(publickey_list[i], secretkey_list[i], extra_list[i]) else: _, comKeyint = xc.crypto_kx_server_session_keys(publickey_list[i], secretkey_list[i], extra_list[i]) exchangekey = int.from_bytes(xc.crypto_hash_sha256(comKeyint), byteorder='big') exchangeKey.append(exchangekey) return exchangeKey
def keyexchange(n, party_i, my_pkey_list, my_skey_list, other_pkey_list): common_key_list = [] for i in range(n): #Generate DH (common) keys if i == party_i: common_key_list.append(0) else: if i > party_i: common_key_raw, _ = nb.crypto_kx_client_session_keys( my_pkey_list[i], my_skey_list[i], other_pkey_list[i]) else: _, common_key_raw = nb.crypto_kx_server_session_keys( my_pkey_list[i], my_skey_list[i], other_pkey_list[i]) #Hash the common keys common_key = int.from_bytes(nb.crypto_hash_sha256(common_key_raw), byteorder='big') common_key_list.append(common_key) return common_key_list
def test_hash(): msg = b"message" h1 = c.crypto_hash(msg) assert len(h1) == c.crypto_hash_BYTES assert tohex(h1) == ("f8daf57a3347cc4d6b9d575b31fe6077" "e2cb487f60a96233c08cb479dbf31538" "cc915ec6d48bdbaa96ddc1a16db4f4f9" "6f37276cfcb3510b8246241770d5952c") assert tohex(h1) == hashlib.sha512(msg).hexdigest() h2 = c.crypto_hash_sha512(msg) assert len(h2) == c.crypto_hash_sha512_BYTES assert tohex(h2) == tohex(h1) h3 = c.crypto_hash_sha256(msg) assert len(h3) == c.crypto_hash_sha256_BYTES assert tohex(h3) == ("ab530a13e45914982b79f9b7e3fba994" "cfd1f3fb22f71cea1afbf02b460c6d1d") assert tohex(h3) == hashlib.sha256(msg).hexdigest()
def dict_keyexchange(peer_list, self_id, my_pkeys, my_skeys, peer_pkeys): # CDB: The last three parameters are now all dictionaries. Dictionary keys # are peer ids to which we gave the key, or from which we received the key. # comkeys is also now a dictionary keyed by peer id. comkeys = {} for peer_id in peer_list: if peer_id > self_id: common_key_raw, _ = nb.crypto_kx_client_session_keys( my_pkeys[peer_id], my_skeys[peer_id], peer_pkeys[peer_id]) else: _, common_key_raw = nb.crypto_kx_server_session_keys( my_pkeys[peer_id], my_skeys[peer_id], peer_pkeys[peer_id]) # Hash the common keys. comkeys[peer_id] = int.from_bytes( nb.crypto_hash_sha256(common_key_raw), byteorder='big') return comkeys