12 char buf
[200], keybuf
[200], plainbuf
[200], cipherbuf
[200], comparebuf
[200], ivbuf
[200];
13 char keytxt
[sizeof(buf
)+1], plaintxt
[sizeof(buf
)+1], ciphertxt
[sizeof(buf
)+1];
14 uint8_t key
[BLOWFISH_MAXKEYBYTES
*2], plain
[100], cipher
[100], compare
[100];
15 uint8_t iv
[BLOWFISH_BLOCKBYTES
];
16 int keysz
, plainsz
, ciphersz
, cskey
, csiv
, csplain
, i
;
17 struct blowfish_expandedkey ek
;
18 struct blowfish_cbc_state cs
;
20 setvbuf(stdout
,0,_IOLBF
,BUFSIZ
);
22 keytxt
[sizeof(buf
)]= 0;
23 plaintxt
[sizeof(buf
)]= 0;
24 ciphertxt
[sizeof(buf
)]= 0;
25 cskey
= csiv
= csplain
= 0;
26 while (fgets(buf
,sizeof(buf
),stdin
)) {
27 if (buf
[sizeof(buf
)-2]) { fprintf(stderr
,"line too long %s...\n",buf
); exit(1); }
28 if (sscanf(buf
,"ecb %s %s %s\n",keytxt
,plaintxt
,ciphertxt
) ==3) {
29 unhex("ecb key",keytxt
,key
,&keysz
,1,sizeof(key
));
30 unhex("ecb plain",plaintxt
,plain
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
31 unhex("ecb cipher",ciphertxt
,cipher
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
32 printf("ecb %s %s %s\n",
33 tohex(key
,keysz
,keybuf
),
34 tohex(plain
,BLOWFISH_BLOCKBYTES
,plainbuf
),
35 tohex(cipher
,BLOWFISH_BLOCKBYTES
,cipherbuf
));
36 blowfish_loadkey(&ek
,key
,keysz
);
37 blowfish_encrypt(&ek
,plain
,compare
);
38 if (memcmp(cipher
,compare
,BLOWFISH_BLOCKBYTES
)) {
39 fprintf(stderr
,"encryption mismatch - got %s\n",
40 tohex(compare
,BLOWFISH_BLOCKBYTES
,comparebuf
));
43 blowfish_decrypt(&ek
,cipher
,compare
);
44 if (memcmp(plain
,compare
,BLOWFISH_BLOCKBYTES
)) {
45 fprintf(stderr
,"decryption mismatch - got %s\n",
46 tohex(compare
,BLOWFISH_BLOCKBYTES
,comparebuf
));
49 } else if (sscanf(buf
,"key %s\n",keytxt
)) {
50 unhex("key",keytxt
,key
,&keysz
,1,sizeof(key
));
51 blowfish_loadkey(&cs
.ek
,key
,keysz
);
53 } else if (sscanf(buf
,"iv %s\n",keytxt
)) {
54 unhex("iv",keytxt
,iv
,0,BLOWFISH_BLOCKBYTES
,BLOWFISH_BLOCKBYTES
);
56 } else if (sscanf(buf
,"plain %s\n",plaintxt
)) {
57 unhex("plain",plaintxt
,plain
,&plainsz
,0,sizeof(plain
));
59 } else if (sscanf(buf
,"cbc %s\n",ciphertxt
)) {
60 if (!cskey
|| !csiv
|| !csplain
) {
61 fprintf(stderr
,"failed to specify%s%s%s\n",
64 csplain ?
"" : " plain");
67 unhex("cbc cipher",ciphertxt
,cipher
,&ciphersz
,0,sizeof(cipher
));
68 printf("key %s\niv %s\nplain %s\ncipher %s\n",
69 tohex(key
,keysz
,keybuf
),
70 tohex(iv
,BLOWFISH_BLOCKBYTES
,ivbuf
),
71 tohex(plain
,plainsz
,plainbuf
),
72 tohex(cipher
,ciphersz
,cipherbuf
));
73 if (plainsz
% BLOWFISH_BLOCKBYTES
||
74 ciphersz
% BLOWFISH_BLOCKBYTES
||
75 plainsz
!= ciphersz
) {
76 fprintf(stderr
,"size mismatch plain=%d cipher=%d block=%d\n",
77 plainsz
,ciphersz
,BLOWFISH_BLOCKBYTES
);
80 blowfish_cbc_setiv(&cs
,iv
);
81 for (i
=0; i
<plainsz
; i
+=BLOWFISH_BLOCKBYTES
)
82 blowfish_cbc_decrypt(&cs
,cipher
+i
,compare
+i
);
83 if (memcmp(plain
,compare
,BLOWFISH_BLOCKBYTES
)) {
84 fprintf(stderr
,"decryption mismatch - got %s\n",
85 tohex(compare
,plainsz
,comparebuf
));
88 blowfish_cbc_setiv(&cs
,iv
);
89 for (i
=0; i
<plainsz
; i
+=BLOWFISH_BLOCKBYTES
)
90 blowfish_cbc_encrypt(&cs
,plain
+i
,compare
+i
);
91 if (memcmp(cipher
,compare
,BLOWFISH_BLOCKBYTES
)) {
92 fprintf(stderr
,"encryption mismatch - got %s\n",
93 tohex(compare
,plainsz
,comparebuf
));
96 } else if (buf
[0]=='#' || buf
[0]=='\n') {
98 fprintf(stderr
,"huh ? %s",buf
);
101 if (ferror(stdin
)) { perror("stdin"); exit(1); }