Coverage Report

Created: 2020-08-14 21:15

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