blob: a42f8619589d3949426863044b5e844e651e685b [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
James Hsiao049359d2009-02-05 16:18:13 +11002/**
3 * AMCC SoC PPC4xx Crypto Driver
4 *
5 * Copyright (c) 2008 Applied Micro Circuits Corporation.
6 * All rights reserved. James Hsiao <jhsiao@amcc.com>
7 *
James Hsiao049359d2009-02-05 16:18:13 +11008 * This file implements the Linux crypto algorithms.
9 */
10
11#include <linux/kernel.h>
12#include <linux/interrupt.h>
13#include <linux/spinlock_types.h>
14#include <linux/scatterlist.h>
15#include <linux/crypto.h>
16#include <linux/hash.h>
17#include <crypto/internal/hash.h>
18#include <linux/dma-mapping.h>
19#include <crypto/algapi.h>
Christian Lampartera0aae822017-10-04 01:00:15 +020020#include <crypto/aead.h>
James Hsiao049359d2009-02-05 16:18:13 +110021#include <crypto/aes.h>
Christian Lamparter59231362017-10-04 01:00:17 +020022#include <crypto/gcm.h>
James Hsiao049359d2009-02-05 16:18:13 +110023#include <crypto/sha.h>
Christian Lamparterf2a13e72017-08-25 15:47:21 +020024#include <crypto/ctr.h>
Christian Lamparterce05ffe2018-04-19 18:41:52 +020025#include <crypto/skcipher.h>
James Hsiao049359d2009-02-05 16:18:13 +110026#include "crypto4xx_reg_def.h"
James Hsiao049359d2009-02-05 16:18:13 +110027#include "crypto4xx_core.h"
Christian Lamparter249c8d92017-08-25 15:47:20 +020028#include "crypto4xx_sa.h"
James Hsiao049359d2009-02-05 16:18:13 +110029
Jingoo Han3a4eac72013-08-09 16:59:57 +090030static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
31 u32 save_iv, u32 ld_h, u32 ld_iv,
32 u32 hdr_proc, u32 h, u32 c, u32 pad_type,
33 u32 op_grp, u32 op, u32 dir)
James Hsiao049359d2009-02-05 16:18:13 +110034{
35 sa->sa_command_0.w = 0;
36 sa->sa_command_0.bf.save_hash_state = save_h;
37 sa->sa_command_0.bf.save_iv = save_iv;
38 sa->sa_command_0.bf.load_hash_state = ld_h;
39 sa->sa_command_0.bf.load_iv = ld_iv;
40 sa->sa_command_0.bf.hdr_proc = hdr_proc;
41 sa->sa_command_0.bf.hash_alg = h;
42 sa->sa_command_0.bf.cipher_alg = c;
43 sa->sa_command_0.bf.pad_type = pad_type & 3;
44 sa->sa_command_0.bf.extend_pad = pad_type >> 2;
45 sa->sa_command_0.bf.op_group = op_grp;
46 sa->sa_command_0.bf.opcode = op;
47 sa->sa_command_0.bf.dir = dir;
48}
49
Jingoo Han3a4eac72013-08-09 16:59:57 +090050static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
51 u32 hmac_mc, u32 cfb, u32 esn,
52 u32 sn_mask, u32 mute, u32 cp_pad,
53 u32 cp_pay, u32 cp_hdr)
James Hsiao049359d2009-02-05 16:18:13 +110054{
55 sa->sa_command_1.w = 0;
56 sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
57 sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
58 sa->sa_command_1.bf.feedback_mode = cfb,
59 sa->sa_command_1.bf.sa_rev = 1;
Christian Lamparter5a4326d32017-10-04 01:00:05 +020060 sa->sa_command_1.bf.hmac_muting = hmac_mc;
James Hsiao049359d2009-02-05 16:18:13 +110061 sa->sa_command_1.bf.extended_seq_num = esn;
62 sa->sa_command_1.bf.seq_num_mask = sn_mask;
63 sa->sa_command_1.bf.mutable_bit_proc = mute;
64 sa->sa_command_1.bf.copy_pad = cp_pad;
65 sa->sa_command_1.bf.copy_payload = cp_pay;
66 sa->sa_command_1.bf.copy_hdr = cp_hdr;
67}
68
Christian Lamparterce05ffe2018-04-19 18:41:52 +020069static inline int crypto4xx_crypt(struct skcipher_request *req,
Christian Lamparter0f7a8132019-05-18 23:28:12 +020070 const unsigned int ivlen, bool decrypt,
71 bool check_blocksize)
James Hsiao049359d2009-02-05 16:18:13 +110072{
Christian Lamparterce05ffe2018-04-19 18:41:52 +020073 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
74 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lamparterc4e90652018-04-19 18:41:53 +020075 __le32 iv[AES_IV_SIZE];
James Hsiao049359d2009-02-05 16:18:13 +110076
Christian Lamparter0f7a8132019-05-18 23:28:12 +020077 if (check_blocksize && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE))
78 return -EINVAL;
79
Christian Lampartercd4dcd62017-10-04 01:00:11 +020080 if (ivlen)
Christian Lamparterce05ffe2018-04-19 18:41:52 +020081 crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
James Hsiao049359d2009-02-05 16:18:13 +110082
83 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparterce05ffe2018-04-19 18:41:52 +020084 req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +020085 ctx->sa_len, 0, NULL);
James Hsiao049359d2009-02-05 16:18:13 +110086}
87
Christian Lamparter0f7a8132019-05-18 23:28:12 +020088int crypto4xx_encrypt_noiv_block(struct skcipher_request *req)
James Hsiao049359d2009-02-05 16:18:13 +110089{
Christian Lamparter0f7a8132019-05-18 23:28:12 +020090 return crypto4xx_crypt(req, 0, false, true);
Christian Lampartera8d79d72018-04-19 18:41:51 +020091}
James Hsiao049359d2009-02-05 16:18:13 +110092
Christian Lamparter0f7a8132019-05-18 23:28:12 +020093int crypto4xx_encrypt_iv_stream(struct skcipher_request *req)
Christian Lampartera8d79d72018-04-19 18:41:51 +020094{
Christian Lamparter0f7a8132019-05-18 23:28:12 +020095 return crypto4xx_crypt(req, AES_IV_SIZE, false, false);
Christian Lampartera8d79d72018-04-19 18:41:51 +020096}
James Hsiao049359d2009-02-05 16:18:13 +110097
Christian Lamparter0f7a8132019-05-18 23:28:12 +020098int crypto4xx_decrypt_noiv_block(struct skcipher_request *req)
Christian Lampartera8d79d72018-04-19 18:41:51 +020099{
Christian Lamparter0f7a8132019-05-18 23:28:12 +0200100 return crypto4xx_crypt(req, 0, true, true);
Christian Lampartera8d79d72018-04-19 18:41:51 +0200101}
102
Christian Lamparter0f7a8132019-05-18 23:28:12 +0200103int crypto4xx_decrypt_iv_stream(struct skcipher_request *req)
Christian Lampartera8d79d72018-04-19 18:41:51 +0200104{
Christian Lamparter0f7a8132019-05-18 23:28:12 +0200105 return crypto4xx_crypt(req, AES_IV_SIZE, true, false);
106}
107
108int crypto4xx_encrypt_iv_block(struct skcipher_request *req)
109{
110 return crypto4xx_crypt(req, AES_IV_SIZE, false, true);
111}
112
113int crypto4xx_decrypt_iv_block(struct skcipher_request *req)
114{
115 return crypto4xx_crypt(req, AES_IV_SIZE, true, true);
James Hsiao049359d2009-02-05 16:18:13 +1100116}
117
118/**
119 * AES Functions
120 */
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200121static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
James Hsiao049359d2009-02-05 16:18:13 +1100122 const u8 *key,
123 unsigned int keylen,
124 unsigned char cm,
125 u8 fb)
126{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200127 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
James Hsiao049359d2009-02-05 16:18:13 +1100128 struct dynamic_sa_ctl *sa;
129 int rc;
130
131 if (keylen != AES_KEYSIZE_256 &&
132 keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200133 crypto_skcipher_set_flags(cipher,
James Hsiao049359d2009-02-05 16:18:13 +1100134 CRYPTO_TFM_RES_BAD_KEY_LEN);
135 return -EINVAL;
136 }
137
138 /* Create SA */
Christian Lamparter2f776902017-10-04 01:00:14 +0200139 if (ctx->sa_in || ctx->sa_out)
James Hsiao049359d2009-02-05 16:18:13 +1100140 crypto4xx_free_sa(ctx);
141
142 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
143 if (rc)
144 return rc;
145
James Hsiao049359d2009-02-05 16:18:13 +1100146 /* Setup SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200147 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100148
Christian Lamparter25baaf82019-04-22 13:25:58 +0200149 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
150 SA_NOT_SAVE_IV : SA_SAVE_IV),
151 SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
152 SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
James Hsiao049359d2009-02-05 16:18:13 +1100153 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
154 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
155 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
156 DIR_INBOUND);
157
158 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
159 fb, SA_EXTENDED_SN_OFF,
160 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
161 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
162 SA_NOT_COPY_HDR);
Christian Lamparter4865b1222017-10-04 01:00:10 +0200163 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
164 key, keylen);
Christian Lamparter453e3092017-08-25 15:47:19 +0200165 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
James Hsiao049359d2009-02-05 16:18:13 +1100166 sa->sa_command_1.bf.key_len = keylen >> 3;
James Hsiao049359d2009-02-05 16:18:13 +1100167
168 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200169 sa = ctx->sa_out;
James Hsiao049359d2009-02-05 16:18:13 +1100170 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
Christian Lamparter25baaf82019-04-22 13:25:58 +0200171 /*
172 * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
173 * it's the DIR_(IN|OUT)BOUND that matters
174 */
175 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
James Hsiao049359d2009-02-05 16:18:13 +1100176
177 return 0;
178}
179
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200180int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
James Hsiao049359d2009-02-05 16:18:13 +1100181 const u8 *key, unsigned int keylen)
182{
183 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
184 CRYPTO_FEEDBACK_MODE_NO_FB);
185}
186
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200187int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200188 const u8 *key, unsigned int keylen)
189{
190 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
191 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
192}
193
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200194int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200195 const u8 *key, unsigned int keylen)
196{
197 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
198 CRYPTO_FEEDBACK_MODE_NO_FB);
199}
200
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200201int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200202 const u8 *key, unsigned int keylen)
203{
204 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
205 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
206}
207
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200208int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200209 const u8 *key, unsigned int keylen)
210{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200211 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200212 int rc;
213
214 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
215 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
216 if (rc)
217 return rc;
218
Christian Lamparter2f776902017-10-04 01:00:14 +0200219 ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
220 CTR_RFC3686_NONCE_SIZE]);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200221
222 return 0;
223}
224
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200225int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200226{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200227 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
228 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200229 __le32 iv[AES_IV_SIZE / 4] = {
Christian Lamparter2f776902017-10-04 01:00:14 +0200230 ctx->iv_nonce,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200231 cpu_to_le32p((u32 *) req->iv),
232 cpu_to_le32p((u32 *) (req->iv + 4)),
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200233 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200234
235 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200236 req->cryptlen, iv, AES_IV_SIZE,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200237 ctx->sa_out, ctx->sa_len, 0, NULL);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200238}
239
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200240int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200241{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200242 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
243 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200244 __le32 iv[AES_IV_SIZE / 4] = {
Christian Lamparter2f776902017-10-04 01:00:14 +0200245 ctx->iv_nonce,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200246 cpu_to_le32p((u32 *) req->iv),
247 cpu_to_le32p((u32 *) (req->iv + 4)),
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200248 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200249
250 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200251 req->cryptlen, iv, AES_IV_SIZE,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200252 ctx->sa_out, ctx->sa_len, 0, NULL);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200253}
254
Christian Lamparter98e87e32018-04-19 18:41:54 +0200255static int
256crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
257{
258 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
259 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
260 size_t iv_len = crypto_skcipher_ivsize(cipher);
261 unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
262 unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
263 AES_BLOCK_SIZE;
264
265 /*
266 * The hardware uses only the last 32-bits as the counter while the
267 * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
268 * the whole IV is a counter. So fallback if the counter is going to
269 * overlow.
270 */
271 if (counter + nblks < counter) {
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200272 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200273 int ret;
274
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200275 skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200276 skcipher_request_set_callback(subreq, req->base.flags,
277 NULL, NULL);
278 skcipher_request_set_crypt(subreq, req->src, req->dst,
279 req->cryptlen, req->iv);
280 ret = encrypt ? crypto_skcipher_encrypt(subreq)
281 : crypto_skcipher_decrypt(subreq);
282 skcipher_request_zero(subreq);
283 return ret;
284 }
285
Christian Lamparter0f7a8132019-05-18 23:28:12 +0200286 return encrypt ? crypto4xx_encrypt_iv_stream(req)
287 : crypto4xx_decrypt_iv_stream(req);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200288}
289
290static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
291 struct crypto_skcipher *cipher,
292 const u8 *key,
293 unsigned int keylen)
294{
295 int rc;
296
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200297 crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
Christian Lamparter98e87e32018-04-19 18:41:54 +0200298 CRYPTO_TFM_REQ_MASK);
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200299 crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
Christian Lamparter98e87e32018-04-19 18:41:54 +0200300 crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200301 rc = crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200302 crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
303 crypto_skcipher_set_flags(cipher,
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200304 crypto_sync_skcipher_get_flags(ctx->sw_cipher.cipher) &
Christian Lamparter98e87e32018-04-19 18:41:54 +0200305 CRYPTO_TFM_RES_MASK);
306
307 return rc;
308}
309
310int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
311 const u8 *key, unsigned int keylen)
312{
313 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
314 int rc;
315
316 rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
317 if (rc)
318 return rc;
319
320 return crypto4xx_setkey_aes(cipher, key, keylen,
321 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
322}
323
324int crypto4xx_encrypt_ctr(struct skcipher_request *req)
325{
326 return crypto4xx_ctr_crypt(req, true);
327}
328
329int crypto4xx_decrypt_ctr(struct skcipher_request *req)
330{
331 return crypto4xx_ctr_crypt(req, false);
332}
333
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200334static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
Christian Lamparter584201f2018-04-19 18:41:56 +0200335 unsigned int len,
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200336 bool is_ccm, bool decrypt)
337{
338 struct crypto_aead *aead = crypto_aead_reqtfm(req);
339
340 /* authsize has to be a multiple of 4 */
341 if (aead->authsize & 3)
342 return true;
343
344 /*
Christian Lamparter584201f2018-04-19 18:41:56 +0200345 * hardware does not handle cases where plaintext
346 * is less than a block.
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200347 */
Christian Lamparter584201f2018-04-19 18:41:56 +0200348 if (len < AES_BLOCK_SIZE)
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200349 return true;
350
Christian Lamparter584201f2018-04-19 18:41:56 +0200351 /* assoc len needs to be a multiple of 4 and <= 1020 */
352 if (req->assoclen & 0x3 || req->assoclen > 1020)
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200353 return true;
354
355 /* CCM supports only counter field length of 2 and 4 bytes */
356 if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
357 return true;
358
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200359 return false;
360}
361
362static int crypto4xx_aead_fallback(struct aead_request *req,
363 struct crypto4xx_ctx *ctx, bool do_decrypt)
364{
Christian Lamparterc4e90652018-04-19 18:41:53 +0200365 struct aead_request *subreq = aead_request_ctx(req);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200366
367 aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
368 aead_request_set_callback(subreq, req->base.flags,
369 req->base.complete, req->base.data);
370 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
371 req->iv);
372 aead_request_set_ad(subreq, req->assoclen);
373 return do_decrypt ? crypto_aead_decrypt(subreq) :
374 crypto_aead_encrypt(subreq);
375}
376
Christian Lamparter98e87e32018-04-19 18:41:54 +0200377static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
378 struct crypto_aead *cipher,
379 const u8 *key,
380 unsigned int keylen)
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200381{
382 int rc;
383
384 crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
385 crypto_aead_set_flags(ctx->sw_cipher.aead,
386 crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
387 rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
388 crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
389 crypto_aead_set_flags(cipher,
390 crypto_aead_get_flags(ctx->sw_cipher.aead) &
391 CRYPTO_TFM_RES_MASK);
392
393 return rc;
394}
395
396/**
397 * AES-CCM Functions
398 */
399
400int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
401 unsigned int keylen)
402{
403 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
404 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
405 struct dynamic_sa_ctl *sa;
406 int rc = 0;
407
Christian Lamparter98e87e32018-04-19 18:41:54 +0200408 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200409 if (rc)
410 return rc;
411
412 if (ctx->sa_in || ctx->sa_out)
413 crypto4xx_free_sa(ctx);
414
415 rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
416 if (rc)
417 return rc;
418
419 /* Setup SA */
420 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
421 sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
422
Christian Lamparter0b5a7f72017-12-23 19:45:46 +0100423 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200424 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
425 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
426 SA_CIPHER_ALG_AES,
427 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
428 SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
429
430 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
431 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
432 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
433 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
434 SA_NOT_COPY_HDR);
435
436 sa->sa_command_1.bf.key_len = keylen >> 3;
437
438 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
439
440 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
441 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
442
443 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
444 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
445 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
446 SA_CIPHER_ALG_AES,
447 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
448 SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
449
450 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
451 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
452 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
453 SA_COPY_PAD, SA_COPY_PAYLOAD,
454 SA_NOT_COPY_HDR);
455
456 sa->sa_command_1.bf.key_len = keylen >> 3;
457 return 0;
458}
459
460static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
461{
462 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200463 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200464 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200465 __le32 iv[16];
Christian Lamparterc4e90652018-04-19 18:41:53 +0200466 u32 tmp_sa[SA_AES128_CCM_LEN + 4];
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200467 struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
Christian Lamparter584201f2018-04-19 18:41:56 +0200468 unsigned int len = req->cryptlen;
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200469
470 if (decrypt)
471 len -= crypto_aead_authsize(aead);
472
Christian Lamparter584201f2018-04-19 18:41:56 +0200473 if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
474 return crypto4xx_aead_fallback(req, ctx, decrypt);
475
Christian Lamparterc4e90652018-04-19 18:41:53 +0200476 memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200477 sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
478
479 if (req->iv[0] == 1) {
480 /* CRYPTO_MODE_AES_ICM */
481 sa->sa_command_1.bf.crypto_mode9_8 = 1;
482 }
483
484 iv[3] = cpu_to_le32(0);
485 crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
486
487 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
488 len, iv, sizeof(iv),
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200489 sa, ctx->sa_len, req->assoclen, rctx->dst);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200490}
491
492int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
493{
494 return crypto4xx_crypt_aes_ccm(req, false);
495}
496
497int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
498{
499 return crypto4xx_crypt_aes_ccm(req, true);
500}
501
502int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
503 unsigned int authsize)
504{
505 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
506 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
507
508 return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
509}
510
James Hsiao049359d2009-02-05 16:18:13 +1100511/**
Christian Lamparter59231362017-10-04 01:00:17 +0200512 * AES-GCM Functions
513 */
514
515static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
516{
517 switch (keylen) {
518 case 16:
519 case 24:
520 case 32:
521 return 0;
522 default:
523 return -EINVAL;
524 }
525}
526
527static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
528 unsigned int keylen)
529{
Ard Biesheuvelda3e7a92019-07-02 21:41:42 +0200530 struct crypto_aes_ctx ctx;
Christian Lamparter59231362017-10-04 01:00:17 +0200531 uint8_t src[16] = { 0 };
Ard Biesheuvelda3e7a92019-07-02 21:41:42 +0200532 int rc;
Christian Lamparter59231362017-10-04 01:00:17 +0200533
Ard Biesheuvelda3e7a92019-07-02 21:41:42 +0200534 rc = aes_expandkey(&ctx, key, keylen);
535 if (rc) {
536 pr_err("aes_expandkey() failed: %d\n", rc);
Christian Lamparter59231362017-10-04 01:00:17 +0200537 return rc;
538 }
539
Ard Biesheuvelda3e7a92019-07-02 21:41:42 +0200540 aes_encrypt(&ctx, src, src);
Christian Lamparter59231362017-10-04 01:00:17 +0200541 crypto4xx_memcpy_to_le32(hash_start, src, 16);
Ard Biesheuvelda3e7a92019-07-02 21:41:42 +0200542 memzero_explicit(&ctx, sizeof(ctx));
543 return 0;
Christian Lamparter59231362017-10-04 01:00:17 +0200544}
545
546int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
547 const u8 *key, unsigned int keylen)
548{
549 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
550 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
551 struct dynamic_sa_ctl *sa;
552 int rc = 0;
553
554 if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
555 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
556 return -EINVAL;
557 }
558
Christian Lamparter98e87e32018-04-19 18:41:54 +0200559 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
Christian Lamparter59231362017-10-04 01:00:17 +0200560 if (rc)
561 return rc;
562
563 if (ctx->sa_in || ctx->sa_out)
564 crypto4xx_free_sa(ctx);
565
566 rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
567 if (rc)
568 return rc;
569
570 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
571
572 sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
573 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
574 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
575 SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
576 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
577 SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
578 DIR_INBOUND);
579 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
580 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
581 SA_SEQ_MASK_ON, SA_MC_DISABLE,
582 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
583 SA_NOT_COPY_HDR);
584
585 sa->sa_command_1.bf.key_len = keylen >> 3;
586
587 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
588 key, keylen);
589
590 rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
591 key, keylen);
592 if (rc) {
593 pr_err("GCM hash key setting failed = %d\n", rc);
594 goto err;
595 }
596
597 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
598 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
599 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
600 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
601
602 return 0;
603err:
604 crypto4xx_free_sa(ctx);
605 return rc;
606}
607
608static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
609 bool decrypt)
610{
611 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lamparter584201f2018-04-19 18:41:56 +0200612 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
Christian Lamparter59231362017-10-04 01:00:17 +0200613 __le32 iv[4];
Christian Lamparter584201f2018-04-19 18:41:56 +0200614 unsigned int len = req->cryptlen;
Christian Lamparter59231362017-10-04 01:00:17 +0200615
Christian Lamparter584201f2018-04-19 18:41:56 +0200616 if (decrypt)
617 len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
618
619 if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
Christian Lamparter59231362017-10-04 01:00:17 +0200620 return crypto4xx_aead_fallback(req, ctx, decrypt);
621
622 crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
623 iv[3] = cpu_to_le32(1);
624
Christian Lamparter59231362017-10-04 01:00:17 +0200625 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
626 len, iv, sizeof(iv),
627 decrypt ? ctx->sa_in : ctx->sa_out,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200628 ctx->sa_len, req->assoclen, rctx->dst);
Christian Lamparter59231362017-10-04 01:00:17 +0200629}
630
631int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
632{
633 return crypto4xx_crypt_aes_gcm(req, false);
634}
635
636int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
637{
638 return crypto4xx_crypt_aes_gcm(req, true);
639}
640
641/**
James Hsiao049359d2009-02-05 16:18:13 +1100642 * HASH SHA1 Functions
643 */
644static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
645 unsigned int sa_len,
646 unsigned char ha,
647 unsigned char hm)
648{
649 struct crypto_alg *alg = tfm->__crt_alg;
Christian Lampartera0aae822017-10-04 01:00:15 +0200650 struct crypto4xx_alg *my_alg;
James Hsiao049359d2009-02-05 16:18:13 +1100651 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200652 struct dynamic_sa_hash160 *sa;
James Hsiao049359d2009-02-05 16:18:13 +1100653 int rc;
654
Christian Lampartera0aae822017-10-04 01:00:15 +0200655 my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
656 alg.u.hash);
James Hsiao049359d2009-02-05 16:18:13 +1100657 ctx->dev = my_alg->dev;
James Hsiao049359d2009-02-05 16:18:13 +1100658
659 /* Create SA */
Christian Lamparter2f776902017-10-04 01:00:14 +0200660 if (ctx->sa_in || ctx->sa_out)
James Hsiao049359d2009-02-05 16:18:13 +1100661 crypto4xx_free_sa(ctx);
662
663 rc = crypto4xx_alloc_sa(ctx, sa_len);
664 if (rc)
665 return rc;
666
Herbert Xu6b1679f2009-07-12 23:08:28 +0800667 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
668 sizeof(struct crypto4xx_ctx));
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200669 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
670 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
James Hsiao049359d2009-02-05 16:18:13 +1100671 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
672 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
673 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
674 SA_OPCODE_HASH, DIR_INBOUND);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200675 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
James Hsiao049359d2009-02-05 16:18:13 +1100676 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
677 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
678 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
679 SA_NOT_COPY_HDR);
James Hsiao049359d2009-02-05 16:18:13 +1100680 /* Need to zero hash digest in SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200681 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
682 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
James Hsiao049359d2009-02-05 16:18:13 +1100683
684 return 0;
685}
686
687int crypto4xx_hash_init(struct ahash_request *req)
688{
689 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
690 int ds;
691 struct dynamic_sa_ctl *sa;
692
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200693 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100694 ds = crypto_ahash_digestsize(
695 __crypto_ahash_cast(req->base.tfm));
696 sa->sa_command_0.bf.digest_len = ds >> 2;
697 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
James Hsiao049359d2009-02-05 16:18:13 +1100698
699 return 0;
700}
701
702int crypto4xx_hash_update(struct ahash_request *req)
703{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200704 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100705 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200706 struct scatterlist dst;
707 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100708
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200709 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100710
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200711 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
712 req->nbytes, NULL, 0, ctx->sa_in,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200713 ctx->sa_len, 0, NULL);
James Hsiao049359d2009-02-05 16:18:13 +1100714}
715
716int crypto4xx_hash_final(struct ahash_request *req)
717{
718 return 0;
719}
720
721int crypto4xx_hash_digest(struct ahash_request *req)
722{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200723 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100724 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200725 struct scatterlist dst;
726 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100727
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200728 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100729
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200730 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
731 req->nbytes, NULL, 0, ctx->sa_in,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200732 ctx->sa_len, 0, NULL);
James Hsiao049359d2009-02-05 16:18:13 +1100733}
734
735/**
736 * SHA1 Algorithm
737 */
738int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
739{
740 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
741 SA_HASH_MODE_HASH);
742}