]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - arcfour.c
changed arcfour api
[avr-crypto-lib.git] / arcfour.c
index abed9dd7856379678b9e73c167571eb6f8d7f1cf..7c35a0353f3fef055708ca56ea39f5fd13f32448 100644 (file)
--- a/arcfour.c
+++ b/arcfour.c
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 /* 
- * File:               arcfour.c
- * Author:     Daniel Otte
- * Date:       07.06.2006
- * License: GPL
+ * File:        arcfour.c
+ * Author:      Daniel Otte
+ * email:       daniel.otte@rub.de
+ * Date:        2006-06-07
+ * License:     GPLv3 or later
  * Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
  * 
  */
  * length is length of key in bytes!
  */
 
-void arcfour_init(arcfour_ctx_t *c, uint8_t *key, uint8_t length){
+void arcfour_init(const void *key, uint8_t length_B, arcfour_ctx_t *ctx){
        uint8_t t;
-       unsigned x,y=0;
+       uint16_t x,y=0;
        for(x=0; x<= 255; ++x)
-               c->s[x]=x;
+               ctx->s[x]=x;
        
        for(x=0; x<= 255; ++x){
-               y += c->s[x] + key[x % length];
+               y += ctx->s[x] + ((uint8_t*)key)[x % length_B];
                y &= 0xff;
-               t = c->s[y];
-               c->s[y] = c->s[x];
-               c->s[x] = t;
-       };
-               
-       c->i = c->j = 0;
+               /* ctx->s[y] <--> ctx->s[x] */
+               t = ctx->s[y];
+               ctx->s[y] = ctx->s[x];
+               ctx->s[x] = t;
+       }       
+       ctx->i = ctx->j = 0;
 }
 
-uint8_t arcfour_gen(arcfour_ctx_t *c){
+uint8_t arcfour_gen(arcfour_ctx_t *ctx){
        uint8_t t;
-       c->i++;
-       c->j += c->s[c->i];
-       t = c->s[c->j];
-       c->s[c->j] = c->s[c->i];
-       c->s[c->i] = t;
-       return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
+       ctx->i++;
+       ctx->j += ctx->s[ctx->i];
+       t = ctx->s[ctx->j];
+       ctx->s[ctx->j] = ctx->s[ctx->i];
+       ctx->s[ctx->i] = t;
+       return ctx->s[(ctx->s[ctx->j] + ctx->s[ctx->i]) & 0xff];
 }