X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=arcfour%2Farcfour.c;h=ba66c165dd44e89e2d5691b2c3b0414fbe3b1d9f;hb=701cee0d98aab48dd3192c8cc7c77eb42581bc56;hp=e07193f2cbfbc9564adff1557c4f78b3b1ed414e;hpb=d32eba56ce10ea6b9eff123b50d9842673b38f2b;p=avr-crypto-lib.git diff --git a/arcfour/arcfour.c b/arcfour/arcfour.c index e07193f..ba66c16 100644 --- a/arcfour/arcfour.c +++ b/arcfour/arcfour.c @@ -33,20 +33,29 @@ * length is length of key in bytes! */ -void arcfour_init(const void *key, uint8_t length_B, arcfour_ctx_t *ctx){ +void arcfour_init(const void *key, uint16_t length_b, arcfour_ctx_t *ctx){ uint8_t t; - uint16_t x,y=0; - for(x=0; x<= 255; ++x) + const uint8_t length_B = length_b/8; + uint8_t nidx = length_B; + uint8_t x=0,y=0; + const uint8_t *kptr = (const uint8_t*)key; + do{ ctx->s[x]=x; + }while((uint8_t)++x); - for(x=0; x<= 255; ++x){ - y += ctx->s[x] + ((uint8_t*)key)[x % length_B]; + do{ + y += ctx->s[x] + *kptr++; + if(!--nidx){ + kptr = (const uint8_t*)key; + nidx = length_B; + } y &= 0xff; /* ctx->s[y] <--> ctx->s[x] */ t = ctx->s[y]; ctx->s[y] = ctx->s[x]; ctx->s[x] = t; - } + }while((uint8_t)++x); + ctx->i = ctx->j = 0; }