1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 
13 #ifndef OPENSSL_NO_RC4
14 
15 # include <openssl/evp.h>
16 # include <openssl/objects.h>
17 # include <openssl/rc4.h>
18 
19 # include "crypto/evp.h"
20 
21 typedef struct {
22     RC4_KEY ks;                 /* working key */
23 } EVP_RC4_KEY;
24 
25 # define data(ctx) ((EVP_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
26 
27 static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28                         const unsigned char *iv, int enc);
29 static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
30                       const unsigned char *in, size_t inl);
31 static const EVP_CIPHER r4_cipher = {
32     NID_rc4,
33     1, EVP_RC4_KEY_SIZE, 0,
34     EVP_CIPH_VARIABLE_LENGTH,
35     rc4_init_key,
36     rc4_cipher,
37     NULL,
38     sizeof(EVP_RC4_KEY),
39     NULL,
40     NULL,
41     NULL,
42     NULL
43 };
44 
45 static const EVP_CIPHER r4_40_cipher = {
46     NID_rc4_40,
47     1, 5 /* 40 bit */ , 0,
48     EVP_CIPH_VARIABLE_LENGTH,
49     rc4_init_key,
50     rc4_cipher,
51     NULL,
52     sizeof(EVP_RC4_KEY),
53     NULL,
54     NULL,
55     NULL,
56     NULL
57 };
58 
EVP_rc4(void)59 const EVP_CIPHER *EVP_rc4(void)
60 {
61     return &r4_cipher;
62 }
63 
EVP_rc4_40(void)64 const EVP_CIPHER *EVP_rc4_40(void)
65 {
66     return &r4_40_cipher;
67 }
68 
rc4_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)69 static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
70                         const unsigned char *iv, int enc)
71 {
72     RC4_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key);
73     return 1;
74 }
75 
rc4_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)76 static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
77                       const unsigned char *in, size_t inl)
78 {
79     RC4(&data(ctx)->ks, inl, in, out);
80     return 1;
81 }
82 #endif
83