|
7 | 7 |
|
8 | 8 | from . import TestCase, requires, FIXME
|
9 | 9 |
|
| 10 | +from parameterized import parameterized |
10 | 11 |
|
11 | 12 | class AESTests(TestCase):
|
12 | 13 |
|
@@ -123,3 +124,211 @@ def test_wrap(self):
|
123 | 124 |
|
124 | 125 | self.assertEqual(key[pkcs11.Attribute.VALUE],
|
125 | 126 | key2[pkcs11.Attribute.VALUE])
|
| 127 | + |
| 128 | + @parameterized.expand([ |
| 129 | + ("POSITIVE_128_BIT", 128, 16, TestCase.assertIsNotNone), |
| 130 | + ("POSITIVE_128_BIT_LONG_IV", 128, 32, TestCase.assertIsNotNone), |
| 131 | + ("NEGATIVE_128_BIT_BAD_IV", 128, 15, TestCase.assertIsNone), |
| 132 | + ("POSITIVE_256_BIT_LONG_IV", 256, 32, TestCase.assertIsNotNone), |
| 133 | + ("NEGATIVE_256_BIT_SHORT_IV", 256, 16, TestCase.assertIsNone), |
| 134 | + ("NEGATIVE_256_BIT_BAD_IV", 256, 31, TestCase.assertIsNone), |
| 135 | + ]) |
| 136 | + @requires(Mechanism.AES_ECB_ENCRYPT_DATA) |
| 137 | + @FIXME.opencryptoki # can't set key attributes |
| 138 | + def test_derive_using_ecb_encrypt(self, test_type, test_key_length, iv_length, assert_fn): |
| 139 | + """Function to test AES Key Derivation using the ECB_ENCRYPT Mechanism. |
| 140 | +
|
| 141 | + Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521 |
| 142 | + """ |
| 143 | + |
| 144 | + # Create the Master Key |
| 145 | + capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] |
| 146 | + capabilities |= pkcs11.MechanismFlag.DERIVE |
| 147 | + key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 148 | + capabilities=capabilities, |
| 149 | + template={ |
| 150 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 151 | + pkcs11.Attribute.DERIVE: True, |
| 152 | + pkcs11.Attribute.SENSITIVE: False, |
| 153 | + }) |
| 154 | + |
| 155 | + self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) |
| 156 | + |
| 157 | + # Derive a Key from the Master Key |
| 158 | + iv = b'0' * iv_length |
| 159 | + try: |
| 160 | + derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 161 | + capabilities=capabilities, |
| 162 | + mechanism=Mechanism.AES_ECB_ENCRYPT_DATA, |
| 163 | + mechanism_param=iv, |
| 164 | + template={ |
| 165 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 166 | + pkcs11.Attribute.SENSITIVE: False, |
| 167 | + }) |
| 168 | + except (pkcs11.exceptions.MechanismParamInvalid, |
| 169 | + pkcs11.exceptions.FunctionFailed) as e: |
| 170 | + derived_key = None |
| 171 | + |
| 172 | + assert_fn(self, derived_key, "{}-bit Key Derivation Failure".format(test_key_length)) |
| 173 | + |
| 174 | + @parameterized.expand([ |
| 175 | + ("POSITIVE_128_BIT", 128, 16), |
| 176 | + ("POSITIVE_256_BIT_LONG_IV", 256, 32), |
| 177 | + ]) |
| 178 | + @requires(Mechanism.AES_ECB_ENCRYPT_DATA) |
| 179 | + @FIXME.opencryptoki # can't set key attributes |
| 180 | + def test_encrypt_with_key_derived_using_ecb_encrypt(self, test_type, test_key_length, iv_length): |
| 181 | + """Function to test Data Encryption/Decryption using a Derived AES Key. |
| 182 | +
|
| 183 | + Function to test Data Encryption/Decryption using an AES Key |
| 184 | + Derived by the ECB_ENCRYPT Mechanism. |
| 185 | +
|
| 186 | + Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521 |
| 187 | + """ |
| 188 | + |
| 189 | + # Create the Master Key |
| 190 | + capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] |
| 191 | + capabilities |= pkcs11.MechanismFlag.DERIVE |
| 192 | + key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 193 | + capabilities=capabilities, |
| 194 | + template={ |
| 195 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 196 | + pkcs11.Attribute.DERIVE: True, |
| 197 | + pkcs11.Attribute.SENSITIVE: False, |
| 198 | + }) |
| 199 | + |
| 200 | + self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) |
| 201 | + |
| 202 | + # Derive a Key from the Master Key |
| 203 | + iv = b'0' * iv_length |
| 204 | + try: |
| 205 | + derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 206 | + capabilities=capabilities, |
| 207 | + mechanism=Mechanism.AES_ECB_ENCRYPT_DATA, |
| 208 | + mechanism_param=iv, |
| 209 | + template={ |
| 210 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 211 | + pkcs11.Attribute.SENSITIVE: False, |
| 212 | + }) |
| 213 | + except (pkcs11.exceptions.MechanismParamInvalid, |
| 214 | + pkcs11.exceptions.FunctionFailed) as e: |
| 215 | + derived_key = None |
| 216 | + |
| 217 | + self.assertTrue(derived_key is not None, "Failed to derive {}-bit Derived Key".format(test_key_length)) |
| 218 | + |
| 219 | + # Test capability of Key to Encrypt/Decrypt data |
| 220 | + data = b'HELLO WORLD' * 1024 |
| 221 | + |
| 222 | + iv = self.session.generate_random(128) |
| 223 | + crypttext = self.key.encrypt(data, mechanism_param=iv) |
| 224 | + text = self.key.decrypt(crypttext, mechanism_param=iv) |
| 225 | + |
| 226 | + self.assertEqual(text, data) |
| 227 | + |
| 228 | + @parameterized.expand([ |
| 229 | + ("POSITIVE_128_BIT", 128, 16, 16, TestCase.assertIsNotNone), |
| 230 | + ("POSITIVE_128_BIT_LONG_DATA", 128, 16, 64, TestCase.assertIsNotNone), |
| 231 | + ("NEGATIVE_128_BIT_BAD_IV", 128, 15, 16, TestCase.assertIsNone), |
| 232 | + ("NEGATIVE_128_BIT_BAD_DATA", 128, 16, 31, TestCase.assertIsNone), |
| 233 | + ("POSITIVE_256_BIT", 256, 16, 32, TestCase.assertIsNotNone), |
| 234 | + ("POSITIVE_256_BIT_LONG_DATA", 256, 16, 64, TestCase.assertIsNotNone), |
| 235 | + ("NEGATIVE_256_BIT_BAD_IV", 256, 15, 16, TestCase.assertIsNone), |
| 236 | + ("NEGATIVE_256_BIT_BAD_DATA", 256, 16, 31, TestCase.assertIsNone), |
| 237 | + ("NEGATIVE_256_BIT_SHORT_DATA", 256, 16, 16, TestCase.assertIsNone), |
| 238 | + ]) |
| 239 | + @requires(Mechanism.AES_CBC_ENCRYPT_DATA) |
| 240 | + @FIXME.opencryptoki # can't set key attributes |
| 241 | + def test_derive_using_cbc_encrypt(self, test_type, test_key_length, iv_length, data_length, assert_fn): |
| 242 | + """Function to test AES Key Derivation using the CBC_ENCRYPT Mechanism. |
| 243 | +
|
| 244 | + Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521 |
| 245 | + """ |
| 246 | + |
| 247 | + # Create the Master Key |
| 248 | + capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] |
| 249 | + capabilities |= pkcs11.MechanismFlag.DERIVE |
| 250 | + key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 251 | + capabilities=capabilities, |
| 252 | + template={ |
| 253 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 254 | + pkcs11.Attribute.DERIVE: True, |
| 255 | + pkcs11.Attribute.SENSITIVE: False, |
| 256 | + }) |
| 257 | + |
| 258 | + self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) |
| 259 | + |
| 260 | + # Derive a Key from the Master Key |
| 261 | + iv = b'0' * iv_length |
| 262 | + data = b'1' * data_length |
| 263 | + try: |
| 264 | + derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 265 | + capabilities=capabilities, |
| 266 | + mechanism=Mechanism.AES_CBC_ENCRYPT_DATA, |
| 267 | + mechanism_param=(iv, data), |
| 268 | + template={ |
| 269 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 270 | + pkcs11.Attribute.SENSITIVE: False, |
| 271 | + }) |
| 272 | + except (pkcs11.exceptions.MechanismParamInvalid, |
| 273 | + pkcs11.exceptions.FunctionFailed, |
| 274 | + IndexError) as e: |
| 275 | + derived_key = None |
| 276 | + |
| 277 | + assert_fn(self, derived_key, "{}-bit Key Derivation Failure".format(test_key_length)) |
| 278 | + |
| 279 | + @parameterized.expand([ |
| 280 | + ("POSITIVE_128_BIT", 128, 16, 16), |
| 281 | + ("POSITIVE_256_BIT", 256, 16, 32), |
| 282 | + ("POSITIVE_256_BIT_LONG_DATA", 256, 16, 64), |
| 283 | + ]) |
| 284 | + @requires(Mechanism.AES_CBC_ENCRYPT_DATA) |
| 285 | + @FIXME.opencryptoki # can't set key attributes |
| 286 | + def test_encrypt_with_key_derived_using_cbc_encrypt(self, test_type, test_key_length, iv_length, data_length): |
| 287 | + """Function to test Data Encryption/Decryption using a Derived AES Key. |
| 288 | +
|
| 289 | + Function to test Data Encryption/Decryption using an AES Key |
| 290 | + Derived by the CBC_ENCRYPT Mechanism. |
| 291 | +
|
| 292 | + Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521 |
| 293 | + """ |
| 294 | + |
| 295 | + # Create the Master Key |
| 296 | + capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] |
| 297 | + capabilities |= pkcs11.MechanismFlag.DERIVE |
| 298 | + key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 299 | + capabilities=capabilities, |
| 300 | + template={ |
| 301 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 302 | + pkcs11.Attribute.DERIVE: True, |
| 303 | + pkcs11.Attribute.SENSITIVE: False, |
| 304 | + }) |
| 305 | + |
| 306 | + self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) |
| 307 | + |
| 308 | + # Derive a Key from the Master Key |
| 309 | + iv = b'0' * iv_length |
| 310 | + data = b'1' * data_length |
| 311 | + try: |
| 312 | + derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, |
| 313 | + capabilities=capabilities, |
| 314 | + mechanism=Mechanism.AES_CBC_ENCRYPT_DATA, |
| 315 | + mechanism_param=(iv, data), |
| 316 | + template={ |
| 317 | + pkcs11.Attribute.EXTRACTABLE: True, |
| 318 | + pkcs11.Attribute.SENSITIVE: False, |
| 319 | + }) |
| 320 | + except (pkcs11.exceptions.MechanismParamInvalid, |
| 321 | + pkcs11.exceptions.FunctionFailed, |
| 322 | + IndexError) as e: |
| 323 | + derived_key = None |
| 324 | + |
| 325 | + self.assertTrue(derived_key is not None, "Failed to derive {}-bit Derived Key".format(test_key_length)) |
| 326 | + |
| 327 | + # Test capability of Key to Encrypt/Decrypt data |
| 328 | + data = b'HELLO WORLD' * 1024 |
| 329 | + |
| 330 | + iv = self.session.generate_random(128) |
| 331 | + crypttext = self.key.encrypt(data, mechanism_param=iv) |
| 332 | + text = self.key.decrypt(crypttext, mechanism_param=iv) |
| 333 | + |
| 334 | + self.assertEqual(text, data) |
0 commit comments