2 * sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC
6 * This file add support for MD5 and SHA1.
8 * You could find the datasheet in Documentation/arm/sunxi/README
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/scatterlist.h>
18 /* This is a totally arbitrary value */
19 #define SS_TIMEOUT 100
21 int sun4i_hash_crainit(struct crypto_tfm *tfm)
23 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
24 sizeof(struct sun4i_req_ctx));
28 /* sun4i_hash_init: initialize request context */
29 int sun4i_hash_init(struct ahash_request *areq)
31 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
32 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
33 struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
34 struct sun4i_ss_alg_template *algt;
35 struct sun4i_ss_ctx *ss;
37 memset(op, 0, sizeof(struct sun4i_req_ctx));
39 algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
42 op->mode = algt->mode;
47 int sun4i_hash_export_md5(struct ahash_request *areq, void *out)
49 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
50 struct md5_state *octx = out;
53 octx->byte_count = op->byte_count + op->len;
55 memcpy(octx->block, op->buf, op->len);
57 if (op->byte_count > 0) {
58 for (i = 0; i < 4; i++)
59 octx->hash[i] = op->hash[i];
61 octx->hash[0] = SHA1_H0;
62 octx->hash[1] = SHA1_H1;
63 octx->hash[2] = SHA1_H2;
64 octx->hash[3] = SHA1_H3;
70 int sun4i_hash_import_md5(struct ahash_request *areq, const void *in)
72 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
73 const struct md5_state *ictx = in;
76 sun4i_hash_init(areq);
78 op->byte_count = ictx->byte_count & ~0x3F;
79 op->len = ictx->byte_count & 0x3F;
81 memcpy(op->buf, ictx->block, op->len);
83 for (i = 0; i < 4; i++)
84 op->hash[i] = ictx->hash[i];
89 int sun4i_hash_export_sha1(struct ahash_request *areq, void *out)
91 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
92 struct sha1_state *octx = out;
95 octx->count = op->byte_count + op->len;
97 memcpy(octx->buffer, op->buf, op->len);
99 if (op->byte_count > 0) {
100 for (i = 0; i < 5; i++)
101 octx->state[i] = op->hash[i];
103 octx->state[0] = SHA1_H0;
104 octx->state[1] = SHA1_H1;
105 octx->state[2] = SHA1_H2;
106 octx->state[3] = SHA1_H3;
107 octx->state[4] = SHA1_H4;
113 int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in)
115 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
116 const struct sha1_state *ictx = in;
119 sun4i_hash_init(areq);
121 op->byte_count = ictx->count & ~0x3F;
122 op->len = ictx->count & 0x3F;
124 memcpy(op->buf, ictx->buffer, op->len);
126 for (i = 0; i < 5; i++)
127 op->hash[i] = ictx->state[i];
133 * sun4i_hash_update: update hash engine
135 * Could be used for both SHA1 and MD5
136 * Write data by step of 32bits and put then in the SS.
138 * Since we cannot leave partial data and hash state in the engine,
139 * we need to get the hash state at the end of this function.
140 * We can get the hash state every 64 bytes
142 * So the first work is to get the number of bytes to write to SS modulo 64
143 * The extra bytes will go to a temporary buffer op->buf storing op->len bytes
145 * So at the begin of update()
146 * if op->len + areq->nbytes < 64
147 * => all data will be written to wait buffer (op->buf) and end=0
148 * if not, write all data from op->buf to the device and position end to
149 * complete to 64bytes
152 * update1 60o => op->len=60
153 * update2 60o => need one more word to have 64 bytes
155 * so write all data from op->buf and one word of SGs
156 * write remaining data in op->buf
157 * final state op->len=56
159 int sun4i_hash_update(struct ahash_request *areq)
164 * i is the total bytes read from SGs, to be compared to areq->nbytes
165 * i is important because we cannot rely on SG length since the sum of
166 * SG->length could be greater than areq->nbytes
169 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
170 struct sun4i_ss_ctx *ss = op->ss;
171 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
172 unsigned int in_i = 0; /* advancement in the current SG */
175 * end is the position when we need to stop writing to the device,
176 * to be compared to i
180 u32 spaces, rx_cnt = SS_RX_DEFAULT;
182 struct sg_mapping_iter mi;
184 dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",
185 __func__, crypto_tfm_alg_name(areq->base.tfm),
186 op->byte_count, areq->nbytes, op->mode,
187 op->len, op->hash[0]);
189 if (areq->nbytes == 0)
192 /* protect against overflow */
193 if (areq->nbytes > UINT_MAX - op->len) {
194 dev_err(ss->dev, "Cannot process too large request\n");
198 if (op->len + areq->nbytes < 64) {
199 /* linearize data to op->buf */
200 copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
201 op->buf + op->len, areq->nbytes, 0);
206 end = ((areq->nbytes + op->len) / 64) * 64 - op->len;
208 if (end > areq->nbytes || areq->nbytes - end > 63) {
209 dev_err(ss->dev, "ERROR: Bound error %u %u\n",
214 spin_lock_bh(&ss->slock);
217 * if some data have been processed before,
218 * we need to restore the partial hash state
220 if (op->byte_count > 0) {
221 ivmode = SS_IV_ARBITRARY;
222 for (i = 0; i < 5; i++)
223 writel(op->hash[i], ss->base + SS_IV0 + i * 4);
225 /* Enable the device */
226 writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
229 sg_miter_start(&mi, areq->src, sg_nents(areq->src),
230 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
236 * we need to linearize in two case:
237 * - the buffer is already used
238 * - the SG does not have enough byte remaining ( < 4)
240 if (op->len > 0 || (mi.length - in_i) < 4) {
242 * if we have entered here we have two reason to stop
243 * - the buffer is full
246 while (op->len < 64 && i < end) {
247 /* how many bytes we can read from current SG */
248 in_r = min3(mi.length - in_i, end - i,
250 memcpy(op->buf + op->len, mi.addr + in_i, in_r);
254 if (in_i == mi.length) {
259 if (op->len > 3 && (op->len % 4) == 0) {
260 /* write buf to the device */
261 writesl(ss->base + SS_RXFIFO, op->buf,
263 op->byte_count += op->len;
267 if (mi.length - in_i > 3 && i < end) {
268 /* how many bytes we can read from current SG */
269 in_r = min3(mi.length - in_i, areq->nbytes - i,
270 ((mi.length - in_i) / 4) * 4);
271 /* how many bytes we can write in the device*/
272 todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4);
273 writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo);
274 op->byte_count += todo * 4;
279 spaces = readl(ss->base + SS_FCSR);
280 rx_cnt = SS_RXFIFO_SPACES(spaces);
282 if (in_i == mi.length) {
289 if ((areq->nbytes - i) < 64) {
290 while (i < areq->nbytes && in_i < mi.length && op->len < 64) {
291 /* how many bytes we can read from current SG */
292 in_r = min3(mi.length - in_i, areq->nbytes - i,
294 memcpy(op->buf + op->len, mi.addr + in_i, in_r);
298 if (in_i == mi.length) {
307 writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
310 v = readl(ss->base + SS_CTL);
312 } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
313 if (i >= SS_TIMEOUT) {
314 dev_err_ratelimited(ss->dev,
315 "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
316 i, SS_TIMEOUT, v, areq->nbytes);
321 /* get the partial hash only if something was written */
322 for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
323 op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
326 writel(0, ss->base + SS_CTL);
327 spin_unlock_bh(&ss->slock);
332 * sun4i_hash_final: finalize hashing operation
334 * If we have some remaining bytes, we write them.
335 * Then ask the SS for finalizing the hashing operation
337 * I do not check RX FIFO size in this function since the size is 32
338 * after each enabling and this function neither write more than 32 words.
340 int sun4i_hash_final(struct ahash_request *areq)
346 unsigned int index, padlen;
348 struct sun4i_req_ctx *op = ahash_request_ctx(areq);
349 struct sun4i_ss_ctx *ss = op->ss;
350 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
353 unsigned int nwait, nbw = 0;
355 dev_dbg(ss->dev, "%s: byte=%llu len=%u mode=%x wl=%u h=%x",
356 __func__, op->byte_count, areq->nbytes, op->mode,
357 op->len, op->hash[0]);
359 spin_lock_bh(&ss->slock);
362 * if we have already written something,
363 * restore the partial hash state
365 if (op->byte_count > 0) {
366 ivmode = SS_IV_ARBITRARY;
367 for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
368 writel(op->hash[i], ss->base + SS_IV0 + i * 4);
370 writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
372 /* write the remaining words of the wait buffer */
376 writesl(ss->base + SS_RXFIFO, op->buf, nwait);
377 op->byte_count += 4 * nwait;
379 nbw = op->len - 4 * nwait;
380 wb = *(u32 *)(op->buf + nwait * 4);
381 wb &= (0xFFFFFFFF >> (4 - nbw) * 8);
384 /* write the remaining bytes of the nbw buffer */
386 wb |= ((1 << 7) << (nbw * 8));
393 * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)
394 * I take the operations from other MD5/SHA1 implementations
397 /* we have already send 4 more byte of which nbw data */
398 if (op->mode == SS_OP_MD5) {
399 index = (op->byte_count + 4) & 0x3f;
400 op->byte_count += nbw;
402 zeros = (120 - index) / 4;
404 zeros = (56 - index) / 4;
406 op->byte_count += nbw;
407 index = op->byte_count & 0x3f;
408 padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
409 zeros = (padlen - 1) / 4;
412 memset(bf + j, 0, 4 * zeros);
415 /* write the length of data */
416 if (op->mode == SS_OP_SHA1) {
417 bits = cpu_to_be64(op->byte_count << 3);
418 bf[j++] = bits & 0xffffffff;
419 bf[j++] = (bits >> 32) & 0xffffffff;
421 bf[j++] = (op->byte_count << 3) & 0xffffffff;
422 bf[j++] = (op->byte_count >> 29) & 0xffffffff;
424 writesl(ss->base + SS_RXFIFO, bf, j);
426 /* Tell the SS to stop the hashing */
427 writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
430 * Wait for SS to finish the hash.
431 * The timeout could happen only in case of bad overcloking
436 v = readl(ss->base + SS_CTL);
438 } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
439 if (i >= SS_TIMEOUT) {
440 dev_err_ratelimited(ss->dev,
441 "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
442 i, SS_TIMEOUT, v, areq->nbytes);
447 /* Get the hash from the device */
448 if (op->mode == SS_OP_SHA1) {
449 for (i = 0; i < 5; i++) {
450 v = cpu_to_be32(readl(ss->base + SS_MD0 + i * 4));
451 memcpy(areq->result + i * 4, &v, 4);
454 for (i = 0; i < 4; i++) {
455 v = readl(ss->base + SS_MD0 + i * 4);
456 memcpy(areq->result + i * 4, &v, 4);
461 writel(0, ss->base + SS_CTL);
462 spin_unlock_bh(&ss->slock);
466 /* sun4i_hash_finup: finalize hashing operation after an update */
467 int sun4i_hash_finup(struct ahash_request *areq)
471 err = sun4i_hash_update(areq);
475 return sun4i_hash_final(areq);
478 /* combo of init/update/final functions */
479 int sun4i_hash_digest(struct ahash_request *areq)
483 err = sun4i_hash_init(areq);
487 err = sun4i_hash_update(areq);
491 return sun4i_hash_final(areq);