2 * Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
24 #ifndef YACA_INTERNAL_H
25 #define YACA_INTERNAL_H
31 #include <openssl/ossl_typ.h>
32 #include <openssl/evp.h>
33 #include <openssl/opensslv.h>
34 #include <openssl/rand.h>
36 #include <yaca_types.h>
41 #define API __attribute__ ((visibility("default")))
42 #define UNUSED __attribute__((unused))
44 /* Functions that handle the hidden nature of internal
45 * OpenSSL structures that don't exist in OpenSSL < 1.1.0
47 #if OPENSSL_VERSION_NUMBER < 0x10100000L
49 static inline EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
54 static inline int EVP_PKEY_up_ref(EVP_PKEY *pkey)
56 if (CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) <= 0)
61 static inline RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
63 if (pkey->type != EVP_PKEY_RSA)
65 return pkey->pkey.rsa;
68 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
70 enum yaca_context_type_e {
71 YACA_CONTEXT_INVALID = 0,
77 enum encrypt_op_type_e {
84 /* Base structure for crypto contexts - to be inherited */
85 struct yaca_context_s {
86 enum yaca_context_type_e type;
88 void (*context_destroy)(const yaca_context_h ctx);
89 int (*get_output_length)(const yaca_context_h ctx, size_t input_len, size_t *output_len);
90 int (*set_property)(yaca_context_h ctx, yaca_property_e property,
91 const void *value, size_t value_len);
92 int (*get_property)(const yaca_context_h ctx, yaca_property_e property,
93 void **value, size_t *value_len);
96 enum context_state_e {
104 /* Base structure for crypto keys - to be inherited */
106 yaca_key_type_e type;
111 * - YACA_KEY_TYPE_SYMMETRIC
112 * - YACA_KEY_TYPE_DES
115 struct yaca_key_simple_s {
116 struct yaca_key_s key;
124 * - YACA_KEY_TYPE_RSA_PUB
125 * - YACA_KEY_TYPE_RSA_PRIV
126 * - YACA_KEY_TYPE_DSA_PUB
127 * - YACA_KEY_TYPE_DSA_PRIV
128 * - YACA_KEY_TYPE_DH_PUB
129 * - YACA_KEY_TYPE_DH_PRIV
130 * - YACA_KEY_TYPE_EC_PUB
131 * - YACA_KEY_TYPE_EC_PRIV
134 struct yaca_key_evp_s {
135 struct yaca_key_s key;
140 int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md);
142 int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
143 yaca_block_cipher_mode_e bcm,
145 const EVP_CIPHER **cipher);
147 int encrypt_initialize(yaca_context_h *ctx,
148 const EVP_CIPHER *cipher,
149 const yaca_key_h sym_key,
151 enum encrypt_op_type_e op_type);
153 int encrypt_update(yaca_context_h ctx,
154 const unsigned char *input, size_t input_len,
155 unsigned char *output, size_t *output_len,
156 enum encrypt_op_type_e op_type);
158 int encrypt_finalize(yaca_context_h ctx,
159 unsigned char *output, size_t *output_len,
160 enum encrypt_op_type_e op_type);
162 struct yaca_key_simple_s *key_get_simple(const yaca_key_h key);
163 struct yaca_key_evp_s *key_get_evp(const yaca_key_h key);
165 int rsa_padding2openssl(yaca_padding_e padding);
168 #endif /* YACA_INTERNAL_H */