Fix and simplify output parameter usage. 57/81657/1
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 27 Jul 2016 12:13:33 +0000 (14:13 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 27 Jul 2016 12:27:30 +0000 (14:27 +0200)
Change-Id: I04d97b11fafe428f6c48c394539cb6e10f45f725

examples/encrypt.c
examples/encrypt_aes_gcm_ccm.c
examples/seal.c
src/simple.c

index 31c8806b61be4fe34b0a17c30898ec7601ff60b2..3f6274cd6862011c070bcc6d57e1c0a0d7586a50 100644 (file)
@@ -94,7 +94,6 @@ void encrypt_advanced(const yaca_encrypt_algorithm_e algo,
        size_t block_len;
        size_t output_len;
        size_t written_len;
-       size_t rem;
 
        printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
 
@@ -126,15 +125,15 @@ void encrypt_advanced(const yaca_encrypt_algorithm_e algo,
                if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = enc_len;
                if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = enc_len - written_len;
-               if (yaca_encrypt_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
+               enc_len = written_len;
+
+               if (yaca_encrypt_finalize(ctx, enc + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               enc_len = rem + written_len;
+               enc_len += written_len;
 
                dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
 
@@ -160,15 +159,15 @@ void encrypt_advanced(const yaca_encrypt_algorithm_e algo,
                if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = dec_len;
                if (yaca_decrypt_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = dec_len - written_len;
-               if (yaca_decrypt_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
+               dec_len = written_len;
+
+               if (yaca_decrypt_finalize(ctx, dec + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               dec_len = rem + written_len;
+               dec_len += written_len;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
        }
index c990d79bdb36699747eb4c4282468c457f9a1ed7..719ff7781b82d1aa9191c5f556b715290856f3e1 100644 (file)
@@ -57,7 +57,6 @@ void encrypt_decrypt_aes_gcm(void)
        size_t block_len;
        size_t output_len;
        size_t written_len;
-       size_t rem;
 
        printf("AES GCM 256bit key encryption/decryption\n");
        printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
@@ -101,15 +100,15 @@ void encrypt_decrypt_aes_gcm(void)
                if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = enc_len;
                if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = enc_len - written_len;
-               if (yaca_encrypt_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
+               enc_len = written_len;
+
+               if (yaca_encrypt_finalize(ctx, enc + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               enc_len = rem + written_len;
+               enc_len += written_len;
 
                /* Set the tag length and get the tag after final encryption */
                if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG_LEN,
@@ -147,20 +146,19 @@ void encrypt_decrypt_aes_gcm(void)
                if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = dec_len;
                if (yaca_decrypt_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = dec_len - written_len;
+               dec_len = written_len;
 
                /* Set expected tag value before final decryption */
                if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, tag, tag_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               if (yaca_decrypt_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
+               if (yaca_decrypt_finalize(ctx, dec + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               dec_len = rem + written_len;
+               dec_len += written_len;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
        }
@@ -200,7 +198,6 @@ void encrypt_decrypt_aes_ccm(void)
        size_t block_len;
        size_t output_len;
        size_t written_len;
-       size_t rem;
        size_t len;
 
        printf("AES CCM 256bit key encryption/decryption\n");
@@ -253,15 +250,15 @@ void encrypt_decrypt_aes_ccm(void)
                if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = enc_len;
                if (yaca_encrypt_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = enc_len - written_len;
-               if (yaca_encrypt_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
+               enc_len = written_len;
+
+               if (yaca_encrypt_finalize(ctx, enc + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               enc_len = rem + written_len;
+               enc_len += written_len;
 
                /* Get the tag after final encryption */
                if (yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)tag, &tag_len) != YACA_ERROR_NONE)
@@ -302,7 +299,6 @@ void encrypt_decrypt_aes_ccm(void)
                if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = dec_len;
                /* The tag verify is performed when you call the final yaca_decrypt_update(),
                 * there is no call to yaca_decrypt_finalize() */
                if (yaca_decrypt_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
index 64b70f780a9eddc0793b77f6d0360c1b53b9b6e9..ff1a9e82b12e6af6c37fd23f29d4aae28f338b0e 100644 (file)
@@ -50,7 +50,6 @@ void encrypt_seal(const yaca_encrypt_algorithm_e algo,
        size_t block_len;
        size_t output_len;
        size_t written_len;
-       size_t rem;
 
        printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
 
@@ -80,15 +79,15 @@ void encrypt_seal(const yaca_encrypt_algorithm_e algo,
                        goto exit;
 
                /* Seal and finalize */
-               written_len = enc_len;
                if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = enc_len - written_len;
-               if (yaca_seal_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
+               enc_len = written_len;
+
+               if (yaca_seal_finalize(ctx, enc + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               enc_len = rem + written_len;
+               enc_len += written_len;
 
                dump_hex(enc, 16, "Encrypted data (16 of %zu bytes): ", enc_len);
 
@@ -115,15 +114,15 @@ void encrypt_seal(const yaca_encrypt_algorithm_e algo,
                        goto exit;
 
                /* Open and finalize */
-               written_len = dec_len;
                if (yaca_open_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = dec_len - written_len;
-               if (yaca_open_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
+               dec_len = written_len;
+
+               if (yaca_open_finalize(ctx, dec + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               dec_len = rem + written_len;
+               dec_len += written_len;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
        }
@@ -163,7 +162,6 @@ void encrypt_seal_aes_gcm(void)
        size_t block_len;
        size_t output_len;
        size_t written_len;
-       size_t rem;
 
        printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
 
@@ -205,15 +203,15 @@ void encrypt_seal_aes_gcm(void)
                if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = enc_len;
                if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = enc_len - written_len;
-               if (yaca_seal_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
+               enc_len = written_len;
+
+               if (yaca_seal_finalize(ctx, enc + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               enc_len = rem + written_len;
+               enc_len += written_len;
 
                /* Set the tag length and get the tag after final encryption */
                if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG_LEN,
@@ -251,20 +249,19 @@ void encrypt_seal_aes_gcm(void)
                if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = dec_len;
                if (yaca_open_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = dec_len - written_len;
-
                /* Set expected tag value before final decryption */
                if (yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, tag, tag_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               if (yaca_open_finalize(ctx, dec + written_len, &rem) != YACA_ERROR_NONE)
+               dec_len = written_len;
+
+               if (yaca_open_finalize(ctx, dec + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               dec_len = rem + written_len;
+               dec_len += written_len;
 
                printf("Decrypted data (16 of %zu bytes): %.16s\n\n", dec_len, dec);
        }
@@ -306,7 +303,6 @@ void encrypt_seal_aes_ccm(void)
        size_t block_len;
        size_t output_len;
        size_t written_len;
-       size_t rem;
        size_t len;
 
        printf("Plain data (16 of %zu bytes): %.16s\n", LOREM4096_SIZE, lorem4096);
@@ -358,15 +354,15 @@ void encrypt_seal_aes_ccm(void)
                if (yaca_malloc(enc_len, (void**)&enc) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = enc_len;
                if (yaca_seal_update(ctx, lorem4096, LOREM4096_SIZE, enc, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               rem = enc_len - written_len;
-               if (yaca_seal_finalize(ctx, enc + written_len, &rem) != YACA_ERROR_NONE)
+               enc_len = written_len;
+
+               if (yaca_seal_finalize(ctx, enc + written_len, &written_len) != YACA_ERROR_NONE)
                        goto exit;
 
-               enc_len = rem + written_len;
+               enc_len += written_len;
 
                /* Get the tag after final encryption */
                if (yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)tag, &tag_len) != YACA_ERROR_NONE)
@@ -408,7 +404,6 @@ void encrypt_seal_aes_ccm(void)
                if (yaca_malloc(dec_len, (void**)&dec) != YACA_ERROR_NONE)
                        goto exit;
 
-               written_len = dec_len;
                /* The tag verify is performed when you call the final yaca_open_update(),
                 * there is no call to yaca_open_finalize() */
                if (yaca_open_update(ctx, enc, enc_len, dec, &written_len) != YACA_ERROR_NONE)
index 101822dfd403adb2f543bc7845b6f7af97a78c29..4301248e116de71db7ec1f115ca054dfb04c7ea9 100644 (file)
@@ -120,22 +120,19 @@ API int yaca_simple_encrypt(yaca_encrypt_algorithm_e algo,
        }
 
        lciphertext_len += out_len;
-
        assert(lciphertext_len > 0);
 
        ret = yaca_malloc(lciphertext_len, (void**)&lciphertext);
        if (ret != YACA_ERROR_NONE)
                goto exit;
 
-       out_len = lciphertext_len;
        ret = yaca_encrypt_update(ctx, plaintext, plaintext_len, lciphertext, &out_len);
        if (ret != YACA_ERROR_NONE)
                goto exit;
 
        assert(out_len <= lciphertext_len);
-
        written = out_len;
-       out_len = lciphertext_len - written;
+
        ret = yaca_encrypt_finalize(ctx, lciphertext + written, &out_len);
        if (ret != YACA_ERROR_NONE)
                goto exit;
@@ -202,15 +199,13 @@ API int yaca_simple_decrypt(yaca_encrypt_algorithm_e algo,
        if (ret != YACA_ERROR_NONE)
                goto exit;
 
-       out_len = lplaintext_len;
        ret = yaca_decrypt_update(ctx, ciphertext, ciphertext_len, lplaintext, &out_len);
        if (ret != YACA_ERROR_NONE)
                goto exit;
 
        assert(out_len <= lplaintext_len);
-
        written = out_len;
-       out_len = lplaintext_len - written;
+
        ret = yaca_decrypt_finalize(ctx, lplaintext + written, &out_len);
        if (ret != YACA_ERROR_NONE)
                goto exit;