www-cgi: whitelist some more HTTP headers
[userv-utils] / ipif / blowfishtest.c
1 /*
2 * test program for blowfish; very hard to use (sorry!)
3 */
4 /*
5 * Copyright (C) 1997,2000,2003 Ian Jackson
6 * This file is part of ipif, part of userv-utils
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with userv-utils; if not, write to the Free Software
20 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <assert.h>
27
28 #include "blowfish.h"
29 #include "hex.h"
30
31 int main(void) {
32 char buf[200], keybuf[200], plainbuf[200], cipherbuf[200], comparebuf[200], ivbuf[200];
33 char keytxt[sizeof(buf)+1], plaintxt[sizeof(buf)+1], ciphertxt[sizeof(buf)+1];
34 uint8_t key[BLOWFISH_MAXKEYBYTES*2], plain[100], cipher[100], compare[100];
35 uint8_t iv[BLOWFISH_BLOCKBYTES];
36 int keysz, plainsz, ciphersz, cskey, csiv, csplain, i;
37 struct blowfish_expandedkey ek;
38 struct blowfish_cbc_state cs;
39
40 setvbuf(stdout,0,_IOLBF,BUFSIZ);
41 buf[sizeof(buf)-2]=0;
42 keytxt[sizeof(buf)]= 0;
43 plaintxt[sizeof(buf)]= 0;
44 ciphertxt[sizeof(buf)]= 0;
45 cskey= csiv= csplain= 0;
46 while (fgets(buf,sizeof(buf),stdin)) {
47 if (buf[sizeof(buf)-2]) { fprintf(stderr,"line too long %s...\n",buf); exit(1); }
48 if (sscanf(buf,"ecb %s %s %s\n",keytxt,plaintxt,ciphertxt) ==3) {
49 unhex("ecb key",keytxt,key,&keysz,1,sizeof(key));
50 unhex("ecb plain",plaintxt,plain,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
51 unhex("ecb cipher",ciphertxt,cipher,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
52 printf("ecb %s %s %s\n",
53 tohex(key,keysz,keybuf),
54 tohex(plain,BLOWFISH_BLOCKBYTES,plainbuf),
55 tohex(cipher,BLOWFISH_BLOCKBYTES,cipherbuf));
56 blowfish_loadkey(&ek,key,keysz);
57 blowfish_encrypt(&ek,plain,compare);
58 if (memcmp(cipher,compare,BLOWFISH_BLOCKBYTES)) {
59 fprintf(stderr,"encryption mismatch - got %s\n",
60 tohex(compare,BLOWFISH_BLOCKBYTES,comparebuf));
61 exit(1);
62 }
63 blowfish_decrypt(&ek,cipher,compare);
64 if (memcmp(plain,compare,BLOWFISH_BLOCKBYTES)) {
65 fprintf(stderr,"decryption mismatch - got %s\n",
66 tohex(compare,BLOWFISH_BLOCKBYTES,comparebuf));
67 exit(1);
68 }
69 } else if (sscanf(buf,"key %s\n",keytxt)) {
70 unhex("key",keytxt,key,&keysz,1,sizeof(key));
71 blowfish_loadkey(&cs.ek,key,keysz);
72 cskey= 1;
73 } else if (sscanf(buf,"iv %s\n",keytxt)) {
74 unhex("iv",keytxt,iv,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
75 csiv= 1;
76 } else if (sscanf(buf,"plain %s\n",plaintxt)) {
77 unhex("plain",plaintxt,plain,&plainsz,0,sizeof(plain));
78 csplain= 1;
79 } else if (sscanf(buf,"cbc %s\n",ciphertxt)) {
80 if (!cskey || !csiv || !csplain) {
81 fprintf(stderr,"failed to specify%s%s%s\n",
82 cskey ? "" : " key",
83 csiv ? "" : " iv",
84 csplain ? "" : " plain");
85 exit(1);
86 }
87 unhex("cbc cipher",ciphertxt,cipher,&ciphersz,0,sizeof(cipher));
88 printf("key %s\niv %s\nplain %s\ncipher %s\n",
89 tohex(key,keysz,keybuf),
90 tohex(iv,BLOWFISH_BLOCKBYTES,ivbuf),
91 tohex(plain,plainsz,plainbuf),
92 tohex(cipher,ciphersz,cipherbuf));
93 if (plainsz % BLOWFISH_BLOCKBYTES ||
94 ciphersz % BLOWFISH_BLOCKBYTES ||
95 plainsz != ciphersz) {
96 fprintf(stderr,"size mismatch plain=%d cipher=%d block=%d\n",
97 plainsz,ciphersz,BLOWFISH_BLOCKBYTES);
98 exit(1);
99 }
100 blowfish_cbc_setiv(&cs,iv);
101 for (i=0; i<plainsz; i+=BLOWFISH_BLOCKBYTES)
102 blowfish_cbc_decrypt(&cs,cipher+i,compare+i);
103 if (memcmp(plain,compare,BLOWFISH_BLOCKBYTES)) {
104 fprintf(stderr,"decryption mismatch - got %s\n",
105 tohex(compare,plainsz,comparebuf));
106 exit(1);
107 }
108 blowfish_cbc_setiv(&cs,iv);
109 for (i=0; i<plainsz; i+=BLOWFISH_BLOCKBYTES)
110 blowfish_cbc_encrypt(&cs,plain+i,compare+i);
111 if (memcmp(cipher,compare,BLOWFISH_BLOCKBYTES)) {
112 fprintf(stderr,"encryption mismatch - got %s\n",
113 tohex(compare,plainsz,comparebuf));
114 exit(1);
115 }
116 } else if (buf[0]=='#' || buf[0]=='\n') {
117 } else {
118 fprintf(stderr,"huh ? %s",buf);
119 }
120 }
121 if (ferror(stdin)) { perror("stdin"); exit(1); }
122 return 0;
123 }