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 | |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 14 | import textwrap |
| 15 | |
Alex Gaynor | cee8d52 | 2013-11-12 09:29:49 -0800 | [diff] [blame^] | 16 | import py |
Paul Kehrer | 79c16e9 | 2013-10-18 17:44:36 -0500 | [diff] [blame] | 17 | import pytest |
| 18 | |
Alex Gaynor | afdddca | 2013-10-21 21:00:20 -0700 | [diff] [blame] | 19 | from .utils import ( |
Paul Kehrer | f7f6a9f | 2013-11-11 20:43:52 -0600 | [diff] [blame] | 20 | load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors, |
| 21 | load_openssl_vectors, load_hash_vectors, |
Alex Gaynor | afdddca | 2013-10-21 21:00:20 -0700 | [diff] [blame] | 22 | ) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 23 | |
| 24 | |
Alex Gaynor | cf5fb33 | 2013-11-11 15:39:52 -0800 | [diff] [blame] | 25 | def test_load_nist_vectors(): |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 26 | vector_data = textwrap.dedent(""" |
| 27 | # CAVS 11.1 |
| 28 | # Config info for aes_values |
| 29 | # AESVS GFSbox test data for CBC |
| 30 | # State : Encrypt and Decrypt |
| 31 | # Key Length : 128 |
| 32 | # Generated on Fri Apr 22 15:11:33 2011 |
| 33 | |
| 34 | [ENCRYPT] |
| 35 | |
| 36 | COUNT = 0 |
| 37 | KEY = 00000000000000000000000000000000 |
| 38 | IV = 00000000000000000000000000000000 |
| 39 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 40 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 41 | |
| 42 | COUNT = 1 |
| 43 | KEY = 00000000000000000000000000000000 |
| 44 | IV = 00000000000000000000000000000000 |
| 45 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 46 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 47 | |
| 48 | [DECRYPT] |
| 49 | |
| 50 | COUNT = 0 |
| 51 | KEY = 00000000000000000000000000000000 |
| 52 | IV = 00000000000000000000000000000000 |
| 53 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 54 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 55 | |
| 56 | COUNT = 1 |
| 57 | KEY = 00000000000000000000000000000000 |
| 58 | IV = 00000000000000000000000000000000 |
| 59 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 60 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 61 | """).splitlines() |
| 62 | |
Alex Gaynor | d3ce703 | 2013-11-11 14:46:20 -0800 | [diff] [blame] | 63 | assert load_nist_vectors(vector_data) == [ |
| 64 | { |
| 65 | "key": b"00000000000000000000000000000000", |
| 66 | "iv": b"00000000000000000000000000000000", |
| 67 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 68 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 69 | }, |
| 70 | { |
| 71 | "key": b"00000000000000000000000000000000", |
| 72 | "iv": b"00000000000000000000000000000000", |
| 73 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 74 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 75 | }, |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 76 | { |
| 77 | "key": b"00000000000000000000000000000000", |
| 78 | "iv": b"00000000000000000000000000000000", |
| 79 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 80 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 81 | }, |
| 82 | { |
| 83 | "key": b"00000000000000000000000000000000", |
| 84 | "iv": b"00000000000000000000000000000000", |
| 85 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 86 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 87 | }, |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 88 | ] |
| 89 | |
| 90 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 91 | def test_load_cryptrec_vectors(): |
| 92 | vector_data = textwrap.dedent(""" |
| 93 | # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/ |
| 94 | # Download is t_camelia.txt |
| 95 | |
| 96 | # Camellia with 128-bit key |
| 97 | |
| 98 | K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 99 | |
| 100 | P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 101 | C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C |
| 102 | |
| 103 | P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 104 | C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3 |
| 105 | |
| 106 | K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 107 | |
| 108 | P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 109 | C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C |
| 110 | """).splitlines() |
| 111 | |
| 112 | assert load_cryptrec_vectors(vector_data) == [ |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame] | 113 | { |
| 114 | "key": b"00000000000000000000000000000000", |
| 115 | "plaintext": b"80000000000000000000000000000000", |
| 116 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 117 | }, |
| 118 | { |
| 119 | "key": b"00000000000000000000000000000000", |
| 120 | "plaintext": b"40000000000000000000000000000000", |
| 121 | "ciphertext": b"48CD6419809672D2349260D89A08D3D3", |
| 122 | }, |
| 123 | { |
| 124 | "key": b"10000000000000000000000000000000", |
| 125 | "plaintext": b"80000000000000000000000000000000", |
| 126 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 127 | }, |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 128 | ] |
| 129 | |
| 130 | |
Donald Stufft | 3359d7e | 2013-10-19 19:33:06 -0400 | [diff] [blame] | 131 | def test_load_cryptrec_vectors_invalid(): |
| 132 | vector_data = textwrap.dedent(""" |
| 133 | # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/ |
| 134 | # Download is t_camelia.txt |
| 135 | |
| 136 | # Camellia with 128-bit key |
| 137 | |
| 138 | E No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 139 | """).splitlines() |
| 140 | |
| 141 | with pytest.raises(ValueError): |
| 142 | load_cryptrec_vectors(vector_data) |
| 143 | |
| 144 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 145 | def test_load_openssl_vectors(): |
Paul Kehrer | 05d7214 | 2013-09-15 14:03:15 -0500 | [diff] [blame] | 146 | vector_data = textwrap.dedent( |
| 147 | """ |
| 148 | # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt |
| 149 | # For all CFB128 encrypts and decrypts, the transformed sequence is |
| 150 | # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec |
| 151 | # CFB128-CAMELLIA128.Encrypt |
| 152 | """ |
| 153 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 154 | "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" |
| 155 | "14F7646187817EB586599146B82BD719:1\n" |
| 156 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 157 | "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" |
| 158 | "A53D28BB82DF741103EA4F921A44880B:1\n\n" |
| 159 | "# CFB128-CAMELLIA128.Decrypt\n" |
| 160 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 161 | "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" |
| 162 | "14F7646187817EB586599146B82BD719:0\n" |
| 163 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 164 | "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" |
| 165 | "A53D28BB82DF741103EA4F921A44880B:0" |
| 166 | ).splitlines() |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 167 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 168 | assert load_openssl_vectors(vector_data) == [ |
Alex Gaynor | 016eed1 | 2013-10-16 14:16:04 -0700 | [diff] [blame] | 169 | { |
| 170 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 171 | "iv": b"000102030405060708090A0B0C0D0E0F", |
| 172 | "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", |
| 173 | "ciphertext": b"14F7646187817EB586599146B82BD719", |
| 174 | }, |
| 175 | { |
| 176 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 177 | "iv": b"14F7646187817EB586599146B82BD719", |
| 178 | "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 179 | "ciphertext": b"A53D28BB82DF741103EA4F921A44880B", |
| 180 | }, |
| 181 | { |
| 182 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 183 | "iv": b"000102030405060708090A0B0C0D0E0F", |
| 184 | "plaintext": b"6BC1BEE22E409F96E93D7E117393172A", |
| 185 | "ciphertext": b"14F7646187817EB586599146B82BD719", |
| 186 | }, |
| 187 | { |
| 188 | "key": b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 189 | "iv": b"14F7646187817EB586599146B82BD719", |
| 190 | "plaintext": b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 191 | "ciphertext": b"A53D28BB82DF741103EA4F921A44880B", |
| 192 | }, |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 193 | ] |
| 194 | |
| 195 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 196 | def test_load_hash_vectors(): |
| 197 | vector_data = textwrap.dedent(""" |
| 198 | |
| 199 | # http://tools.ietf.org/html/rfc1321 |
Paul Kehrer | 87cd0db | 2013-10-18 18:01:26 -0500 | [diff] [blame] | 200 | [irrelevant] |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 201 | |
| 202 | Len = 0 |
| 203 | Msg = 00 |
| 204 | MD = d41d8cd98f00b204e9800998ecf8427e |
| 205 | |
| 206 | Len = 8 |
| 207 | Msg = 61 |
| 208 | MD = 0cc175b9c0f1b6a831c399e269772661 |
| 209 | |
| 210 | Len = 24 |
| 211 | Msg = 616263 |
| 212 | MD = 900150983cd24fb0d6963f7d28e17f72 |
| 213 | |
| 214 | Len = 112 |
| 215 | Msg = 6d65737361676520646967657374 |
| 216 | MD = f96b697d7cb7938d525a2f31aaf161d0 |
| 217 | """).splitlines() |
| 218 | assert load_hash_vectors(vector_data) == [ |
Paul Kehrer | 79c16e9 | 2013-10-18 17:44:36 -0500 | [diff] [blame] | 219 | (b"", "d41d8cd98f00b204e9800998ecf8427e"), |
| 220 | (b"61", "0cc175b9c0f1b6a831c399e269772661"), |
| 221 | (b"616263", "900150983cd24fb0d6963f7d28e17f72"), |
| 222 | (b"6d65737361676520646967657374", "f96b697d7cb7938d525a2f31aaf161d0"), |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 223 | ] |
| 224 | |
| 225 | |
Paul Kehrer | 0317b04 | 2013-10-28 17:34:27 -0500 | [diff] [blame] | 226 | def test_load_hmac_vectors(): |
| 227 | vector_data = textwrap.dedent(""" |
| 228 | Len = 224 |
| 229 | # "Jefe" |
| 230 | Key = 4a656665 |
| 231 | # "what do ya want for nothing?" |
| 232 | Msg = 7768617420646f2079612077616e7420666f72206e6f7468696e673f |
| 233 | MD = 750c783e6ab0b503eaa86e310a5db738 |
| 234 | """).splitlines() |
| 235 | assert load_hash_vectors(vector_data) == [ |
| 236 | (b"7768617420646f2079612077616e7420666f72206e6f7468696e673f", |
| 237 | "750c783e6ab0b503eaa86e310a5db738", |
| 238 | b"4a656665"), |
| 239 | ] |
| 240 | |
| 241 | |
Paul Kehrer | 69e0652 | 2013-10-18 17:28:39 -0500 | [diff] [blame] | 242 | def test_load_hash_vectors_bad_data(): |
| 243 | vector_data = textwrap.dedent(""" |
| 244 | # http://tools.ietf.org/html/rfc1321 |
| 245 | |
| 246 | Len = 0 |
| 247 | Msg = 00 |
| 248 | UNKNOWN=Hello World |
| 249 | """).splitlines() |
| 250 | with pytest.raises(ValueError): |
| 251 | load_hash_vectors(vector_data) |
| 252 | |
Alex Gaynor | cee8d52 | 2013-11-12 09:29:49 -0800 | [diff] [blame^] | 253 | def test_load_vectors_from_file(request): |
| 254 | path = py.path.local(__file__).dirpath().join( |
| 255 | "hazmat", "primitives", "vectors", "t.txt" |
Paul Kehrer | 2b75867 | 2013-10-30 09:01:38 -0500 | [diff] [blame] | 256 | ) |
Alex Gaynor | cee8d52 | 2013-11-12 09:29:49 -0800 | [diff] [blame^] | 257 | path.write(textwrap.dedent(""" |
| 258 | abc |
| 259 | 123 |
| 260 | """)) |
| 261 | request.addfinalizer(path.remove) |
| 262 | vectors = load_vectors_from_file("t.txt", lambda f: f.readlines()) |
| 263 | assert vectors == ["\n", "abc\n", "123\n"] |