1 /* Copyright 2000 Enhanced Software Technologies Inc.
2 All Rights Reserved
3
4 Released for public use under a BSD-style license. See file LICENSE
5 for details.
6
7 common routines for the aescrypt and aesget programs
8 */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17
18 #include "aes.h"
19 #include "aescmdline.h"
20 #include "bin2hex.h"
21 #include "dstring.h"
22
23 char *progname;
24
25 keyInstance key;
26 cipherInstance cipher;
27
usage(void)28 void usage(void) {
29 fprintf(stderr,"%s: Usage: %s -k {keyfile} [-s 128|192|256]\n",progname,progname);
30 exit(1);
31 }
32
33 /* write data, aborting if we have a write error. */
34
ewrite(int outfile,void * buf,ssize_t count)35 void ewrite(int outfile,void *buf, ssize_t count) {
36 ssize_t n, result;
37
38 for (n = result = 0; result < count; result += n) {
39 n = write(outfile, (char *)buf + result, count - result);
40 if (n <= 0) {
41 fprintf(stderr,"%s:error:could not write data.\n",progname);
42 perror(progname);
43 usage();
44 }
45 }
46 }
47
48 /* Routine to set the key once we've read it. */
aes_set_key(char * inkey,int keysize)49 void aes_set_key(char *inkey, int keysize) {
50 int keylen,rqskeysize,rtncode;
51 char *hexkey;
52 rqskeysize = keysize / 4;
53
54 hexkey=d_trim(inkey);
55
56 keylen=strlen(hexkey);
57
58 /* if it's a long key, chop it! */
59 if ( keylen > rqskeysize ) {
60 keylen = rqskeysize; /* chop! */
61 hexkey[rqskeysize]=0;
62 }
63
64 if ( keylen==rqskeysize) {
65 keysize=keylen>>1;
66
67 if ((rtncode = makeKey(&key,DIR_ENCRYPT,keylen*4,hexkey)) !=TRUE) {
68 fprintf(stderr,"%s: reported from makeKey: Key format error:%i\n",progname,rtncode);
69 usage();
70 }
71
72 /* I suppose I should just suck it up. */
73 return;
74 }
75
76 fprintf(stderr,"%s:Key size error:%d[%s]\n",progname,keylen,hexkey);
77 usage();
78 }
79
80 /* Routine to read a 128-bit key from stdin, as a 32-byte hex
81 * null-terminated string:
82 */
read_key_from_stdin(int infile,int keysize)83 void read_key_from_stdin(int infile, int keysize) {
84 char keybuf[65];
85 int n, result, hexkeysize;
86
87 hexkeysize = keysize / 4;
88 for (n = result = 0; result < hexkeysize + 1; result += n) {
89 n = read(infile, keybuf + result, hexkeysize + 1 - result);
90 if (n < 0) {
91 fprintf(stderr,"%s: Error: Could not read key from stdin.\n",progname);
92 perror(progname);
93 usage();
94 } else if (n == 0) {
95 fprintf(stderr,"%s: Error: Key not found on stdin.\n",progname);
96 usage();
97 }
98 }
99
100 keybuf[hexkeysize]=0; /* make sure it is null terminated */
101 aes_set_key(keybuf, keysize);
102 return;
103 }
104
105 /* Routine to read a key from keyfile. */
read_key(char * filename,int keysize)106 void read_key(char *filename, int keysize) {
107 char inbuf[1024];
108 FILE *infile;
109 char *result;
110
111 infile=fopen(filename,"r");
112 if (!infile) {
113 fprintf(stderr,"%s:Error: Key file '%s' not found.\n",progname,filename);
114 usage();
115 }
116
117 while (result=fgets(inbuf,1023,infile), result != NULL) {
118 if ((strncmp(result,"kk=",3)==0) || strncmp(result,"kk:",3)==0) {
119 aes_set_key(result+3, keysize);
120 return;
121 }
122 }
123
124 fprintf(stderr,"%s: Error: Key not found in file '%s'.\n",progname,filename);
125 usage();
126 }
127