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