| 1 | /* |
| 2 | * This file is part of ipif, part of userv-utils |
| 3 | * |
| 4 | * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk> |
| 5 | * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk> |
| 6 | * Copyright 1999,2003 |
| 7 | * Chancellor Masters and Scholars of the University of Cambridge |
| 8 | * Copyright 2010 Tony Finch <fanf@dotat.at> |
| 9 | * |
| 10 | * This is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with userv-utils; if not, see http://www.gnu.org/licenses/. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <assert.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include "blowfish.h" |
| 29 | |
| 30 | static void checkrw(int r, int exp_r, const char *op, FILE *f) { |
| 31 | if (ferror(f)) { perror(op); exit(3); } |
| 32 | if (feof(f)) { fprintf(stderr,"unexpected eof on %s\n",op); exit(2); } |
| 33 | assert(r==exp_r); |
| 34 | } |
| 35 | |
| 36 | int main(void) { |
| 37 | struct blowfish_cbc_state cbc; |
| 38 | unsigned char keybuf[BLOWFISH_MAXKEYBYTES], ivbuf[BLOWFISH_BLOCKBYTES]; |
| 39 | unsigned char ibuf[BLOWFISH_BLOCKBYTES], obuf[BLOWFISH_BLOCKBYTES]; |
| 40 | int r; |
| 41 | |
| 42 | r= fread(keybuf,1,sizeof(keybuf),stdin); checkrw(r,sizeof(keybuf),"input",stdin); |
| 43 | blowfish_loadkey(&cbc.ek,keybuf,sizeof(keybuf)); |
| 44 | |
| 45 | r= fread(ibuf,1,sizeof(ivbuf),stdin); checkrw(r,sizeof(ivbuf),"input",stdin); |
| 46 | blowfish_cbc_setiv(&cbc,ivbuf); |
| 47 | |
| 48 | for (;;) { |
| 49 | r= fread(ibuf,1,sizeof(ibuf),stdin); if (r<sizeof(ibuf) && r>=0) break; |
| 50 | checkrw(r,sizeof(ibuf),"input",stdin); |
| 51 | blowfish_cbc_encrypt(&cbc,ibuf,obuf); |
| 52 | r= fwrite(obuf,1,sizeof(obuf),stdout); checkrw(r,sizeof(obuf),"output",stdout); |
| 53 | } |
| 54 | memset(ibuf+r,sizeof(ibuf)-r,sizeof(ibuf)-r); |
| 55 | blowfish_cbc_encrypt(&cbc,ibuf,obuf); |
| 56 | r= fwrite(obuf,1,sizeof(obuf),stdout); checkrw(r,sizeof(obuf),"output",stdout); |
| 57 | return 0; |
| 58 | }; |