rt+test.ha (7519B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bytes; 5 use crypto::cipher; 6 7 @test fn rt_finish() void = { 8 const key: [16]u8 = [ 9 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 10 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, 11 ]; 12 13 const zero: [CT64_EXPKEYSZ]u8 = [0...]; 14 15 let block = aes(); 16 init(&block, key); 17 cipher::finish(&block); 18 19 assert(len(block.expkey) == len(zero)); 20 assert(bytes::equal(zero, block.expkey)); 21 }; 22 23 @test fn rt_encrypt_128() void = { 24 const key: [16]u8 = [ 25 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 26 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, 27 ]; 28 29 const plain: [16]u8 = [ 30 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 31 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 32 ]; 33 34 const cipher: [16]u8 = [ 35 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 36 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32, 37 ]; 38 39 let result: [16]u8 = [0...]; 40 41 let block = aes(); 42 init(&block, key); 43 defer cipher::finish(&block); 44 cipher::encrypt(&block, result[..], plain[..]); 45 46 assert(bytes::equal(cipher, result)); 47 }; 48 49 @test fn rt_encrypt_128_multiple_blocks() void = { 50 const key: [16]u8 = [ 51 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 52 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, 53 ]; 54 55 const plain: [_]u8 = [ 56 0xdb, 0x3a, 0x36, 0x50, 0x85, 0x60, 0x4c, 0x65, 57 0xc0, 0x22, 0x5a, 0x0d, 0x68, 0x2f, 0x19, 0xc2, 58 0x16, 0x27, 0xdd, 0xae, 0xd7, 0x28, 0x29, 0x23, 59 0xfa, 0x97, 0x28, 0xb0, 0x9a, 0x90, 0xfe, 0xb3, 60 0xde, 0x57, 0xbc, 0x15, 0x15, 0x8d, 0xb6, 0x29, 61 0x61, 0x40, 0x76, 0x51, 0x32, 0xc2, 0x3d, 0x1e, 62 0xdf, 0x97, 0x6a, 0x5e, 0x0a, 0x6b, 0xf6, 0x40, 63 0xb4, 0x08, 0x7e, 0xde, 0x17, 0xa4, 0x58, 0x98 64 ]; 65 66 const cipher: [_]u8 = [ 67 0xdd, 0xf4, 0x53, 0x55, 0x18, 0x7a, 0xf0, 0x65, 68 0x59, 0xd6, 0xdc, 0x4e, 0xc2, 0x89, 0xff, 0x7b, 69 0x54, 0x41, 0x6a, 0xda, 0xa0, 0xad, 0x85, 0xbd, 70 0x75, 0x1e, 0x59, 0x15, 0x6d, 0x30, 0xa8, 0xa8, 71 0xa8, 0x54, 0x6e, 0xab, 0x9c, 0x47, 0x84, 0xd, 72 0x6e, 0xb5, 0x5e, 0xba, 0xb0, 0xd9, 0x47, 0xfa, 73 0x53, 0x6b, 0xff, 0x31, 0x47, 0x86, 0xf8, 0x4c, 74 0x6b, 0x65, 0xaa, 0xa2, 0xd2, 0xd7, 0xc1, 0xdf 75 ]; 76 77 78 let block = aes(); 79 init(&block, key[..]); 80 defer cipher::finish(&block); 81 82 // test from 1 to 4 parallel blocks 83 for (let i = 0z; i < 4; i += 1) { 84 let result: [64]u8 = [0...]; 85 86 let sz = (i + 1) * 16; 87 cipher::encrypt(&block, result[0..sz], plain[0..sz]); 88 89 assert(bytes::equal(cipher[0..sz], result[0..sz])); 90 }; 91 }; 92 93 @test fn rt_decrypt_128_multiple_blocks() void = { 94 const key: [16]u8 = [ 95 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 96 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, 97 ]; 98 99 const plain: [_]u8 = [ 100 0xdb, 0x3a, 0x36, 0x50, 0x85, 0x60, 0x4c, 0x65, 101 0xc0, 0x22, 0x5a, 0x0d, 0x68, 0x2f, 0x19, 0xc2, 102 0x16, 0x27, 0xdd, 0xae, 0xd7, 0x28, 0x29, 0x23, 103 0xfa, 0x97, 0x28, 0xb0, 0x9a, 0x90, 0xfe, 0xb3, 104 0xde, 0x57, 0xbc, 0x15, 0x15, 0x8d, 0xb6, 0x29, 105 0x61, 0x40, 0x76, 0x51, 0x32, 0xc2, 0x3d, 0x1e, 106 0xdf, 0x97, 0x6a, 0x5e, 0x0a, 0x6b, 0xf6, 0x40, 107 0xb4, 0x08, 0x7e, 0xde, 0x17, 0xa4, 0x58, 0x98 108 ]; 109 110 const cipher: [_]u8 = [ 111 0xdd, 0xf4, 0x53, 0x55, 0x18, 0x7a, 0xf0, 0x65, 112 0x59, 0xd6, 0xdc, 0x4e, 0xc2, 0x89, 0xff, 0x7b, 113 0x54, 0x41, 0x6a, 0xda, 0xa0, 0xad, 0x85, 0xbd, 114 0x75, 0x1e, 0x59, 0x15, 0x6d, 0x30, 0xa8, 0xa8, 115 0xa8, 0x54, 0x6e, 0xab, 0x9c, 0x47, 0x84, 0xd, 116 0x6e, 0xb5, 0x5e, 0xba, 0xb0, 0xd9, 0x47, 0xfa, 117 0x53, 0x6b, 0xff, 0x31, 0x47, 0x86, 0xf8, 0x4c, 118 0x6b, 0x65, 0xaa, 0xa2, 0xd2, 0xd7, 0xc1, 0xdf 119 ]; 120 121 122 let block = aes(); 123 init(&block, key[..]); 124 defer cipher::finish(&block); 125 126 // test from 1 to 4 parallel blocks 127 for (let i = 0z; i < 4; i += 1) { 128 let result: [64]u8 = [0...]; 129 130 let sz = (i + 1) * 16; 131 cipher::decrypt(&block, result[0..sz], cipher[0..sz]); 132 133 assert(bytes::equal(plain[0..sz], result[0..sz])); 134 }; 135 }; 136 137 @test fn rt_decrypt_128() void = { 138 const key: []u8 = [ 139 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 140 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c, 141 ]; 142 143 const plain: [16]u8 = [ 144 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 145 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34, 146 ]; 147 148 const cipher: [16]u8 = [ 149 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 150 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32, 151 ]; 152 153 let result: [16]u8 = [0...]; 154 155 let block = aes(); 156 init(&block, key[..]); 157 defer cipher::finish(&block); 158 159 cipher::decrypt(&block, result[..], cipher[..]); 160 161 assert(bytes::equal(plain, result)); 162 }; 163 164 // fips-197.pdf Appendix C.1 165 @test fn rt_example_vector1() void = { 166 const key: [_]u8 = [ 167 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 168 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 169 ]; 170 171 const plain: [_]u8 = [ 172 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 173 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 174 ]; 175 176 const cipher: [_]u8 = [ 177 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 178 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a, 179 ]; 180 181 let result: [16]u8 = [0...]; 182 183 let block = aes(); 184 init(&block, key[..]); 185 defer cipher::finish(&block); 186 187 cipher::encrypt(&block, result[..], plain[..]); 188 assert(bytes::equal(cipher, result)); 189 190 cipher::decrypt(&block, result[..], cipher[..]); 191 assert(bytes::equal(plain, result)); 192 }; 193 194 @test fn rt_example_vector1_in_place() void = { 195 const key: [_]u8 = [ 196 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 197 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 198 ]; 199 200 const plain: [_]u8 = [ 201 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 202 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 203 ]; 204 205 const cipher: [_]u8 = [ 206 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 207 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a, 208 ]; 209 210 let result: [16]u8 = plain; 211 212 let block = aes(); 213 init(&block, key[..]); 214 defer cipher::finish(&block); 215 216 cipher::encrypt(&block, result[..], result[..]); 217 assert(bytes::equal(cipher, result)); 218 219 cipher::decrypt(&block, result[..], result[..]); 220 assert(bytes::equal(plain, result)); 221 }; 222 223 // fips-197.pdf Appendix C.2 224 @test fn rt_example_vector2() void = { 225 const key: [_]u8 = [ 226 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 227 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 228 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 229 ]; 230 231 const plain: [_]u8 = [ 232 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 233 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 234 ]; 235 236 const cipher: [_]u8 = [ 237 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 238 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91, 239 ]; 240 241 let result: [16]u8 = [0...]; 242 let block = aes(); 243 init(&block, key[..]); 244 defer cipher::finish(&block); 245 246 cipher::encrypt(&block, result[..], plain[..]); 247 assert(bytes::equal(cipher, result)); 248 249 cipher::decrypt(&block, result[..], cipher[..]); 250 assert(bytes::equal(plain, result)); 251 }; 252 253 // fips-197.pdf Appendix C.3 254 @test fn rt_example_vector3() void = { 255 const key: []u8 = [ 256 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 257 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 258 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 259 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 260 ]; 261 262 const plain: []u8 = [ 263 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 264 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 265 ]; 266 267 const cipher: []u8 = [ 268 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 269 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89, 270 ]; 271 272 let result: [16]u8 = [0...]; 273 let block = aes(); 274 init(&block, key[..]); 275 defer cipher::finish(&block); 276 277 cipher::encrypt(&block, result[..], plain[..]); 278 assert(bytes::equal(cipher, result)); 279 280 cipher::decrypt(&block, result[..], cipher[..]); 281 assert(bytes::equal(plain, result)); 282 };