1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMCC SoC PPC4xx Crypto Driver
5 * Copyright (c) 2008 Applied Micro Circuits Corporation.
6 * All rights reserved. James Hsiao <jhsiao@amcc.com>
8 * This file implements the Linux crypto algorithms.
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>
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 <crypto/skcipher.h>
26 #include "crypto4xx_reg_def.h"
27 #include "crypto4xx_core.h"
28 #include "crypto4xx_sa.h"
30 static 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)
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;
50 static 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)
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;
60 sa->sa_command_1.bf.hmac_muting = hmac_mc;
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;
69 static inline int crypto4xx_crypt(struct skcipher_request *req,
70 const unsigned int ivlen, bool decrypt)
72 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
73 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
74 __le32 iv[AES_IV_SIZE];
77 crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
79 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
80 req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
81 ctx->sa_len, 0, NULL);
84 int crypto4xx_encrypt_noiv(struct skcipher_request *req)
86 return crypto4xx_crypt(req, 0, false);
89 int crypto4xx_encrypt_iv(struct skcipher_request *req)
91 return crypto4xx_crypt(req, AES_IV_SIZE, false);
94 int crypto4xx_decrypt_noiv(struct skcipher_request *req)
96 return crypto4xx_crypt(req, 0, true);
99 int crypto4xx_decrypt_iv(struct skcipher_request *req)
101 return crypto4xx_crypt(req, AES_IV_SIZE, true);
107 static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
113 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
114 struct dynamic_sa_ctl *sa;
117 if (keylen != AES_KEYSIZE_256 &&
118 keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
119 crypto_skcipher_set_flags(cipher,
120 CRYPTO_TFM_RES_BAD_KEY_LEN);
125 if (ctx->sa_in || ctx->sa_out)
126 crypto4xx_free_sa(ctx);
128 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
135 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
136 SA_NOT_SAVE_IV : SA_SAVE_IV),
137 SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
138 SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
139 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
140 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
141 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
144 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
145 fb, SA_EXTENDED_SN_OFF,
146 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
147 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
149 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
151 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
152 sa->sa_command_1.bf.key_len = keylen >> 3;
154 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
156 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
158 * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
159 * it's the DIR_(IN|OUT)BOUND that matters
161 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
166 int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
167 const u8 *key, unsigned int keylen)
169 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
170 CRYPTO_FEEDBACK_MODE_NO_FB);
173 int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
174 const u8 *key, unsigned int keylen)
176 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
177 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
180 int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
181 const u8 *key, unsigned int keylen)
183 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
184 CRYPTO_FEEDBACK_MODE_NO_FB);
187 int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
188 const u8 *key, unsigned int keylen)
190 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
191 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
194 int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
195 const u8 *key, unsigned int keylen)
197 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
200 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
201 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
205 ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
206 CTR_RFC3686_NONCE_SIZE]);
211 int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
213 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
214 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
215 __le32 iv[AES_IV_SIZE / 4] = {
217 cpu_to_le32p((u32 *) req->iv),
218 cpu_to_le32p((u32 *) (req->iv + 4)),
221 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
222 req->cryptlen, iv, AES_IV_SIZE,
223 ctx->sa_out, ctx->sa_len, 0, NULL);
226 int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
228 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
229 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
230 __le32 iv[AES_IV_SIZE / 4] = {
232 cpu_to_le32p((u32 *) req->iv),
233 cpu_to_le32p((u32 *) (req->iv + 4)),
236 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
237 req->cryptlen, iv, AES_IV_SIZE,
238 ctx->sa_out, ctx->sa_len, 0, NULL);
242 crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
244 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
245 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
246 size_t iv_len = crypto_skcipher_ivsize(cipher);
247 unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
248 unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
252 * The hardware uses only the last 32-bits as the counter while the
253 * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
254 * the whole IV is a counter. So fallback if the counter is going to
257 if (counter + nblks < counter) {
258 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
261 skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
262 skcipher_request_set_callback(subreq, req->base.flags,
264 skcipher_request_set_crypt(subreq, req->src, req->dst,
265 req->cryptlen, req->iv);
266 ret = encrypt ? crypto_skcipher_encrypt(subreq)
267 : crypto_skcipher_decrypt(subreq);
268 skcipher_request_zero(subreq);
272 return encrypt ? crypto4xx_encrypt_iv(req)
273 : crypto4xx_decrypt_iv(req);
276 static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
277 struct crypto_skcipher *cipher,
283 crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
284 CRYPTO_TFM_REQ_MASK);
285 crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
286 crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
287 rc = crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
288 crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
289 crypto_skcipher_set_flags(cipher,
290 crypto_sync_skcipher_get_flags(ctx->sw_cipher.cipher) &
291 CRYPTO_TFM_RES_MASK);
296 int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
297 const u8 *key, unsigned int keylen)
299 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
302 rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
306 return crypto4xx_setkey_aes(cipher, key, keylen,
307 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
310 int crypto4xx_encrypt_ctr(struct skcipher_request *req)
312 return crypto4xx_ctr_crypt(req, true);
315 int crypto4xx_decrypt_ctr(struct skcipher_request *req)
317 return crypto4xx_ctr_crypt(req, false);
320 static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
322 bool is_ccm, bool decrypt)
324 struct crypto_aead *aead = crypto_aead_reqtfm(req);
326 /* authsize has to be a multiple of 4 */
327 if (aead->authsize & 3)
331 * hardware does not handle cases where plaintext
332 * is less than a block.
334 if (len < AES_BLOCK_SIZE)
337 /* assoc len needs to be a multiple of 4 and <= 1020 */
338 if (req->assoclen & 0x3 || req->assoclen > 1020)
341 /* CCM supports only counter field length of 2 and 4 bytes */
342 if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
348 static int crypto4xx_aead_fallback(struct aead_request *req,
349 struct crypto4xx_ctx *ctx, bool do_decrypt)
351 struct aead_request *subreq = aead_request_ctx(req);
353 aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
354 aead_request_set_callback(subreq, req->base.flags,
355 req->base.complete, req->base.data);
356 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
358 aead_request_set_ad(subreq, req->assoclen);
359 return do_decrypt ? crypto_aead_decrypt(subreq) :
360 crypto_aead_encrypt(subreq);
363 static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
364 struct crypto_aead *cipher,
370 crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
371 crypto_aead_set_flags(ctx->sw_cipher.aead,
372 crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
373 rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
374 crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
375 crypto_aead_set_flags(cipher,
376 crypto_aead_get_flags(ctx->sw_cipher.aead) &
377 CRYPTO_TFM_RES_MASK);
386 int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
389 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
390 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
391 struct dynamic_sa_ctl *sa;
394 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
398 if (ctx->sa_in || ctx->sa_out)
399 crypto4xx_free_sa(ctx);
401 rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
406 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
407 sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
409 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
410 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
411 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
413 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
414 SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
416 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
417 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
418 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
419 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
422 sa->sa_command_1.bf.key_len = keylen >> 3;
424 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
426 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
427 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
429 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
430 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
431 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
433 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
434 SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
436 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
437 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
438 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
439 SA_COPY_PAD, SA_COPY_PAYLOAD,
442 sa->sa_command_1.bf.key_len = keylen >> 3;
446 static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
448 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
449 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
450 struct crypto_aead *aead = crypto_aead_reqtfm(req);
452 u32 tmp_sa[SA_AES128_CCM_LEN + 4];
453 struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
454 unsigned int len = req->cryptlen;
457 len -= crypto_aead_authsize(aead);
459 if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
460 return crypto4xx_aead_fallback(req, ctx, decrypt);
462 memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
463 sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
465 if (req->iv[0] == 1) {
466 /* CRYPTO_MODE_AES_ICM */
467 sa->sa_command_1.bf.crypto_mode9_8 = 1;
470 iv[3] = cpu_to_le32(0);
471 crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
473 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
475 sa, ctx->sa_len, req->assoclen, rctx->dst);
478 int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
480 return crypto4xx_crypt_aes_ccm(req, false);
483 int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
485 return crypto4xx_crypt_aes_ccm(req, true);
488 int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
489 unsigned int authsize)
491 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
492 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
494 return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
501 static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
513 static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
516 struct crypto_cipher *aes_tfm = NULL;
517 uint8_t src[16] = { 0 };
520 aes_tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_NEED_FALLBACK);
521 if (IS_ERR(aes_tfm)) {
522 rc = PTR_ERR(aes_tfm);
523 pr_warn("could not load aes cipher driver: %d\n", rc);
527 rc = crypto_cipher_setkey(aes_tfm, key, keylen);
529 pr_err("setkey() failed: %d\n", rc);
533 crypto_cipher_encrypt_one(aes_tfm, src, src);
534 crypto4xx_memcpy_to_le32(hash_start, src, 16);
536 crypto_free_cipher(aes_tfm);
540 int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
541 const u8 *key, unsigned int keylen)
543 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
544 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
545 struct dynamic_sa_ctl *sa;
548 if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
549 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
553 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
557 if (ctx->sa_in || ctx->sa_out)
558 crypto4xx_free_sa(ctx);
560 rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
564 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
566 sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
567 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
568 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
569 SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
570 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
571 SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
573 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
574 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
575 SA_SEQ_MASK_ON, SA_MC_DISABLE,
576 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
579 sa->sa_command_1.bf.key_len = keylen >> 3;
581 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
584 rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
587 pr_err("GCM hash key setting failed = %d\n", rc);
591 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
592 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
593 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
594 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
598 crypto4xx_free_sa(ctx);
602 static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
605 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
606 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
608 unsigned int len = req->cryptlen;
611 len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
613 if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
614 return crypto4xx_aead_fallback(req, ctx, decrypt);
616 crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
617 iv[3] = cpu_to_le32(1);
619 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
621 decrypt ? ctx->sa_in : ctx->sa_out,
622 ctx->sa_len, req->assoclen, rctx->dst);
625 int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
627 return crypto4xx_crypt_aes_gcm(req, false);
630 int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
632 return crypto4xx_crypt_aes_gcm(req, true);
636 * HASH SHA1 Functions
638 static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
643 struct crypto_alg *alg = tfm->__crt_alg;
644 struct crypto4xx_alg *my_alg;
645 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
646 struct dynamic_sa_hash160 *sa;
649 my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
651 ctx->dev = my_alg->dev;
654 if (ctx->sa_in || ctx->sa_out)
655 crypto4xx_free_sa(ctx);
657 rc = crypto4xx_alloc_sa(ctx, sa_len);
661 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
662 sizeof(struct crypto4xx_ctx));
663 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
664 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
665 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
666 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
667 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
668 SA_OPCODE_HASH, DIR_INBOUND);
669 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
670 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
671 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
672 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
674 /* Need to zero hash digest in SA */
675 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
676 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
681 int crypto4xx_hash_init(struct ahash_request *req)
683 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
685 struct dynamic_sa_ctl *sa;
688 ds = crypto_ahash_digestsize(
689 __crypto_ahash_cast(req->base.tfm));
690 sa->sa_command_0.bf.digest_len = ds >> 2;
691 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
696 int crypto4xx_hash_update(struct ahash_request *req)
698 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
699 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
700 struct scatterlist dst;
701 unsigned int ds = crypto_ahash_digestsize(ahash);
703 sg_init_one(&dst, req->result, ds);
705 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
706 req->nbytes, NULL, 0, ctx->sa_in,
707 ctx->sa_len, 0, NULL);
710 int crypto4xx_hash_final(struct ahash_request *req)
715 int crypto4xx_hash_digest(struct ahash_request *req)
717 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
718 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
719 struct scatterlist dst;
720 unsigned int ds = crypto_ahash_digestsize(ahash);
722 sg_init_one(&dst, req->result, ds);
724 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
725 req->nbytes, NULL, 0, ctx->sa_in,
726 ctx->sa_len, 0, NULL);
732 int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
734 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,