2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-11-13 01:46:40 +08:00
|
|
|
/*
|
|
|
|
* AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
|
|
|
|
*
|
2017-07-26 03:12:11 +08:00
|
|
|
* Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
|
2013-11-13 01:46:40 +08:00
|
|
|
*
|
2017-07-26 03:12:11 +08:00
|
|
|
* Author: Gary R Hook <gary.hook@amd.com>
|
2013-11-13 01:46:40 +08:00
|
|
|
* Author: Tom Lendacky <thomas.lendacky@amd.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <crypto/aes.h>
|
2017-07-26 03:21:23 +08:00
|
|
|
#include <crypto/xts.h>
|
2016-06-29 18:04:01 +08:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2013-11-13 01:46:40 +08:00
|
|
|
#include <crypto/scatterwalk.h>
|
|
|
|
|
|
|
|
#include "ccp-crypto.h"
|
|
|
|
|
|
|
|
struct ccp_aes_xts_def {
|
|
|
|
const char *name;
|
|
|
|
const char *drv_name;
|
|
|
|
};
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static const struct ccp_aes_xts_def aes_xts_algs[] = {
|
2013-11-13 01:46:40 +08:00
|
|
|
{
|
|
|
|
.name = "xts(aes)",
|
|
|
|
.drv_name = "xts-aes-ccp",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ccp_unit_size_map {
|
|
|
|
unsigned int size;
|
|
|
|
u32 value;
|
|
|
|
};
|
|
|
|
|
2017-07-26 03:21:34 +08:00
|
|
|
static struct ccp_unit_size_map xts_unit_sizes[] = {
|
2013-11-13 01:46:40 +08:00
|
|
|
{
|
2017-07-26 03:21:34 +08:00
|
|
|
.size = 16,
|
|
|
|
.value = CCP_XTS_AES_UNIT_SIZE_16,
|
2013-11-13 01:46:40 +08:00
|
|
|
},
|
|
|
|
{
|
2017-07-26 03:21:34 +08:00
|
|
|
.size = 512,
|
2013-11-13 01:46:40 +08:00
|
|
|
.value = CCP_XTS_AES_UNIT_SIZE_512,
|
|
|
|
},
|
|
|
|
{
|
2017-07-26 03:21:34 +08:00
|
|
|
.size = 1024,
|
|
|
|
.value = CCP_XTS_AES_UNIT_SIZE_1024,
|
2013-11-13 01:46:40 +08:00
|
|
|
},
|
|
|
|
{
|
2017-07-26 03:21:34 +08:00
|
|
|
.size = 2048,
|
|
|
|
.value = CCP_XTS_AES_UNIT_SIZE_2048,
|
2013-11-13 01:46:40 +08:00
|
|
|
},
|
|
|
|
{
|
2017-07-26 03:21:34 +08:00
|
|
|
.size = 4096,
|
|
|
|
.value = CCP_XTS_AES_UNIT_SIZE_4096,
|
2013-11-13 01:46:40 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
|
|
|
|
{
|
2019-11-10 01:09:29 +08:00
|
|
|
struct skcipher_request *req = skcipher_request_cast(async_req);
|
|
|
|
struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
|
2013-11-13 01:46:40 +08:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
memcpy(req->iv, rctx->iv, AES_BLOCK_SIZE);
|
2013-11-13 01:46:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static int ccp_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2013-11-13 01:46:40 +08:00
|
|
|
unsigned int key_len)
|
|
|
|
{
|
2019-11-10 01:09:29 +08:00
|
|
|
struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2017-07-26 03:21:43 +08:00
|
|
|
unsigned int ccpversion = ccp_version();
|
2017-07-26 03:21:23 +08:00
|
|
|
int ret;
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
ret = xts_verify_key(tfm, key, key_len);
|
2017-07-26 03:21:23 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2013-11-13 01:46:40 +08:00
|
|
|
|
2017-07-26 03:21:43 +08:00
|
|
|
/* Version 3 devices support 128-bit keys; version 5 devices can
|
|
|
|
* accommodate 128- and 256-bit keys.
|
2013-11-13 01:46:40 +08:00
|
|
|
*/
|
|
|
|
switch (key_len) {
|
|
|
|
case AES_KEYSIZE_128 * 2:
|
|
|
|
memcpy(ctx->u.aes.key, key, key_len);
|
|
|
|
break;
|
2017-07-26 03:21:43 +08:00
|
|
|
case AES_KEYSIZE_256 * 2:
|
|
|
|
if (ccpversion > CCP_VERSION(3, 0))
|
|
|
|
memcpy(ctx->u.aes.key, key, key_len);
|
|
|
|
break;
|
2013-11-13 01:46:40 +08:00
|
|
|
}
|
|
|
|
ctx->u.aes.key_len = key_len / 2;
|
|
|
|
sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
|
|
|
|
|
2020-07-07 14:31:57 +08:00
|
|
|
return crypto_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
|
2013-11-13 01:46:40 +08:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static int ccp_aes_xts_crypt(struct skcipher_request *req,
|
2013-11-13 01:46:40 +08:00
|
|
|
unsigned int encrypt)
|
|
|
|
{
|
2019-11-10 01:09:29 +08:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct ccp_aes_req_ctx *rctx = skcipher_request_ctx(req);
|
2017-07-26 03:21:43 +08:00
|
|
|
unsigned int ccpversion = ccp_version();
|
|
|
|
unsigned int fallback = 0;
|
2013-11-13 01:46:40 +08:00
|
|
|
unsigned int unit;
|
2016-05-21 06:33:03 +08:00
|
|
|
u32 unit_size;
|
2013-11-13 01:46:40 +08:00
|
|
|
int ret;
|
|
|
|
|
2013-12-11 00:38:44 +08:00
|
|
|
if (!ctx->u.aes.key_len)
|
2013-11-13 01:46:40 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
if (!req->iv)
|
2013-11-13 01:46:40 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-07-26 03:21:34 +08:00
|
|
|
/* Check conditions under which the CCP can fulfill a request. The
|
|
|
|
* device can handle input plaintext of a length that is a multiple
|
|
|
|
* of the unit_size, bug the crypto implementation only supports
|
|
|
|
* the unit_size being equal to the input length. This limits the
|
|
|
|
* number of scenarios we can handle.
|
|
|
|
*/
|
2016-05-21 06:33:03 +08:00
|
|
|
unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
|
2017-07-26 03:21:34 +08:00
|
|
|
for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) {
|
2019-11-10 01:09:29 +08:00
|
|
|
if (req->cryptlen == xts_unit_sizes[unit].size) {
|
2017-07-26 03:21:34 +08:00
|
|
|
unit_size = unit;
|
|
|
|
break;
|
2016-05-21 06:33:03 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 03:21:43 +08:00
|
|
|
/* The CCP has restrictions on block sizes. Also, a version 3 device
|
|
|
|
* only supports AES-128 operations; version 5 CCPs support both
|
|
|
|
* AES-128 and -256 operations.
|
|
|
|
*/
|
|
|
|
if (unit_size == CCP_XTS_AES_UNIT_SIZE__LAST)
|
|
|
|
fallback = 1;
|
|
|
|
if ((ccpversion < CCP_VERSION(5, 0)) &&
|
|
|
|
(ctx->u.aes.key_len != AES_KEYSIZE_128))
|
|
|
|
fallback = 1;
|
|
|
|
if ((ctx->u.aes.key_len != AES_KEYSIZE_128) &&
|
|
|
|
(ctx->u.aes.key_len != AES_KEYSIZE_256))
|
|
|
|
fallback = 1;
|
|
|
|
if (fallback) {
|
2013-11-13 01:46:40 +08:00
|
|
|
/* Use the fallback to process the request for any
|
|
|
|
* unsupported unit sizes or key sizes
|
|
|
|
*/
|
2020-07-07 14:31:57 +08:00
|
|
|
skcipher_request_set_tfm(&rctx->fallback_req,
|
|
|
|
ctx->u.aes.tfm_skcipher);
|
|
|
|
skcipher_request_set_callback(&rctx->fallback_req,
|
|
|
|
req->base.flags,
|
|
|
|
req->base.complete,
|
|
|
|
req->base.data);
|
|
|
|
skcipher_request_set_crypt(&rctx->fallback_req, req->src,
|
|
|
|
req->dst, req->cryptlen, req->iv);
|
|
|
|
ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
|
|
|
|
crypto_skcipher_decrypt(&rctx->fallback_req);
|
2013-11-13 01:46:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
memcpy(rctx->iv, req->iv, AES_BLOCK_SIZE);
|
2013-11-13 01:46:40 +08:00
|
|
|
sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
|
|
|
|
|
|
|
|
memset(&rctx->cmd, 0, sizeof(rctx->cmd));
|
|
|
|
INIT_LIST_HEAD(&rctx->cmd.entry);
|
|
|
|
rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
|
2017-07-26 03:12:11 +08:00
|
|
|
rctx->cmd.u.xts.type = CCP_AES_TYPE_128;
|
2013-11-13 01:46:40 +08:00
|
|
|
rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
|
|
|
|
: CCP_AES_ACTION_DECRYPT;
|
2016-05-21 06:33:03 +08:00
|
|
|
rctx->cmd.u.xts.unit_size = unit_size;
|
2013-11-13 01:46:40 +08:00
|
|
|
rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
|
|
|
|
rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
|
|
|
|
rctx->cmd.u.xts.iv = &rctx->iv_sg;
|
|
|
|
rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
|
|
|
|
rctx->cmd.u.xts.src = req->src;
|
2019-11-10 01:09:29 +08:00
|
|
|
rctx->cmd.u.xts.src_len = req->cryptlen;
|
2013-11-13 01:46:40 +08:00
|
|
|
rctx->cmd.u.xts.dst = req->dst;
|
|
|
|
|
|
|
|
ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static int ccp_aes_xts_encrypt(struct skcipher_request *req)
|
2013-11-13 01:46:40 +08:00
|
|
|
{
|
|
|
|
return ccp_aes_xts_crypt(req, 1);
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static int ccp_aes_xts_decrypt(struct skcipher_request *req)
|
2013-11-13 01:46:40 +08:00
|
|
|
{
|
|
|
|
return ccp_aes_xts_crypt(req, 0);
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static int ccp_aes_xts_init_tfm(struct crypto_skcipher *tfm)
|
2013-11-13 01:46:40 +08:00
|
|
|
{
|
2019-11-10 01:09:29 +08:00
|
|
|
struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2020-07-07 14:31:57 +08:00
|
|
|
struct crypto_skcipher *fallback_tfm;
|
2013-11-13 01:46:40 +08:00
|
|
|
|
|
|
|
ctx->complete = ccp_aes_xts_complete;
|
|
|
|
ctx->u.aes.key_len = 0;
|
|
|
|
|
2020-07-07 14:31:57 +08:00
|
|
|
fallback_tfm = crypto_alloc_skcipher("xts(aes)", 0,
|
2016-06-29 18:04:01 +08:00
|
|
|
CRYPTO_ALG_NEED_FALLBACK);
|
2013-11-13 01:46:40 +08:00
|
|
|
if (IS_ERR(fallback_tfm)) {
|
2016-06-29 18:04:01 +08:00
|
|
|
pr_warn("could not load fallback driver xts(aes)\n");
|
2013-11-13 01:46:40 +08:00
|
|
|
return PTR_ERR(fallback_tfm);
|
|
|
|
}
|
2016-06-29 18:04:01 +08:00
|
|
|
ctx->u.aes.tfm_skcipher = fallback_tfm;
|
2013-11-13 01:46:40 +08:00
|
|
|
|
2020-07-07 14:31:57 +08:00
|
|
|
crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx) +
|
|
|
|
crypto_skcipher_reqsize(fallback_tfm));
|
2013-11-13 01:46:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
static void ccp_aes_xts_exit_tfm(struct crypto_skcipher *tfm)
|
2013-11-13 01:46:40 +08:00
|
|
|
{
|
2019-11-10 01:09:29 +08:00
|
|
|
struct ccp_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2013-11-13 01:46:40 +08:00
|
|
|
|
2020-07-07 14:31:57 +08:00
|
|
|
crypto_free_skcipher(ctx->u.aes.tfm_skcipher);
|
2013-11-13 01:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ccp_register_aes_xts_alg(struct list_head *head,
|
|
|
|
const struct ccp_aes_xts_def *def)
|
|
|
|
{
|
2019-11-10 01:09:29 +08:00
|
|
|
struct ccp_crypto_skcipher_alg *ccp_alg;
|
|
|
|
struct skcipher_alg *alg;
|
2013-11-13 01:46:40 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
|
|
|
|
if (!ccp_alg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&ccp_alg->entry);
|
|
|
|
|
|
|
|
alg = &ccp_alg->alg;
|
|
|
|
|
2019-11-10 01:09:29 +08:00
|
|
|
snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
|
|
|
|
snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
|
2013-11-13 01:46:40 +08:00
|
|
|
def->drv_name);
|
2019-11-10 01:09:29 +08:00
|
|
|
alg->base.cra_flags = CRYPTO_ALG_ASYNC |
|
2020-07-10 14:20:41 +08:00
|
|
|
CRYPTO_ALG_ALLOCATES_MEMORY |
|
2019-11-10 01:09:29 +08:00
|
|
|
CRYPTO_ALG_KERN_DRIVER_ONLY |
|
|
|
|
CRYPTO_ALG_NEED_FALLBACK;
|
|
|
|
alg->base.cra_blocksize = AES_BLOCK_SIZE;
|
|
|
|
alg->base.cra_ctxsize = sizeof(struct ccp_ctx);
|
|
|
|
alg->base.cra_priority = CCP_CRA_PRIORITY;
|
|
|
|
alg->base.cra_module = THIS_MODULE;
|
|
|
|
|
|
|
|
alg->setkey = ccp_aes_xts_setkey;
|
|
|
|
alg->encrypt = ccp_aes_xts_encrypt;
|
|
|
|
alg->decrypt = ccp_aes_xts_decrypt;
|
|
|
|
alg->min_keysize = AES_MIN_KEY_SIZE * 2;
|
|
|
|
alg->max_keysize = AES_MAX_KEY_SIZE * 2;
|
|
|
|
alg->ivsize = AES_BLOCK_SIZE;
|
|
|
|
alg->init = ccp_aes_xts_init_tfm;
|
|
|
|
alg->exit = ccp_aes_xts_exit_tfm;
|
|
|
|
|
|
|
|
ret = crypto_register_skcipher(alg);
|
2013-11-13 01:46:40 +08:00
|
|
|
if (ret) {
|
2019-11-10 01:09:29 +08:00
|
|
|
pr_err("%s skcipher algorithm registration error (%d)\n",
|
|
|
|
alg->base.cra_name, ret);
|
2013-11-13 01:46:40 +08:00
|
|
|
kfree(ccp_alg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&ccp_alg->entry, head);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ccp_register_aes_xts_algs(struct list_head *head)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
|
|
|
|
ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|