11d6b8a6fSTom Lendacky /* 21d6b8a6fSTom Lendacky * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support 31d6b8a6fSTom Lendacky * 4e652399eSGary R Hook * Copyright (C) 2013,2017 Advanced Micro Devices, Inc. 51d6b8a6fSTom Lendacky * 6e652399eSGary R Hook * Author: Gary R Hook <gary.hook@amd.com> 71d6b8a6fSTom Lendacky * Author: Tom Lendacky <thomas.lendacky@amd.com> 81d6b8a6fSTom Lendacky * 91d6b8a6fSTom Lendacky * This program is free software; you can redistribute it and/or modify 101d6b8a6fSTom Lendacky * it under the terms of the GNU General Public License version 2 as 111d6b8a6fSTom Lendacky * published by the Free Software Foundation. 121d6b8a6fSTom Lendacky */ 131d6b8a6fSTom Lendacky 141d6b8a6fSTom Lendacky #include <linux/module.h> 151d6b8a6fSTom Lendacky #include <linux/sched.h> 161d6b8a6fSTom Lendacky #include <linux/delay.h> 171d6b8a6fSTom Lendacky #include <linux/scatterlist.h> 181d6b8a6fSTom Lendacky #include <crypto/aes.h> 1947f27f16SGary R Hook #include <crypto/xts.h> 20241118deSHerbert Xu #include <crypto/internal/skcipher.h> 211d6b8a6fSTom Lendacky #include <crypto/scatterwalk.h> 221d6b8a6fSTom Lendacky 231d6b8a6fSTom Lendacky #include "ccp-crypto.h" 241d6b8a6fSTom Lendacky 251d6b8a6fSTom Lendacky struct ccp_aes_xts_def { 261d6b8a6fSTom Lendacky const char *name; 271d6b8a6fSTom Lendacky const char *drv_name; 281d6b8a6fSTom Lendacky }; 291d6b8a6fSTom Lendacky 301d6b8a6fSTom Lendacky static struct ccp_aes_xts_def aes_xts_algs[] = { 311d6b8a6fSTom Lendacky { 321d6b8a6fSTom Lendacky .name = "xts(aes)", 331d6b8a6fSTom Lendacky .drv_name = "xts-aes-ccp", 341d6b8a6fSTom Lendacky }, 351d6b8a6fSTom Lendacky }; 361d6b8a6fSTom Lendacky 371d6b8a6fSTom Lendacky struct ccp_unit_size_map { 381d6b8a6fSTom Lendacky unsigned int size; 391d6b8a6fSTom Lendacky u32 value; 401d6b8a6fSTom Lendacky }; 411d6b8a6fSTom Lendacky 427f7216cfSGary R Hook static struct ccp_unit_size_map xts_unit_sizes[] = { 431d6b8a6fSTom Lendacky { 447f7216cfSGary R Hook .size = 16, 457f7216cfSGary R Hook .value = CCP_XTS_AES_UNIT_SIZE_16, 461d6b8a6fSTom Lendacky }, 471d6b8a6fSTom Lendacky { 481d6b8a6fSTom Lendacky .size = 512, 491d6b8a6fSTom Lendacky .value = CCP_XTS_AES_UNIT_SIZE_512, 501d6b8a6fSTom Lendacky }, 511d6b8a6fSTom Lendacky { 527f7216cfSGary R Hook .size = 1024, 537f7216cfSGary R Hook .value = CCP_XTS_AES_UNIT_SIZE_1024, 541d6b8a6fSTom Lendacky }, 551d6b8a6fSTom Lendacky { 567f7216cfSGary R Hook .size = 2048, 577f7216cfSGary R Hook .value = CCP_XTS_AES_UNIT_SIZE_2048, 581d6b8a6fSTom Lendacky }, 591d6b8a6fSTom Lendacky { 607f7216cfSGary R Hook .size = 4096, 617f7216cfSGary R Hook .value = CCP_XTS_AES_UNIT_SIZE_4096, 621d6b8a6fSTom Lendacky }, 631d6b8a6fSTom Lendacky }; 641d6b8a6fSTom Lendacky 651d6b8a6fSTom Lendacky static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret) 661d6b8a6fSTom Lendacky { 671d6b8a6fSTom Lendacky struct ablkcipher_request *req = ablkcipher_request_cast(async_req); 681d6b8a6fSTom Lendacky struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req); 691d6b8a6fSTom Lendacky 701d6b8a6fSTom Lendacky if (ret) 711d6b8a6fSTom Lendacky return ret; 721d6b8a6fSTom Lendacky 731d6b8a6fSTom Lendacky memcpy(req->info, rctx->iv, AES_BLOCK_SIZE); 741d6b8a6fSTom Lendacky 751d6b8a6fSTom Lendacky return 0; 761d6b8a6fSTom Lendacky } 771d6b8a6fSTom Lendacky 781d6b8a6fSTom Lendacky static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key, 791d6b8a6fSTom Lendacky unsigned int key_len) 801d6b8a6fSTom Lendacky { 8147f27f16SGary R Hook struct crypto_tfm *xfm = crypto_ablkcipher_tfm(tfm); 8247f27f16SGary R Hook struct ccp_ctx *ctx = crypto_tfm_ctx(xfm); 8347f27f16SGary R Hook int ret; 8447f27f16SGary R Hook 8547f27f16SGary R Hook ret = xts_check_key(xfm, key, key_len); 8647f27f16SGary R Hook if (ret) 8747f27f16SGary R Hook return ret; 881d6b8a6fSTom Lendacky 891d6b8a6fSTom Lendacky /* Only support 128-bit AES key with a 128-bit Tweak key, 901d6b8a6fSTom Lendacky * otherwise use the fallback 911d6b8a6fSTom Lendacky */ 921d6b8a6fSTom Lendacky switch (key_len) { 931d6b8a6fSTom Lendacky case AES_KEYSIZE_128 * 2: 941d6b8a6fSTom Lendacky memcpy(ctx->u.aes.key, key, key_len); 951d6b8a6fSTom Lendacky break; 961d6b8a6fSTom Lendacky } 971d6b8a6fSTom Lendacky ctx->u.aes.key_len = key_len / 2; 981d6b8a6fSTom Lendacky sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len); 991d6b8a6fSTom Lendacky 100241118deSHerbert Xu return crypto_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len); 1011d6b8a6fSTom Lendacky } 1021d6b8a6fSTom Lendacky 1031d6b8a6fSTom Lendacky static int ccp_aes_xts_crypt(struct ablkcipher_request *req, 1041d6b8a6fSTom Lendacky unsigned int encrypt) 1051d6b8a6fSTom Lendacky { 1061d6b8a6fSTom Lendacky struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm); 1071d6b8a6fSTom Lendacky struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req); 1081d6b8a6fSTom Lendacky unsigned int unit; 109ab6a11a7STom Lendacky u32 unit_size; 1101d6b8a6fSTom Lendacky int ret; 1111d6b8a6fSTom Lendacky 112369f3dabSTom Lendacky if (!ctx->u.aes.key_len) 1131d6b8a6fSTom Lendacky return -EINVAL; 1141d6b8a6fSTom Lendacky 115369f3dabSTom Lendacky if (req->nbytes & (AES_BLOCK_SIZE - 1)) 1161d6b8a6fSTom Lendacky return -EINVAL; 1171d6b8a6fSTom Lendacky 118369f3dabSTom Lendacky if (!req->info) 1191d6b8a6fSTom Lendacky return -EINVAL; 1201d6b8a6fSTom Lendacky 1217f7216cfSGary R Hook /* Check conditions under which the CCP can fulfill a request. The 1227f7216cfSGary R Hook * device can handle input plaintext of a length that is a multiple 1237f7216cfSGary R Hook * of the unit_size, bug the crypto implementation only supports 1247f7216cfSGary R Hook * the unit_size being equal to the input length. This limits the 1257f7216cfSGary R Hook * number of scenarios we can handle. 1267f7216cfSGary R Hook */ 127ab6a11a7STom Lendacky unit_size = CCP_XTS_AES_UNIT_SIZE__LAST; 1287f7216cfSGary R Hook for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) { 1297f7216cfSGary R Hook if (req->nbytes == xts_unit_sizes[unit].size) { 1307f7216cfSGary R Hook unit_size = unit; 1311d6b8a6fSTom Lendacky break; 132ab6a11a7STom Lendacky } 133ab6a11a7STom Lendacky } 134ab6a11a7STom Lendacky if ((unit_size == CCP_XTS_AES_UNIT_SIZE__LAST) || 1351d6b8a6fSTom Lendacky (ctx->u.aes.key_len != AES_KEYSIZE_128)) { 136241118deSHerbert Xu SKCIPHER_REQUEST_ON_STACK(subreq, ctx->u.aes.tfm_skcipher); 137241118deSHerbert Xu 1381d6b8a6fSTom Lendacky /* Use the fallback to process the request for any 1391d6b8a6fSTom Lendacky * unsupported unit sizes or key sizes 1401d6b8a6fSTom Lendacky */ 141241118deSHerbert Xu skcipher_request_set_tfm(subreq, ctx->u.aes.tfm_skcipher); 142241118deSHerbert Xu skcipher_request_set_callback(subreq, req->base.flags, 143241118deSHerbert Xu NULL, NULL); 144241118deSHerbert Xu skcipher_request_set_crypt(subreq, req->src, req->dst, 145241118deSHerbert Xu req->nbytes, req->info); 146241118deSHerbert Xu ret = encrypt ? crypto_skcipher_encrypt(subreq) : 147241118deSHerbert Xu crypto_skcipher_decrypt(subreq); 148241118deSHerbert Xu skcipher_request_zero(subreq); 1491d6b8a6fSTom Lendacky return ret; 1501d6b8a6fSTom Lendacky } 1511d6b8a6fSTom Lendacky 1521d6b8a6fSTom Lendacky memcpy(rctx->iv, req->info, AES_BLOCK_SIZE); 1531d6b8a6fSTom Lendacky sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE); 1541d6b8a6fSTom Lendacky 1551d6b8a6fSTom Lendacky memset(&rctx->cmd, 0, sizeof(rctx->cmd)); 1561d6b8a6fSTom Lendacky INIT_LIST_HEAD(&rctx->cmd.entry); 1571d6b8a6fSTom Lendacky rctx->cmd.engine = CCP_ENGINE_XTS_AES_128; 158e652399eSGary R Hook rctx->cmd.u.xts.type = CCP_AES_TYPE_128; 1591d6b8a6fSTom Lendacky rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT 1601d6b8a6fSTom Lendacky : CCP_AES_ACTION_DECRYPT; 161ab6a11a7STom Lendacky rctx->cmd.u.xts.unit_size = unit_size; 1621d6b8a6fSTom Lendacky rctx->cmd.u.xts.key = &ctx->u.aes.key_sg; 1631d6b8a6fSTom Lendacky rctx->cmd.u.xts.key_len = ctx->u.aes.key_len; 1641d6b8a6fSTom Lendacky rctx->cmd.u.xts.iv = &rctx->iv_sg; 1651d6b8a6fSTom Lendacky rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE; 1661d6b8a6fSTom Lendacky rctx->cmd.u.xts.src = req->src; 1671d6b8a6fSTom Lendacky rctx->cmd.u.xts.src_len = req->nbytes; 1681d6b8a6fSTom Lendacky rctx->cmd.u.xts.dst = req->dst; 1691d6b8a6fSTom Lendacky 1701d6b8a6fSTom Lendacky ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); 1711d6b8a6fSTom Lendacky 1721d6b8a6fSTom Lendacky return ret; 1731d6b8a6fSTom Lendacky } 1741d6b8a6fSTom Lendacky 1751d6b8a6fSTom Lendacky static int ccp_aes_xts_encrypt(struct ablkcipher_request *req) 1761d6b8a6fSTom Lendacky { 1771d6b8a6fSTom Lendacky return ccp_aes_xts_crypt(req, 1); 1781d6b8a6fSTom Lendacky } 1791d6b8a6fSTom Lendacky 1801d6b8a6fSTom Lendacky static int ccp_aes_xts_decrypt(struct ablkcipher_request *req) 1811d6b8a6fSTom Lendacky { 1821d6b8a6fSTom Lendacky return ccp_aes_xts_crypt(req, 0); 1831d6b8a6fSTom Lendacky } 1841d6b8a6fSTom Lendacky 1851d6b8a6fSTom Lendacky static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm) 1861d6b8a6fSTom Lendacky { 1871d6b8a6fSTom Lendacky struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); 188241118deSHerbert Xu struct crypto_skcipher *fallback_tfm; 1891d6b8a6fSTom Lendacky 1901d6b8a6fSTom Lendacky ctx->complete = ccp_aes_xts_complete; 1911d6b8a6fSTom Lendacky ctx->u.aes.key_len = 0; 1921d6b8a6fSTom Lendacky 193241118deSHerbert Xu fallback_tfm = crypto_alloc_skcipher("xts(aes)", 0, 1941d6b8a6fSTom Lendacky CRYPTO_ALG_ASYNC | 1951d6b8a6fSTom Lendacky CRYPTO_ALG_NEED_FALLBACK); 1961d6b8a6fSTom Lendacky if (IS_ERR(fallback_tfm)) { 197241118deSHerbert Xu pr_warn("could not load fallback driver xts(aes)\n"); 1981d6b8a6fSTom Lendacky return PTR_ERR(fallback_tfm); 1991d6b8a6fSTom Lendacky } 200241118deSHerbert Xu ctx->u.aes.tfm_skcipher = fallback_tfm; 2011d6b8a6fSTom Lendacky 202241118deSHerbert Xu tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx); 2031d6b8a6fSTom Lendacky 2041d6b8a6fSTom Lendacky return 0; 2051d6b8a6fSTom Lendacky } 2061d6b8a6fSTom Lendacky 2071d6b8a6fSTom Lendacky static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm) 2081d6b8a6fSTom Lendacky { 2091d6b8a6fSTom Lendacky struct ccp_ctx *ctx = crypto_tfm_ctx(tfm); 2101d6b8a6fSTom Lendacky 211241118deSHerbert Xu crypto_free_skcipher(ctx->u.aes.tfm_skcipher); 2121d6b8a6fSTom Lendacky } 2131d6b8a6fSTom Lendacky 2141d6b8a6fSTom Lendacky static int ccp_register_aes_xts_alg(struct list_head *head, 2151d6b8a6fSTom Lendacky const struct ccp_aes_xts_def *def) 2161d6b8a6fSTom Lendacky { 2171d6b8a6fSTom Lendacky struct ccp_crypto_ablkcipher_alg *ccp_alg; 2181d6b8a6fSTom Lendacky struct crypto_alg *alg; 2191d6b8a6fSTom Lendacky int ret; 2201d6b8a6fSTom Lendacky 2211d6b8a6fSTom Lendacky ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); 2221d6b8a6fSTom Lendacky if (!ccp_alg) 2231d6b8a6fSTom Lendacky return -ENOMEM; 2241d6b8a6fSTom Lendacky 2251d6b8a6fSTom Lendacky INIT_LIST_HEAD(&ccp_alg->entry); 2261d6b8a6fSTom Lendacky 2271d6b8a6fSTom Lendacky alg = &ccp_alg->alg; 2281d6b8a6fSTom Lendacky 2291d6b8a6fSTom Lendacky snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); 2301d6b8a6fSTom Lendacky snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 2311d6b8a6fSTom Lendacky def->drv_name); 2321d6b8a6fSTom Lendacky alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC | 2331d6b8a6fSTom Lendacky CRYPTO_ALG_KERN_DRIVER_ONLY | 2341d6b8a6fSTom Lendacky CRYPTO_ALG_NEED_FALLBACK; 2351d6b8a6fSTom Lendacky alg->cra_blocksize = AES_BLOCK_SIZE; 2361d6b8a6fSTom Lendacky alg->cra_ctxsize = sizeof(struct ccp_ctx); 2371d6b8a6fSTom Lendacky alg->cra_priority = CCP_CRA_PRIORITY; 2381d6b8a6fSTom Lendacky alg->cra_type = &crypto_ablkcipher_type; 2391d6b8a6fSTom Lendacky alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey; 2401d6b8a6fSTom Lendacky alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt; 2411d6b8a6fSTom Lendacky alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt; 2421d6b8a6fSTom Lendacky alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2; 2431d6b8a6fSTom Lendacky alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2; 2441d6b8a6fSTom Lendacky alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE; 2451d6b8a6fSTom Lendacky alg->cra_init = ccp_aes_xts_cra_init; 2461d6b8a6fSTom Lendacky alg->cra_exit = ccp_aes_xts_cra_exit; 2471d6b8a6fSTom Lendacky alg->cra_module = THIS_MODULE; 2481d6b8a6fSTom Lendacky 2491d6b8a6fSTom Lendacky ret = crypto_register_alg(alg); 2501d6b8a6fSTom Lendacky if (ret) { 2511d6b8a6fSTom Lendacky pr_err("%s ablkcipher algorithm registration error (%d)\n", 2521d6b8a6fSTom Lendacky alg->cra_name, ret); 2531d6b8a6fSTom Lendacky kfree(ccp_alg); 2541d6b8a6fSTom Lendacky return ret; 2551d6b8a6fSTom Lendacky } 2561d6b8a6fSTom Lendacky 2571d6b8a6fSTom Lendacky list_add(&ccp_alg->entry, head); 2581d6b8a6fSTom Lendacky 2591d6b8a6fSTom Lendacky return 0; 2601d6b8a6fSTom Lendacky } 2611d6b8a6fSTom Lendacky 2621d6b8a6fSTom Lendacky int ccp_register_aes_xts_algs(struct list_head *head) 2631d6b8a6fSTom Lendacky { 2641d6b8a6fSTom Lendacky int i, ret; 2651d6b8a6fSTom Lendacky 2661d6b8a6fSTom Lendacky for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) { 2671d6b8a6fSTom Lendacky ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]); 2681d6b8a6fSTom Lendacky if (ret) 2691d6b8a6fSTom Lendacky return ret; 2701d6b8a6fSTom Lendacky } 2711d6b8a6fSTom Lendacky 2721d6b8a6fSTom Lendacky return 0; 2731d6b8a6fSTom Lendacky } 274