[BACK]Return to rc4.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/rc4.c, Revision 1.1

1.1     ! deraadt     1: /*
        !             2:
        !             3: Alleged RC4 (based on the Usenet posting in Spring-95)
        !             4:
        !             5: */
        !             6:
        !             7: #include "includes.h"
        !             8: RCSID("$Id: rc4.c,v 1.2 1999/05/04 11:59:00 bg Exp $");
        !             9:
        !            10: #include "rc4.h"
        !            11:
        !            12: void rc4_init(RC4Context *ctx, const unsigned char *key, unsigned int key_len)
        !            13: {
        !            14:   unsigned int t, u;
        !            15:   unsigned int keyindex;
        !            16:   unsigned int stateindex;
        !            17:   unsigned char* state;
        !            18:   unsigned int counter;
        !            19:
        !            20:   assert(key_len > 0);
        !            21:
        !            22:   state = &ctx->state[0];
        !            23:   ctx->x = 0;
        !            24:   ctx->y = 0;
        !            25:   for (counter = 0; counter < 256; counter++)
        !            26:     state[counter] = counter;
        !            27:   keyindex = 0;
        !            28:   stateindex = 0;
        !            29:   for (counter = 0; counter < 256; counter++)
        !            30:     {
        !            31:       t = state[counter];
        !            32:       stateindex = (stateindex + key[keyindex] + t) & 0xff;
        !            33:       u = state[stateindex];
        !            34:       state[stateindex] = t;
        !            35:       state[counter] = u;
        !            36:       if (++keyindex >= key_len)
        !            37:        keyindex = 0;
        !            38:     }
        !            39: }
        !            40:
        !            41: inline unsigned int rc4_byte(RC4Context *ctx)
        !            42: {
        !            43:   unsigned int x;
        !            44:   unsigned int y;
        !            45:   unsigned int sx, sy;
        !            46:   unsigned char *state;
        !            47:
        !            48:   state = ctx->state;
        !            49:   x = (ctx->x + 1) & 0xff;
        !            50:   sx = state[x];
        !            51:   y = (sx + ctx->y) & 0xff;
        !            52:   sy = state[y];
        !            53:   ctx->x = x;
        !            54:   ctx->y = y;
        !            55:   state[y] = sx;
        !            56:   state[x] = sy;
        !            57:   return state[(sx + sy) & 0xff];
        !            58: }
        !            59:
        !            60: void rc4_encrypt(RC4Context *ctx, unsigned char *dest,
        !            61:                 const unsigned char *src, unsigned int len)
        !            62: {
        !            63:   unsigned int i;
        !            64:   for (i = 0; i < len; i++)
        !            65:     dest[i] = src[i] ^ rc4_byte(ctx);
        !            66: }
        !            67:
        !            68: void rc4_decrypt(RC4Context *ctx, unsigned char *dest,
        !            69:                 const unsigned char *src, unsigned int len)
        !            70: {
        !            71:   rc4_encrypt(ctx, dest, src, len);
        !            72: }