888f7b68d04ee848ca5451546cdb4dc558b518ea
[openwrt/staging/jogo.git] / target / linux / apm821xx / patches-4.14 / 020-0025-crypto-crypto4xx-add-aes-gcm-support.patch
1 From 59231368d3a959fc30c5142c406a045f49130daa Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Wed, 4 Oct 2017 01:00:17 +0200
4 Subject: [PATCH 25/25] crypto: crypto4xx - add aes-gcm support
5
6 This patch adds aes-gcm support to crypto4xx.
7
8 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
9 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
10 ---
11 drivers/crypto/amcc/crypto4xx_alg.c | 139 +++++++++++++++++++++++++++++++++++
12 drivers/crypto/amcc/crypto4xx_core.c | 22 ++++++
13 drivers/crypto/amcc/crypto4xx_core.h | 4 +
14 3 files changed, 165 insertions(+)
15
16 --- a/drivers/crypto/amcc/crypto4xx_alg.c
17 +++ b/drivers/crypto/amcc/crypto4xx_alg.c
18 @@ -28,6 +28,7 @@
19 #include <crypto/algapi.h>
20 #include <crypto/aead.h>
21 #include <crypto/aes.h>
22 +#include <crypto/gcm.h>
23 #include <crypto/sha.h>
24 #include <crypto/ctr.h>
25 #include "crypto4xx_reg_def.h"
26 @@ -418,6 +419,144 @@ int crypto4xx_setauthsize_aead(struct cr
27 }
28
29 /**
30 + * AES-GCM Functions
31 + */
32 +
33 +static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
34 +{
35 + switch (keylen) {
36 + case 16:
37 + case 24:
38 + case 32:
39 + return 0;
40 + default:
41 + return -EINVAL;
42 + }
43 +}
44 +
45 +static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
46 + unsigned int keylen)
47 +{
48 + struct crypto_cipher *aes_tfm = NULL;
49 + uint8_t src[16] = { 0 };
50 + int rc = 0;
51 +
52 + aes_tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC |
53 + CRYPTO_ALG_NEED_FALLBACK);
54 + if (IS_ERR(aes_tfm)) {
55 + rc = PTR_ERR(aes_tfm);
56 + pr_warn("could not load aes cipher driver: %d\n", rc);
57 + return rc;
58 + }
59 +
60 + rc = crypto_cipher_setkey(aes_tfm, key, keylen);
61 + if (rc) {
62 + pr_err("setkey() failed: %d\n", rc);
63 + goto out;
64 + }
65 +
66 + crypto_cipher_encrypt_one(aes_tfm, src, src);
67 + crypto4xx_memcpy_to_le32(hash_start, src, 16);
68 +out:
69 + crypto_free_cipher(aes_tfm);
70 + return rc;
71 +}
72 +
73 +int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
74 + const u8 *key, unsigned int keylen)
75 +{
76 + struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
77 + struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
78 + struct dynamic_sa_ctl *sa;
79 + int rc = 0;
80 +
81 + if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
82 + crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
83 + return -EINVAL;
84 + }
85 +
86 + rc = crypto4xx_setup_fallback(ctx, cipher, key, keylen);
87 + if (rc)
88 + return rc;
89 +
90 + if (ctx->sa_in || ctx->sa_out)
91 + crypto4xx_free_sa(ctx);
92 +
93 + rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
94 + if (rc)
95 + return rc;
96 +
97 + sa = (struct dynamic_sa_ctl *) ctx->sa_in;
98 +
99 + sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
100 + set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
101 + SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
102 + SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
103 + SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
104 + SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
105 + DIR_INBOUND);
106 + set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
107 + CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
108 + SA_SEQ_MASK_ON, SA_MC_DISABLE,
109 + SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
110 + SA_NOT_COPY_HDR);
111 +
112 + sa->sa_command_1.bf.key_len = keylen >> 3;
113 +
114 + crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
115 + key, keylen);
116 +
117 + rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
118 + key, keylen);
119 + if (rc) {
120 + pr_err("GCM hash key setting failed = %d\n", rc);
121 + goto err;
122 + }
123 +
124 + memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
125 + sa = (struct dynamic_sa_ctl *) ctx->sa_out;
126 + sa->sa_command_0.bf.dir = DIR_OUTBOUND;
127 + sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
128 +
129 + return 0;
130 +err:
131 + crypto4xx_free_sa(ctx);
132 + return rc;
133 +}
134 +
135 +static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
136 + bool decrypt)
137 +{
138 + struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
139 + unsigned int len = req->cryptlen;
140 + __le32 iv[4];
141 +
142 + if (crypto4xx_aead_need_fallback(req, false, decrypt))
143 + return crypto4xx_aead_fallback(req, ctx, decrypt);
144 +
145 + crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
146 + iv[3] = cpu_to_le32(1);
147 +
148 + if (decrypt)
149 + len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
150 +
151 + return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
152 + len, iv, sizeof(iv),
153 + decrypt ? ctx->sa_in : ctx->sa_out,
154 + ctx->sa_len, req->assoclen);
155 +}
156 +
157 +int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
158 +{
159 + return crypto4xx_crypt_aes_gcm(req, false);
160 +}
161 +
162 +int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
163 +{
164 + return crypto4xx_crypt_aes_gcm(req, true);
165 +}
166 +
167 +/**
168 * HASH SHA1 Functions
169 */
170 static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
171 --- a/drivers/crypto/amcc/crypto4xx_core.c
172 +++ b/drivers/crypto/amcc/crypto4xx_core.c
173 @@ -38,6 +38,7 @@
174 #include <crypto/aead.h>
175 #include <crypto/aes.h>
176 #include <crypto/ctr.h>
177 +#include <crypto/gcm.h>
178 #include <crypto/sha.h>
179 #include <crypto/scatterwalk.h>
180 #include <crypto/internal/aead.h>
181 @@ -1236,6 +1237,27 @@ static struct crypto4xx_alg_common crypt
182 .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
183 .cra_flags = CRYPTO_ALG_ASYNC |
184 CRYPTO_ALG_NEED_FALLBACK |
185 + CRYPTO_ALG_KERN_DRIVER_ONLY,
186 + .cra_blocksize = 1,
187 + .cra_ctxsize = sizeof(struct crypto4xx_ctx),
188 + .cra_module = THIS_MODULE,
189 + },
190 + } },
191 + { .type = CRYPTO_ALG_TYPE_AEAD, .u.aead = {
192 + .setkey = crypto4xx_setkey_aes_gcm,
193 + .setauthsize = crypto4xx_setauthsize_aead,
194 + .encrypt = crypto4xx_encrypt_aes_gcm,
195 + .decrypt = crypto4xx_decrypt_aes_gcm,
196 + .init = crypto4xx_aead_init,
197 + .exit = crypto4xx_aead_exit,
198 + .ivsize = GCM_AES_IV_SIZE,
199 + .maxauthsize = 16,
200 + .base = {
201 + .cra_name = "gcm(aes)",
202 + .cra_driver_name = "gcm-aes-ppc4xx",
203 + .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
204 + .cra_flags = CRYPTO_ALG_ASYNC |
205 + CRYPTO_ALG_NEED_FALLBACK |
206 CRYPTO_ALG_KERN_DRIVER_ONLY,
207 .cra_blocksize = 1,
208 .cra_ctxsize = sizeof(struct crypto4xx_ctx),
209 --- a/drivers/crypto/amcc/crypto4xx_core.h
210 +++ b/drivers/crypto/amcc/crypto4xx_core.h
211 @@ -229,5 +229,9 @@ int crypto4xx_setkey_aes_ccm(struct cryp
212 const u8 *key, unsigned int keylen);
213 int crypto4xx_encrypt_aes_ccm(struct aead_request *req);
214 int crypto4xx_decrypt_aes_ccm(struct aead_request *req);
215 +int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
216 + const u8 *key, unsigned int keylen);
217 +int crypto4xx_encrypt_aes_gcm(struct aead_request *req);
218 +int crypto4xx_decrypt_aes_gcm(struct aead_request *req);
219
220 #endif