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