assuan/
[gnupg.git] / tools / bftest.c
blob6b1e09117aea658c3a22736e0c3313f965ce4d4a
1 /* bftest.c - Blowfish test program
2 * Copyright (C) 1998 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_DOSISH_SYSTEM
26 #include <io.h>
27 #include <fcntl.h>
28 #endif
30 #include <gcrypt.h>
31 #include "util.h"
32 #include "i18n.h"
34 static void
35 my_usage(void)
37 fprintf(stderr, "usage: bftest [-e][-d] algo mode key\n");
38 exit(1);
42 static void
43 i18n_init(void)
45 #ifdef ENABLE_NLS
46 #ifdef HAVE_LC_MESSAGES
47 setlocale( LC_MESSAGES, "" );
48 #else
49 setlocale( LC_ALL, "" );
50 #endif
51 bindtextdomain( PACKAGE, GNUPG_LOCALEDIR );
52 textdomain( PACKAGE );
53 #endif
56 int
57 main(int argc, char **argv)
59 int encode=0;
60 GCRY_CIPHER_HD hd;
61 char buf[4096];
62 int rc, n, size=4096;
63 int algo, mode;
64 const char *s;
66 #ifdef HAVE_DOSISH_SYSTEM
67 setmode( fileno(stdin), O_BINARY );
68 setmode( fileno(stdout), O_BINARY );
69 #endif
71 i18n_init();
72 if( argc > 1 && !strcmp(argv[1], "-e") ) {
73 encode++;
74 argc--; argv++;
76 else if( argc > 1 && !strcmp(argv[1], "-E") ) {
77 encode++;
78 argc--; argv++;
79 size = 10;
81 else if( argc > 1 && !strcmp(argv[1], "-d") ) {
82 argc--; argv++;
84 else if( argc > 1 && !strcmp(argv[1], "-D") ) {
85 argc--; argv++;
86 size = 10;
88 if( argc != 4 )
89 my_usage();
90 argc--; argv++;
91 algo = gcry_cipher_map_name( *argv );
92 argc--; argv++;
93 s = *argv; argc--; argv++;
94 if ( !strcasecmp( s, "cfb" ) )
95 mode = GCRY_CIPHER_MODE_CFB;
96 else if ( !strcasecmp( s, "cbc" ) )
97 mode = GCRY_CIPHER_MODE_CBC;
98 else if ( !strcasecmp( s, "ebc" ) )
99 mode = GCRY_CIPHER_MODE_ECB;
100 else if ( !strcasecmp( s, "none" ) )
101 mode = GCRY_CIPHER_MODE_NONE;
102 else if ( !strcasecmp( s, "stream" ) )
103 mode = GCRY_CIPHER_MODE_STREAM;
104 else {
105 fprintf( stderr,
106 "wrong mode; use one of: none, ecb, cbc, cfb, stream\n");
107 return 1;
110 hd = gcry_cipher_open( algo, mode, 0 );
111 if (!hd )
112 log_fatal("cipher open failed: %s\n", gcry_strerror(-1) );
113 rc = gcry_cipher_setkey( hd, *argv, strlen(*argv) );
114 if ( rc )
115 log_fatal("setkey failed: %s\n", gcry_strerror(rc) );
116 gcry_cipher_setiv( hd, NULL, 0 );
117 while( (n = fread( buf, 1, size, stdin )) > 0 ) {
118 if( encode )
119 gcry_cipher_encrypt( hd, buf, n, buf, n );
120 else
121 gcry_cipher_decrypt( hd, buf, n, buf, n );
122 if( fwrite( buf, 1, n, stdout) != n )
123 log_fatal("write error\n");
125 gcry_cipher_close(hd);
126 return 0;