summaryrefslogtreecommitdiff
path: root/ipif/blowfishtest.c
blob: 8882f03a950b948f692ce3cb01c5ff975768f218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * test program for blowfish; very hard to use (sorry!)
 */
/*
 * This file is part of ipif, part of userv-utils
 *
 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
 * Copyright 1999,2003
 *    Chancellor Masters and Scholars of the University of Cambridge
 * Copyright 2010 Tony Finch <fanf@dotat.at>
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "blowfish.h"
#include "hex.h"

int main(void) {
  char buf[200], keybuf[200], plainbuf[200], cipherbuf[200], comparebuf[200], ivbuf[200];
  char keytxt[sizeof(buf)+1], plaintxt[sizeof(buf)+1], ciphertxt[sizeof(buf)+1];
  uint8_t key[BLOWFISH_MAXKEYBYTES*2], plain[100], cipher[100], compare[100];
  uint8_t iv[BLOWFISH_BLOCKBYTES];
  int keysz, plainsz, ciphersz, cskey, csiv, csplain, i;
  struct blowfish_expandedkey ek;
  struct blowfish_cbc_state cs;

  setvbuf(stdout,0,_IOLBF,BUFSIZ);
  buf[sizeof(buf)-2]=0;
  keytxt[sizeof(buf)]= 0;
  plaintxt[sizeof(buf)]= 0;
  ciphertxt[sizeof(buf)]= 0;
  cskey= csiv= csplain= 0;
  while (fgets(buf,sizeof(buf),stdin)) {
    if (buf[sizeof(buf)-2]) { fprintf(stderr,"line too long %s...\n",buf); exit(1); }
    if (sscanf(buf,"ecb %s %s %s\n",keytxt,plaintxt,ciphertxt) ==3) {
      unhex("ecb key",keytxt,key,&keysz,1,sizeof(key));
      unhex("ecb plain",plaintxt,plain,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
      unhex("ecb cipher",ciphertxt,cipher,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
      printf("ecb %s %s %s\n",
	     tohex(key,keysz,keybuf),
	     tohex(plain,BLOWFISH_BLOCKBYTES,plainbuf),
	     tohex(cipher,BLOWFISH_BLOCKBYTES,cipherbuf));
      blowfish_loadkey(&ek,key,keysz);
      blowfish_encrypt(&ek,plain,compare);
      if (memcmp(cipher,compare,BLOWFISH_BLOCKBYTES)) {
	fprintf(stderr,"encryption mismatch - got %s\n",
		tohex(compare,BLOWFISH_BLOCKBYTES,comparebuf));
	exit(1);
      }
      blowfish_decrypt(&ek,cipher,compare);
      if (memcmp(plain,compare,BLOWFISH_BLOCKBYTES)) {
	fprintf(stderr,"decryption mismatch - got %s\n",
		tohex(compare,BLOWFISH_BLOCKBYTES,comparebuf));
	exit(1);
      } 
   } else if (sscanf(buf,"key %s\n",keytxt)) {
      unhex("key",keytxt,key,&keysz,1,sizeof(key));
      blowfish_loadkey(&cs.ek,key,keysz);
      cskey= 1;
    } else if (sscanf(buf,"iv %s\n",keytxt)) {
      unhex("iv",keytxt,iv,0,BLOWFISH_BLOCKBYTES,BLOWFISH_BLOCKBYTES);
      csiv= 1;
    } else if (sscanf(buf,"plain %s\n",plaintxt)) {
      unhex("plain",plaintxt,plain,&plainsz,0,sizeof(plain));
      csplain= 1;
    } else if (sscanf(buf,"cbc %s\n",ciphertxt)) {
      if (!cskey || !csiv || !csplain) {
	fprintf(stderr,"failed to specify%s%s%s\n",
		cskey ? "" : " key",
		csiv ? "" : " iv",
		csplain ? "" : " plain");
	exit(1);
      }
      unhex("cbc cipher",ciphertxt,cipher,&ciphersz,0,sizeof(cipher));
      printf("key %s\niv %s\nplain %s\ncipher %s\n",
	     tohex(key,keysz,keybuf),
	     tohex(iv,BLOWFISH_BLOCKBYTES,ivbuf),
	     tohex(plain,plainsz,plainbuf),
	     tohex(cipher,ciphersz,cipherbuf));
      if (plainsz % BLOWFISH_BLOCKBYTES ||
	  ciphersz % BLOWFISH_BLOCKBYTES ||
	  plainsz != ciphersz) {
	fprintf(stderr,"size mismatch plain=%d cipher=%d block=%d\n",
		plainsz,ciphersz,BLOWFISH_BLOCKBYTES);
	exit(1);
      }
      blowfish_cbc_setiv(&cs,iv);
      for (i=0; i<plainsz; i+=BLOWFISH_BLOCKBYTES)
	blowfish_cbc_decrypt(&cs,cipher+i,compare+i);
      if (memcmp(plain,compare,BLOWFISH_BLOCKBYTES)) {
	fprintf(stderr,"decryption mismatch - got %s\n",
		tohex(compare,plainsz,comparebuf));
	exit(1);
      }
      blowfish_cbc_setiv(&cs,iv);
      for (i=0; i<plainsz; i+=BLOWFISH_BLOCKBYTES)
	blowfish_cbc_encrypt(&cs,plain+i,compare+i);
      if (memcmp(cipher,compare,BLOWFISH_BLOCKBYTES)) {
	fprintf(stderr,"encryption mismatch - got %s\n",
		tohex(compare,plainsz,comparebuf));
	exit(1);
      }
    } else if (buf[0]=='#' || buf[0]=='\n') {
    } else {
      fprintf(stderr,"huh ? %s",buf);
    }
  }
  if (ferror(stdin)) { perror("stdin"); exit(1); }
  return 0;
}