Coverage Report

Created: 2025-12-10 06:24

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