Rename owl -> yaca
[platform/core/security/yaca.git] / src / crypto.c
1 /*
2  *  Copyright (c) 2016 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 #include "config.h"
20
21 #include <assert.h>
22
23 #include <openssl/crypto.h>
24 #include <openssl/evp.h>
25 #include <openssl/rand.h>
26
27 #include <yaca/crypto.h>
28 #include <yaca/error.h>
29
30 #include "ctx_p.h"
31
32 API int yaca_init(void)
33 {
34
35         OPENSSL_init();
36         OpenSSL_add_all_digests();
37         OpenSSL_add_all_ciphers();
38         /*
39           TODO:
40                 We should prepare for multithreading. Either we or the user should setup static locks.
41                 We should also decide on Openssl config.
42                 Here's a good tutorial for initalization and cleanup: https://wiki.openssl.org/index.php/Library_Initialization
43                 We should also initialize the entropy for random number generator: https://wiki.openssl.org/index.php/Random_Numbers#Initialization
44         */
45         return 0;
46 }
47
48 API void yaca_exit(void)
49 {
50         EVP_cleanup();
51         CRYPTO_cleanup_all_ex_data();
52 }
53
54 API void *yaca_malloc(size_t size)
55 {
56         return OPENSSL_malloc(size);
57 }
58
59 API void *yaca_realloc(void *addr, size_t size)
60 {
61         return OPENSSL_realloc(addr, size);
62 }
63
64 API void yaca_free(void *ptr)
65 {
66         OPENSSL_free(ptr);
67 }
68
69 API int yaca_rand_bytes(char *data, size_t data_len)
70 {
71         int ret;
72
73         if (data == NULL || data_len == 0)
74                 return YACA_ERROR_INVALID_ARGUMENT;
75
76         ret = RAND_bytes((unsigned char *)data, data_len);
77         if (ret == -1)
78                 return YACA_ERROR_NOT_SUPPORTED;
79         if (ret == 1)
80                 return 0;
81
82         return YACA_ERROR_OPENSSL_FAILURE;
83 }
84
85 API int yaca_ctx_set_param(yaca_ctx_h ctx, yaca_ex_param_e param,
86                            const void *value, size_t value_len)
87 {
88         return YACA_ERROR_NOT_IMPLEMENTED;
89 }
90
91 API int yaca_ctx_get_param(const yaca_ctx_h ctx, yaca_ex_param_e param,
92                            void **value, size_t *value_len)
93 {
94         return YACA_ERROR_NOT_IMPLEMENTED;
95 }
96
97 API void yaca_ctx_free(yaca_ctx_h ctx)
98 {
99         yaca_free(ctx);
100         /* TODO: What about digest context? This should free specific contexts as well. */
101 }
102
103 API int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len)
104 {
105         if (ctx == YACA_CTX_NULL)
106                 return YACA_ERROR_INVALID_ARGUMENT;
107
108         return ctx->get_output_length(ctx, input_len);
109 }
110
111 API int yaca_get_iv_length(yaca_enc_algo_e algo,
112                            yaca_block_cipher_mode_e bcm,
113                            size_t key_len)
114 {
115         return YACA_ERROR_NOT_IMPLEMENTED;
116 }