Skip to content

Commit e8b9da1

Browse files
committed
Fix t2b to support other types
1 parent 3c9cfed commit e8b9da1

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

wolfcrypt/ciphers.py

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -553,18 +553,20 @@ def __init__(self, key, IV, aad, tag_bytes=16):
553553
"""
554554
tag_bytes is the number of bytes to use for the authentication tag during encryption
555555
"""
556-
#key = t2b(key)
557-
#IV = t2b(IV)
558-
#aad = t2b(aad)
559-
self._key = key
560-
self._IV = IV
561-
self._aad = aad
562-
if len(key) not in self._key_sizes:
556+
self._key = t2b(key)
557+
self._IV = t2b(IV)
558+
self._aad = t2b(aad)
559+
if len(self._key) not in self._key_sizes:
563560
raise ValueError("key must be %s in length, not %d" %
564-
(self._key_sizes, len(key)))
561+
(self._key_sizes, len(self._key)))
565562
self._native_object = _ffi.new(self._native_type)
566563
self._mode = None
567-
ret = _lib.wc_ChaCha20Poly1305_Init(self._native_object, key, IV, 1)
564+
ret = _lib.wc_ChaCha20Poly1305_Init(
565+
self._native_object,
566+
_ffi.from_buffer(self._key),
567+
_ffi.from_buffer(self._IV),
568+
1
569+
)
568570
if ret < 0:
569571
raise WolfCryptError("Init error (%d)" % ret)
570572

@@ -583,41 +585,54 @@ def encrypt(self, inPlainText):
583585
"""
584586
Add more data to the encryption stream
585587
"""
586-
587-
#inPlainText = t2b(inPlainText)
588+
inPlainText = t2b(inPlainText)
588589
if self._mode is None:
589590
self._mode = _ENCRYPTION
590591
aad = self._aad
591592
elif self._mode == _DECRYPTION:
592593
raise WolfCryptError("Class instance already in use for decryption")
593-
outGeneratedCipherText = _ffi.new("byte[%d]" % (len(inPlainText))) #array of output data (inPlainText) in bytes
594+
outGeneratedCipherText = _ffi.new("byte[%d]" % (len(inPlainText)))
594595
outGeneratedAuthTag = _ffi.new("byte[%d]" % self._tag_bytes)
595-
ret = _lib.wc_ChaCha20Poly1305_Encrypt(self._key, self._IV, aad, len(aad),
596-
inPlainText, len(inPlainText),
597-
outGeneratedCipherText,
598-
outGeneratedAuthTag) #outputs are generatedCipherText and generatedAuthTag
596+
ret = _lib.wc_ChaCha20Poly1305_Encrypt(
597+
_ffi.from_buffer(self._key),
598+
_ffi.from_buffer(self._IV),
599+
_ffi.from_buffer(aad),
600+
len(aad),
601+
_ffi.from_buffer(inPlainText),
602+
len(inPlainText),
603+
outGeneratedCipherText,
604+
outGeneratedAuthTag
605+
)
599606

600607
if ret < 0:
601-
raise WolfCryptError("Decryption error (%d)" % ret)
608+
raise WolfCryptError("Encryption error (%d)" % ret)
602609
return bytes(outGeneratedCipherText), bytes(outGeneratedAuthTag)
603610

604-
def decrypt(self, inGeneratedAuthTag, inGeneratedCipher):#plain text is the output and should be hello world
611+
def decrypt(self, inGeneratedAuthTag, inGeneratedCipher):
605612
"""
606613
Add more data to the decryption stream
607614
"""
608-
inGeneratedCipher = t2b(inGeneratedCipher) #Should be the chipher from encrypt
615+
inGeneratedCipher = t2b(inGeneratedCipher)
616+
inGeneratedAuthTag = t2b(inGeneratedAuthTag)
609617
if self._mode is None:
610618
self._mode = _DECRYPTION
611619
aad = self._aad
612620
elif self._mode == _ENCRYPTION:
613621
raise WolfCryptError("Class instance already in use for decryption")
614-
outPlainText= _ffi.new("byte[%d]" % (len(inGeneratedCipher)))#unsure what to put here
615-
ret = _lib.wc_ChaCha20Poly1305_Decrypt(self._key, self._IV, aad, len(self._aad),
616-
inGeneratedCipher, len(inGeneratedCipher),
617-
inGeneratedAuthTag, outPlainText)
622+
outPlainText = _ffi.new("byte[%d]" % (len(inGeneratedCipher)))
623+
ret = _lib.wc_ChaCha20Poly1305_Decrypt(
624+
_ffi.from_buffer(self._key),
625+
_ffi.from_buffer(self._IV),
626+
_ffi.from_buffer(aad),
627+
len(aad),
628+
_ffi.from_buffer(inGeneratedCipher),
629+
len(inGeneratedCipher),
630+
_ffi.from_buffer(inGeneratedAuthTag),
631+
outPlainText
632+
)
618633
if ret < 0:
619634
raise WolfCryptError("Decryption error (%d)" % ret)
620-
return bytes(outPlainText) # prettysure outplain text is the output
635+
return bytes(outPlainText)
621636

622637
def checkTag(self, authTag):
623638
"""

wolfcrypt/utils.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@
1818
# along with this program; if not, write to the Free Software
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2020

21-
# pylint: disable=unused-import, undefined-variable
21+
# pylint: disable=unused-import
2222

23-
import sys
2423
from binascii import hexlify as b2h, unhexlify as h2b # noqa: F401
2524

2625

27-
_PY3 = sys.version_info[0] == 3
28-
_TEXT_TYPE = str if _PY3 else unicode # noqa: F821
29-
_BINARY_TYPE = bytes if _PY3 else str
30-
31-
3226
def t2b(string):
3327
"""
3428
Converts text to binary.
29+
30+
Passes through bytes, bytearray, and memoryview unchanged.
31+
Encodes str to UTF-8 bytes.
3532
"""
36-
if isinstance(string, _BINARY_TYPE):
33+
if isinstance(string, (bytes, bytearray, memoryview)):
3734
return string
38-
return _TEXT_TYPE(string).encode("utf-8")
35+
return str(string).encode("utf-8")

0 commit comments

Comments
 (0)