11from __future__ import with_statement , division
22
3- import unittest
4- import os
5- import time
6- import shutil
7- import subprocess
83import pytest
9- from binascii import hexlify , unhexlify
10- from hashlib import sha1 , sha256 , sha512
114import hashlib
125
136from six import b , binary_type
147from .keys import SigningKey , VerifyingKey
158from .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
1911from .curves import curves , NIST256p , NIST521p
20- from .ellipticcurve import Point
21- from . import der
22- from . import rfc6979
23- import copy
2412
25- sigs = []
13+ der_sigs = []
2614example_data = b ("some data to sign" )
2715
2816# Just NIST256p with SHA256 is 560 test cases, all curves with all hashes is
3725 sigencode = sigencode_der )
3826 for pos in range (len (signature )):
3927 for xor in (1 << i for i in range (8 )):
40- sigs .append (pytest .param (
28+ der_sigs .append (pytest .param (
4129 key .verifying_key , hash_alg ,
4230 signature , pos , xor ,
4331 id = "{0}-{1}-pos-{2}-xor-{3}" .format (
4432 curve .name , hash_alg , pos , xor )))
4533
4634
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 )
4836def test_fuzzed_der_signatures (verifying_key , hash_alg , signature , pos , xor ):
4937 # check if a malformed DER encoded signature causes the same exception
5038 # 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):
6048 assert True
6149
6250
63- def __main__ ():
64- unittest .main ()
51+ ####
52+ #
53+ # For string encoded signatures, only the length of string is important
54+ #
55+ ####
6556
57+ str_sigs = []
6658
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