Ejemplo n.º 1
0
def test_session():
    initiator = RLPxSession(ECCx(raw_privkey=mk_privkey('secret1')), is_initiator=True)
    initiator_pubk = initiator.ecc.raw_pubkey
    responder = RLPxSession(ECCx(raw_privkey=mk_privkey('secret2')))
    responder_pubk = responder.ecc.raw_pubkey

    auth_msg = initiator.create_auth_message(remote_pubkey=responder_pubk)
    auth_msg_ct = initiator.encrypt_auth_message(auth_msg, responder_pubk)

    responder.decode_authentication(auth_msg_ct)
    auth_ack_msg = responder.create_auth_ack_message()
    auth_ack_msg_ct = responder.encrypt_auth_ack_message(auth_ack_msg, initiator_pubk)

    initiator.decode_auth_ack_message(auth_ack_msg_ct)

    initiator.setup_cipher()
    responder.setup_cipher()

    assert responder.ecdhe_shared_secret == initiator.ecdhe_shared_secret
    assert responder.token == initiator.token
    assert responder.aes_secret == initiator.aes_secret
    assert responder.mac_secret == initiator.mac_secret

    assert responder.egress_mac.digest() == initiator.ingress_mac.digest()
    assert responder.egress_mac.digest() == initiator.ingress_mac.digest()
    assert responder.ingress_mac.digest() == initiator.egress_mac.digest()
    assert responder.ingress_mac.digest() == initiator.egress_mac.digest()
    assert responder.mac_secret == initiator.mac_secret

    return initiator, responder
Ejemplo n.º 2
0
def test_session():
    initiator = RLPxSession(ECCx(raw_privkey=mk_privkey('secret1')),
                            is_initiator=True)
    initiator_pubk = initiator.ecc.raw_pubkey
    responder = RLPxSession(ECCx(raw_privkey=mk_privkey('secret2')))
    responder_pubk = responder.ecc.raw_pubkey

    auth_msg = initiator.create_auth_message(remote_pubkey=responder_pubk)
    auth_msg_ct = initiator.encrypt_auth_message(auth_msg, responder_pubk)

    responder.decode_authentication(auth_msg_ct)
    auth_ack_msg = responder.create_auth_ack_message()
    auth_ack_msg_ct = responder.encrypt_auth_ack_message(
        auth_ack_msg, initiator_pubk)

    initiator.decode_auth_ack_message(auth_ack_msg_ct)

    initiator.setup_cipher()
    responder.setup_cipher()

    assert responder.ecdhe_shared_secret == initiator.ecdhe_shared_secret
    assert responder.token == initiator.token
    assert responder.aes_secret == initiator.aes_secret
    assert responder.mac_secret == initiator.mac_secret

    assert responder.egress_mac.digest() == initiator.ingress_mac.digest()
    assert responder.egress_mac.digest() == initiator.ingress_mac.digest()
    assert responder.ingress_mac.digest() == initiator.egress_mac.digest()
    assert responder.ingress_mac.digest() == initiator.egress_mac.digest()
    assert responder.mac_secret == initiator.mac_secret

    return initiator, responder
Ejemplo n.º 3
0
def test_go_sig():
    """
    go client started with:

    ethereum -port="40404" -loglevel=5  -nodekeyhex="9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658" -bootnodes="enode://2da47499d52d9161a778e4c711e22e8651cb90350ec066452f9516d1d11eb465d1ec42bb27ec6cd4488b8b6a1a411cb5ef83c16cbb8bee194624bb65fef0f7fd@127.0.0.1:30303"
    """

    r_pubkey = "ab16b8c7fc1febb74ceedf1349944ffd4a04d11802451d02e808f08cb3b0c1c1a9c4e1efb7d309a762baa4c9c8da08890b3b712d1666b5b630d6c6a09cbba171".decode(
        'hex')
    d = {
        'signed_data':
        'a061e5b799b5bb3a3a68a7eab6ee11207d90672e796510ac455e985bd206e240',
        'cmd':
        'find_node',
        'body':
        '03f847b840ab16b8c7fc1febb74ceedf1349944ffd4a04d11802451d02e808f08cb3b0c1c1a9c4e1efb7d309a762baa4c9c8da08890b3b712d1666b5b630d6c6a09cbba1718454e869b1',
        'signature':
        '0de032c62e30f4a9f9f07f25ac5377c5a531116147617a6c08f946c97991f351577e53ae138210bdb7447bab53f3398d746d42c64a9ce67a6248e59353f1bc6e01'
    }

    priv_seed = 'test'
    priv_key = mk_privkey(priv_seed)
    assert priv_key == "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658".decode(
        'hex')
    my_pubkey = privtopub(priv_key)
    assert my_pubkey == r_pubkey, (my_pubkey, r_pubkey)
    go_body = d['body'].decode('hex')  # cmd_id, rlp.encoded
    import rlp
    target_node_id, expiry = rlp.decode(go_body[1:])
    assert target_node_id == r_pubkey  # lookup for itself
    go_signed_data = d['signed_data'].decode('hex')
    go_signature = d['signature'].decode('hex')

    b_signature = bitcoin.ecdsa_sign(go_signed_data,
                                     priv_key)  # base64 encoded!

    # https://github.com/vbuterin/pybitcointools/blob/master/bitcoin/main.py#L500
    my_signature = ecdsa_sign(go_signed_data, priv_key)
    assert my_signature == ecdsa_sign(go_signed_data,
                                      priv_key)  # deterministic k

    assert len(go_signed_data) == 32  # sha3()
    assert len(go_signature) == 65
    assert len(my_signature) == 65  # length is okay

    try:
        assert my_signature == go_signature
        failed = False
    except:
        "expected fail, go signatures are not generated with deterministic k"
        failed = True
        pass
    assert failed

    # decoding works when we signed it
    assert my_pubkey == ecdsa_recover(go_signed_data, my_signature)

    # problem we can not decode the pubkey from the go signature
    # and go can not decode ours
    ecdsa_recover(go_signed_data, go_signature)
Ejemplo n.º 4
0
def test_pyelliptic_sig():
    priv_seed = 'test'
    priv_key = mk_privkey(priv_seed)
    my_pubkey = privtopub(priv_key)
    e = ECCx(my_pubkey, priv_key)
    msg = 'a'
    s = pyelliptic.ECC.sign(e, msg)
    assert s == pyelliptic.ECC.sign(e, msg)  # deterministic
Ejemplo n.º 5
0
def test_pyelliptic_sig():
    priv_seed = 'test'
    priv_key = mk_privkey(priv_seed)
    my_pubkey = privtopub(priv_key)
    e = ECCx(raw_privkey=priv_key)
    msg = 'a'
    s = pyelliptic.ECC.sign(e, msg)
    s2 = pyelliptic.ECC.sign(e, msg)
    assert s != s2  # non deterministic
Ejemplo n.º 6
0
def test_go_sig():
    """
    go client started with:

    ethereum -port="40404" -loglevel=5  -nodekeyhex="9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658" -bootnodes="enode://2da47499d52d9161a778e4c711e22e8651cb90350ec066452f9516d1d11eb465d1ec42bb27ec6cd4488b8b6a1a411cb5ef83c16cbb8bee194624bb65fef0f7fd@127.0.0.1:30303"
    """

    r_pubkey = "ab16b8c7fc1febb74ceedf1349944ffd4a04d11802451d02e808f08cb3b0c1c1a9c4e1efb7d309a762baa4c9c8da08890b3b712d1666b5b630d6c6a09cbba171".decode(
        'hex')
    d = {'signed_data': 'a061e5b799b5bb3a3a68a7eab6ee11207d90672e796510ac455e985bd206e240',
         'cmd': 'find_node',
         'body': '03f847b840ab16b8c7fc1febb74ceedf1349944ffd4a04d11802451d02e808f08cb3b0c1c1a9c4e1efb7d309a762baa4c9c8da08890b3b712d1666b5b630d6c6a09cbba1718454e869b1',
         'signature': '0de032c62e30f4a9f9f07f25ac5377c5a531116147617a6c08f946c97991f351577e53ae138210bdb7447bab53f3398d746d42c64a9ce67a6248e59353f1bc6e01'}

    priv_seed = 'test'
    priv_key = mk_privkey(priv_seed)
    assert priv_key == "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658".decode(
        'hex')
    my_pubkey = privtopub(priv_key)
    assert my_pubkey == r_pubkey, (my_pubkey, r_pubkey)
    go_body = d['body'].decode('hex')  # cmd_id, rlp.encoded
    import rlp
    target_node_id, expiry = rlp.decode(go_body[1:])
    assert target_node_id == r_pubkey  # lookup for itself
    go_signed_data = d['signed_data'].decode('hex')
    go_signature = d['signature'].decode('hex')

    my_signature = ecdsa_sign(go_signed_data, priv_key)
    assert my_signature == ecdsa_sign(go_signed_data, priv_key)  # deterministic k

    assert len(go_signed_data) == 32  # sha3()
    assert len(go_signature) == 65
    assert len(my_signature) == 65  # length is okay

    try:
        assert my_signature == go_signature
        failed = False
    except:
        "expected fail, go signatures are not generated with deterministic k"
        failed = True
        pass
    assert failed

    # decoding works when we signed it
    assert my_pubkey == ecdsa_recover(go_signed_data, my_signature)

    # problem we can not decode the pubkey from the go signature
    # and go can not decode ours
    ecdsa_recover(go_signed_data, go_signature)
Ejemplo n.º 7
0
def get_ecc(secret=b''):
    return crypto.ECCx(raw_privkey=crypto.mk_privkey(secret))
Ejemplo n.º 8
0
def test_privtopub():
    priv = crypto.mk_privkey(b'test')
    pub = crypto.privtopub(priv)
    pub2 = crypto.ECCx(raw_privkey=priv).raw_pubkey
    assert pub == pub2
Ejemplo n.º 9
0
def test_session():

    proto = P2PProtocol(peer=PeerMock(), service=WiredService(BaseApp()))
    hello_packet = proto.create_hello()

    responder_privkey = mk_privkey('secret1')
    responder = MultiplexedSession(responder_privkey, hello_packet=hello_packet,
                                   token_by_pubkey=dict())
    p0 = 0
    responder.add_protocol(p0)

    initiator_privkey = mk_privkey('secret2')
    initiator = MultiplexedSession(initiator_privkey, hello_packet=hello_packet,
                                   remote_pubkey=privtopub(responder_privkey))
    initiator.add_protocol(p0)

    # send auth
    msg = initiator.message_queue.get_nowait()
    assert msg  # auth_init
    assert len(msg) == RLPxSession.auth_message_ct_length
    assert initiator.packet_queue.empty()
    assert not responder.is_initiator

    # receive auth
    responder.add_message(msg)
    assert responder.packet_queue.empty()
    assert responder.is_ready

    # send auth ack and hello
    ack_msg = responder.message_queue.get_nowait()
    assert len(msg) >= RLPxSession.auth_ack_message_ct_length  # + hello
    hello_msg = responder.message_queue.get_nowait()
    assert hello_msg

    # receive auth ack & hello
    initiator.add_message(ack_msg + hello_msg)
    assert initiator.is_ready
    hello_packet = initiator.packet_queue.get_nowait()
    assert isinstance(hello_packet, Packet)

    # initiator sends hello
    hello_msg = initiator.message_queue.get_nowait()
    assert hello_msg

    # hello received by responder
    responder.add_message(hello_msg)
    hello_packet = responder.packet_queue.get_nowait()
    assert isinstance(hello_packet, Packet)

    # assert we received an actual hello packet
    data = proto.hello.decode_payload(hello_packet.payload)
    assert data['version']

    # test normal operation
    ping = proto.create_ping()
    initiator.add_packet(ping)
    msg = initiator.message_queue.get_nowait()

    # receive ping
    responder.add_message(msg)
    ping_packet = responder.packet_queue.get_nowait()
    assert isinstance(ping_packet, Packet)
    data = proto.ping.decode_payload(ping_packet.payload)

    # reply with pong
    pong = proto.create_ping()
    responder.add_packet(pong)
    msg = responder.message_queue.get_nowait()

    # receive pong
    initiator.add_message(msg)
    pong_packet = initiator.packet_queue.get_nowait()
    assert isinstance(pong_packet, Packet)
    data = proto.pong.decode_payload(pong_packet.payload)
Ejemplo n.º 10
0
def test_session():

    proto = P2PProtocol(peer=PeerMock(), service=WiredService(BaseApp()))
    hello_packet = proto.create_hello()

    responder_privkey = mk_privkey('secret1')
    responder = MultiplexedSession(responder_privkey, hello_packet=hello_packet)
    p0 = 0
    responder.add_protocol(p0)

    initiator_privkey = mk_privkey('secret2')
    initiator = MultiplexedSession(initiator_privkey, hello_packet=hello_packet,
                                   remote_pubkey=privtopub(responder_privkey))
    initiator.add_protocol(p0)

    # send auth
    msg = initiator.message_queue.get_nowait()
    assert msg  # auth_init
    assert initiator.packet_queue.empty()
    assert not responder.is_initiator

    # receive auth
    responder.add_message(msg)
    assert responder.packet_queue.empty()
    assert responder.is_ready

    # send auth ack and hello
    ack_msg = responder.message_queue.get_nowait()
    hello_msg = responder.message_queue.get_nowait()
    assert hello_msg

    # receive auth ack & hello
    initiator.add_message(ack_msg + hello_msg)
    assert initiator.is_ready
    hello_packet = initiator.packet_queue.get_nowait()
    assert isinstance(hello_packet, Packet)

    # initiator sends hello
    hello_msg = initiator.message_queue.get_nowait()
    assert hello_msg

    # hello received by responder
    responder.add_message(hello_msg)
    hello_packet = responder.packet_queue.get_nowait()
    assert isinstance(hello_packet, Packet)

    # assert we received an actual hello packet
    data = proto.hello.decode_payload(hello_packet.payload)
    assert data['version']

    # test normal operation
    ping = proto.create_ping()
    initiator.add_packet(ping)
    msg = initiator.message_queue.get_nowait()

    # receive ping
    responder.add_message(msg)
    ping_packet = responder.packet_queue.get_nowait()
    assert isinstance(ping_packet, Packet)
    data = proto.ping.decode_payload(ping_packet.payload)

    # reply with pong
    pong = proto.create_ping()
    responder.add_packet(pong)
    msg = responder.message_queue.get_nowait()

    # receive pong
    initiator.add_message(msg)
    pong_packet = initiator.packet_queue.get_nowait()
    assert isinstance(pong_packet, Packet)
    data = proto.pong.decode_payload(pong_packet.payload)
Ejemplo n.º 11
0
def get_ecc(secret=''):
    return crypto.ECCx.from_privkey(crypto.mk_privkey(secret))
Ejemplo n.º 12
0
def test_privtopub():
    priv = crypto.mk_privkey('test')
    pub = crypto.privtopub(priv)
    pub2 = crypto.ECCx(raw_privkey=priv).raw_pubkey
    assert pub == pub2
Ejemplo n.º 13
0
def get_ecc(secret=''):
    return crypto.ECCx(raw_privkey=crypto.mk_privkey(secret))