2 * eax-test.c: test harness for EAX, implementation
5 * This file is Free Software. It was originally written for secnet.
7 * You may redistribute it and/or modify it under the terms of the GNU
8 * General Public License as published by the Free Software
9 * Foundation; either version 2, or (at your option) any later
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * ./eax-foo-test <eax-foo-test.vectors
25 * runs the test vectors, regenerates the file on stdout
26 * grep -v CIPHER <eax-foo-test.vectors | ./eax-foo-test
27 * generates output with CIPHER lines reinserted
28 * All errors result in calls to abort().
38 #define V(vb) ((vb).v), ((vb).len)
40 static struct valbuf msg
, key
, nonce
, header
, cipher
, ourcipher
, returnplain
;
43 static void trydecrypt(_Bool expected
)
45 _Bool actual
= eax_decrypt(-1, V(nonce
), V(header
), V(ourcipher
), tau
,
47 assert(actual
== expected
);
49 returnplain
.len
= ourcipher
.len
- tau
;
50 assert(returnplain
.len
== msg
.len
);
51 assert(!memcmp(returnplain
.v
, msg
.v
, msg
.len
));
55 static void negtest(struct valbuf
*perturb
)
57 unsigned delta
= 0x04;
59 for (i
=0; i
<perturb
->len
; i
++) {
60 perturb
->v
[i
] ^= delta
;
62 perturb
->v
[i
] ^= delta
;
66 static void something(void)
72 eaxtest_blockcipher_key_setup(V(key
));
75 assert(cipher
.len
> msg
.len
);
76 tau
= cipher
.len
- msg
.len
;
77 assert(tau
<= blocksize
);
79 assert(msg
.len
+ blocksize
< sizeof(ourcipher
.v
));
82 ourcipher
.len
= msg
.len
+ tau
;
83 eax_encrypt(-1, V(nonce
), V(header
), V(msg
), tau
, ourcipher
.v
);
85 assert(ourcipher
.len
== cipher
.len
);
86 assert(!memcmp(ourcipher
.v
, cipher
.v
, cipher
.len
));
93 for (i
=0; i
<ourcipher
.len
; i
++)
94 printf("%02X", ourcipher
.v
[i
]);
97 msg
.got
=key
.got
=nonce
.got
=header
.got
=0;
100 static int getputchar(void)
108 int main(int argc
, const char *const *argv
)
117 case 'M': something(); cv
= &msg
; putchar(c
); break;
118 case 'K': cv
= &key
; putchar(c
); break;
119 case 'N': cv
= &nonce
; putchar(c
); break;
120 case 'H': cv
= &header
; putchar(c
); break;
121 case 'C': cv
= &cipher
; putchar(c
); break;
122 case '\n': putchar(c
); continue;
123 case EOF
: something(); exit(0);
124 default: assert(!"unexpected input");
136 if (c
== '\n') break;
137 if (isspace(c
)) continue;
144 assert(cv
->len
< sizeof(cv
->v
));
145 cv
->v
[cv
->len
++] = strtoul(hbuf
,&ep
,16);
149 assert(!ferror(stdin
));
151 assert(!ferror(stdout
));
152 assert(!fflush(stdout
));