Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/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
34.3k
{
19
34.3k
    if (dat->stream.cbc)
20
31.1k
        (*dat->stream.cbc)(in, out, len, dat->ks, dat->iv, dat->enc);
21
3.19k
    else if (dat->enc)
22
3.17k
        CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block);
23
24
    else
24
24
        CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block);
25
26
34.3k
    return 1;
27
34.3k
}
28
29
int ossl_cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out,
30
    const unsigned char *in, size_t len)
31
9
{
32
9
    size_t i, bl = dat->blocksize;
33
34
9
    if (len < bl)
35
0
        return 1;
36
37
9
    if (dat->stream.ecb) {
38
0
        (*dat->stream.ecb)(in, out, len, dat->ks, dat->enc);
39
9
    } else {
40
18
        for (i = 0, len -= bl; i <= len; i += bl)
41
9
            (*dat->block)(in + i, out + i, dat->ks);
42
9
    }
43
44
9
    return 1;
45
9
}
46
47
int ossl_cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
48
    const unsigned char *in, size_t len)
49
131
{
50
131
    int num = dat->num;
51
52
131
    CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
53
131
    dat->num = num;
54
55
131
    return 1;
56
131
}
57
58
int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
59
    const unsigned char *in, size_t len)
60
134
{
61
134
    int num = dat->num;
62
63
134
    CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
64
134
        dat->block);
65
134
    dat->num = num;
66
67
134
    return 1;
68
134
}
69
70
int ossl_cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out,
71
    const unsigned char *in, size_t len)
72
106
{
73
106
    int num = dat->num;
74
75
106
    CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
76
106
        dat->block);
77
106
    dat->num = num;
78
79
106
    return 1;
80
106
}
81
82
int ossl_cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out,
83
    const unsigned char *in, size_t len)
84
139
{
85
139
    int num = dat->num;
86
87
139
    if (dat->use_bits) {
88
1
        CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num,
89
1
            dat->enc, dat->block);
90
1
        dat->num = num;
91
1
        return 1;
92
1
    }
93
94
138
    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
138
    if (len)
102
138
        CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
103
138
            dat->enc, dat->block);
104
105
138
    dat->num = num;
106
107
138
    return 1;
108
139
}
109
110
int ossl_cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
111
    const unsigned char *in, size_t len)
112
176k
{
113
176k
    unsigned int num = dat->num;
114
115
176k
    if (dat->stream.ctr)
116
176k
        CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
117
176k
            &num, dat->stream.ctr);
118
46
    else
119
46
        CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
120
46
            &num, dat->block);
121
176k
    dat->num = num;
122
123
176k
    return 1;
124
176k
}
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
36
{
134
36
    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
36
    if (inl > 0)
141
36
        ossl_cipher_hw_generic_cbc(ctx, out, in, inl);
142
36
    return 1;
143
36
}
144
145
int ossl_cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
146
    const unsigned char *in, size_t inl)
147
35
{
148
35
    size_t chunk = MAXCHUNK;
149
150
35
    if (inl < chunk)
151
35
        chunk = inl;
152
70
    while (inl > 0 && inl >= chunk) {
153
35
        ossl_cipher_hw_generic_cfb8(ctx, out, in, inl);
154
35
        inl -= chunk;
155
35
        in += chunk;
156
35
        out += chunk;
157
35
        if (inl < chunk)
158
35
            chunk = inl;
159
35
    }
160
35
    return 1;
161
35
}
162
163
int ossl_cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
164
    const unsigned char *in, size_t inl)
165
39
{
166
39
    size_t chunk = MAXCHUNK;
167
168
39
    if (inl < chunk)
169
39
        chunk = inl;
170
78
    while (inl > 0 && inl >= chunk) {
171
39
        ossl_cipher_hw_generic_cfb128(ctx, out, in, inl);
172
39
        inl -= chunk;
173
39
        in += chunk;
174
39
        out += chunk;
175
39
        if (inl < chunk)
176
39
            chunk = inl;
177
39
    }
178
39
    return 1;
179
39
}
180
181
int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
182
    const unsigned char *in, size_t inl)
183
43
{
184
43
    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
43
    if (inl > 0)
191
43
        ossl_cipher_hw_generic_ofb128(ctx, out, in, inl);
192
43
    return 1;
193
43
}