Exemple #1
0
    def VerifySignature(message, signature, public_key):

        Crypto.SetupSignatureCurve()

        if type(public_key) is EllipticCurve.ECPoint:

            pubkey_x = public_key.x.value.to_bytes(32, 'big')
            pubkey_y = public_key.y.value.to_bytes(32, 'big')

            public_key = pubkey_x + pubkey_y

        m = message
        try:
            m = binascii.unhexlify(message)
        except Exception as e:
            print("could not get m")

        if len(public_key) == 33:

            public_key = bitcoin.decompress(public_key)
            public_key = public_key[1:]

        try:
            vk = VerifyingKey.from_string(public_key,
                                          curve=NIST256p,
                                          hashfunc=hashlib.sha256)
            res = vk.verify(signature, m, hashfunc=hashlib.sha256)
            return res
        except Exception as e:
            pass

        return False
Exemple #2
0
def derive_child_key(parent_key, parent_chain_code, index, key_type, hardened=False):
    if hardened:
        if key_type == KeyType.PUBLIC:
            exit_with_error("Can not derive a hardened public child key from parent public key")
        input_data = bytearray(b'\x00') + parent_key
    else:
        if key_type == KeyType.PUBLIC:
            input_data = bitcoin.compress(parent_key)
        else:
            public_key = bitcoin.privkey_to_pubkey(parent_key)
            input_data = bitcoin.compress(public_key)

    input_data += int(index).to_bytes(4, byteorder='big')
    key = parent_chain_code
    (key_offset, chain_code) = hmac_digest_and_split(key, input_data)
    if key_type == KeyType.PUBLIC:
        point = bitcoin.decompress(bitcoin.privkey_to_pubkey(key_offset))
        parent_point = bitcoin.decompress(parent_key)
        return bitcoin.add_pubkeys(point, parent_point), chain_code
    else:
        key = (int.from_bytes(parent_key, byteorder='big', signed=False) + int.from_bytes(key_offset, byteorder='big',
                                                                                          signed=False)) % bitcoin.N
        return key.to_bytes(32, byteorder='big', signed=False), chain_code
Exemple #3
0
def get_public_key_from_hex(hex):
    """
    Get public key from hexadecimal representation.

    Args:
        hex (str): The hexadecimal string.

    Returns:
        ecdsa.VerifyingKey: The primary key.

    """
    return ecdsa.VerifyingKey.from_string(bitcoin.decompress(
        binascii.unhexlify(hex))[1:],
                                          curve=ecdsa.SECP256k1)
Exemple #4
0
def extract_bin_ecdsa_pubkey(public_key):
    key_charencoding, key_type = get_public_key_format(public_key)

    if key_charencoding == CharEncoding.hex:
        bin_public_key = unhexlify(public_key)
    elif key_charencoding == CharEncoding.bin:
        bin_public_key = public_key
    else:
        raise ValueError(_errors['IMPROPER_PUBLIC_KEY_FORMAT'])

    if key_type == PubkeyType.ecdsa:
        return bin_public_key
    elif key_type == PubkeyType.uncompressed:
        return bin_public_key[1:]
    elif key_type == PubkeyType.compressed:
        return decompress(bin_public_key)[1:]
    else:
        raise ValueError(_errors['IMPROPER_PUBLIC_KEY_FORMAT'])
Exemple #5
0
def extract_bin_ecdsa_pubkey(public_key):
    key_charencoding, key_type = get_public_key_format(public_key)

    if key_charencoding == CharEncoding.hex:
        bin_public_key = unhexlify(public_key)
    elif key_charencoding == CharEncoding.bin:
        bin_public_key = public_key
    else:
        raise ValueError(_errors['IMPROPER_PUBLIC_KEY_FORMAT'])

    if key_type == PubkeyType.ecdsa:
        return bin_public_key
    elif key_type == PubkeyType.uncompressed:
        return bin_public_key[1:]
    elif key_type == PubkeyType.compressed:
        return decompress(bin_public_key)[1:]
    else:
        raise ValueError(_errors['IMPROPER_PUBLIC_KEY_FORMAT'])
Exemple #6
0
    def VerifySignature(message, signature, public_key, unhex=True):
        """
        Verify the integrity of the message.

        Args:
            message (hexstr or str): the message to verify.
            signature (bytearray): the signature belonging to the message.
            public_key (ECPoint|bytes): the public key to use for verifying the signature. If `public_key` is of type bytes then it should be raw bytes (i.e. b'\xAA\xBB').
            unhex (bool): whether the message should be unhexlified before verifying

        Returns:
            bool: True if verification passes. False otherwise.
        """

        if type(public_key) is EllipticCurve.ECPoint:
            pubkey_x = public_key.x.value.to_bytes(32, 'big')
            pubkey_y = public_key.y.value.to_bytes(32, 'big')

            public_key = pubkey_x + pubkey_y

        if unhex:
            try:
                message = binascii.unhexlify(message)
            except binascii.Error:
                pass
        elif isinstance(message, str):
            message = message.encode('utf-8')

        if len(public_key) == 33:
            public_key = bitcoin.decompress(public_key)
            public_key = public_key[1:]

        try:
            vk = VerifyingKey.from_string(public_key, curve=NIST256p, hashfunc=hashlib.sha256)
            res = vk.verify(signature, message, hashfunc=hashlib.sha256)
            return res
        except Exception:
            pass

        return False
Exemple #7
0
    def VerifySignature(message, signature, public_key):
        """
        Verify the integrity of the message.

        Args:
            message (str): the message to verify.
            signature (bytearray): the signature belonging to the message.
            public_key (ECPoint|bytes): the public key to use for verifying the signature. If `public_key` is of type bytes then it should be raw bytes (i.e. b'\xAA\xBB').

        Returns:
            bool: True if verification passes. False otherwise.
        """
        Crypto.SetupSignatureCurve()

        if type(public_key) is EllipticCurve.ECPoint:
            pubkey_x = public_key.x.value.to_bytes(32, 'big')
            pubkey_y = public_key.y.value.to_bytes(32, 'big')

            public_key = pubkey_x + pubkey_y

        m = message
        try:
            m = binascii.unhexlify(message)
        except Exception as e:
            logger.error("could not get m: %s" % e)

        if len(public_key) == 33:
            public_key = bitcoin.decompress(public_key)
            public_key = public_key[1:]

        try:
            vk = VerifyingKey.from_string(public_key, curve=NIST256p, hashfunc=hashlib.sha256)
            res = vk.verify(signature, m, hashfunc=hashlib.sha256)
            return res
        except Exception as e:
            pass

        return False