示例#1
0
TWOFISH *TwoFishInit(const char *userkey)
{   TWOFISH *tfdata;
  int i,x,m;
  char tkey[TwoFish_KEY_LENGTH+40];

  tfdata=malloc(sizeof(TWOFISH));      /* allocate the TwoFish structure */
  if(tfdata!=NULL)
  {  if(*userkey)
    {  strncpy(tkey,userkey,TwoFish_KEY_LENGTH);      /* use first 32 chars of user supplied password */
      tkey[TwoFish_KEY_LENGTH]=0;              /* make sure it wasn't more */
    }
    else
      strcpy(tkey,TwoFish_DEFAULT_PW);  /* if no key defined, use default password */
    for(i=0,x=0,m=strlen(tkey);i<TwoFish_KEY_LENGTH;i++)  /* copy into data structure */
    {  tfdata->key[i]=tkey[x++];              /* fill the whole keyspace with repeating key. */
      if(x==m)
        x=0;
    }

    if(!TwoFish_MDSready)
      _TwoFish_PrecomputeMDSmatrix();    /* "Wake Up, Neo" */
    _TwoFish_MakeSubKeys(tfdata);      /* generate subkeys */
    _TwoFish_ResetCBC(tfdata);        /* reset the CBC */
    tfdata->output=NULL;          /* nothing to output yet */
    tfdata->dontflush=FALSE;        /* reset decrypt skip block flag */
    if(TwoFish_srand)
    {  TwoFish_srand=FALSE;
      srand(time(NULL));
    }
  }
  return tfdata;              /* return the data pointer */
}
示例#2
0
unsigned long TwoFishDecryptRaw(const char *in,
                char *out,
                unsigned long len,
                TWOFISH *tfdata)
{  _TwoFish_ResetCBC(tfdata);              /* reset CBC flag. */
  tfdata->output=(unsigned char*)out;              /* output straight into output buffer. */
  return _TwoFish_CryptRaw(in,out,len,TRUE,tfdata);  /* and go for it. */
}
示例#3
0
文件: twofish.c 项目: jing-git/ethn
/*  TwoFish Raw Decryption
 *
 *  Does not use header, but does use CBC (if more than one block has to be decrypted).
 *
 *  Input:  Pointer to the buffer of the ciphertext to be decrypted.
 *      Pointer to the buffer receiving the plaintext.
 *      The length of the ciphertext buffer (at least one cipher block).
 *      The TwoFish structure.
 *
 *  Output: The amount of bytes decrypted if successful, otherwise 0.
 */
uint32_t TwoFishDecryptRaw(uint8_t *in,
                           uint8_t *out,
                           uint32_t len,
                           TWOFISH *tfdata)
{
  _TwoFish_ResetCBC(tfdata);              /* reset CBC flag. */
  tfdata->output = out;           /* output straight into output buffer. */
  return _TwoFish_CryptRaw(in, out, len, TRUE, tfdata); /* and go for it. */
}
示例#4
0
文件: twofish.c 项目: jing-git/ethn
/*  TwoFish Initialization
 *
 *  This routine generates a global data structure for use with TwoFish,
 *  initializes important values (such as subkeys, sBoxes), generates subkeys
 *  and precomputes the MDS matrix if not already done.
 *
 *  Input:  User supplied password (will be appended by default password of 'SnortHas2FishEncryptionRoutines!')
 *
 *      Output: Pointer to TWOFISH structure. This data structure contains key dependent data.
 *    This pointer is used with all other crypt functions.
 */
TWOFISH *TwoFishInit(const uint8_t *userkey, uint32_t keysize)
{
  TWOFISH *tfdata;
  int i, x, m;
  uint8_t tkey[TwoFish_KEY_LENGTH + 40];

  memset( tkey, 0, TwoFish_KEY_LENGTH + 40 );
  tfdata = (TWOFISH *)malloc(sizeof(TWOFISH));    /* allocate the TwoFish structure */
  if (tfdata != NULL) {

    /* Changes here prevented a dangerous random key segment for keys of length < TwoFish_KEY_LENGTH */
    if (keysize > 0) {
      memcpy( tkey, userkey, keysize ); /* The rest will be zeros */
    } else {
      memcpy( tkey, TwoFish_DEFAULT_PW, TwoFish_DEFAULT_PW_LEN ); /* if no key defined, use default password */
    }

    /* This loop is awful - surely a loop on memcpy() would be clearer and more efficient */
    for (i = 0, x = 0, m = keysize; i < TwoFish_KEY_LENGTH; i++) { /* copy into data structure */
      tfdata->key[i] = tkey[x++];   /* fill the whole keyspace with repeating key. */
      if (x == m)
        x = 0;
    }

    if (!TwoFish_MDSready)
      _TwoFish_PrecomputeMDSmatrix();   /* "Wake Up, Neo" */
    _TwoFish_MakeSubKeys(tfdata);     /* generate subkeys */
    _TwoFish_ResetCBC(tfdata);      /* reset the CBC */
    tfdata->output = NULL;      /* nothing to output yet */
    tfdata->dontflush = FALSE;      /* reset decrypt skip block flag */
    if (TwoFish_srand) {
      TwoFish_srand = FALSE;
      /* REVISIT: BbMaj7 : Should choose something with less predictability
       * particularly for embedded targets with no real-time clock. */
      srand((unsigned int)time(NULL));
    }
  }
  return tfdata;          /* return the data pointer */
}