Skip to content

Commit fcfaee8

Browse files
author
Memo Ugurbil
committed
fix: seed is part of store operation
1 parent b8bbee8 commit fcfaee8

File tree

2 files changed

+17
-81
lines changed

2 files changed

+17
-81
lines changed

src/nilql/nilql.py

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def generate(
362362

363363
if (
364364
(not isinstance(operations, dict)) or
365-
(not set(operations.keys()).issubset({'store', 'seed', 'match', 'sum'}))
365+
(not set(operations.keys()).issubset({'store', 'match', 'sum'}))
366366
):
367367
raise ValueError('valid operations specification is required')
368368

@@ -393,14 +393,6 @@ def generate(
393393
bytes.__new__(bcl.secret, _random_bytes(32, seed))
394394
)
395395

396-
if secret_key['operations'].get('seed'):
397-
# Symmetric key for encrypting the plaintext or the shares of a plaintext.
398-
secret_key['material'] = (
399-
bcl.symmetric.secret()
400-
if seed is None else
401-
bytes.__new__(bcl.secret, _random_bytes(32, seed))
402-
)
403-
404396
if secret_key['operations'].get('match'):
405397
# Salt for deterministic hashing of the plaintext.
406398
secret_key['material'] = _random_bytes(64, seed)
@@ -743,41 +735,19 @@ def encrypt(
743735
shares = []
744736
aggregate = bytes(len(buffer))
745737
for _ in range(len(key['cluster']['nodes']) - 1):
746-
mask = _random_bytes(len(buffer))
738+
if len(buffer) >= 64:
739+
seed = _random_bytes(64)
740+
mask = _random_bytes(len(buffer), seed)
741+
shares.append(optional_enc(seed))
742+
else:
743+
mask = _random_bytes(len(buffer))
744+
shares.append(optional_enc(mask))
747745
aggregate = bytes(a ^ b for (a, b) in zip(aggregate, mask))
748-
shares.append(optional_enc(mask))
749746
shares.append(optional_enc(
750747
bytes(a ^ b for (a, b) in zip(aggregate, buffer))
751748
))
752749
return list(map(_pack, shares))
753750

754-
# Encrypt a plaintext for storage and retrieval.
755-
if key['operations'].get('seed'):
756-
# For single-node clusters, the data is encrypted using a symmetric key.
757-
if len(key['cluster']['nodes']) == 1:
758-
return _pack(
759-
bcl.symmetric.encrypt(key['material'], bcl.plain(buffer))
760-
)
761-
762-
# For multiple-node clusters, the ciphertext is secret-shared using XOR
763-
# (with each share symmetrically encrypted in the case of a secret key).
764-
optional_enc = (
765-
(lambda s: bcl.symmetric.encrypt(key['material'], bcl.plain(s)))
766-
if 'material' in key else
767-
(lambda s: s)
768-
)
769-
seeds = []
770-
aggregate = bytes(len(buffer))
771-
for _ in range(len(key['cluster']['nodes']) - 1):
772-
seed = _random_bytes(64)
773-
seeds.append(optional_enc(seed))
774-
mask = _random_bytes(len(buffer), seed)
775-
aggregate = bytes(a ^ b for (a, b) in zip(aggregate, mask))
776-
share = optional_enc(
777-
bytes(a ^ b for (a, b) in zip(aggregate, buffer))
778-
)
779-
return list(map(_pack, seeds)) + [_pack(share)]
780-
781751
# Encrypt (i.e., hash) a plaintext for matching.
782752
if key['operations'].get('match'):
783753
# The deterministic salted hash of the encoded plaintext is the ciphertext.
@@ -978,43 +948,11 @@ def decrypt(
978948
except Exception as exc:
979949
raise error from exc
980950

981-
bytes_ = bytes(len(shares[0]))
982-
for share_ in shares:
983-
bytes_ = bytes(a ^ b for (a, b) in zip(bytes_, share_))
984-
985-
return _decode(bytes_)
986-
987-
# Decrypt a value that was encrypted for storage and retrieval.
988-
if key['operations'].get('seed'):
989-
# For single-node clusters, the plaintext is encrypted using a symmetric key.
990-
if len(key['cluster']['nodes']) == 1:
991-
try:
992-
return _decode(
993-
bcl.symmetric.decrypt(
994-
key['material'],
995-
bcl.cipher(_unpack(ciphertext))
996-
)
997-
)
998-
except Exception as exc:
999-
raise error from exc
1000-
1001-
# For multiple-node clusters, the ciphertext is secret-shared using XOR
1002-
# (with each share symmetrically encrypted in the case of a secret key).
1003-
shares = [_unpack(share) for share in ciphertext]
1004-
if 'material' in key:
1005-
try:
1006-
shares = [
1007-
bcl.symmetric.decrypt(key['material'], bcl.cipher(share))
1008-
for share in shares
1009-
]
1010-
except Exception as exc:
1011-
raise error from exc
1012-
1013-
bytes_ = bytes(len(shares[-1]))
951+
bytes_ = bytes(shares[-1])
1014952
for share_ in shares[:-1]:
1015-
share_ = _random_bytes(len(bytes_), share_)
953+
if len(bytes_) != len(share_):
954+
share_ = _random_bytes(len(bytes_), share_)
1016955
bytes_ = bytes(a ^ b for (a, b) in zip(bytes_, share_))
1017-
bytes_ = bytes(a ^ b for (a, b) in zip(bytes_, shares[-1]))
1018956

1019957
return _decode(bytes_)
1020958

test/test_nilql.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,13 @@ def test_encrypt_decrypt_for_seed(self):
313313
"""
314314
Test encryption and decryption for storing.
315315
"""
316-
for cluster in [{'nodes': [{}]}, {'nodes': [{}, {}, {}]}]:
317-
sk = nilql.SecretKey.generate(cluster, {'seed': True})
318-
319-
plaintext = 123
320-
decrypted = nilql.decrypt(sk, nilql.encrypt(sk, plaintext))
321-
self.assertEqual(decrypted, plaintext)
316+
for cluster in [{'nodes': [{}, {}, {}]}]:
317+
sk = nilql.SecretKey.generate(cluster, {'store': True})
322318

323-
plaintext = 'abc'
324-
decrypted = nilql.decrypt(sk, nilql.encrypt(sk, plaintext))
319+
plaintext = "Bart Simpson is a fictional character in the American animated television series The Simpsons who is part of the Simpson family. Described as one of the 100 most important people of the 20th century by Time, Bart was created and designed by Matt Groening in James L. Brooks's office. Bart, alongside the rest of the family, debuted in the short 'Good Night' on The Tracey Ullman Show on April 19, 1987. Two years later, the family received their own series, which premiered on Fox on December 17, 1989. Born on April Fools' Day according to Groening, Bart is ten years old; he is the eldest child and only son of Homer and Marge Simpson, and has two sisters, Lisa and Maggie. Voiced by Nancy Cartwright (pictured), Bart is known for his mischievousness, rebelliousness, and disrespect for authority, as well as his prank calls to Moe, chalkboard gags in the opening sequence, and catchphrases. Bart is considered an iconic fictional television character of the 1990s and has been called an American cultural icon."
320+
e = nilql.encrypt(sk, plaintext)
321+
self.assertEqual((len(e[0]), len(e[1]), len(e[2])), (140, 140, 1408))
322+
decrypted = nilql.decrypt(sk, e)
325323
self.assertEqual(decrypted, plaintext)
326324

327325
def test_encrypt_for_match(self):

0 commit comments

Comments
 (0)