Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
Alex Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 7 | import binascii |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 8 | import itertools |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 9 | import os |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 10 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 11 | import pytest |
| 12 | |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 13 | from cryptography.exceptions import ( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 14 | AlreadyFinalized, |
| 15 | AlreadyUpdated, |
| 16 | InvalidSignature, |
| 17 | InvalidTag, |
| 18 | NotYetFinalized, |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 19 | ) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 20 | from cryptography.hazmat.primitives import hashes, hmac, serialization |
Paul Kehrer | c85f179 | 2014-03-19 09:45:42 -0400 | [diff] [blame] | 21 | from cryptography.hazmat.primitives.asymmetric import rsa |
Paul Kehrer | 21dde56 | 2013-11-06 12:22:09 +0800 | [diff] [blame] | 22 | from cryptography.hazmat.primitives.ciphers import Cipher |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 23 | from cryptography.hazmat.primitives.ciphers.modes import GCM |
Ayrx | ac1a079 | 2014-05-07 17:02:21 +0800 | [diff] [blame] | 24 | from cryptography.hazmat.primitives.kdf.hkdf import HKDF, HKDFExpand |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 25 | from cryptography.hazmat.primitives.kdf.kbkdf import ( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 26 | CounterLocation, |
| 27 | KBKDFHMAC, |
| 28 | Mode, |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 29 | ) |
Paul Kehrer | afc1ccd | 2014-03-19 11:49:32 -0400 | [diff] [blame] | 30 | from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 31 | |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 32 | from ...utils import load_vectors_from_file |
| 33 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 34 | |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 35 | def _load_all_params(path, file_names, param_loader): |
| 36 | all_params = [] |
| 37 | for file_name in file_names: |
| 38 | all_params.extend( |
| 39 | load_vectors_from_file(os.path.join(path, file_name), param_loader) |
| 40 | ) |
| 41 | return all_params |
| 42 | |
Alex Gaynor | 4eec0bb | 2013-12-13 09:12:19 -0800 | [diff] [blame] | 43 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 44 | def generate_encrypt_test( |
| 45 | param_loader, path, file_names, cipher_factory, mode_factory |
| 46 | ): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 47 | all_params = _load_all_params(path, file_names, param_loader) |
| 48 | |
| 49 | @pytest.mark.parametrize("params", all_params) |
| 50 | def test_encryption(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 51 | encrypt_test(backend, cipher_factory, mode_factory, params) |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 52 | |
Alex Gaynor | bd458ae | 2013-10-16 11:59:30 -0700 | [diff] [blame] | 53 | return test_encryption |
| 54 | |
| 55 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 56 | def encrypt_test(backend, cipher_factory, mode_factory, params): |
Paul Kehrer | 5103235 | 2017-05-20 10:09:02 -0700 | [diff] [blame] | 57 | assert backend.cipher_supported( |
Paul Kehrer | dbb64bd | 2016-07-11 02:13:40 +0000 | [diff] [blame] | 58 | cipher_factory(**params), mode_factory(**params) |
Paul Kehrer | 5103235 | 2017-05-20 10:09:02 -0700 | [diff] [blame] | 59 | ) |
Paul Kehrer | dbb64bd | 2016-07-11 02:13:40 +0000 | [diff] [blame] | 60 | |
Paul Kehrer | a620b7d | 2013-12-20 22:59:02 -0600 | [diff] [blame] | 61 | plaintext = params["plaintext"] |
| 62 | ciphertext = params["ciphertext"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 63 | cipher = Cipher( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 64 | cipher_factory(**params), mode_factory(**params), backend=backend |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 65 | ) |
| 66 | encryptor = cipher.encryptor() |
| 67 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 68 | actual_ciphertext += encryptor.finalize() |
| 69 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
| 70 | decryptor = cipher.decryptor() |
| 71 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 72 | actual_plaintext += decryptor.finalize() |
| 73 | assert actual_plaintext == binascii.unhexlify(plaintext) |
| 74 | |
| 75 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 76 | def generate_aead_test( |
| 77 | param_loader, path, file_names, cipher_factory, mode_factory |
| 78 | ): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 79 | all_params = _load_all_params(path, file_names, param_loader) |
| 80 | |
| 81 | @pytest.mark.parametrize("params", all_params) |
| 82 | def test_aead(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 83 | aead_test(backend, cipher_factory, mode_factory, params) |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 84 | |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 85 | return test_aead |
| 86 | |
| 87 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 88 | def aead_test(backend, cipher_factory, mode_factory, params): |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 89 | if mode_factory is GCM and len(params["iv"]) < 16: |
| 90 | # 16 because this is hex encoded data |
| 91 | pytest.skip("Less than 64-bit IVs are no longer supported") |
| 92 | |
| 93 | if ( |
| 94 | mode_factory is GCM |
| 95 | and backend._fips_enabled |
| 96 | and len(params["iv"]) != 24 |
| 97 | ): |
| 98 | # Red Hat disables non-96-bit IV support as part of its FIPS |
| 99 | # patches. The check is for a byte length of 24 because the value is |
| 100 | # hex encoded. |
| 101 | pytest.skip("Non-96-bit IVs unsupported in FIPS mode.") |
| 102 | |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 103 | if params.get("pt") is not None: |
Paul Kehrer | a620b7d | 2013-12-20 22:59:02 -0600 | [diff] [blame] | 104 | plaintext = params["pt"] |
| 105 | ciphertext = params["ct"] |
| 106 | aad = params["aad"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 107 | if params.get("fail") is True: |
| 108 | cipher = Cipher( |
| 109 | cipher_factory(binascii.unhexlify(params["key"])), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 110 | mode_factory( |
| 111 | binascii.unhexlify(params["iv"]), |
| 112 | binascii.unhexlify(params["tag"]), |
| 113 | len(binascii.unhexlify(params["tag"])), |
| 114 | ), |
| 115 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 116 | ) |
| 117 | decryptor = cipher.decryptor() |
| 118 | decryptor.authenticate_additional_data(binascii.unhexlify(aad)) |
| 119 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 120 | with pytest.raises(InvalidTag): |
| 121 | decryptor.finalize() |
| 122 | else: |
| 123 | cipher = Cipher( |
| 124 | cipher_factory(binascii.unhexlify(params["key"])), |
| 125 | mode_factory(binascii.unhexlify(params["iv"]), None), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 126 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 127 | ) |
| 128 | encryptor = cipher.encryptor() |
| 129 | encryptor.authenticate_additional_data(binascii.unhexlify(aad)) |
| 130 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 131 | actual_ciphertext += encryptor.finalize() |
Alex Gaynor | 8f1b8e8 | 2014-06-29 20:43:29 -0700 | [diff] [blame] | 132 | tag_len = len(binascii.unhexlify(params["tag"])) |
| 133 | assert binascii.hexlify(encryptor.tag[:tag_len]) == params["tag"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 134 | cipher = Cipher( |
| 135 | cipher_factory(binascii.unhexlify(params["key"])), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 136 | mode_factory( |
| 137 | binascii.unhexlify(params["iv"]), |
| 138 | binascii.unhexlify(params["tag"]), |
| 139 | min_tag_length=tag_len, |
| 140 | ), |
| 141 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 142 | ) |
| 143 | decryptor = cipher.decryptor() |
| 144 | decryptor.authenticate_additional_data(binascii.unhexlify(aad)) |
| 145 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 146 | actual_plaintext += decryptor.finalize() |
| 147 | assert actual_plaintext == binascii.unhexlify(plaintext) |
| 148 | |
| 149 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 150 | def generate_stream_encryption_test( |
| 151 | param_loader, path, file_names, cipher_factory |
| 152 | ): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 153 | all_params = _load_all_params(path, file_names, param_loader) |
| 154 | |
| 155 | @pytest.mark.parametrize("params", all_params) |
| 156 | def test_stream_encryption(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 157 | stream_encryption_test(backend, cipher_factory, params) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 158 | |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 159 | return test_stream_encryption |
| 160 | |
| 161 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 162 | def stream_encryption_test(backend, cipher_factory, params): |
Paul Kehrer | a620b7d | 2013-12-20 22:59:02 -0600 | [diff] [blame] | 163 | plaintext = params["plaintext"] |
| 164 | ciphertext = params["ciphertext"] |
| 165 | offset = params["offset"] |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 166 | cipher = Cipher(cipher_factory(**params), None, backend=backend) |
| 167 | encryptor = cipher.encryptor() |
| 168 | # throw away offset bytes |
| 169 | encryptor.update(b"\x00" * int(offset)) |
| 170 | actual_ciphertext = encryptor.update(binascii.unhexlify(plaintext)) |
| 171 | actual_ciphertext += encryptor.finalize() |
| 172 | assert actual_ciphertext == binascii.unhexlify(ciphertext) |
| 173 | decryptor = cipher.decryptor() |
| 174 | decryptor.update(b"\x00" * int(offset)) |
| 175 | actual_plaintext = decryptor.update(binascii.unhexlify(ciphertext)) |
| 176 | actual_plaintext += decryptor.finalize() |
| 177 | assert actual_plaintext == binascii.unhexlify(plaintext) |
| 178 | |
| 179 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 180 | def generate_hash_test(param_loader, path, file_names, hash_cls): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 181 | all_params = _load_all_params(path, file_names, param_loader) |
Paul Kehrer | 4da28c3 | 2013-11-07 07:50:17 +0800 | [diff] [blame] | 182 | |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 183 | @pytest.mark.parametrize("params", all_params) |
| 184 | def test_hash(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 185 | hash_test(backend, hash_cls, params) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 186 | |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 187 | return test_hash |
| 188 | |
| 189 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 190 | def hash_test(backend, algorithm, params): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 191 | msg, md = params |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 192 | m = hashes.Hash(algorithm, backend=backend) |
| 193 | m.update(binascii.unhexlify(msg)) |
| 194 | expected_md = md.replace(" ", "").lower().encode("ascii") |
| 195 | assert m.finalize() == binascii.unhexlify(expected_md) |
| 196 | |
| 197 | |
Paul Kehrer | 162a17e | 2018-07-23 19:55:06 +0800 | [diff] [blame] | 198 | def generate_base_hash_test(algorithm, digest_size): |
Paul Kehrer | b078d8e | 2013-12-27 16:33:14 -0600 | [diff] [blame] | 199 | def test_base_hash(self, backend): |
Paul Kehrer | 162a17e | 2018-07-23 19:55:06 +0800 | [diff] [blame] | 200 | base_hash_test(backend, algorithm, digest_size) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 201 | |
Paul Kehrer | b078d8e | 2013-12-27 16:33:14 -0600 | [diff] [blame] | 202 | return test_base_hash |
| 203 | |
| 204 | |
Paul Kehrer | 162a17e | 2018-07-23 19:55:06 +0800 | [diff] [blame] | 205 | def base_hash_test(backend, algorithm, digest_size): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 206 | m = hashes.Hash(algorithm, backend=backend) |
| 207 | assert m.algorithm.digest_size == digest_size |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 208 | m_copy = m.copy() |
| 209 | assert m != m_copy |
| 210 | assert m._ctx != m_copy._ctx |
| 211 | |
| 212 | m.update(b"abc") |
| 213 | copy = m.copy() |
| 214 | copy.update(b"123") |
| 215 | m.update(b"123") |
| 216 | assert copy.finalize() == m.finalize() |
| 217 | |
| 218 | |
Paul Kehrer | b078d8e | 2013-12-27 16:33:14 -0600 | [diff] [blame] | 219 | def generate_base_hmac_test(hash_cls): |
| 220 | def test_base_hmac(self, backend): |
| 221 | base_hmac_test(backend, hash_cls) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 222 | |
Paul Kehrer | b078d8e | 2013-12-27 16:33:14 -0600 | [diff] [blame] | 223 | return test_base_hmac |
| 224 | |
| 225 | |
| 226 | def base_hmac_test(backend, algorithm): |
| 227 | key = b"ab" |
| 228 | h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend) |
| 229 | h_copy = h.copy() |
| 230 | assert h != h_copy |
| 231 | assert h._ctx != h_copy._ctx |
| 232 | |
| 233 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 234 | def generate_hmac_test(param_loader, path, file_names, algorithm): |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 235 | all_params = _load_all_params(path, file_names, param_loader) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 236 | |
Alex Gaynor | e5c5eec | 2013-12-13 08:10:20 -0800 | [diff] [blame] | 237 | @pytest.mark.parametrize("params", all_params) |
| 238 | def test_hmac(self, backend, params): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 239 | hmac_test(backend, algorithm, params) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 240 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 241 | return test_hmac |
| 242 | |
| 243 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 244 | def hmac_test(backend, algorithm, params): |
Alex Gaynor | 36e651c | 2014-01-27 10:08:35 -0800 | [diff] [blame] | 245 | msg, md, key = params |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 246 | h = hmac.HMAC(binascii.unhexlify(key), algorithm, backend=backend) |
| 247 | h.update(binascii.unhexlify(msg)) |
| 248 | assert h.finalize() == binascii.unhexlify(md.encode("ascii")) |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 249 | |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 250 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 251 | def generate_pbkdf2_test(param_loader, path, file_names, algorithm): |
| 252 | all_params = _load_all_params(path, file_names, param_loader) |
| 253 | |
| 254 | @pytest.mark.parametrize("params", all_params) |
| 255 | def test_pbkdf2(self, backend, params): |
| 256 | pbkdf2_test(backend, algorithm, params) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 257 | |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 258 | return test_pbkdf2 |
| 259 | |
| 260 | |
| 261 | def pbkdf2_test(backend, algorithm, params): |
| 262 | # Password and salt can contain \0, which should be loaded as a null char. |
| 263 | # The NIST loader loads them as literal strings so we replace with the |
| 264 | # proper value. |
Paul Kehrer | 1277bc7 | 2014-01-28 17:09:59 -0600 | [diff] [blame] | 265 | kdf = PBKDF2HMAC( |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 266 | algorithm, |
| 267 | int(params["length"]), |
| 268 | params["salt"], |
| 269 | int(params["iterations"]), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 270 | backend, |
Paul Kehrer | 1050ddf | 2014-01-27 21:04:03 -0600 | [diff] [blame] | 271 | ) |
| 272 | derived_key = kdf.derive(params["password"]) |
| 273 | assert binascii.hexlify(derived_key) == params["derived_key"] |
| 274 | |
| 275 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 276 | def generate_aead_exception_test(cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 277 | def test_aead_exception(self, backend): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 278 | aead_exception_test(backend, cipher_factory, mode_factory) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 279 | |
Paul Kehrer | ce9c611 | 2013-11-22 14:10:59 -0600 | [diff] [blame] | 280 | return test_aead_exception |
Paul Kehrer | 22e80cb | 2013-11-20 21:27:00 -0600 | [diff] [blame] | 281 | |
| 282 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 283 | def aead_exception_test(backend, cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 284 | cipher = Cipher( |
| 285 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
| 286 | mode_factory(binascii.unhexlify(b"0" * 24)), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 287 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 288 | ) |
| 289 | encryptor = cipher.encryptor() |
| 290 | encryptor.update(b"a" * 16) |
| 291 | with pytest.raises(NotYetFinalized): |
| 292 | encryptor.tag |
| 293 | with pytest.raises(AlreadyUpdated): |
| 294 | encryptor.authenticate_additional_data(b"b" * 16) |
| 295 | encryptor.finalize() |
| 296 | with pytest.raises(AlreadyFinalized): |
| 297 | encryptor.authenticate_additional_data(b"b" * 16) |
| 298 | with pytest.raises(AlreadyFinalized): |
| 299 | encryptor.update(b"b" * 16) |
| 300 | with pytest.raises(AlreadyFinalized): |
| 301 | encryptor.finalize() |
| 302 | cipher = Cipher( |
| 303 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
| 304 | mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 305 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 306 | ) |
| 307 | decryptor = cipher.decryptor() |
| 308 | decryptor.update(b"a" * 16) |
| 309 | with pytest.raises(AttributeError): |
| 310 | decryptor.tag |
Paul Kehrer | b91221d | 2013-12-04 17:56:40 -0600 | [diff] [blame] | 311 | |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 312 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 313 | def generate_aead_tag_exception_test(cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 314 | def test_aead_tag_exception(self, backend): |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 315 | aead_tag_exception_test(backend, cipher_factory, mode_factory) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 316 | |
Paul Kehrer | b91221d | 2013-12-04 17:56:40 -0600 | [diff] [blame] | 317 | return test_aead_tag_exception |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 318 | |
| 319 | |
Paul Kehrer | 783479c | 2013-12-26 21:08:45 -0600 | [diff] [blame] | 320 | def aead_tag_exception_test(backend, cipher_factory, mode_factory): |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 321 | cipher = Cipher( |
| 322 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
| 323 | mode_factory(binascii.unhexlify(b"0" * 24)), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 324 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 325 | ) |
Alex Gaynor | 516b1ad | 2014-01-01 12:28:37 -0800 | [diff] [blame] | 326 | |
Paul Kehrer | f7b4ede | 2013-12-21 17:25:19 -0600 | [diff] [blame] | 327 | with pytest.raises(ValueError): |
Alex Gaynor | 516b1ad | 2014-01-01 12:28:37 -0800 | [diff] [blame] | 328 | mode_factory(binascii.unhexlify(b"0" * 24), b"000") |
| 329 | |
Alex Gaynor | a947759 | 2014-06-30 10:03:22 -0700 | [diff] [blame] | 330 | with pytest.raises(ValueError): |
| 331 | mode_factory(binascii.unhexlify(b"0" * 24), b"000000", 2) |
| 332 | |
Paul Kehrer | f7b4ede | 2013-12-21 17:25:19 -0600 | [diff] [blame] | 333 | cipher = Cipher( |
| 334 | cipher_factory(binascii.unhexlify(b"0" * 32)), |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 335 | mode_factory(binascii.unhexlify(b"0" * 24), b"0" * 16), |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 336 | backend, |
Alex Gaynor | 21919e2 | 2013-12-13 08:56:32 -0800 | [diff] [blame] | 337 | ) |
| 338 | with pytest.raises(ValueError): |
| 339 | cipher.encryptor() |
David Reid | 66c9cd9 | 2014-01-20 16:05:53 -0800 | [diff] [blame] | 340 | |
| 341 | |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 342 | def hkdf_derive_test(backend, algorithm, params): |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 343 | hkdf = HKDF( |
David Reid | 66c9cd9 | 2014-01-20 16:05:53 -0800 | [diff] [blame] | 344 | algorithm, |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 345 | int(params["l"]), |
| 346 | salt=binascii.unhexlify(params["salt"]) or None, |
| 347 | info=binascii.unhexlify(params["info"]) or None, |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 348 | backend=backend, |
David Reid | 66c9cd9 | 2014-01-20 16:05:53 -0800 | [diff] [blame] | 349 | ) |
| 350 | |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 351 | okm = hkdf.derive(binascii.unhexlify(params["ikm"])) |
| 352 | |
David Reid | 1436730 | 2014-01-27 16:33:31 -0800 | [diff] [blame] | 353 | assert okm == binascii.unhexlify(params["okm"]) |
David Reid | 66c9cd9 | 2014-01-20 16:05:53 -0800 | [diff] [blame] | 354 | |
| 355 | |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 356 | def hkdf_extract_test(backend, algorithm, params): |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 357 | hkdf = HKDF( |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 358 | algorithm, |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 359 | int(params["l"]), |
| 360 | salt=binascii.unhexlify(params["salt"]) or None, |
| 361 | info=binascii.unhexlify(params["info"]) or None, |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 362 | backend=backend, |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 363 | ) |
| 364 | |
David Reid | 15fd643 | 2014-01-30 15:28:09 -0800 | [diff] [blame] | 365 | prk = hkdf._extract(binascii.unhexlify(params["ikm"])) |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 366 | |
David Reid | 1436730 | 2014-01-27 16:33:31 -0800 | [diff] [blame] | 367 | assert prk == binascii.unhexlify(params["prk"]) |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 368 | |
| 369 | |
| 370 | def hkdf_expand_test(backend, algorithm, params): |
Ayrx | ac1a079 | 2014-05-07 17:02:21 +0800 | [diff] [blame] | 371 | hkdf = HKDFExpand( |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 372 | algorithm, |
David Reid | 1436730 | 2014-01-27 16:33:31 -0800 | [diff] [blame] | 373 | int(params["l"]), |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 374 | info=binascii.unhexlify(params["info"]) or None, |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 375 | backend=backend, |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 376 | ) |
| 377 | |
Ayrx | 2d6cd44 | 2014-05-09 14:48:06 +0800 | [diff] [blame] | 378 | okm = hkdf.derive(binascii.unhexlify(params["prk"])) |
David Reid | 0d492db | 2014-01-27 17:05:49 -0800 | [diff] [blame] | 379 | |
David Reid | 1436730 | 2014-01-27 16:33:31 -0800 | [diff] [blame] | 380 | assert okm == binascii.unhexlify(params["okm"]) |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 381 | |
| 382 | |
David Reid | 66c9cd9 | 2014-01-20 16:05:53 -0800 | [diff] [blame] | 383 | def generate_hkdf_test(param_loader, path, file_names, algorithm): |
| 384 | all_params = _load_all_params(path, file_names, param_loader) |
| 385 | |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 386 | all_tests = [hkdf_extract_test, hkdf_expand_test, hkdf_derive_test] |
| 387 | |
| 388 | @pytest.mark.parametrize( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 389 | ("params", "hkdf_test"), itertools.product(all_params, all_tests) |
David Reid | 5443e9d | 2014-01-22 17:18:49 -0800 | [diff] [blame] | 390 | ) |
| 391 | def test_hkdf(self, backend, params, hkdf_test): |
David Reid | 66c9cd9 | 2014-01-20 16:05:53 -0800 | [diff] [blame] | 392 | hkdf_test(backend, algorithm, params) |
| 393 | |
| 394 | return test_hkdf |
Paul Kehrer | b5936a7 | 2014-03-13 21:03:00 -0400 | [diff] [blame] | 395 | |
| 396 | |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 397 | def generate_kbkdf_counter_mode_test(param_loader, path, file_names): |
| 398 | all_params = _load_all_params(path, file_names, param_loader) |
| 399 | |
| 400 | @pytest.mark.parametrize("params", all_params) |
| 401 | def test_kbkdf(self, backend, params): |
| 402 | kbkdf_counter_mode_test(backend, params) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 403 | |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 404 | return test_kbkdf |
| 405 | |
| 406 | |
| 407 | def kbkdf_counter_mode_test(backend, params): |
| 408 | supported_algorithms = { |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 409 | "hmac_sha1": hashes.SHA1, |
| 410 | "hmac_sha224": hashes.SHA224, |
| 411 | "hmac_sha256": hashes.SHA256, |
| 412 | "hmac_sha384": hashes.SHA384, |
| 413 | "hmac_sha512": hashes.SHA512, |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Paul Kehrer | cb0fa2e | 2016-05-29 22:37:33 -0500 | [diff] [blame] | 416 | supported_counter_locations = { |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 417 | "before_fixed": CounterLocation.BeforeFixed, |
| 418 | "after_fixed": CounterLocation.AfterFixed, |
| 419 | } |
| 420 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 421 | algorithm = supported_algorithms.get(params.get("prf")) |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 422 | if algorithm is None or not backend.hmac_supported(algorithm()): |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 423 | pytest.skip( |
| 424 | "KBKDF does not support algorithm: {}".format(params.get("prf")) |
| 425 | ) |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 426 | |
Paul Kehrer | cb0fa2e | 2016-05-29 22:37:33 -0500 | [diff] [blame] | 427 | ctr_loc = supported_counter_locations.get(params.get("ctrlocation")) |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 428 | if ctr_loc is None or not isinstance(ctr_loc, CounterLocation): |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 429 | pytest.skip( |
| 430 | "Does not support counter location: {}".format( |
| 431 | params.get("ctrlocation") |
| 432 | ) |
| 433 | ) |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 434 | |
| 435 | ctrkdf = KBKDFHMAC( |
| 436 | algorithm(), |
| 437 | Mode.CounterMode, |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 438 | params["l"] // 8, |
| 439 | params["rlen"] // 8, |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 440 | None, |
| 441 | ctr_loc, |
| 442 | None, |
| 443 | None, |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 444 | binascii.unhexlify(params["fixedinputdata"]), |
| 445 | backend=backend, |
| 446 | ) |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 447 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 448 | ko = ctrkdf.derive(binascii.unhexlify(params["ki"])) |
Jared | 6d7fe00 | 2016-05-29 17:32:37 -0700 | [diff] [blame] | 449 | assert binascii.hexlify(ko) == params["ko"] |
| 450 | |
| 451 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 452 | def generate_rsa_verification_test( |
| 453 | param_loader, path, file_names, hash_alg, pad_factory |
| 454 | ): |
Paul Kehrer | b5936a7 | 2014-03-13 21:03:00 -0400 | [diff] [blame] | 455 | all_params = _load_all_params(path, file_names, param_loader) |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 456 | all_params = [ |
| 457 | i for i in all_params if i["algorithm"] == hash_alg.name.upper() |
| 458 | ] |
Paul Kehrer | b5936a7 | 2014-03-13 21:03:00 -0400 | [diff] [blame] | 459 | |
| 460 | @pytest.mark.parametrize("params", all_params) |
Paul Kehrer | f29c3c5 | 2014-03-19 09:35:40 -0400 | [diff] [blame] | 461 | def test_rsa_verification(self, backend, params): |
Paul Kehrer | c85f179 | 2014-03-19 09:45:42 -0400 | [diff] [blame] | 462 | rsa_verification_test(backend, params, hash_alg, pad_factory) |
Paul Kehrer | b5936a7 | 2014-03-13 21:03:00 -0400 | [diff] [blame] | 463 | |
Paul Kehrer | f29c3c5 | 2014-03-19 09:35:40 -0400 | [diff] [blame] | 464 | return test_rsa_verification |
Paul Kehrer | b5936a7 | 2014-03-13 21:03:00 -0400 | [diff] [blame] | 465 | |
| 466 | |
Paul Kehrer | c85f179 | 2014-03-19 09:45:42 -0400 | [diff] [blame] | 467 | def rsa_verification_test(backend, params, hash_alg, pad_factory): |
Paul Kehrer | 6af3c6e | 2014-06-06 21:05:23 -0500 | [diff] [blame] | 468 | public_numbers = rsa.RSAPublicNumbers( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 469 | e=params["public_exponent"], n=params["modulus"] |
Paul Kehrer | 762014e | 2014-03-16 17:47:22 -0400 | [diff] [blame] | 470 | ) |
Paul Kehrer | 144a415 | 2014-06-20 11:52:37 -0600 | [diff] [blame] | 471 | public_key = public_numbers.public_key(backend) |
Paul Kehrer | c85f179 | 2014-03-19 09:45:42 -0400 | [diff] [blame] | 472 | pad = pad_factory(params, hash_alg) |
Alex Gaynor | 0242c08 | 2017-12-09 20:58:52 -0500 | [diff] [blame] | 473 | signature = binascii.unhexlify(params["s"]) |
| 474 | msg = binascii.unhexlify(params["msg"]) |
Paul Kehrer | 49c8e21 | 2014-03-18 07:54:34 -0400 | [diff] [blame] | 475 | if params["fail"]: |
| 476 | with pytest.raises(InvalidSignature): |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 477 | public_key.verify(signature, msg, pad, hash_alg) |
Paul Kehrer | 49c8e21 | 2014-03-18 07:54:34 -0400 | [diff] [blame] | 478 | else: |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 479 | public_key.verify(signature, msg, pad, hash_alg) |
Alex Stapleton | 458c09b | 2014-04-23 20:58:37 +0100 | [diff] [blame] | 480 | |
| 481 | |
Paul Kehrer | 6af3c6e | 2014-06-06 21:05:23 -0500 | [diff] [blame] | 482 | def _check_rsa_private_numbers(skey): |
Alex Stapleton | 458c09b | 2014-04-23 20:58:37 +0100 | [diff] [blame] | 483 | assert skey |
Paul Kehrer | 6af3c6e | 2014-06-06 21:05:23 -0500 | [diff] [blame] | 484 | pkey = skey.public_numbers |
| 485 | assert pkey |
| 486 | assert pkey.e |
| 487 | assert pkey.n |
| 488 | assert skey.d |
| 489 | assert skey.p * skey.q == pkey.n |
Alex Stapleton | 458c09b | 2014-04-23 20:58:37 +0100 | [diff] [blame] | 490 | assert skey.dmp1 == rsa.rsa_crt_dmp1(skey.d, skey.p) |
| 491 | assert skey.dmq1 == rsa.rsa_crt_dmq1(skey.d, skey.q) |
| 492 | assert skey.iqmp == rsa.rsa_crt_iqmp(skey.p, skey.q) |
Paul Kehrer | 85c1106 | 2014-12-22 07:56:05 -0600 | [diff] [blame] | 493 | |
| 494 | |
| 495 | def _check_dsa_private_numbers(skey): |
| 496 | assert skey |
| 497 | pkey = skey.public_numbers |
| 498 | params = pkey.parameter_numbers |
| 499 | assert pow(params.g, skey.x, params.p) == pkey.y |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 500 | |
| 501 | |
| 502 | def skip_fips_traditional_openssl(backend, fmt): |
| 503 | if ( |
| 504 | fmt is serialization.PrivateFormat.TraditionalOpenSSL |
| 505 | and backend._fips_enabled |
| 506 | ): |
| 507 | pytest.skip( |
| 508 | "Traditional OpenSSL key format is not supported in FIPS mode." |
| 509 | ) |