atmel-sha.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Support for ATMEL SHA1/SHA256 HW acceleration.
  6. *
  7. * Copyright (c) 2012 Eukréa Electromatique - ATMEL
  8. * Author: Nicolas Royer <nicolas@eukrea.com>
  9. *
  10. * Some ideas are from omap-sham.c drivers.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/clk.h>
  17. #include <linux/io.h>
  18. #include <linux/hw_random.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/device.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/init.h>
  23. #include <linux/errno.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/of_device.h>
  29. #include <linux/delay.h>
  30. #include <linux/crypto.h>
  31. #include <crypto/scatterwalk.h>
  32. #include <crypto/algapi.h>
  33. #include <crypto/sha1.h>
  34. #include <crypto/sha2.h>
  35. #include <crypto/hash.h>
  36. #include <crypto/internal/hash.h>
  37. #include "atmel-sha-regs.h"
  38. #include "atmel-authenc.h"
  39. #define ATMEL_SHA_PRIORITY 300
  40. /* SHA flags */
  41. #define SHA_FLAGS_BUSY BIT(0)
  42. #define SHA_FLAGS_FINAL BIT(1)
  43. #define SHA_FLAGS_DMA_ACTIVE BIT(2)
  44. #define SHA_FLAGS_OUTPUT_READY BIT(3)
  45. #define SHA_FLAGS_INIT BIT(4)
  46. #define SHA_FLAGS_CPU BIT(5)
  47. #define SHA_FLAGS_DMA_READY BIT(6)
  48. #define SHA_FLAGS_DUMP_REG BIT(7)
  49. /* bits[11:8] are reserved. */
  50. #define SHA_FLAGS_FINUP BIT(16)
  51. #define SHA_FLAGS_SG BIT(17)
  52. #define SHA_FLAGS_ERROR BIT(23)
  53. #define SHA_FLAGS_PAD BIT(24)
  54. #define SHA_FLAGS_RESTORE BIT(25)
  55. #define SHA_FLAGS_IDATAR0 BIT(26)
  56. #define SHA_FLAGS_WAIT_DATARDY BIT(27)
  57. #define SHA_OP_INIT 0
  58. #define SHA_OP_UPDATE 1
  59. #define SHA_OP_FINAL 2
  60. #define SHA_OP_DIGEST 3
  61. #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
  62. #define ATMEL_SHA_DMA_THRESHOLD 56
  63. struct atmel_sha_caps {
  64. bool has_dma;
  65. bool has_dualbuff;
  66. bool has_sha224;
  67. bool has_sha_384_512;
  68. bool has_uihv;
  69. bool has_hmac;
  70. };
  71. struct atmel_sha_dev;
  72. /*
  73. * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
  74. * tested by the ahash_prepare_alg() function.
  75. */
  76. struct atmel_sha_reqctx {
  77. struct atmel_sha_dev *dd;
  78. unsigned long flags;
  79. unsigned long op;
  80. u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
  81. u64 digcnt[2];
  82. size_t bufcnt;
  83. size_t buflen;
  84. dma_addr_t dma_addr;
  85. /* walk state */
  86. struct scatterlist *sg;
  87. unsigned int offset; /* offset in current sg */
  88. unsigned int total; /* total request */
  89. size_t block_size;
  90. size_t hash_size;
  91. u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
  92. };
  93. typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
  94. struct atmel_sha_ctx {
  95. struct atmel_sha_dev *dd;
  96. atmel_sha_fn_t start;
  97. unsigned long flags;
  98. };
  99. #define ATMEL_SHA_QUEUE_LENGTH 50
  100. struct atmel_sha_dma {
  101. struct dma_chan *chan;
  102. struct dma_slave_config dma_conf;
  103. struct scatterlist *sg;
  104. int nents;
  105. unsigned int last_sg_length;
  106. };
  107. struct atmel_sha_dev {
  108. struct list_head list;
  109. unsigned long phys_base;
  110. struct device *dev;
  111. struct clk *iclk;
  112. int irq;
  113. void __iomem *io_base;
  114. spinlock_t lock;
  115. struct tasklet_struct done_task;
  116. struct tasklet_struct queue_task;
  117. unsigned long flags;
  118. struct crypto_queue queue;
  119. struct ahash_request *req;
  120. bool is_async;
  121. bool force_complete;
  122. atmel_sha_fn_t resume;
  123. atmel_sha_fn_t cpu_transfer_complete;
  124. struct atmel_sha_dma dma_lch_in;
  125. struct atmel_sha_caps caps;
  126. struct scatterlist tmp;
  127. u32 hw_version;
  128. };
  129. struct atmel_sha_drv {
  130. struct list_head dev_list;
  131. spinlock_t lock;
  132. };
  133. static struct atmel_sha_drv atmel_sha = {
  134. .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
  135. .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
  136. };
  137. #ifdef VERBOSE_DEBUG
  138. static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
  139. {
  140. switch (offset) {
  141. case SHA_CR:
  142. return "CR";
  143. case SHA_MR:
  144. return "MR";
  145. case SHA_IER:
  146. return "IER";
  147. case SHA_IDR:
  148. return "IDR";
  149. case SHA_IMR:
  150. return "IMR";
  151. case SHA_ISR:
  152. return "ISR";
  153. case SHA_MSR:
  154. return "MSR";
  155. case SHA_BCR:
  156. return "BCR";
  157. case SHA_REG_DIN(0):
  158. case SHA_REG_DIN(1):
  159. case SHA_REG_DIN(2):
  160. case SHA_REG_DIN(3):
  161. case SHA_REG_DIN(4):
  162. case SHA_REG_DIN(5):
  163. case SHA_REG_DIN(6):
  164. case SHA_REG_DIN(7):
  165. case SHA_REG_DIN(8):
  166. case SHA_REG_DIN(9):
  167. case SHA_REG_DIN(10):
  168. case SHA_REG_DIN(11):
  169. case SHA_REG_DIN(12):
  170. case SHA_REG_DIN(13):
  171. case SHA_REG_DIN(14):
  172. case SHA_REG_DIN(15):
  173. snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
  174. break;
  175. case SHA_REG_DIGEST(0):
  176. case SHA_REG_DIGEST(1):
  177. case SHA_REG_DIGEST(2):
  178. case SHA_REG_DIGEST(3):
  179. case SHA_REG_DIGEST(4):
  180. case SHA_REG_DIGEST(5):
  181. case SHA_REG_DIGEST(6):
  182. case SHA_REG_DIGEST(7):
  183. case SHA_REG_DIGEST(8):
  184. case SHA_REG_DIGEST(9):
  185. case SHA_REG_DIGEST(10):
  186. case SHA_REG_DIGEST(11):
  187. case SHA_REG_DIGEST(12):
  188. case SHA_REG_DIGEST(13):
  189. case SHA_REG_DIGEST(14):
  190. case SHA_REG_DIGEST(15):
  191. if (wr)
  192. snprintf(tmp, sz, "IDATAR[%u]",
  193. 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
  194. else
  195. snprintf(tmp, sz, "ODATAR[%u]",
  196. (offset - SHA_REG_DIGEST(0)) >> 2);
  197. break;
  198. case SHA_HW_VERSION:
  199. return "HWVER";
  200. default:
  201. snprintf(tmp, sz, "0x%02x", offset);
  202. break;
  203. }
  204. return tmp;
  205. }
  206. #endif /* VERBOSE_DEBUG */
  207. static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
  208. {
  209. u32 value = readl_relaxed(dd->io_base + offset);
  210. #ifdef VERBOSE_DEBUG
  211. if (dd->flags & SHA_FLAGS_DUMP_REG) {
  212. char tmp[16];
  213. dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
  214. atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
  215. }
  216. #endif /* VERBOSE_DEBUG */
  217. return value;
  218. }
  219. static inline void atmel_sha_write(struct atmel_sha_dev *dd,
  220. u32 offset, u32 value)
  221. {
  222. #ifdef VERBOSE_DEBUG
  223. if (dd->flags & SHA_FLAGS_DUMP_REG) {
  224. char tmp[16];
  225. dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
  226. atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
  227. }
  228. #endif /* VERBOSE_DEBUG */
  229. writel_relaxed(value, dd->io_base + offset);
  230. }
  231. static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
  232. {
  233. struct ahash_request *req = dd->req;
  234. dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
  235. SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
  236. SHA_FLAGS_DUMP_REG);
  237. clk_disable(dd->iclk);
  238. if ((dd->is_async || dd->force_complete) && req->base.complete)
  239. req->base.complete(&req->base, err);
  240. /* handle new request */
  241. tasklet_schedule(&dd->queue_task);
  242. return err;
  243. }
  244. static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
  245. {
  246. size_t count;
  247. while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
  248. count = min(ctx->sg->length - ctx->offset, ctx->total);
  249. count = min(count, ctx->buflen - ctx->bufcnt);
  250. if (count <= 0) {
  251. /*
  252. * Check if count <= 0 because the buffer is full or
  253. * because the sg length is 0. In the latest case,
  254. * check if there is another sg in the list, a 0 length
  255. * sg doesn't necessarily mean the end of the sg list.
  256. */
  257. if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
  258. ctx->sg = sg_next(ctx->sg);
  259. continue;
  260. } else {
  261. break;
  262. }
  263. }
  264. scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
  265. ctx->offset, count, 0);
  266. ctx->bufcnt += count;
  267. ctx->offset += count;
  268. ctx->total -= count;
  269. if (ctx->offset == ctx->sg->length) {
  270. ctx->sg = sg_next(ctx->sg);
  271. if (ctx->sg)
  272. ctx->offset = 0;
  273. else
  274. ctx->total = 0;
  275. }
  276. }
  277. return 0;
  278. }
  279. /*
  280. * The purpose of this padding is to ensure that the padded message is a
  281. * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
  282. * The bit "1" is appended at the end of the message followed by
  283. * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
  284. * 128 bits block (SHA384/SHA512) equals to the message length in bits
  285. * is appended.
  286. *
  287. * For SHA1/SHA224/SHA256, padlen is calculated as followed:
  288. * - if message length < 56 bytes then padlen = 56 - message length
  289. * - else padlen = 64 + 56 - message length
  290. *
  291. * For SHA384/SHA512, padlen is calculated as followed:
  292. * - if message length < 112 bytes then padlen = 112 - message length
  293. * - else padlen = 128 + 112 - message length
  294. */
  295. static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
  296. {
  297. unsigned int index, padlen;
  298. __be64 bits[2];
  299. u64 size[2];
  300. size[0] = ctx->digcnt[0];
  301. size[1] = ctx->digcnt[1];
  302. size[0] += ctx->bufcnt;
  303. if (size[0] < ctx->bufcnt)
  304. size[1]++;
  305. size[0] += length;
  306. if (size[0] < length)
  307. size[1]++;
  308. bits[1] = cpu_to_be64(size[0] << 3);
  309. bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
  310. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  311. case SHA_FLAGS_SHA384:
  312. case SHA_FLAGS_SHA512:
  313. index = ctx->bufcnt & 0x7f;
  314. padlen = (index < 112) ? (112 - index) : ((128+112) - index);
  315. *(ctx->buffer + ctx->bufcnt) = 0x80;
  316. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  317. memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
  318. ctx->bufcnt += padlen + 16;
  319. ctx->flags |= SHA_FLAGS_PAD;
  320. break;
  321. default:
  322. index = ctx->bufcnt & 0x3f;
  323. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  324. *(ctx->buffer + ctx->bufcnt) = 0x80;
  325. memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
  326. memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
  327. ctx->bufcnt += padlen + 8;
  328. ctx->flags |= SHA_FLAGS_PAD;
  329. break;
  330. }
  331. }
  332. static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
  333. {
  334. struct atmel_sha_dev *dd = NULL;
  335. struct atmel_sha_dev *tmp;
  336. spin_lock_bh(&atmel_sha.lock);
  337. if (!tctx->dd) {
  338. list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
  339. dd = tmp;
  340. break;
  341. }
  342. tctx->dd = dd;
  343. } else {
  344. dd = tctx->dd;
  345. }
  346. spin_unlock_bh(&atmel_sha.lock);
  347. return dd;
  348. }
  349. static int atmel_sha_init(struct ahash_request *req)
  350. {
  351. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  352. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  353. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  354. struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
  355. ctx->dd = dd;
  356. ctx->flags = 0;
  357. dev_dbg(dd->dev, "init: digest size: %u\n",
  358. crypto_ahash_digestsize(tfm));
  359. switch (crypto_ahash_digestsize(tfm)) {
  360. case SHA1_DIGEST_SIZE:
  361. ctx->flags |= SHA_FLAGS_SHA1;
  362. ctx->block_size = SHA1_BLOCK_SIZE;
  363. break;
  364. case SHA224_DIGEST_SIZE:
  365. ctx->flags |= SHA_FLAGS_SHA224;
  366. ctx->block_size = SHA224_BLOCK_SIZE;
  367. break;
  368. case SHA256_DIGEST_SIZE:
  369. ctx->flags |= SHA_FLAGS_SHA256;
  370. ctx->block_size = SHA256_BLOCK_SIZE;
  371. break;
  372. case SHA384_DIGEST_SIZE:
  373. ctx->flags |= SHA_FLAGS_SHA384;
  374. ctx->block_size = SHA384_BLOCK_SIZE;
  375. break;
  376. case SHA512_DIGEST_SIZE:
  377. ctx->flags |= SHA_FLAGS_SHA512;
  378. ctx->block_size = SHA512_BLOCK_SIZE;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. ctx->bufcnt = 0;
  384. ctx->digcnt[0] = 0;
  385. ctx->digcnt[1] = 0;
  386. ctx->buflen = SHA_BUFFER_LEN;
  387. return 0;
  388. }
  389. static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
  390. {
  391. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  392. u32 valmr = SHA_MR_MODE_AUTO;
  393. unsigned int i, hashsize = 0;
  394. if (likely(dma)) {
  395. if (!dd->caps.has_dma)
  396. atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
  397. valmr = SHA_MR_MODE_PDC;
  398. if (dd->caps.has_dualbuff)
  399. valmr |= SHA_MR_DUALBUFF;
  400. } else {
  401. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  402. }
  403. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  404. case SHA_FLAGS_SHA1:
  405. valmr |= SHA_MR_ALGO_SHA1;
  406. hashsize = SHA1_DIGEST_SIZE;
  407. break;
  408. case SHA_FLAGS_SHA224:
  409. valmr |= SHA_MR_ALGO_SHA224;
  410. hashsize = SHA256_DIGEST_SIZE;
  411. break;
  412. case SHA_FLAGS_SHA256:
  413. valmr |= SHA_MR_ALGO_SHA256;
  414. hashsize = SHA256_DIGEST_SIZE;
  415. break;
  416. case SHA_FLAGS_SHA384:
  417. valmr |= SHA_MR_ALGO_SHA384;
  418. hashsize = SHA512_DIGEST_SIZE;
  419. break;
  420. case SHA_FLAGS_SHA512:
  421. valmr |= SHA_MR_ALGO_SHA512;
  422. hashsize = SHA512_DIGEST_SIZE;
  423. break;
  424. default:
  425. break;
  426. }
  427. /* Setting CR_FIRST only for the first iteration */
  428. if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
  429. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  430. } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
  431. const u32 *hash = (const u32 *)ctx->digest;
  432. /*
  433. * Restore the hardware context: update the User Initialize
  434. * Hash Value (UIHV) with the value saved when the latest
  435. * 'update' operation completed on this very same crypto
  436. * request.
  437. */
  438. ctx->flags &= ~SHA_FLAGS_RESTORE;
  439. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  440. for (i = 0; i < hashsize / sizeof(u32); ++i)
  441. atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
  442. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  443. valmr |= SHA_MR_UIHV;
  444. }
  445. /*
  446. * WARNING: If the UIHV feature is not available, the hardware CANNOT
  447. * process concurrent requests: the internal registers used to store
  448. * the hash/digest are still set to the partial digest output values
  449. * computed during the latest round.
  450. */
  451. atmel_sha_write(dd, SHA_MR, valmr);
  452. }
  453. static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
  454. atmel_sha_fn_t resume)
  455. {
  456. u32 isr = atmel_sha_read(dd, SHA_ISR);
  457. if (unlikely(isr & SHA_INT_DATARDY))
  458. return resume(dd);
  459. dd->resume = resume;
  460. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  461. return -EINPROGRESS;
  462. }
  463. static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
  464. size_t length, int final)
  465. {
  466. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  467. int count, len32;
  468. const u32 *buffer = (const u32 *)buf;
  469. dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
  470. ctx->digcnt[1], ctx->digcnt[0], length, final);
  471. atmel_sha_write_ctrl(dd, 0);
  472. /* should be non-zero before next lines to disable clocks later */
  473. ctx->digcnt[0] += length;
  474. if (ctx->digcnt[0] < length)
  475. ctx->digcnt[1]++;
  476. if (final)
  477. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  478. len32 = DIV_ROUND_UP(length, sizeof(u32));
  479. dd->flags |= SHA_FLAGS_CPU;
  480. for (count = 0; count < len32; count++)
  481. atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
  482. return -EINPROGRESS;
  483. }
  484. static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  485. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  486. {
  487. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  488. int len32;
  489. dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
  490. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  491. len32 = DIV_ROUND_UP(length1, sizeof(u32));
  492. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
  493. atmel_sha_write(dd, SHA_TPR, dma_addr1);
  494. atmel_sha_write(dd, SHA_TCR, len32);
  495. len32 = DIV_ROUND_UP(length2, sizeof(u32));
  496. atmel_sha_write(dd, SHA_TNPR, dma_addr2);
  497. atmel_sha_write(dd, SHA_TNCR, len32);
  498. atmel_sha_write_ctrl(dd, 1);
  499. /* should be non-zero before next lines to disable clocks later */
  500. ctx->digcnt[0] += length1;
  501. if (ctx->digcnt[0] < length1)
  502. ctx->digcnt[1]++;
  503. if (final)
  504. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  505. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  506. /* Start DMA transfer */
  507. atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
  508. return -EINPROGRESS;
  509. }
  510. static void atmel_sha_dma_callback(void *data)
  511. {
  512. struct atmel_sha_dev *dd = data;
  513. dd->is_async = true;
  514. /* dma_lch_in - completed - wait DATRDY */
  515. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  516. }
  517. static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  518. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  519. {
  520. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  521. struct dma_async_tx_descriptor *in_desc;
  522. struct scatterlist sg[2];
  523. dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
  524. ctx->digcnt[1], ctx->digcnt[0], length1, final);
  525. dd->dma_lch_in.dma_conf.src_maxburst = 16;
  526. dd->dma_lch_in.dma_conf.dst_maxburst = 16;
  527. dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
  528. if (length2) {
  529. sg_init_table(sg, 2);
  530. sg_dma_address(&sg[0]) = dma_addr1;
  531. sg_dma_len(&sg[0]) = length1;
  532. sg_dma_address(&sg[1]) = dma_addr2;
  533. sg_dma_len(&sg[1]) = length2;
  534. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
  535. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  536. } else {
  537. sg_init_table(sg, 1);
  538. sg_dma_address(&sg[0]) = dma_addr1;
  539. sg_dma_len(&sg[0]) = length1;
  540. in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
  541. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  542. }
  543. if (!in_desc)
  544. return atmel_sha_complete(dd, -EINVAL);
  545. in_desc->callback = atmel_sha_dma_callback;
  546. in_desc->callback_param = dd;
  547. atmel_sha_write_ctrl(dd, 1);
  548. /* should be non-zero before next lines to disable clocks later */
  549. ctx->digcnt[0] += length1;
  550. if (ctx->digcnt[0] < length1)
  551. ctx->digcnt[1]++;
  552. if (final)
  553. dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
  554. dd->flags |= SHA_FLAGS_DMA_ACTIVE;
  555. /* Start DMA transfer */
  556. dmaengine_submit(in_desc);
  557. dma_async_issue_pending(dd->dma_lch_in.chan);
  558. return -EINPROGRESS;
  559. }
  560. static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
  561. size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
  562. {
  563. if (dd->caps.has_dma)
  564. return atmel_sha_xmit_dma(dd, dma_addr1, length1,
  565. dma_addr2, length2, final);
  566. else
  567. return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
  568. dma_addr2, length2, final);
  569. }
  570. static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
  571. {
  572. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  573. int bufcnt;
  574. atmel_sha_append_sg(ctx);
  575. atmel_sha_fill_padding(ctx, 0);
  576. bufcnt = ctx->bufcnt;
  577. ctx->bufcnt = 0;
  578. return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
  579. }
  580. static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
  581. struct atmel_sha_reqctx *ctx,
  582. size_t length, int final)
  583. {
  584. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  585. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  586. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  587. dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
  588. ctx->block_size);
  589. return atmel_sha_complete(dd, -EINVAL);
  590. }
  591. ctx->flags &= ~SHA_FLAGS_SG;
  592. /* next call does not fail... so no unmap in the case of error */
  593. return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
  594. }
  595. static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
  596. {
  597. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  598. unsigned int final;
  599. size_t count;
  600. atmel_sha_append_sg(ctx);
  601. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  602. dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
  603. ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
  604. if (final)
  605. atmel_sha_fill_padding(ctx, 0);
  606. if (final || (ctx->bufcnt == ctx->buflen)) {
  607. count = ctx->bufcnt;
  608. ctx->bufcnt = 0;
  609. return atmel_sha_xmit_dma_map(dd, ctx, count, final);
  610. }
  611. return 0;
  612. }
  613. static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
  614. {
  615. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  616. unsigned int length, final, tail;
  617. struct scatterlist *sg;
  618. unsigned int count;
  619. if (!ctx->total)
  620. return 0;
  621. if (ctx->bufcnt || ctx->offset)
  622. return atmel_sha_update_dma_slow(dd);
  623. dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
  624. ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
  625. sg = ctx->sg;
  626. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  627. return atmel_sha_update_dma_slow(dd);
  628. if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
  629. /* size is not ctx->block_size aligned */
  630. return atmel_sha_update_dma_slow(dd);
  631. length = min(ctx->total, sg->length);
  632. if (sg_is_last(sg)) {
  633. if (!(ctx->flags & SHA_FLAGS_FINUP)) {
  634. /* not last sg must be ctx->block_size aligned */
  635. tail = length & (ctx->block_size - 1);
  636. length -= tail;
  637. }
  638. }
  639. ctx->total -= length;
  640. ctx->offset = length; /* offset where to start slow */
  641. final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
  642. /* Add padding */
  643. if (final) {
  644. tail = length & (ctx->block_size - 1);
  645. length -= tail;
  646. ctx->total += tail;
  647. ctx->offset = length; /* offset where to start slow */
  648. sg = ctx->sg;
  649. atmel_sha_append_sg(ctx);
  650. atmel_sha_fill_padding(ctx, length);
  651. ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
  652. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  653. if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
  654. dev_err(dd->dev, "dma %zu bytes error\n",
  655. ctx->buflen + ctx->block_size);
  656. return atmel_sha_complete(dd, -EINVAL);
  657. }
  658. if (length == 0) {
  659. ctx->flags &= ~SHA_FLAGS_SG;
  660. count = ctx->bufcnt;
  661. ctx->bufcnt = 0;
  662. return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
  663. 0, final);
  664. } else {
  665. ctx->sg = sg;
  666. if (!dma_map_sg(dd->dev, ctx->sg, 1,
  667. DMA_TO_DEVICE)) {
  668. dev_err(dd->dev, "dma_map_sg error\n");
  669. return atmel_sha_complete(dd, -EINVAL);
  670. }
  671. ctx->flags |= SHA_FLAGS_SG;
  672. count = ctx->bufcnt;
  673. ctx->bufcnt = 0;
  674. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
  675. length, ctx->dma_addr, count, final);
  676. }
  677. }
  678. if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
  679. dev_err(dd->dev, "dma_map_sg error\n");
  680. return atmel_sha_complete(dd, -EINVAL);
  681. }
  682. ctx->flags |= SHA_FLAGS_SG;
  683. /* next call does not fail... so no unmap in the case of error */
  684. return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
  685. 0, final);
  686. }
  687. static void atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
  688. {
  689. struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
  690. if (ctx->flags & SHA_FLAGS_SG) {
  691. dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
  692. if (ctx->sg->length == ctx->offset) {
  693. ctx->sg = sg_next(ctx->sg);
  694. if (ctx->sg)
  695. ctx->offset = 0;
  696. }
  697. if (ctx->flags & SHA_FLAGS_PAD) {
  698. dma_unmap_single(dd->dev, ctx->dma_addr,
  699. ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
  700. }
  701. } else {
  702. dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
  703. ctx->block_size, DMA_TO_DEVICE);
  704. }
  705. }
  706. static int atmel_sha_update_req(struct atmel_sha_dev *dd)
  707. {
  708. struct ahash_request *req = dd->req;
  709. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  710. int err;
  711. dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
  712. ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
  713. if (ctx->flags & SHA_FLAGS_CPU)
  714. err = atmel_sha_update_cpu(dd);
  715. else
  716. err = atmel_sha_update_dma_start(dd);
  717. /* wait for dma completion before can take more data */
  718. dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
  719. err, ctx->digcnt[1], ctx->digcnt[0]);
  720. return err;
  721. }
  722. static int atmel_sha_final_req(struct atmel_sha_dev *dd)
  723. {
  724. struct ahash_request *req = dd->req;
  725. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  726. int err = 0;
  727. int count;
  728. if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
  729. atmel_sha_fill_padding(ctx, 0);
  730. count = ctx->bufcnt;
  731. ctx->bufcnt = 0;
  732. err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
  733. }
  734. /* faster to handle last block with cpu */
  735. else {
  736. atmel_sha_fill_padding(ctx, 0);
  737. count = ctx->bufcnt;
  738. ctx->bufcnt = 0;
  739. err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
  740. }
  741. dev_dbg(dd->dev, "final_req: err: %d\n", err);
  742. return err;
  743. }
  744. static void atmel_sha_copy_hash(struct ahash_request *req)
  745. {
  746. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  747. u32 *hash = (u32 *)ctx->digest;
  748. unsigned int i, hashsize;
  749. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  750. case SHA_FLAGS_SHA1:
  751. hashsize = SHA1_DIGEST_SIZE;
  752. break;
  753. case SHA_FLAGS_SHA224:
  754. case SHA_FLAGS_SHA256:
  755. hashsize = SHA256_DIGEST_SIZE;
  756. break;
  757. case SHA_FLAGS_SHA384:
  758. case SHA_FLAGS_SHA512:
  759. hashsize = SHA512_DIGEST_SIZE;
  760. break;
  761. default:
  762. /* Should not happen... */
  763. return;
  764. }
  765. for (i = 0; i < hashsize / sizeof(u32); ++i)
  766. hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
  767. ctx->flags |= SHA_FLAGS_RESTORE;
  768. }
  769. static void atmel_sha_copy_ready_hash(struct ahash_request *req)
  770. {
  771. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  772. if (!req->result)
  773. return;
  774. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  775. default:
  776. case SHA_FLAGS_SHA1:
  777. memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
  778. break;
  779. case SHA_FLAGS_SHA224:
  780. memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
  781. break;
  782. case SHA_FLAGS_SHA256:
  783. memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
  784. break;
  785. case SHA_FLAGS_SHA384:
  786. memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
  787. break;
  788. case SHA_FLAGS_SHA512:
  789. memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
  790. break;
  791. }
  792. }
  793. static int atmel_sha_finish(struct ahash_request *req)
  794. {
  795. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  796. struct atmel_sha_dev *dd = ctx->dd;
  797. if (ctx->digcnt[0] || ctx->digcnt[1])
  798. atmel_sha_copy_ready_hash(req);
  799. dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
  800. ctx->digcnt[0], ctx->bufcnt);
  801. return 0;
  802. }
  803. static void atmel_sha_finish_req(struct ahash_request *req, int err)
  804. {
  805. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  806. struct atmel_sha_dev *dd = ctx->dd;
  807. if (!err) {
  808. atmel_sha_copy_hash(req);
  809. if (SHA_FLAGS_FINAL & dd->flags)
  810. err = atmel_sha_finish(req);
  811. } else {
  812. ctx->flags |= SHA_FLAGS_ERROR;
  813. }
  814. /* atomic operation is not needed here */
  815. (void)atmel_sha_complete(dd, err);
  816. }
  817. static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
  818. {
  819. int err;
  820. err = clk_enable(dd->iclk);
  821. if (err)
  822. return err;
  823. if (!(SHA_FLAGS_INIT & dd->flags)) {
  824. atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
  825. dd->flags |= SHA_FLAGS_INIT;
  826. }
  827. return 0;
  828. }
  829. static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
  830. {
  831. return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
  832. }
  833. static int atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
  834. {
  835. int err;
  836. err = atmel_sha_hw_init(dd);
  837. if (err)
  838. return err;
  839. dd->hw_version = atmel_sha_get_version(dd);
  840. dev_info(dd->dev,
  841. "version: 0x%x\n", dd->hw_version);
  842. clk_disable(dd->iclk);
  843. return 0;
  844. }
  845. static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
  846. struct ahash_request *req)
  847. {
  848. struct crypto_async_request *async_req, *backlog;
  849. struct atmel_sha_ctx *ctx;
  850. unsigned long flags;
  851. bool start_async;
  852. int err = 0, ret = 0;
  853. spin_lock_irqsave(&dd->lock, flags);
  854. if (req)
  855. ret = ahash_enqueue_request(&dd->queue, req);
  856. if (SHA_FLAGS_BUSY & dd->flags) {
  857. spin_unlock_irqrestore(&dd->lock, flags);
  858. return ret;
  859. }
  860. backlog = crypto_get_backlog(&dd->queue);
  861. async_req = crypto_dequeue_request(&dd->queue);
  862. if (async_req)
  863. dd->flags |= SHA_FLAGS_BUSY;
  864. spin_unlock_irqrestore(&dd->lock, flags);
  865. if (!async_req)
  866. return ret;
  867. if (backlog)
  868. backlog->complete(backlog, -EINPROGRESS);
  869. ctx = crypto_tfm_ctx(async_req->tfm);
  870. dd->req = ahash_request_cast(async_req);
  871. start_async = (dd->req != req);
  872. dd->is_async = start_async;
  873. dd->force_complete = false;
  874. /* WARNING: ctx->start() MAY change dd->is_async. */
  875. err = ctx->start(dd);
  876. return (start_async) ? ret : err;
  877. }
  878. static int atmel_sha_done(struct atmel_sha_dev *dd);
  879. static int atmel_sha_start(struct atmel_sha_dev *dd)
  880. {
  881. struct ahash_request *req = dd->req;
  882. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  883. int err;
  884. dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %u\n",
  885. ctx->op, req->nbytes);
  886. err = atmel_sha_hw_init(dd);
  887. if (err)
  888. return atmel_sha_complete(dd, err);
  889. /*
  890. * atmel_sha_update_req() and atmel_sha_final_req() can return either:
  891. * -EINPROGRESS: the hardware is busy and the SHA driver will resume
  892. * its job later in the done_task.
  893. * This is the main path.
  894. *
  895. * 0: the SHA driver can continue its job then release the hardware
  896. * later, if needed, with atmel_sha_finish_req().
  897. * This is the alternate path.
  898. *
  899. * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
  900. * been called, hence the hardware has been released.
  901. * The SHA driver must stop its job without calling
  902. * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
  903. * called a second time.
  904. *
  905. * Please note that currently, atmel_sha_final_req() never returns 0.
  906. */
  907. dd->resume = atmel_sha_done;
  908. if (ctx->op == SHA_OP_UPDATE) {
  909. err = atmel_sha_update_req(dd);
  910. if (!err && (ctx->flags & SHA_FLAGS_FINUP))
  911. /* no final() after finup() */
  912. err = atmel_sha_final_req(dd);
  913. } else if (ctx->op == SHA_OP_FINAL) {
  914. err = atmel_sha_final_req(dd);
  915. }
  916. if (!err)
  917. /* done_task will not finish it, so do it here */
  918. atmel_sha_finish_req(req, err);
  919. dev_dbg(dd->dev, "exit, err: %d\n", err);
  920. return err;
  921. }
  922. static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
  923. {
  924. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  925. struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
  926. struct atmel_sha_dev *dd = tctx->dd;
  927. ctx->op = op;
  928. return atmel_sha_handle_queue(dd, req);
  929. }
  930. static int atmel_sha_update(struct ahash_request *req)
  931. {
  932. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  933. if (!req->nbytes)
  934. return 0;
  935. ctx->total = req->nbytes;
  936. ctx->sg = req->src;
  937. ctx->offset = 0;
  938. if (ctx->flags & SHA_FLAGS_FINUP) {
  939. if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
  940. /* faster to use CPU for short transfers */
  941. ctx->flags |= SHA_FLAGS_CPU;
  942. } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
  943. atmel_sha_append_sg(ctx);
  944. return 0;
  945. }
  946. return atmel_sha_enqueue(req, SHA_OP_UPDATE);
  947. }
  948. static int atmel_sha_final(struct ahash_request *req)
  949. {
  950. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  951. ctx->flags |= SHA_FLAGS_FINUP;
  952. if (ctx->flags & SHA_FLAGS_ERROR)
  953. return 0; /* uncompleted hash is not needed */
  954. if (ctx->flags & SHA_FLAGS_PAD)
  955. /* copy ready hash (+ finalize hmac) */
  956. return atmel_sha_finish(req);
  957. return atmel_sha_enqueue(req, SHA_OP_FINAL);
  958. }
  959. static int atmel_sha_finup(struct ahash_request *req)
  960. {
  961. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  962. int err1, err2;
  963. ctx->flags |= SHA_FLAGS_FINUP;
  964. err1 = atmel_sha_update(req);
  965. if (err1 == -EINPROGRESS ||
  966. (err1 == -EBUSY && (ahash_request_flags(req) &
  967. CRYPTO_TFM_REQ_MAY_BACKLOG)))
  968. return err1;
  969. /*
  970. * final() has to be always called to cleanup resources
  971. * even if udpate() failed, except EINPROGRESS
  972. */
  973. err2 = atmel_sha_final(req);
  974. return err1 ?: err2;
  975. }
  976. static int atmel_sha_digest(struct ahash_request *req)
  977. {
  978. return atmel_sha_init(req) ?: atmel_sha_finup(req);
  979. }
  980. static int atmel_sha_export(struct ahash_request *req, void *out)
  981. {
  982. const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  983. memcpy(out, ctx, sizeof(*ctx));
  984. return 0;
  985. }
  986. static int atmel_sha_import(struct ahash_request *req, const void *in)
  987. {
  988. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  989. memcpy(ctx, in, sizeof(*ctx));
  990. return 0;
  991. }
  992. static int atmel_sha_cra_init(struct crypto_tfm *tfm)
  993. {
  994. struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  995. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  996. sizeof(struct atmel_sha_reqctx));
  997. ctx->start = atmel_sha_start;
  998. return 0;
  999. }
  1000. static void atmel_sha_alg_init(struct ahash_alg *alg)
  1001. {
  1002. alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
  1003. alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  1004. alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_ctx);
  1005. alg->halg.base.cra_module = THIS_MODULE;
  1006. alg->halg.base.cra_init = atmel_sha_cra_init;
  1007. alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
  1008. alg->init = atmel_sha_init;
  1009. alg->update = atmel_sha_update;
  1010. alg->final = atmel_sha_final;
  1011. alg->finup = atmel_sha_finup;
  1012. alg->digest = atmel_sha_digest;
  1013. alg->export = atmel_sha_export;
  1014. alg->import = atmel_sha_import;
  1015. }
  1016. static struct ahash_alg sha_1_256_algs[] = {
  1017. {
  1018. .halg.base.cra_name = "sha1",
  1019. .halg.base.cra_driver_name = "atmel-sha1",
  1020. .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
  1021. .halg.digestsize = SHA1_DIGEST_SIZE,
  1022. },
  1023. {
  1024. .halg.base.cra_name = "sha256",
  1025. .halg.base.cra_driver_name = "atmel-sha256",
  1026. .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
  1027. .halg.digestsize = SHA256_DIGEST_SIZE,
  1028. },
  1029. };
  1030. static struct ahash_alg sha_224_alg = {
  1031. .halg.base.cra_name = "sha224",
  1032. .halg.base.cra_driver_name = "atmel-sha224",
  1033. .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
  1034. .halg.digestsize = SHA224_DIGEST_SIZE,
  1035. };
  1036. static struct ahash_alg sha_384_512_algs[] = {
  1037. {
  1038. .halg.base.cra_name = "sha384",
  1039. .halg.base.cra_driver_name = "atmel-sha384",
  1040. .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
  1041. .halg.base.cra_alignmask = 0x3,
  1042. .halg.digestsize = SHA384_DIGEST_SIZE,
  1043. },
  1044. {
  1045. .halg.base.cra_name = "sha512",
  1046. .halg.base.cra_driver_name = "atmel-sha512",
  1047. .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
  1048. .halg.base.cra_alignmask = 0x3,
  1049. .halg.digestsize = SHA512_DIGEST_SIZE,
  1050. },
  1051. };
  1052. static void atmel_sha_queue_task(unsigned long data)
  1053. {
  1054. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  1055. atmel_sha_handle_queue(dd, NULL);
  1056. }
  1057. static int atmel_sha_done(struct atmel_sha_dev *dd)
  1058. {
  1059. int err = 0;
  1060. if (SHA_FLAGS_CPU & dd->flags) {
  1061. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  1062. dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
  1063. goto finish;
  1064. }
  1065. } else if (SHA_FLAGS_DMA_READY & dd->flags) {
  1066. if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
  1067. dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
  1068. atmel_sha_update_dma_stop(dd);
  1069. }
  1070. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  1071. /* hash or semi-hash ready */
  1072. dd->flags &= ~(SHA_FLAGS_DMA_READY |
  1073. SHA_FLAGS_OUTPUT_READY);
  1074. err = atmel_sha_update_dma_start(dd);
  1075. if (err != -EINPROGRESS)
  1076. goto finish;
  1077. }
  1078. }
  1079. return err;
  1080. finish:
  1081. /* finish curent request */
  1082. atmel_sha_finish_req(dd->req, err);
  1083. return err;
  1084. }
  1085. static void atmel_sha_done_task(unsigned long data)
  1086. {
  1087. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  1088. dd->is_async = true;
  1089. (void)dd->resume(dd);
  1090. }
  1091. static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
  1092. {
  1093. struct atmel_sha_dev *sha_dd = dev_id;
  1094. u32 reg;
  1095. reg = atmel_sha_read(sha_dd, SHA_ISR);
  1096. if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
  1097. atmel_sha_write(sha_dd, SHA_IDR, reg);
  1098. if (SHA_FLAGS_BUSY & sha_dd->flags) {
  1099. sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
  1100. if (!(SHA_FLAGS_CPU & sha_dd->flags))
  1101. sha_dd->flags |= SHA_FLAGS_DMA_READY;
  1102. tasklet_schedule(&sha_dd->done_task);
  1103. } else {
  1104. dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
  1105. }
  1106. return IRQ_HANDLED;
  1107. }
  1108. return IRQ_NONE;
  1109. }
  1110. /* DMA transfer functions */
  1111. static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
  1112. struct scatterlist *sg,
  1113. size_t len)
  1114. {
  1115. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1116. struct ahash_request *req = dd->req;
  1117. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1118. size_t bs = ctx->block_size;
  1119. int nents;
  1120. for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  1121. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  1122. return false;
  1123. /*
  1124. * This is the last sg, the only one that is allowed to
  1125. * have an unaligned length.
  1126. */
  1127. if (len <= sg->length) {
  1128. dma->nents = nents + 1;
  1129. dma->last_sg_length = sg->length;
  1130. sg->length = ALIGN(len, sizeof(u32));
  1131. return true;
  1132. }
  1133. /* All other sg lengths MUST be aligned to the block size. */
  1134. if (!IS_ALIGNED(sg->length, bs))
  1135. return false;
  1136. len -= sg->length;
  1137. }
  1138. return false;
  1139. }
  1140. static void atmel_sha_dma_callback2(void *data)
  1141. {
  1142. struct atmel_sha_dev *dd = data;
  1143. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1144. struct scatterlist *sg;
  1145. int nents;
  1146. dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1147. sg = dma->sg;
  1148. for (nents = 0; nents < dma->nents - 1; ++nents)
  1149. sg = sg_next(sg);
  1150. sg->length = dma->last_sg_length;
  1151. dd->is_async = true;
  1152. (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
  1153. }
  1154. static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
  1155. struct scatterlist *src,
  1156. size_t len,
  1157. atmel_sha_fn_t resume)
  1158. {
  1159. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1160. struct dma_slave_config *config = &dma->dma_conf;
  1161. struct dma_chan *chan = dma->chan;
  1162. struct dma_async_tx_descriptor *desc;
  1163. dma_cookie_t cookie;
  1164. unsigned int sg_len;
  1165. int err;
  1166. dd->resume = resume;
  1167. /*
  1168. * dma->nents has already been initialized by
  1169. * atmel_sha_dma_check_aligned().
  1170. */
  1171. dma->sg = src;
  1172. sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1173. if (!sg_len) {
  1174. err = -ENOMEM;
  1175. goto exit;
  1176. }
  1177. config->src_maxburst = 16;
  1178. config->dst_maxburst = 16;
  1179. err = dmaengine_slave_config(chan, config);
  1180. if (err)
  1181. goto unmap_sg;
  1182. desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
  1183. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1184. if (!desc) {
  1185. err = -ENOMEM;
  1186. goto unmap_sg;
  1187. }
  1188. desc->callback = atmel_sha_dma_callback2;
  1189. desc->callback_param = dd;
  1190. cookie = dmaengine_submit(desc);
  1191. err = dma_submit_error(cookie);
  1192. if (err)
  1193. goto unmap_sg;
  1194. dma_async_issue_pending(chan);
  1195. return -EINPROGRESS;
  1196. unmap_sg:
  1197. dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1198. exit:
  1199. return atmel_sha_complete(dd, err);
  1200. }
  1201. /* CPU transfer functions */
  1202. static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
  1203. {
  1204. struct ahash_request *req = dd->req;
  1205. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1206. const u32 *words = (const u32 *)ctx->buffer;
  1207. size_t i, num_words;
  1208. u32 isr, din, din_inc;
  1209. din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
  1210. for (;;) {
  1211. /* Write data into the Input Data Registers. */
  1212. num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
  1213. for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
  1214. atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
  1215. ctx->offset += ctx->bufcnt;
  1216. ctx->total -= ctx->bufcnt;
  1217. if (!ctx->total)
  1218. break;
  1219. /*
  1220. * Prepare next block:
  1221. * Fill ctx->buffer now with the next data to be written into
  1222. * IDATARx: it gives time for the SHA hardware to process
  1223. * the current data so the SHA_INT_DATARDY flag might be set
  1224. * in SHA_ISR when polling this register at the beginning of
  1225. * the next loop.
  1226. */
  1227. ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  1228. scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  1229. ctx->offset, ctx->bufcnt, 0);
  1230. /* Wait for hardware to be ready again. */
  1231. isr = atmel_sha_read(dd, SHA_ISR);
  1232. if (!(isr & SHA_INT_DATARDY)) {
  1233. /* Not ready yet. */
  1234. dd->resume = atmel_sha_cpu_transfer;
  1235. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  1236. return -EINPROGRESS;
  1237. }
  1238. }
  1239. if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
  1240. return dd->cpu_transfer_complete(dd);
  1241. return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
  1242. }
  1243. static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
  1244. struct scatterlist *sg,
  1245. unsigned int len,
  1246. bool idatar0_only,
  1247. bool wait_data_ready,
  1248. atmel_sha_fn_t resume)
  1249. {
  1250. struct ahash_request *req = dd->req;
  1251. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1252. if (!len)
  1253. return resume(dd);
  1254. ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
  1255. if (idatar0_only)
  1256. ctx->flags |= SHA_FLAGS_IDATAR0;
  1257. if (wait_data_ready)
  1258. ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
  1259. ctx->sg = sg;
  1260. ctx->total = len;
  1261. ctx->offset = 0;
  1262. /* Prepare the first block to be written. */
  1263. ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  1264. scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  1265. ctx->offset, ctx->bufcnt, 0);
  1266. dd->cpu_transfer_complete = resume;
  1267. return atmel_sha_cpu_transfer(dd);
  1268. }
  1269. static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
  1270. const void *data, unsigned int datalen,
  1271. bool auto_padding,
  1272. atmel_sha_fn_t resume)
  1273. {
  1274. struct ahash_request *req = dd->req;
  1275. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1276. u32 msglen = (auto_padding) ? datalen : 0;
  1277. u32 mr = SHA_MR_MODE_AUTO;
  1278. if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
  1279. return atmel_sha_complete(dd, -EINVAL);
  1280. mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  1281. atmel_sha_write(dd, SHA_MR, mr);
  1282. atmel_sha_write(dd, SHA_MSR, msglen);
  1283. atmel_sha_write(dd, SHA_BCR, msglen);
  1284. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1285. sg_init_one(&dd->tmp, data, datalen);
  1286. return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
  1287. }
  1288. /* hmac functions */
  1289. struct atmel_sha_hmac_key {
  1290. bool valid;
  1291. unsigned int keylen;
  1292. u8 buffer[SHA512_BLOCK_SIZE];
  1293. u8 *keydup;
  1294. };
  1295. static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
  1296. {
  1297. memset(hkey, 0, sizeof(*hkey));
  1298. }
  1299. static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
  1300. {
  1301. kfree(hkey->keydup);
  1302. memset(hkey, 0, sizeof(*hkey));
  1303. }
  1304. static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
  1305. const u8 *key,
  1306. unsigned int keylen)
  1307. {
  1308. atmel_sha_hmac_key_release(hkey);
  1309. if (keylen > sizeof(hkey->buffer)) {
  1310. hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
  1311. if (!hkey->keydup)
  1312. return -ENOMEM;
  1313. } else {
  1314. memcpy(hkey->buffer, key, keylen);
  1315. }
  1316. hkey->valid = true;
  1317. hkey->keylen = keylen;
  1318. return 0;
  1319. }
  1320. static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
  1321. const u8 **key,
  1322. unsigned int *keylen)
  1323. {
  1324. if (!hkey->valid)
  1325. return false;
  1326. *keylen = hkey->keylen;
  1327. *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
  1328. return true;
  1329. }
  1330. struct atmel_sha_hmac_ctx {
  1331. struct atmel_sha_ctx base;
  1332. struct atmel_sha_hmac_key hkey;
  1333. u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
  1334. u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
  1335. atmel_sha_fn_t resume;
  1336. };
  1337. static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  1338. atmel_sha_fn_t resume);
  1339. static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  1340. const u8 *key, unsigned int keylen);
  1341. static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
  1342. static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
  1343. static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
  1344. static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
  1345. static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
  1346. static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
  1347. static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
  1348. static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
  1349. static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  1350. atmel_sha_fn_t resume)
  1351. {
  1352. struct ahash_request *req = dd->req;
  1353. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1354. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1355. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1356. unsigned int keylen;
  1357. const u8 *key;
  1358. size_t bs;
  1359. hmac->resume = resume;
  1360. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  1361. case SHA_FLAGS_SHA1:
  1362. ctx->block_size = SHA1_BLOCK_SIZE;
  1363. ctx->hash_size = SHA1_DIGEST_SIZE;
  1364. break;
  1365. case SHA_FLAGS_SHA224:
  1366. ctx->block_size = SHA224_BLOCK_SIZE;
  1367. ctx->hash_size = SHA256_DIGEST_SIZE;
  1368. break;
  1369. case SHA_FLAGS_SHA256:
  1370. ctx->block_size = SHA256_BLOCK_SIZE;
  1371. ctx->hash_size = SHA256_DIGEST_SIZE;
  1372. break;
  1373. case SHA_FLAGS_SHA384:
  1374. ctx->block_size = SHA384_BLOCK_SIZE;
  1375. ctx->hash_size = SHA512_DIGEST_SIZE;
  1376. break;
  1377. case SHA_FLAGS_SHA512:
  1378. ctx->block_size = SHA512_BLOCK_SIZE;
  1379. ctx->hash_size = SHA512_DIGEST_SIZE;
  1380. break;
  1381. default:
  1382. return atmel_sha_complete(dd, -EINVAL);
  1383. }
  1384. bs = ctx->block_size;
  1385. if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
  1386. return resume(dd);
  1387. /* Compute K' from K. */
  1388. if (unlikely(keylen > bs))
  1389. return atmel_sha_hmac_prehash_key(dd, key, keylen);
  1390. /* Prepare ipad. */
  1391. memcpy((u8 *)hmac->ipad, key, keylen);
  1392. memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
  1393. return atmel_sha_hmac_compute_ipad_hash(dd);
  1394. }
  1395. static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  1396. const u8 *key, unsigned int keylen)
  1397. {
  1398. return atmel_sha_cpu_hash(dd, key, keylen, true,
  1399. atmel_sha_hmac_prehash_key_done);
  1400. }
  1401. static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
  1402. {
  1403. struct ahash_request *req = dd->req;
  1404. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1405. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1406. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1407. size_t ds = crypto_ahash_digestsize(tfm);
  1408. size_t bs = ctx->block_size;
  1409. size_t i, num_words = ds / sizeof(u32);
  1410. /* Prepare ipad. */
  1411. for (i = 0; i < num_words; ++i)
  1412. hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1413. memset((u8 *)hmac->ipad + ds, 0, bs - ds);
  1414. return atmel_sha_hmac_compute_ipad_hash(dd);
  1415. }
  1416. static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
  1417. {
  1418. struct ahash_request *req = dd->req;
  1419. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1420. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1421. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1422. size_t bs = ctx->block_size;
  1423. size_t i, num_words = bs / sizeof(u32);
  1424. memcpy(hmac->opad, hmac->ipad, bs);
  1425. for (i = 0; i < num_words; ++i) {
  1426. hmac->ipad[i] ^= 0x36363636;
  1427. hmac->opad[i] ^= 0x5c5c5c5c;
  1428. }
  1429. return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
  1430. atmel_sha_hmac_compute_opad_hash);
  1431. }
  1432. static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
  1433. {
  1434. struct ahash_request *req = dd->req;
  1435. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1436. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1437. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1438. size_t bs = ctx->block_size;
  1439. size_t hs = ctx->hash_size;
  1440. size_t i, num_words = hs / sizeof(u32);
  1441. for (i = 0; i < num_words; ++i)
  1442. hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1443. return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
  1444. atmel_sha_hmac_setup_done);
  1445. }
  1446. static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
  1447. {
  1448. struct ahash_request *req = dd->req;
  1449. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1450. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1451. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1452. size_t hs = ctx->hash_size;
  1453. size_t i, num_words = hs / sizeof(u32);
  1454. for (i = 0; i < num_words; ++i)
  1455. hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1456. atmel_sha_hmac_key_release(&hmac->hkey);
  1457. return hmac->resume(dd);
  1458. }
  1459. static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
  1460. {
  1461. struct ahash_request *req = dd->req;
  1462. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1463. int err;
  1464. err = atmel_sha_hw_init(dd);
  1465. if (err)
  1466. return atmel_sha_complete(dd, err);
  1467. switch (ctx->op) {
  1468. case SHA_OP_INIT:
  1469. err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
  1470. break;
  1471. case SHA_OP_UPDATE:
  1472. dd->resume = atmel_sha_done;
  1473. err = atmel_sha_update_req(dd);
  1474. break;
  1475. case SHA_OP_FINAL:
  1476. dd->resume = atmel_sha_hmac_final;
  1477. err = atmel_sha_final_req(dd);
  1478. break;
  1479. case SHA_OP_DIGEST:
  1480. err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
  1481. break;
  1482. default:
  1483. return atmel_sha_complete(dd, -EINVAL);
  1484. }
  1485. return err;
  1486. }
  1487. static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  1488. unsigned int keylen)
  1489. {
  1490. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1491. return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
  1492. }
  1493. static int atmel_sha_hmac_init(struct ahash_request *req)
  1494. {
  1495. int err;
  1496. err = atmel_sha_init(req);
  1497. if (err)
  1498. return err;
  1499. return atmel_sha_enqueue(req, SHA_OP_INIT);
  1500. }
  1501. static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
  1502. {
  1503. struct ahash_request *req = dd->req;
  1504. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1505. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1506. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1507. size_t bs = ctx->block_size;
  1508. size_t hs = ctx->hash_size;
  1509. ctx->bufcnt = 0;
  1510. ctx->digcnt[0] = bs;
  1511. ctx->digcnt[1] = 0;
  1512. ctx->flags |= SHA_FLAGS_RESTORE;
  1513. memcpy(ctx->digest, hmac->ipad, hs);
  1514. return atmel_sha_complete(dd, 0);
  1515. }
  1516. static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
  1517. {
  1518. struct ahash_request *req = dd->req;
  1519. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1520. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1521. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1522. u32 *digest = (u32 *)ctx->digest;
  1523. size_t ds = crypto_ahash_digestsize(tfm);
  1524. size_t bs = ctx->block_size;
  1525. size_t hs = ctx->hash_size;
  1526. size_t i, num_words;
  1527. u32 mr;
  1528. /* Save d = SHA((K' + ipad) | msg). */
  1529. num_words = ds / sizeof(u32);
  1530. for (i = 0; i < num_words; ++i)
  1531. digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1532. /* Restore context to finish computing SHA((K' + opad) | d). */
  1533. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1534. num_words = hs / sizeof(u32);
  1535. for (i = 0; i < num_words; ++i)
  1536. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1537. mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
  1538. mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  1539. atmel_sha_write(dd, SHA_MR, mr);
  1540. atmel_sha_write(dd, SHA_MSR, bs + ds);
  1541. atmel_sha_write(dd, SHA_BCR, ds);
  1542. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1543. sg_init_one(&dd->tmp, digest, ds);
  1544. return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
  1545. atmel_sha_hmac_final_done);
  1546. }
  1547. static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
  1548. {
  1549. /*
  1550. * req->result might not be sizeof(u32) aligned, so copy the
  1551. * digest into ctx->digest[] before memcpy() the data into
  1552. * req->result.
  1553. */
  1554. atmel_sha_copy_hash(dd->req);
  1555. atmel_sha_copy_ready_hash(dd->req);
  1556. return atmel_sha_complete(dd, 0);
  1557. }
  1558. static int atmel_sha_hmac_digest(struct ahash_request *req)
  1559. {
  1560. int err;
  1561. err = atmel_sha_init(req);
  1562. if (err)
  1563. return err;
  1564. return atmel_sha_enqueue(req, SHA_OP_DIGEST);
  1565. }
  1566. static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
  1567. {
  1568. struct ahash_request *req = dd->req;
  1569. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1570. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1571. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1572. size_t hs = ctx->hash_size;
  1573. size_t i, num_words = hs / sizeof(u32);
  1574. bool use_dma = false;
  1575. u32 mr;
  1576. /* Special case for empty message. */
  1577. if (!req->nbytes)
  1578. return atmel_sha_complete(dd, -EINVAL); // TODO:
  1579. /* Check DMA threshold and alignment. */
  1580. if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
  1581. atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
  1582. use_dma = true;
  1583. /* Write both initial hash values to compute a HMAC. */
  1584. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1585. for (i = 0; i < num_words; ++i)
  1586. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  1587. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  1588. for (i = 0; i < num_words; ++i)
  1589. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1590. /* Write the Mode, Message Size, Bytes Count then Control Registers. */
  1591. mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
  1592. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  1593. if (use_dma)
  1594. mr |= SHA_MR_MODE_IDATAR0;
  1595. else
  1596. mr |= SHA_MR_MODE_AUTO;
  1597. atmel_sha_write(dd, SHA_MR, mr);
  1598. atmel_sha_write(dd, SHA_MSR, req->nbytes);
  1599. atmel_sha_write(dd, SHA_BCR, req->nbytes);
  1600. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1601. /* Process data. */
  1602. if (use_dma)
  1603. return atmel_sha_dma_start(dd, req->src, req->nbytes,
  1604. atmel_sha_hmac_final_done);
  1605. return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true,
  1606. atmel_sha_hmac_final_done);
  1607. }
  1608. static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
  1609. {
  1610. struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  1611. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1612. sizeof(struct atmel_sha_reqctx));
  1613. hmac->base.start = atmel_sha_hmac_start;
  1614. atmel_sha_hmac_key_init(&hmac->hkey);
  1615. return 0;
  1616. }
  1617. static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
  1618. {
  1619. struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  1620. atmel_sha_hmac_key_release(&hmac->hkey);
  1621. }
  1622. static void atmel_sha_hmac_alg_init(struct ahash_alg *alg)
  1623. {
  1624. alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
  1625. alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  1626. alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx);
  1627. alg->halg.base.cra_module = THIS_MODULE;
  1628. alg->halg.base.cra_init = atmel_sha_hmac_cra_init;
  1629. alg->halg.base.cra_exit = atmel_sha_hmac_cra_exit;
  1630. alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
  1631. alg->init = atmel_sha_hmac_init;
  1632. alg->update = atmel_sha_update;
  1633. alg->final = atmel_sha_final;
  1634. alg->digest = atmel_sha_hmac_digest;
  1635. alg->setkey = atmel_sha_hmac_setkey;
  1636. alg->export = atmel_sha_export;
  1637. alg->import = atmel_sha_import;
  1638. }
  1639. static struct ahash_alg sha_hmac_algs[] = {
  1640. {
  1641. .halg.base.cra_name = "hmac(sha1)",
  1642. .halg.base.cra_driver_name = "atmel-hmac-sha1",
  1643. .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
  1644. .halg.digestsize = SHA1_DIGEST_SIZE,
  1645. },
  1646. {
  1647. .halg.base.cra_name = "hmac(sha224)",
  1648. .halg.base.cra_driver_name = "atmel-hmac-sha224",
  1649. .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
  1650. .halg.digestsize = SHA224_DIGEST_SIZE,
  1651. },
  1652. {
  1653. .halg.base.cra_name = "hmac(sha256)",
  1654. .halg.base.cra_driver_name = "atmel-hmac-sha256",
  1655. .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
  1656. .halg.digestsize = SHA256_DIGEST_SIZE,
  1657. },
  1658. {
  1659. .halg.base.cra_name = "hmac(sha384)",
  1660. .halg.base.cra_driver_name = "atmel-hmac-sha384",
  1661. .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
  1662. .halg.digestsize = SHA384_DIGEST_SIZE,
  1663. },
  1664. {
  1665. .halg.base.cra_name = "hmac(sha512)",
  1666. .halg.base.cra_driver_name = "atmel-hmac-sha512",
  1667. .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
  1668. .halg.digestsize = SHA512_DIGEST_SIZE,
  1669. },
  1670. };
  1671. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  1672. /* authenc functions */
  1673. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
  1674. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
  1675. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
  1676. struct atmel_sha_authenc_ctx {
  1677. struct crypto_ahash *tfm;
  1678. };
  1679. struct atmel_sha_authenc_reqctx {
  1680. struct atmel_sha_reqctx base;
  1681. atmel_aes_authenc_fn_t cb;
  1682. struct atmel_aes_dev *aes_dev;
  1683. /* _init() parameters. */
  1684. struct scatterlist *assoc;
  1685. u32 assoclen;
  1686. u32 textlen;
  1687. /* _final() parameters. */
  1688. u32 *digest;
  1689. unsigned int digestlen;
  1690. };
  1691. static void atmel_sha_authenc_complete(struct crypto_async_request *areq,
  1692. int err)
  1693. {
  1694. struct ahash_request *req = areq->data;
  1695. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1696. authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
  1697. }
  1698. static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
  1699. {
  1700. struct ahash_request *req = dd->req;
  1701. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1702. int err;
  1703. /*
  1704. * Force atmel_sha_complete() to call req->base.complete(), ie
  1705. * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
  1706. */
  1707. dd->force_complete = true;
  1708. err = atmel_sha_hw_init(dd);
  1709. return authctx->cb(authctx->aes_dev, err, dd->is_async);
  1710. }
  1711. bool atmel_sha_authenc_is_ready(void)
  1712. {
  1713. struct atmel_sha_ctx dummy;
  1714. dummy.dd = NULL;
  1715. return (atmel_sha_find_dev(&dummy) != NULL);
  1716. }
  1717. EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
  1718. unsigned int atmel_sha_authenc_get_reqsize(void)
  1719. {
  1720. return sizeof(struct atmel_sha_authenc_reqctx);
  1721. }
  1722. EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
  1723. struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
  1724. {
  1725. struct atmel_sha_authenc_ctx *auth;
  1726. struct crypto_ahash *tfm;
  1727. struct atmel_sha_ctx *tctx;
  1728. const char *name;
  1729. int err = -EINVAL;
  1730. switch (mode & SHA_FLAGS_MODE_MASK) {
  1731. case SHA_FLAGS_HMAC_SHA1:
  1732. name = "atmel-hmac-sha1";
  1733. break;
  1734. case SHA_FLAGS_HMAC_SHA224:
  1735. name = "atmel-hmac-sha224";
  1736. break;
  1737. case SHA_FLAGS_HMAC_SHA256:
  1738. name = "atmel-hmac-sha256";
  1739. break;
  1740. case SHA_FLAGS_HMAC_SHA384:
  1741. name = "atmel-hmac-sha384";
  1742. break;
  1743. case SHA_FLAGS_HMAC_SHA512:
  1744. name = "atmel-hmac-sha512";
  1745. break;
  1746. default:
  1747. goto error;
  1748. }
  1749. tfm = crypto_alloc_ahash(name, 0, 0);
  1750. if (IS_ERR(tfm)) {
  1751. err = PTR_ERR(tfm);
  1752. goto error;
  1753. }
  1754. tctx = crypto_ahash_ctx(tfm);
  1755. tctx->start = atmel_sha_authenc_start;
  1756. tctx->flags = mode;
  1757. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  1758. if (!auth) {
  1759. err = -ENOMEM;
  1760. goto err_free_ahash;
  1761. }
  1762. auth->tfm = tfm;
  1763. return auth;
  1764. err_free_ahash:
  1765. crypto_free_ahash(tfm);
  1766. error:
  1767. return ERR_PTR(err);
  1768. }
  1769. EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
  1770. void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
  1771. {
  1772. if (auth)
  1773. crypto_free_ahash(auth->tfm);
  1774. kfree(auth);
  1775. }
  1776. EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
  1777. int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
  1778. const u8 *key, unsigned int keylen, u32 flags)
  1779. {
  1780. struct crypto_ahash *tfm = auth->tfm;
  1781. crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
  1782. crypto_ahash_set_flags(tfm, flags & CRYPTO_TFM_REQ_MASK);
  1783. return crypto_ahash_setkey(tfm, key, keylen);
  1784. }
  1785. EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
  1786. int atmel_sha_authenc_schedule(struct ahash_request *req,
  1787. struct atmel_sha_authenc_ctx *auth,
  1788. atmel_aes_authenc_fn_t cb,
  1789. struct atmel_aes_dev *aes_dev)
  1790. {
  1791. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1792. struct atmel_sha_reqctx *ctx = &authctx->base;
  1793. struct crypto_ahash *tfm = auth->tfm;
  1794. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  1795. struct atmel_sha_dev *dd;
  1796. /* Reset request context (MUST be done first). */
  1797. memset(authctx, 0, sizeof(*authctx));
  1798. /* Get SHA device. */
  1799. dd = atmel_sha_find_dev(tctx);
  1800. if (!dd)
  1801. return cb(aes_dev, -ENODEV, false);
  1802. /* Init request context. */
  1803. ctx->dd = dd;
  1804. ctx->buflen = SHA_BUFFER_LEN;
  1805. authctx->cb = cb;
  1806. authctx->aes_dev = aes_dev;
  1807. ahash_request_set_tfm(req, tfm);
  1808. ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
  1809. return atmel_sha_handle_queue(dd, req);
  1810. }
  1811. EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
  1812. int atmel_sha_authenc_init(struct ahash_request *req,
  1813. struct scatterlist *assoc, unsigned int assoclen,
  1814. unsigned int textlen,
  1815. atmel_aes_authenc_fn_t cb,
  1816. struct atmel_aes_dev *aes_dev)
  1817. {
  1818. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1819. struct atmel_sha_reqctx *ctx = &authctx->base;
  1820. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1821. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1822. struct atmel_sha_dev *dd = ctx->dd;
  1823. if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
  1824. return atmel_sha_complete(dd, -EINVAL);
  1825. authctx->cb = cb;
  1826. authctx->aes_dev = aes_dev;
  1827. authctx->assoc = assoc;
  1828. authctx->assoclen = assoclen;
  1829. authctx->textlen = textlen;
  1830. ctx->flags = hmac->base.flags;
  1831. return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
  1832. }
  1833. EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
  1834. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
  1835. {
  1836. struct ahash_request *req = dd->req;
  1837. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1838. struct atmel_sha_reqctx *ctx = &authctx->base;
  1839. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1840. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1841. size_t hs = ctx->hash_size;
  1842. size_t i, num_words = hs / sizeof(u32);
  1843. u32 mr, msg_size;
  1844. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1845. for (i = 0; i < num_words; ++i)
  1846. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  1847. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  1848. for (i = 0; i < num_words; ++i)
  1849. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1850. mr = (SHA_MR_MODE_IDATAR0 |
  1851. SHA_MR_HMAC |
  1852. SHA_MR_DUALBUFF);
  1853. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  1854. atmel_sha_write(dd, SHA_MR, mr);
  1855. msg_size = authctx->assoclen + authctx->textlen;
  1856. atmel_sha_write(dd, SHA_MSR, msg_size);
  1857. atmel_sha_write(dd, SHA_BCR, msg_size);
  1858. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1859. /* Process assoc data. */
  1860. return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
  1861. true, false,
  1862. atmel_sha_authenc_init_done);
  1863. }
  1864. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
  1865. {
  1866. struct ahash_request *req = dd->req;
  1867. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1868. return authctx->cb(authctx->aes_dev, 0, dd->is_async);
  1869. }
  1870. int atmel_sha_authenc_final(struct ahash_request *req,
  1871. u32 *digest, unsigned int digestlen,
  1872. atmel_aes_authenc_fn_t cb,
  1873. struct atmel_aes_dev *aes_dev)
  1874. {
  1875. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1876. struct atmel_sha_reqctx *ctx = &authctx->base;
  1877. struct atmel_sha_dev *dd = ctx->dd;
  1878. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  1879. case SHA_FLAGS_SHA1:
  1880. authctx->digestlen = SHA1_DIGEST_SIZE;
  1881. break;
  1882. case SHA_FLAGS_SHA224:
  1883. authctx->digestlen = SHA224_DIGEST_SIZE;
  1884. break;
  1885. case SHA_FLAGS_SHA256:
  1886. authctx->digestlen = SHA256_DIGEST_SIZE;
  1887. break;
  1888. case SHA_FLAGS_SHA384:
  1889. authctx->digestlen = SHA384_DIGEST_SIZE;
  1890. break;
  1891. case SHA_FLAGS_SHA512:
  1892. authctx->digestlen = SHA512_DIGEST_SIZE;
  1893. break;
  1894. default:
  1895. return atmel_sha_complete(dd, -EINVAL);
  1896. }
  1897. if (authctx->digestlen > digestlen)
  1898. authctx->digestlen = digestlen;
  1899. authctx->cb = cb;
  1900. authctx->aes_dev = aes_dev;
  1901. authctx->digest = digest;
  1902. return atmel_sha_wait_for_data_ready(dd,
  1903. atmel_sha_authenc_final_done);
  1904. }
  1905. EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
  1906. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
  1907. {
  1908. struct ahash_request *req = dd->req;
  1909. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1910. size_t i, num_words = authctx->digestlen / sizeof(u32);
  1911. for (i = 0; i < num_words; ++i)
  1912. authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1913. return atmel_sha_complete(dd, 0);
  1914. }
  1915. void atmel_sha_authenc_abort(struct ahash_request *req)
  1916. {
  1917. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1918. struct atmel_sha_reqctx *ctx = &authctx->base;
  1919. struct atmel_sha_dev *dd = ctx->dd;
  1920. /* Prevent atmel_sha_complete() from calling req->base.complete(). */
  1921. dd->is_async = false;
  1922. dd->force_complete = false;
  1923. (void)atmel_sha_complete(dd, 0);
  1924. }
  1925. EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
  1926. #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
  1927. static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  1928. {
  1929. int i;
  1930. if (dd->caps.has_hmac)
  1931. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
  1932. crypto_unregister_ahash(&sha_hmac_algs[i]);
  1933. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  1934. crypto_unregister_ahash(&sha_1_256_algs[i]);
  1935. if (dd->caps.has_sha224)
  1936. crypto_unregister_ahash(&sha_224_alg);
  1937. if (dd->caps.has_sha_384_512) {
  1938. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  1939. crypto_unregister_ahash(&sha_384_512_algs[i]);
  1940. }
  1941. }
  1942. static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  1943. {
  1944. int err, i, j;
  1945. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  1946. atmel_sha_alg_init(&sha_1_256_algs[i]);
  1947. err = crypto_register_ahash(&sha_1_256_algs[i]);
  1948. if (err)
  1949. goto err_sha_1_256_algs;
  1950. }
  1951. if (dd->caps.has_sha224) {
  1952. atmel_sha_alg_init(&sha_224_alg);
  1953. err = crypto_register_ahash(&sha_224_alg);
  1954. if (err)
  1955. goto err_sha_224_algs;
  1956. }
  1957. if (dd->caps.has_sha_384_512) {
  1958. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  1959. atmel_sha_alg_init(&sha_384_512_algs[i]);
  1960. err = crypto_register_ahash(&sha_384_512_algs[i]);
  1961. if (err)
  1962. goto err_sha_384_512_algs;
  1963. }
  1964. }
  1965. if (dd->caps.has_hmac) {
  1966. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
  1967. atmel_sha_hmac_alg_init(&sha_hmac_algs[i]);
  1968. err = crypto_register_ahash(&sha_hmac_algs[i]);
  1969. if (err)
  1970. goto err_sha_hmac_algs;
  1971. }
  1972. }
  1973. return 0;
  1974. /*i = ARRAY_SIZE(sha_hmac_algs);*/
  1975. err_sha_hmac_algs:
  1976. for (j = 0; j < i; j++)
  1977. crypto_unregister_ahash(&sha_hmac_algs[j]);
  1978. i = ARRAY_SIZE(sha_384_512_algs);
  1979. err_sha_384_512_algs:
  1980. for (j = 0; j < i; j++)
  1981. crypto_unregister_ahash(&sha_384_512_algs[j]);
  1982. crypto_unregister_ahash(&sha_224_alg);
  1983. err_sha_224_algs:
  1984. i = ARRAY_SIZE(sha_1_256_algs);
  1985. err_sha_1_256_algs:
  1986. for (j = 0; j < i; j++)
  1987. crypto_unregister_ahash(&sha_1_256_algs[j]);
  1988. return err;
  1989. }
  1990. static int atmel_sha_dma_init(struct atmel_sha_dev *dd)
  1991. {
  1992. dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
  1993. if (IS_ERR(dd->dma_lch_in.chan)) {
  1994. dev_err(dd->dev, "DMA channel is not available\n");
  1995. return PTR_ERR(dd->dma_lch_in.chan);
  1996. }
  1997. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  1998. SHA_REG_DIN(0);
  1999. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  2000. dd->dma_lch_in.dma_conf.src_addr_width =
  2001. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2002. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  2003. dd->dma_lch_in.dma_conf.dst_addr_width =
  2004. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2005. dd->dma_lch_in.dma_conf.device_fc = false;
  2006. return 0;
  2007. }
  2008. static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  2009. {
  2010. dma_release_channel(dd->dma_lch_in.chan);
  2011. }
  2012. static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  2013. {
  2014. dd->caps.has_dma = 0;
  2015. dd->caps.has_dualbuff = 0;
  2016. dd->caps.has_sha224 = 0;
  2017. dd->caps.has_sha_384_512 = 0;
  2018. dd->caps.has_uihv = 0;
  2019. dd->caps.has_hmac = 0;
  2020. /* keep only major version number */
  2021. switch (dd->hw_version & 0xff0) {
  2022. case 0x700:
  2023. case 0x510:
  2024. dd->caps.has_dma = 1;
  2025. dd->caps.has_dualbuff = 1;
  2026. dd->caps.has_sha224 = 1;
  2027. dd->caps.has_sha_384_512 = 1;
  2028. dd->caps.has_uihv = 1;
  2029. dd->caps.has_hmac = 1;
  2030. break;
  2031. case 0x420:
  2032. dd->caps.has_dma = 1;
  2033. dd->caps.has_dualbuff = 1;
  2034. dd->caps.has_sha224 = 1;
  2035. dd->caps.has_sha_384_512 = 1;
  2036. dd->caps.has_uihv = 1;
  2037. break;
  2038. case 0x410:
  2039. dd->caps.has_dma = 1;
  2040. dd->caps.has_dualbuff = 1;
  2041. dd->caps.has_sha224 = 1;
  2042. dd->caps.has_sha_384_512 = 1;
  2043. break;
  2044. case 0x400:
  2045. dd->caps.has_dma = 1;
  2046. dd->caps.has_dualbuff = 1;
  2047. dd->caps.has_sha224 = 1;
  2048. break;
  2049. case 0x320:
  2050. break;
  2051. default:
  2052. dev_warn(dd->dev,
  2053. "Unmanaged sha version, set minimum capabilities\n");
  2054. break;
  2055. }
  2056. }
  2057. #if defined(CONFIG_OF)
  2058. static const struct of_device_id atmel_sha_dt_ids[] = {
  2059. { .compatible = "atmel,at91sam9g46-sha" },
  2060. { /* sentinel */ }
  2061. };
  2062. MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  2063. #endif
  2064. static int atmel_sha_probe(struct platform_device *pdev)
  2065. {
  2066. struct atmel_sha_dev *sha_dd;
  2067. struct device *dev = &pdev->dev;
  2068. struct resource *sha_res;
  2069. int err;
  2070. sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
  2071. if (!sha_dd)
  2072. return -ENOMEM;
  2073. sha_dd->dev = dev;
  2074. platform_set_drvdata(pdev, sha_dd);
  2075. INIT_LIST_HEAD(&sha_dd->list);
  2076. spin_lock_init(&sha_dd->lock);
  2077. tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  2078. (unsigned long)sha_dd);
  2079. tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
  2080. (unsigned long)sha_dd);
  2081. crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  2082. /* Get the base address */
  2083. sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2084. if (!sha_res) {
  2085. dev_err(dev, "no MEM resource info\n");
  2086. err = -ENODEV;
  2087. goto err_tasklet_kill;
  2088. }
  2089. sha_dd->phys_base = sha_res->start;
  2090. /* Get the IRQ */
  2091. sha_dd->irq = platform_get_irq(pdev, 0);
  2092. if (sha_dd->irq < 0) {
  2093. err = sha_dd->irq;
  2094. goto err_tasklet_kill;
  2095. }
  2096. err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  2097. IRQF_SHARED, "atmel-sha", sha_dd);
  2098. if (err) {
  2099. dev_err(dev, "unable to request sha irq.\n");
  2100. goto err_tasklet_kill;
  2101. }
  2102. /* Initializing the clock */
  2103. sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk");
  2104. if (IS_ERR(sha_dd->iclk)) {
  2105. dev_err(dev, "clock initialization failed.\n");
  2106. err = PTR_ERR(sha_dd->iclk);
  2107. goto err_tasklet_kill;
  2108. }
  2109. sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res);
  2110. if (IS_ERR(sha_dd->io_base)) {
  2111. dev_err(dev, "can't ioremap\n");
  2112. err = PTR_ERR(sha_dd->io_base);
  2113. goto err_tasklet_kill;
  2114. }
  2115. err = clk_prepare(sha_dd->iclk);
  2116. if (err)
  2117. goto err_tasklet_kill;
  2118. err = atmel_sha_hw_version_init(sha_dd);
  2119. if (err)
  2120. goto err_iclk_unprepare;
  2121. atmel_sha_get_cap(sha_dd);
  2122. if (sha_dd->caps.has_dma) {
  2123. err = atmel_sha_dma_init(sha_dd);
  2124. if (err)
  2125. goto err_iclk_unprepare;
  2126. dev_info(dev, "using %s for DMA transfers\n",
  2127. dma_chan_name(sha_dd->dma_lch_in.chan));
  2128. }
  2129. spin_lock(&atmel_sha.lock);
  2130. list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  2131. spin_unlock(&atmel_sha.lock);
  2132. err = atmel_sha_register_algs(sha_dd);
  2133. if (err)
  2134. goto err_algs;
  2135. dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
  2136. sha_dd->caps.has_sha224 ? "/SHA224" : "",
  2137. sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
  2138. return 0;
  2139. err_algs:
  2140. spin_lock(&atmel_sha.lock);
  2141. list_del(&sha_dd->list);
  2142. spin_unlock(&atmel_sha.lock);
  2143. if (sha_dd->caps.has_dma)
  2144. atmel_sha_dma_cleanup(sha_dd);
  2145. err_iclk_unprepare:
  2146. clk_unprepare(sha_dd->iclk);
  2147. err_tasklet_kill:
  2148. tasklet_kill(&sha_dd->queue_task);
  2149. tasklet_kill(&sha_dd->done_task);
  2150. return err;
  2151. }
  2152. static int atmel_sha_remove(struct platform_device *pdev)
  2153. {
  2154. struct atmel_sha_dev *sha_dd = platform_get_drvdata(pdev);
  2155. spin_lock(&atmel_sha.lock);
  2156. list_del(&sha_dd->list);
  2157. spin_unlock(&atmel_sha.lock);
  2158. atmel_sha_unregister_algs(sha_dd);
  2159. tasklet_kill(&sha_dd->queue_task);
  2160. tasklet_kill(&sha_dd->done_task);
  2161. if (sha_dd->caps.has_dma)
  2162. atmel_sha_dma_cleanup(sha_dd);
  2163. clk_unprepare(sha_dd->iclk);
  2164. return 0;
  2165. }
  2166. static struct platform_driver atmel_sha_driver = {
  2167. .probe = atmel_sha_probe,
  2168. .remove = atmel_sha_remove,
  2169. .driver = {
  2170. .name = "atmel_sha",
  2171. .of_match_table = of_match_ptr(atmel_sha_dt_ids),
  2172. },
  2173. };
  2174. module_platform_driver(atmel_sha_driver);
  2175. MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
  2176. MODULE_LICENSE("GPL v2");
  2177. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");