Coverage Report

Created: 2026-04-01 06:39

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
35.1k
{
19
35.1k
    if (dat->stream.cbc)
20
31.8k
        (*dat->stream.cbc)(in, out, len, dat->ks, dat->iv, dat->enc);
21
3.32k
    else if (dat->enc)
22
3.30k
        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
35.1k
    return 1;
27
35.1k
}
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
127
{
50
127
    int num = dat->num;
51
52
127
    CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
53
127
    dat->num = num;
54
55
127
    return 1;
56
127
}
57
58
int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
59
    const unsigned char *in, size_t len)
60
141
{
61
141
    int num = dat->num;
62
63
141
    CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
64
141
        dat->block);
65
141
    dat->num = num;
66
67
141
    return 1;
68
141
}
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
163k
{
113
163k
    unsigned int num = dat->num;
114
115
163k
    if (dat->stream.ctr)
116
163k
        CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
117
163k
            &num, dat->stream.ctr);
118
43
    else
119
43
        CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
120
43
            &num, dat->block);
121
163k
    dat->num = num;
122
123
163k
    return 1;
124
163k
}
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
37
{
148
37
    size_t chunk = MAXCHUNK;
149
150
37
    if (inl < chunk)
151
37
        chunk = inl;
152
74
    while (inl > 0 && inl >= chunk) {
153
37
        ossl_cipher_hw_generic_cfb8(ctx, out, in, inl);
154
37
        inl -= chunk;
155
37
        in += chunk;
156
37
        out += chunk;
157
37
        if (inl < chunk)
158
37
            chunk = inl;
159
37
    }
160
37
    return 1;
161
37
}
162
163
int ossl_cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
164
    const unsigned char *in, size_t inl)
165
43
{
166
43
    size_t chunk = MAXCHUNK;
167
168
43
    if (inl < chunk)
169
43
        chunk = inl;
170
86
    while (inl > 0 && inl >= chunk) {
171
43
        ossl_cipher_hw_generic_cfb128(ctx, out, in, inl);
172
43
        inl -= chunk;
173
43
        in += chunk;
174
43
        out += chunk;
175
43
        if (inl < chunk)
176
43
            chunk = inl;
177
43
    }
178
43
    return 1;
179
43
}
180
181
int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
182
    const unsigned char *in, size_t inl)
183
39
{
184
39
    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
39
    if (inl > 0)
191
39
        ossl_cipher_hw_generic_ofb128(ctx, out, in, inl);
192
39
    return 1;
193
39
}