예제 #1
0
파일: auth.py 프로젝트: ycdk/py-evm
    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
파일: auth.py 프로젝트: ycdk/py-evm
    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
파일: test_ecies.py 프로젝트: ycdk/py-evm
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))