Coverage Report

Created: 2025-06-22 06:56

/src/openssl/crypto/rc4/rc4_enc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2020 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 <openssl/rc4.h>
17
#include "rc4_local.h"
18
19
/*-
20
 * RC4 as implemented from a posting from
21
 * Newsgroups: sci.crypt
22
 * Subject: RC4 Algorithm revealed.
23
 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
24
 * Date: Wed, 14 Sep 1994 06:35:31 GMT
25
 */
26
27
void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
28
         unsigned char *outdata)
29
0
{
30
0
    register RC4_INT *d;
31
0
    register RC4_INT x, y, tx, ty;
32
0
    size_t i;
33
34
0
    x = key->x;
35
0
    y = key->y;
36
0
    d = key->data;
37
38
0
#define LOOP(in,out) \
39
0
                x=((x+1)&0xff); \
40
0
                tx=d[x]; \
41
0
                y=(tx+y)&0xff; \
42
0
                d[x]=ty=d[y]; \
43
0
                d[y]=tx; \
44
0
                (out) = d[(tx+ty)&0xff]^ (in);
45
46
0
    i = len >> 3;
47
0
    if (i) {
48
0
        for (;;) {
49
0
            LOOP(indata[0], outdata[0]);
50
0
            LOOP(indata[1], outdata[1]);
51
0
            LOOP(indata[2], outdata[2]);
52
0
            LOOP(indata[3], outdata[3]);
53
0
            LOOP(indata[4], outdata[4]);
54
0
            LOOP(indata[5], outdata[5]);
55
0
            LOOP(indata[6], outdata[6]);
56
0
            LOOP(indata[7], outdata[7]);
57
0
            indata += 8;
58
0
            outdata += 8;
59
0
            if (--i == 0)
60
0
                break;
61
0
        }
62
0
    }
63
0
    i = len & 0x07;
64
0
    if (i) {
65
0
        for (;;) {
66
0
            LOOP(indata[0], outdata[0]);
67
0
            if (--i == 0)
68
0
                break;
69
0
            LOOP(indata[1], outdata[1]);
70
0
            if (--i == 0)
71
0
                break;
72
0
            LOOP(indata[2], outdata[2]);
73
0
            if (--i == 0)
74
0
                break;
75
0
            LOOP(indata[3], outdata[3]);
76
0
            if (--i == 0)
77
0
                break;
78
0
            LOOP(indata[4], outdata[4]);
79
0
            if (--i == 0)
80
0
                break;
81
0
            LOOP(indata[5], outdata[5]);
82
0
            if (--i == 0)
83
0
                break;
84
0
            LOOP(indata[6], outdata[6]);
85
0
            if (--i == 0)
86
0
                break;
87
0
        }
88
0
    }
89
0
    key->x = x;
90
0
    key->y = y;
91
0
}