@@ -538,133 +538,78 @@ def set_iv(self, nonce, counter = 0):
538538if _lib .CHACHA20_POLY1305_ENABLED :
539539 class ChaCha20Poly1305 (object ):
540540 """
541- ChaCha20 Poly1305
541+ ChaCha20-Poly1305 AEAD cipher.
542+
543+ One-shot encrypt/decrypt interface (non-streaming).
542544 """
543- block_size = 16
544- _key_sizes = [16 , 24 , 32 ]
545- _native_type = "ChaChaPoly_Aead *"
546- _aad = None
545+ _key_sizes = [32 ]
547546 _tag_bytes = 16
548- _mode = None
549- _key = bytes ()
550- _IV = bytes ()
551547
552- def __init__ (self , key , IV , aad , tag_bytes = 16 ):
553- """
554- tag_bytes is the number of bytes to use for the authentication tag during encryption
555- """
548+ def __init__ (self , key ):
556549 self ._key = t2b (key )
557- self ._IV = t2b (IV )
558- self ._aad = t2b (aad )
559550 if len (self ._key ) not in self ._key_sizes :
560551 raise ValueError ("key must be %s in length, not %d" %
561552 (self ._key_sizes , len (self ._key )))
562- self ._native_object = _ffi .new (self ._native_type )
563- self ._mode = None
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- )
570- if ret < 0 :
571- raise WolfCryptError ("Init error (%d)" % ret )
572553
573- def set_aad (self , data ):
574- """
575- Set the additional authentication data for the stream
554+ def encrypt (self , aad , iv , plaintext ):
576555 """
577- if self ._mode is not None :
578- raise WolfCryptError ("AAD can only be set before encrypt() or decrypt() is called" )
579- self ._aad = t2b (data )
580-
581- def get_aad (self ):
582- return self ._aad
556+ Encrypt plaintext data using the IV/nonce provided. The
557+ associated data (aad) is not encrypted but is included in the
558+ authentication tag.
583559
584- def encrypt ( self , inPlainText ):
560+ Returns a tuple of (ciphertext, authTag).
585561 """
586- Add more data to the encryption stream
587- """
588- inPlainText = t2b (inPlainText )
589- if self ._mode is None :
590- self ._mode = _ENCRYPTION
591- aad = self ._aad
592- elif self ._mode == _DECRYPTION :
593- raise WolfCryptError ("Class instance already in use for decryption" )
594- outGeneratedCipherText = _ffi .new ("byte[%d]" % (len (inPlainText )))
595- outGeneratedAuthTag = _ffi .new ("byte[%d]" % self ._tag_bytes )
562+ aad = t2b (aad )
563+ iv = t2b (iv )
564+ if len (iv ) != 12 :
565+ raise ValueError ("iv must be 12 bytes, got %d" % len (iv ))
566+ plaintext = t2b (plaintext )
567+ ciphertext = _ffi .new ("byte[%d]" % len (plaintext ))
568+ authTag = _ffi .new ("byte[%d]" % self ._tag_bytes )
596569 ret = _lib .wc_ChaCha20Poly1305_Encrypt (
597570 _ffi .from_buffer (self ._key ),
598- _ffi .from_buffer (self . _IV ),
571+ _ffi .from_buffer (iv ),
599572 _ffi .from_buffer (aad ),
600573 len (aad ),
601- _ffi .from_buffer (inPlainText ),
602- len (inPlainText ),
603- outGeneratedCipherText ,
604- outGeneratedAuthTag
574+ _ffi .from_buffer (plaintext ),
575+ len (plaintext ),
576+ ciphertext ,
577+ authTag
605578 )
606-
607579 if ret < 0 :
608580 raise WolfCryptError ("Encryption error (%d)" % ret )
609- return bytes (outGeneratedCipherText ), bytes (outGeneratedAuthTag )
581+ return bytes (ciphertext ), bytes (authTag )
610582
611- def decrypt (self , inGeneratedAuthTag , inGeneratedCipher ):
583+ def decrypt (self , aad , iv , authTag , ciphertext ):
612584 """
613- Add more data to the decryption stream
585+ Decrypt the ciphertext using the IV/nonce and authentication tag
586+ provided. The integrity of the associated data (aad) is checked.
587+
588+ Returns the decrypted plaintext.
614589 """
615- inGeneratedCipher = t2b (inGeneratedCipher )
616- inGeneratedAuthTag = t2b (inGeneratedAuthTag )
617- if self ._mode is None :
618- self ._mode = _DECRYPTION
619- aad = self ._aad
620- elif self ._mode == _ENCRYPTION :
621- raise WolfCryptError ("Class instance already in use for decryption" )
622- outPlainText = _ffi .new ("byte[%d]" % (len (inGeneratedCipher )))
590+ aad = t2b (aad )
591+ iv = t2b (iv )
592+ if len (iv ) != 12 :
593+ raise ValueError ("iv must be 12 bytes, got %d" % len (iv ))
594+ authTag = t2b (authTag )
595+ if len (authTag ) != self ._tag_bytes :
596+ raise ValueError ("authTag must be %d bytes, got %d" %
597+ (self ._tag_bytes , len (authTag )))
598+ ciphertext = t2b (ciphertext )
599+ plaintext = _ffi .new ("byte[%d]" % len (ciphertext ))
623600 ret = _lib .wc_ChaCha20Poly1305_Decrypt (
624601 _ffi .from_buffer (self ._key ),
625- _ffi .from_buffer (self . _IV ),
602+ _ffi .from_buffer (iv ),
626603 _ffi .from_buffer (aad ),
627604 len (aad ),
628- _ffi .from_buffer (inGeneratedCipher ),
629- len (inGeneratedCipher ),
630- _ffi .from_buffer (inGeneratedAuthTag ),
631- outPlainText
605+ _ffi .from_buffer (ciphertext ),
606+ len (ciphertext ),
607+ _ffi .from_buffer (authTag ),
608+ plaintext
632609 )
633610 if ret < 0 :
634611 raise WolfCryptError ("Decryption error (%d)" % ret )
635- return bytes (outPlainText )
636-
637- def checkTag (self , authTag ):
638- """
639- Check the authentication tag for the stream
640- """
641- authTag = t2b (authTag )
642- ret = _lib .wc_ChaCha20Poly1305_CheckTag (authTag , len (authTag ))
643- if ret < 0 :
644- raise WolfCryptError ("Decryption error (%d)" % ret )
645-
646- def final (self , authTag = None ):
647- """
648- When encrypting, finalize the stream and return an authentication tag for the stream.
649- When decrypting, verify the authentication tag for the stream.
650- The authTag parameter is only used for decrypting.
651- """
652- if self ._mode is None :
653- raise WolfCryptError ("Final called with no encryption or decryption" )
654- elif self ._mode == _ENCRYPTION :
655- authTag = _ffi .new ("byte[%d]" % self ._tag_bytes )
656- ret = _lib .wc_ChaCha20Poly1305_Final (self ._native_type , authTag )
657- if ret < 0 :
658- raise WolfCryptError ("Encryption error (%d)" % ret )
659- return _ffi .buffer (authTag )[:]
660- else :
661- if authTag is None :
662- raise WolfCryptError ("authTag parameter required" )
663- authTag = t2b (authTag )
664- self ._native_object = _ffi .new (self ._native_type )
665- ret = _lib .wc_ChaCha20Poly1305_Final (self ._native_type , authTag )
666- if ret < 0 :
667- raise WolfCryptError ("Decryption error (%d)" % ret )
612+ return bytes (plaintext )
668613
669614if _lib .DES3_ENABLED :
670615 class Des3 (_Cipher ):
0 commit comments