Skip to content

Commit 56d48e6

Browse files
Add more tests
1 parent 13d1a4f commit 56d48e6

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

python/ql/test/experimental/query-tests/Security/CWE-208/TimingAttackAgainstHash/TimingAttackAgainstHash.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,26 @@
55
"""
66
import hashlib
77
import hmac
8+
from cryptography.hazmat.primitives import cmac
9+
from cryptography.hazmat.primitives.ciphers import algorithms
10+
from Crypto.Signature import pkcs1_15
11+
from Crypto.Hash import SHA256
12+
from Crypto.PublicKey import RSA
813

9-
key = "e179017a-62b0-4996-8a38-e91aa9f1"
14+
15+
def UnsafeCmacCheck(actualCmac):
16+
expected = cmac.CMAC(algorithms.AES(key))
17+
expected.update(b"message to authenticate")
18+
expected.finalize()
19+
return actualCmac == expected
20+
21+
22+
def UnsafeCheckSignature(expected):
23+
message = b'To be signed'
24+
key = RSA.import_key(open('private_key.der').read())
25+
h = SHA256.new(message)
26+
signature = pkcs1_15.new(key).sign(h)
27+
return expected == signature
1028

1129
def sign(pre_key, msg, alg):
1230
return hmac.new(pre_key, msg, alg).digest()
@@ -15,6 +33,7 @@ def verifyGood(msg, sig):
1533
return constant_time_string_compare(sig, sign(key, msg, hashlib.sha256)) #good
1634

1735
def verifyBad(msg, sig):
36+
key = "e179017a-62b0-4996-8a38-e91aa9f1"
1837
return sig == sign(key, msg, hashlib.sha256) #bad
1938

2039
def constant_time_string_compare(a, b):

0 commit comments

Comments
 (0)