1 /*
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 /*
11 * RC4 low level APIs are deprecated for public use, but still ok for internal
12 * use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18
19 #ifndef OPENSSL_NO_RC4
20
21 # include <openssl/evp.h>
22 # include <openssl/objects.h>
23 # include <openssl/rc4.h>
24
25 # include "crypto/evp.h"
26
27 typedef struct {
28 RC4_KEY ks; /* working key */
29 } EVP_RC4_KEY;
30
31 # define data(ctx) ((EVP_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
32
33 static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
34 const unsigned char *iv, int enc);
35 static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
36 const unsigned char *in, size_t inl);
37 static const EVP_CIPHER r4_cipher = {
38 NID_rc4,
39 1, EVP_RC4_KEY_SIZE, 0,
40 EVP_CIPH_VARIABLE_LENGTH,
41 EVP_ORIG_GLOBAL,
42 rc4_init_key,
43 rc4_cipher,
44 NULL,
45 sizeof(EVP_RC4_KEY),
46 NULL,
47 NULL,
48 NULL,
49 NULL
50 };
51
52 static const EVP_CIPHER r4_40_cipher = {
53 NID_rc4_40,
54 1, 5 /* 40 bit */ , 0,
55 EVP_CIPH_VARIABLE_LENGTH,
56 EVP_ORIG_GLOBAL,
57 rc4_init_key,
58 rc4_cipher,
59 NULL,
60 sizeof(EVP_RC4_KEY),
61 NULL,
62 NULL,
63 NULL,
64 NULL
65 };
66
EVP_rc4(void)67 const EVP_CIPHER *EVP_rc4(void)
68 {
69 return &r4_cipher;
70 }
71
EVP_rc4_40(void)72 const EVP_CIPHER *EVP_rc4_40(void)
73 {
74 return &r4_40_cipher;
75 }
76
rc4_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)77 static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
78 const unsigned char *iv, int enc)
79 {
80 int keylen;
81
82 if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) <= 0)
83 return 0;
84 RC4_set_key(&data(ctx)->ks, keylen, key);
85 return 1;
86 }
87
rc4_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,size_t inl)88 static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
89 const unsigned char *in, size_t inl)
90 {
91 RC4(&data(ctx)->ks, inl, in, out);
92 return 1;
93 }
94 #endif
95