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