Changeset 9ceee9d for zcrypt.c


Ignore:
Timestamp:
Jul 4, 2003, 12:26:03 AM (17 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
3ba3af0
Parents:
5b85d19
Message:
There is now a zcrypt command
Replies to zcrypted messages now work
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zcrypt.c

    r09489b89 r9ceee9d  
    267267}
    268268
    269 int zcrypt_decrypt(char *out, char *in, char *class, char *instance) {
    270   /* written by kretch for owl. */
    271   /* return 0 on success, otherwise -1 */
     269/* The 'owl_zcrypt_decrypt' function was written by kretch for Owl.
     270 * Decrypt the message in 'in' on class 'class' and instance
     271 * 'instance' and leave the result in 'out'.  Out must be a buffer
     272 * allocated by the caller.
     273 *
     274 * return 0 on success, otherwise -1
     275 */
     276int owl_zcrypt_decrypt(char *out, char *in, char *class, char *instance) {
    272277  char *fname, keystring[MAX_KEY], *inptr, *endptr;
    273278  FILE *fkey;
     
    278283 
    279284  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
     285  if (!fname) return(-1);
    280286  fkey=fopen(fname, "r");
    281287  if (!fkey) return(-1);
     
    314320}
    315321
    316 int zcrypt_encrypt(char *out, char *in, char *class, char *instance) {
     322int owl_zcrypt_encrypt(char *out, char *in, char *class, char *instance) {
     323  /*  static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) { */
     324  char *fname, keystring[MAX_KEY];
     325  FILE *fkey;
     326  des_cblock key;
     327  des_key_schedule schedule;
     328  char input[8], output[8];
     329  int size, length, i;
     330  char *inbuff = NULL, *inptr;
     331  int use_buffer = FALSE;
     332  int num_blocks=0, last_block_size=0;
     333
     334  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
     335  if (!fname) return(-1);
     336  fkey=fopen(fname, "r");
     337  if (!fkey) return(-1);
     338  fgets(keystring, MAX_KEY-1, fkey);
     339  fclose(fkey);
     340
     341  des_string_to_key(keystring, key);
     342  des_key_sched(key, schedule);
     343
     344  inbuff=in;
     345  length=strlen(inbuff);
     346  num_blocks=(length+7)/8;
     347  last_block_size=((length+7)%8)+1;
     348  use_buffer=TRUE;
     349
     350  strcpy(out, "");
     351 
     352  inptr=inbuff;
     353  while (TRUE) {
     354    /* Get 8 bytes from buffer */
     355    if (num_blocks > 1) {
     356      size = 8;
     357      memcpy(input, inptr, size);
     358      inptr+=8;
     359      num_blocks--;
     360    } else if (num_blocks == 1) {
     361      size=last_block_size;
     362      memcpy(input, inptr, size);
     363      num_blocks--;
     364    } else {
     365      size=0;
     366    }
     367
     368    /* Check for EOF and pad the string to 8 chars, if needed */
     369    if (size == 0) break;     /* END OF INPUT: BREAK FROM while LOOP! */
     370     
     371    if (size<8) memset(input + size, 0, 8 - size);
     372
     373    /* Encrypt and output the block */
     374    des_ecb_encrypt(input, output, schedule, TRUE);
     375
     376    for (i = 0; i < 8; i++) {
     377      sprintf(out, "%s%c", out, ((output[i] & 0xf0) >> 4) + BASE_CODE);
     378      sprintf(out, "%s%c", out, (output[i] & 0x0f)        + BASE_CODE);
     379    }
     380
     381    if (size < 8) break;
     382  }
    317383  return(0);
    318384}
     
    367433  /* Determine names to look for in .crypt-table */
    368434  if (instance) {
    369     sprintf(varname[numsearch++], "crypt-%s-%s:", (class?class:"message"),
    370             instance);
     435    sprintf(varname[numsearch++], "crypt-%s-%s:", (class?class:"message"), instance);
    371436  }
    372437  if (class) {
     
    413478
    414479    if (keyfile == NULL) {
    415       printf("Could not find key table entry.\n");
     480      /* printf("Could not find key table entry.\n"); */
    416481    } else {
    417482      /* Prepare result to be returned */
     
    421486        strcpy(keyfile, temp);
    422487      } else {
    423         printf("Memory allocation error.\n");
     488        /* printf("Memory allocation error.\n"); */
    424489      }
    425490    }
     
    427492    fclose(fsearch);
    428493  } else {
    429     printf("Could not open key table file: %s\n", filename);
     494    /* printf("Could not open key table file: %s\n", filename); */
    430495  }
    431496
Note: See TracChangeset for help on using the changeset viewer.