crypto-ansi_cprng: Fine-tuning for three function implementations
Subsystems (2)
cover crypto-ansi_cprng: Fine-tuning for three function implementations
From: Markus Elfring <[email protected]>
Date: Sat, 21 Oct 2017 19:06:54 +0200
Two update suggestions were taken into account
from static source code analysis.
Markus Elfring (3):
Use common error handling code in get_prng_bytes() Delete two variable assignments in get_prng_bytes() Delete unnecessary blank lines
crypto/ansi_cprng.c | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-)
Signature · 1 line
2.14.2
PATCH 1/3 crypto-ansi_cprng: Use common error handling code in get_prng_bytes()
From: Markus Elfring <[email protected]>
Date: Sat, 21 Oct 2017 15:17:52 +0200
Adjust jump targets so that a bit of exception handling can be better reused at the end of this function.
This issue was detected by using the Coccinelle software.
--- crypto/ansi_cprng.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-)
crypto/ansi_cprng.c +16 -18
@@ -195,7 +195,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, err = -EINVAL ;if (ctx->flags &PRNG_NEED_RESET ) - goto done; + goto unlock;/* * If the FIXED_SIZE flag is on, only return whole blocks of
@@ -204,7 +204,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, err = -EINVAL ;if (ctx->flags &PRNG_FIXED_SIZE ) {if (nbytes <DEFAULT_BLK_SZ ) - goto done; + goto unlock; byte_count =DEFAULT_BLK_SZ ; }
@@ -219,13 +219,9 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, remainder: -if (ctx->rand_data_valid =DEFAULT_BLK_SZ ) { -if (_get_more_prng_bytes (ctx, do_cont_test) <0 ) { -memset (buf,0 , nbytes); - err = -EINVAL ; - goto done; - } - } +if (ctx->rand_data_valid =DEFAULT_BLK_SZ && +_get_more_prng_bytes (ctx, do_cont_test) <0 ) + goto reset_memory;/* * Copy any data less than an entire block
@@ -238,7 +234,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, byte_count--; ctx->rand_data_valid++;if (byte_count =0 ) - goto done; + goto unlock; } }
@@ -246,13 +242,10 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,* Now copy whole blocks */ for (; byte_count >=DEFAULT_BLK_SZ ; byte_count -=DEFAULT_BLK_SZ ) { -if (ctx->rand_data_valid =DEFAULT_BLK_SZ ) { -if (_get_more_prng_bytes (ctx, do_cont_test) <0 ) { -memset (buf,0 , nbytes); - err = -EINVAL ; - goto done; - } - } +if (ctx->rand_data_valid =DEFAULT_BLK_SZ && +_get_more_prng_bytes (ctx, do_cont_test) <0 ) + goto reset_memory; +if (ctx->rand_data_valid >0 ) goto empty_rbuf;memcpy (ptr, ctx->rand_data,DEFAULT_BLK_SZ );
@@ -266,11 +259,16 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,if (byte_count) goto remainder; -done: +unlock:spin_unlock_bh (&ctx->prng_lock);dbgprint (KERN_CRIT "returning %d from get_prng_bytes in context %p\n" , err, ctx);return err; + +reset_memory: +memset (buf,0 , nbytes); + err = -EINVAL ; + goto unlock; }static void free_prng_context (struct prng_context *ctx)
Signature · 1 line
2.14.2
PATCH 2/3 crypto-ansi_cprng: Delete two variable assignments in get_prng_bytes()
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Sat, 21 Oct 2017 16:10:43 +0200
Adjust a jump target so that two error code assignments which are redundant before condition checks can be removed because the same value will be set as an exception handling at the end of this function.
--- crypto/ansi_cprng.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
crypto/ansi_cprng.c +3 -5
@@ -192,19 +192,16 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,spin_lock_bh (&ctx->prng_lock); - - err = -EINVAL ;if (ctx->flags &PRNG_NEED_RESET ) - goto unlock; + goto failure_indication;/* * If the FIXED_SIZE flag is on, only return whole blocks of * pseudo random data */ - err = -EINVAL ;if (ctx->flags &PRNG_FIXED_SIZE ) {if (nbytes <DEFAULT_BLK_SZ ) - goto unlock; + goto failure_indication; byte_count =DEFAULT_BLK_SZ ; }
@@ -267,6 +264,7 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx, reset_memory:memset (buf,0 , nbytes); +failure_indication: err = -EINVAL ; goto unlock; }
Signature · 1 line
2.14.2
PATCH 3/3 crypto-ansi_cprng: Delete unnecessary blank lines
In reply to SF Markus ElfringFrom: Markus Elfring <[email protected]>
Date: Sat, 21 Oct 2017 16:27:56 +0200
The script "checkpatch.pl" pointed information out like the following.
CHECK: Please don't use multiple blank lines
Thus fix the affected source code places.
--- crypto/ansi_cprng.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
crypto/ansi_cprng.c +1 -8
@@ -40,7 +40,6 @@* for implementation details */ -struct prng_context {spinlock_t prng_lock;unsigned char rand_data[DEFAULT_BLK_SZ ];
@@ -77,8 +76,8 @@ static void xor_vectors(unsigned char *in1, unsigned char *in2,for (i =0 ; i < size; i++) out[i] = in1[i] ^ in2[i]; - } +/* * Returns DEFAULT_BLK_SZ bytes of random data per call * returns 0 if generation succeeded, <0 if something went wrong
@@ -89,7 +88,6 @@ static int _get_more_prng_bytes(struct prng_context *ctx, int cont_test)unsigned char tmp[DEFAULT_BLK_SZ ];unsigned char *output =NULL ; -dbgprint (KERN_CRIT "Calling _get_more_prng_bytes for context %p\n" , ctx);
@@ -101,7 +99,6 @@ static int _get_more_prng_bytes(struct prng_context *ctx, int cont_test)* This algorithm is a 3 stage state machine */ for (i =0 ; i <3 ; i++) { -switch (i) {case 0 :/*
@@ -156,10 +153,8 @@ static int _get_more_prng_bytes(struct prng_context *ctx, int cont_test)break ; } -/* do the encryption */ crypto_cipher_encrypt_one (ctx->tfm, output, tmp); - }/*
@@ -190,7 +185,6 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,unsigned int byte_count = (unsigned int )nbytes;int err; -spin_lock_bh (&ctx->prng_lock);if (ctx->flags &PRNG_NEED_RESET ) goto failure_indication;
@@ -214,7 +208,6 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,dbgprint (KERN_CRIT "getting %d random bytes for context %p\n" , byte_count, ctx); - remainder:if (ctx->rand_data_valid =DEFAULT_BLK_SZ &&_get_more_prng_bytes (ctx, do_cont_test) <0 )
Signature · 1 line
2.14.2