• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
3  *
4  * Copyright (C) 2013 Advanced Micro Devices, Inc.
5  *
6  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7  *
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.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/scatterlist.h>
17 #include <linux/crypto.h>
18 #include <crypto/algapi.h>
19 #include <crypto/aes.h>
20 #include <crypto/scatterwalk.h>
21 
22 #include "ccp-crypto.h"
23 
24 
25 struct ccp_aes_xts_def {
26 	const char *name;
27 	const char *drv_name;
28 };
29 
30 static struct ccp_aes_xts_def aes_xts_algs[] = {
31 	{
32 		.name		= "xts(aes)",
33 		.drv_name	= "xts-aes-ccp",
34 	},
35 };
36 
37 struct ccp_unit_size_map {
38 	unsigned int size;
39 	u32 value;
40 };
41 
42 static struct ccp_unit_size_map unit_size_map[] = {
43 	{
44 		.size	= 4096,
45 		.value	= CCP_XTS_AES_UNIT_SIZE_4096,
46 	},
47 	{
48 		.size	= 2048,
49 		.value	= CCP_XTS_AES_UNIT_SIZE_2048,
50 	},
51 	{
52 		.size	= 1024,
53 		.value	= CCP_XTS_AES_UNIT_SIZE_1024,
54 	},
55 	{
56 		.size	= 512,
57 		.value	= CCP_XTS_AES_UNIT_SIZE_512,
58 	},
59 	{
60 		.size	= 256,
61 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
62 	},
63 	{
64 		.size	= 128,
65 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
66 	},
67 	{
68 		.size	= 64,
69 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
70 	},
71 	{
72 		.size	= 32,
73 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
74 	},
75 	{
76 		.size	= 16,
77 		.value	= CCP_XTS_AES_UNIT_SIZE_16,
78 	},
79 	{
80 		.size	= 1,
81 		.value	= CCP_XTS_AES_UNIT_SIZE__LAST,
82 	},
83 };
84 
ccp_aes_xts_complete(struct crypto_async_request * async_req,int ret)85 static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
86 {
87 	struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
88 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
89 
90 	if (ret)
91 		return ret;
92 
93 	memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
94 
95 	return 0;
96 }
97 
ccp_aes_xts_setkey(struct crypto_ablkcipher * tfm,const u8 * key,unsigned int key_len)98 static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
99 			      unsigned int key_len)
100 {
101 	struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
102 
103 	/* Only support 128-bit AES key with a 128-bit Tweak key,
104 	 * otherwise use the fallback
105 	 */
106 	switch (key_len) {
107 	case AES_KEYSIZE_128 * 2:
108 		memcpy(ctx->u.aes.key, key, key_len);
109 		break;
110 	}
111 	ctx->u.aes.key_len = key_len / 2;
112 	sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
113 
114 	return crypto_ablkcipher_setkey(ctx->u.aes.tfm_ablkcipher, key,
115 					key_len);
116 }
117 
ccp_aes_xts_crypt(struct ablkcipher_request * req,unsigned int encrypt)118 static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
119 			     unsigned int encrypt)
120 {
121 	struct crypto_tfm *tfm =
122 		crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
123 	struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
124 	struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
125 	unsigned int unit;
126 	u32 unit_size;
127 	int ret;
128 
129 	if (!ctx->u.aes.key_len)
130 		return -EINVAL;
131 
132 	if (req->nbytes & (AES_BLOCK_SIZE - 1))
133 		return -EINVAL;
134 
135 	if (!req->info)
136 		return -EINVAL;
137 
138 	unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
139 	if (req->nbytes <= unit_size_map[0].size) {
140 		for (unit = 0; unit < ARRAY_SIZE(unit_size_map); unit++) {
141 			if (!(req->nbytes & (unit_size_map[unit].size - 1))) {
142 				unit_size = unit_size_map[unit].value;
143 				break;
144 			}
145 		}
146 	}
147 
148 	if ((unit_size == CCP_XTS_AES_UNIT_SIZE__LAST) ||
149 	    (ctx->u.aes.key_len != AES_KEYSIZE_128)) {
150 		/* Use the fallback to process the request for any
151 		 * unsupported unit sizes or key sizes
152 		 */
153 		ablkcipher_request_set_tfm(req, ctx->u.aes.tfm_ablkcipher);
154 		ret = (encrypt) ? crypto_ablkcipher_encrypt(req) :
155 				  crypto_ablkcipher_decrypt(req);
156 		ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
157 
158 		return ret;
159 	}
160 
161 	memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
162 	sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
163 
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;
177 
178 	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
179 
180 	return ret;
181 }
182 
ccp_aes_xts_encrypt(struct ablkcipher_request * req)183 static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
184 {
185 	return ccp_aes_xts_crypt(req, 1);
186 }
187 
ccp_aes_xts_decrypt(struct ablkcipher_request * req)188 static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
189 {
190 	return ccp_aes_xts_crypt(req, 0);
191 }
192 
ccp_aes_xts_cra_init(struct crypto_tfm * tfm)193 static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
194 {
195 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
196 	struct crypto_ablkcipher *fallback_tfm;
197 
198 	ctx->complete = ccp_aes_xts_complete;
199 	ctx->u.aes.key_len = 0;
200 
201 	fallback_tfm = crypto_alloc_ablkcipher(crypto_tfm_alg_name(tfm), 0,
202 					       CRYPTO_ALG_ASYNC |
203 					       CRYPTO_ALG_NEED_FALLBACK);
204 	if (IS_ERR(fallback_tfm)) {
205 		pr_warn("could not load fallback driver %s\n",
206 			crypto_tfm_alg_name(tfm));
207 		return PTR_ERR(fallback_tfm);
208 	}
209 	ctx->u.aes.tfm_ablkcipher = fallback_tfm;
210 
211 	tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx) +
212 				      fallback_tfm->base.crt_ablkcipher.reqsize;
213 
214 	return 0;
215 }
216 
ccp_aes_xts_cra_exit(struct crypto_tfm * tfm)217 static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
218 {
219 	struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
220 
221 	if (ctx->u.aes.tfm_ablkcipher)
222 		crypto_free_ablkcipher(ctx->u.aes.tfm_ablkcipher);
223 	ctx->u.aes.tfm_ablkcipher = NULL;
224 }
225 
226 
ccp_register_aes_xts_alg(struct list_head * head,const struct ccp_aes_xts_def * def)227 static int ccp_register_aes_xts_alg(struct list_head *head,
228 				    const struct ccp_aes_xts_def *def)
229 {
230 	struct ccp_crypto_ablkcipher_alg *ccp_alg;
231 	struct crypto_alg *alg;
232 	int ret;
233 
234 	ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
235 	if (!ccp_alg)
236 		return -ENOMEM;
237 
238 	INIT_LIST_HEAD(&ccp_alg->entry);
239 
240 	alg = &ccp_alg->alg;
241 
242 	snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
243 	snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
244 		 def->drv_name);
245 	alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
246 			 CRYPTO_ALG_KERN_DRIVER_ONLY |
247 			 CRYPTO_ALG_NEED_FALLBACK;
248 	alg->cra_blocksize = AES_BLOCK_SIZE;
249 	alg->cra_ctxsize = sizeof(struct ccp_ctx);
250 	alg->cra_priority = CCP_CRA_PRIORITY;
251 	alg->cra_type = &crypto_ablkcipher_type;
252 	alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
253 	alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
254 	alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
255 	alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
256 	alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
257 	alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
258 	alg->cra_init = ccp_aes_xts_cra_init;
259 	alg->cra_exit = ccp_aes_xts_cra_exit;
260 	alg->cra_module = THIS_MODULE;
261 
262 	ret = crypto_register_alg(alg);
263 	if (ret) {
264 		pr_err("%s ablkcipher algorithm registration error (%d)\n",
265 			alg->cra_name, ret);
266 		kfree(ccp_alg);
267 		return ret;
268 	}
269 
270 	list_add(&ccp_alg->entry, head);
271 
272 	return 0;
273 }
274 
ccp_register_aes_xts_algs(struct list_head * head)275 int ccp_register_aes_xts_algs(struct list_head *head)
276 {
277 	int i, ret;
278 
279 	for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
280 		ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
281 		if (ret)
282 			return ret;
283 	}
284 
285 	return 0;
286 }
287