def decrypt_str(self, content, key, salt): # content contains the gcm tag and salt_explicit in plaintext salt_explicit, gcm_tag = struct.unpack_from('!q16s', content) cipher = Cipher(algorithms.AES(key), modes.GCM(initialization_vector=self._bulid_iv(salt, salt_explicit), tag=gcm_tag), backend=default_backend() ).decryptor() return cipher.update(content[24:]) + cipher.finalize()
def encrypt_str(self, content, key, salt, salt_explicit): # return the encrypted content prepended with the # gcm tag and salt_explicit cipher = Cipher(algorithms.AES(key), modes.GCM(initialization_vector=self._bulid_iv(salt, salt_explicit)), backend=default_backend() ).encryptor() ciphertext = cipher.update(content) + cipher.finalize() return struct.pack('!q16s', salt_explicit, cipher.tag) + ciphertext
def generate_session_keys(self, shared_secret): hkdf = HKDFExpand(algorithm=hashes.SHA256(), backend=default_backend(), length=40, info="key_generation") key = hkdf.derive(shared_secret) kf = key[:16] kb = key[16:32] sf = key[32:36] sb = key[36:40] return [kf, kb, sf, sb, 1, 1]
def decrypt_str(self, content, key, salt): # content contains the gcm tag and salt_explicit in plaintext salt_explicit, gcm_tag = struct.unpack_from('!q16s', content) cipher = Cipher(algorithms.AES(key), modes.GCM(initialization_vector=self._bulid_iv( salt, salt_explicit), tag=gcm_tag), backend=default_backend()).decryptor() return cipher.update(content[24:]) + cipher.finalize()