Ejemplo n.º 1
0
def pk_to_curve25519(ed: ed25519.Ed25519PublicKey) -> x25519.X25519PublicKey:
    raw = ed.public_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PublicFormat.Raw,
    )

    # This is libsodium's crypto_sign_ed25519_pk_to_curve25519 translated into
    # the Pyton module ge25519.

    from ge25519 import ge25519, ge25519_p3
    from fe25519 import fe25519

    if ge25519.has_small_order(raw) != 0:
        raise RuntimeError("Doesn' thave small order")

    # frombytes in libsodium appears to be the same as
    # frombytes_negate_vartime; as ge25519 only implements the from_bytes
    # version, we have to do the root check manually.
    A = ge25519_p3.from_bytes(raw)
    if A.root_check:
        raise RuntimeError("Root check failed")

    if not A.is_on_main_subgroup():
        raise RuntimeError("It's on the main subgroup")

    one_minus_y = fe25519.one() - A.Y
    x = A.Y + fe25519.one()
    x = x * one_minus_y.invert()

    return x25519.X25519PublicKey.from_public_bytes(bytes(x.to_bytes()))
Ejemplo n.º 2
0
 def encode_public(
     self, public_key: ed25519.Ed25519PublicKey, f_pub: _FragList
 ) -> None:
     """Write Ed25519 public key"""
     raw_public_key = public_key.public_bytes(
         Encoding.Raw, PublicFormat.Raw
     )
     f_pub.put_sshstr(raw_public_key)
Ejemplo n.º 3
0
def pk_to_curve25519(ed: ed25519.Ed25519PublicKey) -> x25519.X25519PublicKey:
    raw = ed.public_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PublicFormat.Raw,
    )

    ed_nacl = nacl.signing.VerifyKey(key=raw, )

    x_nacl = ed_nacl.to_curve25519_public_key()

    return x25519.X25519PublicKey.from_public_bytes(x_nacl.encode())
Ejemplo n.º 4
0
    def public_key_to_bytes(public_key: Ed25519PublicKey) -> bytes:
        """Returns the raw hex encoded bytes of a Ed25519PublicKey.

        Args:
            public_key (Ed25519PublicKey): Public key to encode.

        Returns:
            bytes: Byte encoding.
        """
        return public_key.public_bytes(serialization.Encoding.Raw,
                                       serialization.PublicFormat.Raw)
Ejemplo n.º 5
0
def public_key_bytes(public_key: Ed25519PublicKey) -> bytes:
    """convert cryptography.hazmat.primitives.asymmetric.ed25519.Ed25519PublicKey into raw bytes"""

    return public_key.public_bytes(encoding=serialization.Encoding.Raw,
                                   format=serialization.PublicFormat.Raw)