| 1 | /**/ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <assert.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include "blowfish.h" |
| 8 | |
| 9 | static void checkrw(int r, int exp_r, const char *op, FILE *f) { |
| 10 | if (ferror(f)) { perror(op); exit(3); } |
| 11 | if (feof(f)) { fprintf(stderr,"unexpected eof on %s\n",op); exit(2); } |
| 12 | assert(r==exp_r); |
| 13 | } |
| 14 | |
| 15 | int main(void) { |
| 16 | struct blowfish_cbc_state cbc; |
| 17 | unsigned char keybuf[BLOWFISH_MAXKEYBYTES], ivbuf[BLOWFISH_BLOCKBYTES]; |
| 18 | unsigned char ibuf[BLOWFISH_BLOCKBYTES], obuf[BLOWFISH_BLOCKBYTES]; |
| 19 | int r; |
| 20 | |
| 21 | r= fread(keybuf,1,sizeof(keybuf),stdin); checkrw(r,sizeof(keybuf),"input",stdin); |
| 22 | blowfish_loadkey(&cbc.ek,keybuf,sizeof(keybuf)); |
| 23 | |
| 24 | r= fread(ibuf,1,sizeof(ivbuf),stdin); checkrw(r,sizeof(ivbuf),"input",stdin); |
| 25 | blowfish_cbc_setiv(&cbc,ivbuf); |
| 26 | |
| 27 | for (;;) { |
| 28 | r= fread(ibuf,1,sizeof(ibuf),stdin); if (r<sizeof(ibuf) && r>=0) break; |
| 29 | checkrw(r,sizeof(ibuf),"input",stdin); |
| 30 | blowfish_cbc_encrypt(&cbc,ibuf,obuf); |
| 31 | r= fwrite(obuf,1,sizeof(obuf),stdout); checkrw(r,sizeof(obuf),"output",stdout); |
| 32 | } |
| 33 | memset(ibuf+r,sizeof(ibuf)-r,sizeof(ibuf)-r); |
| 34 | blowfish_cbc_encrypt(&cbc,ibuf,obuf); |
| 35 | r= fwrite(obuf,1,sizeof(obuf),stdout); checkrw(r,sizeof(obuf),"output",stdout); |
| 36 | return 0; |
| 37 | }; |