1
1
from __future__ import with_statement , division
2
2
3
- import unittest
4
- import os
5
- import time
6
- import shutil
7
- import subprocess
8
3
import pytest
9
- from binascii import hexlify , unhexlify
10
- from hashlib import sha1 , sha256 , sha512
11
4
import hashlib
12
5
13
6
from six import b , binary_type
14
7
from .keys import SigningKey , VerifyingKey
15
8
from .keys import BadSignatureError
16
- from . import util
17
- from .util import sigencode_der , sigencode_strings
18
- from .util import sigdecode_der , sigdecode_strings
9
+ from .util import sigencode_der , sigencode_string
10
+ from .util import sigdecode_der , sigdecode_string
19
11
from .curves import curves , NIST256p , NIST521p
20
- from .ellipticcurve import Point
21
- from . import der
22
- from . import rfc6979
23
- import copy
24
12
25
- sigs = []
13
+ der_sigs = []
26
14
example_data = b ("some data to sign" )
27
15
28
16
# Just NIST256p with SHA256 is 560 test cases, all curves with all hashes is
37
25
sigencode = sigencode_der )
38
26
for pos in range (len (signature )):
39
27
for xor in (1 << i for i in range (8 )):
40
- sigs .append (pytest .param (
28
+ der_sigs .append (pytest .param (
41
29
key .verifying_key , hash_alg ,
42
30
signature , pos , xor ,
43
31
id = "{0}-{1}-pos-{2}-xor-{3}" .format (
44
32
curve .name , hash_alg , pos , xor )))
45
33
46
34
47
- @pytest .mark .parametrize ("verifying_key,hash_alg,signature,pos,xor" , sigs )
35
+ @pytest .mark .parametrize ("verifying_key,hash_alg,signature,pos,xor" , der_sigs )
48
36
def test_fuzzed_der_signatures (verifying_key , hash_alg , signature , pos , xor ):
49
37
# check if a malformed DER encoded signature causes the same exception
50
38
# to be raised irrespective of the type of error
@@ -60,9 +48,40 @@ def test_fuzzed_der_signatures(verifying_key, hash_alg, signature, pos, xor):
60
48
assert True
61
49
62
50
63
- def __main__ ():
64
- unittest .main ()
51
+ ####
52
+ #
53
+ # For string encoded signatures, only the length of string is important
54
+ #
55
+ ####
65
56
57
+ str_sigs = []
66
58
67
- if __name__ == "__main__" :
68
- __main__ ()
59
+ #for curve in curves:
60
+ for curve in [NIST256p ]:
61
+ #for hash_alg in ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"]:
62
+ for hash_alg in ["sha256" ]:
63
+ key = SigningKey .generate (curve )
64
+ signature = key .sign (example_data , hashfunc = getattr (hashlib , hash_alg ),
65
+ sigencode = sigencode_string )
66
+ for trunc in range (len (signature )):
67
+ str_sigs .append (pytest .param (
68
+ key .verifying_key , hash_alg ,
69
+ signature , trunc ,
70
+ id = "{0}-{1}-trunc-{2}" .format (
71
+ curve .name , hash_alg , trunc )))
72
+
73
+
74
+ @pytest .mark .parametrize ("verifying_key,hash_alg,signature,trunc" , str_sigs )
75
+ def test_truncated_string_signatures (verifying_key , hash_alg , signature , trunc ):
76
+ # check if a malformed string encoded signature causes the same exception
77
+ # to be raised irrespective of the type of error
78
+ sig = bytearray (signature )
79
+ sig = sig [:trunc ]
80
+ sig = binary_type (sig )
81
+
82
+ try :
83
+ verifying_key .verify (sig , example_data , getattr (hashlib , hash_alg ),
84
+ sigdecode_string )
85
+ assert False
86
+ except BadSignatureError :
87
+ assert True
0 commit comments