コード例 #1
0
ファイル: ciphers.py プロジェクト: kaleb-himes/wolfssl
    def __init__(self, key, mode, IV=None):
        if mode not in _FEEDBACK_MODES:
            raise ValueError("this mode is not supported")

        if mode == MODE_CBC:
            if IV is None:
                raise ValueError("this mode requires an 'IV' string")
        else:
            raise ValueError("this mode is not supported by this cipher")

        if self.key_size:
            if self.key_size != len(key):
                raise ValueError("key must be %d in length" % self.key_size)
        elif self._key_sizes:
            if len(key) not in self._key_sizes:
                raise ValueError("key must be %s in length" % self._key_sizes)
        else:
            if not len(key):
                raise ValueError("key must not be 0 in length")

        if IV is not None and len(IV) != self.block_size:
            raise ValueError("IV must be %d in length" % self.block_size)

        self._native_object = _ffi.new(self._native_type)
        self._enc = None
        self._dec = None
        self._key = t2b(key)

        if IV:
            self._IV = t2b(IV)
        else:
            self._IV = t2b("\0" * self.block_size)
コード例 #2
0
    def __init__(self, key, mode, IV=None):
        if mode not in _FEEDBACK_MODES:
            raise ValueError("this mode is not supported")

        if mode == MODE_CBC:
            if IV is None:
                raise ValueError("this mode requires an 'IV' string")
        else:
            raise ValueError("this mode is not supported by this cipher")

        if self.key_size:
            if self.key_size != len(key):
                raise ValueError("key must be %d in length" % self.key_size)
        elif self._key_sizes:
            if len(key) not in self._key_sizes:
                raise ValueError("key must be %s in length" % self._key_sizes)
        else:
            if not len(key):
                raise ValueError("key must not be 0 in length")

        if IV is not None and len(IV) != self.block_size:
            raise ValueError("IV must be %d in length" % self.block_size)

        self._native_object = _ffi.new(self._native_type)
        self._enc = None
        self._dec = None
        self._key = t2b(key)

        if IV:
            self._IV = t2b(IV)
        else:
            self._IV = t2b("\0" * self.block_size)
コード例 #3
0
ファイル: ciphers.py プロジェクト: kaleb-himes/wolfssl
    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:
                raise WolfCryptError("Invalid key error (%d)" % ret)

        result = t2b("\0" * len(string))
        ret = self._encrypt(result, string)
        if ret < 0:
            raise WolfCryptError("Encryption error (%d)" % ret)

        return result
コード例 #4
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:
                raise WolfCryptError("Invalid key error (%d)" % ret)

        result = t2b("\0" * len(string))
        ret = self._encrypt(result, string)
        if ret < 0:
            raise WolfCryptError("Encryption error (%d)" % ret)

        return result
コード例 #5
0
def vectors():
    TestVector = namedtuple("TestVector", "digest")
    TestVector.__new__.__defaults__ = (None, ) * len(TestVector._fields)

    return {
        Sha:
        TestVector(digest=t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d")),
        Sha256:
        TestVector(digest=t2b("96e02e7b1cbcd6f104fe1fdb4652027a" +
                              "5505b68652b70095c6318f9dce0d1844")),
        Sha384:
        TestVector(
            digest=t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" +
                       "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b")),
        Sha512:
        TestVector(digest=t2b("88fcf67ffd8558d713f9cedcd852db47" +
                              "9e6573f0bd9955610a993f609637553c" +
                              "e8fff55e644ee8a106aae19c07f91b3f" +
                              "2a2a6d40dfa7302c0fa6a1a9a5bfa03f")),
        HmacSha:
        TestVector(digest=t2b("5dfabcfb3a25540824867cd21f065f52f73491e0")),
        HmacSha256:
        TestVector(digest=t2b("4b641d721493d80f019d9447830ebfee" +
                              "89234a7d594378b89f8bb73873576bf6")),
        HmacSha384:
        TestVector(
            digest=t2b("e72c72070c9c5c78e3286593068a510c1740cdf9dc34b512" +
                       "ccec97320295db1fe673216b46fe72e81f399a9ec04780ab")),
        HmacSha512:
        TestVector(digest=t2b("c7f48db79314fc2b5be9a93fd58601a1" +
                              "bf42f397ec7f66dba034d44503890e6b" +
                              "5708242dcd71a248a78162d815c685f6" +
                              "038a4ac8cb34b8bf18986dbd300c9b41")),
    }
コード例 #6
0
        def __init__(self, key = None):  # pylint: disable=super-init-not-called

            _Rsa.__init__(self)  # pylint: disable=non-parent-init-called

            idx = _ffi.new("word32*")
            idx[0] = 0

            if key != None:
                key = t2b(key)
                ret = _lib.wc_RsaPrivateKeyDecode(key, idx,
                                              self.native_object, len(key))
                if ret < 0:
                    idx[0] = 0
                    ret = _lib.wc_GetPkcs8TraditionalOffset(key, idx, len(key))
                    if ret < 0:
                        raise WolfCryptError("Invalid key error (%d)" % ret)

                    ret = _lib.wc_RsaPrivateKeyDecode(key, idx,
                                              self.native_object, len(key))
                    if ret < 0:
                        raise WolfCryptError("Invalid key error (%d)" % ret)

                self.size = len(key)
                self.output_size = _lib.wc_RsaEncryptSize(self.native_object)
                if self.output_size <= 0:  # pragma: no cover
                    raise WolfCryptError("Invalid key size error (%d)" %
                            self.output_size)
コード例 #7
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    def verify_raw(self, R, S, data):
        """
        Verifies signature from its raw elements **R** and **S**, using the
        public key data in the object.

        Returns **True** in case of a valid signature, otherwise **False**.
        """
        data = t2b(data)
        status = _ffi.new("int[1]")
        mpR = _ffi.new("mp_int[1]")
        mpS = _ffi.new("mp_int[1]")
        ret = _lib.mp_init(mpR)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("wolfCrypt error (%d)" % ret)
        ret = _lib.mp_init(mpS)
        if ret != 0:  # pragma: no cover
            raise WolfCryptError("wolfCrypt error (%d)" % ret)

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

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


        ret = _lib.wc_ecc_verify_hash_ex(mpR, mpS,
                                      data, len(data),
                                      status, self.native_object)

        if ret < 0:
            raise WolfCryptError("Verify error (%d)" % ret)

        return status[0] == 1
コード例 #8
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    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)[:]
コード例 #9
0
ファイル: ciphers.py プロジェクト: haydenroche5/wolfcrypt-py
            def verify_raw(self, R, S, data):
                """
                Verifies signature from its raw elements **R** and **S**, using
                the public key data in the object.

                Returns **True** in case of a valid signature, otherwise
                **False**.
                """
                data = t2b(data)
                status = _ffi.new("int[1]")
                mpR = _ffi.new("mp_int[1]")
                mpS = _ffi.new("mp_int[1]")
                ret = _lib.mp_init(mpR)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("wolfCrypt error (%d)" % ret)
                ret = _lib.mp_init(mpS)
                if ret != 0:  # pragma: no cover
                    raise WolfCryptError("wolfCrypt error (%d)" % ret)

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

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

                ret = _lib.wc_ecc_verify_hash_ex(mpR, mpS, data, len(data),
                                                 status, self.native_object)

                if ret < 0:
                    raise WolfCryptError("Verify error (%d)" % ret)

                return status[0] == 1
コード例 #10
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)[:]
コード例 #11
0
ファイル: test_hashes.py プロジェクト: xriss/gamecake
class TestHmacSha(unittest.TestCase):
    _class = HmacSha
    digest = t2b("5dfabcfb3a25540824867cd21f065f52f73491e0")

    def setUp(self):
        self.hash = self._class(_HMAC_KEY)

    def test_new(self):
        # update inside constructor
        assert self._class.new(_HMAC_KEY,
                               "wolfcrypt").hexdigest() == self.digest

    def test_hash_update_001(self):
        self.hash.update("wolfcrypt")

        assert self.hash.hexdigest() == self.digest

    def test_hash_update_002(self):
        self.hash.update("wolf")
        self.hash.update("crypt")

        assert self.hash.hexdigest() == self.digest

    def test_hash_copy(self):
        copy = self.hash.copy()

        assert self.hash.hexdigest() == copy.hexdigest()

        self.hash.update("wolfcrypt")

        assert self.hash.hexdigest() != copy.hexdigest()

        copy.update("wolfcrypt")

        assert self.hash.hexdigest() == copy.hexdigest() == self.digest
コード例 #12
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    def decode_key(self, key, pub = None):
        """
        Decodes an ED25519 private + pub key
        """
        key = t2b(key)

        if (len(key) < _lib.wc_ed25519_priv_size(self.native_object)/2):
            raise WolfCryptError("Key decode error: key too short")

        idx = _ffi.new("word32*")
        idx[0] = 0
        if pub:
            ret = _lib.wc_ed25519_import_private_key(key, len(key), pub, len(pub), self.native_object);
            if ret < 0:
                raise WolfCryptError("Key decode error (%d)" % ret)
        else:
            ret = _lib.wc_ed25519_import_private_only(key, len(key), self.native_object);
            if ret < 0:
                raise WolfCryptError("Key decode error (%d)" % ret)
            pubkey = _ffi.new("byte[%d]" % (self.size * 4))
            ret = _lib.wc_ed25519_make_public(self.native_object, pubkey, self.size)
            if ret < 0:
                raise WolfCryptError("Public key generate error (%d)" % ret)
            ret = _lib.wc_ed25519_import_public(pubkey, self.size, self.native_object);

        if self.size <= 0:  # pragma: no cover
            raise WolfCryptError("Key decode error (%d)" % self.size)
        if self.max_signature_size <= 0:  # pragma: no cover
            raise WolfCryptError(
                "Key decode error (%d)" % self.max_signature_size)
コード例 #13
0
        def decode_key(self, key, pub = None):
            """
            Decodes an ED25519 private + pub key
            """
            key = t2b(key)

            if (len(key) < _lib.wc_ed25519_priv_size(self.native_object)/2):
                raise WolfCryptError("Key decode error: key too short")

            idx = _ffi.new("word32*")
            idx[0] = 0
            if pub:
                ret = _lib.wc_ed25519_import_private_key(key, len(key), pub,
                        len(pub), self.native_object);
                if ret < 0:
                    raise WolfCryptError("Key decode error (%d)" % ret)
            else:
                ret = _lib.wc_ed25519_import_private_only(key, len(key),
                        self.native_object);
                if ret < 0:
                    raise WolfCryptError("Key decode error (%d)" % ret)
                pubkey = _ffi.new("byte[%d]" % (self.size * 4))
                ret = _lib.wc_ed25519_make_public(self.native_object, pubkey,
                        self.size)
                if ret < 0:
                    raise WolfCryptError("Public key generate error (%d)" % ret)
                ret = _lib.wc_ed25519_import_public(pubkey, self.size,
                        self.native_object);

            if self.size <= 0:  # pragma: no cover
                raise WolfCryptError("Key decode error (%d)" % self.size)
            if self.max_signature_size <= 0:  # pragma: no cover
                raise WolfCryptError(
                    "Key decode error (%d)" % self.max_signature_size)
コード例 #14
0
def vectors():
    TestVector = namedtuple("TestVector", "digest")
    TestVector.__new__.__defaults__ = (None, ) * len(TestVector._fields)

    # test vector dictionary
    vectorArray = {}

    if _lib.SHA_ENABLED:
        vectorArray[Sha] = TestVector(
            digest=t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d"))

    if _lib.SHA256_ENABLED:
        vectorArray[Sha256] = TestVector(
            digest=t2b("96e02e7b1cbcd6f104fe1fdb4652027a" +
                       "5505b68652b70095c6318f9dce0d1844"))

    if _lib.SHA384_ENABLED:
        vectorArray[Sha384] = TestVector(
            digest=t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" +
                       "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b"))

    if _lib.SHA512_ENABLED:
        vectorArray[Sha512] = TestVector(
            digest=t2b("88fcf67ffd8558d713f9cedcd852db47" +
                       "9e6573f0bd9955610a993f609637553c" +
                       "e8fff55e644ee8a106aae19c07f91b3f" +
                       "2a2a6d40dfa7302c0fa6a1a9a5bfa03f"))
    if _lib.SHA3_ENABLED:
        vectorArray[Sha3] = TestVector(
            digest=t2b("6170dedf06f83c3305ec18b7558384a5" +
                       "a62d86e42c143d416aaec32f971986c1" +
                       "e84edf61df308cc6d8c310d1956e1908"))
    if _lib.HMAC_ENABLED:
        if _lib.SHA_ENABLED:
            vectorArray[HmacSha] = TestVector(
                digest=t2b("5dfabcfb3a25540824867cd21f065f52f73491e0"))
        if _lib.SHA256_ENABLED:
            vectorArray[HmacSha256] = TestVector(
                digest=t2b("4b641d721493d80f019d9447830ebfee" +
                           "89234a7d594378b89f8bb73873576bf6"))
        if _lib.SHA384_ENABLED:
            vectorArray[HmacSha384] = TestVector(
                digest=t2b("e72c72070c9c5c78e3286593068a510c1740cdf9dc34b512" +
                           "ccec97320295db1fe673216b46fe72e81f399a9ec04780ab"))
        if _lib.SHA512_ENABLED:
            vectorArray[HmacSha512] = TestVector(
                digest=t2b("c7f48db79314fc2b5be9a93fd58601a1" +
                           "bf42f397ec7f66dba034d44503890e6b" +
                           "5708242dcd71a248a78162d815c685f6" +
                           "038a4ac8cb34b8bf18986dbd300c9b41"))

    return vectorArray
コード例 #15
0
ファイル: test_hashes.py プロジェクト: wolfSSL/wolfcrypt-py
def vectors():
    TestVector = namedtuple("TestVector", "digest")
    TestVector.__new__.__defaults__ = (None, ) * len(TestVector._fields)

    # test vector dictionary
    vectorArray = {}

    if _lib.SHA_ENABLED:
        vectorArray[Sha] = TestVector(
            digest=t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d"))

    if _lib.SHA256_ENABLED:
        vectorArray[Sha256] = TestVector(
            digest=t2b("96e02e7b1cbcd6f104fe1fdb4652027a" +
                       "5505b68652b70095c6318f9dce0d1844"))

    if _lib.SHA384_ENABLED:
        vectorArray[Sha384] = TestVector(
            digest=t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" +
                       "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b"))

    if _lib.SHA512_ENABLED:
        vectorArray[Sha512] = TestVector(
            digest=t2b("88fcf67ffd8558d713f9cedcd852db47" +
                       "9e6573f0bd9955610a993f609637553c" +
                       "e8fff55e644ee8a106aae19c07f91b3f" +
                       "2a2a6d40dfa7302c0fa6a1a9a5bfa03f"))
    if _lib.SHA3_ENABLED:
        vectorArray[Sha3] = TestVector(
            digest=t2b("6170dedf06f83c3305ec18b7558384a5" +
                       "a62d86e42c143d416aaec32f971986c1" +
                       "e84edf61df308cc6d8c310d1956e1908"))
    if _lib.HMAC_ENABLED:
        if _lib.SHA_ENABLED:
            vectorArray[HmacSha] = TestVector(
                digest=t2b("7ab9aca2c87c7c45ba2ffa52f719fdbd8fbff62d"))
        if _lib.SHA256_ENABLED:
            vectorArray[HmacSha256] = TestVector(digest=t2b(
                "9041ac8c66fc350a1a0d5f4fff9d8ef74721d5a43ec8893a2" +
                "875cf69576c45c2"))
        if _lib.SHA384_ENABLED:
            vectorArray[HmacSha384] = TestVector(digest=t2b(
                "f8c589ddf5489404f85c3c718a8345f207fb1ed6c6f5ecb09" +
                "8e8be8aeb1aaa9f0c6dd84c141410b29a47a1a2b3a85ae0"))
        if _lib.SHA512_ENABLED:
            vectorArray[HmacSha512] = TestVector(digest=t2b(
                "7708a12ca110cd81a334bd4e8bddc4314acd3ed218bbff7c6" +
                "486e149fc145e9f5c05f05e919f7c2bc027266e986679984c" +
                "3ade1a14084ad7627a65c3671a2d05"))

    return vectorArray
コード例 #16
0
class TestRsaPublic(unittest.TestCase):
    prv = "3082025C02010002818100BC730EA849F374A2A9EF18A5DA559921F9C8ECB36D" \
        + "48E53535757737ECD161905F3ED9E4D5DF94CAC1A9D719DA86C9E84DC4613682" \
        + "FEABAD7E7725BB8D11A5BC623AA838CC39A20466B4F7F7F3AADA4D020EBB5E8D" \
        + "6948DC77C9280E22E96BA426BA4CE8C1FD4A6F2B1FEF8AAEF69062E5641EEB2B" \
        + "3C67C8DC2700F6916865A902030100010281801397EAE8387825A25C04CE0D40" \
        + "7C31E5C470CD9B823B5809863B665FDC3190F14FD5DB15DDDED73B9593311831" \
        + "0E5EA3D6A21A716E81481C4BCFDB8E7A866132DCFB55C1166D279224458BF1B8" \
        + "48B14B1DACDEDADD8E2FC291FBA5A96EF83A6AF1FD5018EF9FE7C3CA78EA56D3" \
        + "D3725B96DD4E064E3AC3D9BE72B66507074C01024100FA47D47A7C923C55EF81" \
        + "F041302DA3CF8F1CE6872705700DDF9835D6F18B382F24B5D084B6794F712994" \
        + "5AF0646AACE772C6ED4D59983E673AF3742CF9611769024100C0C1820D0CEBC6" \
        + "2FDC92F99D821A31E9E9F74BF282871CEE166AD11D188270F3C0B62FF6F3F71D" \
        + "F18623C84EEB8F568E8FF5BFF1F72BB5CC3DC657390C1B54410241009D7E05DE" \
        + "EDF4B7B2FBFC304B551DE32F0147966905CD0E2E2CBD8363B6AB7CB76DCA5B64" \
        + "A7CEBE86DF3B53DE61D21EEBA5F637EDACAB78D94CE755FBD71199C102401898" \
        + "1829E61E2739702168AC0A2FA172C121869538C65890A0579CBAE3A7B115C8DE" \
        + "F61BC2612376EFB09D1C44BE1343396717C89DCAFBF545648B38822CF2810240" \
        + "3989E59C195530BAB7488C48140EF49F7E779743E1B419353123759C3B44AD69" \
        + "1256EE0061641666D37C742B15B4A2FEBF086B1A5D3F9012B105863129DBD9E2"

    pub = "30819F300D06092A864886F70D010101050003818D0030818902818100BC730E" \
        + "A849F374A2A9EF18A5DA559921F9C8ECB36D48E53535757737ECD161905F3ED9" \
        + "E4D5DF94CAC1A9D719DA86C9E84DC4613682FEABAD7E7725BB8D11A5BC623AA8" \
        + "38CC39A20466B4F7F7F3AADA4D020EBB5E8D6948DC77C9280E22E96BA426BA4C" \
        + "E8C1FD4A6F2B1FEF8AAEF69062E5641EEB2B3C67C8DC2700F6916865A90203010001"

    plain = t2b("Everyone gets Friday off.")


    def setUp(self):
        self.private = RsaPrivate(h2b(self.prv))
        self.public  = RsaPublic(h2b(self.pub))


    def test_raises(self):
        # invalid key
        self.assertRaises(WolfCryptError, RsaPublic, 'key')


    def test_output_size(self):
        assert self.public.output_size == 1024 / 8


    def test_encrypt_decrypt(self):
        cipher = self.public.encrypt(self.plain)
        result = self.private.decrypt(cipher)

        assert len(cipher) == self.public.output_size == 1024 / 8
        assert self.plain == result


    def test_sign_verify(self):
        signature = self.private.sign(self.plain)
        result    = self.public.verify(signature)

        assert len(signature) == self.public.output_size == 1024 / 8
        assert self.plain == result
コード例 #17
0
    def test_multi_decryption(self):
        result = t2b("")
        segments = tuple(self.cipher[i:i + self.aes.block_size] \
            for i in range(0, len(self.cipher), self.aes.block_size))

        for segment in segments:
            result += self.aes.decrypt(segment)

        assert result == self.plain
コード例 #18
0
ファイル: test_ciphers.py プロジェクト: atigyi/wolfssl
    def test_multi_encryption(self):
        result = t2b("")
        segments = tuple(self.plain[i:i + Des3.block_size] \
            for i in range(0, len(self.plain), Des3.block_size))

        for segment in segments:
            result += self.des3.encrypt(segment)

        assert result == self.cipher
コード例 #19
0
ファイル: ciphers.py プロジェクト: dgarske/wolfssl
    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 = t2b("\0" * self.output_size)

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

        if ret < 0:
            raise WolfCryptError("Decryption error (%d)" % ret)

        return plaintext[:ret]
コード例 #20
0
ファイル: test_ciphers.py プロジェクト: kaleb-himes/wolfssl
    def test_multi_decryption(self):
        result = t2b("")
        segments = tuple(self.cipher[i:i + self.aes.block_size] \
            for i in range(0, len(self.cipher), self.aes.block_size))

        for segment in segments:
            result += self.aes.decrypt(segment)

        assert result == self.plain
コード例 #21
0
ファイル: ciphers.py プロジェクト: dgarske/wolfssl
    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 = t2b("\0" * self.output_size)

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

        if ret < 0:
            raise WolfCryptError("Verify error (%d)" % ret)

        return plaintext[:ret]
コード例 #22
0
    def test_multi_encryption(self):
        result = t2b("")
        segments = tuple(self.plain[i:i + Des3.block_size] \
            for i in range(0, len(self.plain), Des3.block_size))

        for segment in segments:
            result += self.des3.encrypt(segment)

        assert result == self.cipher
コード例 #23
0
ファイル: hashes.py プロジェクト: wolfSSL/wolfcrypt-py
    def __init__(self, key, string=None):  # pylint: disable=W0231
        key = t2b(key)

        self._native_object = _ffi.new(self._native_type)
        ret = self._init(self._type, key)
        if ret < 0:  # pragma: no cover
            raise WolfCryptError("Hmac init error (%d)" % ret)

        if string:
            self.update(string)
コード例 #24
0
ファイル: hashes.py プロジェクト: kaleb-himes/wolfssl
    def __init__(self, key, string=None):
        key = t2b(key)

        self._native_object = _ffi.new(self._native_type)
        ret = self._init(self._type, key)
        if ret < 0:
            raise WolfCryptError("Hmac init error (%d)" % ret)

        if (string):
            self.update(string)
コード例 #25
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 = t2b("\0" * self.output_size)

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

        if ret < 0:
            raise WolfCryptError("Verify error (%d)" % ret)

        return plaintext[:ret]
コード例 #26
0
ファイル: hashes.py プロジェクト: xriss/gamecake
    def __init__(self, key, string=None):
        key = t2b(key)

        self._native_object = _ffi.new(self._native_type)
        ret = self._init(self._type, key)
        if ret < 0:
            raise WolfCryptError("Hmac init error (%d)" % ret)

        if (string):
            self.update(string)
コード例 #27
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 = t2b("\0" * self.output_size)

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

        if ret < 0:
            raise WolfCryptError("Decryption error (%d)" % ret)

        return plaintext[:ret]
コード例 #28
0
        def __init__(self, key, string=None):  # pylint: disable=W0231
            key = t2b(key)

            self._native_object = _ffi.new(self._native_type)
            ret = self._init(self._type, key)
            if ret < 0:  # pragma: no cover
                raise WolfCryptError("Hmac init error (%d)" % ret)

            if string:
                self.update(string)
コード例 #29
0
    def bytes(self, length):
        """
        Generate and return a random sequence of length bytes.
        """
        result = t2b("\0" * length)

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

        return result
コード例 #30
0
    def byte(self):
        """
        Generate and return a random byte.
        """
        result = t2b("\0")

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

        return result
コード例 #31
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 = t2b("\0" * self.output_size)

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

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

        return signature
コード例 #32
0
ファイル: hashes.py プロジェクト: kaleb-himes/wolfssl
    def update(self, string):
        """
        Hashes **string** into the current state of the hashing
        object. update() can be called any number of times during
        a hashing object's lifetime.
        """
        string = t2b(string)

        ret = self._update(string)
        if ret < 0:
            raise WolfCryptError("Hash update error (%d)" % ret)
コード例 #33
0
class TestAes(unittest.TestCase):
    key    = "0123456789abcdef"
    IV     = "1234567890abcdef"
    plain  = t2b("now is the time ")
    cipher = h2b("959492575f4281532ccc9d4677a233cb")


    def setUp(self):
        self.aes = Aes.new(self.key, MODE_CBC, self.IV)


    def test_raises(self):
        # invalid key length
        self.assertRaises(ValueError, Aes.new, "key", MODE_CBC, self.IV)

        # invalid mode
        self.assertRaises(ValueError, Aes.new, self.key, MODE_ECB, self.IV)

        # invalid iv length
        self.assertRaises(ValueError, Aes.new, self.key, MODE_CBC, "IV")

        # invalid data length
        self.assertRaises(ValueError, self.aes.encrypt, "foo")
        self.assertRaises(ValueError, self.aes.decrypt, "bar")


    def test_single_encryption(self):
        assert self.aes.encrypt(self.plain) == self.cipher


    def test_multi_encryption(self):
        result = t2b("")
        segments = tuple(self.plain[i:i + self.aes.block_size] \
            for i in range(0, len(self.plain), self.aes.block_size))

        for segment in segments:
            result += self.aes.encrypt(segment)

        assert result == self.cipher


    def test_single_decryption(self):
        assert self.aes.decrypt(self.cipher) == self.plain


    def test_multi_decryption(self):
        result = t2b("")
        segments = tuple(self.cipher[i:i + self.aes.block_size] \
            for i in range(0, len(self.cipher), self.aes.block_size))

        for segment in segments:
            result += self.aes.decrypt(segment)

        assert result == self.plain
コード例 #34
0
ファイル: ciphers.py プロジェクト: dgarske/wolfssl
    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 = t2b("\0" * self.output_size)

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

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

        return signature
コード例 #35
0
ファイル: hashes.py プロジェクト: xriss/gamecake
    def update(self, string):
        """
        Hashes **string** into the current state of the hashing
        object. update() can be called any number of times during
        a hashing object's lifetime.
        """
        string = t2b(string)

        ret = self._update(string)
        if ret < 0:
            raise WolfCryptError("Hash update error (%d)" % ret)
コード例 #36
0
ファイル: random.py プロジェクト: atigyi/wolfssl
    def bytes(self, length):
        """
        Generate and return a random sequence of length bytes.
        """
        result = t2b("\0" * length)

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

        return result
コード例 #37
0
ファイル: random.py プロジェクト: atigyi/wolfssl
    def byte(self):
        """
        Generate and return a random byte.
        """
        result = t2b("\0")

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

        return result
コード例 #38
0
def test_encrypt_short_tag():
    key = "fedcba9876543210"
    iv = "0123456789abcdef"
    gcm = AesGcmStream(key, iv, 12)
    buf = gcm.encrypt("hello world")
    authTag = gcm.final()
    assert b2h(authTag) == bytes('ac8fcee96dc6ef8e5236da19', 'utf-8')
    assert b2h(buf) == bytes('5ba7d42e1bf01d7998e932', "utf-8")
    gcmdec = AesGcmStream(key, iv)
    bufdec = gcmdec.decrypt(buf)
    gcmdec.final(authTag)
    assert bufdec == t2b("hello world")
コード例 #39
0
 def __init__(self, key="", size=32):
     self._native_object = _ffi.new(self._native_type)
     self._enc = None
     self._dec = None
     self._key = None
     if len(key) > 0:
         if not size in self._key_sizes:
             raise ValueError("Invalid key size %d" % size)
         self._key = t2b(key)
         self.key_size = size
     self._IV_nonce = []
     self._IV_counter = 0
コード例 #40
0
ファイル: ciphers.py プロジェクト: dgarske/wolfssl
    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 = t2b("\0" * self.output_size)

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

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

        return ciphertext
コード例 #41
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 = t2b("\0" * self.output_size)

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

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

        return ciphertext
コード例 #42
0
def test_multipart():
    key = "fedcba9876543210"
    iv = "0123456789abcdef"
    gcm = AesGcmStream(key, iv)
    buf = gcm.encrypt("hello")
    buf += gcm.encrypt(" world")
    authTag = gcm.final()
    assert b2h(authTag) == bytes('ac8fcee96dc6ef8e5236da19b6197d2e', 'utf-8')
    assert b2h(buf) == bytes('5ba7d42e1bf01d7998e932', "utf-8")
    gcmdec = AesGcmStream(key, iv)
    bufdec = gcmdec.decrypt(buf[:5])
    bufdec += gcmdec.decrypt(buf[5:])
    gcmdec.final(authTag)
    assert bufdec == t2b("hello world")
コード例 #43
0
ファイル: test_ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
def test_rsa_encrypt_decrypt(rsa_private, rsa_public):
    plaintext = t2b("Everyone gets Friday off.")

    # normal usage, encrypt with public, decrypt with pirate
    ciphertext = rsa_public.encrypt(plaintext)

    assert 1024 / 8 == len(ciphertext) == rsa_public.output_size
    assert plaintext == rsa_private.decrypt(ciphertext)

    # private object holds both private and public info, so it can also encrypt
    # using the known public key.
    ciphertext = rsa_private.encrypt(plaintext)

    assert 1024 / 8 == len(ciphertext) == rsa_private.output_size
    assert plaintext == rsa_private.decrypt(ciphertext)
コード例 #44
0
ファイル: test_ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
def test_rsa_sign_verify(rsa_private, rsa_public):
    plaintext = t2b("Everyone gets Friday off.")

    # normal usage, sign with private, verify with public
    signature = rsa_private.sign(plaintext)

    assert 1024 / 8 == len(signature) == rsa_private.output_size
    assert plaintext == rsa_public.verify(signature)

    # private object holds both private and public info, so it can also verify
    # using the known public key.
    signature = rsa_private.sign(plaintext)

    assert 1024 / 8 == len(signature) == rsa_private.output_size
    assert plaintext == rsa_private.verify(signature)
コード例 #45
0
ファイル: ciphers.py プロジェクト: kaleb-himes/wolfssl
    def __init__(self, key):
        key = t2b(key)

        _Rsa.__init__(self)

        idx = _ffi.new("word32*")
        idx[0] = 0

        ret = _lib.wc_RsaPrivateKeyDecode(key, idx, self.native_object,len(key))
        if ret < 0:
            raise WolfCryptError("Invalid key error (%d)" % ret)

        self.output_size = _lib.wc_RsaEncryptSize(self.native_object)
        if self.output_size <= 0:
            raise WolfCryptError("Invalid key error (%d)" % self.output_size)
コード例 #46
0
    def test_rsa_pkcs8_encrypt_decrypt(rsa_private_pkcs8, rsa_public):
        plaintext = t2b("Everyone gets Friday off.")

        # normal usage, encrypt with public, decrypt with private
        ciphertext = rsa_public.encrypt(plaintext)

        assert 1024 / 8 == len(ciphertext) == rsa_public.output_size
        assert plaintext == rsa_private_pkcs8.decrypt(ciphertext)

        # private object holds both private and public info, so it can also encrypt
        # using the known public key.
        ciphertext = rsa_private_pkcs8.encrypt(plaintext)

        assert 1024 / 8 == len(ciphertext) == rsa_private_pkcs8.output_size
        assert plaintext == rsa_private_pkcs8.decrypt(ciphertext)
コード例 #47
0
        def test_rsa_pss_sign_verify(rsa_private_pss, rsa_public_pss):
            plaintext = t2b("Everyone gets Friday off yippee.")

            # normal usage, sign with private, verify with public
            signature = rsa_private_pss.sign_pss(plaintext)

            assert 1024 / 8 == len(signature) == rsa_private_pss.output_size
            assert 0 == rsa_public_pss.verify_pss(plaintext, signature)

            # private object holds both private and public info, so it can also verify
            # using the known public key.
            signature = rsa_private_pss.sign_pss(plaintext)

            assert 1024 / 8 == len(signature) == rsa_private_pss.output_size
            assert 0 == rsa_private_pss.verify_pss(plaintext, signature)
コード例 #48
0
    def test_rsa_pkcs8_sign_verify(rsa_private_pkcs8, rsa_public):
        plaintext = t2b("Everyone gets Friday off.")

        # normal usage, sign with private, verify with public
        signature = rsa_private_pkcs8.sign(plaintext)

        assert 1024 / 8 == len(signature) == rsa_private_pkcs8.output_size
        assert plaintext == rsa_public.verify(signature)

        # private object holds both private and public info, so it can also verify
        # using the known public key.
        signature = rsa_private_pkcs8.sign(plaintext)

        assert 1024 / 8 == len(signature) == rsa_private_pkcs8.output_size
        assert plaintext == rsa_private_pkcs8.verify(signature)
コード例 #49
0
class TestDes3(unittest.TestCase):
    key = h2b("0123456789abcdeffedeba987654321089abcdef01234567")
    IV = h2b("1234567890abcdef")
    plain = t2b("Now is the time for all ")
    cipher = h2b("43a0297ed184f80e8964843212d508981894157487127db0")

    def setUp(self):
        self.des3 = Des3.new(self.key, MODE_CBC, self.IV)

    def test_raises(self):
        # invalid key length
        self.assertRaises(ValueError, Des3.new, "key", MODE_CBC, self.IV)

        # invalid mode
        self.assertRaises(ValueError, Des3.new, self.key, MODE_ECB, self.IV)

        # invalid iv length
        self.assertRaises(ValueError, Des3.new, self.key, MODE_CBC, "IV")

        # invalid data length
        self.assertRaises(ValueError, self.des3.encrypt, "foo")
        self.assertRaises(ValueError, self.des3.decrypt, "bar")

    def test_single_encryption(self):
        assert self.des3.encrypt(self.plain) == self.cipher

    def test_multi_encryption(self):
        result = t2b("")
        segments = tuple(self.plain[i:i + Des3.block_size] \
            for i in range(0, len(self.plain), Des3.block_size))

        for segment in segments:
            result += self.des3.encrypt(segment)

        assert result == self.cipher

    def test_single_decryption(self):
        assert self.des3.decrypt(self.cipher) == self.plain

    def test_multi_decryption(self):
        result = t2b("")
        segments = tuple(self.cipher[i:i + Des3.block_size] \
            for i in range(0, len(self.cipher), Des3.block_size))

        for segment in segments:
            result += self.des3.decrypt(segment)

        assert result == self.plain
コード例 #50
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    def __init__(self, key):  # pylint: disable=super-init-not-called
        key = t2b(key)

        _Rsa.__init__(self)  # pylint: disable=non-parent-init-called

        idx = _ffi.new("word32*")
        idx[0] = 0

        ret = _lib.wc_RsaPrivateKeyDecode(key, idx,
                                          self.native_object, len(key))
        if ret < 0:
            raise WolfCryptError("Invalid key error (%d)" % ret)

        self.output_size = _lib.wc_RsaEncryptSize(self.native_object)
        if self.output_size <= 0:  # pragma: no cover
            raise WolfCryptError("Invalid key error (%d)" % self.output_size)
コード例 #51
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    def verify(self, signature, data):
        """
        Verifies **signature**, using the public key data in the object.

        Returns **True** in case of a valid signature, otherwise **False**.
        """
        data = t2b(data)
        status = _ffi.new("int[1]")

        ret = _lib.wc_ed25519_verify_msg(signature, len(signature),
                                      data, len(data),
                                      status, self.native_object)

        if ret < 0:
            raise WolfCryptError("Verify error (%d)" % ret)

        return status[0] == 1
コード例 #52
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    def decode_key(self, key):
        """
        Decodes an ECC private key from an ASN sequence.
        """
        key = t2b(key)

        idx = _ffi.new("word32*")
        idx[0] = 0

        ret = _lib.wc_EccPrivateKeyDecode(key, idx,
                                          self.native_object, len(key))
        if ret < 0:
            raise WolfCryptError("Key decode error (%d)" % ret)
        if self.size <= 0:  # pragma: no cover
            raise WolfCryptError("Key decode error (%d)" % self.size)
        if self.max_signature_size <= 0:  # pragma: no cover
            raise WolfCryptError(
                "Key decode error (%d)" % self.max_signature_size)
コード例 #53
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    def decode_key(self, key):
        """
        Decodes an ED25519 public key
        """
        key = t2b(key)
        if (len(key) < _lib.wc_ed25519_pub_size(self.native_object)):
            raise WolfCryptError("Key decode error: key too short")

        idx = _ffi.new("word32*")
        idx[0] = 0
        ret = _lib.wc_ed25519_import_public(key, len(key), self.native_object)
        if ret < 0:
            raise WolfCryptError("Key decode error (%d)" % ret)
        if self.size <= 0:  # pragma: no cover
            raise WolfCryptError("Key decode error (%d)" % self.size)
        if self.max_signature_size <= 0:  # pragma: no cover
            raise WolfCryptError(
                "Key decode error (%d)" % self.max_signature_size)
コード例 #54
0
ファイル: hashes.py プロジェクト: kaleb-himes/wolfssl
    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 = t2b("\0" * 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:
                raise WolfCryptError("Hash finalize error (%d)" % ret)

        return result
コード例 #55
0
ファイル: test_hashes.py プロジェクト: wolfSSL/wolfcrypt-py
def vectors():
    TestVector = namedtuple("TestVector", "digest")
    TestVector.__new__.__defaults__ = (None,) * len(TestVector._fields)

    return {
        Sha: TestVector(
            digest=t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d")
        ),
        Sha256: TestVector(
            digest=t2b("96e02e7b1cbcd6f104fe1fdb4652027a" +
                       "5505b68652b70095c6318f9dce0d1844")
        ),
        Sha384: TestVector(
            digest=t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" +
                       "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b")
        ),
        Sha512: TestVector(
            digest=t2b("88fcf67ffd8558d713f9cedcd852db47" +
                       "9e6573f0bd9955610a993f609637553c" +
                       "e8fff55e644ee8a106aae19c07f91b3f" +
                       "2a2a6d40dfa7302c0fa6a1a9a5bfa03f")
        ),
        HmacSha: TestVector(
            digest=t2b("5dfabcfb3a25540824867cd21f065f52f73491e0")
        ),
        HmacSha256: TestVector(
            digest=t2b("4b641d721493d80f019d9447830ebfee" +
                       "89234a7d594378b89f8bb73873576bf6")
        ),
        HmacSha384: TestVector(
            digest=t2b("e72c72070c9c5c78e3286593068a510c1740cdf9dc34b512" +
                       "ccec97320295db1fe673216b46fe72e81f399a9ec04780ab")
        ),
        HmacSha512: TestVector(
            digest=t2b("c7f48db79314fc2b5be9a93fd58601a1" +
                       "bf42f397ec7f66dba034d44503890e6b" +
                       "5708242dcd71a248a78162d815c685f6" +
                       "038a4ac8cb34b8bf18986dbd300c9b41")
        ),
    }
コード例 #56
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    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])[:]
コード例 #57
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    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)[:]
コード例 #58
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    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)[:]
コード例 #59
0
ファイル: ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
    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)[:]
コード例 #60
0
ファイル: test_ciphers.py プロジェクト: wolfSSL/wolfcrypt-py
def vectors():
    TestVector = namedtuple("TestVector", "key iv plaintext ciphertext raw_key")
    TestVector.__new__.__defaults__ = (None,) * len(TestVector._fields)

    return {
        Aes: TestVector(
            key="0123456789abcdef",
            iv="1234567890abcdef",
            plaintext=t2b("now is the time "),
            ciphertext=h2b("959492575f4281532ccc9d4677a233cb")
        ),
        Des3: TestVector(
            key=h2b("0123456789abcdeffedeba987654321089abcdef01234567"),
            iv=h2b("1234567890abcdef"),
            plaintext=t2b("Now is the time for all "),
            ciphertext=h2b("43a0297ed184f80e8964843212d508981894157487127db0")
        ),
        RsaPublic: TestVector(
            key=h2b(
                "30819F300D06092A864886F70D010101050003818D0030818902818100BC"
                "730EA849F374A2A9EF18A5DA559921F9C8ECB36D48E53535757737ECD161"
                "905F3ED9E4D5DF94CAC1A9D719DA86C9E84DC4613682FEABAD7E7725BB8D"
                "11A5BC623AA838CC39A20466B4F7F7F3AADA4D020EBB5E8D6948DC77C928"
                "0E22E96BA426BA4CE8C1FD4A6F2B1FEF8AAEF69062E5641EEB2B3C67C8DC"
                "2700F6916865A90203010001")
        ),
        RsaPrivate: TestVector(
            key=h2b(
                "3082025C02010002818100BC730EA849F374A2A9EF18A5DA559921F9C8EC"
                "B36D48E53535757737ECD161905F3ED9E4D5DF94CAC1A9D719DA86C9E84D"
                "C4613682FEABAD7E7725BB8D11A5BC623AA838CC39A20466B4F7F7F3AADA"
                "4D020EBB5E8D6948DC77C9280E22E96BA426BA4CE8C1FD4A6F2B1FEF8AAE"
                "F69062E5641EEB2B3C67C8DC2700F6916865A902030100010281801397EA"
                "E8387825A25C04CE0D407C31E5C470CD9B823B5809863B665FDC3190F14F"
                "D5DB15DDDED73B95933118310E5EA3D6A21A716E81481C4BCFDB8E7A8661"
                "32DCFB55C1166D279224458BF1B848B14B1DACDEDADD8E2FC291FBA5A96E"
                "F83A6AF1FD5018EF9FE7C3CA78EA56D3D3725B96DD4E064E3AC3D9BE72B6"
                "6507074C01024100FA47D47A7C923C55EF81F041302DA3CF8F1CE6872705"
                "700DDF9835D6F18B382F24B5D084B6794F7129945AF0646AACE772C6ED4D"
                "59983E673AF3742CF9611769024100C0C1820D0CEBC62FDC92F99D821A31"
                "E9E9F74BF282871CEE166AD11D188270F3C0B62FF6F3F71DF18623C84EEB"
                "8F568E8FF5BFF1F72BB5CC3DC657390C1B54410241009D7E05DEEDF4B7B2"
                "FBFC304B551DE32F0147966905CD0E2E2CBD8363B6AB7CB76DCA5B64A7CE"
                "BE86DF3B53DE61D21EEBA5F637EDACAB78D94CE755FBD71199C102401898"
                "1829E61E2739702168AC0A2FA172C121869538C65890A0579CBAE3A7B115"
                "C8DEF61BC2612376EFB09D1C44BE1343396717C89DCAFBF545648B38822C"
                "F28102403989E59C195530BAB7488C48140EF49F7E779743E1B419353123"
                "759C3B44AD691256EE0061641666D37C742B15B4A2FEBF086B1A5D3F9012"
                "B105863129DBD9E2")
        ),
        EccPublic: TestVector(
            key=h2b(
                "3059301306072A8648CE3D020106082A8648CE3D0301070342000455BFF4"
                "0F44509A3DCE9BB7F0C54DF5707BD4EC248E1980EC5A4CA22403622C9BDA"
                "EFA2351243847616C6569506CC01A9BDF6751A42F7BDA9B236225FC75D7F"
                "B4"
            ),
            raw_key=h2b(
                "55bff40f44509a3dce9bb7f0c54df5707bd4ec248e1980ec5a4ca22403622c9b"
                "daefa2351243847616c6569506cc01a9bdf6751a42f7bda9b236225fc75d7fb4"
            )
        ),
        EccPrivate: TestVector(
            key=h2b(
                "30770201010420F8CF926BBD1E28F1A8ABA1234F3274188850AD7EC7EC92"
                "F88F974DAF568965C7A00A06082A8648CE3D030107A1440342000455BFF4"
                "0F44509A3DCE9BB7F0C54DF5707BD4EC248E1980EC5A4CA22403622C9BDA"
                "EFA2351243847616C6569506CC01A9BDF6751A42F7BDA9B236225FC75D7F"
                "B4"
            ),
            raw_key=h2b(
                "55bff40f44509a3dce9bb7f0c54df5707bd4ec248e1980ec5a4ca22403622c9b"
                "daefa2351243847616c6569506cc01a9bdf6751a42f7bda9b236225fc75d7fb4"
                "f8cf926bbd1e28f1a8aba1234f3274188850ad7ec7ec92f88f974daf568965c7"
            )
        ),
        Ed25519Private: TestVector(
             key = h2b(
                 "47CD22B276161AA18BA1E0D13DBE84FE4840E4395D784F555A92E8CF739B"
                 "F86B"
            )
        ),
        Ed25519Public: TestVector(
            key=h2b(
                "8498C65F4841145F9C51E8BFF4504B5527E0D5753964B7CB3C707A2B9747"
                "FC96"
            )
        )
    }