]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/crypto/ccp/ccp-crypto-aes-galois.c
ff02b713c6f6605c874def99e052e50ec30f344a
[mirror_ubuntu-bionic-kernel.git] / drivers / crypto / ccp / ccp-crypto-aes-galois.c
1 /*
2 * AMD Cryptographic Coprocessor (CCP) AES GCM crypto API support
3 *
4 * Copyright (C) 2016,2017 Advanced Micro Devices, Inc.
5 *
6 * Author: Gary R Hook <gary.hook@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/internal/aead.h>
19 #include <crypto/algapi.h>
20 #include <crypto/aes.h>
21 #include <crypto/ctr.h>
22 #include <crypto/gcm.h>
23 #include <crypto/scatterwalk.h>
24 #include <linux/delay.h>
25
26 #include "ccp-crypto.h"
27
28 static int ccp_aes_gcm_complete(struct crypto_async_request *async_req, int ret)
29 {
30 return ret;
31 }
32
33 static int ccp_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
34 unsigned int key_len)
35 {
36 struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
37
38 switch (key_len) {
39 case AES_KEYSIZE_128:
40 ctx->u.aes.type = CCP_AES_TYPE_128;
41 break;
42 case AES_KEYSIZE_192:
43 ctx->u.aes.type = CCP_AES_TYPE_192;
44 break;
45 case AES_KEYSIZE_256:
46 ctx->u.aes.type = CCP_AES_TYPE_256;
47 break;
48 default:
49 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
50 return -EINVAL;
51 }
52
53 ctx->u.aes.mode = CCP_AES_MODE_GCM;
54 ctx->u.aes.key_len = key_len;
55
56 memcpy(ctx->u.aes.key, key, key_len);
57 sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
58
59 return 0;
60 }
61
62 static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
63 unsigned int authsize)
64 {
65 return 0;
66 }
67
68 static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
69 {
70 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
71 struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
72 struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
73 struct scatterlist *iv_sg = NULL;
74 unsigned int iv_len = 0;
75 int i;
76 int ret = 0;
77
78 if (!ctx->u.aes.key_len)
79 return -EINVAL;
80
81 if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
82 return -EINVAL;
83
84 if (!req->iv)
85 return -EINVAL;
86
87 /*
88 * 5 parts:
89 * plaintext/ciphertext input
90 * AAD
91 * key
92 * IV
93 * Destination+tag buffer
94 */
95
96 /* Prepare the IV: 12 bytes + an integer (counter) */
97 memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
98 for (i = 0; i < 3; i++)
99 rctx->iv[i + GCM_AES_IV_SIZE] = 0;
100 rctx->iv[AES_BLOCK_SIZE - 1] = 1;
101
102 /* Set up a scatterlist for the IV */
103 iv_sg = &rctx->iv_sg;
104 iv_len = AES_BLOCK_SIZE;
105 sg_init_one(iv_sg, rctx->iv, iv_len);
106
107 /* The AAD + plaintext are concatenated in the src buffer */
108 memset(&rctx->cmd, 0, sizeof(rctx->cmd));
109 INIT_LIST_HEAD(&rctx->cmd.entry);
110 rctx->cmd.engine = CCP_ENGINE_AES;
111 rctx->cmd.u.aes.type = ctx->u.aes.type;
112 rctx->cmd.u.aes.mode = ctx->u.aes.mode;
113 rctx->cmd.u.aes.action = encrypt;
114 rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
115 rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
116 rctx->cmd.u.aes.iv = iv_sg;
117 rctx->cmd.u.aes.iv_len = iv_len;
118 rctx->cmd.u.aes.src = req->src;
119 rctx->cmd.u.aes.src_len = req->cryptlen;
120 rctx->cmd.u.aes.aad_len = req->assoclen;
121
122 /* The cipher text + the tag are in the dst buffer */
123 rctx->cmd.u.aes.dst = req->dst;
124
125 ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
126
127 return ret;
128 }
129
130 static int ccp_aes_gcm_encrypt(struct aead_request *req)
131 {
132 return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
133 }
134
135 static int ccp_aes_gcm_decrypt(struct aead_request *req)
136 {
137 return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
138 }
139
140 static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
141 {
142 struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
143
144 ctx->complete = ccp_aes_gcm_complete;
145 ctx->u.aes.key_len = 0;
146
147 crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
148
149 return 0;
150 }
151
152 static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
153 {
154 }
155
156 static struct aead_alg ccp_aes_gcm_defaults = {
157 .setkey = ccp_aes_gcm_setkey,
158 .setauthsize = ccp_aes_gcm_setauthsize,
159 .encrypt = ccp_aes_gcm_encrypt,
160 .decrypt = ccp_aes_gcm_decrypt,
161 .init = ccp_aes_gcm_cra_init,
162 .ivsize = GCM_AES_IV_SIZE,
163 .maxauthsize = AES_BLOCK_SIZE,
164 .base = {
165 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
166 CRYPTO_ALG_ASYNC |
167 CRYPTO_ALG_KERN_DRIVER_ONLY |
168 CRYPTO_ALG_NEED_FALLBACK,
169 .cra_blocksize = AES_BLOCK_SIZE,
170 .cra_ctxsize = sizeof(struct ccp_ctx),
171 .cra_priority = CCP_CRA_PRIORITY,
172 .cra_type = &crypto_ablkcipher_type,
173 .cra_exit = ccp_aes_gcm_cra_exit,
174 .cra_module = THIS_MODULE,
175 },
176 };
177
178 struct ccp_aes_aead_def {
179 enum ccp_aes_mode mode;
180 unsigned int version;
181 const char *name;
182 const char *driver_name;
183 unsigned int blocksize;
184 unsigned int ivsize;
185 struct aead_alg *alg_defaults;
186 };
187
188 static struct ccp_aes_aead_def aes_aead_algs[] = {
189 {
190 .mode = CCP_AES_MODE_GHASH,
191 .version = CCP_VERSION(5, 0),
192 .name = "gcm(aes)",
193 .driver_name = "gcm-aes-ccp",
194 .blocksize = 1,
195 .ivsize = AES_BLOCK_SIZE,
196 .alg_defaults = &ccp_aes_gcm_defaults,
197 },
198 };
199
200 static int ccp_register_aes_aead(struct list_head *head,
201 const struct ccp_aes_aead_def *def)
202 {
203 struct ccp_crypto_aead *ccp_aead;
204 struct aead_alg *alg;
205 int ret;
206
207 ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
208 if (!ccp_aead)
209 return -ENOMEM;
210
211 INIT_LIST_HEAD(&ccp_aead->entry);
212
213 ccp_aead->mode = def->mode;
214
215 /* Copy the defaults and override as necessary */
216 alg = &ccp_aead->alg;
217 *alg = *def->alg_defaults;
218 snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
219 snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
220 def->driver_name);
221 alg->base.cra_blocksize = def->blocksize;
222 alg->base.cra_ablkcipher.ivsize = def->ivsize;
223
224 ret = crypto_register_aead(alg);
225 if (ret) {
226 pr_err("%s ablkcipher algorithm registration error (%d)\n",
227 alg->base.cra_name, ret);
228 kfree(ccp_aead);
229 return ret;
230 }
231
232 list_add(&ccp_aead->entry, head);
233
234 return 0;
235 }
236
237 int ccp_register_aes_aeads(struct list_head *head)
238 {
239 int i, ret;
240 unsigned int ccpversion = ccp_version();
241
242 for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
243 if (aes_aead_algs[i].version > ccpversion)
244 continue;
245 ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
246 if (ret)
247 return ret;
248 }
249
250 return 0;
251 }