atmel-sha.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  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/mod_devicetable.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. ahash_request_complete(req, 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. crypto_request_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.digestsize = SHA384_DIGEST_SIZE,
  1042. },
  1043. {
  1044. .halg.base.cra_name = "sha512",
  1045. .halg.base.cra_driver_name = "atmel-sha512",
  1046. .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
  1047. .halg.digestsize = SHA512_DIGEST_SIZE,
  1048. },
  1049. };
  1050. static void atmel_sha_queue_task(unsigned long data)
  1051. {
  1052. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  1053. atmel_sha_handle_queue(dd, NULL);
  1054. }
  1055. static int atmel_sha_done(struct atmel_sha_dev *dd)
  1056. {
  1057. int err = 0;
  1058. if (SHA_FLAGS_CPU & dd->flags) {
  1059. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  1060. dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
  1061. goto finish;
  1062. }
  1063. } else if (SHA_FLAGS_DMA_READY & dd->flags) {
  1064. if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
  1065. dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
  1066. atmel_sha_update_dma_stop(dd);
  1067. }
  1068. if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
  1069. /* hash or semi-hash ready */
  1070. dd->flags &= ~(SHA_FLAGS_DMA_READY |
  1071. SHA_FLAGS_OUTPUT_READY);
  1072. err = atmel_sha_update_dma_start(dd);
  1073. if (err != -EINPROGRESS)
  1074. goto finish;
  1075. }
  1076. }
  1077. return err;
  1078. finish:
  1079. /* finish curent request */
  1080. atmel_sha_finish_req(dd->req, err);
  1081. return err;
  1082. }
  1083. static void atmel_sha_done_task(unsigned long data)
  1084. {
  1085. struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
  1086. dd->is_async = true;
  1087. (void)dd->resume(dd);
  1088. }
  1089. static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
  1090. {
  1091. struct atmel_sha_dev *sha_dd = dev_id;
  1092. u32 reg;
  1093. reg = atmel_sha_read(sha_dd, SHA_ISR);
  1094. if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
  1095. atmel_sha_write(sha_dd, SHA_IDR, reg);
  1096. if (SHA_FLAGS_BUSY & sha_dd->flags) {
  1097. sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
  1098. if (!(SHA_FLAGS_CPU & sha_dd->flags))
  1099. sha_dd->flags |= SHA_FLAGS_DMA_READY;
  1100. tasklet_schedule(&sha_dd->done_task);
  1101. } else {
  1102. dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
  1103. }
  1104. return IRQ_HANDLED;
  1105. }
  1106. return IRQ_NONE;
  1107. }
  1108. /* DMA transfer functions */
  1109. static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
  1110. struct scatterlist *sg,
  1111. size_t len)
  1112. {
  1113. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1114. struct ahash_request *req = dd->req;
  1115. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1116. size_t bs = ctx->block_size;
  1117. int nents;
  1118. for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  1119. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  1120. return false;
  1121. /*
  1122. * This is the last sg, the only one that is allowed to
  1123. * have an unaligned length.
  1124. */
  1125. if (len <= sg->length) {
  1126. dma->nents = nents + 1;
  1127. dma->last_sg_length = sg->length;
  1128. sg->length = ALIGN(len, sizeof(u32));
  1129. return true;
  1130. }
  1131. /* All other sg lengths MUST be aligned to the block size. */
  1132. if (!IS_ALIGNED(sg->length, bs))
  1133. return false;
  1134. len -= sg->length;
  1135. }
  1136. return false;
  1137. }
  1138. static void atmel_sha_dma_callback2(void *data)
  1139. {
  1140. struct atmel_sha_dev *dd = data;
  1141. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1142. struct scatterlist *sg;
  1143. int nents;
  1144. dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1145. sg = dma->sg;
  1146. for (nents = 0; nents < dma->nents - 1; ++nents)
  1147. sg = sg_next(sg);
  1148. sg->length = dma->last_sg_length;
  1149. dd->is_async = true;
  1150. (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
  1151. }
  1152. static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
  1153. struct scatterlist *src,
  1154. size_t len,
  1155. atmel_sha_fn_t resume)
  1156. {
  1157. struct atmel_sha_dma *dma = &dd->dma_lch_in;
  1158. struct dma_slave_config *config = &dma->dma_conf;
  1159. struct dma_chan *chan = dma->chan;
  1160. struct dma_async_tx_descriptor *desc;
  1161. dma_cookie_t cookie;
  1162. unsigned int sg_len;
  1163. int err;
  1164. dd->resume = resume;
  1165. /*
  1166. * dma->nents has already been initialized by
  1167. * atmel_sha_dma_check_aligned().
  1168. */
  1169. dma->sg = src;
  1170. sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1171. if (!sg_len) {
  1172. err = -ENOMEM;
  1173. goto exit;
  1174. }
  1175. config->src_maxburst = 16;
  1176. config->dst_maxburst = 16;
  1177. err = dmaengine_slave_config(chan, config);
  1178. if (err)
  1179. goto unmap_sg;
  1180. desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
  1181. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1182. if (!desc) {
  1183. err = -ENOMEM;
  1184. goto unmap_sg;
  1185. }
  1186. desc->callback = atmel_sha_dma_callback2;
  1187. desc->callback_param = dd;
  1188. cookie = dmaengine_submit(desc);
  1189. err = dma_submit_error(cookie);
  1190. if (err)
  1191. goto unmap_sg;
  1192. dma_async_issue_pending(chan);
  1193. return -EINPROGRESS;
  1194. unmap_sg:
  1195. dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
  1196. exit:
  1197. return atmel_sha_complete(dd, err);
  1198. }
  1199. /* CPU transfer functions */
  1200. static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
  1201. {
  1202. struct ahash_request *req = dd->req;
  1203. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1204. const u32 *words = (const u32 *)ctx->buffer;
  1205. size_t i, num_words;
  1206. u32 isr, din, din_inc;
  1207. din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
  1208. for (;;) {
  1209. /* Write data into the Input Data Registers. */
  1210. num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
  1211. for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
  1212. atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
  1213. ctx->offset += ctx->bufcnt;
  1214. ctx->total -= ctx->bufcnt;
  1215. if (!ctx->total)
  1216. break;
  1217. /*
  1218. * Prepare next block:
  1219. * Fill ctx->buffer now with the next data to be written into
  1220. * IDATARx: it gives time for the SHA hardware to process
  1221. * the current data so the SHA_INT_DATARDY flag might be set
  1222. * in SHA_ISR when polling this register at the beginning of
  1223. * the next loop.
  1224. */
  1225. ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  1226. scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  1227. ctx->offset, ctx->bufcnt, 0);
  1228. /* Wait for hardware to be ready again. */
  1229. isr = atmel_sha_read(dd, SHA_ISR);
  1230. if (!(isr & SHA_INT_DATARDY)) {
  1231. /* Not ready yet. */
  1232. dd->resume = atmel_sha_cpu_transfer;
  1233. atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
  1234. return -EINPROGRESS;
  1235. }
  1236. }
  1237. if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
  1238. return dd->cpu_transfer_complete(dd);
  1239. return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
  1240. }
  1241. static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
  1242. struct scatterlist *sg,
  1243. unsigned int len,
  1244. bool idatar0_only,
  1245. bool wait_data_ready,
  1246. atmel_sha_fn_t resume)
  1247. {
  1248. struct ahash_request *req = dd->req;
  1249. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1250. if (!len)
  1251. return resume(dd);
  1252. ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
  1253. if (idatar0_only)
  1254. ctx->flags |= SHA_FLAGS_IDATAR0;
  1255. if (wait_data_ready)
  1256. ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
  1257. ctx->sg = sg;
  1258. ctx->total = len;
  1259. ctx->offset = 0;
  1260. /* Prepare the first block to be written. */
  1261. ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
  1262. scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
  1263. ctx->offset, ctx->bufcnt, 0);
  1264. dd->cpu_transfer_complete = resume;
  1265. return atmel_sha_cpu_transfer(dd);
  1266. }
  1267. static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
  1268. const void *data, unsigned int datalen,
  1269. bool auto_padding,
  1270. atmel_sha_fn_t resume)
  1271. {
  1272. struct ahash_request *req = dd->req;
  1273. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1274. u32 msglen = (auto_padding) ? datalen : 0;
  1275. u32 mr = SHA_MR_MODE_AUTO;
  1276. if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
  1277. return atmel_sha_complete(dd, -EINVAL);
  1278. mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  1279. atmel_sha_write(dd, SHA_MR, mr);
  1280. atmel_sha_write(dd, SHA_MSR, msglen);
  1281. atmel_sha_write(dd, SHA_BCR, msglen);
  1282. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1283. sg_init_one(&dd->tmp, data, datalen);
  1284. return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
  1285. }
  1286. /* hmac functions */
  1287. struct atmel_sha_hmac_key {
  1288. bool valid;
  1289. unsigned int keylen;
  1290. u8 buffer[SHA512_BLOCK_SIZE];
  1291. u8 *keydup;
  1292. };
  1293. static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
  1294. {
  1295. memset(hkey, 0, sizeof(*hkey));
  1296. }
  1297. static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
  1298. {
  1299. kfree(hkey->keydup);
  1300. memset(hkey, 0, sizeof(*hkey));
  1301. }
  1302. static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
  1303. const u8 *key,
  1304. unsigned int keylen)
  1305. {
  1306. atmel_sha_hmac_key_release(hkey);
  1307. if (keylen > sizeof(hkey->buffer)) {
  1308. hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
  1309. if (!hkey->keydup)
  1310. return -ENOMEM;
  1311. } else {
  1312. memcpy(hkey->buffer, key, keylen);
  1313. }
  1314. hkey->valid = true;
  1315. hkey->keylen = keylen;
  1316. return 0;
  1317. }
  1318. static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
  1319. const u8 **key,
  1320. unsigned int *keylen)
  1321. {
  1322. if (!hkey->valid)
  1323. return false;
  1324. *keylen = hkey->keylen;
  1325. *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
  1326. return true;
  1327. }
  1328. struct atmel_sha_hmac_ctx {
  1329. struct atmel_sha_ctx base;
  1330. struct atmel_sha_hmac_key hkey;
  1331. u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
  1332. u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
  1333. atmel_sha_fn_t resume;
  1334. };
  1335. static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  1336. atmel_sha_fn_t resume);
  1337. static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  1338. const u8 *key, unsigned int keylen);
  1339. static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
  1340. static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
  1341. static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
  1342. static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
  1343. static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
  1344. static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
  1345. static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
  1346. static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
  1347. static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
  1348. atmel_sha_fn_t resume)
  1349. {
  1350. struct ahash_request *req = dd->req;
  1351. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1352. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1353. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1354. unsigned int keylen;
  1355. const u8 *key;
  1356. size_t bs;
  1357. hmac->resume = resume;
  1358. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  1359. case SHA_FLAGS_SHA1:
  1360. ctx->block_size = SHA1_BLOCK_SIZE;
  1361. ctx->hash_size = SHA1_DIGEST_SIZE;
  1362. break;
  1363. case SHA_FLAGS_SHA224:
  1364. ctx->block_size = SHA224_BLOCK_SIZE;
  1365. ctx->hash_size = SHA256_DIGEST_SIZE;
  1366. break;
  1367. case SHA_FLAGS_SHA256:
  1368. ctx->block_size = SHA256_BLOCK_SIZE;
  1369. ctx->hash_size = SHA256_DIGEST_SIZE;
  1370. break;
  1371. case SHA_FLAGS_SHA384:
  1372. ctx->block_size = SHA384_BLOCK_SIZE;
  1373. ctx->hash_size = SHA512_DIGEST_SIZE;
  1374. break;
  1375. case SHA_FLAGS_SHA512:
  1376. ctx->block_size = SHA512_BLOCK_SIZE;
  1377. ctx->hash_size = SHA512_DIGEST_SIZE;
  1378. break;
  1379. default:
  1380. return atmel_sha_complete(dd, -EINVAL);
  1381. }
  1382. bs = ctx->block_size;
  1383. if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
  1384. return resume(dd);
  1385. /* Compute K' from K. */
  1386. if (unlikely(keylen > bs))
  1387. return atmel_sha_hmac_prehash_key(dd, key, keylen);
  1388. /* Prepare ipad. */
  1389. memcpy((u8 *)hmac->ipad, key, keylen);
  1390. memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
  1391. return atmel_sha_hmac_compute_ipad_hash(dd);
  1392. }
  1393. static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
  1394. const u8 *key, unsigned int keylen)
  1395. {
  1396. return atmel_sha_cpu_hash(dd, key, keylen, true,
  1397. atmel_sha_hmac_prehash_key_done);
  1398. }
  1399. static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
  1400. {
  1401. struct ahash_request *req = dd->req;
  1402. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1403. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1404. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1405. size_t ds = crypto_ahash_digestsize(tfm);
  1406. size_t bs = ctx->block_size;
  1407. size_t i, num_words = ds / sizeof(u32);
  1408. /* Prepare ipad. */
  1409. for (i = 0; i < num_words; ++i)
  1410. hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1411. memset((u8 *)hmac->ipad + ds, 0, bs - ds);
  1412. return atmel_sha_hmac_compute_ipad_hash(dd);
  1413. }
  1414. static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
  1415. {
  1416. struct ahash_request *req = dd->req;
  1417. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1418. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1419. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1420. size_t bs = ctx->block_size;
  1421. size_t i, num_words = bs / sizeof(u32);
  1422. unsafe_memcpy(hmac->opad, hmac->ipad, bs,
  1423. "fortified memcpy causes -Wrestrict warning");
  1424. for (i = 0; i < num_words; ++i) {
  1425. hmac->ipad[i] ^= 0x36363636;
  1426. hmac->opad[i] ^= 0x5c5c5c5c;
  1427. }
  1428. return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
  1429. atmel_sha_hmac_compute_opad_hash);
  1430. }
  1431. static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
  1432. {
  1433. struct ahash_request *req = dd->req;
  1434. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1435. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1436. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1437. size_t bs = ctx->block_size;
  1438. size_t hs = ctx->hash_size;
  1439. size_t i, num_words = hs / sizeof(u32);
  1440. for (i = 0; i < num_words; ++i)
  1441. hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1442. return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
  1443. atmel_sha_hmac_setup_done);
  1444. }
  1445. static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
  1446. {
  1447. struct ahash_request *req = dd->req;
  1448. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1449. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1450. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1451. size_t hs = ctx->hash_size;
  1452. size_t i, num_words = hs / sizeof(u32);
  1453. for (i = 0; i < num_words; ++i)
  1454. hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1455. atmel_sha_hmac_key_release(&hmac->hkey);
  1456. return hmac->resume(dd);
  1457. }
  1458. static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
  1459. {
  1460. struct ahash_request *req = dd->req;
  1461. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1462. int err;
  1463. err = atmel_sha_hw_init(dd);
  1464. if (err)
  1465. return atmel_sha_complete(dd, err);
  1466. switch (ctx->op) {
  1467. case SHA_OP_INIT:
  1468. err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
  1469. break;
  1470. case SHA_OP_UPDATE:
  1471. dd->resume = atmel_sha_done;
  1472. err = atmel_sha_update_req(dd);
  1473. break;
  1474. case SHA_OP_FINAL:
  1475. dd->resume = atmel_sha_hmac_final;
  1476. err = atmel_sha_final_req(dd);
  1477. break;
  1478. case SHA_OP_DIGEST:
  1479. err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
  1480. break;
  1481. default:
  1482. return atmel_sha_complete(dd, -EINVAL);
  1483. }
  1484. return err;
  1485. }
  1486. static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  1487. unsigned int keylen)
  1488. {
  1489. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1490. return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
  1491. }
  1492. static int atmel_sha_hmac_init(struct ahash_request *req)
  1493. {
  1494. int err;
  1495. err = atmel_sha_init(req);
  1496. if (err)
  1497. return err;
  1498. return atmel_sha_enqueue(req, SHA_OP_INIT);
  1499. }
  1500. static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
  1501. {
  1502. struct ahash_request *req = dd->req;
  1503. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1504. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1505. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1506. size_t bs = ctx->block_size;
  1507. size_t hs = ctx->hash_size;
  1508. ctx->bufcnt = 0;
  1509. ctx->digcnt[0] = bs;
  1510. ctx->digcnt[1] = 0;
  1511. ctx->flags |= SHA_FLAGS_RESTORE;
  1512. memcpy(ctx->digest, hmac->ipad, hs);
  1513. return atmel_sha_complete(dd, 0);
  1514. }
  1515. static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
  1516. {
  1517. struct ahash_request *req = dd->req;
  1518. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1519. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1520. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1521. u32 *digest = (u32 *)ctx->digest;
  1522. size_t ds = crypto_ahash_digestsize(tfm);
  1523. size_t bs = ctx->block_size;
  1524. size_t hs = ctx->hash_size;
  1525. size_t i, num_words;
  1526. u32 mr;
  1527. /* Save d = SHA((K' + ipad) | msg). */
  1528. num_words = ds / sizeof(u32);
  1529. for (i = 0; i < num_words; ++i)
  1530. digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1531. /* Restore context to finish computing SHA((K' + opad) | d). */
  1532. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1533. num_words = hs / sizeof(u32);
  1534. for (i = 0; i < num_words; ++i)
  1535. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1536. mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
  1537. mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
  1538. atmel_sha_write(dd, SHA_MR, mr);
  1539. atmel_sha_write(dd, SHA_MSR, bs + ds);
  1540. atmel_sha_write(dd, SHA_BCR, ds);
  1541. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1542. sg_init_one(&dd->tmp, digest, ds);
  1543. return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
  1544. atmel_sha_hmac_final_done);
  1545. }
  1546. static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
  1547. {
  1548. /*
  1549. * req->result might not be sizeof(u32) aligned, so copy the
  1550. * digest into ctx->digest[] before memcpy() the data into
  1551. * req->result.
  1552. */
  1553. atmel_sha_copy_hash(dd->req);
  1554. atmel_sha_copy_ready_hash(dd->req);
  1555. return atmel_sha_complete(dd, 0);
  1556. }
  1557. static int atmel_sha_hmac_digest(struct ahash_request *req)
  1558. {
  1559. int err;
  1560. err = atmel_sha_init(req);
  1561. if (err)
  1562. return err;
  1563. return atmel_sha_enqueue(req, SHA_OP_DIGEST);
  1564. }
  1565. static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
  1566. {
  1567. struct ahash_request *req = dd->req;
  1568. struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
  1569. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1570. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1571. struct scatterlist *sgbuf;
  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. req->nbytes = 0;
  1579. ctx->bufcnt = 0;
  1580. ctx->digcnt[0] = 0;
  1581. ctx->digcnt[1] = 0;
  1582. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  1583. case SHA_FLAGS_SHA1:
  1584. case SHA_FLAGS_SHA224:
  1585. case SHA_FLAGS_SHA256:
  1586. atmel_sha_fill_padding(ctx, 64);
  1587. break;
  1588. case SHA_FLAGS_SHA384:
  1589. case SHA_FLAGS_SHA512:
  1590. atmel_sha_fill_padding(ctx, 128);
  1591. break;
  1592. }
  1593. sg_init_one(&dd->tmp, ctx->buffer, ctx->bufcnt);
  1594. }
  1595. /* Check DMA threshold and alignment. */
  1596. if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
  1597. atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
  1598. use_dma = true;
  1599. /* Write both initial hash values to compute a HMAC. */
  1600. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1601. for (i = 0; i < num_words; ++i)
  1602. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  1603. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  1604. for (i = 0; i < num_words; ++i)
  1605. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1606. /* Write the Mode, Message Size, Bytes Count then Control Registers. */
  1607. mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
  1608. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  1609. if (use_dma)
  1610. mr |= SHA_MR_MODE_IDATAR0;
  1611. else
  1612. mr |= SHA_MR_MODE_AUTO;
  1613. atmel_sha_write(dd, SHA_MR, mr);
  1614. atmel_sha_write(dd, SHA_MSR, req->nbytes);
  1615. atmel_sha_write(dd, SHA_BCR, req->nbytes);
  1616. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1617. /* Special case for empty message. */
  1618. if (!req->nbytes) {
  1619. sgbuf = &dd->tmp;
  1620. req->nbytes = ctx->bufcnt;
  1621. } else {
  1622. sgbuf = req->src;
  1623. }
  1624. /* Process data. */
  1625. if (use_dma)
  1626. return atmel_sha_dma_start(dd, sgbuf, req->nbytes,
  1627. atmel_sha_hmac_final_done);
  1628. return atmel_sha_cpu_start(dd, sgbuf, req->nbytes, false, true,
  1629. atmel_sha_hmac_final_done);
  1630. }
  1631. static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
  1632. {
  1633. struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  1634. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  1635. sizeof(struct atmel_sha_reqctx));
  1636. hmac->base.start = atmel_sha_hmac_start;
  1637. atmel_sha_hmac_key_init(&hmac->hkey);
  1638. return 0;
  1639. }
  1640. static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
  1641. {
  1642. struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
  1643. atmel_sha_hmac_key_release(&hmac->hkey);
  1644. }
  1645. static void atmel_sha_hmac_alg_init(struct ahash_alg *alg)
  1646. {
  1647. alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
  1648. alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
  1649. alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx);
  1650. alg->halg.base.cra_module = THIS_MODULE;
  1651. alg->halg.base.cra_init = atmel_sha_hmac_cra_init;
  1652. alg->halg.base.cra_exit = atmel_sha_hmac_cra_exit;
  1653. alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
  1654. alg->init = atmel_sha_hmac_init;
  1655. alg->update = atmel_sha_update;
  1656. alg->final = atmel_sha_final;
  1657. alg->digest = atmel_sha_hmac_digest;
  1658. alg->setkey = atmel_sha_hmac_setkey;
  1659. alg->export = atmel_sha_export;
  1660. alg->import = atmel_sha_import;
  1661. }
  1662. static struct ahash_alg sha_hmac_algs[] = {
  1663. {
  1664. .halg.base.cra_name = "hmac(sha1)",
  1665. .halg.base.cra_driver_name = "atmel-hmac-sha1",
  1666. .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
  1667. .halg.digestsize = SHA1_DIGEST_SIZE,
  1668. },
  1669. {
  1670. .halg.base.cra_name = "hmac(sha224)",
  1671. .halg.base.cra_driver_name = "atmel-hmac-sha224",
  1672. .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
  1673. .halg.digestsize = SHA224_DIGEST_SIZE,
  1674. },
  1675. {
  1676. .halg.base.cra_name = "hmac(sha256)",
  1677. .halg.base.cra_driver_name = "atmel-hmac-sha256",
  1678. .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
  1679. .halg.digestsize = SHA256_DIGEST_SIZE,
  1680. },
  1681. {
  1682. .halg.base.cra_name = "hmac(sha384)",
  1683. .halg.base.cra_driver_name = "atmel-hmac-sha384",
  1684. .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
  1685. .halg.digestsize = SHA384_DIGEST_SIZE,
  1686. },
  1687. {
  1688. .halg.base.cra_name = "hmac(sha512)",
  1689. .halg.base.cra_driver_name = "atmel-hmac-sha512",
  1690. .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
  1691. .halg.digestsize = SHA512_DIGEST_SIZE,
  1692. },
  1693. };
  1694. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  1695. /* authenc functions */
  1696. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
  1697. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
  1698. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
  1699. struct atmel_sha_authenc_ctx {
  1700. struct crypto_ahash *tfm;
  1701. };
  1702. struct atmel_sha_authenc_reqctx {
  1703. struct atmel_sha_reqctx base;
  1704. atmel_aes_authenc_fn_t cb;
  1705. struct atmel_aes_dev *aes_dev;
  1706. /* _init() parameters. */
  1707. struct scatterlist *assoc;
  1708. u32 assoclen;
  1709. u32 textlen;
  1710. /* _final() parameters. */
  1711. u32 *digest;
  1712. unsigned int digestlen;
  1713. };
  1714. static void atmel_sha_authenc_complete(void *data, int err)
  1715. {
  1716. struct ahash_request *req = data;
  1717. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1718. authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
  1719. }
  1720. static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
  1721. {
  1722. struct ahash_request *req = dd->req;
  1723. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1724. int err;
  1725. /*
  1726. * Force atmel_sha_complete() to call req->base.complete(), ie
  1727. * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
  1728. */
  1729. dd->force_complete = true;
  1730. err = atmel_sha_hw_init(dd);
  1731. return authctx->cb(authctx->aes_dev, err, dd->is_async);
  1732. }
  1733. bool atmel_sha_authenc_is_ready(void)
  1734. {
  1735. struct atmel_sha_ctx dummy;
  1736. dummy.dd = NULL;
  1737. return (atmel_sha_find_dev(&dummy) != NULL);
  1738. }
  1739. EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
  1740. unsigned int atmel_sha_authenc_get_reqsize(void)
  1741. {
  1742. return sizeof(struct atmel_sha_authenc_reqctx);
  1743. }
  1744. EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
  1745. struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
  1746. {
  1747. struct atmel_sha_authenc_ctx *auth;
  1748. struct crypto_ahash *tfm;
  1749. struct atmel_sha_ctx *tctx;
  1750. const char *name;
  1751. int err = -EINVAL;
  1752. switch (mode & SHA_FLAGS_MODE_MASK) {
  1753. case SHA_FLAGS_HMAC_SHA1:
  1754. name = "atmel-hmac-sha1";
  1755. break;
  1756. case SHA_FLAGS_HMAC_SHA224:
  1757. name = "atmel-hmac-sha224";
  1758. break;
  1759. case SHA_FLAGS_HMAC_SHA256:
  1760. name = "atmel-hmac-sha256";
  1761. break;
  1762. case SHA_FLAGS_HMAC_SHA384:
  1763. name = "atmel-hmac-sha384";
  1764. break;
  1765. case SHA_FLAGS_HMAC_SHA512:
  1766. name = "atmel-hmac-sha512";
  1767. break;
  1768. default:
  1769. goto error;
  1770. }
  1771. tfm = crypto_alloc_ahash(name, 0, 0);
  1772. if (IS_ERR(tfm)) {
  1773. err = PTR_ERR(tfm);
  1774. goto error;
  1775. }
  1776. tctx = crypto_ahash_ctx(tfm);
  1777. tctx->start = atmel_sha_authenc_start;
  1778. tctx->flags = mode;
  1779. auth = kzalloc(sizeof(*auth), GFP_KERNEL);
  1780. if (!auth) {
  1781. err = -ENOMEM;
  1782. goto err_free_ahash;
  1783. }
  1784. auth->tfm = tfm;
  1785. return auth;
  1786. err_free_ahash:
  1787. crypto_free_ahash(tfm);
  1788. error:
  1789. return ERR_PTR(err);
  1790. }
  1791. EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
  1792. void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
  1793. {
  1794. if (auth)
  1795. crypto_free_ahash(auth->tfm);
  1796. kfree(auth);
  1797. }
  1798. EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
  1799. int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
  1800. const u8 *key, unsigned int keylen, u32 flags)
  1801. {
  1802. struct crypto_ahash *tfm = auth->tfm;
  1803. crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
  1804. crypto_ahash_set_flags(tfm, flags & CRYPTO_TFM_REQ_MASK);
  1805. return crypto_ahash_setkey(tfm, key, keylen);
  1806. }
  1807. EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
  1808. int atmel_sha_authenc_schedule(struct ahash_request *req,
  1809. struct atmel_sha_authenc_ctx *auth,
  1810. atmel_aes_authenc_fn_t cb,
  1811. struct atmel_aes_dev *aes_dev)
  1812. {
  1813. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1814. struct atmel_sha_reqctx *ctx = &authctx->base;
  1815. struct crypto_ahash *tfm = auth->tfm;
  1816. struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
  1817. struct atmel_sha_dev *dd;
  1818. /* Reset request context (MUST be done first). */
  1819. memset(authctx, 0, sizeof(*authctx));
  1820. /* Get SHA device. */
  1821. dd = atmel_sha_find_dev(tctx);
  1822. if (!dd)
  1823. return cb(aes_dev, -ENODEV, false);
  1824. /* Init request context. */
  1825. ctx->dd = dd;
  1826. ctx->buflen = SHA_BUFFER_LEN;
  1827. authctx->cb = cb;
  1828. authctx->aes_dev = aes_dev;
  1829. ahash_request_set_tfm(req, tfm);
  1830. ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
  1831. return atmel_sha_handle_queue(dd, req);
  1832. }
  1833. EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
  1834. int atmel_sha_authenc_init(struct ahash_request *req,
  1835. struct scatterlist *assoc, unsigned int assoclen,
  1836. unsigned int textlen,
  1837. atmel_aes_authenc_fn_t cb,
  1838. struct atmel_aes_dev *aes_dev)
  1839. {
  1840. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1841. struct atmel_sha_reqctx *ctx = &authctx->base;
  1842. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1843. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1844. struct atmel_sha_dev *dd = ctx->dd;
  1845. if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
  1846. return atmel_sha_complete(dd, -EINVAL);
  1847. authctx->cb = cb;
  1848. authctx->aes_dev = aes_dev;
  1849. authctx->assoc = assoc;
  1850. authctx->assoclen = assoclen;
  1851. authctx->textlen = textlen;
  1852. ctx->flags = hmac->base.flags;
  1853. return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
  1854. }
  1855. EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
  1856. static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
  1857. {
  1858. struct ahash_request *req = dd->req;
  1859. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1860. struct atmel_sha_reqctx *ctx = &authctx->base;
  1861. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1862. struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
  1863. size_t hs = ctx->hash_size;
  1864. size_t i, num_words = hs / sizeof(u32);
  1865. u32 mr, msg_size;
  1866. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
  1867. for (i = 0; i < num_words; ++i)
  1868. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
  1869. atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
  1870. for (i = 0; i < num_words; ++i)
  1871. atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
  1872. mr = (SHA_MR_MODE_IDATAR0 |
  1873. SHA_MR_HMAC |
  1874. SHA_MR_DUALBUFF);
  1875. mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
  1876. atmel_sha_write(dd, SHA_MR, mr);
  1877. msg_size = authctx->assoclen + authctx->textlen;
  1878. atmel_sha_write(dd, SHA_MSR, msg_size);
  1879. atmel_sha_write(dd, SHA_BCR, msg_size);
  1880. atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
  1881. /* Process assoc data. */
  1882. return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
  1883. true, false,
  1884. atmel_sha_authenc_init_done);
  1885. }
  1886. static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
  1887. {
  1888. struct ahash_request *req = dd->req;
  1889. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1890. return authctx->cb(authctx->aes_dev, 0, dd->is_async);
  1891. }
  1892. int atmel_sha_authenc_final(struct ahash_request *req,
  1893. u32 *digest, unsigned int digestlen,
  1894. atmel_aes_authenc_fn_t cb,
  1895. struct atmel_aes_dev *aes_dev)
  1896. {
  1897. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1898. struct atmel_sha_reqctx *ctx = &authctx->base;
  1899. struct atmel_sha_dev *dd = ctx->dd;
  1900. switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
  1901. case SHA_FLAGS_SHA1:
  1902. authctx->digestlen = SHA1_DIGEST_SIZE;
  1903. break;
  1904. case SHA_FLAGS_SHA224:
  1905. authctx->digestlen = SHA224_DIGEST_SIZE;
  1906. break;
  1907. case SHA_FLAGS_SHA256:
  1908. authctx->digestlen = SHA256_DIGEST_SIZE;
  1909. break;
  1910. case SHA_FLAGS_SHA384:
  1911. authctx->digestlen = SHA384_DIGEST_SIZE;
  1912. break;
  1913. case SHA_FLAGS_SHA512:
  1914. authctx->digestlen = SHA512_DIGEST_SIZE;
  1915. break;
  1916. default:
  1917. return atmel_sha_complete(dd, -EINVAL);
  1918. }
  1919. if (authctx->digestlen > digestlen)
  1920. authctx->digestlen = digestlen;
  1921. authctx->cb = cb;
  1922. authctx->aes_dev = aes_dev;
  1923. authctx->digest = digest;
  1924. return atmel_sha_wait_for_data_ready(dd,
  1925. atmel_sha_authenc_final_done);
  1926. }
  1927. EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
  1928. static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
  1929. {
  1930. struct ahash_request *req = dd->req;
  1931. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1932. size_t i, num_words = authctx->digestlen / sizeof(u32);
  1933. for (i = 0; i < num_words; ++i)
  1934. authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
  1935. return atmel_sha_complete(dd, 0);
  1936. }
  1937. void atmel_sha_authenc_abort(struct ahash_request *req)
  1938. {
  1939. struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
  1940. struct atmel_sha_reqctx *ctx = &authctx->base;
  1941. struct atmel_sha_dev *dd = ctx->dd;
  1942. /* Prevent atmel_sha_complete() from calling req->base.complete(). */
  1943. dd->is_async = false;
  1944. dd->force_complete = false;
  1945. (void)atmel_sha_complete(dd, 0);
  1946. }
  1947. EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
  1948. #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
  1949. static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
  1950. {
  1951. int i;
  1952. if (dd->caps.has_hmac)
  1953. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
  1954. crypto_unregister_ahash(&sha_hmac_algs[i]);
  1955. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
  1956. crypto_unregister_ahash(&sha_1_256_algs[i]);
  1957. if (dd->caps.has_sha224)
  1958. crypto_unregister_ahash(&sha_224_alg);
  1959. if (dd->caps.has_sha_384_512) {
  1960. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
  1961. crypto_unregister_ahash(&sha_384_512_algs[i]);
  1962. }
  1963. }
  1964. static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
  1965. {
  1966. int err, i, j;
  1967. for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
  1968. atmel_sha_alg_init(&sha_1_256_algs[i]);
  1969. err = crypto_register_ahash(&sha_1_256_algs[i]);
  1970. if (err)
  1971. goto err_sha_1_256_algs;
  1972. }
  1973. if (dd->caps.has_sha224) {
  1974. atmel_sha_alg_init(&sha_224_alg);
  1975. err = crypto_register_ahash(&sha_224_alg);
  1976. if (err)
  1977. goto err_sha_224_algs;
  1978. }
  1979. if (dd->caps.has_sha_384_512) {
  1980. for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
  1981. atmel_sha_alg_init(&sha_384_512_algs[i]);
  1982. err = crypto_register_ahash(&sha_384_512_algs[i]);
  1983. if (err)
  1984. goto err_sha_384_512_algs;
  1985. }
  1986. }
  1987. if (dd->caps.has_hmac) {
  1988. for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
  1989. atmel_sha_hmac_alg_init(&sha_hmac_algs[i]);
  1990. err = crypto_register_ahash(&sha_hmac_algs[i]);
  1991. if (err)
  1992. goto err_sha_hmac_algs;
  1993. }
  1994. }
  1995. return 0;
  1996. /*i = ARRAY_SIZE(sha_hmac_algs);*/
  1997. err_sha_hmac_algs:
  1998. for (j = 0; j < i; j++)
  1999. crypto_unregister_ahash(&sha_hmac_algs[j]);
  2000. i = ARRAY_SIZE(sha_384_512_algs);
  2001. err_sha_384_512_algs:
  2002. for (j = 0; j < i; j++)
  2003. crypto_unregister_ahash(&sha_384_512_algs[j]);
  2004. crypto_unregister_ahash(&sha_224_alg);
  2005. err_sha_224_algs:
  2006. i = ARRAY_SIZE(sha_1_256_algs);
  2007. err_sha_1_256_algs:
  2008. for (j = 0; j < i; j++)
  2009. crypto_unregister_ahash(&sha_1_256_algs[j]);
  2010. return err;
  2011. }
  2012. static int atmel_sha_dma_init(struct atmel_sha_dev *dd)
  2013. {
  2014. dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
  2015. if (IS_ERR(dd->dma_lch_in.chan)) {
  2016. return dev_err_probe(dd->dev, PTR_ERR(dd->dma_lch_in.chan),
  2017. "DMA channel is not available\n");
  2018. }
  2019. dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
  2020. SHA_REG_DIN(0);
  2021. dd->dma_lch_in.dma_conf.src_maxburst = 1;
  2022. dd->dma_lch_in.dma_conf.src_addr_width =
  2023. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2024. dd->dma_lch_in.dma_conf.dst_maxburst = 1;
  2025. dd->dma_lch_in.dma_conf.dst_addr_width =
  2026. DMA_SLAVE_BUSWIDTH_4_BYTES;
  2027. dd->dma_lch_in.dma_conf.device_fc = false;
  2028. return 0;
  2029. }
  2030. static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
  2031. {
  2032. dma_release_channel(dd->dma_lch_in.chan);
  2033. }
  2034. static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
  2035. {
  2036. dd->caps.has_dma = 0;
  2037. dd->caps.has_dualbuff = 0;
  2038. dd->caps.has_sha224 = 0;
  2039. dd->caps.has_sha_384_512 = 0;
  2040. dd->caps.has_uihv = 0;
  2041. dd->caps.has_hmac = 0;
  2042. /* keep only major version number */
  2043. switch (dd->hw_version & 0xff0) {
  2044. case 0x700:
  2045. case 0x600:
  2046. case 0x510:
  2047. dd->caps.has_dma = 1;
  2048. dd->caps.has_dualbuff = 1;
  2049. dd->caps.has_sha224 = 1;
  2050. dd->caps.has_sha_384_512 = 1;
  2051. dd->caps.has_uihv = 1;
  2052. dd->caps.has_hmac = 1;
  2053. break;
  2054. case 0x420:
  2055. dd->caps.has_dma = 1;
  2056. dd->caps.has_dualbuff = 1;
  2057. dd->caps.has_sha224 = 1;
  2058. dd->caps.has_sha_384_512 = 1;
  2059. dd->caps.has_uihv = 1;
  2060. break;
  2061. case 0x410:
  2062. dd->caps.has_dma = 1;
  2063. dd->caps.has_dualbuff = 1;
  2064. dd->caps.has_sha224 = 1;
  2065. dd->caps.has_sha_384_512 = 1;
  2066. break;
  2067. case 0x400:
  2068. dd->caps.has_dma = 1;
  2069. dd->caps.has_dualbuff = 1;
  2070. dd->caps.has_sha224 = 1;
  2071. break;
  2072. case 0x320:
  2073. break;
  2074. default:
  2075. dev_warn(dd->dev,
  2076. "Unmanaged sha version, set minimum capabilities\n");
  2077. break;
  2078. }
  2079. }
  2080. static const struct of_device_id atmel_sha_dt_ids[] = {
  2081. { .compatible = "atmel,at91sam9g46-sha" },
  2082. { /* sentinel */ }
  2083. };
  2084. MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
  2085. static int atmel_sha_probe(struct platform_device *pdev)
  2086. {
  2087. struct atmel_sha_dev *sha_dd;
  2088. struct device *dev = &pdev->dev;
  2089. struct resource *sha_res;
  2090. int err;
  2091. sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
  2092. if (!sha_dd)
  2093. return -ENOMEM;
  2094. sha_dd->dev = dev;
  2095. platform_set_drvdata(pdev, sha_dd);
  2096. INIT_LIST_HEAD(&sha_dd->list);
  2097. spin_lock_init(&sha_dd->lock);
  2098. tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
  2099. (unsigned long)sha_dd);
  2100. tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
  2101. (unsigned long)sha_dd);
  2102. crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
  2103. sha_dd->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &sha_res);
  2104. if (IS_ERR(sha_dd->io_base)) {
  2105. err = PTR_ERR(sha_dd->io_base);
  2106. goto err_tasklet_kill;
  2107. }
  2108. sha_dd->phys_base = sha_res->start;
  2109. /* Get the IRQ */
  2110. sha_dd->irq = platform_get_irq(pdev, 0);
  2111. if (sha_dd->irq < 0) {
  2112. err = sha_dd->irq;
  2113. goto err_tasklet_kill;
  2114. }
  2115. err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
  2116. IRQF_SHARED, "atmel-sha", sha_dd);
  2117. if (err) {
  2118. dev_err(dev, "unable to request sha irq.\n");
  2119. goto err_tasklet_kill;
  2120. }
  2121. /* Initializing the clock */
  2122. sha_dd->iclk = devm_clk_get_prepared(&pdev->dev, "sha_clk");
  2123. if (IS_ERR(sha_dd->iclk)) {
  2124. dev_err(dev, "clock initialization failed.\n");
  2125. err = PTR_ERR(sha_dd->iclk);
  2126. goto err_tasklet_kill;
  2127. }
  2128. err = atmel_sha_hw_version_init(sha_dd);
  2129. if (err)
  2130. goto err_tasklet_kill;
  2131. atmel_sha_get_cap(sha_dd);
  2132. if (sha_dd->caps.has_dma) {
  2133. err = atmel_sha_dma_init(sha_dd);
  2134. if (err)
  2135. goto err_tasklet_kill;
  2136. dev_info(dev, "using %s for DMA transfers\n",
  2137. dma_chan_name(sha_dd->dma_lch_in.chan));
  2138. }
  2139. spin_lock(&atmel_sha.lock);
  2140. list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
  2141. spin_unlock(&atmel_sha.lock);
  2142. err = atmel_sha_register_algs(sha_dd);
  2143. if (err)
  2144. goto err_algs;
  2145. dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
  2146. sha_dd->caps.has_sha224 ? "/SHA224" : "",
  2147. sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
  2148. return 0;
  2149. err_algs:
  2150. spin_lock(&atmel_sha.lock);
  2151. list_del(&sha_dd->list);
  2152. spin_unlock(&atmel_sha.lock);
  2153. if (sha_dd->caps.has_dma)
  2154. atmel_sha_dma_cleanup(sha_dd);
  2155. err_tasklet_kill:
  2156. tasklet_kill(&sha_dd->queue_task);
  2157. tasklet_kill(&sha_dd->done_task);
  2158. return err;
  2159. }
  2160. static void atmel_sha_remove(struct platform_device *pdev)
  2161. {
  2162. struct atmel_sha_dev *sha_dd = platform_get_drvdata(pdev);
  2163. spin_lock(&atmel_sha.lock);
  2164. list_del(&sha_dd->list);
  2165. spin_unlock(&atmel_sha.lock);
  2166. atmel_sha_unregister_algs(sha_dd);
  2167. tasklet_kill(&sha_dd->queue_task);
  2168. tasklet_kill(&sha_dd->done_task);
  2169. if (sha_dd->caps.has_dma)
  2170. atmel_sha_dma_cleanup(sha_dd);
  2171. }
  2172. static struct platform_driver atmel_sha_driver = {
  2173. .probe = atmel_sha_probe,
  2174. .remove_new = atmel_sha_remove,
  2175. .driver = {
  2176. .name = "atmel_sha",
  2177. .of_match_table = atmel_sha_dt_ids,
  2178. },
  2179. };
  2180. module_platform_driver(atmel_sha_driver);
  2181. MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
  2182. MODULE_LICENSE("GPL v2");
  2183. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");