Example #1
0
    block_size = 1
    iv_size = 0

    def new(self, key, iv=None, initial_bytes=0):
        """Construct a new chacha20-poly1305 cipher object"""

        # pylint: disable=no-self-use,unused-argument
        return _Chacha20Poly1305Cipher(key)


try:
    # pylint: disable=wrong-import-position,wrong-import-order
    from libnacl import nacl

    _CHACHA20_KEYBYTES = nacl.crypto_stream_chacha20_keybytes()
    _CHACHA20_NONCEBYTES = nacl.crypto_stream_chacha20_noncebytes()

    _chacha20 = nacl.crypto_stream_chacha20
    _chacha20_xor_ic = nacl.crypto_stream_chacha20_xor_ic

    _POLY1305_BYTES = nacl.crypto_onetimeauth_poly1305_bytes()
    _POLY1305_KEYBYTES = nacl.crypto_onetimeauth_poly1305_keybytes()

    _poly1305 = nacl.crypto_onetimeauth_poly1305
    _poly1305_verify = nacl.crypto_onetimeauth_poly1305_verify
except (ImportError, OSError, AttributeError): # pragma: no cover
    pass
else:
    register_cipher('chacha20-poly1305', 'chacha', _Chacha20Poly1305Factory())
Example #2
0
    def verify_and_decrypt(self, header, data, nonce, tag):
        if len(nonce) != _CHACHA20_NONCEBYTES:
            raise ValueError('Invalid chacha20-poly1305 nonce size')

        if self._verify_tag(header + data, nonce, tag):
            plaintext = self._crypt(self._key, data, nonce, 1)
        else:
            plaintext = None

        return plaintext

try:
    from libnacl import nacl

    _CHACHA20_KEYBYTES = nacl.crypto_stream_chacha20_keybytes()
    _CHACHA20_NONCEBYTES = nacl.crypto_stream_chacha20_noncebytes()

    _chacha20 = nacl.crypto_stream_chacha20
    _chacha20_xor_ic = nacl.crypto_stream_chacha20_xor_ic

    _POLY1305_BYTES = nacl.crypto_onetimeauth_poly1305_bytes()
    _POLY1305_KEYBYTES = nacl.crypto_onetimeauth_poly1305_keybytes()

    _poly1305 = nacl.crypto_onetimeauth_poly1305
    _poly1305_verify = nacl.crypto_onetimeauth_poly1305_verify
except (ImportError, OSError, AttributeError):
    pass
else:
    register_cipher('chacha20-poly1305', 'chacha', _Chacha20Poly1305Cipher)