def parse_pubkey(blob):
    """
    Parse SSH public key from given blob.

    Construct a verifier for ECDSA signatures.
    The verifier returns the signatures in the required SSH format.
    Currently, NIST256P1 and ED25519 elliptic curves are supported.
    """
    fp = fingerprint(blob)
    s = io.BytesIO(blob)
    key_type = util.read_frame(s)
    log.debug('key type: %s', key_type)
    assert key_type in SUPPORTED_KEY_TYPES, key_type

    result = {'blob': blob, 'type': key_type, 'fingerprint': fp}

    if key_type == SSH_NIST256_KEY_TYPE:
        curve_name = util.read_frame(s)
        log.debug('curve name: %s', curve_name)
        point = util.read_frame(s)
        assert s.read() == b''
        _type, point = point[:1], point[1:]
        assert _type == SSH_NIST256_DER_OCTET
        size = len(point) // 2
        assert len(point) == 2 * size
        coords = (util.bytes2num(point[:size]), util.bytes2num(point[size:]))

        curve = ecdsa.NIST256p
        point = ecdsa.ellipticcurve.Point(curve.curve, *coords)

        def ecdsa_verifier(sig, msg):
            assert len(sig) == 2 * size
            sig_decode = ecdsa.util.sigdecode_string
            vk = ecdsa.VerifyingKey.from_public_point(point, curve, hashfunc)
            vk.verify(signature=sig, data=msg, sigdecode=sig_decode)
            parts = [sig[:size], sig[size:]]
            return b''.join([util.frame(b'\x00' + p) for p in parts])

        result.update(point=coords, curve=CURVE_NIST256,
                      verifier=ecdsa_verifier)

    if key_type == SSH_ED25519_KEY_TYPE:
        pubkey = util.read_frame(s)
        assert s.read() == b''

        def ed25519_verify(sig, msg):
            assert len(sig) == 64
            vk = ed25519.VerifyingKey(pubkey)
            vk.verify(sig, msg)
            return sig

        result.update(curve=CURVE_ED25519, verifier=ed25519_verify)

    return result
def parse_pubkey ( blob ) : fp = fingerprint ( blob ) s = io . BytesIO ( blob ) key_type = util . read_frame ( s ) log . debug ( 'key type: %s' , key_type ) assert key_type in SUPPORTED_KEY_TYPES , key_type result = { 'blob' : blob , 'type' : key_type , 'fingerprint' : fp } if key_type == SSH_NIST256_KEY_TYPE : curve_name = util . read_frame ( s ) log . debug ( 'curve name: %s' , curve_name ) point = util . read_frame ( s ) assert s . read ( ) == b'' _type , point = point [ : 1 ] , point [ 1 : ] assert _type == SSH_NIST256_DER_OCTET size = len ( point ) // 2 assert len ( point ) == 2 * size coords = ( util . bytes2num ( point [ : size ] ) , util . bytes2num ( point [ size : ] ) ) curve = ecdsa . NIST256p point = ecdsa . ellipticcurve . Point ( curve . curve , * coords ) def ecdsa_verifier ( sig , msg ) : assert len ( sig ) == 2 * size sig_decode = ecdsa . util . sigdecode_string vk = ecdsa . VerifyingKey . from_public_point ( point , curve , hashfunc ) vk . verify ( signature = sig , data = msg , sigdecode = sig_decode ) parts = [ sig [ : size ] , sig [ size : ] ] return b'' . join ( [ util . frame ( b'\x00' + p ) for p in parts ] ) result . update ( point = coords , curve = CURVE_NIST256 , verifier = ecdsa_verifier ) if key_type == SSH_ED25519_KEY_TYPE : pubkey = util . read_frame ( s ) assert s . read ( ) == b'' def ed25519_verify ( sig , msg ) : assert len ( sig ) == 64 vk = ed25519 . VerifyingKey ( pubkey ) vk . verify ( sig , msg ) return sig result . update ( curve = CURVE_ED25519 , verifier = ed25519_verify ) return result
Parse SSH public key from given blob.

    Construct a verifier for ECDSA signatures.
    The verifier returns the signatures in the required SSH format.
    Currently, NIST256P1 and ED25519 elliptic curves are supported.