File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/ciphers.zip
Back
PK W��Z p� � base.pynu �[��� # This file is dual licensed under the terms of the Apache License, Version # 2.0, and the BSD License. See the LICENSE file in the root of this repository # for complete details. import abc import typing from cryptography import utils from cryptography.exceptions import ( AlreadyFinalized, AlreadyUpdated, NotYetFinalized, UnsupportedAlgorithm, _Reasons, ) from cryptography.hazmat.backends import _get_backend from cryptography.hazmat.backends.interfaces import CipherBackend from cryptography.hazmat.primitives._cipheralgorithm import CipherAlgorithm from cryptography.hazmat.primitives.ciphers import modes class BlockCipherAlgorithm(metaclass=abc.ABCMeta): @abc.abstractproperty def block_size(self) -> int: """ The size of a block as an integer in bits (e.g. 64, 128). """ class CipherContext(metaclass=abc.ABCMeta): @abc.abstractmethod def update(self, data: bytes) -> bytes: """ Processes the provided bytes through the cipher and returns the results as bytes. """ @abc.abstractmethod def update_into(self, data: bytes, buf) -> int: """ Processes the provided bytes and writes the resulting data into the provided buffer. Returns the number of bytes written. """ @abc.abstractmethod def finalize(self) -> bytes: """ Returns the results of processing the final block as bytes. """ class AEADCipherContext(metaclass=abc.ABCMeta): @abc.abstractmethod def authenticate_additional_data(self, data: bytes) -> None: """ Authenticates the provided bytes. """ class AEADDecryptionContext(metaclass=abc.ABCMeta): @abc.abstractmethod def finalize_with_tag(self, tag: bytes) -> bytes: """ Returns the results of processing the final block as bytes and allows delayed passing of the authentication tag. """ class AEADEncryptionContext(metaclass=abc.ABCMeta): @abc.abstractproperty def tag(self) -> bytes: """ Returns tag bytes. This is only available after encryption is finalized. """ class Cipher(object): def __init__( self, algorithm: CipherAlgorithm, mode: typing.Optional[modes.Mode], backend=None, ): backend = _get_backend(backend) if not isinstance(backend, CipherBackend): raise UnsupportedAlgorithm( "Backend object does not implement CipherBackend.", _Reasons.BACKEND_MISSING_INTERFACE, ) if not isinstance(algorithm, CipherAlgorithm): raise TypeError("Expected interface of CipherAlgorithm.") if mode is not None: mode.validate_for_algorithm(algorithm) self.algorithm = algorithm self.mode = mode self._backend = backend def encryptor(self): if isinstance(self.mode, modes.ModeWithAuthenticationTag): if self.mode.tag is not None: raise ValueError( "Authentication tag must be None when encrypting." ) ctx = self._backend.create_symmetric_encryption_ctx( self.algorithm, self.mode ) return self._wrap_ctx(ctx, encrypt=True) def decryptor(self): ctx = self._backend.create_symmetric_decryption_ctx( self.algorithm, self.mode ) return self._wrap_ctx(ctx, encrypt=False) def _wrap_ctx(self, ctx, encrypt): if isinstance(self.mode, modes.ModeWithAuthenticationTag): if encrypt: return _AEADEncryptionContext(ctx) else: return _AEADCipherContext(ctx) else: return _CipherContext(ctx) @utils.register_interface(CipherContext) class _CipherContext(object): def __init__(self, ctx): self._ctx = ctx def update(self, data: bytes) -> bytes: if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") return self._ctx.update(data) def update_into(self, data: bytes, buf) -> int: if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") return self._ctx.update_into(data, buf) def finalize(self) -> bytes: if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") data = self._ctx.finalize() self._ctx = None return data @utils.register_interface(AEADCipherContext) @utils.register_interface(CipherContext) @utils.register_interface(AEADDecryptionContext) class _AEADCipherContext(object): def __init__(self, ctx): self._ctx = ctx self._bytes_processed = 0 self._aad_bytes_processed = 0 self._tag = None self._updated = False def _check_limit(self, data_size: int): if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") self._updated = True self._bytes_processed += data_size if self._bytes_processed > self._ctx._mode._MAX_ENCRYPTED_BYTES: raise ValueError( "{} has a maximum encrypted byte limit of {}".format( self._ctx._mode.name, self._ctx._mode._MAX_ENCRYPTED_BYTES ) ) def update(self, data: bytes) -> bytes: self._check_limit(len(data)) return self._ctx.update(data) def update_into(self, data: bytes, buf) -> int: self._check_limit(len(data)) return self._ctx.update_into(data, buf) def finalize(self) -> bytes: if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") data = self._ctx.finalize() self._tag = self._ctx.tag self._ctx = None return data def finalize_with_tag(self, tag: bytes) -> bytes: if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") data = self._ctx.finalize_with_tag(tag) self._tag = self._ctx.tag self._ctx = None return data def authenticate_additional_data(self, data: bytes) -> None: if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") if self._updated: raise AlreadyUpdated("Update has been called on this context.") self._aad_bytes_processed += len(data) if self._aad_bytes_processed > self._ctx._mode._MAX_AAD_BYTES: raise ValueError( "{} has a maximum AAD byte limit of {}".format( self._ctx._mode.name, self._ctx._mode._MAX_AAD_BYTES ) ) self._ctx.authenticate_additional_data(data) @utils.register_interface(AEADEncryptionContext) class _AEADEncryptionContext(_AEADCipherContext): @property def tag(self) -> bytes: if self._ctx is not None: raise NotYetFinalized( "You must finalize encryption before " "getting the tag." ) assert self._tag is not None return self._tag PK W��Z�� � � aead.pynu �[��� # This file is dual licensed under the terms of the Apache License, Version # 2.0, and the BSD License. See the LICENSE file in the root of this repository # for complete details. import os import typing from cryptography import exceptions, utils from cryptography.hazmat.backends.openssl import aead from cryptography.hazmat.backends.openssl.backend import backend class ChaCha20Poly1305(object): _MAX_SIZE = 2 ** 32 def __init__(self, key: bytes): if not backend.aead_cipher_supported(self): raise exceptions.UnsupportedAlgorithm( "ChaCha20Poly1305 is not supported by this version of OpenSSL", exceptions._Reasons.UNSUPPORTED_CIPHER, ) utils._check_byteslike("key", key) if len(key) != 32: raise ValueError("ChaCha20Poly1305 key must be 32 bytes.") self._key = key @classmethod def generate_key(cls) -> bytes: return os.urandom(32) def encrypt( self, nonce: bytes, data: bytes, associated_data: typing.Optional[bytes], ) -> bytes: if associated_data is None: associated_data = b"" if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE: # This is OverflowError to match what cffi would raise raise OverflowError( "Data or associated data too long. Max 2**32 bytes" ) self._check_params(nonce, data, associated_data) return aead._encrypt(backend, self, nonce, data, associated_data, 16) def decrypt( self, nonce: bytes, data: bytes, associated_data: typing.Optional[bytes], ) -> bytes: if associated_data is None: associated_data = b"" self._check_params(nonce, data, associated_data) return aead._decrypt(backend, self, nonce, data, associated_data, 16) def _check_params( self, nonce: bytes, data: bytes, associated_data: bytes, ) -> None: utils._check_byteslike("nonce", nonce) utils._check_bytes("data", data) utils._check_bytes("associated_data", associated_data) if len(nonce) != 12: raise ValueError("Nonce must be 12 bytes") class AESCCM(object): _MAX_SIZE = 2 ** 32 def __init__(self, key: bytes, tag_length: int = 16): utils._check_byteslike("key", key) if len(key) not in (16, 24, 32): raise ValueError("AESCCM key must be 128, 192, or 256 bits.") self._key = key if not isinstance(tag_length, int): raise TypeError("tag_length must be an integer") if tag_length not in (4, 6, 8, 10, 12, 14, 16): raise ValueError("Invalid tag_length") self._tag_length = tag_length @classmethod def generate_key(cls, bit_length: int) -> bytes: if not isinstance(bit_length, int): raise TypeError("bit_length must be an integer") if bit_length not in (128, 192, 256): raise ValueError("bit_length must be 128, 192, or 256") return os.urandom(bit_length // 8) def encrypt( self, nonce: bytes, data: bytes, associated_data: typing.Optional[bytes], ) -> bytes: if associated_data is None: associated_data = b"" if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE: # This is OverflowError to match what cffi would raise raise OverflowError( "Data or associated data too long. Max 2**32 bytes" ) self._check_params(nonce, data, associated_data) self._validate_lengths(nonce, len(data)) return aead._encrypt( backend, self, nonce, data, associated_data, self._tag_length ) def decrypt( self, nonce: bytes, data: bytes, associated_data: typing.Optional[bytes], ) -> bytes: if associated_data is None: associated_data = b"" self._check_params(nonce, data, associated_data) return aead._decrypt( backend, self, nonce, data, associated_data, self._tag_length ) def _validate_lengths(self, nonce: bytes, data_len: int): # For information about computing this, see # https://tools.ietf.org/html/rfc3610#section-2.1 l_val = 15 - len(nonce) if 2 ** (8 * l_val) < data_len: raise ValueError("Data too long for nonce") def _check_params(self, nonce: bytes, data: bytes, associated_data: bytes): utils._check_byteslike("nonce", nonce) utils._check_bytes("data", data) utils._check_bytes("associated_data", associated_data) if not 7 <= len(nonce) <= 13: raise ValueError("Nonce must be between 7 and 13 bytes") class AESGCM(object): _MAX_SIZE = 2 ** 32 def __init__(self, key: bytes): utils._check_byteslike("key", key) if len(key) not in (16, 24, 32): raise ValueError("AESGCM key must be 128, 192, or 256 bits.") self._key = key @classmethod def generate_key(cls, bit_length: int) -> bytes: if not isinstance(bit_length, int): raise TypeError("bit_length must be an integer") if bit_length not in (128, 192, 256): raise ValueError("bit_length must be 128, 192, or 256") return os.urandom(bit_length // 8) def encrypt( self, nonce: bytes, data: bytes, associated_data: typing.Optional[bytes], ) -> bytes: if associated_data is None: associated_data = b"" if len(data) > self._MAX_SIZE or len(associated_data) > self._MAX_SIZE: # This is OverflowError to match what cffi would raise raise OverflowError( "Data or associated data too long. Max 2**32 bytes" ) self._check_params(nonce, data, associated_data) return aead._encrypt(backend, self, nonce, data, associated_data, 16) def decrypt( self, nonce: bytes, data: bytes, associated_data: typing.Optional[bytes], ) -> bytes: if associated_data is None: associated_data = b"" self._check_params(nonce, data, associated_data) return aead._decrypt(backend, self, nonce, data, associated_data, 16) def _check_params( self, nonce: bytes, data: bytes, associated_data: bytes, ) -> None: utils._check_byteslike("nonce", nonce) utils._check_bytes("data", data) utils._check_bytes("associated_data", associated_data) if len(nonce) < 8 or len(nonce) > 128: raise ValueError("Nonce must be between 8 and 128 bytes") PK W��Z,C��] ] __pycache__/aead.cpython-310.pycnu �[��� o �)%a� � @ sl d dl Z d dlZd dlmZmZ d dlmZ d dlmZ G dd� de �Z G dd� de �ZG d d � d e �ZdS )� N)� exceptions�utils)�aead)�backendc @ s� e Zd ZdZdefdd�Zedefdd��Zded ed ej e defdd�Z ded ed ej e defd d�Zded ed eddfdd�ZdS )�ChaCha20Poly1305� �keyc C sD t �| �s t�dtjj��t�d|� t|�dkrt d��|| _ d S )Nz<ChaCha20Poly1305 is not supported by this version of OpenSSLr � z&ChaCha20Poly1305 key must be 32 bytes.)r �aead_cipher_supportedr �UnsupportedAlgorithm�_Reasons�UNSUPPORTED_CIPHERr �_check_byteslike�len� ValueError�_key��selfr � r �M/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/ciphers/aead.py�__init__ s � zChaCha20Poly1305.__init__�returnc C s t �d�S )Nr )�os�urandom)�clsr r r �generate_key s zChaCha20Poly1305.generate_key�nonce�data�associated_datac C �R |d u rd}t |�| jkst |�| jkrtd��| �|||� t�t| |||d�S �N� �1Data or associated data too long. Max 2**32 bytes� �r � _MAX_SIZE� OverflowError� _check_paramsr �_encryptr �r r r r r r r �encrypt"