fixes python3 issues

gh-pages
Moisés Guimarães 2016-04-30 23:14:19 -03:00
parent 0ea89208db
commit 647f65c0cc
8 changed files with 150 additions and 306 deletions

View File

@ -25,7 +25,7 @@ A Python wrapper that encapsulates wolfSSL's wolfCrypt API
$ python setup.py install
...
Finished processing dependencies for wolfcrypt==0.1
Finished processing dependencies for wolfcrypt==0.1.0
4. Test locally with ``tox``::

View File

@ -19,12 +19,13 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import unittest
from wolfcrypt.ciphers import *
from wolfcrypt.utils import _t2b, _h2b
class TestDes3(unittest.TestCase):
key = "0123456789abcdeffedeba987654321089abcdef01234567".decode("hex")
IV = "1234567890abcdef".decode("hex")
plain = "Now is the time for all "
cipher = "43a0297ed184f80e8964843212d508981894157487127db0".decode("hex")
key = _h2b("0123456789abcdeffedeba987654321089abcdef01234567")
IV = _h2b("1234567890abcdef")
plain = _t2b("Now is the time for all ")
cipher = _h2b("43a0297ed184f80e8964843212d508981894157487127db0")
def setUp(self):
@ -51,7 +52,7 @@ class TestDes3(unittest.TestCase):
def test_multi_encryption(self):
result = ""
result = _t2b("")
segments = tuple(self.plain[i:i + Des3.block_size] \
for i in range(0, len(self.plain), Des3.block_size))
@ -66,7 +67,7 @@ class TestDes3(unittest.TestCase):
def test_multi_decryption(self):
result = ""
result = _t2b("")
segments = tuple(self.cipher[i:i + Des3.block_size] \
for i in range(0, len(self.cipher), Des3.block_size))
@ -79,8 +80,8 @@ class TestDes3(unittest.TestCase):
class TestAes(unittest.TestCase):
key = "0123456789abcdef"
IV = "1234567890abcdef"
plain = "now is the time "
cipher = "959492575f4281532ccc9d4677a233cb".decode("hex")
plain = _t2b("now is the time ")
cipher = _h2b("959492575f4281532ccc9d4677a233cb")
def setUp(self):
@ -107,7 +108,7 @@ class TestAes(unittest.TestCase):
def test_multi_encryption(self):
result = ""
result = _t2b("")
segments = tuple(self.plain[i:i + self.aes.block_size] \
for i in range(0, len(self.plain), self.aes.block_size))
@ -122,7 +123,7 @@ class TestAes(unittest.TestCase):
def test_multi_decryption(self):
result = ""
result = _t2b("")
segments = tuple(self.cipher[i:i + self.aes.block_size] \
for i in range(0, len(self.cipher), self.aes.block_size))
@ -153,11 +154,11 @@ class TestRsaPrivate(unittest.TestCase):
+ "3989E59C195530BAB7488C48140EF49F7E779743E1B419353123759C3B44AD69" \
+ "1256EE0061641666D37C742B15B4A2FEBF086B1A5D3F9012B105863129DBD9E2"
plain = "Everyone gets Friday off."
plain = _t2b("Everyone gets Friday off.")
def setUp(self):
self.rsa = RsaPrivate(self.key.decode("hex"))
self.rsa = RsaPrivate(_h2b(self.key))
def test_raises(self):
@ -212,12 +213,12 @@ class TestRsaPublic(unittest.TestCase):
+ "38CC39A20466B4F7F7F3AADA4D020EBB5E8D6948DC77C9280E22E96BA426BA4C" \
+ "E8C1FD4A6F2B1FEF8AAEF69062E5641EEB2B3C67C8DC2700F6916865A90203010001"
plain = "Everyone gets Friday off."
plain = _t2b("Everyone gets Friday off.")
def setUp(self):
self.private = RsaPrivate(self.prv.decode("hex"))
self.public = RsaPublic(self.pub.decode("hex"))
self.private = RsaPrivate(_h2b(self.prv))
self.public = RsaPublic(_h2b(self.pub))
def test_raises(self):

View File

@ -19,26 +19,28 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import unittest
from wolfcrypt.hashes import *
from wolfcrypt.utils import _t2b, _h2b
class TestSha(unittest.TestCase):
digest = "1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d"
_class = Sha
digest = _t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d")
def setUp(self):
self.hash = Sha()
self.hash = self._class()
def test_new(self):
# update inside constructor
assert Sha.new("wolfcrypt").hexdigest() == self.digest
assert self._class.new("wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
assert self.hash.digest() == _h2b(self.digest)
def test_hash_update_002(self):
@ -46,7 +48,7 @@ class TestSha(unittest.TestCase):
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
assert self.hash.digest() == _h2b(self.digest)
def test_hash_copy(self):
@ -63,155 +65,47 @@ class TestSha(unittest.TestCase):
assert self.hash.hexdigest() == copy.hexdigest() == self.digest
class TestSha256(unittest.TestCase):
digest = "96e02e7b1cbcd6f104fe1fdb4652027a5505b68652b70095c6318f9dce0d1844"
class TestSha256(TestSha):
_class = Sha256
digest = _t2b("96e02e7b1cbcd6f104fe1fdb4652027a" \
+ "5505b68652b70095c6318f9dce0d1844")
def setUp(self):
self.hash = Sha256()
class TestSha384(TestSha):
_class = Sha384
digest = _t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" \
+ "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b")
def test_new(self):
# update inside constructor
assert Sha256.new("wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
self.hash.update("wolf")
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
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
class TestSha384(unittest.TestCase):
digest = "4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" \
+ "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b"
def setUp(self):
self.hash = Sha384()
def test_new(self):
# update inside constructor
assert Sha384.new("wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
self.hash.update("wolf")
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
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
class TestSha512(unittest.TestCase):
digest = "88fcf67ffd8558d713f9cedcd852db479e6573f0bd9955610a993f609637553c"\
+ "e8fff55e644ee8a106aae19c07f91b3f2a2a6d40dfa7302c0fa6a1a9a5bfa03f"
def setUp(self):
self.hash = Sha512()
def test_new(self):
# update inside constructor
assert Sha512.new("wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
self.hash.update("wolf")
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
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
class TestSha512(TestSha):
_class = Sha512
digest = _t2b("88fcf67ffd8558d713f9cedcd852db47" \
+ "9e6573f0bd9955610a993f609637553c" \
+ "e8fff55e644ee8a106aae19c07f91b3f" \
+ "2a2a6d40dfa7302c0fa6a1a9a5bfa03f")
_HMAC_KEY = "python"
class TestHmacSha(unittest.TestCase):
digest = "5dfabcfb3a25540824867cd21f065f52f73491e0"
_class = HmacSha
digest = _t2b("5dfabcfb3a25540824867cd21f065f52f73491e0")
def setUp(self):
self.hash = HmacSha(_HMAC_KEY)
self.hash = self._class(_HMAC_KEY)
def test_new(self):
# update inside constructor
assert HmacSha.new(_HMAC_KEY, "wolfcrypt").hexdigest() == self.digest
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
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
@ -219,7 +113,6 @@ class TestHmacSha(unittest.TestCase):
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_copy(self):
@ -236,129 +129,21 @@ class TestHmacSha(unittest.TestCase):
assert self.hash.hexdigest() == copy.hexdigest() == self.digest
class TestHmacSha256(unittest.TestCase):
digest = "4b641d721493d80f019d9447830ebfee89234a7d594378b89f8bb73873576bf6"
class TestHmacSha256(TestHmacSha):
_class = HmacSha256
digest = _t2b("4b641d721493d80f019d9447830ebfee" \
+ "89234a7d594378b89f8bb73873576bf6")
def setUp(self):
self.hash = HmacSha256(_HMAC_KEY)
class TestHmacSha384(TestHmacSha):
_class = HmacSha384
digest = _t2b("e72c72070c9c5c78e3286593068a510c1740cdf9dc34b512" \
+ "ccec97320295db1fe673216b46fe72e81f399a9ec04780ab")
def test_new(self):
# update inside constructor
assert HmacSha256.new(_HMAC_KEY, "wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
self.hash.update("wolf")
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
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
class TestHmacSha384(unittest.TestCase):
digest = "e72c72070c9c5c78e3286593068a510c1740cdf9dc34b512" \
+ "ccec97320295db1fe673216b46fe72e81f399a9ec04780ab"
def setUp(self):
self.hash = HmacSha384(_HMAC_KEY)
def test_new(self):
# update inside constructor
assert HmacSha384.new(_HMAC_KEY, "wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
self.hash.update("wolf")
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
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
class TestHmacSha512(unittest.TestCase):
digest = "c7f48db79314fc2b5be9a93fd58601a1bf42f397ec7f66dba034d44503890e6b"\
+ "5708242dcd71a248a78162d815c685f6038a4ac8cb34b8bf18986dbd300c9b41"
def setUp(self):
self.hash = HmacSha512(_HMAC_KEY)
def test_new(self):
# update inside constructor
assert HmacSha512.new(_HMAC_KEY, "wolfcrypt").hexdigest() == self.digest
def test_hash_update_001(self):
self.hash.update("wolfcrypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
def test_hash_update_002(self):
self.hash.update("wolf")
self.hash.update("crypt")
assert self.hash.hexdigest() == self.digest
assert self.hash.digest() == self.digest.decode("hex")
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
class TestHmacSha512(TestHmacSha):
_class = HmacSha512
digest = _t2b("c7f48db79314fc2b5be9a93fd58601a1" \
+ "bf42f397ec7f66dba034d44503890e6b" \
+ "5708242dcd71a248a78162d815c685f6" \
+ "038a4ac8cb34b8bf18986dbd300c9b41")

View File

@ -1,5 +1,5 @@
[tox]
envlist=py27
envlist=py26,py27,py35
[testenv]
deps=-rrequirements-testing.txt

View File

@ -17,8 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
from wolfcrypt._ffi import ffi as _ffi
from wolfcrypt._ffi import lib as _lib
from wolfcrypt._ffi import ffi as _ffi
from wolfcrypt._ffi import lib as _lib
from wolfcrypt.utils import _t2b
from wolfcrypt.random import Random
@ -61,8 +62,12 @@ class _Cipher(object):
self._native_object = _ffi.new(self._native_type)
self._enc = None
self._dec = None
self._key = key
self._IV = IV if IV else "\0" * self.block_size
self._key = _t2b(key)
if IV:
self._IV = _t2b(IV)
else:
self._IV = _t2b("\0" * self.block_size)
@classmethod
@ -72,6 +77,8 @@ class _Cipher(object):
def encrypt(self, string):
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)
@ -80,13 +87,15 @@ class _Cipher(object):
self._enc = _ffi.new(self._native_type)
self._set_key(_ENCRYPTION)
result = "\0" * len(string)
result = _t2b("\0" * len(string))
self._encrypt(result, string)
return result
def decrypt(self, string):
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)
@ -95,7 +104,7 @@ class _Cipher(object):
self._dec = _ffi.new(self._native_type)
self._set_key(_DECRYPTION)
result = "\0" * len(string)
result = _t2b("\0" * len(string))
self._decrypt(result, string)
return result
@ -162,6 +171,8 @@ class _Rsa(object):
class RsaPublic(_Rsa):
def __init__(self, key):
key = _t2b(key)
_Rsa.__init__(self)
idx = _ffi.new("word32*")
@ -177,7 +188,8 @@ class RsaPublic(_Rsa):
def encrypt(self, plaintext):
ciphertext = "\0" * self.output_size
plaintext = _t2b(plaintext)
ciphertext = _t2b("\0" * self.output_size)
ret = _lib.wc_RsaPublicEncrypt(plaintext, len(plaintext),
ciphertext, len(ciphertext),
@ -191,7 +203,8 @@ class RsaPublic(_Rsa):
def verify(self, signature):
plaintext = "\0" * self.output_size
signature = _t2b(signature)
plaintext = _t2b("\0" * self.output_size)
ret = _lib.wc_RsaSSL_Verify(signature, len(signature),
plaintext, len(plaintext),
@ -205,6 +218,8 @@ class RsaPublic(_Rsa):
class RsaPrivate(RsaPublic):
def __init__(self, key):
key = _t2b(key)
_Rsa.__init__(self)
idx = _ffi.new("word32*")
@ -217,7 +232,8 @@ class RsaPrivate(RsaPublic):
def decrypt(self, ciphertext):
plaintext = "\0" * self.output_size
ciphertext = _t2b(ciphertext)
plaintext = _t2b("\0" * self.output_size)
ret = _lib.wc_RsaPrivateDecrypt(ciphertext, len(ciphertext),
plaintext, len(plaintext),
@ -230,7 +246,8 @@ class RsaPrivate(RsaPublic):
def sign(self, plaintext):
signature = "\0" * self.output_size
plaintext = _t2b(plaintext)
signature = _t2b("\0" * self.output_size)
ret = _lib.wc_RsaSSL_Sign(plaintext, len(plaintext),
signature, len(signature),

View File

@ -17,8 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
from wolfcrypt._ffi import ffi as _ffi
from wolfcrypt._ffi import lib as _lib
from wolfcrypt._ffi import ffi as _ffi
from wolfcrypt._ffi import lib as _lib
from wolfcrypt.utils import _t2b, _b2h
class _Hash(object):
@ -33,7 +34,7 @@ class _Hash(object):
self = cls(string)
if (string):
self._update(string)
self.update(string)
return self
@ -50,11 +51,13 @@ class _Hash(object):
def update(self, string):
string = _t2b(string)
self._update(string)
def digest(self):
ret = "\0" * self.digest_size
ret = _t2b("\0" * self.digest_size)
if self._native_object:
obj = _ffi.new(self._native_type)
@ -67,7 +70,7 @@ class _Hash(object):
def hexdigest(self):
return self.digest().encode("hex")
return _b2h(self.digest())
class Sha(_Hash):
@ -77,15 +80,15 @@ class Sha(_Hash):
def _init(self):
_lib.wc_InitSha(self._native_object)
return _lib.wc_InitSha(self._native_object)
def _update(self, data):
_lib.wc_ShaUpdate(self._native_object, data, len(data))
return _lib.wc_ShaUpdate(self._native_object, data, len(data))
def _final(self, obj, ret):
_lib.wc_ShaFinal(obj, ret)
return _lib.wc_ShaFinal(obj, ret)
class Sha256(_Hash):
@ -95,15 +98,15 @@ class Sha256(_Hash):
def _init(self):
_lib.wc_InitSha256(self._native_object)
return _lib.wc_InitSha256(self._native_object)
def _update(self, data):
_lib.wc_Sha256Update(self._native_object, data, len(data))
return _lib.wc_Sha256Update(self._native_object, data, len(data))
def _final(self, obj, ret):
_lib.wc_Sha256Final(obj, ret)
return _lib.wc_Sha256Final(obj, ret)
class Sha384(_Hash):
@ -113,15 +116,15 @@ class Sha384(_Hash):
def _init(self):
_lib.wc_InitSha384(self._native_object)
return _lib.wc_InitSha384(self._native_object)
def _update(self, data):
_lib.wc_Sha384Update(self._native_object, data, len(data))
return _lib.wc_Sha384Update(self._native_object, data, len(data))
def _final(self, obj, ret):
_lib.wc_Sha384Final(obj, ret)
return _lib.wc_Sha384Final(obj, ret)
class Sha512(_Hash):
@ -131,15 +134,15 @@ class Sha512(_Hash):
def _init(self):
_lib.wc_InitSha512(self._native_object)
return _lib.wc_InitSha512(self._native_object)
def _update(self, data):
_lib.wc_Sha512Update(self._native_object, data, len(data))
return _lib.wc_Sha512Update(self._native_object, data, len(data))
def _final(self, obj, ret):
_lib.wc_Sha512Final(obj, ret)
return _lib.wc_Sha512Final(obj, ret)
# Hmac types
@ -158,6 +161,8 @@ class _Hmac(_Hash):
def __init__(self, key):
key = _t2b(key)
self._native_object = _ffi.new(self._native_type)
self._init(self._type, key)
@ -168,21 +173,21 @@ class _Hmac(_Hash):
self = cls(key)
if (string):
self._update(string)
self.update(string)
return self
def _init(self, type, key):
_lib.wc_HmacSetKey(self._native_object, type, key, len(key))
return _lib.wc_HmacSetKey(self._native_object, type, key, len(key))
def _update(self, data):
_lib.wc_HmacUpdate(self._native_object, data, len(data))
return _lib.wc_HmacUpdate(self._native_object, data, len(data))
def _final(self, obj, ret):
_lib.wc_HmacFinal(obj, ret)
return _lib.wc_HmacFinal(obj, ret)
class HmacSha(_Hmac):

View File

@ -17,8 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
from wolfcrypt._ffi import ffi as _ffi
from wolfcrypt._ffi import lib as _lib
from wolfcrypt._ffi import ffi as _ffi
from wolfcrypt._ffi import lib as _lib
from wolfcrypt.utils import _t2b
class Random(object):
@ -34,7 +35,7 @@ class Random(object):
def byte(self):
ret = "\0"
ret = _t2b("\0")
_lib.wc_RNG_GenerateByte(self.native_object, ret)
@ -42,7 +43,7 @@ class Random(object):
def bytes(self, length):
ret = "\0" * length
ret = _t2b("\0" * length)
_lib.wc_RNG_GenerateBlock(self.native_object, ret, length)

35
wolfcrypt/utils.py 100644
View File

@ -0,0 +1,35 @@
# utils.py
#
# Copyright (C) 2006-2016 wolfSSL Inc.
#
# This file is part of wolfSSL. (formerly known as CyaSSL)
#
# wolfSSL is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# wolfSSL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import sys
from binascii import hexlify as _b2h, unhexlify as _h2b
if sys.version_info[0] == 3:
_text_type = str
_binary_type = bytes
else:
_text_type = unicode
_binary_type = str
def _t2b(s):
if isinstance(s, _binary_type):
return s
return _text_type(s).encode("utf-8")