2 * eax-test.c: test harness for EAX, implementation
5 * This file is Free Software. It was originally written for secnet.
7 * Copyright 2013 Ian Jackson
8 * Copyright 2013 Mark Wooding
10 * You may redistribute secnet as a whole and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3, or (at your option) any
15 * You may redistribute this file and/or modify it under the terms of
16 * the GNU General Public License as published by the Free Software
17 * Foundation; either version 2, or (at your option) any later
20 * This software is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this software; if not, see
27 * https://www.gnu.org/licenses/gpl.html.
32 * ./eax-foo-test <eax-foo-test.vectors
33 * runs the test vectors, regenerates the file on stdout
34 * grep -v CIPHER <eax-foo-test.vectors | ./eax-foo-test
35 * generates output with CIPHER lines reinserted
36 * All errors result in calls to abort().
46 #define V(vb) ((vb).v), ((vb).len)
48 static struct valbuf msg, key, nonce, header, cipher, ourcipher, returnplain;
51 static void trydecrypt(_Bool expected)
53 _Bool actual = eax_decrypt(-1, V(nonce), V(header), V(ourcipher), tau,
55 assert(actual == expected);
57 returnplain.len = ourcipher.len - tau;
58 assert(returnplain.len == msg.len);
59 assert(!memcmp(returnplain.v, msg.v, msg.len));
63 static void negtest(struct valbuf *perturb)
65 unsigned delta = 0x04;
67 for (i=0; i<perturb->len; i++) {
68 perturb->v[i] ^= delta;
70 perturb->v[i] ^= delta;
74 static void something(void)
80 eaxtest_blockcipher_key_setup(V(key));
83 assert(cipher.len > msg.len);
84 tau = cipher.len - msg.len;
85 assert(tau <= blocksize);
87 assert(msg.len + blocksize < sizeof(ourcipher.v));
90 ourcipher.len = msg.len + tau;
91 eax_encrypt(-1, V(nonce), V(header), V(msg), tau, ourcipher.v);
93 assert(ourcipher.len == cipher.len);
94 assert(!memcmp(ourcipher.v, cipher.v, cipher.len));
101 for (i=0; i<ourcipher.len; i++)
102 printf("%02X", ourcipher.v[i]);
105 msg.got=key.got=nonce.got=header.got=0;
108 static int getputchar(void)
116 int main(int argc, const char *const *argv)
125 case 'M': something(); cv = &msg; putchar(c); break;
126 case 'K': cv = &key; putchar(c); break;
127 case 'N': cv = &nonce; putchar(c); break;
128 case 'H': cv = &header; putchar(c); break;
129 case 'C': cv = &cipher; putchar(c); break;
130 case '\n': putchar(c); continue;
131 case EOF: something(); exit(0);
132 default: assert(!"unexpected input");
144 if (c == '\n') break;
145 if (isspace(c)) continue;
152 assert(cv->len < sizeof(cv->v));
153 cv->v[cv->len++] = strtoul(hbuf,&ep,16);
157 assert(!ferror(stdin));
159 assert(!ferror(stdout));
160 assert(!fflush(stdout));