7cc56418da804bf0e87e7173d299bc3044a353a9
[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 /* Functions that handle the hidden nature of internal
45  * OpenSSL structures that don't exist in OpenSSL < 1.1.0
46  */
47 #if OPENSSL_VERSION_NUMBER < 0x10100000L
48
49 static inline EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
50 {
51         return ctx->pctx;
52 }
53
54 static inline int EVP_PKEY_up_ref(EVP_PKEY *pkey)
55 {
56         if (CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY) <= 0)
57                 return 0;
58         return 1;
59 }
60
61 static inline RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
62 {
63         if (pkey->type != EVP_PKEY_RSA)
64                 return NULL;
65         return pkey->pkey.rsa;
66 }
67
68 #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
69
70 enum yaca_context_type_e {
71         YACA_CONTEXT_INVALID = 0,
72         YACA_CONTEXT_DIGEST,
73         YACA_CONTEXT_SIGN,
74         YACA_CONTEXT_ENCRYPT
75 };
76
77 enum encrypt_op_type_e {
78         OP_ENCRYPT = 0,
79         OP_DECRYPT = 1,
80         OP_SEAL    = 2,
81         OP_OPEN    = 3
82 };
83
84 /* Base structure for crypto contexts - to be inherited */
85 struct yaca_context_s {
86         enum yaca_context_type_e type;
87
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);
94 };
95
96 enum context_state_e {
97         CTX_INITIALIZED = 0,
98         CTX_MSG_UPDATED,
99         CTX_FINALIZED,
100
101         CTX_COUNT,
102 };
103
104 /* Base structure for crypto keys - to be inherited */
105 struct yaca_key_s {
106         yaca_key_type_e type;
107 };
108
109 /**
110  * Internal type for:
111  * - YACA_KEY_TYPE_SYMMETRIC
112  * - YACA_KEY_TYPE_DES
113  * - YACA_KEY_TYPE_IV
114  */
115 struct yaca_key_simple_s {
116         struct yaca_key_s key;
117
118         size_t bit_len;
119         char d[];
120 };
121
122 /**
123  * Internal type for:
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
132  *
133  */
134 struct yaca_key_evp_s {
135         struct yaca_key_s key;
136
137         EVP_PKEY *evp;
138 };
139
140 int digest_get_algorithm(yaca_digest_algorithm_e algo, const EVP_MD **md);
141
142 int encrypt_get_algorithm(yaca_encrypt_algorithm_e algo,
143                                                   yaca_block_cipher_mode_e bcm,
144                                                   size_t key_bit_len,
145                                                   const EVP_CIPHER **cipher);
146
147 int encrypt_initialize(yaca_context_h *ctx,
148                                            const EVP_CIPHER *cipher,
149                                            const yaca_key_h sym_key,
150                                            const yaca_key_h iv,
151                                            enum encrypt_op_type_e op_type);
152
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);
157
158 int encrypt_finalize(yaca_context_h ctx,
159                                          unsigned char *output, size_t *output_len,
160                                          enum encrypt_op_type_e op_type);
161
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);
164
165 int rsa_padding2openssl(yaca_padding_e padding);
166
167
168 #endif /* YACA_INTERNAL_H */