Skip to content

Commit

Permalink
crypto: rng - RNGs must return 0 in success case
Browse files Browse the repository at this point in the history
Change the RNGs to always return 0 in success case.

This patch ensures that seqiv.c works with RNGs other than krng. seqiv
expects that any return code other than 0 is an error. Without the
patch, rfc4106(gcm(aes)) will not work when using a DRBG or an ANSI
X9.31 RNG.

Signed-off-by: Stephan Mueller <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
smuellerDD authored and herbertx committed Mar 9, 2015
1 parent 4842234 commit cde001e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 5 additions & 1 deletion crypto/ansi_cprng.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ static int get_prng_bytes(char *buf, size_t nbytes, struct prng_context *ctx,
byte_count = DEFAULT_BLK_SZ;
}

err = byte_count;
/*
* Return 0 in case of success as mandated by the kernel
* crypto API interface definition.
*/
err = 0;

dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",
byte_count, ctx);
Expand Down
7 changes: 6 additions & 1 deletion crypto/drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ static void drbg_restore_shadow(struct drbg_state *drbg,
* as defined in SP800-90A. The additional input is mixed into
* the state in addition to the pulled entropy.
*
* return: generated number of bytes
* return: 0 when all bytes are generated; < 0 in case of an error
*/
static int drbg_generate(struct drbg_state *drbg,
unsigned char *buf, unsigned int buflen,
Expand Down Expand Up @@ -1419,6 +1419,11 @@ static int drbg_generate(struct drbg_state *drbg,
}
#endif

/*
* All operations were successful, return 0 as mandated by
* the kernel crypto API interface.
*/
len = 0;
err:
shadow->d_ops->crypto_fini(shadow);
drbg_restore_shadow(drbg, &shadow);
Expand Down
3 changes: 1 addition & 2 deletions include/crypto/rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ static inline void crypto_free_rng(struct crypto_rng *tfm)
* This function fills the caller-allocated buffer with random numbers using the
* random number generator referenced by the cipher handle.
*
* Return: > 0 function was successful and returns the number of generated
* bytes; < 0 if an error occurred
* Return: 0 function was successful; < 0 if an error occurred
*/
static inline int crypto_rng_get_bytes(struct crypto_rng *tfm,
u8 *rdata, unsigned int dlen)
Expand Down

0 comments on commit cde001e

Please sign in to comment.