]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/crypto/ccp/ccp-crypto-aes-galois.c
Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[karo-tx-linux.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 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         return 0;
67 }
68
69 static int ccp_aes_gcm_crypt(struct aead_request *req, bool encrypt)
70 {
71         struct crypto_aead *tfm = crypto_aead_reqtfm(req);
72         struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
73         struct ccp_aes_req_ctx *rctx = aead_request_ctx(req);
74         struct scatterlist *iv_sg = NULL;
75         unsigned int iv_len = 0;
76         int i;
77         int ret = 0;
78
79         if (!ctx->u.aes.key_len)
80                 return -EINVAL;
81
82         if (ctx->u.aes.mode != CCP_AES_MODE_GCM)
83                 return -EINVAL;
84
85         if (!req->iv)
86                 return -EINVAL;
87
88         /*
89          * 5 parts:
90          *   plaintext/ciphertext input
91          *   AAD
92          *   key
93          *   IV
94          *   Destination+tag buffer
95          */
96
97         /* Prepare the IV: 12 bytes + an integer (counter) */
98         memcpy(rctx->iv, req->iv, AES_GCM_IVSIZE);
99         for (i = 0; i < 3; i++)
100                 rctx->iv[i + AES_GCM_IVSIZE] = 0;
101         rctx->iv[AES_BLOCK_SIZE - 1] = 1;
102
103         /* Set up a scatterlist for the IV */
104         iv_sg = &rctx->iv_sg;
105         iv_len = AES_BLOCK_SIZE;
106         sg_init_one(iv_sg, rctx->iv, iv_len);
107
108         /* The AAD + plaintext are concatenated in the src buffer */
109         memset(&rctx->cmd, 0, sizeof(rctx->cmd));
110         INIT_LIST_HEAD(&rctx->cmd.entry);
111         rctx->cmd.engine = CCP_ENGINE_AES;
112         rctx->cmd.u.aes.type = ctx->u.aes.type;
113         rctx->cmd.u.aes.mode = ctx->u.aes.mode;
114         rctx->cmd.u.aes.action = encrypt;
115         rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
116         rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
117         rctx->cmd.u.aes.iv = iv_sg;
118         rctx->cmd.u.aes.iv_len = iv_len;
119         rctx->cmd.u.aes.src = req->src;
120         rctx->cmd.u.aes.src_len = req->cryptlen;
121         rctx->cmd.u.aes.aad_len = req->assoclen;
122
123         /* The cipher text + the tag are in the dst buffer */
124         rctx->cmd.u.aes.dst = req->dst;
125
126         ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
127
128         return ret;
129 }
130
131 static int ccp_aes_gcm_encrypt(struct aead_request *req)
132 {
133         return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_ENCRYPT);
134 }
135
136 static int ccp_aes_gcm_decrypt(struct aead_request *req)
137 {
138         return ccp_aes_gcm_crypt(req, CCP_AES_ACTION_DECRYPT);
139 }
140
141 static int ccp_aes_gcm_cra_init(struct crypto_aead *tfm)
142 {
143         struct ccp_ctx *ctx = crypto_aead_ctx(tfm);
144
145         ctx->complete = ccp_aes_gcm_complete;
146         ctx->u.aes.key_len = 0;
147
148         crypto_aead_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
149
150         return 0;
151 }
152
153 static void ccp_aes_gcm_cra_exit(struct crypto_tfm *tfm)
154 {
155 }
156
157 static struct aead_alg ccp_aes_gcm_defaults = {
158         .setkey = ccp_aes_gcm_setkey,
159         .setauthsize = ccp_aes_gcm_setauthsize,
160         .encrypt = ccp_aes_gcm_encrypt,
161         .decrypt = ccp_aes_gcm_decrypt,
162         .init = ccp_aes_gcm_cra_init,
163         .ivsize = AES_GCM_IVSIZE,
164         .maxauthsize = AES_BLOCK_SIZE,
165         .base = {
166                 .cra_flags      = CRYPTO_ALG_TYPE_ABLKCIPHER |
167                                   CRYPTO_ALG_ASYNC |
168                                   CRYPTO_ALG_KERN_DRIVER_ONLY |
169                                   CRYPTO_ALG_NEED_FALLBACK,
170                 .cra_blocksize  = AES_BLOCK_SIZE,
171                 .cra_ctxsize    = sizeof(struct ccp_ctx),
172                 .cra_priority   = CCP_CRA_PRIORITY,
173                 .cra_type       = &crypto_ablkcipher_type,
174                 .cra_exit       = ccp_aes_gcm_cra_exit,
175                 .cra_module     = THIS_MODULE,
176         },
177 };
178
179 struct ccp_aes_aead_def {
180         enum ccp_aes_mode mode;
181         unsigned int version;
182         const char *name;
183         const char *driver_name;
184         unsigned int blocksize;
185         unsigned int ivsize;
186         struct aead_alg *alg_defaults;
187 };
188
189 static struct ccp_aes_aead_def aes_aead_algs[] = {
190         {
191                 .mode           = CCP_AES_MODE_GHASH,
192                 .version        = CCP_VERSION(5, 0),
193                 .name           = "gcm(aes)",
194                 .driver_name    = "gcm-aes-ccp",
195                 .blocksize      = 1,
196                 .ivsize         = AES_BLOCK_SIZE,
197                 .alg_defaults   = &ccp_aes_gcm_defaults,
198         },
199 };
200
201 static int ccp_register_aes_aead(struct list_head *head,
202                                  const struct ccp_aes_aead_def *def)
203 {
204         struct ccp_crypto_aead *ccp_aead;
205         struct aead_alg *alg;
206         int ret;
207
208         ccp_aead = kzalloc(sizeof(*ccp_aead), GFP_KERNEL);
209         if (!ccp_aead)
210                 return -ENOMEM;
211
212         INIT_LIST_HEAD(&ccp_aead->entry);
213
214         ccp_aead->mode = def->mode;
215
216         /* Copy the defaults and override as necessary */
217         alg = &ccp_aead->alg;
218         *alg = *def->alg_defaults;
219         snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
220         snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
221                  def->driver_name);
222         alg->base.cra_blocksize = def->blocksize;
223         alg->base.cra_ablkcipher.ivsize = def->ivsize;
224
225         ret = crypto_register_aead(alg);
226         if (ret) {
227                 pr_err("%s ablkcipher algorithm registration error (%d)\n",
228                        alg->base.cra_name, ret);
229                 kfree(ccp_aead);
230                 return ret;
231         }
232
233         list_add(&ccp_aead->entry, head);
234
235         return 0;
236 }
237
238 int ccp_register_aes_aeads(struct list_head *head)
239 {
240         int i, ret;
241         unsigned int ccpversion = ccp_version();
242
243         for (i = 0; i < ARRAY_SIZE(aes_aead_algs); i++) {
244                 if (aes_aead_algs[i].version > ccpversion)
245                         continue;
246                 ret = ccp_register_aes_aead(head, &aes_aead_algs[i]);
247                 if (ret)
248                         return ret;
249         }
250
251         return 0;
252 }