a b+@sjUddlmZmZmZmZddlmZddlm Z m Z ddl m Z e Zeed<e Zeed<e Zeed<e Zeed<e Zeed <e Zeed <e Zeed <e Zeed <e Z eed <e!dddZ"GdddZ#e#e!e!dddZ$defe#e!ee!ee!dddZ%e#e!e!ddddZ&d#e#e!ee!ee!efdddZ'e#dd d!d"Z(dS)$) ByteStringOptionalTuplecast) exceptions)ffilib)ensure,crypto_secretstream_xchacha20poly1305_ABYTES1crypto_secretstream_xchacha20poly1305_HEADERBYTES.crypto_secretstream_xchacha20poly1305_KEYBYTES6crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX0crypto_secretstream_xchacha20poly1305_STATEBYTES1crypto_secretstream_xchacha20poly1305_TAG_MESSAGE.crypto_secretstream_xchacha20poly1305_TAG_PUSH/crypto_secretstream_xchacha20poly1305_TAG_REKEY/crypto_secretstream_xchacha20poly1305_TAG_FINALreturncCs(tdt}t|t|ddS)zd Generate a key for use with :func:`.crypto_secretstream_xchacha20poly1305_init_push`. unsigned char[]N)rnewr r,crypto_secretstream_xchacha20poly1305_keygenbuffer)ZkeybufrH/tmp/pip-target-98j97qn4/lib/python/nacl/bindings/crypto_secretstream.pyr4s  rc@s&eZdZdZgdZddddZdS)+crypto_secretstream_xchacha20poly1305_statezN An object wrapping the crypto_secretstream_xchacha20poly1305 state. )statebufrawbuftagbufNrcCstdt|_d|_d|_dS)z Initialize a clean state object.rN)rrrrrr)selfrrr__init__Js z4crypto_secretstream_xchacha20poly1305_state.__init__)__name__ __module__ __qualname____doc__ __slots__r rrrrrBsr)statekeyrcCstt|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 ValueErrorrrr r/crypto_secretstream_xchacha20poly1305_init_pushr RuntimeErrorr)r&r'Z headerbufrcrrrr3Us0 r3N)r&madtagrc Cstt|tdtjdtt|tdtjdtt|tkdtjdt|dupVt|tdtjdt|t }|j dust|j |krt d||_ |durt 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 r(r)zMessage is not byteszMessage is too longN%Additional data must be bytes or Nonerrr,)r r-rr.r/r0r1r r2r rrrNULLr*crypto_secretstream_xchacha20poly1305_pushrr4r)r&r6r7r8clenadlenr5rrrr;sF   r;)r&headerr'rcCstt|tdtjdtt|tdtjdtt|tkdtjdtt|tdtjdtt|t kdtjd|j durt 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 r(r)zHeader must be a bytes sequencezInvalid header lengthr*r+Nzunsigned char *rr,)r r-rr.r/r0r1r r2r rrrr/crypto_secretstream_xchacha20poly1305_init_pullrr4)r&r>r'r5rrrr?s>    r?)r&cr7rc CsDtt|tdtjdt|jdudtjdtt|tdtjdtt|t kdtjdtt|t t kdtjdt|dupt|tdtjdt|t }|j dust|j |krt d ||_ |durt j}d }nt|}t|j|j t j|j|t|||}t|d kd tjdt |j |ddttt|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) r(r)NzOState must be initialized using crypto_secretstream_xchacha20poly1305_init_pullzCiphertext is not byteszCiphertext is too shortzCiphertext is too longr9rrr,)r r-rr.r/rr2r0r1r r rrrr:r*crypto_secretstream_xchacha20poly1305_pullrr4rintr)r&r@r7mlenr=r5rrrrAsn    rA)r&rcCs&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 r(r)N)r r-rr.r/r+crypto_secretstream_xchacha20poly1305_rekeyr)r&rrrrDQs rD)N))typingrrrrZnaclrr.Z nacl._sodiumrrZnacl.exceptionsr Z,crypto_secretstream_xchacha20poly1305_abytesr rB__annotations__Z1crypto_secretstream_xchacha20poly1305_headerbytesr Z.crypto_secretstream_xchacha20poly1305_keybytesr Z6crypto_secretstream_xchacha20poly1305_messagebytes_maxr Z0crypto_secretstream_xchacha20poly1305_statebytesrZ1crypto_secretstream_xchacha20poly1305_tag_messagerZ.crypto_secretstream_xchacha20poly1305_tag_pushrZ/crypto_secretstream_xchacha20poly1305_tag_rekeyrZ/crypto_secretstream_xchacha20poly1305_tag_finalrr0rrr3r;r?rArDrrrrsd            / D 8 Y