11import libnacl
2+ import libnacl .public
23import pytest
4+ from packaging .version import Version
35
46from threema .gateway import (
57 e2e ,
@@ -25,10 +27,35 @@ def test_incorrect_ciphertext(self):
2527 e2e ._pk_decrypt (key_pair , nonce , data_encrypted + b'0' )
2628 assert 'decrypt' in str (exc_info .value )
2729
30+ def test_invalid_non_contributory_public_key (self ):
31+ all_zero_public_key = libnacl .public .PublicKey (
32+ bytes (libnacl .crypto_box_PUBLICKEYBYTES ))
33+ key_pair = key .Key .generate_secret_key , all_zero_public_key
34+ data_in = b"meow"
35+ nonce = b"0" * 24
36+
37+ with pytest .raises (libnacl .CryptError ) as exc_info :
38+ e2e ._pk_encrypt (key_pair , data_in , nonce = nonce )
39+ assert "Invalid public key (non-contributory)" in str (exc_info .value )
40+
41+ with pytest .raises (libnacl .CryptError ) as exc_info :
42+ e2e ._pk_decrypt (key_pair , nonce , bytes (5 ))
43+ assert "Invalid public key (non-contributory)" in str (exc_info .value )
44+
2845 def test_valid (self ):
2946 key_pair = key .Key .generate_pair ()
3047 data_in = b'meow'
3148 nonce = b'0' * 24
3249 _ , data_encrypted = e2e ._pk_encrypt (key_pair , data_in , nonce = nonce )
3350 data_out = e2e ._pk_decrypt (key_pair , nonce , data_encrypted )
3451 assert data_in == data_out
52+
53+ @pytest .mark .skipif (
54+ Version (libnacl .sodium_version_string ().decode ("ascii" )) < Version ("1.0.7" ),
55+ reason = "no zero-result check on X25519 in this libnacl backend" )
56+ def test_libsodium_non_contributory_public_key (self ):
57+ all_zero_pk = libnacl .public .PublicKey (bytes (libnacl .crypto_box_PUBLICKEYBYTES ))
58+ alice_sk , _ = key .Key .generate_pair ()
59+ with pytest .raises (libnacl .CryptError ) as exc_info :
60+ libnacl .public .Box (alice_sk , all_zero_pk )
61+ assert 'Unable to compute shared key' in str (exc_info )
0 commit comments