예제 #1
0
    def encrypt(self, string):
        """
        Encrypts a non-empty string, using the key-dependent data in
        the object, and with the appropriate feedback mode.

        The string's length must be an exact multiple of the algorithm's
        block size or, in CFB mode, of the segment size.

        Returns a string containing the ciphertext.
        """
        string = t2b(string)

        if not string or len(string) % self.block_size:
            raise ValueError("string must be a multiple of %d in length" %
                             self.block_size)

        if self._enc is None:
            self._enc = _ffi.new(self._native_type)
            ret = self._set_key(_ENCRYPTION)
            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Invalid key error (%d)" % ret)

        result = _ffi.new("byte[%d]" % len(string))
        ret = self._encrypt(result, string)
        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Encryption error (%d)" % ret)

        return _ffi.buffer(result)[:]
예제 #2
0
    def decrypt(self, string):
        """
        Decrypts **string**, using the key-dependent data in the
        object and with the appropriate feedback mode.

        The string's length must be an exact multiple of the algorithm's
        block size or, in CFB mode, of the segment size.

        Returns a string containing the plaintext.
        """
        string = t2b(string)

        if not string or len(string) % self.block_size:
            raise ValueError(
                "string must be a multiple of %d in length" % self.block_size)

        if self._dec is None:
            self._dec = _ffi.new(self._native_type)
            ret = self._set_key(_DECRYPTION)
            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Invalid key error (%d)" % ret)

        result = _ffi.new("byte[%d]" % len(string))
        ret = self._decrypt(result, string)
        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Decryption error (%d)" % ret)

        return _ffi.buffer(result)[:]
예제 #3
0
    def decrypt(self, string):
        """
        Decrypts **string**, using the key-dependent data in the
        object and with the appropriate feedback mode.

        The string's length must be an exact multiple of the algorithm's
        block size or, in CFB mode, of the segment size.

        Returns a string containing the plaintext.
        """
        string = t2b(string)

        if not string:
            raise ValueError(
                    "empty string not allowed")

        if len(string) % self.block_size and not "ChaCha" in self._native_type:
            raise ValueError(
                "string must be a multiple of %d in length" % self.block_size)

        if self._dec is None:
            self._dec = _ffi.new(self._native_type)
            ret = self._set_key(_DECRYPTION)
            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Invalid key error (%d)" % ret)

        result = _ffi.new("byte[%d]" % len(string))
        ret = self._decrypt(result, string)
        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Decryption error (%d)" % ret)

        return _ffi.buffer(result)[:]
예제 #4
0
    def encode_key_raw(self):
        """
        Encodes the ECC public key in its two raw elements

        Returns (Qx, Qy)
        """
        Qx = _ffi.new("byte[%d]" % (self.size))
        Qy = _ffi.new("byte[%d]" % (self.size))
        qx_size = _ffi.new("word32[1]")
        qy_size = _ffi.new("word32[1]")
        qx_size[0] = self.size
        qy_size[0] = self.size

        ret = _lib.wc_ecc_export_public_raw(self.native_object, Qx, qx_size, Qy, qy_size);
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Key encode error (%d)" % ret)

        return _ffi.buffer(Qx, qx_size[0])[:], _ffi.buffer(Qy, qy_size[0])[:]
예제 #5
0
        def encode_key_raw(self):
            """
            Encodes the ECC public key in its two raw elements

            Returns (Qx, Qy)
            """
            Qx = _ffi.new("byte[%d]" % (self.size))
            Qy = _ffi.new("byte[%d]" % (self.size))
            qx_size = _ffi.new("word32[1]")
            qy_size = _ffi.new("word32[1]")
            qx_size[0] = self.size
            qy_size[0] = self.size

            ret = _lib.wc_ecc_export_public_raw(self.native_object, Qx,
                    qx_size, Qy, qy_size);
            if ret != 0:  # pragma: no cover
                raise WolfCryptError("Key encode error (%d)" % ret)

            return _ffi.buffer(Qx, qx_size[0])[:], _ffi.buffer(Qy,
                    qy_size[0])[:]
예제 #6
0
    def bytes(self, length):
        """
        Generate and return a random sequence of length bytes.
        """
        result = _ffi.new('byte[%d]' % length)

        ret = _lib.wc_RNG_GenerateBlock(self.native_object, result, length)
        if ret < 0:  # pragma: no cover
            raise WolfCryptError("RNG generate block error (%d)" % ret)

        return _ffi.buffer(result, length)[:]
예제 #7
0
    def encode_key(self):
        """
        Encodes the ED25519 private key.

        Returns the encoded key.
        """
        key = _ffi.new("byte[%d]" % (self.size * 4))
        pubkey = _ffi.new("byte[%d]" % (self.size * 4))
        size = _ffi.new("word32[1]")

        size[0] = _lib.wc_ed25519_priv_size(self.native_object)

        ret = _lib.wc_ed25519_export_private_only(self.native_object, key, size)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Private key encode error (%d)" % ret)
        ret = _lib.wc_ed25519_export_public(self.native_object, pubkey, size)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Public key encode error (%d)" % ret)

        return _ffi.buffer(key, size[0])[:], _ffi.buffer(pubkey, size[0])[:]
예제 #8
0
    def byte(self):
        """
        Generate and return a random byte.
        """
        result = _ffi.new('byte[1]')

        ret = _lib.wc_RNG_GenerateByte(self.native_object, result)
        if ret < 0:
            raise WolfCryptError("RNG generate byte error (%d)" % ret)

        return _ffi.buffer(result, 1)[:]
예제 #9
0
    def bytes(self, length):
        """
        Generate and return a random sequence of length bytes.
        """
        result = _ffi.new('byte[%d]' % length)

        ret = _lib.wc_RNG_GenerateBlock(self.native_object, result, length)
        if ret < 0:
            raise WolfCryptError("RNG generate block error (%d)" % ret)

        return _ffi.buffer(result, length)[:]
예제 #10
0
    def byte(self):
        """
        Generate and return a random byte.
        """
        result = _ffi.new('byte[1]')

        ret = _lib.wc_RNG_GenerateByte(self.native_object, result)
        if ret < 0:  # pragma: no cover
            raise WolfCryptError("RNG generate byte error (%d)" % ret)

        return _ffi.buffer(result, 1)[:]
예제 #11
0
            def encode_key(self):
                """
                Encodes the RSA private and public keys in an ASN sequence.

                Returns the encoded key.
                """
                priv = _ffi.new("byte[%d]" % (self.size * 4))
                pub = _ffi.new("byte[%d]" % (self.size * 4))

                ret = _lib.wc_RsaKeyToDer(self.native_object, priv, self.size)
                if ret <= 0:  # pragma: no cover
                    raise WolfCryptError("Private RSA key error (%d)" % ret)
                privlen = ret
                ret = _lib.wc_RsaKeyToPublicDer(self.native_object, pub,
                                                self.size)
                if ret <= 0:  # pragma: no cover
                    raise WolfCryptError("Public RSA key encode error (%d)" %
                                         ret)
                publen = ret
                return _ffi.buffer(priv, privlen)[:], _ffi.buffer(pub,
                                                                  publen)[:]
예제 #12
0
        def encode_key(self):
            """
            Encodes the ED25519 private key.

            Returns the encoded key.
            """
            key = _ffi.new("byte[%d]" % (self.size * 4))
            pubkey = _ffi.new("byte[%d]" % (self.size * 4))
            size = _ffi.new("word32[1]")

            size[0] = _lib.wc_ed25519_priv_size(self.native_object)

            ret = _lib.wc_ed25519_export_private_only(self.native_object,
                    key, size)
            if ret != 0:  # pragma: no cover
                raise WolfCryptError("Private key encode error (%d)" % ret)
            ret = _lib.wc_ed25519_export_public(self.native_object, pubkey,
                    size)
            if ret != 0:  # pragma: no cover
                raise WolfCryptError("Public key encode error (%d)" % ret)

            return _ffi.buffer(key, size[0])[:], _ffi.buffer(pubkey, size[0])[:]
예제 #13
0
        def encode_key(self):
            """
            Encodes the ECC private key in an ASN sequence.

            Returns the encoded key.
            """
            key = _ffi.new("byte[%d]" % (self.size * 4))

            ret = _lib.wc_EccKeyToDer(self.native_object, key, len(key))
            if ret <= 0:  # pragma: no cover
                raise WolfCryptError("Key encode error (%d)" % ret)

            return _ffi.buffer(key, ret)[:]
예제 #14
0
    def encode_key(self):
        """
        Encodes the ECC private key in an ASN sequence.

        Returns the encoded key.
        """
        key = _ffi.new("byte[%d]" % (self.size * 4))

        ret = _lib.wc_EccKeyToDer(self.native_object, key, len(key))
        if ret <= 0:  # pragma: no cover
            raise WolfCryptError("Key encode error (%d)" % ret)

        return _ffi.buffer(key, ret)[:]
예제 #15
0
            def sign_raw(self, plaintext, rng=Random()):
                """
                Signs **plaintext**, using the private key data in the object.

                Returns the signature in its two raw components r, s
                """
                plaintext = t2b(plaintext)
                R = _ffi.new("mp_int[1]");
                S = _ffi.new("mp_int[1]");

                R_bin = _ffi.new("unsigned char[%d]" % self.size )
                S_bin = _ffi.new("unsigned char[%d]" % self.size )

                ret = _lib.mp_init(R)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("wolfCrypt error (%d)" % ret)
                ret = _lib.mp_init(S)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("wolfCrypt error (%d)" % ret)

                ret = _lib.wc_ecc_sign_hash_ex(plaintext, len(plaintext),
                                            rng.native_object,
                                            self.native_object,
                                            R, S)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("Signature error (%d)" % ret)

                ret = _lib.mp_to_unsigned_bin(R, R_bin)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("wolfCrypt error (%d)" % ret)

                ret = _lib.mp_to_unsigned_bin(S, S_bin)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("wolfCrypt error (%d)" % ret)

                return _ffi.buffer(R_bin, self.size)[:], _ffi.buffer(S_bin,
                        self.size)[:]
예제 #16
0
    def PBKDF2(password, salt, iterations, key_length, hash_type):
        if isinstance(salt, str):
            salt = str.encode(salt)

        if isinstance(password, str):
            password = str.encode(password)

        key = _ffi.new("byte[%d]" % key_length)
        ret = _lib.wc_PBKDF2(key, password, len(password), salt, len(salt),
                             iterations, key_length, hash_type)

        if ret != 0:
            raise WolfCryptError("PBKDF2 error (%d)" % ret)

        return _ffi.buffer(key, key_length)[:]
예제 #17
0
        def export_x963(self):
            """
            Exports the public key data of the object in ANSI X9.63 format.

            Returns the exported key.
            """
            x963 = _ffi.new("byte[%d]" % (self.size * 4))
            x963_size = _ffi.new("word32[1]")
            x963_size[0] = self.size * 4

            ret = _lib.wc_ecc_export_x963(self.native_object, x963, x963_size)
            if ret != 0:  # pragma: no cover
                raise WolfCryptError("x963 export error (%d)" % ret)

            return _ffi.buffer(x963, x963_size[0])[:]
예제 #18
0
    def sign_raw(self, plaintext, rng=Random()):
        """
        Signs **plaintext**, using the private key data in the object.

        Returns the signature in its two raw components r, s
        """
        plaintext = t2b(plaintext)
        R = _ffi.new("mp_int[1]");
        S = _ffi.new("mp_int[1]");

        R_bin = _ffi.new("unsigned char[%d]" % self.size )
        S_bin = _ffi.new("unsigned char[%d]" % self.size )

        ret = _lib.mp_init(R)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("wolfCrypt error (%d)" % ret)
        ret = _lib.mp_init(S)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("wolfCrypt error (%d)" % ret)

        ret = _lib.wc_ecc_sign_hash_ex(plaintext, len(plaintext),
                                    rng.native_object,
                                    self.native_object,
                                    R, S)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Signature error (%d)" % ret)

        ret = _lib.mp_to_unsigned_bin(R, R_bin)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("wolfCrypt error (%d)" % ret)

        ret = _lib.mp_to_unsigned_bin(S, S_bin)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("wolfCrypt error (%d)" % ret)

        return _ffi.buffer(R_bin, self.size)[:], _ffi.buffer(S_bin, self.size)[:]
예제 #19
0
    def export_x963(self):
        """
        Exports the public key data of the object in ANSI X9.63 format.

        Returns the exported key.
        """
        x963 = _ffi.new("byte[%d]" % (self.size * 4))
        x963_size = _ffi.new("word32[1]")
        x963_size[0] = self.size * 4

        ret = _lib.wc_ecc_export_x963(self.native_object, x963, x963_size)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("x963 export error (%d)" % ret)

        return _ffi.buffer(x963, x963_size[0])[:]
예제 #20
0
    def shared_secret(self, peer):
        """
        Generates a new secret key using the private key data in the object
        and the peer's public key.

        Returns the shared secret.
        """
        shared_secret = _ffi.new("byte[%d]" % self.max_signature_size)
        secret_size = _ffi.new("word32[1]")
        secret_size[0] = self.max_signature_size

        ret = _lib.wc_ecc_shared_secret(self.native_object, peer.native_object,
                                        shared_secret, secret_size)

        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Shared secret error (%d)" % ret)

        return _ffi.buffer(shared_secret, secret_size[0])[:]
예제 #21
0
        def verify(self, signature):
            """
            Verifies **signature**, using the public key data in the
            object. The signature's length must be equal to:

                **self.output_size**

            Returns a string containing the plaintext.
            """
            signature = t2b(signature)
            plaintext = _ffi.new("byte[%d]" % self.output_size)

            ret = _lib.wc_RsaSSL_Verify(signature, len(signature), plaintext,
                                        self.output_size, self.native_object)

            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Verify error (%d)" % ret)

            return _ffi.buffer(plaintext, ret)[:]
예제 #22
0
    def digest(self):
        """
        Returns the hash value of this hashing object as a string
        containing 8-bit data. The object is not altered in any
        way by this function; you can continue updating the object
        after calling this function.
        """
        result = _ffi.new("byte[%d]" % self.digest_size)

        if self._native_object:
            obj = _ffi.new(self._native_type)

            _ffi.memmove(obj, self._native_object, self._native_size)

            ret = self._final(obj, result)
            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Hash finalize error (%d)" % ret)

        return _ffi.buffer(result, self.digest_size)[:]
예제 #23
0
    def decrypt(self, ciphertext):
        """
        Decrypts **ciphertext**, using the private key data in the
        object. The ciphertext's length must be equal to:

            **self.output_size**

        Returns a string containing the plaintext.
        """
        ciphertext = t2b(ciphertext)
        plaintext = _ffi.new("byte[%d]" % self.output_size)

        ret = _lib.wc_RsaPrivateDecrypt(ciphertext, len(ciphertext), plaintext,
                                        self.output_size, self.native_object)

        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Decryption error (%d)" % ret)

        return _ffi.buffer(plaintext, ret)[:]
예제 #24
0
    def digest(self):
        """
        Returns the hash value of this hashing object as a string
        containing 8-bit data. The object is not altered in any
        way by this function; you can continue updating the object
        after calling this function.
        """
        result = _ffi.new("byte[%d]" % self.digest_size)

        if self._native_object:
            obj = _ffi.new(self._native_type)

            _ffi.memmove(obj, self._native_object, self._native_size)

            ret = self._final(obj, result)
            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Hash finalize error (%d)" % ret)

        return _ffi.buffer(result, self.digest_size)[:]
예제 #25
0
    def shared_secret(self, peer):
        """
        Generates a new secret key using the private key data in the object
        and the peer's public key.

        Returns the shared secret.
        """
        shared_secret = _ffi.new("byte[%d]" % self.max_signature_size)
        secret_size = _ffi.new("word32[1]")
        secret_size[0] = self.max_signature_size

        ret = _lib.wc_ecc_shared_secret(self.native_object,
                                        peer.native_object,
                                        shared_secret, secret_size)

        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Shared secret error (%d)" % ret)

        return _ffi.buffer(shared_secret, secret_size[0])[:]
예제 #26
0
    def verify(self, signature):
        """
        Verifies **signature**, using the public key data in the
        object. The signature's length must be equal to:

            **self.output_size**

        Returns a string containing the plaintext.
        """
        signature = t2b(signature)
        plaintext = _ffi.new("byte[%d]" % self.output_size)

        ret = _lib.wc_RsaSSL_Verify(signature, len(signature),
                                    plaintext, self.output_size,
                                    self.native_object)

        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Verify error (%d)" % ret)

        return _ffi.buffer(plaintext, ret)[:]
예제 #27
0
        def sign(self, plaintext):
            """
            Signs **plaintext**, using the private key data in the object.
            The plaintext's length must not be greater than:

                **self.output_size - self.RSA_MIN_PAD_SIZE**

            Returns a string containing the signature.
            """
            plaintext = t2b(plaintext)
            signature = _ffi.new("byte[%d]" % self.output_size)

            ret = _lib.wc_RsaSSL_Sign(plaintext, len(plaintext), signature,
                                      self.output_size, self.native_object,
                                      self._random.native_object)

            if ret != self.output_size:  # pragma: no cover
                raise WolfCryptError("Signature error (%d)" % ret)

            return _ffi.buffer(signature, self.output_size)[:]
예제 #28
0
    def decrypt(self, ciphertext):
        """
        Decrypts **ciphertext**, using the private key data in the
        object. The ciphertext's length must be equal to:

            **self.output_size**

        Returns a string containing the plaintext.
        """
        ciphertext = t2b(ciphertext)
        plaintext = _ffi.new("byte[%d]" % self.output_size)

        ret = _lib.wc_RsaPrivateDecrypt(ciphertext, len(ciphertext),
                                        plaintext, self.output_size,
                                        self.native_object)

        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Decryption error (%d)" % ret)

        return _ffi.buffer(plaintext, ret)[:]
예제 #29
0
    def sign(self, plaintext):
        """
        Signs **plaintext**, using the private key data in the object.

        Returns the signature.
        """
        plaintext = t2b(plaintext)
        signature = _ffi.new("byte[%d]" % self.max_signature_size)

        signature_size = _ffi.new("word32[1]")
        signature_size[0] = self.max_signature_size

        ret = _lib.wc_ed25519_sign_msg(plaintext, len(plaintext),
                                    signature, signature_size,
                                    self.native_object)

        if ret != 0:  # pragma: no cover
            raise WolfCryptError("Signature error (%d)" % ret)

        return _ffi.buffer(signature, signature_size[0])[:]
예제 #30
0
        def sign(self, plaintext):
            """
            Signs **plaintext**, using the private key data in the object.

            Returns the signature.
            """
            plaintext = t2b(plaintext)
            signature = _ffi.new("byte[%d]" % self.max_signature_size)

            signature_size = _ffi.new("word32[1]")
            signature_size[0] = self.max_signature_size

            ret = _lib.wc_ed25519_sign_msg(plaintext, len(plaintext),
                                        signature, signature_size,
                                        self.native_object)

            if ret != 0:  # pragma: no cover
                raise WolfCryptError("Signature error (%d)" % ret)

            return _ffi.buffer(signature, signature_size[0])[:]
예제 #31
0
    def encrypt(self, plaintext):
        """
        Encrypts **plaintext**, using the public key data in the
        object. The plaintext's length must not be greater than:

            **self.output_size - self.RSA_MIN_PAD_SIZE**

        Returns a string containing the ciphertext.
        """

        plaintext = t2b(plaintext)
        ciphertext = _ffi.new("byte[%d]" % self.output_size)

        ret = _lib.wc_RsaPublicEncrypt(plaintext, len(plaintext), ciphertext,
                                       self.output_size, self.native_object,
                                       self._random.native_object)

        if ret != self.output_size:  # pragma: no cover
            raise WolfCryptError("Encryption error (%d)" % ret)

        return _ffi.buffer(ciphertext)[:]
예제 #32
0
    def sign(self, plaintext):
        """
        Signs **plaintext**, using the private key data in the object.
        The plaintext's length must not be greater than:

            **self.output_size - self.RSA_MIN_PAD_SIZE**

        Returns a string containing the signature.
        """
        plaintext = t2b(plaintext)
        signature = _ffi.new("byte[%d]" % self.output_size)

        ret = _lib.wc_RsaSSL_Sign(plaintext, len(plaintext),
                                  signature, self.output_size,
                                  self.native_object,
                                  self._random.native_object)

        if ret != self.output_size:  # pragma: no cover
            raise WolfCryptError("Signature error (%d)" % ret)

        return _ffi.buffer(signature, self.output_size)[:]
예제 #33
0
    def encrypt(self, plaintext):
        """
        Encrypts **plaintext**, using the public key data in the
        object. The plaintext's length must not be greater than:

            **self.output_size - self.RSA_MIN_PAD_SIZE**

        Returns a string containing the ciphertext.
        """

        plaintext = t2b(plaintext)
        ciphertext = _ffi.new("byte[%d]" % self.output_size)

        ret = _lib.wc_RsaPublicEncrypt(plaintext, len(plaintext),
                                       ciphertext, self.output_size,
                                       self.native_object,
                                       self._random.native_object)

        if ret != self.output_size:  # pragma: no cover
            raise WolfCryptError("Encryption error (%d)" % ret)

        return _ffi.buffer(ciphertext)[:]