Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/modes/cbc128.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2021 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
#include <string.h>
11
#include <openssl/crypto.h>
12
#include "crypto/modes.h"
13
14
#if !defined(STRICT_ALIGNMENT) && !defined(PEDANTIC)
15
0
#define STRICT_ALIGNMENT 0
16
#endif
17
18
#if defined(__GNUC__) && !STRICT_ALIGNMENT
19
typedef size_t size_t_aX __attribute((__aligned__(1)));
20
#else
21
typedef size_t size_t_aX;
22
#endif
23
24
void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
25
    size_t len, const void *key,
26
    unsigned char ivec[16], block128_f block)
27
0
{
28
0
    size_t n;
29
0
    const unsigned char *iv = ivec;
30
31
0
    if (len == 0)
32
0
        return;
33
34
0
#if !defined(OPENSSL_SMALL_FOOTPRINT)
35
0
    if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
36
0
        while (len >= 16) {
37
0
            for (n = 0; n < 16; ++n)
38
0
                out[n] = in[n] ^ iv[n];
39
0
            (*block)(out, out, key);
40
0
            iv = out;
41
0
            len -= 16;
42
0
            in += 16;
43
0
            out += 16;
44
0
        }
45
0
    } else {
46
0
        while (len >= 16) {
47
0
            for (n = 0; n < 16; n += sizeof(size_t))
48
0
                *(size_t_aX *)(out + n) = *(size_t_aX *)(in + n) ^ *(size_t_aX *)(iv + n);
49
0
            (*block)(out, out, key);
50
0
            iv = out;
51
0
            len -= 16;
52
0
            in += 16;
53
0
            out += 16;
54
0
        }
55
0
    }
56
0
#endif
57
0
    while (len) {
58
0
        for (n = 0; n < 16 && n < len; ++n)
59
0
            out[n] = in[n] ^ iv[n];
60
0
        for (; n < 16; ++n)
61
0
            out[n] = iv[n];
62
0
        (*block)(out, out, key);
63
0
        iv = out;
64
0
        if (len <= 16)
65
0
            break;
66
0
        len -= 16;
67
0
        in += 16;
68
0
        out += 16;
69
0
    }
70
0
    if (ivec != iv)
71
0
        memcpy(ivec, iv, 16);
72
0
}
73
74
void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
75
    size_t len, const void *key,
76
    unsigned char ivec[16], block128_f block)
77
0
{
78
0
    size_t n;
79
0
    union {
80
0
        size_t t[16 / sizeof(size_t)];
81
0
        unsigned char c[16];
82
0
    } tmp;
83
84
0
    if (len == 0)
85
0
        return;
86
87
0
#if !defined(OPENSSL_SMALL_FOOTPRINT)
88
0
    if (in != out) {
89
0
        const unsigned char *iv = ivec;
90
91
0
        if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
92
0
            while (len >= 16) {
93
0
                (*block)(in, out, key);
94
0
                for (n = 0; n < 16; ++n)
95
0
                    out[n] ^= iv[n];
96
0
                iv = in;
97
0
                len -= 16;
98
0
                in += 16;
99
0
                out += 16;
100
0
            }
101
0
        } else if (16 % sizeof(size_t) == 0) { /* always true */
102
0
            while (len >= 16) {
103
0
                size_t_aX *out_t = (size_t_aX *)out;
104
0
                size_t_aX *iv_t = (size_t_aX *)iv;
105
106
0
                (*block)(in, out, key);
107
0
                for (n = 0; n < 16 / sizeof(size_t); n++)
108
0
                    out_t[n] ^= iv_t[n];
109
0
                iv = in;
110
0
                len -= 16;
111
0
                in += 16;
112
0
                out += 16;
113
0
            }
114
0
        }
115
0
        if (ivec != iv)
116
0
            memcpy(ivec, iv, 16);
117
0
    } else {
118
0
        if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) {
119
0
            unsigned char c;
120
0
            while (len >= 16) {
121
0
                (*block)(in, tmp.c, key);
122
0
                for (n = 0; n < 16; ++n) {
123
0
                    c = in[n];
124
0
                    out[n] = tmp.c[n] ^ ivec[n];
125
0
                    ivec[n] = c;
126
0
                }
127
0
                len -= 16;
128
0
                in += 16;
129
0
                out += 16;
130
0
            }
131
0
        } else if (16 % sizeof(size_t) == 0) { /* always true */
132
0
            while (len >= 16) {
133
0
                size_t c;
134
0
                size_t_aX *out_t = (size_t_aX *)out;
135
0
                size_t_aX *ivec_t = (size_t_aX *)ivec;
136
0
                const size_t_aX *in_t = (const size_t_aX *)in;
137
138
0
                (*block)(in, tmp.c, key);
139
0
                for (n = 0; n < 16 / sizeof(size_t); n++) {
140
0
                    c = in_t[n];
141
0
                    out_t[n] = tmp.t[n] ^ ivec_t[n];
142
0
                    ivec_t[n] = c;
143
0
                }
144
0
                len -= 16;
145
0
                in += 16;
146
0
                out += 16;
147
0
            }
148
0
        }
149
0
    }
150
0
#endif
151
0
    while (len) {
152
0
        unsigned char c;
153
0
        (*block)(in, tmp.c, key);
154
0
        for (n = 0; n < 16 && n < len; ++n) {
155
0
            c = in[n];
156
0
            out[n] = tmp.c[n] ^ ivec[n];
157
0
            ivec[n] = c;
158
0
        }
159
0
        if (len <= 16) {
160
0
            for (; n < 16; ++n)
161
0
                ivec[n] = in[n];
162
0
            break;
163
0
        }
164
0
        len -= 16;
165
0
        in += 16;
166
0
        out += 16;
167
0
    }
168
0
}