2 * Support for Marvell's crypto engine which can be found on some Orion5X
5 * Author: Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
9 #include <crypto/aes.h>
10 #include <crypto/algapi.h>
11 #include <linux/crypto.h>
12 #include <linux/interrupt.h>
14 #include <linux/kthread.h>
15 #include <linux/platform_device.h>
16 #include <linux/scatterlist.h>
17 #include <linux/slab.h>
18 #include <crypto/internal/hash.h>
19 #include <crypto/sha.h>
23 #define MV_CESA "MV-CESA:"
24 #define MAX_HW_HASH_SIZE 0xFFFF
28 * /---------------------------------------\
29 * | | request complete
31 * IDLE -> new request -> BUSY -> done -> DEQUEUE
33 * | | more scatter entries
43 * struct req_progress - used for every crypt request
44 * @src_sg_it: sg iterator for src
45 * @dst_sg_it: sg iterator for dst
46 * @sg_src_left: bytes left in src to process (scatter list)
47 * @src_start: offset to add to src start position (scatter list)
48 * @crypt_len: length of current hw crypt/hash process
49 * @hw_nbytes: total bytes to process in hw for this request
50 * @copy_back: whether to copy data back (crypt) or not (hash)
51 * @sg_dst_left: bytes left dst to process in this scatter list
52 * @dst_start: offset to add to dst start position (scatter list)
53 * @hw_processed_bytes: number of bytes processed by hw (request).
55 * sg helper are used to iterate over the scatterlist. Since the size of the
56 * SRAM may be less than the scatter size, this struct struct is used to keep
57 * track of progress within current scatterlist.
60 struct sg_mapping_iter src_sg_it
;
61 struct sg_mapping_iter dst_sg_it
;
62 void (*complete
) (void);
63 void (*process
) (int is_first
);
74 int hw_processed_bytes
;
81 struct task_struct
*queue_th
;
83 /* the lock protects queue and eng_st */
85 struct crypto_queue queue
;
86 enum engine_status eng_st
;
87 struct crypto_async_request
*cur_req
;
88 struct req_progress p
;
95 static struct crypto_priv
*cpg
;
98 u8 aes_enc_key
[AES_KEY_LEN
];
101 u32 need_calc_aes_dkey
;
119 struct mv_tfm_hash_ctx
{
120 struct crypto_shash
*fallback
;
121 struct crypto_shash
*base_hash
;
122 u32 ivs
[2 * SHA1_DIGEST_SIZE
/ 4];
127 struct mv_req_hash_ctx
{
129 u32 state
[SHA1_DIGEST_SIZE
/ 4];
130 u8 buffer
[SHA1_BLOCK_SIZE
];
131 int first_hash
; /* marks that we don't have previous state */
132 int last_chunk
; /* marks that this is the 'final' request */
133 int extra_bytes
; /* unprocessed bytes in buffer */
138 static void compute_aes_dec_key(struct mv_ctx
*ctx
)
140 struct crypto_aes_ctx gen_aes_key
;
143 if (!ctx
->need_calc_aes_dkey
)
146 crypto_aes_expand_key(&gen_aes_key
, ctx
->aes_enc_key
, ctx
->key_len
);
148 key_pos
= ctx
->key_len
+ 24;
149 memcpy(ctx
->aes_dec_key
, &gen_aes_key
.key_enc
[key_pos
], 4 * 4);
150 switch (ctx
->key_len
) {
151 case AES_KEYSIZE_256
:
154 case AES_KEYSIZE_192
:
156 memcpy(&ctx
->aes_dec_key
[4], &gen_aes_key
.key_enc
[key_pos
],
160 ctx
->need_calc_aes_dkey
= 0;
163 static int mv_setkey_aes(struct crypto_ablkcipher
*cipher
, const u8
*key
,
166 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(cipher
);
167 struct mv_ctx
*ctx
= crypto_tfm_ctx(tfm
);
170 case AES_KEYSIZE_128
:
171 case AES_KEYSIZE_192
:
172 case AES_KEYSIZE_256
:
175 crypto_ablkcipher_set_flags(cipher
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
179 ctx
->need_calc_aes_dkey
= 1;
181 memcpy(ctx
->aes_enc_key
, key
, AES_KEY_LEN
);
185 static void copy_src_to_buf(struct req_progress
*p
, char *dbuf
, int len
)
192 if (!p
->sg_src_left
) {
193 ret
= sg_miter_next(&p
->src_sg_it
);
195 p
->sg_src_left
= p
->src_sg_it
.length
;
199 sbuf
= p
->src_sg_it
.addr
+ p
->src_start
;
201 copy_len
= min(p
->sg_src_left
, len
);
202 memcpy(dbuf
, sbuf
, copy_len
);
204 p
->src_start
+= copy_len
;
205 p
->sg_src_left
-= copy_len
;
212 static void setup_data_in(void)
214 struct req_progress
*p
= &cpg
->p
;
216 min(p
->hw_nbytes
- p
->hw_processed_bytes
, cpg
->max_req_size
);
217 copy_src_to_buf(p
, cpg
->sram
+ SRAM_DATA_IN_START
+ p
->crypt_len
,
218 data_in_sram
- p
->crypt_len
);
219 p
->crypt_len
= data_in_sram
;
222 static void mv_process_current_q(int first_block
)
224 struct ablkcipher_request
*req
= ablkcipher_request_cast(cpg
->cur_req
);
225 struct mv_ctx
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
226 struct mv_req_ctx
*req_ctx
= ablkcipher_request_ctx(req
);
227 struct sec_accel_config op
;
229 switch (req_ctx
->op
) {
231 op
.config
= CFG_OP_CRYPT_ONLY
| CFG_ENCM_AES
| CFG_ENC_MODE_ECB
;
235 op
.config
= CFG_OP_CRYPT_ONLY
| CFG_ENCM_AES
| CFG_ENC_MODE_CBC
;
236 op
.enc_iv
= ENC_IV_POINT(SRAM_DATA_IV
) |
237 ENC_IV_BUF_POINT(SRAM_DATA_IV_BUF
);
239 memcpy(cpg
->sram
+ SRAM_DATA_IV
, req
->info
, 16);
242 if (req_ctx
->decrypt
) {
243 op
.config
|= CFG_DIR_DEC
;
244 memcpy(cpg
->sram
+ SRAM_DATA_KEY_P
, ctx
->aes_dec_key
,
247 op
.config
|= CFG_DIR_ENC
;
248 memcpy(cpg
->sram
+ SRAM_DATA_KEY_P
, ctx
->aes_enc_key
,
252 switch (ctx
->key_len
) {
253 case AES_KEYSIZE_128
:
254 op
.config
|= CFG_AES_LEN_128
;
256 case AES_KEYSIZE_192
:
257 op
.config
|= CFG_AES_LEN_192
;
259 case AES_KEYSIZE_256
:
260 op
.config
|= CFG_AES_LEN_256
;
263 op
.enc_p
= ENC_P_SRC(SRAM_DATA_IN_START
) |
264 ENC_P_DST(SRAM_DATA_OUT_START
);
265 op
.enc_key_p
= SRAM_DATA_KEY_P
;
268 op
.enc_len
= cpg
->p
.crypt_len
;
269 memcpy(cpg
->sram
+ SRAM_CONFIG
, &op
,
270 sizeof(struct sec_accel_config
));
273 writel(SEC_CMD_EN_SEC_ACCL0
, cpg
->reg
+ SEC_ACCEL_CMD
);
276 * XXX: add timer if the interrupt does not occur for some mystery
281 static void mv_crypto_algo_completion(void)
283 struct ablkcipher_request
*req
= ablkcipher_request_cast(cpg
->cur_req
);
284 struct mv_req_ctx
*req_ctx
= ablkcipher_request_ctx(req
);
286 sg_miter_stop(&cpg
->p
.src_sg_it
);
287 sg_miter_stop(&cpg
->p
.dst_sg_it
);
289 if (req_ctx
->op
!= COP_AES_CBC
)
292 memcpy(req
->info
, cpg
->sram
+ SRAM_DATA_IV_BUF
, 16);
295 static void mv_process_hash_current(int first_block
)
297 struct ahash_request
*req
= ahash_request_cast(cpg
->cur_req
);
298 const struct mv_tfm_hash_ctx
*tfm_ctx
= crypto_tfm_ctx(req
->base
.tfm
);
299 struct mv_req_hash_ctx
*req_ctx
= ahash_request_ctx(req
);
300 struct req_progress
*p
= &cpg
->p
;
301 struct sec_accel_config op
= { 0 };
304 switch (req_ctx
->op
) {
307 op
.config
= CFG_OP_MAC_ONLY
| CFG_MACM_SHA1
;
310 op
.config
= CFG_OP_MAC_ONLY
| CFG_MACM_HMAC_SHA1
;
311 memcpy(cpg
->sram
+ SRAM_HMAC_IV_IN
,
312 tfm_ctx
->ivs
, sizeof(tfm_ctx
->ivs
));
317 MAC_SRC_DATA_P(SRAM_DATA_IN_START
) | MAC_SRC_TOTAL_LEN((u32
)
324 MAC_DIGEST_P(SRAM_DIGEST_BUF
) | MAC_FRAG_LEN(p
->crypt_len
);
326 MAC_INNER_IV_P(SRAM_HMAC_IV_IN
) |
327 MAC_OUTER_IV_P(SRAM_HMAC_IV_OUT
);
329 is_last
= req_ctx
->last_chunk
330 && (p
->hw_processed_bytes
+ p
->crypt_len
>= p
->hw_nbytes
)
331 && (req_ctx
->count
<= MAX_HW_HASH_SIZE
);
332 if (req_ctx
->first_hash
) {
334 op
.config
|= CFG_NOT_FRAG
;
336 op
.config
|= CFG_FIRST_FRAG
;
338 req_ctx
->first_hash
= 0;
341 op
.config
|= CFG_LAST_FRAG
;
343 op
.config
|= CFG_MID_FRAG
;
346 writel(req_ctx
->state
[0], cpg
->reg
+ DIGEST_INITIAL_VAL_A
);
347 writel(req_ctx
->state
[1], cpg
->reg
+ DIGEST_INITIAL_VAL_B
);
348 writel(req_ctx
->state
[2], cpg
->reg
+ DIGEST_INITIAL_VAL_C
);
349 writel(req_ctx
->state
[3], cpg
->reg
+ DIGEST_INITIAL_VAL_D
);
350 writel(req_ctx
->state
[4], cpg
->reg
+ DIGEST_INITIAL_VAL_E
);
354 memcpy(cpg
->sram
+ SRAM_CONFIG
, &op
, sizeof(struct sec_accel_config
));
357 writel(SEC_CMD_EN_SEC_ACCL0
, cpg
->reg
+ SEC_ACCEL_CMD
);
360 * XXX: add timer if the interrupt does not occur for some mystery
365 static inline int mv_hash_import_sha1_ctx(const struct mv_req_hash_ctx
*ctx
,
366 struct shash_desc
*desc
)
369 struct sha1_state shash_state
;
371 shash_state
.count
= ctx
->count
+ ctx
->count_add
;
372 for (i
= 0; i
< 5; i
++)
373 shash_state
.state
[i
] = ctx
->state
[i
];
374 memcpy(shash_state
.buffer
, ctx
->buffer
, sizeof(shash_state
.buffer
));
375 return crypto_shash_import(desc
, &shash_state
);
378 static int mv_hash_final_fallback(struct ahash_request
*req
)
380 const struct mv_tfm_hash_ctx
*tfm_ctx
= crypto_tfm_ctx(req
->base
.tfm
);
381 struct mv_req_hash_ctx
*req_ctx
= ahash_request_ctx(req
);
383 struct shash_desc shash
;
384 char ctx
[crypto_shash_descsize(tfm_ctx
->fallback
)];
388 desc
.shash
.tfm
= tfm_ctx
->fallback
;
389 desc
.shash
.flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
390 if (unlikely(req_ctx
->first_hash
)) {
391 crypto_shash_init(&desc
.shash
);
392 crypto_shash_update(&desc
.shash
, req_ctx
->buffer
,
393 req_ctx
->extra_bytes
);
395 /* only SHA1 for now....
397 rc
= mv_hash_import_sha1_ctx(req_ctx
, &desc
.shash
);
401 rc
= crypto_shash_final(&desc
.shash
, req
->result
);
406 static void mv_hash_algo_completion(void)
408 struct ahash_request
*req
= ahash_request_cast(cpg
->cur_req
);
409 struct mv_req_hash_ctx
*ctx
= ahash_request_ctx(req
);
411 if (ctx
->extra_bytes
)
412 copy_src_to_buf(&cpg
->p
, ctx
->buffer
, ctx
->extra_bytes
);
413 sg_miter_stop(&cpg
->p
.src_sg_it
);
415 if (likely(ctx
->last_chunk
)) {
416 if (likely(ctx
->count
<= MAX_HW_HASH_SIZE
)) {
417 memcpy(req
->result
, cpg
->sram
+ SRAM_DIGEST_BUF
,
418 crypto_ahash_digestsize(crypto_ahash_reqtfm
421 mv_hash_final_fallback(req
);
423 ctx
->state
[0] = readl(cpg
->reg
+ DIGEST_INITIAL_VAL_A
);
424 ctx
->state
[1] = readl(cpg
->reg
+ DIGEST_INITIAL_VAL_B
);
425 ctx
->state
[2] = readl(cpg
->reg
+ DIGEST_INITIAL_VAL_C
);
426 ctx
->state
[3] = readl(cpg
->reg
+ DIGEST_INITIAL_VAL_D
);
427 ctx
->state
[4] = readl(cpg
->reg
+ DIGEST_INITIAL_VAL_E
);
431 static void dequeue_complete_req(void)
433 struct crypto_async_request
*req
= cpg
->cur_req
;
436 cpg
->p
.hw_processed_bytes
+= cpg
->p
.crypt_len
;
437 if (cpg
->p
.copy_back
) {
438 int need_copy_len
= cpg
->p
.crypt_len
;
443 if (!cpg
->p
.sg_dst_left
) {
444 ret
= sg_miter_next(&cpg
->p
.dst_sg_it
);
446 cpg
->p
.sg_dst_left
= cpg
->p
.dst_sg_it
.length
;
447 cpg
->p
.dst_start
= 0;
450 buf
= cpg
->p
.dst_sg_it
.addr
;
451 buf
+= cpg
->p
.dst_start
;
453 dst_copy
= min(need_copy_len
, cpg
->p
.sg_dst_left
);
456 cpg
->sram
+ SRAM_DATA_OUT_START
+ sram_offset
,
458 sram_offset
+= dst_copy
;
459 cpg
->p
.sg_dst_left
-= dst_copy
;
460 need_copy_len
-= dst_copy
;
461 cpg
->p
.dst_start
+= dst_copy
;
462 } while (need_copy_len
> 0);
465 cpg
->p
.crypt_len
= 0;
467 BUG_ON(cpg
->eng_st
!= ENGINE_W_DEQUEUE
);
468 if (cpg
->p
.hw_processed_bytes
< cpg
->p
.hw_nbytes
) {
469 /* process next scatter list entry */
470 cpg
->eng_st
= ENGINE_BUSY
;
474 cpg
->eng_st
= ENGINE_IDLE
;
476 req
->complete(req
, 0);
481 static int count_sgs(struct scatterlist
*sl
, unsigned int total_bytes
)
487 cur_len
= sl
[i
].length
;
489 if (total_bytes
> cur_len
)
490 total_bytes
-= cur_len
;
498 static void mv_start_new_crypt_req(struct ablkcipher_request
*req
)
500 struct req_progress
*p
= &cpg
->p
;
503 cpg
->cur_req
= &req
->base
;
504 memset(p
, 0, sizeof(struct req_progress
));
505 p
->hw_nbytes
= req
->nbytes
;
506 p
->complete
= mv_crypto_algo_completion
;
507 p
->process
= mv_process_current_q
;
510 num_sgs
= count_sgs(req
->src
, req
->nbytes
);
511 sg_miter_start(&p
->src_sg_it
, req
->src
, num_sgs
, SG_MITER_FROM_SG
);
513 num_sgs
= count_sgs(req
->dst
, req
->nbytes
);
514 sg_miter_start(&p
->dst_sg_it
, req
->dst
, num_sgs
, SG_MITER_TO_SG
);
516 mv_process_current_q(1);
519 static void mv_start_new_hash_req(struct ahash_request
*req
)
521 struct req_progress
*p
= &cpg
->p
;
522 struct mv_req_hash_ctx
*ctx
= ahash_request_ctx(req
);
523 int num_sgs
, hw_bytes
, old_extra_bytes
, rc
;
524 cpg
->cur_req
= &req
->base
;
525 memset(p
, 0, sizeof(struct req_progress
));
526 hw_bytes
= req
->nbytes
+ ctx
->extra_bytes
;
527 old_extra_bytes
= ctx
->extra_bytes
;
529 ctx
->extra_bytes
= hw_bytes
% SHA1_BLOCK_SIZE
;
530 if (ctx
->extra_bytes
!= 0
531 && (!ctx
->last_chunk
|| ctx
->count
> MAX_HW_HASH_SIZE
))
532 hw_bytes
-= ctx
->extra_bytes
;
534 ctx
->extra_bytes
= 0;
536 num_sgs
= count_sgs(req
->src
, req
->nbytes
);
537 sg_miter_start(&p
->src_sg_it
, req
->src
, num_sgs
, SG_MITER_FROM_SG
);
540 p
->hw_nbytes
= hw_bytes
;
541 p
->complete
= mv_hash_algo_completion
;
542 p
->process
= mv_process_hash_current
;
544 if (unlikely(old_extra_bytes
)) {
545 memcpy(cpg
->sram
+ SRAM_DATA_IN_START
, ctx
->buffer
,
547 p
->crypt_len
= old_extra_bytes
;
550 mv_process_hash_current(1);
552 copy_src_to_buf(p
, ctx
->buffer
+ old_extra_bytes
,
553 ctx
->extra_bytes
- old_extra_bytes
);
554 sg_miter_stop(&p
->src_sg_it
);
556 rc
= mv_hash_final_fallback(req
);
559 cpg
->eng_st
= ENGINE_IDLE
;
561 req
->base
.complete(&req
->base
, rc
);
566 static int queue_manag(void *data
)
568 cpg
->eng_st
= ENGINE_IDLE
;
570 struct crypto_async_request
*async_req
= NULL
;
571 struct crypto_async_request
*backlog
;
573 __set_current_state(TASK_INTERRUPTIBLE
);
575 if (cpg
->eng_st
== ENGINE_W_DEQUEUE
)
576 dequeue_complete_req();
578 spin_lock_irq(&cpg
->lock
);
579 if (cpg
->eng_st
== ENGINE_IDLE
) {
580 backlog
= crypto_get_backlog(&cpg
->queue
);
581 async_req
= crypto_dequeue_request(&cpg
->queue
);
583 BUG_ON(cpg
->eng_st
!= ENGINE_IDLE
);
584 cpg
->eng_st
= ENGINE_BUSY
;
587 spin_unlock_irq(&cpg
->lock
);
590 backlog
->complete(backlog
, -EINPROGRESS
);
595 if (async_req
->tfm
->__crt_alg
->cra_type
!=
596 &crypto_ahash_type
) {
597 struct ablkcipher_request
*req
=
598 ablkcipher_request_cast(async_req
);
599 mv_start_new_crypt_req(req
);
601 struct ahash_request
*req
=
602 ahash_request_cast(async_req
);
603 mv_start_new_hash_req(req
);
610 } while (!kthread_should_stop());
614 static int mv_handle_req(struct crypto_async_request
*req
)
619 spin_lock_irqsave(&cpg
->lock
, flags
);
620 ret
= crypto_enqueue_request(&cpg
->queue
, req
);
621 spin_unlock_irqrestore(&cpg
->lock
, flags
);
622 wake_up_process(cpg
->queue_th
);
626 static int mv_enc_aes_ecb(struct ablkcipher_request
*req
)
628 struct mv_req_ctx
*req_ctx
= ablkcipher_request_ctx(req
);
630 req_ctx
->op
= COP_AES_ECB
;
631 req_ctx
->decrypt
= 0;
633 return mv_handle_req(&req
->base
);
636 static int mv_dec_aes_ecb(struct ablkcipher_request
*req
)
638 struct mv_ctx
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
639 struct mv_req_ctx
*req_ctx
= ablkcipher_request_ctx(req
);
641 req_ctx
->op
= COP_AES_ECB
;
642 req_ctx
->decrypt
= 1;
644 compute_aes_dec_key(ctx
);
645 return mv_handle_req(&req
->base
);
648 static int mv_enc_aes_cbc(struct ablkcipher_request
*req
)
650 struct mv_req_ctx
*req_ctx
= ablkcipher_request_ctx(req
);
652 req_ctx
->op
= COP_AES_CBC
;
653 req_ctx
->decrypt
= 0;
655 return mv_handle_req(&req
->base
);
658 static int mv_dec_aes_cbc(struct ablkcipher_request
*req
)
660 struct mv_ctx
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
661 struct mv_req_ctx
*req_ctx
= ablkcipher_request_ctx(req
);
663 req_ctx
->op
= COP_AES_CBC
;
664 req_ctx
->decrypt
= 1;
666 compute_aes_dec_key(ctx
);
667 return mv_handle_req(&req
->base
);
670 static int mv_cra_init(struct crypto_tfm
*tfm
)
672 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct mv_req_ctx
);
676 static void mv_init_hash_req_ctx(struct mv_req_hash_ctx
*ctx
, int op
,
677 int is_last
, unsigned int req_len
,
680 memset(ctx
, 0, sizeof(*ctx
));
682 ctx
->count
= req_len
;
684 ctx
->last_chunk
= is_last
;
685 ctx
->count_add
= count_add
;
688 static void mv_update_hash_req_ctx(struct mv_req_hash_ctx
*ctx
, int is_last
,
691 ctx
->last_chunk
= is_last
;
692 ctx
->count
+= req_len
;
695 static int mv_hash_init(struct ahash_request
*req
)
697 const struct mv_tfm_hash_ctx
*tfm_ctx
= crypto_tfm_ctx(req
->base
.tfm
);
698 mv_init_hash_req_ctx(ahash_request_ctx(req
), tfm_ctx
->op
, 0, 0,
703 static int mv_hash_update(struct ahash_request
*req
)
708 mv_update_hash_req_ctx(ahash_request_ctx(req
), 0, req
->nbytes
);
709 return mv_handle_req(&req
->base
);
712 static int mv_hash_final(struct ahash_request
*req
)
714 struct mv_req_hash_ctx
*ctx
= ahash_request_ctx(req
);
716 ahash_request_set_crypt(req
, NULL
, req
->result
, 0);
717 mv_update_hash_req_ctx(ctx
, 1, 0);
718 return mv_handle_req(&req
->base
);
721 static int mv_hash_finup(struct ahash_request
*req
)
723 mv_update_hash_req_ctx(ahash_request_ctx(req
), 1, req
->nbytes
);
724 return mv_handle_req(&req
->base
);
727 static int mv_hash_digest(struct ahash_request
*req
)
729 const struct mv_tfm_hash_ctx
*tfm_ctx
= crypto_tfm_ctx(req
->base
.tfm
);
730 mv_init_hash_req_ctx(ahash_request_ctx(req
), tfm_ctx
->op
, 1,
731 req
->nbytes
, tfm_ctx
->count_add
);
732 return mv_handle_req(&req
->base
);
735 static void mv_hash_init_ivs(struct mv_tfm_hash_ctx
*ctx
, const void *istate
,
738 const struct sha1_state
*isha1_state
= istate
, *osha1_state
= ostate
;
740 for (i
= 0; i
< 5; i
++) {
741 ctx
->ivs
[i
] = cpu_to_be32(isha1_state
->state
[i
]);
742 ctx
->ivs
[i
+ 5] = cpu_to_be32(osha1_state
->state
[i
]);
746 static int mv_hash_setkey(struct crypto_ahash
*tfm
, const u8
* key
,
750 struct mv_tfm_hash_ctx
*ctx
= crypto_tfm_ctx(&tfm
->base
);
756 rc
= crypto_shash_setkey(ctx
->fallback
, key
, keylen
);
760 /* Can't see a way to extract the ipad/opad from the fallback tfm
761 so I'm basically copying code from the hmac module */
762 bs
= crypto_shash_blocksize(ctx
->base_hash
);
763 ds
= crypto_shash_digestsize(ctx
->base_hash
);
764 ss
= crypto_shash_statesize(ctx
->base_hash
);
768 struct shash_desc shash
;
769 char ctx
[crypto_shash_descsize(ctx
->base_hash
)];
775 desc
.shash
.tfm
= ctx
->base_hash
;
776 desc
.shash
.flags
= crypto_shash_get_flags(ctx
->base_hash
) &
777 CRYPTO_TFM_REQ_MAY_SLEEP
;
783 crypto_shash_digest(&desc
.shash
, key
, keylen
, ipad
);
789 memcpy(ipad
, key
, keylen
);
791 memset(ipad
+ keylen
, 0, bs
- keylen
);
792 memcpy(opad
, ipad
, bs
);
794 for (i
= 0; i
< bs
; i
++) {
799 rc
= crypto_shash_init(&desc
.shash
) ? :
800 crypto_shash_update(&desc
.shash
, ipad
, bs
) ? :
801 crypto_shash_export(&desc
.shash
, ipad
) ? :
802 crypto_shash_init(&desc
.shash
) ? :
803 crypto_shash_update(&desc
.shash
, opad
, bs
) ? :
804 crypto_shash_export(&desc
.shash
, opad
);
807 mv_hash_init_ivs(ctx
, ipad
, opad
);
813 static int mv_cra_hash_init(struct crypto_tfm
*tfm
, const char *base_hash_name
,
814 enum hash_op op
, int count_add
)
816 const char *fallback_driver_name
= tfm
->__crt_alg
->cra_name
;
817 struct mv_tfm_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
818 struct crypto_shash
*fallback_tfm
= NULL
;
819 struct crypto_shash
*base_hash
= NULL
;
823 ctx
->count_add
= count_add
;
825 /* Allocate a fallback and abort if it failed. */
826 fallback_tfm
= crypto_alloc_shash(fallback_driver_name
, 0,
827 CRYPTO_ALG_NEED_FALLBACK
);
828 if (IS_ERR(fallback_tfm
)) {
829 printk(KERN_WARNING MV_CESA
830 "Fallback driver '%s' could not be loaded!\n",
831 fallback_driver_name
);
832 err
= PTR_ERR(fallback_tfm
);
835 ctx
->fallback
= fallback_tfm
;
837 if (base_hash_name
) {
838 /* Allocate a hash to compute the ipad/opad of hmac. */
839 base_hash
= crypto_alloc_shash(base_hash_name
, 0,
840 CRYPTO_ALG_NEED_FALLBACK
);
841 if (IS_ERR(base_hash
)) {
842 printk(KERN_WARNING MV_CESA
843 "Base driver '%s' could not be loaded!\n",
845 err
= PTR_ERR(base_hash
);
849 ctx
->base_hash
= base_hash
;
851 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
852 sizeof(struct mv_req_hash_ctx
) +
853 crypto_shash_descsize(ctx
->fallback
));
856 crypto_free_shash(fallback_tfm
);
861 static void mv_cra_hash_exit(struct crypto_tfm
*tfm
)
863 struct mv_tfm_hash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
865 crypto_free_shash(ctx
->fallback
);
867 crypto_free_shash(ctx
->base_hash
);
870 static int mv_cra_hash_sha1_init(struct crypto_tfm
*tfm
)
872 return mv_cra_hash_init(tfm
, NULL
, COP_SHA1
, 0);
875 static int mv_cra_hash_hmac_sha1_init(struct crypto_tfm
*tfm
)
877 return mv_cra_hash_init(tfm
, "sha1", COP_HMAC_SHA1
, SHA1_BLOCK_SIZE
);
880 irqreturn_t
crypto_int(int irq
, void *priv
)
884 val
= readl(cpg
->reg
+ SEC_ACCEL_INT_STATUS
);
885 if (!(val
& SEC_INT_ACCEL0_DONE
))
888 val
&= ~SEC_INT_ACCEL0_DONE
;
889 writel(val
, cpg
->reg
+ FPGA_INT_STATUS
);
890 writel(val
, cpg
->reg
+ SEC_ACCEL_INT_STATUS
);
891 BUG_ON(cpg
->eng_st
!= ENGINE_BUSY
);
892 cpg
->eng_st
= ENGINE_W_DEQUEUE
;
893 wake_up_process(cpg
->queue_th
);
897 struct crypto_alg mv_aes_alg_ecb
= {
898 .cra_name
= "ecb(aes)",
899 .cra_driver_name
= "mv-ecb-aes",
901 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
903 .cra_ctxsize
= sizeof(struct mv_ctx
),
905 .cra_type
= &crypto_ablkcipher_type
,
906 .cra_module
= THIS_MODULE
,
907 .cra_init
= mv_cra_init
,
910 .min_keysize
= AES_MIN_KEY_SIZE
,
911 .max_keysize
= AES_MAX_KEY_SIZE
,
912 .setkey
= mv_setkey_aes
,
913 .encrypt
= mv_enc_aes_ecb
,
914 .decrypt
= mv_dec_aes_ecb
,
919 struct crypto_alg mv_aes_alg_cbc
= {
920 .cra_name
= "cbc(aes)",
921 .cra_driver_name
= "mv-cbc-aes",
923 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
924 .cra_blocksize
= AES_BLOCK_SIZE
,
925 .cra_ctxsize
= sizeof(struct mv_ctx
),
927 .cra_type
= &crypto_ablkcipher_type
,
928 .cra_module
= THIS_MODULE
,
929 .cra_init
= mv_cra_init
,
932 .ivsize
= AES_BLOCK_SIZE
,
933 .min_keysize
= AES_MIN_KEY_SIZE
,
934 .max_keysize
= AES_MAX_KEY_SIZE
,
935 .setkey
= mv_setkey_aes
,
936 .encrypt
= mv_enc_aes_cbc
,
937 .decrypt
= mv_dec_aes_cbc
,
942 struct ahash_alg mv_sha1_alg
= {
943 .init
= mv_hash_init
,
944 .update
= mv_hash_update
,
945 .final
= mv_hash_final
,
946 .finup
= mv_hash_finup
,
947 .digest
= mv_hash_digest
,
949 .digestsize
= SHA1_DIGEST_SIZE
,
952 .cra_driver_name
= "mv-sha1",
955 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
956 .cra_blocksize
= SHA1_BLOCK_SIZE
,
957 .cra_ctxsize
= sizeof(struct mv_tfm_hash_ctx
),
958 .cra_init
= mv_cra_hash_sha1_init
,
959 .cra_exit
= mv_cra_hash_exit
,
960 .cra_module
= THIS_MODULE
,
965 struct ahash_alg mv_hmac_sha1_alg
= {
966 .init
= mv_hash_init
,
967 .update
= mv_hash_update
,
968 .final
= mv_hash_final
,
969 .finup
= mv_hash_finup
,
970 .digest
= mv_hash_digest
,
971 .setkey
= mv_hash_setkey
,
973 .digestsize
= SHA1_DIGEST_SIZE
,
975 .cra_name
= "hmac(sha1)",
976 .cra_driver_name
= "mv-hmac-sha1",
979 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
980 .cra_blocksize
= SHA1_BLOCK_SIZE
,
981 .cra_ctxsize
= sizeof(struct mv_tfm_hash_ctx
),
982 .cra_init
= mv_cra_hash_hmac_sha1_init
,
983 .cra_exit
= mv_cra_hash_exit
,
984 .cra_module
= THIS_MODULE
,
989 static int mv_probe(struct platform_device
*pdev
)
991 struct crypto_priv
*cp
;
992 struct resource
*res
;
997 printk(KERN_ERR MV_CESA
"Second crypto dev?\n");
1001 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
1005 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
1009 spin_lock_init(&cp
->lock
);
1010 crypto_init_queue(&cp
->queue
, 50);
1011 cp
->reg
= ioremap(res
->start
, resource_size(res
));
1017 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "sram");
1022 cp
->sram_size
= resource_size(res
);
1023 cp
->max_req_size
= cp
->sram_size
- SRAM_CFG_SPACE
;
1024 cp
->sram
= ioremap(res
->start
, cp
->sram_size
);
1030 irq
= platform_get_irq(pdev
, 0);
1031 if (irq
< 0 || irq
== NO_IRQ
) {
1033 goto err_unmap_sram
;
1037 platform_set_drvdata(pdev
, cp
);
1040 cp
->queue_th
= kthread_run(queue_manag
, cp
, "mv_crypto");
1041 if (IS_ERR(cp
->queue_th
)) {
1042 ret
= PTR_ERR(cp
->queue_th
);
1043 goto err_unmap_sram
;
1046 ret
= request_irq(irq
, crypto_int
, IRQF_DISABLED
, dev_name(&pdev
->dev
),
1051 writel(SEC_INT_ACCEL0_DONE
, cpg
->reg
+ SEC_ACCEL_INT_MASK
);
1052 writel(SEC_CFG_STOP_DIG_ERR
, cpg
->reg
+ SEC_ACCEL_CFG
);
1053 writel(SRAM_CONFIG
, cpg
->reg
+ SEC_ACCEL_DESC_P0
);
1055 ret
= crypto_register_alg(&mv_aes_alg_ecb
);
1057 printk(KERN_WARNING MV_CESA
1058 "Could not register aes-ecb driver\n");
1062 ret
= crypto_register_alg(&mv_aes_alg_cbc
);
1064 printk(KERN_WARNING MV_CESA
1065 "Could not register aes-cbc driver\n");
1069 ret
= crypto_register_ahash(&mv_sha1_alg
);
1073 printk(KERN_WARNING MV_CESA
"Could not register sha1 driver\n");
1075 ret
= crypto_register_ahash(&mv_hmac_sha1_alg
);
1077 cpg
->has_hmac_sha1
= 1;
1079 printk(KERN_WARNING MV_CESA
1080 "Could not register hmac-sha1 driver\n");
1085 crypto_unregister_alg(&mv_aes_alg_ecb
);
1089 kthread_stop(cp
->queue_th
);
1097 platform_set_drvdata(pdev
, NULL
);
1101 static int mv_remove(struct platform_device
*pdev
)
1103 struct crypto_priv
*cp
= platform_get_drvdata(pdev
);
1105 crypto_unregister_alg(&mv_aes_alg_ecb
);
1106 crypto_unregister_alg(&mv_aes_alg_cbc
);
1108 crypto_unregister_ahash(&mv_sha1_alg
);
1109 if (cp
->has_hmac_sha1
)
1110 crypto_unregister_ahash(&mv_hmac_sha1_alg
);
1111 kthread_stop(cp
->queue_th
);
1112 free_irq(cp
->irq
, cp
);
1113 memset(cp
->sram
, 0, cp
->sram_size
);
1121 static struct platform_driver marvell_crypto
= {
1123 .remove
= mv_remove
,
1125 .owner
= THIS_MODULE
,
1126 .name
= "mv_crypto",
1129 MODULE_ALIAS("platform:mv_crypto");
1131 static int __init
mv_crypto_init(void)
1133 return platform_driver_register(&marvell_crypto
);
1135 module_init(mv_crypto_init
);
1137 static void __exit
mv_crypto_exit(void)
1139 platform_driver_unregister(&marvell_crypto
);
1141 module_exit(mv_crypto_exit
);
1143 MODULE_AUTHOR("Sebastian Andrzej Siewior <sebastian@breakpoint.cc>");
1144 MODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
1145 MODULE_LICENSE("GPL");