Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/e_camellia.c
Line
Count
Source
1
/*
2
 * Copyright 2006-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
/*
11
 * Camellia low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <openssl/opensslconf.h>
17
18
#include <openssl/evp.h>
19
#include <openssl/err.h>
20
#include <string.h>
21
#include <assert.h>
22
#include <openssl/camellia.h>
23
#include "crypto/evp.h"
24
#include "crypto/modes.h"
25
#include "crypto/cmll_platform.h"
26
#include "evp_local.h"
27
28
static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
29
    const unsigned char *iv, int enc);
30
31
/* Camellia subkey Structure */
32
typedef struct {
33
    CAMELLIA_KEY ks;
34
    block128_f block;
35
    union {
36
        cbc128_f cbc;
37
        ctr128_f ctr;
38
    } stream;
39
} EVP_CAMELLIA_KEY;
40
41
0
#define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
42
43
/* Attribute operation for Camellia */
44
#define data(ctx) EVP_C_DATA(EVP_CAMELLIA_KEY, ctx)
45
46
#if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
47
/* ---------^^^ this is not a typo, just a way to detect that
48
 * assembler support was in general requested... */
49
#include "crypto/sparc_arch.h"
50
51
static int cmll_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
52
    const unsigned char *iv, int enc)
53
{
54
    int ret, mode, bits;
55
    EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx);
56
57
    mode = EVP_CIPHER_CTX_get_mode(ctx);
58
    bits = EVP_CIPHER_CTX_get_key_length(ctx) * 8;
59
60
    cmll_t4_set_key(key, bits, &dat->ks);
61
62
    if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
63
        && !enc) {
64
        ret = 0;
65
        dat->block = (block128_f)cmll_t4_decrypt;
66
        switch (bits) {
67
        case 128:
68
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)cmll128_t4_cbc_decrypt : NULL;
69
            break;
70
        case 192:
71
        case 256:
72
            dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)cmll256_t4_cbc_decrypt : NULL;
73
            break;
74
        default:
75
            ret = -1;
76
        }
77
    } else {
78
        ret = 0;
79
        dat->block = (block128_f)cmll_t4_encrypt;
80
        switch (bits) {
81
        case 128:
82
            if (mode == EVP_CIPH_CBC_MODE)
83
                dat->stream.cbc = (cbc128_f)cmll128_t4_cbc_encrypt;
84
            else if (mode == EVP_CIPH_CTR_MODE)
85
                dat->stream.ctr = (ctr128_f)cmll128_t4_ctr32_encrypt;
86
            else
87
                dat->stream.cbc = NULL;
88
            break;
89
        case 192:
90
        case 256:
91
            if (mode == EVP_CIPH_CBC_MODE)
92
                dat->stream.cbc = (cbc128_f)cmll256_t4_cbc_encrypt;
93
            else if (mode == EVP_CIPH_CTR_MODE)
94
                dat->stream.ctr = (ctr128_f)cmll256_t4_ctr32_encrypt;
95
            else
96
                dat->stream.cbc = NULL;
97
            break;
98
        default:
99
            ret = -1;
100
        }
101
    }
102
103
    if (ret < 0) {
104
        ERR_raise(ERR_LIB_EVP, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
105
        return 0;
106
    }
107
108
    return 1;
109
}
110
111
#define cmll_t4_cbc_cipher camellia_cbc_cipher
112
static int cmll_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
113
    const unsigned char *in, size_t len);
114
115
#define cmll_t4_ecb_cipher camellia_ecb_cipher
116
static int cmll_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
117
    const unsigned char *in, size_t len);
118
119
#define cmll_t4_ofb_cipher camellia_ofb_cipher
120
static int cmll_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
121
    const unsigned char *in, size_t len);
122
123
#define cmll_t4_cfb_cipher camellia_cfb_cipher
124
static int cmll_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
125
    const unsigned char *in, size_t len);
126
127
#define cmll_t4_cfb8_cipher camellia_cfb8_cipher
128
static int cmll_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
129
    const unsigned char *in, size_t len);
130
131
#define cmll_t4_cfb1_cipher camellia_cfb1_cipher
132
static int cmll_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
133
    const unsigned char *in, size_t len);
134
135
#define cmll_t4_ctr_cipher camellia_ctr_cipher
136
static int cmll_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
137
    const unsigned char *in, size_t len);
138
139
#define BLOCK_CIPHER_generic(nid, keylen, blocksize, ivlen, nmode, mode, MODE, flags)         \
140
    static const EVP_CIPHER cmll_t4_##keylen##_##mode = {                                     \
141
        nid##_##keylen##_##nmode, blocksize, keylen / 8, ivlen,                               \
142
        flags | EVP_CIPH_##MODE##_MODE,                                                       \
143
        EVP_ORIG_GLOBAL,                                                                      \
144
        cmll_t4_init_key,                                                                     \
145
        cmll_t4_##mode##_cipher,                                                              \
146
        NULL,                                                                                 \
147
        sizeof(EVP_CAMELLIA_KEY),                                                             \
148
        NULL, NULL, NULL, NULL                                                                \
149
    };                                                                                        \
150
    static const EVP_CIPHER camellia_##keylen##_##mode = {                                    \
151
        nid##_##keylen##_##nmode, blocksize,                                                  \
152
        keylen / 8, ivlen,                                                                    \
153
        flags | EVP_CIPH_##MODE##_MODE,                                                       \
154
        EVP_ORIG_GLOBAL,                                                                      \
155
        camellia_init_key,                                                                    \
156
        camellia_##mode##_cipher,                                                             \
157
        NULL,                                                                                 \
158
        sizeof(EVP_CAMELLIA_KEY),                                                             \
159
        NULL, NULL, NULL, NULL                                                                \
160
    };                                                                                        \
161
    const EVP_CIPHER *EVP_camellia_##keylen##_##mode(void)                                    \
162
    {                                                                                         \
163
        return SPARC_CMLL_CAPABLE ? &cmll_t4_##keylen##_##mode : &camellia_##keylen##_##mode; \
164
    }
165
166
#else
167
168
#define BLOCK_CIPHER_generic(nid, keylen, blocksize, ivlen, nmode, mode, MODE, flags) \
169
    static const EVP_CIPHER camellia_##keylen##_##mode = {                            \
170
        nid##_##keylen##_##nmode, blocksize, keylen / 8, ivlen,                       \
171
        flags | EVP_CIPH_##MODE##_MODE,                                               \
172
        EVP_ORIG_GLOBAL,                                                              \
173
        camellia_init_key,                                                            \
174
        camellia_##mode##_cipher,                                                     \
175
        NULL,                                                                         \
176
        sizeof(EVP_CAMELLIA_KEY),                                                     \
177
        NULL, NULL, NULL, NULL                                                        \
178
    };                                                                                \
179
    const EVP_CIPHER *EVP_camellia_##keylen##_##mode(void)                            \
180
63
    {                                                                                 \
181
63
        return &camellia_##keylen##_##mode;                                           \
182
63
    }
EVP_camellia_128_cbc
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_128_ecb
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_128_ofb
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_128_cfb128
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_128_cfb1
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_128_cfb8
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_128_ctr
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_cbc
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_ecb
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_ofb
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_cfb128
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_cfb1
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_cfb8
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_192_ctr
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_cbc
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_ecb
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_ofb
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_cfb128
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_cfb1
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_cfb8
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
EVP_camellia_256_ctr
Line
Count
Source
180
3
    {                                                                                 \
181
3
        return &camellia_##keylen##_##mode;                                           \
182
3
    }
183
184
#endif
185
186
#define BLOCK_CIPHER_generic_pack(nid, keylen, flags)                                                          \
187
    BLOCK_CIPHER_generic(nid, keylen, 16, 16, cbc, cbc, CBC, flags | EVP_CIPH_FLAG_DEFAULT_ASN1)               \
188
        BLOCK_CIPHER_generic(nid, keylen, 16, 0, ecb, ecb, ECB, flags | EVP_CIPH_FLAG_DEFAULT_ASN1)            \
189
            BLOCK_CIPHER_generic(nid, keylen, 1, 16, ofb128, ofb, OFB, flags | EVP_CIPH_FLAG_DEFAULT_ASN1)     \
190
                BLOCK_CIPHER_generic(nid, keylen, 1, 16, cfb128, cfb, CFB, flags | EVP_CIPH_FLAG_DEFAULT_ASN1) \
191
                    BLOCK_CIPHER_generic(nid, keylen, 1, 16, cfb1, cfb1, CFB, flags)                           \
192
                        BLOCK_CIPHER_generic(nid, keylen, 1, 16, cfb8, cfb8, CFB, flags)                       \
193
                            BLOCK_CIPHER_generic(nid, keylen, 1, 16, ctr, ctr, CTR, flags)
194
195
/* The subkey for Camellia is generated. */
196
static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
197
    const unsigned char *iv, int enc)
198
0
{
199
0
    int ret, mode;
200
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
201
202
0
    ret = Camellia_set_key(key, EVP_CIPHER_CTX_get_key_length(ctx) * 8,
203
0
        &dat->ks);
204
0
    if (ret < 0) {
205
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
206
0
        return 0;
207
0
    }
208
209
0
    mode = EVP_CIPHER_CTX_get_mode(ctx);
210
0
    if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
211
0
        && !enc) {
212
0
        dat->block = (block128_f)Camellia_decrypt;
213
0
        dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)Camellia_cbc_encrypt : NULL;
214
0
    } else {
215
0
        dat->block = (block128_f)Camellia_encrypt;
216
0
        dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ? (cbc128_f)Camellia_cbc_encrypt : NULL;
217
0
    }
218
219
0
    return 1;
220
0
}
221
222
static int camellia_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
223
    const unsigned char *in, size_t len)
224
0
{
225
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
226
227
0
    if (dat->stream.cbc)
228
0
        (*dat->stream.cbc)(in, out, len, &dat->ks, ctx->iv,
229
0
            EVP_CIPHER_CTX_is_encrypting(ctx));
230
0
    else if (EVP_CIPHER_CTX_is_encrypting(ctx))
231
0
        CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
232
0
    else
233
0
        CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
234
235
0
    return 1;
236
0
}
237
238
static int camellia_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
239
    const unsigned char *in, size_t len)
240
0
{
241
0
    size_t bl = EVP_CIPHER_CTX_get_block_size(ctx);
242
0
    size_t i;
243
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
244
245
0
    if (len < bl)
246
0
        return 1;
247
248
0
    for (i = 0, len -= bl; i <= len; i += bl)
249
0
        (*dat->block)(in + i, out + i, &dat->ks);
250
251
0
    return 1;
252
0
}
253
254
static int camellia_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
255
    const unsigned char *in, size_t len)
256
0
{
257
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
258
259
0
    int num = EVP_CIPHER_CTX_get_num(ctx);
260
0
    CRYPTO_ofb128_encrypt(in, out, len, &dat->ks, ctx->iv, &num, dat->block);
261
0
    EVP_CIPHER_CTX_set_num(ctx, num);
262
0
    return 1;
263
0
}
264
265
static int camellia_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
266
    const unsigned char *in, size_t len)
267
0
{
268
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
269
270
0
    int num = EVP_CIPHER_CTX_get_num(ctx);
271
0
    CRYPTO_cfb128_encrypt(in, out, len, &dat->ks, ctx->iv, &num,
272
0
        EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
273
0
    EVP_CIPHER_CTX_set_num(ctx, num);
274
0
    return 1;
275
0
}
276
277
static int camellia_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
278
    const unsigned char *in, size_t len)
279
0
{
280
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
281
282
0
    int num = EVP_CIPHER_CTX_get_num(ctx);
283
0
    CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks, ctx->iv, &num,
284
0
        EVP_CIPHER_CTX_is_encrypting(ctx), dat->block);
285
0
    EVP_CIPHER_CTX_set_num(ctx, num);
286
0
    return 1;
287
0
}
288
289
static int camellia_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
290
    const unsigned char *in, size_t len)
291
0
{
292
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
293
294
0
    if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) {
295
0
        int num = EVP_CIPHER_CTX_get_num(ctx);
296
0
        CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks, ctx->iv, &num,
297
0
            EVP_CIPHER_CTX_is_encrypting(ctx),
298
0
            dat->block);
299
0
        EVP_CIPHER_CTX_set_num(ctx, num);
300
0
        return 1;
301
0
    }
302
303
0
    while (len >= MAXBITCHUNK) {
304
0
        int num = EVP_CIPHER_CTX_get_num(ctx);
305
0
        CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
306
0
            ctx->iv, &num,
307
0
            EVP_CIPHER_CTX_is_encrypting(ctx),
308
0
            dat->block);
309
0
        EVP_CIPHER_CTX_set_num(ctx, num);
310
0
        len -= MAXBITCHUNK;
311
0
        out += MAXBITCHUNK;
312
0
        in += MAXBITCHUNK;
313
0
    }
314
0
    if (len) {
315
0
        int num = EVP_CIPHER_CTX_get_num(ctx);
316
0
        CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
317
0
            ctx->iv, &num,
318
0
            EVP_CIPHER_CTX_is_encrypting(ctx),
319
0
            dat->block);
320
0
        EVP_CIPHER_CTX_set_num(ctx, num);
321
0
    }
322
323
0
    return 1;
324
0
}
325
326
static int camellia_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
327
    const unsigned char *in, size_t len)
328
0
{
329
0
    int snum = EVP_CIPHER_CTX_get_num(ctx);
330
0
    unsigned int num;
331
0
    EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY, ctx);
332
333
0
    if (snum < 0)
334
0
        return 0;
335
0
    num = snum;
336
0
    if (dat->stream.ctr)
337
0
        CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks, ctx->iv,
338
0
            EVP_CIPHER_CTX_buf_noconst(ctx),
339
0
            &num,
340
0
            dat->stream.ctr);
341
0
    else
342
0
        CRYPTO_ctr128_encrypt(in, out, len, &dat->ks, ctx->iv,
343
0
            EVP_CIPHER_CTX_buf_noconst(ctx), &num,
344
0
            dat->block);
345
0
    EVP_CIPHER_CTX_set_num(ctx, num);
346
0
    return 1;
347
0
}
348
349
BLOCK_CIPHER_generic_pack(NID_camellia, 128, 0)
350
    BLOCK_CIPHER_generic_pack(NID_camellia, 192, 0)
351
        BLOCK_CIPHER_generic_pack(NID_camellia, 256, 0)