test-tbc-encrypt.c (2214B)
1 #include <stdint.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "cipher.h" 6 7 #include "test-helpers.h" 8 9 10 struct vector 11 { 12 char *name; 13 uint8_t key[KEY_BYTES]; 14 uint8_t tweak[TWEAK_BYTES]; 15 uint8_t message[BLOCK_BYTES]; 16 uint8_t ciphertext[BLOCK_BYTES]; 17 }; 18 19 typedef struct vector vector; 20 21 22 /* [0]: LSB */ 23 const vector VECTORS[] = { 24 { 25 .name = "order", 26 .tweak = { 27 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 28 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 29 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 30 }, 31 .key = { 32 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 33 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f 34 }, 35 .message = { 36 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 37 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 38 }, 39 .ciphertext = { 40 0x03, 0xb0, 0x31, 0x5e, 0xd8, 0x98, 0x43, 0x7e, 41 0xc5, 0x06, 0x4a, 0x83, 0x64, 0x11, 0xf8, 0x02 42 } 43 }, 44 { 45 .name = "random", 46 .tweak = { 47 0xa8, 0x43, 0xf3, 0x10, 0x81, 0x11, 0x1c, 0x84, 48 0xdf, 0xf8, 0x2e, 0xfa, 0x90, 0x90, 0x26, 0x21, 49 0x7d, 0x8d, 0x43, 0x12, 0x2a, 0xb3, 0xd2, 0x4d 50 }, 51 .key = { 52 0xc1, 0x96, 0xc6, 0x0a, 0x02, 0x73, 0x91, 0x68, 53 0x7f, 0xf4, 0x23, 0x4d, 0x3d, 0xd5, 0xf9, 0x9b 54 }, 55 .message = { 56 0xbc, 0xd7, 0xf0, 0x29, 0x84, 0xb6, 0xc8, 0xf9, 57 0x9c, 0x9d, 0x1d, 0xbd, 0x0d, 0x30, 0x94, 0x0b 58 }, 59 .ciphertext = { 60 0x97, 0xff, 0x85, 0x27, 0xb6, 0x09, 0x1f, 0x51, 61 0xf3, 0xcb, 0xfd, 0xd0, 0xf2, 0x72, 0xa5, 0x90 62 } 63 } 64 }; 65 66 67 int main() 68 { 69 int diff = 0; 70 71 for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) 72 { 73 uint8_t ciphertext[BLOCK_BYTES]; 74 lilliput_tbc_encrypt(v->key, v->tweak, v->message, ciphertext); 75 76 if (memcmp(ciphertext, v->ciphertext, sizeof(ciphertext)) != 0) 77 { 78 REPORT_DIFFERENCE(v->name, "ciphertext"); 79 dump_c_initializer(BLOCK_BYTES, ciphertext); 80 diff++; 81 } 82 } 83 84 return diff; 85 }