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 | |
| 16 | from .utils import load_nist_vectors, load_nist_vectors_from_file |
| 17 | |
| 18 | |
| 19 | def test_load_nist_vectors_encrypt(): |
| 20 | vector_data = textwrap.dedent(""" |
| 21 | # CAVS 11.1 |
| 22 | # Config info for aes_values |
| 23 | # AESVS GFSbox test data for CBC |
| 24 | # State : Encrypt and Decrypt |
| 25 | # Key Length : 128 |
| 26 | # Generated on Fri Apr 22 15:11:33 2011 |
| 27 | |
| 28 | [ENCRYPT] |
| 29 | |
| 30 | COUNT = 0 |
| 31 | KEY = 00000000000000000000000000000000 |
| 32 | IV = 00000000000000000000000000000000 |
| 33 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 34 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 35 | |
| 36 | COUNT = 1 |
| 37 | KEY = 00000000000000000000000000000000 |
| 38 | IV = 00000000000000000000000000000000 |
| 39 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 40 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 41 | |
| 42 | [DECRYPT] |
| 43 | |
| 44 | COUNT = 0 |
| 45 | KEY = 00000000000000000000000000000000 |
| 46 | IV = 00000000000000000000000000000000 |
| 47 | CIPHERTEXT = 0336763e966d92595a567cc9ce537f5e |
| 48 | PLAINTEXT = f34481ec3cc627bacd5dc3fb08f273e6 |
| 49 | |
| 50 | COUNT = 1 |
| 51 | KEY = 00000000000000000000000000000000 |
| 52 | IV = 00000000000000000000000000000000 |
| 53 | CIPHERTEXT = a9a1631bf4996954ebc093957b234589 |
| 54 | PLAINTEXT = 9798c4640bad75c7c3227db910174e72 |
| 55 | """).splitlines() |
| 56 | |
| 57 | assert load_nist_vectors(vector_data, "ENCRYPT", |
| 58 | ["key", "iv", "plaintext", "ciphertext"], |
| 59 | ) == [ |
| 60 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 61 | b"00000000000000000000000000000000", |
| 62 | b"00000000000000000000000000000000", |
| 63 | b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 64 | b"0336763e966d92595a567cc9ce537f5e", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 65 | ), |
| 66 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 67 | b"00000000000000000000000000000000", |
| 68 | b"00000000000000000000000000000000", |
| 69 | b"9798c4640bad75c7c3227db910174e72", |
| 70 | b"a9a1631bf4996954ebc093957b234589", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 71 | ), |
| 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 | |
| 113 | assert load_nist_vectors(vector_data, "DECRYPT", |
| 114 | ["key", "iv", "ciphertext", "plaintext"], |
| 115 | ) == [ |
| 116 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 117 | b"00000000000000000000000000000000", |
| 118 | b"00000000000000000000000000000000", |
| 119 | b"0336763e966d92595a567cc9ce537f5e", |
| 120 | b"f34481ec3cc627bacd5dc3fb08f273e6", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 121 | ), |
| 122 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 123 | b"00000000000000000000000000000000", |
| 124 | b"00000000000000000000000000000000", |
| 125 | b"a9a1631bf4996954ebc093957b234589", |
| 126 | b"9798c4640bad75c7c3227db910174e72", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 127 | ), |
| 128 | ] |
| 129 | |
| 130 | |
| 131 | def test_load_nist_vectors_from_file_encrypt(): |
| 132 | assert load_nist_vectors_from_file( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 133 | "AES/KAT/CBCGFSbox128.rsp", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 134 | "ENCRYPT", |
| 135 | ["key", "iv", "plaintext", "ciphertext"], |
| 136 | ) == [ |
| 137 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 138 | b"00000000000000000000000000000000", |
| 139 | b"00000000000000000000000000000000", |
| 140 | b"f34481ec3cc627bacd5dc3fb08f273e6", |
| 141 | b"0336763e966d92595a567cc9ce537f5e", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 142 | ), |
| 143 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 144 | b"00000000000000000000000000000000", |
| 145 | b"00000000000000000000000000000000", |
| 146 | b"9798c4640bad75c7c3227db910174e72", |
| 147 | b"a9a1631bf4996954ebc093957b234589", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 148 | ), |
| 149 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 150 | b"00000000000000000000000000000000", |
| 151 | b"00000000000000000000000000000000", |
| 152 | b"96ab5c2ff612d9dfaae8c31f30c42168", |
| 153 | b"ff4f8391a6a40ca5b25d23bedd44a597", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 154 | ), |
| 155 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 156 | b"00000000000000000000000000000000", |
| 157 | b"00000000000000000000000000000000", |
| 158 | b"6a118a874519e64e9963798a503f1d35", |
| 159 | b"dc43be40be0e53712f7e2bf5ca707209", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 160 | ), |
| 161 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 162 | b"00000000000000000000000000000000", |
| 163 | b"00000000000000000000000000000000", |
| 164 | b"cb9fceec81286ca3e989bd979b0cb284", |
| 165 | b"92beedab1895a94faa69b632e5cc47ce", |
| 166 | ), |
| 167 | ( |
| 168 | b"00000000000000000000000000000000", |
| 169 | b"00000000000000000000000000000000", |
| 170 | b"b26aeb1874e47ca8358ff22378f09144", |
| 171 | b"459264f4798f6a78bacb89c15ed3d601", |
| 172 | ), |
| 173 | ( |
| 174 | b"00000000000000000000000000000000", |
| 175 | b"00000000000000000000000000000000", |
| 176 | b"58c8e00b2631686d54eab84b91f0aca1", |
| 177 | b"08a4e2efec8a8e3312ca7460b9040bbf", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 178 | ), |
| 179 | ] |
| 180 | |
| 181 | |
| 182 | def test_load_nist_vectors_from_file_decypt(): |
| 183 | assert load_nist_vectors_from_file( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 184 | "AES/KAT/CBCGFSbox128.rsp", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 185 | "DECRYPT", |
| 186 | ["key", "iv", "ciphertext", "plaintext"], |
| 187 | ) == [ |
| 188 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 189 | b"00000000000000000000000000000000", |
| 190 | b"00000000000000000000000000000000", |
| 191 | b"0336763e966d92595a567cc9ce537f5e", |
| 192 | b"f34481ec3cc627bacd5dc3fb08f273e6", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 193 | ), |
| 194 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 195 | b"00000000000000000000000000000000", |
| 196 | b"00000000000000000000000000000000", |
| 197 | b"a9a1631bf4996954ebc093957b234589", |
| 198 | b"9798c4640bad75c7c3227db910174e72", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 199 | ), |
| 200 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 201 | b"00000000000000000000000000000000", |
| 202 | b"00000000000000000000000000000000", |
| 203 | b"ff4f8391a6a40ca5b25d23bedd44a597", |
| 204 | b"96ab5c2ff612d9dfaae8c31f30c42168", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 205 | ), |
| 206 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 207 | b"00000000000000000000000000000000", |
| 208 | b"00000000000000000000000000000000", |
| 209 | b"dc43be40be0e53712f7e2bf5ca707209", |
| 210 | b"6a118a874519e64e9963798a503f1d35", |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 211 | ), |
| 212 | ( |
Alex Gaynor | f312a5c | 2013-08-10 15:23:38 -0400 | [diff] [blame] | 213 | b"00000000000000000000000000000000", |
| 214 | b"00000000000000000000000000000000", |
| 215 | b"92beedab1895a94faa69b632e5cc47ce", |
| 216 | b"cb9fceec81286ca3e989bd979b0cb284", |
| 217 | ), |
| 218 | ( |
| 219 | b"00000000000000000000000000000000", |
| 220 | b"00000000000000000000000000000000", |
| 221 | b"459264f4798f6a78bacb89c15ed3d601", |
| 222 | b"b26aeb1874e47ca8358ff22378f09144" |
| 223 | ), |
| 224 | ( |
| 225 | b"00000000000000000000000000000000", |
| 226 | b"00000000000000000000000000000000", |
| 227 | b"08a4e2efec8a8e3312ca7460b9040bbf", |
| 228 | b"58c8e00b2631686d54eab84b91f0aca1" |
Donald Stufft | 9e1a48b | 2013-08-09 00:32:30 -0400 | [diff] [blame] | 229 | ), |
| 230 | ] |