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