ACR: Return error codes from all API functions 89/72989/4
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 3 Jun 2016 11:06:47 +0000 (13:06 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Mon, 6 Jun 2016 12:41:18 +0000 (14:41 +0200)
According to HQ Tizen API development Team all API functions must return error
codes. Code adjusted.

Change-Id: Ie6e80480cad0cf32094e78898575fa6c4af91a5e

15 files changed:
api/yaca/yaca_crypto.h
api/yaca/yaca_key.h
examples/encrypt.c
examples/encrypt_aes_gcm_ccm.c
examples/key_exchange.c
examples/misc.c
examples/seal.c
examples/sign.c
src/crypto.c
src/digest.c
src/encrypt.c
src/key.c
src/seal.c
src/sign.c
src/simple.c

index 3105e34..b342ec0 100644 (file)
@@ -66,54 +66,74 @@ int yaca_init(void);
  * @since_tizen 3.0
  *
  * @see yaca_init()
+ *
+ * @return #YACA_ERROR_NONE on success
+ * @retval #YACA_ERROR_NONE Successful
  */
-void yaca_exit(void);
+int yaca_exit(void);
 
 /**
  * @brief  Allocates the memory.
  *
  * @since_tizen 3.0
  *
- * @param[in] size  Size of the allocation (bytes)
+ * @param[in]  size   Size of the allocation (bytes)
+ * @param[out] memory Allocated memory
  *
- * @return NULL on failure, pointer to allocated memory otherwise
+ * @return #YACA_ERROR_NONE on success, negative on error
+ * @retval #YACA_ERROR_NONE Successful
+ * @retval #YACA_ERROR_INVALID_ARGUMENT Required parameters have incorrect values (NULL, 0)
+ * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
  *
  * @see yaca_zalloc()
  * @see yaca_realloc()
  * @see yaca_free()
  */
-void *yaca_malloc(size_t size);
+int yaca_malloc(size_t size, void **memory);
 
 /**
  * @brief  Allocates the zeroed memory.
  *
  * @since_tizen 3.0
  *
- * @param[in] size  Size of the allocation (bytes)
+ * @param[in]  size    Size of the allocation (bytes)
+ * @param[out] memory  Allocated memory
  *
- * @return NULL on failure, pointer to allocated and zeroed memory otherwise
+ * @return #YACA_ERROR_NONE on success, negative on error
+ * @retval #YACA_ERROR_NONE Successful
+ * @retval #YACA_ERROR_INVALID_ARGUMENT Required parameters have incorrect values (NULL, 0)
+ * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
  *
  * @see yaca_malloc()
  * @see yaca_realloc()
  * @see yaca_free()
  */
-void *yaca_zalloc(size_t size);
+int yaca_zalloc(size_t size, void **memory);
 
 /**
  * @brief  Re-allocates the memory.
  *
  * @since_tizen 3.0
  *
- * @param[in] addr  Address of the memory to be reallocated
- * @param[in] size  Size of the new allocation (bytes)
+ * @remarks  In case of failure the function doesn't free the memory pointed by @b memory.
+ *
+ * @remarks  If @b *memory is NULL then the call is equivalent to yaca_malloc().
  *
- * @return NULL on failure, pointer to allocated memory otherwise
+ * @remarks  If the function fails the contents of @b memory will be left unchanged.
+ *
+ * @param[in]     size    Size of the new allocation (bytes)
+ * @param[in,out] memory  Memory to be reallocated
+ *
+ * @return #YACA_ERROR_NONE on success, negative on error
+ * @retval #YACA_ERROR_NONE Successful
+ * @retval #YACA_ERROR_INVALID_ARGUMENT Required parameters have incorrect values (NULL, 0)
+ * @retval #YACA_ERROR_OUT_OF_MEMORY Out of memory error
  *
  * @see yaca_malloc()
  * @see yaca_zalloc()
  * @see yaca_free()
  */
-void *yaca_realloc(void *addr, size_t size);
+int yaca_realloc(size_t size, void **memory);
 
 /**
  * @brief  Frees the memory allocated by yaca_malloc(), yaca_zalloc(),
@@ -123,11 +143,14 @@ void *yaca_realloc(void *addr, size_t size);
  *
  * @param[in] ptr  Pointer to the memory to be freed
  *
+ * @return #YACA_ERROR_NONE on success
+ * @retval #YACA_ERROR_NONE Successful
+ *
  * @see yaca_malloc()
  * @see yaca_zalloc()
  * @see yaca_realloc()
  */
-void yaca_free(void *ptr);
+int yaca_free(void *ptr);
 
 /**
  * @brief  Generates random data.
@@ -204,10 +227,13 @@ int yaca_ctx_get_param(const yaca_ctx_h ctx,
  *
  * @param[in,out] ctx  Crypto context
  *
+ * @return #YACA_ERROR_NONE on success
+ * @retval #YACA_ERROR_NONE Successful
+ *
  * @see #yaca_ctx_h
  *
  */
-void yaca_ctx_free(yaca_ctx_h ctx);
+int yaca_ctx_free(yaca_ctx_h ctx);
 
 /**
  * @brief  Returns the output length for a given algorithm. Can only be called
index daf55a8..8c70a1e 100644 (file)
@@ -229,11 +229,14 @@ int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key);
  *
  * @param[in,out] key  Key to be freed
  *
+ * @return #YACA_ERROR_NONE on success
+ * @retval #YACA_ERROR_NONE Successful
+ *
  * @see yaca_key_import()
  * @see yaca_key_export()
  * @see yaca_key_gen()
  */
-void yaca_key_free(yaca_key_h key);
+int yaca_key_free(yaca_key_h key);
 
 /**@}*/
 
index b56f364..f4a460a 100644 (file)
@@ -121,7 +121,7 @@ void encrypt_advanced(const yaca_enc_algo_e algo,
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
-               if ((enc = yaca_malloc(enc_size)) == NULL)
+               if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
                out_size = enc_size;
@@ -153,7 +153,7 @@ void encrypt_advanced(const yaca_enc_algo_e algo,
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
-               if ((dec = yaca_malloc(dec_size)) == NULL)
+               if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
                out_size = dec_size;
index 08f8e92..11f58e5 100644 (file)
@@ -70,13 +70,13 @@ void encrypt_decrypt_aes_gcm(void)
        if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((aad = yaca_zalloc(aad_size)) == NULL)
+       if (yaca_zalloc(aad_size, (void**)&aad) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_rand_bytes(aad, aad_size) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((tag = yaca_zalloc(tag_size)) == NULL)
+       if (yaca_zalloc(tag_size, (void**)&tag) != YACA_ERROR_NONE)
                goto exit;
 
        /* Encryption */
@@ -96,7 +96,7 @@ void encrypt_decrypt_aes_gcm(void)
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
-               if ((enc = yaca_malloc(enc_size)) == NULL)
+               if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
                out_size = enc_size;
@@ -140,7 +140,7 @@ void encrypt_decrypt_aes_gcm(void)
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
-               if ((dec = yaca_malloc(dec_size)) == NULL)
+               if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
                out_size = dec_size;
@@ -210,13 +210,13 @@ void encrypt_decrypt_aes_ccm(void)
        if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((aad = yaca_zalloc(aad_size)) == NULL)
+       if (yaca_zalloc(aad_size, (void**)&aad) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_rand_bytes(aad, aad_size) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((tag = yaca_zalloc(tag_size)) == NULL)
+       if (yaca_zalloc(tag_size, (void**)&tag) != YACA_ERROR_NONE)
                goto exit;
 
        /* Encryption */
@@ -244,7 +244,7 @@ void encrypt_decrypt_aes_ccm(void)
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
-               if ((enc = yaca_malloc(enc_size)) == NULL)
+               if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
                out_size = enc_size;
@@ -291,7 +291,7 @@ void encrypt_decrypt_aes_ccm(void)
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
-               if ((dec = yaca_malloc(dec_size)) == NULL)
+               if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
                out_size = dec_size;
index 3bc8e32..7a3ee5c 100644 (file)
@@ -64,8 +64,7 @@ void key_exchange_dh(void)
        rewind(fp);
 
        /* allocate memory for entire content */
-       buffer = yaca_malloc(size+1);
-       if (buffer == NULL)
+       if (yaca_malloc(size + 1, (void**)&buffer) != YACA_ERROR_NONE)
                goto exit;
 
        /* copy the file into the buffer */
@@ -127,8 +126,7 @@ void key_exchange_ecdh(void)
        rewind(fp);
 
        /* allocate memory for entire content */
-       buffer = yaca_malloc(size+1);
-       if (buffer == NULL)
+       if (yaca_malloc(size + 1, (void**)&buffer) != YACA_ERROR_NONE)
                goto exit;
 
        /* copy the file into the buffer */
index 04e30fd..5a121a8 100644 (file)
@@ -30,6 +30,7 @@
 #include <openssl/bio.h>
 
 #include <yaca_crypto.h>
+#include <yaca_error.h>
 
 #include "misc.h"
 
@@ -92,18 +93,15 @@ int read_file(const char *path, char **data, size_t *data_len)
 
                if (read > 0) {
                        if (buf == NULL) {
-                               buf = yaca_malloc(read);
-                               if (buf == NULL) {
+                               if (yaca_malloc(read, (void**)&buf) != YACA_ERROR_NONE) {
                                        ret = -1;
                                        break;
                                }
                        } else {
-                               char *new_buf = yaca_realloc(buf, buf_len + read);
-                               if (new_buf == NULL) {
+                               if (yaca_realloc(buf_len + read, (void**)&buf) != YACA_ERROR_NONE) {
                                        ret = -1;
                                        break;
                                }
-                               buf = new_buf;
                        }
 
                        memcpy(buf + buf_len, tmp, read);
@@ -132,7 +130,6 @@ int read_file(const char *path, char **data, size_t *data_len)
 int read_stdin_line(const char *prompt, char **string)
 {
        char *buf = NULL;
-       char *ret;
        size_t size;
        ssize_t read;
 
@@ -145,8 +142,7 @@ int read_stdin_line(const char *prompt, char **string)
                return -1;
        }
 
-       ret = yaca_realloc(buf, read);
-       if (ret == NULL) {
+       if (yaca_realloc(read, (void**)&buf) != YACA_ERROR_NONE) {
                free(buf);
                return -1;
        }
index 23d8400..db5ab36 100644 (file)
@@ -75,7 +75,7 @@ void encrypt_seal(void)
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
-               if ((enc = yaca_malloc(enc_size)) == NULL)
+               if (yaca_malloc(enc_size, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
                /* Seal and finalize */
@@ -108,7 +108,7 @@ void encrypt_seal(void)
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
-               if ((dec = yaca_malloc(dec_size)) == NULL)
+               if (yaca_malloc(dec_size, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
                /* Open and finalize */
index 111f704..e4c2a6f 100644 (file)
@@ -195,7 +195,7 @@ void sign_verify_asym(yaca_key_type_e type, const char *algo)
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((signature = yaca_malloc(signature_len)) == NULL)
+       if (yaca_malloc(signature_len, (void**)&signature) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_sign_final(ctx, signature, &signature_len) != YACA_ERROR_NONE)
@@ -252,7 +252,7 @@ void sign_verify_hmac(void)
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((signature1 = yaca_malloc(signature_len)) == NULL)
+       if (yaca_malloc(signature_len, (void**)&signature1) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_sign_final(ctx, signature1, &signature_len) != YACA_ERROR_NONE)
@@ -274,7 +274,7 @@ void sign_verify_hmac(void)
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((signature2 = yaca_malloc(signature_len)) == NULL)
+       if (yaca_malloc(signature_len, (void**)&signature2) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_sign_final(ctx, signature2, &signature_len) != YACA_ERROR_NONE)
@@ -315,7 +315,7 @@ void sign_verify_cmac(void)
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((signature1 = yaca_malloc(signature_len)) == NULL)
+       if (yaca_malloc(signature_len, (void**)&signature1) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_sign_final(ctx, signature1, &signature_len))
@@ -337,7 +337,7 @@ void sign_verify_cmac(void)
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
                goto exit;
 
-       if ((signature2 = yaca_malloc(signature_len)) == NULL)
+       if (yaca_malloc(signature_len, (void**)&signature2) != YACA_ERROR_NONE)
                goto exit;
 
        if (yaca_sign_final(ctx, signature2, &signature_len))
index 57c7371..79ead7a 100644 (file)
@@ -70,6 +70,7 @@ static void destroy_mutexes(int count)
 
 API int yaca_init(void)
 {
+       int ret;
        if (mutexes != NULL)
                return YACA_ERROR_INTERNAL; // TODO introduce new one?
 
@@ -93,9 +94,9 @@ API int yaca_init(void)
        OpenSSL_add_all_ciphers();
 
        /* enable threads support */
-       mutexes = yaca_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
-       if (mutexes == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t), (void**)&mutexes);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        for (int i = 0; i < CRYPTO_num_locks(); i++) {
                if (pthread_mutex_init(&mutexes[i], NULL) != 0) {
@@ -130,7 +131,7 @@ API int yaca_init(void)
        return YACA_ERROR_NONE;
 }
 
-API void yaca_exit(void)
+API int yaca_exit(void)
 {
        ERR_free_strings();
        ERR_remove_thread_state(NULL);
@@ -143,29 +144,56 @@ API void yaca_exit(void)
        CRYPTO_set_locking_callback(NULL);
 
        destroy_mutexes(CRYPTO_num_locks());
+
+       return YACA_ERROR_NONE;
 }
 
-API void *yaca_malloc(size_t size)
+API int yaca_malloc(size_t size, void **memory)
 {
-       return OPENSSL_malloc(size);
+       if (size == 0 || memory == NULL)
+               return YACA_ERROR_INVALID_ARGUMENT;
+
+       *memory = OPENSSL_malloc(size);
+       if (*memory == NULL) {
+               ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
+               return YACA_ERROR_OUT_OF_MEMORY;
+       }
+
+       return YACA_ERROR_NONE;
 }
 
-API void *yaca_zalloc(size_t size)
+API int yaca_zalloc(size_t size, void **memory)
 {
-       void *blob = OPENSSL_malloc(size);
-       if (blob != NULL)
-               memset(blob, 0, size);
-       return blob;
+       int ret = yaca_malloc(size, memory);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
+
+       memset(*memory, 0, size);
+
+       return YACA_ERROR_NONE;
 }
 
-API void *yaca_realloc(void *addr, size_t size)
+API int yaca_realloc(size_t size, void **memory)
 {
-       return OPENSSL_realloc(addr, size);
+       if (size == 0 || memory == NULL)
+               return YACA_ERROR_INVALID_ARGUMENT;
+
+       void *tmp = OPENSSL_realloc(*memory, size);
+       if (tmp == NULL) {
+               ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
+               return YACA_ERROR_OUT_OF_MEMORY;
+       }
+
+       *memory = tmp;
+
+       return YACA_ERROR_NONE;
 }
 
-API void yaca_free(void *ptr)
+API int yaca_free(void *ptr)
 {
        OPENSSL_free(ptr);
+
+       return YACA_ERROR_NONE;
 }
 
 API int yaca_rand_bytes(char *data, size_t data_len)
@@ -203,13 +231,15 @@ API int yaca_ctx_get_param(const yaca_ctx_h ctx, yaca_ex_param_e param,
        return ctx->get_param(ctx, param, value, value_len);
 }
 
-API void yaca_ctx_free(yaca_ctx_h ctx)
+API int yaca_ctx_free(yaca_ctx_h ctx)
 {
        if (ctx != YACA_CTX_NULL) {
                assert(ctx->ctx_destroy != NULL);
                ctx->ctx_destroy(ctx);
                yaca_free(ctx);
        }
+
+       return YACA_ERROR_NONE;
 }
 
 API int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
index 7a19e4a..d36ce67 100644 (file)
@@ -122,9 +122,9 @@ API int yaca_digest_init(yaca_ctx_h *ctx, yaca_digest_algo_e algo)
        if (ctx == NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nc = yaca_zalloc(sizeof(struct yaca_digest_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_digest_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->ctx.type = YACA_CTX_DIGEST;
        nc->ctx.ctx_destroy = destroy_digest_context;
index d56aad1..ab623f9 100644 (file)
@@ -327,9 +327,9 @@ static int encrypt_init(yaca_ctx_h *ctx,
        if (lkey == NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nc = yaca_zalloc(sizeof(struct yaca_encrypt_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_encrypt_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->ctx.type = YACA_CTX_ENCRYPT;
        nc->ctx.ctx_destroy = destroy_encrypt_ctx;
index 7a6c043..7f7a605 100644 (file)
--- a/src/key.c
+++ b/src/key.c
@@ -219,11 +219,9 @@ int import_simple(yaca_key_h *key,
                }
        }
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_data_len);
-       if (nk == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_data_len, (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
 
        memcpy(nk->d, key_data, key_data_len);
        nk->bits = key_data_len * 8;
@@ -381,11 +379,9 @@ int import_evp(yaca_key_h *key,
                goto exit;
        }
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_evp_s));
-       if (nk == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_evp_s), (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
 
        nk->evp = pkey;
        *key = (yaca_key_h)nk;
@@ -404,17 +400,16 @@ int export_simple_raw(struct yaca_key_simple_s *simple_key,
                       char **data,
                       size_t *data_len)
 {
+       int ret;
        assert(simple_key != NULL);
        assert(data != NULL);
        assert(data_len != NULL);
 
        size_t key_len = simple_key->bits / 8;
 
-       *data = yaca_malloc(key_len);
-       if (*data == NULL) {
-               ERROR_DUMP(YACA_ERROR_OUT_OF_MEMORY);
-               return YACA_ERROR_OUT_OF_MEMORY;
-       }
+       ret = yaca_malloc(key_len, (void**)data);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        memcpy(*data, simple_key->d, key_len);
        *data_len = key_len;
@@ -475,12 +470,9 @@ int export_simple_base64(struct yaca_key_simple_s *simple_key,
                goto exit;
        }
 
-       *data = yaca_malloc(bio_data_len);
-       if (*data == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
-               ERROR_DUMP(ret);
+       ret = yaca_malloc(bio_data_len, (void**)data);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
 
        memcpy(*data, bio_data, bio_data_len);
        *data_len = bio_data_len;
@@ -697,12 +689,9 @@ int export_evp(struct yaca_key_evp_s *evp_key,
                goto exit;
        }
 
-       *data = yaca_malloc(bio_data_len);
-       if (*data == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
-               ERROR_DUMP(ret);
+       ret = yaca_malloc(bio_data_len, (void**)data);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
 
        memcpy(*data, bio_data, bio_data_len);
        *data_len = bio_data_len;
@@ -722,9 +711,9 @@ int gen_simple(struct yaca_key_simple_s **out, size_t key_bits)
        struct yaca_key_simple_s *nk;
        size_t key_byte_len = key_bits / 8;
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len);
-       if (nk == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len, (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nk->bits = key_bits;
 
@@ -749,9 +738,9 @@ int gen_simple_des(struct yaca_key_simple_s **out, size_t key_bits)
        struct yaca_key_simple_s *nk;
        size_t key_byte_len = key_bits / 8;
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len);
-       if (nk == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len, (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        DES_cblock *des_key = (DES_cblock*)nk->d;
        if (key_byte_len >= 8) {
@@ -802,9 +791,9 @@ int gen_evp_rsa(struct yaca_key_evp_s **out, size_t key_bits)
        EVP_PKEY_CTX *ctx;
        EVP_PKEY *pkey = NULL;
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_evp_s));
-       if (nk == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_evp_s), (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
        if (ctx == NULL) {
@@ -865,9 +854,9 @@ int gen_evp_dsa(struct yaca_key_evp_s **out, size_t key_bits)
        EVP_PKEY *pkey = NULL;
        EVP_PKEY *params = NULL;
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_evp_s));
-       if (nk == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_evp_s), (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL);
        if (pctx == NULL) {
@@ -1152,9 +1141,9 @@ API int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key)
        if (prv_key == YACA_KEY_NULL || evp_key == NULL || pub_key == NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_evp_s));
-       if (nk == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_evp_s), (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        mem = BIO_new(BIO_s_mem());
        if (mem == NULL) {
@@ -1209,7 +1198,7 @@ exit:
        return ret;
 }
 
-API void yaca_key_free(yaca_key_h key)
+API int yaca_key_free(yaca_key_h key)
 {
        struct yaca_key_simple_s *simple_key = key_get_simple(key);
        struct yaca_key_evp_s *evp_key = key_get_evp(key);
@@ -1221,6 +1210,8 @@ API void yaca_key_free(yaca_key_h key)
                EVP_PKEY_free(evp_key->evp);
                yaca_free(evp_key);
        }
+
+       return YACA_ERROR_NONE;
 }
 
 API int yaca_key_derive_pbkdf2(const char *password,
@@ -1247,9 +1238,9 @@ API int yaca_key_derive_pbkdf2(const char *password,
        if (ret != YACA_ERROR_NONE)
                return ret;
 
-       nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len);
-       if (nk == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len, (void**)&nk);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nk->bits = key_bits;
        nk->key.type = YACA_KEY_TYPE_SYMMETRIC; // TODO: how to handle other keys?
index 7dd3ba8..aab049b 100644 (file)
@@ -124,9 +124,9 @@ static int seal_init(yaca_ctx_h *ctx,
        lpub = key_get_evp(pub_key);
        assert(lpub);
 
-       nc = yaca_zalloc(sizeof(struct yaca_seal_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_seal_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->ctx.type = YACA_CTX_SEAL;
        nc->ctx.ctx_destroy = destroy_seal_ctx;
@@ -148,11 +148,9 @@ static int seal_init(yaca_ctx_h *ctx,
        }
 
        pub_key_length = ret;
-       lkey = yaca_zalloc(sizeof(struct yaca_key_simple_s) + pub_key_length);
-       if (lkey == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + pub_key_length, (void**)&lkey);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
        key_data = (unsigned char*)lkey->d;
 
        ret = encrypt_get_algorithm(algo, bcm, sym_key_bits, &cipher);
@@ -168,11 +166,9 @@ static int seal_init(yaca_ctx_h *ctx,
 
        iv_length = ret;
        if (iv_length > 0) {
-               liv = yaca_zalloc(sizeof(struct yaca_key_simple_s) + iv_length);
-               if (liv == NULL) {
-                       ret = YACA_ERROR_OUT_OF_MEMORY;
+               ret = yaca_zalloc(sizeof(struct yaca_key_simple_s) + iv_length, (void**)&liv);
+               if (ret != YACA_ERROR_NONE)
                        goto exit;
-               }
                iv_data = (unsigned char*)liv->d;
        }
 
@@ -245,9 +241,9 @@ static int open_init(yaca_ctx_h *ctx,
        if (lkey == NULL || lkey->key.type != YACA_KEY_TYPE_SYMMETRIC)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nc = yaca_zalloc(sizeof(struct yaca_seal_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_seal_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->ctx.type = YACA_CTX_SEAL;
        nc->ctx.ctx_destroy = destroy_seal_ctx;
index 54ad806..2d8046d 100644 (file)
@@ -223,9 +223,9 @@ int get_sign_param(const yaca_ctx_h ctx,
                return ret;
        }
 
-       *value = yaca_malloc(sizeof(yaca_padding_e));
-       if (*value == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_malloc(sizeof(yaca_padding_e), value);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        memcpy(*value, &padding, sizeof(yaca_padding_e));
        *value_len = sizeof(yaca_padding_e);
@@ -255,9 +255,9 @@ API int yaca_sign_init(yaca_ctx_h *ctx,
                return YACA_ERROR_INVALID_ARGUMENT;
        }
 
-       nc = yaca_zalloc(sizeof(struct yaca_sign_ctx_s));
-       if (nc == NULL)
-               return  YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->op_type = OP_SIGN;
        nc->ctx.type = YACA_CTX_SIGN;
@@ -308,9 +308,9 @@ API int yaca_sign_hmac_init(yaca_ctx_h *ctx,
            (key->type != YACA_KEY_TYPE_SYMMETRIC && key->type != YACA_KEY_TYPE_DES))
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nc = yaca_zalloc(sizeof(struct yaca_sign_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->op_type = OP_SIGN;
        nc->ctx.type = YACA_CTX_SIGN;
@@ -373,9 +373,9 @@ API int yaca_sign_cmac_init(yaca_ctx_h *ctx,
            (key->type != YACA_KEY_TYPE_SYMMETRIC && key->type != YACA_KEY_TYPE_DES))
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       nc = yaca_zalloc(sizeof(struct yaca_sign_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->op_type = OP_SIGN;
        nc->ctx.type = YACA_CTX_SIGN;
@@ -507,9 +507,9 @@ API int yaca_verify_init(yaca_ctx_h *ctx,
                return YACA_ERROR_INVALID_ARGUMENT;
        }
 
-       nc = yaca_zalloc(sizeof(struct yaca_sign_ctx_s));
-       if (nc == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_zalloc(sizeof(struct yaca_sign_ctx_s), (void**)&nc);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        nc->op_type = OP_VERIFY;
        nc->ctx.type = YACA_CTX_SIGN;
index e3196aa..889e858 100644 (file)
@@ -59,8 +59,8 @@ API int yaca_digest_calc(yaca_digest_algo_e algo,
        if (ret != YACA_ERROR_NONE)
                goto exit;
 
-       ldigest = yaca_malloc(ldigest_len);
-       if (ldigest == NULL)
+       ret = yaca_malloc(ldigest_len, (void**)&ldigest);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
 
        ret = yaca_digest_final(ctx, ldigest, &ldigest_len);
@@ -90,7 +90,6 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
        yaca_ctx_h ctx;
        int ret;
        char *lcipher = NULL;
-       char *rcipher = NULL;
        size_t out_len, lcipher_len, written;
 
        if (plain == NULL || plain_len == 0 || cipher == NULL || cipher_len == NULL ||
@@ -116,8 +115,8 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
 
        lcipher_len += out_len;
 
-       lcipher = yaca_malloc(lcipher_len);
-       if (lcipher == NULL)
+       ret = yaca_malloc(lcipher_len, (void**)&lcipher);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
 
        out_len = lcipher_len;
@@ -136,13 +135,11 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
        written += out_len;
        assert(written <= lcipher_len);
 
-       rcipher = yaca_realloc(lcipher, written);
-       if (rcipher == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_realloc(written, (void**)&lcipher);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
 
-       *cipher = rcipher;
+       *cipher = lcipher;
        *cipher_len = written;
        lcipher = NULL;
        ret = YACA_ERROR_NONE;
@@ -166,7 +163,6 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
        yaca_ctx_h ctx;
        int ret;
        char *lplain = NULL;
-       char *rplain = NULL;
        size_t out_len, lplain_len, written;
 
        if (cipher == NULL || cipher_len == 0 || plain == NULL || plain_len == NULL ||
@@ -192,8 +188,8 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
 
        lplain_len += out_len;
 
-       lplain = yaca_malloc(lplain_len);
-       if (lplain == NULL)
+       ret = yaca_malloc(lplain_len, (void**)&lplain);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
 
        out_len = lplain_len;
@@ -212,13 +208,11 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
        written += out_len;
        assert(written <= lplain_len);
 
-       rplain = yaca_realloc(lplain, written);
-       if (rplain == NULL) {
-               ret = YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_realloc(written, (void**)&lplain);
+       if (ret != YACA_ERROR_NONE)
                goto exit;
-       }
 
-       *plain = rplain;
+       *plain = lplain;
        *plain_len = written;
        lplain = NULL;
        ret = YACA_ERROR_NONE;
@@ -246,9 +240,9 @@ static int sign(const yaca_ctx_h ctx, const char *data, size_t data_len,
        if (ret != YACA_ERROR_NONE)
                return ret;
 
-       *signature = yaca_malloc(*signature_len);
-       if (signature == NULL)
-               return YACA_ERROR_OUT_OF_MEMORY;
+       ret = yaca_malloc(*signature_len, (void**)signature);
+       if (ret != YACA_ERROR_NONE)
+               return ret;
 
        ret = yaca_sign_final(ctx, *signature, signature_len);
        if (ret != YACA_ERROR_NONE) {