blob: 5c3c2d0552b49f6ba51549b911208f966ea00c8d [file] [log] [blame]
Forest Bond92b96792009-06-13 07:38:31 -04001/*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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 *
19 * File: rc4.c
20 *
21 * Purpose:
22 *
23 * Functions:
24 *
25 * Revision History:
26 *
27 * Author: Kyle Hsu
28 *
29 * Date: Sep 4, 2002
30 *
31 */
32
Forest Bond92b96792009-06-13 07:38:31 -040033#include "rc4.h"
Forest Bond92b96792009-06-13 07:38:31 -040034
Andres Morecc856e62010-05-17 21:34:01 -030035void rc4_init(PRC4Ext pRC4, PBYTE pbyKey, unsigned int cbKey_len)
Forest Bond92b96792009-06-13 07:38:31 -040036{
Andres Morecc856e62010-05-17 21:34:01 -030037 unsigned int ust1, ust2;
38 unsigned int keyindex;
39 unsigned int stateindex;
Daniel Kenji Toyama69d5b492010-03-29 22:26:46 +110040 PBYTE pbyst;
Andres Morecc856e62010-05-17 21:34:01 -030041 unsigned int idx;
Forest Bond92b96792009-06-13 07:38:31 -040042
Daniel Kenji Toyama69d5b492010-03-29 22:26:46 +110043 pbyst = pRC4->abystate;
44 pRC4->ux = 0;
45 pRC4->uy = 0;
46 for (idx = 0; idx < 256; idx++)
47 pbyst[idx] = (BYTE)idx;
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];
54 pbyst[stateindex] = (BYTE)ust1;
55 pbyst[idx] = (BYTE)ust2;
56 if (++keyindex >= cbKey_len)
57 keyindex = 0;
58 }
Forest Bond92b96792009-06-13 07:38:31 -040059}
60
Andres Morecc856e62010-05-17 21:34:01 -030061unsigned int rc4_byte(PRC4Ext pRC4)
Forest Bond92b96792009-06-13 07:38:31 -040062{
Andres Morecc856e62010-05-17 21:34:01 -030063 unsigned int ux;
64 unsigned int uy;
65 unsigned int ustx, usty;
Daniel Kenji Toyama69d5b492010-03-29 22:26:46 +110066 PBYTE pbyst;
Forest Bond92b96792009-06-13 07:38:31 -040067
Daniel Kenji Toyama69d5b492010-03-29 22:26:46 +110068 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;
75 pbyst[uy] = (BYTE)ustx;
76 pbyst[ux] = (BYTE)usty;
Forest Bond92b96792009-06-13 07:38:31 -040077
Daniel Kenji Toyama69d5b492010-03-29 22:26:46 +110078 return pbyst[(ustx + usty) & 0xff];
Forest Bond92b96792009-06-13 07:38:31 -040079}
80
Andres More8611a292010-05-01 14:25:00 -030081void rc4_encrypt(PRC4Ext pRC4, PBYTE pbyDest,
Andres Morecc856e62010-05-17 21:34:01 -030082 PBYTE pbySrc, unsigned int cbData_len)
Forest Bond92b96792009-06-13 07:38:31 -040083{
Andres Morecc856e62010-05-17 21:34:01 -030084 unsigned int ii;
Daniel Kenji Toyama69d5b492010-03-29 22:26:46 +110085 for (ii = 0; ii < cbData_len; ii++)
86 pbyDest[ii] = (BYTE)(pbySrc[ii] ^ rc4_byte(pRC4));
Forest Bond92b96792009-06-13 07:38:31 -040087}