crypto: remove CRYPTO_TFM_RES_BAD_KEY_LEN
[linux-block.git] / drivers / crypto / amcc / crypto4xx_alg.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
049359d6
JH
2/**
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 *
049359d6
JH
8 * 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>
a0aae821 20#include <crypto/aead.h>
049359d6 21#include <crypto/aes.h>
59231368 22#include <crypto/gcm.h>
049359d6 23#include <crypto/sha.h>
f2a13e7c 24#include <crypto/ctr.h>
ce05ffe1 25#include <crypto/skcipher.h>
049359d6 26#include "crypto4xx_reg_def.h"
049359d6 27#include "crypto4xx_core.h"
249c8d98 28#include "crypto4xx_sa.h"
049359d6 29
3a4eac79
JH
30static 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)
049359d6
JH
34{
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
3a4eac79
JH
50static 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)
049359d6
JH
54{
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;
5a4326d3 60 sa->sa_command_1.bf.hmac_muting = hmac_mc;
049359d6
JH
61 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
ce05ffe1 69static inline int crypto4xx_crypt(struct skcipher_request *req,
0f7a8137
CL
70 const unsigned int ivlen, bool decrypt,
71 bool check_blocksize)
049359d6 72{
ce05ffe1
CL
73 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
74 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
c4e90650 75 __le32 iv[AES_IV_SIZE];
049359d6 76
0f7a8137
CL
77 if (check_blocksize && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE))
78 return -EINVAL;
79
cd4dcd6d 80 if (ivlen)
ce05ffe1 81 crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
049359d6
JH
82
83 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
ce05ffe1 84 req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
658c9d2b 85 ctx->sa_len, 0, NULL);
049359d6
JH
86}
87
0f7a8137
CL
88int crypto4xx_encrypt_noiv_block(struct skcipher_request *req)
89{
90 return crypto4xx_crypt(req, 0, false, true);
91}
92
93int crypto4xx_encrypt_iv_stream(struct skcipher_request *req)
94{
95 return crypto4xx_crypt(req, AES_IV_SIZE, false, false);
96}
97
98int crypto4xx_decrypt_noiv_block(struct skcipher_request *req)
049359d6 99{
0f7a8137 100 return crypto4xx_crypt(req, 0, true, true);
a8d79d7b 101}
049359d6 102
0f7a8137 103int crypto4xx_decrypt_iv_stream(struct skcipher_request *req)
a8d79d7b 104{
0f7a8137 105 return crypto4xx_crypt(req, AES_IV_SIZE, true, false);
a8d79d7b 106}
049359d6 107
0f7a8137 108int crypto4xx_encrypt_iv_block(struct skcipher_request *req)
a8d79d7b 109{
0f7a8137 110 return crypto4xx_crypt(req, AES_IV_SIZE, false, true);
a8d79d7b
CL
111}
112
0f7a8137 113int crypto4xx_decrypt_iv_block(struct skcipher_request *req)
a8d79d7b 114{
0f7a8137 115 return crypto4xx_crypt(req, AES_IV_SIZE, true, true);
049359d6
JH
116}
117
118/**
119 * AES Functions
120 */
ce05ffe1 121static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
049359d6
JH
122 const u8 *key,
123 unsigned int keylen,
124 unsigned char cm,
125 u8 fb)
126{
ce05ffe1 127 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
049359d6
JH
128 struct dynamic_sa_ctl *sa;
129 int rc;
130
674f368a
EB
131 if (keylen != AES_KEYSIZE_256 && keylen != AES_KEYSIZE_192 &&
132 keylen != AES_KEYSIZE_128)
049359d6 133 return -EINVAL;
049359d6
JH
134
135 /* Create SA */
2f77690d 136 if (ctx->sa_in || ctx->sa_out)
049359d6
JH
137 crypto4xx_free_sa(ctx);
138
139 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
140 if (rc)
141 return rc;
142
049359d6 143 /* Setup SA */
9e0a0b3a 144 sa = ctx->sa_in;
049359d6 145
25baaf8e
CL
146 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
147 SA_NOT_SAVE_IV : SA_SAVE_IV),
148 SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
149 SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
049359d6
JH
150 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
151 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
152 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
153 DIR_INBOUND);
154
155 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
156 fb, SA_EXTENDED_SN_OFF,
157 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
158 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
159 SA_NOT_COPY_HDR);
4865b122
CL
160 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
161 key, keylen);
453e3090 162 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
049359d6 163 sa->sa_command_1.bf.key_len = keylen >> 3;
049359d6
JH
164
165 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
9e0a0b3a 166 sa = ctx->sa_out;
049359d6 167 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
25baaf8e
CL
168 /*
169 * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
170 * it's the DIR_(IN|OUT)BOUND that matters
171 */
172 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
049359d6
JH
173
174 return 0;
175}
176
ce05ffe1 177int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
049359d6
JH
178 const u8 *key, unsigned int keylen)
179{
180 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
181 CRYPTO_FEEDBACK_MODE_NO_FB);
182}
183
ce05ffe1 184int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
f2a13e7c
CL
185 const u8 *key, unsigned int keylen)
186{
187 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
188 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
189}
190
ce05ffe1 191int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
f2a13e7c
CL
192 const u8 *key, unsigned int keylen)
193{
194 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
195 CRYPTO_FEEDBACK_MODE_NO_FB);
196}
197
ce05ffe1 198int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
f2a13e7c
CL
199 const u8 *key, unsigned int keylen)
200{
201 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
202 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
203}
204
ce05ffe1 205int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
f2a13e7c
CL
206 const u8 *key, unsigned int keylen)
207{
ce05ffe1 208 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
f2a13e7c
CL
209 int rc;
210
211 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
212 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
213 if (rc)
214 return rc;
215
2f77690d
CL
216 ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
217 CTR_RFC3686_NONCE_SIZE]);
f2a13e7c
CL
218
219 return 0;
220}
221
ce05ffe1 222int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
f2a13e7c 223{
ce05ffe1
CL
224 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
225 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
cd4dcd6d 226 __le32 iv[AES_IV_SIZE / 4] = {
2f77690d 227 ctx->iv_nonce,
ce05ffe1
CL
228 cpu_to_le32p((u32 *) req->iv),
229 cpu_to_le32p((u32 *) (req->iv + 4)),
cd4dcd6d 230 cpu_to_le32(1) };
f2a13e7c
CL
231
232 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
ce05ffe1 233 req->cryptlen, iv, AES_IV_SIZE,
658c9d2b 234 ctx->sa_out, ctx->sa_len, 0, NULL);
f2a13e7c
CL
235}
236
ce05ffe1 237int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
f2a13e7c 238{
ce05ffe1
CL
239 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
240 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
cd4dcd6d 241 __le32 iv[AES_IV_SIZE / 4] = {
2f77690d 242 ctx->iv_nonce,
ce05ffe1
CL
243 cpu_to_le32p((u32 *) req->iv),
244 cpu_to_le32p((u32 *) (req->iv + 4)),
cd4dcd6d 245 cpu_to_le32(1) };
f2a13e7c
CL
246
247 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
ce05ffe1 248 req->cryptlen, iv, AES_IV_SIZE,
658c9d2b 249 ctx->sa_out, ctx->sa_len, 0, NULL);
f2a13e7c
CL
250}
251
98e87e3d
CL
252static int
253crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
254{
255 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
256 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
257 size_t iv_len = crypto_skcipher_ivsize(cipher);
258 unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
259 unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
260 AES_BLOCK_SIZE;
261
262 /*
263 * The hardware uses only the last 32-bits as the counter while the
264 * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
265 * the whole IV is a counter. So fallback if the counter is going to
266 * overlow.
267 */
268 if (counter + nblks < counter) {
9848e4c8 269 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
98e87e3d
CL
270 int ret;
271
9848e4c8 272 skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
98e87e3d
CL
273 skcipher_request_set_callback(subreq, req->base.flags,
274 NULL, NULL);
275 skcipher_request_set_crypt(subreq, req->src, req->dst,
276 req->cryptlen, req->iv);
277 ret = encrypt ? crypto_skcipher_encrypt(subreq)
278 : crypto_skcipher_decrypt(subreq);
279 skcipher_request_zero(subreq);
280 return ret;
281 }
282
0f7a8137
CL
283 return encrypt ? crypto4xx_encrypt_iv_stream(req)
284 : crypto4xx_decrypt_iv_stream(req);
98e87e3d
CL
285}
286
287static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
288 struct crypto_skcipher *cipher,
289 const u8 *key,
290 unsigned int keylen)
291{
292 int rc;
293
9848e4c8 294 crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
98e87e3d 295 CRYPTO_TFM_REQ_MASK);
9848e4c8 296 crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
98e87e3d 297 crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
9848e4c8 298 rc = crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
98e87e3d
CL
299 crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
300 crypto_skcipher_set_flags(cipher,
9848e4c8 301 crypto_sync_skcipher_get_flags(ctx->sw_cipher.cipher) &
98e87e3d
CL
302 CRYPTO_TFM_RES_MASK);
303
304 return rc;
305}
306
307int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
308 const u8 *key, unsigned int keylen)
309{
310 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
311 int rc;
312
313 rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
314 if (rc)
315 return rc;
316
317 return crypto4xx_setkey_aes(cipher, key, keylen,
318 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
319}
320
321int crypto4xx_encrypt_ctr(struct skcipher_request *req)
322{
323 return crypto4xx_ctr_crypt(req, true);
324}
325
326int crypto4xx_decrypt_ctr(struct skcipher_request *req)
327{
328 return crypto4xx_ctr_crypt(req, false);
329}
330
65ea8b67 331static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
584201f1 332 unsigned int len,
65ea8b67
CL
333 bool is_ccm, bool decrypt)
334{
335 struct crypto_aead *aead = crypto_aead_reqtfm(req);
336
337 /* authsize has to be a multiple of 4 */
338 if (aead->authsize & 3)
339 return true;
340
341 /*
584201f1
CL
342 * hardware does not handle cases where plaintext
343 * is less than a block.
65ea8b67 344 */
584201f1 345 if (len < AES_BLOCK_SIZE)
65ea8b67
CL
346 return true;
347
584201f1
CL
348 /* assoc len needs to be a multiple of 4 and <= 1020 */
349 if (req->assoclen & 0x3 || req->assoclen > 1020)
65ea8b67
CL
350 return true;
351
352 /* CCM supports only counter field length of 2 and 4 bytes */
353 if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
354 return true;
355
65ea8b67
CL
356 return false;
357}
358
359static int crypto4xx_aead_fallback(struct aead_request *req,
360 struct crypto4xx_ctx *ctx, bool do_decrypt)
361{
c4e90650 362 struct aead_request *subreq = aead_request_ctx(req);
65ea8b67
CL
363
364 aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
365 aead_request_set_callback(subreq, req->base.flags,
366 req->base.complete, req->base.data);
367 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
368 req->iv);
369 aead_request_set_ad(subreq, req->assoclen);
370 return do_decrypt ? crypto_aead_decrypt(subreq) :
371 crypto_aead_encrypt(subreq);
372}
373
98e87e3d
CL
374static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
375 struct crypto_aead *cipher,
376 const u8 *key,
377 unsigned int keylen)
65ea8b67
CL
378{
379 int rc;
380
381 crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
382 crypto_aead_set_flags(ctx->sw_cipher.aead,
383 crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
384 rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
385 crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
386 crypto_aead_set_flags(cipher,
387 crypto_aead_get_flags(ctx->sw_cipher.aead) &
388 CRYPTO_TFM_RES_MASK);
389
390 return rc;
391}
392
393/**
394 * AES-CCM Functions
395 */
396
397int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
398 unsigned int keylen)
399{
400 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
401 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
402 struct dynamic_sa_ctl *sa;
403 int rc = 0;
404
98e87e3d 405 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
65ea8b67
CL
406 if (rc)
407 return rc;
408
409 if (ctx->sa_in || ctx->sa_out)
410 crypto4xx_free_sa(ctx);
411
412 rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
413 if (rc)
414 return rc;
415
416 /* Setup SA */
417 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
418 sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
419
0b5a7f71 420 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
65ea8b67
CL
421 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
422 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
423 SA_CIPHER_ALG_AES,
424 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
425 SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
426
427 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
428 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
429 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
430 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
431 SA_NOT_COPY_HDR);
432
433 sa->sa_command_1.bf.key_len = keylen >> 3;
434
435 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
436
437 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
438 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
439
440 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
441 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
442 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
443 SA_CIPHER_ALG_AES,
444 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
445 SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
446
447 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
448 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
449 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
450 SA_COPY_PAD, SA_COPY_PAYLOAD,
451 SA_NOT_COPY_HDR);
452
453 sa->sa_command_1.bf.key_len = keylen >> 3;
454 return 0;
455}
456
457static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
458{
459 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
658c9d2b 460 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
65ea8b67 461 struct crypto_aead *aead = crypto_aead_reqtfm(req);
65ea8b67 462 __le32 iv[16];
c4e90650 463 u32 tmp_sa[SA_AES128_CCM_LEN + 4];
65ea8b67 464 struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
584201f1 465 unsigned int len = req->cryptlen;
65ea8b67
CL
466
467 if (decrypt)
468 len -= crypto_aead_authsize(aead);
469
584201f1
CL
470 if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
471 return crypto4xx_aead_fallback(req, ctx, decrypt);
472
c4e90650 473 memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
65ea8b67
CL
474 sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
475
476 if (req->iv[0] == 1) {
477 /* CRYPTO_MODE_AES_ICM */
478 sa->sa_command_1.bf.crypto_mode9_8 = 1;
479 }
480
481 iv[3] = cpu_to_le32(0);
482 crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
483
484 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
485 len, iv, sizeof(iv),
658c9d2b 486 sa, ctx->sa_len, req->assoclen, rctx->dst);
65ea8b67
CL
487}
488
489int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
490{
491 return crypto4xx_crypt_aes_ccm(req, false);
492}
493
494int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
495{
496 return crypto4xx_crypt_aes_ccm(req, true);
497}
498
499int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
500 unsigned int authsize)
501{
502 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
503 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
504
505 return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
506}
507
59231368
CL
508/**
509 * AES-GCM Functions
510 */
511
512static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
513{
514 switch (keylen) {
515 case 16:
516 case 24:
517 case 32:
518 return 0;
519 default:
520 return -EINVAL;
521 }
522}
523
524static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
525 unsigned int keylen)
526{
da3e7a97 527 struct crypto_aes_ctx ctx;
59231368 528 uint8_t src[16] = { 0 };
da3e7a97 529 int rc;
59231368 530
da3e7a97 531 rc = aes_expandkey(&ctx, key, keylen);
59231368 532 if (rc) {
da3e7a97
AB
533 pr_err("aes_expandkey() failed: %d\n", rc);
534 return rc;
59231368
CL
535 }
536
da3e7a97 537 aes_encrypt(&ctx, src, src);
59231368 538 crypto4xx_memcpy_to_le32(hash_start, src, 16);
da3e7a97
AB
539 memzero_explicit(&ctx, sizeof(ctx));
540 return 0;
59231368
CL
541}
542
543int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
544 const u8 *key, unsigned int keylen)
545{
546 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
547 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
548 struct dynamic_sa_ctl *sa;
549 int rc = 0;
550
674f368a 551 if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0)
59231368 552 return -EINVAL;
59231368 553
98e87e3d 554 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
59231368
CL
555 if (rc)
556 return rc;
557
558 if (ctx->sa_in || ctx->sa_out)
559 crypto4xx_free_sa(ctx);
560
561 rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
562 if (rc)
563 return rc;
564
565 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
566
567 sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
568 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
569 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
570 SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
571 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
572 SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
573 DIR_INBOUND);
574 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
575 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
576 SA_SEQ_MASK_ON, SA_MC_DISABLE,
577 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
578 SA_NOT_COPY_HDR);
579
580 sa->sa_command_1.bf.key_len = keylen >> 3;
581
582 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
583 key, keylen);
584
585 rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
586 key, keylen);
587 if (rc) {
588 pr_err("GCM hash key setting failed = %d\n", rc);
589 goto err;
590 }
591
592 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
593 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
594 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
595 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
596
597 return 0;
598err:
599 crypto4xx_free_sa(ctx);
600 return rc;
601}
602
603static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
604 bool decrypt)
605{
606 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
584201f1 607 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
59231368 608 __le32 iv[4];
584201f1
CL
609 unsigned int len = req->cryptlen;
610
611 if (decrypt)
612 len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
59231368 613
584201f1 614 if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
59231368
CL
615 return crypto4xx_aead_fallback(req, ctx, decrypt);
616
617 crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
618 iv[3] = cpu_to_le32(1);
619
59231368
CL
620 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
621 len, iv, sizeof(iv),
622 decrypt ? ctx->sa_in : ctx->sa_out,
658c9d2b 623 ctx->sa_len, req->assoclen, rctx->dst);
59231368
CL
624}
625
626int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
627{
628 return crypto4xx_crypt_aes_gcm(req, false);
629}
630
631int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
632{
633 return crypto4xx_crypt_aes_gcm(req, true);
634}
635
049359d6
JH
636/**
637 * HASH SHA1 Functions
638 */
639static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
640 unsigned int sa_len,
641 unsigned char ha,
642 unsigned char hm)
643{
644 struct crypto_alg *alg = tfm->__crt_alg;
a0aae821 645 struct crypto4xx_alg *my_alg;
049359d6 646 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
9e0a0b3a 647 struct dynamic_sa_hash160 *sa;
049359d6
JH
648 int rc;
649
a0aae821
CL
650 my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
651 alg.u.hash);
049359d6 652 ctx->dev = my_alg->dev;
049359d6
JH
653
654 /* Create SA */
2f77690d 655 if (ctx->sa_in || ctx->sa_out)
049359d6
JH
656 crypto4xx_free_sa(ctx);
657
658 rc = crypto4xx_alloc_sa(ctx, sa_len);
659 if (rc)
660 return rc;
661
6b1679f4
HX
662 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
663 sizeof(struct crypto4xx_ctx));
9e0a0b3a
CL
664 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
665 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
049359d6
JH
666 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
667 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
668 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
669 SA_OPCODE_HASH, DIR_INBOUND);
9e0a0b3a 670 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
049359d6
JH
671 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
672 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
673 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
674 SA_NOT_COPY_HDR);
049359d6 675 /* Need to zero hash digest in SA */
9e0a0b3a
CL
676 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
677 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
049359d6
JH
678
679 return 0;
680}
681
682int crypto4xx_hash_init(struct ahash_request *req)
683{
684 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
685 int ds;
686 struct dynamic_sa_ctl *sa;
687
9e0a0b3a 688 sa = ctx->sa_in;
049359d6
JH
689 ds = crypto_ahash_digestsize(
690 __crypto_ahash_cast(req->base.tfm));
691 sa->sa_command_0.bf.digest_len = ds >> 2;
692 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
049359d6
JH
693
694 return 0;
695}
696
697int crypto4xx_hash_update(struct ahash_request *req)
698{
cd4dcd6d 699 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
049359d6 700 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
cd4dcd6d
CL
701 struct scatterlist dst;
702 unsigned int ds = crypto_ahash_digestsize(ahash);
049359d6 703
cd4dcd6d 704 sg_init_one(&dst, req->result, ds);
049359d6 705
cd4dcd6d
CL
706 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
707 req->nbytes, NULL, 0, ctx->sa_in,
658c9d2b 708 ctx->sa_len, 0, NULL);
049359d6
JH
709}
710
711int crypto4xx_hash_final(struct ahash_request *req)
712{
713 return 0;
714}
715
716int crypto4xx_hash_digest(struct ahash_request *req)
717{
cd4dcd6d 718 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
049359d6 719 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
cd4dcd6d
CL
720 struct scatterlist dst;
721 unsigned int ds = crypto_ahash_digestsize(ahash);
049359d6 722
cd4dcd6d 723 sg_init_one(&dst, req->result, ds);
049359d6 724
cd4dcd6d
CL
725 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
726 req->nbytes, NULL, 0, ctx->sa_in,
658c9d2b 727 ctx->sa_len, 0, NULL);
049359d6
JH
728}
729
730/**
731 * SHA1 Algorithm
732 */
733int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
734{
735 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
736 SA_HASH_MODE_HASH);
737}