Changeset dacb555


Ignore:
Timestamp:
Feb 9, 2010, 9:49:41 PM (15 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Branches:
master, release-1.10, release-1.6, release-1.7, release-1.8, release-1.9
Children:
a52eeb1
Parents:
d35d2ba
git-author:
Anders Kaseorg <andersk@mit.edu> (02/07/10 22:55:21)
git-committer:
Anders Kaseorg <andersk@mit.edu> (02/09/10 21:49:41)
Message:
zcrypt: Change zcrypt functions to allocate their output buffer.

The logic to determine the right buffer size is complicated and does
not belong in callers.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Reviewed-by: Nelson Elhage <nelhage@mit.edu>
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • functions.c

    r2a17b63 rdacb555  
    406406  char *cryptmsg;
    407407  owl_message *m;
    408 #ifdef OWL_ENABLE_ZCRYPT
    409   int ret;
    410 #endif
    411408
    412409  /* create the zwrite and send the message */
     
    419416  mymsg=owl_zwrite_get_message(&z);
    420417#ifdef OWL_ENABLE_ZCRYPT
    421   /* Allocate enough space for the crypted message. For each byte of
    422    * the message, the encoded cyphertext will have two bytes. Block
    423    * size is 8 bytes of input, or 16 bytes of output, so make sure we
    424    * have at least one block worth of space allocated. If the message
    425    * is empty, no blocks are sent, but we still allocate one
    426    * block. The additional 16 bytes also provide space for the null
    427    * terminator, as we will never use all of it for cyphertext.
    428    */
    429   cryptmsg=owl_malloc((strlen(mymsg)*2)+16);
    430   ret=owl_zcrypt_encrypt(cryptmsg, mymsg, owl_zwrite_get_class(&z), owl_zwrite_get_instance(&z));
    431   if (ret) {
     418  cryptmsg = owl_zcrypt_encrypt(mymsg, owl_zwrite_get_class(&z), owl_zwrite_get_instance(&z));
     419  if (!cryptmsg) {
    432420    owl_function_error("Error in zcrypt, possibly no key found.  Message not sent.");
    433421    owl_function_beep();
    434     owl_free(cryptmsg);
    435422    owl_zwrite_free(&z);
    436423    return;
  • message.c

    rd35d2ba rdacb555  
    841841  /* if zcrypt is enabled try to decrypt the message */
    842842  if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) {
    843     char *out;
    844     int ret;
    845 
    846     out=owl_malloc(strlen(owl_message_get_body(m))*16+20);
    847     ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
    848     if (ret == 0)
     843    char *out = owl_zcrypt_decrypt(owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m));
     844    if (out) {
    849845      owl_message_set_body(m, out);
    850     owl_free(out);
     846      owl_free(out);
     847    }
    851848  }
    852849#endif 
  • zcrypt.c

    r3f18ad5 rdacb555  
    7676/* The 'owl_zcrypt_decrypt' function was written by kretch for Owl.
    7777 * Decrypt the message in 'in' on class 'class' and instance
    78  * 'instance' and leave the result in 'out'.  Out must be a buffer
    79  * allocated by the caller.
    80  *
    81  * return 0 on success, otherwise -1
     78 * 'instance'.  Return must be freed by caller.
    8279 */
    83 int owl_zcrypt_decrypt(char *out, const char *in, const char *class, const char *instance) {
     80char *owl_zcrypt_decrypt(const char *in, const char *class, const char *instance)
     81{
    8482  const char *inptr, *endptr;
    8583  char *fname, keystring[MAX_KEY];
    8684  FILE *fkey;
    8785  des_key_schedule schedule;
     86  char *out;
    8887  unsigned char input[8], output[8];
    8988  int i, c1, c2;
    9089 
    9190  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
    92   if (!fname) return(-1);
     91  if (!fname) return NULL;
    9392  fkey=fopen(fname, "r");
    94   if (!fkey) return(-1);
     93  if (!fkey) return NULL;
    9594  if (!fgets(keystring, MAX_KEY-1, fkey)) {
    9695    fclose(fkey);
    97     return -1;
     96    return NULL;
    9897  }
    9998  fclose(fkey);
    10099
     100  out = owl_malloc(strlen(in) * 16 + 20);
    101101  strcpy(out, "");
    102102
     
    120120  if (out[0] && out[strlen(out) - 1] != '\n')
    121121    strcat(out, "\n");
    122   return(0);
    123 }
    124 
    125 int owl_zcrypt_encrypt(char *out, const char *in, const char *class, const char *instance) {
     122  return out;
     123}
     124
     125char *owl_zcrypt_encrypt(const char *in, const char *class, const char *instance)
     126{
    126127  char *fname, keystring[MAX_KEY];
    127128  FILE *fkey;
    128129  des_key_schedule schedule;
     130  char *out;
    129131  unsigned char input[8], output[8];
    130132  int size, length, i;
     
    133135
    134136  fname=GetZephyrVarKeyFile("zcrypt", class, instance);
    135   if (!fname) return(-1);
     137  if (!fname) return NULL;
    136138  fkey=fopen(fname, "r");
    137   if (!fkey) return(-1);
     139  if (!fkey) return NULL;
    138140  if (!fgets(keystring, MAX_KEY-1, fkey)) {
    139141    fclose(fkey);
    140     return -1;
     142    return NULL;
    141143  }
    142144  fclose(fkey);
    143145
    144146  owl_zcrypt_string_to_schedule(keystring, &schedule);
     147
     148  /* Allocate enough space for the crypted message. For each byte of
     149   * the message, the encoded cyphertext will have two bytes. Block
     150   * size is 8 bytes of input, or 16 bytes of output, so make sure we
     151   * have at least one block worth of space allocated. If the message
     152   * is empty, no blocks are sent, but we still allocate one
     153   * block. The additional 16 bytes also provide space for the null
     154   * terminator, as we will never use all of it for cyphertext.
     155   */
     156  out = owl_malloc((strlen(in) * 2) + 16);
    145157
    146158  inbuff=in;
     
    182194    if (size < 8) break;
    183195  }
    184   return(0);
     196  return out;
    185197}
    186198
Note: See TracChangeset for help on using the changeset viewer.