From 216474fe7481b73964074d03c013685f22f4f1f0 Mon Sep 17 00:00:00 2001 From: Mateusz Kulikowski Date: Fri, 1 Apr 2016 19:22:15 +0200 Subject: [PATCH] More cleanups: - added missing TODOs as comments - upgraded owl_exit - added license and init/exit to test.c Signed-off-by: Mateusz Kulikowski --- examples/test.c | 26 +++++++++++++++++++++++++- src/crypto.c | 11 ++++++++++- src/key.c | 3 ++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/examples/test.c b/examples/test.c index ef49267..1a1b2a3 100644 --- a/examples/test.c +++ b/examples/test.c @@ -1,7 +1,25 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + #include +#include #include #include - #include "misc.h" /** Simple test for development of library (before API is ready) */ @@ -13,6 +31,10 @@ int main(int argc, char* argv[]) size_t kl; int ret; + ret = owl_init(); + if (ret < 0) + return ret; + printf("Generating key using CryptoAPI.. "); ret = owl_key_gen(&key, OWL_KEY_TYPE_SYMMETRIC, OWL_KEY_UNSAFE_128BIT); if (ret < 0) @@ -27,5 +49,7 @@ int main(int argc, char* argv[]) dump_hex(k, kl, "%zu-bit key: \n", kl); + owl_exit(); + return 0; } diff --git a/src/crypto.c b/src/crypto.c index 733d21b..f9c1e52 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -31,16 +31,24 @@ API int owl_init(void) { + OPENSSL_init(); OpenSSL_add_all_digests(); OpenSSL_add_all_ciphers(); - + /* + TODO: + We should prepare for multithreading. Either we or the user should setup static locks. + We should also decide on Openssl config. + Here's a good tutorial for initalization and cleanup: https://wiki.openssl.org/index.php/Library_Initialization + We should also initialize the entropy for random number generator: https://wiki.openssl.org/index.php/Random_Numbers#Initialization + */ return 0; } API void owl_exit(void) { EVP_cleanup(); + CRYPTO_cleanup_all_ex_data(); } API void *owl_malloc(size_t size) @@ -89,6 +97,7 @@ API int owl_ctx_get_param(const owl_ctx_h ctx, owl_ex_param_e param, API void owl_ctx_free(owl_ctx_h ctx) { owl_free(ctx); + /* TODO: What about digest context? This should free specific contexts as well. */ } API int owl_get_output_length(const owl_ctx_h ctx, size_t input_len) diff --git a/src/key.c b/src/key.c index f7ca6c1..3e66514 100644 --- a/src/key.c +++ b/src/key.c @@ -62,6 +62,7 @@ API int owl_key_import(owl_key_h *key, if (key_fmt != OWL_KEY_FORMAT_RAW) return OWL_ERROR_NOT_IMPLEMENTED; + /* TODO: Overflow on an unsigned value in an undefined behaviour, unless explicitly allowed by a compile flag. */ if (sizeof(struct owl_key_s) + data_len < data_len) return OWL_ERROR_TOO_BIG_ARGUMENT; @@ -69,7 +70,7 @@ API int owl_key_import(owl_key_h *key, if (nk == NULL) return OWL_ERROR_OUT_OF_MEMORY; - memcpy(nk->d, data, data_len); + memcpy(nk->d, data, data_len); /* TODO: CRYPTO_/OPENSSL_... */ nk->length = data_len * 8; nk->type = key_type; *key = nk; -- 2.7.4