1- import pytest
2- from packaging .version import Version
31import libnacl
42import libnacl .public
3+ import pytest
4+ from packaging .version import Version
5+
56from threema .gateway import (
67 e2e ,
78 key ,
@@ -26,6 +27,21 @@ def test_incorrect_ciphertext(self):
2627 e2e ._pk_decrypt (key_pair , nonce , data_encrypted + b'0' )
2728 assert 'decrypt' in str (exc_info .value )
2829
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+
2945 def test_valid (self ):
3046 key_pair = key .Key .generate_pair ()
3147 data_in = b'meow'
@@ -34,25 +50,12 @@ def test_valid(self):
3450 data_out = e2e ._pk_decrypt (key_pair , nonce , data_encrypted )
3551 assert data_in == data_out
3652
37- def test_invalid_pk (self ):
38- all_zero_pk = libnacl .public .PublicKey (bytes (libnacl .crypto_box_PUBLICKEYBYTES ))
39- key_pair = key .Key .generate_secret_key , all_zero_pk
40- data_in = b'meow'
41- nonce = b'0' * 24
42- with pytest .raises (libnacl .CryptError ) as exc_info :
43- e2e ._pk_encrypt (key_pair , data_in , nonce = nonce )
44- assert 'Invalid public key' in str (exc_info .value )
45-
46- with pytest .raises (libnacl .CryptError ) as exc_info :
47- e2e ._pk_decrypt (key_pair , nonce , bytes (5 ))
48- assert 'Invalid public key' in str (exc_info .value )
49-
50- @pytest .mark .skipif (Version (libnacl .sodium_version_string ().decode ("ascii" )) < Version ("1.0.7" ),
51- reason = "no zero-result check on X25519 in this version of libsodium" )
52- def test_contributory (self ):
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 ):
5357 all_zero_pk = libnacl .public .PublicKey (bytes (libnacl .crypto_box_PUBLICKEYBYTES ))
5458 alice_sk , _ = key .Key .generate_pair ()
5559 with pytest .raises (libnacl .CryptError ) as exc_info :
56- alice_box = libnacl .public .Box (alice_sk , all_zero_pk )
60+ libnacl .public .Box (alice_sk , all_zero_pk )
5761 assert 'Unable to compute shared key' in str (exc_info )
58-
0 commit comments