1108713a7SNeal Liu // SPDX-License-Identifier: GPL-2.0+
2108713a7SNeal Liu /*
3108713a7SNeal Liu * Copyright (c) 2021 Aspeed Technology Inc.
4108713a7SNeal Liu */
5108713a7SNeal Liu
6108713a7SNeal Liu #include "aspeed-hace.h"
7*304506f2SHerbert Xu #include <crypto/engine.h>
8*304506f2SHerbert Xu #include <crypto/hmac.h>
9*304506f2SHerbert Xu #include <crypto/internal/hash.h>
10*304506f2SHerbert Xu #include <crypto/scatterwalk.h>
11*304506f2SHerbert Xu #include <crypto/sha1.h>
12*304506f2SHerbert Xu #include <crypto/sha2.h>
13*304506f2SHerbert Xu #include <linux/dma-mapping.h>
14*304506f2SHerbert Xu #include <linux/err.h>
15*304506f2SHerbert Xu #include <linux/io.h>
16*304506f2SHerbert Xu #include <linux/kernel.h>
17*304506f2SHerbert Xu #include <linux/string.h>
18108713a7SNeal Liu
19108713a7SNeal Liu #ifdef CONFIG_CRYPTO_DEV_ASPEED_DEBUG
20108713a7SNeal Liu #define AHASH_DBG(h, fmt, ...) \
21108713a7SNeal Liu dev_info((h)->dev, "%s() " fmt, __func__, ##__VA_ARGS__)
22108713a7SNeal Liu #else
23108713a7SNeal Liu #define AHASH_DBG(h, fmt, ...) \
24108713a7SNeal Liu dev_dbg((h)->dev, "%s() " fmt, __func__, ##__VA_ARGS__)
25108713a7SNeal Liu #endif
26108713a7SNeal Liu
27108713a7SNeal Liu /* Initialization Vectors for SHA-family */
28108713a7SNeal Liu static const __be32 sha1_iv[8] = {
29108713a7SNeal Liu cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
30108713a7SNeal Liu cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
31108713a7SNeal Liu cpu_to_be32(SHA1_H4), 0, 0, 0
32108713a7SNeal Liu };
33108713a7SNeal Liu
34108713a7SNeal Liu static const __be32 sha224_iv[8] = {
35108713a7SNeal Liu cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
36108713a7SNeal Liu cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
37108713a7SNeal Liu cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
38108713a7SNeal Liu cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
39108713a7SNeal Liu };
40108713a7SNeal Liu
41108713a7SNeal Liu static const __be32 sha256_iv[8] = {
42108713a7SNeal Liu cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
43108713a7SNeal Liu cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
44108713a7SNeal Liu cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
45108713a7SNeal Liu cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
46108713a7SNeal Liu };
47108713a7SNeal Liu
48108713a7SNeal Liu static const __be64 sha384_iv[8] = {
49108713a7SNeal Liu cpu_to_be64(SHA384_H0), cpu_to_be64(SHA384_H1),
50108713a7SNeal Liu cpu_to_be64(SHA384_H2), cpu_to_be64(SHA384_H3),
51108713a7SNeal Liu cpu_to_be64(SHA384_H4), cpu_to_be64(SHA384_H5),
52108713a7SNeal Liu cpu_to_be64(SHA384_H6), cpu_to_be64(SHA384_H7)
53108713a7SNeal Liu };
54108713a7SNeal Liu
55108713a7SNeal Liu static const __be64 sha512_iv[8] = {
56108713a7SNeal Liu cpu_to_be64(SHA512_H0), cpu_to_be64(SHA512_H1),
57108713a7SNeal Liu cpu_to_be64(SHA512_H2), cpu_to_be64(SHA512_H3),
58108713a7SNeal Liu cpu_to_be64(SHA512_H4), cpu_to_be64(SHA512_H5),
59108713a7SNeal Liu cpu_to_be64(SHA512_H6), cpu_to_be64(SHA512_H7)
60108713a7SNeal Liu };
61108713a7SNeal Liu
62108713a7SNeal Liu /* The purpose of this padding is to ensure that the padded message is a
63108713a7SNeal Liu * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
64108713a7SNeal Liu * The bit "1" is appended at the end of the message followed by
65108713a7SNeal Liu * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
66108713a7SNeal Liu * 128 bits block (SHA384/SHA512) equals to the message length in bits
67108713a7SNeal Liu * is appended.
68108713a7SNeal Liu *
69108713a7SNeal Liu * For SHA1/SHA224/SHA256, padlen is calculated as followed:
70108713a7SNeal Liu * - if message length < 56 bytes then padlen = 56 - message length
71108713a7SNeal Liu * - else padlen = 64 + 56 - message length
72108713a7SNeal Liu *
73108713a7SNeal Liu * For SHA384/SHA512, padlen is calculated as followed:
74108713a7SNeal Liu * - if message length < 112 bytes then padlen = 112 - message length
75108713a7SNeal Liu * - else padlen = 128 + 112 - message length
76108713a7SNeal Liu */
aspeed_ahash_fill_padding(struct aspeed_hace_dev * hace_dev,struct aspeed_sham_reqctx * rctx)77108713a7SNeal Liu static void aspeed_ahash_fill_padding(struct aspeed_hace_dev *hace_dev,
78108713a7SNeal Liu struct aspeed_sham_reqctx *rctx)
79108713a7SNeal Liu {
80108713a7SNeal Liu unsigned int index, padlen;
81108713a7SNeal Liu __be64 bits[2];
82108713a7SNeal Liu
83108713a7SNeal Liu AHASH_DBG(hace_dev, "rctx flags:0x%x\n", (u32)rctx->flags);
84108713a7SNeal Liu
85108713a7SNeal Liu switch (rctx->flags & SHA_FLAGS_MASK) {
86108713a7SNeal Liu case SHA_FLAGS_SHA1:
87108713a7SNeal Liu case SHA_FLAGS_SHA224:
88108713a7SNeal Liu case SHA_FLAGS_SHA256:
89108713a7SNeal Liu bits[0] = cpu_to_be64(rctx->digcnt[0] << 3);
90108713a7SNeal Liu index = rctx->bufcnt & 0x3f;
91108713a7SNeal Liu padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
92108713a7SNeal Liu *(rctx->buffer + rctx->bufcnt) = 0x80;
93108713a7SNeal Liu memset(rctx->buffer + rctx->bufcnt + 1, 0, padlen - 1);
94108713a7SNeal Liu memcpy(rctx->buffer + rctx->bufcnt + padlen, bits, 8);
95108713a7SNeal Liu rctx->bufcnt += padlen + 8;
96108713a7SNeal Liu break;
97108713a7SNeal Liu default:
98108713a7SNeal Liu bits[1] = cpu_to_be64(rctx->digcnt[0] << 3);
99108713a7SNeal Liu bits[0] = cpu_to_be64(rctx->digcnt[1] << 3 |
100108713a7SNeal Liu rctx->digcnt[0] >> 61);
101108713a7SNeal Liu index = rctx->bufcnt & 0x7f;
102108713a7SNeal Liu padlen = (index < 112) ? (112 - index) : ((128 + 112) - index);
103108713a7SNeal Liu *(rctx->buffer + rctx->bufcnt) = 0x80;
104108713a7SNeal Liu memset(rctx->buffer + rctx->bufcnt + 1, 0, padlen - 1);
105108713a7SNeal Liu memcpy(rctx->buffer + rctx->bufcnt + padlen, bits, 16);
106108713a7SNeal Liu rctx->bufcnt += padlen + 16;
107108713a7SNeal Liu break;
108108713a7SNeal Liu }
109108713a7SNeal Liu }
110108713a7SNeal Liu
111108713a7SNeal Liu /*
112108713a7SNeal Liu * Prepare DMA buffer before hardware engine
113108713a7SNeal Liu * processing.
114108713a7SNeal Liu */
aspeed_ahash_dma_prepare(struct aspeed_hace_dev * hace_dev)115108713a7SNeal Liu static int aspeed_ahash_dma_prepare(struct aspeed_hace_dev *hace_dev)
116108713a7SNeal Liu {
117108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
118108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
119108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
120108713a7SNeal Liu int length, remain;
121108713a7SNeal Liu
122108713a7SNeal Liu length = rctx->total + rctx->bufcnt;
123108713a7SNeal Liu remain = length % rctx->block_size;
124108713a7SNeal Liu
125108713a7SNeal Liu AHASH_DBG(hace_dev, "length:0x%x, remain:0x%x\n", length, remain);
126108713a7SNeal Liu
127108713a7SNeal Liu if (rctx->bufcnt)
128108713a7SNeal Liu memcpy(hash_engine->ahash_src_addr, rctx->buffer, rctx->bufcnt);
129108713a7SNeal Liu
130108713a7SNeal Liu if (rctx->total + rctx->bufcnt < ASPEED_CRYPTO_SRC_DMA_BUF_LEN) {
131108713a7SNeal Liu scatterwalk_map_and_copy(hash_engine->ahash_src_addr +
132108713a7SNeal Liu rctx->bufcnt, rctx->src_sg,
133108713a7SNeal Liu rctx->offset, rctx->total - remain, 0);
134108713a7SNeal Liu rctx->offset += rctx->total - remain;
135108713a7SNeal Liu
136108713a7SNeal Liu } else {
137108713a7SNeal Liu dev_warn(hace_dev->dev, "Hash data length is too large\n");
138108713a7SNeal Liu return -EINVAL;
139108713a7SNeal Liu }
140108713a7SNeal Liu
141108713a7SNeal Liu scatterwalk_map_and_copy(rctx->buffer, rctx->src_sg,
142108713a7SNeal Liu rctx->offset, remain, 0);
143108713a7SNeal Liu
144108713a7SNeal Liu rctx->bufcnt = remain;
145108713a7SNeal Liu rctx->digest_dma_addr = dma_map_single(hace_dev->dev, rctx->digest,
146108713a7SNeal Liu SHA512_DIGEST_SIZE,
147108713a7SNeal Liu DMA_BIDIRECTIONAL);
148108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->digest_dma_addr)) {
149108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx digest error\n");
150108713a7SNeal Liu return -ENOMEM;
151108713a7SNeal Liu }
152108713a7SNeal Liu
153108713a7SNeal Liu hash_engine->src_length = length - remain;
154108713a7SNeal Liu hash_engine->src_dma = hash_engine->ahash_src_dma_addr;
155108713a7SNeal Liu hash_engine->digest_dma = rctx->digest_dma_addr;
156108713a7SNeal Liu
157108713a7SNeal Liu return 0;
158108713a7SNeal Liu }
159108713a7SNeal Liu
160108713a7SNeal Liu /*
161108713a7SNeal Liu * Prepare DMA buffer as SG list buffer before
162108713a7SNeal Liu * hardware engine processing.
163108713a7SNeal Liu */
aspeed_ahash_dma_prepare_sg(struct aspeed_hace_dev * hace_dev)164108713a7SNeal Liu static int aspeed_ahash_dma_prepare_sg(struct aspeed_hace_dev *hace_dev)
165108713a7SNeal Liu {
166108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
167108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
168108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
169108713a7SNeal Liu struct aspeed_sg_list *src_list;
170108713a7SNeal Liu struct scatterlist *s;
171108713a7SNeal Liu int length, remain, sg_len, i;
172108713a7SNeal Liu int rc = 0;
173108713a7SNeal Liu
174108713a7SNeal Liu remain = (rctx->total + rctx->bufcnt) % rctx->block_size;
175108713a7SNeal Liu length = rctx->total + rctx->bufcnt - remain;
176108713a7SNeal Liu
177aa450316SNeal Liu AHASH_DBG(hace_dev, "%s:0x%x, %s:%zu, %s:0x%x, %s:0x%x\n",
178108713a7SNeal Liu "rctx total", rctx->total, "bufcnt", rctx->bufcnt,
179108713a7SNeal Liu "length", length, "remain", remain);
180108713a7SNeal Liu
181108713a7SNeal Liu sg_len = dma_map_sg(hace_dev->dev, rctx->src_sg, rctx->src_nents,
182108713a7SNeal Liu DMA_TO_DEVICE);
183108713a7SNeal Liu if (!sg_len) {
184108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map_sg() src error\n");
185108713a7SNeal Liu rc = -ENOMEM;
186108713a7SNeal Liu goto end;
187108713a7SNeal Liu }
188108713a7SNeal Liu
189108713a7SNeal Liu src_list = (struct aspeed_sg_list *)hash_engine->ahash_src_addr;
190108713a7SNeal Liu rctx->digest_dma_addr = dma_map_single(hace_dev->dev, rctx->digest,
191108713a7SNeal Liu SHA512_DIGEST_SIZE,
192108713a7SNeal Liu DMA_BIDIRECTIONAL);
193108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->digest_dma_addr)) {
194108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx digest error\n");
195108713a7SNeal Liu rc = -ENOMEM;
196108713a7SNeal Liu goto free_src_sg;
197108713a7SNeal Liu }
198108713a7SNeal Liu
199108713a7SNeal Liu if (rctx->bufcnt != 0) {
200efc96d43SHerbert Xu u32 phy_addr;
201efc96d43SHerbert Xu u32 len;
202efc96d43SHerbert Xu
203108713a7SNeal Liu rctx->buffer_dma_addr = dma_map_single(hace_dev->dev,
204108713a7SNeal Liu rctx->buffer,
205108713a7SNeal Liu rctx->block_size * 2,
206108713a7SNeal Liu DMA_TO_DEVICE);
207108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->buffer_dma_addr)) {
208108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx buffer error\n");
209108713a7SNeal Liu rc = -ENOMEM;
210108713a7SNeal Liu goto free_rctx_digest;
211108713a7SNeal Liu }
212108713a7SNeal Liu
213efc96d43SHerbert Xu phy_addr = rctx->buffer_dma_addr;
214efc96d43SHerbert Xu len = rctx->bufcnt;
215efc96d43SHerbert Xu length -= len;
216108713a7SNeal Liu
217108713a7SNeal Liu /* Last sg list */
218108713a7SNeal Liu if (length == 0)
219efc96d43SHerbert Xu len |= HASH_SG_LAST_LIST;
220108713a7SNeal Liu
221efc96d43SHerbert Xu src_list[0].phy_addr = cpu_to_le32(phy_addr);
222efc96d43SHerbert Xu src_list[0].len = cpu_to_le32(len);
223108713a7SNeal Liu src_list++;
224108713a7SNeal Liu }
225108713a7SNeal Liu
226108713a7SNeal Liu if (length != 0) {
227108713a7SNeal Liu for_each_sg(rctx->src_sg, s, sg_len, i) {
228efc96d43SHerbert Xu u32 phy_addr = sg_dma_address(s);
229efc96d43SHerbert Xu u32 len = sg_dma_len(s);
230108713a7SNeal Liu
231efc96d43SHerbert Xu if (length > len)
232efc96d43SHerbert Xu length -= len;
233efc96d43SHerbert Xu else {
234108713a7SNeal Liu /* Last sg list */
235efc96d43SHerbert Xu len = length;
236efc96d43SHerbert Xu len |= HASH_SG_LAST_LIST;
237108713a7SNeal Liu length = 0;
238108713a7SNeal Liu }
239108713a7SNeal Liu
240efc96d43SHerbert Xu src_list[i].phy_addr = cpu_to_le32(phy_addr);
241efc96d43SHerbert Xu src_list[i].len = cpu_to_le32(len);
242108713a7SNeal Liu }
243108713a7SNeal Liu }
244108713a7SNeal Liu
245108713a7SNeal Liu if (length != 0) {
246108713a7SNeal Liu rc = -EINVAL;
247108713a7SNeal Liu goto free_rctx_buffer;
248108713a7SNeal Liu }
249108713a7SNeal Liu
250108713a7SNeal Liu rctx->offset = rctx->total - remain;
251108713a7SNeal Liu hash_engine->src_length = rctx->total + rctx->bufcnt - remain;
252108713a7SNeal Liu hash_engine->src_dma = hash_engine->ahash_src_dma_addr;
253108713a7SNeal Liu hash_engine->digest_dma = rctx->digest_dma_addr;
254108713a7SNeal Liu
255108713a7SNeal Liu return 0;
256108713a7SNeal Liu
257108713a7SNeal Liu free_rctx_buffer:
258108713a7SNeal Liu if (rctx->bufcnt != 0)
259108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->buffer_dma_addr,
260108713a7SNeal Liu rctx->block_size * 2, DMA_TO_DEVICE);
261108713a7SNeal Liu free_rctx_digest:
262108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
263108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
264108713a7SNeal Liu free_src_sg:
265108713a7SNeal Liu dma_unmap_sg(hace_dev->dev, rctx->src_sg, rctx->src_nents,
266108713a7SNeal Liu DMA_TO_DEVICE);
267108713a7SNeal Liu end:
268108713a7SNeal Liu return rc;
269108713a7SNeal Liu }
270108713a7SNeal Liu
aspeed_ahash_complete(struct aspeed_hace_dev * hace_dev)271108713a7SNeal Liu static int aspeed_ahash_complete(struct aspeed_hace_dev *hace_dev)
272108713a7SNeal Liu {
273108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
274108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
275108713a7SNeal Liu
276108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
277108713a7SNeal Liu
278108713a7SNeal Liu hash_engine->flags &= ~CRYPTO_FLAGS_BUSY;
279108713a7SNeal Liu
280108713a7SNeal Liu crypto_finalize_hash_request(hace_dev->crypt_engine_hash, req, 0);
281108713a7SNeal Liu
282108713a7SNeal Liu return 0;
283108713a7SNeal Liu }
284108713a7SNeal Liu
285108713a7SNeal Liu /*
286108713a7SNeal Liu * Copy digest to the corresponding request result.
287108713a7SNeal Liu * This function will be called at final() stage.
288108713a7SNeal Liu */
aspeed_ahash_transfer(struct aspeed_hace_dev * hace_dev)289108713a7SNeal Liu static int aspeed_ahash_transfer(struct aspeed_hace_dev *hace_dev)
290108713a7SNeal Liu {
291108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
292108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
293108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
294108713a7SNeal Liu
295108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
296108713a7SNeal Liu
297108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
298108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
299108713a7SNeal Liu
300108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->buffer_dma_addr,
301108713a7SNeal Liu rctx->block_size * 2, DMA_TO_DEVICE);
302108713a7SNeal Liu
303108713a7SNeal Liu memcpy(req->result, rctx->digest, rctx->digsize);
304108713a7SNeal Liu
305108713a7SNeal Liu return aspeed_ahash_complete(hace_dev);
306108713a7SNeal Liu }
307108713a7SNeal Liu
308108713a7SNeal Liu /*
309108713a7SNeal Liu * Trigger hardware engines to do the math.
310108713a7SNeal Liu */
aspeed_hace_ahash_trigger(struct aspeed_hace_dev * hace_dev,aspeed_hace_fn_t resume)311108713a7SNeal Liu static int aspeed_hace_ahash_trigger(struct aspeed_hace_dev *hace_dev,
312108713a7SNeal Liu aspeed_hace_fn_t resume)
313108713a7SNeal Liu {
314108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
315108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
316108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
317108713a7SNeal Liu
318aa450316SNeal Liu AHASH_DBG(hace_dev, "src_dma:%pad, digest_dma:%pad, length:%zu\n",
319aa450316SNeal Liu &hash_engine->src_dma, &hash_engine->digest_dma,
320108713a7SNeal Liu hash_engine->src_length);
321108713a7SNeal Liu
322108713a7SNeal Liu rctx->cmd |= HASH_CMD_INT_ENABLE;
323108713a7SNeal Liu hash_engine->resume = resume;
324108713a7SNeal Liu
325108713a7SNeal Liu ast_hace_write(hace_dev, hash_engine->src_dma, ASPEED_HACE_HASH_SRC);
326108713a7SNeal Liu ast_hace_write(hace_dev, hash_engine->digest_dma,
327108713a7SNeal Liu ASPEED_HACE_HASH_DIGEST_BUFF);
328108713a7SNeal Liu ast_hace_write(hace_dev, hash_engine->digest_dma,
329108713a7SNeal Liu ASPEED_HACE_HASH_KEY_BUFF);
330108713a7SNeal Liu ast_hace_write(hace_dev, hash_engine->src_length,
331108713a7SNeal Liu ASPEED_HACE_HASH_DATA_LEN);
332108713a7SNeal Liu
333108713a7SNeal Liu /* Memory barrier to ensure all data setup before engine starts */
334108713a7SNeal Liu mb();
335108713a7SNeal Liu
336108713a7SNeal Liu ast_hace_write(hace_dev, rctx->cmd, ASPEED_HACE_HASH_CMD);
337108713a7SNeal Liu
338108713a7SNeal Liu return -EINPROGRESS;
339108713a7SNeal Liu }
340108713a7SNeal Liu
341108713a7SNeal Liu /*
342108713a7SNeal Liu * HMAC resume aims to do the second pass produces
343108713a7SNeal Liu * the final HMAC code derived from the inner hash
344108713a7SNeal Liu * result and the outer key.
345108713a7SNeal Liu */
aspeed_ahash_hmac_resume(struct aspeed_hace_dev * hace_dev)346108713a7SNeal Liu static int aspeed_ahash_hmac_resume(struct aspeed_hace_dev *hace_dev)
347108713a7SNeal Liu {
348108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
349108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
350108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
351108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
352108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
353108713a7SNeal Liu struct aspeed_sha_hmac_ctx *bctx = tctx->base;
354108713a7SNeal Liu int rc = 0;
355108713a7SNeal Liu
356108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
357108713a7SNeal Liu
358108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
359108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
360108713a7SNeal Liu
361108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->buffer_dma_addr,
362108713a7SNeal Liu rctx->block_size * 2, DMA_TO_DEVICE);
363108713a7SNeal Liu
364108713a7SNeal Liu /* o key pad + hash sum 1 */
365108713a7SNeal Liu memcpy(rctx->buffer, bctx->opad, rctx->block_size);
366108713a7SNeal Liu memcpy(rctx->buffer + rctx->block_size, rctx->digest, rctx->digsize);
367108713a7SNeal Liu
368108713a7SNeal Liu rctx->bufcnt = rctx->block_size + rctx->digsize;
369108713a7SNeal Liu rctx->digcnt[0] = rctx->block_size + rctx->digsize;
370108713a7SNeal Liu
371108713a7SNeal Liu aspeed_ahash_fill_padding(hace_dev, rctx);
372108713a7SNeal Liu memcpy(rctx->digest, rctx->sha_iv, rctx->ivsize);
373108713a7SNeal Liu
374108713a7SNeal Liu rctx->digest_dma_addr = dma_map_single(hace_dev->dev, rctx->digest,
375108713a7SNeal Liu SHA512_DIGEST_SIZE,
376108713a7SNeal Liu DMA_BIDIRECTIONAL);
377108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->digest_dma_addr)) {
378108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx digest error\n");
379108713a7SNeal Liu rc = -ENOMEM;
380108713a7SNeal Liu goto end;
381108713a7SNeal Liu }
382108713a7SNeal Liu
383108713a7SNeal Liu rctx->buffer_dma_addr = dma_map_single(hace_dev->dev, rctx->buffer,
384108713a7SNeal Liu rctx->block_size * 2,
385108713a7SNeal Liu DMA_TO_DEVICE);
386108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->buffer_dma_addr)) {
387108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx buffer error\n");
388108713a7SNeal Liu rc = -ENOMEM;
389108713a7SNeal Liu goto free_rctx_digest;
390108713a7SNeal Liu }
391108713a7SNeal Liu
392108713a7SNeal Liu hash_engine->src_dma = rctx->buffer_dma_addr;
393108713a7SNeal Liu hash_engine->src_length = rctx->bufcnt;
394108713a7SNeal Liu hash_engine->digest_dma = rctx->digest_dma_addr;
395108713a7SNeal Liu
396108713a7SNeal Liu return aspeed_hace_ahash_trigger(hace_dev, aspeed_ahash_transfer);
397108713a7SNeal Liu
398108713a7SNeal Liu free_rctx_digest:
399108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
400108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
401108713a7SNeal Liu end:
402108713a7SNeal Liu return rc;
403108713a7SNeal Liu }
404108713a7SNeal Liu
aspeed_ahash_req_final(struct aspeed_hace_dev * hace_dev)405108713a7SNeal Liu static int aspeed_ahash_req_final(struct aspeed_hace_dev *hace_dev)
406108713a7SNeal Liu {
407108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
408108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
409108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
410108713a7SNeal Liu int rc = 0;
411108713a7SNeal Liu
412108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
413108713a7SNeal Liu
414108713a7SNeal Liu aspeed_ahash_fill_padding(hace_dev, rctx);
415108713a7SNeal Liu
416108713a7SNeal Liu rctx->digest_dma_addr = dma_map_single(hace_dev->dev,
417108713a7SNeal Liu rctx->digest,
418108713a7SNeal Liu SHA512_DIGEST_SIZE,
419108713a7SNeal Liu DMA_BIDIRECTIONAL);
420108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->digest_dma_addr)) {
421108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx digest error\n");
422108713a7SNeal Liu rc = -ENOMEM;
423108713a7SNeal Liu goto end;
424108713a7SNeal Liu }
425108713a7SNeal Liu
426108713a7SNeal Liu rctx->buffer_dma_addr = dma_map_single(hace_dev->dev,
427108713a7SNeal Liu rctx->buffer,
428108713a7SNeal Liu rctx->block_size * 2,
429108713a7SNeal Liu DMA_TO_DEVICE);
430108713a7SNeal Liu if (dma_mapping_error(hace_dev->dev, rctx->buffer_dma_addr)) {
431108713a7SNeal Liu dev_warn(hace_dev->dev, "dma_map() rctx buffer error\n");
432108713a7SNeal Liu rc = -ENOMEM;
433108713a7SNeal Liu goto free_rctx_digest;
434108713a7SNeal Liu }
435108713a7SNeal Liu
436108713a7SNeal Liu hash_engine->src_dma = rctx->buffer_dma_addr;
437108713a7SNeal Liu hash_engine->src_length = rctx->bufcnt;
438108713a7SNeal Liu hash_engine->digest_dma = rctx->digest_dma_addr;
439108713a7SNeal Liu
440108713a7SNeal Liu if (rctx->flags & SHA_FLAGS_HMAC)
441108713a7SNeal Liu return aspeed_hace_ahash_trigger(hace_dev,
442108713a7SNeal Liu aspeed_ahash_hmac_resume);
443108713a7SNeal Liu
444108713a7SNeal Liu return aspeed_hace_ahash_trigger(hace_dev, aspeed_ahash_transfer);
445108713a7SNeal Liu
446108713a7SNeal Liu free_rctx_digest:
447108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
448108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
449108713a7SNeal Liu end:
450108713a7SNeal Liu return rc;
451108713a7SNeal Liu }
452108713a7SNeal Liu
aspeed_ahash_update_resume_sg(struct aspeed_hace_dev * hace_dev)453108713a7SNeal Liu static int aspeed_ahash_update_resume_sg(struct aspeed_hace_dev *hace_dev)
454108713a7SNeal Liu {
455108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
456108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
457108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
458108713a7SNeal Liu
459108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
460108713a7SNeal Liu
461108713a7SNeal Liu dma_unmap_sg(hace_dev->dev, rctx->src_sg, rctx->src_nents,
462108713a7SNeal Liu DMA_TO_DEVICE);
463108713a7SNeal Liu
464108713a7SNeal Liu if (rctx->bufcnt != 0)
465108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->buffer_dma_addr,
466108713a7SNeal Liu rctx->block_size * 2,
467108713a7SNeal Liu DMA_TO_DEVICE);
468108713a7SNeal Liu
469108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
470108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
471108713a7SNeal Liu
472108713a7SNeal Liu scatterwalk_map_and_copy(rctx->buffer, rctx->src_sg, rctx->offset,
473108713a7SNeal Liu rctx->total - rctx->offset, 0);
474108713a7SNeal Liu
475108713a7SNeal Liu rctx->bufcnt = rctx->total - rctx->offset;
476108713a7SNeal Liu rctx->cmd &= ~HASH_CMD_HASH_SRC_SG_CTRL;
477108713a7SNeal Liu
478108713a7SNeal Liu if (rctx->flags & SHA_FLAGS_FINUP)
479108713a7SNeal Liu return aspeed_ahash_req_final(hace_dev);
480108713a7SNeal Liu
481108713a7SNeal Liu return aspeed_ahash_complete(hace_dev);
482108713a7SNeal Liu }
483108713a7SNeal Liu
aspeed_ahash_update_resume(struct aspeed_hace_dev * hace_dev)484108713a7SNeal Liu static int aspeed_ahash_update_resume(struct aspeed_hace_dev *hace_dev)
485108713a7SNeal Liu {
486108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
487108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
488108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
489108713a7SNeal Liu
490108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
491108713a7SNeal Liu
492108713a7SNeal Liu dma_unmap_single(hace_dev->dev, rctx->digest_dma_addr,
493108713a7SNeal Liu SHA512_DIGEST_SIZE, DMA_BIDIRECTIONAL);
494108713a7SNeal Liu
495108713a7SNeal Liu if (rctx->flags & SHA_FLAGS_FINUP)
496108713a7SNeal Liu return aspeed_ahash_req_final(hace_dev);
497108713a7SNeal Liu
498108713a7SNeal Liu return aspeed_ahash_complete(hace_dev);
499108713a7SNeal Liu }
500108713a7SNeal Liu
aspeed_ahash_req_update(struct aspeed_hace_dev * hace_dev)501108713a7SNeal Liu static int aspeed_ahash_req_update(struct aspeed_hace_dev *hace_dev)
502108713a7SNeal Liu {
503108713a7SNeal Liu struct aspeed_engine_hash *hash_engine = &hace_dev->hash_engine;
504108713a7SNeal Liu struct ahash_request *req = hash_engine->req;
505108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
506108713a7SNeal Liu aspeed_hace_fn_t resume;
507108713a7SNeal Liu int ret;
508108713a7SNeal Liu
509108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
510108713a7SNeal Liu
511108713a7SNeal Liu if (hace_dev->version == AST2600_VERSION) {
512108713a7SNeal Liu rctx->cmd |= HASH_CMD_HASH_SRC_SG_CTRL;
513108713a7SNeal Liu resume = aspeed_ahash_update_resume_sg;
514108713a7SNeal Liu
515108713a7SNeal Liu } else {
516108713a7SNeal Liu resume = aspeed_ahash_update_resume;
517108713a7SNeal Liu }
518108713a7SNeal Liu
519108713a7SNeal Liu ret = hash_engine->dma_prepare(hace_dev);
520108713a7SNeal Liu if (ret)
521108713a7SNeal Liu return ret;
522108713a7SNeal Liu
523108713a7SNeal Liu return aspeed_hace_ahash_trigger(hace_dev, resume);
524108713a7SNeal Liu }
525108713a7SNeal Liu
aspeed_hace_hash_handle_queue(struct aspeed_hace_dev * hace_dev,struct ahash_request * req)526108713a7SNeal Liu static int aspeed_hace_hash_handle_queue(struct aspeed_hace_dev *hace_dev,
527108713a7SNeal Liu struct ahash_request *req)
528108713a7SNeal Liu {
529108713a7SNeal Liu return crypto_transfer_hash_request_to_engine(
530108713a7SNeal Liu hace_dev->crypt_engine_hash, req);
531108713a7SNeal Liu }
532108713a7SNeal Liu
aspeed_ahash_do_request(struct crypto_engine * engine,void * areq)533108713a7SNeal Liu static int aspeed_ahash_do_request(struct crypto_engine *engine, void *areq)
534108713a7SNeal Liu {
535108713a7SNeal Liu struct ahash_request *req = ahash_request_cast(areq);
536108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
537108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
538108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
539108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
540108713a7SNeal Liu struct aspeed_engine_hash *hash_engine;
541108713a7SNeal Liu int ret = 0;
542108713a7SNeal Liu
543108713a7SNeal Liu hash_engine = &hace_dev->hash_engine;
544108713a7SNeal Liu hash_engine->flags |= CRYPTO_FLAGS_BUSY;
545108713a7SNeal Liu
546108713a7SNeal Liu if (rctx->op == SHA_OP_UPDATE)
547108713a7SNeal Liu ret = aspeed_ahash_req_update(hace_dev);
548108713a7SNeal Liu else if (rctx->op == SHA_OP_FINAL)
549108713a7SNeal Liu ret = aspeed_ahash_req_final(hace_dev);
550108713a7SNeal Liu
551108713a7SNeal Liu if (ret != -EINPROGRESS)
552108713a7SNeal Liu return ret;
553108713a7SNeal Liu
554108713a7SNeal Liu return 0;
555108713a7SNeal Liu }
556108713a7SNeal Liu
aspeed_ahash_prepare_request(struct crypto_engine * engine,void * areq)55713bba5b5SHerbert Xu static void aspeed_ahash_prepare_request(struct crypto_engine *engine,
558108713a7SNeal Liu void *areq)
559108713a7SNeal Liu {
560108713a7SNeal Liu struct ahash_request *req = ahash_request_cast(areq);
561108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
562108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
563108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
564108713a7SNeal Liu struct aspeed_engine_hash *hash_engine;
565108713a7SNeal Liu
566108713a7SNeal Liu hash_engine = &hace_dev->hash_engine;
567108713a7SNeal Liu hash_engine->req = req;
568108713a7SNeal Liu
569108713a7SNeal Liu if (hace_dev->version == AST2600_VERSION)
570108713a7SNeal Liu hash_engine->dma_prepare = aspeed_ahash_dma_prepare_sg;
571108713a7SNeal Liu else
572108713a7SNeal Liu hash_engine->dma_prepare = aspeed_ahash_dma_prepare;
57313bba5b5SHerbert Xu }
574108713a7SNeal Liu
aspeed_ahash_do_one(struct crypto_engine * engine,void * areq)57513bba5b5SHerbert Xu static int aspeed_ahash_do_one(struct crypto_engine *engine, void *areq)
57613bba5b5SHerbert Xu {
57713bba5b5SHerbert Xu aspeed_ahash_prepare_request(engine, areq);
57813bba5b5SHerbert Xu return aspeed_ahash_do_request(engine, areq);
579108713a7SNeal Liu }
580108713a7SNeal Liu
aspeed_sham_update(struct ahash_request * req)581108713a7SNeal Liu static int aspeed_sham_update(struct ahash_request *req)
582108713a7SNeal Liu {
583108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
584108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
585108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
586108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
587108713a7SNeal Liu
588108713a7SNeal Liu AHASH_DBG(hace_dev, "req->nbytes: %d\n", req->nbytes);
589108713a7SNeal Liu
590108713a7SNeal Liu rctx->total = req->nbytes;
591108713a7SNeal Liu rctx->src_sg = req->src;
592108713a7SNeal Liu rctx->offset = 0;
593108713a7SNeal Liu rctx->src_nents = sg_nents(req->src);
594108713a7SNeal Liu rctx->op = SHA_OP_UPDATE;
595108713a7SNeal Liu
596108713a7SNeal Liu rctx->digcnt[0] += rctx->total;
597108713a7SNeal Liu if (rctx->digcnt[0] < rctx->total)
598108713a7SNeal Liu rctx->digcnt[1]++;
599108713a7SNeal Liu
600108713a7SNeal Liu if (rctx->bufcnt + rctx->total < rctx->block_size) {
601108713a7SNeal Liu scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt,
602108713a7SNeal Liu rctx->src_sg, rctx->offset,
603108713a7SNeal Liu rctx->total, 0);
604108713a7SNeal Liu rctx->bufcnt += rctx->total;
605108713a7SNeal Liu
606108713a7SNeal Liu return 0;
607108713a7SNeal Liu }
608108713a7SNeal Liu
609108713a7SNeal Liu return aspeed_hace_hash_handle_queue(hace_dev, req);
610108713a7SNeal Liu }
611108713a7SNeal Liu
aspeed_sham_shash_digest(struct crypto_shash * tfm,u32 flags,const u8 * data,unsigned int len,u8 * out)612108713a7SNeal Liu static int aspeed_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
613108713a7SNeal Liu const u8 *data, unsigned int len, u8 *out)
614108713a7SNeal Liu {
615108713a7SNeal Liu SHASH_DESC_ON_STACK(shash, tfm);
616108713a7SNeal Liu
617108713a7SNeal Liu shash->tfm = tfm;
618108713a7SNeal Liu
619108713a7SNeal Liu return crypto_shash_digest(shash, data, len, out);
620108713a7SNeal Liu }
621108713a7SNeal Liu
aspeed_sham_final(struct ahash_request * req)622108713a7SNeal Liu static int aspeed_sham_final(struct ahash_request *req)
623108713a7SNeal Liu {
624108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
625108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
626108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
627108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
628108713a7SNeal Liu
629108713a7SNeal Liu AHASH_DBG(hace_dev, "req->nbytes:%d, rctx->total:%d\n",
630108713a7SNeal Liu req->nbytes, rctx->total);
631108713a7SNeal Liu rctx->op = SHA_OP_FINAL;
632108713a7SNeal Liu
633108713a7SNeal Liu return aspeed_hace_hash_handle_queue(hace_dev, req);
634108713a7SNeal Liu }
635108713a7SNeal Liu
aspeed_sham_finup(struct ahash_request * req)636108713a7SNeal Liu static int aspeed_sham_finup(struct ahash_request *req)
637108713a7SNeal Liu {
638108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
639108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
640108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
641108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
642108713a7SNeal Liu int rc1, rc2;
643108713a7SNeal Liu
644108713a7SNeal Liu AHASH_DBG(hace_dev, "req->nbytes: %d\n", req->nbytes);
645108713a7SNeal Liu
646108713a7SNeal Liu rctx->flags |= SHA_FLAGS_FINUP;
647108713a7SNeal Liu
648108713a7SNeal Liu rc1 = aspeed_sham_update(req);
649108713a7SNeal Liu if (rc1 == -EINPROGRESS || rc1 == -EBUSY)
650108713a7SNeal Liu return rc1;
651108713a7SNeal Liu
652108713a7SNeal Liu /*
653108713a7SNeal Liu * final() has to be always called to cleanup resources
654108713a7SNeal Liu * even if update() failed, except EINPROGRESS
655108713a7SNeal Liu */
656108713a7SNeal Liu rc2 = aspeed_sham_final(req);
657108713a7SNeal Liu
658108713a7SNeal Liu return rc1 ? : rc2;
659108713a7SNeal Liu }
660108713a7SNeal Liu
aspeed_sham_init(struct ahash_request * req)661108713a7SNeal Liu static int aspeed_sham_init(struct ahash_request *req)
662108713a7SNeal Liu {
663108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
664108713a7SNeal Liu struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
665108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
666108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
667108713a7SNeal Liu struct aspeed_sha_hmac_ctx *bctx = tctx->base;
668108713a7SNeal Liu
669108713a7SNeal Liu AHASH_DBG(hace_dev, "%s: digest size:%d\n",
670108713a7SNeal Liu crypto_tfm_alg_name(&tfm->base),
671108713a7SNeal Liu crypto_ahash_digestsize(tfm));
672108713a7SNeal Liu
673108713a7SNeal Liu rctx->cmd = HASH_CMD_ACC_MODE;
674108713a7SNeal Liu rctx->flags = 0;
675108713a7SNeal Liu
676108713a7SNeal Liu switch (crypto_ahash_digestsize(tfm)) {
677108713a7SNeal Liu case SHA1_DIGEST_SIZE:
678108713a7SNeal Liu rctx->cmd |= HASH_CMD_SHA1 | HASH_CMD_SHA_SWAP;
679108713a7SNeal Liu rctx->flags |= SHA_FLAGS_SHA1;
680108713a7SNeal Liu rctx->digsize = SHA1_DIGEST_SIZE;
681108713a7SNeal Liu rctx->block_size = SHA1_BLOCK_SIZE;
682108713a7SNeal Liu rctx->sha_iv = sha1_iv;
683108713a7SNeal Liu rctx->ivsize = 32;
684108713a7SNeal Liu memcpy(rctx->digest, sha1_iv, rctx->ivsize);
685108713a7SNeal Liu break;
686108713a7SNeal Liu case SHA224_DIGEST_SIZE:
687108713a7SNeal Liu rctx->cmd |= HASH_CMD_SHA224 | HASH_CMD_SHA_SWAP;
688108713a7SNeal Liu rctx->flags |= SHA_FLAGS_SHA224;
689108713a7SNeal Liu rctx->digsize = SHA224_DIGEST_SIZE;
690108713a7SNeal Liu rctx->block_size = SHA224_BLOCK_SIZE;
691108713a7SNeal Liu rctx->sha_iv = sha224_iv;
692108713a7SNeal Liu rctx->ivsize = 32;
693108713a7SNeal Liu memcpy(rctx->digest, sha224_iv, rctx->ivsize);
694108713a7SNeal Liu break;
695108713a7SNeal Liu case SHA256_DIGEST_SIZE:
696108713a7SNeal Liu rctx->cmd |= HASH_CMD_SHA256 | HASH_CMD_SHA_SWAP;
697108713a7SNeal Liu rctx->flags |= SHA_FLAGS_SHA256;
698108713a7SNeal Liu rctx->digsize = SHA256_DIGEST_SIZE;
699108713a7SNeal Liu rctx->block_size = SHA256_BLOCK_SIZE;
700108713a7SNeal Liu rctx->sha_iv = sha256_iv;
701108713a7SNeal Liu rctx->ivsize = 32;
702108713a7SNeal Liu memcpy(rctx->digest, sha256_iv, rctx->ivsize);
703108713a7SNeal Liu break;
704108713a7SNeal Liu case SHA384_DIGEST_SIZE:
705108713a7SNeal Liu rctx->cmd |= HASH_CMD_SHA512_SER | HASH_CMD_SHA384 |
706108713a7SNeal Liu HASH_CMD_SHA_SWAP;
707108713a7SNeal Liu rctx->flags |= SHA_FLAGS_SHA384;
708108713a7SNeal Liu rctx->digsize = SHA384_DIGEST_SIZE;
709108713a7SNeal Liu rctx->block_size = SHA384_BLOCK_SIZE;
710108713a7SNeal Liu rctx->sha_iv = (const __be32 *)sha384_iv;
711108713a7SNeal Liu rctx->ivsize = 64;
712108713a7SNeal Liu memcpy(rctx->digest, sha384_iv, rctx->ivsize);
713108713a7SNeal Liu break;
714108713a7SNeal Liu case SHA512_DIGEST_SIZE:
715108713a7SNeal Liu rctx->cmd |= HASH_CMD_SHA512_SER | HASH_CMD_SHA512 |
716108713a7SNeal Liu HASH_CMD_SHA_SWAP;
717108713a7SNeal Liu rctx->flags |= SHA_FLAGS_SHA512;
718108713a7SNeal Liu rctx->digsize = SHA512_DIGEST_SIZE;
719108713a7SNeal Liu rctx->block_size = SHA512_BLOCK_SIZE;
720108713a7SNeal Liu rctx->sha_iv = (const __be32 *)sha512_iv;
721108713a7SNeal Liu rctx->ivsize = 64;
722108713a7SNeal Liu memcpy(rctx->digest, sha512_iv, rctx->ivsize);
723108713a7SNeal Liu break;
724108713a7SNeal Liu default:
725108713a7SNeal Liu dev_warn(tctx->hace_dev->dev, "digest size %d not support\n",
726108713a7SNeal Liu crypto_ahash_digestsize(tfm));
727108713a7SNeal Liu return -EINVAL;
728108713a7SNeal Liu }
729108713a7SNeal Liu
730108713a7SNeal Liu rctx->bufcnt = 0;
731108713a7SNeal Liu rctx->total = 0;
732108713a7SNeal Liu rctx->digcnt[0] = 0;
733108713a7SNeal Liu rctx->digcnt[1] = 0;
734108713a7SNeal Liu
735108713a7SNeal Liu /* HMAC init */
736108713a7SNeal Liu if (tctx->flags & SHA_FLAGS_HMAC) {
737108713a7SNeal Liu rctx->digcnt[0] = rctx->block_size;
738108713a7SNeal Liu rctx->bufcnt = rctx->block_size;
739108713a7SNeal Liu memcpy(rctx->buffer, bctx->ipad, rctx->block_size);
740108713a7SNeal Liu rctx->flags |= SHA_FLAGS_HMAC;
741108713a7SNeal Liu }
742108713a7SNeal Liu
743108713a7SNeal Liu return 0;
744108713a7SNeal Liu }
745108713a7SNeal Liu
aspeed_sham_digest(struct ahash_request * req)746108713a7SNeal Liu static int aspeed_sham_digest(struct ahash_request *req)
747108713a7SNeal Liu {
748108713a7SNeal Liu return aspeed_sham_init(req) ? : aspeed_sham_finup(req);
749108713a7SNeal Liu }
750108713a7SNeal Liu
aspeed_sham_setkey(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)751108713a7SNeal Liu static int aspeed_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
752108713a7SNeal Liu unsigned int keylen)
753108713a7SNeal Liu {
754108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_ahash_ctx(tfm);
755108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
756108713a7SNeal Liu struct aspeed_sha_hmac_ctx *bctx = tctx->base;
757108713a7SNeal Liu int ds = crypto_shash_digestsize(bctx->shash);
758108713a7SNeal Liu int bs = crypto_shash_blocksize(bctx->shash);
759108713a7SNeal Liu int err = 0;
760108713a7SNeal Liu int i;
761108713a7SNeal Liu
762108713a7SNeal Liu AHASH_DBG(hace_dev, "%s: keylen:%d\n", crypto_tfm_alg_name(&tfm->base),
763108713a7SNeal Liu keylen);
764108713a7SNeal Liu
765108713a7SNeal Liu if (keylen > bs) {
766108713a7SNeal Liu err = aspeed_sham_shash_digest(bctx->shash,
767108713a7SNeal Liu crypto_shash_get_flags(bctx->shash),
768108713a7SNeal Liu key, keylen, bctx->ipad);
769108713a7SNeal Liu if (err)
770108713a7SNeal Liu return err;
771108713a7SNeal Liu keylen = ds;
772108713a7SNeal Liu
773108713a7SNeal Liu } else {
774108713a7SNeal Liu memcpy(bctx->ipad, key, keylen);
775108713a7SNeal Liu }
776108713a7SNeal Liu
777108713a7SNeal Liu memset(bctx->ipad + keylen, 0, bs - keylen);
778108713a7SNeal Liu memcpy(bctx->opad, bctx->ipad, bs);
779108713a7SNeal Liu
780108713a7SNeal Liu for (i = 0; i < bs; i++) {
781108713a7SNeal Liu bctx->ipad[i] ^= HMAC_IPAD_VALUE;
782108713a7SNeal Liu bctx->opad[i] ^= HMAC_OPAD_VALUE;
783108713a7SNeal Liu }
784108713a7SNeal Liu
785108713a7SNeal Liu return err;
786108713a7SNeal Liu }
787108713a7SNeal Liu
aspeed_sham_cra_init(struct crypto_tfm * tfm)788108713a7SNeal Liu static int aspeed_sham_cra_init(struct crypto_tfm *tfm)
789108713a7SNeal Liu {
790108713a7SNeal Liu struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
791108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_tfm_ctx(tfm);
792108713a7SNeal Liu struct aspeed_hace_alg *ast_alg;
793108713a7SNeal Liu
794*304506f2SHerbert Xu ast_alg = container_of(alg, struct aspeed_hace_alg, alg.ahash.base);
795108713a7SNeal Liu tctx->hace_dev = ast_alg->hace_dev;
796108713a7SNeal Liu tctx->flags = 0;
797108713a7SNeal Liu
798108713a7SNeal Liu crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
799108713a7SNeal Liu sizeof(struct aspeed_sham_reqctx));
800108713a7SNeal Liu
801108713a7SNeal Liu if (ast_alg->alg_base) {
802108713a7SNeal Liu /* hmac related */
803108713a7SNeal Liu struct aspeed_sha_hmac_ctx *bctx = tctx->base;
804108713a7SNeal Liu
805108713a7SNeal Liu tctx->flags |= SHA_FLAGS_HMAC;
806108713a7SNeal Liu bctx->shash = crypto_alloc_shash(ast_alg->alg_base, 0,
807108713a7SNeal Liu CRYPTO_ALG_NEED_FALLBACK);
808108713a7SNeal Liu if (IS_ERR(bctx->shash)) {
809108713a7SNeal Liu dev_warn(ast_alg->hace_dev->dev,
810108713a7SNeal Liu "base driver '%s' could not be loaded.\n",
811108713a7SNeal Liu ast_alg->alg_base);
812108713a7SNeal Liu return PTR_ERR(bctx->shash);
813108713a7SNeal Liu }
814108713a7SNeal Liu }
815108713a7SNeal Liu
816108713a7SNeal Liu return 0;
817108713a7SNeal Liu }
818108713a7SNeal Liu
aspeed_sham_cra_exit(struct crypto_tfm * tfm)819108713a7SNeal Liu static void aspeed_sham_cra_exit(struct crypto_tfm *tfm)
820108713a7SNeal Liu {
821108713a7SNeal Liu struct aspeed_sham_ctx *tctx = crypto_tfm_ctx(tfm);
822108713a7SNeal Liu struct aspeed_hace_dev *hace_dev = tctx->hace_dev;
823108713a7SNeal Liu
824108713a7SNeal Liu AHASH_DBG(hace_dev, "%s\n", crypto_tfm_alg_name(tfm));
825108713a7SNeal Liu
826108713a7SNeal Liu if (tctx->flags & SHA_FLAGS_HMAC) {
827108713a7SNeal Liu struct aspeed_sha_hmac_ctx *bctx = tctx->base;
828108713a7SNeal Liu
829108713a7SNeal Liu crypto_free_shash(bctx->shash);
830108713a7SNeal Liu }
831108713a7SNeal Liu }
832108713a7SNeal Liu
aspeed_sham_export(struct ahash_request * req,void * out)833108713a7SNeal Liu static int aspeed_sham_export(struct ahash_request *req, void *out)
834108713a7SNeal Liu {
835108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
836108713a7SNeal Liu
837108713a7SNeal Liu memcpy(out, rctx, sizeof(*rctx));
838108713a7SNeal Liu
839108713a7SNeal Liu return 0;
840108713a7SNeal Liu }
841108713a7SNeal Liu
aspeed_sham_import(struct ahash_request * req,const void * in)842108713a7SNeal Liu static int aspeed_sham_import(struct ahash_request *req, const void *in)
843108713a7SNeal Liu {
844108713a7SNeal Liu struct aspeed_sham_reqctx *rctx = ahash_request_ctx(req);
845108713a7SNeal Liu
846108713a7SNeal Liu memcpy(rctx, in, sizeof(*rctx));
847108713a7SNeal Liu
848108713a7SNeal Liu return 0;
849108713a7SNeal Liu }
850108713a7SNeal Liu
851efc96d43SHerbert Xu static struct aspeed_hace_alg aspeed_ahash_algs[] = {
852108713a7SNeal Liu {
853*304506f2SHerbert Xu .alg.ahash.base = {
854108713a7SNeal Liu .init = aspeed_sham_init,
855108713a7SNeal Liu .update = aspeed_sham_update,
856108713a7SNeal Liu .final = aspeed_sham_final,
857108713a7SNeal Liu .finup = aspeed_sham_finup,
858108713a7SNeal Liu .digest = aspeed_sham_digest,
859108713a7SNeal Liu .export = aspeed_sham_export,
860108713a7SNeal Liu .import = aspeed_sham_import,
861108713a7SNeal Liu .halg = {
862108713a7SNeal Liu .digestsize = SHA1_DIGEST_SIZE,
863108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
864108713a7SNeal Liu .base = {
865108713a7SNeal Liu .cra_name = "sha1",
866108713a7SNeal Liu .cra_driver_name = "aspeed-sha1",
867108713a7SNeal Liu .cra_priority = 300,
868108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
869108713a7SNeal Liu CRYPTO_ALG_ASYNC |
870108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
871108713a7SNeal Liu .cra_blocksize = SHA1_BLOCK_SIZE,
872108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx),
873108713a7SNeal Liu .cra_alignmask = 0,
874108713a7SNeal Liu .cra_module = THIS_MODULE,
875108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
876108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
877108713a7SNeal Liu }
878108713a7SNeal Liu }
879108713a7SNeal Liu },
880*304506f2SHerbert Xu .alg.ahash.op = {
881*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
882*304506f2SHerbert Xu },
883108713a7SNeal Liu },
884108713a7SNeal Liu {
885*304506f2SHerbert Xu .alg.ahash.base = {
886108713a7SNeal Liu .init = aspeed_sham_init,
887108713a7SNeal Liu .update = aspeed_sham_update,
888108713a7SNeal Liu .final = aspeed_sham_final,
889108713a7SNeal Liu .finup = aspeed_sham_finup,
890108713a7SNeal Liu .digest = aspeed_sham_digest,
891108713a7SNeal Liu .export = aspeed_sham_export,
892108713a7SNeal Liu .import = aspeed_sham_import,
893108713a7SNeal Liu .halg = {
894108713a7SNeal Liu .digestsize = SHA256_DIGEST_SIZE,
895108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
896108713a7SNeal Liu .base = {
897108713a7SNeal Liu .cra_name = "sha256",
898108713a7SNeal Liu .cra_driver_name = "aspeed-sha256",
899108713a7SNeal Liu .cra_priority = 300,
900108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
901108713a7SNeal Liu CRYPTO_ALG_ASYNC |
902108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
903108713a7SNeal Liu .cra_blocksize = SHA256_BLOCK_SIZE,
904108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx),
905108713a7SNeal Liu .cra_alignmask = 0,
906108713a7SNeal Liu .cra_module = THIS_MODULE,
907108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
908108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
909108713a7SNeal Liu }
910108713a7SNeal Liu }
911108713a7SNeal Liu },
912*304506f2SHerbert Xu .alg.ahash.op = {
913*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
914*304506f2SHerbert Xu },
915108713a7SNeal Liu },
916108713a7SNeal Liu {
917*304506f2SHerbert Xu .alg.ahash.base = {
918108713a7SNeal Liu .init = aspeed_sham_init,
919108713a7SNeal Liu .update = aspeed_sham_update,
920108713a7SNeal Liu .final = aspeed_sham_final,
921108713a7SNeal Liu .finup = aspeed_sham_finup,
922108713a7SNeal Liu .digest = aspeed_sham_digest,
923108713a7SNeal Liu .export = aspeed_sham_export,
924108713a7SNeal Liu .import = aspeed_sham_import,
925108713a7SNeal Liu .halg = {
926108713a7SNeal Liu .digestsize = SHA224_DIGEST_SIZE,
927108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
928108713a7SNeal Liu .base = {
929108713a7SNeal Liu .cra_name = "sha224",
930108713a7SNeal Liu .cra_driver_name = "aspeed-sha224",
931108713a7SNeal Liu .cra_priority = 300,
932108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
933108713a7SNeal Liu CRYPTO_ALG_ASYNC |
934108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
935108713a7SNeal Liu .cra_blocksize = SHA224_BLOCK_SIZE,
936108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx),
937108713a7SNeal Liu .cra_alignmask = 0,
938108713a7SNeal Liu .cra_module = THIS_MODULE,
939108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
940108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
941108713a7SNeal Liu }
942108713a7SNeal Liu }
943108713a7SNeal Liu },
944*304506f2SHerbert Xu .alg.ahash.op = {
945*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
946*304506f2SHerbert Xu },
947108713a7SNeal Liu },
948108713a7SNeal Liu {
949108713a7SNeal Liu .alg_base = "sha1",
950*304506f2SHerbert Xu .alg.ahash.base = {
951108713a7SNeal Liu .init = aspeed_sham_init,
952108713a7SNeal Liu .update = aspeed_sham_update,
953108713a7SNeal Liu .final = aspeed_sham_final,
954108713a7SNeal Liu .finup = aspeed_sham_finup,
955108713a7SNeal Liu .digest = aspeed_sham_digest,
956108713a7SNeal Liu .setkey = aspeed_sham_setkey,
957108713a7SNeal Liu .export = aspeed_sham_export,
958108713a7SNeal Liu .import = aspeed_sham_import,
959108713a7SNeal Liu .halg = {
960108713a7SNeal Liu .digestsize = SHA1_DIGEST_SIZE,
961108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
962108713a7SNeal Liu .base = {
963108713a7SNeal Liu .cra_name = "hmac(sha1)",
964108713a7SNeal Liu .cra_driver_name = "aspeed-hmac-sha1",
965108713a7SNeal Liu .cra_priority = 300,
966108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
967108713a7SNeal Liu CRYPTO_ALG_ASYNC |
968108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
969108713a7SNeal Liu .cra_blocksize = SHA1_BLOCK_SIZE,
970108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx) +
971108713a7SNeal Liu sizeof(struct aspeed_sha_hmac_ctx),
972108713a7SNeal Liu .cra_alignmask = 0,
973108713a7SNeal Liu .cra_module = THIS_MODULE,
974108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
975108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
976108713a7SNeal Liu }
977108713a7SNeal Liu }
978108713a7SNeal Liu },
979*304506f2SHerbert Xu .alg.ahash.op = {
980*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
981*304506f2SHerbert Xu },
982108713a7SNeal Liu },
983108713a7SNeal Liu {
984108713a7SNeal Liu .alg_base = "sha224",
985*304506f2SHerbert Xu .alg.ahash.base = {
986108713a7SNeal Liu .init = aspeed_sham_init,
987108713a7SNeal Liu .update = aspeed_sham_update,
988108713a7SNeal Liu .final = aspeed_sham_final,
989108713a7SNeal Liu .finup = aspeed_sham_finup,
990108713a7SNeal Liu .digest = aspeed_sham_digest,
991108713a7SNeal Liu .setkey = aspeed_sham_setkey,
992108713a7SNeal Liu .export = aspeed_sham_export,
993108713a7SNeal Liu .import = aspeed_sham_import,
994108713a7SNeal Liu .halg = {
995108713a7SNeal Liu .digestsize = SHA224_DIGEST_SIZE,
996108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
997108713a7SNeal Liu .base = {
998108713a7SNeal Liu .cra_name = "hmac(sha224)",
999108713a7SNeal Liu .cra_driver_name = "aspeed-hmac-sha224",
1000108713a7SNeal Liu .cra_priority = 300,
1001108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1002108713a7SNeal Liu CRYPTO_ALG_ASYNC |
1003108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
1004108713a7SNeal Liu .cra_blocksize = SHA224_BLOCK_SIZE,
1005108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx) +
1006108713a7SNeal Liu sizeof(struct aspeed_sha_hmac_ctx),
1007108713a7SNeal Liu .cra_alignmask = 0,
1008108713a7SNeal Liu .cra_module = THIS_MODULE,
1009108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
1010108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
1011108713a7SNeal Liu }
1012108713a7SNeal Liu }
1013108713a7SNeal Liu },
1014*304506f2SHerbert Xu .alg.ahash.op = {
1015*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
1016*304506f2SHerbert Xu },
1017108713a7SNeal Liu },
1018108713a7SNeal Liu {
1019108713a7SNeal Liu .alg_base = "sha256",
1020*304506f2SHerbert Xu .alg.ahash.base = {
1021108713a7SNeal Liu .init = aspeed_sham_init,
1022108713a7SNeal Liu .update = aspeed_sham_update,
1023108713a7SNeal Liu .final = aspeed_sham_final,
1024108713a7SNeal Liu .finup = aspeed_sham_finup,
1025108713a7SNeal Liu .digest = aspeed_sham_digest,
1026108713a7SNeal Liu .setkey = aspeed_sham_setkey,
1027108713a7SNeal Liu .export = aspeed_sham_export,
1028108713a7SNeal Liu .import = aspeed_sham_import,
1029108713a7SNeal Liu .halg = {
1030108713a7SNeal Liu .digestsize = SHA256_DIGEST_SIZE,
1031108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
1032108713a7SNeal Liu .base = {
1033108713a7SNeal Liu .cra_name = "hmac(sha256)",
1034108713a7SNeal Liu .cra_driver_name = "aspeed-hmac-sha256",
1035108713a7SNeal Liu .cra_priority = 300,
1036108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1037108713a7SNeal Liu CRYPTO_ALG_ASYNC |
1038108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
1039108713a7SNeal Liu .cra_blocksize = SHA256_BLOCK_SIZE,
1040108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx) +
1041108713a7SNeal Liu sizeof(struct aspeed_sha_hmac_ctx),
1042108713a7SNeal Liu .cra_alignmask = 0,
1043108713a7SNeal Liu .cra_module = THIS_MODULE,
1044108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
1045108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
1046108713a7SNeal Liu }
1047108713a7SNeal Liu }
1048108713a7SNeal Liu },
1049*304506f2SHerbert Xu .alg.ahash.op = {
1050*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
1051*304506f2SHerbert Xu },
1052108713a7SNeal Liu },
1053108713a7SNeal Liu };
1054108713a7SNeal Liu
1055efc96d43SHerbert Xu static struct aspeed_hace_alg aspeed_ahash_algs_g6[] = {
1056108713a7SNeal Liu {
1057*304506f2SHerbert Xu .alg.ahash.base = {
1058108713a7SNeal Liu .init = aspeed_sham_init,
1059108713a7SNeal Liu .update = aspeed_sham_update,
1060108713a7SNeal Liu .final = aspeed_sham_final,
1061108713a7SNeal Liu .finup = aspeed_sham_finup,
1062108713a7SNeal Liu .digest = aspeed_sham_digest,
1063108713a7SNeal Liu .export = aspeed_sham_export,
1064108713a7SNeal Liu .import = aspeed_sham_import,
1065108713a7SNeal Liu .halg = {
1066108713a7SNeal Liu .digestsize = SHA384_DIGEST_SIZE,
1067108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
1068108713a7SNeal Liu .base = {
1069108713a7SNeal Liu .cra_name = "sha384",
1070108713a7SNeal Liu .cra_driver_name = "aspeed-sha384",
1071108713a7SNeal Liu .cra_priority = 300,
1072108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1073108713a7SNeal Liu CRYPTO_ALG_ASYNC |
1074108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
1075108713a7SNeal Liu .cra_blocksize = SHA384_BLOCK_SIZE,
1076108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx),
1077108713a7SNeal Liu .cra_alignmask = 0,
1078108713a7SNeal Liu .cra_module = THIS_MODULE,
1079108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
1080108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
1081108713a7SNeal Liu }
1082108713a7SNeal Liu }
1083108713a7SNeal Liu },
1084*304506f2SHerbert Xu .alg.ahash.op = {
1085*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
1086*304506f2SHerbert Xu },
1087108713a7SNeal Liu },
1088108713a7SNeal Liu {
1089*304506f2SHerbert Xu .alg.ahash.base = {
1090108713a7SNeal Liu .init = aspeed_sham_init,
1091108713a7SNeal Liu .update = aspeed_sham_update,
1092108713a7SNeal Liu .final = aspeed_sham_final,
1093108713a7SNeal Liu .finup = aspeed_sham_finup,
1094108713a7SNeal Liu .digest = aspeed_sham_digest,
1095108713a7SNeal Liu .export = aspeed_sham_export,
1096108713a7SNeal Liu .import = aspeed_sham_import,
1097108713a7SNeal Liu .halg = {
1098108713a7SNeal Liu .digestsize = SHA512_DIGEST_SIZE,
1099108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
1100108713a7SNeal Liu .base = {
1101108713a7SNeal Liu .cra_name = "sha512",
1102108713a7SNeal Liu .cra_driver_name = "aspeed-sha512",
1103108713a7SNeal Liu .cra_priority = 300,
1104108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1105108713a7SNeal Liu CRYPTO_ALG_ASYNC |
1106108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
1107108713a7SNeal Liu .cra_blocksize = SHA512_BLOCK_SIZE,
1108108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx),
1109108713a7SNeal Liu .cra_alignmask = 0,
1110108713a7SNeal Liu .cra_module = THIS_MODULE,
1111108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
1112108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
1113108713a7SNeal Liu }
1114108713a7SNeal Liu }
1115108713a7SNeal Liu },
1116*304506f2SHerbert Xu .alg.ahash.op = {
1117*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
1118*304506f2SHerbert Xu },
1119108713a7SNeal Liu },
1120108713a7SNeal Liu {
1121108713a7SNeal Liu .alg_base = "sha384",
1122*304506f2SHerbert Xu .alg.ahash.base = {
1123108713a7SNeal Liu .init = aspeed_sham_init,
1124108713a7SNeal Liu .update = aspeed_sham_update,
1125108713a7SNeal Liu .final = aspeed_sham_final,
1126108713a7SNeal Liu .finup = aspeed_sham_finup,
1127108713a7SNeal Liu .digest = aspeed_sham_digest,
1128108713a7SNeal Liu .setkey = aspeed_sham_setkey,
1129108713a7SNeal Liu .export = aspeed_sham_export,
1130108713a7SNeal Liu .import = aspeed_sham_import,
1131108713a7SNeal Liu .halg = {
1132108713a7SNeal Liu .digestsize = SHA384_DIGEST_SIZE,
1133108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
1134108713a7SNeal Liu .base = {
1135108713a7SNeal Liu .cra_name = "hmac(sha384)",
1136108713a7SNeal Liu .cra_driver_name = "aspeed-hmac-sha384",
1137108713a7SNeal Liu .cra_priority = 300,
1138108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1139108713a7SNeal Liu CRYPTO_ALG_ASYNC |
1140108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
1141108713a7SNeal Liu .cra_blocksize = SHA384_BLOCK_SIZE,
1142108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx) +
1143108713a7SNeal Liu sizeof(struct aspeed_sha_hmac_ctx),
1144108713a7SNeal Liu .cra_alignmask = 0,
1145108713a7SNeal Liu .cra_module = THIS_MODULE,
1146108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
1147108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
1148108713a7SNeal Liu }
1149108713a7SNeal Liu }
1150108713a7SNeal Liu },
1151*304506f2SHerbert Xu .alg.ahash.op = {
1152*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
1153*304506f2SHerbert Xu },
1154108713a7SNeal Liu },
1155108713a7SNeal Liu {
1156108713a7SNeal Liu .alg_base = "sha512",
1157*304506f2SHerbert Xu .alg.ahash.base = {
1158108713a7SNeal Liu .init = aspeed_sham_init,
1159108713a7SNeal Liu .update = aspeed_sham_update,
1160108713a7SNeal Liu .final = aspeed_sham_final,
1161108713a7SNeal Liu .finup = aspeed_sham_finup,
1162108713a7SNeal Liu .digest = aspeed_sham_digest,
1163108713a7SNeal Liu .setkey = aspeed_sham_setkey,
1164108713a7SNeal Liu .export = aspeed_sham_export,
1165108713a7SNeal Liu .import = aspeed_sham_import,
1166108713a7SNeal Liu .halg = {
1167108713a7SNeal Liu .digestsize = SHA512_DIGEST_SIZE,
1168108713a7SNeal Liu .statesize = sizeof(struct aspeed_sham_reqctx),
1169108713a7SNeal Liu .base = {
1170108713a7SNeal Liu .cra_name = "hmac(sha512)",
1171108713a7SNeal Liu .cra_driver_name = "aspeed-hmac-sha512",
1172108713a7SNeal Liu .cra_priority = 300,
1173108713a7SNeal Liu .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1174108713a7SNeal Liu CRYPTO_ALG_ASYNC |
1175108713a7SNeal Liu CRYPTO_ALG_KERN_DRIVER_ONLY,
1176108713a7SNeal Liu .cra_blocksize = SHA512_BLOCK_SIZE,
1177108713a7SNeal Liu .cra_ctxsize = sizeof(struct aspeed_sham_ctx) +
1178108713a7SNeal Liu sizeof(struct aspeed_sha_hmac_ctx),
1179108713a7SNeal Liu .cra_alignmask = 0,
1180108713a7SNeal Liu .cra_module = THIS_MODULE,
1181108713a7SNeal Liu .cra_init = aspeed_sham_cra_init,
1182108713a7SNeal Liu .cra_exit = aspeed_sham_cra_exit,
1183108713a7SNeal Liu }
1184108713a7SNeal Liu }
1185108713a7SNeal Liu },
1186*304506f2SHerbert Xu .alg.ahash.op = {
1187*304506f2SHerbert Xu .do_one_request = aspeed_ahash_do_one,
1188*304506f2SHerbert Xu },
1189108713a7SNeal Liu },
1190108713a7SNeal Liu };
1191108713a7SNeal Liu
aspeed_unregister_hace_hash_algs(struct aspeed_hace_dev * hace_dev)1192108713a7SNeal Liu void aspeed_unregister_hace_hash_algs(struct aspeed_hace_dev *hace_dev)
1193108713a7SNeal Liu {
1194108713a7SNeal Liu int i;
1195108713a7SNeal Liu
1196108713a7SNeal Liu for (i = 0; i < ARRAY_SIZE(aspeed_ahash_algs); i++)
1197*304506f2SHerbert Xu crypto_engine_unregister_ahash(&aspeed_ahash_algs[i].alg.ahash);
1198108713a7SNeal Liu
1199108713a7SNeal Liu if (hace_dev->version != AST2600_VERSION)
1200108713a7SNeal Liu return;
1201108713a7SNeal Liu
1202108713a7SNeal Liu for (i = 0; i < ARRAY_SIZE(aspeed_ahash_algs_g6); i++)
1203*304506f2SHerbert Xu crypto_engine_unregister_ahash(&aspeed_ahash_algs_g6[i].alg.ahash);
1204108713a7SNeal Liu }
1205108713a7SNeal Liu
aspeed_register_hace_hash_algs(struct aspeed_hace_dev * hace_dev)1206108713a7SNeal Liu void aspeed_register_hace_hash_algs(struct aspeed_hace_dev *hace_dev)
1207108713a7SNeal Liu {
1208108713a7SNeal Liu int rc, i;
1209108713a7SNeal Liu
1210108713a7SNeal Liu AHASH_DBG(hace_dev, "\n");
1211108713a7SNeal Liu
1212108713a7SNeal Liu for (i = 0; i < ARRAY_SIZE(aspeed_ahash_algs); i++) {
1213108713a7SNeal Liu aspeed_ahash_algs[i].hace_dev = hace_dev;
1214*304506f2SHerbert Xu rc = crypto_engine_register_ahash(&aspeed_ahash_algs[i].alg.ahash);
1215108713a7SNeal Liu if (rc) {
1216108713a7SNeal Liu AHASH_DBG(hace_dev, "Failed to register %s\n",
1217*304506f2SHerbert Xu aspeed_ahash_algs[i].alg.ahash.base.halg.base.cra_name);
1218108713a7SNeal Liu }
1219108713a7SNeal Liu }
1220108713a7SNeal Liu
1221108713a7SNeal Liu if (hace_dev->version != AST2600_VERSION)
1222108713a7SNeal Liu return;
1223108713a7SNeal Liu
1224108713a7SNeal Liu for (i = 0; i < ARRAY_SIZE(aspeed_ahash_algs_g6); i++) {
1225108713a7SNeal Liu aspeed_ahash_algs_g6[i].hace_dev = hace_dev;
1226*304506f2SHerbert Xu rc = crypto_engine_register_ahash(&aspeed_ahash_algs_g6[i].alg.ahash);
1227108713a7SNeal Liu if (rc) {
1228108713a7SNeal Liu AHASH_DBG(hace_dev, "Failed to register %s\n",
1229*304506f2SHerbert Xu aspeed_ahash_algs_g6[i].alg.ahash.base.halg.base.cra_name);
1230108713a7SNeal Liu }
1231108713a7SNeal Liu }
1232108713a7SNeal Liu }
1233