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 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 16 | from .utils import (load_nist_vectors, load_nist_vectors_from_file, |
| 17 | load_cryptrec_vectors, load_cryptrec_vectors_from_file, |
| 18 | load_openssl_vectors, load_openssl_vectors_from_file) |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def test_load_nist_vectors_encrypt(): |
| 22 | vector_data = textwrap.dedent(""" |
| 23 | # CAVS 11.1 |
| 24 | # Config info for aes_values |
| 25 | # AESVS GFSbox test data for CBC |
| 26 | # State : Encrypt and Decrypt |
| 27 | # Key Length : 128 |
| 28 | # Generated on Fri Apr 22 15:11:33 2011 |
| 29 | |
| 30 | [ENCRYPT] |
| 31 | |
| 32 | COUNT = 0 |
| 33 | KEY = 00000000000000000000000000000000 |
| 34 | IV = 00000000000000000000000000000000 |
| 35 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 36 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 37 | |
| 38 | COUNT = 1 |
| 39 | KEY = 00000000000000000000000000000000 |
| 40 | IV = 00000000000000000000000000000000 |
| 41 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 42 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 43 | |
| 44 | [DECRYPT] |
| 45 | |
| 46 | COUNT = 0 |
| 47 | KEY = 00000000000000000000000000000000 |
| 48 | IV = 00000000000000000000000000000000 |
| 49 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 50 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 51 | |
| 52 | COUNT = 1 |
| 53 | KEY = 00000000000000000000000000000000 |
| 54 | IV = 00000000000000000000000000000000 |
| 55 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 56 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 57 | """).splitlines() |
| 58 | |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 59 | assert load_nist_vectors(vector_data, "ENCRYPT") == [ |
| 60 | { |
| 61 | "key": b"00000000000000000000000000000000", |
| 62 | "iv": b"00000000000000000000000000000000", |
| 63 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 64 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 65 | }, |
| 66 | { |
| 67 | "key": b"00000000000000000000000000000000", |
| 68 | "iv": b"00000000000000000000000000000000", |
| 69 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 70 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 71 | }, |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 72 | ] |
| 73 | |
| 74 | |
| 75 | def test_load_nist_vectors_decrypt(): |
| 76 | vector_data = textwrap.dedent(""" |
| 77 | # CAVS 11.1 |
| 78 | # Config info for aes_values |
| 79 | # AESVS GFSbox test data for CBC |
| 80 | # State : Encrypt and Decrypt |
| 81 | # Key Length : 128 |
| 82 | # Generated on Fri Apr 22 15:11:33 2011 |
| 83 | |
| 84 | [ENCRYPT] |
| 85 | |
| 86 | COUNT = 0 |
| 87 | KEY = 00000000000000000000000000000000 |
| 88 | IV = 00000000000000000000000000000000 |
| 89 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 90 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 91 | |
| 92 | COUNT = 1 |
| 93 | KEY = 00000000000000000000000000000000 |
| 94 | IV = 00000000000000000000000000000000 |
| 95 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 96 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 97 | |
| 98 | [DECRYPT] |
| 99 | |
| 100 | COUNT = 0 |
| 101 | KEY = 00000000000000000000000000000000 |
| 102 | IV = 00000000000000000000000000000000 |
| 103 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 104 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 105 | |
| 106 | COUNT = 1 |
| 107 | KEY = 00000000000000000000000000000000 |
| 108 | IV = 00000000000000000000000000000000 |
| 109 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 110 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 111 | """).splitlines() |
| 112 | |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 113 | assert load_nist_vectors(vector_data, "DECRYPT") == [ |
| 114 | { |
| 115 | "key": b"00000000000000000000000000000000", |
| 116 | "iv": b"00000000000000000000000000000000", |
| 117 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 118 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 119 | }, |
| 120 | { |
| 121 | "key": b"00000000000000000000000000000000", |
| 122 | "iv": b"00000000000000000000000000000000", |
| 123 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 124 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 125 | }, |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 126 | ] |
| 127 | |
| 128 | |
| 129 | def test_load_nist_vectors_from_file_encrypt(): |
| 130 | assert load_nist_vectors_from_file( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 131 | "AES/KAT/CBCGFSbox128.rsp", |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 132 | "ENCRYPT" |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 133 | ) == [ |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 134 | { |
| 135 | "key": b"00000000000000000000000000000000", |
| 136 | "iv": b"00000000000000000000000000000000", |
| 137 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 138 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 139 | }, |
| 140 | { |
| 141 | "key": b"00000000000000000000000000000000", |
| 142 | "iv": b"00000000000000000000000000000000", |
| 143 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 144 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 145 | }, |
| 146 | { |
| 147 | "key": b"00000000000000000000000000000000", |
| 148 | "iv": b"00000000000000000000000000000000", |
| 149 | "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168", |
| 150 | "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597", |
| 151 | }, |
| 152 | { |
| 153 | "key": b"00000000000000000000000000000000", |
| 154 | "iv": b"00000000000000000000000000000000", |
| 155 | "plaintext": b"6a118a874519e64e9963798a503f1d35", |
| 156 | "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209", |
| 157 | }, |
| 158 | { |
| 159 | "key": b"00000000000000000000000000000000", |
| 160 | "iv": b"00000000000000000000000000000000", |
| 161 | "plaintext": b"cb9fceec81286ca3e989bd979b0cb284", |
| 162 | "ciphertext": b"92beedab1895a94faa69b632e5cc47ce", |
| 163 | }, |
| 164 | { |
| 165 | "key": b"00000000000000000000000000000000", |
| 166 | "iv": b"00000000000000000000000000000000", |
| 167 | "plaintext": b"b26aeb1874e47ca8358ff22378f09144", |
| 168 | "ciphertext": b"459264f4798f6a78bacb89c15ed3d601", |
| 169 | }, |
| 170 | { |
| 171 | "key": b"00000000000000000000000000000000", |
| 172 | "iv": b"00000000000000000000000000000000", |
| 173 | "plaintext": b"58c8e00b2631686d54eab84b91f0aca1", |
| 174 | "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf", |
| 175 | }, |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 176 | ] |
| 177 | |
| 178 | |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 179 | def test_load_nist_vectors_from_file_decrypt(): |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 180 | assert load_nist_vectors_from_file( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 181 | "AES/KAT/CBCGFSbox128.rsp", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 182 | "DECRYPT", |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 183 | ) == [ |
| 184 | { |
| 185 | "key": b"00000000000000000000000000000000", |
| 186 | "iv": b"00000000000000000000000000000000", |
| 187 | "plaintext": b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 188 | "ciphertext": b"0336763e966d92595a567cc9ce537f5e", |
| 189 | }, |
| 190 | { |
| 191 | "key": b"00000000000000000000000000000000", |
| 192 | "iv": b"00000000000000000000000000000000", |
| 193 | "plaintext": b"9798c4640bad75c7c3227db910174e72", |
| 194 | "ciphertext": b"a9a1631bf4996954ebc093957b234589", |
| 195 | }, |
| 196 | { |
| 197 | "key": b"00000000000000000000000000000000", |
| 198 | "iv": b"00000000000000000000000000000000", |
| 199 | "plaintext": b"96ab5c2ff612d9dfaae8c31f30c42168", |
| 200 | "ciphertext": b"ff4f8391a6a40ca5b25d23bedd44a597", |
| 201 | }, |
| 202 | { |
| 203 | "key": b"00000000000000000000000000000000", |
| 204 | "iv": b"00000000000000000000000000000000", |
| 205 | "plaintext": b"6a118a874519e64e9963798a503f1d35", |
| 206 | "ciphertext": b"dc43be40be0e53712f7e2bf5ca707209", |
| 207 | }, |
| 208 | { |
| 209 | "key": b"00000000000000000000000000000000", |
| 210 | "iv": b"00000000000000000000000000000000", |
| 211 | "plaintext": b"cb9fceec81286ca3e989bd979b0cb284", |
| 212 | "ciphertext": b"92beedab1895a94faa69b632e5cc47ce", |
| 213 | }, |
| 214 | { |
| 215 | "key": b"00000000000000000000000000000000", |
| 216 | "iv": b"00000000000000000000000000000000", |
| 217 | "plaintext": b"b26aeb1874e47ca8358ff22378f09144", |
| 218 | "ciphertext": b"459264f4798f6a78bacb89c15ed3d601", |
| 219 | }, |
| 220 | { |
| 221 | "key": b"00000000000000000000000000000000", |
| 222 | "iv": b"00000000000000000000000000000000", |
| 223 | "plaintext": b"58c8e00b2631686d54eab84b91f0aca1", |
| 224 | "ciphertext": b"08a4e2efec8a8e3312ca7460b9040bbf", |
| 225 | }, |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 226 | ] |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 227 | |
| 228 | |
| 229 | def test_load_cryptrec_vectors(): |
| 230 | vector_data = textwrap.dedent(""" |
| 231 | # Vectors taken from http://info.isl.ntt.co.jp/crypt/eng/camellia/ |
| 232 | # Download is t_camelia.txt |
| 233 | |
| 234 | # Camellia with 128-bit key |
| 235 | |
| 236 | K No.001 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 237 | |
| 238 | P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 239 | C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C |
| 240 | |
| 241 | P No.002 : 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 242 | C No.002 : 48 CD 64 19 80 96 72 D2 34 92 60 D8 9A 08 D3 D3 |
| 243 | |
| 244 | K No.002 : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 245 | |
| 246 | P No.001 : 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 247 | C No.001 : 07 92 3A 39 EB 0A 81 7D 1C 4D 87 BD B8 2D 1F 1C |
| 248 | """).splitlines() |
| 249 | |
| 250 | assert load_cryptrec_vectors(vector_data) == [ |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 251 | { |
| 252 | "key": b"00000000000000000000000000000000", |
| 253 | "plaintext": b"80000000000000000000000000000000", |
| 254 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 255 | }, |
| 256 | { |
| 257 | "key": b"00000000000000000000000000000000", |
| 258 | "plaintext": b"40000000000000000000000000000000", |
| 259 | "ciphertext": b"48CD6419809672D2349260D89A08D3D3", |
| 260 | }, |
| 261 | { |
| 262 | "key": b"10000000000000000000000000000000", |
| 263 | "plaintext": b"80000000000000000000000000000000", |
| 264 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 265 | }, |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 266 | ] |
| 267 | |
| 268 | |
| 269 | def test_load_cryptrec_vectors_from_file_encrypt(): |
| 270 | test_set = load_cryptrec_vectors_from_file( |
| 271 | "Camellia/NTT/camellia-128-ecb.txt" |
| 272 | ) |
| 273 | assert test_set[0] == ( |
Alex Gaynor | 1fe70b1 | 2013-10-16 11:59:17 -0700 | [diff] [blame^] | 274 | { |
| 275 | "key": b"00000000000000000000000000000000", |
| 276 | "plaintext": b"80000000000000000000000000000000", |
| 277 | "ciphertext": b"07923A39EB0A817D1C4D87BDB82D1F1C", |
| 278 | } |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 279 | ) |
| 280 | assert len(test_set) == 1280 |
| 281 | |
| 282 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 283 | def test_load_openssl_vectors(): |
Paul Kehrer | 05d7214 | 2013-09-15 14:03:15 -0500 | [diff] [blame] | 284 | vector_data = textwrap.dedent( |
| 285 | """ |
| 286 | # We don't support CFB{1,8}-CAMELLIAxxx.{En,De}crypt |
| 287 | # For all CFB128 encrypts and decrypts, the transformed sequence is |
| 288 | # CAMELLIA-bits-CFB:key:IV/ciphertext':plaintext:ciphertext:encdec |
| 289 | # CFB128-CAMELLIA128.Encrypt |
| 290 | """ |
| 291 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 292 | "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" |
| 293 | "14F7646187817EB586599146B82BD719:1\n" |
| 294 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 295 | "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" |
| 296 | "A53D28BB82DF741103EA4F921A44880B:1\n\n" |
| 297 | "# CFB128-CAMELLIA128.Decrypt\n" |
| 298 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 299 | "000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:" |
| 300 | "14F7646187817EB586599146B82BD719:0\n" |
| 301 | "CAMELLIA-128-CFB:2B7E151628AED2A6ABF7158809CF4F3C:" |
| 302 | "14F7646187817EB586599146B82BD719:AE2D8A571E03AC9C9EB76FAC45AF8E51:" |
| 303 | "A53D28BB82DF741103EA4F921A44880B:0" |
| 304 | ).splitlines() |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 305 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 306 | assert load_openssl_vectors(vector_data) == [ |
| 307 | ( |
| 308 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 309 | b"000102030405060708090A0B0C0D0E0F", |
| 310 | b"6BC1BEE22E409F96E93D7E117393172A", |
| 311 | b"14F7646187817EB586599146B82BD719", |
| 312 | ), |
| 313 | ( |
| 314 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 315 | b"14F7646187817EB586599146B82BD719", |
| 316 | b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 317 | b"A53D28BB82DF741103EA4F921A44880B", |
| 318 | ), |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 319 | ( |
| 320 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 321 | b"000102030405060708090A0B0C0D0E0F", |
| 322 | b"6BC1BEE22E409F96E93D7E117393172A", |
| 323 | b"14F7646187817EB586599146B82BD719", |
| 324 | ), |
| 325 | ( |
| 326 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 327 | b"14F7646187817EB586599146B82BD719", |
| 328 | b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 329 | b"A53D28BB82DF741103EA4F921A44880B", |
| 330 | ), |
| 331 | ] |
| 332 | |
| 333 | |
Paul Kehrer | 6b99a1b | 2013-09-24 16:50:21 -0500 | [diff] [blame] | 334 | def test_load_openssl_vectors_from_file(): |
| 335 | test_list = load_openssl_vectors_from_file("Camellia/camellia-ofb.txt") |
| 336 | assert len(test_list) == 24 |
Paul Kehrer | 1951bf6 | 2013-09-15 12:05:43 -0500 | [diff] [blame] | 337 | assert test_list[:4] == [ |
| 338 | ( |
| 339 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 340 | b"000102030405060708090A0B0C0D0E0F", |
| 341 | b"6BC1BEE22E409F96E93D7E117393172A", |
| 342 | b"14F7646187817EB586599146B82BD719", |
| 343 | ), |
| 344 | ( |
| 345 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 346 | b"50FE67CC996D32B6DA0937E99BAFEC60", |
| 347 | b"AE2D8A571E03AC9C9EB76FAC45AF8E51", |
| 348 | b"25623DB569CA51E01482649977E28D84", |
| 349 | ), |
| 350 | ( |
| 351 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 352 | b"D9A4DADA0892239F6B8B3D7680E15674", |
| 353 | b"30C81C46A35CE411E5FBC1191A0A52EF", |
| 354 | b"C776634A60729DC657D12B9FCA801E98", |
| 355 | ), |
| 356 | ( |
| 357 | b"2B7E151628AED2A6ABF7158809CF4F3C", |
| 358 | b"A78819583F0308E7A6BF36B1386ABF23", |
| 359 | b"F69F2445DF4F9B17AD2B417BE66C3710", |
| 360 | b"D776379BE0E50825E681DA1A4C980E8E", |
| 361 | ), |
| 362 | ] |