Coverage Report

Created: 2018-08-29 13:53

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