CyaSSL is an SSL library for devices like mbed.
Dependents: cyassl-client Sync
arc4.c
00001 /* arc4.c 00002 * 00003 * Copyright (C) 2006-2009 Sawtooth Consulting Ltd. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * CyaSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00020 */ 00021 00022 00023 #include "arc4.h" 00024 00025 00026 00027 void Arc4SetKey(Arc4* arc4, const byte* key, word32 length) 00028 { 00029 word32 i; 00030 word32 keyIndex = 0, stateIndex = 0; 00031 00032 arc4->x = 1; 00033 arc4->y = 0; 00034 00035 for (i = 0; i < ARC4_STATE_SIZE; i++) 00036 arc4->state[i] = i; 00037 00038 for (i = 0; i < ARC4_STATE_SIZE; i++) { 00039 word32 a = arc4->state[i]; 00040 stateIndex += key[keyIndex] + a; 00041 stateIndex &= 0xFF; 00042 arc4->state[i] = arc4->state[stateIndex]; 00043 arc4->state[stateIndex] = a; 00044 00045 if (++keyIndex >= length) 00046 keyIndex = 0; 00047 } 00048 } 00049 00050 00051 static INLINE word32 MakeByte(word32* x, word32* y, byte* s) 00052 { 00053 word32 a = s[*x], b; 00054 *y = (*y+a) & 0xff; 00055 00056 b = s[*y]; 00057 s[*x] = b; 00058 s[*y] = a; 00059 *x = (*x+1) & 0xff; 00060 00061 return s[(a+b) & 0xff]; 00062 } 00063 00064 00065 void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length) 00066 { 00067 word32 x = arc4->x; 00068 word32 y = arc4->y; 00069 00070 while(length--) 00071 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state); 00072 00073 arc4->x = x; 00074 arc4->y = y; 00075 } 00076
Generated on Tue Jul 12 2022 18:43:18 by 1.7.2