Coverage Report

Created: 2025-12-31 06:58

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
33.7k
{
19
33.7k
    if (dat->stream.cbc)
20
33.7k
        (*dat->stream.cbc)(in, out, len, dat->ks, dat->iv, dat->enc);
21
23
    else if (dat->enc)
22
23
        CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block);
23
0
    else
24
0
        CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block);
25
26
33.7k
    return 1;
27
33.7k
}
28
29
int ossl_cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out,
30
    const unsigned char *in, size_t len)
31
2
{
32
2
    size_t i, bl = dat->blocksize;
33
34
2
    if (len < bl)
35
0
        return 1;
36
37
2
    if (dat->stream.ecb) {
38
0
        (*dat->stream.ecb)(in, out, len, dat->ks, dat->enc);
39
2
    } else {
40
4
        for (i = 0, len -= bl; i <= len; i += bl)
41
2
            (*dat->block)(in + i, out + i, dat->ks);
42
2
    }
43
44
2
    return 1;
45
2
}
46
47
int ossl_cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
48
    const unsigned char *in, size_t len)
49
96
{
50
96
    int num = dat->num;
51
52
96
    CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
53
96
    dat->num = num;
54
55
96
    return 1;
56
96
}
57
58
int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
59
    const unsigned char *in, size_t len)
60
96
{
61
96
    int num = dat->num;
62
63
96
    CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
64
96
        dat->block);
65
96
    dat->num = num;
66
67
96
    return 1;
68
96
}
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
160
{
85
160
    int num = dat->num;
86
87
160
    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
160
    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
160
    if (len)
102
160
        CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
103
160
            dat->enc, dat->block);
104
105
160
    dat->num = num;
106
107
160
    return 1;
108
160
}
109
110
int ossl_cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
111
    const unsigned char *in, size_t len)
112
114k
{
113
114k
    unsigned int num = dat->num;
114
115
114k
    if (dat->stream.ctr)
116
114k
        CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
117
114k
            &num, dat->stream.ctr);
118
0
    else
119
0
        CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
120
0
            &num, dat->block);
121
114k
    dat->num = num;
122
123
114k
    return 1;
124
114k
}
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
9
{
134
9
    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
9
    if (inl > 0)
141
9
        ossl_cipher_hw_generic_cbc(ctx, out, in, inl);
142
9
    return 1;
143
9
}
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
32
{
166
32
    size_t chunk = MAXCHUNK;
167
168
32
    if (inl < chunk)
169
32
        chunk = inl;
170
64
    while (inl > 0 && inl >= chunk) {
171
32
        ossl_cipher_hw_generic_cfb128(ctx, out, in, inl);
172
32
        inl -= chunk;
173
32
        in += chunk;
174
32
        out += chunk;
175
32
        if (inl < chunk)
176
32
            chunk = inl;
177
32
    }
178
32
    return 1;
179
32
}
180
181
int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
182
    const unsigned char *in, size_t inl)
183
32
{
184
32
    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
32
    if (inl > 0)
191
32
        ossl_cipher_hw_generic_ofb128(ctx, out, in, inl);
192
32
    return 1;
193
32
}