test-ae-roundtrip.c (3235B)
1 #include <stdio.h> 2 #include <stdint.h> 3 #include <string.h> 4 5 #include "lilliput-ae.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 nonce[NONCE_BYTES]; 15 size_t auth_len; 16 uint8_t *auth; 17 size_t message_len; 18 uint8_t *message; 19 }; 20 21 typedef struct vector vector; 22 23 24 /* Keys and nonces generated with /dev/urandom. */ 25 26 const vector VECTORS[] = { 27 { 28 .name = "short", 29 .key = { 30 0x69, 0x5a, 0x10, 0x7a, 0x34, 0xb6, 0x80, 0xc1, 31 0x54, 0x85, 0xca, 0xc0, 0xfb, 0xf6, 0x09, 0x4c, 32 0x9d, 0xd7, 0x04, 0xfa, 0x1a, 0xb7, 0xb9, 0x22, 33 0x52, 0xea, 0x36, 0xd3, 0x12, 0x51, 0x3f, 0x86 34 }, 35 .nonce = { 36 0x85, 0xc6, 0x6b, 0xf4, 0x82, 0x99, 0x3a, 0x14, 37 0x87, 0x7e, 0x45, 0x48, 0xe4, 0x51, 0x6c 38 }, 39 .auth_len = 8, 40 .auth = (uint8_t*)"deadbeef", 41 .message_len = 4, 42 .message = (uint8_t[]){ 43 0xde, 0xad, 0xbe, 0xef 44 } 45 }, 46 { 47 .name = "block-sized", 48 .key = { 49 0x9c, 0x59, 0x6a, 0xfb, 0x07, 0x67, 0xd7, 0x52, 50 0xae, 0xfb, 0xde, 0x3f, 0x9b, 0x68, 0x69, 0x60, 51 0x22, 0x48, 0x77, 0x95, 0x6f, 0xba, 0x5e, 0x17, 52 0x25, 0x2a, 0xa0, 0x7f, 0x0e, 0xd8, 0xc3, 0x16 53 }, 54 .nonce = { 55 0x2f, 0xae, 0xfb, 0xa5, 0xd0, 0xc8, 0x2c, 0xc2, 56 0xb5, 0x16, 0x2e, 0xcc, 0xf8, 0x4f, 0xc8 57 }, 58 .auth_len = 13, 59 .auth = (uint8_t*)"some metadata", 60 .message_len = 2*BLOCK_BYTES, 61 .message = (uint8_t*)"32-byte long, i.e. 2*BLOCK_BYTES" 62 }, 63 { 64 .name = "arbitrarily long", 65 .key = { 66 0xe3, 0x95, 0x7d, 0xc3, 0xc3, 0x29, 0x81, 0x0a, 67 0x06, 0x20, 0x33, 0xf9, 0x05, 0x6e, 0xb0, 0x45, 68 0x81, 0x33, 0x64, 0x7d, 0x9f, 0x31, 0x8f, 0x98, 69 0x1c, 0x97, 0x06, 0x95, 0x6a, 0xe9, 0x93, 0x54 70 }, 71 .nonce = { 72 0x34, 0x87, 0x0f, 0xf9, 0x75, 0x49, 0x4c, 0x02, 73 0x6c, 0xac, 0x50, 0xfc, 0xc8, 0xe9, 0xed 74 }, 75 .auth_len = 30, 76 .auth = (uint8_t*)"a bunch of associated metadata", 77 .message_len = 59, 78 .message = (uint8_t*)"here comes the placeholder: foobar ipsum dolor sit baz quux" 79 } 80 }; 81 82 83 int main() 84 { 85 int diff = 0; 86 87 for (const vector *v=VECTORS; v<ARRAY_END(VECTORS); v++) 88 { 89 uint8_t ciphertext[v->message_len]; 90 uint8_t tag[TAG_BYTES]; 91 92 lilliput_ae_encrypt( 93 v->message_len, v->message, 94 v->auth_len, v->auth, 95 v->key, v->nonce, 96 ciphertext, 97 tag 98 ); 99 100 uint8_t deciphered[v->message_len]; 101 bool valid = lilliput_ae_decrypt( 102 v->message_len, ciphertext, 103 v->auth_len, v->auth, 104 v->key, v->nonce, tag, 105 deciphered 106 ); 107 108 if (!valid) 109 { 110 REPORT_INVALID(v->name); 111 diff++; 112 continue; 113 } 114 115 if (memcmp(deciphered, v->message, v->message_len) != 0) 116 { 117 REPORT_DIFFERENCE(v->name, "deciphered plaintext"); 118 diff++; 119 continue; 120 } 121 } 122 123 return diff; 124 }