GNU Linux-libre 4.14.313-gnu1
[releases.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/scatterwalk.h>
23 #include <linux/delay.h>
24
25 #include "ccp-crypto.h"
26
27 #define AES_GCM_IVSIZE  12
28
29 static int ccp_aes_gcm_complete(struct crypto_async_request *async_req, int ret)
30 {
31         return ret;
32 }
33
34 static int ccp_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
35                               unsigned int key_len)
36 {
37         struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
38
39         switch (key_len) {
40         case AES_KEYSIZE_128:
41                 ctx->u.aes.type = CCP_AES_TYPE_128;
42                 break;
43         case AES_KEYSIZE_192:
44                 ctx->u.aes.type = CCP_AES_TYPE_192;
45                 break;
46         case AES_KEYSIZE_256:
47                 ctx->u.aes.type = CCP_AES_TYPE_256;
48                 break;
49         default:
50                 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
51                 return -EINVAL;
52         }
53
54         ctx->u.aes.mode = CCP_AES_MODE_GCM;
55         ctx->u.aes.key_len = key_len;
56
57         memcpy(ctx->u.aes.key, key, key_len);
58         sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
59
60         return 0;
61 }
62
63 static int ccp_aes_gcm_setauthsize(struct crypto_aead *tfm,
64                                    unsigned int authsize)
65 {
66         switch (authsize) {
67         case 16:
68         case 15:
69         case 14:
70         case 13:
71         case 12:
72         case 8:
73         case 4:
74                 break;
75         default:
76                 return -EINVAL;
77         }
78
79         return 0;
80 }
81
82 static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
83 {
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;
89         int i;
90         int ret = 0;
91
92         if (!ctx->u.aes.key_len)
93                 return -EINVAL;
94
95         if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
96                 return -EINVAL;
97
98         if (!req->iv)
99                 return -EINVAL;
100
101         /*
102          * 5 parts:
103          *   plaintext/ciphertext input
104          *   AAD
105          *   key
106          *   IV
107          *   Destination+tag buffer
108          */
109
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;
115
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);
120
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;
136
137         /* The cipher text + the tag are in the dst buffer */
138         rctx->cmd.u.aes.dst = req->dst;
139
140         ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
141
142         return ret;
143 }
144
145 static int ccp_aes_gcm_encrypt(struct aead_request *req)
146 {
147         return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
148 }
149
150 static int ccp_aes_gcm_decrypt(struct aead_request *req)
151 {
152         return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
153 }
154
155 static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
156 {
157         struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
158
159         ctx->complete = ccp_aes_gcm_complete;
160         ctx->u.aes.key_len = 0;
161
162         crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
163
164         return 0;
165 }
166
167 static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
168 {
169 }
170
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,
179         .base = {
180                 .cra_flags      = CRYPTO_ALG_TYPE_ABLKCIPHER |
181                                   CRYPTO_ALG_ASYNC |
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,
190         },
191 };
192
193 struct ccp_aes_aead_def {
194         enum ccp_aes_mode mode;
195         unsigned int version;
196         const char *name;
197         const char *driver_name;
198         unsigned int blocksize;
199         unsigned int ivsize;
200         struct aead_alg *alg_defaults;
201 };
202
203 static struct ccp_aes_aead_def aes_aead_algs[] = {
204         {
205                 .mode           = CCP_AES_MODE_GHASH,
206                 .version        = CCP_VERSION(5, 0),
207                 .name           = "gcm(aes)",
208                 .driver_name    = "gcm-aes-ccp",
209                 .blocksize      = 1,
210                 .ivsize         = AES_BLOCK_SIZE,
211                 .alg_defaults   = &ccp_aes_gcm_defaults,
212         },
213 };
214
215 static int ccp_register_aes_aead(struct list_head *head,
216                                  const struct ccp_aes_aead_def *def)
217 {
218         struct ccp_crypto_aead *ccp_aead;
219         struct aead_alg *alg;
220         int ret;
221
222         ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
223         if (!ccp_aead)
224                 return -ENOMEM;
225
226         INIT_LIST_HEAD(&ccp_aead->entry);
227
228         ccp_aead->mode = def->mode;
229
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",
235                  def->driver_name);
236         alg->base.cra_blocksize = def->blocksize;
237         alg->base.cra_ablkcipher.ivsize = def->ivsize;
238
239         ret = crypto_register_aead(alg);
240         if (ret) {
241                 pr_err("%s ablkcipher algorithm registration error (%d)\n",
242                        alg->base.cra_name, ret);
243                 kfree(ccp_aead);
244                 return ret;
245         }
246
247         list_add(&ccp_aead->entry, head);
248
249         return 0;
250 }
251
252 int ccp_register_aes_aeads(struct list_head *head)
253 {
254         int i, ret;
255         unsigned int ccpversion = ccp_version();
256
257         for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
258                 if (aes_aead_algs[i].version > ccpversion)
259                         continue;
260                 ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
261                 if (ret)
262                         return ret;
263         }
264
265         return 0;
266 }