2008-05-26 19:13:21 +00:00
|
|
|
/* arcfour.c */
|
|
|
|
/*
|
2009-02-04 13:50:15 +00:00
|
|
|
This file is part of the AVR-Crypto-Lib.
|
2008-05-26 19:13:21 +00:00
|
|
|
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2008-11-25 01:23:22 +00:00
|
|
|
/*
|
2008-07-03 04:11:34 +00:00
|
|
|
* File: arcfour.c
|
|
|
|
* Author: Daniel Otte
|
|
|
|
* email: daniel.otte@rub.de
|
|
|
|
* Date: 2006-06-07
|
|
|
|
* License: GPLv3 or later
|
2006-07-31 16:04:26 +00:00
|
|
|
* Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
|
2008-11-25 01:23:22 +00:00
|
|
|
*
|
2006-07-31 16:04:26 +00:00
|
|
|
*/
|
2008-11-25 01:23:22 +00:00
|
|
|
|
2006-07-31 16:04:26 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "arcfour.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* length is length of key in bytes!
|
|
|
|
*/
|
|
|
|
|
2008-08-06 18:04:23 +00:00
|
|
|
void arcfour_init(const void *key, uint8_t length_B, arcfour_ctx_t *ctx){
|
2006-07-31 16:04:26 +00:00
|
|
|
uint8_t t;
|
2008-08-06 18:04:23 +00:00
|
|
|
uint16_t x,y=0;
|
2006-07-31 16:04:26 +00:00
|
|
|
for(x=0; x<= 255; ++x)
|
2008-08-02 04:38:17 +00:00
|
|
|
ctx->s[x]=x;
|
2008-11-25 01:23:22 +00:00
|
|
|
|
2006-07-31 16:04:26 +00:00
|
|
|
for(x=0; x<= 255; ++x){
|
2008-08-02 04:38:17 +00:00
|
|
|
y += ctx->s[x] + ((uint8_t*)key)[x % length_B];
|
2006-07-31 16:04:26 +00:00
|
|
|
y &= 0xff;
|
2008-08-06 18:04:23 +00:00
|
|
|
/* ctx->s[y] <--> ctx->s[x] */
|
2008-08-02 04:38:17 +00:00
|
|
|
t = ctx->s[y];
|
|
|
|
ctx->s[y] = ctx->s[x];
|
|
|
|
ctx->s[x] = t;
|
2008-11-25 01:23:22 +00:00
|
|
|
}
|
2008-08-02 04:38:17 +00:00
|
|
|
ctx->i = ctx->j = 0;
|
2006-07-31 16:04:26 +00:00
|
|
|
}
|
|
|
|
|
2008-08-02 04:38:17 +00:00
|
|
|
uint8_t arcfour_gen(arcfour_ctx_t *ctx){
|
2006-07-31 16:04:26 +00:00
|
|
|
uint8_t t;
|
2008-08-02 04:38:17 +00:00
|
|
|
ctx->i++;
|
|
|
|
ctx->j += ctx->s[ctx->i];
|
2008-08-07 09:45:50 +00:00
|
|
|
/* ctx->s[i] <--> ctx->s[j] */
|
2008-08-02 04:38:17 +00:00
|
|
|
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];
|
2006-07-31 16:04:26 +00:00
|
|
|
}
|
|
|
|
|