Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenrc4.c
Go to the documentation of this file.
00001 /* 00002 * dlls/rsaenh/rc4.c 00003 * RC4 functions 00004 * 00005 * Copyright 2004 Michael Jung 00006 * Based on public domain code by Tom St Denis (tomstdenis@iahu.ca) 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 00021 */ 00022 00023 /* 00024 * This file contains code from the LibTomCrypt cryptographic 00025 * library written by Tom St Denis (tomstdenis@iahu.ca). LibTomCrypt 00026 * is in the public domain. The code in this file is tailored to 00027 * special requirements. Take a look at http://libtomcrypt.org for the 00028 * original version. 00029 */ 00030 00031 #include "tomcrypt.h" 00032 00033 int rc4_start(prng_state *prng) 00034 { 00035 /* set keysize to zero */ 00036 prng->rc4.x = 0; 00037 00038 return CRYPT_OK; 00039 } 00040 00041 int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng) 00042 { 00043 /* trim as required */ 00044 if (prng->rc4.x + len > 256) { 00045 if (prng->rc4.x == 256) { 00046 /* I can't possibly accept another byte, ok maybe a mint wafer... */ 00047 return CRYPT_OK; 00048 } else { 00049 /* only accept part of it */ 00050 len = 256 - prng->rc4.x; 00051 } 00052 } 00053 00054 while (len--) { 00055 prng->rc4.buf[prng->rc4.x++] = *buf++; 00056 } 00057 00058 return CRYPT_OK; 00059 } 00060 00061 int rc4_ready(prng_state *prng) 00062 { 00063 unsigned char key[256], tmp, *s; 00064 int keylen, x, y, j; 00065 00066 /* extract the key */ 00067 s = prng->rc4.buf; 00068 memcpy(key, s, 256); 00069 keylen = prng->rc4.x; 00070 00071 /* make RC4 perm and shuffle */ 00072 for (x = 0; x < 256; x++) { 00073 s[x] = x; 00074 } 00075 00076 for (j = x = y = 0; x < 256; x++) { 00077 y = (y + prng->rc4.buf[x] + key[j++]) & 255; 00078 if (j == keylen) { 00079 j = 0; 00080 } 00081 tmp = s[x]; s[x] = s[y]; s[y] = tmp; 00082 } 00083 prng->rc4.x = 0; 00084 prng->rc4.y = 0; 00085 00086 return CRYPT_OK; 00087 } 00088 00089 unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng) 00090 { 00091 unsigned char x, y, *s, tmp; 00092 unsigned long n; 00093 00094 n = len; 00095 x = prng->rc4.x; 00096 y = prng->rc4.y; 00097 s = prng->rc4.buf; 00098 while (len--) { 00099 x = (x + 1) & 255; 00100 y = (y + s[x]) & 255; 00101 tmp = s[x]; s[x] = s[y]; s[y] = tmp; 00102 tmp = (s[x] + s[y]) & 255; 00103 *buf++ ^= s[tmp]; 00104 } 00105 prng->rc4.x = x; 00106 prng->rc4.y = y; 00107 return n; 00108 } Generated on Mon May 28 2012 04:25:47 for ReactOS by
1.7.6.1
|