Download | Plain Text | Line Numbers


#include <string.h>
#include <assert.h>
#include "cipher.h"
 
static unsigned char key[] = {
  0x0b, 0x59, 0xed, 0x3d, 0x31, 0x1b, 0x3b, 0xce,
  0x69, 0x14, 0xa9, 0x8f, 0xe8, 0xbe, 0x49, 0x42,
  0x13, 0xa4, 0x98, 0x0a, 0x89, 0x9d, 0xb0, 0x4f,
  0xa9, 0x0b, 0x25, 0x4e, 0xc4, 0x74, 0x2b, 0x63,
  0x44, 0x91, 0xe0, 0xc6, 0x40, 0x87, 0xeb, 0x16,
  0x91, 0xff, 0xcd, 0x9c, 0xf8, 0x56, 0x5a, 0xd7,
  0x86, 0x6b, 0x6e, 0xad, 0xc6, 0x91, 0xc7, 0xaa,
  0x6b, 0xf0, 0x30, 0xac, 0xe8, 0x4f, 0xb0, 0xe2,
  0x5b, 0xf7, 0xa8, 0xb1, 0xec, 0xf9, 0x69, 0x5a,
  0x3d, 0xce, 0xfe, 0x0e, 0x94, 0x1b, 0x19, 0x99,
  0x32, 0xd7, 0x8a, 0x8f, 0xa4, 0xe7, 0x54, 0xce,
  0xb7, 0x6d, 0xd6, 0x9f, 0xc2, 0xd0, 0xd9, 0x62,
  0xcb, 0xd1, 0xf0, 0x2a, 0x36, 0xcf, 0x6d, 0xdd,
  0x23, 0xd3, 0xc5, 0x45, 0x3e, 0x15, 0x64, 0x2d,
  0x05, 0x59, 0x85, 0x20, 0x2c, 0x56, 0x64, 0xce,
  0xff, 0xb5, 0x42, 0x6e, 0x64, 0x9a, 0x46, 0x14,
  0x6e, 0x58, 0x82, 0xff, 0x44, 0x29, 0xaf, 0xbc,
  0xac, 0x4b, 0xd3, 0x53, 0x44, 0xb9, 0x70, 0x17,
  0x8c, 0x0c, 0x66, 0xae, 0xfd, 0x8d, 0x95, 0x8d,
  0x36, 0x07, 0xe4, 0x72, 0x6e, 0xdd, 0x07, 0xda,
  0x17, 0x66, 0xb2, 0xff, 0xae, 0x26, 0xaa, 0x7f,
  0x88, 0xe1, 0xd3, 0xf2, 0xcc, 0x9b, 0xc5, 0x9b,
  0xf5, 0x31, 0x19, 0x3b, 0xd8, 0xb3, 0xbc, 0xaa,
  0x67, 0x31, 0x05, 0x0e, 0x75, 0x25, 0x04, 0x0f,
  0x7f, 0xb9, 0x33, 0xad, 0x72, 0xa0, 0xf2, 0x51,
  0xde, 0xd0, 0xf9, 0xb3, 0xe2, 0x36, 0xbc, 0xaf,
  0xe0, 0xcb, 0x0c, 0x1e, 0x4a, 0xf7, 0x04, 0xda,
  0x9c, 0xf0, 0xa3, 0x7e, 0x84, 0xef, 0x98, 0x47,
  0x8d, 0x59, 0x0c, 0xf2, 0x79, 0x3e, 0x37, 0xf4,
  0x4d, 0xfe, 0x29, 0x9d, 0xec, 0xe8, 0x84, 0x7b,
  0xe6, 0x1f, 0x93, 0xbf, 0x27, 0x2b, 0xa7, 0x8e,
  0x45, 0xfc, 0x68, 0x92, 0xb6, 0xb7, 0x33, 0x14,
};
 
static unsigned int key_size = sizeof(key);
 
int CipherInit(ctx_t *ctx)
{
  unsigned char val, oldval, tmp;
  unsigned int i;
 
  assert(key_size == sizeof(ctx->cipher));
 
  /* initalize values */
  ctx->out = NULL;
  ctx->off1 = 0;
  ctx->off2 = 0;
  ctx->prepend_hashkey = 1;
 
  for(i = 0; i < key_size; ++i)
    ctx->cipher[i] = (unsigned char)i;
 
  oldval = 0;
  for(i = 0; i < key_size; ++i)
  {
    val = ctx->cipher[i] + oldval + key[i];
 
    /* swap values */
    tmp = ctx->cipher[i];
    ctx->cipher[i] = ctx->cipher[val];
    ctx->cipher[val] = tmp;
 
    oldval = val;
  }
 
  return i;
}
 
uint32_t CipherUpdate(ctx_t *ctx, const char *in_data, uint32_t in_len)
{
  uint32_t in_ix = 0, out_ix = 0;
  unsigned char off1, off2;
  unsigned char val1, val2;
 
  assert(key_size == sizeof(ctx->cipher));
 
  if (ctx->prepend_hashkey == 1)
  {
    ctx->out[out_ix++] = '#';
    ctx->prepend_hashkey = 0;
    /* store in_len inside ctx buffer. this is completely
     * insane but confixx does it this way too
     */
    ctx->ret = in_len + 1;
  }
  else
    ctx->ret = in_len;
 
  /* retrieve offsets */
  off1 = ctx->off1;
  off2 = ctx->off2;
 
  while(in_ix < in_len)
  {
    ++off1;
 
    val1 = ctx->cipher[off1];
    val2 = ctx->cipher[((unsigned char)(val1 + off2))];
 
    /* swap values */
    ctx->cipher[((unsigned char)(val1 + off2))] = val1;
    ctx->cipher[off1] = val2;
 
    off2 = val1 + off2;
 
    /* "decrypt" */
    ctx->out[out_ix++] = in_data[in_ix++] ^ ctx->cipher[((unsigned char)(val1 + val2))];
  }
 
  /* store offsets */
  ctx->off1 = off1;
  ctx->off2 = off2;
 
  return ctx->ret;
}