Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 1 | /* |
Jim Lieb | 612822f | 2009-08-12 14:54:03 -0700 | [diff] [blame] | 2 | * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc. |
| 3 | * All rights reserved. |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
Jim Lieb | 612822f | 2009-08-12 14:54:03 -0700 | [diff] [blame] | 19 | * File: rc4.c |
| 20 | * |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 21 | * Purpose: |
| 22 | * |
| 23 | * Functions: |
| 24 | * |
| 25 | * Revision History: |
| 26 | * |
| 27 | * Author: Kyle Hsu |
| 28 | * |
| 29 | * Date: Sep 4, 2002 |
| 30 | * |
| 31 | */ |
| 32 | |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 33 | #include "rc4.h" |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 34 | |
Charles Clément | 2989e96 | 2010-06-05 15:13:47 -0700 | [diff] [blame] | 35 | void rc4_init(PRC4Ext pRC4, unsigned char *pbyKey, unsigned int cbKey_len) |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 36 | { |
Charles Clément | b6e95cd | 2010-06-02 09:52:01 -0700 | [diff] [blame] | 37 | unsigned int ust1, ust2; |
| 38 | unsigned int keyindex; |
| 39 | unsigned int stateindex; |
Charles Clément | 2989e96 | 2010-06-05 15:13:47 -0700 | [diff] [blame] | 40 | unsigned char *pbyst; |
Charles Clément | b6e95cd | 2010-06-02 09:52:01 -0700 | [diff] [blame] | 41 | unsigned int idx; |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 42 | |
| 43 | pbyst = pRC4->abystate; |
| 44 | pRC4->ux = 0; |
| 45 | pRC4->uy = 0; |
| 46 | for (idx = 0; idx < 256; idx++) |
Charles Clément | 3fc9b58 | 2010-06-24 11:02:27 -0700 | [diff] [blame] | 47 | pbyst[idx] = (unsigned char)idx; |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 48 | keyindex = 0; |
| 49 | stateindex = 0; |
| 50 | for (idx = 0; idx < 256; idx++) { |
| 51 | ust1 = pbyst[idx]; |
| 52 | stateindex = (stateindex + pbyKey[keyindex] + ust1) & 0xff; |
| 53 | ust2 = pbyst[stateindex]; |
Charles Clément | 3fc9b58 | 2010-06-24 11:02:27 -0700 | [diff] [blame] | 54 | pbyst[stateindex] = (unsigned char)ust1; |
| 55 | pbyst[idx] = (unsigned char)ust2; |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 56 | if (++keyindex >= cbKey_len) |
| 57 | keyindex = 0; |
| 58 | } |
| 59 | } |
| 60 | |
Charles Clément | b6e95cd | 2010-06-02 09:52:01 -0700 | [diff] [blame] | 61 | unsigned int rc4_byte(PRC4Ext pRC4) |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 62 | { |
Charles Clément | b6e95cd | 2010-06-02 09:52:01 -0700 | [diff] [blame] | 63 | unsigned int ux; |
| 64 | unsigned int uy; |
| 65 | unsigned int ustx, usty; |
Charles Clément | 2989e96 | 2010-06-05 15:13:47 -0700 | [diff] [blame] | 66 | unsigned char *pbyst; |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 67 | |
| 68 | pbyst = pRC4->abystate; |
| 69 | ux = (pRC4->ux + 1) & 0xff; |
| 70 | ustx = pbyst[ux]; |
| 71 | uy = (ustx + pRC4->uy) & 0xff; |
| 72 | usty = pbyst[uy]; |
| 73 | pRC4->ux = ux; |
| 74 | pRC4->uy = uy; |
Charles Clément | 3fc9b58 | 2010-06-24 11:02:27 -0700 | [diff] [blame] | 75 | pbyst[uy] = (unsigned char)ustx; |
| 76 | pbyst[ux] = (unsigned char)usty; |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 77 | |
| 78 | return pbyst[(ustx + usty) & 0xff]; |
| 79 | } |
| 80 | |
Charles Clément | 2989e96 | 2010-06-05 15:13:47 -0700 | [diff] [blame] | 81 | void rc4_encrypt(PRC4Ext pRC4, unsigned char *pbyDest, |
| 82 | unsigned char *pbySrc, unsigned int cbData_len) |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 83 | { |
Charles Clément | b6e95cd | 2010-06-02 09:52:01 -0700 | [diff] [blame] | 84 | unsigned int ii; |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 85 | for (ii = 0; ii < cbData_len; ii++) |
Charles Clément | 3fc9b58 | 2010-06-24 11:02:27 -0700 | [diff] [blame] | 86 | pbyDest[ii] = (unsigned char)(pbySrc[ii] ^ rc4_byte(pRC4)); |
Forest Bond | 5449c68 | 2009-04-25 10:30:44 -0400 | [diff] [blame] | 87 | } |