Handle unreachable code in different ways 08/231708/9
authorLukasz Pawelczyk <l.pawelczyk@samsung.com>
Thu, 23 Apr 2020 13:24:52 +0000 (15:24 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Fri, 26 Jun 2020 15:36:20 +0000 (17:36 +0200)
The idea is to remove unreachable code where possible. To change it to
asserts where it makes sense. To mark it with explicit assert(false)
where unreachable code can't be removed.

There are cases where unreachable code needs to exist to silence
compiler warnings. Eg. default cases for switch. Mark them with
assert(false) so it's immediately visible that the flow should never
reach them (e.g. because the check has been performed earlier and it's
internal function).

Change-Id: I7d53c9772fe54b5c4dfd0f7205eec633fe4b9c20

src/crypto.c
src/digest.c
src/encrypt.c
src/key.c
src/rsa.c
src/sign.c
src/simple.c

index a22d9c5a8c563fc6b3167ba8b29ec8475cde6718..7123ea84d7610113cae55ceac2db3a27013058dc 100644 (file)
@@ -62,8 +62,7 @@ static int getrandom_wrapper(unsigned char *buf, int num)
        size_t remaining = num;
 
 #ifndef SYS_getrandom
-       if (urandom_fd == -2)
-               return 0;
+       assert(urandom_fd != -2);
 #endif /* SYS_getrandom */
 
        while (remaining > 0) {
index 1d7cbad26994210e6fb68afc84b8bfc09f09be35..9812d6dac9ff4aec3456ac00bdff5120d120005c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
  *
@@ -86,8 +86,10 @@ static int get_digest_output_length(const yaca_context_h ctx,
        assert(output_len != NULL);
 
        struct yaca_digest_context_s *c = get_digest_context(ctx);
+       assert(c != NULL);
+       assert(c->md_ctx != NULL);
 
-       if (c == NULL || input_len != 0)
+       if (input_len != 0)
                return YACA_ERROR_INVALID_PARAMETER;
 
        int md_size = EVP_MD_CTX_size(c->md_ctx);
@@ -102,9 +104,7 @@ static int get_digest_output_length(const yaca_context_h ctx,
 static void destroy_digest_context(yaca_context_h ctx)
 {
        struct yaca_digest_context_s *c = get_digest_context(ctx);
-
-       if (c == NULL)
-               return;
+       assert(c != NULL);
 
        EVP_MD_CTX_destroy(c->md_ctx);
        c->md_ctx = NULL;
index e3b6fe48552c115cf5eaf29f74c8103980506aeb..dfc2fcb32fec6c3372d44154ed020a5e3afbc743 100644 (file)
@@ -260,6 +260,7 @@ static bool is_valid_tag_len(int mode, size_t tag_len)
                }
                return false;
        default:
+               assert(false);
                return false;
        }
 }
@@ -280,9 +281,7 @@ static struct yaca_encrypt_context_s *get_encrypt_context(const yaca_context_h c
 static void destroy_encrypt_context(const yaca_context_h ctx)
 {
        struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
-
-       if (c == NULL)
-               return;
+       assert(c != NULL);
 
        if (c->backup_ctx != NULL) {
                yaca_key_destroy(c->backup_ctx->iv);
@@ -299,11 +298,9 @@ static int get_encrypt_output_length(const yaca_context_h ctx, size_t input_len,
 {
        assert(output_len != NULL);
 
-       struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
        int block_size;
-
-       if (c == NULL)
-               return YACA_ERROR_INVALID_PARAMETER;
+       struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
+       assert(c != NULL);
        assert(c->cipher_ctx != NULL);
 
        block_size = EVP_CIPHER_CTX_block_size(c->cipher_ctx);
@@ -320,8 +317,7 @@ static int get_encrypt_output_length(const yaca_context_h ctx, size_t input_len,
        } else {
                *output_len = block_size;
        }
-       if (*output_len == 0)
-               return YACA_ERROR_INTERNAL;
+       assert(*output_len != 0);
 
        return YACA_ERROR_NONE;
 }
@@ -331,8 +327,7 @@ static int get_wrap_output_length(const yaca_context_h ctx, size_t input_len, si
        assert(output_len != NULL);
 
        struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
-       if (c == NULL)
-               return YACA_ERROR_INVALID_PARAMETER;
+       assert(c != NULL);
        assert(c->cipher_ctx != NULL);
 
        bool encryption = is_encryption_op(c->op_type);
@@ -509,13 +504,11 @@ static int encrypt_ctx_setup(struct yaca_encrypt_context_s *c,
        assert(key != YACA_KEY_NULL);
 
        const EVP_CIPHER *cipher = EVP_CIPHER_CTX_cipher(c->cipher_ctx);
-
        if (cipher == NULL)
                return YACA_ERROR_INTERNAL;
 
        lkey = key_get_simple(key);
-       if (lkey == NULL)
-               return YACA_ERROR_INVALID_PARAMETER;
+       assert(lkey != NULL);
 
        liv = key_get_simple(iv);
 
@@ -687,13 +680,14 @@ static int set_encrypt_property(yaca_context_h ctx,
                                                                const void *value,
                                                                size_t value_len)
 {
-       struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
        int len;
        int ret = YACA_ERROR_NONE;
+       struct yaca_encrypt_context_s *c = get_encrypt_context(ctx);
+       assert(c != NULL);
+       assert(c->cipher_ctx != NULL);
 
-       if (c == NULL || value == NULL || value_len == 0)
+       if (value == NULL || value_len == 0)
                return YACA_ERROR_INVALID_PARAMETER;
-       assert(c->cipher_ctx != NULL);
 
        int mode = EVP_CIPHER_CTX_mode(c->cipher_ctx);
        int nid = EVP_CIPHER_CTX_nid(c->cipher_ctx);
@@ -963,8 +957,7 @@ int encrypt_initialize(yaca_context_h *ctx,
                return YACA_ERROR_INVALID_PARAMETER;
 
        lsym_key = key_get_simple(sym_key);
-       if (lsym_key == NULL)
-               return YACA_ERROR_INVALID_PARAMETER;
+       assert(lsym_key != NULL);
 
        if (lsym_key->key.type != YACA_KEY_TYPE_DES &&
                lsym_key->key.type != YACA_KEY_TYPE_SYMMETRIC)
index af701e499b9eedff1778ea2a90c0047c6e3c34f1..15bc937812605ebdc24e2e315583f38ac01e61c0 100644 (file)
--- a/src/key.c
+++ b/src/key.c
@@ -429,13 +429,10 @@ static int import_evp(yaca_key_h *key,
 
        /* Neither PEM nor DER will ever be shorter then 4 bytes (12 seems
         * to be minimum for DER, much more for PEM). This is just to make
-        * sure we have at least 4 bytes for strncmp() below.
-        */
-       if (data_len < 4)
-               return YACA_ERROR_INVALID_PARAMETER;
-
-       /* This is because of BIO_new_mem_buf() having its length param typed int */
-       if (data_len > INT_MAX)
+        * sure we have at least 4 bytes for strncmp() below. INT_MAX is
+        * because of BIO_new_mem_buf() having its length param typed
+        * int */
+       if (data_len < 4 || data_len > INT_MAX)
                return YACA_ERROR_INVALID_PARAMETER;
 
        src = BIO_new_mem_buf(data, data_len);
@@ -750,7 +747,8 @@ static int export_evp_default_bio(struct yaca_key_evp_s *evp_key,
                        break;
 
                default:
-                       return YACA_ERROR_INVALID_PARAMETER;
+                       assert(false);
+                       return YACA_ERROR_INTERNAL;
                }
 
                break;
@@ -790,7 +788,8 @@ static int export_evp_default_bio(struct yaca_key_evp_s *evp_key,
                }
 
                default:
-                       return YACA_ERROR_INVALID_PARAMETER;
+                       assert(false);
+                       return YACA_ERROR_INTERNAL;
                }
 
                break;
@@ -1083,7 +1082,6 @@ static int generate_evp_pkey_params(int evp_id, size_t key_bit_len, EVP_PKEY **p
 
                break;
        default:
-               /* We shouldn't be here */
                assert(false);
                return YACA_ERROR_INTERNAL;
        }
@@ -1225,7 +1223,7 @@ static int generate_evp(yaca_key_type_e out_type, size_t key_bit_len,
        assert(key_bit_len > 0 || params != NULL);
 
        int ret;
-       int evp_id;
+       int evp_id = -1;
        EVP_PKEY *pkey_out = NULL;
        EVP_PKEY *pkey_params = NULL;
 
@@ -1234,11 +1232,8 @@ static int generate_evp(yaca_key_type_e out_type, size_t key_bit_len,
                yaca_key_type_e params_type = params->key.type;
 
                ret = convert_params_to_priv(params_type, &key_type);
-               if (ret != YACA_ERROR_NONE)
-                       return ret;
-
-               if (out_type != key_type)
-                       return YACA_ERROR_INVALID_PARAMETER;
+               assert(ret == YACA_ERROR_NONE);
+               assert(out_type == key_type);
 
                pkey_params = params->evp;
        }
@@ -1249,8 +1244,7 @@ static int generate_evp(yaca_key_type_e out_type, size_t key_bit_len,
        case YACA_KEY_TYPE_EC_PARAMS:
                assert(params == NULL);
                ret = convert_params_to_evp_id(out_type, &evp_id);
-               if (ret != YACA_ERROR_NONE)
-                       return ret;
+               assert(ret == YACA_ERROR_NONE);
 
                ret = generate_evp_pkey_params(evp_id, key_bit_len, &pkey_out);
                break;
@@ -1259,13 +1253,13 @@ static int generate_evp(yaca_key_type_e out_type, size_t key_bit_len,
        case YACA_KEY_TYPE_DH_PRIV:
        case YACA_KEY_TYPE_EC_PRIV:
                ret = convert_priv_to_evp_id(out_type, &evp_id);
-               if (ret != YACA_ERROR_NONE)
-                       return ret;
+               assert(ret == YACA_ERROR_NONE);
 
                ret = generate_evp_pkey_key(evp_id, key_bit_len, pkey_params, &pkey_out);
                break;
        default:
-               return YACA_ERROR_INVALID_PARAMETER;
+               assert(false);
+               return YACA_ERROR_INTERNAL;
        }
        if (ret != YACA_ERROR_NONE)
                return ret;
@@ -1449,7 +1443,6 @@ API int yaca_key_get_bit_length(const yaca_key_h key, size_t *key_bit_len)
                        return convert_nid_to_ec(nid, key_bit_len);
                }
                default:
-                       /* We shouldn't be here */
                        assert(false);
                        return YACA_ERROR_INTERNAL;
                }
@@ -1575,6 +1568,7 @@ API int yaca_key_generate(yaca_key_type_e key_type,
                *key = (yaca_key_h)nk_evp;
        } else {
                assert(false);
+               return YACA_ERROR_INTERNAL;
        }
 
        return YACA_ERROR_NONE;
@@ -1591,9 +1585,7 @@ API int yaca_key_generate_from_parameters(const yaca_key_h params, yaca_key_h *p
        if (evp_params == NULL || prv_key == NULL)
                return YACA_ERROR_INVALID_PARAMETER;
 
-       ret = yaca_key_get_type(params, &params_type);
-       if (ret != YACA_ERROR_NONE)
-               return ret;
+       params_type = evp_params->key.type;
 
        ret = convert_params_to_priv(params_type, &key_type);
        if (ret != YACA_ERROR_NONE)
@@ -1624,9 +1616,7 @@ API int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key)
        if (evp_key == NULL || pub_key == NULL)
                return YACA_ERROR_INVALID_PARAMETER;
 
-       ret = yaca_key_get_type(prv_key, &prv_type);
-       if (ret != YACA_ERROR_NONE)
-               return ret;
+       prv_type = evp_key->key.type;
 
        ret = convert_priv_to_pub(prv_type, &pub_type);
        if (ret != YACA_ERROR_NONE)
@@ -1688,9 +1678,7 @@ API int yaca_key_extract_parameters(const yaca_key_h key, yaca_key_h *params)
        if (evp_key == NULL || params == NULL)
                return YACA_ERROR_INVALID_PARAMETER;
 
-       ret = yaca_key_get_type(key, &key_type);
-       if (ret != YACA_ERROR_NONE)
-               return ret;
+       key_type = evp_key->key.type;
 
        ret = convert_priv_to_params(key_type, &params_type);
        if (ret != YACA_ERROR_NONE)
index 7fbb6e40c42c1c13a99a4cea8da8d7148d3083ff..21077b91ed4132b545d63bdd8e61eb582fa0a4a7 100644 (file)
--- a/src/rsa.c
+++ b/src/rsa.c
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
  *
@@ -50,6 +50,7 @@ int rsa_padding2openssl(yaca_padding_e padding)
        case YACA_PADDING_PKCS1_SSLV23:
                return RSA_SSLV23_PADDING;
        default:
+               assert(false);
                return -1;
        }
 }
@@ -75,7 +76,6 @@ static int encrypt_decrypt(yaca_padding_e padding,
                return YACA_ERROR_INVALID_PARAMETER;
 
        lpadding = rsa_padding2openssl(padding);
-       assert(lpadding != -1);
 
        lasym_key = key_get_evp(key);
        assert(lasym_key != NULL);
index 03f1cb07c7241b8265b009e26a88082b7060e87c..66fd5b2c700cdd70fad345bf106f72a91f9e83a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
  *
@@ -84,14 +84,14 @@ static int get_sign_output_length(const yaca_context_h ctx,
 {
        assert(output_len != NULL);
 
-       struct yaca_sign_context_s *c = get_sign_context(ctx);
        EVP_PKEY_CTX *pctx;
+       struct yaca_sign_context_s *c = get_sign_context(ctx);
+       assert(c != NULL);
+       assert(c->md_ctx != NULL);
 
-       if (c == NULL || input_len != 0)
+       if (input_len != 0)
                return YACA_ERROR_INVALID_PARAMETER;
 
-       assert(c->md_ctx != NULL);
-
        pctx = EVP_MD_CTX_pkey_ctx(c->md_ctx);
        if (pctx == NULL)
                return YACA_ERROR_INTERNAL;
@@ -115,9 +115,7 @@ static int get_sign_output_length(const yaca_context_h ctx,
 static void destroy_sign_context(yaca_context_h ctx)
 {
        struct yaca_sign_context_s *c = get_sign_context(ctx);
-
-       if (c == NULL)
-               return;
+       assert(c != NULL);
 
        EVP_MD_CTX_destroy(c->md_ctx);
        c->md_ctx = NULL;
@@ -129,17 +127,17 @@ int set_sign_property(yaca_context_h ctx,
                                          size_t value_len)
 {
        int ret;
-       struct yaca_sign_context_s *c = get_sign_context(ctx);
        yaca_padding_e padding;
        int pad;
        EVP_PKEY *pkey;
        EVP_PKEY_CTX *pctx;
+       struct yaca_sign_context_s *c = get_sign_context(ctx);
+       assert(c != NULL);
+       assert(c->md_ctx != NULL);
 
-       if (c == NULL || value == NULL || c->state == CTX_FINALIZED)
+       if (value == NULL || c->state == CTX_FINALIZED)
                return YACA_ERROR_INVALID_PARAMETER;
 
-       assert(c->md_ctx != NULL);
-
        pctx = EVP_MD_CTX_pkey_ctx(c->md_ctx);
        if (pctx == NULL)
                return YACA_ERROR_INTERNAL;
@@ -160,7 +158,6 @@ int set_sign_property(yaca_context_h ctx,
        }
 
        pad = rsa_padding2openssl(padding);
-       assert(pad != -1);
 
        pkey = EVP_PKEY_CTX_get0_pkey(pctx);
        if (pkey == NULL) {
index e4c7a38c982264cdbb910c69091f124523190238..e1ec24b3136ce63bac1289a2ace80e1bccf96f59 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Krzysztof Jackiewicz <k.jackiewicz@samsung.com>
  *
@@ -147,12 +147,6 @@ API int yaca_simple_encrypt(yaca_encrypt_algorithm_e algo,
        written += out_len;
        assert(written <= lciphertext_len);
 
-       if (((bcm == YACA_BCM_CBC || bcm == YACA_BCM_ECB) && written == 0) ||
-               (bcm != YACA_BCM_CBC && bcm != YACA_BCM_ECB && plaintext_len == 0 && written > 0)) {
-               ret = YACA_ERROR_INTERNAL;
-               goto exit;
-       }
-
        if (written > 0) {
                ret = yaca_realloc(written, (void**)&lciphertext);
                if (ret != YACA_ERROR_NONE)
@@ -239,11 +233,6 @@ API int yaca_simple_decrypt(yaca_encrypt_algorithm_e algo,
        written += out_len;
        assert(written <= lplaintext_len);
 
-       if (bcm != YACA_BCM_CBC && bcm != YACA_BCM_ECB && ciphertext_len == 0 && written > 0) {
-               ret = YACA_ERROR_INTERNAL;
-               goto exit;
-       }
-
        if (written > 0) {
                ret = yaca_realloc(written, (void**)&lplaintext);
                if (ret != YACA_ERROR_NONE)