2 * test program for blowfish; very hard to use (sorry!)
5 * This file is part of ipif, part of userv-utils
7 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
8 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
10 * Chancellor Masters and Scholars of the University of Cambridge
11 * Copyright 2010 Tony Finch <fanf@dotat.at>
13 * This is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
36 char buf
[200], keybuf
[200], plainbuf
[200], cipherbuf
[200], comparebuf
[200], ivbuf
[200];
37 char keytxt
[sizeof(buf
)+1], plaintxt
[sizeof(buf
)+1], ciphertxt
[sizeof(buf
)+1];
38 uint8_t key
[BLOWFISH_MAXKEYBYTES
*2], plain
[100], cipher
[100], compare
[100];
39 uint8_t iv
[BLOWFISH_BLOCKBYTES
];
40 int keysz
, plainsz
, ciphersz
, cskey
, csiv
, csplain
, i
;
41 struct blowfish_expandedkey ek
;
42 struct blowfish_cbc_state cs
;
44 setvbuf(stdout
,0,_IOLBF
,BUFSIZ
);
46 keytxt
[sizeof(buf
)]= 0;
47 plaintxt
[sizeof(buf
)]= 0;
48 ciphertxt
[sizeof(buf
)]= 0;
49 cskey
= csiv
= csplain
= 0;
50 while (fgets(buf
,sizeof(buf
),stdin
)) {
51 if (buf
[sizeof(buf
)-2]) { fprintf(stderr
,"line too long %s...\n",buf
); exit(1); }
52 if (sscanf(buf
,"ecb %s %s %s\n",keytxt
,plaintxt
,ciphertxt
) ==3) {
53 unhex("ecb key",keytxt
,key
,&keysz
,1,sizeof(key
));
54 unhex("ecb plain",plaintxt
,plain
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
55 unhex("ecb cipher",ciphertxt
,cipher
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
56 printf("ecb %s %s %s\n",
57 tohex(key
,keysz
,keybuf
),
58 tohex(plain
,BLOWFISH_BLOCKBYTES
,plainbuf
),
59 tohex(cipher
,BLOWFISH_BLOCKBYTES
,cipherbuf
));
60 blowfish_loadkey(&ek
,key
,keysz
);
61 blowfish_encrypt(&ek
,plain
,compare
);
62 if (memcmp(cipher
,compare
,BLOWFISH_BLOCKBYTES
)) {
63 fprintf(stderr
,"encryption mismatch - got %s\n",
64 tohex(compare
,BLOWFISH_BLOCKBYTES
,comparebuf
));
67 blowfish_decrypt(&ek
,cipher
,compare
);
68 if (memcmp(plain
,compare
,BLOWFISH_BLOCKBYTES
)) {
69 fprintf(stderr
,"decryption mismatch - got %s\n",
70 tohex(compare
,BLOWFISH_BLOCKBYTES
,comparebuf
));
73 } else if (sscanf(buf
,"key %s\n",keytxt
)) {
74 unhex("key",keytxt
,key
,&keysz
,1,sizeof(key
));
75 blowfish_loadkey(&cs
.ek
,key
,keysz
);
77 } else if (sscanf(buf
,"iv %s\n",keytxt
)) {
78 unhex("iv",keytxt
,iv
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
80 } else if (sscanf(buf
,"plain %s\n",plaintxt
)) {
81 unhex("plain",plaintxt
,plain
,&plainsz
,0,sizeof(plain
));
83 } else if (sscanf(buf
,"cbc %s\n",ciphertxt
)) {
84 if (!cskey
|| !csiv
|| !csplain
) {
85 fprintf(stderr
,"failed to specify%s%s%s\n",
88 csplain ?
"" : " plain");
91 unhex("cbc cipher",ciphertxt
,cipher
,&ciphersz
,0,sizeof(cipher
));
92 printf("key %s\niv %s\nplain %s\ncipher %s\n",
93 tohex(key
,keysz
,keybuf
),
94 tohex(iv
,BLOWFISH_BLOCKBYTES
,ivbuf
),
95 tohex(plain
,plainsz
,plainbuf
),
96 tohex(cipher
,ciphersz
,cipherbuf
));
97 if (plainsz
% BLOWFISH_BLOCKBYTES
||
98 ciphersz
% BLOWFISH_BLOCKBYTES
||
99 plainsz
!= ciphersz
) {
100 fprintf(stderr
,"size mismatch plain=%d cipher=%d block=%d\n",
101 plainsz
,ciphersz
,BLOWFISH_BLOCKBYTES
);
104 blowfish_cbc_setiv(&cs
,iv
);
105 for (i
=0; i
<plainsz
; i
+=BLOWFISH_BLOCKBYTES
)
106 blowfish_cbc_decrypt(&cs
,cipher
+i
,compare
+i
);
107 if (memcmp(plain
,compare
,BLOWFISH_BLOCKBYTES
)) {
108 fprintf(stderr
,"decryption mismatch - got %s\n",
109 tohex(compare
,plainsz
,comparebuf
));
112 blowfish_cbc_setiv(&cs
,iv
);
113 for (i
=0; i
<plainsz
; i
+=BLOWFISH_BLOCKBYTES
)
114 blowfish_cbc_encrypt(&cs
,plain
+i
,compare
+i
);
115 if (memcmp(cipher
,compare
,BLOWFISH_BLOCKBYTES
)) {
116 fprintf(stderr
,"encryption mismatch - got %s\n",
117 tohex(compare
,plainsz
,comparebuf
));
120 } else if (buf
[0]=='#' || buf
[0]=='\n') {
122 fprintf(stderr
,"huh ? %s",buf
);
125 if (ferror(stdin
)) { perror("stdin"); exit(1); }