def new(**kwargs): """Create a new ChaCha20-Poly1305 or XChaCha20-Poly1305 AEAD cipher. :keyword key: The secret key to use. It must be 32 bytes long. :type key: byte string :keyword nonce: A value that must never be reused for any other encryption done with this key. For ChaCha20-Poly1305, it must be 8 or 12 bytes long. For XChaCha20-Poly1305, it must be 24 bytes long. If not provided, 12 ``bytes`` will be generated randomly (you can find them back in the ``nonce`` attribute). :type nonce: bytes, bytearray, memoryview :Return: a :class:`Cryptodome.Cipher.ChaCha20.ChaCha20Poly1305Cipher` object """ try: key = kwargs.pop("key") except KeyError as e: raise TypeError("Missing parameter %s" % e) self._len_ct += len(plaintext) if len(key) != 32: raise ValueError("Key must be 32 bytes long") nonce = kwargs.pop("nonce", None) if nonce is None: nonce = get_random_bytes(12) if len(nonce) in (8, 12): pass elif len(nonce) == 24: key = _HChaCha20(key, nonce[:16]) nonce = b'\x00\x00\x00\x00' + nonce[16:] else: raise ValueError("Nonce must be 8, 12 or 24 bytes long") if not is_buffer(nonce): raise TypeError("nonce must be bytes, bytearray or memoryview") if kwargs: raise TypeError("Unknown parameters: " + str(kwargs)) return ChaCha20Poly1305Cipher(key, nonce)
def __init__(self, factory, key, nonce, mac_len, cipher_params): """EAX cipher mode""" self.block_size = factory.block_size """The block size of the underlying cipher, in bytes.""" self.nonce = _copy_bytes(None, None, nonce) """The nonce originally used to create the object.""" self._mac_len = mac_len self._mac_tag = None # Cache for MAC tag # Allowed transitions after initialization self._next = [ self.update, self.encrypt, self.decrypt, self.digest, self.verify ] # MAC tag length if not (4 <= self._mac_len <= self.block_size): raise ValueError("Parameter 'mac_len' must not be larger than %d" % self.block_size) # Nonce cannot be empty and must be a byte string if len(self.nonce) == 0: raise ValueError("Nonce cannot be empty in EAX mode") if not is_buffer(nonce): raise TypeError("nonce must be bytes, bytearray or memoryview") self._omac = [ CMAC.new(key, b'\x00' * (self.block_size - 1) + struct.pack('B', i), ciphermod=factory, cipher_params=cipher_params) for i in range(0, 3) ] # Compute MAC of nonce self._omac[0].update(self.nonce) self._signer = self._omac[1] # MAC of the nonce is also the initial counter for CTR encryption counter_int = bytes_to_long(self._omac[0].digest()) self._cipher = factory.new(key, factory.MODE_CTR, initial_value=counter_int, nonce=b"", **cipher_params)
def __init__(self, factory, key, nonce, mac_len, cipher_params): """EAX cipher mode""" self.block_size = factory.block_size """The block size of the underlying cipher, in bytes.""" self.nonce = _copy_bytes(None, None, nonce) """The nonce originally used to create the object.""" self._mac_len = mac_len self._mac_tag = None # Cache for MAC tag # Allowed transitions after initialization self._next = [self.update, self.encrypt, self.decrypt, self.digest, self.verify] # MAC tag length if not (4 <= self._mac_len <= self.block_size): raise ValueError("Parameter 'mac_len' must not be larger than %d" % self.block_size) # Nonce cannot be empty and must be a byte string if len(self.nonce) == 0: raise ValueError("Nonce cannot be empty in EAX mode") if not is_buffer(nonce): raise TypeError("nonce must be bytes, bytearray or memoryview") self._omac = [ CMAC.new(key, b'\x00' * (self.block_size - 1) + struct.pack('B', i), ciphermod=factory, cipher_params=cipher_params) for i in range(0, 3) ] # Compute MAC of nonce self._omac[0].update(self.nonce) self._signer = self._omac[1] # MAC of the nonce is also the initial counter for CTR encryption counter_int = bytes_to_long(self._omac[0].digest()) self._cipher = factory.new(key, factory.MODE_CTR, initial_value=counter_int, nonce=b"", **cipher_params)
def new(**kwargs): """Create a new ChaCha20-Poly1305 AEAD cipher. :keyword key: The secret key to use. It must be 32 bytes long. :type key: byte string :keyword nonce: A value that must never be reused for any other encryption done with this key. It must be 8 or 12 bytes long. If not provided, 12 ``bytes`` will be generated randomly (you can find them back in the ``nonce`` attribute). :type nonce: bytes, bytearray, memoryview :Return: a :class:`Cryptodome.Cipher.ChaCha20.ChaCha20Poly1305Cipher` object """ try: key = kwargs.pop("key") except KeyError as e: raise TypeError("Missing parameter %s" % e) self._len_ct += len(plaintext) if len(key) != 32: raise ValueError("Key must be 32 bytes long") nonce = kwargs.pop("nonce", None) if nonce is None: nonce = get_random_bytes(12) if len(nonce) not in (8, 12): raise ValueError("Nonce must be 8 or 12 bytes long") if not is_buffer(nonce): raise TypeError("nonce must be bytes, bytearray or memoryview") if kwargs: raise TypeError("Unknown parameters: " + str(kwargs)) return ChaCha20Poly1305Cipher(key, nonce)
def __init__(self, factory, key, nonce, kwargs): self.block_size = factory.block_size """The block size of the underlying cipher, in bytes.""" self._factory = factory self._cipher_params = kwargs if len(key) not in (32, 48, 64): raise ValueError("Incorrect key length (%d bytes)" % len(key)) if nonce is not None: if not is_buffer(nonce): raise TypeError( "When provided, the nonce must be bytes, bytearray or memoryview" ) if len(nonce) == 0: raise ValueError("When provided, the nonce must be non-empty") self.nonce = _copy_bytes(None, None, nonce) """Public attribute is only available in case of non-deterministic encryption.""" subkey_size = len(key) // 2 self._mac_tag = None # Cache for MAC tag self._kdf = _S2V(key[:subkey_size], ciphermod=factory, cipher_params=self._cipher_params) self._subkey_cipher = key[subkey_size:] # Purely for the purpose of verifying that cipher_params are OK factory.new(key[:subkey_size], factory.MODE_ECB, **kwargs) # Allowed transitions after initialization self._next = [ self.update, self.encrypt, self.decrypt, self.digest, self.verify ]
def __init__(self, factory, key, nonce, kwargs): self.block_size = factory.block_size """The block size of the underlying cipher, in bytes.""" self._factory = factory self._cipher_params = kwargs if len(key) not in (32, 48, 64): raise ValueError("Incorrect key length (%d bytes)" % len(key)) if nonce is not None: if not is_buffer(nonce): raise TypeError("When provided, the nonce must be bytes, bytearray or memoryview") if len(nonce) == 0: raise ValueError("When provided, the nonce must be non-empty") self.nonce = _copy_bytes(None, None, nonce) """Public attribute is only available in case of non-deterministic encryption.""" subkey_size = len(key) // 2 self._mac_tag = None # Cache for MAC tag self._kdf = _S2V(key[:subkey_size], ciphermod=factory, cipher_params=self._cipher_params) self._subkey_cipher = key[subkey_size:] # Purely for the purpose of verifying that cipher_params are OK factory.new(key[:subkey_size], factory.MODE_ECB, **kwargs) # Allowed transitions after initialization self._next = [self.update, self.encrypt, self.decrypt, self.digest, self.verify]
def __init__(self, factory, nonce, mac_len, cipher_params): if factory.block_size != 16: raise ValueError("OCB mode is only available for ciphers" " that operate on 128 bits blocks") self.block_size = 16 """The block size of the underlying cipher, in bytes.""" self.nonce = _copy_bytes(None, None, nonce) """Nonce used for this session.""" if len(nonce) not in range(1, 16): raise ValueError("Nonce must be at most 15 bytes long") if not is_buffer(nonce): raise TypeError("Nonce must be bytes, bytearray or memoryview") self._mac_len = mac_len if not 8 <= mac_len <= 16: raise ValueError("MAC tag must be between 8 and 16 bytes long") # Cache for MAC tag self._mac_tag = None # Cache for unaligned associated data self._cache_A = b"" # Cache for unaligned ciphertext/plaintext self._cache_P = b"" # Allowed transitions after initialization self._next = [ self.update, self.encrypt, self.decrypt, self.digest, self.verify ] # Compute Offset_0 params_without_key = dict(cipher_params) key = params_without_key.pop("key") nonce = (struct.pack('B', self._mac_len << 4 & 0xFF) + b'\x00' * (14 - len(nonce)) + b'\x01' + self.nonce) bottom_bits = bord(nonce[15]) & 0x3F # 6 bits, 0..63 top_bits = bord(nonce[15]) & 0xC0 # 2 bits ktop_cipher = factory.new(key, factory.MODE_ECB, **params_without_key) ktop = ktop_cipher.encrypt(struct.pack('15sB', nonce[:15], top_bits)) stretch = ktop + strxor(ktop[:8], ktop[1:9]) # 192 bits offset_0 = long_to_bytes( bytes_to_long(stretch) >> (64 - bottom_bits), 24)[8:] # Create low-level cipher instance raw_cipher = factory._create_base_cipher(cipher_params) if cipher_params: raise TypeError("Unknown keywords: " + str(cipher_params)) self._state = VoidPointer() result = _raw_ocb_lib.OCB_start_operation(raw_cipher.get(), offset_0, c_size_t(len(offset_0)), self._state.address_of()) if result: raise ValueError("Error %d while instantiating the OCB mode" % result) # Ensure that object disposal of this Python object will (eventually) # free the memory allocated by the raw library for the cipher mode self._state = SmartPointer(self._state.get(), _raw_ocb_lib.OCB_stop_operation) # Memory allocated for the underlying block cipher is now owed # by the cipher mode raw_cipher.release()
def __init__(self, factory, nonce, mac_len, cipher_params): if factory.block_size != 16: raise ValueError("OCB mode is only available for ciphers" " that operate on 128 bits blocks") self.block_size = 16 """The block size of the underlying cipher, in bytes.""" self.nonce = _copy_bytes(None, None, nonce) """Nonce used for this session.""" if len(nonce) not in range(1, 16): raise ValueError("Nonce must be at most 15 bytes long") if not is_buffer(nonce): raise TypeError("Nonce must be bytes, bytearray or memoryview") self._mac_len = mac_len if not 8 <= mac_len <= 16: raise ValueError("MAC tag must be between 8 and 16 bytes long") # Cache for MAC tag self._mac_tag = None # Cache for unaligned associated data self._cache_A = b"" # Cache for unaligned ciphertext/plaintext self._cache_P = b"" # Allowed transitions after initialization self._next = [self.update, self.encrypt, self.decrypt, self.digest, self.verify] # Compute Offset_0 params_without_key = dict(cipher_params) key = params_without_key.pop("key") nonce = (struct.pack('B', self._mac_len << 4 & 0xFF) + b'\x00' * (14 - len(nonce)) + b'\x01' + self.nonce) bottom_bits = bord(nonce[15]) & 0x3F # 6 bits, 0..63 top_bits = bord(nonce[15]) & 0xC0 # 2 bits ktop_cipher = factory.new(key, factory.MODE_ECB, **params_without_key) ktop = ktop_cipher.encrypt(struct.pack('15sB', nonce[:15], top_bits)) stretch = ktop + strxor(ktop[:8], ktop[1:9]) # 192 bits offset_0 = long_to_bytes(bytes_to_long(stretch) >> (64 - bottom_bits), 24)[8:] # Create low-level cipher instance raw_cipher = factory._create_base_cipher(cipher_params) if cipher_params: raise TypeError("Unknown keywords: " + str(cipher_params)) self._state = VoidPointer() result = _raw_ocb_lib.OCB_start_operation(raw_cipher.get(), offset_0, c_size_t(len(offset_0)), self._state.address_of()) if result: raise ValueError("Error %d while instantiating the OCB mode" % result) # Ensure that object disposal of this Python object will (eventually) # free the memory allocated by the raw library for the cipher mode self._state = SmartPointer(self._state.get(), _raw_ocb_lib.OCB_stop_operation) # Memory allocated for the underlying block cipher is now owed # by the cipher mode raw_cipher.release()
def __init__(self, factory, key, nonce, mac_len, cipher_params, ghash_c): self.block_size = factory.block_size if self.block_size != 16: raise ValueError("GCM mode is only available for ciphers" " that operate on 128 bits blocks") if len(nonce) == 0: raise ValueError("Nonce cannot be empty") if not is_buffer(nonce): raise TypeError("Nonce must be bytes, bytearray or memoryview") # See NIST SP 800 38D, 5.2.1.1 if len(nonce) > 2**64 - 1: raise ValueError("Nonce exceeds maximum length") self.nonce = _copy_bytes(None, None, nonce) """Nonce""" self._factory = factory self._key = _copy_bytes(None, None, key) self._tag = None # Cache for MAC tag self._mac_len = mac_len if not (4 <= mac_len <= 16): raise ValueError("Parameter 'mac_len' must be in the range 4..16") # Allowed transitions after initialization self._next = [ self.update, self.encrypt, self.decrypt, self.digest, self.verify ] self._no_more_assoc_data = False # Length of associated data self._auth_len = 0 # Length of the ciphertext or plaintext self._msg_len = 0 # Step 1 in SP800-38D, Algorithm 4 (encryption) - Compute H # See also Algorithm 5 (decryption) hash_subkey = factory.new(key, self._factory.MODE_ECB, **cipher_params).encrypt(b'\x00' * 16) # Step 2 - Compute J0 if len(self.nonce) == 12: j0 = self.nonce + b"\x00\x00\x00\x01" else: fill = (16 - (len(nonce) % 16)) % 16 + 8 ghash_in = (self.nonce + b'\x00' * fill + long_to_bytes(8 * len(nonce), 8)) j0 = _GHASH(hash_subkey, ghash_c).update(ghash_in).digest() # Step 3 - Prepare GCTR cipher for encryption/decryption nonce_ctr = j0[:12] iv_ctr = (bytes_to_long(j0) + 1) & 0xFFFFFFFF self._cipher = factory.new(key, self._factory.MODE_CTR, initial_value=iv_ctr, nonce=nonce_ctr, **cipher_params) # Step 5 - Bootstrat GHASH self._signer = _GHASH(hash_subkey, ghash_c) # Step 6 - Prepare GCTR cipher for GMAC self._tag_cipher = factory.new(key, self._factory.MODE_CTR, initial_value=j0, nonce=b"", **cipher_params) # Cache for data to authenticate self._cache = b"" self._status = MacStatus.PROCESSING_AUTH_DATA
def __init__(self, factory, key, nonce, mac_len, cipher_params, ghash_c): self.block_size = factory.block_size if self.block_size != 16: raise ValueError("GCM mode is only available for ciphers" " that operate on 128 bits blocks") if len(nonce) == 0: raise ValueError("Nonce cannot be empty") if not is_buffer(nonce): raise TypeError("Nonce must be bytes, bytearray or memoryview") # See NIST SP 800 38D, 5.2.1.1 if len(nonce) > 2**64 - 1: raise ValueError("Nonce exceeds maximum length") self.nonce = _copy_bytes(None, None, nonce) """Nonce""" self._factory = factory self._key = _copy_bytes(None, None, key) self._tag = None # Cache for MAC tag self._mac_len = mac_len if not (4 <= mac_len <= 16): raise ValueError("Parameter 'mac_len' must be in the range 4..16") # Allowed transitions after initialization self._next = [self.update, self.encrypt, self.decrypt, self.digest, self.verify] self._no_more_assoc_data = False # Length of associated data self._auth_len = 0 # Length of the ciphertext or plaintext self._msg_len = 0 # Step 1 in SP800-38D, Algorithm 4 (encryption) - Compute H # See also Algorithm 5 (decryption) hash_subkey = factory.new(key, self._factory.MODE_ECB, **cipher_params ).encrypt(b'\x00' * 16) # Step 2 - Compute J0 (integer, not byte string!) if len(self.nonce) == 12: self._j0 = bytes_to_long(self.nonce + b"\x00\x00\x00\x01") else: fill = (16 - (len(nonce) % 16)) % 16 + 8 ghash_in = (self.nonce + b'\x00' * fill + long_to_bytes(8 * len(nonce), 8)) self._j0 = bytes_to_long(_GHASH(hash_subkey, ghash_c) .update(ghash_in) .digest()) # Step 3 - Prepare GCTR cipher for encryption/decryption nonce_ctr = long_to_bytes(self._j0 >> 32, 12) iv_ctr = (self._j0 + 1) & 0xFFFFFFFF self._cipher = factory.new(key, self._factory.MODE_CTR, initial_value=iv_ctr, nonce=nonce_ctr, **cipher_params) # Step 5 - Bootstrat GHASH self._signer = _GHASH(hash_subkey, ghash_c) # Step 6 - Prepare GCTR cipher for GMAC self._tag_cipher = factory.new(key, self._factory.MODE_CTR, initial_value=self._j0, nonce=b"", **cipher_params) # Cache for data to authenticate self._cache = b"" self._status = MacStatus.PROCESSING_AUTH_DATA