Remove support for OpenSSL 1.0.x, it's EOL
[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
28 #include <stddef.h>
29 #include <stdbool.h>
30
31 #include <openssl/ossl_typ.h>
32 #include <openssl/evp.h>
33 #include <openssl/opensslv.h>
34 #include <openssl/rand.h>
35
36 #include <yaca_types.h>
37
38 #include "debug.h"
39
40
41 #define API __attribute__ ((visibility("default")))
42 #define UNUSED __attribute__((unused))
43
44
45 enum yaca_context_type_e {
46         YACA_CONTEXT_INVALID = 0,
47         YACA_CONTEXT_DIGEST,
48         YACA_CONTEXT_SIGN,
49         YACA_CONTEXT_ENCRYPT
50 };
51
52 enum encrypt_op_type_e {
53         OP_ENCRYPT = 0,
54         OP_DECRYPT = 1,
55         OP_SEAL    = 2,
56         OP_OPEN    = 3
57 };
58
59 /* Base structure for crypto contexts - to be inherited */
60 struct yaca_context_s {
61         enum yaca_context_type_e type;
62
63         void (*context_destroy)(const yaca_context_h ctx);
64         int (*get_output_length)(const yaca_context_h ctx, size_t input_len, size_t *output_len);
65         int (*set_property)(yaca_context_h ctx, yaca_property_e property,
66                                                 const void *value, size_t value_len);
67         int (*get_property)(const yaca_context_h ctx, yaca_property_e property,
68                                                 void **value, size_t *value_len);
69 };
70
71 enum context_state_e {
72         CTX_INITIALIZED = 0,
73         CTX_MSG_UPDATED,
74         CTX_FINALIZED,
75
76         CTX_COUNT,
77 };
78
79 /* Base structure for crypto keys - to be inherited */
80 struct yaca_key_s {
81         yaca_key_type_e type;
82 };
83
84 /**
85  * Internal type for:
86  * - YACA_KEY_TYPE_SYMMETRIC
87  * - YACA_KEY_TYPE_DES
88  * - YACA_KEY_TYPE_IV
89  */
90 struct yaca_key_simple_s {
91         struct yaca_key_s key;
92
93         size_t bit_len;
94         char d[];
95 };
96
97 /**
98  * Internal type for:
99  * - YACA_KEY_TYPE_RSA_PUB
100  * - YACA_KEY_TYPE_RSA_PRIV
101  * - YACA_KEY_TYPE_DSA_PUB
102  * - YACA_KEY_TYPE_DSA_PRIV
103  * - YACA_KEY_TYPE_DH_PUB
104  * - YACA_KEY_TYPE_DH_PRIV
105  * - YACA_KEY_TYPE_EC_PUB
106  * - YACA_KEY_TYPE_EC_PRIV
107  *
108  */
109 struct yaca_key_evp_s {
110         struct yaca_key_s key;
111
112         EVP_PKEY *evp;
113 };
114
115 int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md);
116
117 int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
118                                                   yaca_block_cipher_mode_e bcm,
119                                                   size_t key_bit_len,
120                                                   const EVP_CIPHER **cipher);
121
122 int encrypt_initialize(yaca_context_h *ctx,
123                                            const EVP_CIPHER *cipher,
124                                            const yaca_key_h sym_key,
125                                            const yaca_key_h iv,
126                                            enum encrypt_op_type_e op_type);
127
128 int encrypt_update(yaca_context_h ctx,
129                                    const unsigned char *input, size_t input_len,
130                                    unsigned char *output, size_t *output_len,
131                                    enum encrypt_op_type_e op_type);
132
133 int encrypt_finalize(yaca_context_h ctx,
134                                          unsigned char *output, size_t *output_len,
135                                          enum encrypt_op_type_e op_type);
136
137 struct yaca_key_simple_s *key_get_simple(const yaca_key_h key);
138 struct yaca_key_evp_s *key_get_evp(const yaca_key_h key);
139
140 int rsa_padding2openssl(yaca_padding_e padding);
141
142
143 #endif /* YACA_INTERNAL_H */