Coverage Report

Created: 2024-11-21 07:03

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