1151912Sphk/* crypto/evp/e_rc4.c */
2151912Sphk/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3209440Smav * All rights reserved.
4151912Sphk *
5151912Sphk * This package is an SSL implementation written
6151912Sphk * by Eric Young (eay@cryptsoft.com).
7151912Sphk * The implementation was written so as to conform with Netscapes SSL.
8151912Sphk *
9151912Sphk * This library is free for commercial and non-commercial use as long as
10151912Sphk * the following conditions are aheared to.  The following conditions
11151912Sphk * apply to all code found in this distribution, be it the RC4, RSA,
12151912Sphk * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13151912Sphk * included with this distribution is covered by the same copyright terms
14151912Sphk * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15151912Sphk *
16151912Sphk * Copyright remains Eric Young's, and as such any Copyright notices in
17151912Sphk * the code are not to be removed.
18151912Sphk * If this package is used in a product, Eric Young should be given attribution
19151912Sphk * as the author of the parts of the library used.
20151912Sphk * This can be in the form of a textual message at program startup or
21151912Sphk * in documentation (online or textual) provided with the package.
22151912Sphk *
23151912Sphk * Redistribution and use in source and binary forms, with or without
24151912Sphk * modification, are permitted provided that the following conditions
25151912Sphk * are met:
26151912Sphk * 1. Redistributions of source code must retain the copyright
27151912Sphk *    notice, this list of conditions and the following disclaimer.
28151912Sphk * 2. Redistributions in binary form must reproduce the above copyright
29151912Sphk *    notice, this list of conditions and the following disclaimer in the
30151912Sphk *    documentation and/or other materials provided with the distribution.
31151912Sphk * 3. All advertising materials mentioning features or use of this software
32268351Smarcel *    must display the following acknowledgement:
33209371Smav *    "This product includes cryptographic software written by
34209371Smav *     Eric Young (eay@cryptsoft.com)"
35209371Smav *    The word 'cryptographic' can be left out if the rouines from the library
36209371Smav *    being used are not cryptographic related :-).
37151912Sphk * 4. If you include any Windows specific code (or a derivative thereof) from
38159217Snjl *    the apps directory (application code) you must include an acknowledgement:
39151912Sphk *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40151912Sphk *
41209371Smav * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42151912Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43151912Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44209371Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45209371Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46209371Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47151912Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48159217Snjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49193530Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50193530Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51193530Sjkim * SUCH DAMAGE.
52151912Sphk *
53175385Sjhb * The licence and distribution terms for any publically available version or
54151912Sphk * derivative of this code cannot be changed.  i.e. this code cannot simply be
55209371Smav * copied and put under another distribution licence
56209371Smav * [including the GNU Public Licence.]
57209371Smav */
58209371Smav
59203062Savg#include <stdio.h>
60240286Smav#include "cryptlib.h"
61203062Savg
62213302Smav#ifndef OPENSSL_NO_RC4
63232797Smav
64203062Savg# include <openssl/evp.h>
65151912Sphk# include <openssl/objects.h>
66151912Sphk# include <openssl/rc4.h>
67209371Smav# include "evp_locl.h"
68169574Stakawata
69151931Sscottl/* FIXME: surely this is available elsewhere? */
70151935Sscottl# define EVP_RC4_KEY_SIZE                16
71151931Sscottl
72151931Sscottltypedef struct {
73209371Smav    RC4_KEY ks;                 /* working key */
74151912Sphk} EVP_RC4_KEY;
75209371Smav
76209371Smav# define data(ctx) ((EVP_RC4_KEY *)(ctx)->cipher_data)
77209371Smav
78209371Smavstatic int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
79209440Smav                        const unsigned char *iv, int enc);
80212533Smavstatic int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
81212238Smav                      const unsigned char *in, unsigned int inl);
82159217Snjlstatic const EVP_CIPHER r4_cipher = {
83209371Smav    NID_rc4,
84209371Smav    1, EVP_RC4_KEY_SIZE, 0,
85151912Sphk    EVP_CIPH_VARIABLE_LENGTH,
86209371Smav    rc4_init_key,
87209440Smav    rc4_cipher,
88209371Smav    NULL,
89209371Smav    sizeof(EVP_RC4_KEY),
90209371Smav    NULL,
91209371Smav    NULL,
92209371Smav    NULL,
93209371Smav    NULL
94209371Smav};
95209371Smav
96212323Smavstatic const EVP_CIPHER r4_40_cipher = {
97212323Smav    NID_rc4_40,
98209371Smav    1, 5 /* 40 bit */ , 0,
99209371Smav    EVP_CIPH_VARIABLE_LENGTH,
100209371Smav    rc4_init_key,
101209371Smav    rc4_cipher,
102209371Smav    NULL,
103209371Smav    sizeof(EVP_RC4_KEY),
104209371Smav    NULL,
105212491Smav    NULL,
106209371Smav    NULL,
107209371Smav    NULL
108209371Smav};
109151912Sphk
110151912Sphkconst EVP_CIPHER *EVP_rc4(void)
111159217Snjl{
112209371Smav    return (&r4_cipher);
113151912Sphk}
114159217Snjl
115159217Snjlconst EVP_CIPHER *EVP_rc4_40(void)
116159217Snjl{
117151912Sphk    return (&r4_40_cipher);
118151912Sphk}
119209371Smav
120151912Sphkstatic int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
121151912Sphk                        const unsigned char *iv, int enc)
122175385Sjhb{
123151912Sphk    RC4_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key);
124151912Sphk    return 1;
125175361Sjhb}
126209371Smav
127175361Sjhbstatic int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
128175361Sjhb                      const unsigned char *in, unsigned int inl)
129175385Sjhb{
130175385Sjhb    RC4(&data(ctx)->ks, inl, in, out);
131209440Smav    return 1;
132209440Smav}
133209440Smav#endif
134209440Smav