2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2007-11-26 14:24:11 +00:00
|
|
|
/*
|
|
|
|
* GCM: Galois/Counter Mode.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/gf128mul.h>
|
2007-12-17 07:33:17 +00:00
|
|
|
#include <crypto/internal/aead.h>
|
2007-12-07 11:26:11 +00:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2009-08-06 05:34:26 +00:00
|
|
|
#include <crypto/internal/hash.h>
|
2015-05-21 07:11:11 +00:00
|
|
|
#include <crypto/null.h>
|
2007-12-07 10:52:49 +00:00
|
|
|
#include <crypto/scatterwalk.h>
|
2017-08-22 08:08:17 +00:00
|
|
|
#include <crypto/gcm.h>
|
2009-08-06 05:34:26 +00:00
|
|
|
#include <crypto/hash.h>
|
2007-11-26 14:24:11 +00:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
struct gcm_instance_ctx {
|
2007-12-07 11:26:11 +00:00
|
|
|
struct crypto_skcipher_spawn ctr;
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_ahash_spawn ghash;
|
2007-11-26 14:24:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct crypto_gcm_ctx {
|
2016-07-12 05:17:39 +00:00
|
|
|
struct crypto_skcipher *ctr;
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_ahash *ghash;
|
2007-11-26 14:24:11 +00:00
|
|
|
};
|
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
struct crypto_rfc4106_ctx {
|
|
|
|
struct crypto_aead *child;
|
|
|
|
u8 nonce[4];
|
|
|
|
};
|
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
struct crypto_rfc4106_req_ctx {
|
|
|
|
struct scatterlist src[3];
|
|
|
|
struct scatterlist dst[3];
|
|
|
|
struct aead_request subreq;
|
|
|
|
};
|
|
|
|
|
2013-04-07 13:43:41 +00:00
|
|
|
struct crypto_rfc4543_instance_ctx {
|
|
|
|
struct crypto_aead_spawn aead;
|
|
|
|
};
|
|
|
|
|
2010-01-17 10:52:11 +00:00
|
|
|
struct crypto_rfc4543_ctx {
|
|
|
|
struct crypto_aead *child;
|
2018-09-19 02:10:51 +00:00
|
|
|
struct crypto_sync_skcipher *null;
|
2010-01-17 10:52:11 +00:00
|
|
|
u8 nonce[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct crypto_rfc4543_req_ctx {
|
|
|
|
struct aead_request subreq;
|
|
|
|
};
|
|
|
|
|
2007-11-26 14:24:11 +00:00
|
|
|
struct crypto_gcm_ghash_ctx {
|
2009-08-06 05:34:26 +00:00
|
|
|
unsigned int cryptlen;
|
|
|
|
struct scatterlist *src;
|
2015-06-16 05:54:18 +00:00
|
|
|
int (*complete)(struct aead_request *req, u32 flags);
|
2007-11-26 14:24:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct crypto_gcm_req_priv_ctx {
|
2015-06-16 05:54:18 +00:00
|
|
|
u8 iv[16];
|
2007-11-26 14:24:11 +00:00
|
|
|
u8 auth_tag[16];
|
2007-12-04 08:17:50 +00:00
|
|
|
u8 iauth_tag[16];
|
2015-06-16 05:54:18 +00:00
|
|
|
struct scatterlist src[3];
|
|
|
|
struct scatterlist dst[3];
|
|
|
|
struct scatterlist sg;
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_ghash_ctx ghash_ctx;
|
|
|
|
union {
|
|
|
|
struct ahash_request ahreq;
|
2016-07-12 05:17:39 +00:00
|
|
|
struct skcipher_request skreq;
|
2009-08-06 05:34:26 +00:00
|
|
|
} u;
|
2007-11-26 14:24:11 +00:00
|
|
|
};
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static struct {
|
|
|
|
u8 buf[16];
|
|
|
|
struct scatterlist sg;
|
|
|
|
} *gcm_zeroes;
|
|
|
|
|
|
|
|
static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2007-12-12 11:16:38 +00:00
|
|
|
static inline struct crypto_gcm_req_priv_ctx *crypto_gcm_reqctx(
|
|
|
|
struct aead_request *req)
|
|
|
|
{
|
|
|
|
unsigned long align = crypto_aead_alignmask(crypto_aead_reqtfm(req));
|
|
|
|
|
|
|
|
return (void *)PTR_ALIGN((u8 *)aead_request_ctx(req), align + 1);
|
|
|
|
}
|
|
|
|
|
2007-11-26 14:24:11 +00:00
|
|
|
static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
|
|
|
|
unsigned int keylen)
|
|
|
|
{
|
|
|
|
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_ahash *ghash = ctx->ghash;
|
2016-07-12 05:17:39 +00:00
|
|
|
struct crypto_skcipher *ctr = ctx->ctr;
|
2007-12-17 13:42:08 +00:00
|
|
|
struct {
|
|
|
|
be128 hash;
|
2016-09-23 08:47:32 +00:00
|
|
|
u8 iv[16];
|
2007-12-17 13:42:08 +00:00
|
|
|
|
2017-10-18 07:00:42 +00:00
|
|
|
struct crypto_wait wait;
|
2007-12-17 13:42:08 +00:00
|
|
|
|
|
|
|
struct scatterlist sg[1];
|
2016-07-12 05:17:39 +00:00
|
|
|
struct skcipher_request req;
|
2007-12-17 13:42:08 +00:00
|
|
|
} *data;
|
|
|
|
int err;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2016-07-12 05:17:39 +00:00
|
|
|
crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
|
|
|
|
CRYPTO_TFM_REQ_MASK);
|
|
|
|
err = crypto_skcipher_setkey(ctr, key, keylen);
|
2007-11-26 14:24:11 +00:00
|
|
|
if (err)
|
2007-12-17 13:42:08 +00:00
|
|
|
return err;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2016-07-12 05:17:39 +00:00
|
|
|
data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
|
2007-12-17 13:42:08 +00:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-10-18 07:00:42 +00:00
|
|
|
crypto_init_wait(&data->wait);
|
2007-12-17 13:42:08 +00:00
|
|
|
sg_init_one(data->sg, &data->hash, sizeof(data->hash));
|
2016-07-12 05:17:39 +00:00
|
|
|
skcipher_request_set_tfm(&data->req, ctr);
|
|
|
|
skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
|
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG,
|
2017-10-18 07:00:42 +00:00
|
|
|
crypto_req_done,
|
|
|
|
&data->wait);
|
2016-07-12 05:17:39 +00:00
|
|
|
skcipher_request_set_crypt(&data->req, data->sg, data->sg,
|
|
|
|
sizeof(data->hash), data->iv);
|
|
|
|
|
2017-10-18 07:00:42 +00:00
|
|
|
err = crypto_wait_req(crypto_skcipher_encrypt(&data->req),
|
|
|
|
&data->wait);
|
2007-12-17 13:42:08 +00:00
|
|
|
|
2007-11-26 14:24:11 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
crypto_ahash_clear_flags(ghash, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_ahash_set_flags(ghash, crypto_aead_get_flags(aead) &
|
|
|
|
CRYPTO_TFM_REQ_MASK);
|
|
|
|
err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
|
2007-12-17 13:42:08 +00:00
|
|
|
out:
|
2020-08-07 06:18:13 +00:00
|
|
|
kfree_sensitive(data);
|
2007-11-26 14:24:11 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
static int crypto_gcm_setauthsize(struct crypto_aead *tfm,
|
|
|
|
unsigned int authsize)
|
|
|
|
{
|
2019-07-31 13:05:54 +00:00
|
|
|
return crypto_gcm_check_authsize(authsize);
|
2007-12-17 07:33:17 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static void crypto_gcm_init_common(struct aead_request *req)
|
2007-11-26 14:24:11 +00:00
|
|
|
{
|
2007-12-12 11:16:38 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2007-12-17 13:42:08 +00:00
|
|
|
__be32 counter = cpu_to_be32(1);
|
2015-06-16 05:54:18 +00:00
|
|
|
struct scatterlist *sg;
|
2007-12-17 13:42:08 +00:00
|
|
|
|
|
|
|
memset(pctx->auth_tag, 0, sizeof(pctx->auth_tag));
|
2017-08-22 08:08:17 +00:00
|
|
|
memcpy(pctx->iv, req->iv, GCM_AES_IV_SIZE);
|
|
|
|
memcpy(pctx->iv + GCM_AES_IV_SIZE, &counter, 4);
|
2007-12-17 13:42:08 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
sg_init_table(pctx->src, 3);
|
2007-12-17 13:42:08 +00:00
|
|
|
sg_set_buf(pctx->src, pctx->auth_tag, sizeof(pctx->auth_tag));
|
2015-06-16 05:54:18 +00:00
|
|
|
sg = scatterwalk_ffwd(pctx->src + 1, req->src, req->assoclen);
|
|
|
|
if (sg != pctx->src + 1)
|
2015-08-07 16:15:13 +00:00
|
|
|
sg_chain(pctx->src, 2, sg);
|
2007-12-17 13:42:08 +00:00
|
|
|
|
|
|
|
if (req->src != req->dst) {
|
2015-06-16 05:54:18 +00:00
|
|
|
sg_init_table(pctx->dst, 3);
|
2007-12-17 13:42:08 +00:00
|
|
|
sg_set_buf(pctx->dst, pctx->auth_tag, sizeof(pctx->auth_tag));
|
2015-06-16 05:54:18 +00:00
|
|
|
sg = scatterwalk_ffwd(pctx->dst + 1, req->dst, req->assoclen);
|
|
|
|
if (sg != pctx->dst + 1)
|
2015-08-07 16:15:13 +00:00
|
|
|
sg_chain(pctx->dst, 2, sg);
|
2007-12-17 13:42:08 +00:00
|
|
|
}
|
2015-06-16 05:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void crypto_gcm_init_crypt(struct aead_request *req,
|
|
|
|
unsigned int cryptlen)
|
|
|
|
{
|
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
|
|
|
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
|
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2016-07-12 05:17:39 +00:00
|
|
|
struct skcipher_request *skreq = &pctx->u.skreq;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct scatterlist *dst;
|
|
|
|
|
|
|
|
dst = req->src == req->dst ? pctx->src : pctx->dst;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2016-07-12 05:17:39 +00:00
|
|
|
skcipher_request_set_tfm(skreq, ctx->ctr);
|
|
|
|
skcipher_request_set_crypt(skreq, pctx->src, dst,
|
2007-12-17 13:42:08 +00:00
|
|
|
cryptlen + sizeof(pctx->auth_tag),
|
2015-06-16 05:54:18 +00:00
|
|
|
pctx->iv);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned int gcm_remain(unsigned int len)
|
|
|
|
{
|
|
|
|
len &= 0xfU;
|
|
|
|
return len ? 16 - len : 0;
|
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_len_done(void *data, int err);
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
static int gcm_hash_update(struct aead_request *req,
|
2014-07-25 09:53:38 +00:00
|
|
|
crypto_completion_t compl,
|
2009-08-06 05:34:26 +00:00
|
|
|
struct scatterlist *src,
|
2015-06-16 05:54:18 +00:00
|
|
|
unsigned int len, u32 flags)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct ahash_request *ahreq = &pctx->u.ahreq;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
ahash_request_set_callback(ahreq, flags, compl, req);
|
2009-08-06 05:34:26 +00:00
|
|
|
ahash_request_set_crypt(ahreq, src, NULL, len);
|
|
|
|
|
|
|
|
return crypto_ahash_update(ahreq);
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
static int gcm_hash_remain(struct aead_request *req,
|
|
|
|
unsigned int remain,
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_completion_t compl, u32 flags)
|
2007-11-26 14:24:11 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash_update(req, compl, &gcm_zeroes->sg, remain, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_len(struct aead_request *req, u32 flags)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct ahash_request *ahreq = &pctx->u.ahreq;
|
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
2019-01-10 20:17:52 +00:00
|
|
|
be128 lengths;
|
2009-08-06 05:34:26 +00:00
|
|
|
|
|
|
|
lengths.a = cpu_to_be64(req->assoclen * 8);
|
|
|
|
lengths.b = cpu_to_be64(gctx->cryptlen * 8);
|
|
|
|
memcpy(pctx->iauth_tag, &lengths, 16);
|
2015-06-16 05:54:18 +00:00
|
|
|
sg_init_one(&pctx->sg, pctx->iauth_tag, 16);
|
|
|
|
ahash_request_set_callback(ahreq, flags, gcm_hash_len_done, req);
|
|
|
|
ahash_request_set_crypt(ahreq, &pctx->sg,
|
|
|
|
pctx->iauth_tag, sizeof(lengths));
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return crypto_ahash_finup(ahreq);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_len_continue(struct aead_request *req, u32 flags)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2007-12-12 11:16:38 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return gctx->complete(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_len_done(void *data, int err)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
err = gcm_hash_len_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
out:
|
|
|
|
aead_request_complete(req, err);
|
2009-11-16 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_crypt_remain_continue(struct aead_request *req, u32 flags)
|
2009-11-16 13:52:22 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash_len(req, flags) ?:
|
|
|
|
gcm_hash_len_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_crypt_remain_done(void *data, int err)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = gcm_hash_crypt_remain_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
aead_request_complete(req, err);
|
2009-11-16 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_crypt_continue(struct aead_request *req, u32 flags)
|
2009-11-16 13:52:22 +00:00
|
|
|
{
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
|
|
|
unsigned int remain;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
remain = gcm_remain(gctx->cryptlen);
|
|
|
|
if (remain)
|
|
|
|
return gcm_hash_remain(req, remain,
|
|
|
|
gcm_hash_crypt_remain_done, flags) ?:
|
|
|
|
gcm_hash_crypt_remain_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash_crypt_remain_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_crypt_done(void *data, int err)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = gcm_hash_crypt_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
aead_request_complete(req, err);
|
2009-11-16 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_assoc_remain_continue(struct aead_request *req, u32 flags)
|
2009-11-16 13:52:22 +00:00
|
|
|
{
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (gctx->cryptlen)
|
|
|
|
return gcm_hash_update(req, gcm_hash_crypt_done,
|
|
|
|
gctx->src, gctx->cryptlen, flags) ?:
|
|
|
|
gcm_hash_crypt_continue(req, flags);
|
|
|
|
|
|
|
|
return gcm_hash_crypt_remain_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_assoc_remain_done(void *data, int err)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = gcm_hash_assoc_remain_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
aead_request_complete(req, err);
|
2009-11-16 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_assoc_continue(struct aead_request *req, u32 flags)
|
2009-11-16 13:52:22 +00:00
|
|
|
{
|
2009-08-06 05:34:26 +00:00
|
|
|
unsigned int remain;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
remain = gcm_remain(req->assoclen);
|
|
|
|
if (remain)
|
|
|
|
return gcm_hash_remain(req, remain,
|
|
|
|
gcm_hash_assoc_remain_done, flags) ?:
|
|
|
|
gcm_hash_assoc_remain_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash_assoc_remain_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_assoc_done(void *data, int err)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = gcm_hash_assoc_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
aead_request_complete(req, err);
|
2009-11-16 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash_init_continue(struct aead_request *req, u32 flags)
|
2009-11-16 13:52:22 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
if (req->assoclen)
|
|
|
|
return gcm_hash_update(req, gcm_hash_assoc_done,
|
|
|
|
req->src, req->assoclen, flags) ?:
|
|
|
|
gcm_hash_assoc_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash_assoc_remain_continue(req, flags);
|
2009-11-16 13:52:22 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_hash_init_done(void *data, int err)
|
2009-11-16 13:52:22 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-11-16 13:52:22 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = gcm_hash_init_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
out:
|
|
|
|
aead_request_complete(req, err);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_hash(struct aead_request *req, u32 flags)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct ahash_request *ahreq = &pctx->u.ahreq;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
|
2009-08-06 05:34:26 +00:00
|
|
|
|
|
|
|
ahash_request_set_tfm(ahreq, ctx->ghash);
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
ahash_request_set_callback(ahreq, flags, gcm_hash_init_done, req);
|
|
|
|
return crypto_ahash_init(ahreq) ?:
|
|
|
|
gcm_hash_init_continue(req, flags);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_enc_copy_hash(struct aead_request *req, u32 flags)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
|
|
|
u8 *auth_tag = pctx->auth_tag;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_xor(auth_tag, pctx->iauth_tag, 16);
|
|
|
|
scatterwalk_map_and_copy(auth_tag, req->dst,
|
|
|
|
req->assoclen + req->cryptlen,
|
2007-12-04 08:17:50 +00:00
|
|
|
crypto_aead_authsize(aead), 1);
|
2015-06-16 05:54:18 +00:00
|
|
|
return 0;
|
2007-12-04 08:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_encrypt_continue(struct aead_request *req, u32 flags)
|
2007-12-04 08:17:50 +00:00
|
|
|
{
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
2007-12-04 08:17:50 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
gctx->src = sg_next(req->src == req->dst ? pctx->src : pctx->dst);
|
|
|
|
gctx->cryptlen = req->cryptlen;
|
|
|
|
gctx->complete = gcm_enc_copy_hash;
|
2007-12-04 08:17:50 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash(req, flags);
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_encrypt_done(void *data, int err)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = gcm_encrypt_continue(req, 0);
|
|
|
|
if (err == -EINPROGRESS)
|
|
|
|
return;
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
out:
|
2009-11-16 13:52:22 +00:00
|
|
|
aead_request_complete(req, err);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2007-11-26 14:24:11 +00:00
|
|
|
static int crypto_gcm_encrypt(struct aead_request *req)
|
|
|
|
{
|
2007-12-12 11:16:38 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2016-07-12 05:17:39 +00:00
|
|
|
struct skcipher_request *skreq = &pctx->u.skreq;
|
2015-06-16 05:54:18 +00:00
|
|
|
u32 flags = aead_request_flags(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_gcm_init_common(req);
|
|
|
|
crypto_gcm_init_crypt(req, req->cryptlen);
|
2016-07-12 05:17:39 +00:00
|
|
|
skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2016-07-12 05:17:39 +00:00
|
|
|
return crypto_skcipher_encrypt(skreq) ?:
|
2015-06-16 05:54:18 +00:00
|
|
|
gcm_encrypt_continue(req, flags);
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_gcm_verify(struct aead_request *req)
|
2007-12-17 13:42:08 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2007-12-17 13:42:08 +00:00
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
|
|
|
u8 *auth_tag = pctx->auth_tag;
|
|
|
|
u8 *iauth_tag = pctx->iauth_tag;
|
|
|
|
unsigned int authsize = crypto_aead_authsize(aead);
|
|
|
|
unsigned int cryptlen = req->cryptlen - authsize;
|
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
crypto_xor(auth_tag, iauth_tag, 16);
|
2015-06-16 05:54:18 +00:00
|
|
|
scatterwalk_map_and_copy(iauth_tag, req->src,
|
|
|
|
req->assoclen + cryptlen, authsize, 0);
|
crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks
When comparing MAC hashes, AEAD authentication tags, or other hash
values in the context of authentication or integrity checking, it
is important not to leak timing information to a potential attacker,
i.e. when communication happens over a network.
Bytewise memory comparisons (such as memcmp) are usually optimized so
that they return a nonzero value as soon as a mismatch is found. E.g,
on x86_64/i5 for 512 bytes this can be ~50 cyc for a full mismatch
and up to ~850 cyc for a full match (cold). This early-return behavior
can leak timing information as a side channel, allowing an attacker to
iteratively guess the correct result.
This patch adds a new method crypto_memneq ("memory not equal to each
other") to the crypto API that compares memory areas of the same length
in roughly "constant time" (cache misses could change the timing, but
since they don't reveal information about the content of the strings
being compared, they are effectively benign). Iow, best and worst case
behaviour take the same amount of time to complete (in contrast to
memcmp).
Note that crypto_memneq (unlike memcmp) can only be used to test for
equality or inequality, NOT for lexicographical order. This, however,
is not an issue for its use-cases within the crypto API.
We tried to locate all of the places in the crypto API where memcmp was
being used for authentication or integrity checking, and convert them
over to crypto_memneq.
crypto_memneq is declared noinline, placed in its own source file,
and compiled with optimizations that might increase code size disabled
("Os") because a smart compiler (or LTO) might notice that the return
value is always compared against zero/nonzero, and might then
reintroduce the same early-return optimization that we are trying to
avoid.
Using #pragma or __attribute__ optimization annotations of the code
for disabling optimization was avoided as it seems to be considered
broken or unmaintained for long time in GCC [1]. Therefore, we work
around that by specifying the compile flag for memneq.o directly in
the Makefile. We found that this seems to be most appropriate.
As we use ("Os"), this patch also provides a loop-free "fast-path" for
frequently used 16 byte digests. Similarly to kernel library string
functions, leave an option for future even further optimized architecture
specific assembler implementations.
This was a joint work of James Yonan and Daniel Borkmann. Also thanks
for feedback from Florian Weimer on this and earlier proposals [2].
[1] http://gcc.gnu.org/ml/gcc/2012-07/msg00211.html
[2] https://lkml.org/lkml/2013/2/10/131
Signed-off-by: James Yonan <james@openvpn.net>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Florian Weimer <fw@deneb.enyo.de>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2013-09-26 08:20:39 +00:00
|
|
|
return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
|
2007-12-17 13:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 05:58:44 +00:00
|
|
|
static void gcm_decrypt_done(void *data, int err)
|
2007-11-26 14:24:11 +00:00
|
|
|
{
|
2023-02-08 05:58:44 +00:00
|
|
|
struct aead_request *req = data;
|
2007-12-17 13:42:08 +00:00
|
|
|
|
|
|
|
if (!err)
|
2015-06-16 05:54:18 +00:00
|
|
|
err = crypto_gcm_verify(req);
|
2007-12-17 13:42:08 +00:00
|
|
|
|
|
|
|
aead_request_complete(req, err);
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
|
2009-08-06 05:34:26 +00:00
|
|
|
{
|
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2016-07-12 05:17:39 +00:00
|
|
|
struct skcipher_request *skreq = &pctx->u.skreq;
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_gcm_init_crypt(req, gctx->cryptlen);
|
2016-07-12 05:17:39 +00:00
|
|
|
skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
|
|
|
|
return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
}
|
|
|
|
|
2007-11-26 14:24:11 +00:00
|
|
|
static int crypto_gcm_decrypt(struct aead_request *req)
|
|
|
|
{
|
2007-12-04 08:17:50 +00:00
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
2007-12-12 11:16:38 +00:00
|
|
|
struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
|
2007-12-04 08:17:50 +00:00
|
|
|
unsigned int authsize = crypto_aead_authsize(aead);
|
2009-08-06 05:34:26 +00:00
|
|
|
unsigned int cryptlen = req->cryptlen;
|
2015-06-16 05:54:18 +00:00
|
|
|
u32 flags = aead_request_flags(req);
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2007-12-04 08:17:50 +00:00
|
|
|
cryptlen -= authsize;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_gcm_init_common(req);
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
gctx->src = sg_next(pctx->src);
|
|
|
|
gctx->cryptlen = cryptlen;
|
|
|
|
gctx->complete = gcm_dec_hash_continue;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
return gcm_hash(req, flags);
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
|
2007-11-26 14:24:11 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_instance *inst = aead_alg_instance(tfm);
|
|
|
|
struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
|
|
|
|
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
|
2016-07-12 05:17:39 +00:00
|
|
|
struct crypto_skcipher *ctr;
|
2009-08-06 05:34:26 +00:00
|
|
|
struct crypto_ahash *ghash;
|
2007-11-26 14:24:11 +00:00
|
|
|
unsigned long align;
|
|
|
|
int err;
|
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
ghash = crypto_spawn_ahash(&ictx->ghash);
|
|
|
|
if (IS_ERR(ghash))
|
|
|
|
return PTR_ERR(ghash);
|
|
|
|
|
2016-10-28 16:52:19 +00:00
|
|
|
ctr = crypto_spawn_skcipher(&ictx->ctr);
|
2007-11-26 14:24:11 +00:00
|
|
|
err = PTR_ERR(ctr);
|
|
|
|
if (IS_ERR(ctr))
|
2009-08-06 05:34:26 +00:00
|
|
|
goto err_free_hash;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
|
|
|
ctx->ctr = ctr;
|
2009-08-06 05:34:26 +00:00
|
|
|
ctx->ghash = ghash;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
align = crypto_aead_alignmask(tfm);
|
2007-11-26 14:24:11 +00:00
|
|
|
align &= ~(crypto_tfm_ctx_alignment() - 1);
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_aead_set_reqsize(tfm,
|
2015-05-11 09:47:59 +00:00
|
|
|
align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
|
2016-07-12 05:17:39 +00:00
|
|
|
max(sizeof(struct skcipher_request) +
|
|
|
|
crypto_skcipher_reqsize(ctr),
|
2009-08-06 05:34:26 +00:00
|
|
|
sizeof(struct ahash_request) +
|
2015-05-11 09:47:59 +00:00
|
|
|
crypto_ahash_reqsize(ghash)));
|
2007-11-26 14:24:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-08-06 05:34:26 +00:00
|
|
|
|
|
|
|
err_free_hash:
|
|
|
|
crypto_free_ahash(ghash);
|
|
|
|
return err;
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
|
2007-11-26 14:24:11 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
crypto_free_ahash(ctx->ghash);
|
2016-07-12 05:17:39 +00:00
|
|
|
crypto_free_skcipher(ctx->ctr);
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
static void crypto_gcm_free(struct aead_instance *inst)
|
|
|
|
{
|
|
|
|
struct gcm_instance_ctx *ctx = aead_instance_ctx(inst);
|
|
|
|
|
|
|
|
crypto_drop_skcipher(&ctx->ctr);
|
|
|
|
crypto_drop_ahash(&ctx->ghash);
|
|
|
|
kfree(inst);
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_gcm_create_common(struct crypto_template *tmpl,
|
|
|
|
struct rtattr **tb,
|
|
|
|
const char *ctr_name,
|
|
|
|
const char *ghash_name)
|
2007-11-26 14:24:11 +00:00
|
|
|
{
|
2020-01-03 03:58:45 +00:00
|
|
|
u32 mask;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_instance *inst;
|
2020-01-03 03:58:57 +00:00
|
|
|
struct gcm_instance_ctx *ctx;
|
2016-07-12 05:17:39 +00:00
|
|
|
struct skcipher_alg *ctr;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct hash_alg_common *ghash;
|
2007-11-26 14:24:11 +00:00
|
|
|
int err;
|
|
|
|
|
2020-07-10 06:20:38 +00:00
|
|
|
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2020-01-03 03:58:45 +00:00
|
|
|
|
2007-12-07 11:26:11 +00:00
|
|
|
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
|
|
|
|
if (!inst)
|
2020-01-03 03:58:57 +00:00
|
|
|
return -ENOMEM;
|
2015-06-16 05:54:18 +00:00
|
|
|
ctx = aead_instance_ctx(inst);
|
2020-01-03 03:58:57 +00:00
|
|
|
|
|
|
|
err = crypto_grab_ahash(&ctx->ghash, aead_crypto_instance(inst),
|
|
|
|
ghash_name, 0, mask);
|
2009-08-06 05:34:26 +00:00
|
|
|
if (err)
|
|
|
|
goto err_free_inst;
|
2020-01-03 03:58:57 +00:00
|
|
|
ghash = crypto_spawn_ahash_alg(&ctx->ghash);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
err = -EINVAL;
|
crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
GCM instances can be created by either the "gcm" template, which only
allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
which allows choosing the ctr and ghash implementations, e.g.
"gcm_base(ctr(aes-generic),ghash-generic)".
However, a "gcm_base" instance prevents a "gcm" instance from being
registered using the same implementations. Nor will the instance be
found by lookups of "gcm". This can be used as a denial of service.
Moreover, "gcm_base" instances are never tested by the crypto
self-tests, even if there are compatible "gcm" tests.
The root cause of these problems is that instances of the two templates
use different cra_names. Therefore, fix these problems by making
"gcm_base" instances set the same cra_name as "gcm" instances, e.g.
"gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
This requires extracting the block cipher name from the name of the ctr
algorithm. It also requires starting to verify that the algorithms are
really ctr and ghash, not something else entirely. But it would be
bizarre if anyone were actually using non-gcm-compatible algorithms with
gcm_base, so this shouldn't break anyone in practice.
Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-18 21:43:02 +00:00
|
|
|
if (strcmp(ghash->base.cra_name, "ghash") != 0 ||
|
|
|
|
ghash->digestsize != 16)
|
2020-01-03 03:58:57 +00:00
|
|
|
goto err_free_inst;
|
2015-06-16 05:54:18 +00:00
|
|
|
|
2020-01-03 03:58:45 +00:00
|
|
|
err = crypto_grab_skcipher(&ctx->ctr, aead_crypto_instance(inst),
|
|
|
|
ctr_name, 0, mask);
|
2007-12-07 11:26:11 +00:00
|
|
|
if (err)
|
2020-01-03 03:58:57 +00:00
|
|
|
goto err_free_inst;
|
2016-07-12 05:17:39 +00:00
|
|
|
ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
|
2007-11-26 14:24:11 +00:00
|
|
|
|
crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
GCM instances can be created by either the "gcm" template, which only
allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
which allows choosing the ctr and ghash implementations, e.g.
"gcm_base(ctr(aes-generic),ghash-generic)".
However, a "gcm_base" instance prevents a "gcm" instance from being
registered using the same implementations. Nor will the instance be
found by lookups of "gcm". This can be used as a denial of service.
Moreover, "gcm_base" instances are never tested by the crypto
self-tests, even if there are compatible "gcm" tests.
The root cause of these problems is that instances of the two templates
use different cra_names. Therefore, fix these problems by making
"gcm_base" instances set the same cra_name as "gcm" instances, e.g.
"gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
This requires extracting the block cipher name from the name of the ctr
algorithm. It also requires starting to verify that the algorithms are
really ctr and ghash, not something else entirely. But it would be
bizarre if anyone were actually using non-gcm-compatible algorithms with
gcm_base, so this shouldn't break anyone in practice.
Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-18 21:43:02 +00:00
|
|
|
/* The skcipher algorithm must be CTR mode, using 16-byte blocks. */
|
2016-10-17 15:10:06 +00:00
|
|
|
err = -EINVAL;
|
crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
GCM instances can be created by either the "gcm" template, which only
allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
which allows choosing the ctr and ghash implementations, e.g.
"gcm_base(ctr(aes-generic),ghash-generic)".
However, a "gcm_base" instance prevents a "gcm" instance from being
registered using the same implementations. Nor will the instance be
found by lookups of "gcm". This can be used as a denial of service.
Moreover, "gcm_base" instances are never tested by the crypto
self-tests, even if there are compatible "gcm" tests.
The root cause of these problems is that instances of the two templates
use different cra_names. Therefore, fix these problems by making
"gcm_base" instances set the same cra_name as "gcm" instances, e.g.
"gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
This requires extracting the block cipher name from the name of the ctr
algorithm. It also requires starting to verify that the algorithms are
really ctr and ghash, not something else entirely. But it would be
bizarre if anyone were actually using non-gcm-compatible algorithms with
gcm_base, so this shouldn't break anyone in practice.
Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-18 21:43:02 +00:00
|
|
|
if (strncmp(ctr->base.cra_name, "ctr(", 4) != 0 ||
|
|
|
|
crypto_skcipher_alg_ivsize(ctr) != 16 ||
|
|
|
|
ctr->base.cra_blocksize != 1)
|
2020-01-03 03:58:57 +00:00
|
|
|
goto err_free_inst;
|
2007-12-07 12:31:10 +00:00
|
|
|
|
crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
GCM instances can be created by either the "gcm" template, which only
allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
which allows choosing the ctr and ghash implementations, e.g.
"gcm_base(ctr(aes-generic),ghash-generic)".
However, a "gcm_base" instance prevents a "gcm" instance from being
registered using the same implementations. Nor will the instance be
found by lookups of "gcm". This can be used as a denial of service.
Moreover, "gcm_base" instances are never tested by the crypto
self-tests, even if there are compatible "gcm" tests.
The root cause of these problems is that instances of the two templates
use different cra_names. Therefore, fix these problems by making
"gcm_base" instances set the same cra_name as "gcm" instances, e.g.
"gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
This requires extracting the block cipher name from the name of the ctr
algorithm. It also requires starting to verify that the algorithms are
really ctr and ghash, not something else entirely. But it would be
bizarre if anyone were actually using non-gcm-compatible algorithms with
gcm_base, so this shouldn't break anyone in practice.
Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-18 21:43:02 +00:00
|
|
|
err = -ENAMETOOLONG;
|
|
|
|
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
|
|
|
|
"gcm(%s", ctr->base.cra_name + 4) >= CRYPTO_MAX_ALG_NAME)
|
2020-01-03 03:58:57 +00:00
|
|
|
goto err_free_inst;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
|
2016-07-12 05:17:39 +00:00
|
|
|
"gcm_base(%s,%s)", ctr->base.cra_driver_name,
|
2020-01-03 03:58:57 +00:00
|
|
|
ghash->base.cra_driver_name) >=
|
2007-12-07 12:31:10 +00:00
|
|
|
CRYPTO_MAX_ALG_NAME)
|
2020-01-03 03:58:57 +00:00
|
|
|
goto err_free_inst;
|
2007-11-26 14:24:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_priority = (ghash->base.cra_priority +
|
2016-07-12 05:17:39 +00:00
|
|
|
ctr->base.cra_priority) / 2;
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_blocksize = 1;
|
|
|
|
inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
|
2016-07-12 05:17:39 +00:00
|
|
|
ctr->base.cra_alignmask;
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
|
2017-08-22 08:08:17 +00:00
|
|
|
inst->alg.ivsize = GCM_AES_IV_SIZE;
|
2016-07-12 05:17:39 +00:00
|
|
|
inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.maxauthsize = 16;
|
|
|
|
inst->alg.init = crypto_gcm_init_tfm;
|
|
|
|
inst->alg.exit = crypto_gcm_exit_tfm;
|
|
|
|
inst->alg.setkey = crypto_gcm_setkey;
|
|
|
|
inst->alg.setauthsize = crypto_gcm_setauthsize;
|
|
|
|
inst->alg.encrypt = crypto_gcm_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_gcm_decrypt;
|
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
inst->free = crypto_gcm_free;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
err = aead_register_instance(tmpl, inst);
|
2020-01-03 03:58:57 +00:00
|
|
|
if (err) {
|
2007-11-26 14:24:11 +00:00
|
|
|
err_free_inst:
|
2020-01-03 03:58:57 +00:00
|
|
|
crypto_gcm_free(inst);
|
|
|
|
}
|
|
|
|
return err;
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_gcm_create(struct crypto_template *tmpl, struct rtattr **tb)
|
2007-12-07 12:31:10 +00:00
|
|
|
{
|
|
|
|
const char *cipher_name;
|
|
|
|
char ctr_name[CRYPTO_MAX_ALG_NAME];
|
|
|
|
|
|
|
|
cipher_name = crypto_attr_alg_name(tb[1]);
|
|
|
|
if (IS_ERR(cipher_name))
|
2015-06-16 05:54:18 +00:00
|
|
|
return PTR_ERR(cipher_name);
|
2007-12-07 12:31:10 +00:00
|
|
|
|
|
|
|
if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)", cipher_name) >=
|
|
|
|
CRYPTO_MAX_ALG_NAME)
|
2015-06-16 05:54:18 +00:00
|
|
|
return -ENAMETOOLONG;
|
2007-12-07 12:31:10 +00:00
|
|
|
|
crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
GCM instances can be created by either the "gcm" template, which only
allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
which allows choosing the ctr and ghash implementations, e.g.
"gcm_base(ctr(aes-generic),ghash-generic)".
However, a "gcm_base" instance prevents a "gcm" instance from being
registered using the same implementations. Nor will the instance be
found by lookups of "gcm". This can be used as a denial of service.
Moreover, "gcm_base" instances are never tested by the crypto
self-tests, even if there are compatible "gcm" tests.
The root cause of these problems is that instances of the two templates
use different cra_names. Therefore, fix these problems by making
"gcm_base" instances set the same cra_name as "gcm" instances, e.g.
"gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
This requires extracting the block cipher name from the name of the ctr
algorithm. It also requires starting to verify that the algorithms are
really ctr and ghash, not something else entirely. But it would be
bizarre if anyone were actually using non-gcm-compatible algorithms with
gcm_base, so this shouldn't break anyone in practice.
Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-18 21:43:02 +00:00
|
|
|
return crypto_gcm_create_common(tmpl, tb, ctr_name, "ghash");
|
2007-12-07 12:31:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_gcm_base_create(struct crypto_template *tmpl,
|
|
|
|
struct rtattr **tb)
|
2007-12-07 12:31:10 +00:00
|
|
|
{
|
|
|
|
const char *ctr_name;
|
2009-08-06 05:34:26 +00:00
|
|
|
const char *ghash_name;
|
2007-12-07 12:31:10 +00:00
|
|
|
|
|
|
|
ctr_name = crypto_attr_alg_name(tb[1]);
|
|
|
|
if (IS_ERR(ctr_name))
|
2015-06-16 05:54:18 +00:00
|
|
|
return PTR_ERR(ctr_name);
|
2007-12-07 12:31:10 +00:00
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
ghash_name = crypto_attr_alg_name(tb[2]);
|
|
|
|
if (IS_ERR(ghash_name))
|
2015-06-16 05:54:18 +00:00
|
|
|
return PTR_ERR(ghash_name);
|
2009-08-06 05:34:26 +00:00
|
|
|
|
crypto: gcm - fix incompatibility between "gcm" and "gcm_base"
GCM instances can be created by either the "gcm" template, which only
allows choosing the block cipher, e.g. "gcm(aes)"; or by "gcm_base",
which allows choosing the ctr and ghash implementations, e.g.
"gcm_base(ctr(aes-generic),ghash-generic)".
However, a "gcm_base" instance prevents a "gcm" instance from being
registered using the same implementations. Nor will the instance be
found by lookups of "gcm". This can be used as a denial of service.
Moreover, "gcm_base" instances are never tested by the crypto
self-tests, even if there are compatible "gcm" tests.
The root cause of these problems is that instances of the two templates
use different cra_names. Therefore, fix these problems by making
"gcm_base" instances set the same cra_name as "gcm" instances, e.g.
"gcm(aes)" instead of "gcm_base(ctr(aes-generic),ghash-generic)".
This requires extracting the block cipher name from the name of the ctr
algorithm. It also requires starting to verify that the algorithms are
really ctr and ghash, not something else entirely. But it would be
bizarre if anyone were actually using non-gcm-compatible algorithms with
gcm_base, so this shouldn't break anyone in practice.
Fixes: d00aa19b507b ("[CRYPTO] gcm: Allow block cipher parameter")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-04-18 21:43:02 +00:00
|
|
|
return crypto_gcm_create_common(tmpl, tb, ctr_name, ghash_name);
|
2007-12-07 12:31:10 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
static int crypto_rfc4106_setkey(struct crypto_aead *parent, const u8 *key,
|
|
|
|
unsigned int keylen)
|
|
|
|
{
|
|
|
|
struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
|
|
|
|
struct crypto_aead *child = ctx->child;
|
|
|
|
|
|
|
|
if (keylen < 4)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
keylen -= 4;
|
|
|
|
memcpy(ctx->nonce, key + keylen, 4);
|
|
|
|
|
|
|
|
crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
|
|
|
|
CRYPTO_TFM_REQ_MASK);
|
2019-12-31 03:19:38 +00:00
|
|
|
return crypto_aead_setkey(child, key, keylen);
|
2007-12-17 07:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_rfc4106_setauthsize(struct crypto_aead *parent,
|
|
|
|
unsigned int authsize)
|
|
|
|
{
|
|
|
|
struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent);
|
2019-07-31 13:05:54 +00:00
|
|
|
int err;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2019-07-31 13:05:54 +00:00
|
|
|
err = crypto_rfc4106_check_authsize(authsize);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
return crypto_aead_setauthsize(ctx->child, authsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req)
|
|
|
|
{
|
2015-07-08 23:17:30 +00:00
|
|
|
struct crypto_rfc4106_req_ctx *rctx = aead_request_ctx(req);
|
2007-12-17 07:33:17 +00:00
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
|
|
|
struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(aead);
|
2015-07-08 23:17:30 +00:00
|
|
|
struct aead_request *subreq = &rctx->subreq;
|
2007-12-17 07:33:17 +00:00
|
|
|
struct crypto_aead *child = ctx->child;
|
2015-07-08 23:17:30 +00:00
|
|
|
struct scatterlist *sg;
|
2007-12-17 07:33:17 +00:00
|
|
|
u8 *iv = PTR_ALIGN((u8 *)(subreq + 1) + crypto_aead_reqsize(child),
|
|
|
|
crypto_aead_alignmask(child) + 1);
|
|
|
|
|
2017-08-22 08:08:17 +00:00
|
|
|
scatterwalk_map_and_copy(iv + GCM_AES_IV_SIZE, req->src, 0, req->assoclen - 8, 0);
|
2015-07-08 23:17:30 +00:00
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
memcpy(iv, ctx->nonce, 4);
|
|
|
|
memcpy(iv + 4, req->iv, 8);
|
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
sg_init_table(rctx->src, 3);
|
2017-08-22 08:08:17 +00:00
|
|
|
sg_set_buf(rctx->src, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
|
2015-07-08 23:17:30 +00:00
|
|
|
sg = scatterwalk_ffwd(rctx->src + 1, req->src, req->assoclen);
|
|
|
|
if (sg != rctx->src + 1)
|
|
|
|
sg_chain(rctx->src, 2, sg);
|
|
|
|
|
|
|
|
if (req->src != req->dst) {
|
|
|
|
sg_init_table(rctx->dst, 3);
|
2017-08-22 08:08:17 +00:00
|
|
|
sg_set_buf(rctx->dst, iv + GCM_AES_IV_SIZE, req->assoclen - 8);
|
2015-07-08 23:17:30 +00:00
|
|
|
sg = scatterwalk_ffwd(rctx->dst + 1, req->dst, req->assoclen);
|
|
|
|
if (sg != rctx->dst + 1)
|
|
|
|
sg_chain(rctx->dst, 2, sg);
|
|
|
|
}
|
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
aead_request_set_tfm(subreq, child);
|
|
|
|
aead_request_set_callback(subreq, req->base.flags, req->base.complete,
|
|
|
|
req->base.data);
|
2015-07-08 23:17:30 +00:00
|
|
|
aead_request_set_crypt(subreq, rctx->src,
|
|
|
|
req->src == req->dst ? rctx->src : rctx->dst,
|
|
|
|
req->cryptlen, iv);
|
|
|
|
aead_request_set_ad(subreq, req->assoclen - 8);
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
return subreq;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_rfc4106_encrypt(struct aead_request *req)
|
|
|
|
{
|
2019-07-31 13:05:54 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = crypto_ipsec_check_assoclen(req->assoclen);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2015-07-08 23:17:30 +00:00
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
req = crypto_rfc4106_crypt(req);
|
|
|
|
|
|
|
|
return crypto_aead_encrypt(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_rfc4106_decrypt(struct aead_request *req)
|
|
|
|
{
|
2019-07-31 13:05:54 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = crypto_ipsec_check_assoclen(req->assoclen);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2015-07-08 23:17:30 +00:00
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
req = crypto_rfc4106_crypt(req);
|
|
|
|
|
|
|
|
return crypto_aead_decrypt(req);
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_rfc4106_init_tfm(struct crypto_aead *tfm)
|
2007-12-17 07:33:17 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_instance *inst = aead_alg_instance(tfm);
|
|
|
|
struct crypto_aead_spawn *spawn = aead_instance_ctx(inst);
|
|
|
|
struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
|
2007-12-17 07:33:17 +00:00
|
|
|
struct crypto_aead *aead;
|
|
|
|
unsigned long align;
|
|
|
|
|
|
|
|
aead = crypto_spawn_aead(spawn);
|
|
|
|
if (IS_ERR(aead))
|
|
|
|
return PTR_ERR(aead);
|
|
|
|
|
|
|
|
ctx->child = aead;
|
|
|
|
|
|
|
|
align = crypto_aead_alignmask(aead);
|
|
|
|
align &= ~(crypto_tfm_ctx_alignment() - 1);
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_aead_set_reqsize(
|
|
|
|
tfm,
|
2015-07-08 23:17:30 +00:00
|
|
|
sizeof(struct crypto_rfc4106_req_ctx) +
|
2015-05-11 09:47:59 +00:00
|
|
|
ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
|
2015-07-08 23:17:30 +00:00
|
|
|
align + 24);
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static void crypto_rfc4106_exit_tfm(struct crypto_aead *tfm)
|
2007-12-17 07:33:17 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(tfm);
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
crypto_free_aead(ctx->child);
|
|
|
|
}
|
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
static void crypto_rfc4106_free(struct aead_instance *inst)
|
|
|
|
{
|
|
|
|
crypto_drop_aead(aead_instance_ctx(inst));
|
|
|
|
kfree(inst);
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_rfc4106_create(struct crypto_template *tmpl,
|
|
|
|
struct rtattr **tb)
|
2007-12-17 07:33:17 +00:00
|
|
|
{
|
2020-01-03 03:58:46 +00:00
|
|
|
u32 mask;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_instance *inst;
|
2007-12-17 07:33:17 +00:00
|
|
|
struct crypto_aead_spawn *spawn;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_alg *alg;
|
2007-12-17 07:33:17 +00:00
|
|
|
int err;
|
|
|
|
|
2020-07-10 06:20:38 +00:00
|
|
|
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2020-01-03 03:58:46 +00:00
|
|
|
|
2007-12-17 07:33:17 +00:00
|
|
|
inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
|
|
|
|
if (!inst)
|
2015-06-16 05:54:18 +00:00
|
|
|
return -ENOMEM;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
spawn = aead_instance_ctx(inst);
|
2020-01-03 03:58:46 +00:00
|
|
|
err = crypto_grab_aead(spawn, aead_crypto_instance(inst),
|
2020-02-26 04:59:18 +00:00
|
|
|
crypto_attr_alg_name(tb[1]), 0, mask);
|
2007-12-17 07:33:17 +00:00
|
|
|
if (err)
|
2020-02-26 04:59:18 +00:00
|
|
|
goto err_free_inst;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
alg = crypto_spawn_aead_alg(spawn);
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
/* Underlying IV size must be 12. */
|
2017-08-22 08:08:17 +00:00
|
|
|
if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
|
2020-02-26 04:59:18 +00:00
|
|
|
goto err_free_inst;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
/* Not a stream cipher? */
|
2015-06-16 05:54:18 +00:00
|
|
|
if (alg->base.cra_blocksize != 1)
|
2020-02-26 04:59:18 +00:00
|
|
|
goto err_free_inst;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
|
|
|
err = -ENAMETOOLONG;
|
2015-06-16 05:54:18 +00:00
|
|
|
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
|
|
|
|
"rfc4106(%s)", alg->base.cra_name) >=
|
|
|
|
CRYPTO_MAX_ALG_NAME ||
|
|
|
|
snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
|
|
|
|
"rfc4106(%s)", alg->base.cra_driver_name) >=
|
2007-12-17 07:33:17 +00:00
|
|
|
CRYPTO_MAX_ALG_NAME)
|
2020-02-26 04:59:18 +00:00
|
|
|
goto err_free_inst;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_priority = alg->base.cra_priority;
|
|
|
|
inst->alg.base.cra_blocksize = 1;
|
|
|
|
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2017-08-22 08:08:17 +00:00
|
|
|
inst->alg.ivsize = GCM_RFC4106_IV_SIZE;
|
2016-07-12 05:17:39 +00:00
|
|
|
inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.init = crypto_rfc4106_init_tfm;
|
|
|
|
inst->alg.exit = crypto_rfc4106_exit_tfm;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.setkey = crypto_rfc4106_setkey;
|
|
|
|
inst->alg.setauthsize = crypto_rfc4106_setauthsize;
|
|
|
|
inst->alg.encrypt = crypto_rfc4106_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_rfc4106_decrypt;
|
2007-12-17 07:33:17 +00:00
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
inst->free = crypto_rfc4106_free;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
err = aead_register_instance(tmpl, inst);
|
2020-02-26 04:59:18 +00:00
|
|
|
if (err) {
|
|
|
|
err_free_inst:
|
|
|
|
crypto_rfc4106_free(inst);
|
|
|
|
}
|
2015-06-16 05:54:18 +00:00
|
|
|
return err;
|
2007-12-17 07:33:17 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 10:52:11 +00:00
|
|
|
static int crypto_rfc4543_setkey(struct crypto_aead *parent, const u8 *key,
|
|
|
|
unsigned int keylen)
|
|
|
|
{
|
|
|
|
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
|
|
|
|
struct crypto_aead *child = ctx->child;
|
|
|
|
|
|
|
|
if (keylen < 4)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
keylen -= 4;
|
|
|
|
memcpy(ctx->nonce, key + keylen, 4);
|
|
|
|
|
|
|
|
crypto_aead_clear_flags(child, CRYPTO_TFM_REQ_MASK);
|
|
|
|
crypto_aead_set_flags(child, crypto_aead_get_flags(parent) &
|
|
|
|
CRYPTO_TFM_REQ_MASK);
|
2019-12-31 03:19:38 +00:00
|
|
|
return crypto_aead_setkey(child, key, keylen);
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_rfc4543_setauthsize(struct crypto_aead *parent,
|
|
|
|
unsigned int authsize)
|
|
|
|
{
|
|
|
|
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(parent);
|
|
|
|
|
|
|
|
if (authsize != 16)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return crypto_aead_setauthsize(ctx->child, authsize);
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_rfc4543_crypt(struct aead_request *req, bool enc)
|
2010-01-17 10:52:11 +00:00
|
|
|
{
|
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
|
|
|
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_rfc4543_req_ctx *rctx = aead_request_ctx(req);
|
2010-01-17 10:52:11 +00:00
|
|
|
struct aead_request *subreq = &rctx->subreq;
|
|
|
|
unsigned int authsize = crypto_aead_authsize(aead);
|
|
|
|
u8 *iv = PTR_ALIGN((u8 *)(rctx + 1) + crypto_aead_reqsize(ctx->child),
|
|
|
|
crypto_aead_alignmask(ctx->child) + 1);
|
2015-06-16 05:54:18 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (req->src != req->dst) {
|
|
|
|
err = crypto_rfc4543_copy_src_to_dst(req, enc);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
memcpy(iv, ctx->nonce, 4);
|
|
|
|
memcpy(iv + 4, req->iv, 8);
|
|
|
|
|
|
|
|
aead_request_set_tfm(subreq, ctx->child);
|
2015-06-16 05:54:18 +00:00
|
|
|
aead_request_set_callback(subreq, req->base.flags,
|
|
|
|
req->base.complete, req->base.data);
|
|
|
|
aead_request_set_crypt(subreq, req->src, req->dst,
|
|
|
|
enc ? 0 : authsize, iv);
|
|
|
|
aead_request_set_ad(subreq, req->assoclen + req->cryptlen -
|
|
|
|
subreq->cryptlen);
|
|
|
|
|
|
|
|
return enc ? crypto_aead_encrypt(subreq) : crypto_aead_decrypt(subreq);
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-07 13:43:41 +00:00
|
|
|
static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
|
|
|
|
{
|
|
|
|
struct crypto_aead *aead = crypto_aead_reqtfm(req);
|
|
|
|
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(aead);
|
|
|
|
unsigned int authsize = crypto_aead_authsize(aead);
|
2015-06-16 05:54:18 +00:00
|
|
|
unsigned int nbytes = req->assoclen + req->cryptlen -
|
|
|
|
(enc ? 0 : authsize);
|
2018-09-19 02:10:51 +00:00
|
|
|
SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
|
2016-07-12 05:17:39 +00:00
|
|
|
|
2018-09-19 02:10:51 +00:00
|
|
|
skcipher_request_set_sync_tfm(nreq, ctx->null);
|
2016-07-12 05:17:39 +00:00
|
|
|
skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
|
|
|
|
skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
|
2013-04-07 13:43:41 +00:00
|
|
|
|
2016-07-12 05:17:39 +00:00
|
|
|
return crypto_skcipher_encrypt(nreq);
|
2013-04-07 13:43:41 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 10:52:11 +00:00
|
|
|
static int crypto_rfc4543_encrypt(struct aead_request *req)
|
|
|
|
{
|
2019-08-02 08:47:33 +00:00
|
|
|
return crypto_ipsec_check_assoclen(req->assoclen) ?:
|
|
|
|
crypto_rfc4543_crypt(req, true);
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_rfc4543_decrypt(struct aead_request *req)
|
|
|
|
{
|
2019-08-02 08:47:33 +00:00
|
|
|
return crypto_ipsec_check_assoclen(req->assoclen) ?:
|
|
|
|
crypto_rfc4543_crypt(req, false);
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
|
2010-01-17 10:52:11 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_instance *inst = aead_alg_instance(tfm);
|
|
|
|
struct crypto_rfc4543_instance_ctx *ictx = aead_instance_ctx(inst);
|
2013-04-07 13:43:41 +00:00
|
|
|
struct crypto_aead_spawn *spawn = &ictx->aead;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
|
2010-01-17 10:52:11 +00:00
|
|
|
struct crypto_aead *aead;
|
2018-09-19 02:10:51 +00:00
|
|
|
struct crypto_sync_skcipher *null;
|
2010-01-17 10:52:11 +00:00
|
|
|
unsigned long align;
|
2013-04-07 13:43:41 +00:00
|
|
|
int err = 0;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
aead = crypto_spawn_aead(spawn);
|
|
|
|
if (IS_ERR(aead))
|
|
|
|
return PTR_ERR(aead);
|
|
|
|
|
2017-12-07 18:56:34 +00:00
|
|
|
null = crypto_get_default_null_skcipher();
|
2013-04-07 13:43:41 +00:00
|
|
|
err = PTR_ERR(null);
|
|
|
|
if (IS_ERR(null))
|
|
|
|
goto err_free_aead;
|
|
|
|
|
2010-01-17 10:52:11 +00:00
|
|
|
ctx->child = aead;
|
2013-04-07 13:43:41 +00:00
|
|
|
ctx->null = null;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
align = crypto_aead_alignmask(aead);
|
|
|
|
align &= ~(crypto_tfm_ctx_alignment() - 1);
|
2015-06-16 05:54:18 +00:00
|
|
|
crypto_aead_set_reqsize(
|
|
|
|
tfm,
|
2015-05-11 09:47:59 +00:00
|
|
|
sizeof(struct crypto_rfc4543_req_ctx) +
|
|
|
|
ALIGN(crypto_aead_reqsize(aead), crypto_tfm_ctx_alignment()) +
|
2017-08-22 08:08:17 +00:00
|
|
|
align + GCM_AES_IV_SIZE);
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-04-07 13:43:41 +00:00
|
|
|
|
|
|
|
err_free_aead:
|
|
|
|
crypto_free_aead(aead);
|
|
|
|
return err;
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
|
2010-01-17 10:52:11 +00:00
|
|
|
{
|
2015-06-16 05:54:18 +00:00
|
|
|
struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
crypto_free_aead(ctx->child);
|
2017-12-07 18:56:34 +00:00
|
|
|
crypto_put_default_null_skcipher();
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 23:17:30 +00:00
|
|
|
static void crypto_rfc4543_free(struct aead_instance *inst)
|
|
|
|
{
|
|
|
|
struct crypto_rfc4543_instance_ctx *ctx = aead_instance_ctx(inst);
|
|
|
|
|
|
|
|
crypto_drop_aead(&ctx->aead);
|
|
|
|
|
|
|
|
kfree(inst);
|
|
|
|
}
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
static int crypto_rfc4543_create(struct crypto_template *tmpl,
|
|
|
|
struct rtattr **tb)
|
2010-01-17 10:52:11 +00:00
|
|
|
{
|
2020-01-03 03:58:46 +00:00
|
|
|
u32 mask;
|
2015-06-16 05:54:18 +00:00
|
|
|
struct aead_instance *inst;
|
|
|
|
struct aead_alg *alg;
|
2013-04-07 13:43:41 +00:00
|
|
|
struct crypto_rfc4543_instance_ctx *ctx;
|
2010-01-17 10:52:11 +00:00
|
|
|
int err;
|
|
|
|
|
2020-07-10 06:20:38 +00:00
|
|
|
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2020-01-03 03:58:46 +00:00
|
|
|
|
2013-04-07 13:43:41 +00:00
|
|
|
inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
|
2010-01-17 10:52:11 +00:00
|
|
|
if (!inst)
|
2015-06-16 05:54:18 +00:00
|
|
|
return -ENOMEM;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
ctx = aead_instance_ctx(inst);
|
2020-02-26 04:59:19 +00:00
|
|
|
err = crypto_grab_aead(&ctx->aead, aead_crypto_instance(inst),
|
|
|
|
crypto_attr_alg_name(tb[1]), 0, mask);
|
2010-01-17 10:52:11 +00:00
|
|
|
if (err)
|
2020-02-26 04:59:19 +00:00
|
|
|
goto err_free_inst;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2020-02-26 04:59:19 +00:00
|
|
|
alg = crypto_spawn_aead_alg(&ctx->aead);
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
/* Underlying IV size must be 12. */
|
2017-08-22 08:08:17 +00:00
|
|
|
if (crypto_aead_alg_ivsize(alg) != GCM_AES_IV_SIZE)
|
2020-02-26 04:59:19 +00:00
|
|
|
goto err_free_inst;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
/* Not a stream cipher? */
|
2015-06-16 05:54:18 +00:00
|
|
|
if (alg->base.cra_blocksize != 1)
|
2020-02-26 04:59:19 +00:00
|
|
|
goto err_free_inst;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
|
|
|
err = -ENAMETOOLONG;
|
2015-06-16 05:54:18 +00:00
|
|
|
if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
|
|
|
|
"rfc4543(%s)", alg->base.cra_name) >=
|
|
|
|
CRYPTO_MAX_ALG_NAME ||
|
|
|
|
snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
|
|
|
|
"rfc4543(%s)", alg->base.cra_driver_name) >=
|
2010-01-17 10:52:11 +00:00
|
|
|
CRYPTO_MAX_ALG_NAME)
|
2020-02-26 04:59:19 +00:00
|
|
|
goto err_free_inst;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_priority = alg->base.cra_priority;
|
|
|
|
inst->alg.base.cra_blocksize = 1;
|
|
|
|
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2017-08-22 08:08:17 +00:00
|
|
|
inst->alg.ivsize = GCM_RFC4543_IV_SIZE;
|
2016-07-12 05:17:39 +00:00
|
|
|
inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.init = crypto_rfc4543_init_tfm;
|
|
|
|
inst->alg.exit = crypto_rfc4543_exit_tfm;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
inst->alg.setkey = crypto_rfc4543_setkey;
|
|
|
|
inst->alg.setauthsize = crypto_rfc4543_setauthsize;
|
|
|
|
inst->alg.encrypt = crypto_rfc4543_encrypt;
|
|
|
|
inst->alg.decrypt = crypto_rfc4543_decrypt;
|
2010-01-17 10:52:11 +00:00
|
|
|
|
2020-02-26 04:59:19 +00:00
|
|
|
inst->free = crypto_rfc4543_free;
|
2015-07-08 23:17:30 +00:00
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
err = aead_register_instance(tmpl, inst);
|
2020-02-26 04:59:19 +00:00
|
|
|
if (err) {
|
|
|
|
err_free_inst:
|
|
|
|
crypto_rfc4543_free(inst);
|
|
|
|
}
|
2015-06-16 05:54:18 +00:00
|
|
|
return err;
|
2010-01-17 10:52:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 05:58:13 +00:00
|
|
|
static struct crypto_template crypto_gcm_tmpls[] = {
|
|
|
|
{
|
|
|
|
.name = "gcm_base",
|
|
|
|
.create = crypto_gcm_base_create,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
}, {
|
|
|
|
.name = "gcm",
|
|
|
|
.create = crypto_gcm_create,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
}, {
|
|
|
|
.name = "rfc4106",
|
|
|
|
.create = crypto_rfc4106_create,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
}, {
|
|
|
|
.name = "rfc4543",
|
|
|
|
.create = crypto_rfc4543_create,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
},
|
2010-01-17 10:52:11 +00:00
|
|
|
};
|
|
|
|
|
2007-11-26 14:24:11 +00:00
|
|
|
static int __init crypto_gcm_module_init(void)
|
|
|
|
{
|
2007-12-07 12:31:10 +00:00
|
|
|
int err;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
|
2009-08-06 05:34:26 +00:00
|
|
|
if (!gcm_zeroes)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-06-16 05:54:18 +00:00
|
|
|
sg_init_one(&gcm_zeroes->sg, gcm_zeroes->buf, sizeof(gcm_zeroes->buf));
|
|
|
|
|
2019-01-18 05:58:13 +00:00
|
|
|
err = crypto_register_templates(crypto_gcm_tmpls,
|
|
|
|
ARRAY_SIZE(crypto_gcm_tmpls));
|
2007-12-07 12:31:10 +00:00
|
|
|
if (err)
|
2019-01-18 05:58:13 +00:00
|
|
|
kfree(gcm_zeroes);
|
2007-12-07 12:31:10 +00:00
|
|
|
|
2009-08-06 05:34:26 +00:00
|
|
|
return err;
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit crypto_gcm_module_exit(void)
|
|
|
|
{
|
2009-08-06 05:34:26 +00:00
|
|
|
kfree(gcm_zeroes);
|
2019-01-18 05:58:13 +00:00
|
|
|
crypto_unregister_templates(crypto_gcm_tmpls,
|
|
|
|
ARRAY_SIZE(crypto_gcm_tmpls));
|
2007-11-26 14:24:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 04:57:42 +00:00
|
|
|
subsys_initcall(crypto_gcm_module_init);
|
2007-11-26 14:24:11 +00:00
|
|
|
module_exit(crypto_gcm_module_exit);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("Galois/Counter Mode");
|
|
|
|
MODULE_AUTHOR("Mikko Herranen <mh1@iki.fi>");
|
2014-11-21 01:05:53 +00:00
|
|
|
MODULE_ALIAS_CRYPTO("gcm_base");
|
|
|
|
MODULE_ALIAS_CRYPTO("rfc4106");
|
|
|
|
MODULE_ALIAS_CRYPTO("rfc4543");
|
2014-11-25 00:32:38 +00:00
|
|
|
MODULE_ALIAS_CRYPTO("gcm");
|