@@ -114,3 +114,68 @@ def test_unwrap_invalid_wrapped_key_length(self, backend):
114114 # Keys to unwrap must be a multiple of 8 bytes
115115 with pytest .raises (ValueError ):
116116 keywrap .aes_key_unwrap (b"sixteen_byte_key" , b"\x00 " * 27 , backend )
117+
118+
119+ @pytest .mark .supported (
120+ only_if = lambda backend : backend .cipher_supported (
121+ algorithms .AES (b"\x00 " * 16 ), modes .ECB ()
122+ ),
123+ skip_message = "Does not support AES key wrap (RFC 5649) because AES-ECB"
124+ " is unsupported" ,
125+ )
126+ @pytest .mark .requires_backend_interface (interface = CipherBackend )
127+ class TestAESKeyWrapWithPadding (object ):
128+ @pytest .mark .parametrize (
129+ "params" ,
130+ _load_all_params (
131+ os .path .join ("keywrap" , "kwtestvectors" ),
132+ ["KWP_AE_128.txt" , "KWP_AE_192.txt" , "KWP_AE_256.txt" ],
133+ load_nist_vectors
134+ )
135+ )
136+ def test_wrap (self , backend , params ):
137+ wrapping_key = binascii .unhexlify (params ["k" ])
138+ key_to_wrap = binascii .unhexlify (params ["p" ])
139+ wrapped_key = keywrap .aes_key_wrap_with_padding (
140+ wrapping_key , key_to_wrap , backend
141+ )
142+ assert params ["c" ] == binascii .hexlify (wrapped_key )
143+
144+ @pytest .mark .parametrize (
145+ "params" ,
146+ _load_all_params (
147+ os .path .join ("keywrap" , "kwtestvectors" ),
148+ ["KWP_AD_128.txt" , "KWP_AD_192.txt" , "KWP_AD_256.txt" ],
149+ load_nist_vectors
150+ )
151+ )
152+ def test_unwrap (self , backend , params ):
153+ wrapping_key = binascii .unhexlify (params ["k" ])
154+ wrapped_key = binascii .unhexlify (params ["c" ])
155+ if params .get ("fail" ) is True :
156+ with pytest .raises (keywrap .InvalidUnwrap ):
157+ keywrap .aes_key_unwrap_with_padding (
158+ wrapping_key , wrapped_key , backend
159+ )
160+ else :
161+ unwrapped_key = keywrap .aes_key_unwrap_with_padding (
162+ wrapping_key , wrapped_key , backend
163+ )
164+ assert params ["p" ] == binascii .hexlify (unwrapped_key )
165+
166+ def test_unwrap_invalid_wrapped_key_length (self , backend ):
167+ # Keys to unwrap must be at least 16 bytes
168+ with pytest .raises (ValueError , match = 'Must be at least 16 bytes' ):
169+ keywrap .aes_key_unwrap_with_padding (
170+ b"sixteen_byte_key" , b"\x00 " * 15 , backend
171+ )
172+
173+ def test_wrap_invalid_key_length (self , backend ):
174+ with pytest .raises (ValueError , match = 'must be a valid AES key length' ):
175+ keywrap .aes_key_wrap_with_padding (b"badkey" , b"\x00 " , backend )
176+
177+ def test_unwrap_invalid_key_length (self , backend ):
178+ with pytest .raises (ValueError , match = 'must be a valid AES key length' ):
179+ keywrap .aes_key_unwrap_with_padding (
180+ b"badkey" , b"\x00 " * 16 , backend
181+ )
0 commit comments