123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- # Copyright 2017 Donald Stufft and individual contributors
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from typing import Optional
- from nacl import exceptions as exc
- from nacl._sodium import ffi, lib
- from nacl.exceptions import ensure
- """
- Implementations of authenticated encription with associated data (*AEAD*)
- constructions building on the chacha20 stream cipher and the poly1305
- authenticator
- """
- crypto_aead_chacha20poly1305_ietf_KEYBYTES: int = (
- lib.crypto_aead_chacha20poly1305_ietf_keybytes()
- )
- crypto_aead_chacha20poly1305_ietf_NSECBYTES: int = (
- lib.crypto_aead_chacha20poly1305_ietf_nsecbytes()
- )
- crypto_aead_chacha20poly1305_ietf_NPUBBYTES: int = (
- lib.crypto_aead_chacha20poly1305_ietf_npubbytes()
- )
- crypto_aead_chacha20poly1305_ietf_ABYTES: int = (
- lib.crypto_aead_chacha20poly1305_ietf_abytes()
- )
- crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX: int = (
- lib.crypto_aead_chacha20poly1305_ietf_messagebytes_max()
- )
- _aead_chacha20poly1305_ietf_CRYPTBYTES_MAX = (
- crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX
- + crypto_aead_chacha20poly1305_ietf_ABYTES
- )
- crypto_aead_chacha20poly1305_KEYBYTES: int = (
- lib.crypto_aead_chacha20poly1305_keybytes()
- )
- crypto_aead_chacha20poly1305_NSECBYTES: int = (
- lib.crypto_aead_chacha20poly1305_nsecbytes()
- )
- crypto_aead_chacha20poly1305_NPUBBYTES: int = (
- lib.crypto_aead_chacha20poly1305_npubbytes()
- )
- crypto_aead_chacha20poly1305_ABYTES: int = (
- lib.crypto_aead_chacha20poly1305_abytes()
- )
- crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX: int = (
- lib.crypto_aead_chacha20poly1305_messagebytes_max()
- )
- _aead_chacha20poly1305_CRYPTBYTES_MAX = (
- crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX
- + crypto_aead_chacha20poly1305_ABYTES
- )
- crypto_aead_xchacha20poly1305_ietf_KEYBYTES: int = (
- lib.crypto_aead_xchacha20poly1305_ietf_keybytes()
- )
- crypto_aead_xchacha20poly1305_ietf_NSECBYTES: int = (
- lib.crypto_aead_xchacha20poly1305_ietf_nsecbytes()
- )
- crypto_aead_xchacha20poly1305_ietf_NPUBBYTES: int = (
- lib.crypto_aead_xchacha20poly1305_ietf_npubbytes()
- )
- crypto_aead_xchacha20poly1305_ietf_ABYTES: int = (
- lib.crypto_aead_xchacha20poly1305_ietf_abytes()
- )
- crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX: int = (
- lib.crypto_aead_xchacha20poly1305_ietf_messagebytes_max()
- )
- _aead_xchacha20poly1305_ietf_CRYPTBYTES_MAX = (
- crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX
- + crypto_aead_xchacha20poly1305_ietf_ABYTES
- )
- def crypto_aead_chacha20poly1305_ietf_encrypt(
- message: bytes, aad: Optional[bytes], nonce: bytes, key: bytes
- ) -> bytes:
- """
- Encrypt the given ``message`` using the IETF ratified chacha20poly1305
- construction described in RFC7539.
- :param message:
- :type message: bytes
- :param aad:
- :type aad: Optional[bytes]
- :param nonce:
- :type nonce: bytes
- :param key:
- :type key: bytes
- :return: authenticated ciphertext
- :rtype: bytes
- """
- ensure(
- isinstance(message, bytes),
- "Input message type must be bytes",
- raising=exc.TypeError,
- )
- mlen = len(message)
- ensure(
- mlen <= crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX,
- "Message must be at most {} bytes long".format(
- crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX
- ),
- raising=exc.ValueError,
- )
- ensure(
- isinstance(aad, bytes) or (aad is None),
- "Additional data must be bytes or None",
- raising=exc.TypeError,
- )
- ensure(
- isinstance(nonce, bytes)
- and len(nonce) == crypto_aead_chacha20poly1305_ietf_NPUBBYTES,
- "Nonce must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_ietf_NPUBBYTES
- ),
- raising=exc.TypeError,
- )
- ensure(
- isinstance(key, bytes)
- and len(key) == crypto_aead_chacha20poly1305_ietf_KEYBYTES,
- "Key must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_ietf_KEYBYTES
- ),
- raising=exc.TypeError,
- )
- if aad:
- _aad = aad
- aalen = len(aad)
- else:
- _aad = ffi.NULL
- aalen = 0
- mxout = mlen + crypto_aead_chacha20poly1305_ietf_ABYTES
- clen = ffi.new("unsigned long long *")
- ciphertext = ffi.new("unsigned char[]", mxout)
- res = lib.crypto_aead_chacha20poly1305_ietf_encrypt(
- ciphertext, clen, message, mlen, _aad, aalen, ffi.NULL, nonce, key
- )
- ensure(res == 0, "Encryption failed.", raising=exc.CryptoError)
- return ffi.buffer(ciphertext, clen[0])[:]
- def crypto_aead_chacha20poly1305_ietf_decrypt(
- ciphertext: bytes, aad: Optional[bytes], nonce: bytes, key: bytes
- ) -> bytes:
- """
- Decrypt the given ``ciphertext`` using the IETF ratified chacha20poly1305
- construction described in RFC7539.
- :param ciphertext:
- :type ciphertext: bytes
- :param aad:
- :type aad: Optional[bytes]
- :param nonce:
- :type nonce: bytes
- :param key:
- :type key: bytes
- :return: message
- :rtype: bytes
- """
- ensure(
- isinstance(ciphertext, bytes),
- "Input ciphertext type must be bytes",
- raising=exc.TypeError,
- )
- clen = len(ciphertext)
- ensure(
- clen <= _aead_chacha20poly1305_ietf_CRYPTBYTES_MAX,
- "Ciphertext must be at most {} bytes long".format(
- _aead_chacha20poly1305_ietf_CRYPTBYTES_MAX
- ),
- raising=exc.ValueError,
- )
- ensure(
- isinstance(aad, bytes) or (aad is None),
- "Additional data must be bytes or None",
- raising=exc.TypeError,
- )
- ensure(
- isinstance(nonce, bytes)
- and len(nonce) == crypto_aead_chacha20poly1305_ietf_NPUBBYTES,
- "Nonce must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_ietf_NPUBBYTES
- ),
- raising=exc.TypeError,
- )
- ensure(
- isinstance(key, bytes)
- and len(key) == crypto_aead_chacha20poly1305_ietf_KEYBYTES,
- "Key must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_ietf_KEYBYTES
- ),
- raising=exc.TypeError,
- )
- mxout = clen - crypto_aead_chacha20poly1305_ietf_ABYTES
- mlen = ffi.new("unsigned long long *")
- message = ffi.new("unsigned char[]", mxout)
- if aad:
- _aad = aad
- aalen = len(aad)
- else:
- _aad = ffi.NULL
- aalen = 0
- res = lib.crypto_aead_chacha20poly1305_ietf_decrypt(
- message, mlen, ffi.NULL, ciphertext, clen, _aad, aalen, nonce, key
- )
- ensure(res == 0, "Decryption failed.", raising=exc.CryptoError)
- return ffi.buffer(message, mlen[0])[:]
- def crypto_aead_chacha20poly1305_encrypt(
- message: bytes, aad: Optional[bytes], nonce: bytes, key: bytes
- ) -> bytes:
- """
- Encrypt the given ``message`` using the "legacy" construction
- described in draft-agl-tls-chacha20poly1305.
- :param message:
- :type message: bytes
- :param aad:
- :type aad: Optional[bytes]
- :param nonce:
- :type nonce: bytes
- :param key:
- :type key: bytes
- :return: authenticated ciphertext
- :rtype: bytes
- """
- ensure(
- isinstance(message, bytes),
- "Input message type must be bytes",
- raising=exc.TypeError,
- )
- mlen = len(message)
- ensure(
- mlen <= crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX,
- "Message must be at most {} bytes long".format(
- crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX
- ),
- raising=exc.ValueError,
- )
- ensure(
- isinstance(aad, bytes) or (aad is None),
- "Additional data must be bytes or None",
- raising=exc.TypeError,
- )
- ensure(
- isinstance(nonce, bytes)
- and len(nonce) == crypto_aead_chacha20poly1305_NPUBBYTES,
- "Nonce must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_NPUBBYTES
- ),
- raising=exc.TypeError,
- )
- ensure(
- isinstance(key, bytes)
- and len(key) == crypto_aead_chacha20poly1305_KEYBYTES,
- "Key must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_KEYBYTES
- ),
- raising=exc.TypeError,
- )
- if aad:
- _aad = aad
- aalen = len(aad)
- else:
- _aad = ffi.NULL
- aalen = 0
- mlen = len(message)
- mxout = mlen + crypto_aead_chacha20poly1305_ietf_ABYTES
- clen = ffi.new("unsigned long long *")
- ciphertext = ffi.new("unsigned char[]", mxout)
- res = lib.crypto_aead_chacha20poly1305_encrypt(
- ciphertext, clen, message, mlen, _aad, aalen, ffi.NULL, nonce, key
- )
- ensure(res == 0, "Encryption failed.", raising=exc.CryptoError)
- return ffi.buffer(ciphertext, clen[0])[:]
- def crypto_aead_chacha20poly1305_decrypt(
- ciphertext: bytes, aad: Optional[bytes], nonce: bytes, key: bytes
- ) -> bytes:
- """
- Decrypt the given ``ciphertext`` using the "legacy" construction
- described in draft-agl-tls-chacha20poly1305.
- :param ciphertext: authenticated ciphertext
- :type ciphertext: bytes
- :param aad:
- :type aad: Optional[bytes]
- :param nonce:
- :type nonce: bytes
- :param key:
- :type key: bytes
- :return: message
- :rtype: bytes
- """
- ensure(
- isinstance(ciphertext, bytes),
- "Input ciphertext type must be bytes",
- raising=exc.TypeError,
- )
- clen = len(ciphertext)
- ensure(
- clen <= _aead_chacha20poly1305_CRYPTBYTES_MAX,
- "Ciphertext must be at most {} bytes long".format(
- _aead_chacha20poly1305_CRYPTBYTES_MAX
- ),
- raising=exc.ValueError,
- )
- ensure(
- isinstance(aad, bytes) or (aad is None),
- "Additional data must be bytes or None",
- raising=exc.TypeError,
- )
- ensure(
- isinstance(nonce, bytes)
- and len(nonce) == crypto_aead_chacha20poly1305_NPUBBYTES,
- "Nonce must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_NPUBBYTES
- ),
- raising=exc.TypeError,
- )
- ensure(
- isinstance(key, bytes)
- and len(key) == crypto_aead_chacha20poly1305_KEYBYTES,
- "Key must be a {} bytes long bytes sequence".format(
- crypto_aead_chacha20poly1305_KEYBYTES
- ),
- raising=exc.TypeError,
- )
- mxout = clen - crypto_aead_chacha20poly1305_ABYTES
- mlen = ffi.new("unsigned long long *")
- message = ffi.new("unsigned char[]", mxout)
- if aad:
- _aad = aad
- aalen = len(aad)
- else:
- _aad = ffi.NULL
- aalen = 0
- res = lib.crypto_aead_chacha20poly1305_decrypt(
- message, mlen, ffi.NULL, ciphertext, clen, _aad, aalen, nonce, key
- )
- ensure(res == 0, "Decryption failed.", raising=exc.CryptoError)
- return ffi.buffer(message, mlen[0])[:]
- def crypto_aead_xchacha20poly1305_ietf_encrypt(
- message: bytes, aad: Optional[bytes], nonce: bytes, key: bytes
- ) -> bytes:
- """
- Encrypt the given ``message`` using the long-nonces xchacha20poly1305
- construction.
- :param message:
- :type message: bytes
- :param aad:
- :type aad: Optional[bytes]
- :param nonce:
- :type nonce: bytes
- :param key:
- :type key: bytes
- :return: authenticated ciphertext
- :rtype: bytes
- """
- ensure(
- isinstance(message, bytes),
- "Input message type must be bytes",
- raising=exc.TypeError,
- )
- mlen = len(message)
- ensure(
- mlen <= crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX,
- "Message must be at most {} bytes long".format(
- crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX
- ),
- raising=exc.ValueError,
- )
- ensure(
- isinstance(aad, bytes) or (aad is None),
- "Additional data must be bytes or None",
- raising=exc.TypeError,
- )
- ensure(
- isinstance(nonce, bytes)
- and len(nonce) == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,
- "Nonce must be a {} bytes long bytes sequence".format(
- crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
- ),
- raising=exc.TypeError,
- )
- ensure(
- isinstance(key, bytes)
- and len(key) == crypto_aead_xchacha20poly1305_ietf_KEYBYTES,
- "Key must be a {} bytes long bytes sequence".format(
- crypto_aead_xchacha20poly1305_ietf_KEYBYTES
- ),
- raising=exc.TypeError,
- )
- if aad:
- _aad = aad
- aalen = len(aad)
- else:
- _aad = ffi.NULL
- aalen = 0
- mlen = len(message)
- mxout = mlen + crypto_aead_xchacha20poly1305_ietf_ABYTES
- clen = ffi.new("unsigned long long *")
- ciphertext = ffi.new("unsigned char[]", mxout)
- res = lib.crypto_aead_xchacha20poly1305_ietf_encrypt(
- ciphertext, clen, message, mlen, _aad, aalen, ffi.NULL, nonce, key
- )
- ensure(res == 0, "Encryption failed.", raising=exc.CryptoError)
- return ffi.buffer(ciphertext, clen[0])[:]
- def crypto_aead_xchacha20poly1305_ietf_decrypt(
- ciphertext: bytes, aad: Optional[bytes], nonce: bytes, key: bytes
- ) -> bytes:
- """
- Decrypt the given ``ciphertext`` using the long-nonces xchacha20poly1305
- construction.
- :param ciphertext: authenticated ciphertext
- :type ciphertext: bytes
- :param aad:
- :type aad: Optional[bytes]
- :param nonce:
- :type nonce: bytes
- :param key:
- :type key: bytes
- :return: message
- :rtype: bytes
- """
- ensure(
- isinstance(ciphertext, bytes),
- "Input ciphertext type must be bytes",
- raising=exc.TypeError,
- )
- clen = len(ciphertext)
- ensure(
- clen <= _aead_xchacha20poly1305_ietf_CRYPTBYTES_MAX,
- "Ciphertext must be at most {} bytes long".format(
- _aead_xchacha20poly1305_ietf_CRYPTBYTES_MAX
- ),
- raising=exc.ValueError,
- )
- ensure(
- isinstance(aad, bytes) or (aad is None),
- "Additional data must be bytes or None",
- raising=exc.TypeError,
- )
- ensure(
- isinstance(nonce, bytes)
- and len(nonce) == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,
- "Nonce must be a {} bytes long bytes sequence".format(
- crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
- ),
- raising=exc.TypeError,
- )
- ensure(
- isinstance(key, bytes)
- and len(key) == crypto_aead_xchacha20poly1305_ietf_KEYBYTES,
- "Key must be a {} bytes long bytes sequence".format(
- crypto_aead_xchacha20poly1305_ietf_KEYBYTES
- ),
- raising=exc.TypeError,
- )
- mxout = clen - crypto_aead_xchacha20poly1305_ietf_ABYTES
- mlen = ffi.new("unsigned long long *")
- message = ffi.new("unsigned char[]", mxout)
- if aad:
- _aad = aad
- aalen = len(aad)
- else:
- _aad = ffi.NULL
- aalen = 0
- res = lib.crypto_aead_xchacha20poly1305_ietf_decrypt(
- message, mlen, ffi.NULL, ciphertext, clen, _aad, aalen, nonce, key
- )
- ensure(res == 0, "Decryption failed.", raising=exc.CryptoError)
- return ffi.buffer(message, mlen[0])[:]
|