Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
rc4.c
00001 /* 00002 * Copyright (c) 2007, Cameron Rich 00003 * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright notice, 00010 * this list of conditions and the following disclaimer. 00011 * * Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * * Neither the name of the axTLS project nor the names of its contributors 00015 * may be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00026 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 /** 00032 * An implementation of the RC4/ARC4 algorithm. 00033 * Originally written by Christophe Devine. 00034 */ 00035 00036 #include <string.h> 00037 #include "os_port.h" 00038 #include "crypto.h " 00039 00040 /** 00041 * Get ready for an encrypt/decrypt operation 00042 */ 00043 void RC4_setup(RC4_CTX *ctx, const uint8_t *key, int length) 00044 { 00045 int i, j = 0, k = 0, a; 00046 uint8_t *m; 00047 00048 ctx->x = 0; 00049 ctx->y = 0; 00050 m = ctx->m; 00051 00052 for (i = 0; i < 256; i++) 00053 m[i] = i; 00054 00055 for (i = 0; i < 256; i++) 00056 { 00057 a = m[i]; 00058 j = (uint8_t)(j + a + key[k]); 00059 m[i] = m[j]; 00060 m[j] = a; 00061 00062 if (++k >= length) 00063 k = 0; 00064 } 00065 } 00066 00067 /** 00068 * Perform the encrypt/decrypt operation (can use it for either since 00069 * this is a stream cipher). 00070 * NOTE: *msg and *out must be the same pointer (performance tweak) 00071 */ 00072 void RC4_crypt(RC4_CTX *ctx, const uint8_t *msg, uint8_t *out, int length) 00073 { 00074 int i; 00075 uint8_t *m, x, y, a, b; 00076 00077 x = ctx->x; 00078 y = ctx->y; 00079 m = ctx->m; 00080 00081 for (i = 0; i < length; i++) 00082 { 00083 a = m[++x]; 00084 y += a; 00085 m[x] = b = m[y]; 00086 m[y] = a; 00087 out[i] ^= m[(uint8_t)(a + b)]; 00088 } 00089 00090 ctx->x = x; 00091 ctx->y = y; 00092 }
Generated on Tue Jul 12 2022 13:05:27 by
1.7.2