Coverage Report

Created: 2023-06-07 07:24

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