Пример #1
0
 def encode_public(self, public_key: ec.EllipticCurvePublicKey,
                   f_pub: _FragList) -> None:
     """Write ECDSA public key"""
     point = public_key.public_bytes(Encoding.X962,
                                     PublicFormat.UncompressedPoint)
     f_pub.put_sshstr(self.ssh_curve_name)
     f_pub.put_sshstr(point)
Пример #2
0
 def calculate_secret(self, slot: SLOT,
                      peer_public_key: ec.EllipticCurvePublicKey) -> bytes:
     key_type = KEY_TYPE.from_public_key(peer_public_key)
     if key_type.algorithm != ALGORITHM.EC:
         raise ValueError("Unsupported key type")
     data = peer_public_key.public_bytes(Encoding.X962,
                                         PublicFormat.UncompressedPoint)
     return self._use_private_key(slot, key_type, data, True)
Пример #3
0
def get_public_key_bytes_compressed(
        public_key: ec.EllipticCurvePublicKey) -> bytes:
    """ Returns the bytes from a cryptography ec.EllipticCurvePublicKey in a compressed format

        :param public_key: Public key object
        :type public_key: ec.EllipticCurvePublicKey

        :rtype: bytes
    """
    return public_key.public_bytes(Encoding.X962, PublicFormat.CompressedPoint)
Пример #4
0
    def derive_ecdh(self, public_key: ec.EllipticCurvePublicKey) -> bytes:
        """Perform an ECDH key exchange as specified in SP 800-56A.

        :param public_key: The public key to use for the key exchange.
        :return: The resulting shared key.
        """
        point = public_key.public_bytes(Encoding.X962,
                                        PublicFormat.UncompressedPoint)
        msg = struct.pack("!H", self.id) + point
        return self.session.send_secure_cmd(COMMAND.DERIVE_ECDH, msg)
Пример #5
0
def generate_public_pem(public_key: ec.EllipticCurvePublicKey) -> bytes:
    """
    Generates a Privacy Enhanced Mail (pem) from the public key
    This may be send to the other party

    :param public_key:  ec.EllipticCurvePublicKey
    :return: Privacy Enhanced Mail message
    :rtype: bytes
    """
    __assure_public_key(public_key)
    pem = public_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo)
    return pem
Пример #6
0
def get_public_key_bytes_compressed(
        public_key: ec.EllipticCurvePublicKey) -> bytes:
    """Return the bytes of a pubkey in the compressed format."""
    return public_key.public_bytes(Encoding.X962, PublicFormat.CompressedPoint)