Fix API for get_output_length() 32/69532/5
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Fri, 13 May 2016 14:45:41 +0000 (16:45 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 18 May 2016 07:12:52 +0000 (00:12 -0700)
Return the value by size_t param instead of signed int return. The
return value is used only for error codes from now on.

Change-Id: I8c4665342316f35d9aea1d2b3605a1248fc31b17

13 files changed:
api/yaca/crypto.h
examples/digest.c
examples/encrypt.c
examples/encrypt_aes_gcm.c
examples/seal.c
examples/sign.c
src/crypto.c
src/digest.c
src/encrypt.c
src/internal.h
src/seal.c
src/sign.c
src/simple.c

index 205cce2..ec6ac1b 100644 (file)
@@ -162,29 +162,28 @@ void yaca_ctx_free(yaca_ctx_h ctx);
  * @brief  Returns the output length for a given algorithm. Can only be called
  *         on an initialized context.
  *
- * @param[in] ctx        Previously initialized crypto context.
- * @param[in] input_len  Length of the input data to be processed.
+ * @param[in] ctx         Previously initialized crypto context.
+ * @param[in] input_len   Length of the input data to be processed.
+ * @param[in] output_len  Required length of the output.
  *
  * @return negative on error or length of output.
  */
-// TODO: this function should probably return the value by param of
-// size_t type and leave the return int value only to report errors
-int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len);
+int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len);
 
 /**
  * @brief  Wrapper - returns the length of the digest (for a given context).
  */
-#define yaca_get_digest_length(ctxa) yaca_get_output_length((ctxa), 0)
+#define yaca_get_digest_length(ctxa, output_len) yaca_get_output_length((ctxa), 0, (output_len))
 
 /**
  * @brief  Wrapper - returns the length of the signature (for a given context).
  */
-#define yaca_get_sign_length(ctxa) yaca_get_output_length((ctxa), 0)
+#define yaca_get_sign_length(ctxa, output_len) yaca_get_output_length((ctxa), 0, (output_len))
 
 /**
  * @brief  Wrapper - returns the length of the block (for a given context).
  */
-#define yaca_get_block_length(ctxa) yaca_get_output_length((ctxa), 0)
+#define yaca_get_block_length(ctxa, output_len) yaca_get_output_length((ctxa), 0, (output_len))
 
 /**@}*/
 
index 9ee0b41..a5ef093 100644 (file)
@@ -59,10 +59,9 @@ void digest_advanced(void)
        if (ret < 0)
                goto exit_ctx;
 
-       // TODO: rename to yaca_digest_get_length??
        size_t digest_len;
-       digest_len = yaca_get_digest_length(ctx);
-       if (digest_len <= 0)
+       ret = yaca_get_digest_length(ctx, &digest_len);
+       if (ret != 0)
                goto exit_ctx;
 
        {
index 8615abe..6233f7b 100644 (file)
@@ -134,18 +134,14 @@ void encrypt_advanced(void)
                if (ret)
                        goto ex_iv;
 
-               ret = yaca_get_block_length(ctx);
-               if (ret < 0)
+               ret = yaca_get_block_length(ctx, &block_len);
+               if (ret != 0)
                        goto ex_ctx;
 
-               block_len = ret;
-
-               ret = yaca_get_output_length(ctx, LOREM4096_SIZE);
-               if (ret < 0)
+               ret = yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len);
+               if (ret != 0)
                        goto ex_ctx;
 
-               output_len = ret;
-
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
                enc = yaca_malloc(enc_size);
@@ -184,18 +180,14 @@ void encrypt_advanced(void)
                        goto ex_of;
                }
 
-               ret = yaca_get_block_length(ctx);
-               if (ret < 0)
+               ret = yaca_get_block_length(ctx, &block_len);
+               if (ret != 0)
                        goto ex_of;
 
-               block_len = ret;
-
-               ret = yaca_get_output_length(ctx, LOREM4096_SIZE);
-               if (ret < 0)
+               ret = yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len);
+               if (ret != 0)
                        goto ex_ctx;
 
-               output_len = ret;
-
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
                dec = yaca_malloc(dec_size);
index 9bc60fe..23fd4e9 100644 (file)
@@ -52,7 +52,7 @@ void encrypt_decrypt_aes_gcm(void)
        size_t aad_len;
        size_t tag_len;
 
-       printf("Plain data (16 of %zu bytes): %.16s\n", (size_t)4096, lorem4096);
+       printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
 
        /// Key generation
 
@@ -77,6 +77,8 @@ void encrypt_decrypt_aes_gcm(void)
 
        /// Encryption
        {
+               size_t len;
+
                ret = yaca_encrypt_init(&ctx, YACA_ENC_AES, YACA_BCM_GCM, key, iv);
                if (ret < 0)
                        goto clean;
@@ -85,21 +87,20 @@ void encrypt_decrypt_aes_gcm(void)
                if (ret < 0)
                        goto clean;
 
-               ret = yaca_encrypt_update(ctx, lorem4096, 4096, NULL, &ciphertext_len);
-               if (ret != 42)
-                       goto clean;// TODO: what error code?
+               ret = yaca_get_output_length(ctx, LOREM4096_SIZE, &ciphertext_len);
+               if (ret != 0)
+                       goto clean;
 
-               ret = yaca_get_block_length(ctx);
-               if (ret < 0)
+               ret = yaca_get_block_length(ctx, &len);
+               if (ret != 0)
                        goto clean;
 
-               ciphertext_len += ret ; // Add block size for finalize
+               ciphertext_len += len ; // Add block size for finalize
                ciphertext = yaca_malloc(ciphertext_len);
                if (ciphertext == NULL)
                        goto clean;
 
-               size_t len;
-               ret = yaca_encrypt_update(ctx, lorem4096, 4096, ciphertext, &len);
+               ret = yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, ciphertext, &len);
                if (ret < 0)
                        goto clean;
 
@@ -132,15 +133,15 @@ void encrypt_decrypt_aes_gcm(void)
                if (ret < 0)
                        goto clean;
 
-               ret = yaca_decrypt_update(ctx, ciphertext, ciphertext_len, NULL, &plaintext_len);
-               if (ret != 42)
-                       goto clean; // TODO: what error code?
+               ret = yaca_get_output_length(ctx, ciphertext_len, &plaintext_len);
+               if (ret != 0)
+                       goto clean;
 
-               ret = yaca_get_block_length(ctx);
-               if (ret < 0)
+               ret = yaca_get_block_length(ctx, &len);
+               if (ret != 0)
                        goto clean;
 
-               plaintext_len += ret; // Add block size for finalize
+               plaintext_len += len; // Add block size for finalize
                plaintext = yaca_malloc(plaintext_len);
                if (plaintext == NULL)
                        goto clean;
index c75b721..4da8228 100644 (file)
@@ -65,10 +65,10 @@ void encrypt_seal(void)
                if (yaca_seal_init(&ctx, key_pub, algo, bcm, key_bits, &aes_key, &iv) != 0)
                        goto ex_pubk;
 
-               if ((block_len = yaca_get_block_length(ctx)) <= 0)
+               if (yaca_get_block_length(ctx, &block_len) != 0)
                        goto ex_ak;
 
-               if ((output_len = yaca_get_output_length(ctx, LOREM4096_SIZE)) <= 0)
+               if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != 0)
                        goto ex_ak;
 
                /* Calculate max output: size of update + final chunks */
@@ -98,10 +98,10 @@ void encrypt_seal(void)
                if (yaca_open_init(&ctx, key_priv, algo, bcm, key_bits, aes_key, iv) != 0)
                        goto ex_of;
 
-               if ((block_len = yaca_get_block_length(ctx)) <= 0)
+               if (yaca_get_block_length(ctx, &block_len) != 0)
                        goto ex_of;
 
-               if ((output_len = yaca_get_output_length(ctx, LOREM4096_SIZE)) <= 0)
+               if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != 0)
                        goto ex_of;
 
                /* Calculate max output: size of update + final chunks */
index c5c9a5e..dab16bd 100644 (file)
@@ -65,7 +65,7 @@ void sign_verify_asym(yaca_key_type_e type, const char *algo)
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
                goto finish;
 
-       if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
+       if (yaca_get_sign_length(ctx, &signature_len) != 0)
                goto finish;
 
        if ((signature = yaca_malloc(signature_len)) == NULL)
@@ -123,7 +123,7 @@ void sign_verify_hmac(void)
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != 0)
                goto finish;
 
-       if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
+       if (yaca_get_sign_length(ctx, &signature_len) != 0)
                goto finish;
 
        if ((signature = yaca_malloc(signature_len)) == NULL)
@@ -176,7 +176,7 @@ void sign_verify_cmac(void)
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
                goto finish;
 
-       if ((signature_len = yaca_get_sign_length(ctx)) <= 0)
+       if (yaca_get_sign_length(ctx, &signature_len) != 0)
                goto finish;
 
        if ((signature = yaca_malloc(signature_len)) == NULL)
index 650bdcf..df5ba7d 100644 (file)
@@ -120,10 +120,10 @@ API void yaca_ctx_free(yaca_ctx_h ctx)
        }
 }
 
-API int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len)
+API int yaca_get_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
 {
        if (ctx == YACA_CTX_NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       return ctx->get_output_length(ctx, input_len);
+       return ctx->get_output_length(ctx, input_len, output_len);
 }
index b9f4ce4..10d4dbf 100644 (file)
@@ -49,14 +49,15 @@ static struct yaca_digest_ctx_s *get_digest_ctx(const yaca_ctx_h ctx)
        }
 }
 
-static int get_digest_output_length(const yaca_ctx_h ctx, size_t input_len)
+static int get_digest_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
 {
        struct yaca_digest_ctx_s *c = get_digest_ctx(ctx);
 
        if (c == NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       return EVP_MD_CTX_size(c->mdctx);
+       *output_len = EVP_MD_CTX_size(c->mdctx);
+       return 0;
 }
 
 static void destroy_digest_context(yaca_ctx_h ctx)
index ec2176f..758fd2b 100644 (file)
@@ -68,7 +68,7 @@ static void destroy_encrypt_ctx(const yaca_ctx_h ctx)
        nc->cipher_ctx = NULL;
 }
 
-static int get_encrypt_output_length(const yaca_ctx_h ctx, size_t input_len)
+static int get_encrypt_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
 {
        struct yaca_encrypt_ctx_s *nc = get_encrypt_ctx(ctx);
        int block_size;
@@ -77,14 +77,21 @@ static int get_encrypt_output_length(const yaca_ctx_h ctx, size_t input_len)
                return YACA_ERROR_INVALID_ARGUMENT;
 
        block_size = EVP_CIPHER_CTX_block_size(nc->cipher_ctx);
-       if (block_size == 0) {
+       if (block_size <= 0) {
                ERROR_DUMP(YACA_ERROR_INTERNAL);
                return YACA_ERROR_INTERNAL;
        }
 
-       if (input_len > 0)
-               return block_size + input_len - 1;
-       return block_size;
+       if (input_len > 0) {
+               if ((size_t)block_size > SIZE_MAX - input_len + 1)
+                       return YACA_ERROR_TOO_BIG_ARGUMENT;
+
+               *output_len = block_size + input_len - 1;
+       } else {
+               *output_len = block_size;
+       }
+
+       return 0;
 }
 
 static const char *encrypt_algo_to_str(yaca_enc_algo_e algo)
index 5d1f49a..0aa764a 100644 (file)
@@ -46,7 +46,7 @@ struct yaca_ctx_s
        enum yaca_ctx_type_e type;
 
        void (*ctx_destroy)(const yaca_ctx_h ctx);
-       int (*get_output_length)(const yaca_ctx_h ctx, size_t input_len);
+       int (*get_output_length)(const yaca_ctx_h ctx, size_t input_len, size_t *output_len);
        int (*set_param)(yaca_ctx_h ctx, yaca_ex_param_e param,
                         const void *value, size_t value_len);
        int (*get_param)(const yaca_ctx_h ctx, yaca_ex_param_e param,
index a5fb2ae..586aac1 100644 (file)
@@ -68,7 +68,7 @@ static void destroy_seal_ctx(const yaca_ctx_h ctx)
        nc->cipher_ctx = NULL;
 }
 
-static int get_seal_output_length(const yaca_ctx_h ctx, size_t input_len)
+static int get_seal_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
 {
        struct yaca_seal_ctx_s *nc = get_seal_ctx(ctx);
        int block_size;
@@ -83,9 +83,16 @@ static int get_seal_output_length(const yaca_ctx_h ctx, size_t input_len)
                return YACA_ERROR_INTERNAL;
        }
 
-       if (input_len > 0)
-               return block_size + input_len - 1;
-       return block_size;
+       if (input_len > 0) {
+               if ((size_t)block_size > SIZE_MAX - input_len + 1)
+                       return YACA_ERROR_TOO_BIG_ARGUMENT;
+
+               *output_len = block_size + input_len - 1;
+       } else {
+               *output_len = block_size;
+       }
+
+       return 0;
 }
 
 static int seal_init(yaca_ctx_h *ctx,
index b885cc6..e629210 100644 (file)
@@ -56,7 +56,7 @@ static struct yaca_sign_ctx_s *get_sign_ctx(const yaca_ctx_h ctx)
        }
 }
 
-static int get_sign_output_length(const yaca_ctx_h ctx, size_t input_len)
+static int get_sign_output_length(const yaca_ctx_h ctx, size_t input_len, size_t *output_len)
 {
        struct yaca_sign_ctx_s *c = get_sign_ctx(ctx);
 
@@ -75,7 +75,8 @@ static int get_sign_output_length(const yaca_ctx_h ctx, size_t input_len)
                return YACA_ERROR_INVALID_ARGUMENT;
        }
 
-       return len;
+       *output_len = len;
+       return 0;
 }
 
 static void destroy_sign_context(yaca_ctx_h ctx)
index bb8eeec..d82d4f2 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <assert.h>
 #include <limits.h>
+#include <stdint.h>
 
 #include <openssl/crypto.h>
 #include <openssl/rand.h>
@@ -52,11 +53,10 @@ API int yaca_digest_calc(yaca_digest_algo_e algo,
        if (ret < 0)
                goto err;
 
-       ret = yaca_get_digest_length(ctx);
-       if (ret < 0)
+       ret = yaca_get_digest_length(ctx, &ldigest_len);
+       if (ret != 0)
                goto err;
 
-       ldigest_len = ret;
        ldigest = yaca_malloc(ldigest_len);
        if (!ldigest)
                goto err;
@@ -97,24 +97,24 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
            sym_key == YACA_KEY_NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       if (plain_len > INT_MAX) /* TODO: this is because get_output_length returns signed int - perhaps we should change that */
-               return YACA_ERROR_TOO_BIG_ARGUMENT;
-
        ret = yaca_encrypt_init(&ctx, algo, bcm, sym_key, iv);
        if (ret != 0)
                return ret;
 
-       ret = yaca_get_block_length(ctx);
-       if (ret <= 0)
+       ret = yaca_get_block_length(ctx, &lcipher_len);
+       if (ret != 0)
                goto err;
 
-       lcipher_len = ret;
+       ret = yaca_get_output_length(ctx, plain_len, &out_len);
+       if (ret != 0)
+               goto err;
 
-       ret = yaca_get_output_length(ctx, plain_len);
-       if (ret <= 0)
+       if (out_len > SIZE_MAX - lcipher_len) {
+               ret = YACA_ERROR_TOO_BIG_ARGUMENT;
                goto err;
+       }
 
-       lcipher_len += ret;
+       lcipher_len += out_len;
 
        lcipher = yaca_malloc(lcipher_len);
        if (lcipher == NULL)
@@ -174,24 +174,24 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
            sym_key == YACA_KEY_NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
 
-       if (cipher_len > INT_MAX) /* TODO: this is because get_output_length returns signed int - perhaps we should change that */
-               return YACA_ERROR_TOO_BIG_ARGUMENT;
-
        ret = yaca_decrypt_init(&ctx, algo, bcm, sym_key, iv);
        if (ret != 0)
                return ret;
 
-       ret = yaca_get_block_length(ctx);
-       if (ret <= 0)
+       ret = yaca_get_block_length(ctx, &lplain_len);
+       if (ret != 0)
                goto err;
 
-       lplain_len = ret;
+       ret = yaca_get_output_length(ctx, cipher_len, &out_len);
+       if (ret != 0)
+               goto err;
 
-       ret = yaca_get_output_length(ctx, cipher_len);
-       if (ret <= 0)
+       if (out_len > SIZE_MAX - lplain_len) {
+               ret = YACA_ERROR_TOO_BIG_ARGUMENT;
                goto err;
+       }
 
-       lplain_len += ret;
+       lplain_len += out_len;
 
        lplain = yaca_malloc(lplain_len);
        if (!lplain)