Пример #1
0
    def derive_secrets(self,
                       initiator_nonce: bytes,
                       responder_nonce: bytes,
                       remote_ephemeral_pubkey: datatypes.PublicKey,
                       auth_init_ciphertext: bytes,
                       auth_ack_ciphertext: bytes
                       ) -> Tuple[bytes, bytes, sha3.keccak_256, sha3.keccak_256]:
        """Derive base secrets from ephemeral key agreement."""
        # ecdhe-shared-secret = ecdh.agree(ephemeral-privkey, remote-ephemeral-pubk)
        ecdhe_shared_secret = ecies.ecdh_agree(
            self.ephemeral_privkey, remote_ephemeral_pubkey)

        # shared-secret = sha3(ecdhe-shared-secret || sha3(nonce || initiator-nonce))
        shared_secret = keccak(
            ecdhe_shared_secret + keccak(responder_nonce + initiator_nonce))

        # aes-secret = sha3(ecdhe-shared-secret || shared-secret)
        aes_secret = keccak(ecdhe_shared_secret + shared_secret)

        # mac-secret = sha3(ecdhe-shared-secret || aes-secret)
        mac_secret = keccak(ecdhe_shared_secret + aes_secret)

        # setup sha3 instances for the MACs
        # egress-mac = sha3.update(mac-secret ^ recipient-nonce || auth-sent-init)
        mac1 = sha3.keccak_256(sxor(mac_secret, responder_nonce) + auth_init_ciphertext)
        # ingress-mac = sha3.update(mac-secret ^ initiator-nonce || auth-recvd-ack)
        mac2 = sha3.keccak_256(sxor(mac_secret, initiator_nonce) + auth_ack_ciphertext)

        if self._is_initiator:
            egress_mac, ingress_mac = mac1, mac2
        else:
            egress_mac, ingress_mac = mac2, mac1

        return aes_secret, mac_secret, egress_mac, ingress_mac
Пример #2
0
    def create_auth_message(self, nonce: bytes) -> bytes:
        ecdh_shared_secret = ecies.ecdh_agree(self.privkey, self.remote.pubkey)
        secret_xor_nonce = sxor(ecdh_shared_secret, nonce)

        # S(ephemeral-privk, ecdh-shared-secret ^ nonce)
        S = self.ephemeral_privkey.sign_msg_hash(secret_xor_nonce).to_bytes()

        # S || H(ephemeral-pubk) || pubk || nonce || 0x0
        return (S + keccak(self.ephemeral_pubkey.to_bytes()) +
                self.pubkey.to_bytes() + nonce + b'\x00')
Пример #3
0
    def decode_authentication(self, ciphertext: bytes) -> Tuple[datatypes.PublicKey, bytes]:
        """Decrypts and decodes the auth_init message.

        Returns the initiator's ephemeral pubkey and nonce.
        """
        if len(ciphertext) < ENCRYPTED_AUTH_MSG_LEN:
            raise ValueError("Auth msg too short: {}".format(len(ciphertext)))
        elif len(ciphertext) == ENCRYPTED_AUTH_MSG_LEN:
            sig, initiator_pubkey, initiator_nonce, version = decode_auth_plain(
                ciphertext, self.privkey)
        else:
            sig, initiator_pubkey, initiator_nonce, version = decode_auth_eip8(
                ciphertext, self.privkey)
            self.got_eip8_auth = True

        # recover initiator ephemeral pubkey from sig
        #     S(ephemeral-privk, ecdh-shared-secret ^ nonce)
        shared_secret = ecies.ecdh_agree(self.privkey, initiator_pubkey)

        ephem_pubkey = sig.recover_public_key_from_msg_hash(
            sxor(shared_secret, initiator_nonce))

        return ephem_pubkey, initiator_nonce
Пример #4
0
def test_ecdh(privkey_hex, pubkey_hex, ecdh_expected):
    privkey = keys.PrivateKey(decode_hex(privkey_hex))
    pubkey = keys.PublicKey(decode_hex(pubkey_hex))
    assert ecdh_expected == encode_hex(ecies.ecdh_agree(privkey, pubkey))