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
30 #include <openssl/ossl_typ.h>
31 #include <openssl/err.h>
32 #include <openssl/evp.h>
33 #include <openssl/opensslv.h>
34 #include <openssl/rand.h>
36 #include <yaca_types.h>
38 #define API __attribute__ ((visibility("default")))
39 #define UNUSED __attribute__((unused))
41 /* Functions that handle the hidden nature of internal
42 * OpenSSL structures that don't exist in OpenSSL < 1.1.0
44 #if OPENSSL_VERSION_NUMBER < 0x10100000L
46 static inline EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
51 static inline int EVP_PKEY_up_ref(EVP_PKEY *pkey)
53 if (CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) <= 0)
58 static inline RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
60 if (pkey->type != EVP_PKEY_RSA)
62 return pkey->pkey.rsa;
65 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
67 enum yaca_context_type_e {
68 YACA_CONTEXT_INVALID = 0,
74 enum encrypt_op_type_e {
81 /* Base structure for crypto contexts - to be inherited */
82 struct yaca_context_s {
83 enum yaca_context_type_e type;
85 void (*context_destroy)(const yaca_context_h ctx);
86 int (*get_output_length)(const yaca_context_h ctx, size_t input_len, size_t *output_len);
87 int (*set_property)(yaca_context_h ctx, yaca_property_e property,
88 const void *value, size_t value_len);
89 int (*get_property)(const yaca_context_h ctx, yaca_property_e property,
90 void **value, size_t *value_len);
93 enum context_state_e {
101 /* Base structure for crypto keys - to be inherited */
103 yaca_key_type_e type;
108 * - YACA_KEY_TYPE_SYMMETRIC
109 * - YACA_KEY_TYPE_DES
112 struct yaca_key_simple_s {
113 struct yaca_key_s key;
121 * - YACA_KEY_TYPE_RSA_PUB
122 * - YACA_KEY_TYPE_RSA_PRIV
123 * - YACA_KEY_TYPE_DSA_PUB
124 * - YACA_KEY_TYPE_DSA_PRIV
125 * - YACA_KEY_TYPE_DH_PUB
126 * - YACA_KEY_TYPE_DH_PRIV
127 * - YACA_KEY_TYPE_EC_PUB
128 * - YACA_KEY_TYPE_EC_PRIV
131 struct yaca_key_evp_s {
132 struct yaca_key_s key;
137 int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md);
139 int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
140 yaca_block_cipher_mode_e bcm,
142 const EVP_CIPHER **cipher);
144 int encrypt_initialize(yaca_context_h *ctx,
145 const EVP_CIPHER *cipher,
146 const yaca_key_h sym_key,
148 enum encrypt_op_type_e op_type);
150 int encrypt_update(yaca_context_h ctx,
151 const unsigned char *input, size_t input_len,
152 unsigned char *output, size_t *output_len,
153 enum encrypt_op_type_e op_type);
155 int encrypt_finalize(yaca_context_h ctx,
156 unsigned char *output, size_t *output_len,
157 enum encrypt_op_type_e op_type);
159 struct yaca_key_simple_s *key_get_simple(const yaca_key_h key);
160 struct yaca_key_evp_s *key_get_evp(const yaca_key_h key);
162 yaca_key_h key_copy(const yaca_key_h key);
164 void error_dump(const char *file, int line, const char *function, int code);
165 #define ERROR_DUMP(code) error_dump(__FILE__, __LINE__, __func__, (code))
166 #define ERROR_CLEAR() ERR_clear_error()
169 * Function responsible for translating the openssl error to yaca error and
170 * clearing/dumping the openssl error queue. Use only after openssl function
173 * The function checks only first error in the queue. If the function doesn't
174 * find any error in openssl queue or is not able to translate it, it will
175 * return YACA_ERROR_INTERNAL and dump openssl errors if any. If the
176 * translation succeeds the function will clear the error queue and return the
177 * result of translation.
179 int error_handle(const char *file, int line, const char *function);
180 #define ERROR_HANDLE() error_handle(__FILE__, __LINE__, __func__)
182 int rsa_padding2openssl(yaca_padding_e padding);
184 #endif /* YACA_INTERNAL_H */