]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
0789c7d8 | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
4547a719 | 12 | #include "internal/provider.h" |
ec577822 | 13 | #include <openssl/rand.h> |
3c27208f | 14 | #include <openssl/rsa.h> |
ec577822 BM |
15 | #include <openssl/evp.h> |
16 | #include <openssl/objects.h> | |
17 | #include <openssl/x509.h> | |
29604f49 | 18 | #include <openssl/evp.h> |
d02b48c6 | 19 | |
0f113f3e MC |
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) | |
23 | { | |
24 | unsigned char key[EVP_MAX_KEY_LENGTH]; | |
29604f49 P |
25 | const OSSL_PROVIDER *prov; |
26 | OSSL_LIB_CTX *libctx = NULL; | |
4547a719 | 27 | EVP_PKEY_CTX *pctx = NULL; |
29604f49 | 28 | const EVP_CIPHER *cipher; |
3c184592 | 29 | int i, len; |
2eb2b4f3 | 30 | int rv = 0; |
0f113f3e | 31 | |
29604f49 | 32 | if (type != NULL) { |
846ec07d | 33 | EVP_CIPHER_CTX_reset(ctx); |
0f113f3e MC |
34 | if (!EVP_EncryptInit_ex(ctx, type, NULL, NULL, NULL)) |
35 | return 0; | |
36 | } | |
29604f49 | 37 | if ((cipher = EVP_CIPHER_CTX_get0_cipher(ctx)) != NULL |
ed576acd | 38 | && (prov = EVP_CIPHER_get0_provider(cipher)) != NULL) |
29604f49 | 39 | libctx = ossl_provider_libctx(prov); |
0f113f3e MC |
40 | if ((npubk <= 0) || !pubk) |
41 | return 1; | |
9420b403 | 42 | |
0f113f3e MC |
43 | if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) |
44 | return 0; | |
2eb2b4f3 | 45 | |
ed576acd | 46 | len = EVP_CIPHER_CTX_get_iv_length(ctx); |
5cbd2ea3 | 47 | if (len < 0 || RAND_priv_bytes_ex(libctx, iv, len, 0) <= 0) |
3c184592 P |
48 | goto err; |
49 | ||
ed576acd | 50 | len = EVP_CIPHER_CTX_get_key_length(ctx); |
3c184592 | 51 | if (len < 0) |
2eb2b4f3 | 52 | goto err; |
d02b48c6 | 53 | |
0f113f3e | 54 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) |
2eb2b4f3 | 55 | goto err; |
58964a49 | 56 | |
0f113f3e | 57 | for (i = 0; i < npubk; i++) { |
3c184592 | 58 | size_t keylen = len; |
9420b403 | 59 | |
4547a719 P |
60 | pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pubk[i], NULL); |
61 | if (pctx == NULL) { | |
9420b403 | 62 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
2eb2b4f3 SL |
63 | goto err; |
64 | } | |
9420b403 RL |
65 | |
66 | if (EVP_PKEY_encrypt_init(pctx) <= 0 | |
67 | || EVP_PKEY_encrypt(pctx, ek[i], &keylen, key, keylen) <= 0) | |
68 | goto err; | |
69 | ekl[i] = (int)keylen; | |
70 | EVP_PKEY_CTX_free(pctx); | |
0f113f3e | 71 | } |
4547a719 | 72 | pctx = NULL; |
2eb2b4f3 SL |
73 | rv = npubk; |
74 | err: | |
4547a719 | 75 | EVP_PKEY_CTX_free(pctx); |
2eb2b4f3 SL |
76 | OPENSSL_cleanse(key, sizeof(key)); |
77 | return rv; | |
0f113f3e | 78 | } |
d02b48c6 | 79 | |
e9ba6963 | 80 | int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
0f113f3e MC |
81 | { |
82 | int i; | |
83 | i = EVP_EncryptFinal_ex(ctx, out, outl); | |
84 | if (i) | |
85 | i = EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL); | |
86 | return i; | |
87 | } |