2 * AMD Cryptographic Coprocessor (CCP) AES GCM crypto API support
4 * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
6 * Author: Gary R Hook <gary.hook@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 <linux/crypto.h>
18 #include <crypto/internal/aead.h>
19 #include <crypto/algapi.h>
20 #include <crypto/aes.h>
21 #include <crypto/ctr.h>
22 #include <crypto/scatterwalk.h>
23 #include <linux/delay.h>
25 #include "ccp-crypto.h"
27 #define AES_GCM_IVSIZE 12
29 static int ccp_aes_gcm_complete(struct crypto_async_request *async_req, int ret)
34 static int ccp_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
37 struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
41 ctx->u.aes.type = CCP_AES_TYPE_128;
44 ctx->u.aes.type = CCP_AES_TYPE_192;
47 ctx->u.aes.type = CCP_AES_TYPE_256;
50 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
54 ctx->u.aes.mode = CCP_AES_MODE_GCM;
55 ctx->u.aes.key_len = key_len;
57 memcpy(ctx->u.aes.key, key, key_len);
58 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
63 static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
64 unsigned int authsize)
82 static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
84 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
85 struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
86 struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
87 struct scatterlist *iv_sg = NULL;
88 unsigned int iv_len = 0;
92 if (!ctx->u.aes.key_len)
95 if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
103 * plaintext/ciphertext input
107 * Destination+tag buffer
110 /* Prepare the IV: 12 bytes + an integer (counter) */
111 memcpy(rctx->iv, req->iv, AES_GCM_IVSIZE);
112 for (i = 0; i < 3; i++)
113 rctx->iv[i + AES_GCM_IVSIZE] = 0;
114 rctx->iv[AES_BLOCK_SIZE - 1] = 1;
116 /* Set up a scatterlist for the IV */
117 iv_sg = &rctx->iv_sg;
118 iv_len = AES_BLOCK_SIZE;
119 sg_init_one(iv_sg, rctx->iv, iv_len);
121 /* The AAD + plaintext are concatenated in the src buffer */
122 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
123 INIT_LIST_HEAD(&rctx->cmd.entry);
124 rctx->cmd.engine = CCP_ENGINE_AES;
125 rctx->cmd.u.aes.authsize = crypto_aead_authsize(tfm);
126 rctx->cmd.u.aes.type = ctx->u.aes.type;
127 rctx->cmd.u.aes.mode = ctx->u.aes.mode;
128 rctx->cmd.u.aes.action = encrypt;
129 rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
130 rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
131 rctx->cmd.u.aes.iv = iv_sg;
132 rctx->cmd.u.aes.iv_len = iv_len;
133 rctx->cmd.u.aes.src = req->src;
134 rctx->cmd.u.aes.src_len = req->cryptlen;
135 rctx->cmd.u.aes.aad_len = req->assoclen;
137 /* The cipher text + the tag are in the dst buffer */
138 rctx->cmd.u.aes.dst = req->dst;
140 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
145 static int ccp_aes_gcm_encrypt(struct aead_request *req)
147 return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
150 static int ccp_aes_gcm_decrypt(struct aead_request *req)
152 return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
155 static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
157 struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
159 ctx->complete = ccp_aes_gcm_complete;
160 ctx->u.aes.key_len = 0;
162 crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
167 static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
171 static struct aead_alg ccp_aes_gcm_defaults = {
172 .setkey = ccp_aes_gcm_setkey,
173 .setauthsize = ccp_aes_gcm_setauthsize,
174 .encrypt = ccp_aes_gcm_encrypt,
175 .decrypt = ccp_aes_gcm_decrypt,
176 .init = ccp_aes_gcm_cra_init,
177 .ivsize = AES_GCM_IVSIZE,
178 .maxauthsize = AES_BLOCK_SIZE,
180 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
182 CRYPTO_ALG_KERN_DRIVER_ONLY |
183 CRYPTO_ALG_NEED_FALLBACK,
184 .cra_blocksize = AES_BLOCK_SIZE,
185 .cra_ctxsize = sizeof(struct ccp_ctx),
186 .cra_priority = CCP_CRA_PRIORITY,
187 .cra_type = &crypto_ablkcipher_type,
188 .cra_exit = ccp_aes_gcm_cra_exit,
189 .cra_module = THIS_MODULE,
193 struct ccp_aes_aead_def {
194 enum ccp_aes_mode mode;
195 unsigned int version;
197 const char *driver_name;
198 unsigned int blocksize;
200 struct aead_alg *alg_defaults;
203 static struct ccp_aes_aead_def aes_aead_algs[] = {
205 .mode = CCP_AES_MODE_GHASH,
206 .version = CCP_VERSION(5, 0),
208 .driver_name = "gcm-aes-ccp",
210 .ivsize = AES_BLOCK_SIZE,
211 .alg_defaults = &ccp_aes_gcm_defaults,
215 static int ccp_register_aes_aead(struct list_head *head,
216 const struct ccp_aes_aead_def *def)
218 struct ccp_crypto_aead *ccp_aead;
219 struct aead_alg *alg;
222 ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
226 INIT_LIST_HEAD(&ccp_aead->entry);
228 ccp_aead->mode = def->mode;
230 /* Copy the defaults and override as necessary */
231 alg = &ccp_aead->alg;
232 *alg = *def->alg_defaults;
233 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
234 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
236 alg->base.cra_blocksize = def->blocksize;
237 alg->base.cra_ablkcipher.ivsize = def->ivsize;
239 ret = crypto_register_aead(alg);
241 pr_err("%s ablkcipher algorithm registration error (%d)\n",
242 alg->base.cra_name, ret);
247 list_add(&ccp_aead->entry, head);
252 int ccp_register_aes_aeads(struct list_head *head)
255 unsigned int ccpversion = ccp_version();
257 for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
258 if (aes_aead_algs[i].version > ccpversion)
260 ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);