Source code for pyflocker.ciphers.backends.cryptography_.symmetric
"""Cryptography backend specific templates and tools for symmetric ciphers."""from__future__importannotationsimporttypingfromcryptographyimportexceptionsasbkxfrompyflocker.ciphersimportbase,exc
[docs]classNonAEADCipherTemplate(base.BaseNonAEADCipher):""" Template class to provide the default behavior if BaseNonAEADCipher. Subclasses need to provide: - `_encrypting` - `_ctx` """_encrypting:bool_ctx:typing.Any
[docs]classAuthenticationMixin:"""Mixin class to provide authentication behavior to ciphers. Classes inheriting this must provide these attributes: Attributes: _updated: A boolean indicating whether ``update()`` method of ``_cipher`` was called. _ctx: A cipher context from cryptography package that has ``finalize()`` method. _tag: A byte sequence denoting the MAC tag generated by the cipher after encryption. """_updated:bool_ctx:typing.Anyis_encrypting:typing.Callable_tag:bytes|None
[docs]deffinalize(self,tag:bytes|None=None)->None:ifself._ctxisNone:raiseexc.AlreadyFinalizedifnotself.is_encrypting():iftagisNone:msg="tag is required for finalization"raiseValueError(msg)ctx,self._ctx=self._ctx,Nonetry:ctx.finalize_with_tag(tag)exceptbkx.InvalidTagase:raiseexc.DecryptionErrorfromeelse:ctx,self._ctx=self._ctx,Nonectx.finalize()self._tag=ctx.tag
[docs]classAEADCipherTemplate(AuthenticationMixin,base.BaseAEADCipher):""" Template class to provide the default behavior of BaseAEADCipher. Subclasses need to provide the following attributes: - `_encrypting` - `_ctx` """# these are *not* class variables_updated:bool=False_tag:bytes|None=None_encrypting:bool_ctx:typing.Any