Skip to content

Commit bf6f507

Browse files
committed
Python: Port cryptodome tests to crypto
I don't know if this is really a smart test-setup... I feel a bit stupid when doing this xD
1 parent f825438 commit bf6f507

File tree

6 files changed

+28
-30
lines changed

6 files changed

+28
-30
lines changed

python/ql/test/experimental/library-tests/frameworks/crypto/test_aes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
2222
# using separate .encrypt calls on individual lines does not work
2323
whole_plantext = secret_message + padding
24-
encrypted = cipher.encrypt(whole_plantext)
24+
encrypted = cipher.encrypt(whole_plantext) # $ CryptographicOperation CryptographicOperationAlgorithm=AES CryptographicOperationInput=whole_plantext
2525

2626
print("encrypted={}".format(encrypted))
2727

2828
print()
2929

3030
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
31-
decrypted = cipher.decrypt(encrypted)
31+
decrypted = cipher.decrypt(encrypted) # $ CryptographicOperation CryptographicOperationAlgorithm=AES CryptographicOperationInput=encrypted
3232

3333
decrypted = decrypted[:-padding_len]
3434

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from Crypto.Hash import MD5
22

3-
hasher = MD5.new(b"secret message") # $ MISSING: CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=MD5
3+
hasher = MD5.new(b"secret message") # $ CryptographicOperation CryptographicOperationInput=b"secret message" CryptographicOperationAlgorithm=MD5
44
print(hasher.hexdigest())
55

66

7-
hasher = MD5.new() # $ MISSING: CryptographicOperation CryptographicOperationAlgorithm=MD5
8-
hasher.update(b"secret") # $ MISSING: CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=MD5
9-
hasher.update(b" message") # $ MISSING: CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=MD5
7+
hasher = MD5.new() # $ CryptographicOperation CryptographicOperationAlgorithm=MD5
8+
hasher.update(b"secret") # $ CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=MD5
9+
hasher.update(b" message") # $ CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=MD5
1010
print(hasher.hexdigest())

python/ql/test/experimental/library-tests/frameworks/crypto/test_rc4.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
secret_message = b"secret message"
1818

1919
cipher = ARC4.new(key)
20-
encrypted = cipher.encrypt(secret_message)
20+
encrypted = cipher.encrypt(secret_message) # $ CryptographicOperation CryptographicOperationAlgorithm=ARC4 CryptographicOperationInput=secret_message
2121

2222
print("encrypted={}".format(encrypted))
2323

2424
print()
2525

2626
cipher = ARC4.new(key)
27-
decrypted = cipher.decrypt(encrypted)
27+
decrypted = cipher.decrypt(encrypted) # $ CryptographicOperation CryptographicOperationAlgorithm=ARC4 CryptographicOperationInput=encrypted
2828

2929
print("decrypted={}".format(decrypted))
3030
assert decrypted == secret_message

python/ql/test/library-tests/frameworks/crypto/test_dsa.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020

2121
signer = DSS.new(private_key, mode='fips-186-3')
2222

23-
hasher = SHA256.new(message)
24-
signature = signer.sign(hasher)
23+
hasher = SHA256.new(message) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message
24+
signature = signer.sign(hasher) # $ CryptographicOperation CryptographicOperationInput=hasher # MISSING: CryptographicOperationAlgorithm=DSA
2525

2626
print("signature={}".format(signature))
2727

2828
print()
2929

3030
verifier = DSS.new(public_key, mode='fips-186-3')
3131

32-
hasher = SHA256.new(message)
33-
verifier.verify(hasher, signature)
32+
hasher = SHA256.new(message) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message
33+
verifier.verify(hasher, signature) # $ CryptographicOperation CryptographicOperationInput=hasher CryptographicOperationInput=signature # MISSING: CryptographicOperationAlgorithm=DSA
3434
print("Signature verified (as expected)")
3535

3636
try:
37-
hasher = SHA256.new(b"other message")
38-
verifier.verify(hasher, signature)
37+
hasher = SHA256.new(b"other message") # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=b"other message"
38+
verifier.verify(hasher, signature) # $ CryptographicOperation CryptographicOperationInput=hasher CryptographicOperationInput=signature # MISSING: CryptographicOperationAlgorithm=DSA
3939
raise Exception("Signature verified (unexpected)")
4040
except ValueError:
4141
print("Signature mismatch (as expected)")

python/ql/test/library-tests/frameworks/crypto/test_ec.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
signer = DSS.new(private_key, mode='fips-186-3')
1919

20-
hasher = SHA256.new(message)
21-
signature = signer.sign(hasher)
20+
hasher = SHA256.new(message) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message
21+
signature = signer.sign(hasher) # $ CryptographicOperation CryptographicOperationInput=hasher # MISSING: CryptographicOperationAlgorithm=ECDSA
2222

2323
print("signature={}".format(signature))
2424

2525
print()
2626

2727
verifier = DSS.new(public_key, mode='fips-186-3')
2828

29-
hasher = SHA256.new(message)
30-
verifier.verify(hasher, signature)
29+
hasher = SHA256.new(message) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message
30+
verifier.verify(hasher, signature) # $ CryptographicOperation CryptographicOperationInput=hasher CryptographicOperationInput=signature
3131
print("Signature verified (as expected)")
3232

3333
try:
34-
hasher = SHA256.new(b"other message")
35-
verifier.verify(hasher, signature)
34+
hasher = SHA256.new(b"other message") # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=b"other message"
35+
verifier.verify(hasher, signature) # $ CryptographicOperation CryptographicOperationInput=hasher CryptographicOperationInput=signature # MISSING: CryptographicOperationAlgorithm=ECDSA
3636
raise Exception("Signature verified (unexpected)")
3737
except ValueError:
3838
print("Signature mismatch (as expected)")

python/ql/test/library-tests/frameworks/crypto/test_rsa.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323

2424
encrypt_cipher = PKCS1_OAEP.new(public_key)
2525

26-
encrypted = encrypt_cipher.encrypt(secret_message)
26+
encrypted = encrypt_cipher.encrypt(secret_message) # $ CryptographicOperation CryptographicOperationInput=secret_message # MISSING: CryptographicOperationAlgorithm=RSA-OAEP?
2727

2828
print("encrypted={}".format(encrypted))
2929

3030
print()
3131

3232
decrypt_cipher = PKCS1_OAEP.new(private_key)
3333

34-
decrypted = decrypt_cipher.decrypt(
35-
encrypted,
36-
)
34+
decrypted = decrypt_cipher.decrypt(encrypted) # $ CryptographicOperation CryptographicOperationInput=encrypted # MISSING: CryptographicOperationAlgorithm=RSA-OAEP?
3735

3836
print("decrypted={}".format(decrypted))
3937
assert decrypted == secret_message
@@ -51,23 +49,23 @@
5149

5250
signer = pss.new(private_key)
5351

54-
hasher = SHA256.new(message)
55-
signature = signer.sign(hasher)
52+
hasher = SHA256.new(message) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message
53+
signature = signer.sign(hasher) # $ CryptographicOperation CryptographicOperationInput=hasher # MISSING: CryptographicOperationAlgorithm=RSA-PSS?
5654

5755
print("signature={}".format(signature))
5856

5957
print()
6058

6159
verifier = pss.new(public_key)
6260

63-
hasher = SHA256.new(message)
64-
verifier.verify(hasher, signature)
61+
hasher = SHA256.new(message) # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=message
62+
verifier.verify(hasher, signature) # $ CryptographicOperation CryptographicOperationInput=hasher CryptographicOperationInput=signature # MISSING: CryptographicOperationAlgorithm=RSA-PSS?
6563
print("Signature verified (as expected)")
6664

6765
try:
6866
verifier = pss.new(public_key)
69-
hasher = SHA256.new(b"other message")
70-
verifier.verify(hasher, signature)
67+
hasher = SHA256.new(b"other message") # $ CryptographicOperation CryptographicOperationAlgorithm=SHA256 CryptographicOperationInput=b"other message"
68+
verifier.verify(hasher, signature) # $ CryptographicOperation CryptographicOperationInput=hasher CryptographicOperationInput=signature # MISSING: CryptographicOperationAlgorithm=RSA-PSS?
7169
raise Exception("Signature verified (unexpected)")
7270
except ValueError:
7371
print("Signature mismatch (as expected)")

0 commit comments

Comments
 (0)