11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci/*
111cb0ef41Sopenharmony_ci * RC4 low level APIs are deprecated for public use, but still ok for internal
121cb0ef41Sopenharmony_ci * use.
131cb0ef41Sopenharmony_ci */
141cb0ef41Sopenharmony_ci#include "internal/deprecated.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci#include <stdio.h>
171cb0ef41Sopenharmony_ci#include "internal/cryptlib.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_RC4
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci# include <openssl/evp.h>
221cb0ef41Sopenharmony_ci# include <openssl/objects.h>
231cb0ef41Sopenharmony_ci# include <openssl/rc4.h>
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci# include "crypto/evp.h"
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_citypedef struct {
281cb0ef41Sopenharmony_ci    RC4_KEY ks;                 /* working key */
291cb0ef41Sopenharmony_ci} EVP_RC4_KEY;
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci# define data(ctx) ((EVP_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_cistatic int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
341cb0ef41Sopenharmony_ci                        const unsigned char *iv, int enc);
351cb0ef41Sopenharmony_cistatic int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
361cb0ef41Sopenharmony_ci                      const unsigned char *in, size_t inl);
371cb0ef41Sopenharmony_cistatic const EVP_CIPHER r4_cipher = {
381cb0ef41Sopenharmony_ci    NID_rc4,
391cb0ef41Sopenharmony_ci    1, EVP_RC4_KEY_SIZE, 0,
401cb0ef41Sopenharmony_ci    EVP_CIPH_VARIABLE_LENGTH,
411cb0ef41Sopenharmony_ci    EVP_ORIG_GLOBAL,
421cb0ef41Sopenharmony_ci    rc4_init_key,
431cb0ef41Sopenharmony_ci    rc4_cipher,
441cb0ef41Sopenharmony_ci    NULL,
451cb0ef41Sopenharmony_ci    sizeof(EVP_RC4_KEY),
461cb0ef41Sopenharmony_ci    NULL,
471cb0ef41Sopenharmony_ci    NULL,
481cb0ef41Sopenharmony_ci    NULL,
491cb0ef41Sopenharmony_ci    NULL
501cb0ef41Sopenharmony_ci};
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_cistatic const EVP_CIPHER r4_40_cipher = {
531cb0ef41Sopenharmony_ci    NID_rc4_40,
541cb0ef41Sopenharmony_ci    1, 5 /* 40 bit */ , 0,
551cb0ef41Sopenharmony_ci    EVP_CIPH_VARIABLE_LENGTH,
561cb0ef41Sopenharmony_ci    EVP_ORIG_GLOBAL,
571cb0ef41Sopenharmony_ci    rc4_init_key,
581cb0ef41Sopenharmony_ci    rc4_cipher,
591cb0ef41Sopenharmony_ci    NULL,
601cb0ef41Sopenharmony_ci    sizeof(EVP_RC4_KEY),
611cb0ef41Sopenharmony_ci    NULL,
621cb0ef41Sopenharmony_ci    NULL,
631cb0ef41Sopenharmony_ci    NULL,
641cb0ef41Sopenharmony_ci    NULL
651cb0ef41Sopenharmony_ci};
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciconst EVP_CIPHER *EVP_rc4(void)
681cb0ef41Sopenharmony_ci{
691cb0ef41Sopenharmony_ci    return &r4_cipher;
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciconst EVP_CIPHER *EVP_rc4_40(void)
731cb0ef41Sopenharmony_ci{
741cb0ef41Sopenharmony_ci    return &r4_40_cipher;
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cistatic int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
781cb0ef41Sopenharmony_ci                        const unsigned char *iv, int enc)
791cb0ef41Sopenharmony_ci{
801cb0ef41Sopenharmony_ci    int keylen;
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) <= 0)
831cb0ef41Sopenharmony_ci        return 0;
841cb0ef41Sopenharmony_ci    RC4_set_key(&data(ctx)->ks, keylen, key);
851cb0ef41Sopenharmony_ci    return 1;
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cistatic int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
891cb0ef41Sopenharmony_ci                      const unsigned char *in, size_t inl)
901cb0ef41Sopenharmony_ci{
911cb0ef41Sopenharmony_ci    RC4(&data(ctx)->ks, inl, in, out);
921cb0ef41Sopenharmony_ci    return 1;
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci#endif
95