Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 | # implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
Alex Gaynor | ab53bc5 | 2013-11-12 09:37:59 -0800 | [diff] [blame] | 14 | import os |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 15 | import textwrap |
| 16 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 17 | import pretend |
| 18 | |
Paul Kehrer | 79c16e9 | 2013-10-18 17:44:36 -0500 | [diff] [blame] | 19 | import pytest |
| 20 | |
Alex Gaynor | afdddca | 2013-10-21 21:00:20 -0700 | [diff] [blame] | 21 | from .utils import ( |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 22 | load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors, |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 23 | load_openssl_vectors, load_hash_vectors, check_for_iface, |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 24 | check_backend_support, select_backends |
Alex Gaynor | afdddca | 2013-10-21 21:00:20 -0700 | [diff] [blame] | 25 | ) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 26 | |
| 27 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 28 | class FakeInterface(object): |
| 29 | pass |
| 30 | |
| 31 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 32 | def test_select_one_backend(): |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 33 | b1 = pretend.stub(name="b1") |
| 34 | b2 = pretend.stub(name="b2") |
| 35 | b3 = pretend.stub(name="b3") |
| 36 | backends = [b1, b2, b3] |
| 37 | name = "b2" |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 38 | selected_backends = select_backends(name, backends) |
| 39 | assert len(selected_backends) == 1 |
| 40 | assert selected_backends[0] == b2 |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 41 | |
| 42 | |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 43 | def test_select_no_backend(): |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 44 | b1 = pretend.stub(name="b1") |
| 45 | b2 = pretend.stub(name="b2") |
| 46 | b3 = pretend.stub(name="b3") |
| 47 | backends = [b1, b2, b3] |
| 48 | name = "back!" |
| 49 | with pytest.raises(ValueError): |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 50 | select_backends(name, backends) |
| 51 | |
| 52 | |
| 53 | def test_select_backends_none(): |
| 54 | b1 = pretend.stub(name="b1") |
| 55 | b2 = pretend.stub(name="b2") |
| 56 | b3 = pretend.stub(name="b3") |
| 57 | backends = [b1, b2, b3] |
| 58 | name = None |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 59 | selected_backends = select_backends(name, backends) |
| 60 | assert len(selected_backends) == 3 |
Paul Kehrer | c421e63 | 2014-01-18 09:22:21 -0600 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def test_select_two_backends(): |
| 64 | b1 = pretend.stub(name="b1") |
| 65 | b2 = pretend.stub(name="b2") |
| 66 | b3 = pretend.stub(name="b3") |
| 67 | backends = [b1, b2, b3] |
| 68 | name = "b2 ,b1 " |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 69 | selected_backends = select_backends(name, backends) |
| 70 | assert len(selected_backends) == 2 |
| 71 | assert selected_backends == [b1, b2] |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 72 | |
| 73 | |
Alex Gaynor | 2b3f942 | 2013-12-24 21:55:24 -0800 | [diff] [blame] | 74 | def test_check_for_iface(): |
| 75 | item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True}) |
| 76 | with pytest.raises(pytest.skip.Exception) as exc_info: |
| 77 | check_for_iface("fake_name", FakeInterface, item) |
| 78 | assert exc_info.value.args[0] == "True backend does not support fake_name" |
| 79 | |
| 80 | item = pretend.stub( |
| 81 | keywords=["fake_name"], |
| 82 | funcargs={"backend": FakeInterface()} |
| 83 | ) |
| 84 | check_for_iface("fake_name", FakeInterface, item) |
| 85 | |
| 86 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 87 | def test_check_backend_support_skip(): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 88 | supported = pretend.stub( |
| 89 | kwargs={"only_if": lambda backend: False, "skip_message": "Nope"} |
| 90 | ) |
| 91 | item = pretend.stub(keywords={"supported": supported}, |
| 92 | funcargs={"backend": True}) |
| 93 | with pytest.raises(pytest.skip.Exception) as exc_info: |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 94 | check_backend_support(item) |
Paul Kehrer | f03334e | 2014-01-02 23:16:14 -0600 | [diff] [blame] | 95 | assert exc_info.value.args[0] == "Nope (True)" |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 96 | |
| 97 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 98 | def test_check_backend_support_no_skip(): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 99 | supported = pretend.stub( |
| 100 | kwargs={"only_if": lambda backend: True, "skip_message": "Nope"} |
| 101 | ) |
| 102 | item = pretend.stub(keywords={"supported": supported}, |
| 103 | funcargs={"backend": True}) |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 104 | assert check_backend_support(item) is None |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 105 | |
| 106 | |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 107 | def test_check_backend_support_no_backend(): |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 108 | supported = pretend.stub( |
| 109 | kwargs={"only_if": "notalambda", "skip_message": "Nope"} |
| 110 | ) |
| 111 | item = pretend.stub(keywords={"supported": supported}, |
| 112 | funcargs={}) |
Paul Kehrer | ec49550 | 2013-12-27 15:51:40 -0600 | [diff] [blame] | 113 | with pytest.raises(ValueError): |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 114 | check_backend_support(item) |
Paul Kehrer | 5a8fdf8 | 2013-12-26 20:13:45 -0600 | [diff] [blame] | 115 | |
| 116 | |
Alex Gaynor | cf5fb33 | 2013-11-11 15:39:52 -0800 | [diff] [blame] | 117 | def test_load_nist_vectors(): |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 118 | vector_data = textwrap.dedent(""" |
| 119 | # CAVS 11.1 |
| 120 | # Config info for aes_values |
| 121 | # AESVS GFSbox test data for CBC |
| 122 | # State : Encrypt and Decrypt |
| 123 | # Key Length : 128 |
| 124 | # Generated on Fri Apr 22 15:11:33 2011 |
| 125 | |
| 126 | [ENCRYPT] |
| 127 | |
| 128 | COUNT = 0 |
| 129 | KEY = 00000000000000000000000000000000 |
| 130 | IV = 00000000000000000000000000000000 |
| 131 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 132 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 133 | |
| 134 | COUNT = 1 |
| 135 | KEY = 00000000000000000000000000000000 |
| 136 | IV = 00000000000000000000000000000000 |
| 137 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 138 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 139 | |
| 140 | [DECRYPT] |
| 141 | |
| 142 | COUNT = 0 |
| 143 | KEY = 00000000000000000000000000000000 |
| 144 | IV = 00000000000000000000000000000000 |
| 145 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 146 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 147 | |
| 148 | COUNT = 1 |
| 149 | KEY = 00000000000000000000000000000000 |
| 150 | IV = 00000000000000000000000000000000 |
| 151 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 152 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 153 | """).splitlines() |
| 154 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 155 | assert load_nist_vectors(vector_data) == [ |
| 156 | { |
| 157 | "key": b"00000000000000000000000000000000", |
| 158 | "iv": b"00000000000000000000000000000000", |
| 159 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 160 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 161 | }, |
| 162 | { |
| 163 | "key": b"00000000000000000000000000000000", |
| 164 | "iv": b"00000000000000000000000000000000", |
| 165 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 166 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 167 | }, |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 168 | { |
| 169 | "key": b"00000000000000000000000000000000", |
| 170 | "iv": b"00000000000000000000000000000000", |
| 171 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 172 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 173 | }, |
| 174 | { |
| 175 | "key": b"00000000000000000000000000000000", |
| 176 | "iv": b"00000000000000000000000000000000", |
| 177 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 178 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 179 | }, |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 180 | ] |
| 181 | |
| 182 | |
Paul Kehrer | 6fb1a5a | 2014-01-29 13:44:07 -0600 | [diff] [blame] | 183 | def test_load_nist_vectors_with_null_chars(): |
| 184 | vector_data = textwrap.dedent(""" |
| 185 | COUNT = 0 |
| 186 | KEY = thing\\0withnulls |
| 187 | |
| 188 | COUNT = 1 |
| 189 | KEY = 00000000000000000000000000000000 |
| 190 | """).splitlines() |
| 191 | |
| 192 | assert load_nist_vectors(vector_data) == [ |
| 193 | { |
| 194 | "key": b"thing\x00withnulls", |
| 195 | }, |
| 196 | { |
| 197 | "key": b"00000000000000000000000000000000", |
| 198 | }, |
| 199 | ] |
| 200 | |
| 201 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 202 | def test_load_cryptrec_vectors(): |
| 203 | vector_data = textwrap.dedent(""" |
| 204 | # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/ |
| 205 | # Download is t_camelia.txt |
| 206 | |
| 207 | # Camellia with 128-bit key |
| 208 | |
| 209 | K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 210 | |
| 211 | P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 212 | C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C |
| 213 | |
| 214 | P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 215 | C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3 |
| 216 | |
| 217 | K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 218 | |
| 219 | P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 220 | C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C |
| 221 | """).splitlines() |
| 222 | |
| 223 | assert load_cryptrec_vectors(vector_data) == [ |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 224 | { |
| 225 | "key": b"00000000000000000000000000000000", |
| 226 | "plaintext": b"80000000000000000000000000000000", |
| 227 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 228 | }, |
| 229 | { |
| 230 | "key": b"00000000000000000000000000000000", |
| 231 | "plaintext": b"40000000000000000000000000000000", |
| 232 | "ciphertext": b"48CD6419809672D2349260D89A08D3D3", |
| 233 | }, |
| 234 | { |
| 235 | "key": b"10000000000000000000000000000000", |
| 236 | "plaintext": b"80000000000000000000000000000000", |
| 237 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 238 | }, |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 239 | ] |
| 240 | |
| 241 | |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 242 | def test_load_cryptrec_vectors_invalid(): |
| 243 | vector_data = textwrap.dedent(""" |
| 244 | # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/ |
| 245 | # Download is t_camelia.txt |
| 246 | |
| 247 | # Camellia with 128-bit key |
| 248 | |
| 249 | E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 250 | """).splitlines() |
| 251 | |
| 252 | with pytest.raises(ValueError): |
| 253 | load_cryptrec_vectors(vector_data) |
| 254 | |
| 255 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 256 | def test_load_openssl_vectors(): |
Paul Kehrer | 05d7214 | 2013-09-15 14:03:15 -0500 | [diff] [blame] | 257 | vector_data = textwrap.dedent( |
| 258 | """ |
| 259 | # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt |
| 260 | # For all CFB128 encrypts and decrypts, the transformed sequence is |
| 261 | # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec |
| 262 | # CFB128-CAMELLIA128.Encrypt |
| 263 | """ |
| 264 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 265 | "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" |
| 266 | "14F7646187817EB586599146B82BD719:1\n" |
| 267 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 268 | "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" |
| 269 | "A53D28BB82DF741103EA4F921A44880B:1\n\n" |
| 270 | "# CFB128-CAMELLIA128.Decrypt\n" |
| 271 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 272 | "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" |
| 273 | "14F7646187817EB586599146B82BD719:0\n" |
| 274 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 275 | "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" |
| 276 | "A53D28BB82DF741103EA4F921A44880B:0" |
| 277 | ).splitlines() |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 278 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 279 | assert load_openssl_vectors(vector_data) == [ |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 280 | { |
| 281 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 282 | "iv": b"000102030405060708090A0B0C0D0E0F", |
| 283 | "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", |
| 284 | "ciphertext": b"14F7646187817EB586599146B82BD719", |
| 285 | }, |
| 286 | { |
| 287 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 288 | "iv": b"14F7646187817EB586599146B82BD719", |
| 289 | "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 290 | "ciphertext": b"A53D28BB82DF741103EA4F921A44880B", |
| 291 | }, |
| 292 | { |
| 293 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 294 | "iv": b"000102030405060708090A0B0C0D0E0F", |
| 295 | "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", |
| 296 | "ciphertext": b"14F7646187817EB586599146B82BD719", |
| 297 | }, |
| 298 | { |
| 299 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 300 | "iv": b"14F7646187817EB586599146B82BD719", |
| 301 | "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 302 | "ciphertext": b"A53D28BB82DF741103EA4F921A44880B", |
| 303 | }, |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 304 | ] |
| 305 | |
| 306 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 307 | def test_load_hash_vectors(): |
| 308 | vector_data = textwrap.dedent(""" |
| 309 | |
| 310 | # http://tools.ietf.org/html/rfc1321 |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 311 | [irrelevant] |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 312 | |
| 313 | Len = 0 |
| 314 | Msg = 00 |
| 315 | MD = d41d8cd98f00b204e9800998ecf8427e |
| 316 | |
| 317 | Len = 8 |
| 318 | Msg = 61 |
| 319 | MD = 0cc175b9c0f1b6a831c399e269772661 |
| 320 | |
| 321 | Len = 24 |
| 322 | Msg = 616263 |
| 323 | MD = 900150983cd24fb0d6963f7d28e17f72 |
| 324 | |
| 325 | Len = 112 |
| 326 | Msg = 6d65737361676520646967657374 |
| 327 | MD = f96b697d7cb7938d525a2f31aaf161d0 |
| 328 | """).splitlines() |
| 329 | assert load_hash_vectors(vector_data) == [ |
Paul Kehrer | 79c16e9 | 2013-10-18 17:44:36 -0500 | [diff] [blame] | 330 | (b"", "d41d8cd98f00b204e9800998ecf8427e"), |
| 331 | (b"61", "0cc175b9c0f1b6a831c399e269772661"), |
| 332 | (b"616263", "900150983cd24fb0d6963f7d28e17f72"), |
| 333 | (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"), |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 334 | ] |
| 335 | |
| 336 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 337 | def test_load_hmac_vectors(): |
| 338 | vector_data = textwrap.dedent(""" |
| 339 | Len = 224 |
| 340 | # "Jefe" |
| 341 | Key = 4a656665 |
| 342 | # "what do ya want for nothing?" |
| 343 | Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f |
| 344 | MD = 750c783e6ab0b503eaa86e310a5db738 |
| 345 | """).splitlines() |
| 346 | assert load_hash_vectors(vector_data) == [ |
| 347 | (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f", |
| 348 | "750c783e6ab0b503eaa86e310a5db738", |
| 349 | b"4a656665"), |
| 350 | ] |
| 351 | |
| 352 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 353 | def test_load_hash_vectors_bad_data(): |
| 354 | vector_data = textwrap.dedent(""" |
| 355 | # http://tools.ietf.org/html/rfc1321 |
| 356 | |
| 357 | Len = 0 |
| 358 | Msg = 00 |
| 359 | UNKNOWN=Hello World |
| 360 | """).splitlines() |
| 361 | with pytest.raises(ValueError): |
| 362 | load_hash_vectors(vector_data) |
| 363 | |
Alex Gaynor | 41172ab | 2013-11-12 10:00:42 -0800 | [diff] [blame] | 364 | |
Alex Gaynor | ab53bc5 | 2013-11-12 09:37:59 -0800 | [diff] [blame] | 365 | def test_load_vectors_from_file(): |
| 366 | vectors = load_vectors_from_file( |
| 367 | os.path.join("ciphers", "Blowfish", "bf-cfb.txt"), |
| 368 | load_nist_vectors, |
Paul Kehrer | 2b75867 | 2013-10-30 09:01:38 -0500 | [diff] [blame] | 369 | ) |
Alex Gaynor | ab53bc5 | 2013-11-12 09:37:59 -0800 | [diff] [blame] | 370 | assert vectors == [ |
| 371 | { |
Alex Gaynor | c2f45d5 | 2013-11-12 09:50:25 -0800 | [diff] [blame] | 372 | "key": b"0123456789ABCDEFF0E1D2C3B4A59687", |
| 373 | "iv": b"FEDCBA9876543210", |
Alex Gaynor | ab53bc5 | 2013-11-12 09:37:59 -0800 | [diff] [blame] | 374 | "plaintext": ( |
Alex Gaynor | c2f45d5 | 2013-11-12 09:50:25 -0800 | [diff] [blame] | 375 | b"37363534333231204E6F77206973207468652074696D6520666F722000" |
Alex Gaynor | ab53bc5 | 2013-11-12 09:37:59 -0800 | [diff] [blame] | 376 | ), |
| 377 | "ciphertext": ( |
Alex Gaynor | c2f45d5 | 2013-11-12 09:50:25 -0800 | [diff] [blame] | 378 | b"E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3" |
Alex Gaynor | ab53bc5 | 2013-11-12 09:37:59 -0800 | [diff] [blame] | 379 | ), |
| 380 | } |
| 381 | ] |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 382 | |
| 383 | |
| 384 | def test_load_nist_gcm_vectors(): |
| 385 | vector_data = textwrap.dedent(""" |
| 386 | [Keylen = 128] |
| 387 | [IVlen = 96] |
| 388 | [PTlen = 0] |
| 389 | [AADlen = 0] |
| 390 | [Taglen = 128] |
| 391 | |
| 392 | Count = 0 |
| 393 | Key = 11754cd72aec309bf52f7687212e8957 |
| 394 | IV = 3c819d9a9bed087615030b65 |
| 395 | PT = |
| 396 | AAD = |
| 397 | CT = |
| 398 | Tag = 250327c674aaf477aef2675748cf6971 |
| 399 | |
| 400 | Count = 1 |
| 401 | Key = 272f16edb81a7abbea887357a58c1917 |
| 402 | IV = 794ec588176c703d3d2a7a07 |
| 403 | PT = |
| 404 | AAD = |
| 405 | CT = |
| 406 | Tag = b6e6f197168f5049aeda32dafbdaeb |
| 407 | |
| 408 | Count = 2 |
| 409 | Key = a49a5e26a2f8cb63d05546c2a62f5343 |
| 410 | IV = 907763b19b9b4ab6bd4f0281 |
| 411 | CT = |
| 412 | AAD = |
| 413 | Tag = a2be08210d8c470a8df6e8fbd79ec5cf |
| 414 | FAIL |
| 415 | |
| 416 | Count = 3 |
| 417 | Key = 5c1155084cc0ede76b3bc22e9f7574ef |
| 418 | IV = 9549e4ba69a61cad7856efc1 |
| 419 | PT = d1448fa852b84408e2dad8381f363de7 |
| 420 | AAD = e98e9d9c618e46fef32660976f854ee3 |
| 421 | CT = f78b60ca125218493bea1c50a2e12ef4 |
| 422 | Tag = d72da7f5c6cf0bca7242c71835809449 |
| 423 | |
| 424 | [Keylen = 128] |
| 425 | [IVlen = 96] |
| 426 | [PTlen = 0] |
| 427 | [AADlen = 0] |
| 428 | [Taglen = 120] |
| 429 | |
| 430 | Count = 0 |
| 431 | Key = eac258e99c55e6ae8ef1da26640613d7 |
| 432 | IV = 4e8df20faaf2c8eebe922902 |
| 433 | CT = |
| 434 | AAD = |
| 435 | Tag = e39aeaebe86aa309a4d062d6274339 |
| 436 | PT = |
| 437 | |
| 438 | Count = 1 |
| 439 | Key = 3726cf02fcc6b8639a5497652c94350d |
| 440 | IV = 55fef82cde693ce76efcc193 |
| 441 | CT = |
| 442 | AAD = |
| 443 | Tag = 3d68111a81ed22d2ef5bccac4fc27f |
| 444 | FAIL |
| 445 | |
| 446 | Count = 2 |
| 447 | Key = f202299d5fd74f03b12d2119a6c4c038 |
| 448 | IV = eec51e7958c3f20a1bb71815 |
| 449 | CT = |
| 450 | AAD = |
| 451 | Tag = a81886b3fb26e51fca87b267e1e157 |
| 452 | FAIL |
| 453 | |
| 454 | Count = 3 |
| 455 | Key = fd52925f39546b4c55ffb6b20c59898c |
| 456 | IV = f5cf3227444afd905a5f6dba |
| 457 | CT = |
| 458 | AAD = |
| 459 | Tag = 1665b0f1a0b456e1664cfd3de08ccd |
| 460 | PT = |
Paul Kehrer | c985dbb | 2013-11-18 14:11:55 -0600 | [diff] [blame] | 461 | |
| 462 | [Keylen = 128] |
| 463 | [IVlen = 8] |
| 464 | [PTlen = 104] |
| 465 | [AADlen = 0] |
| 466 | [Taglen = 128] |
| 467 | |
| 468 | Count = 0 |
| 469 | Key = 58fab7632bcf10d2bcee58520bf37414 |
| 470 | IV = 3c |
| 471 | CT = 15c4db4cbb451211179d57017f |
| 472 | AAD = |
| 473 | Tag = eae841d4355feeb3f786bc86625f1e5b |
| 474 | FAIL |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 475 | """).splitlines() |
| 476 | assert load_nist_vectors(vector_data) == [ |
| 477 | {'aad': b'', |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 478 | 'pt': b'', |
| 479 | 'iv': b'3c819d9a9bed087615030b65', |
| 480 | 'tag': b'250327c674aaf477aef2675748cf6971', |
| 481 | 'key': b'11754cd72aec309bf52f7687212e8957', |
| 482 | 'ct': b''}, |
| 483 | {'aad': b'', |
| 484 | 'pt': b'', |
| 485 | 'iv': b'794ec588176c703d3d2a7a07', |
| 486 | 'tag': b'b6e6f197168f5049aeda32dafbdaeb', |
| 487 | 'key': b'272f16edb81a7abbea887357a58c1917', |
| 488 | 'ct': b''}, |
| 489 | {'aad': b'', |
| 490 | 'iv': b'907763b19b9b4ab6bd4f0281', |
| 491 | 'tag': b'a2be08210d8c470a8df6e8fbd79ec5cf', |
| 492 | 'key': b'a49a5e26a2f8cb63d05546c2a62f5343', |
| 493 | 'ct': b'', |
Paul Kehrer | c985dbb | 2013-11-18 14:11:55 -0600 | [diff] [blame] | 494 | 'fail': True}, |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 495 | {'aad': b'e98e9d9c618e46fef32660976f854ee3', |
| 496 | 'pt': b'd1448fa852b84408e2dad8381f363de7', |
| 497 | 'iv': b'9549e4ba69a61cad7856efc1', |
| 498 | 'tag': b'd72da7f5c6cf0bca7242c71835809449', |
| 499 | 'key': b'5c1155084cc0ede76b3bc22e9f7574ef', |
| 500 | 'ct': b'f78b60ca125218493bea1c50a2e12ef4'}, |
Paul Kehrer | c985dbb | 2013-11-18 14:11:55 -0600 | [diff] [blame] | 501 | {'aad': b'', |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 502 | 'pt': b'', |
| 503 | 'iv': b'4e8df20faaf2c8eebe922902', |
| 504 | 'tag': b'e39aeaebe86aa309a4d062d6274339', |
| 505 | 'key': b'eac258e99c55e6ae8ef1da26640613d7', |
| 506 | 'ct': b''}, |
| 507 | {'aad': b'', |
| 508 | 'iv': b'55fef82cde693ce76efcc193', |
| 509 | 'tag': b'3d68111a81ed22d2ef5bccac4fc27f', |
| 510 | 'key': b'3726cf02fcc6b8639a5497652c94350d', |
| 511 | 'ct': b'', |
| 512 | 'fail': True}, |
| 513 | {'aad': b'', |
| 514 | 'iv': b'eec51e7958c3f20a1bb71815', |
| 515 | 'tag': b'a81886b3fb26e51fca87b267e1e157', |
| 516 | 'key': b'f202299d5fd74f03b12d2119a6c4c038', |
| 517 | 'ct': b'', |
| 518 | 'fail': True}, |
| 519 | {'aad': b'', |
| 520 | 'pt': b'', |
| 521 | 'iv': b'f5cf3227444afd905a5f6dba', |
| 522 | 'tag': b'1665b0f1a0b456e1664cfd3de08ccd', |
| 523 | 'key': b'fd52925f39546b4c55ffb6b20c59898c', |
| 524 | 'ct': b''}, |
| 525 | {'aad': b'', |
Paul Kehrer | 749ac5b | 2013-11-18 18:12:41 -0600 | [diff] [blame] | 526 | 'iv': b'3c', |
| 527 | 'tag': b'eae841d4355feeb3f786bc86625f1e5b', |
| 528 | 'key': b'58fab7632bcf10d2bcee58520bf37414', |
| 529 | 'ct': b'15c4db4cbb451211179d57017f', |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 530 | 'fail': True}, |
Paul Kehrer | a43b669 | 2013-11-12 15:35:49 -0600 | [diff] [blame] | 531 | ] |