Those functions can be static now
[platform/core/security/yaca.git] / src / internal.h
1 /*
2  *  Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file internal.h
21  * @brief
22  */
23
24 #ifndef YACA_INTERNAL_H
25 #define YACA_INTERNAL_H
26
27 #include <stddef.h>
28 #include <stdbool.h>
29
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>
35
36 #include <yaca_types.h>
37
38 #define API __attribute__ ((visibility("default")))
39 #define UNUSED __attribute__((unused))
40
41 /* Functions that handle the hidden nature of internal
42  * OpenSSL structures that don't exist in OpenSSL < 1.1.0
43  */
44 #if OPENSSL_VERSION_NUMBER < 0x10100000L
45
46 static inline EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
47 {
48         return ctx->pctx;
49 }
50
51 static inline int EVP_PKEY_up_ref(EVP_PKEY *pkey)
52 {
53         if (CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) <= 0)
54                 return 0;
55         return 1;
56 }
57
58 static inline RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
59 {
60         if (pkey->type != EVP_PKEY_RSA)
61                 return NULL;
62         return pkey->pkey.rsa;
63 }
64
65 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
66
67 enum yaca_context_type_e {
68         YACA_CONTEXT_INVALID = 0,
69         YACA_CONTEXT_DIGEST,
70         YACA_CONTEXT_SIGN,
71         YACA_CONTEXT_ENCRYPT
72 };
73
74 enum encrypt_op_type_e {
75         OP_ENCRYPT = 0,
76         OP_DECRYPT = 1,
77         OP_SEAL    = 2,
78         OP_OPEN    = 3
79 };
80
81 /* Base structure for crypto contexts - to be inherited */
82 struct yaca_context_s {
83         enum yaca_context_type_e type;
84
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);
91 };
92
93 enum context_state_e {
94         CTX_INITIALIZED = 0,
95         CTX_MSG_UPDATED,
96         CTX_FINALIZED,
97
98         CTX_COUNT,
99 };
100
101 /* Base structure for crypto keys - to be inherited */
102 struct yaca_key_s {
103         yaca_key_type_e type;
104 };
105
106 /**
107  * Internal type for:
108  * - YACA_KEY_TYPE_SYMMETRIC
109  * - YACA_KEY_TYPE_DES
110  * - YACA_KEY_TYPE_IV
111  */
112 struct yaca_key_simple_s {
113         struct yaca_key_s key;
114
115         size_t bit_len;
116         char d[];
117 };
118
119 /**
120  * Internal type for:
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
129  *
130  */
131 struct yaca_key_evp_s {
132         struct yaca_key_s key;
133
134         EVP_PKEY *evp;
135 };
136
137 int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md);
138
139 int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
140                                                   yaca_block_cipher_mode_e bcm,
141                                                   size_t key_bit_len,
142                                                   const EVP_CIPHER **cipher);
143
144 int encrypt_initialize(yaca_context_h *ctx,
145                                            const EVP_CIPHER *cipher,
146                                            const yaca_key_h sym_key,
147                                            const yaca_key_h iv,
148                                            enum encrypt_op_type_e op_type);
149
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);
154
155 int encrypt_finalize(yaca_context_h ctx,
156                                          unsigned char *output, size_t *output_len,
157                                          enum encrypt_op_type_e op_type);
158
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);
161
162 yaca_key_h key_copy(const yaca_key_h key);
163
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()
167
168 /**
169  * Function responsible for translating the openssl error to yaca error and
170  * clearing/dumping the openssl error queue. Use only after openssl function
171  * failure.
172  *
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.
178  */
179 int error_handle(const char *file, int line, const char *function);
180 #define ERROR_HANDLE() error_handle(__FILE__, __LINE__, __func__)
181
182 int rsa_padding2openssl(yaca_padding_e padding);
183
184 #endif /* YACA_INTERNAL_H */