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

Subversion Repositories tv80

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

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 77 ghutchis
  /*
42
  _asm
43
    ld    d,4(ix)
44
    ld    e,5(ix)
45
    ld    h,6(ix)
46
    ld    l,7(ix)
47
    ld    a, (de)
48
    ld    b, a
49
    ld    a, (hl)
50
    ld    (de), a
51
    ld    (hl), b
52
  _endasm;
53
  */
54 73 ghutchis
        uint8_t temp;
55
 
56
        temp = *a;
57
        *a = *b;
58
        *b = temp;
59
}
60
 
61
/*
62
 * Initialize an RC4 state buffer using the supplied key,
63
 * which can have arbitrary length.
64
 */
65
void rc4_init(struct rc4_state *const state, const uint8_t *key, int keylen)
66
{
67
        uint8_t j;
68
        int i;
69
 
70
        /* Initialize state with identity permutation */
71 77 ghutchis
#ifndef ORIGINAL_C
72
    _asm
73
    ld    d, 4(ix)
74
    ld    e, 5(ix)
75
    ld    b, #255
76
state_init:
77
    ld    h, #0
78
    ld    l, b
79
    add   hl, de
80
    ld    (hl), b
81
    djnz  state_init
82
    ex    de, hl
83
    ld    (hl), #0
84
    ex    de, hl
85
    ld    l, #0
86
    ld    h, #1
87
    add   hl, de
88
    ld    (hl), #0
89
    inc   hl
90
    ld    (hl), #0
91
    _endasm;
92
#else
93 73 ghutchis
        for (i = 0; i < 256; i++)
94
                state->perm[i] = (uint8_t)i;
95
        state->index1 = 0;
96
        state->index2 = 0;
97 77 ghutchis
#endif
98
 
99 73 ghutchis
        /* Randomize the permutation using key data */
100
        for (j = i = 0; i < 256; i++) {
101 77 ghutchis
          uint8_t tmp;
102
          j += state->perm[i] + key[i % keylen];
103
          tmp = state->perm[i];
104
          state->perm[i] = state->perm[j];
105
          state->perm[j] = tmp;
106
          //swap_bytes(&state->perm[i], &state->perm[j]);
107 73 ghutchis
        }
108
}
109
 
110
/*
111
 * Encrypt some data using the supplied RC4 state buffer.
112
 * The input and output buffers may be the same buffer.
113
 * Since RC4 is a stream cypher, this function is used
114
 * for both encryption and decryption.
115
 */
116
void rc4_crypt(struct rc4_state *const state,
117
        const uint8_t *inbuf, uint8_t *outbuf, int buflen)
118
{
119
        int i;
120
        uint8_t j;
121
 
122
        for (i = 0; i < buflen; i++) {
123
 
124
                /* Update modification indicies */
125
                state->index1++;
126
                state->index2 += state->perm[state->index1];
127
 
128
                /* Modify permutation */
129
                swap_bytes(&state->perm[state->index1],
130
                    &state->perm[state->index2]);
131
 
132
                /* Encrypt/decrypt next byte */
133
                j = state->perm[state->index1] + state->perm[state->index2];
134
                outbuf[i] = inbuf[i] ^ state->perm[j];
135
        }
136
}

powered by: WebSVN 2.1.0

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