Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/rc2/rc2_cbc.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
 * RC2 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/rc2.h>
17
#include "rc2_local.h"
18
19
void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
20
                     RC2_KEY *ks, unsigned char *iv, int encrypt)
21
0
{
22
0
    register unsigned long tin0, tin1;
23
0
    register unsigned long tout0, tout1, xor0, xor1;
24
0
    register long l = length;
25
0
    unsigned long tin[2];
26
27
0
    if (encrypt) {
28
0
        c2l(iv, tout0);
29
0
        c2l(iv, tout1);
30
0
        iv -= 8;
31
0
        for (l -= 8; l >= 0; l -= 8) {
32
0
            c2l(in, tin0);
33
0
            c2l(in, tin1);
34
0
            tin0 ^= tout0;
35
0
            tin1 ^= tout1;
36
0
            tin[0] = tin0;
37
0
            tin[1] = tin1;
38
0
            RC2_encrypt(tin, ks);
39
0
            tout0 = tin[0];
40
0
            l2c(tout0, out);
41
0
            tout1 = tin[1];
42
0
            l2c(tout1, out);
43
0
        }
44
0
        if (l != -8) {
45
0
            c2ln(in, tin0, tin1, l + 8);
46
0
            tin0 ^= tout0;
47
0
            tin1 ^= tout1;
48
0
            tin[0] = tin0;
49
0
            tin[1] = tin1;
50
0
            RC2_encrypt(tin, ks);
51
0
            tout0 = tin[0];
52
0
            l2c(tout0, out);
53
0
            tout1 = tin[1];
54
0
            l2c(tout1, out);
55
0
        }
56
0
        l2c(tout0, iv);
57
0
        l2c(tout1, iv);
58
0
    } else {
59
0
        c2l(iv, xor0);
60
0
        c2l(iv, xor1);
61
0
        iv -= 8;
62
0
        for (l -= 8; l >= 0; l -= 8) {
63
0
            c2l(in, tin0);
64
0
            tin[0] = tin0;
65
0
            c2l(in, tin1);
66
0
            tin[1] = tin1;
67
0
            RC2_decrypt(tin, ks);
68
0
            tout0 = tin[0] ^ xor0;
69
0
            tout1 = tin[1] ^ xor1;
70
0
            l2c(tout0, out);
71
0
            l2c(tout1, out);
72
0
            xor0 = tin0;
73
0
            xor1 = tin1;
74
0
        }
75
0
        if (l != -8) {
76
0
            c2l(in, tin0);
77
0
            tin[0] = tin0;
78
0
            c2l(in, tin1);
79
0
            tin[1] = tin1;
80
0
            RC2_decrypt(tin, ks);
81
0
            tout0 = tin[0] ^ xor0;
82
0
            tout1 = tin[1] ^ xor1;
83
0
            l2cn(tout0, tout1, out, l + 8);
84
0
            xor0 = tin0;
85
0
            xor1 = tin1;
86
0
        }
87
0
        l2c(xor0, iv);
88
0
        l2c(xor1, iv);
89
0
    }
90
0
    tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
91
0
    tin[0] = tin[1] = 0;
92
0
}
93
94
void RC2_encrypt(unsigned long *d, RC2_KEY *key)
95
0
{
96
0
    int i, n;
97
0
    register RC2_INT *p0, *p1;
98
0
    register RC2_INT x0, x1, x2, x3, t;
99
0
    unsigned long l;
100
101
0
    l = d[0];
102
0
    x0 = (RC2_INT) l & 0xffff;
103
0
    x1 = (RC2_INT) (l >> 16L);
104
0
    l = d[1];
105
0
    x2 = (RC2_INT) l & 0xffff;
106
0
    x3 = (RC2_INT) (l >> 16L);
107
108
0
    n = 3;
109
0
    i = 5;
110
111
0
    p0 = p1 = &(key->data[0]);
112
0
    for (;;) {
113
0
        t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff;
114
0
        x0 = (t << 1) | (t >> 15);
115
0
        t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff;
116
0
        x1 = (t << 2) | (t >> 14);
117
0
        t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff;
118
0
        x2 = (t << 3) | (t >> 13);
119
0
        t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff;
120
0
        x3 = (t << 5) | (t >> 11);
121
122
0
        if (--i == 0) {
123
0
            if (--n == 0)
124
0
                break;
125
0
            i = (n == 2) ? 6 : 5;
126
127
0
            x0 += p1[x3 & 0x3f];
128
0
            x1 += p1[x0 & 0x3f];
129
0
            x2 += p1[x1 & 0x3f];
130
0
            x3 += p1[x2 & 0x3f];
131
0
        }
132
0
    }
133
134
0
    d[0] =
135
0
        (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
136
0
    d[1] =
137
0
        (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
138
0
}
139
140
void RC2_decrypt(unsigned long *d, RC2_KEY *key)
141
0
{
142
0
    int i, n;
143
0
    register RC2_INT *p0, *p1;
144
0
    register RC2_INT x0, x1, x2, x3, t;
145
0
    unsigned long l;
146
147
0
    l = d[0];
148
0
    x0 = (RC2_INT) l & 0xffff;
149
0
    x1 = (RC2_INT) (l >> 16L);
150
0
    l = d[1];
151
0
    x2 = (RC2_INT) l & 0xffff;
152
0
    x3 = (RC2_INT) (l >> 16L);
153
154
0
    n = 3;
155
0
    i = 5;
156
157
0
    p0 = &(key->data[63]);
158
0
    p1 = &(key->data[0]);
159
0
    for (;;) {
160
0
        t = ((x3 << 11) | (x3 >> 5)) & 0xffff;
161
0
        x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff;
162
0
        t = ((x2 << 13) | (x2 >> 3)) & 0xffff;
163
0
        x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff;
164
0
        t = ((x1 << 14) | (x1 >> 2)) & 0xffff;
165
0
        x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff;
166
0
        t = ((x0 << 15) | (x0 >> 1)) & 0xffff;
167
0
        x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff;
168
169
0
        if (--i == 0) {
170
0
            if (--n == 0)
171
0
                break;
172
0
            i = (n == 2) ? 6 : 5;
173
174
0
            x3 = (x3 - p1[x2 & 0x3f]) & 0xffff;
175
0
            x2 = (x2 - p1[x1 & 0x3f]) & 0xffff;
176
0
            x1 = (x1 - p1[x0 & 0x3f]) & 0xffff;
177
0
            x0 = (x0 - p1[x3 & 0x3f]) & 0xffff;
178
0
        }
179
0
    }
180
181
0
    d[0] =
182
0
        (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
183
0
    d[1] =
184
0
        (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
185
0
}