2 * test program for blowfish; very hard to use (sorry!)
5 * Copyright (C) 1997,2000,2003 Ian Jackson
6 * This file is part of ipif, part of userv-utils
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.
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.
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.
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
;
40 setvbuf(stdout
,0,_IOLBF
,BUFSIZ
);
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
));
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
));
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
);
73 } else if (sscanf(buf
,"iv %s\n",keytxt
)) {
74 unhex("iv",keytxt
,iv
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
76 } else if (sscanf(buf
,"plain %s\n",plaintxt
)) {
77 unhex("plain",plaintxt
,plain
,&plainsz
,0,sizeof(plain
));
79 } else if (sscanf(buf
,"cbc %s\n",ciphertxt
)) {
80 if (!cskey
|| !csiv
|| !csplain
) {
81 fprintf(stderr
,"failed to specify%s%s%s\n",
84 csplain ?
"" : " plain");
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
);
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
));
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
));
116 } else if (buf
[0]=='#' || buf
[0]=='\n') {
118 fprintf(stderr
,"huh ? %s",buf
);
121 if (ferror(stdin
)) { perror("stdin"); exit(1); }