Commit | Line | Data |
---|---|---|
f0e54a99 | 1 | /* |
2 | * test program for blowfish; very hard to use (sorry!) | |
3 | */ | |
4 | /* | |
c07be359 | 5 | * This file is part of ipif, part of userv-utils |
f0e54a99 | 6 | * |
9028e234 IJ |
7 | * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk> |
8 | * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk> | |
9 | * Copyright 1999,2003 | |
10 | * Chancellor Masters and Scholars of the University of Cambridge | |
11 | * Copyright 2010 Tony Finch <fanf@dotat.at> | |
12 | * | |
f0e54a99 | 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 | |
9028e234 | 15 | * the Free Software Foundation; either version 3 of the License, or |
f0e54a99 | 16 | * (at your option) any later version. |
17 | * | |
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. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
9028e234 IJ |
24 | * along with userv-utils; if not, see http://www.gnu.org/licenses/. |
25 | */ | |
2dc68225 | 26 | |
27 | #include <stdio.h> | |
28 | #include <string.h> | |
29 | #include <stdlib.h> | |
30 | #include <assert.h> | |
31 | ||
32 | #include "blowfish.h" | |
33 | #include "hex.h" | |
34 | ||
35 | int main(void) { | |
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]; | |
ed509ebd | 38 | uint8_t key[BLOWFISH_MAXKEYBYTES*2], plain[100], cipher[100], compare[100]; |
39 | uint8_t iv[BLOWFISH_BLOCKBYTES]; | |
2dc68225 | 40 | int keysz, plainsz, ciphersz, cskey, csiv, csplain, i; |
41 | struct blowfish_expandedkey ek; | |
42 | struct blowfish_cbc_state cs; | |
43 | ||
44 | setvbuf(stdout,0,_IOLBF,BUFSIZ); | |
45 | buf[sizeof(buf)-2]=0; | |
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)); | |
65 | exit(1); | |
66 | } | |
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)); | |
71 | exit(1); | |
72 | } | |
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); | |
76 | cskey= 1; | |
77 | } else if (sscanf(buf,"iv %s\n",keytxt)) { | |
78 | unhex("iv",keytxt,iv,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES); | |
79 | csiv= 1; | |
80 | } else if (sscanf(buf,"plain %s\n",plaintxt)) { | |
81 | unhex("plain",plaintxt,plain,&plainsz,0,sizeof(plain)); | |
82 | csplain= 1; | |
83 | } else if (sscanf(buf,"cbc %s\n",ciphertxt)) { | |
84 | if (!cskey || !csiv || !csplain) { | |
85 | fprintf(stderr,"failed to specify%s%s%s\n", | |
86 | cskey ? "" : " key", | |
87 | csiv ? "" : " iv", | |
88 | csplain ? "" : " plain"); | |
89 | exit(1); | |
90 | } | |
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); | |
102 | exit(1); | |
103 | } | |
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)); | |
110 | exit(1); | |
111 | } | |
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)); | |
118 | exit(1); | |
119 | } | |
120 | } else if (buf[0]=='#' || buf[0]=='\n') { | |
121 | } else { | |
122 | fprintf(stderr,"huh ? %s",buf); | |
123 | } | |
124 | } | |
125 | if (ferror(stdin)) { perror("stdin"); exit(1); } | |
126 | return 0; | |
127 | } |