atmel-aes.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for ATMEL AES HW acceleration.
  5. *
  6. * Copyright (c) 2012 Eukréa Electromatique - ATMEL
  7. * Author: Nicolas Royer <nicolas@eukrea.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * Some ideas are from omap-aes.c driver.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/hw_random.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/of_device.h>
  31. #include <linux/delay.h>
  32. #include <linux/crypto.h>
  33. #include <crypto/scatterwalk.h>
  34. #include <crypto/algapi.h>
  35. #include <crypto/aes.h>
  36. #include <crypto/xts.h>
  37. #include <crypto/internal/aead.h>
  38. #include <linux/platform_data/crypto-atmel.h>
  39. #include <dt-bindings/dma/at91.h>
  40. #include "atmel-aes-regs.h"
  41. #include "atmel-authenc.h"
  42. #define ATMEL_AES_PRIORITY 300
  43. #define ATMEL_AES_BUFFER_ORDER 2
  44. #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
  45. #define CFB8_BLOCK_SIZE 1
  46. #define CFB16_BLOCK_SIZE 2
  47. #define CFB32_BLOCK_SIZE 4
  48. #define CFB64_BLOCK_SIZE 8
  49. #define SIZE_IN_WORDS(x) ((x) >> 2)
  50. /* AES flags */
  51. /* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
  52. #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
  53. #define AES_FLAGS_GTAGEN AES_MR_GTAGEN
  54. #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
  55. #define AES_FLAGS_ECB AES_MR_OPMOD_ECB
  56. #define AES_FLAGS_CBC AES_MR_OPMOD_CBC
  57. #define AES_FLAGS_OFB AES_MR_OPMOD_OFB
  58. #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
  59. #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
  60. #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
  61. #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
  62. #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
  63. #define AES_FLAGS_CTR AES_MR_OPMOD_CTR
  64. #define AES_FLAGS_GCM AES_MR_OPMOD_GCM
  65. #define AES_FLAGS_XTS AES_MR_OPMOD_XTS
  66. #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
  67. AES_FLAGS_ENCRYPT | \
  68. AES_FLAGS_GTAGEN)
  69. #define AES_FLAGS_INIT BIT(2)
  70. #define AES_FLAGS_BUSY BIT(3)
  71. #define AES_FLAGS_DUMP_REG BIT(4)
  72. #define AES_FLAGS_OWN_SHA BIT(5)
  73. #define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY)
  74. #define ATMEL_AES_QUEUE_LENGTH 50
  75. #define ATMEL_AES_DMA_THRESHOLD 256
  76. struct atmel_aes_caps {
  77. bool has_dualbuff;
  78. bool has_cfb64;
  79. bool has_gcm;
  80. bool has_xts;
  81. bool has_authenc;
  82. u32 max_burst_size;
  83. };
  84. struct atmel_aes_dev;
  85. typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
  86. struct atmel_aes_base_ctx {
  87. struct atmel_aes_dev *dd;
  88. atmel_aes_fn_t start;
  89. int keylen;
  90. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  91. u16 block_size;
  92. };
  93. struct atmel_aes_ctx {
  94. struct atmel_aes_base_ctx base;
  95. };
  96. struct atmel_aes_ctr_ctx {
  97. struct atmel_aes_base_ctx base;
  98. u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
  99. size_t offset;
  100. struct scatterlist src[2];
  101. struct scatterlist dst[2];
  102. };
  103. struct atmel_aes_gcm_ctx {
  104. struct atmel_aes_base_ctx base;
  105. struct scatterlist src[2];
  106. struct scatterlist dst[2];
  107. u32 j0[AES_BLOCK_SIZE / sizeof(u32)];
  108. u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
  109. u32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
  110. size_t textlen;
  111. const u32 *ghash_in;
  112. u32 *ghash_out;
  113. atmel_aes_fn_t ghash_resume;
  114. };
  115. struct atmel_aes_xts_ctx {
  116. struct atmel_aes_base_ctx base;
  117. u32 key2[AES_KEYSIZE_256 / sizeof(u32)];
  118. };
  119. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  120. struct atmel_aes_authenc_ctx {
  121. struct atmel_aes_base_ctx base;
  122. struct atmel_sha_authenc_ctx *auth;
  123. };
  124. #endif
  125. struct atmel_aes_reqctx {
  126. unsigned long mode;
  127. };
  128. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  129. struct atmel_aes_authenc_reqctx {
  130. struct atmel_aes_reqctx base;
  131. struct scatterlist src[2];
  132. struct scatterlist dst[2];
  133. size_t textlen;
  134. u32 digest[SHA512_DIGEST_SIZE / sizeof(u32)];
  135. /* auth_req MUST be place last. */
  136. struct ahash_request auth_req;
  137. };
  138. #endif
  139. struct atmel_aes_dma {
  140. struct dma_chan *chan;
  141. struct scatterlist *sg;
  142. int nents;
  143. unsigned int remainder;
  144. unsigned int sg_len;
  145. };
  146. struct atmel_aes_dev {
  147. struct list_head list;
  148. unsigned long phys_base;
  149. void __iomem *io_base;
  150. struct crypto_async_request *areq;
  151. struct atmel_aes_base_ctx *ctx;
  152. bool is_async;
  153. atmel_aes_fn_t resume;
  154. atmel_aes_fn_t cpu_transfer_complete;
  155. struct device *dev;
  156. struct clk *iclk;
  157. int irq;
  158. unsigned long flags;
  159. spinlock_t lock;
  160. struct crypto_queue queue;
  161. struct tasklet_struct done_task;
  162. struct tasklet_struct queue_task;
  163. size_t total;
  164. size_t datalen;
  165. u32 *data;
  166. struct atmel_aes_dma src;
  167. struct atmel_aes_dma dst;
  168. size_t buflen;
  169. void *buf;
  170. struct scatterlist aligned_sg;
  171. struct scatterlist *real_dst;
  172. struct atmel_aes_caps caps;
  173. u32 hw_version;
  174. };
  175. struct atmel_aes_drv {
  176. struct list_head dev_list;
  177. spinlock_t lock;
  178. };
  179. static struct atmel_aes_drv atmel_aes = {
  180. .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
  181. .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
  182. };
  183. #ifdef VERBOSE_DEBUG
  184. static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
  185. {
  186. switch (offset) {
  187. case AES_CR:
  188. return "CR";
  189. case AES_MR:
  190. return "MR";
  191. case AES_ISR:
  192. return "ISR";
  193. case AES_IMR:
  194. return "IMR";
  195. case AES_IER:
  196. return "IER";
  197. case AES_IDR:
  198. return "IDR";
  199. case AES_KEYWR(0):
  200. case AES_KEYWR(1):
  201. case AES_KEYWR(2):
  202. case AES_KEYWR(3):
  203. case AES_KEYWR(4):
  204. case AES_KEYWR(5):
  205. case AES_KEYWR(6):
  206. case AES_KEYWR(7):
  207. snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
  208. break;
  209. case AES_IDATAR(0):
  210. case AES_IDATAR(1):
  211. case AES_IDATAR(2):
  212. case AES_IDATAR(3):
  213. snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
  214. break;
  215. case AES_ODATAR(0):
  216. case AES_ODATAR(1):
  217. case AES_ODATAR(2):
  218. case AES_ODATAR(3):
  219. snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
  220. break;
  221. case AES_IVR(0):
  222. case AES_IVR(1):
  223. case AES_IVR(2):
  224. case AES_IVR(3):
  225. snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
  226. break;
  227. case AES_AADLENR:
  228. return "AADLENR";
  229. case AES_CLENR:
  230. return "CLENR";
  231. case AES_GHASHR(0):
  232. case AES_GHASHR(1):
  233. case AES_GHASHR(2):
  234. case AES_GHASHR(3):
  235. snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
  236. break;
  237. case AES_TAGR(0):
  238. case AES_TAGR(1):
  239. case AES_TAGR(2):
  240. case AES_TAGR(3):
  241. snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
  242. break;
  243. case AES_CTRR:
  244. return "CTRR";
  245. case AES_GCMHR(0):
  246. case AES_GCMHR(1):
  247. case AES_GCMHR(2):
  248. case AES_GCMHR(3):
  249. snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
  250. break;
  251. case AES_EMR:
  252. return "EMR";
  253. case AES_TWR(0):
  254. case AES_TWR(1):
  255. case AES_TWR(2):
  256. case AES_TWR(3):
  257. snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2);
  258. break;
  259. case AES_ALPHAR(0):
  260. case AES_ALPHAR(1):
  261. case AES_ALPHAR(2):
  262. case AES_ALPHAR(3):
  263. snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2);
  264. break;
  265. default:
  266. snprintf(tmp, sz, "0x%02x", offset);
  267. break;
  268. }
  269. return tmp;
  270. }
  271. #endif /* VERBOSE_DEBUG */
  272. /* Shared functions */
  273. static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
  274. {
  275. u32 value = readl_relaxed(dd->io_base + offset);
  276. #ifdef VERBOSE_DEBUG
  277. if (dd->flags & AES_FLAGS_DUMP_REG) {
  278. char tmp[16];
  279. dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
  280. atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
  281. }
  282. #endif /* VERBOSE_DEBUG */
  283. return value;
  284. }
  285. static inline void atmel_aes_write(struct atmel_aes_dev *dd,
  286. u32 offset, u32 value)
  287. {
  288. #ifdef VERBOSE_DEBUG
  289. if (dd->flags & AES_FLAGS_DUMP_REG) {
  290. char tmp[16];
  291. dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
  292. atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
  293. }
  294. #endif /* VERBOSE_DEBUG */
  295. writel_relaxed(value, dd->io_base + offset);
  296. }
  297. static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
  298. u32 *value, int count)
  299. {
  300. for (; count--; value++, offset += 4)
  301. *value = atmel_aes_read(dd, offset);
  302. }
  303. static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
  304. const u32 *value, int count)
  305. {
  306. for (; count--; value++, offset += 4)
  307. atmel_aes_write(dd, offset, *value);
  308. }
  309. static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
  310. u32 *value)
  311. {
  312. atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
  313. }
  314. static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
  315. const u32 *value)
  316. {
  317. atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
  318. }
  319. static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
  320. atmel_aes_fn_t resume)
  321. {
  322. u32 isr = atmel_aes_read(dd, AES_ISR);
  323. if (unlikely(isr & AES_INT_DATARDY))
  324. return resume(dd);
  325. dd->resume = resume;
  326. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  327. return -EINPROGRESS;
  328. }
  329. static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
  330. {
  331. len &= block_size - 1;
  332. return len ? block_size - len : 0;
  333. }
  334. static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
  335. {
  336. struct atmel_aes_dev *aes_dd = NULL;
  337. struct atmel_aes_dev *tmp;
  338. spin_lock_bh(&atmel_aes.lock);
  339. if (!ctx->dd) {
  340. list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
  341. aes_dd = tmp;
  342. break;
  343. }
  344. ctx->dd = aes_dd;
  345. } else {
  346. aes_dd = ctx->dd;
  347. }
  348. spin_unlock_bh(&atmel_aes.lock);
  349. return aes_dd;
  350. }
  351. static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
  352. {
  353. int err;
  354. err = clk_enable(dd->iclk);
  355. if (err)
  356. return err;
  357. if (!(dd->flags & AES_FLAGS_INIT)) {
  358. atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
  359. atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
  360. dd->flags |= AES_FLAGS_INIT;
  361. }
  362. return 0;
  363. }
  364. static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
  365. {
  366. return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
  367. }
  368. static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
  369. {
  370. int err;
  371. err = atmel_aes_hw_init(dd);
  372. if (err)
  373. return err;
  374. dd->hw_version = atmel_aes_get_version(dd);
  375. dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
  376. clk_disable(dd->iclk);
  377. return 0;
  378. }
  379. static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
  380. const struct atmel_aes_reqctx *rctx)
  381. {
  382. /* Clear all but persistent flags and set request flags. */
  383. dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
  384. }
  385. static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
  386. {
  387. return (dd->flags & AES_FLAGS_ENCRYPT);
  388. }
  389. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  390. static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err);
  391. #endif
  392. static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
  393. {
  394. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  395. atmel_aes_authenc_complete(dd, err);
  396. #endif
  397. clk_disable(dd->iclk);
  398. dd->flags &= ~AES_FLAGS_BUSY;
  399. if (dd->is_async)
  400. dd->areq->complete(dd->areq, err);
  401. tasklet_schedule(&dd->queue_task);
  402. return err;
  403. }
  404. static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma,
  405. const u32 *iv, const u32 *key, int keylen)
  406. {
  407. u32 valmr = 0;
  408. /* MR register must be set before IV registers */
  409. if (keylen == AES_KEYSIZE_128)
  410. valmr |= AES_MR_KEYSIZE_128;
  411. else if (keylen == AES_KEYSIZE_192)
  412. valmr |= AES_MR_KEYSIZE_192;
  413. else
  414. valmr |= AES_MR_KEYSIZE_256;
  415. valmr |= dd->flags & AES_FLAGS_MODE_MASK;
  416. if (use_dma) {
  417. valmr |= AES_MR_SMOD_IDATAR0;
  418. if (dd->caps.has_dualbuff)
  419. valmr |= AES_MR_DUALBUFF;
  420. } else {
  421. valmr |= AES_MR_SMOD_AUTO;
  422. }
  423. atmel_aes_write(dd, AES_MR, valmr);
  424. atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen));
  425. if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
  426. atmel_aes_write_block(dd, AES_IVR(0), iv);
  427. }
  428. static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
  429. const u32 *iv)
  430. {
  431. atmel_aes_write_ctrl_key(dd, use_dma, iv,
  432. dd->ctx->key, dd->ctx->keylen);
  433. }
  434. /* CPU transfer */
  435. static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
  436. {
  437. int err = 0;
  438. u32 isr;
  439. for (;;) {
  440. atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
  441. dd->data += 4;
  442. dd->datalen -= AES_BLOCK_SIZE;
  443. if (dd->datalen < AES_BLOCK_SIZE)
  444. break;
  445. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  446. isr = atmel_aes_read(dd, AES_ISR);
  447. if (!(isr & AES_INT_DATARDY)) {
  448. dd->resume = atmel_aes_cpu_transfer;
  449. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  450. return -EINPROGRESS;
  451. }
  452. }
  453. if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
  454. dd->buf, dd->total))
  455. err = -EINVAL;
  456. if (err)
  457. return atmel_aes_complete(dd, err);
  458. return dd->cpu_transfer_complete(dd);
  459. }
  460. static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
  461. struct scatterlist *src,
  462. struct scatterlist *dst,
  463. size_t len,
  464. atmel_aes_fn_t resume)
  465. {
  466. size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
  467. if (unlikely(len == 0))
  468. return -EINVAL;
  469. sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
  470. dd->total = len;
  471. dd->real_dst = dst;
  472. dd->cpu_transfer_complete = resume;
  473. dd->datalen = len + padlen;
  474. dd->data = (u32 *)dd->buf;
  475. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  476. return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
  477. }
  478. /* DMA transfer */
  479. static void atmel_aes_dma_callback(void *data);
  480. static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
  481. struct scatterlist *sg,
  482. size_t len,
  483. struct atmel_aes_dma *dma)
  484. {
  485. int nents;
  486. if (!IS_ALIGNED(len, dd->ctx->block_size))
  487. return false;
  488. for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  489. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  490. return false;
  491. if (len <= sg->length) {
  492. if (!IS_ALIGNED(len, dd->ctx->block_size))
  493. return false;
  494. dma->nents = nents+1;
  495. dma->remainder = sg->length - len;
  496. sg->length = len;
  497. return true;
  498. }
  499. if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
  500. return false;
  501. len -= sg->length;
  502. }
  503. return false;
  504. }
  505. static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
  506. {
  507. struct scatterlist *sg = dma->sg;
  508. int nents = dma->nents;
  509. if (!dma->remainder)
  510. return;
  511. while (--nents > 0 && sg)
  512. sg = sg_next(sg);
  513. if (!sg)
  514. return;
  515. sg->length += dma->remainder;
  516. }
  517. static int atmel_aes_map(struct atmel_aes_dev *dd,
  518. struct scatterlist *src,
  519. struct scatterlist *dst,
  520. size_t len)
  521. {
  522. bool src_aligned, dst_aligned;
  523. size_t padlen;
  524. dd->total = len;
  525. dd->src.sg = src;
  526. dd->dst.sg = dst;
  527. dd->real_dst = dst;
  528. src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
  529. if (src == dst)
  530. dst_aligned = src_aligned;
  531. else
  532. dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
  533. if (!src_aligned || !dst_aligned) {
  534. padlen = atmel_aes_padlen(len, dd->ctx->block_size);
  535. if (dd->buflen < len + padlen)
  536. return -ENOMEM;
  537. if (!src_aligned) {
  538. sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
  539. dd->src.sg = &dd->aligned_sg;
  540. dd->src.nents = 1;
  541. dd->src.remainder = 0;
  542. }
  543. if (!dst_aligned) {
  544. dd->dst.sg = &dd->aligned_sg;
  545. dd->dst.nents = 1;
  546. dd->dst.remainder = 0;
  547. }
  548. sg_init_table(&dd->aligned_sg, 1);
  549. sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
  550. }
  551. if (dd->src.sg == dd->dst.sg) {
  552. dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
  553. DMA_BIDIRECTIONAL);
  554. dd->dst.sg_len = dd->src.sg_len;
  555. if (!dd->src.sg_len)
  556. return -EFAULT;
  557. } else {
  558. dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
  559. DMA_TO_DEVICE);
  560. if (!dd->src.sg_len)
  561. return -EFAULT;
  562. dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
  563. DMA_FROM_DEVICE);
  564. if (!dd->dst.sg_len) {
  565. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  566. DMA_TO_DEVICE);
  567. return -EFAULT;
  568. }
  569. }
  570. return 0;
  571. }
  572. static void atmel_aes_unmap(struct atmel_aes_dev *dd)
  573. {
  574. if (dd->src.sg == dd->dst.sg) {
  575. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  576. DMA_BIDIRECTIONAL);
  577. if (dd->src.sg != &dd->aligned_sg)
  578. atmel_aes_restore_sg(&dd->src);
  579. } else {
  580. dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
  581. DMA_FROM_DEVICE);
  582. if (dd->dst.sg != &dd->aligned_sg)
  583. atmel_aes_restore_sg(&dd->dst);
  584. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  585. DMA_TO_DEVICE);
  586. if (dd->src.sg != &dd->aligned_sg)
  587. atmel_aes_restore_sg(&dd->src);
  588. }
  589. if (dd->dst.sg == &dd->aligned_sg)
  590. sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
  591. dd->buf, dd->total);
  592. }
  593. static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
  594. enum dma_slave_buswidth addr_width,
  595. enum dma_transfer_direction dir,
  596. u32 maxburst)
  597. {
  598. struct dma_async_tx_descriptor *desc;
  599. struct dma_slave_config config;
  600. dma_async_tx_callback callback;
  601. struct atmel_aes_dma *dma;
  602. int err;
  603. memset(&config, 0, sizeof(config));
  604. config.direction = dir;
  605. config.src_addr_width = addr_width;
  606. config.dst_addr_width = addr_width;
  607. config.src_maxburst = maxburst;
  608. config.dst_maxburst = maxburst;
  609. switch (dir) {
  610. case DMA_MEM_TO_DEV:
  611. dma = &dd->src;
  612. callback = NULL;
  613. config.dst_addr = dd->phys_base + AES_IDATAR(0);
  614. break;
  615. case DMA_DEV_TO_MEM:
  616. dma = &dd->dst;
  617. callback = atmel_aes_dma_callback;
  618. config.src_addr = dd->phys_base + AES_ODATAR(0);
  619. break;
  620. default:
  621. return -EINVAL;
  622. }
  623. err = dmaengine_slave_config(dma->chan, &config);
  624. if (err)
  625. return err;
  626. desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
  627. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  628. if (!desc)
  629. return -ENOMEM;
  630. desc->callback = callback;
  631. desc->callback_param = dd;
  632. dmaengine_submit(desc);
  633. dma_async_issue_pending(dma->chan);
  634. return 0;
  635. }
  636. static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd,
  637. enum dma_transfer_direction dir)
  638. {
  639. struct atmel_aes_dma *dma;
  640. switch (dir) {
  641. case DMA_MEM_TO_DEV:
  642. dma = &dd->src;
  643. break;
  644. case DMA_DEV_TO_MEM:
  645. dma = &dd->dst;
  646. break;
  647. default:
  648. return;
  649. }
  650. dmaengine_terminate_all(dma->chan);
  651. }
  652. static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
  653. struct scatterlist *src,
  654. struct scatterlist *dst,
  655. size_t len,
  656. atmel_aes_fn_t resume)
  657. {
  658. enum dma_slave_buswidth addr_width;
  659. u32 maxburst;
  660. int err;
  661. switch (dd->ctx->block_size) {
  662. case CFB8_BLOCK_SIZE:
  663. addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  664. maxburst = 1;
  665. break;
  666. case CFB16_BLOCK_SIZE:
  667. addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  668. maxburst = 1;
  669. break;
  670. case CFB32_BLOCK_SIZE:
  671. case CFB64_BLOCK_SIZE:
  672. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  673. maxburst = 1;
  674. break;
  675. case AES_BLOCK_SIZE:
  676. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  677. maxburst = dd->caps.max_burst_size;
  678. break;
  679. default:
  680. err = -EINVAL;
  681. goto exit;
  682. }
  683. err = atmel_aes_map(dd, src, dst, len);
  684. if (err)
  685. goto exit;
  686. dd->resume = resume;
  687. /* Set output DMA transfer first */
  688. err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
  689. maxburst);
  690. if (err)
  691. goto unmap;
  692. /* Then set input DMA transfer */
  693. err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
  694. maxburst);
  695. if (err)
  696. goto output_transfer_stop;
  697. return -EINPROGRESS;
  698. output_transfer_stop:
  699. atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
  700. unmap:
  701. atmel_aes_unmap(dd);
  702. exit:
  703. return atmel_aes_complete(dd, err);
  704. }
  705. static void atmel_aes_dma_stop(struct atmel_aes_dev *dd)
  706. {
  707. atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV);
  708. atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
  709. atmel_aes_unmap(dd);
  710. }
  711. static void atmel_aes_dma_callback(void *data)
  712. {
  713. struct atmel_aes_dev *dd = data;
  714. atmel_aes_dma_stop(dd);
  715. dd->is_async = true;
  716. (void)dd->resume(dd);
  717. }
  718. static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
  719. struct crypto_async_request *new_areq)
  720. {
  721. struct crypto_async_request *areq, *backlog;
  722. struct atmel_aes_base_ctx *ctx;
  723. unsigned long flags;
  724. bool start_async;
  725. int err, ret = 0;
  726. spin_lock_irqsave(&dd->lock, flags);
  727. if (new_areq)
  728. ret = crypto_enqueue_request(&dd->queue, new_areq);
  729. if (dd->flags & AES_FLAGS_BUSY) {
  730. spin_unlock_irqrestore(&dd->lock, flags);
  731. return ret;
  732. }
  733. backlog = crypto_get_backlog(&dd->queue);
  734. areq = crypto_dequeue_request(&dd->queue);
  735. if (areq)
  736. dd->flags |= AES_FLAGS_BUSY;
  737. spin_unlock_irqrestore(&dd->lock, flags);
  738. if (!areq)
  739. return ret;
  740. if (backlog)
  741. backlog->complete(backlog, -EINPROGRESS);
  742. ctx = crypto_tfm_ctx(areq->tfm);
  743. dd->areq = areq;
  744. dd->ctx = ctx;
  745. start_async = (areq != new_areq);
  746. dd->is_async = start_async;
  747. /* WARNING: ctx->start() MAY change dd->is_async. */
  748. err = ctx->start(dd);
  749. return (start_async) ? ret : err;
  750. }
  751. /* AES async block ciphers */
  752. static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
  753. {
  754. return atmel_aes_complete(dd, 0);
  755. }
  756. static int atmel_aes_start(struct atmel_aes_dev *dd)
  757. {
  758. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  759. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  760. bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD ||
  761. dd->ctx->block_size != AES_BLOCK_SIZE);
  762. int err;
  763. atmel_aes_set_mode(dd, rctx);
  764. err = atmel_aes_hw_init(dd);
  765. if (err)
  766. return atmel_aes_complete(dd, err);
  767. atmel_aes_write_ctrl(dd, use_dma, req->info);
  768. if (use_dma)
  769. return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
  770. atmel_aes_transfer_complete);
  771. return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
  772. atmel_aes_transfer_complete);
  773. }
  774. static inline struct atmel_aes_ctr_ctx *
  775. atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
  776. {
  777. return container_of(ctx, struct atmel_aes_ctr_ctx, base);
  778. }
  779. static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
  780. {
  781. struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
  782. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  783. struct scatterlist *src, *dst;
  784. size_t datalen;
  785. u32 ctr;
  786. u16 blocks, start, end;
  787. bool use_dma, fragmented = false;
  788. /* Check for transfer completion. */
  789. ctx->offset += dd->total;
  790. if (ctx->offset >= req->nbytes)
  791. return atmel_aes_transfer_complete(dd);
  792. /* Compute data length. */
  793. datalen = req->nbytes - ctx->offset;
  794. blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
  795. ctr = be32_to_cpu(ctx->iv[3]);
  796. /* Check 16bit counter overflow. */
  797. start = ctr & 0xffff;
  798. end = start + blocks - 1;
  799. if (blocks >> 16 || end < start) {
  800. ctr |= 0xffff;
  801. datalen = AES_BLOCK_SIZE * (0x10000 - start);
  802. fragmented = true;
  803. }
  804. use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
  805. /* Jump to offset. */
  806. src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
  807. dst = ((req->src == req->dst) ? src :
  808. scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
  809. /* Configure hardware. */
  810. atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
  811. if (unlikely(fragmented)) {
  812. /*
  813. * Increment the counter manually to cope with the hardware
  814. * counter overflow.
  815. */
  816. ctx->iv[3] = cpu_to_be32(ctr);
  817. crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
  818. }
  819. if (use_dma)
  820. return atmel_aes_dma_start(dd, src, dst, datalen,
  821. atmel_aes_ctr_transfer);
  822. return atmel_aes_cpu_start(dd, src, dst, datalen,
  823. atmel_aes_ctr_transfer);
  824. }
  825. static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
  826. {
  827. struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
  828. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  829. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  830. int err;
  831. atmel_aes_set_mode(dd, rctx);
  832. err = atmel_aes_hw_init(dd);
  833. if (err)
  834. return atmel_aes_complete(dd, err);
  835. memcpy(ctx->iv, req->info, AES_BLOCK_SIZE);
  836. ctx->offset = 0;
  837. dd->total = 0;
  838. return atmel_aes_ctr_transfer(dd);
  839. }
  840. static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  841. {
  842. struct atmel_aes_base_ctx *ctx;
  843. struct atmel_aes_reqctx *rctx;
  844. struct atmel_aes_dev *dd;
  845. ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  846. switch (mode & AES_FLAGS_OPMODE_MASK) {
  847. case AES_FLAGS_CFB8:
  848. ctx->block_size = CFB8_BLOCK_SIZE;
  849. break;
  850. case AES_FLAGS_CFB16:
  851. ctx->block_size = CFB16_BLOCK_SIZE;
  852. break;
  853. case AES_FLAGS_CFB32:
  854. ctx->block_size = CFB32_BLOCK_SIZE;
  855. break;
  856. case AES_FLAGS_CFB64:
  857. ctx->block_size = CFB64_BLOCK_SIZE;
  858. break;
  859. default:
  860. ctx->block_size = AES_BLOCK_SIZE;
  861. break;
  862. }
  863. dd = atmel_aes_find_dev(ctx);
  864. if (!dd)
  865. return -ENODEV;
  866. rctx = ablkcipher_request_ctx(req);
  867. rctx->mode = mode;
  868. return atmel_aes_handle_queue(dd, &req->base);
  869. }
  870. static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  871. unsigned int keylen)
  872. {
  873. struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  874. if (keylen != AES_KEYSIZE_128 &&
  875. keylen != AES_KEYSIZE_192 &&
  876. keylen != AES_KEYSIZE_256) {
  877. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  878. return -EINVAL;
  879. }
  880. memcpy(ctx->key, key, keylen);
  881. ctx->keylen = keylen;
  882. return 0;
  883. }
  884. static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
  885. {
  886. return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
  887. }
  888. static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
  889. {
  890. return atmel_aes_crypt(req, AES_FLAGS_ECB);
  891. }
  892. static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
  893. {
  894. return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
  895. }
  896. static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
  897. {
  898. return atmel_aes_crypt(req, AES_FLAGS_CBC);
  899. }
  900. static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
  901. {
  902. return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
  903. }
  904. static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
  905. {
  906. return atmel_aes_crypt(req, AES_FLAGS_OFB);
  907. }
  908. static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
  909. {
  910. return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
  911. }
  912. static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
  913. {
  914. return atmel_aes_crypt(req, AES_FLAGS_CFB128);
  915. }
  916. static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
  917. {
  918. return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
  919. }
  920. static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
  921. {
  922. return atmel_aes_crypt(req, AES_FLAGS_CFB64);
  923. }
  924. static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
  925. {
  926. return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
  927. }
  928. static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
  929. {
  930. return atmel_aes_crypt(req, AES_FLAGS_CFB32);
  931. }
  932. static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
  933. {
  934. return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
  935. }
  936. static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
  937. {
  938. return atmel_aes_crypt(req, AES_FLAGS_CFB16);
  939. }
  940. static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
  941. {
  942. return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
  943. }
  944. static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
  945. {
  946. return atmel_aes_crypt(req, AES_FLAGS_CFB8);
  947. }
  948. static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
  949. {
  950. return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
  951. }
  952. static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
  953. {
  954. return atmel_aes_crypt(req, AES_FLAGS_CTR);
  955. }
  956. static int atmel_aes_cra_init(struct crypto_tfm *tfm)
  957. {
  958. struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  959. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  960. ctx->base.start = atmel_aes_start;
  961. return 0;
  962. }
  963. static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm)
  964. {
  965. struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  966. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  967. ctx->base.start = atmel_aes_ctr_start;
  968. return 0;
  969. }
  970. static void atmel_aes_cra_exit(struct crypto_tfm *tfm)
  971. {
  972. }
  973. static struct crypto_alg aes_algs[] = {
  974. {
  975. .cra_name = "ecb(aes)",
  976. .cra_driver_name = "atmel-ecb-aes",
  977. .cra_priority = ATMEL_AES_PRIORITY,
  978. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  979. .cra_blocksize = AES_BLOCK_SIZE,
  980. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  981. .cra_alignmask = 0xf,
  982. .cra_type = &crypto_ablkcipher_type,
  983. .cra_module = THIS_MODULE,
  984. .cra_init = atmel_aes_cra_init,
  985. .cra_exit = atmel_aes_cra_exit,
  986. .cra_u.ablkcipher = {
  987. .min_keysize = AES_MIN_KEY_SIZE,
  988. .max_keysize = AES_MAX_KEY_SIZE,
  989. .setkey = atmel_aes_setkey,
  990. .encrypt = atmel_aes_ecb_encrypt,
  991. .decrypt = atmel_aes_ecb_decrypt,
  992. }
  993. },
  994. {
  995. .cra_name = "cbc(aes)",
  996. .cra_driver_name = "atmel-cbc-aes",
  997. .cra_priority = ATMEL_AES_PRIORITY,
  998. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  999. .cra_blocksize = AES_BLOCK_SIZE,
  1000. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1001. .cra_alignmask = 0xf,
  1002. .cra_type = &crypto_ablkcipher_type,
  1003. .cra_module = THIS_MODULE,
  1004. .cra_init = atmel_aes_cra_init,
  1005. .cra_exit = atmel_aes_cra_exit,
  1006. .cra_u.ablkcipher = {
  1007. .min_keysize = AES_MIN_KEY_SIZE,
  1008. .max_keysize = AES_MAX_KEY_SIZE,
  1009. .ivsize = AES_BLOCK_SIZE,
  1010. .setkey = atmel_aes_setkey,
  1011. .encrypt = atmel_aes_cbc_encrypt,
  1012. .decrypt = atmel_aes_cbc_decrypt,
  1013. }
  1014. },
  1015. {
  1016. .cra_name = "ofb(aes)",
  1017. .cra_driver_name = "atmel-ofb-aes",
  1018. .cra_priority = ATMEL_AES_PRIORITY,
  1019. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1020. .cra_blocksize = AES_BLOCK_SIZE,
  1021. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1022. .cra_alignmask = 0xf,
  1023. .cra_type = &crypto_ablkcipher_type,
  1024. .cra_module = THIS_MODULE,
  1025. .cra_init = atmel_aes_cra_init,
  1026. .cra_exit = atmel_aes_cra_exit,
  1027. .cra_u.ablkcipher = {
  1028. .min_keysize = AES_MIN_KEY_SIZE,
  1029. .max_keysize = AES_MAX_KEY_SIZE,
  1030. .ivsize = AES_BLOCK_SIZE,
  1031. .setkey = atmel_aes_setkey,
  1032. .encrypt = atmel_aes_ofb_encrypt,
  1033. .decrypt = atmel_aes_ofb_decrypt,
  1034. }
  1035. },
  1036. {
  1037. .cra_name = "cfb(aes)",
  1038. .cra_driver_name = "atmel-cfb-aes",
  1039. .cra_priority = ATMEL_AES_PRIORITY,
  1040. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1041. .cra_blocksize = AES_BLOCK_SIZE,
  1042. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1043. .cra_alignmask = 0xf,
  1044. .cra_type = &crypto_ablkcipher_type,
  1045. .cra_module = THIS_MODULE,
  1046. .cra_init = atmel_aes_cra_init,
  1047. .cra_exit = atmel_aes_cra_exit,
  1048. .cra_u.ablkcipher = {
  1049. .min_keysize = AES_MIN_KEY_SIZE,
  1050. .max_keysize = AES_MAX_KEY_SIZE,
  1051. .ivsize = AES_BLOCK_SIZE,
  1052. .setkey = atmel_aes_setkey,
  1053. .encrypt = atmel_aes_cfb_encrypt,
  1054. .decrypt = atmel_aes_cfb_decrypt,
  1055. }
  1056. },
  1057. {
  1058. .cra_name = "cfb32(aes)",
  1059. .cra_driver_name = "atmel-cfb32-aes",
  1060. .cra_priority = ATMEL_AES_PRIORITY,
  1061. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1062. .cra_blocksize = CFB32_BLOCK_SIZE,
  1063. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1064. .cra_alignmask = 0x3,
  1065. .cra_type = &crypto_ablkcipher_type,
  1066. .cra_module = THIS_MODULE,
  1067. .cra_init = atmel_aes_cra_init,
  1068. .cra_exit = atmel_aes_cra_exit,
  1069. .cra_u.ablkcipher = {
  1070. .min_keysize = AES_MIN_KEY_SIZE,
  1071. .max_keysize = AES_MAX_KEY_SIZE,
  1072. .ivsize = AES_BLOCK_SIZE,
  1073. .setkey = atmel_aes_setkey,
  1074. .encrypt = atmel_aes_cfb32_encrypt,
  1075. .decrypt = atmel_aes_cfb32_decrypt,
  1076. }
  1077. },
  1078. {
  1079. .cra_name = "cfb16(aes)",
  1080. .cra_driver_name = "atmel-cfb16-aes",
  1081. .cra_priority = ATMEL_AES_PRIORITY,
  1082. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1083. .cra_blocksize = CFB16_BLOCK_SIZE,
  1084. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1085. .cra_alignmask = 0x1,
  1086. .cra_type = &crypto_ablkcipher_type,
  1087. .cra_module = THIS_MODULE,
  1088. .cra_init = atmel_aes_cra_init,
  1089. .cra_exit = atmel_aes_cra_exit,
  1090. .cra_u.ablkcipher = {
  1091. .min_keysize = AES_MIN_KEY_SIZE,
  1092. .max_keysize = AES_MAX_KEY_SIZE,
  1093. .ivsize = AES_BLOCK_SIZE,
  1094. .setkey = atmel_aes_setkey,
  1095. .encrypt = atmel_aes_cfb16_encrypt,
  1096. .decrypt = atmel_aes_cfb16_decrypt,
  1097. }
  1098. },
  1099. {
  1100. .cra_name = "cfb8(aes)",
  1101. .cra_driver_name = "atmel-cfb8-aes",
  1102. .cra_priority = ATMEL_AES_PRIORITY,
  1103. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1104. .cra_blocksize = CFB8_BLOCK_SIZE,
  1105. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1106. .cra_alignmask = 0x0,
  1107. .cra_type = &crypto_ablkcipher_type,
  1108. .cra_module = THIS_MODULE,
  1109. .cra_init = atmel_aes_cra_init,
  1110. .cra_exit = atmel_aes_cra_exit,
  1111. .cra_u.ablkcipher = {
  1112. .min_keysize = AES_MIN_KEY_SIZE,
  1113. .max_keysize = AES_MAX_KEY_SIZE,
  1114. .ivsize = AES_BLOCK_SIZE,
  1115. .setkey = atmel_aes_setkey,
  1116. .encrypt = atmel_aes_cfb8_encrypt,
  1117. .decrypt = atmel_aes_cfb8_decrypt,
  1118. }
  1119. },
  1120. {
  1121. .cra_name = "ctr(aes)",
  1122. .cra_driver_name = "atmel-ctr-aes",
  1123. .cra_priority = ATMEL_AES_PRIORITY,
  1124. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1125. .cra_blocksize = 1,
  1126. .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
  1127. .cra_alignmask = 0xf,
  1128. .cra_type = &crypto_ablkcipher_type,
  1129. .cra_module = THIS_MODULE,
  1130. .cra_init = atmel_aes_ctr_cra_init,
  1131. .cra_exit = atmel_aes_cra_exit,
  1132. .cra_u.ablkcipher = {
  1133. .min_keysize = AES_MIN_KEY_SIZE,
  1134. .max_keysize = AES_MAX_KEY_SIZE,
  1135. .ivsize = AES_BLOCK_SIZE,
  1136. .setkey = atmel_aes_setkey,
  1137. .encrypt = atmel_aes_ctr_encrypt,
  1138. .decrypt = atmel_aes_ctr_decrypt,
  1139. }
  1140. },
  1141. };
  1142. static struct crypto_alg aes_cfb64_alg = {
  1143. .cra_name = "cfb64(aes)",
  1144. .cra_driver_name = "atmel-cfb64-aes",
  1145. .cra_priority = ATMEL_AES_PRIORITY,
  1146. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1147. .cra_blocksize = CFB64_BLOCK_SIZE,
  1148. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1149. .cra_alignmask = 0x7,
  1150. .cra_type = &crypto_ablkcipher_type,
  1151. .cra_module = THIS_MODULE,
  1152. .cra_init = atmel_aes_cra_init,
  1153. .cra_exit = atmel_aes_cra_exit,
  1154. .cra_u.ablkcipher = {
  1155. .min_keysize = AES_MIN_KEY_SIZE,
  1156. .max_keysize = AES_MAX_KEY_SIZE,
  1157. .ivsize = AES_BLOCK_SIZE,
  1158. .setkey = atmel_aes_setkey,
  1159. .encrypt = atmel_aes_cfb64_encrypt,
  1160. .decrypt = atmel_aes_cfb64_decrypt,
  1161. }
  1162. };
  1163. /* gcm aead functions */
  1164. static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
  1165. const u32 *data, size_t datalen,
  1166. const u32 *ghash_in, u32 *ghash_out,
  1167. atmel_aes_fn_t resume);
  1168. static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
  1169. static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
  1170. static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
  1171. static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
  1172. static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
  1173. static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
  1174. static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
  1175. static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
  1176. static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
  1177. static inline struct atmel_aes_gcm_ctx *
  1178. atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
  1179. {
  1180. return container_of(ctx, struct atmel_aes_gcm_ctx, base);
  1181. }
  1182. static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
  1183. const u32 *data, size_t datalen,
  1184. const u32 *ghash_in, u32 *ghash_out,
  1185. atmel_aes_fn_t resume)
  1186. {
  1187. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1188. dd->data = (u32 *)data;
  1189. dd->datalen = datalen;
  1190. ctx->ghash_in = ghash_in;
  1191. ctx->ghash_out = ghash_out;
  1192. ctx->ghash_resume = resume;
  1193. atmel_aes_write_ctrl(dd, false, NULL);
  1194. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
  1195. }
  1196. static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
  1197. {
  1198. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1199. /* Set the data length. */
  1200. atmel_aes_write(dd, AES_AADLENR, dd->total);
  1201. atmel_aes_write(dd, AES_CLENR, 0);
  1202. /* If needed, overwrite the GCM Intermediate Hash Word Registers */
  1203. if (ctx->ghash_in)
  1204. atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
  1205. return atmel_aes_gcm_ghash_finalize(dd);
  1206. }
  1207. static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
  1208. {
  1209. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1210. u32 isr;
  1211. /* Write data into the Input Data Registers. */
  1212. while (dd->datalen > 0) {
  1213. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  1214. dd->data += 4;
  1215. dd->datalen -= AES_BLOCK_SIZE;
  1216. isr = atmel_aes_read(dd, AES_ISR);
  1217. if (!(isr & AES_INT_DATARDY)) {
  1218. dd->resume = atmel_aes_gcm_ghash_finalize;
  1219. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  1220. return -EINPROGRESS;
  1221. }
  1222. }
  1223. /* Read the computed hash from GHASHRx. */
  1224. atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
  1225. return ctx->ghash_resume(dd);
  1226. }
  1227. static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
  1228. {
  1229. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1230. struct aead_request *req = aead_request_cast(dd->areq);
  1231. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1232. struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
  1233. size_t ivsize = crypto_aead_ivsize(tfm);
  1234. size_t datalen, padlen;
  1235. const void *iv = req->iv;
  1236. u8 *data = dd->buf;
  1237. int err;
  1238. atmel_aes_set_mode(dd, rctx);
  1239. err = atmel_aes_hw_init(dd);
  1240. if (err)
  1241. return atmel_aes_complete(dd, err);
  1242. if (likely(ivsize == 12)) {
  1243. memcpy(ctx->j0, iv, ivsize);
  1244. ctx->j0[3] = cpu_to_be32(1);
  1245. return atmel_aes_gcm_process(dd);
  1246. }
  1247. padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
  1248. datalen = ivsize + padlen + AES_BLOCK_SIZE;
  1249. if (datalen > dd->buflen)
  1250. return atmel_aes_complete(dd, -EINVAL);
  1251. memcpy(data, iv, ivsize);
  1252. memset(data + ivsize, 0, padlen + sizeof(u64));
  1253. ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
  1254. return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
  1255. NULL, ctx->j0, atmel_aes_gcm_process);
  1256. }
  1257. static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
  1258. {
  1259. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1260. struct aead_request *req = aead_request_cast(dd->areq);
  1261. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1262. bool enc = atmel_aes_is_encrypt(dd);
  1263. u32 authsize;
  1264. /* Compute text length. */
  1265. authsize = crypto_aead_authsize(tfm);
  1266. ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
  1267. /*
  1268. * According to tcrypt test suite, the GCM Automatic Tag Generation
  1269. * fails when both the message and its associated data are empty.
  1270. */
  1271. if (likely(req->assoclen != 0 || ctx->textlen != 0))
  1272. dd->flags |= AES_FLAGS_GTAGEN;
  1273. atmel_aes_write_ctrl(dd, false, NULL);
  1274. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
  1275. }
  1276. static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
  1277. {
  1278. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1279. struct aead_request *req = aead_request_cast(dd->areq);
  1280. u32 j0_lsw, *j0 = ctx->j0;
  1281. size_t padlen;
  1282. /* Write incr32(J0) into IV. */
  1283. j0_lsw = j0[3];
  1284. j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
  1285. atmel_aes_write_block(dd, AES_IVR(0), j0);
  1286. j0[3] = j0_lsw;
  1287. /* Set aad and text lengths. */
  1288. atmel_aes_write(dd, AES_AADLENR, req->assoclen);
  1289. atmel_aes_write(dd, AES_CLENR, ctx->textlen);
  1290. /* Check whether AAD are present. */
  1291. if (unlikely(req->assoclen == 0)) {
  1292. dd->datalen = 0;
  1293. return atmel_aes_gcm_data(dd);
  1294. }
  1295. /* Copy assoc data and add padding. */
  1296. padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
  1297. if (unlikely(req->assoclen + padlen > dd->buflen))
  1298. return atmel_aes_complete(dd, -EINVAL);
  1299. sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
  1300. /* Write assoc data into the Input Data register. */
  1301. dd->data = (u32 *)dd->buf;
  1302. dd->datalen = req->assoclen + padlen;
  1303. return atmel_aes_gcm_data(dd);
  1304. }
  1305. static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
  1306. {
  1307. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1308. struct aead_request *req = aead_request_cast(dd->areq);
  1309. bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
  1310. struct scatterlist *src, *dst;
  1311. u32 isr, mr;
  1312. /* Write AAD first. */
  1313. while (dd->datalen > 0) {
  1314. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  1315. dd->data += 4;
  1316. dd->datalen -= AES_BLOCK_SIZE;
  1317. isr = atmel_aes_read(dd, AES_ISR);
  1318. if (!(isr & AES_INT_DATARDY)) {
  1319. dd->resume = atmel_aes_gcm_data;
  1320. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  1321. return -EINPROGRESS;
  1322. }
  1323. }
  1324. /* GMAC only. */
  1325. if (unlikely(ctx->textlen == 0))
  1326. return atmel_aes_gcm_tag_init(dd);
  1327. /* Prepare src and dst scatter lists to transfer cipher/plain texts */
  1328. src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
  1329. dst = ((req->src == req->dst) ? src :
  1330. scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
  1331. if (use_dma) {
  1332. /* Update the Mode Register for DMA transfers. */
  1333. mr = atmel_aes_read(dd, AES_MR);
  1334. mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
  1335. mr |= AES_MR_SMOD_IDATAR0;
  1336. if (dd->caps.has_dualbuff)
  1337. mr |= AES_MR_DUALBUFF;
  1338. atmel_aes_write(dd, AES_MR, mr);
  1339. return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
  1340. atmel_aes_gcm_tag_init);
  1341. }
  1342. return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
  1343. atmel_aes_gcm_tag_init);
  1344. }
  1345. static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
  1346. {
  1347. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1348. struct aead_request *req = aead_request_cast(dd->areq);
  1349. u64 *data = dd->buf;
  1350. if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
  1351. if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
  1352. dd->resume = atmel_aes_gcm_tag_init;
  1353. atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
  1354. return -EINPROGRESS;
  1355. }
  1356. return atmel_aes_gcm_finalize(dd);
  1357. }
  1358. /* Read the GCM Intermediate Hash Word Registers. */
  1359. atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
  1360. data[0] = cpu_to_be64(req->assoclen * 8);
  1361. data[1] = cpu_to_be64(ctx->textlen * 8);
  1362. return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
  1363. ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
  1364. }
  1365. static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
  1366. {
  1367. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1368. unsigned long flags;
  1369. /*
  1370. * Change mode to CTR to complete the tag generation.
  1371. * Use J0 as Initialization Vector.
  1372. */
  1373. flags = dd->flags;
  1374. dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
  1375. dd->flags |= AES_FLAGS_CTR;
  1376. atmel_aes_write_ctrl(dd, false, ctx->j0);
  1377. dd->flags = flags;
  1378. atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
  1379. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
  1380. }
  1381. static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
  1382. {
  1383. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1384. struct aead_request *req = aead_request_cast(dd->areq);
  1385. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1386. bool enc = atmel_aes_is_encrypt(dd);
  1387. u32 offset, authsize, itag[4], *otag = ctx->tag;
  1388. int err;
  1389. /* Read the computed tag. */
  1390. if (likely(dd->flags & AES_FLAGS_GTAGEN))
  1391. atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
  1392. else
  1393. atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
  1394. offset = req->assoclen + ctx->textlen;
  1395. authsize = crypto_aead_authsize(tfm);
  1396. if (enc) {
  1397. scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
  1398. err = 0;
  1399. } else {
  1400. scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
  1401. err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
  1402. }
  1403. return atmel_aes_complete(dd, err);
  1404. }
  1405. static int atmel_aes_gcm_crypt(struct aead_request *req,
  1406. unsigned long mode)
  1407. {
  1408. struct atmel_aes_base_ctx *ctx;
  1409. struct atmel_aes_reqctx *rctx;
  1410. struct atmel_aes_dev *dd;
  1411. ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  1412. ctx->block_size = AES_BLOCK_SIZE;
  1413. dd = atmel_aes_find_dev(ctx);
  1414. if (!dd)
  1415. return -ENODEV;
  1416. rctx = aead_request_ctx(req);
  1417. rctx->mode = AES_FLAGS_GCM | mode;
  1418. return atmel_aes_handle_queue(dd, &req->base);
  1419. }
  1420. static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1421. unsigned int keylen)
  1422. {
  1423. struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
  1424. if (keylen != AES_KEYSIZE_256 &&
  1425. keylen != AES_KEYSIZE_192 &&
  1426. keylen != AES_KEYSIZE_128) {
  1427. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1428. return -EINVAL;
  1429. }
  1430. memcpy(ctx->key, key, keylen);
  1431. ctx->keylen = keylen;
  1432. return 0;
  1433. }
  1434. static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
  1435. unsigned int authsize)
  1436. {
  1437. /* Same as crypto_gcm_authsize() from crypto/gcm.c */
  1438. switch (authsize) {
  1439. case 4:
  1440. case 8:
  1441. case 12:
  1442. case 13:
  1443. case 14:
  1444. case 15:
  1445. case 16:
  1446. break;
  1447. default:
  1448. return -EINVAL;
  1449. }
  1450. return 0;
  1451. }
  1452. static int atmel_aes_gcm_encrypt(struct aead_request *req)
  1453. {
  1454. return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
  1455. }
  1456. static int atmel_aes_gcm_decrypt(struct aead_request *req)
  1457. {
  1458. return atmel_aes_gcm_crypt(req, 0);
  1459. }
  1460. static int atmel_aes_gcm_init(struct crypto_aead *tfm)
  1461. {
  1462. struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
  1463. crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
  1464. ctx->base.start = atmel_aes_gcm_start;
  1465. return 0;
  1466. }
  1467. static void atmel_aes_gcm_exit(struct crypto_aead *tfm)
  1468. {
  1469. }
  1470. static struct aead_alg aes_gcm_alg = {
  1471. .setkey = atmel_aes_gcm_setkey,
  1472. .setauthsize = atmel_aes_gcm_setauthsize,
  1473. .encrypt = atmel_aes_gcm_encrypt,
  1474. .decrypt = atmel_aes_gcm_decrypt,
  1475. .init = atmel_aes_gcm_init,
  1476. .exit = atmel_aes_gcm_exit,
  1477. .ivsize = 12,
  1478. .maxauthsize = AES_BLOCK_SIZE,
  1479. .base = {
  1480. .cra_name = "gcm(aes)",
  1481. .cra_driver_name = "atmel-gcm-aes",
  1482. .cra_priority = ATMEL_AES_PRIORITY,
  1483. .cra_flags = CRYPTO_ALG_ASYNC,
  1484. .cra_blocksize = 1,
  1485. .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
  1486. .cra_alignmask = 0xf,
  1487. .cra_module = THIS_MODULE,
  1488. },
  1489. };
  1490. /* xts functions */
  1491. static inline struct atmel_aes_xts_ctx *
  1492. atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx)
  1493. {
  1494. return container_of(ctx, struct atmel_aes_xts_ctx, base);
  1495. }
  1496. static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd);
  1497. static int atmel_aes_xts_start(struct atmel_aes_dev *dd)
  1498. {
  1499. struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx);
  1500. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  1501. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  1502. unsigned long flags;
  1503. int err;
  1504. atmel_aes_set_mode(dd, rctx);
  1505. err = atmel_aes_hw_init(dd);
  1506. if (err)
  1507. return atmel_aes_complete(dd, err);
  1508. /* Compute the tweak value from req->info with ecb(aes). */
  1509. flags = dd->flags;
  1510. dd->flags &= ~AES_FLAGS_MODE_MASK;
  1511. dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
  1512. atmel_aes_write_ctrl_key(dd, false, NULL,
  1513. ctx->key2, ctx->base.keylen);
  1514. dd->flags = flags;
  1515. atmel_aes_write_block(dd, AES_IDATAR(0), req->info);
  1516. return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data);
  1517. }
  1518. static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd)
  1519. {
  1520. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  1521. bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD);
  1522. u32 tweak[AES_BLOCK_SIZE / sizeof(u32)];
  1523. static const u32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), };
  1524. u8 *tweak_bytes = (u8 *)tweak;
  1525. int i;
  1526. /* Read the computed ciphered tweak value. */
  1527. atmel_aes_read_block(dd, AES_ODATAR(0), tweak);
  1528. /*
  1529. * Hardware quirk:
  1530. * the order of the ciphered tweak bytes need to be reversed before
  1531. * writing them into the ODATARx registers.
  1532. */
  1533. for (i = 0; i < AES_BLOCK_SIZE/2; ++i) {
  1534. u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i];
  1535. tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i];
  1536. tweak_bytes[i] = tmp;
  1537. }
  1538. /* Process the data. */
  1539. atmel_aes_write_ctrl(dd, use_dma, NULL);
  1540. atmel_aes_write_block(dd, AES_TWR(0), tweak);
  1541. atmel_aes_write_block(dd, AES_ALPHAR(0), one);
  1542. if (use_dma)
  1543. return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
  1544. atmel_aes_transfer_complete);
  1545. return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
  1546. atmel_aes_transfer_complete);
  1547. }
  1548. static int atmel_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1549. unsigned int keylen)
  1550. {
  1551. struct atmel_aes_xts_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  1552. int err;
  1553. err = xts_check_key(crypto_ablkcipher_tfm(tfm), key, keylen);
  1554. if (err)
  1555. return err;
  1556. memcpy(ctx->base.key, key, keylen/2);
  1557. memcpy(ctx->key2, key + keylen/2, keylen/2);
  1558. ctx->base.keylen = keylen/2;
  1559. return 0;
  1560. }
  1561. static int atmel_aes_xts_encrypt(struct ablkcipher_request *req)
  1562. {
  1563. return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT);
  1564. }
  1565. static int atmel_aes_xts_decrypt(struct ablkcipher_request *req)
  1566. {
  1567. return atmel_aes_crypt(req, AES_FLAGS_XTS);
  1568. }
  1569. static int atmel_aes_xts_cra_init(struct crypto_tfm *tfm)
  1570. {
  1571. struct atmel_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  1572. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  1573. ctx->base.start = atmel_aes_xts_start;
  1574. return 0;
  1575. }
  1576. static struct crypto_alg aes_xts_alg = {
  1577. .cra_name = "xts(aes)",
  1578. .cra_driver_name = "atmel-xts-aes",
  1579. .cra_priority = ATMEL_AES_PRIORITY,
  1580. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1581. .cra_blocksize = AES_BLOCK_SIZE,
  1582. .cra_ctxsize = sizeof(struct atmel_aes_xts_ctx),
  1583. .cra_alignmask = 0xf,
  1584. .cra_type = &crypto_ablkcipher_type,
  1585. .cra_module = THIS_MODULE,
  1586. .cra_init = atmel_aes_xts_cra_init,
  1587. .cra_exit = atmel_aes_cra_exit,
  1588. .cra_u.ablkcipher = {
  1589. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1590. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1591. .ivsize = AES_BLOCK_SIZE,
  1592. .setkey = atmel_aes_xts_setkey,
  1593. .encrypt = atmel_aes_xts_encrypt,
  1594. .decrypt = atmel_aes_xts_decrypt,
  1595. }
  1596. };
  1597. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  1598. /* authenc aead functions */
  1599. static int atmel_aes_authenc_start(struct atmel_aes_dev *dd);
  1600. static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
  1601. bool is_async);
  1602. static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
  1603. bool is_async);
  1604. static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd);
  1605. static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
  1606. bool is_async);
  1607. static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err)
  1608. {
  1609. struct aead_request *req = aead_request_cast(dd->areq);
  1610. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1611. if (err && (dd->flags & AES_FLAGS_OWN_SHA))
  1612. atmel_sha_authenc_abort(&rctx->auth_req);
  1613. dd->flags &= ~AES_FLAGS_OWN_SHA;
  1614. }
  1615. static int atmel_aes_authenc_start(struct atmel_aes_dev *dd)
  1616. {
  1617. struct aead_request *req = aead_request_cast(dd->areq);
  1618. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1619. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1620. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1621. int err;
  1622. atmel_aes_set_mode(dd, &rctx->base);
  1623. err = atmel_aes_hw_init(dd);
  1624. if (err)
  1625. return atmel_aes_complete(dd, err);
  1626. return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth,
  1627. atmel_aes_authenc_init, dd);
  1628. }
  1629. static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
  1630. bool is_async)
  1631. {
  1632. struct aead_request *req = aead_request_cast(dd->areq);
  1633. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1634. if (is_async)
  1635. dd->is_async = true;
  1636. if (err)
  1637. return atmel_aes_complete(dd, err);
  1638. /* If here, we've got the ownership of the SHA device. */
  1639. dd->flags |= AES_FLAGS_OWN_SHA;
  1640. /* Configure the SHA device. */
  1641. return atmel_sha_authenc_init(&rctx->auth_req,
  1642. req->src, req->assoclen,
  1643. rctx->textlen,
  1644. atmel_aes_authenc_transfer, dd);
  1645. }
  1646. static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
  1647. bool is_async)
  1648. {
  1649. struct aead_request *req = aead_request_cast(dd->areq);
  1650. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1651. bool enc = atmel_aes_is_encrypt(dd);
  1652. struct scatterlist *src, *dst;
  1653. u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
  1654. u32 emr;
  1655. if (is_async)
  1656. dd->is_async = true;
  1657. if (err)
  1658. return atmel_aes_complete(dd, err);
  1659. /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
  1660. src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
  1661. dst = src;
  1662. if (req->src != req->dst)
  1663. dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
  1664. /* Configure the AES device. */
  1665. memcpy(iv, req->iv, sizeof(iv));
  1666. /*
  1667. * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
  1668. * 'true' even if the data transfer is actually performed by the CPU (so
  1669. * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
  1670. * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
  1671. * must be set to *_MR_SMOD_IDATAR0.
  1672. */
  1673. atmel_aes_write_ctrl(dd, true, iv);
  1674. emr = AES_EMR_PLIPEN;
  1675. if (!enc)
  1676. emr |= AES_EMR_PLIPD;
  1677. atmel_aes_write(dd, AES_EMR, emr);
  1678. /* Transfer data. */
  1679. return atmel_aes_dma_start(dd, src, dst, rctx->textlen,
  1680. atmel_aes_authenc_digest);
  1681. }
  1682. static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd)
  1683. {
  1684. struct aead_request *req = aead_request_cast(dd->areq);
  1685. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1686. /* atmel_sha_authenc_final() releases the SHA device. */
  1687. dd->flags &= ~AES_FLAGS_OWN_SHA;
  1688. return atmel_sha_authenc_final(&rctx->auth_req,
  1689. rctx->digest, sizeof(rctx->digest),
  1690. atmel_aes_authenc_final, dd);
  1691. }
  1692. static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
  1693. bool is_async)
  1694. {
  1695. struct aead_request *req = aead_request_cast(dd->areq);
  1696. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1697. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1698. bool enc = atmel_aes_is_encrypt(dd);
  1699. u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest;
  1700. u32 offs, authsize;
  1701. if (is_async)
  1702. dd->is_async = true;
  1703. if (err)
  1704. goto complete;
  1705. offs = req->assoclen + rctx->textlen;
  1706. authsize = crypto_aead_authsize(tfm);
  1707. if (enc) {
  1708. scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1);
  1709. } else {
  1710. scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0);
  1711. if (crypto_memneq(idigest, odigest, authsize))
  1712. err = -EBADMSG;
  1713. }
  1714. complete:
  1715. return atmel_aes_complete(dd, err);
  1716. }
  1717. static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key,
  1718. unsigned int keylen)
  1719. {
  1720. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1721. struct crypto_authenc_keys keys;
  1722. u32 flags;
  1723. int err;
  1724. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  1725. goto badkey;
  1726. if (keys.enckeylen > sizeof(ctx->base.key))
  1727. goto badkey;
  1728. /* Save auth key. */
  1729. flags = crypto_aead_get_flags(tfm);
  1730. err = atmel_sha_authenc_setkey(ctx->auth,
  1731. keys.authkey, keys.authkeylen,
  1732. &flags);
  1733. crypto_aead_set_flags(tfm, flags & CRYPTO_TFM_RES_MASK);
  1734. if (err) {
  1735. memzero_explicit(&keys, sizeof(keys));
  1736. return err;
  1737. }
  1738. /* Save enc key. */
  1739. ctx->base.keylen = keys.enckeylen;
  1740. memcpy(ctx->base.key, keys.enckey, keys.enckeylen);
  1741. memzero_explicit(&keys, sizeof(keys));
  1742. return 0;
  1743. badkey:
  1744. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1745. memzero_explicit(&keys, sizeof(keys));
  1746. return -EINVAL;
  1747. }
  1748. static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm,
  1749. unsigned long auth_mode)
  1750. {
  1751. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1752. unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize();
  1753. ctx->auth = atmel_sha_authenc_spawn(auth_mode);
  1754. if (IS_ERR(ctx->auth))
  1755. return PTR_ERR(ctx->auth);
  1756. crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) +
  1757. auth_reqsize));
  1758. ctx->base.start = atmel_aes_authenc_start;
  1759. return 0;
  1760. }
  1761. static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm)
  1762. {
  1763. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1);
  1764. }
  1765. static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm)
  1766. {
  1767. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224);
  1768. }
  1769. static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm)
  1770. {
  1771. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256);
  1772. }
  1773. static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm)
  1774. {
  1775. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384);
  1776. }
  1777. static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm)
  1778. {
  1779. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512);
  1780. }
  1781. static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm)
  1782. {
  1783. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1784. atmel_sha_authenc_free(ctx->auth);
  1785. }
  1786. static int atmel_aes_authenc_crypt(struct aead_request *req,
  1787. unsigned long mode)
  1788. {
  1789. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1790. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1791. struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
  1792. u32 authsize = crypto_aead_authsize(tfm);
  1793. bool enc = (mode & AES_FLAGS_ENCRYPT);
  1794. struct atmel_aes_dev *dd;
  1795. /* Compute text length. */
  1796. if (!enc && req->cryptlen < authsize)
  1797. return -EINVAL;
  1798. rctx->textlen = req->cryptlen - (enc ? 0 : authsize);
  1799. /*
  1800. * Currently, empty messages are not supported yet:
  1801. * the SHA auto-padding can be used only on non-empty messages.
  1802. * Hence a special case needs to be implemented for empty message.
  1803. */
  1804. if (!rctx->textlen && !req->assoclen)
  1805. return -EINVAL;
  1806. rctx->base.mode = mode;
  1807. ctx->block_size = AES_BLOCK_SIZE;
  1808. dd = atmel_aes_find_dev(ctx);
  1809. if (!dd)
  1810. return -ENODEV;
  1811. return atmel_aes_handle_queue(dd, &req->base);
  1812. }
  1813. static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req)
  1814. {
  1815. return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
  1816. }
  1817. static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req)
  1818. {
  1819. return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC);
  1820. }
  1821. static struct aead_alg aes_authenc_algs[] = {
  1822. {
  1823. .setkey = atmel_aes_authenc_setkey,
  1824. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1825. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1826. .init = atmel_aes_authenc_hmac_sha1_init_tfm,
  1827. .exit = atmel_aes_authenc_exit_tfm,
  1828. .ivsize = AES_BLOCK_SIZE,
  1829. .maxauthsize = SHA1_DIGEST_SIZE,
  1830. .base = {
  1831. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1832. .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes",
  1833. .cra_priority = ATMEL_AES_PRIORITY,
  1834. .cra_flags = CRYPTO_ALG_ASYNC,
  1835. .cra_blocksize = AES_BLOCK_SIZE,
  1836. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1837. .cra_alignmask = 0xf,
  1838. .cra_module = THIS_MODULE,
  1839. },
  1840. },
  1841. {
  1842. .setkey = atmel_aes_authenc_setkey,
  1843. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1844. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1845. .init = atmel_aes_authenc_hmac_sha224_init_tfm,
  1846. .exit = atmel_aes_authenc_exit_tfm,
  1847. .ivsize = AES_BLOCK_SIZE,
  1848. .maxauthsize = SHA224_DIGEST_SIZE,
  1849. .base = {
  1850. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  1851. .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes",
  1852. .cra_priority = ATMEL_AES_PRIORITY,
  1853. .cra_flags = CRYPTO_ALG_ASYNC,
  1854. .cra_blocksize = AES_BLOCK_SIZE,
  1855. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1856. .cra_alignmask = 0xf,
  1857. .cra_module = THIS_MODULE,
  1858. },
  1859. },
  1860. {
  1861. .setkey = atmel_aes_authenc_setkey,
  1862. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1863. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1864. .init = atmel_aes_authenc_hmac_sha256_init_tfm,
  1865. .exit = atmel_aes_authenc_exit_tfm,
  1866. .ivsize = AES_BLOCK_SIZE,
  1867. .maxauthsize = SHA256_DIGEST_SIZE,
  1868. .base = {
  1869. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1870. .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes",
  1871. .cra_priority = ATMEL_AES_PRIORITY,
  1872. .cra_flags = CRYPTO_ALG_ASYNC,
  1873. .cra_blocksize = AES_BLOCK_SIZE,
  1874. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1875. .cra_alignmask = 0xf,
  1876. .cra_module = THIS_MODULE,
  1877. },
  1878. },
  1879. {
  1880. .setkey = atmel_aes_authenc_setkey,
  1881. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1882. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1883. .init = atmel_aes_authenc_hmac_sha384_init_tfm,
  1884. .exit = atmel_aes_authenc_exit_tfm,
  1885. .ivsize = AES_BLOCK_SIZE,
  1886. .maxauthsize = SHA384_DIGEST_SIZE,
  1887. .base = {
  1888. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  1889. .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes",
  1890. .cra_priority = ATMEL_AES_PRIORITY,
  1891. .cra_flags = CRYPTO_ALG_ASYNC,
  1892. .cra_blocksize = AES_BLOCK_SIZE,
  1893. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1894. .cra_alignmask = 0xf,
  1895. .cra_module = THIS_MODULE,
  1896. },
  1897. },
  1898. {
  1899. .setkey = atmel_aes_authenc_setkey,
  1900. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1901. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1902. .init = atmel_aes_authenc_hmac_sha512_init_tfm,
  1903. .exit = atmel_aes_authenc_exit_tfm,
  1904. .ivsize = AES_BLOCK_SIZE,
  1905. .maxauthsize = SHA512_DIGEST_SIZE,
  1906. .base = {
  1907. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  1908. .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes",
  1909. .cra_priority = ATMEL_AES_PRIORITY,
  1910. .cra_flags = CRYPTO_ALG_ASYNC,
  1911. .cra_blocksize = AES_BLOCK_SIZE,
  1912. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1913. .cra_alignmask = 0xf,
  1914. .cra_module = THIS_MODULE,
  1915. },
  1916. },
  1917. };
  1918. #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
  1919. /* Probe functions */
  1920. static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
  1921. {
  1922. dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
  1923. dd->buflen = ATMEL_AES_BUFFER_SIZE;
  1924. dd->buflen &= ~(AES_BLOCK_SIZE - 1);
  1925. if (!dd->buf) {
  1926. dev_err(dd->dev, "unable to alloc pages.\n");
  1927. return -ENOMEM;
  1928. }
  1929. return 0;
  1930. }
  1931. static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
  1932. {
  1933. free_page((unsigned long)dd->buf);
  1934. }
  1935. static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
  1936. {
  1937. struct at_dma_slave *sl = slave;
  1938. if (sl && sl->dma_dev == chan->device->dev) {
  1939. chan->private = sl;
  1940. return true;
  1941. } else {
  1942. return false;
  1943. }
  1944. }
  1945. static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
  1946. struct crypto_platform_data *pdata)
  1947. {
  1948. struct at_dma_slave *slave;
  1949. int err = -ENOMEM;
  1950. dma_cap_mask_t mask;
  1951. dma_cap_zero(mask);
  1952. dma_cap_set(DMA_SLAVE, mask);
  1953. /* Try to grab 2 DMA channels */
  1954. slave = &pdata->dma_slave->rxdata;
  1955. dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
  1956. slave, dd->dev, "tx");
  1957. if (!dd->src.chan)
  1958. goto err_dma_in;
  1959. slave = &pdata->dma_slave->txdata;
  1960. dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
  1961. slave, dd->dev, "rx");
  1962. if (!dd->dst.chan)
  1963. goto err_dma_out;
  1964. return 0;
  1965. err_dma_out:
  1966. dma_release_channel(dd->src.chan);
  1967. err_dma_in:
  1968. dev_warn(dd->dev, "no DMA channel available\n");
  1969. return err;
  1970. }
  1971. static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
  1972. {
  1973. dma_release_channel(dd->dst.chan);
  1974. dma_release_channel(dd->src.chan);
  1975. }
  1976. static void atmel_aes_queue_task(unsigned long data)
  1977. {
  1978. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  1979. atmel_aes_handle_queue(dd, NULL);
  1980. }
  1981. static void atmel_aes_done_task(unsigned long data)
  1982. {
  1983. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  1984. dd->is_async = true;
  1985. (void)dd->resume(dd);
  1986. }
  1987. static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
  1988. {
  1989. struct atmel_aes_dev *aes_dd = dev_id;
  1990. u32 reg;
  1991. reg = atmel_aes_read(aes_dd, AES_ISR);
  1992. if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
  1993. atmel_aes_write(aes_dd, AES_IDR, reg);
  1994. if (AES_FLAGS_BUSY & aes_dd->flags)
  1995. tasklet_schedule(&aes_dd->done_task);
  1996. else
  1997. dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
  1998. return IRQ_HANDLED;
  1999. }
  2000. return IRQ_NONE;
  2001. }
  2002. static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
  2003. {
  2004. int i;
  2005. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  2006. if (dd->caps.has_authenc)
  2007. for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++)
  2008. crypto_unregister_aead(&aes_authenc_algs[i]);
  2009. #endif
  2010. if (dd->caps.has_xts)
  2011. crypto_unregister_alg(&aes_xts_alg);
  2012. if (dd->caps.has_gcm)
  2013. crypto_unregister_aead(&aes_gcm_alg);
  2014. if (dd->caps.has_cfb64)
  2015. crypto_unregister_alg(&aes_cfb64_alg);
  2016. for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
  2017. crypto_unregister_alg(&aes_algs[i]);
  2018. }
  2019. static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
  2020. {
  2021. int err, i, j;
  2022. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  2023. err = crypto_register_alg(&aes_algs[i]);
  2024. if (err)
  2025. goto err_aes_algs;
  2026. }
  2027. if (dd->caps.has_cfb64) {
  2028. err = crypto_register_alg(&aes_cfb64_alg);
  2029. if (err)
  2030. goto err_aes_cfb64_alg;
  2031. }
  2032. if (dd->caps.has_gcm) {
  2033. err = crypto_register_aead(&aes_gcm_alg);
  2034. if (err)
  2035. goto err_aes_gcm_alg;
  2036. }
  2037. if (dd->caps.has_xts) {
  2038. err = crypto_register_alg(&aes_xts_alg);
  2039. if (err)
  2040. goto err_aes_xts_alg;
  2041. }
  2042. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  2043. if (dd->caps.has_authenc) {
  2044. for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) {
  2045. err = crypto_register_aead(&aes_authenc_algs[i]);
  2046. if (err)
  2047. goto err_aes_authenc_alg;
  2048. }
  2049. }
  2050. #endif
  2051. return 0;
  2052. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  2053. /* i = ARRAY_SIZE(aes_authenc_algs); */
  2054. err_aes_authenc_alg:
  2055. for (j = 0; j < i; j++)
  2056. crypto_unregister_aead(&aes_authenc_algs[j]);
  2057. crypto_unregister_alg(&aes_xts_alg);
  2058. #endif
  2059. err_aes_xts_alg:
  2060. crypto_unregister_aead(&aes_gcm_alg);
  2061. err_aes_gcm_alg:
  2062. crypto_unregister_alg(&aes_cfb64_alg);
  2063. err_aes_cfb64_alg:
  2064. i = ARRAY_SIZE(aes_algs);
  2065. err_aes_algs:
  2066. for (j = 0; j < i; j++)
  2067. crypto_unregister_alg(&aes_algs[j]);
  2068. return err;
  2069. }
  2070. static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
  2071. {
  2072. dd->caps.has_dualbuff = 0;
  2073. dd->caps.has_cfb64 = 0;
  2074. dd->caps.has_gcm = 0;
  2075. dd->caps.has_xts = 0;
  2076. dd->caps.has_authenc = 0;
  2077. dd->caps.max_burst_size = 1;
  2078. /* keep only major version number */
  2079. switch (dd->hw_version & 0xff0) {
  2080. case 0x500:
  2081. dd->caps.has_dualbuff = 1;
  2082. dd->caps.has_cfb64 = 1;
  2083. dd->caps.has_gcm = 1;
  2084. dd->caps.has_xts = 1;
  2085. dd->caps.has_authenc = 1;
  2086. dd->caps.max_burst_size = 4;
  2087. break;
  2088. case 0x200:
  2089. dd->caps.has_dualbuff = 1;
  2090. dd->caps.has_cfb64 = 1;
  2091. dd->caps.has_gcm = 1;
  2092. dd->caps.max_burst_size = 4;
  2093. break;
  2094. case 0x130:
  2095. dd->caps.has_dualbuff = 1;
  2096. dd->caps.has_cfb64 = 1;
  2097. dd->caps.max_burst_size = 4;
  2098. break;
  2099. case 0x120:
  2100. break;
  2101. default:
  2102. dev_warn(dd->dev,
  2103. "Unmanaged aes version, set minimum capabilities\n");
  2104. break;
  2105. }
  2106. }
  2107. #if defined(CONFIG_OF)
  2108. static const struct of_device_id atmel_aes_dt_ids[] = {
  2109. { .compatible = "atmel,at91sam9g46-aes" },
  2110. { /* sentinel */ }
  2111. };
  2112. MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
  2113. static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
  2114. {
  2115. struct device_node *np = pdev->dev.of_node;
  2116. struct crypto_platform_data *pdata;
  2117. if (!np) {
  2118. dev_err(&pdev->dev, "device node not found\n");
  2119. return ERR_PTR(-EINVAL);
  2120. }
  2121. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  2122. if (!pdata) {
  2123. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  2124. return ERR_PTR(-ENOMEM);
  2125. }
  2126. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  2127. sizeof(*(pdata->dma_slave)),
  2128. GFP_KERNEL);
  2129. if (!pdata->dma_slave) {
  2130. dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
  2131. devm_kfree(&pdev->dev, pdata);
  2132. return ERR_PTR(-ENOMEM);
  2133. }
  2134. return pdata;
  2135. }
  2136. #else
  2137. static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
  2138. {
  2139. return ERR_PTR(-EINVAL);
  2140. }
  2141. #endif
  2142. static int atmel_aes_probe(struct platform_device *pdev)
  2143. {
  2144. struct atmel_aes_dev *aes_dd;
  2145. struct crypto_platform_data *pdata;
  2146. struct device *dev = &pdev->dev;
  2147. struct resource *aes_res;
  2148. int err;
  2149. pdata = pdev->dev.platform_data;
  2150. if (!pdata) {
  2151. pdata = atmel_aes_of_init(pdev);
  2152. if (IS_ERR(pdata)) {
  2153. err = PTR_ERR(pdata);
  2154. goto aes_dd_err;
  2155. }
  2156. }
  2157. if (!pdata->dma_slave) {
  2158. err = -ENXIO;
  2159. goto aes_dd_err;
  2160. }
  2161. aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
  2162. if (aes_dd == NULL) {
  2163. dev_err(dev, "unable to alloc data struct.\n");
  2164. err = -ENOMEM;
  2165. goto aes_dd_err;
  2166. }
  2167. aes_dd->dev = dev;
  2168. platform_set_drvdata(pdev, aes_dd);
  2169. INIT_LIST_HEAD(&aes_dd->list);
  2170. spin_lock_init(&aes_dd->lock);
  2171. tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
  2172. (unsigned long)aes_dd);
  2173. tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
  2174. (unsigned long)aes_dd);
  2175. crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
  2176. aes_dd->irq = -1;
  2177. /* Get the base address */
  2178. aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2179. if (!aes_res) {
  2180. dev_err(dev, "no MEM resource info\n");
  2181. err = -ENODEV;
  2182. goto res_err;
  2183. }
  2184. aes_dd->phys_base = aes_res->start;
  2185. /* Get the IRQ */
  2186. aes_dd->irq = platform_get_irq(pdev, 0);
  2187. if (aes_dd->irq < 0) {
  2188. dev_err(dev, "no IRQ resource info\n");
  2189. err = aes_dd->irq;
  2190. goto res_err;
  2191. }
  2192. err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
  2193. IRQF_SHARED, "atmel-aes", aes_dd);
  2194. if (err) {
  2195. dev_err(dev, "unable to request aes irq.\n");
  2196. goto res_err;
  2197. }
  2198. /* Initializing the clock */
  2199. aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
  2200. if (IS_ERR(aes_dd->iclk)) {
  2201. dev_err(dev, "clock initialization failed.\n");
  2202. err = PTR_ERR(aes_dd->iclk);
  2203. goto res_err;
  2204. }
  2205. aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
  2206. if (IS_ERR(aes_dd->io_base)) {
  2207. dev_err(dev, "can't ioremap\n");
  2208. err = PTR_ERR(aes_dd->io_base);
  2209. goto res_err;
  2210. }
  2211. err = clk_prepare(aes_dd->iclk);
  2212. if (err)
  2213. goto res_err;
  2214. err = atmel_aes_hw_version_init(aes_dd);
  2215. if (err)
  2216. goto iclk_unprepare;
  2217. atmel_aes_get_cap(aes_dd);
  2218. #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
  2219. if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) {
  2220. err = -EPROBE_DEFER;
  2221. goto iclk_unprepare;
  2222. }
  2223. #endif
  2224. err = atmel_aes_buff_init(aes_dd);
  2225. if (err)
  2226. goto err_aes_buff;
  2227. err = atmel_aes_dma_init(aes_dd, pdata);
  2228. if (err)
  2229. goto err_aes_dma;
  2230. spin_lock(&atmel_aes.lock);
  2231. list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
  2232. spin_unlock(&atmel_aes.lock);
  2233. err = atmel_aes_register_algs(aes_dd);
  2234. if (err)
  2235. goto err_algs;
  2236. dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
  2237. dma_chan_name(aes_dd->src.chan),
  2238. dma_chan_name(aes_dd->dst.chan));
  2239. return 0;
  2240. err_algs:
  2241. spin_lock(&atmel_aes.lock);
  2242. list_del(&aes_dd->list);
  2243. spin_unlock(&atmel_aes.lock);
  2244. atmel_aes_dma_cleanup(aes_dd);
  2245. err_aes_dma:
  2246. atmel_aes_buff_cleanup(aes_dd);
  2247. err_aes_buff:
  2248. iclk_unprepare:
  2249. clk_unprepare(aes_dd->iclk);
  2250. res_err:
  2251. tasklet_kill(&aes_dd->done_task);
  2252. tasklet_kill(&aes_dd->queue_task);
  2253. aes_dd_err:
  2254. if (err != -EPROBE_DEFER)
  2255. dev_err(dev, "initialization failed.\n");
  2256. return err;
  2257. }
  2258. static int atmel_aes_remove(struct platform_device *pdev)
  2259. {
  2260. struct atmel_aes_dev *aes_dd;
  2261. aes_dd = platform_get_drvdata(pdev);
  2262. if (!aes_dd)
  2263. return -ENODEV;
  2264. spin_lock(&atmel_aes.lock);
  2265. list_del(&aes_dd->list);
  2266. spin_unlock(&atmel_aes.lock);
  2267. atmel_aes_unregister_algs(aes_dd);
  2268. tasklet_kill(&aes_dd->done_task);
  2269. tasklet_kill(&aes_dd->queue_task);
  2270. atmel_aes_dma_cleanup(aes_dd);
  2271. atmel_aes_buff_cleanup(aes_dd);
  2272. clk_unprepare(aes_dd->iclk);
  2273. return 0;
  2274. }
  2275. static struct platform_driver atmel_aes_driver = {
  2276. .probe = atmel_aes_probe,
  2277. .remove = atmel_aes_remove,
  2278. .driver = {
  2279. .name = "atmel_aes",
  2280. .of_match_table = of_match_ptr(atmel_aes_dt_ids),
  2281. },
  2282. };
  2283. module_platform_driver(atmel_aes_driver);
  2284. MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
  2285. MODULE_LICENSE("GPL v2");
  2286. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");