f62172f02ee61c977b5bba91a699430eb86d339c
[userv-utils] / ipif / blowfishspeed.c
1 /*
2 * Copyright (C) 1997,2000,2003 Ian Jackson
3 * This file is part of ipif, part of userv-utils
4 *
5 * This is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with userv-utils; if not, write to the Free Software
17 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include <stdio.h>
21 #include <assert.h>
22 #include <string.h>
23
24 #include "blowfish.h"
25
26 static void checkrw(int r, int exp_r, const char *op, FILE *f) {
27 if (ferror(f)) { perror(op); exit(3); }
28 if (feof(f)) { fprintf(stderr,"unexpected eof on %s\n",op); exit(2); }
29 assert(r==exp_r);
30 }
31
32 int main(void) {
33 struct blowfish_cbc_state cbc;
34 unsigned char keybuf[BLOWFISH_MAXKEYBYTES], ivbuf[BLOWFISH_BLOCKBYTES];
35 unsigned char ibuf[BLOWFISH_BLOCKBYTES], obuf[BLOWFISH_BLOCKBYTES];
36 int r;
37
38 r= fread(keybuf,1,sizeof(keybuf),stdin); checkrw(r,sizeof(keybuf),"input",stdin);
39 blowfish_loadkey(&cbc.ek,keybuf,sizeof(keybuf));
40
41 r= fread(ibuf,1,sizeof(ivbuf),stdin); checkrw(r,sizeof(ivbuf),"input",stdin);
42 blowfish_cbc_setiv(&cbc,ivbuf);
43
44 for (;;) {
45 r= fread(ibuf,1,sizeof(ibuf),stdin); if (r<sizeof(ibuf) && r>=0) break;
46 checkrw(r,sizeof(ibuf),"input",stdin);
47 blowfish_cbc_encrypt(&cbc,ibuf,obuf);
48 r= fwrite(obuf,1,sizeof(obuf),stdout); checkrw(r,sizeof(obuf),"output",stdout);
49 }
50 memset(ibuf+r,sizeof(ibuf)-r,sizeof(ibuf)-r);
51 blowfish_cbc_encrypt(&cbc,ibuf,obuf);
52 r= fwrite(obuf,1,sizeof(obuf),stdout); checkrw(r,sizeof(obuf),"output",stdout);
53 return 0;
54 };