xref: /third_party/openssl/crypto/rc4/rc4_skey.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 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 <openssl/rc4.h>
17e1051a39Sopenharmony_ci#include "rc4_local.h"
18e1051a39Sopenharmony_ci#include <openssl/opensslv.h>
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ciconst char *RC4_options(void)
21e1051a39Sopenharmony_ci{
22e1051a39Sopenharmony_ci    if (sizeof(RC4_INT) == 1)
23e1051a39Sopenharmony_ci        return "rc4(char)";
24e1051a39Sopenharmony_ci    else
25e1051a39Sopenharmony_ci        return "rc4(int)";
26e1051a39Sopenharmony_ci}
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci/*-
29e1051a39Sopenharmony_ci * RC4 as implemented from a posting from
30e1051a39Sopenharmony_ci * Newsgroups: sci.crypt
31e1051a39Sopenharmony_ci * Subject: RC4 Algorithm revealed.
32e1051a39Sopenharmony_ci * Message-ID: <sternCvKL4B.Hyy@netcom.com>
33e1051a39Sopenharmony_ci * Date: Wed, 14 Sep 1994 06:35:31 GMT
34e1051a39Sopenharmony_ci */
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_civoid RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
37e1051a39Sopenharmony_ci{
38e1051a39Sopenharmony_ci    register RC4_INT tmp;
39e1051a39Sopenharmony_ci    register int id1, id2;
40e1051a39Sopenharmony_ci    register RC4_INT *d;
41e1051a39Sopenharmony_ci    unsigned int i;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    d = &(key->data[0]);
44e1051a39Sopenharmony_ci    key->x = 0;
45e1051a39Sopenharmony_ci    key->y = 0;
46e1051a39Sopenharmony_ci    id1 = id2 = 0;
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci#define SK_LOOP(d,n) { \
49e1051a39Sopenharmony_ci                tmp=d[(n)]; \
50e1051a39Sopenharmony_ci                id2 = (data[id1] + tmp + id2) & 0xff; \
51e1051a39Sopenharmony_ci                if (++id1 == len) id1=0; \
52e1051a39Sopenharmony_ci                d[(n)]=d[id2]; \
53e1051a39Sopenharmony_ci                d[id2]=tmp; }
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci    for (i = 0; i < 256; i++)
56e1051a39Sopenharmony_ci        d[i] = i;
57e1051a39Sopenharmony_ci    for (i = 0; i < 256; i += 4) {
58e1051a39Sopenharmony_ci        SK_LOOP(d, i + 0);
59e1051a39Sopenharmony_ci        SK_LOOP(d, i + 1);
60e1051a39Sopenharmony_ci        SK_LOOP(d, i + 2);
61e1051a39Sopenharmony_ci        SK_LOOP(d, i + 3);
62e1051a39Sopenharmony_ci    }
63e1051a39Sopenharmony_ci}
64