1111import pytest
1212from binascii import hexlify , unhexlify
1313from hashlib import sha1 , sha256 , sha512
14+ import hashlib
15+ from functools import partial
1416
1517from hypothesis import given
1618import hypothesis .strategies as st
@@ -708,15 +710,15 @@ class OpenSSL(unittest.TestCase):
708710 run_openssl ("ecparam -list_curves" )
709711 .split ('\n ' ))
710712
711- def get_openssl_messagedigest_arg (self ):
713+ def get_openssl_messagedigest_arg (self , hash_name ):
712714 v = run_openssl ("version" )
713715 # e.g. "OpenSSL 1.0.0 29 Mar 2010", or "OpenSSL 1.0.0a 1 Jun 2010",
714716 # or "OpenSSL 0.9.8o 01 Jun 2010"
715717 vs = v .split ()[1 ].split ("." )
716718 if vs >= ["1" , "0" , "0" ]: # pragma: no cover
717- return "-SHA1"
719+ return "-{0}" . format ( hash_name )
718720 else : # pragma: no cover
719- return "-ecdsa-with-SHA1"
721+ return "-ecdsa-with-{0}" . format ( hash_name )
720722
721723 # sk: 1:OpenSSL->python 2:python->OpenSSL
722724 # vk: 3:OpenSSL->python 4:python->OpenSSL
@@ -727,6 +729,11 @@ def get_openssl_messagedigest_arg(self):
727729 def test_from_openssl_nist192p (self ):
728730 return self .do_test_from_openssl (NIST192p )
729731
732+ @pytest .mark .skipif ("prime192v1" not in OPENSSL_SUPPORTED_CURVES ,
733+ reason = "system openssl does not support prime192v1" )
734+ def test_from_openssl_nist192p_sha256 (self ):
735+ return self .do_test_from_openssl (NIST192p , "SHA256" )
736+
730737 @pytest .mark .skipif ("secp224r1" not in OPENSSL_SUPPORTED_CURVES ,
731738 reason = "system openssl does not support secp224r1" )
732739 def test_from_openssl_nist224p (self ):
@@ -737,6 +744,16 @@ def test_from_openssl_nist224p(self):
737744 def test_from_openssl_nist256p (self ):
738745 return self .do_test_from_openssl (NIST256p )
739746
747+ @pytest .mark .skipif ("prime256v1" not in OPENSSL_SUPPORTED_CURVES ,
748+ reason = "system openssl does not support prime256v1" )
749+ def test_from_openssl_nist256p_sha384 (self ):
750+ return self .do_test_from_openssl (NIST256p , "SHA384" )
751+
752+ @pytest .mark .skipif ("prime256v1" not in OPENSSL_SUPPORTED_CURVES ,
753+ reason = "system openssl does not support prime256v1" )
754+ def test_from_openssl_nist256p_sha512 (self ):
755+ return self .do_test_from_openssl (NIST256p , "SHA512" )
756+
740757 @pytest .mark .skipif ("secp384r1" not in OPENSSL_SUPPORTED_CURVES ,
741758 reason = "system openssl does not support secp384r1" )
742759 def test_from_openssl_nist384p (self ):
@@ -787,12 +804,12 @@ def test_from_openssl_brainpoolp384r1(self):
787804 def test_from_openssl_brainpoolp512r1 (self ):
788805 return self .do_test_from_openssl (BRAINPOOLP512r1 )
789806
790- def do_test_from_openssl (self , curve ):
807+ def do_test_from_openssl (self , curve , hash_name = "SHA1" ):
791808 curvename = curve .openssl_name
792809 assert curvename
793810 # OpenSSL: create sk, vk, sign.
794811 # Python: read vk(3), checksig(5), read sk(1), sign, check
795- mdarg = self .get_openssl_messagedigest_arg ()
812+ mdarg = self .get_openssl_messagedigest_arg (hash_name )
796813 if os .path .isdir ("t" ): # pragma: no cover
797814 shutil .rmtree ("t" )
798815 os .mkdir ("t" )
@@ -809,19 +826,30 @@ def do_test_from_openssl(self, curve):
809826 with open ("t/data.sig" , "rb" ) as e :
810827 sig_der = e .read ()
811828 self .assertTrue (vk .verify (sig_der , data , # 5
812- hashfunc = sha1 , sigdecode = sigdecode_der ))
829+ hashfunc = partial (hashlib .new , hash_name ),
830+ sigdecode = sigdecode_der ))
813831
814832 with open ("t/privkey.pem" ) as e :
815833 fp = e .read ()
816834 sk = SigningKey .from_pem (fp ) # 1
817- sig = sk .sign (data )
818- self .assertTrue (vk .verify (sig , data ))
835+ sig = sk .sign (
836+ data ,
837+ hashfunc = partial (hashlib .new , hash_name ),
838+ )
839+ self .assertTrue (vk .verify (sig ,
840+ data ,
841+ hashfunc = partial (hashlib .new , hash_name )))
819842
820843 @pytest .mark .skipif ("prime192v1" not in OPENSSL_SUPPORTED_CURVES ,
821844 reason = "system openssl does not support prime192v1" )
822845 def test_to_openssl_nist192p (self ):
823846 self .do_test_to_openssl (NIST192p )
824847
848+ @pytest .mark .skipif ("prime192v1" not in OPENSSL_SUPPORTED_CURVES ,
849+ reason = "system openssl does not support prime192v1" )
850+ def test_to_openssl_nist192p_sha256 (self ):
851+ self .do_test_to_openssl (NIST192p , "SHA256" )
852+
825853 @pytest .mark .skipif ("secp224r1" not in OPENSSL_SUPPORTED_CURVES ,
826854 reason = "system openssl does not support secp224r1" )
827855 def test_to_openssl_nist224p (self ):
@@ -832,6 +860,16 @@ def test_to_openssl_nist224p(self):
832860 def test_to_openssl_nist256p (self ):
833861 self .do_test_to_openssl (NIST256p )
834862
863+ @pytest .mark .skipif ("prime256v1" not in OPENSSL_SUPPORTED_CURVES ,
864+ reason = "system openssl does not support prime256v1" )
865+ def test_to_openssl_nist256p_sha384 (self ):
866+ self .do_test_to_openssl (NIST256p , "SHA384" )
867+
868+ @pytest .mark .skipif ("prime256v1" not in OPENSSL_SUPPORTED_CURVES ,
869+ reason = "system openssl does not support prime256v1" )
870+ def test_to_openssl_nist256p_sha512 (self ):
871+ self .do_test_to_openssl (NIST256p , "SHA512" )
872+
835873 @pytest .mark .skipif ("secp384r1" not in OPENSSL_SUPPORTED_CURVES ,
836874 reason = "system openssl does not support secp384r1" )
837875 def test_to_openssl_nist384p (self ):
@@ -882,12 +920,12 @@ def test_to_openssl_brainpoolp384r1(self):
882920 def test_to_openssl_brainpoolp512r1 (self ):
883921 self .do_test_to_openssl (BRAINPOOLP512r1 )
884922
885- def do_test_to_openssl (self , curve ):
923+ def do_test_to_openssl (self , curve , hash_name = "SHA1" ):
886924 curvename = curve .openssl_name
887925 assert curvename
888926 # Python: create sk, vk, sign.
889927 # OpenSSL: read vk(4), checksig(6), read sk(2), sign, check
890- mdarg = self .get_openssl_messagedigest_arg ()
928+ mdarg = self .get_openssl_messagedigest_arg (hash_name )
891929 if os .path .isdir ("t" ): # pragma: no cover
892930 shutil .rmtree ("t" )
893931 os .mkdir ("t" )
@@ -898,7 +936,8 @@ def do_test_to_openssl(self, curve):
898936 e .write (vk .to_der ()) # 4
899937 with open ("t/pubkey.pem" , "wb" ) as e :
900938 e .write (vk .to_pem ()) # 4
901- sig_der = sk .sign (data , hashfunc = sha1 , sigencode = sigencode_der )
939+ sig_der = sk .sign (data , hashfunc = partial (hashlib .new , hash_name ),
940+ sigencode = sigencode_der )
902941
903942 with open ("t/data.sig" , "wb" ) as e :
904943 e .write (sig_der ) # 6
0 commit comments