Skip to content

Commit

Permalink
crypto: null - Add default null skcipher
Browse files Browse the repository at this point in the history
This patch adds a default null skcipher for users such as gcm
to perform copies on SG lists.

Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
herbertx committed May 22, 2015
1 parent 63293c6 commit 3302346
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
39 changes: 39 additions & 0 deletions crypto/crypto_null.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <linux/mm.h>
#include <linux/string.h>

static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
static struct crypto_blkcipher *crypto_default_null_skcipher;
static int crypto_default_null_skcipher_refcnt;

static int null_compress(struct crypto_tfm *tfm, const u8 *src,
unsigned int slen, u8 *dst, unsigned int *dlen)
{
Expand Down Expand Up @@ -149,6 +153,41 @@ MODULE_ALIAS_CRYPTO("compress_null");
MODULE_ALIAS_CRYPTO("digest_null");
MODULE_ALIAS_CRYPTO("cipher_null");

struct crypto_blkcipher *crypto_get_default_null_skcipher(void)
{
struct crypto_blkcipher *tfm;

mutex_lock(&crypto_default_null_skcipher_lock);
tfm = crypto_default_null_skcipher;

if (!tfm) {
tfm = crypto_alloc_blkcipher("ecb(cipher_null)", 0, 0);
if (IS_ERR(tfm))
goto unlock;

crypto_default_null_skcipher = tfm;
}

crypto_default_null_skcipher_refcnt++;

unlock:
mutex_unlock(&crypto_default_null_skcipher_lock);

return tfm;
}
EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);

void crypto_put_default_null_skcipher(void)
{
mutex_lock(&crypto_default_null_skcipher_lock);
if (!--crypto_default_null_skcipher_refcnt) {
crypto_free_blkcipher(crypto_default_null_skcipher);
crypto_default_null_skcipher = NULL;
}
mutex_unlock(&crypto_default_null_skcipher_lock);
}
EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);

static int __init crypto_null_mod_init(void)
{
int ret = 0;
Expand Down
3 changes: 3 additions & 0 deletions include/crypto/null.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
#define NULL_DIGEST_SIZE 0
#define NULL_IV_SIZE 0

struct crypto_blkcipher *crypto_get_default_null_skcipher(void);
void crypto_put_default_null_skcipher(void);

#endif

0 comments on commit 3302346

Please sign in to comment.