Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/include/crypto/evp.h
Line
Count
Source
1
/*
2
 * Copyright 2015-2022 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
#ifndef OSSL_CRYPTO_EVP_H
11
#define OSSL_CRYPTO_EVP_H
12
#pragma once
13
14
#include <openssl/evp.h>
15
#include <openssl/core_dispatch.h>
16
#include "internal/refcount.h"
17
#include "crypto/ecx.h"
18
19
/*
20
 * Don't free up md_ctx->pctx in EVP_MD_CTX_reset, use the reserved flag
21
 * values in evp.h
22
 */
23
31.0M
#define EVP_MD_CTX_FLAG_KEEP_PKEY_CTX 0x0400
24
25
#define evp_pkey_ctx_is_legacy(ctx) \
26
258k
    ((ctx)->keymgmt == NULL)
27
#define evp_pkey_ctx_is_provided(ctx) \
28
62.9k
    (!evp_pkey_ctx_is_legacy(ctx))
29
30
struct evp_pkey_ctx_st {
31
    /* Actual operation */
32
    int operation;
33
34
    /*
35
     * Library context, property query, keytype and keymgmt associated with
36
     * this context
37
     */
38
    OSSL_LIB_CTX *libctx;
39
    char *propquery;
40
    const char *keytype;
41
    /* If |pkey| below is set, this field is always a reference to its keymgmt */
42
    EVP_KEYMGMT *keymgmt;
43
44
    union {
45
        struct {
46
            void *genctx;
47
        } keymgmt;
48
49
        struct {
50
            EVP_KEYEXCH *exchange;
51
            /*
52
             * Opaque ctx returned from a providers exchange algorithm
53
             * implementation OSSL_FUNC_keyexch_newctx()
54
             */
55
            void *algctx;
56
        } kex;
57
58
        struct {
59
            EVP_SIGNATURE *signature;
60
            /*
61
             * Opaque ctx returned from a providers signature algorithm
62
             * implementation OSSL_FUNC_signature_newctx()
63
             */
64
            void *algctx;
65
        } sig;
66
67
        struct {
68
            EVP_ASYM_CIPHER *cipher;
69
            /*
70
             * Opaque ctx returned from a providers asymmetric cipher algorithm
71
             * implementation OSSL_FUNC_asym_cipher_newctx()
72
             */
73
            void *algctx;
74
        } ciph;
75
        struct {
76
            EVP_KEM *kem;
77
            /*
78
             * Opaque ctx returned from a providers KEM algorithm
79
             * implementation OSSL_FUNC_kem_newctx()
80
             */
81
            void *algctx;
82
        } encap;
83
    } op;
84
85
    /*
86
     * Cached parameters.  Inits of operations that depend on these should
87
     * call evp_pkey_ctx_use_delayed_data() when the operation has been set
88
     * up properly.
89
     */
90
    struct {
91
        /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */
92
        char *dist_id_name; /* The name used with EVP_PKEY_CTX_ctrl_str() */
93
        void *dist_id; /* The distinguishing ID itself */
94
        size_t dist_id_len; /* The length of the distinguishing ID */
95
96
        /* Indicators of what has been set.  Keep them together! */
97
        unsigned int dist_id_set : 1;
98
    } cached_parameters;
99
100
    /* Application specific data, usually used by the callback */
101
    void *app_data;
102
    /* Keygen callback */
103
    EVP_PKEY_gen_cb *pkey_gencb;
104
    /* implementation specific keygen data */
105
    int *keygen_info;
106
    int keygen_info_count;
107
108
    /* Legacy fields below */
109
110
    /* EVP_PKEY identity */
111
    int legacy_keytype;
112
    /* Method associated with this operation */
113
    const EVP_PKEY_METHOD *pmeth;
114
    /* Engine that implements this method or NULL if builtin */
115
    ENGINE *engine;
116
    /* Key: may be NULL */
117
    EVP_PKEY *pkey;
118
    /* Peer key for key agreement, may be NULL */
119
    EVP_PKEY *peerkey;
120
    /* Algorithm specific data */
121
    void *data;
122
    /* Indicator if digest_custom needs to be called */
123
    unsigned int flag_call_digest_custom : 1;
124
    /*
125
     * Used to support taking custody of memory in the case of a provider being
126
     * used with the deprecated EVP_PKEY_CTX_set_rsa_keygen_pubexp() API. This
127
     * member should NOT be used for any other purpose and should be removed
128
     * when said deprecated API is excised completely.
129
     */
130
    BIGNUM *rsa_pubexp;
131
} /* EVP_PKEY_CTX */;
132
133
0
#define EVP_PKEY_FLAG_DYNAMIC 1
134
135
struct evp_pkey_method_st {
136
    int pkey_id;
137
    int flags;
138
    int (*init)(EVP_PKEY_CTX *ctx);
139
    int (*copy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
140
    void (*cleanup)(EVP_PKEY_CTX *ctx);
141
    int (*paramgen_init)(EVP_PKEY_CTX *ctx);
142
    int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
143
    int (*keygen_init)(EVP_PKEY_CTX *ctx);
144
    int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
145
    int (*sign_init)(EVP_PKEY_CTX *ctx);
146
    int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
147
        const unsigned char *tbs, size_t tbslen);
148
    int (*verify_init)(EVP_PKEY_CTX *ctx);
149
    int (*verify)(EVP_PKEY_CTX *ctx,
150
        const unsigned char *sig, size_t siglen,
151
        const unsigned char *tbs, size_t tbslen);
152
    int (*verify_recover_init)(EVP_PKEY_CTX *ctx);
153
    int (*verify_recover)(EVP_PKEY_CTX *ctx,
154
        unsigned char *rout, size_t *routlen,
155
        const unsigned char *sig, size_t siglen);
156
    int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
157
    int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
158
        EVP_MD_CTX *mctx);
159
    int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
160
    int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
161
        EVP_MD_CTX *mctx);
162
    int (*encrypt_init)(EVP_PKEY_CTX *ctx);
163
    int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
164
        const unsigned char *in, size_t inlen);
165
    int (*decrypt_init)(EVP_PKEY_CTX *ctx);
166
    int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
167
        const unsigned char *in, size_t inlen);
168
    int (*derive_init)(EVP_PKEY_CTX *ctx);
169
    int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
170
    int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
171
    int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
172
    int (*digestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
173
        const unsigned char *tbs, size_t tbslen);
174
    int (*digestverify)(EVP_MD_CTX *ctx, const unsigned char *sig,
175
        size_t siglen, const unsigned char *tbs,
176
        size_t tbslen);
177
    int (*check)(EVP_PKEY *pkey);
178
    int (*public_check)(EVP_PKEY *pkey);
179
    int (*param_check)(EVP_PKEY *pkey);
180
181
    int (*digest_custom)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx);
182
} /* EVP_PKEY_METHOD */;
183
184
DEFINE_STACK_OF_CONST(EVP_PKEY_METHOD)
185
186
void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx);
187
188
const EVP_PKEY_METHOD *ossl_dh_pkey_method(void);
189
const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void);
190
const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void);
191
const EVP_PKEY_METHOD *ossl_ec_pkey_method(void);
192
const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void);
193
const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void);
194
const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void);
195
const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void);
196
const EVP_PKEY_METHOD *ossl_rsa_pkey_method(void);
197
const EVP_PKEY_METHOD *ossl_rsa_pss_pkey_method(void);
198
199
struct evp_mac_st {
200
    OSSL_PROVIDER *prov;
201
    int name_id;
202
    char *type_name;
203
    const char *description;
204
205
    CRYPTO_REF_COUNT refcnt;
206
    CRYPTO_RWLOCK *lock;
207
208
    OSSL_FUNC_mac_newctx_fn *newctx;
209
    OSSL_FUNC_mac_dupctx_fn *dupctx;
210
    OSSL_FUNC_mac_freectx_fn *freectx;
211
    OSSL_FUNC_mac_init_fn *init;
212
    OSSL_FUNC_mac_update_fn *update;
213
    OSSL_FUNC_mac_final_fn *final;
214
    OSSL_FUNC_mac_gettable_params_fn *gettable_params;
215
    OSSL_FUNC_mac_gettable_ctx_params_fn *gettable_ctx_params;
216
    OSSL_FUNC_mac_settable_ctx_params_fn *settable_ctx_params;
217
    OSSL_FUNC_mac_get_params_fn *get_params;
218
    OSSL_FUNC_mac_get_ctx_params_fn *get_ctx_params;
219
    OSSL_FUNC_mac_set_ctx_params_fn *set_ctx_params;
220
};
221
222
struct evp_kdf_st {
223
    OSSL_PROVIDER *prov;
224
    int name_id;
225
    char *type_name;
226
    const char *description;
227
    CRYPTO_REF_COUNT refcnt;
228
    CRYPTO_RWLOCK *lock;
229
230
    OSSL_FUNC_kdf_newctx_fn *newctx;
231
    OSSL_FUNC_kdf_dupctx_fn *dupctx;
232
    OSSL_FUNC_kdf_freectx_fn *freectx;
233
    OSSL_FUNC_kdf_reset_fn *reset;
234
    OSSL_FUNC_kdf_derive_fn *derive;
235
    OSSL_FUNC_kdf_gettable_params_fn *gettable_params;
236
    OSSL_FUNC_kdf_gettable_ctx_params_fn *gettable_ctx_params;
237
    OSSL_FUNC_kdf_settable_ctx_params_fn *settable_ctx_params;
238
    OSSL_FUNC_kdf_get_params_fn *get_params;
239
    OSSL_FUNC_kdf_get_ctx_params_fn *get_ctx_params;
240
    OSSL_FUNC_kdf_set_ctx_params_fn *set_ctx_params;
241
};
242
243
42.1M
#define EVP_ORIG_DYNAMIC 0
244
996
#define EVP_ORIG_GLOBAL 1
245
4.24M
#define EVP_ORIG_METH 2
246
247
struct evp_md_st {
248
    /* nid */
249
    int type;
250
251
    /* Legacy structure members */
252
    int pkey_type;
253
    int md_size;
254
    unsigned long flags;
255
    int origin;
256
    int (*init)(EVP_MD_CTX *ctx);
257
    int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
258
    int (*final)(EVP_MD_CTX *ctx, unsigned char *md);
259
    int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from);
260
    int (*cleanup)(EVP_MD_CTX *ctx);
261
    int block_size;
262
    int ctx_size; /* how big does the ctx->md_data need to be */
263
    /* control function */
264
    int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
265
266
    /* New structure members */
267
    /* Above comment to be removed when legacy has gone */
268
    int name_id;
269
    char *type_name;
270
    const char *description;
271
    OSSL_PROVIDER *prov;
272
    CRYPTO_REF_COUNT refcnt;
273
    CRYPTO_RWLOCK *lock;
274
    OSSL_FUNC_digest_newctx_fn *newctx;
275
    OSSL_FUNC_digest_init_fn *dinit;
276
    OSSL_FUNC_digest_update_fn *dupdate;
277
    OSSL_FUNC_digest_final_fn *dfinal;
278
    OSSL_FUNC_digest_digest_fn *digest;
279
    OSSL_FUNC_digest_freectx_fn *freectx;
280
    OSSL_FUNC_digest_dupctx_fn *dupctx;
281
    OSSL_FUNC_digest_get_params_fn *get_params;
282
    OSSL_FUNC_digest_set_ctx_params_fn *set_ctx_params;
283
    OSSL_FUNC_digest_get_ctx_params_fn *get_ctx_params;
284
    OSSL_FUNC_digest_gettable_params_fn *gettable_params;
285
    OSSL_FUNC_digest_settable_ctx_params_fn *settable_ctx_params;
286
    OSSL_FUNC_digest_gettable_ctx_params_fn *gettable_ctx_params;
287
288
} /* EVP_MD */;
289
290
struct evp_cipher_st {
291
    int nid;
292
293
    int block_size;
294
    /* Default value for variable length ciphers */
295
    int key_len;
296
    int iv_len;
297
298
    /* Legacy structure members */
299
    /* Various flags */
300
    unsigned long flags;
301
    /* How the EVP_CIPHER was created. */
302
    int origin;
303
    /* init key */
304
    int (*init)(EVP_CIPHER_CTX *ctx, const unsigned char *key,
305
        const unsigned char *iv, int enc);
306
    /* encrypt/decrypt data */
307
    int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
308
        const unsigned char *in, size_t inl);
309
    /* cleanup ctx */
310
    int (*cleanup)(EVP_CIPHER_CTX *);
311
    /* how big ctx->cipher_data needs to be */
312
    int ctx_size;
313
    /* Populate a ASN1_TYPE with parameters */
314
    int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
315
    /* Get parameters from a ASN1_TYPE */
316
    int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
317
    /* Miscellaneous operations */
318
    int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
319
    /* Application data */
320
    void *app_data;
321
322
    /* New structure members */
323
    /* Above comment to be removed when legacy has gone */
324
    int name_id;
325
    char *type_name;
326
    const char *description;
327
    OSSL_PROVIDER *prov;
328
    CRYPTO_REF_COUNT refcnt;
329
    CRYPTO_RWLOCK *lock;
330
    OSSL_FUNC_cipher_newctx_fn *newctx;
331
    OSSL_FUNC_cipher_encrypt_init_fn *einit;
332
    OSSL_FUNC_cipher_decrypt_init_fn *dinit;
333
    OSSL_FUNC_cipher_update_fn *cupdate;
334
    OSSL_FUNC_cipher_final_fn *cfinal;
335
    OSSL_FUNC_cipher_cipher_fn *ccipher;
336
    OSSL_FUNC_cipher_freectx_fn *freectx;
337
    OSSL_FUNC_cipher_dupctx_fn *dupctx;
338
    OSSL_FUNC_cipher_get_params_fn *get_params;
339
    OSSL_FUNC_cipher_get_ctx_params_fn *get_ctx_params;
340
    OSSL_FUNC_cipher_set_ctx_params_fn *set_ctx_params;
341
    OSSL_FUNC_cipher_gettable_params_fn *gettable_params;
342
    OSSL_FUNC_cipher_gettable_ctx_params_fn *gettable_ctx_params;
343
    OSSL_FUNC_cipher_settable_ctx_params_fn *settable_ctx_params;
344
} /* EVP_CIPHER */;
345
346
/* Macros to code block cipher wrappers */
347
348
/* Wrapper functions for each cipher mode */
349
350
#define EVP_C_DATA(kstruct, ctx) \
351
0
    ((kstruct *)EVP_CIPHER_CTX_get_cipher_data(ctx))
352
353
#define BLOCK_CIPHER_ecb_loop()                       \
354
0
    size_t i, bl;                                     \
355
0
    bl = EVP_CIPHER_CTX_get0_cipher(ctx)->block_size; \
356
0
    if (inl < bl)                                     \
357
0
        return 1;                                     \
358
0
    inl -= bl;                                        \
359
0
    for (i = 0; i <= inl; i += bl)
360
361
#define BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched)                                                            \
362
    static int cname##_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)           \
363
0
    {                                                                                                                     \
364
0
        BLOCK_CIPHER_ecb_loop()                                                                                           \
365
0
            cprefix##_ecb_encrypt(in + i, out + i, &EVP_C_DATA(kstruct, ctx)->ksched, EVP_CIPHER_CTX_is_encrypting(ctx)); \
366
0
        return 1;                                                                                                         \
367
0
    }
Unexecuted instantiation: e_aria.c:aria_128_ecb_cipher
Unexecuted instantiation: e_aria.c:aria_192_ecb_cipher
Unexecuted instantiation: e_aria.c:aria_256_ecb_cipher
Unexecuted instantiation: e_bf.c:bf_ecb_cipher
Unexecuted instantiation: e_cast.c:cast5_ecb_cipher
Unexecuted instantiation: e_rc2.c:rc2_ecb_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_ecb_cipher
Unexecuted instantiation: e_seed.c:seed_ecb_cipher
Unexecuted instantiation: e_sm4.c:sm4_ecb_cipher
368
369
0
#define EVP_MAXCHUNK ((size_t)1 << 30)
370
371
#define BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)                                                      \
372
    static int cname##_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)            \
373
0
    {                                                                                                                      \
374
0
        while (inl >= EVP_MAXCHUNK) {                                                                                      \
375
0
            int num = EVP_CIPHER_CTX_get_num(ctx);                                                                         \
376
0
            cprefix##_ofb##cbits##_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, &num); \
377
0
            EVP_CIPHER_CTX_set_num(ctx, num);                                                                              \
378
0
            inl -= EVP_MAXCHUNK;                                                                                           \
379
0
            in += EVP_MAXCHUNK;                                                                                            \
380
0
            out += EVP_MAXCHUNK;                                                                                           \
381
0
        }                                                                                                                  \
382
0
        if (inl) {                                                                                                         \
383
0
            int num = EVP_CIPHER_CTX_get_num(ctx);                                                                         \
384
0
            cprefix##_ofb##cbits##_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, &num);          \
385
0
            EVP_CIPHER_CTX_set_num(ctx, num);                                                                              \
386
0
        }                                                                                                                  \
387
0
        return 1;                                                                                                          \
388
0
    }
Unexecuted instantiation: e_aria.c:aria_128_ofb_cipher
Unexecuted instantiation: e_aria.c:aria_192_ofb_cipher
Unexecuted instantiation: e_aria.c:aria_256_ofb_cipher
Unexecuted instantiation: e_bf.c:bf_ofb_cipher
Unexecuted instantiation: e_cast.c:cast5_ofb_cipher
Unexecuted instantiation: e_idea.c:idea_ofb_cipher
Unexecuted instantiation: e_rc2.c:rc2_ofb_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_ofb_cipher
Unexecuted instantiation: e_seed.c:seed_ofb_cipher
Unexecuted instantiation: e_sm4.c:sm4_ofb_cipher
389
390
#define BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched)                                                                                 \
391
    static int cname##_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)                                \
392
0
    {                                                                                                                                          \
393
0
        while (inl >= EVP_MAXCHUNK) {                                                                                                          \
394
0
            cprefix##_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx)); \
395
0
            inl -= EVP_MAXCHUNK;                                                                                                               \
396
0
            in += EVP_MAXCHUNK;                                                                                                                \
397
0
            out += EVP_MAXCHUNK;                                                                                                               \
398
0
        }                                                                                                                                      \
399
0
        if (inl)                                                                                                                               \
400
0
            cprefix##_cbc_encrypt(in, out, (long)inl, &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv, EVP_CIPHER_CTX_is_encrypting(ctx));          \
401
0
        return 1;                                                                                                                              \
402
0
    }
Unexecuted instantiation: e_aria.c:aria_128_cbc_cipher
Unexecuted instantiation: e_aria.c:aria_192_cbc_cipher
Unexecuted instantiation: e_aria.c:aria_256_cbc_cipher
Unexecuted instantiation: e_bf.c:bf_cbc_cipher
Unexecuted instantiation: e_cast.c:cast5_cbc_cipher
Unexecuted instantiation: e_idea.c:idea_cbc_cipher
Unexecuted instantiation: e_rc2.c:rc2_cbc_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_cbc_cipher
Unexecuted instantiation: e_seed.c:seed_cbc_cipher
Unexecuted instantiation: e_sm4.c:sm4_cbc_cipher
403
404
#define BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched)                                                                                       \
405
    static int cname##_cfb##cbits##_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl)                                    \
406
0
    {                                                                                                                                                       \
407
0
        size_t chunk = EVP_MAXCHUNK;                                                                                                                        \
408
0
        if (cbits == 1)                                                                                                                                     \
409
0
            chunk >>= 3;                                                                                                                                    \
410
0
        if (inl < chunk)                                                                                                                                    \
411
0
            chunk = inl;                                                                                                                                    \
412
0
        while (inl && inl >= chunk) {                                                                                                                       \
413
0
            int num = EVP_CIPHER_CTX_get_num(ctx);                                                                                                          \
414
0
            cprefix##_cfb##cbits##_encrypt(in, out, (long)((cbits == 1) && !EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) ? chunk * 8 : chunk), \
415
0
                &EVP_C_DATA(kstruct, ctx)->ksched, ctx->iv,                                                                                                 \
416
0
                &num, EVP_CIPHER_CTX_is_encrypting(ctx));                                                                                                   \
417
0
            EVP_CIPHER_CTX_set_num(ctx, num);                                                                                                               \
418
0
            inl -= chunk;                                                                                                                                   \
419
0
            in += chunk;                                                                                                                                    \
420
0
            out += chunk;                                                                                                                                   \
421
0
            if (inl < chunk)                                                                                                                                \
422
0
                chunk = inl;                                                                                                                                \
423
0
        }                                                                                                                                                   \
424
0
        return 1;                                                                                                                                           \
425
0
    }
Unexecuted instantiation: e_aria.c:aria_128_cfb128_cipher
Unexecuted instantiation: e_aria.c:aria_192_cfb128_cipher
Unexecuted instantiation: e_aria.c:aria_256_cfb128_cipher
Unexecuted instantiation: e_aria.c:aria_128_cfb1_cipher
Unexecuted instantiation: e_aria.c:aria_192_cfb1_cipher
Unexecuted instantiation: e_aria.c:aria_256_cfb1_cipher
Unexecuted instantiation: e_aria.c:aria_128_cfb8_cipher
Unexecuted instantiation: e_aria.c:aria_192_cfb8_cipher
Unexecuted instantiation: e_aria.c:aria_256_cfb8_cipher
Unexecuted instantiation: e_bf.c:bf_cfb64_cipher
Unexecuted instantiation: e_cast.c:cast5_cfb64_cipher
Unexecuted instantiation: e_idea.c:idea_cfb64_cipher
Unexecuted instantiation: e_rc2.c:rc2_cfb64_cipher
Unexecuted instantiation: e_rc5.c:rc5_32_12_16_cfb64_cipher
Unexecuted instantiation: e_seed.c:seed_cfb128_cipher
Unexecuted instantiation: e_sm4.c:sm4_cfb128_cipher
426
427
#define BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched) \
428
    BLOCK_CIPHER_func_cbc(cname, cprefix, kstruct, ksched)             \
429
        BLOCK_CIPHER_func_cfb(cname, cprefix, cbits, kstruct, ksched)  \
430
            BLOCK_CIPHER_func_ecb(cname, cprefix, kstruct, ksched)     \
431
                BLOCK_CIPHER_func_ofb(cname, cprefix, cbits, kstruct, ksched)
432
433
#define BLOCK_CIPHER_def1(cname, nmode, mode, MODE, kstruct, nid, block_size, \
434
    key_len, iv_len, flags, init_key, cleanup,                                \
435
    set_asn1, get_asn1, ctrl)                                                 \
436
    static const EVP_CIPHER cname##_##mode = {                                \
437
        nid##_##nmode, block_size, key_len, iv_len,                           \
438
        flags | EVP_CIPH_##MODE##_MODE,                                       \
439
        EVP_ORIG_GLOBAL,                                                      \
440
        init_key,                                                             \
441
        cname##_##mode##_cipher,                                              \
442
        cleanup,                                                              \
443
        sizeof(kstruct),                                                      \
444
        set_asn1, get_asn1,                                                   \
445
        ctrl,                                                                 \
446
        NULL                                                                  \
447
    };                                                                        \
448
9.84k
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb128
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb128
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb128
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb1
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb1
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb1
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_128_cfb8
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_192_cfb8
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_aria_256_cfb8
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_bf_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_cast5_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb1
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_cfb8
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
Unexecuted instantiation: EVP_des_ede_ecb
EVP_des_ede3_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
Unexecuted instantiation: EVP_des_ede3_ecb
EVP_des_ede3_cfb1
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_des_ede3_cfb8
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_idea_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc2_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_cfb64
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_rc5_32_12_16_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_cfb128
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_seed_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_sm4_cbc
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_sm4_cfb128
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_sm4_ofb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
EVP_sm4_ecb
Line
Count
Source
448
164
    const EVP_CIPHER *EVP_##cname##_##mode(void) { return &cname##_##mode; }
449
450
#define BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len,         \
451
    iv_len, flags, init_key, cleanup, set_asn1,                                \
452
    get_asn1, ctrl)                                                            \
453
    BLOCK_CIPHER_def1(cname, cbc, cbc, CBC, kstruct, nid, block_size, key_len, \
454
        iv_len, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
455
456
#define BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len,                 \
457
    iv_len, cbits, flags, init_key, cleanup,                               \
458
    set_asn1, get_asn1, ctrl)                                              \
459
    BLOCK_CIPHER_def1(cname, cfb##cbits, cfb##cbits, CFB, kstruct, nid, 1, \
460
        key_len, iv_len, flags, init_key, cleanup, set_asn1,               \
461
        get_asn1, ctrl)
462
463
#define BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len,          \
464
    iv_len, cbits, flags, init_key, cleanup,                        \
465
    set_asn1, get_asn1, ctrl)                                       \
466
    BLOCK_CIPHER_def1(cname, ofb##cbits, ofb, OFB, kstruct, nid, 1, \
467
        key_len, iv_len, flags, init_key, cleanup, set_asn1,        \
468
        get_asn1, ctrl)
469
470
#define BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len,         \
471
    flags, init_key, cleanup, set_asn1,                                        \
472
    get_asn1, ctrl)                                                            \
473
    BLOCK_CIPHER_def1(cname, ecb, ecb, ECB, kstruct, nid, block_size, key_len, \
474
        0, flags, init_key, cleanup, set_asn1, get_asn1, ctrl)
475
476
#define BLOCK_CIPHER_defs(cname, kstruct,                                             \
477
    nid, block_size, key_len, iv_len, cbits, flags,                                   \
478
    init_key, cleanup, set_asn1, get_asn1, ctrl)                                      \
479
    BLOCK_CIPHER_def_cbc(cname, kstruct, nid, block_size, key_len, iv_len, flags,     \
480
        init_key, cleanup, set_asn1, get_asn1, ctrl)                                  \
481
        BLOCK_CIPHER_def_cfb(cname, kstruct, nid, key_len, iv_len, cbits,             \
482
            flags, init_key, cleanup, set_asn1, get_asn1, ctrl)                       \
483
            BLOCK_CIPHER_def_ofb(cname, kstruct, nid, key_len, iv_len, cbits,         \
484
                flags, init_key, cleanup, set_asn1, get_asn1, ctrl)                   \
485
                BLOCK_CIPHER_def_ecb(cname, kstruct, nid, block_size, key_len, flags, \
486
                    init_key, cleanup, set_asn1, get_asn1, ctrl)
487
488
/*-
489
#define BLOCK_CIPHER_defs(cname, kstruct, \
490
                                nid, block_size, key_len, iv_len, flags,\
491
                                 init_key, cleanup, set_asn1, get_asn1, ctrl)\
492
static const EVP_CIPHER cname##_cbc = {\
493
        nid##_cbc, block_size, key_len, iv_len, \
494
        flags | EVP_CIPH_CBC_MODE,\
495
        EVP_ORIG_GLOBAL,\
496
        init_key,\
497
        cname##_cbc_cipher,\
498
        cleanup,\
499
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
500
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
501
        set_asn1, get_asn1,\
502
        ctrl, \
503
        NULL \
504
};\
505
const EVP_CIPHER *EVP_##cname##_cbc(void) { return &cname##_cbc; }\
506
static const EVP_CIPHER cname##_cfb = {\
507
        nid##_cfb64, 1, key_len, iv_len, \
508
        flags | EVP_CIPH_CFB_MODE,\
509
        EVP_ORIG_GLOBAL,\
510
        init_key,\
511
        cname##_cfb_cipher,\
512
        cleanup,\
513
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
514
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
515
        set_asn1, get_asn1,\
516
        ctrl,\
517
        NULL \
518
};\
519
const EVP_CIPHER *EVP_##cname##_cfb(void) { return &cname##_cfb; }\
520
static const EVP_CIPHER cname##_ofb = {\
521
        nid##_ofb64, 1, key_len, iv_len, \
522
        flags | EVP_CIPH_OFB_MODE,\
523
        EVP_ORIG_GLOBAL,\
524
        init_key,\
525
        cname##_ofb_cipher,\
526
        cleanup,\
527
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
528
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
529
        set_asn1, get_asn1,\
530
        ctrl,\
531
        NULL \
532
};\
533
const EVP_CIPHER *EVP_##cname##_ofb(void) { return &cname##_ofb; }\
534
static const EVP_CIPHER cname##_ecb = {\
535
        nid##_ecb, block_size, key_len, iv_len, \
536
        flags | EVP_CIPH_ECB_MODE,\
537
        EVP_ORIG_GLOBAL,\
538
        init_key,\
539
        cname##_ecb_cipher,\
540
        cleanup,\
541
        sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+\
542
                sizeof((((EVP_CIPHER_CTX *)NULL)->c.kstruct)),\
543
        set_asn1, get_asn1,\
544
        ctrl,\
545
        NULL \
546
};\
547
const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
548
*/
549
550
#define IMPLEMENT_BLOCK_CIPHER(cname, ksched, cprefix, kstruct, nid,        \
551
    block_size, key_len, iv_len, cbits,                                     \
552
    flags, init_key,                                                        \
553
    cleanup, set_asn1, get_asn1, ctrl)                                      \
554
    BLOCK_CIPHER_all_funcs(cname, cprefix, cbits, kstruct, ksched)          \
555
        BLOCK_CIPHER_defs(cname, kstruct, nid, block_size, key_len, iv_len, \
556
            cbits, flags, init_key, cleanup, set_asn1,                      \
557
            get_asn1, ctrl)
558
559
#define IMPLEMENT_CFBR(cipher, cprefix, kstruct, ksched, keysize, cbits, iv_len, fl) \
560
    BLOCK_CIPHER_func_cfb(cipher##_##keysize, cprefix, cbits, kstruct, ksched)       \
561
        BLOCK_CIPHER_def_cfb(cipher##_##keysize, kstruct,                            \
562
            NID_##cipher##_##keysize, keysize / 8, iv_len, cbits,                    \
563
            (fl) | EVP_CIPH_FLAG_DEFAULT_ASN1,                                       \
564
            cipher##_init_key, NULL, NULL, NULL, NULL)
565
566
typedef struct {
567
    unsigned char iv[EVP_MAX_IV_LENGTH];
568
    unsigned int iv_len;
569
    unsigned int tag_len;
570
} evp_cipher_aead_asn1_params;
571
572
int evp_cipher_param_to_asn1_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
573
    evp_cipher_aead_asn1_params *params);
574
575
int evp_cipher_asn1_to_param_ex(EVP_CIPHER_CTX *c, ASN1_TYPE *type,
576
    evp_cipher_aead_asn1_params *params);
577
578
/*
579
 * To support transparent execution of operation in backends other
580
 * than the "origin" key, we support transparent export/import to
581
 * those providers, and maintain a cache of the imported keydata,
582
 * so we don't need to redo the export/import every time we perform
583
 * the same operation in that same provider.
584
 * This requires that the "origin" backend (whether it's a legacy or a
585
 * provider "origin") implements exports, and that the target provider
586
 * has an EVP_KEYMGMT that implements import.
587
 */
588
typedef struct {
589
    EVP_KEYMGMT *keymgmt;
590
    void *keydata;
591
    int selection;
592
} OP_CACHE_ELEM;
593
594
DEFINE_STACK_OF(OP_CACHE_ELEM)
595
596
/*
597
 * An EVP_PKEY can have the following states:
598
 *
599
 * untyped & empty:
600
 *
601
 *     type == EVP_PKEY_NONE && keymgmt == NULL
602
 *
603
 * typed & empty:
604
 *
605
 *     (type != EVP_PKEY_NONE && pkey.ptr == NULL)      ## legacy (libcrypto only)
606
 *     || (keymgmt != NULL && keydata == NULL)          ## provider side
607
 *
608
 * fully assigned:
609
 *
610
 *     (type != EVP_PKEY_NONE && pkey.ptr != NULL)      ## legacy (libcrypto only)
611
 *     || (keymgmt != NULL && keydata != NULL)          ## provider side
612
 *
613
 * The easiest way to detect a legacy key is:
614
 *
615
 *     keymgmt == NULL && type != EVP_PKEY_NONE
616
 *
617
 * The easiest way to detect a provider side key is:
618
 *
619
 *     keymgmt != NULL
620
 */
621
#define evp_pkey_is_blank(pk) \
622
207k
    ((pk)->type == EVP_PKEY_NONE && (pk)->keymgmt == NULL)
623
#define evp_pkey_is_typed(pk) \
624
0
    ((pk)->type != EVP_PKEY_NONE || (pk)->keymgmt != NULL)
625
#ifndef FIPS_MODULE
626
#define evp_pkey_is_assigned(pk) \
627
999k
    ((pk)->pkey.ptr != NULL || (pk)->keydata != NULL)
628
#else
629
#define evp_pkey_is_assigned(pk) \
630
    ((pk)->keydata != NULL)
631
#endif
632
#define evp_pkey_is_legacy(pk) \
633
1.90M
    ((pk)->type != EVP_PKEY_NONE && (pk)->keymgmt == NULL)
634
#define evp_pkey_is_provided(pk) \
635
2.83M
    ((pk)->keymgmt != NULL)
636
637
union legacy_pkey_st {
638
    void *ptr;
639
    struct rsa_st *rsa; /* RSA */
640
#ifndef OPENSSL_NO_DSA
641
    struct dsa_st *dsa; /* DSA */
642
#endif
643
#ifndef OPENSSL_NO_DH
644
    struct dh_st *dh; /* DH */
645
#endif
646
#ifndef OPENSSL_NO_EC
647
    struct ec_key_st *ec; /* ECC */
648
    ECX_KEY *ecx; /* X25519, X448, Ed25519, Ed448 */
649
#endif
650
};
651
652
struct evp_pkey_st {
653
    /* == Legacy attributes == */
654
    int type;
655
    int save_type;
656
657
#ifndef FIPS_MODULE
658
    /*
659
     * Legacy key "origin" is composed of a pointer to an EVP_PKEY_ASN1_METHOD,
660
     * a pointer to a low level key and possibly a pointer to an engine.
661
     */
662
    const EVP_PKEY_ASN1_METHOD *ameth;
663
    ENGINE *engine;
664
    ENGINE *pmeth_engine; /* If not NULL public key ENGINE to use */
665
666
    /* Union to store the reference to an origin legacy key */
667
    union legacy_pkey_st pkey;
668
669
    /* Union to store the reference to a non-origin legacy key */
670
    union legacy_pkey_st legacy_cache_pkey;
671
#endif
672
673
    /* == Common attributes == */
674
    CRYPTO_REF_COUNT references;
675
    CRYPTO_RWLOCK *lock;
676
#ifndef FIPS_MODULE
677
    STACK_OF(X509_ATTRIBUTE) *attributes; /* [ 0 ] */
678
    int save_parameters;
679
    unsigned int foreign : 1; /* the low-level key is using an engine or an app-method */
680
    CRYPTO_EX_DATA ex_data;
681
#endif
682
683
    /* == Provider attributes == */
684
685
    /*
686
     * Provider keydata "origin" is composed of a pointer to an EVP_KEYMGMT
687
     * and a pointer to the provider side key data.  This is never used at
688
     * the same time as the legacy key data above.
689
     */
690
    EVP_KEYMGMT *keymgmt;
691
    void *keydata;
692
    /*
693
     * If any libcrypto code does anything that may modify the keydata
694
     * contents, this dirty counter must be incremented.
695
     */
696
    size_t dirty_cnt;
697
698
    /*
699
     * To support transparent execution of operation in backends other
700
     * than the "origin" key, we support transparent export/import to
701
     * those providers, and maintain a cache of the imported keydata,
702
     * so we don't need to redo the export/import every time we perform
703
     * the same operation in that same provider.
704
     */
705
    STACK_OF(OP_CACHE_ELEM) *operation_cache;
706
707
    /*
708
     * We keep a copy of that "origin"'s dirty count, so we know if the
709
     * operation cache needs flushing.
710
     */
711
    size_t dirty_cnt_copy;
712
713
    /* Cache of key object information */
714
    struct {
715
        int bits;
716
        int security_bits;
717
        int size;
718
    } cache;
719
} /* EVP_PKEY */;
720
721
#define EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)            \
722
3.95M
    ((ctx)->operation == EVP_PKEY_OP_SIGN            \
723
1.86M
        || (ctx)->operation == EVP_PKEY_OP_SIGNCTX   \
724
1.86M
        || (ctx)->operation == EVP_PKEY_OP_VERIFY    \
725
1.86M
        || (ctx)->operation == EVP_PKEY_OP_VERIFYCTX \
726
3.95M
        || (ctx)->operation == EVP_PKEY_OP_VERIFYRECOVER)
727
728
#define EVP_PKEY_CTX_IS_DERIVE_OP(ctx) \
729
1.87M
    ((ctx)->operation == EVP_PKEY_OP_DERIVE)
730
731
#define EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)  \
732
1.45M
    ((ctx)->operation == EVP_PKEY_OP_ENCRYPT \
733
1.45M
        || (ctx)->operation == EVP_PKEY_OP_DECRYPT)
734
735
#define EVP_PKEY_CTX_IS_GEN_OP(ctx)           \
736
1.44M
    ((ctx)->operation == EVP_PKEY_OP_PARAMGEN \
737
1.44M
        || (ctx)->operation == EVP_PKEY_OP_KEYGEN)
738
739
#define EVP_PKEY_CTX_IS_FROMDATA_OP(ctx) \
740
14.3k
    ((ctx)->operation == EVP_PKEY_OP_FROMDATA)
741
742
#define EVP_PKEY_CTX_IS_KEM_OP(ctx)              \
743
1.29M
    ((ctx)->operation == EVP_PKEY_OP_ENCAPSULATE \
744
1.29M
        || (ctx)->operation == EVP_PKEY_OP_DECAPSULATE)
745
746
void openssl_add_all_ciphers_int(void);
747
void openssl_add_all_digests_int(void);
748
void evp_cleanup_int(void);
749
void evp_app_cleanup_int(void);
750
void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
751
    EVP_KEYMGMT **keymgmt,
752
    const char *propquery);
753
#ifndef FIPS_MODULE
754
int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src);
755
void *evp_pkey_get_legacy(EVP_PKEY *pk);
756
void evp_pkey_free_legacy(EVP_PKEY *x);
757
EVP_PKEY *evp_pkcs82pkey_legacy(const PKCS8_PRIV_KEY_INFO *p8inf,
758
    OSSL_LIB_CTX *libctx, const char *propq);
759
#endif
760
761
/*
762
 * KEYMGMT utility functions
763
 */
764
765
/*
766
 * Key import structure and helper function, to be used as an export callback
767
 */
768
struct evp_keymgmt_util_try_import_data_st {
769
    EVP_KEYMGMT *keymgmt;
770
    void *keydata;
771
772
    int selection;
773
};
774
int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg);
775
int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
776
    void *keydata);
777
EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata);
778
779
int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
780
    OSSL_CALLBACK *export_cb, void *export_cbarg);
781
void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
782
    int selection);
783
OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
784
    EVP_KEYMGMT *keymgmt,
785
    int selection);
786
int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk, int locking);
787
int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
788
    void *keydata, int selection);
789
void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk);
790
void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
791
    int selection, const OSSL_PARAM params[]);
792
int evp_keymgmt_util_has(EVP_PKEY *pk, int selection);
793
int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection);
794
int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection);
795
void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
796
    void *genctx, OSSL_CALLBACK *cb, void *cbarg);
797
int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
798
    void *keydata,
799
    char *mdname, size_t mdname_sz);
800
const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
801
    int op_id);
802
803
/*
804
 * KEYMGMT provider interface functions
805
 */
806
void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt);
807
void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keyddata);
808
int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt,
809
    void *keydata, OSSL_PARAM params[]);
810
int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt,
811
    void *keydata, const OSSL_PARAM params[]);
812
void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
813
    const OSSL_PARAM params[]);
814
int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
815
    void *template);
816
int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
817
    const OSSL_PARAM params[]);
818
void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
819
    OSSL_CALLBACK *cb, void *cbarg);
820
void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx);
821
822
int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt);
823
void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
824
    const void *objref, size_t objref_sz);
825
826
int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keyddata, int selection);
827
int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
828
    int selection, int checktype);
829
int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
830
    const void *keydata1, const void *keydata2,
831
    int selection);
832
833
int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
834
    int selection, const OSSL_PARAM params[]);
835
const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
836
    int selection);
837
int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
838
    int selection, OSSL_CALLBACK *param_cb, void *cbarg);
839
const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
840
    int selection);
841
void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt,
842
    const void *keydata_from, int selection);
843
EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
844
    const char *name,
845
    const char *properties);
846
847
/* Pulling defines out of C source files */
848
849
#define EVP_RC4_KEY_SIZE 16
850
#ifndef TLS1_1_VERSION
851
9.31k
#define TLS1_1_VERSION 0x0302
852
#endif
853
854
void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);
855
856
/* EVP_ENCODE_CTX flags */
857
/* Don't generate new lines when encoding */
858
573
#define EVP_ENCODE_CTX_NO_NEWLINES 1
859
/* Use the SRP base64 alphabet instead of the standard one */
860
1.60M
#define EVP_ENCODE_CTX_USE_SRP_ALPHABET 2
861
862
const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
863
    const char *name);
864
const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx,
865
    const char *name);
866
867
int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
868
    const unsigned char *salt, int saltlen, int iter,
869
    const EVP_MD *digest, int keylen,
870
    unsigned char *out,
871
    OSSL_LIB_CTX *libctx, const char *propq);
872
873
#ifndef FIPS_MODULE
874
/*
875
 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
876
 *
877
 * Return 1 on success, 0 or negative for errors.
878
 *
879
 * In particular they return -2 if any of the params is not supported.
880
 *
881
 * They are not available in FIPS_MODULE as they depend on
882
 *      - EVP_PKEY_CTX_{get,set}_params()
883
 *      - EVP_PKEY_CTX_{gettable,settable}_params()
884
 *
885
 */
886
int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
887
int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
888
889
EVP_MD_CTX *evp_md_ctx_new_ex(EVP_PKEY *pkey, const ASN1_OCTET_STRING *id,
890
    OSSL_LIB_CTX *libctx, const char *propq);
891
int evp_pkey_name2type(const char *name);
892
const char *evp_pkey_type2name(int type);
893
894
int evp_pkey_ctx_set1_id_prov(EVP_PKEY_CTX *ctx, const void *id, int len);
895
int evp_pkey_ctx_get1_id_prov(EVP_PKEY_CTX *ctx, void *id);
896
int evp_pkey_ctx_get1_id_len_prov(EVP_PKEY_CTX *ctx, size_t *id_len);
897
898
int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx);
899
#endif /* !defined(FIPS_MODULE) */
900
901
int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx);
902
int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov);
903
904
int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
905
    int loadconfig);
906
int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
907
    int loadconfig, int mirrored);
908
char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig);
909
910
void evp_md_ctx_clear_digest(EVP_MD_CTX *ctx, int force, int keep_digest);
911
912
/* Three possible states: */
913
0
#define EVP_PKEY_STATE_UNKNOWN 0
914
0
#define EVP_PKEY_STATE_LEGACY 1
915
211k
#define EVP_PKEY_STATE_PROVIDER 2
916
int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx);
917
918
/* These two must ONLY be called for provider side operations */
919
int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *ctx,
920
    int keytype, int optype,
921
    int cmd, int p1, void *p2);
922
int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *ctx,
923
    const char *name, const char *value);
924
925
/* These two must ONLY be called for legacy operations */
926
int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params);
927
int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params);
928
929
/* This must ONLY be called for legacy EVP_PKEYs */
930
int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params);
931
932
/* Same as the public get0 functions but are not const */
933
#ifndef OPENSSL_NO_DEPRECATED_3_0
934
DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey);
935
EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey);
936
RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey);
937
#endif
938
939
/* Get internal identification number routines */
940
int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher);
941
int evp_cipher_get_number(const EVP_CIPHER *cipher);
942
int evp_kdf_get_number(const EVP_KDF *kdf);
943
int evp_kem_get_number(const EVP_KEM *wrap);
944
int evp_keyexch_get_number(const EVP_KEYEXCH *keyexch);
945
int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt);
946
int evp_mac_get_number(const EVP_MAC *mac);
947
int evp_md_get_number(const EVP_MD *md);
948
int evp_rand_get_number(const EVP_RAND *rand);
949
int evp_signature_get_number(const EVP_SIGNATURE *signature);
950
951
#endif /* OSSL_CRYPTO_EVP_H */