Allow "-" as exroutes parameter.
[userv-utils] / ipif / blowfishtest.c
CommitLineData
2dc68225 1/**/
2
3#include <stdio.h>
4#include <string.h>
5#include <stdlib.h>
6#include <assert.h>
7
8#include "blowfish.h"
9#include "hex.h"
10
11int main(void) {
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 key[BLOWFISH_MAXKEYBYTES*2], plain[100], cipher[100], compare[100];
15 uint8 iv[BLOWFISH_BLOCKBYTES];
16 int keysz, plainsz, ciphersz, cskey, csiv, csplain, i;
17 struct blowfish_expandedkey ek;
18 struct blowfish_cbc_state cs;
19
20 setvbuf(stdout,0,_IOLBF,BUFSIZ);
21 buf[sizeof(buf)-2]=0;
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));
41 exit(1);
42 }
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));
47 exit(1);
48 }
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);
52 cskey= 1;
53 } else if (sscanf(buf,"iv %s\n",keytxt)) {
54 unhex("iv",keytxt,iv,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
55 csiv= 1;
56 } else if (sscanf(buf,"plain %s\n",plaintxt)) {
57 unhex("plain",plaintxt,plain,&plainsz,0,sizeof(plain));
58 csplain= 1;
59 } else if (sscanf(buf,"cbc %s\n",ciphertxt)) {
60 if (!cskey || !csiv || !csplain) {
61 fprintf(stderr,"failed to specify%s%s%s\n",
62 cskey ? "" : " key",
63 csiv ? "" : " iv",
64 csplain ? "" : " plain");
65 exit(1);
66 }
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);
78 exit(1);
79 }
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));
86 exit(1);
87 }
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));
94 exit(1);
95 }
96 } else if (buf[0]=='#' || buf[0]=='\n') {
97 } else {
98 fprintf(stderr,"huh ? %s",buf);
99 }
100 }
101 if (ferror(stdin)) { perror("stdin"); exit(1); }
102 return 0;
103}