atmel-aes.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177
  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/internal/aead.h>
  37. #include <linux/platform_data/crypto-atmel.h>
  38. #include <dt-bindings/dma/at91.h>
  39. #include "atmel-aes-regs.h"
  40. #define ATMEL_AES_PRIORITY 300
  41. #define ATMEL_AES_BUFFER_ORDER 2
  42. #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
  43. #define CFB8_BLOCK_SIZE 1
  44. #define CFB16_BLOCK_SIZE 2
  45. #define CFB32_BLOCK_SIZE 4
  46. #define CFB64_BLOCK_SIZE 8
  47. #define SIZE_IN_WORDS(x) ((x) >> 2)
  48. /* AES flags */
  49. /* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
  50. #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
  51. #define AES_FLAGS_GTAGEN AES_MR_GTAGEN
  52. #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
  53. #define AES_FLAGS_ECB AES_MR_OPMOD_ECB
  54. #define AES_FLAGS_CBC AES_MR_OPMOD_CBC
  55. #define AES_FLAGS_OFB AES_MR_OPMOD_OFB
  56. #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
  57. #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
  58. #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
  59. #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
  60. #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
  61. #define AES_FLAGS_CTR AES_MR_OPMOD_CTR
  62. #define AES_FLAGS_GCM AES_MR_OPMOD_GCM
  63. #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
  64. AES_FLAGS_ENCRYPT | \
  65. AES_FLAGS_GTAGEN)
  66. #define AES_FLAGS_INIT BIT(2)
  67. #define AES_FLAGS_BUSY BIT(3)
  68. #define AES_FLAGS_DUMP_REG BIT(4)
  69. #define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY)
  70. #define ATMEL_AES_QUEUE_LENGTH 50
  71. #define ATMEL_AES_DMA_THRESHOLD 256
  72. struct atmel_aes_caps {
  73. bool has_dualbuff;
  74. bool has_cfb64;
  75. bool has_ctr32;
  76. bool has_gcm;
  77. u32 max_burst_size;
  78. };
  79. struct atmel_aes_dev;
  80. typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
  81. struct atmel_aes_base_ctx {
  82. struct atmel_aes_dev *dd;
  83. atmel_aes_fn_t start;
  84. int keylen;
  85. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  86. u16 block_size;
  87. };
  88. struct atmel_aes_ctx {
  89. struct atmel_aes_base_ctx base;
  90. };
  91. struct atmel_aes_ctr_ctx {
  92. struct atmel_aes_base_ctx base;
  93. u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
  94. size_t offset;
  95. struct scatterlist src[2];
  96. struct scatterlist dst[2];
  97. };
  98. struct atmel_aes_gcm_ctx {
  99. struct atmel_aes_base_ctx base;
  100. struct scatterlist src[2];
  101. struct scatterlist dst[2];
  102. u32 j0[AES_BLOCK_SIZE / sizeof(u32)];
  103. u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
  104. u32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
  105. size_t textlen;
  106. const u32 *ghash_in;
  107. u32 *ghash_out;
  108. atmel_aes_fn_t ghash_resume;
  109. };
  110. struct atmel_aes_reqctx {
  111. unsigned long mode;
  112. };
  113. struct atmel_aes_dma {
  114. struct dma_chan *chan;
  115. struct scatterlist *sg;
  116. int nents;
  117. unsigned int remainder;
  118. unsigned int sg_len;
  119. };
  120. struct atmel_aes_dev {
  121. struct list_head list;
  122. unsigned long phys_base;
  123. void __iomem *io_base;
  124. struct crypto_async_request *areq;
  125. struct atmel_aes_base_ctx *ctx;
  126. bool is_async;
  127. atmel_aes_fn_t resume;
  128. atmel_aes_fn_t cpu_transfer_complete;
  129. struct device *dev;
  130. struct clk *iclk;
  131. int irq;
  132. unsigned long flags;
  133. spinlock_t lock;
  134. struct crypto_queue queue;
  135. struct tasklet_struct done_task;
  136. struct tasklet_struct queue_task;
  137. size_t total;
  138. size_t datalen;
  139. u32 *data;
  140. struct atmel_aes_dma src;
  141. struct atmel_aes_dma dst;
  142. size_t buflen;
  143. void *buf;
  144. struct scatterlist aligned_sg;
  145. struct scatterlist *real_dst;
  146. struct atmel_aes_caps caps;
  147. u32 hw_version;
  148. };
  149. struct atmel_aes_drv {
  150. struct list_head dev_list;
  151. spinlock_t lock;
  152. };
  153. static struct atmel_aes_drv atmel_aes = {
  154. .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
  155. .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
  156. };
  157. #ifdef VERBOSE_DEBUG
  158. static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
  159. {
  160. switch (offset) {
  161. case AES_CR:
  162. return "CR";
  163. case AES_MR:
  164. return "MR";
  165. case AES_ISR:
  166. return "ISR";
  167. case AES_IMR:
  168. return "IMR";
  169. case AES_IER:
  170. return "IER";
  171. case AES_IDR:
  172. return "IDR";
  173. case AES_KEYWR(0):
  174. case AES_KEYWR(1):
  175. case AES_KEYWR(2):
  176. case AES_KEYWR(3):
  177. case AES_KEYWR(4):
  178. case AES_KEYWR(5):
  179. case AES_KEYWR(6):
  180. case AES_KEYWR(7):
  181. snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
  182. break;
  183. case AES_IDATAR(0):
  184. case AES_IDATAR(1):
  185. case AES_IDATAR(2):
  186. case AES_IDATAR(3):
  187. snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
  188. break;
  189. case AES_ODATAR(0):
  190. case AES_ODATAR(1):
  191. case AES_ODATAR(2):
  192. case AES_ODATAR(3):
  193. snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
  194. break;
  195. case AES_IVR(0):
  196. case AES_IVR(1):
  197. case AES_IVR(2):
  198. case AES_IVR(3):
  199. snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
  200. break;
  201. case AES_AADLENR:
  202. return "AADLENR";
  203. case AES_CLENR:
  204. return "CLENR";
  205. case AES_GHASHR(0):
  206. case AES_GHASHR(1):
  207. case AES_GHASHR(2):
  208. case AES_GHASHR(3):
  209. snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
  210. break;
  211. case AES_TAGR(0):
  212. case AES_TAGR(1):
  213. case AES_TAGR(2):
  214. case AES_TAGR(3):
  215. snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
  216. break;
  217. case AES_CTRR:
  218. return "CTRR";
  219. case AES_GCMHR(0):
  220. case AES_GCMHR(1):
  221. case AES_GCMHR(2):
  222. case AES_GCMHR(3):
  223. snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
  224. break;
  225. default:
  226. snprintf(tmp, sz, "0x%02x", offset);
  227. break;
  228. }
  229. return tmp;
  230. }
  231. #endif /* VERBOSE_DEBUG */
  232. /* Shared functions */
  233. static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
  234. {
  235. u32 value = readl_relaxed(dd->io_base + offset);
  236. #ifdef VERBOSE_DEBUG
  237. if (dd->flags & AES_FLAGS_DUMP_REG) {
  238. char tmp[16];
  239. dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
  240. atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
  241. }
  242. #endif /* VERBOSE_DEBUG */
  243. return value;
  244. }
  245. static inline void atmel_aes_write(struct atmel_aes_dev *dd,
  246. u32 offset, u32 value)
  247. {
  248. #ifdef VERBOSE_DEBUG
  249. if (dd->flags & AES_FLAGS_DUMP_REG) {
  250. char tmp[16];
  251. dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
  252. atmel_aes_reg_name(offset, tmp));
  253. }
  254. #endif /* VERBOSE_DEBUG */
  255. writel_relaxed(value, dd->io_base + offset);
  256. }
  257. static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
  258. u32 *value, int count)
  259. {
  260. for (; count--; value++, offset += 4)
  261. *value = atmel_aes_read(dd, offset);
  262. }
  263. static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
  264. const u32 *value, int count)
  265. {
  266. for (; count--; value++, offset += 4)
  267. atmel_aes_write(dd, offset, *value);
  268. }
  269. static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
  270. u32 *value)
  271. {
  272. atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
  273. }
  274. static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
  275. const u32 *value)
  276. {
  277. atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
  278. }
  279. static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
  280. atmel_aes_fn_t resume)
  281. {
  282. u32 isr = atmel_aes_read(dd, AES_ISR);
  283. if (unlikely(isr & AES_INT_DATARDY))
  284. return resume(dd);
  285. dd->resume = resume;
  286. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  287. return -EINPROGRESS;
  288. }
  289. static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
  290. {
  291. len &= block_size - 1;
  292. return len ? block_size - len : 0;
  293. }
  294. static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
  295. {
  296. struct atmel_aes_dev *aes_dd = NULL;
  297. struct atmel_aes_dev *tmp;
  298. spin_lock_bh(&atmel_aes.lock);
  299. if (!ctx->dd) {
  300. list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
  301. aes_dd = tmp;
  302. break;
  303. }
  304. ctx->dd = aes_dd;
  305. } else {
  306. aes_dd = ctx->dd;
  307. }
  308. spin_unlock_bh(&atmel_aes.lock);
  309. return aes_dd;
  310. }
  311. static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
  312. {
  313. int err;
  314. err = clk_enable(dd->iclk);
  315. if (err)
  316. return err;
  317. if (!(dd->flags & AES_FLAGS_INIT)) {
  318. atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
  319. atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
  320. dd->flags |= AES_FLAGS_INIT;
  321. }
  322. return 0;
  323. }
  324. static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
  325. {
  326. return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
  327. }
  328. static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
  329. {
  330. int err;
  331. err = atmel_aes_hw_init(dd);
  332. if (err)
  333. return err;
  334. dd->hw_version = atmel_aes_get_version(dd);
  335. dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
  336. clk_disable(dd->iclk);
  337. return 0;
  338. }
  339. static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
  340. const struct atmel_aes_reqctx *rctx)
  341. {
  342. /* Clear all but persistent flags and set request flags. */
  343. dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
  344. }
  345. static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
  346. {
  347. return (dd->flags & AES_FLAGS_ENCRYPT);
  348. }
  349. static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
  350. {
  351. clk_disable(dd->iclk);
  352. dd->flags &= ~AES_FLAGS_BUSY;
  353. if (dd->is_async)
  354. dd->areq->complete(dd->areq, err);
  355. tasklet_schedule(&dd->queue_task);
  356. return err;
  357. }
  358. static void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
  359. const u32 *iv)
  360. {
  361. u32 valmr = 0;
  362. /* MR register must be set before IV registers */
  363. if (dd->ctx->keylen == AES_KEYSIZE_128)
  364. valmr |= AES_MR_KEYSIZE_128;
  365. else if (dd->ctx->keylen == AES_KEYSIZE_192)
  366. valmr |= AES_MR_KEYSIZE_192;
  367. else
  368. valmr |= AES_MR_KEYSIZE_256;
  369. valmr |= dd->flags & AES_FLAGS_MODE_MASK;
  370. if (use_dma) {
  371. valmr |= AES_MR_SMOD_IDATAR0;
  372. if (dd->caps.has_dualbuff)
  373. valmr |= AES_MR_DUALBUFF;
  374. } else {
  375. valmr |= AES_MR_SMOD_AUTO;
  376. }
  377. atmel_aes_write(dd, AES_MR, valmr);
  378. atmel_aes_write_n(dd, AES_KEYWR(0), dd->ctx->key,
  379. SIZE_IN_WORDS(dd->ctx->keylen));
  380. if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
  381. atmel_aes_write_block(dd, AES_IVR(0), iv);
  382. }
  383. /* CPU transfer */
  384. static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
  385. {
  386. int err = 0;
  387. u32 isr;
  388. for (;;) {
  389. atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
  390. dd->data += 4;
  391. dd->datalen -= AES_BLOCK_SIZE;
  392. if (dd->datalen < AES_BLOCK_SIZE)
  393. break;
  394. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  395. isr = atmel_aes_read(dd, AES_ISR);
  396. if (!(isr & AES_INT_DATARDY)) {
  397. dd->resume = atmel_aes_cpu_transfer;
  398. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  399. return -EINPROGRESS;
  400. }
  401. }
  402. if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
  403. dd->buf, dd->total))
  404. err = -EINVAL;
  405. if (err)
  406. return atmel_aes_complete(dd, err);
  407. return dd->cpu_transfer_complete(dd);
  408. }
  409. static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
  410. struct scatterlist *src,
  411. struct scatterlist *dst,
  412. size_t len,
  413. atmel_aes_fn_t resume)
  414. {
  415. size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
  416. if (unlikely(len == 0))
  417. return -EINVAL;
  418. sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
  419. dd->total = len;
  420. dd->real_dst = dst;
  421. dd->cpu_transfer_complete = resume;
  422. dd->datalen = len + padlen;
  423. dd->data = (u32 *)dd->buf;
  424. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  425. return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
  426. }
  427. /* DMA transfer */
  428. static void atmel_aes_dma_callback(void *data);
  429. static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
  430. struct scatterlist *sg,
  431. size_t len,
  432. struct atmel_aes_dma *dma)
  433. {
  434. int nents;
  435. if (!IS_ALIGNED(len, dd->ctx->block_size))
  436. return false;
  437. for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  438. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  439. return false;
  440. if (len <= sg->length) {
  441. if (!IS_ALIGNED(len, dd->ctx->block_size))
  442. return false;
  443. dma->nents = nents+1;
  444. dma->remainder = sg->length - len;
  445. sg->length = len;
  446. return true;
  447. }
  448. if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
  449. return false;
  450. len -= sg->length;
  451. }
  452. return false;
  453. }
  454. static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
  455. {
  456. struct scatterlist *sg = dma->sg;
  457. int nents = dma->nents;
  458. if (!dma->remainder)
  459. return;
  460. while (--nents > 0 && sg)
  461. sg = sg_next(sg);
  462. if (!sg)
  463. return;
  464. sg->length += dma->remainder;
  465. }
  466. static int atmel_aes_map(struct atmel_aes_dev *dd,
  467. struct scatterlist *src,
  468. struct scatterlist *dst,
  469. size_t len)
  470. {
  471. bool src_aligned, dst_aligned;
  472. size_t padlen;
  473. dd->total = len;
  474. dd->src.sg = src;
  475. dd->dst.sg = dst;
  476. dd->real_dst = dst;
  477. src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
  478. if (src == dst)
  479. dst_aligned = src_aligned;
  480. else
  481. dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
  482. if (!src_aligned || !dst_aligned) {
  483. padlen = atmel_aes_padlen(len, dd->ctx->block_size);
  484. if (dd->buflen < len + padlen)
  485. return -ENOMEM;
  486. if (!src_aligned) {
  487. sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
  488. dd->src.sg = &dd->aligned_sg;
  489. dd->src.nents = 1;
  490. dd->src.remainder = 0;
  491. }
  492. if (!dst_aligned) {
  493. dd->dst.sg = &dd->aligned_sg;
  494. dd->dst.nents = 1;
  495. dd->dst.remainder = 0;
  496. }
  497. sg_init_table(&dd->aligned_sg, 1);
  498. sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
  499. }
  500. if (dd->src.sg == dd->dst.sg) {
  501. dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
  502. DMA_BIDIRECTIONAL);
  503. dd->dst.sg_len = dd->src.sg_len;
  504. if (!dd->src.sg_len)
  505. return -EFAULT;
  506. } else {
  507. dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
  508. DMA_TO_DEVICE);
  509. if (!dd->src.sg_len)
  510. return -EFAULT;
  511. dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
  512. DMA_FROM_DEVICE);
  513. if (!dd->dst.sg_len) {
  514. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  515. DMA_TO_DEVICE);
  516. return -EFAULT;
  517. }
  518. }
  519. return 0;
  520. }
  521. static void atmel_aes_unmap(struct atmel_aes_dev *dd)
  522. {
  523. if (dd->src.sg == dd->dst.sg) {
  524. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  525. DMA_BIDIRECTIONAL);
  526. if (dd->src.sg != &dd->aligned_sg)
  527. atmel_aes_restore_sg(&dd->src);
  528. } else {
  529. dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
  530. DMA_FROM_DEVICE);
  531. if (dd->dst.sg != &dd->aligned_sg)
  532. atmel_aes_restore_sg(&dd->dst);
  533. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  534. DMA_TO_DEVICE);
  535. if (dd->src.sg != &dd->aligned_sg)
  536. atmel_aes_restore_sg(&dd->src);
  537. }
  538. if (dd->dst.sg == &dd->aligned_sg)
  539. sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
  540. dd->buf, dd->total);
  541. }
  542. static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
  543. enum dma_slave_buswidth addr_width,
  544. enum dma_transfer_direction dir,
  545. u32 maxburst)
  546. {
  547. struct dma_async_tx_descriptor *desc;
  548. struct dma_slave_config config;
  549. dma_async_tx_callback callback;
  550. struct atmel_aes_dma *dma;
  551. int err;
  552. memset(&config, 0, sizeof(config));
  553. config.direction = dir;
  554. config.src_addr_width = addr_width;
  555. config.dst_addr_width = addr_width;
  556. config.src_maxburst = maxburst;
  557. config.dst_maxburst = maxburst;
  558. switch (dir) {
  559. case DMA_MEM_TO_DEV:
  560. dma = &dd->src;
  561. callback = NULL;
  562. config.dst_addr = dd->phys_base + AES_IDATAR(0);
  563. break;
  564. case DMA_DEV_TO_MEM:
  565. dma = &dd->dst;
  566. callback = atmel_aes_dma_callback;
  567. config.src_addr = dd->phys_base + AES_ODATAR(0);
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. err = dmaengine_slave_config(dma->chan, &config);
  573. if (err)
  574. return err;
  575. desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
  576. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  577. if (!desc)
  578. return -ENOMEM;
  579. desc->callback = callback;
  580. desc->callback_param = dd;
  581. dmaengine_submit(desc);
  582. dma_async_issue_pending(dma->chan);
  583. return 0;
  584. }
  585. static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd,
  586. enum dma_transfer_direction dir)
  587. {
  588. struct atmel_aes_dma *dma;
  589. switch (dir) {
  590. case DMA_MEM_TO_DEV:
  591. dma = &dd->src;
  592. break;
  593. case DMA_DEV_TO_MEM:
  594. dma = &dd->dst;
  595. break;
  596. default:
  597. return;
  598. }
  599. dmaengine_terminate_all(dma->chan);
  600. }
  601. static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
  602. struct scatterlist *src,
  603. struct scatterlist *dst,
  604. size_t len,
  605. atmel_aes_fn_t resume)
  606. {
  607. enum dma_slave_buswidth addr_width;
  608. u32 maxburst;
  609. int err;
  610. switch (dd->ctx->block_size) {
  611. case CFB8_BLOCK_SIZE:
  612. addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  613. maxburst = 1;
  614. break;
  615. case CFB16_BLOCK_SIZE:
  616. addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  617. maxburst = 1;
  618. break;
  619. case CFB32_BLOCK_SIZE:
  620. case CFB64_BLOCK_SIZE:
  621. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  622. maxburst = 1;
  623. break;
  624. case AES_BLOCK_SIZE:
  625. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  626. maxburst = dd->caps.max_burst_size;
  627. break;
  628. default:
  629. err = -EINVAL;
  630. goto exit;
  631. }
  632. err = atmel_aes_map(dd, src, dst, len);
  633. if (err)
  634. goto exit;
  635. dd->resume = resume;
  636. /* Set output DMA transfer first */
  637. err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
  638. maxburst);
  639. if (err)
  640. goto unmap;
  641. /* Then set input DMA transfer */
  642. err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
  643. maxburst);
  644. if (err)
  645. goto output_transfer_stop;
  646. return -EINPROGRESS;
  647. output_transfer_stop:
  648. atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
  649. unmap:
  650. atmel_aes_unmap(dd);
  651. exit:
  652. return atmel_aes_complete(dd, err);
  653. }
  654. static void atmel_aes_dma_stop(struct atmel_aes_dev *dd)
  655. {
  656. atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV);
  657. atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
  658. atmel_aes_unmap(dd);
  659. }
  660. static void atmel_aes_dma_callback(void *data)
  661. {
  662. struct atmel_aes_dev *dd = data;
  663. atmel_aes_dma_stop(dd);
  664. dd->is_async = true;
  665. (void)dd->resume(dd);
  666. }
  667. static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
  668. struct crypto_async_request *new_areq)
  669. {
  670. struct crypto_async_request *areq, *backlog;
  671. struct atmel_aes_base_ctx *ctx;
  672. unsigned long flags;
  673. int err, ret = 0;
  674. spin_lock_irqsave(&dd->lock, flags);
  675. if (new_areq)
  676. ret = crypto_enqueue_request(&dd->queue, new_areq);
  677. if (dd->flags & AES_FLAGS_BUSY) {
  678. spin_unlock_irqrestore(&dd->lock, flags);
  679. return ret;
  680. }
  681. backlog = crypto_get_backlog(&dd->queue);
  682. areq = crypto_dequeue_request(&dd->queue);
  683. if (areq)
  684. dd->flags |= AES_FLAGS_BUSY;
  685. spin_unlock_irqrestore(&dd->lock, flags);
  686. if (!areq)
  687. return ret;
  688. if (backlog)
  689. backlog->complete(backlog, -EINPROGRESS);
  690. ctx = crypto_tfm_ctx(areq->tfm);
  691. dd->areq = areq;
  692. dd->ctx = ctx;
  693. dd->is_async = (areq != new_areq);
  694. err = ctx->start(dd);
  695. return (dd->is_async) ? ret : err;
  696. }
  697. /* AES async block ciphers */
  698. static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
  699. {
  700. return atmel_aes_complete(dd, 0);
  701. }
  702. static int atmel_aes_start(struct atmel_aes_dev *dd)
  703. {
  704. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  705. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  706. bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD ||
  707. dd->ctx->block_size != AES_BLOCK_SIZE);
  708. int err;
  709. atmel_aes_set_mode(dd, rctx);
  710. err = atmel_aes_hw_init(dd);
  711. if (err)
  712. return atmel_aes_complete(dd, err);
  713. atmel_aes_write_ctrl(dd, use_dma, req->info);
  714. if (use_dma)
  715. return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
  716. atmel_aes_transfer_complete);
  717. return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
  718. atmel_aes_transfer_complete);
  719. }
  720. static inline struct atmel_aes_ctr_ctx *
  721. atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
  722. {
  723. return container_of(ctx, struct atmel_aes_ctr_ctx, base);
  724. }
  725. static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
  726. {
  727. struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
  728. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  729. struct scatterlist *src, *dst;
  730. u32 ctr, blocks;
  731. size_t datalen;
  732. bool use_dma, fragmented = false;
  733. /* Check for transfer completion. */
  734. ctx->offset += dd->total;
  735. if (ctx->offset >= req->nbytes)
  736. return atmel_aes_transfer_complete(dd);
  737. /* Compute data length. */
  738. datalen = req->nbytes - ctx->offset;
  739. blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
  740. ctr = be32_to_cpu(ctx->iv[3]);
  741. if (dd->caps.has_ctr32) {
  742. /* Check 32bit counter overflow. */
  743. u32 start = ctr;
  744. u32 end = start + blocks - 1;
  745. if (end < start) {
  746. ctr |= 0xffffffff;
  747. datalen = AES_BLOCK_SIZE * -start;
  748. fragmented = true;
  749. }
  750. } else {
  751. /* Check 16bit counter overflow. */
  752. u16 start = ctr & 0xffff;
  753. u16 end = start + (u16)blocks - 1;
  754. if (blocks >> 16 || end < start) {
  755. ctr |= 0xffff;
  756. datalen = AES_BLOCK_SIZE * (0x10000-start);
  757. fragmented = true;
  758. }
  759. }
  760. use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
  761. /* Jump to offset. */
  762. src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
  763. dst = ((req->src == req->dst) ? src :
  764. scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
  765. /* Configure hardware. */
  766. atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
  767. if (unlikely(fragmented)) {
  768. /*
  769. * Increment the counter manually to cope with the hardware
  770. * counter overflow.
  771. */
  772. ctx->iv[3] = cpu_to_be32(ctr);
  773. crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
  774. }
  775. if (use_dma)
  776. return atmel_aes_dma_start(dd, src, dst, datalen,
  777. atmel_aes_ctr_transfer);
  778. return atmel_aes_cpu_start(dd, src, dst, datalen,
  779. atmel_aes_ctr_transfer);
  780. }
  781. static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
  782. {
  783. struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
  784. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  785. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  786. int err;
  787. atmel_aes_set_mode(dd, rctx);
  788. err = atmel_aes_hw_init(dd);
  789. if (err)
  790. return atmel_aes_complete(dd, err);
  791. memcpy(ctx->iv, req->info, AES_BLOCK_SIZE);
  792. ctx->offset = 0;
  793. dd->total = 0;
  794. return atmel_aes_ctr_transfer(dd);
  795. }
  796. static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  797. {
  798. struct atmel_aes_base_ctx *ctx;
  799. struct atmel_aes_reqctx *rctx;
  800. struct atmel_aes_dev *dd;
  801. ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
  802. switch (mode & AES_FLAGS_OPMODE_MASK) {
  803. case AES_FLAGS_CFB8:
  804. ctx->block_size = CFB8_BLOCK_SIZE;
  805. break;
  806. case AES_FLAGS_CFB16:
  807. ctx->block_size = CFB16_BLOCK_SIZE;
  808. break;
  809. case AES_FLAGS_CFB32:
  810. ctx->block_size = CFB32_BLOCK_SIZE;
  811. break;
  812. case AES_FLAGS_CFB64:
  813. ctx->block_size = CFB64_BLOCK_SIZE;
  814. break;
  815. default:
  816. ctx->block_size = AES_BLOCK_SIZE;
  817. break;
  818. }
  819. dd = atmel_aes_find_dev(ctx);
  820. if (!dd)
  821. return -ENODEV;
  822. rctx = ablkcipher_request_ctx(req);
  823. rctx->mode = mode;
  824. return atmel_aes_handle_queue(dd, &req->base);
  825. }
  826. static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  827. unsigned int keylen)
  828. {
  829. struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  830. if (keylen != AES_KEYSIZE_128 &&
  831. keylen != AES_KEYSIZE_192 &&
  832. keylen != AES_KEYSIZE_256) {
  833. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  834. return -EINVAL;
  835. }
  836. memcpy(ctx->key, key, keylen);
  837. ctx->keylen = keylen;
  838. return 0;
  839. }
  840. static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
  841. {
  842. return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
  843. }
  844. static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
  845. {
  846. return atmel_aes_crypt(req, AES_FLAGS_ECB);
  847. }
  848. static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
  849. {
  850. return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
  851. }
  852. static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
  853. {
  854. return atmel_aes_crypt(req, AES_FLAGS_CBC);
  855. }
  856. static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
  857. {
  858. return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
  859. }
  860. static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
  861. {
  862. return atmel_aes_crypt(req, AES_FLAGS_OFB);
  863. }
  864. static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
  865. {
  866. return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
  867. }
  868. static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
  869. {
  870. return atmel_aes_crypt(req, AES_FLAGS_CFB128);
  871. }
  872. static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
  873. {
  874. return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
  875. }
  876. static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
  877. {
  878. return atmel_aes_crypt(req, AES_FLAGS_CFB64);
  879. }
  880. static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
  881. {
  882. return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
  883. }
  884. static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
  885. {
  886. return atmel_aes_crypt(req, AES_FLAGS_CFB32);
  887. }
  888. static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
  889. {
  890. return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
  891. }
  892. static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
  893. {
  894. return atmel_aes_crypt(req, AES_FLAGS_CFB16);
  895. }
  896. static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
  897. {
  898. return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
  899. }
  900. static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
  901. {
  902. return atmel_aes_crypt(req, AES_FLAGS_CFB8);
  903. }
  904. static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
  905. {
  906. return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
  907. }
  908. static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
  909. {
  910. return atmel_aes_crypt(req, AES_FLAGS_CTR);
  911. }
  912. static int atmel_aes_cra_init(struct crypto_tfm *tfm)
  913. {
  914. struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  915. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  916. ctx->base.start = atmel_aes_start;
  917. return 0;
  918. }
  919. static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm)
  920. {
  921. struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  922. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  923. ctx->base.start = atmel_aes_ctr_start;
  924. return 0;
  925. }
  926. static void atmel_aes_cra_exit(struct crypto_tfm *tfm)
  927. {
  928. }
  929. static struct crypto_alg aes_algs[] = {
  930. {
  931. .cra_name = "ecb(aes)",
  932. .cra_driver_name = "atmel-ecb-aes",
  933. .cra_priority = ATMEL_AES_PRIORITY,
  934. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  935. .cra_blocksize = AES_BLOCK_SIZE,
  936. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  937. .cra_alignmask = 0xf,
  938. .cra_type = &crypto_ablkcipher_type,
  939. .cra_module = THIS_MODULE,
  940. .cra_init = atmel_aes_cra_init,
  941. .cra_exit = atmel_aes_cra_exit,
  942. .cra_u.ablkcipher = {
  943. .min_keysize = AES_MIN_KEY_SIZE,
  944. .max_keysize = AES_MAX_KEY_SIZE,
  945. .setkey = atmel_aes_setkey,
  946. .encrypt = atmel_aes_ecb_encrypt,
  947. .decrypt = atmel_aes_ecb_decrypt,
  948. }
  949. },
  950. {
  951. .cra_name = "cbc(aes)",
  952. .cra_driver_name = "atmel-cbc-aes",
  953. .cra_priority = ATMEL_AES_PRIORITY,
  954. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  955. .cra_blocksize = AES_BLOCK_SIZE,
  956. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  957. .cra_alignmask = 0xf,
  958. .cra_type = &crypto_ablkcipher_type,
  959. .cra_module = THIS_MODULE,
  960. .cra_init = atmel_aes_cra_init,
  961. .cra_exit = atmel_aes_cra_exit,
  962. .cra_u.ablkcipher = {
  963. .min_keysize = AES_MIN_KEY_SIZE,
  964. .max_keysize = AES_MAX_KEY_SIZE,
  965. .ivsize = AES_BLOCK_SIZE,
  966. .setkey = atmel_aes_setkey,
  967. .encrypt = atmel_aes_cbc_encrypt,
  968. .decrypt = atmel_aes_cbc_decrypt,
  969. }
  970. },
  971. {
  972. .cra_name = "ofb(aes)",
  973. .cra_driver_name = "atmel-ofb-aes",
  974. .cra_priority = ATMEL_AES_PRIORITY,
  975. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  976. .cra_blocksize = AES_BLOCK_SIZE,
  977. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  978. .cra_alignmask = 0xf,
  979. .cra_type = &crypto_ablkcipher_type,
  980. .cra_module = THIS_MODULE,
  981. .cra_init = atmel_aes_cra_init,
  982. .cra_exit = atmel_aes_cra_exit,
  983. .cra_u.ablkcipher = {
  984. .min_keysize = AES_MIN_KEY_SIZE,
  985. .max_keysize = AES_MAX_KEY_SIZE,
  986. .ivsize = AES_BLOCK_SIZE,
  987. .setkey = atmel_aes_setkey,
  988. .encrypt = atmel_aes_ofb_encrypt,
  989. .decrypt = atmel_aes_ofb_decrypt,
  990. }
  991. },
  992. {
  993. .cra_name = "cfb(aes)",
  994. .cra_driver_name = "atmel-cfb-aes",
  995. .cra_priority = ATMEL_AES_PRIORITY,
  996. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  997. .cra_blocksize = AES_BLOCK_SIZE,
  998. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  999. .cra_alignmask = 0xf,
  1000. .cra_type = &crypto_ablkcipher_type,
  1001. .cra_module = THIS_MODULE,
  1002. .cra_init = atmel_aes_cra_init,
  1003. .cra_exit = atmel_aes_cra_exit,
  1004. .cra_u.ablkcipher = {
  1005. .min_keysize = AES_MIN_KEY_SIZE,
  1006. .max_keysize = AES_MAX_KEY_SIZE,
  1007. .ivsize = AES_BLOCK_SIZE,
  1008. .setkey = atmel_aes_setkey,
  1009. .encrypt = atmel_aes_cfb_encrypt,
  1010. .decrypt = atmel_aes_cfb_decrypt,
  1011. }
  1012. },
  1013. {
  1014. .cra_name = "cfb32(aes)",
  1015. .cra_driver_name = "atmel-cfb32-aes",
  1016. .cra_priority = ATMEL_AES_PRIORITY,
  1017. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1018. .cra_blocksize = CFB32_BLOCK_SIZE,
  1019. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1020. .cra_alignmask = 0x3,
  1021. .cra_type = &crypto_ablkcipher_type,
  1022. .cra_module = THIS_MODULE,
  1023. .cra_init = atmel_aes_cra_init,
  1024. .cra_exit = atmel_aes_cra_exit,
  1025. .cra_u.ablkcipher = {
  1026. .min_keysize = AES_MIN_KEY_SIZE,
  1027. .max_keysize = AES_MAX_KEY_SIZE,
  1028. .ivsize = AES_BLOCK_SIZE,
  1029. .setkey = atmel_aes_setkey,
  1030. .encrypt = atmel_aes_cfb32_encrypt,
  1031. .decrypt = atmel_aes_cfb32_decrypt,
  1032. }
  1033. },
  1034. {
  1035. .cra_name = "cfb16(aes)",
  1036. .cra_driver_name = "atmel-cfb16-aes",
  1037. .cra_priority = ATMEL_AES_PRIORITY,
  1038. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1039. .cra_blocksize = CFB16_BLOCK_SIZE,
  1040. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1041. .cra_alignmask = 0x1,
  1042. .cra_type = &crypto_ablkcipher_type,
  1043. .cra_module = THIS_MODULE,
  1044. .cra_init = atmel_aes_cra_init,
  1045. .cra_exit = atmel_aes_cra_exit,
  1046. .cra_u.ablkcipher = {
  1047. .min_keysize = AES_MIN_KEY_SIZE,
  1048. .max_keysize = AES_MAX_KEY_SIZE,
  1049. .ivsize = AES_BLOCK_SIZE,
  1050. .setkey = atmel_aes_setkey,
  1051. .encrypt = atmel_aes_cfb16_encrypt,
  1052. .decrypt = atmel_aes_cfb16_decrypt,
  1053. }
  1054. },
  1055. {
  1056. .cra_name = "cfb8(aes)",
  1057. .cra_driver_name = "atmel-cfb8-aes",
  1058. .cra_priority = ATMEL_AES_PRIORITY,
  1059. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1060. .cra_blocksize = CFB8_BLOCK_SIZE,
  1061. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1062. .cra_alignmask = 0x0,
  1063. .cra_type = &crypto_ablkcipher_type,
  1064. .cra_module = THIS_MODULE,
  1065. .cra_init = atmel_aes_cra_init,
  1066. .cra_exit = atmel_aes_cra_exit,
  1067. .cra_u.ablkcipher = {
  1068. .min_keysize = AES_MIN_KEY_SIZE,
  1069. .max_keysize = AES_MAX_KEY_SIZE,
  1070. .ivsize = AES_BLOCK_SIZE,
  1071. .setkey = atmel_aes_setkey,
  1072. .encrypt = atmel_aes_cfb8_encrypt,
  1073. .decrypt = atmel_aes_cfb8_decrypt,
  1074. }
  1075. },
  1076. {
  1077. .cra_name = "ctr(aes)",
  1078. .cra_driver_name = "atmel-ctr-aes",
  1079. .cra_priority = ATMEL_AES_PRIORITY,
  1080. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1081. .cra_blocksize = 1,
  1082. .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
  1083. .cra_alignmask = 0xf,
  1084. .cra_type = &crypto_ablkcipher_type,
  1085. .cra_module = THIS_MODULE,
  1086. .cra_init = atmel_aes_ctr_cra_init,
  1087. .cra_exit = atmel_aes_cra_exit,
  1088. .cra_u.ablkcipher = {
  1089. .min_keysize = AES_MIN_KEY_SIZE,
  1090. .max_keysize = AES_MAX_KEY_SIZE,
  1091. .ivsize = AES_BLOCK_SIZE,
  1092. .setkey = atmel_aes_setkey,
  1093. .encrypt = atmel_aes_ctr_encrypt,
  1094. .decrypt = atmel_aes_ctr_decrypt,
  1095. }
  1096. },
  1097. };
  1098. static struct crypto_alg aes_cfb64_alg = {
  1099. .cra_name = "cfb64(aes)",
  1100. .cra_driver_name = "atmel-cfb64-aes",
  1101. .cra_priority = ATMEL_AES_PRIORITY,
  1102. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1103. .cra_blocksize = CFB64_BLOCK_SIZE,
  1104. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1105. .cra_alignmask = 0x7,
  1106. .cra_type = &crypto_ablkcipher_type,
  1107. .cra_module = THIS_MODULE,
  1108. .cra_init = atmel_aes_cra_init,
  1109. .cra_exit = atmel_aes_cra_exit,
  1110. .cra_u.ablkcipher = {
  1111. .min_keysize = AES_MIN_KEY_SIZE,
  1112. .max_keysize = AES_MAX_KEY_SIZE,
  1113. .ivsize = AES_BLOCK_SIZE,
  1114. .setkey = atmel_aes_setkey,
  1115. .encrypt = atmel_aes_cfb64_encrypt,
  1116. .decrypt = atmel_aes_cfb64_decrypt,
  1117. }
  1118. };
  1119. /* gcm aead functions */
  1120. static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
  1121. const u32 *data, size_t datalen,
  1122. const u32 *ghash_in, u32 *ghash_out,
  1123. atmel_aes_fn_t resume);
  1124. static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
  1125. static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
  1126. static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
  1127. static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
  1128. static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
  1129. static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
  1130. static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
  1131. static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
  1132. static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
  1133. static inline struct atmel_aes_gcm_ctx *
  1134. atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
  1135. {
  1136. return container_of(ctx, struct atmel_aes_gcm_ctx, base);
  1137. }
  1138. static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
  1139. const u32 *data, size_t datalen,
  1140. const u32 *ghash_in, u32 *ghash_out,
  1141. atmel_aes_fn_t resume)
  1142. {
  1143. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1144. dd->data = (u32 *)data;
  1145. dd->datalen = datalen;
  1146. ctx->ghash_in = ghash_in;
  1147. ctx->ghash_out = ghash_out;
  1148. ctx->ghash_resume = resume;
  1149. atmel_aes_write_ctrl(dd, false, NULL);
  1150. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
  1151. }
  1152. static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
  1153. {
  1154. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1155. /* Set the data length. */
  1156. atmel_aes_write(dd, AES_AADLENR, dd->total);
  1157. atmel_aes_write(dd, AES_CLENR, 0);
  1158. /* If needed, overwrite the GCM Intermediate Hash Word Registers */
  1159. if (ctx->ghash_in)
  1160. atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
  1161. return atmel_aes_gcm_ghash_finalize(dd);
  1162. }
  1163. static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
  1164. {
  1165. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1166. u32 isr;
  1167. /* Write data into the Input Data Registers. */
  1168. while (dd->datalen > 0) {
  1169. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  1170. dd->data += 4;
  1171. dd->datalen -= AES_BLOCK_SIZE;
  1172. isr = atmel_aes_read(dd, AES_ISR);
  1173. if (!(isr & AES_INT_DATARDY)) {
  1174. dd->resume = atmel_aes_gcm_ghash_finalize;
  1175. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  1176. return -EINPROGRESS;
  1177. }
  1178. }
  1179. /* Read the computed hash from GHASHRx. */
  1180. atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
  1181. return ctx->ghash_resume(dd);
  1182. }
  1183. static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
  1184. {
  1185. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1186. struct aead_request *req = aead_request_cast(dd->areq);
  1187. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1188. struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
  1189. size_t ivsize = crypto_aead_ivsize(tfm);
  1190. size_t datalen, padlen;
  1191. const void *iv = req->iv;
  1192. u8 *data = dd->buf;
  1193. int err;
  1194. atmel_aes_set_mode(dd, rctx);
  1195. err = atmel_aes_hw_init(dd);
  1196. if (err)
  1197. return atmel_aes_complete(dd, err);
  1198. if (likely(ivsize == 12)) {
  1199. memcpy(ctx->j0, iv, ivsize);
  1200. ctx->j0[3] = cpu_to_be32(1);
  1201. return atmel_aes_gcm_process(dd);
  1202. }
  1203. padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
  1204. datalen = ivsize + padlen + AES_BLOCK_SIZE;
  1205. if (datalen > dd->buflen)
  1206. return atmel_aes_complete(dd, -EINVAL);
  1207. memcpy(data, iv, ivsize);
  1208. memset(data + ivsize, 0, padlen + sizeof(u64));
  1209. ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
  1210. return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
  1211. NULL, ctx->j0, atmel_aes_gcm_process);
  1212. }
  1213. static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
  1214. {
  1215. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1216. struct aead_request *req = aead_request_cast(dd->areq);
  1217. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1218. bool enc = atmel_aes_is_encrypt(dd);
  1219. u32 authsize;
  1220. /* Compute text length. */
  1221. authsize = crypto_aead_authsize(tfm);
  1222. ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
  1223. /*
  1224. * According to tcrypt test suite, the GCM Automatic Tag Generation
  1225. * fails when both the message and its associated data are empty.
  1226. */
  1227. if (likely(req->assoclen != 0 || ctx->textlen != 0))
  1228. dd->flags |= AES_FLAGS_GTAGEN;
  1229. atmel_aes_write_ctrl(dd, false, NULL);
  1230. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
  1231. }
  1232. static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
  1233. {
  1234. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1235. struct aead_request *req = aead_request_cast(dd->areq);
  1236. u32 j0_lsw, *j0 = ctx->j0;
  1237. size_t padlen;
  1238. /* Write incr32(J0) into IV. */
  1239. j0_lsw = j0[3];
  1240. j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
  1241. atmel_aes_write_block(dd, AES_IVR(0), j0);
  1242. j0[3] = j0_lsw;
  1243. /* Set aad and text lengths. */
  1244. atmel_aes_write(dd, AES_AADLENR, req->assoclen);
  1245. atmel_aes_write(dd, AES_CLENR, ctx->textlen);
  1246. /* Check whether AAD are present. */
  1247. if (unlikely(req->assoclen == 0)) {
  1248. dd->datalen = 0;
  1249. return atmel_aes_gcm_data(dd);
  1250. }
  1251. /* Copy assoc data and add padding. */
  1252. padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
  1253. if (unlikely(req->assoclen + padlen > dd->buflen))
  1254. return atmel_aes_complete(dd, -EINVAL);
  1255. sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
  1256. /* Write assoc data into the Input Data register. */
  1257. dd->data = (u32 *)dd->buf;
  1258. dd->datalen = req->assoclen + padlen;
  1259. return atmel_aes_gcm_data(dd);
  1260. }
  1261. static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
  1262. {
  1263. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1264. struct aead_request *req = aead_request_cast(dd->areq);
  1265. bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
  1266. struct scatterlist *src, *dst;
  1267. u32 isr, mr;
  1268. /* Write AAD first. */
  1269. while (dd->datalen > 0) {
  1270. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  1271. dd->data += 4;
  1272. dd->datalen -= AES_BLOCK_SIZE;
  1273. isr = atmel_aes_read(dd, AES_ISR);
  1274. if (!(isr & AES_INT_DATARDY)) {
  1275. dd->resume = atmel_aes_gcm_data;
  1276. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  1277. return -EINPROGRESS;
  1278. }
  1279. }
  1280. /* GMAC only. */
  1281. if (unlikely(ctx->textlen == 0))
  1282. return atmel_aes_gcm_tag_init(dd);
  1283. /* Prepare src and dst scatter lists to transfer cipher/plain texts */
  1284. src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
  1285. dst = ((req->src == req->dst) ? src :
  1286. scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
  1287. if (use_dma) {
  1288. /* Update the Mode Register for DMA transfers. */
  1289. mr = atmel_aes_read(dd, AES_MR);
  1290. mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
  1291. mr |= AES_MR_SMOD_IDATAR0;
  1292. if (dd->caps.has_dualbuff)
  1293. mr |= AES_MR_DUALBUFF;
  1294. atmel_aes_write(dd, AES_MR, mr);
  1295. return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
  1296. atmel_aes_gcm_tag_init);
  1297. }
  1298. return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
  1299. atmel_aes_gcm_tag_init);
  1300. }
  1301. static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
  1302. {
  1303. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1304. struct aead_request *req = aead_request_cast(dd->areq);
  1305. u64 *data = dd->buf;
  1306. if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
  1307. if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
  1308. dd->resume = atmel_aes_gcm_tag_init;
  1309. atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
  1310. return -EINPROGRESS;
  1311. }
  1312. return atmel_aes_gcm_finalize(dd);
  1313. }
  1314. /* Read the GCM Intermediate Hash Word Registers. */
  1315. atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
  1316. data[0] = cpu_to_be64(req->assoclen * 8);
  1317. data[1] = cpu_to_be64(ctx->textlen * 8);
  1318. return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
  1319. ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
  1320. }
  1321. static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
  1322. {
  1323. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1324. unsigned long flags;
  1325. /*
  1326. * Change mode to CTR to complete the tag generation.
  1327. * Use J0 as Initialization Vector.
  1328. */
  1329. flags = dd->flags;
  1330. dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
  1331. dd->flags |= AES_FLAGS_CTR;
  1332. atmel_aes_write_ctrl(dd, false, ctx->j0);
  1333. dd->flags = flags;
  1334. atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
  1335. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
  1336. }
  1337. static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
  1338. {
  1339. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1340. struct aead_request *req = aead_request_cast(dd->areq);
  1341. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1342. bool enc = atmel_aes_is_encrypt(dd);
  1343. u32 offset, authsize, itag[4], *otag = ctx->tag;
  1344. int err;
  1345. /* Read the computed tag. */
  1346. if (likely(dd->flags & AES_FLAGS_GTAGEN))
  1347. atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
  1348. else
  1349. atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
  1350. offset = req->assoclen + ctx->textlen;
  1351. authsize = crypto_aead_authsize(tfm);
  1352. if (enc) {
  1353. scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
  1354. err = 0;
  1355. } else {
  1356. scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
  1357. err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
  1358. }
  1359. return atmel_aes_complete(dd, err);
  1360. }
  1361. static int atmel_aes_gcm_crypt(struct aead_request *req,
  1362. unsigned long mode)
  1363. {
  1364. struct atmel_aes_base_ctx *ctx;
  1365. struct atmel_aes_reqctx *rctx;
  1366. struct atmel_aes_dev *dd;
  1367. ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  1368. ctx->block_size = AES_BLOCK_SIZE;
  1369. dd = atmel_aes_find_dev(ctx);
  1370. if (!dd)
  1371. return -ENODEV;
  1372. rctx = aead_request_ctx(req);
  1373. rctx->mode = AES_FLAGS_GCM | mode;
  1374. return atmel_aes_handle_queue(dd, &req->base);
  1375. }
  1376. static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1377. unsigned int keylen)
  1378. {
  1379. struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
  1380. if (keylen != AES_KEYSIZE_256 &&
  1381. keylen != AES_KEYSIZE_192 &&
  1382. keylen != AES_KEYSIZE_128) {
  1383. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1384. return -EINVAL;
  1385. }
  1386. memcpy(ctx->key, key, keylen);
  1387. ctx->keylen = keylen;
  1388. return 0;
  1389. }
  1390. static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
  1391. unsigned int authsize)
  1392. {
  1393. /* Same as crypto_gcm_authsize() from crypto/gcm.c */
  1394. switch (authsize) {
  1395. case 4:
  1396. case 8:
  1397. case 12:
  1398. case 13:
  1399. case 14:
  1400. case 15:
  1401. case 16:
  1402. break;
  1403. default:
  1404. return -EINVAL;
  1405. }
  1406. return 0;
  1407. }
  1408. static int atmel_aes_gcm_encrypt(struct aead_request *req)
  1409. {
  1410. return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
  1411. }
  1412. static int atmel_aes_gcm_decrypt(struct aead_request *req)
  1413. {
  1414. return atmel_aes_gcm_crypt(req, 0);
  1415. }
  1416. static int atmel_aes_gcm_init(struct crypto_aead *tfm)
  1417. {
  1418. struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
  1419. crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
  1420. ctx->base.start = atmel_aes_gcm_start;
  1421. return 0;
  1422. }
  1423. static void atmel_aes_gcm_exit(struct crypto_aead *tfm)
  1424. {
  1425. }
  1426. static struct aead_alg aes_gcm_alg = {
  1427. .setkey = atmel_aes_gcm_setkey,
  1428. .setauthsize = atmel_aes_gcm_setauthsize,
  1429. .encrypt = atmel_aes_gcm_encrypt,
  1430. .decrypt = atmel_aes_gcm_decrypt,
  1431. .init = atmel_aes_gcm_init,
  1432. .exit = atmel_aes_gcm_exit,
  1433. .ivsize = 12,
  1434. .maxauthsize = AES_BLOCK_SIZE,
  1435. .base = {
  1436. .cra_name = "gcm(aes)",
  1437. .cra_driver_name = "atmel-gcm-aes",
  1438. .cra_priority = ATMEL_AES_PRIORITY,
  1439. .cra_flags = CRYPTO_ALG_ASYNC,
  1440. .cra_blocksize = 1,
  1441. .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
  1442. .cra_alignmask = 0xf,
  1443. .cra_module = THIS_MODULE,
  1444. },
  1445. };
  1446. /* Probe functions */
  1447. static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
  1448. {
  1449. dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
  1450. dd->buflen = ATMEL_AES_BUFFER_SIZE;
  1451. dd->buflen &= ~(AES_BLOCK_SIZE - 1);
  1452. if (!dd->buf) {
  1453. dev_err(dd->dev, "unable to alloc pages.\n");
  1454. return -ENOMEM;
  1455. }
  1456. return 0;
  1457. }
  1458. static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
  1459. {
  1460. free_page((unsigned long)dd->buf);
  1461. }
  1462. static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
  1463. {
  1464. struct at_dma_slave *sl = slave;
  1465. if (sl && sl->dma_dev == chan->device->dev) {
  1466. chan->private = sl;
  1467. return true;
  1468. } else {
  1469. return false;
  1470. }
  1471. }
  1472. static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
  1473. struct crypto_platform_data *pdata)
  1474. {
  1475. struct at_dma_slave *slave;
  1476. int err = -ENOMEM;
  1477. dma_cap_mask_t mask;
  1478. dma_cap_zero(mask);
  1479. dma_cap_set(DMA_SLAVE, mask);
  1480. /* Try to grab 2 DMA channels */
  1481. slave = &pdata->dma_slave->rxdata;
  1482. dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
  1483. slave, dd->dev, "tx");
  1484. if (!dd->src.chan)
  1485. goto err_dma_in;
  1486. slave = &pdata->dma_slave->txdata;
  1487. dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
  1488. slave, dd->dev, "rx");
  1489. if (!dd->dst.chan)
  1490. goto err_dma_out;
  1491. return 0;
  1492. err_dma_out:
  1493. dma_release_channel(dd->src.chan);
  1494. err_dma_in:
  1495. dev_warn(dd->dev, "no DMA channel available\n");
  1496. return err;
  1497. }
  1498. static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
  1499. {
  1500. dma_release_channel(dd->dst.chan);
  1501. dma_release_channel(dd->src.chan);
  1502. }
  1503. static void atmel_aes_queue_task(unsigned long data)
  1504. {
  1505. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  1506. atmel_aes_handle_queue(dd, NULL);
  1507. }
  1508. static void atmel_aes_done_task(unsigned long data)
  1509. {
  1510. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  1511. dd->is_async = true;
  1512. (void)dd->resume(dd);
  1513. }
  1514. static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
  1515. {
  1516. struct atmel_aes_dev *aes_dd = dev_id;
  1517. u32 reg;
  1518. reg = atmel_aes_read(aes_dd, AES_ISR);
  1519. if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
  1520. atmel_aes_write(aes_dd, AES_IDR, reg);
  1521. if (AES_FLAGS_BUSY & aes_dd->flags)
  1522. tasklet_schedule(&aes_dd->done_task);
  1523. else
  1524. dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
  1525. return IRQ_HANDLED;
  1526. }
  1527. return IRQ_NONE;
  1528. }
  1529. static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
  1530. {
  1531. int i;
  1532. if (dd->caps.has_gcm)
  1533. crypto_unregister_aead(&aes_gcm_alg);
  1534. if (dd->caps.has_cfb64)
  1535. crypto_unregister_alg(&aes_cfb64_alg);
  1536. for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
  1537. crypto_unregister_alg(&aes_algs[i]);
  1538. }
  1539. static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
  1540. {
  1541. int err, i, j;
  1542. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  1543. err = crypto_register_alg(&aes_algs[i]);
  1544. if (err)
  1545. goto err_aes_algs;
  1546. }
  1547. if (dd->caps.has_cfb64) {
  1548. err = crypto_register_alg(&aes_cfb64_alg);
  1549. if (err)
  1550. goto err_aes_cfb64_alg;
  1551. }
  1552. if (dd->caps.has_gcm) {
  1553. err = crypto_register_aead(&aes_gcm_alg);
  1554. if (err)
  1555. goto err_aes_gcm_alg;
  1556. }
  1557. return 0;
  1558. err_aes_gcm_alg:
  1559. crypto_unregister_alg(&aes_cfb64_alg);
  1560. err_aes_cfb64_alg:
  1561. i = ARRAY_SIZE(aes_algs);
  1562. err_aes_algs:
  1563. for (j = 0; j < i; j++)
  1564. crypto_unregister_alg(&aes_algs[j]);
  1565. return err;
  1566. }
  1567. static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
  1568. {
  1569. dd->caps.has_dualbuff = 0;
  1570. dd->caps.has_cfb64 = 0;
  1571. dd->caps.has_ctr32 = 0;
  1572. dd->caps.has_gcm = 0;
  1573. dd->caps.max_burst_size = 1;
  1574. /* keep only major version number */
  1575. switch (dd->hw_version & 0xff0) {
  1576. case 0x500:
  1577. dd->caps.has_dualbuff = 1;
  1578. dd->caps.has_cfb64 = 1;
  1579. dd->caps.has_ctr32 = 1;
  1580. dd->caps.has_gcm = 1;
  1581. dd->caps.max_burst_size = 4;
  1582. break;
  1583. case 0x200:
  1584. dd->caps.has_dualbuff = 1;
  1585. dd->caps.has_cfb64 = 1;
  1586. dd->caps.has_ctr32 = 1;
  1587. dd->caps.has_gcm = 1;
  1588. dd->caps.max_burst_size = 4;
  1589. break;
  1590. case 0x130:
  1591. dd->caps.has_dualbuff = 1;
  1592. dd->caps.has_cfb64 = 1;
  1593. dd->caps.max_burst_size = 4;
  1594. break;
  1595. case 0x120:
  1596. break;
  1597. default:
  1598. dev_warn(dd->dev,
  1599. "Unmanaged aes version, set minimum capabilities\n");
  1600. break;
  1601. }
  1602. }
  1603. #if defined(CONFIG_OF)
  1604. static const struct of_device_id atmel_aes_dt_ids[] = {
  1605. { .compatible = "atmel,at91sam9g46-aes" },
  1606. { /* sentinel */ }
  1607. };
  1608. MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
  1609. static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
  1610. {
  1611. struct device_node *np = pdev->dev.of_node;
  1612. struct crypto_platform_data *pdata;
  1613. if (!np) {
  1614. dev_err(&pdev->dev, "device node not found\n");
  1615. return ERR_PTR(-EINVAL);
  1616. }
  1617. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1618. if (!pdata) {
  1619. dev_err(&pdev->dev, "could not allocate memory for pdata\n");
  1620. return ERR_PTR(-ENOMEM);
  1621. }
  1622. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  1623. sizeof(*(pdata->dma_slave)),
  1624. GFP_KERNEL);
  1625. if (!pdata->dma_slave) {
  1626. dev_err(&pdev->dev, "could not allocate memory for dma_slave\n");
  1627. devm_kfree(&pdev->dev, pdata);
  1628. return ERR_PTR(-ENOMEM);
  1629. }
  1630. return pdata;
  1631. }
  1632. #else
  1633. static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
  1634. {
  1635. return ERR_PTR(-EINVAL);
  1636. }
  1637. #endif
  1638. static int atmel_aes_probe(struct platform_device *pdev)
  1639. {
  1640. struct atmel_aes_dev *aes_dd;
  1641. struct crypto_platform_data *pdata;
  1642. struct device *dev = &pdev->dev;
  1643. struct resource *aes_res;
  1644. int err;
  1645. pdata = pdev->dev.platform_data;
  1646. if (!pdata) {
  1647. pdata = atmel_aes_of_init(pdev);
  1648. if (IS_ERR(pdata)) {
  1649. err = PTR_ERR(pdata);
  1650. goto aes_dd_err;
  1651. }
  1652. }
  1653. if (!pdata->dma_slave) {
  1654. err = -ENXIO;
  1655. goto aes_dd_err;
  1656. }
  1657. aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
  1658. if (aes_dd == NULL) {
  1659. dev_err(dev, "unable to alloc data struct.\n");
  1660. err = -ENOMEM;
  1661. goto aes_dd_err;
  1662. }
  1663. aes_dd->dev = dev;
  1664. platform_set_drvdata(pdev, aes_dd);
  1665. INIT_LIST_HEAD(&aes_dd->list);
  1666. spin_lock_init(&aes_dd->lock);
  1667. tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
  1668. (unsigned long)aes_dd);
  1669. tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
  1670. (unsigned long)aes_dd);
  1671. crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
  1672. aes_dd->irq = -1;
  1673. /* Get the base address */
  1674. aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1675. if (!aes_res) {
  1676. dev_err(dev, "no MEM resource info\n");
  1677. err = -ENODEV;
  1678. goto res_err;
  1679. }
  1680. aes_dd->phys_base = aes_res->start;
  1681. /* Get the IRQ */
  1682. aes_dd->irq = platform_get_irq(pdev, 0);
  1683. if (aes_dd->irq < 0) {
  1684. dev_err(dev, "no IRQ resource info\n");
  1685. err = aes_dd->irq;
  1686. goto res_err;
  1687. }
  1688. err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
  1689. IRQF_SHARED, "atmel-aes", aes_dd);
  1690. if (err) {
  1691. dev_err(dev, "unable to request aes irq.\n");
  1692. goto res_err;
  1693. }
  1694. /* Initializing the clock */
  1695. aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
  1696. if (IS_ERR(aes_dd->iclk)) {
  1697. dev_err(dev, "clock initialization failed.\n");
  1698. err = PTR_ERR(aes_dd->iclk);
  1699. goto res_err;
  1700. }
  1701. aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
  1702. if (IS_ERR(aes_dd->io_base)) {
  1703. dev_err(dev, "can't ioremap\n");
  1704. err = PTR_ERR(aes_dd->io_base);
  1705. goto res_err;
  1706. }
  1707. err = clk_prepare(aes_dd->iclk);
  1708. if (err)
  1709. goto res_err;
  1710. err = atmel_aes_hw_version_init(aes_dd);
  1711. if (err)
  1712. goto iclk_unprepare;
  1713. atmel_aes_get_cap(aes_dd);
  1714. err = atmel_aes_buff_init(aes_dd);
  1715. if (err)
  1716. goto err_aes_buff;
  1717. err = atmel_aes_dma_init(aes_dd, pdata);
  1718. if (err)
  1719. goto err_aes_dma;
  1720. spin_lock(&atmel_aes.lock);
  1721. list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
  1722. spin_unlock(&atmel_aes.lock);
  1723. err = atmel_aes_register_algs(aes_dd);
  1724. if (err)
  1725. goto err_algs;
  1726. dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
  1727. dma_chan_name(aes_dd->src.chan),
  1728. dma_chan_name(aes_dd->dst.chan));
  1729. return 0;
  1730. err_algs:
  1731. spin_lock(&atmel_aes.lock);
  1732. list_del(&aes_dd->list);
  1733. spin_unlock(&atmel_aes.lock);
  1734. atmel_aes_dma_cleanup(aes_dd);
  1735. err_aes_dma:
  1736. atmel_aes_buff_cleanup(aes_dd);
  1737. err_aes_buff:
  1738. iclk_unprepare:
  1739. clk_unprepare(aes_dd->iclk);
  1740. res_err:
  1741. tasklet_kill(&aes_dd->done_task);
  1742. tasklet_kill(&aes_dd->queue_task);
  1743. aes_dd_err:
  1744. dev_err(dev, "initialization failed.\n");
  1745. return err;
  1746. }
  1747. static int atmel_aes_remove(struct platform_device *pdev)
  1748. {
  1749. static struct atmel_aes_dev *aes_dd;
  1750. aes_dd = platform_get_drvdata(pdev);
  1751. if (!aes_dd)
  1752. return -ENODEV;
  1753. spin_lock(&atmel_aes.lock);
  1754. list_del(&aes_dd->list);
  1755. spin_unlock(&atmel_aes.lock);
  1756. atmel_aes_unregister_algs(aes_dd);
  1757. tasklet_kill(&aes_dd->done_task);
  1758. tasklet_kill(&aes_dd->queue_task);
  1759. atmel_aes_dma_cleanup(aes_dd);
  1760. atmel_aes_buff_cleanup(aes_dd);
  1761. clk_unprepare(aes_dd->iclk);
  1762. return 0;
  1763. }
  1764. static struct platform_driver atmel_aes_driver = {
  1765. .probe = atmel_aes_probe,
  1766. .remove = atmel_aes_remove,
  1767. .driver = {
  1768. .name = "atmel_aes",
  1769. .of_match_table = of_match_ptr(atmel_aes_dt_ids),
  1770. },
  1771. };
  1772. module_platform_driver(atmel_aes_driver);
  1773. MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
  1774. MODULE_LICENSE("GPL v2");
  1775. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");