2 * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
4 * Copyright (C) 2013 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/scatterlist.h>
17 #include <crypto/aes.h>
18 #include <crypto/internal/skcipher.h>
19 #include <crypto/scatterwalk.h>
21 #include "ccp-crypto.h"
23 struct ccp_aes_xts_def {
28 static struct ccp_aes_xts_def aes_xts_algs[] = {
31 .drv_name = "xts-aes-ccp",
35 struct ccp_unit_size_map {
40 static struct ccp_unit_size_map unit_size_map[] = {
43 .value = CCP_XTS_AES_UNIT_SIZE_4096,
47 .value = CCP_XTS_AES_UNIT_SIZE_2048,
51 .value = CCP_XTS_AES_UNIT_SIZE_1024,
55 .value = CCP_XTS_AES_UNIT_SIZE_512,
59 .value = CCP_XTS_AES_UNIT_SIZE__LAST,
63 .value = CCP_XTS_AES_UNIT_SIZE__LAST,
67 .value = CCP_XTS_AES_UNIT_SIZE__LAST,
71 .value = CCP_XTS_AES_UNIT_SIZE__LAST,
75 .value = CCP_XTS_AES_UNIT_SIZE_16,
79 .value = CCP_XTS_AES_UNIT_SIZE__LAST,
83 static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
85 struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
86 struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
91 memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
96 static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
99 struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
101 /* Only support 128-bit AES key with a 128-bit Tweak key,
102 * otherwise use the fallback
105 case AES_KEYSIZE_128 * 2:
106 memcpy(ctx->u.aes.key, key, key_len);
109 ctx->u.aes.key_len = key_len / 2;
110 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
112 return crypto_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
115 static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
116 unsigned int encrypt)
118 struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
119 struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
124 if (!ctx->u.aes.key_len)
127 if (req->nbytes & (AES_BLOCK_SIZE - 1))
133 unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
134 if (req->nbytes <= unit_size_map[0].size) {
135 for (unit = 0; unit < ARRAY_SIZE(unit_size_map); unit++) {
136 if (!(req->nbytes & (unit_size_map[unit].size - 1))) {
137 unit_size = unit_size_map[unit].value;
143 if ((unit_size == CCP_XTS_AES_UNIT_SIZE__LAST) ||
144 (ctx->u.aes.key_len != AES_KEYSIZE_128)) {
145 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->u.aes.tfm_skcipher);
147 /* Use the fallback to process the request for any
148 * unsupported unit sizes or key sizes
150 skcipher_request_set_tfm(subreq, ctx->u.aes.tfm_skcipher);
151 skcipher_request_set_callback(subreq, req->base.flags,
153 skcipher_request_set_crypt(subreq, req->src, req->dst,
154 req->nbytes, req->info);
155 ret = encrypt ? crypto_skcipher_encrypt(subreq) :
156 crypto_skcipher_decrypt(subreq);
157 skcipher_request_zero(subreq);
161 memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
162 sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
164 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
165 INIT_LIST_HEAD(&rctx->cmd.entry);
166 rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
167 rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
168 : CCP_AES_ACTION_DECRYPT;
169 rctx->cmd.u.xts.unit_size = unit_size;
170 rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
171 rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
172 rctx->cmd.u.xts.iv = &rctx->iv_sg;
173 rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
174 rctx->cmd.u.xts.src = req->src;
175 rctx->cmd.u.xts.src_len = req->nbytes;
176 rctx->cmd.u.xts.dst = req->dst;
178 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
183 static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
185 return ccp_aes_xts_crypt(req, 1);
188 static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
190 return ccp_aes_xts_crypt(req, 0);
193 static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
195 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
196 struct crypto_skcipher *fallback_tfm;
198 ctx->complete = ccp_aes_xts_complete;
199 ctx->u.aes.key_len = 0;
201 fallback_tfm = crypto_alloc_skcipher("xts(aes)", 0,
203 CRYPTO_ALG_NEED_FALLBACK);
204 if (IS_ERR(fallback_tfm)) {
205 pr_warn("could not load fallback driver xts(aes)\n");
206 return PTR_ERR(fallback_tfm);
208 ctx->u.aes.tfm_skcipher = fallback_tfm;
210 tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
215 static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
217 struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
219 crypto_free_skcipher(ctx->u.aes.tfm_skcipher);
222 static int ccp_register_aes_xts_alg(struct list_head *head,
223 const struct ccp_aes_xts_def *def)
225 struct ccp_crypto_ablkcipher_alg *ccp_alg;
226 struct crypto_alg *alg;
229 ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
233 INIT_LIST_HEAD(&ccp_alg->entry);
237 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
238 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
240 alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
241 CRYPTO_ALG_KERN_DRIVER_ONLY |
242 CRYPTO_ALG_NEED_FALLBACK;
243 alg->cra_blocksize = AES_BLOCK_SIZE;
244 alg->cra_ctxsize = sizeof(struct ccp_ctx);
245 alg->cra_priority = CCP_CRA_PRIORITY;
246 alg->cra_type = &crypto_ablkcipher_type;
247 alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
248 alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
249 alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
250 alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
251 alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
252 alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
253 alg->cra_init = ccp_aes_xts_cra_init;
254 alg->cra_exit = ccp_aes_xts_cra_exit;
255 alg->cra_module = THIS_MODULE;
257 ret = crypto_register_alg(alg);
259 pr_err("%s ablkcipher algorithm registration error (%d)\n",
265 list_add(&ccp_alg->entry, head);
270 int ccp_register_aes_xts_algs(struct list_head *head)
274 for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
275 ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);