Error handling refactoring. 53/72553/8
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 1 Jun 2016 10:00:24 +0000 (12:00 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Mon, 6 Jun 2016 10:34:47 +0000 (12:34 +0200)
Multiple goto labels replaced with single 'free' label.

Change-Id: I4936f2ef178c9b6fbf58a38beda7d21a700232a9

13 files changed:
examples/digest.c
examples/encrypt.c
examples/encrypt_aes_gcm_ccm.c
examples/key_exchange.c
examples/key_import_export.c
examples/seal.c
examples/sign.c
src/digest.c
src/encrypt.c
src/key.c
src/seal.c
src/sign.c
src/simple.c

index e4699e9..8a77fe4 100644 (file)
@@ -58,24 +58,24 @@ void digest_advanced(void)
 
        ret = yaca_digest_update(ctx, lorem1024, 1024);
        if (ret != YACA_ERROR_NONE)
-               goto exit_ctx;
+               goto exit;
 
        size_t digest_len;
        ret = yaca_get_digest_length(ctx, &digest_len);
        if (ret != YACA_ERROR_NONE)
-               goto exit_ctx;
+               goto exit;
 
        {
                char digest[digest_len];
 
                ret = yaca_digest_final(ctx, digest, &digest_len);
                if (ret != YACA_ERROR_NONE)
-                       goto exit_ctx;
+                       goto exit;
 
                dump_hex(digest, digest_len, "Message digest: ");
        }
 
-exit_ctx:
+exit:
        yaca_ctx_free(ctx);
 }
 
index 7c18aa9..b56f364 100644 (file)
@@ -70,7 +70,6 @@ void encrypt_simple(const yaca_enc_algo_e algo,
        printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
 
 exit:
-
        yaca_free(enc);
        yaca_free(dec);
        yaca_key_free(iv);
@@ -104,34 +103,34 @@ void encrypt_advanced(const yaca_enc_algo_e algo,
                return;
 
        if (yaca_get_iv_bits(algo, bcm, key_bits, &iv_bits) != YACA_ERROR_NONE)
-               goto ex_key;
+               goto exit;
 
        if (iv_bits > 0 && yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
-               goto ex_key;
+               goto exit;
 
        /* Encryption */
        {
                if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
-                       goto ex_iv;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto ex_ctx;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto ex_ctx;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
                if ((enc = yaca_malloc(enc_size)) == NULL)
-                       goto ex_ctx;
+                       goto exit;
 
                out_size = enc_size;
                if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                rem = enc_size - out_size;
                if (yaca_encrypt_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                enc_size = rem + out_size;
 
@@ -144,41 +143,37 @@ void encrypt_advanced(const yaca_enc_algo_e algo,
        /* Decryption */
        {
                if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
                if ((dec = yaca_malloc(dec_size)) == NULL)
-                       goto ex_of;
+                       goto exit;
 
                out_size = dec_size;
                if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
-                       goto ex_in;
+                       goto exit;
 
                rem = dec_size - out_size;
                if (yaca_decrypt_final(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
-                       goto ex_in;
+                       goto exit;
 
                dec_size = rem + out_size;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
        }
 
-ex_in:
+exit:
        yaca_free(dec);
-ex_of:
        yaca_free(enc);
-ex_ctx:
        yaca_ctx_free(ctx);
-ex_iv:
        yaca_key_free(iv);
-ex_key:
        yaca_key_free(key);
 }
 
index 523153a..08f8e92 100644 (file)
@@ -68,54 +68,54 @@ void encrypt_decrypt_aes_gcm(void)
 
        /* IV generation */
        if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        if ((aad = yaca_zalloc(aad_size)) == NULL)
-               goto clean;
+               goto exit;
 
        if (yaca_rand_bytes(aad, aad_size) != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        if ((tag = yaca_zalloc(tag_size)) == NULL)
-               goto clean;
+               goto exit;
 
        /* Encryption */
        {
                if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Provide any AAD data */
                if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
                if ((enc = yaca_malloc(enc_size)) == NULL)
-                       goto clean;
+                       goto exit;
 
                out_size = enc_size;
                if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                rem = enc_size - out_size;
                if (yaca_encrypt_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                enc_size = rem + out_size;
 
                /* Set the tag length and get the tag after final encryption */
                if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_TAG_LEN,
                                       (void*)&tag_size, sizeof(tag_size)) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_ctx_get_param(ctx, YACA_PARAM_GCM_TAG, (void**)tag, &tag_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
 
@@ -126,42 +126,42 @@ void encrypt_decrypt_aes_gcm(void)
        /* Decryption */
        {
                if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Provide any AAD data */
                if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
                if ((dec = yaca_malloc(dec_size)) == NULL)
-                       goto clean;
+                       goto exit;
 
                out_size = dec_size;
                if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                rem = dec_size - out_size;
 
                /* Set expected tag value before final decryption */
                if (yaca_ctx_set_param(ctx, YACA_PARAM_GCM_TAG, tag, tag_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_decrypt_final(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                dec_size = rem + out_size;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
        }
 
-clean:
+exit:
        yaca_free(enc);
        yaca_free(dec);
        yaca_free(tag);
@@ -208,58 +208,58 @@ void encrypt_decrypt_aes_ccm(void)
 
        /* IV generation */
        if (yaca_key_gen(YACA_KEY_TYPE_IV, iv_bits, &iv) != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        if ((aad = yaca_zalloc(aad_size)) == NULL)
-               goto clean;
+               goto exit;
 
        if (yaca_rand_bytes(aad, aad_size) != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        if ((tag = yaca_zalloc(tag_size)) == NULL)
-               goto clean;
+               goto exit;
 
        /* Encryption */
        {
                if (yaca_encrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Set tag length (optionally) */
                if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_TAG_LEN,
                                       (void*)&tag_size, sizeof(tag_size)) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* The total plain text length must be passed (only needed if AAD is passed) */
                if (yaca_encrypt_update(ctx, NULL, LOREM4096_SIZE , NULL, &len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
                if ((enc = yaca_malloc(enc_size)) == NULL)
-                       goto clean;
+                       goto exit;
 
                out_size = enc_size;
                if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                rem = enc_size - out_size;
                if (yaca_encrypt_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                enc_size = rem + out_size;
 
                /* Get the tag after final encryption */
                if (yaca_ctx_get_param(ctx, YACA_PARAM_CCM_TAG, (void**)tag, &tag_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_size);
 
@@ -270,42 +270,42 @@ void encrypt_decrypt_aes_ccm(void)
        /* Decryption */
        {
                if (yaca_decrypt_init(&ctx, algo, bcm, key, iv) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Set expected tag value */
                if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_TAG, tag, tag_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* The total encrypted text length must be passed (only needed if AAD is passed) */
                if (yaca_decrypt_update(ctx, NULL, enc_size , NULL, &len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_ctx_set_param(ctx, YACA_PARAM_CCM_AAD, aad, aad_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
                if ((dec = yaca_malloc(dec_size)) == NULL)
-                       goto clean;
+                       goto exit;
 
                out_size = dec_size;
                /* The tag verify is performed when you call the final yaca_decrypt_update(),
                 * there is no call to yaca_decrypt_final() */
                if (yaca_decrypt_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
-                       goto clean;
+                       goto exit;
 
                dec_size = out_size;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_size, dec);
        }
 
-clean:
+exit:
        yaca_free(enc);
        yaca_free(dec);
        yaca_free(tag);
index 6a51e34..3bc8e32 100644 (file)
@@ -48,16 +48,16 @@ void key_exchange_dh(void)
        // generate  private, public key
        ret = yaca_key_gen(YACA_KEY_TYPE_DH_PRIV, YACA_KEY_2048BIT, &private_key);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        ret = yaca_key_extract_public(private_key, &public_key);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        // get peer public key from file
        // add helper to read key from file to buffer?
        fp = fopen("key.pub", "r");
-       if (!fp) goto clean;
+       if (!fp) goto exit;
 
        fseek(fp, 0L, SEEK_END);
        size = ftell(fp);
@@ -66,23 +66,23 @@ void key_exchange_dh(void)
        /* allocate memory for entire content */
        buffer = yaca_malloc(size+1);
        if (buffer == NULL)
-               goto clean;
+               goto exit;
 
        /* copy the file into the buffer */
        if (1 != fread(buffer, size, 1, fp))
-               goto clean;
+               goto exit;
 
        ret = yaca_key_import(YACA_KEY_TYPE_DH_PUB, NULL,
                              buffer, size, &peer_key);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        // derive secret
        ret = yaca_key_derive_dh(private_key, peer_key, &secret);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
-clean:
+exit:
        yaca_key_free(private_key);
        yaca_key_free(public_key);
        yaca_key_free(peer_key);
@@ -111,16 +111,16 @@ void key_exchange_ecdh(void)
        // generate  private, public key
        ret = yaca_key_gen(YACA_KEY_TYPE_EC_PRIV, YACA_KEY_CURVE_P256, &private_key);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        ret = yaca_key_extract_public(private_key, &public_key);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        // get peer public key from file
        fp = fopen("key.pub", "r");
        if (fp == NULL)
-               goto clean;
+               goto exit;
 
        fseek(fp, 0L, SEEK_END);
        size = ftell(fp);
@@ -129,22 +129,22 @@ void key_exchange_ecdh(void)
        /* allocate memory for entire content */
        buffer = yaca_malloc(size+1);
        if (buffer == NULL)
-               goto clean;
+               goto exit;
 
        /* copy the file into the buffer */
        if (1 != fread(buffer, size, 1, fp))
-               goto clean;
+               goto exit;
 
        ret = yaca_key_import(YACA_KEY_TYPE_EC_PUB, NULL, buffer, size, &peer_key);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
        // derive secret
        ret = yaca_key_derive_dh(private_key, peer_key, &secret);
        if (ret != YACA_ERROR_NONE)
-               goto clean;
+               goto exit;
 
-clean:
+exit:
        yaca_key_free(private_key);
        yaca_key_free(public_key);
        yaca_key_free(peer_key);
index f6f77fd..077657c 100644 (file)
@@ -50,7 +50,7 @@ int key_import_export_sym(yaca_key_h sym)
                return ret;
        ret = yaca_key_import(YACA_KEY_TYPE_SYMMETRIC, NULL, b64, b64_len, &b64_imported);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\n\t***** BASE64 exported key: *****\n%.*s\n", (int)b64_len, b64);
        yaca_free(b64);
@@ -58,7 +58,7 @@ int key_import_export_sym(yaca_key_h sym)
 
        ret = yaca_key_export(b64_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_BASE64, NULL, &b64, &b64_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\t***** BASE64 imported key: *****\n%.*s\n", (int)b64_len, b64);
 
@@ -67,10 +67,10 @@ int key_import_export_sym(yaca_key_h sym)
 
        ret = yaca_key_export(sym, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_RAW, NULL, &raw, &raw_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
        ret = yaca_key_import(YACA_KEY_TYPE_SYMMETRIC, NULL, raw, raw_len, &raw_imported);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        dump_hex(raw, raw_len, "\n\t***** RAW exported key: *****");
        yaca_free(raw);
@@ -78,13 +78,11 @@ int key_import_export_sym(yaca_key_h sym)
 
        ret = yaca_key_export(raw_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_RAW, NULL, &raw, &raw_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        dump_hex(raw, raw_len, "\t***** RAW imported key: *****");
 
-       ret = YACA_ERROR_NONE;
-
-free:
+exit:
        yaca_key_free(raw_imported);
        yaca_key_free(b64_imported);
        yaca_free(raw);
@@ -122,7 +120,7 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
                return ret;
        ret = yaca_key_import(priv_type, NULL, pem_prv, pem_prv_len, &pem_prv_imported);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\n\t***** %s PEM exported private key: *****\n%.*s", algo, (int)pem_prv_len, pem_prv);
        yaca_free(pem_prv);
@@ -130,7 +128,7 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(pem_prv_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_prv, &pem_prv_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\t***** %s PEM imported private key: *****\n%.*s", algo, (int)pem_prv_len, pem_prv);
 
@@ -139,10 +137,10 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(priv, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_prv, &der_prv_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
        ret = yaca_key_import(priv_type, NULL, der_prv, der_prv_len, &der_prv_imported);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        dump_hex(der_prv, der_prv_len, "\n\t***** %s DER exported private key: *****", algo);
        yaca_free(der_prv);
@@ -150,7 +148,7 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(der_prv_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_prv, &der_prv_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        dump_hex(der_prv, der_prv_len, "\t***** %s DER imported private key: *****", algo);
 
@@ -159,10 +157,10 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(pub, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_pub, &pem_pub_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
        ret = yaca_key_import(pub_type, NULL, pem_pub, pem_pub_len, &pem_pub_imported);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\n\t***** %s PEM exported public key: *****\n%.*s", algo, (int)pem_pub_len, pem_pub);
        yaca_free(pem_pub);
@@ -170,7 +168,7 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(pem_pub_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pem_pub, &pem_pub_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\t***** %s PEM imported public key: *****\n%.*s", algo, (int)pem_pub_len, pem_pub);
 
@@ -179,10 +177,10 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(pub, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_pub, &der_pub_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
        ret = yaca_key_import(pub_type, NULL, der_pub, der_pub_len, &der_pub_imported);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        dump_hex(der_pub, der_pub_len, "\n\t***** %s DER exported public key: *****", algo);
        yaca_free(der_pub);
@@ -190,13 +188,11 @@ int key_import_export_asym(yaca_key_h priv, yaca_key_h pub,
 
        ret = yaca_key_export(der_pub_imported, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_DER, NULL, &der_pub, &der_pub_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        dump_hex(der_pub, der_pub_len, "\t***** %s DER imported public key: *****", algo);
 
-       ret = YACA_ERROR_NONE;
-
-free:
+exit:
        yaca_key_free(der_pub_imported);
        yaca_key_free(pem_pub_imported);
        yaca_key_free(der_prv_imported);
@@ -226,22 +222,21 @@ int key_import_x509(void)
 
        ret = yaca_key_import(YACA_KEY_TYPE_RSA_PUB, NULL, pub, pub_len, &rsa_pub_from_cert);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        yaca_free(pub);
        pub = NULL;
 
        ret = yaca_key_export(rsa_pub_from_cert, YACA_KEY_FORMAT_DEFAULT, YACA_KEY_FILE_FORMAT_PEM, NULL, &pub, &pub_len);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\n\t***** RSA X509 imported public key: *****\n%.*s", (int)pub_len, pub);
 
-       ret = YACA_ERROR_NONE;
-
-free:
+exit:
        yaca_key_free(rsa_pub_from_cert);
        yaca_free(pub);
+
        return ret;
 }
 
@@ -266,19 +261,19 @@ int main()
 
        ret = yaca_key_gen(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_1024BIT, &rsa_priv);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        ret = yaca_key_extract_public(rsa_priv, &rsa_pub);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        ret = yaca_key_gen(YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_1024BIT, &dsa_priv);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        ret = yaca_key_extract_public(dsa_priv, &dsa_pub);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        printf("\t***************************************\n");
        printf("\t************** SYMMETRIC **************\n");
@@ -316,13 +311,13 @@ int main()
        else
                printf("\n\t*********** X509 - failure ************\n\n");
 
-free:
+exit:
        yaca_key_free(dsa_pub);
        yaca_key_free(dsa_priv);
        yaca_key_free(rsa_pub);
        yaca_key_free(rsa_priv);
        yaca_key_free(sym);
-exit:
+
        yaca_exit();
 
        return ret;
index be640ff..23d8400 100644 (file)
@@ -60,32 +60,32 @@ void encrypt_seal(void)
                return;
 
        if (yaca_key_extract_public(key_priv, &key_pub) != YACA_ERROR_NONE)
-               goto ex_prvk;
+               goto exit;
 
        /* Encrypt a.k.a. seal */
        {
                if (yaca_seal_init(&ctx, key_pub, algo, bcm, key_bits, &aes_key, &iv) != YACA_ERROR_NONE)
-                       goto ex_pubk;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto ex_ak;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto ex_ak;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                enc_size = output_len + block_len;
                if ((enc = yaca_malloc(enc_size)) == NULL)
-                       goto ex_ak;
+                       goto exit;
 
                /* Seal and finalize */
                out_size = enc_size;
                if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &out_size) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                rem = enc_size - out_size;
                if (yaca_seal_final(ctx, enc + out_size, &rem) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                enc_size = rem + out_size;
 
@@ -98,44 +98,40 @@ void encrypt_seal(void)
        /* Decrypt a.k.a. open */
        {
                if (yaca_open_init(&ctx, key_priv, algo, bcm, key_bits, aes_key, iv) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                if (yaca_get_block_length(ctx, &block_len) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                if (yaca_get_output_length(ctx, LOREM4096_SIZE, &output_len) != YACA_ERROR_NONE)
-                       goto ex_of;
+                       goto exit;
 
                /* Calculate max output: size of update + final chunks */
                dec_size = output_len + block_len;
                if ((dec = yaca_malloc(dec_size)) == NULL)
-                       goto ex_of;
+                       goto exit;
 
                /* Open and finalize */
                out_size = dec_size;
                if (yaca_open_update(ctx, enc, enc_size, dec, &out_size) != YACA_ERROR_NONE)
-                       goto ex_in;
+                       goto exit;
 
                rem = dec_size - out_size;
                if (yaca_open_final(ctx, dec + out_size, &rem) != YACA_ERROR_NONE)
-                       goto ex_in;
+                       goto exit;
 
                dec_size = rem + out_size;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n", dec_size, dec);
        }
 
-ex_in:
+exit:
        yaca_free(dec);
-ex_of:
        yaca_free(enc);
-ex_ak:
        yaca_ctx_free(ctx);
        yaca_key_free(aes_key);
        yaca_key_free(iv);
-ex_pubk:
        yaca_key_free(key_pub);
-ex_prvk:
        yaca_key_free(key_priv);
 }
 
index f789f24..fe85a02 100644 (file)
@@ -47,7 +47,7 @@ void simple_sign_verify_asym(yaca_key_type_e type, const char *algo)
                return;
 
        if (yaca_key_extract_public(prv, &pub) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        // SIGN
        if (yaca_sign(YACA_DIGEST_SHA512,
@@ -56,7 +56,7 @@ void simple_sign_verify_asym(yaca_key_type_e type, const char *algo)
                      LOREM4096_SIZE,
                      &signature,
                      &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        dump_hex(signature, signature_len, "[Simple API] %s Signature of lorem4096:", algo);
 
@@ -71,7 +71,7 @@ void simple_sign_verify_asym(yaca_key_type_e type, const char *algo)
        else
                printf("[Simple API] %s verification successful\n", algo);
 
-finish:
+exit:
        yaca_free(signature);
        yaca_key_free(prv);
        yaca_key_free(pub);
@@ -96,7 +96,7 @@ void simple_sign_verify_hmac(void)
                      LOREM4096_SIZE,
                      &signature1,
                      &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        dump_hex(signature1, signature_len, "[Simple API] HMAC Signature of lorem4096:");
 
@@ -107,14 +107,14 @@ void simple_sign_verify_hmac(void)
                      LOREM4096_SIZE,
                      &signature2,
                      &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
                printf("[Simple API] HMAC verification failed\n");
        else
                printf("[Simple API] HMAC verification successful\n");
 
-finish:
+exit:
        yaca_free(signature1);
        yaca_free(signature2);
        yaca_key_free(key);
@@ -139,7 +139,7 @@ void simple_sign_verify_cmac(void)
                      LOREM4096_SIZE,
                      &signature1,
                      &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        dump_hex(signature1, signature_len, "[Simple API] CMAC Signature of lorem4096:");
 
@@ -151,14 +151,14 @@ void simple_sign_verify_cmac(void)
                      LOREM4096_SIZE,
                      &signature2,
                      &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
                printf("[Simple API] CMAC verification failed\n");
        else
                printf("[Simple API] CMAC verification successful\n");
 
-finish:
+exit:
        yaca_free(signature1);
        yaca_free(signature2);
        yaca_key_free(key);
@@ -180,26 +180,26 @@ void sign_verify_asym(yaca_key_type_e type, const char *algo)
                return;
 
        if (yaca_key_extract_public(prv, &pub) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        // SIGN
        if (yaca_sign_init(&ctx, YACA_DIGEST_SHA512, prv) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if ((signature = yaca_malloc(signature_len)) == NULL)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_final(ctx, signature, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        dump_hex(signature, signature_len, "[Advanced API] %s Signature of lorem4096:", algo);
 
@@ -209,20 +209,20 @@ void sign_verify_asym(yaca_key_type_e type, const char *algo)
 
        // VERIFY
        if (yaca_verify_init(&ctx, YACA_DIGEST_SHA512, pub) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_ctx_set_param(ctx, YACA_PARAM_PADDING, (char*)(&padding), sizeof(padding)) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_verify_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_verify_final(ctx, signature, signature_len) != YACA_ERROR_NONE)
                printf("[Advanced API] %s verification failed\n", algo);
        else
                printf("[Advanced API] %s verification successful\n", algo);
 
-finish:
+exit:
        yaca_free(signature);
        yaca_key_free(prv);
        yaca_key_free(pub);
@@ -244,19 +244,19 @@ void sign_verify_hmac(void)
 
        // SIGN
        if (yaca_sign_hmac_init(&ctx, YACA_DIGEST_SHA512, key) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if ((signature1 = yaca_malloc(signature_len)) == NULL)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_final(ctx, signature1, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        dump_hex(signature1, signature_len, "[Advanced API] HMAC Signature of lorem4096:");
 
@@ -266,26 +266,26 @@ void sign_verify_hmac(void)
 
        // VERIFY
        if (yaca_sign_hmac_init(&ctx, YACA_DIGEST_SHA512, key) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if ((signature2 = yaca_malloc(signature_len)) == NULL)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_final(ctx, signature2, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
                printf("[Advanced API] HMAC verification failed\n");
        else
                printf("[Advanced API] HMAC verification successful\n");
 
-finish:
+exit:
        yaca_free(signature1);
        yaca_free(signature2);
        yaca_key_free(key);
@@ -307,19 +307,19 @@ void sign_verify_cmac(void)
 
        // SIGN
        if (yaca_sign_cmac_init(&ctx, YACA_ENC_AES, key) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
-               goto finish;
+               goto exit;
 
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if ((signature1 = yaca_malloc(signature_len)) == NULL)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_final(ctx, signature1, &signature_len))
-               goto finish;
+               goto exit;
 
        dump_hex(signature1, signature_len, "[Advanced API] CMAC Signature of lorem4096:");
 
@@ -329,26 +329,26 @@ void sign_verify_cmac(void)
 
        // VERIFY
        if (yaca_sign_cmac_init(&ctx, YACA_ENC_AES, key) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_update(ctx, lorem4096, LOREM4096_SIZE))
-               goto finish;
+               goto exit;
 
        if (yaca_get_sign_length(ctx, &signature_len) != YACA_ERROR_NONE)
-               goto finish;
+               goto exit;
 
        if ((signature2 = yaca_malloc(signature_len)) == NULL)
-               goto finish;
+               goto exit;
 
        if (yaca_sign_final(ctx, signature2, &signature_len))
-               goto finish;
+               goto exit;
 
        if (yaca_memcmp(signature1, signature2, signature_len) != YACA_ERROR_NONE)
                printf("[Advanced API] CMAC verification failed\n");
        else
                printf("[Advanced API] CMAC verification successful\n");
 
-finish:
+exit:
        yaca_free(signature1);
        yaca_free(signature2);
        yaca_key_free(key);
index 47d8f57..7a19e4a 100644 (file)
@@ -132,30 +132,29 @@ API int yaca_digest_init(yaca_ctx_h *ctx, yaca_digest_algo_e algo)
 
        ret = digest_get_algorithm(algo, &md);
        if (ret != YACA_ERROR_NONE)
-               goto free;
+               goto exit;
 
        nc->mdctx = EVP_MD_CTX_create();
        if (nc->mdctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free;
+               goto exit;
        }
 
        ret = EVP_DigestInit(nc->mdctx, md);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto ctx;
+               goto exit;
        }
 
        *ctx = (yaca_ctx_h)nc;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
 
-       return YACA_ERROR_NONE;
+exit:
+       yaca_ctx_free((yaca_ctx_h)nc);
 
-ctx:
-       EVP_MD_CTX_destroy(nc->mdctx);
-free:
-       yaca_free(nc);
        return ret;
 }
 
index 27eeb57..d56aad1 100644 (file)
@@ -341,42 +341,42 @@ static int encrypt_init(yaca_ctx_h *ctx,
 
        ret = yaca_key_get_bits(sym_key, &key_bits);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        ret = encrypt_get_algorithm(algo, bcm, key_bits, &cipher);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        ret = EVP_CIPHER_iv_length(cipher);
        if (ret < 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_free;
+               goto exit;
        }
 
        iv_bits = ret * 8;
        if (iv_bits == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_free;
+               goto exit;
        }
 
        if (iv_bits != 0) { /* cipher requires iv*/
                liv = key_get_simple(iv);
                if (liv == NULL) { /* iv was not provided */
                        ret = YACA_ERROR_INVALID_ARGUMENT;
-                       goto err_free;
+                       goto exit;
                }
                ret = yaca_key_get_bits(iv, &iv_bits_check);
                if (ret != YACA_ERROR_NONE) {
                        ret = YACA_ERROR_INVALID_ARGUMENT;
-                       goto err_free;
+                       goto exit;
                }
                /* IV length doesn't match cipher (GCM & CCM supports variable IV length) */
                if (iv_bits != iv_bits_check &&
                    bcm != YACA_BCM_GCM &&
                    bcm != YACA_BCM_CCM) {
                        ret = YACA_ERROR_INVALID_ARGUMENT;
-                       goto err_free;
+                       goto exit;
                }
                iv_data = (unsigned char*)liv->d;
        }
@@ -385,7 +385,7 @@ static int encrypt_init(yaca_ctx_h *ctx,
        if (nc->cipher_ctx == NULL) {
                ret =  YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_free;
+               goto exit;
        }
 
        switch (op_type) {
@@ -397,13 +397,13 @@ static int encrypt_init(yaca_ctx_h *ctx,
                break;
        default:
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_ctx;
+               goto exit;
        }
 
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_ctx;
+               goto exit;
        }
 
        /* Handling of algorithms with variable key length */
@@ -411,7 +411,7 @@ static int encrypt_init(yaca_ctx_h *ctx,
        if (ret != 1) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
                ERROR_DUMP(ret);
-               goto err_ctx;
+               goto exit;
        }
 
        /* Handling of algorithms with variable IV length */
@@ -427,7 +427,7 @@ static int encrypt_init(yaca_ctx_h *ctx,
                if (ret != 1) {
                        ret = YACA_ERROR_INVALID_ARGUMENT;
                        ERROR_DUMP(ret);
-                       goto err_ctx;
+                       goto exit;
                }
        }
 
@@ -444,22 +444,22 @@ static int encrypt_init(yaca_ctx_h *ctx,
                break;
        default:
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_ctx;
+               goto exit;
        }
 
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_ctx;
+               goto exit;
        }
 
        *ctx = (yaca_ctx_h)nc;
-       return YACA_ERROR_NONE;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
+
+exit:
+       yaca_ctx_free((yaca_ctx_h)nc);
 
-err_ctx:
-       EVP_CIPHER_CTX_free(nc->cipher_ctx);
-err_free:
-       yaca_free(nc);
        return ret;
 }
 
index f9ed709..7a6c043 100644 (file)
--- a/src/key.c
+++ b/src/key.c
@@ -110,7 +110,7 @@ int base64_decode(const char *data, size_t data_len, BIO **output)
        if (src == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        BIO_push(b64, src);
@@ -119,7 +119,7 @@ int base64_decode(const char *data, size_t data_len, BIO **output)
        if (dst == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
@@ -130,7 +130,7 @@ int base64_decode(const char *data, size_t data_len, BIO **output)
                if (ret < 0) {
                        ret = YACA_ERROR_INTERNAL;
                        ERROR_DUMP(ret);
-                       goto free_bio;
+                       goto exit;
                }
 
                if (ret == YACA_ERROR_NONE)
@@ -139,7 +139,7 @@ int base64_decode(const char *data, size_t data_len, BIO **output)
                if (BIO_write(dst, tmpbuf, ret) != ret) {
                        ret = YACA_ERROR_INTERNAL;
                        ERROR_DUMP(ret);
-                       goto free_bio;
+                       goto exit;
                }
        }
 
@@ -150,18 +150,18 @@ int base64_decode(const char *data, size_t data_len, BIO **output)
        if (out_len < 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
        if ((size_t)out_len != b64_len) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto free_bio;
+               goto exit;
        }
 
        *output = dst;
        dst = NULL;
        ret = YACA_ERROR_NONE;
 
-free_bio:
+exit:
        BIO_free_all(b64);
        BIO_free_all(dst);
 
@@ -205,7 +205,7 @@ int import_simple(yaca_key_h *key,
        /* key_bits has to fit in size_t */
        if (key_data_len > SIZE_MAX / 8) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto out;
+               goto exit;
        }
 
        /* DES key length verification */
@@ -215,14 +215,14 @@ int import_simple(yaca_key_h *key,
                    key_bits != YACA_KEY_UNSAFE_128BIT &&
                    key_bits != YACA_KEY_192BIT) {
                        ret = YACA_ERROR_INVALID_ARGUMENT;
-                       goto out;
+                       goto exit;
                }
        }
 
        nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_data_len);
        if (nk == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
-               goto out;
+               goto exit;
        }
 
        memcpy(nk->d, key_data, key_data_len);
@@ -230,10 +230,12 @@ int import_simple(yaca_key_h *key,
        nk->key.type = key_type;
 
        *key = (yaca_key_h)nk;
+       nk = NULL;
        ret = YACA_ERROR_NONE;
 
-out:
+exit:
        BIO_free_all(decoded);
+
        return ret;
 }
 
@@ -371,18 +373,18 @@ int import_evp(yaca_key_h *key,
 
        default:
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto free;
+               goto exit;
        }
 
        if (type != key_type) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto free;
+               goto exit;
        }
 
        nk = yaca_zalloc(sizeof(struct yaca_key_evp_s));
        if (nk == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
-               goto free;
+               goto exit;
        }
 
        nk->evp = pkey;
@@ -392,8 +394,9 @@ int import_evp(yaca_key_h *key,
        pkey = NULL;
        ret = YACA_ERROR_NONE;
 
-free:
+exit:
        EVP_PKEY_free(pkey);
+
        return ret;
 }
 
@@ -445,7 +448,7 @@ int export_simple_base64(struct yaca_key_simple_s *simple_key,
        if (mem == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        BIO_push(b64, mem);
@@ -455,35 +458,35 @@ int export_simple_base64(struct yaca_key_simple_s *simple_key,
        if (ret <= 0 || (unsigned)ret != key_len) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        ret = BIO_flush(b64);
        if (ret <= 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        bio_data_len = BIO_get_mem_data(mem, &bio_data);
        if (bio_data_len <= 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        *data = yaca_malloc(bio_data_len);
        if (*data == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        memcpy(*data, bio_data, bio_data_len);
        *data_len = bio_data_len;
        ret = YACA_ERROR_NONE;
 
-free_bio:
+exit:
        BIO_free_all(b64);
 
        return ret;
@@ -678,35 +681,36 @@ int export_evp(struct yaca_key_evp_s *evp_key,
        }
 
        if (ret != YACA_ERROR_NONE)
-               goto free_bio;
+               goto exit;
 
        ret = BIO_flush(mem);
        if (ret <= 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        bio_data_len = BIO_get_mem_data(mem, &bio_data);
        if (bio_data_len <= 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        *data = yaca_malloc(bio_data_len);
        if (*data == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        memcpy(*data, bio_data, bio_data_len);
        *data_len = bio_data_len;
        ret = YACA_ERROR_NONE;
 
-free_bio:
+exit:
        BIO_free_all(mem);
+
        return ret;
 }
 
@@ -752,28 +756,37 @@ int gen_simple_des(struct yaca_key_simple_s **out, size_t key_bits)
        DES_cblock *des_key = (DES_cblock*)nk->d;
        if (key_byte_len >= 8) {
                ret = DES_random_key(des_key);
-               if (ret != 1)
-                       goto free_nk;
+               if (ret != 1) {
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       goto exit;
+               }
        }
        if (key_byte_len >= 16) {
                ret = DES_random_key(des_key + 1);
-               if (ret != 1)
-                       goto free_nk;
+               if (ret != 1) {
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       goto exit;
+               }
        }
        if (key_byte_len >= 24) {
                ret = DES_random_key(des_key + 2);
-               if (ret != 1)
-                       goto free_nk;
+               if (ret != 1) {
+                       ret = YACA_ERROR_INTERNAL;
+                       ERROR_DUMP(ret);
+                       goto exit;
+               }
        }
 
        nk->bits = key_bits;
        *out = nk;
-       return YACA_ERROR_NONE;
+       nk = NULL;
+       ret = YACA_ERROR_NONE;
 
-free_nk:
+exit:
        yaca_free(nk);
-       ret = YACA_ERROR_INTERNAL;
-       ERROR_DUMP(ret);
+
        return ret;
 }
 
@@ -797,38 +810,38 @@ int gen_evp_rsa(struct yaca_key_evp_s **out, size_t key_bits)
        if (ctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_nk;
+               goto exit;
        }
 
        ret = EVP_PKEY_keygen_init(ctx);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bits);
        if (ret != 1) {
                ret = ERROR_HANDLE();
-               goto free_ctx;
+               goto exit;
        }
 
        ret = EVP_PKEY_keygen(ctx, &pkey);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        nk->evp = pkey;
-
+       pkey = NULL;
        *out = nk;
        nk = NULL;
+
        ret = YACA_ERROR_NONE;
 
-free_ctx:
+exit:
        EVP_PKEY_CTX_free(ctx);
-free_nk:
        yaca_free(nk);
 
        return ret;
@@ -847,8 +860,8 @@ int gen_evp_dsa(struct yaca_key_evp_s **out, size_t key_bits)
 
        int ret;
        struct yaca_key_evp_s *nk;
-       EVP_PKEY_CTX *pctx;
-       EVP_PKEY_CTX *kctx;
+       EVP_PKEY_CTX *pctx = NULL;
+       EVP_PKEY_CTX *kctx = NULL;
        EVP_PKEY *pkey = NULL;
        EVP_PKEY *params = NULL;
 
@@ -860,63 +873,61 @@ int gen_evp_dsa(struct yaca_key_evp_s **out, size_t key_bits)
        if (pctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_nk;
+               goto exit;
        }
 
        ret = EVP_PKEY_paramgen_init(pctx);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pctx;
+               goto exit;
        }
 
        ret = EVP_PKEY_CTX_set_dsa_paramgen_bits(pctx, key_bits);
        if (ret != 1) {
                ret = ERROR_HANDLE();
-               goto free_pctx;
+               goto exit;
        }
 
        ret = EVP_PKEY_paramgen(pctx, &params);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pctx;
+               goto exit;
        }
 
        kctx = EVP_PKEY_CTX_new(params, NULL);
        if (kctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_params;
+               goto exit;
        }
 
        ret = EVP_PKEY_keygen_init(kctx);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_kctx;
+               goto exit;
        }
 
        ret = EVP_PKEY_keygen(kctx, &pkey);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_kctx;
+               goto exit;
        }
 
        nk->evp = pkey;
-
+       pkey = NULL;
        *out = nk;
        nk = NULL;
+
        ret = YACA_ERROR_NONE;
 
-free_kctx:
+exit:
        EVP_PKEY_CTX_free(kctx);
-free_params:
        EVP_PKEY_free(params);
-free_pctx:
        EVP_PKEY_CTX_free(pctx);
-free_nk:
        yaca_free(nk);
 
        return ret;
@@ -1135,8 +1146,8 @@ API int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key)
        int ret;
        struct yaca_key_evp_s *evp_key = key_get_evp(prv_key);
        struct yaca_key_evp_s *nk;
-       BIO *mem;
-       EVP_PKEY *pkey;
+       BIO *mem = NULL;
+       EVP_PKEY *pkey = NULL;
 
        if (prv_key == YACA_KEY_NULL || evp_key == NULL || pub_key == NULL)
                return YACA_ERROR_INVALID_ARGUMENT;
@@ -1149,51 +1160,50 @@ API int yaca_key_extract_public(const yaca_key_h prv_key, yaca_key_h *pub_key)
        if (mem == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_nk;
+               goto exit;
        }
 
        ret = i2d_PUBKEY_bio(mem, evp_key->evp);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        pkey = d2i_PUBKEY_bio(mem, NULL);
        if (pkey == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_bio;
+               goto exit;
        }
 
        BIO_free(mem);
        mem = NULL;
 
-       nk->evp = pkey;
-       *pub_key = (yaca_key_h)nk;
-
        switch (prv_key->type) {
        case YACA_KEY_TYPE_RSA_PRIV:
-               (*pub_key)->type = YACA_KEY_TYPE_RSA_PUB;
+               nk->key.type = YACA_KEY_TYPE_RSA_PUB;
                break;
        case YACA_KEY_TYPE_DSA_PRIV:
-               (*pub_key)->type = YACA_KEY_TYPE_DSA_PUB;
+               nk->key.type = YACA_KEY_TYPE_DSA_PUB;
                break;
 //     case YACA_KEY_TYPE_EC_PRIV:
-//             (*pub_key)->type = YACA_KEY_TYPE_EC_PUB;
+//             nk->key.type = YACA_KEY_TYPE_EC_PUB;
 //             break;
        default:
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto free_pkey;
+               goto exit;
        }
 
-       return YACA_ERROR_NONE;
+       nk->evp = pkey;
+       pkey = NULL;
+       *pub_key = (yaca_key_h)nk;
+       nk = NULL;
+       ret = YACA_ERROR_NONE;
 
-free_pkey:
+exit:
        EVP_PKEY_free(pkey);
-free_bio:
        BIO_free(mem);
-free_nk:
        yaca_free(nk);
 
        return ret;
@@ -1250,12 +1260,14 @@ API int yaca_key_derive_pbkdf2(const char *password,
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err;
+               goto exit;
        }
 
        *key = (yaca_key_h)nk;
-       return YACA_ERROR_NONE;
-err:
+       nk = NULL;
+       ret = YACA_ERROR_NONE;
+exit:
        yaca_free(nk);
+
        return ret;
 }
index cdd3cbf..c07fe3f 100644 (file)
@@ -105,8 +105,8 @@ static int seal_init(yaca_ctx_h *ctx,
                      yaca_key_h *iv)
 {
        struct yaca_key_evp_s *lpub;
-       struct yaca_key_simple_s *lkey;
-       struct yaca_key_simple_s *liv;
+       struct yaca_key_simple_s *lkey = NULL;
+       struct yaca_key_simple_s *liv = NULL;
        struct yaca_seal_ctx_s *nc;
        const EVP_CIPHER *cipher;
        int pub_key_length;
@@ -134,39 +134,39 @@ static int seal_init(yaca_ctx_h *ctx,
        if (nc->cipher_ctx == NULL) {
                ret =  YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_free;
+               goto exit;
        }
 
        ret = EVP_PKEY_size(lpub->evp);
        if (ret <= 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_ctx;
+               goto exit;
        }
 
        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;
-               goto err_ctx;
+               goto exit;
        }
 
        ret = encrypt_get_algorithm(algo, bcm, sym_key_bits, &cipher);
        if (ret != YACA_ERROR_NONE)
-               goto err_key;
+               goto exit;
 
        ret = EVP_CIPHER_iv_length(cipher);
        if (ret < 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_key;
+               goto exit;
        }
 
        iv_length = ret;
        liv = yaca_zalloc(sizeof(struct yaca_key_simple_s) + iv_length);
        if (liv == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
-               goto err_key;
+               goto exit;
        }
 
        unsigned char *key_data = (unsigned char*)lkey->d;
@@ -183,29 +183,28 @@ static int seal_init(yaca_ctx_h *ctx,
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_iv;
+               goto exit;
        }
 
        lkey->bits = key_data_length * 8;
        lkey->key.type = YACA_KEY_TYPE_SYMMETRIC;
        *sym_key = (yaca_key_h)lkey;
+       lkey = NULL;
 
        liv->bits = iv_length * 8;
        liv->key.type = YACA_KEY_TYPE_IV;
        *iv = (yaca_key_h)liv;
+       liv = NULL;
 
        *ctx = (yaca_ctx_h)nc;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
 
-       return YACA_ERROR_NONE;
-
-err_iv:
+exit:
        yaca_free(liv);
-err_key:
        yaca_free(lkey);
-err_ctx:
-       EVP_CIPHER_CTX_free(nc->cipher_ctx);
-err_free:
-       yaca_free(nc);
+       yaca_ctx_free((yaca_ctx_h)nc);
+
        return ret;
 }
 
@@ -249,44 +248,44 @@ static int open_init(yaca_ctx_h *ctx,
 
        ret = encrypt_get_algorithm(algo, bcm, sym_key_bits, &cipher);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        ret = EVP_CIPHER_iv_length(cipher);
        if (ret < 0) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_free;
+               goto exit;
        }
 
        iv_bits = ret * 8;
        if (iv_bits == 0 && iv != NULL) { /* 0 -> cipher doesn't use iv, but it was provided */
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_free;
+               goto exit;
        }
 
        liv = key_get_simple(iv);
        /* cipher requires iv, but none was provided, or provided wrong iv */
        if (iv_bits != 0 && (liv == NULL || liv->key.type != YACA_KEY_TYPE_IV)) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_free;
+               goto exit;
        }
 
        // TODO: handling of algorithms with variable IV length
        ret = yaca_key_get_bits(iv, &iv_bits_check);
        if (ret != YACA_ERROR_NONE) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_free;
+               goto exit;
        }
        if (iv_bits != iv_bits_check) { /* IV length doesn't match cipher */
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err_free;
+               goto exit;
        }
 
        nc->cipher_ctx = EVP_CIPHER_CTX_new();
        if (nc->cipher_ctx == NULL) {
                ret =  YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_free;
+               goto exit;
        }
 
        ret = EVP_OpenInit(nc->cipher_ctx, cipher,
@@ -297,16 +296,16 @@ static int open_init(yaca_ctx_h *ctx,
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto err_ctx;
+               goto exit;
        }
 
        *ctx = (yaca_ctx_h)nc;
-       return YACA_ERROR_NONE;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
+
+exit:
+       yaca_ctx_free((yaca_ctx_h)nc);
 
-err_ctx:
-       EVP_CIPHER_CTX_free(nc->cipher_ctx);
-err_free:
-       yaca_free(nc);
        return ret;
 }
 
index e7dedc4..54ad806 100644 (file)
@@ -268,27 +268,27 @@ API int yaca_sign_init(yaca_ctx_h *ctx,
 
        ret = digest_get_algorithm(algo, &md);
        if (ret != YACA_ERROR_NONE)
-               goto free_ctx;
+               goto exit;
 
        nc->mdctx = EVP_MD_CTX_create();
        if (nc->mdctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        ret = EVP_DigestSignInit(nc->mdctx, NULL, md, NULL, evp_key->evp);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        *ctx = (yaca_ctx_h)nc;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
 
-       return YACA_ERROR_NONE;
-
-free_ctx:
+exit:
        yaca_ctx_free((yaca_ctx_h)nc);
 
        return ret;
@@ -324,33 +324,35 @@ API int yaca_sign_hmac_init(yaca_ctx_h *ctx,
        if (pkey == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        ret = digest_get_algorithm(algo, &md);
        if (ret != YACA_ERROR_NONE)
-               goto free_pkey;
+               goto exit;
 
        nc->mdctx = EVP_MD_CTX_create();
        if (nc->mdctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pkey;
+               goto exit;
        }
 
        ret = EVP_DigestSignInit(nc->mdctx, NULL, md, NULL, pkey);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pkey;
+               goto exit;
        }
 
+       pkey = NULL;
+
        *ctx = (yaca_ctx_h)nc;
-       return YACA_ERROR_NONE;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
 
-free_pkey:
+exit:
        EVP_PKEY_free(pkey);
-free_ctx:
        yaca_ctx_free((yaca_ctx_h)nc);
 
        return ret;
@@ -382,60 +384,60 @@ API int yaca_sign_cmac_init(yaca_ctx_h *ctx,
 
        ret = encrypt_get_algorithm(algo, YACA_BCM_CBC, simple_key->bits, &cipher);
        if (ret != YACA_ERROR_NONE)
-               goto free_ctx;
+               goto exit;
 
        // create and initialize low level CMAC context
        cmac_ctx = CMAC_CTX_new();
        if (cmac_ctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
-       if (CMAC_Init(cmac_ctx, simple_key->d, simple_key->bits/8, cipher, NULL) != 1) {
+       if (CMAC_Init(cmac_ctx, simple_key->d, simple_key->bits / 8, cipher, NULL) != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               // TODO refactor error handling: use single cleanup label
-               goto free_cmac_ctx;
+               goto exit;
        }
 
        // create key and assign CMAC context to it
        pkey = EVP_PKEY_new();
-       if (!pkey) {
+       if (pkey == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_cmac_ctx;
+               goto exit;
        }
 
        if (EVP_PKEY_assign(pkey, EVP_PKEY_CMAC, cmac_ctx) != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pkey;
+               goto exit;
        }
-       // TODO refactor error handling: set cmac_ctx to NULL
+
+       cmac_ctx = NULL;
 
        nc->mdctx = EVP_MD_CTX_create();
        if (nc->mdctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pkey;
+               goto exit;
        }
 
        if (EVP_DigestSignInit(nc->mdctx, NULL, NULL, NULL, pkey) != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_pkey;
+               goto exit;
        }
-       // TODO refactor error handling: set mdctx to NULL, set pkey to NULL
+
+       pkey = NULL;
 
        *ctx = (yaca_ctx_h)nc;
-       return YACA_ERROR_NONE;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
 
-free_pkey:
+exit:
        EVP_PKEY_free(pkey);
-free_cmac_ctx:
        CMAC_CTX_free(cmac_ctx);
-free_ctx:
        yaca_ctx_free((yaca_ctx_h)nc);
 
        return ret;
@@ -518,27 +520,27 @@ API int yaca_verify_init(yaca_ctx_h *ctx,
 
        ret = digest_get_algorithm(algo, &md);
        if (ret != YACA_ERROR_NONE)
-               goto free_ctx;
+               goto exit;
 
        nc->mdctx = EVP_MD_CTX_create();
        if (nc->mdctx == NULL) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        ret = EVP_DigestVerifyInit(nc->mdctx, NULL, md, NULL, evp_key->evp);
        if (ret != 1) {
                ret = YACA_ERROR_INTERNAL;
                ERROR_DUMP(ret);
-               goto free_ctx;
+               goto exit;
        }
 
        *ctx = (yaca_ctx_h)nc;
+       nc = NULL;
+       ret = YACA_ERROR_NONE;
 
-       return YACA_ERROR_NONE;
-
-free_ctx:
+exit:
        yaca_ctx_free((yaca_ctx_h)nc);
 
        return ret;
index 6779b0d..f759af3 100644 (file)
@@ -41,7 +41,7 @@ API int yaca_digest_calc(yaca_digest_algo_e algo,
 {
        yaca_ctx_h ctx;
        int ret;
-       char *ldigest;
+       char *ldigest = NULL;
        size_t ldigest_len;
 
        if (data == NULL || data_len == 0 || digest == NULL || digest_len == NULL)
@@ -53,30 +53,28 @@ API int yaca_digest_calc(yaca_digest_algo_e algo,
 
        ret = yaca_digest_update(ctx, data, data_len);
        if (ret != YACA_ERROR_NONE)
-               goto err;
+               goto exit;
 
        ret = yaca_get_digest_length(ctx, &ldigest_len);
        if (ret != YACA_ERROR_NONE)
-               goto err;
+               goto exit;
 
        ldigest = yaca_malloc(ldigest_len);
-       if (!ldigest)
-               goto err;
+       if (ldigest == NULL)
+               goto exit;
 
        ret = yaca_digest_final(ctx, ldigest, &ldigest_len);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
-
-       yaca_ctx_free(ctx);
+               goto exit;
 
        *digest_len = ldigest_len;
        *digest = ldigest;
-       return YACA_ERROR_NONE;
+       ldigest = NULL;
 
-err_free:
+exit:
        yaca_free(ldigest);
-err:
        yaca_ctx_free(ctx);
+
        return ret;
 }
 
@@ -91,8 +89,8 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
 {
        yaca_ctx_h ctx;
        int ret;
-       char *lcipher;
-       char *rcipher;
+       char *lcipher = NULL;
+       char *rcipher = NULL;
        size_t out_len, lcipher_len, written;
 
        if (plain == NULL || plain_len == 0 || cipher == NULL || cipher_len == NULL ||
@@ -105,27 +103,27 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
 
        ret = yaca_get_block_length(ctx, &lcipher_len);
        if (ret != YACA_ERROR_NONE)
-               goto err;
+               goto exit;
 
        ret = yaca_get_output_length(ctx, plain_len, &out_len);
        if (ret != YACA_ERROR_NONE)
-               goto err;
+               goto exit;
 
        if (out_len > SIZE_MAX - lcipher_len) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err;
+               goto exit;
        }
 
        lcipher_len += out_len;
 
        lcipher = yaca_malloc(lcipher_len);
        if (lcipher == NULL)
-               goto err;
+               goto exit;
 
        out_len = lcipher_len;
        ret = yaca_encrypt_update(ctx, plain, plain_len, lcipher, &out_len);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        assert(out_len <= lcipher_len);
 
@@ -133,7 +131,7 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
        out_len = lcipher_len - written;
        ret = yaca_encrypt_final(ctx, lcipher + written, &out_len);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        written += out_len;
        assert(written <= lcipher_len);
@@ -141,19 +139,18 @@ API int yaca_encrypt(yaca_enc_algo_e algo,
        rcipher = yaca_realloc(lcipher, written);
        if (rcipher == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
-               goto err_free;
+               goto exit;
        }
 
-       yaca_ctx_free(ctx);
-
        *cipher = rcipher;
        *cipher_len = written;
-       return YACA_ERROR_NONE;
+       lcipher = NULL;
+       ret = YACA_ERROR_NONE;
 
-err_free:
+exit:
        yaca_free(lcipher);
-err:
        yaca_ctx_free(ctx);
+
        return ret;
 }
 
@@ -168,8 +165,8 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
 {
        yaca_ctx_h ctx;
        int ret;
-       char *lplain;
-       char *rplain;
+       char *lplain = NULL;
+       char *rplain = NULL;
        size_t out_len, lplain_len, written;
 
        if (cipher == NULL || cipher_len == 0 || plain == NULL || plain_len == NULL ||
@@ -182,27 +179,27 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
 
        ret = yaca_get_block_length(ctx, &lplain_len);
        if (ret != YACA_ERROR_NONE)
-               goto err;
+               goto exit;
 
        ret = yaca_get_output_length(ctx, cipher_len, &out_len);
        if (ret != YACA_ERROR_NONE)
-               goto err;
+               goto exit;
 
        if (out_len > SIZE_MAX - lplain_len) {
                ret = YACA_ERROR_INVALID_ARGUMENT;
-               goto err;
+               goto exit;
        }
 
        lplain_len += out_len;
 
        lplain = yaca_malloc(lplain_len);
-       if (!lplain)
-               goto err;
+       if (lplain == NULL)
+               goto exit;
 
        out_len = lplain_len;
        ret = yaca_decrypt_update(ctx, cipher, cipher_len, lplain, &out_len);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        assert(out_len <= lplain_len);
 
@@ -210,7 +207,7 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
        out_len = lplain_len - written;
        ret = yaca_decrypt_final(ctx, lplain + written, &out_len);
        if (ret != YACA_ERROR_NONE)
-               goto err_free;
+               goto exit;
 
        written += out_len;
        assert(written <= lplain_len);
@@ -218,19 +215,18 @@ API int yaca_decrypt(yaca_enc_algo_e algo,
        rplain = yaca_realloc(lplain, written);
        if (rplain == NULL) {
                ret = YACA_ERROR_OUT_OF_MEMORY;
-               goto err_free;
+               goto exit;
        }
 
-       yaca_ctx_free(ctx);
-
        *plain = rplain;
        *plain_len = written;
-       return YACA_ERROR_NONE;
+       lplain = NULL;
+       ret = YACA_ERROR_NONE;
 
-err_free:
+exit:
        yaca_free(lplain);
-err:
        yaca_ctx_free(ctx);
+
        return ret;
 }
 
@@ -300,11 +296,11 @@ API int yaca_verify(yaca_digest_algo_e algo,
 
        ret = yaca_verify_update(ctx, data, data_len);
        if (ret != YACA_ERROR_NONE)
-               goto free_ctx;
+               goto exit;
 
        ret = yaca_verify_final(ctx, signature, signature_len);
 
-free_ctx:
+exit:
        yaca_ctx_free(ctx);
 
        return ret;