sun4i-ss-hash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * sun4i-ss-hash.c - hardware cryptographic accelerator for Allwinner A20 SoC
  3. *
  4. * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
  5. *
  6. * This file add support for MD5 and SHA1.
  7. *
  8. * You could find the datasheet in Documentation/arm/sunxi/README
  9. *
  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.
  14. */
  15. #include "sun4i-ss.h"
  16. #include <linux/scatterlist.h>
  17. /* This is a totally arbitrary value */
  18. #define SS_TIMEOUT 100
  19. int sun4i_hash_crainit(struct crypto_tfm *tfm)
  20. {
  21. struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
  22. struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
  23. struct sun4i_ss_alg_template *algt;
  24. memset(op, 0, sizeof(struct sun4i_tfm_ctx));
  25. algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
  26. op->ss = algt->ss;
  27. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  28. sizeof(struct sun4i_req_ctx));
  29. return 0;
  30. }
  31. /* sun4i_hash_init: initialize request context */
  32. int sun4i_hash_init(struct ahash_request *areq)
  33. {
  34. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  35. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  36. struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
  37. struct sun4i_ss_alg_template *algt;
  38. memset(op, 0, sizeof(struct sun4i_req_ctx));
  39. algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
  40. op->mode = algt->mode;
  41. return 0;
  42. }
  43. int sun4i_hash_export_md5(struct ahash_request *areq, void *out)
  44. {
  45. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  46. struct md5_state *octx = out;
  47. int i;
  48. octx->byte_count = op->byte_count + op->len;
  49. memcpy(octx->block, op->buf, op->len);
  50. if (op->byte_count > 0) {
  51. for (i = 0; i < 4; i++)
  52. octx->hash[i] = op->hash[i];
  53. } else {
  54. octx->hash[0] = SHA1_H0;
  55. octx->hash[1] = SHA1_H1;
  56. octx->hash[2] = SHA1_H2;
  57. octx->hash[3] = SHA1_H3;
  58. }
  59. return 0;
  60. }
  61. int sun4i_hash_import_md5(struct ahash_request *areq, const void *in)
  62. {
  63. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  64. const struct md5_state *ictx = in;
  65. int i;
  66. sun4i_hash_init(areq);
  67. op->byte_count = ictx->byte_count & ~0x3F;
  68. op->len = ictx->byte_count & 0x3F;
  69. memcpy(op->buf, ictx->block, op->len);
  70. for (i = 0; i < 4; i++)
  71. op->hash[i] = ictx->hash[i];
  72. return 0;
  73. }
  74. int sun4i_hash_export_sha1(struct ahash_request *areq, void *out)
  75. {
  76. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  77. struct sha1_state *octx = out;
  78. int i;
  79. octx->count = op->byte_count + op->len;
  80. memcpy(octx->buffer, op->buf, op->len);
  81. if (op->byte_count > 0) {
  82. for (i = 0; i < 5; i++)
  83. octx->state[i] = op->hash[i];
  84. } else {
  85. octx->state[0] = SHA1_H0;
  86. octx->state[1] = SHA1_H1;
  87. octx->state[2] = SHA1_H2;
  88. octx->state[3] = SHA1_H3;
  89. octx->state[4] = SHA1_H4;
  90. }
  91. return 0;
  92. }
  93. int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in)
  94. {
  95. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  96. const struct sha1_state *ictx = in;
  97. int i;
  98. sun4i_hash_init(areq);
  99. op->byte_count = ictx->count & ~0x3F;
  100. op->len = ictx->count & 0x3F;
  101. memcpy(op->buf, ictx->buffer, op->len);
  102. for (i = 0; i < 5; i++)
  103. op->hash[i] = ictx->state[i];
  104. return 0;
  105. }
  106. #define SS_HASH_UPDATE 1
  107. #define SS_HASH_FINAL 2
  108. /*
  109. * sun4i_hash_update: update hash engine
  110. *
  111. * Could be used for both SHA1 and MD5
  112. * Write data by step of 32bits and put then in the SS.
  113. *
  114. * Since we cannot leave partial data and hash state in the engine,
  115. * we need to get the hash state at the end of this function.
  116. * We can get the hash state every 64 bytes
  117. *
  118. * So the first work is to get the number of bytes to write to SS modulo 64
  119. * The extra bytes will go to a temporary buffer op->buf storing op->len bytes
  120. *
  121. * So at the begin of update()
  122. * if op->len + areq->nbytes < 64
  123. * => all data will be written to wait buffer (op->buf) and end=0
  124. * if not, write all data from op->buf to the device and position end to
  125. * complete to 64bytes
  126. *
  127. * example 1:
  128. * update1 60o => op->len=60
  129. * update2 60o => need one more word to have 64 bytes
  130. * end=4
  131. * so write all data from op->buf and one word of SGs
  132. * write remaining data in op->buf
  133. * final state op->len=56
  134. */
  135. static int sun4i_hash(struct ahash_request *areq)
  136. {
  137. u32 v, ivmode = 0;
  138. unsigned int i = 0;
  139. /*
  140. * i is the total bytes read from SGs, to be compared to areq->nbytes
  141. * i is important because we cannot rely on SG length since the sum of
  142. * SG->length could be greater than areq->nbytes
  143. */
  144. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  145. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  146. struct sun4i_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
  147. struct sun4i_ss_ctx *ss = tfmctx->ss;
  148. unsigned int in_i = 0; /* advancement in the current SG */
  149. unsigned int end;
  150. /*
  151. * end is the position when we need to stop writing to the device,
  152. * to be compared to i
  153. */
  154. int in_r, err = 0;
  155. unsigned int todo;
  156. u32 spaces, rx_cnt = SS_RX_DEFAULT;
  157. size_t copied = 0;
  158. struct sg_mapping_iter mi;
  159. unsigned int j = 0;
  160. int zeros;
  161. unsigned int index, padlen;
  162. __be64 bits;
  163. u32 bf[32];
  164. u32 wb = 0;
  165. unsigned int nwait, nbw = 0;
  166. struct scatterlist *in_sg = areq->src;
  167. dev_dbg(ss->dev, "%s %s bc=%llu len=%u mode=%x wl=%u h0=%0x",
  168. __func__, crypto_tfm_alg_name(areq->base.tfm),
  169. op->byte_count, areq->nbytes, op->mode,
  170. op->len, op->hash[0]);
  171. if (unlikely(areq->nbytes == 0) && (op->flags & SS_HASH_FINAL) == 0)
  172. return 0;
  173. /* protect against overflow */
  174. if (unlikely(areq->nbytes > UINT_MAX - op->len)) {
  175. dev_err(ss->dev, "Cannot process too large request\n");
  176. return -EINVAL;
  177. }
  178. if (op->len + areq->nbytes < 64 && (op->flags & SS_HASH_FINAL) == 0) {
  179. /* linearize data to op->buf */
  180. copied = sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
  181. op->buf + op->len, areq->nbytes, 0);
  182. op->len += copied;
  183. return 0;
  184. }
  185. spin_lock_bh(&ss->slock);
  186. /*
  187. * if some data have been processed before,
  188. * we need to restore the partial hash state
  189. */
  190. if (op->byte_count > 0) {
  191. ivmode = SS_IV_ARBITRARY;
  192. for (i = 0; i < 5; i++)
  193. writel(op->hash[i], ss->base + SS_IV0 + i * 4);
  194. }
  195. /* Enable the device */
  196. writel(op->mode | SS_ENABLED | ivmode, ss->base + SS_CTL);
  197. if ((op->flags & SS_HASH_UPDATE) == 0)
  198. goto hash_final;
  199. /* start of handling data */
  200. if ((op->flags & SS_HASH_FINAL) == 0) {
  201. end = ((areq->nbytes + op->len) / 64) * 64 - op->len;
  202. if (end > areq->nbytes || areq->nbytes - end > 63) {
  203. dev_err(ss->dev, "ERROR: Bound error %u %u\n",
  204. end, areq->nbytes);
  205. err = -EINVAL;
  206. goto release_ss;
  207. }
  208. } else {
  209. /* Since we have the flag final, we can go up to modulo 4 */
  210. end = ((areq->nbytes + op->len) / 4) * 4 - op->len;
  211. }
  212. /* TODO if SGlen % 4 and op->len == 0 then DMA */
  213. i = 1;
  214. while (in_sg && i == 1) {
  215. if ((in_sg->length % 4) != 0)
  216. i = 0;
  217. in_sg = sg_next(in_sg);
  218. }
  219. if (i == 1 && op->len == 0)
  220. dev_dbg(ss->dev, "We can DMA\n");
  221. i = 0;
  222. sg_miter_start(&mi, areq->src, sg_nents(areq->src),
  223. SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  224. sg_miter_next(&mi);
  225. in_i = 0;
  226. do {
  227. /*
  228. * we need to linearize in two case:
  229. * - the buffer is already used
  230. * - the SG does not have enough byte remaining ( < 4)
  231. */
  232. if (op->len > 0 || (mi.length - in_i) < 4) {
  233. /*
  234. * if we have entered here we have two reason to stop
  235. * - the buffer is full
  236. * - reach the end
  237. */
  238. while (op->len < 64 && i < end) {
  239. /* how many bytes we can read from current SG */
  240. in_r = min3(mi.length - in_i, end - i,
  241. 64 - op->len);
  242. memcpy(op->buf + op->len, mi.addr + in_i, in_r);
  243. op->len += in_r;
  244. i += in_r;
  245. in_i += in_r;
  246. if (in_i == mi.length) {
  247. sg_miter_next(&mi);
  248. in_i = 0;
  249. }
  250. }
  251. if (op->len > 3 && (op->len % 4) == 0) {
  252. /* write buf to the device */
  253. writesl(ss->base + SS_RXFIFO, op->buf,
  254. op->len / 4);
  255. op->byte_count += op->len;
  256. op->len = 0;
  257. }
  258. }
  259. if (mi.length - in_i > 3 && i < end) {
  260. /* how many bytes we can read from current SG */
  261. in_r = min3(mi.length - in_i, areq->nbytes - i,
  262. ((mi.length - in_i) / 4) * 4);
  263. /* how many bytes we can write in the device*/
  264. todo = min3((u32)(end - i) / 4, rx_cnt, (u32)in_r / 4);
  265. writesl(ss->base + SS_RXFIFO, mi.addr + in_i, todo);
  266. op->byte_count += todo * 4;
  267. i += todo * 4;
  268. in_i += todo * 4;
  269. rx_cnt -= todo;
  270. if (rx_cnt == 0) {
  271. spaces = readl(ss->base + SS_FCSR);
  272. rx_cnt = SS_RXFIFO_SPACES(spaces);
  273. }
  274. if (in_i == mi.length) {
  275. sg_miter_next(&mi);
  276. in_i = 0;
  277. }
  278. }
  279. } while (i < end);
  280. /*
  281. * Now we have written to the device all that we can,
  282. * store the remaining bytes in op->buf
  283. */
  284. if ((areq->nbytes - i) < 64) {
  285. while (i < areq->nbytes && in_i < mi.length && op->len < 64) {
  286. /* how many bytes we can read from current SG */
  287. in_r = min3(mi.length - in_i, areq->nbytes - i,
  288. 64 - op->len);
  289. memcpy(op->buf + op->len, mi.addr + in_i, in_r);
  290. op->len += in_r;
  291. i += in_r;
  292. in_i += in_r;
  293. if (in_i == mi.length) {
  294. sg_miter_next(&mi);
  295. in_i = 0;
  296. }
  297. }
  298. }
  299. sg_miter_stop(&mi);
  300. /*
  301. * End of data process
  302. * Now if we have the flag final go to finalize part
  303. * If not, store the partial hash
  304. */
  305. if ((op->flags & SS_HASH_FINAL) > 0)
  306. goto hash_final;
  307. writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
  308. i = 0;
  309. do {
  310. v = readl(ss->base + SS_CTL);
  311. i++;
  312. } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
  313. if (unlikely(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);
  317. err = -EIO;
  318. goto release_ss;
  319. }
  320. for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++)
  321. op->hash[i] = readl(ss->base + SS_MD0 + i * 4);
  322. goto release_ss;
  323. /*
  324. * hash_final: finalize hashing operation
  325. *
  326. * If we have some remaining bytes, we write them.
  327. * Then ask the SS for finalizing the hashing operation
  328. *
  329. * I do not check RX FIFO size in this function since the size is 32
  330. * after each enabling and this function neither write more than 32 words.
  331. * If we come from the update part, we cannot have more than
  332. * 3 remaining bytes to write and SS is fast enough to not care about it.
  333. */
  334. hash_final:
  335. /* write the remaining words of the wait buffer */
  336. if (op->len > 0) {
  337. nwait = op->len / 4;
  338. if (nwait > 0) {
  339. writesl(ss->base + SS_RXFIFO, op->buf, nwait);
  340. op->byte_count += 4 * nwait;
  341. }
  342. nbw = op->len - 4 * nwait;
  343. wb = *(u32 *)(op->buf + nwait * 4);
  344. wb &= (0xFFFFFFFF >> (4 - nbw) * 8);
  345. }
  346. /* write the remaining bytes of the nbw buffer */
  347. if (nbw > 0) {
  348. wb |= ((1 << 7) << (nbw * 8));
  349. bf[j++] = wb;
  350. } else {
  351. bf[j++] = 1 << 7;
  352. }
  353. /*
  354. * number of space to pad to obtain 64o minus 8(size) minus 4 (final 1)
  355. * I take the operations from other MD5/SHA1 implementations
  356. */
  357. /* we have already send 4 more byte of which nbw data */
  358. if (op->mode == SS_OP_MD5) {
  359. index = (op->byte_count + 4) & 0x3f;
  360. op->byte_count += nbw;
  361. if (index > 56)
  362. zeros = (120 - index) / 4;
  363. else
  364. zeros = (56 - index) / 4;
  365. } else {
  366. op->byte_count += nbw;
  367. index = op->byte_count & 0x3f;
  368. padlen = (index < 56) ? (56 - index) : ((64 + 56) - index);
  369. zeros = (padlen - 1) / 4;
  370. }
  371. memset(bf + j, 0, 4 * zeros);
  372. j += zeros;
  373. /* write the length of data */
  374. if (op->mode == SS_OP_SHA1) {
  375. bits = cpu_to_be64(op->byte_count << 3);
  376. bf[j++] = bits & 0xffffffff;
  377. bf[j++] = (bits >> 32) & 0xffffffff;
  378. } else {
  379. bf[j++] = (op->byte_count << 3) & 0xffffffff;
  380. bf[j++] = (op->byte_count >> 29) & 0xffffffff;
  381. }
  382. writesl(ss->base + SS_RXFIFO, bf, j);
  383. /* Tell the SS to stop the hashing */
  384. writel(op->mode | SS_ENABLED | SS_DATA_END, ss->base + SS_CTL);
  385. /*
  386. * Wait for SS to finish the hash.
  387. * The timeout could happen only in case of bad overclocking
  388. * or driver bug.
  389. */
  390. i = 0;
  391. do {
  392. v = readl(ss->base + SS_CTL);
  393. i++;
  394. } while (i < SS_TIMEOUT && (v & SS_DATA_END) > 0);
  395. if (unlikely(i >= SS_TIMEOUT)) {
  396. dev_err_ratelimited(ss->dev,
  397. "ERROR: hash end timeout %d>%d ctl=%x len=%u\n",
  398. i, SS_TIMEOUT, v, areq->nbytes);
  399. err = -EIO;
  400. goto release_ss;
  401. }
  402. /* Get the hash from the device */
  403. if (op->mode == SS_OP_SHA1) {
  404. for (i = 0; i < 5; i++) {
  405. v = cpu_to_be32(readl(ss->base + SS_MD0 + i * 4));
  406. memcpy(areq->result + i * 4, &v, 4);
  407. }
  408. } else {
  409. for (i = 0; i < 4; i++) {
  410. v = readl(ss->base + SS_MD0 + i * 4);
  411. memcpy(areq->result + i * 4, &v, 4);
  412. }
  413. }
  414. release_ss:
  415. writel(0, ss->base + SS_CTL);
  416. spin_unlock_bh(&ss->slock);
  417. return err;
  418. }
  419. int sun4i_hash_final(struct ahash_request *areq)
  420. {
  421. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  422. op->flags = SS_HASH_FINAL;
  423. return sun4i_hash(areq);
  424. }
  425. int sun4i_hash_update(struct ahash_request *areq)
  426. {
  427. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  428. op->flags = SS_HASH_UPDATE;
  429. return sun4i_hash(areq);
  430. }
  431. /* sun4i_hash_finup: finalize hashing operation after an update */
  432. int sun4i_hash_finup(struct ahash_request *areq)
  433. {
  434. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  435. op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;
  436. return sun4i_hash(areq);
  437. }
  438. /* combo of init/update/final functions */
  439. int sun4i_hash_digest(struct ahash_request *areq)
  440. {
  441. int err;
  442. struct sun4i_req_ctx *op = ahash_request_ctx(areq);
  443. err = sun4i_hash_init(areq);
  444. if (err != 0)
  445. return err;
  446. op->flags = SS_HASH_UPDATE | SS_HASH_FINAL;
  447. return sun4i_hash(areq);
  448. }