Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/des/cfb_enc.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2022 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
 * DES low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include "internal/e_os.h"
17
#include "des_local.h"
18
#include <assert.h>
19
20
/*
21
 * The input and output are loaded in multiples of 8 bits. What this means is
22
 * that if you hame numbits=12 and length=2 the first 12 bits will be
23
 * retrieved from the first byte and half the second.  The second 12 bits
24
 * will come from the 3rd and half the 4th byte.
25
 */
26
/*
27
 * Until Aug 1 2003 this function did not correctly implement CFB-r, so it
28
 * will not be compatible with any encryption prior to that date. Ben.
29
 */
30
void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
31
    long length, DES_key_schedule *schedule,
32
    DES_cblock *ivec, int enc)
33
0
{
34
0
    register DES_LONG d0, d1, v0, v1;
35
0
    register unsigned long l = length;
36
0
    register int num = numbits / 8, n = (numbits + 7) / 8, i, rem = numbits % 8;
37
0
    DES_LONG ti[2];
38
0
    unsigned char *iv;
39
#ifndef L_ENDIAN
40
    unsigned char ovec[16];
41
#else
42
0
    unsigned int sh[4];
43
0
    unsigned char *ovec = (unsigned char *)sh;
44
45
    /* I kind of count that compiler optimizes away this assertion, */
46
0
    assert(sizeof(sh[0]) == 4); /* as this holds true for all, */
47
    /* but 16-bit platforms...      */
48
49
0
#endif
50
51
0
    if (numbits <= 0 || numbits > 64)
52
0
        return;
53
0
    iv = &(*ivec)[0];
54
0
    c2l(iv, v0);
55
0
    c2l(iv, v1);
56
0
    if (enc) {
57
0
        while (l >= (unsigned long)n) {
58
0
            l -= n;
59
0
            ti[0] = v0;
60
0
            ti[1] = v1;
61
0
            DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT);
62
0
            c2ln(in, d0, d1, n);
63
0
            in += n;
64
0
            d0 ^= ti[0];
65
0
            d1 ^= ti[1];
66
0
            l2cn(d0, d1, out, n);
67
0
            out += n;
68
            /*
69
             * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
70
             * gcc :-(
71
             */
72
0
            if (numbits == 32) {
73
0
                v0 = v1;
74
0
                v1 = d0;
75
0
            } else if (numbits == 64) {
76
0
                v0 = d0;
77
0
                v1 = d1;
78
0
            } else {
79
#ifndef L_ENDIAN
80
                iv = &ovec[0];
81
                l2c(v0, iv);
82
                l2c(v1, iv);
83
                l2c(d0, iv);
84
                l2c(d1, iv);
85
#else
86
0
                sh[0] = v0, sh[1] = v1, sh[2] = d0, sh[3] = d1;
87
0
#endif
88
0
                if (rem == 0)
89
0
                    memmove(ovec, ovec + num, 8);
90
0
                else
91
0
                    for (i = 0; i < 8; ++i)
92
0
                        ovec[i] = ovec[i + num] << rem | ovec[i + num + 1] >> (8 - rem);
93
0
#ifdef L_ENDIAN
94
0
                v0 = sh[0], v1 = sh[1];
95
#else
96
                iv = &ovec[0];
97
                c2l(iv, v0);
98
                c2l(iv, v1);
99
#endif
100
0
            }
101
0
        }
102
0
    } else {
103
0
        while (l >= (unsigned long)n) {
104
0
            l -= n;
105
0
            ti[0] = v0;
106
0
            ti[1] = v1;
107
0
            DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT);
108
0
            c2ln(in, d0, d1, n);
109
0
            in += n;
110
            /*
111
             * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
112
             * gcc :-(
113
             */
114
0
            if (numbits == 32) {
115
0
                v0 = v1;
116
0
                v1 = d0;
117
0
            } else if (numbits == 64) {
118
0
                v0 = d0;
119
0
                v1 = d1;
120
0
            } else {
121
#ifndef L_ENDIAN
122
                iv = &ovec[0];
123
                l2c(v0, iv);
124
                l2c(v1, iv);
125
                l2c(d0, iv);
126
                l2c(d1, iv);
127
#else
128
0
                sh[0] = v0, sh[1] = v1, sh[2] = d0, sh[3] = d1;
129
0
#endif
130
0
                if (rem == 0)
131
0
                    memmove(ovec, ovec + num, 8);
132
0
                else
133
0
                    for (i = 0; i < 8; ++i)
134
0
                        ovec[i] = ovec[i + num] << rem | ovec[i + num + 1] >> (8 - rem);
135
0
#ifdef L_ENDIAN
136
0
                v0 = sh[0], v1 = sh[1];
137
#else
138
                iv = &ovec[0];
139
                c2l(iv, v0);
140
                c2l(iv, v1);
141
#endif
142
0
            }
143
0
            d0 ^= ti[0];
144
0
            d1 ^= ti[1];
145
0
            l2cn(d0, d1, out, n);
146
0
            out += n;
147
0
        }
148
0
    }
149
0
    iv = &(*ivec)[0];
150
0
    l2c(v0, iv);
151
0
    l2c(v1, iv);
152
0
    v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0;
153
0
}