@@ -432,27 +432,29 @@ class ChaCha20Poly1305(object):
432432 """
433433 block_size = 16
434434 _key_sizes = [16 , 24 , 32 ]
435- _native_type = "ChaCha *"
436- _aad = bytes ()
435+ _native_type = "ChaChaPoly_Aead *"
436+ _aad = None
437437 _tag_bytes = 16
438438 _mode = None
439- generatedCipherText = bytes ()
440- generatedPlainText = bytes ()
439+ _key = bytes ()
440+ _IV = bytes ()
441441
442- def __init__ (self , key , IV , isEncrypt ):
442+ def __init__ (self , key , IV , aad , tag_bytes = 16 ):
443443 """
444444 tag_bytes is the number of bytes to use for the authentication tag during encryption
445445 """
446- key = t2b (key )
447- IV = t2b (IV )
448- isEncrypt = True
446+ #key = t2b(key)
447+ #IV = t2b(IV)
448+ #aad = t2b(aad)
449+ self ._key = key
450+ self ._IV = IV
451+ self ._aad = aad
449452 if len (key ) not in self ._key_sizes :
450453 raise ValueError ("key must be %s in length, not %d" %
451454 (self ._key_sizes , len (key )))
452455 self ._native_object = _ffi .new (self ._native_type )
453- _lib .wc_AesInit (self ._native_object , _ffi .NULL , - 2 )
454- ret = _lib .wc_ChaCha20Poly1305_Init (self ._native_object , self ._aad , len (self ._aad ),
455- key , len (key ), IV , len (IV ), isEncrypt )
456+ self ._mode = None
457+ ret = _lib .wc_ChaCha20Poly1305_Init (self ._native_object , key , IV , 1 )
456458 if ret < 0 :
457459 raise WolfCryptError ("Init error (%d)" % ret )
458460
@@ -467,41 +469,45 @@ def set_aad(self, data):
467469 def get_aad (self ):
468470 return self ._aad
469471
470- def encrypt (self , data ):
472+ def encrypt (self , inPlainText ):
471473 """
472474 Add more data to the encryption stream
473475 """
474- data = t2b ( data )
475- aad = bytes ( )
476+
477+ #inPlainText = t2b(inPlainText )
476478 if self ._mode is None :
477479 self ._mode = _ENCRYPTION
478480 aad = self ._aad
479481 elif self ._mode == _DECRYPTION :
480482 raise WolfCryptError ("Class instance already in use for decryption" )
481- self ._buf = _ffi .new ("byte[%d]" % (len (data )))
482- ret = _lib .wc_ChaCha20Poly1305_UpdateData (self ._native_type , aad , len (aad ))
483+ outGeneratedCipherText = _ffi .new ("byte[%d]" % (len (inPlainText ))) #array of output data (inPlainText) in bytes
484+ outGeneratedAuthTag = _ffi .new ("byte[%d]" % self ._tag_bytes )
485+ ret = _lib .wc_ChaCha20Poly1305_Encrypt (self ._key , self ._IV , aad , len (aad ),
486+ inPlainText , len (inPlainText ),
487+ outGeneratedCipherText ,
488+ outGeneratedAuthTag ) #outputs are generatedCipherText and generatedAuthTag
489+
483490 if ret < 0 :
484491 raise WolfCryptError ("Decryption error (%d)" % ret )
485- return bytes (self . _buf )
492+ return bytes (outGeneratedCipherText ), bytes ( outGeneratedAuthTag )
486493
487- def decrypt (self , data ):
494+ def decrypt (self , inGeneratedAuthTag , inGeneratedCipher ): #plain text is the output and should be hello world
488495 """
489496 Add more data to the decryption stream
490497 """
491- aad = bytes ()
492- data = t2b (data )
498+ inGeneratedCipher = t2b (inGeneratedCipher ) #Should be the chipher from encrypt
493499 if self ._mode is None :
494500 self ._mode = _DECRYPTION
495501 aad = self ._aad
496502 elif self ._mode == _ENCRYPTION :
497503 raise WolfCryptError ("Class instance already in use for decryption" )
498- self . _buf = _ffi .new ("byte[%d]" % (len (data )))
499- ret = _lib .wc_ChaCha20Poly1305_Decrypt (self ._key , self_IV , aad , len (aad ),
500- generatedCipherText , len (generatedCipherText ),
501- authTag , generatedPlainText )
504+ outPlainText = _ffi .new ("byte[%d]" % (len (inGeneratedCipher )))#unsure what to put here
505+ ret = _lib .wc_ChaCha20Poly1305_Decrypt (self ._key , self . _IV , aad , len (self . _aad ),
506+ inGeneratedCipher , len (inGeneratedCipher ),
507+ inGeneratedAuthTag , outPlainText )
502508 if ret < 0 :
503509 raise WolfCryptError ("Decryption error (%d)" % ret )
504- return bytes (self . _buf )
510+ return bytes (outPlainText ) # prettysure outplain text is the output
505511
506512 def checkTag (self , authTag ):
507513 """
@@ -530,6 +536,7 @@ def final(self, authTag=None):
530536 if authTag is None :
531537 raise WolfCryptError ("authTag parameter required" )
532538 authTag = t2b (authTag )
539+ self ._native_object = _ffi .new (self ._native_type )
533540 ret = _lib .wc_ChaCha20Poly1305_Final (self ._native_type , authTag )
534541 if ret < 0 :
535542 raise WolfCryptError ("Decryption error (%d)" % ret )
0 commit comments