traces-ae.c (4563B)
1 #include <stdio.h> 2 #include <stdint.h> 3 4 #include "lilliput-ae.h" 5 6 #include "debug.h" 7 #include "test-helpers.h" 8 9 10 FILE *DUMP; 11 12 13 struct vector 14 { 15 char *name; 16 uint8_t key[KEY_BYTES]; 17 uint8_t nonce[NONCE_BYTES]; 18 size_t auth_len; 19 uint8_t *auth; 20 size_t message_len; 21 uint8_t *message; 22 }; 23 24 typedef struct vector vector; 25 26 27 const vector VECTORS[] = { 28 { 29 .name = "order", 30 .key = { 31 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 32 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f 33 }, 34 .nonce = { 35 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 36 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e 37 }, 38 .auth_len = 64, 39 .auth = (uint8_t[]) { 40 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 41 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 42 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 43 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 44 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 45 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 46 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 47 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f 48 }, 49 .message_len = 64, 50 .message = (uint8_t[]) { 51 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 52 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 53 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 54 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 55 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 56 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 57 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 58 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f 59 } 60 }, 61 { 62 .name = "order-padded", 63 .key = { 64 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 65 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f 66 }, 67 .nonce = { 68 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 69 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e 70 }, 71 .auth_len = 66, 72 .auth = (uint8_t[]) { 73 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 74 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 75 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 76 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 77 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 78 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 79 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 80 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 81 0x40, 0x41 82 }, 83 .message_len = 66, 84 .message = (uint8_t[]) { 85 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 86 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 87 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 88 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 89 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 90 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 91 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 92 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 93 0x40, 0x41 94 } 95 } 96 }; 97 98 99 int main(int argc, char **argv) 100 { 101 if (argc < 3) 102 { 103 fprintf(stderr, "usage: %s OUTPUT-FOLDER PREFIX\n", argv[0]); 104 return 1; 105 } 106 107 for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) 108 { 109 debug_open_dump(argv[1], argv[2], v->name); 110 debug_dump_buffer("message", v->message_len, v->message, 0); 111 debug_dump_buffer("associated data", v->auth_len, v->auth, 0); 112 debug_dump_buffer("key", KEY_BYTES, v->key, 0); 113 debug_dump_buffer("nonce", NONCE_BYTES, v->nonce, 0); 114 115 uint8_t ciphertext[v->message_len]; 116 uint8_t tag[TAG_BYTES]; 117 118 lilliput_ae_encrypt( 119 v->message_len, v->message, 120 v->auth_len, v->auth, 121 v->key, v->nonce, 122 ciphertext, 123 tag 124 ); 125 126 debug_dump_buffer("ciphertext", v->message_len, ciphertext, 0); 127 debug_dump_buffer("tag", TAG_BYTES, tag, 0); 128 129 fprintf(DUMP, "DECRYPTING\n"); 130 131 uint8_t cleartext[v->message_len]; 132 lilliput_ae_decrypt( 133 sizeof(ciphertext), ciphertext, 134 v->auth_len, v->auth, 135 v->key, v->nonce, 136 tag, 137 cleartext 138 ); 139 140 debug_dump_buffer("cleartext", sizeof(cleartext), cleartext, 0); 141 142 fclose(DUMP); 143 } 144 }