OpenCores
URL https://opencores.org/ocsvn/tv80/tv80/trunk

Subversion Repositories tv80

[/] [tv80/] [trunk/] [tests/] [rc4.c] - Blame information for rev 73

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 73 ghutchis
/*
2
 * rc4.c
3
 *
4
 * Copyright (c) 1996-2000 Whistle Communications, Inc.
5
 * All rights reserved.
6
 *
7
 * Subject to the following obligations and disclaimer of warranty, use and
8
 * redistribution of this software, in source or object code forms, with or
9
 * without modifications are expressly permitted by Whistle Communications;
10
 * provided, however, that:
11
 * 1. Any and all reproductions of the source or object code must include the
12
 *    copyright notice above and the following disclaimer of warranties; and
13
 * 2. No rights are granted, in any manner or form, to use Whistle
14
 *    Communications, Inc. trademarks, including the mark "WHISTLE
15
 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16
 *    such appears in the above copyright notice or in the software.
17
 *
18
 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19
 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20
 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21
 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23
 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24
 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25
 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26
 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27
 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28
 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29
 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30
 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33
 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34
 * OF SUCH DAMAGE.
35
 */
36
 
37
#include <rc4.h>
38
 
39
void swap_bytes(uint8_t *a, uint8_t *b)
40
{
41
        uint8_t temp;
42
 
43
        temp = *a;
44
        *a = *b;
45
        *b = temp;
46
}
47
 
48
/*
49
 * Initialize an RC4 state buffer using the supplied key,
50
 * which can have arbitrary length.
51
 */
52
void rc4_init(struct rc4_state *const state, const uint8_t *key, int keylen)
53
{
54
        uint8_t j;
55
        int i;
56
 
57
        /* Initialize state with identity permutation */
58
        for (i = 0; i < 256; i++)
59
                state->perm[i] = (uint8_t)i;
60
        state->index1 = 0;
61
        state->index2 = 0;
62
 
63
        /* Randomize the permutation using key data */
64
        for (j = i = 0; i < 256; i++) {
65
                j += state->perm[i] + key[i % keylen];
66
                swap_bytes(&state->perm[i], &state->perm[j]);
67
        }
68
}
69
 
70
/*
71
 * Encrypt some data using the supplied RC4 state buffer.
72
 * The input and output buffers may be the same buffer.
73
 * Since RC4 is a stream cypher, this function is used
74
 * for both encryption and decryption.
75
 */
76
void rc4_crypt(struct rc4_state *const state,
77
        const uint8_t *inbuf, uint8_t *outbuf, int buflen)
78
{
79
        int i;
80
        uint8_t j;
81
 
82
        for (i = 0; i < buflen; i++) {
83
 
84
                /* Update modification indicies */
85
                state->index1++;
86
                state->index2 += state->perm[state->index1];
87
 
88
                /* Modify permutation */
89
                swap_bytes(&state->perm[state->index1],
90
                    &state->perm[state->index2]);
91
 
92
                /* Encrypt/decrypt next byte */
93
                j = state->perm[state->index1] + state->perm[state->index2];
94
                outbuf[i] = inbuf[i] ^ state->perm[j];
95
        }
96
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.