List Info

Thread: rc4 for m_utils?




rc4 for m_utils?
user name
2007-06-12 22:26:01
Hi All,

just knocked this up (and tested) - i was looking through the code for the gaim myspace plugin and noticed that it uses RC4 during authentication - thought it might be a candidate for inclusion in the utils module? i followed the format used there for MD5 and SHA1 as closely as i could

(note: called 'ARC4' for 'Alleged RC4' since apparently actual RC4 was never publically released - not sure it makes any difference anymore)

typedef struct {
    char lookup[256];
    int x, y;
} mir_arc4_ctx;

struct ARC4_INTERFACE {
    int cbSize;
    void (*arc4_init)(mir_arc4_ctx *ctx, char *key, int keylen);
    void (*arc4_crypt)(mir_arc4_ctx *ctx, char *dataIn, char *dataOut, int datalen);
};

void swap(char *b1, char *b2) {
 char b = *b1;
 ;*b1 = *b2;
 ;*b2 = b;
}

void arc4_init(mir_arc4_ctx *ctx, char *key, int keylen) {
    int i;
 &nbsp;  for(i = 0; i < 256; i++)
 ; &nbsp;  &nbsp;  ctx->lookup[i] = i;

&nbsp; &nbsp; ctx->x = 0; &nbsp; 
 &nbsp;  for(i = 0; i < 256; i++) {
 &nbsp; &nbsp;   ; ctx->x = (key[i % keylen] + ctx->lookup[i] + ctx->x) & 0xFF;
&nbsp; &nbsp; &nbsp; &nbsp; swap(&ctx->lookup[ctx->;x], &ctx-&gt;lookup[i]);
 ; &nbsp; }
 &nbsp;  ctx->x = 0;
 &nbsp;  ctx->y = 0;
}

void arc4_crypt(mir_arc4_ctx *ctx, char *dataIn, char *dataOut, int datalen) {
 &nbsp;  for(int i = 0; i < datalen; i++) {
 &nbsp;   &nbsp;  ctx->x = (ctx->x + 1) & 0xFF;
&nbsp; &nbsp;  &nbsp;  ctx->y = (ctx->lookup[ctx-&gt;x] + ctx->y) & 0xFF;
&nbsp; &nbsp;  &nbsp;  swap(&ctx->lookup[ctx->;x], &ctx-&gt;lookup[ctx->y]);
 &nbsp;   &nbsp;  dataOut[i] = (dataIn[i] ^ ctx->lookup[(ctx-&gt;lookup[ctx->x] + ctx->lookup[ctx-&gt;y]) & 0xFF]);
  ;  }
}

int GetARC4Interface(WPARAM wParam, LPARAM lParam) {
 &nbsp;  struct ARC4_INTERFACE *arc4i = (struct ARC4_INTERFACE*) lParam;
&nbsp; &nbsp; if ( arc4i == NULL )
 &nbsp;   &nbsp;  return 1;
 &nbsp;  if ( arc4i->cbSize != sizeof( struct ARC4_INTERFACE ))
 ; &nbsp;  &nbsp;  return 1;
 &nbsp; 
 &nbsp;  arc4i->arc4_init = arc4_init;
 &nbsp;  arc4i->arc4_crypt = arc4_crypt;
 &nbsp;  return 0;
}


Scott
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )