Exemplo n.º 1
0
def test_ecdsa_raw_sign():
    msg = "aa" * 31
    with pytest.raises(Exception) as e_info:
        btc.ecdsa_raw_sign(msg, None, None, rawmsg=True)
    assert e_info.match("Invalid hash input")
    #build non-raw priv object as input
    privraw = "aa" * 32
    msghash = b"\xbb" * 32
    sig = binascii.hexlify(
        btc.ecdsa_raw_sign(msghash, privraw, False, rawpriv=False,
                           rawmsg=True)).decode('ascii')
    assert sig == "3045022100b81960b4969b423199dea555f562a66b7f49dea5836a0168361f1a5f8a3c8298022003eea7d7ee4462e3e9d6d59220f950564caeb77f7b1cdb42af3c83b013ff3b2f"
def test_valid_sigs(setup_ecc):
    for v in vectors['vectors']:
        msg = v['msg']
        sig = v['sig']
        priv = v['privkey']
        assert sig == btc.ecdsa_raw_sign(msg, priv, True, rawmsg=True) + '01'
        #check that the signature verifies against the key(pair)
        pubkey = btc.privtopub(priv)
        assert btc.ecdsa_raw_verify(msg, pubkey, sig[:-2], True, rawmsg=True)
        #check that it fails to verify against corrupted signatures
        for i in [0, 1, 2, 4, 7, 25, 55]:
            #corrupt one byte
            binsig = binascii.unhexlify(sig)
            checksig = binascii.hexlify(binsig[:i] +
                                        chr((ord(binsig[i]) + 1) % 256) +
                                        binsig[i + 1:-1])

            #this kind of corruption will sometimes lead to an assert
            #failure (if the DER format is corrupted) and sometimes lead
            #to a signature verification failure.
            try:
                res = btc.ecdsa_raw_verify(msg,
                                           pubkey,
                                           checksig,
                                           True,
                                           rawmsg=True)
            except:
                continue
            assert res == False
def test_valid_sigs(setup_ecc):
    for v in vectors['vectors']:
        msg, sig, priv = (binascii.unhexlify(v[a])
                          for a in ["msg", "sig", "privkey"])
        assert sig == btc.ecdsa_raw_sign(msg, priv, rawmsg=True) + b'\x01'
        # check that the signature verifies against the key(pair)
        pubkey = btc.privkey_to_pubkey(priv)
        assert btc.ecdsa_raw_verify(msg, pubkey, sig[:-1], rawmsg=True)
        # check that it fails to verify against corrupted signatures
        for i in [0, 1, 2, 4, 7, 25, 55]:
            # corrupt one byte
            checksig = sig[:i] + chr(
                (ord(sig[i:i + 1]) + 1) % 256).encode() + sig[i + 1:-1]

            # this kind of corruption will sometimes lead to an assert
            # failure (if the DER format is corrupted) and sometimes lead
            # to a signature verification failure.
            try:
                res = btc.ecdsa_raw_verify(msg, pubkey, checksig, rawmsg=True)
            except:
                continue
            assert res == False