blob: 42dea66c74bfced5fce3d6b034e6d9b3f6f1327a [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#ifndef ASR_SHA_H
2#define ASR_SHA_H
3
4#include <linux/scatterlist.h>
5#include <linux/crypto.h>
6#include <crypto/sha.h>
7#include <crypto/hash.h>
8#include <crypto/internal/hash.h>
9
10#define ASR_SHA_BUFFER_ORDER 2
11#define ASR_SHA_BUFFER_SIZE (PAGE_SIZE << ASR_SHA_BUFFER_ORDER)
12
13/* SHA flags */
14#define SHA_FLAGS_BUSY BIT(0)
15#define SHA_FLAGS_INIT BIT(1)
16#define SHA_FLAGS_FINAL BIT(2)
17#define SHA_FLAGS_FINUP BIT(3)
18#define SHA_FLAGS_OUTPUT_READY BIT(4)
19#define SHA_FLAGS_ALGO_MASK GENMASK(10, 5)
20#define SHA_FLAGS_SHA1 BIT(5)
21#define SHA_FLAGS_SHA224 BIT(6)
22#define SHA_FLAGS_SHA256 BIT(7)
23#define SHA_FLAGS_HMAC BIT(8)
24#define SHA_FLAGS_PAD BIT(9)
25#define SHA_FLAGS_ERROR BIT(10)
26
27#define SHA_OP_INIT 1
28#define SHA_OP_UPDATE 2
29#define SHA_OP_FINAL 3
30
31#define HASH_BUF_LEN 64
32
33typedef enum {
34 HASH_SHA1 = 0x0,
35 HASH_SHA224 = 0X2,
36 HASH_SHA256 = 0x3,
37} HASH_ALGO_T;
38
39struct asr_te200_sha;
40
41typedef int (*asr_sha_fn_t)(struct asr_te200_sha *);
42typedef irqreturn_t (*asr_sha_irq_t)(void *);
43
44struct asr_te200_sha {
45 unsigned long phys_base;
46 struct device *dev;
47 struct clk *iclk;
48 int irq;
49 void __iomem *io_base;
50
51 spinlock_t lock;
52 struct mutex sha_lock;
53
54 int err;
55 struct tasklet_struct done_task;
56 struct tasklet_struct queue_task;
57
58 unsigned long flags;
59 struct crypto_queue queue;
60 struct ahash_request *req;
61 bool is_async;
62 bool force_complete;
63 asr_sha_fn_t resume;
64
65 int alg;
66};
67
68typedef struct te200_hash_context {
69 volatile uint32_t count; /* Used to store the extra data count */
70 unsigned char extra_data[HASH_BUF_LEN]; /* Buffer to store the extra data*/
71 volatile uint64_t total_bits_num; /* Total process data bits number */
72 unsigned char
73 hash_temp[64 / 2]; /* Buffer to store hash temp value */
74 int alg;
75 uint32_t hash_temp_valid;
76 uint32_t finish_flag; /* This flag indicates if the context need to finish.
77 * 0 not, 1 need to do. */
78} te200_hash_context_t;
79
80/*
81 * .statesize = sizeof(struct asr_sha_reqctx) must be <= PAGE_SIZE / 8 as
82 * tested by the ahash_prepare_alg() function.
83 */
84
85struct asr_sha_reqctx {
86 struct asr_te200_sha *dd;
87 unsigned long op;
88
89 u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
90 u64 digcnt[2];
91 void *buffer;
92 size_t bufcnt;
93 size_t buflen;
94
95 /* walk state */
96 struct scatterlist *sg;
97 unsigned int offset; /* offset in current sg */
98 unsigned int total; /* total request */
99
100 int alg;
101 unsigned long flags;
102
103 te200_hash_context_t hash_ctx;
104};
105
106struct asr_sha_ctx {
107 struct asr_te200_sha *dd;
108 asr_sha_fn_t start;
109
110 unsigned long flags;
111};
112
113#define ASR_SHA_QUEUE_LENGTH 50
114
115int asr_te200_hash_init(struct asr_sha_reqctx *reqctx, int alg);
116int asr_te200_hash_proc(struct asr_sha_reqctx *reqctx, const uint8_t *src, size_t size);
117int asr_te200_hash_finish(struct asr_sha_reqctx *reqctx, uint8_t *out, uint32_t out_size);
118#endif