Index: Makefile.in =================================================================== --- Makefile.in (revision 2115) +++ Makefile.in (revision 2116) @@ -77,7 +77,7 @@ LIBS= -COMMON_OBJ=bin2hex.o $(ALGORITHM).o dstring.o +COMMON_OBJ=aescmdline.o bin2hex.o $(ALGORITHM).o dstring.o CRYPT_OBJ=aescrypt.o dorandom.o $(COMMON_OBJ) @@ -122,12 +122,15 @@ # I'm keeping track of dependencies from the start, sigh. -aescrypt.o: aescrypt.c bin2hex.o dorandom.o $(ALGORITHM).o +aescrypt.o: aescrypt.c aescmdline.o bin2hex.o dorandom.o $(ALGORITHM).o $(CC) -c $(CFLAGS) aescrypt.c -aesget.o: aesget.c bin2hex.o $(ALGORITHM).o +aesget.o: aesget.c aescmdline.o bin2hex.o $(ALGORITHM).o $(CC) -c $(CFLAGS) aesget.c +aescmdline.o: aescmdline.c aes.h aescmdline.h bin2hex.h dstring.h + $(CC) -c $(CFLAGS) aescmdline.c + dorandom.o: dorandom.c dorandom.h $(CC) -c $(CFLAGS) dorandom.c Index: aescmdline.c =================================================================== --- aescmdline.c (revision 0) +++ aescmdline.c (revision 2116) @@ -0,0 +1,118 @@ +/* Copyright 2000 Enhanced Software Technologies Inc. + All Rights Reserved + + Released for public use under a BSD-style license. See file LICENSE + for details. + + common routines for the aescrypt and aesget programs +*/ + +#include "config.h" + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> + +#include "aes.h" +#include "aescmdline.h" +#include "bin2hex.h" +#include "dstring.h" + +char *progname; + +keyInstance key; +cipherInstance cipher; + +void usage(void) { + fprintf(stderr,"%s: Usage: %s -k {keyfile} [-s 128|192|256]\n",progname,progname); + exit(1); +} + +/* write data, aborting if we have a write error. */ + +void ewrite(int outfile,void *buf, size_t count) { + int result; + + result=write(outfile,buf,count); + if (result!=(int) count) { + fprintf(stderr,"%s:error:could not write data.\n",progname); + perror(progname); + usage(); + } +} + +/* Routine to set the key once we've read it. */ +void aes_set_key(char *inkey, int keysize) { + int keylen,rqskeysize,rtncode; + char *hexkey; + rqskeysize = keysize / 4; + + hexkey=d_trim(inkey); + + keylen=strlen(hexkey); + + /* if it's a long key, chop it! */ + if ( keylen > rqskeysize ) { + keylen = rqskeysize; /* chop! */ + hexkey[rqskeysize]=0; + } + + if ( keylen==rqskeysize) { + keysize=keylen>>1; + + if ((rtncode = makeKey(&key,DIR_ENCRYPT,keylen*4,hexkey)) !=TRUE) { + fprintf(stderr,"%s: reported from makeKey: Key format error:%i\n",progname,rtncode); + usage(); + } + + /* I suppose I should just suck it up. */ + return; + } + + fprintf(stderr,"%s:Key size error:%d[%s]\n",progname,keylen,hexkey); + usage(); +} + +/* Routine to read a 128-bit key from stdin, as a 32-byte hex + * null-terminated string: + */ +void read_key_from_stdin(int infile, int keysize) { + unsigned char keybuf[65]; + int result, hexkeysize; + + hexkeysize = keysize / 4; + result=read(infile,keybuf, hexkeysize+1); + if (result<hexkeysize+1) { + fprintf(stderr,"%s: Error: Key not found on stdin.\n",progname); + usage(); + } + + keybuf[hexkeysize]=0; /* make sure it is null terminated */ + aes_set_key(keybuf, keysize); + return; +} + +/* Routine to read a key from keyfile. */ +void read_key(char *filename, int keysize) { + char inbuf[1024]; + FILE *infile; + char *result; + + infile=fopen(filename,"r"); + if (!infile) { + fprintf(stderr,"%s:Error: Key file '%s' not found.\n",progname,filename); + usage(); + } + + while (result=fgets(inbuf,1023,infile), result != NULL) { + if ((strncmp(result,"kk=",3)==0) || strncmp(result,"kk:",3)==0) { + aes_set_key(result+3, keysize); + return; + } + } + + fprintf(stderr,"%s: Error: Key not found in file '%s'.\n",progname,filename); + usage(); +} Index: aesget.c =================================================================== --- aesget.c (revision 2115) +++ aesget.c (revision 2116) @@ -17,42 +17,15 @@ #include <stdlib.h> #include "aes.h" +#include "aescmdline.h" #include "bin2hex.h" #include "dstring.h" -static char *progname; - - -keyInstance key; -cipherInstance cipher; - - - static int cfb128_idx=-1; static char cfb_blk[16]; static char cfb_crypt[16]; - -static void usage(void) { - fprintf(stderr,"%s:Usage: %s -k {keyfile}\n",progname,progname); - exit(1); -} - - -/* write data, aborting if we have a write error. */ - -static void ewrite(int outfile,void *buf, size_t count) { - int result; - - result=write(outfile,buf,count); - if (result!=(int) count) { - fprintf(stderr,"%s:error:could not write data.\n",progname); - perror(progname); - usage(); - } -} - static void decryptblock(char *src, int srclen) { int i,ch; @@ -111,81 +84,6 @@ return; } -/* Routine to set the key once we've read it. */ -static void aes_set_key(char *inkey, int keysize) { - int keylen,rqskeysize,rtncode; - char *hexkey; - - rqskeysize = keysize / 4; - hexkey=d_trim(inkey); - - keylen=strlen(hexkey); - if ( keylen > rqskeysize ) { - keylen = rqskeysize; /* chop! */ - hexkey[rqskeysize]=0; - } - - if ( keylen==rqskeysize) { - keysize=keylen>>1; - - if ((rtncode = makeKey(&key,DIR_ENCRYPT,keylen*4,hexkey)) !=TRUE) { - fprintf(stderr,"%s: reported from makeKey: Key format error:%i\n",progname,rtncode); - usage(); - } - - return; - } - - fprintf(stderr,"%s:Key size error:%d[%s]\n",progname,keylen,hexkey); - usage(); -} - - - -/* Routine to read a 128-bit key from stdin, as a 32-byte hex - * null-terminated string: - */ -static void read_key_from_stdin(int infile, int keysize) { - unsigned char keybuf[65]; - int result, hexkeysize; - - hexkeysize = keysize / 4; - result=read(infile,keybuf,hexkeysize+1); - if (result<hexkeysize+1) { - fprintf(stderr,"%s: Error: Key not find on stdin.\n",progname); - usage(); - } - keybuf[hexkeysize]=0; /* make sure it is null terminated! */ - aes_set_key(keybuf, keysize); - return; -} - - -/* Routine to read a key from keyfile. */ -static void read_key(char *filename, int keysize) { - char inbuf[1024]; - FILE *infile; - char *result; - - infile=fopen(filename,"r"); - if (!infile) { - fprintf(stderr,"%s:Error: Key file '%s' not found.\n",progname,filename); - usage(); - } - - while (result=fgets(inbuf,1023,infile), result != NULL) { - if ((strncmp(result,"kk=",3)==0) || strncmp(result,"kk:",3) == 0) { - aes_set_key(result+3, keysize); - return; - } - } - - fprintf(stderr,"%s: Error: Key not found in file '%s'.\n",progname,filename); - usage(); -} - - - int main(int argc, char **argv) { char * infile; Index: aescmdline.h =================================================================== --- aescmdline.h (revision 0) +++ aescmdline.h (revision 2116) @@ -0,0 +1,15 @@ +#ifndef INCLUDED_AESCMDLINE_H +#define INCLUDED_AESCMDLINE_H + +extern char *progname; + +extern keyInstance key; +extern cipherInstance cipher; + +void aes_set_key(char *_inkey, int _keysize); +void ewrite(int _outfile, void *_buf, size_t _count); +void read_key_from_stdin(int _infile, int _keysize); +void read_key(char *_filename, int _keysize); +void usage(void); + +#endif Index: aescrypt.c =================================================================== --- aescrypt.c (revision 2115) +++ aescrypt.c (revision 2116) @@ -20,41 +20,16 @@ #include <stdlib.h> #include "rijndael.h" +#include "aescmdline.h" #include "bin2hex.h" #include "dstring.h" #include "dorandom.h" -static char *progname; - -keyInstance key; -cipherInstance cipher; - static int cfb128_idx=-1; static char *cfb_blk; static char cfb_crypt[16]; - -static void usage(void) { - fprintf(stderr,"%s: Usage: %s -k {keyfile} [-s 128|192|256]\n",progname,progname); - exit(1); -} - - - -/* write data, aborting if we have a write error. */ - -static void ewrite(int outfile,void *buf, size_t count) { - int result; - - result=write(outfile,buf,count); - if (result!=(int) count) { - fprintf(stderr,"%s:error:could not write data.\n",progname); - perror(progname); - usage(); - } -} - static void cryptblock(char *src, int srclen) { int i,ch; @@ -130,86 +105,7 @@ return; } - - -/* Routine to set the key once we've read it. */ -static void aes_set_key(char *inkey, int keysize) { - int keylen,rqskeysize,rtncode; - char *hexkey; - rqskeysize = keysize / 4; - - hexkey=d_trim(inkey); - - keylen=strlen(hexkey); - - /* if it's a long key, chop it! */ - if ( keylen > rqskeysize ) { - keylen = rqskeysize; /* chop! */ - hexkey[rqskeysize]=0; - } - - if ( keylen==rqskeysize) { - keysize=keylen>>1; - - if ((rtncode = makeKey(&key,DIR_ENCRYPT,keylen*4,hexkey)) !=TRUE) { - fprintf(stderr,"%s: reported from makeKey: Key format error:%i\n",progname,rtncode); - usage(); - } - - /* I suppose I should just suck it up. */ - return; - } - - fprintf(stderr,"%s:Key size error:%d[%s]\n",progname,keylen,hexkey); - usage(); -} - -/* Routine to read a 128-bit key from stdin, as a 32-byte hex - * null-terminated string: - */ -static void read_key_from_stdin(int infile, int keysize) { - unsigned char keybuf[65]; - int result, hexkeysize; - - hexkeysize = keysize / 4; - result=read(infile,keybuf, hexkeysize+1); - if (result<hexkeysize+1) { - fprintf(stderr,"%s: Error: Key not found on stdin.\n",progname); - usage(); - } - - keybuf[hexkeysize]=0; /* make sure it is null terminated */ - aes_set_key(keybuf, keysize); - return; -} - - -/* Routine to read a key from keyfile. */ -static void read_key(char *filename, int keysize) { - char inbuf[1024]; - FILE *infile; - char *result; - - infile=fopen(filename,"r"); - if (!infile) { - fprintf(stderr,"%s:Error: Key file '%s' not found.\n",progname,filename); - usage(); - } - - while (result=fgets(inbuf,1023,infile), result != NULL) { - if ((strncmp(result,"kk=",3)==0) || strncmp(result,"kk:",3)==0) { - aes_set_key(result+3, keysize); - return; - } - } - - fprintf(stderr,"%s: Error: Key not found in file '%s'.\n",progname,filename); - usage(); -} - - - int main(int argc, char **argv) { char * infile;