Skip to content

Commit 3302346

Browse files
committed
crypto: null - Add default null skcipher
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]>
1 parent 63293c6 commit 3302346

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

crypto/crypto_null.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <linux/mm.h>
2626
#include <linux/string.h>
2727

28+
static DEFINE_MUTEX(crypto_default_null_skcipher_lock);
29+
static struct crypto_blkcipher *crypto_default_null_skcipher;
30+
static int crypto_default_null_skcipher_refcnt;
31+
2832
static int null_compress(struct crypto_tfm *tfm, const u8 *src,
2933
unsigned int slen, u8 *dst, unsigned int *dlen)
3034
{
@@ -149,6 +153,41 @@ MODULE_ALIAS_CRYPTO("compress_null");
149153
MODULE_ALIAS_CRYPTO("digest_null");
150154
MODULE_ALIAS_CRYPTO("cipher_null");
151155

156+
struct crypto_blkcipher *crypto_get_default_null_skcipher(void)
157+
{
158+
struct crypto_blkcipher *tfm;
159+
160+
mutex_lock(&crypto_default_null_skcipher_lock);
161+
tfm = crypto_default_null_skcipher;
162+
163+
if (!tfm) {
164+
tfm = crypto_alloc_blkcipher("ecb(cipher_null)", 0, 0);
165+
if (IS_ERR(tfm))
166+
goto unlock;
167+
168+
crypto_default_null_skcipher = tfm;
169+
}
170+
171+
crypto_default_null_skcipher_refcnt++;
172+
173+
unlock:
174+
mutex_unlock(&crypto_default_null_skcipher_lock);
175+
176+
return tfm;
177+
}
178+
EXPORT_SYMBOL_GPL(crypto_get_default_null_skcipher);
179+
180+
void crypto_put_default_null_skcipher(void)
181+
{
182+
mutex_lock(&crypto_default_null_skcipher_lock);
183+
if (!--crypto_default_null_skcipher_refcnt) {
184+
crypto_free_blkcipher(crypto_default_null_skcipher);
185+
crypto_default_null_skcipher = NULL;
186+
}
187+
mutex_unlock(&crypto_default_null_skcipher_lock);
188+
}
189+
EXPORT_SYMBOL_GPL(crypto_put_default_null_skcipher);
190+
152191
static int __init crypto_null_mod_init(void)
153192
{
154193
int ret = 0;

include/crypto/null.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
#define NULL_DIGEST_SIZE 0
99
#define NULL_IV_SIZE 0
1010

11+
struct crypto_blkcipher *crypto_get_default_null_skcipher(void);
12+
void crypto_put_default_null_skcipher(void);
13+
1114
#endif

0 commit comments

Comments
 (0)