2 * Copyright (c) 2016 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
28 #include <openssl/crypto.h>
29 #include <openssl/evp.h>
30 #include <openssl/rand.h>
31 #include <openssl/err.h>
33 #include <yaca_crypto.h>
34 #include <yaca_error.h>
38 static pthread_mutex_t *mutexes = NULL;
40 static void locking_callback(int mode, int type, const char *file, int line)
42 /* Ignore NULL mutexes and lock/unlock error codes as we can't do anything
48 if (mode & CRYPTO_LOCK)
49 pthread_mutex_lock(&mutexes[type]);
50 else if (mode & CRYPTO_UNLOCK)
51 pthread_mutex_unlock(&mutexes[type]);
54 static unsigned long thread_id_callback()
56 return pthread_self();
59 static void destroy_mutexes(int count)
61 if (mutexes != NULL) {
62 for (int i = 0; i < count; i++) {
63 /* Ignore returned value as we can't do anything about it */
64 pthread_mutex_destroy(&mutexes[i]);
71 API int yaca_init(void)
75 return YACA_ERROR_INTERNAL; // TODO introduce new one?
79 /* This should never fail on a /dev/random equipped system. If it does it
80 * means we might need to figure out another way of a truly random seed.
81 * https://wiki.openssl.org/index.php/Random_Numbers
83 * Another things to maybe consider for the future:
84 * - entropy on a mobile device (no mouse/keyboard)
85 * - fork safety: https://wiki.openssl.org/index.php/Random_fork-safety
86 * - hardware random generator (RdRand on new Intels, Samsung hardware?)
88 if (RAND_status() != 1) {
89 ERROR_DUMP(YACA_ERROR_INTERNAL);
90 return YACA_ERROR_INTERNAL;
93 OpenSSL_add_all_digests();
94 OpenSSL_add_all_ciphers();
96 /* enable threads support */
97 ret = yaca_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t), (void**)&mutexes);
98 if (ret != YACA_ERROR_NONE)
101 for (int i = 0; i < CRYPTO_num_locks(); i++) {
102 if (pthread_mutex_init(&mutexes[i], NULL) != 0) {
103 int ret = YACA_ERROR_NONE;
106 ret = YACA_ERROR_OUT_OF_MEMORY;
113 ret = YACA_ERROR_INTERNAL;
121 CRYPTO_set_id_callback(thread_id_callback);
122 CRYPTO_set_locking_callback(locking_callback);
126 We should also decide on Openssl config.
127 Here's a good tutorial for initalization and cleanup: https://wiki.openssl.org/index.php/Library_Initialization
128 We should also initialize the entropy for random number generator: https://wiki.openssl.org/index.php/Random_Numbers#Initialization
131 return YACA_ERROR_NONE;
134 API int yaca_exit(void)
137 ERR_remove_thread_state(NULL);
140 CRYPTO_cleanup_all_ex_data();
142 /* threads support cleanup */
143 CRYPTO_set_id_callback(NULL);
144 CRYPTO_set_locking_callback(NULL);
146 destroy_mutexes(CRYPTO_num_locks());
148 return YACA_ERROR_NONE;
151 API int yaca_malloc(size_t size, void **memory)
153 if (size == 0 || memory == NULL)
154 return YACA_ERROR_INVALID_ARGUMENT;
156 *memory = OPENSSL_malloc(size);
157 if (*memory == NULL) {
158 ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
159 return YACA_ERROR_OUT_OF_MEMORY;
162 return YACA_ERROR_NONE;
165 API int yaca_zalloc(size_t size, void **memory)
167 int ret = yaca_malloc(size, memory);
168 if (ret != YACA_ERROR_NONE)
171 memset(*memory, 0, size);
173 return YACA_ERROR_NONE;
176 API int yaca_realloc(size_t size, void **memory)
178 if (size == 0 || memory == NULL)
179 return YACA_ERROR_INVALID_ARGUMENT;
181 void *tmp = OPENSSL_realloc(*memory, size);
183 ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
184 return YACA_ERROR_OUT_OF_MEMORY;
189 return YACA_ERROR_NONE;
192 API int yaca_free(void *ptr)
196 return YACA_ERROR_NONE;
199 API int yaca_rand_bytes(char *data, size_t data_len)
203 if (data == NULL || data_len == 0)
204 return YACA_ERROR_INVALID_ARGUMENT;
206 ret = RAND_bytes((unsigned char *)data, data_len);
208 ret = YACA_ERROR_INTERNAL;
213 return YACA_ERROR_NONE;
216 API int yaca_ctx_set_param(yaca_ctx_h ctx, yaca_ex_param_e param,
217 const void *value, size_t value_len)
219 if (ctx == YACA_CTX_NULL || ctx->set_param == NULL)
220 return YACA_ERROR_INVALID_ARGUMENT;
222 return ctx->set_param(ctx, param, value, value_len);
225 API int yaca_ctx_get_param(const yaca_ctx_h ctx, yaca_ex_param_e param,
226 void **value, size_t *value_len)
228 if (ctx == YACA_CTX_NULL || ctx->get_param == NULL)
229 return YACA_ERROR_INVALID_ARGUMENT;
231 return ctx->get_param(ctx, param, value, value_len);
234 API int yaca_ctx_free(yaca_ctx_h ctx)
236 if (ctx != YACA_CTX_NULL) {
237 assert(ctx->ctx_destroy != NULL);
238 ctx->ctx_destroy(ctx);
242 return YACA_ERROR_NONE;
245 API int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
247 if (ctx == YACA_CTX_NULL)
248 return YACA_ERROR_INVALID_ARGUMENT;
250 return ctx->get_output_length(ctx, input_len, output_len);
253 API int yaca_memcmp(const void *first, const void *second, size_t len)
255 if (CRYPTO_memcmp(first, second, len) == 0)
256 return YACA_ERROR_NONE;
258 return YACA_ERROR_DATA_MISMATCH;