U q`(@sddlmZmZmZddlmZddlmZm Z ddl m Z e Z e Ze Ze Ze Ze Ze Ze Ze ZddZGdddeZ d d Z!d efd d Z"ddZ#dddZ$ddZ%d S))absolute_importdivisionprint_function) exceptions)ffilib)ensurecCs(tdt}t|t|ddS)zd Generate a key for use with :func:`.crypto_secretstream_xchacha20poly1305_init_push`. unsigned char[]N)rnew.crypto_secretstream_xchacha20poly1305_KEYBYTESr,crypto_secretstream_xchacha20poly1305_keygenbuffer)ZkeybufrH/tmp/pip-target-nv4zd3e_/lib/python/nacl/bindings/crypto_secretstream.pyr ,s  r c@s"eZdZdZdddgZddZdS)+crypto_secretstream_xchacha20poly1305_statezN An object wrapping the crypto_secretstream_xchacha20poly1305 state. statebufrawbuftagbufcCstdt|_d|_d|_dS)z! Initialize a clean state object.r N)rr 0crypto_secretstream_xchacha20poly1305_STATEBYTESrrr)selfrrr__init__As z4crypto_secretstream_xchacha20poly1305_state.__init__N)__name__ __module__ __qualname____doc__ __slots__rrrrrr:s rcCstt|tdtjdtt|tdtjdtt|tkdtjdt dt }t |j||}t|dkdtjdt |ddS) a\ Initialize a crypto_secretstream_xchacha20poly1305 encryption buffer. :param state: a secretstream state object :type state: crypto_secretstream_xchacha20poly1305_state :param key: must be :data:`.crypto_secretstream_xchacha20poly1305_KEYBYTES` long :type key: bytes :return: header :rtype: bytes BState must be a crypto_secretstream_xchacha20poly1305_state objectZraisingKey must be a bytes sequenceInvalid key lengthzunsigned char []rUnexpected failureN)r isinstancerexc TypeErrorbyteslenr ValueErrorrr 1crypto_secretstream_xchacha20poly1305_HEADERBYTESr/crypto_secretstream_xchacha20poly1305_init_pushr RuntimeErrorr )statekeyZ headerbufrcrrrr(Ls4  r(Nc Cstt|tdtjdtt|tdtjdtt|tkdtjdt|dkpVt|tdtjdt|t }|j dkst|j |krt d||_ |dkrt j }d}nt|}t|j|j t j |t||||}t|dkd tjdt |j |ddS) a Add an encrypted message to the secret stream. :param state: a secretstream state object :type state: crypto_secretstream_xchacha20poly1305_state :param m: the message to encrypt, the maximum length of an individual message is :data:`.crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX`. :type m: bytes :param ad: additional data to include in the authentication tag :type ad: bytes or None :param tag: the message tag, usually :data:`.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE` or :data:`.crypto_secretstream_xchacha20poly1305_TAG_FINAL`. :type tag: int :return: ciphertext :rtype: bytes rrzMessage is not byteszMessage is too longN%Additional data must be bytes or Noner rr )rr!rr"r#r$r%6crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAXr&,crypto_secretstream_xchacha20poly1305_ABYTESrrr NULLr*crypto_secretstream_xchacha20poly1305_pushrr)r )r*madtagclenadlenr,rrrr1usF  r1cCstt|tdtjdtt|tdtjdtt|tkdtjdtt|tdtjdtt|t kdtjd|j dkrt d|_ t |j||}t|d kd tjddS) a Initialize a crypto_secretstream_xchacha20poly1305 decryption buffer. :param state: a secretstream state object :type state: crypto_secretstream_xchacha20poly1305_state :param header: must be :data:`.crypto_secretstream_xchacha20poly1305_HEADERBYTES` long :type header: bytes :param key: must be :data:`.crypto_secretstream_xchacha20poly1305_KEYBYTES` long :type key: bytes rrzHeader must be a bytes sequencezInvalid header lengthrrNzunsigned char *rr )rr!rr"r#r$r%r'r&r rrr r/crypto_secretstream_xchacha20poly1305_init_pullrr))r*headerr+r,rrrr7sB    r7c Cs>tt|tdtjdt|jdk dtjdtt|tdtjdtt|t kdtjdtt|t t kdtjdt|dkpt|tdtjdt|t }|j dkst|j |krt d ||_ |dkrt j}d }nt|}t|j|j t j|j|t|||}t|d kd tjdt |j |ddt|jd fS) aM Read a decrypted message from the secret stream. :param state: a secretstream state object :type state: crypto_secretstream_xchacha20poly1305_state :param c: the ciphertext to decrypt, the maximum length of an individual ciphertext is :data:`.crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX` + :data:`.crypto_secretstream_xchacha20poly1305_ABYTES`. :type c: bytes :param ad: additional data to include in the authentication tag :type ad: bytes or None :return: (message, tag) :rtype: (bytes, int) rrNzOState must be initialized using crypto_secretstream_xchacha20poly1305_init_pullzCiphertext is not byteszCiphertext is too shortzCiphertext is too longr-r rr )rr!rr"r#rr&r$r%r/r.rrr r0r*crypto_secretstream_xchacha20poly1305_pullrr)r int)r*cr3mlenr6r,rrrr9sj  r9cCs&tt|tdtjdt|jdS)a Explicitly change the encryption key in the stream. Normally the stream is re-keyed as needed or an explicit ``tag`` of :data:`.crypto_secretstream_xchacha20poly1305_TAG_REKEY` is added to a message to ensure forward secrecy, but this method can be used instead if the re-keying is controlled without adding the tag. :param state: a secretstream state object :type state: crypto_secretstream_xchacha20poly1305_state rrN)rr!rr"r#r+crypto_secretstream_xchacha20poly1305_rekeyr)r*rrrr=1s r=)N)& __future__rrrZnaclrr"Z nacl._sodiumrrZnacl.exceptionsrZ,crypto_secretstream_xchacha20poly1305_abytesr/Z1crypto_secretstream_xchacha20poly1305_headerbytesr'Z.crypto_secretstream_xchacha20poly1305_keybytesr Z6crypto_secretstream_xchacha20poly1305_messagebytes_maxr.Z0crypto_secretstream_xchacha20poly1305_statebytesrZ1crypto_secretstream_xchacha20poly1305_tag_messageZ1crypto_secretstream_xchacha20poly1305_TAG_MESSAGEZ.crypto_secretstream_xchacha20poly1305_tag_pushZ.crypto_secretstream_xchacha20poly1305_TAG_PUSHZ/crypto_secretstream_xchacha20poly1305_tag_rekeyZ/crypto_secretstream_xchacha20poly1305_TAG_REKEYZ/crypto_secretstream_xchacha20poly1305_tag_finalZ/crypto_secretstream_xchacha20poly1305_TAG_FINALr objectrr(r1r7r9r=rrrrs<  , @0 L