def decrypt_key_material(enc_str1, enc_str2, enc_str3, rounds): hash_material = base64.b64decode(enc_str1) hash_material_hash = monocypher.blake2b(hash_material) nonce = hash_material_hash[0:24] hash_material_hash = hash_material for i in range(rounds): hash_material_hash = monocypher.blake2b(hash_material_hash) slice1 = hash_material_hash[:32] slice2 = hash_material_hash[32:] key = [] for i, b1 in enumerate(slice1): kb = b1 ^ slice2[i] key.append(kb) key = bytes(key) mac = base64.b64decode(enc_str2) encbuf = base64.b64decode(enc_str3) key_material = monocypher.unlock(key, nonce, mac, encbuf) return key_material
def decrypt_response(key, response_data): response_data_nob64 = base64.b64decode(response_data) nonce = response_data_nob64[0:24] ciphertext = response_data_nob64[24:-16] mac = response_data_nob64[-16:] plain_response = monocypher.unlock(key, nonce, mac, ciphertext) return plain_response
def test_symmetric(self): random = np.random.RandomState(seed=1) for i in range(10): length = random.randint(1, 4096) key = bytes(random.randint(0, 256, 32, dtype=np.uint8)) nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8)) msg = bytes(random.randint(0, 256, length, dtype=np.uint8)) mac, c = monocypher.lock(key, nonce, msg) msg2 = monocypher.unlock(key, nonce, mac, c) self.assertNotEqual(msg, c) self.assertEqual(msg, msg2)
def test_symmetric_aead(self): random = np.random.RandomState(seed=1) for i in range(10): message_length = random.randint(1, 4096) aead_length = random.randint(1, 128) key = bytes(random.randint(0, 256, 32, dtype=np.uint8)) nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8)) aead = bytes(random.randint(0, 256, aead_length, dtype=np.uint8)) msg = bytes(random.randint(0, 256, message_length, dtype=np.uint8)) mac, c = monocypher.lock(key, nonce, msg, associated_data=aead) msg2 = monocypher.unlock(key, nonce, mac, c, associated_data=aead) self.assertEqual(msg, msg2)
def test_symmetric(self): random = np.random.RandomState(seed=1) for i in range(10): length = random.randint(1, 4096) key = bytes(random.randint(0, 256, 32, dtype=np.uint8)) nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8)) msg = bytes(random.randint(0, 256, length, dtype=np.uint8)) mac, c = monocypher.lock(key, nonce, msg) msg2 = monocypher.unlock(key, nonce, mac, c) self.assertNotEqual(msg, c) self.assertEqual(msg, msg2) e = monocypher.Encrypt(key, nonce) c2 = e.update(msg) mac2 = e.finalize() self.assertEqual(mac, mac2) self.assertEqual(c, c2) d = monocypher.Decrypt(key, nonce) msg3 = d.update(c2) self.assertEqual(0, d.finalize(mac2)) self.assertEqual(msg, msg3)
def decrypt(self, signing_key, encryption_key, nonce, associated_data=None): """Decrypt the next tag, if needed""" tag, flags, value, _ = self._read_tag() if flags & FLAG_ENCRYPT: tag2, _, value2, _ = self._read_tag() if tag2 != TAG_ENCRYPTION: raise ValueError('Encrypted data must be followed by ENC tag') mac = value2[:16] signature = value2[16:] value = monocypher.unlock(encryption_key, nonce, mac, value, associated_data) if value is None: raise ValueError('Decryption failed') if not monocypher.signature_check(signature, signing_key, value): raise ValueError('Signature check failed') if flags & FLAG_COMPRESS: value = zlib.decompress(value) return tag, value
def test_symmetric_aead(self): random = np.random.RandomState(seed=1) for i in range(10): message_length = random.randint(1, 4096) aead_length = random.randint(1, 128) key = bytes(random.randint(0, 256, 32, dtype=np.uint8)) nonce = bytes(random.randint(0, 256, 24, dtype=np.uint8)) aead = bytes(random.randint(0, 256, aead_length, dtype=np.uint8)) msg = bytes(random.randint(0, 256, message_length, dtype=np.uint8)) mac, c = monocypher.lock(key, nonce, msg, associated_data=aead) msg2 = monocypher.unlock(key, nonce, mac, c, associated_data=aead) self.assertEqual(msg, msg2) e = monocypher.Encrypt(key, nonce, associated_data=aead) c2 = e.update(msg) mac2 = e.finalize() self.assertEqual(mac, mac2) self.assertEqual(c, c2) d = monocypher.Decrypt(key, nonce, associated_data=aead) msg3 = d.update(c2) self.assertEqual(0, d.finalize(mac2)) self.assertEqual(msg, msg3)