source: zcrypt.c @ 8ea0ffc

release-1.5
Last change on this file since 8ea0ffc was 212d8b7, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
zcrypt: Don’t allocate 32 times as much memory as we need for decrypting. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Nelson Elhage <nelhage@mit.edu>
  • Property mode set to 100644
File size: 7.5 KB
Line 
1/* This file is stolen and slightly modified code */
2
3/* zcrypt.c -- Read in a data stream from stdin & dump a decrypted/encrypted *
4 *   datastream.  Reads the string to make the key from from the first       *
5 *   parameter.  Encrypts or decrypts according to -d or -e flag.  (-e is    *
6 *   default.)  Will invoke zwrite if the -c option is provided for          *
7 *   encryption.  If a zephyr class is specified & the keyfile name omitted  *
8 *   the ~/.crypt-table will be checked for "crypt-classname" and then       *
9 *   "crypt-default" for the keyfile name.                                   */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/types.h>
15#include <sys/wait.h>
16#include "owl.h"
17
18#ifdef OWL_ENABLE_ZCRYPT
19
20#define BASE_CODE 70
21#define LAST_CODE (BASE_CODE + 15)
22#define OUTPUT_BLOCK_SIZE 16
23#include <unistd.h>
24#include <sys/types.h>
25
26#ifdef HAVE_KERBEROS_IV
27#include <kerberosIV/des.h>
28#else
29#include <openssl/des.h>
30#endif
31
32#define MAX_KEY 128
33
34#ifndef TRUE
35#define TRUE -1
36#endif
37#ifndef FALSE
38#define FALSE 0
39#endif
40
41#define ZWRITE_OPT_NOAUTH     (1<<0)
42#define ZWRITE_OPT_SIGNATURE  (1<<1)
43#define ZWRITE_OPT_IGNOREVARS (1<<2)
44#define ZWRITE_OPT_VERBOSE    (1<<3)
45#define ZWRITE_OPT_QUIET      (1<<4)
46#define ZCRYPT_OPT_MESSAGE    (1<<5)
47#define ZCRYPT_OPT_IGNOREDOT  (1<<6)
48
49typedef struct
50{
51  int flags;
52  char *signature;
53  char *message;
54} ZWRITEOPTIONS;
55
56char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance);
57
58#define M_NONE            0
59#define M_ZEPHYR_ENCRYPT  1
60#define M_DECRYPT         2
61#define M_ENCRYPT         3
62#define M_RANDOMIZE       4
63#define M_SETKEY          5
64
65static void owl_zcrypt_string_to_schedule(const char *keystring, des_key_schedule *schedule) {
66#ifdef HAVE_KERBEROS_IV
67  des_cblock key;
68#else
69  des_cblock _key, *key = &_key;
70#endif
71
72  des_string_to_key(keystring, key);
73  des_key_sched(key, *schedule);
74}
75
76/* The 'owl_zcrypt_decrypt' function was written by kretch for Owl.
77 * Decrypt the message in 'in' on class 'class' and instance
78 * 'instance'.  Return must be freed by caller.
79 */
80char *owl_zcrypt_decrypt(const char *in, const char *class, const char *instance)
81{
82  char *fname, keystring[MAX_KEY];
83  FILE *fkey;
84 
85  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
86  if (!fname) return NULL;
87  fkey=fopen(fname, "r");
88  if (!fkey) return NULL;
89  if (!fgets(keystring, MAX_KEY-1, fkey)) {
90    fclose(fkey);
91    return NULL;
92  }
93  fclose(fkey);
94
95  return owl_zcrypt_decrypt_with_key(in, keystring);
96}
97
98char *owl_zcrypt_decrypt_with_key(const char *in, const char *keystring)
99{
100  const char *inptr, *endptr;
101  char *out;
102  des_key_schedule schedule;
103  unsigned char input[8], output[8];
104  int i, c1, c2;
105
106  /* We read in some number of full 16-byte blocks and write out the
107   * same number of 8-byte blocks, plus a null terminator.
108   */
109  out = owl_malloc((strlen(in) / 16) * 8 + 1);
110
111  strcpy(out, "");
112
113  output[0] = '\0';    /* In case no message at all                 */
114
115  owl_zcrypt_string_to_schedule(keystring, &schedule);
116
117  inptr=in;
118  endptr = in + strlen(in);
119  while (inptr + 16 <= endptr) {
120    for (i=0; i<8; i++) {
121      c1=(inptr[0])-BASE_CODE;
122      c2=(inptr[1])-BASE_CODE;
123      input[i]=c1 * 0x10 + c2;
124      inptr+=2;
125    }
126    des_ecb_encrypt(&input, &output, schedule, FALSE);
127    strncat(out, (const char *)output, 8);
128  }
129
130  return out;
131}
132
133char *owl_zcrypt_encrypt(const char *in, const char *class, const char *instance)
134{
135  char *fname, keystring[MAX_KEY];
136  FILE *fkey;
137
138  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
139  if (!fname) return NULL;
140  fkey=fopen(fname, "r");
141  if (!fkey) return NULL;
142  if (!fgets(keystring, MAX_KEY-1, fkey)) {
143    fclose(fkey);
144    return NULL;
145  }
146  fclose(fkey);
147
148  return owl_zcrypt_encrypt_with_key(in, keystring);
149}
150
151char *owl_zcrypt_encrypt_with_key(const char *in, const char *keystring)
152{
153  des_key_schedule schedule;
154  char *out;
155  unsigned char input[8], output[8];
156  int size, length, i;
157  const char *inbuff = NULL, *inptr;
158  int num_blocks=0, last_block_size=0;
159
160  owl_zcrypt_string_to_schedule(keystring, &schedule);
161
162  /* Allocate enough space for the crypted message. For each byte of
163   * the message, the encoded cyphertext will have two bytes. Block
164   * size is 8 bytes of input, or 16 bytes of output, so make sure we
165   * have at least one block worth of space allocated. If the message
166   * is empty, no blocks are sent, but we still allocate one
167   * block. The additional 16 bytes also provide space for the null
168   * terminator, as we will never use all of it for cyphertext.
169   */
170  out = owl_malloc((strlen(in) * 2) + 16);
171
172  inbuff=in;
173  length=strlen(inbuff);
174  num_blocks=(length+7)/8;
175  last_block_size=((length+7)%8)+1;
176
177  strcpy(out, "");
178 
179  inptr=inbuff;
180  while (TRUE) {
181    /* Get 8 bytes from buffer */
182    if (num_blocks > 1) {
183      size = 8;
184      memcpy(input, inptr, size);
185      inptr+=8;
186      num_blocks--;
187    } else if (num_blocks == 1) {
188      size=last_block_size;
189      memcpy(input, inptr, size);
190      num_blocks--;
191    } else {
192      size=0;
193    }
194
195    /* Check for EOF and pad the string to 8 chars, if needed */
196    if (size == 0) break;     /* END OF INPUT: BREAK FROM while LOOP! */
197     
198    if (size<8) memset(input + size, 0, 8 - size);
199
200    /* Encrypt and output the block */
201    des_ecb_encrypt(&input, &output, schedule, TRUE);
202
203    for (i = 0; i < 8; i++) {
204      sprintf(out + strlen(out), "%c", ((output[i] & 0xf0) >> 4) + BASE_CODE);
205      sprintf(out + strlen(out), "%c", (output[i] & 0x0f)        + BASE_CODE);
206    }
207
208    if (size < 8) break;
209  }
210  return out;
211}
212
213
214#define MAX_BUFF 258
215#define MAX_SEARCH 3
216/* Find the class/instance in the .crypt-table */
217char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance) {
218  char *keyfile = NULL;
219  char *varname[MAX_SEARCH];
220  int length[MAX_SEARCH], i;
221  char buffer[MAX_BUFF];
222  char *filename;
223  char result[MAX_SEARCH][MAX_BUFF];
224  int numsearch = 0;
225  FILE *fsearch;
226
227  memset(varname, 0, sizeof(varname));
228
229  /* Determine names to look for in .crypt-table */
230  if (instance) {
231    varname[numsearch++] = owl_sprintf("crypt-%s-%s:", (class?class:"message"), instance);
232  }
233  if (class) {
234    varname[numsearch++] = owl_sprintf("crypt-%s:", class);
235  }
236  varname[numsearch++] = owl_strdup("crypt-default:");
237
238  /* Setup the result array, and determine string lengths */
239  for (i = 0; i < numsearch; i++) {
240    result[i][0] = '\0';
241    length[i] = strlen(varname[i]);
242  }
243
244  /* Open~/.crypt-table */
245  filename = owl_sprintf("%s/.crypt-table", getenv("HOME"));
246  fsearch = fopen(filename, "r");
247  if (fsearch) {
248    /* Scan file for a match */
249    while (!feof(fsearch)) {
250      if (!fgets(buffer, MAX_BUFF - 3, fsearch)) break;
251      for (i = 0; i < numsearch; i++) {
252        if (strncasecmp(varname[i], buffer, length[i]) == 0) {
253          int j;
254          for (j = length[i]; buffer[j] == ' '; j++)
255            ;
256          strcpy(result[i], &buffer[j]);
257          if (*result[i]) {
258            if (result[i][strlen(result[i])-1] == '\n') {
259              result[i][strlen(result[i])-1] = '\0';
260            }
261          }
262        }
263      }
264    }
265
266    /* Pick the "best" match found */
267    keyfile = NULL;
268    for (i = 0; i < numsearch; i++) {
269      if (*result[i]) {
270        keyfile = result[i];
271        break;
272      }
273    }
274
275    if (keyfile == NULL) {
276      /* printf("Could not find key table entry.\n"); */
277    } else {
278      /* Prepare result to be returned */
279      keyfile = owl_strdup(keyfile);
280    }
281   
282    fclose(fsearch);
283  } else {
284    /* printf("Could not open key table file: %s\n", filename); */
285  }
286
287  for(i = 0; i < MAX_SEARCH; i++) {
288    owl_free(varname[i]);
289  }
290
291  owl_free(filename);
292
293  return(keyfile);
294}
295
296#endif
Note: See TracBrowser for help on using the repository browser.