@@ -853,6 +853,28 @@ def merge_templates(default_template, *user_templates):
853853 }
854854
855855
856+ cdef _check_key_type(key_type):
857+ if not isinstance (key_type, KeyType):
858+ raise ArgumentsBad(" `key_type` must be KeyType." )
859+
860+
861+ cdef _check_bit_length(value, name, required = False ):
862+ if value is None and not required:
863+ return
864+ if not isinstance (value, int ):
865+ raise ArgumentsBad(" `%s ` is the length in bits." % name)
866+
867+
868+ cdef _resolve_capabilities(key_type, capabilities):
869+ if capabilities is not None :
870+ return capabilities
871+ try :
872+ return DEFAULT_KEY_CAPABILITIES[key_type]
873+ except KeyError :
874+ raise ArgumentsBad(" No default capabilities for this key "
875+ " type. Please specify `capabilities`." )
876+
877+
856878cdef class Session(HasFuncList, types.Session):
857879 """ Extend Session with implementation."""
858880
@@ -971,11 +993,8 @@ cdef class Session(HasFuncList, types.Session):
971993 def generate_domain_parameters (self , key_type , param_length , store = False ,
972994 mechanism = None , mechanism_param = None ,
973995 template = None ):
974- if not isinstance (key_type, KeyType):
975- raise ArgumentsBad(" `key_type` must be KeyType." )
976-
977- if not isinstance (param_length, int ):
978- raise ArgumentsBad(" `param_length` is the length in bits." )
996+ _check_key_type(key_type)
997+ _check_bit_length(param_length, " param_length" , required = True )
979998
980999 mech = MechanismWithParam(
9811000 key_type, DEFAULT_PARAM_GENERATE_MECHANISMS,
@@ -999,18 +1018,9 @@ cdef class Session(HasFuncList, types.Session):
9991018 mechanism = None , mechanism_param = None ,
10001019 template = None ):
10011020
1002- if not isinstance (key_type, KeyType):
1003- raise ArgumentsBad(" `key_type` must be KeyType." )
1004-
1005- if key_length is not None and not isinstance (key_length, int ):
1006- raise ArgumentsBad(" `key_length` is the length in bits." )
1007-
1008- if capabilities is None :
1009- try :
1010- capabilities = DEFAULT_KEY_CAPABILITIES[key_type]
1011- except KeyError :
1012- raise ArgumentsBad(" No default capabilities for this key "
1013- " type. Please specify `capabilities`." )
1021+ _check_key_type(key_type)
1022+ _check_bit_length(key_length, " key_length" )
1023+ capabilities = _resolve_capabilities(key_type, capabilities)
10141024
10151025 mech = MechanismWithParam(
10161026 key_type, DEFAULT_GENERATE_MECHANISMS,
@@ -1052,18 +1062,9 @@ cdef class Session(HasFuncList, types.Session):
10521062 mechanism = None , mechanism_param = None ,
10531063 public_template = None , private_template = None ):
10541064
1055- if not isinstance (key_type, KeyType):
1056- raise ArgumentsBad(" `key_type` must be KeyType." )
1057-
1058- if key_length is not None and not isinstance (key_length, int ):
1059- raise ArgumentsBad(" `key_length` is the length in bits." )
1060-
1061- if capabilities is None :
1062- try :
1063- capabilities = DEFAULT_KEY_CAPABILITIES[key_type]
1064- except KeyError :
1065- raise ArgumentsBad(" No default capabilities for this key "
1066- " type. Please specify `capabilities`." )
1065+ _check_key_type(key_type)
1066+ _check_bit_length(key_length, " key_length" )
1067+ capabilities = _resolve_capabilities(key_type, capabilities)
10671068
10681069 mech = MechanismWithParam(
10691070 key_type, DEFAULT_GENERATE_MECHANISMS,
@@ -1457,12 +1458,7 @@ class GenerateWithParametersMixin(types.DomainParameters):
14571458 public_template = None , private_template = None ):
14581459
14591460 cdef Session session = self .session
1460- if capabilities is None :
1461- try :
1462- capabilities = DEFAULT_KEY_CAPABILITIES[self .key_type]
1463- except KeyError :
1464- raise ArgumentsBad(" No default capabilities for this key "
1465- " type. Please specify `capabilities`." )
1461+ capabilities = _resolve_capabilities(self .key_type, capabilities)
14661462
14671463 mech = MechanismWithParam(self .key_type, DEFAULT_GENERATE_MECHANISMS, mechanism, mechanism_param)
14681464
@@ -1881,15 +1877,8 @@ class UnwrapMixin(types.UnwrapMixin):
18811877 if not isinstance (object_class, ObjectClass):
18821878 raise ArgumentsBad(" `object_class` must be ObjectClass." )
18831879
1884- if not isinstance (key_type, KeyType):
1885- raise ArgumentsBad(" `key_type` must be KeyType." )
1886-
1887- if capabilities is None :
1888- try :
1889- capabilities = DEFAULT_KEY_CAPABILITIES[key_type]
1890- except KeyError :
1891- raise ArgumentsBad(" No default capabilities for this key "
1892- " type. Please specify `capabilities`." )
1880+ _check_key_type(key_type)
1881+ capabilities = _resolve_capabilities(key_type, capabilities)
18931882
18941883 mech = MechanismWithParam(self .key_type, DEFAULT_WRAP_MECHANISMS, mechanism, mechanism_param)
18951884
@@ -1932,18 +1921,9 @@ class DeriveMixin(types.DeriveMixin):
19321921 mechanism = None , mechanism_param = None ,
19331922 template = None ):
19341923
1935- if not isinstance (key_type, KeyType):
1936- raise ArgumentsBad(" `key_type` must be KeyType." )
1937-
1938- if not isinstance (key_length, int ):
1939- raise ArgumentsBad(" `key_length` is the length in bits." )
1940-
1941- if capabilities is None :
1942- try :
1943- capabilities = DEFAULT_KEY_CAPABILITIES[key_type]
1944- except KeyError :
1945- raise ArgumentsBad(" No default capabilities for this key "
1946- " type. Please specify `capabilities`." )
1924+ _check_key_type(key_type)
1925+ _check_bit_length(key_length, " key_length" , required = True )
1926+ capabilities = _resolve_capabilities(key_type, capabilities)
19471927
19481928 mech = MechanismWithParam(self .key_type, DEFAULT_DERIVE_MECHANISMS, mechanism, mechanism_param)
19491929
@@ -1979,15 +1959,8 @@ class EncapsulateMixin(types.EncapsulateMixin):
19791959 mechanism = None , mechanism_param = None ,
19801960 template = None ):
19811961
1982- if not isinstance (key_type, KeyType):
1983- raise ArgumentsBad(" `key_type` must be KeyType." )
1984-
1985- if capabilities is None :
1986- try :
1987- capabilities = DEFAULT_KEY_CAPABILITIES[key_type]
1988- except KeyError :
1989- raise ArgumentsBad(" No default capabilities for this key "
1990- " type. Please specify `capabilities`." )
1962+ _check_key_type(key_type)
1963+ capabilities = _resolve_capabilities(key_type, capabilities)
19911964
19921965 mech = MechanismWithParam(self .key_type, DEFAULT_ENCAPSULATE_MECHANISMS, mechanism, mechanism_param)
19931966
@@ -2038,15 +2011,8 @@ class DecapsulateMixin(types.DecapsulateMixin):
20382011 mechanism = None , mechanism_param = None ,
20392012 template = None ):
20402013
2041- if not isinstance (key_type, KeyType):
2042- raise ArgumentsBad(" `key_type` must be KeyType." )
2043-
2044- if capabilities is None :
2045- try :
2046- capabilities = DEFAULT_KEY_CAPABILITIES[key_type]
2047- except KeyError :
2048- raise ArgumentsBad(" No default capabilities for this key "
2049- " type. Please specify `capabilities`." )
2014+ _check_key_type(key_type)
2015+ capabilities = _resolve_capabilities(key_type, capabilities)
20502016
20512017 mech = MechanismWithParam(self .key_type, DEFAULT_ENCAPSULATE_MECHANISMS, mechanism, mechanism_param)
20522018
0 commit comments