Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/ciphers/ciphercommon_hw.c
Line
Count
Source
1
/*
2
 * Copyright 2019-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 "prov/ciphercommon.h"
11
12
/*-
13
 * The generic cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
14
 * Used if there is no special hardware implementations.
15
 */
16
int ossl_cipher_hw_generic_cbc(PROV_CIPHER_CTX *dat, unsigned char *out,
17
    const unsigned char *in, size_t len)
18
36.0k
{
19
36.0k
    if (dat->stream.cbc)
20
35.0k
        (*dat->stream.cbc)(in, out, len, dat->ks, dat->iv, dat->enc);
21
982
    else if (dat->enc)
22
970
        CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block);
23
12
    else
24
12
        CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block);
25
26
36.0k
    return 1;
27
36.0k
}
28
29
int ossl_cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out,
30
    const unsigned char *in, size_t len)
31
0
{
32
0
    size_t i, bl = dat->blocksize;
33
34
0
    if (len < bl)
35
0
        return 1;
36
37
0
    if (dat->stream.ecb) {
38
0
        (*dat->stream.ecb)(in, out, len, dat->ks, dat->enc);
39
0
    } else {
40
0
        for (i = 0, len -= bl; i <= len; i += bl)
41
0
            (*dat->block)(in + i, out + i, dat->ks);
42
0
    }
43
44
0
    return 1;
45
0
}
46
47
int ossl_cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
48
    const unsigned char *in, size_t len)
49
112
{
50
112
    int num = dat->num;
51
52
112
    CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
53
112
    dat->num = num;
54
55
112
    return 1;
56
112
}
57
58
int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
59
    const unsigned char *in, size_t len)
60
132
{
61
132
    int num = dat->num;
62
63
132
    CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
64
132
        dat->block);
65
132
    dat->num = num;
66
67
132
    return 1;
68
132
}
69
70
int ossl_cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out,
71
    const unsigned char *in, size_t len)
72
96
{
73
96
    int num = dat->num;
74
75
96
    CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
76
96
        dat->block);
77
96
    dat->num = num;
78
79
96
    return 1;
80
96
}
81
82
int ossl_cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out,
83
    const unsigned char *in, size_t len)
84
128
{
85
128
    int num = dat->num;
86
87
128
    if (dat->use_bits) {
88
0
        CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num,
89
0
            dat->enc, dat->block);
90
0
        dat->num = num;
91
0
        return 1;
92
0
    }
93
94
128
    while (len >= MAXBITCHUNK) {
95
0
        CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks,
96
0
            dat->iv, &num, dat->enc, dat->block);
97
0
        len -= MAXBITCHUNK;
98
0
        out += MAXBITCHUNK;
99
0
        in += MAXBITCHUNK;
100
0
    }
101
128
    if (len)
102
128
        CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
103
128
            dat->enc, dat->block);
104
105
128
    dat->num = num;
106
107
128
    return 1;
108
128
}
109
110
int ossl_cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
111
    const unsigned char *in, size_t len)
112
133k
{
113
133k
    unsigned int num = dat->num;
114
115
133k
    if (dat->stream.ctr)
116
132k
        CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
117
132k
            &num, dat->stream.ctr);
118
36
    else
119
36
        CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
120
36
            &num, dat->block);
121
133k
    dat->num = num;
122
123
133k
    return 1;
124
133k
}
125
126
/*-
127
 * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
128
 * Used if there is no special hardware implementations.
129
 */
130
131
int ossl_cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
132
    const unsigned char *in, size_t inl)
133
23
{
134
23
    while (inl >= MAXCHUNK) {
135
0
        ossl_cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK);
136
0
        inl -= MAXCHUNK;
137
0
        in += MAXCHUNK;
138
0
        out += MAXCHUNK;
139
0
    }
140
23
    if (inl > 0)
141
23
        ossl_cipher_hw_generic_cbc(ctx, out, in, inl);
142
23
    return 1;
143
23
}
144
145
int ossl_cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
146
    const unsigned char *in, size_t inl)
147
32
{
148
32
    size_t chunk = MAXCHUNK;
149
150
32
    if (inl < chunk)
151
32
        chunk = inl;
152
64
    while (inl > 0 && inl >= chunk) {
153
32
        ossl_cipher_hw_generic_cfb8(ctx, out, in, inl);
154
32
        inl -= chunk;
155
32
        in += chunk;
156
32
        out += chunk;
157
32
        if (inl < chunk)
158
32
            chunk = inl;
159
32
    }
160
32
    return 1;
161
32
}
162
163
int ossl_cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
164
    const unsigned char *in, size_t inl)
165
40
{
166
40
    size_t chunk = MAXCHUNK;
167
168
40
    if (inl < chunk)
169
40
        chunk = inl;
170
80
    while (inl > 0 && inl >= chunk) {
171
40
        ossl_cipher_hw_generic_cfb128(ctx, out, in, inl);
172
40
        inl -= chunk;
173
40
        in += chunk;
174
40
        out += chunk;
175
40
        if (inl < chunk)
176
40
            chunk = inl;
177
40
    }
178
40
    return 1;
179
40
}
180
181
int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
182
    const unsigned char *in, size_t inl)
183
38
{
184
38
    while (inl >= MAXCHUNK) {
185
0
        ossl_cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK);
186
0
        inl -= MAXCHUNK;
187
0
        in += MAXCHUNK;
188
0
        out += MAXCHUNK;
189
0
    }
190
38
    if (inl > 0)
191
38
        ossl_cipher_hw_generic_ofb128(ctx, out, in, inl);
192
38
    return 1;
193
38
}