kitty/kitty_tests/crypto.py
Kovid Goyal 8f0b7eff31
Encrypt sessions saved at logout with a key in the macOS Keychain
Every snapshot is encrypted with AES-256-GCM using a random key, stored
in the login keychain under a random id. The name of every file is
authenticated, so files cannot be swapped. The key is deleted before the
snapshot whenever it is removed, so copies of the snapshot left on disk
or in backups cannot be read.

The keychain is never allowed to show any UI. If storing the key fails
nothing is saved, if reading it fails the snapshot is deleted, with the
error logged in both cases.

The key is generated, stored and read only in C, in mlocked memory that
is cleared when freed, via a new random_secret() and the Keychain
functions in cocoa_window.m.
2026-10-16 11:57:31 +05:30

96 lines
3.6 KiB
Python

#!/usr/bin/env python
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import os
import sys
from .base import BaseTest
def is_rlimit_memlock_too_low() -> bool:
"""On supported systems, return true if the MEMLOCK limit is too low to
run the crypto test."""
try:
import resource
except ModuleNotFoundError:
return False
memlock_limit, _ = resource.getrlimit(resource.RLIMIT_MEMLOCK)
pagesize = resource.getpagesize()
return memlock_limit <= pagesize
class TestCrypto(BaseTest):
def test_random_secret(self):
if is_rlimit_memlock_too_low():
self.skipTest('RLIMIT_MEMLOCK is too low')
from kitty.fast_data_types import AES256GCMDecrypt, AES256GCMEncrypt, random_secret
a, b = random_secret(), random_secret()
self.ae((len(a), len(random_secret(64))), (32, 64))
self.assertNotEqual(a, b)
with self.assertRaises(ValueError):
random_secret(0)
e = AES256GCMEncrypt(a)
ciphertext = e.add_data_to_be_encrypted(b'data', True)
self.ae(AES256GCMDecrypt(a, e.iv, e.tag).add_data_to_be_decrypted(ciphertext, True), b'data')
def test_elliptic_curve_data_exchange(self):
if is_rlimit_memlock_too_low():
self.skipTest('RLIMIT_MEMLOCK is too low')
from kitty.fast_data_types import AES256GCMDecrypt, AES256GCMEncrypt, CryptoError, EllipticCurveKey
alice = EllipticCurveKey()
bob = EllipticCurveKey()
alice_secret = alice.derive_secret(bob.public)
bob_secret = bob.derive_secret(alice.public)
self.assertEqual(len(alice_secret), 32)
self.assertEqual(len(bob_secret), 32)
self.assertEqual(alice_secret, bob_secret)
auth_data = os.urandom(213)
plaintext = os.urandom(1011)
e = AES256GCMEncrypt(alice_secret)
e.add_authenticated_but_unencrypted_data(auth_data)
ciphertext = e.add_data_to_be_encrypted(plaintext, True)
d = AES256GCMDecrypt(bob_secret, e.iv, e.tag)
d.add_data_to_be_authenticated_but_not_decrypted(auth_data)
q = d.add_data_to_be_decrypted(ciphertext, True)
self.ae(q, plaintext)
def corrupt_data(data):
b = bytearray(data)
b[0] = (b[0] + 13) % 256
return bytes(b)
d = AES256GCMDecrypt(bob_secret, e.iv, corrupt_data(e.tag))
d.add_data_to_be_authenticated_but_not_decrypted(auth_data)
self.assertRaises(CryptoError, d.add_data_to_be_decrypted, ciphertext, True)
d = AES256GCMDecrypt(bob_secret, e.iv, e.tag)
d.add_data_to_be_authenticated_but_not_decrypted(corrupt_data(auth_data))
self.assertRaises(CryptoError, d.add_data_to_be_decrypted, ciphertext, True)
d = AES256GCMDecrypt(bob_secret, e.iv, e.tag)
d.add_data_to_be_authenticated_but_not_decrypted(auth_data)
self.assertRaises(CryptoError, d.add_data_to_be_decrypted, corrupt_data(ciphertext), True)
def test_short_tag_does_not_leak(self):
if is_rlimit_memlock_too_low():
self.skipTest('RLIMIT_MEMLOCK is too low')
from kitty.fast_data_types import AES256GCMDecrypt, EllipticCurveKey
secret = EllipticCurveKey().derive_secret(EllipticCurveKey().public)
iv = os.urandom(12)
def attempt():
with self.assertRaises(ValueError):
AES256GCMDecrypt(secret, iv, b'short')
attempt()
before = sys.getallocatedblocks()
for i in range(10000):
attempt()
self.assertLess(sys.getallocatedblocks() - before, 1000)