]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/p_seal.c
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include "internal/provider.h"
13 #include <openssl/rand.h>
14 #include <openssl/rsa.h>
15 #include <openssl/evp.h>
16 #include <openssl/objects.h>
17 #include <openssl/x509.h>
18 #include <openssl/evp.h>
20 int EVP_SealInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*type
,
21 unsigned char **ek
, int *ekl
, unsigned char *iv
,
22 EVP_PKEY
**pubk
, int npubk
)
24 unsigned char key
[EVP_MAX_KEY_LENGTH
];
25 const OSSL_PROVIDER
*prov
;
26 OSSL_LIB_CTX
*libctx
= NULL
;
27 EVP_PKEY_CTX
*pctx
= NULL
;
28 const EVP_CIPHER
*cipher
;
33 EVP_CIPHER_CTX_reset(ctx
);
34 if (!EVP_EncryptInit_ex(ctx
, type
, NULL
, NULL
, NULL
))
37 if ((cipher
= EVP_CIPHER_CTX_get0_cipher(ctx
)) != NULL
38 && (prov
= EVP_CIPHER_provider(cipher
)) != NULL
)
39 libctx
= ossl_provider_libctx(prov
);
40 if ((npubk
<= 0) || !pubk
)
43 if (EVP_CIPHER_CTX_rand_key(ctx
, key
) <= 0)
46 len
= EVP_CIPHER_CTX_iv_length(ctx
);
47 if (len
< 0 || RAND_priv_bytes_ex(libctx
, iv
, len
, 0) <= 0)
50 len
= EVP_CIPHER_CTX_key_length(ctx
);
54 if (!EVP_EncryptInit_ex(ctx
, NULL
, NULL
, key
, iv
))
57 for (i
= 0; i
< npubk
; i
++) {
60 pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pubk
[i
], NULL
);
62 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
66 if (EVP_PKEY_encrypt_init(pctx
) <= 0
67 || EVP_PKEY_encrypt(pctx
, ek
[i
], &keylen
, key
, keylen
) <= 0)
70 EVP_PKEY_CTX_free(pctx
);
75 EVP_PKEY_CTX_free(pctx
);
76 OPENSSL_cleanse(key
, sizeof(key
));
80 int EVP_SealFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
83 i
= EVP_EncryptFinal_ex(ctx
, out
, outl
);
85 i
= EVP_EncryptInit_ex(ctx
, NULL
, NULL
, NULL
, NULL
);