From: Dariusz Michaluk Date: Wed, 27 Jul 2016 12:13:33 +0000 (+0200) Subject: Fix and simplify output parameter usage. X-Git-Tag: submit/tizen/20160901.024233~35^2~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fbb2263a06ef38923c6b2cab68f77d554e79c5a9;p=platform%2Fcore%2Fsecurity%2Fyaca.git Fix and simplify output parameter usage. Change-Id: I04d97b11fafe428f6c48c394539cb6e10f45f725 --- diff --git a/examples/encrypt.c b/examples/encrypt.c index 31c8806..3f6274c 100644 --- a/examples/encrypt.c +++ b/examples/encrypt.c @@ -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); } diff --git a/examples/encrypt_aes_gcm_ccm.c b/examples/encrypt_aes_gcm_ccm.c index c990d79..719ff77 100644 --- a/examples/encrypt_aes_gcm_ccm.c +++ b/examples/encrypt_aes_gcm_ccm.c @@ -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) diff --git a/examples/seal.c b/examples/seal.c index 64b70f7..ff1a9e8 100644 --- a/examples/seal.c +++ b/examples/seal.c @@ -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) diff --git a/src/simple.c b/src/simple.c index 101822d..4301248 100644 --- a/src/simple.c +++ b/src/simple.c @@ -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;