stm32-cryp.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2017
  3. * Author: Fabien Dessenne <fabien.dessenne@st.com>
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset.h>
  14. #include <crypto/aes.h>
  15. #include <crypto/des.h>
  16. #include <crypto/engine.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <crypto/internal/aead.h>
  19. #define DRIVER_NAME "stm32-cryp"
  20. /* Bit [0] encrypt / decrypt */
  21. #define FLG_ENCRYPT BIT(0)
  22. /* Bit [8..1] algo & operation mode */
  23. #define FLG_AES BIT(1)
  24. #define FLG_DES BIT(2)
  25. #define FLG_TDES BIT(3)
  26. #define FLG_ECB BIT(4)
  27. #define FLG_CBC BIT(5)
  28. #define FLG_CTR BIT(6)
  29. #define FLG_GCM BIT(7)
  30. #define FLG_CCM BIT(8)
  31. /* Mode mask = bits [15..0] */
  32. #define FLG_MODE_MASK GENMASK(15, 0)
  33. /* Bit [31..16] status */
  34. #define FLG_CCM_PADDED_WA BIT(16)
  35. /* Registers */
  36. #define CRYP_CR 0x00000000
  37. #define CRYP_SR 0x00000004
  38. #define CRYP_DIN 0x00000008
  39. #define CRYP_DOUT 0x0000000C
  40. #define CRYP_DMACR 0x00000010
  41. #define CRYP_IMSCR 0x00000014
  42. #define CRYP_RISR 0x00000018
  43. #define CRYP_MISR 0x0000001C
  44. #define CRYP_K0LR 0x00000020
  45. #define CRYP_K0RR 0x00000024
  46. #define CRYP_K1LR 0x00000028
  47. #define CRYP_K1RR 0x0000002C
  48. #define CRYP_K2LR 0x00000030
  49. #define CRYP_K2RR 0x00000034
  50. #define CRYP_K3LR 0x00000038
  51. #define CRYP_K3RR 0x0000003C
  52. #define CRYP_IV0LR 0x00000040
  53. #define CRYP_IV0RR 0x00000044
  54. #define CRYP_IV1LR 0x00000048
  55. #define CRYP_IV1RR 0x0000004C
  56. #define CRYP_CSGCMCCM0R 0x00000050
  57. #define CRYP_CSGCM0R 0x00000070
  58. /* Registers values */
  59. #define CR_DEC_NOT_ENC 0x00000004
  60. #define CR_TDES_ECB 0x00000000
  61. #define CR_TDES_CBC 0x00000008
  62. #define CR_DES_ECB 0x00000010
  63. #define CR_DES_CBC 0x00000018
  64. #define CR_AES_ECB 0x00000020
  65. #define CR_AES_CBC 0x00000028
  66. #define CR_AES_CTR 0x00000030
  67. #define CR_AES_KP 0x00000038
  68. #define CR_AES_GCM 0x00080000
  69. #define CR_AES_CCM 0x00080008
  70. #define CR_AES_UNKNOWN 0xFFFFFFFF
  71. #define CR_ALGO_MASK 0x00080038
  72. #define CR_DATA32 0x00000000
  73. #define CR_DATA16 0x00000040
  74. #define CR_DATA8 0x00000080
  75. #define CR_DATA1 0x000000C0
  76. #define CR_KEY128 0x00000000
  77. #define CR_KEY192 0x00000100
  78. #define CR_KEY256 0x00000200
  79. #define CR_FFLUSH 0x00004000
  80. #define CR_CRYPEN 0x00008000
  81. #define CR_PH_INIT 0x00000000
  82. #define CR_PH_HEADER 0x00010000
  83. #define CR_PH_PAYLOAD 0x00020000
  84. #define CR_PH_FINAL 0x00030000
  85. #define CR_PH_MASK 0x00030000
  86. #define CR_NBPBL_SHIFT 20
  87. #define SR_BUSY 0x00000010
  88. #define SR_OFNE 0x00000004
  89. #define IMSCR_IN BIT(0)
  90. #define IMSCR_OUT BIT(1)
  91. #define MISR_IN BIT(0)
  92. #define MISR_OUT BIT(1)
  93. /* Misc */
  94. #define AES_BLOCK_32 (AES_BLOCK_SIZE / sizeof(u32))
  95. #define GCM_CTR_INIT 2
  96. #define _walked_in (cryp->in_walk.offset - cryp->in_sg->offset)
  97. #define _walked_out (cryp->out_walk.offset - cryp->out_sg->offset)
  98. struct stm32_cryp_caps {
  99. bool swap_final;
  100. bool padding_wa;
  101. };
  102. struct stm32_cryp_ctx {
  103. struct crypto_engine_ctx enginectx;
  104. struct stm32_cryp *cryp;
  105. int keylen;
  106. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  107. unsigned long flags;
  108. };
  109. struct stm32_cryp_reqctx {
  110. unsigned long mode;
  111. };
  112. struct stm32_cryp {
  113. struct list_head list;
  114. struct device *dev;
  115. void __iomem *regs;
  116. struct clk *clk;
  117. unsigned long flags;
  118. u32 irq_status;
  119. const struct stm32_cryp_caps *caps;
  120. struct stm32_cryp_ctx *ctx;
  121. struct crypto_engine *engine;
  122. struct mutex lock; /* protects req / areq */
  123. struct ablkcipher_request *req;
  124. struct aead_request *areq;
  125. size_t authsize;
  126. size_t hw_blocksize;
  127. size_t total_in;
  128. size_t total_in_save;
  129. size_t total_out;
  130. size_t total_out_save;
  131. struct scatterlist *in_sg;
  132. struct scatterlist *out_sg;
  133. struct scatterlist *out_sg_save;
  134. struct scatterlist in_sgl;
  135. struct scatterlist out_sgl;
  136. bool sgs_copied;
  137. int in_sg_len;
  138. int out_sg_len;
  139. struct scatter_walk in_walk;
  140. struct scatter_walk out_walk;
  141. u32 last_ctr[4];
  142. u32 gcm_ctr;
  143. };
  144. struct stm32_cryp_list {
  145. struct list_head dev_list;
  146. spinlock_t lock; /* protect dev_list */
  147. };
  148. static struct stm32_cryp_list cryp_list = {
  149. .dev_list = LIST_HEAD_INIT(cryp_list.dev_list),
  150. .lock = __SPIN_LOCK_UNLOCKED(cryp_list.lock),
  151. };
  152. static inline bool is_aes(struct stm32_cryp *cryp)
  153. {
  154. return cryp->flags & FLG_AES;
  155. }
  156. static inline bool is_des(struct stm32_cryp *cryp)
  157. {
  158. return cryp->flags & FLG_DES;
  159. }
  160. static inline bool is_tdes(struct stm32_cryp *cryp)
  161. {
  162. return cryp->flags & FLG_TDES;
  163. }
  164. static inline bool is_ecb(struct stm32_cryp *cryp)
  165. {
  166. return cryp->flags & FLG_ECB;
  167. }
  168. static inline bool is_cbc(struct stm32_cryp *cryp)
  169. {
  170. return cryp->flags & FLG_CBC;
  171. }
  172. static inline bool is_ctr(struct stm32_cryp *cryp)
  173. {
  174. return cryp->flags & FLG_CTR;
  175. }
  176. static inline bool is_gcm(struct stm32_cryp *cryp)
  177. {
  178. return cryp->flags & FLG_GCM;
  179. }
  180. static inline bool is_ccm(struct stm32_cryp *cryp)
  181. {
  182. return cryp->flags & FLG_CCM;
  183. }
  184. static inline bool is_encrypt(struct stm32_cryp *cryp)
  185. {
  186. return cryp->flags & FLG_ENCRYPT;
  187. }
  188. static inline bool is_decrypt(struct stm32_cryp *cryp)
  189. {
  190. return !is_encrypt(cryp);
  191. }
  192. static inline u32 stm32_cryp_read(struct stm32_cryp *cryp, u32 ofst)
  193. {
  194. return readl_relaxed(cryp->regs + ofst);
  195. }
  196. static inline void stm32_cryp_write(struct stm32_cryp *cryp, u32 ofst, u32 val)
  197. {
  198. writel_relaxed(val, cryp->regs + ofst);
  199. }
  200. static inline int stm32_cryp_wait_busy(struct stm32_cryp *cryp)
  201. {
  202. u32 status;
  203. return readl_relaxed_poll_timeout(cryp->regs + CRYP_SR, status,
  204. !(status & SR_BUSY), 10, 100000);
  205. }
  206. static inline int stm32_cryp_wait_enable(struct stm32_cryp *cryp)
  207. {
  208. u32 status;
  209. return readl_relaxed_poll_timeout(cryp->regs + CRYP_CR, status,
  210. !(status & CR_CRYPEN), 10, 100000);
  211. }
  212. static inline int stm32_cryp_wait_output(struct stm32_cryp *cryp)
  213. {
  214. u32 status;
  215. return readl_relaxed_poll_timeout(cryp->regs + CRYP_SR, status,
  216. status & SR_OFNE, 10, 100000);
  217. }
  218. static int stm32_cryp_read_auth_tag(struct stm32_cryp *cryp);
  219. static struct stm32_cryp *stm32_cryp_find_dev(struct stm32_cryp_ctx *ctx)
  220. {
  221. struct stm32_cryp *tmp, *cryp = NULL;
  222. spin_lock_bh(&cryp_list.lock);
  223. if (!ctx->cryp) {
  224. list_for_each_entry(tmp, &cryp_list.dev_list, list) {
  225. cryp = tmp;
  226. break;
  227. }
  228. ctx->cryp = cryp;
  229. } else {
  230. cryp = ctx->cryp;
  231. }
  232. spin_unlock_bh(&cryp_list.lock);
  233. return cryp;
  234. }
  235. static int stm32_cryp_check_aligned(struct scatterlist *sg, size_t total,
  236. size_t align)
  237. {
  238. int len = 0;
  239. if (!total)
  240. return 0;
  241. if (!IS_ALIGNED(total, align))
  242. return -EINVAL;
  243. while (sg) {
  244. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  245. return -EINVAL;
  246. if (!IS_ALIGNED(sg->length, align))
  247. return -EINVAL;
  248. len += sg->length;
  249. sg = sg_next(sg);
  250. }
  251. if (len != total)
  252. return -EINVAL;
  253. return 0;
  254. }
  255. static int stm32_cryp_check_io_aligned(struct stm32_cryp *cryp)
  256. {
  257. int ret;
  258. ret = stm32_cryp_check_aligned(cryp->in_sg, cryp->total_in,
  259. cryp->hw_blocksize);
  260. if (ret)
  261. return ret;
  262. ret = stm32_cryp_check_aligned(cryp->out_sg, cryp->total_out,
  263. cryp->hw_blocksize);
  264. return ret;
  265. }
  266. static void sg_copy_buf(void *buf, struct scatterlist *sg,
  267. unsigned int start, unsigned int nbytes, int out)
  268. {
  269. struct scatter_walk walk;
  270. if (!nbytes)
  271. return;
  272. scatterwalk_start(&walk, sg);
  273. scatterwalk_advance(&walk, start);
  274. scatterwalk_copychunks(buf, &walk, nbytes, out);
  275. scatterwalk_done(&walk, out, 0);
  276. }
  277. static int stm32_cryp_copy_sgs(struct stm32_cryp *cryp)
  278. {
  279. void *buf_in, *buf_out;
  280. int pages, total_in, total_out;
  281. if (!stm32_cryp_check_io_aligned(cryp)) {
  282. cryp->sgs_copied = 0;
  283. return 0;
  284. }
  285. total_in = ALIGN(cryp->total_in, cryp->hw_blocksize);
  286. pages = total_in ? get_order(total_in) : 1;
  287. buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages);
  288. total_out = ALIGN(cryp->total_out, cryp->hw_blocksize);
  289. pages = total_out ? get_order(total_out) : 1;
  290. buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages);
  291. if (!buf_in || !buf_out) {
  292. dev_err(cryp->dev, "Can't allocate pages when unaligned\n");
  293. cryp->sgs_copied = 0;
  294. return -EFAULT;
  295. }
  296. sg_copy_buf(buf_in, cryp->in_sg, 0, cryp->total_in, 0);
  297. sg_init_one(&cryp->in_sgl, buf_in, total_in);
  298. cryp->in_sg = &cryp->in_sgl;
  299. cryp->in_sg_len = 1;
  300. sg_init_one(&cryp->out_sgl, buf_out, total_out);
  301. cryp->out_sg_save = cryp->out_sg;
  302. cryp->out_sg = &cryp->out_sgl;
  303. cryp->out_sg_len = 1;
  304. cryp->sgs_copied = 1;
  305. return 0;
  306. }
  307. static void stm32_cryp_hw_write_iv(struct stm32_cryp *cryp, u32 *iv)
  308. {
  309. if (!iv)
  310. return;
  311. stm32_cryp_write(cryp, CRYP_IV0LR, cpu_to_be32(*iv++));
  312. stm32_cryp_write(cryp, CRYP_IV0RR, cpu_to_be32(*iv++));
  313. if (is_aes(cryp)) {
  314. stm32_cryp_write(cryp, CRYP_IV1LR, cpu_to_be32(*iv++));
  315. stm32_cryp_write(cryp, CRYP_IV1RR, cpu_to_be32(*iv++));
  316. }
  317. }
  318. static void stm32_cryp_hw_write_key(struct stm32_cryp *c)
  319. {
  320. unsigned int i;
  321. int r_id;
  322. if (is_des(c)) {
  323. stm32_cryp_write(c, CRYP_K1LR, cpu_to_be32(c->ctx->key[0]));
  324. stm32_cryp_write(c, CRYP_K1RR, cpu_to_be32(c->ctx->key[1]));
  325. } else {
  326. r_id = CRYP_K3RR;
  327. for (i = c->ctx->keylen / sizeof(u32); i > 0; i--, r_id -= 4)
  328. stm32_cryp_write(c, r_id,
  329. cpu_to_be32(c->ctx->key[i - 1]));
  330. }
  331. }
  332. static u32 stm32_cryp_get_hw_mode(struct stm32_cryp *cryp)
  333. {
  334. if (is_aes(cryp) && is_ecb(cryp))
  335. return CR_AES_ECB;
  336. if (is_aes(cryp) && is_cbc(cryp))
  337. return CR_AES_CBC;
  338. if (is_aes(cryp) && is_ctr(cryp))
  339. return CR_AES_CTR;
  340. if (is_aes(cryp) && is_gcm(cryp))
  341. return CR_AES_GCM;
  342. if (is_aes(cryp) && is_ccm(cryp))
  343. return CR_AES_CCM;
  344. if (is_des(cryp) && is_ecb(cryp))
  345. return CR_DES_ECB;
  346. if (is_des(cryp) && is_cbc(cryp))
  347. return CR_DES_CBC;
  348. if (is_tdes(cryp) && is_ecb(cryp))
  349. return CR_TDES_ECB;
  350. if (is_tdes(cryp) && is_cbc(cryp))
  351. return CR_TDES_CBC;
  352. dev_err(cryp->dev, "Unknown mode\n");
  353. return CR_AES_UNKNOWN;
  354. }
  355. static unsigned int stm32_cryp_get_input_text_len(struct stm32_cryp *cryp)
  356. {
  357. return is_encrypt(cryp) ? cryp->areq->cryptlen :
  358. cryp->areq->cryptlen - cryp->authsize;
  359. }
  360. static int stm32_cryp_gcm_init(struct stm32_cryp *cryp, u32 cfg)
  361. {
  362. int ret;
  363. u32 iv[4];
  364. /* Phase 1 : init */
  365. memcpy(iv, cryp->areq->iv, 12);
  366. iv[3] = cpu_to_be32(GCM_CTR_INIT);
  367. cryp->gcm_ctr = GCM_CTR_INIT;
  368. stm32_cryp_hw_write_iv(cryp, iv);
  369. stm32_cryp_write(cryp, CRYP_CR, cfg | CR_PH_INIT | CR_CRYPEN);
  370. /* Wait for end of processing */
  371. ret = stm32_cryp_wait_enable(cryp);
  372. if (ret)
  373. dev_err(cryp->dev, "Timeout (gcm init)\n");
  374. return ret;
  375. }
  376. static int stm32_cryp_ccm_init(struct stm32_cryp *cryp, u32 cfg)
  377. {
  378. int ret;
  379. u8 iv[AES_BLOCK_SIZE], b0[AES_BLOCK_SIZE];
  380. u32 *d;
  381. unsigned int i, textlen;
  382. /* Phase 1 : init. Firstly set the CTR value to 1 (not 0) */
  383. memcpy(iv, cryp->areq->iv, AES_BLOCK_SIZE);
  384. memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
  385. iv[AES_BLOCK_SIZE - 1] = 1;
  386. stm32_cryp_hw_write_iv(cryp, (u32 *)iv);
  387. /* Build B0 */
  388. memcpy(b0, iv, AES_BLOCK_SIZE);
  389. b0[0] |= (8 * ((cryp->authsize - 2) / 2));
  390. if (cryp->areq->assoclen)
  391. b0[0] |= 0x40;
  392. textlen = stm32_cryp_get_input_text_len(cryp);
  393. b0[AES_BLOCK_SIZE - 2] = textlen >> 8;
  394. b0[AES_BLOCK_SIZE - 1] = textlen & 0xFF;
  395. /* Enable HW */
  396. stm32_cryp_write(cryp, CRYP_CR, cfg | CR_PH_INIT | CR_CRYPEN);
  397. /* Write B0 */
  398. d = (u32 *)b0;
  399. for (i = 0; i < AES_BLOCK_32; i++) {
  400. if (!cryp->caps->padding_wa)
  401. *d = cpu_to_be32(*d);
  402. stm32_cryp_write(cryp, CRYP_DIN, *d++);
  403. }
  404. /* Wait for end of processing */
  405. ret = stm32_cryp_wait_enable(cryp);
  406. if (ret)
  407. dev_err(cryp->dev, "Timeout (ccm init)\n");
  408. return ret;
  409. }
  410. static int stm32_cryp_hw_init(struct stm32_cryp *cryp)
  411. {
  412. int ret;
  413. u32 cfg, hw_mode;
  414. /* Disable interrupt */
  415. stm32_cryp_write(cryp, CRYP_IMSCR, 0);
  416. /* Set key */
  417. stm32_cryp_hw_write_key(cryp);
  418. /* Set configuration */
  419. cfg = CR_DATA8 | CR_FFLUSH;
  420. switch (cryp->ctx->keylen) {
  421. case AES_KEYSIZE_128:
  422. cfg |= CR_KEY128;
  423. break;
  424. case AES_KEYSIZE_192:
  425. cfg |= CR_KEY192;
  426. break;
  427. default:
  428. case AES_KEYSIZE_256:
  429. cfg |= CR_KEY256;
  430. break;
  431. }
  432. hw_mode = stm32_cryp_get_hw_mode(cryp);
  433. if (hw_mode == CR_AES_UNKNOWN)
  434. return -EINVAL;
  435. /* AES ECB/CBC decrypt: run key preparation first */
  436. if (is_decrypt(cryp) &&
  437. ((hw_mode == CR_AES_ECB) || (hw_mode == CR_AES_CBC))) {
  438. stm32_cryp_write(cryp, CRYP_CR, cfg | CR_AES_KP | CR_CRYPEN);
  439. /* Wait for end of processing */
  440. ret = stm32_cryp_wait_busy(cryp);
  441. if (ret) {
  442. dev_err(cryp->dev, "Timeout (key preparation)\n");
  443. return ret;
  444. }
  445. }
  446. cfg |= hw_mode;
  447. if (is_decrypt(cryp))
  448. cfg |= CR_DEC_NOT_ENC;
  449. /* Apply config and flush (valid when CRYPEN = 0) */
  450. stm32_cryp_write(cryp, CRYP_CR, cfg);
  451. switch (hw_mode) {
  452. case CR_AES_GCM:
  453. case CR_AES_CCM:
  454. /* Phase 1 : init */
  455. if (hw_mode == CR_AES_CCM)
  456. ret = stm32_cryp_ccm_init(cryp, cfg);
  457. else
  458. ret = stm32_cryp_gcm_init(cryp, cfg);
  459. if (ret)
  460. return ret;
  461. /* Phase 2 : header (authenticated data) */
  462. if (cryp->areq->assoclen) {
  463. cfg |= CR_PH_HEADER;
  464. } else if (stm32_cryp_get_input_text_len(cryp)) {
  465. cfg |= CR_PH_PAYLOAD;
  466. stm32_cryp_write(cryp, CRYP_CR, cfg);
  467. } else {
  468. cfg |= CR_PH_INIT;
  469. }
  470. break;
  471. case CR_DES_CBC:
  472. case CR_TDES_CBC:
  473. case CR_AES_CBC:
  474. case CR_AES_CTR:
  475. stm32_cryp_hw_write_iv(cryp, (u32 *)cryp->req->info);
  476. break;
  477. default:
  478. break;
  479. }
  480. /* Enable now */
  481. cfg |= CR_CRYPEN;
  482. stm32_cryp_write(cryp, CRYP_CR, cfg);
  483. cryp->flags &= ~FLG_CCM_PADDED_WA;
  484. return 0;
  485. }
  486. static void stm32_cryp_finish_req(struct stm32_cryp *cryp, int err)
  487. {
  488. if (!err && (is_gcm(cryp) || is_ccm(cryp)))
  489. /* Phase 4 : output tag */
  490. err = stm32_cryp_read_auth_tag(cryp);
  491. if (cryp->sgs_copied) {
  492. void *buf_in, *buf_out;
  493. int pages, len;
  494. buf_in = sg_virt(&cryp->in_sgl);
  495. buf_out = sg_virt(&cryp->out_sgl);
  496. sg_copy_buf(buf_out, cryp->out_sg_save, 0,
  497. cryp->total_out_save, 1);
  498. len = ALIGN(cryp->total_in_save, cryp->hw_blocksize);
  499. pages = len ? get_order(len) : 1;
  500. free_pages((unsigned long)buf_in, pages);
  501. len = ALIGN(cryp->total_out_save, cryp->hw_blocksize);
  502. pages = len ? get_order(len) : 1;
  503. free_pages((unsigned long)buf_out, pages);
  504. }
  505. if (is_gcm(cryp) || is_ccm(cryp)) {
  506. crypto_finalize_aead_request(cryp->engine, cryp->areq, err);
  507. cryp->areq = NULL;
  508. } else {
  509. crypto_finalize_ablkcipher_request(cryp->engine, cryp->req,
  510. err);
  511. cryp->req = NULL;
  512. }
  513. memset(cryp->ctx->key, 0, cryp->ctx->keylen);
  514. mutex_unlock(&cryp->lock);
  515. }
  516. static int stm32_cryp_cpu_start(struct stm32_cryp *cryp)
  517. {
  518. /* Enable interrupt and let the IRQ handler do everything */
  519. stm32_cryp_write(cryp, CRYP_IMSCR, IMSCR_IN | IMSCR_OUT);
  520. return 0;
  521. }
  522. static int stm32_cryp_cipher_one_req(struct crypto_engine *engine, void *areq);
  523. static int stm32_cryp_prepare_cipher_req(struct crypto_engine *engine,
  524. void *areq);
  525. static int stm32_cryp_cra_init(struct crypto_tfm *tfm)
  526. {
  527. struct stm32_cryp_ctx *ctx = crypto_tfm_ctx(tfm);
  528. tfm->crt_ablkcipher.reqsize = sizeof(struct stm32_cryp_reqctx);
  529. ctx->enginectx.op.do_one_request = stm32_cryp_cipher_one_req;
  530. ctx->enginectx.op.prepare_request = stm32_cryp_prepare_cipher_req;
  531. ctx->enginectx.op.unprepare_request = NULL;
  532. return 0;
  533. }
  534. static int stm32_cryp_aead_one_req(struct crypto_engine *engine, void *areq);
  535. static int stm32_cryp_prepare_aead_req(struct crypto_engine *engine,
  536. void *areq);
  537. static int stm32_cryp_aes_aead_init(struct crypto_aead *tfm)
  538. {
  539. struct stm32_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  540. tfm->reqsize = sizeof(struct stm32_cryp_reqctx);
  541. ctx->enginectx.op.do_one_request = stm32_cryp_aead_one_req;
  542. ctx->enginectx.op.prepare_request = stm32_cryp_prepare_aead_req;
  543. ctx->enginectx.op.unprepare_request = NULL;
  544. return 0;
  545. }
  546. static int stm32_cryp_crypt(struct ablkcipher_request *req, unsigned long mode)
  547. {
  548. struct stm32_cryp_ctx *ctx = crypto_ablkcipher_ctx(
  549. crypto_ablkcipher_reqtfm(req));
  550. struct stm32_cryp_reqctx *rctx = ablkcipher_request_ctx(req);
  551. struct stm32_cryp *cryp = stm32_cryp_find_dev(ctx);
  552. if (!cryp)
  553. return -ENODEV;
  554. rctx->mode = mode;
  555. return crypto_transfer_ablkcipher_request_to_engine(cryp->engine, req);
  556. }
  557. static int stm32_cryp_aead_crypt(struct aead_request *req, unsigned long mode)
  558. {
  559. struct stm32_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  560. struct stm32_cryp_reqctx *rctx = aead_request_ctx(req);
  561. struct stm32_cryp *cryp = stm32_cryp_find_dev(ctx);
  562. if (!cryp)
  563. return -ENODEV;
  564. rctx->mode = mode;
  565. return crypto_transfer_aead_request_to_engine(cryp->engine, req);
  566. }
  567. static int stm32_cryp_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  568. unsigned int keylen)
  569. {
  570. struct stm32_cryp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  571. memcpy(ctx->key, key, keylen);
  572. ctx->keylen = keylen;
  573. return 0;
  574. }
  575. static int stm32_cryp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  576. unsigned int keylen)
  577. {
  578. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  579. keylen != AES_KEYSIZE_256)
  580. return -EINVAL;
  581. else
  582. return stm32_cryp_setkey(tfm, key, keylen);
  583. }
  584. static int stm32_cryp_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  585. unsigned int keylen)
  586. {
  587. if (keylen != DES_KEY_SIZE)
  588. return -EINVAL;
  589. else
  590. return stm32_cryp_setkey(tfm, key, keylen);
  591. }
  592. static int stm32_cryp_tdes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  593. unsigned int keylen)
  594. {
  595. if (keylen != (3 * DES_KEY_SIZE))
  596. return -EINVAL;
  597. else
  598. return stm32_cryp_setkey(tfm, key, keylen);
  599. }
  600. static int stm32_cryp_aes_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  601. unsigned int keylen)
  602. {
  603. struct stm32_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  604. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  605. keylen != AES_KEYSIZE_256)
  606. return -EINVAL;
  607. memcpy(ctx->key, key, keylen);
  608. ctx->keylen = keylen;
  609. return 0;
  610. }
  611. static int stm32_cryp_aes_gcm_setauthsize(struct crypto_aead *tfm,
  612. unsigned int authsize)
  613. {
  614. return authsize == AES_BLOCK_SIZE ? 0 : -EINVAL;
  615. }
  616. static int stm32_cryp_aes_ccm_setauthsize(struct crypto_aead *tfm,
  617. unsigned int authsize)
  618. {
  619. switch (authsize) {
  620. case 4:
  621. case 6:
  622. case 8:
  623. case 10:
  624. case 12:
  625. case 14:
  626. case 16:
  627. break;
  628. default:
  629. return -EINVAL;
  630. }
  631. return 0;
  632. }
  633. static int stm32_cryp_aes_ecb_encrypt(struct ablkcipher_request *req)
  634. {
  635. return stm32_cryp_crypt(req, FLG_AES | FLG_ECB | FLG_ENCRYPT);
  636. }
  637. static int stm32_cryp_aes_ecb_decrypt(struct ablkcipher_request *req)
  638. {
  639. return stm32_cryp_crypt(req, FLG_AES | FLG_ECB);
  640. }
  641. static int stm32_cryp_aes_cbc_encrypt(struct ablkcipher_request *req)
  642. {
  643. return stm32_cryp_crypt(req, FLG_AES | FLG_CBC | FLG_ENCRYPT);
  644. }
  645. static int stm32_cryp_aes_cbc_decrypt(struct ablkcipher_request *req)
  646. {
  647. return stm32_cryp_crypt(req, FLG_AES | FLG_CBC);
  648. }
  649. static int stm32_cryp_aes_ctr_encrypt(struct ablkcipher_request *req)
  650. {
  651. return stm32_cryp_crypt(req, FLG_AES | FLG_CTR | FLG_ENCRYPT);
  652. }
  653. static int stm32_cryp_aes_ctr_decrypt(struct ablkcipher_request *req)
  654. {
  655. return stm32_cryp_crypt(req, FLG_AES | FLG_CTR);
  656. }
  657. static int stm32_cryp_aes_gcm_encrypt(struct aead_request *req)
  658. {
  659. return stm32_cryp_aead_crypt(req, FLG_AES | FLG_GCM | FLG_ENCRYPT);
  660. }
  661. static int stm32_cryp_aes_gcm_decrypt(struct aead_request *req)
  662. {
  663. return stm32_cryp_aead_crypt(req, FLG_AES | FLG_GCM);
  664. }
  665. static int stm32_cryp_aes_ccm_encrypt(struct aead_request *req)
  666. {
  667. return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM | FLG_ENCRYPT);
  668. }
  669. static int stm32_cryp_aes_ccm_decrypt(struct aead_request *req)
  670. {
  671. return stm32_cryp_aead_crypt(req, FLG_AES | FLG_CCM);
  672. }
  673. static int stm32_cryp_des_ecb_encrypt(struct ablkcipher_request *req)
  674. {
  675. return stm32_cryp_crypt(req, FLG_DES | FLG_ECB | FLG_ENCRYPT);
  676. }
  677. static int stm32_cryp_des_ecb_decrypt(struct ablkcipher_request *req)
  678. {
  679. return stm32_cryp_crypt(req, FLG_DES | FLG_ECB);
  680. }
  681. static int stm32_cryp_des_cbc_encrypt(struct ablkcipher_request *req)
  682. {
  683. return stm32_cryp_crypt(req, FLG_DES | FLG_CBC | FLG_ENCRYPT);
  684. }
  685. static int stm32_cryp_des_cbc_decrypt(struct ablkcipher_request *req)
  686. {
  687. return stm32_cryp_crypt(req, FLG_DES | FLG_CBC);
  688. }
  689. static int stm32_cryp_tdes_ecb_encrypt(struct ablkcipher_request *req)
  690. {
  691. return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB | FLG_ENCRYPT);
  692. }
  693. static int stm32_cryp_tdes_ecb_decrypt(struct ablkcipher_request *req)
  694. {
  695. return stm32_cryp_crypt(req, FLG_TDES | FLG_ECB);
  696. }
  697. static int stm32_cryp_tdes_cbc_encrypt(struct ablkcipher_request *req)
  698. {
  699. return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC | FLG_ENCRYPT);
  700. }
  701. static int stm32_cryp_tdes_cbc_decrypt(struct ablkcipher_request *req)
  702. {
  703. return stm32_cryp_crypt(req, FLG_TDES | FLG_CBC);
  704. }
  705. static int stm32_cryp_prepare_req(struct ablkcipher_request *req,
  706. struct aead_request *areq)
  707. {
  708. struct stm32_cryp_ctx *ctx;
  709. struct stm32_cryp *cryp;
  710. struct stm32_cryp_reqctx *rctx;
  711. int ret;
  712. if (!req && !areq)
  713. return -EINVAL;
  714. ctx = req ? crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)) :
  715. crypto_aead_ctx(crypto_aead_reqtfm(areq));
  716. cryp = ctx->cryp;
  717. if (!cryp)
  718. return -ENODEV;
  719. mutex_lock(&cryp->lock);
  720. rctx = req ? ablkcipher_request_ctx(req) : aead_request_ctx(areq);
  721. rctx->mode &= FLG_MODE_MASK;
  722. ctx->cryp = cryp;
  723. cryp->flags = (cryp->flags & ~FLG_MODE_MASK) | rctx->mode;
  724. cryp->hw_blocksize = is_aes(cryp) ? AES_BLOCK_SIZE : DES_BLOCK_SIZE;
  725. cryp->ctx = ctx;
  726. if (req) {
  727. cryp->req = req;
  728. cryp->total_in = req->nbytes;
  729. cryp->total_out = cryp->total_in;
  730. } else {
  731. /*
  732. * Length of input and output data:
  733. * Encryption case:
  734. * INPUT = AssocData || PlainText
  735. * <- assoclen -> <- cryptlen ->
  736. * <------- total_in ----------->
  737. *
  738. * OUTPUT = AssocData || CipherText || AuthTag
  739. * <- assoclen -> <- cryptlen -> <- authsize ->
  740. * <---------------- total_out ----------------->
  741. *
  742. * Decryption case:
  743. * INPUT = AssocData || CipherText || AuthTag
  744. * <- assoclen -> <--------- cryptlen --------->
  745. * <- authsize ->
  746. * <---------------- total_in ------------------>
  747. *
  748. * OUTPUT = AssocData || PlainText
  749. * <- assoclen -> <- crypten - authsize ->
  750. * <---------- total_out ----------------->
  751. */
  752. cryp->areq = areq;
  753. cryp->authsize = crypto_aead_authsize(crypto_aead_reqtfm(areq));
  754. cryp->total_in = areq->assoclen + areq->cryptlen;
  755. if (is_encrypt(cryp))
  756. /* Append auth tag to output */
  757. cryp->total_out = cryp->total_in + cryp->authsize;
  758. else
  759. /* No auth tag in output */
  760. cryp->total_out = cryp->total_in - cryp->authsize;
  761. }
  762. cryp->total_in_save = cryp->total_in;
  763. cryp->total_out_save = cryp->total_out;
  764. cryp->in_sg = req ? req->src : areq->src;
  765. cryp->out_sg = req ? req->dst : areq->dst;
  766. cryp->out_sg_save = cryp->out_sg;
  767. cryp->in_sg_len = sg_nents_for_len(cryp->in_sg, cryp->total_in);
  768. if (cryp->in_sg_len < 0) {
  769. dev_err(cryp->dev, "Cannot get in_sg_len\n");
  770. ret = cryp->in_sg_len;
  771. goto out;
  772. }
  773. cryp->out_sg_len = sg_nents_for_len(cryp->out_sg, cryp->total_out);
  774. if (cryp->out_sg_len < 0) {
  775. dev_err(cryp->dev, "Cannot get out_sg_len\n");
  776. ret = cryp->out_sg_len;
  777. goto out;
  778. }
  779. ret = stm32_cryp_copy_sgs(cryp);
  780. if (ret)
  781. goto out;
  782. scatterwalk_start(&cryp->in_walk, cryp->in_sg);
  783. scatterwalk_start(&cryp->out_walk, cryp->out_sg);
  784. if (is_gcm(cryp) || is_ccm(cryp)) {
  785. /* In output, jump after assoc data */
  786. scatterwalk_advance(&cryp->out_walk, cryp->areq->assoclen);
  787. cryp->total_out -= cryp->areq->assoclen;
  788. }
  789. ret = stm32_cryp_hw_init(cryp);
  790. out:
  791. if (ret)
  792. mutex_unlock(&cryp->lock);
  793. return ret;
  794. }
  795. static int stm32_cryp_prepare_cipher_req(struct crypto_engine *engine,
  796. void *areq)
  797. {
  798. struct ablkcipher_request *req = container_of(areq,
  799. struct ablkcipher_request,
  800. base);
  801. return stm32_cryp_prepare_req(req, NULL);
  802. }
  803. static int stm32_cryp_cipher_one_req(struct crypto_engine *engine, void *areq)
  804. {
  805. struct ablkcipher_request *req = container_of(areq,
  806. struct ablkcipher_request,
  807. base);
  808. struct stm32_cryp_ctx *ctx = crypto_ablkcipher_ctx(
  809. crypto_ablkcipher_reqtfm(req));
  810. struct stm32_cryp *cryp = ctx->cryp;
  811. if (!cryp)
  812. return -ENODEV;
  813. return stm32_cryp_cpu_start(cryp);
  814. }
  815. static int stm32_cryp_prepare_aead_req(struct crypto_engine *engine, void *areq)
  816. {
  817. struct aead_request *req = container_of(areq, struct aead_request,
  818. base);
  819. return stm32_cryp_prepare_req(NULL, req);
  820. }
  821. static int stm32_cryp_aead_one_req(struct crypto_engine *engine, void *areq)
  822. {
  823. struct aead_request *req = container_of(areq, struct aead_request,
  824. base);
  825. struct stm32_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  826. struct stm32_cryp *cryp = ctx->cryp;
  827. if (!cryp)
  828. return -ENODEV;
  829. if (unlikely(!cryp->areq->assoclen &&
  830. !stm32_cryp_get_input_text_len(cryp))) {
  831. /* No input data to process: get tag and finish */
  832. stm32_cryp_finish_req(cryp, 0);
  833. return 0;
  834. }
  835. return stm32_cryp_cpu_start(cryp);
  836. }
  837. static u32 *stm32_cryp_next_out(struct stm32_cryp *cryp, u32 *dst,
  838. unsigned int n)
  839. {
  840. scatterwalk_advance(&cryp->out_walk, n);
  841. if (unlikely(cryp->out_sg->length == _walked_out)) {
  842. cryp->out_sg = sg_next(cryp->out_sg);
  843. if (cryp->out_sg) {
  844. scatterwalk_start(&cryp->out_walk, cryp->out_sg);
  845. return (sg_virt(cryp->out_sg) + _walked_out);
  846. }
  847. }
  848. return (u32 *)((u8 *)dst + n);
  849. }
  850. static u32 *stm32_cryp_next_in(struct stm32_cryp *cryp, u32 *src,
  851. unsigned int n)
  852. {
  853. scatterwalk_advance(&cryp->in_walk, n);
  854. if (unlikely(cryp->in_sg->length == _walked_in)) {
  855. cryp->in_sg = sg_next(cryp->in_sg);
  856. if (cryp->in_sg) {
  857. scatterwalk_start(&cryp->in_walk, cryp->in_sg);
  858. return (sg_virt(cryp->in_sg) + _walked_in);
  859. }
  860. }
  861. return (u32 *)((u8 *)src + n);
  862. }
  863. static int stm32_cryp_read_auth_tag(struct stm32_cryp *cryp)
  864. {
  865. u32 cfg, size_bit, *dst, d32;
  866. u8 *d8;
  867. unsigned int i, j;
  868. int ret = 0;
  869. /* Update Config */
  870. cfg = stm32_cryp_read(cryp, CRYP_CR);
  871. cfg &= ~CR_PH_MASK;
  872. cfg |= CR_PH_FINAL;
  873. cfg &= ~CR_DEC_NOT_ENC;
  874. cfg |= CR_CRYPEN;
  875. stm32_cryp_write(cryp, CRYP_CR, cfg);
  876. if (is_gcm(cryp)) {
  877. /* GCM: write aad and payload size (in bits) */
  878. size_bit = cryp->areq->assoclen * 8;
  879. if (cryp->caps->swap_final)
  880. size_bit = cpu_to_be32(size_bit);
  881. stm32_cryp_write(cryp, CRYP_DIN, 0);
  882. stm32_cryp_write(cryp, CRYP_DIN, size_bit);
  883. size_bit = is_encrypt(cryp) ? cryp->areq->cryptlen :
  884. cryp->areq->cryptlen - AES_BLOCK_SIZE;
  885. size_bit *= 8;
  886. if (cryp->caps->swap_final)
  887. size_bit = cpu_to_be32(size_bit);
  888. stm32_cryp_write(cryp, CRYP_DIN, 0);
  889. stm32_cryp_write(cryp, CRYP_DIN, size_bit);
  890. } else {
  891. /* CCM: write CTR0 */
  892. u8 iv[AES_BLOCK_SIZE];
  893. u32 *iv32 = (u32 *)iv;
  894. memcpy(iv, cryp->areq->iv, AES_BLOCK_SIZE);
  895. memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
  896. for (i = 0; i < AES_BLOCK_32; i++) {
  897. if (!cryp->caps->padding_wa)
  898. *iv32 = cpu_to_be32(*iv32);
  899. stm32_cryp_write(cryp, CRYP_DIN, *iv32++);
  900. }
  901. }
  902. /* Wait for output data */
  903. ret = stm32_cryp_wait_output(cryp);
  904. if (ret) {
  905. dev_err(cryp->dev, "Timeout (read tag)\n");
  906. return ret;
  907. }
  908. if (is_encrypt(cryp)) {
  909. /* Get and write tag */
  910. dst = sg_virt(cryp->out_sg) + _walked_out;
  911. for (i = 0; i < AES_BLOCK_32; i++) {
  912. if (cryp->total_out >= sizeof(u32)) {
  913. /* Read a full u32 */
  914. *dst = stm32_cryp_read(cryp, CRYP_DOUT);
  915. dst = stm32_cryp_next_out(cryp, dst,
  916. sizeof(u32));
  917. cryp->total_out -= sizeof(u32);
  918. } else if (!cryp->total_out) {
  919. /* Empty fifo out (data from input padding) */
  920. stm32_cryp_read(cryp, CRYP_DOUT);
  921. } else {
  922. /* Read less than an u32 */
  923. d32 = stm32_cryp_read(cryp, CRYP_DOUT);
  924. d8 = (u8 *)&d32;
  925. for (j = 0; j < cryp->total_out; j++) {
  926. *((u8 *)dst) = *(d8++);
  927. dst = stm32_cryp_next_out(cryp, dst, 1);
  928. }
  929. cryp->total_out = 0;
  930. }
  931. }
  932. } else {
  933. /* Get and check tag */
  934. u32 in_tag[AES_BLOCK_32], out_tag[AES_BLOCK_32];
  935. scatterwalk_map_and_copy(in_tag, cryp->in_sg,
  936. cryp->total_in_save - cryp->authsize,
  937. cryp->authsize, 0);
  938. for (i = 0; i < AES_BLOCK_32; i++)
  939. out_tag[i] = stm32_cryp_read(cryp, CRYP_DOUT);
  940. if (crypto_memneq(in_tag, out_tag, cryp->authsize))
  941. ret = -EBADMSG;
  942. }
  943. /* Disable cryp */
  944. cfg &= ~CR_CRYPEN;
  945. stm32_cryp_write(cryp, CRYP_CR, cfg);
  946. return ret;
  947. }
  948. static void stm32_cryp_check_ctr_counter(struct stm32_cryp *cryp)
  949. {
  950. u32 cr;
  951. if (unlikely(cryp->last_ctr[3] == 0xFFFFFFFF)) {
  952. cryp->last_ctr[3] = 0;
  953. cryp->last_ctr[2]++;
  954. if (!cryp->last_ctr[2]) {
  955. cryp->last_ctr[1]++;
  956. if (!cryp->last_ctr[1])
  957. cryp->last_ctr[0]++;
  958. }
  959. cr = stm32_cryp_read(cryp, CRYP_CR);
  960. stm32_cryp_write(cryp, CRYP_CR, cr & ~CR_CRYPEN);
  961. stm32_cryp_hw_write_iv(cryp, (u32 *)cryp->last_ctr);
  962. stm32_cryp_write(cryp, CRYP_CR, cr);
  963. }
  964. cryp->last_ctr[0] = stm32_cryp_read(cryp, CRYP_IV0LR);
  965. cryp->last_ctr[1] = stm32_cryp_read(cryp, CRYP_IV0RR);
  966. cryp->last_ctr[2] = stm32_cryp_read(cryp, CRYP_IV1LR);
  967. cryp->last_ctr[3] = stm32_cryp_read(cryp, CRYP_IV1RR);
  968. }
  969. static bool stm32_cryp_irq_read_data(struct stm32_cryp *cryp)
  970. {
  971. unsigned int i, j;
  972. u32 d32, *dst;
  973. u8 *d8;
  974. size_t tag_size;
  975. /* Do no read tag now (if any) */
  976. if (is_encrypt(cryp) && (is_gcm(cryp) || is_ccm(cryp)))
  977. tag_size = cryp->authsize;
  978. else
  979. tag_size = 0;
  980. dst = sg_virt(cryp->out_sg) + _walked_out;
  981. for (i = 0; i < cryp->hw_blocksize / sizeof(u32); i++) {
  982. if (likely(cryp->total_out - tag_size >= sizeof(u32))) {
  983. /* Read a full u32 */
  984. *dst = stm32_cryp_read(cryp, CRYP_DOUT);
  985. dst = stm32_cryp_next_out(cryp, dst, sizeof(u32));
  986. cryp->total_out -= sizeof(u32);
  987. } else if (cryp->total_out == tag_size) {
  988. /* Empty fifo out (data from input padding) */
  989. d32 = stm32_cryp_read(cryp, CRYP_DOUT);
  990. } else {
  991. /* Read less than an u32 */
  992. d32 = stm32_cryp_read(cryp, CRYP_DOUT);
  993. d8 = (u8 *)&d32;
  994. for (j = 0; j < cryp->total_out - tag_size; j++) {
  995. *((u8 *)dst) = *(d8++);
  996. dst = stm32_cryp_next_out(cryp, dst, 1);
  997. }
  998. cryp->total_out = tag_size;
  999. }
  1000. }
  1001. return !(cryp->total_out - tag_size) || !cryp->total_in;
  1002. }
  1003. static void stm32_cryp_irq_write_block(struct stm32_cryp *cryp)
  1004. {
  1005. unsigned int i, j;
  1006. u32 *src;
  1007. u8 d8[4];
  1008. size_t tag_size;
  1009. /* Do no write tag (if any) */
  1010. if (is_decrypt(cryp) && (is_gcm(cryp) || is_ccm(cryp)))
  1011. tag_size = cryp->authsize;
  1012. else
  1013. tag_size = 0;
  1014. src = sg_virt(cryp->in_sg) + _walked_in;
  1015. for (i = 0; i < cryp->hw_blocksize / sizeof(u32); i++) {
  1016. if (likely(cryp->total_in - tag_size >= sizeof(u32))) {
  1017. /* Write a full u32 */
  1018. stm32_cryp_write(cryp, CRYP_DIN, *src);
  1019. src = stm32_cryp_next_in(cryp, src, sizeof(u32));
  1020. cryp->total_in -= sizeof(u32);
  1021. } else if (cryp->total_in == tag_size) {
  1022. /* Write padding data */
  1023. stm32_cryp_write(cryp, CRYP_DIN, 0);
  1024. } else {
  1025. /* Write less than an u32 */
  1026. memset(d8, 0, sizeof(u32));
  1027. for (j = 0; j < cryp->total_in - tag_size; j++) {
  1028. d8[j] = *((u8 *)src);
  1029. src = stm32_cryp_next_in(cryp, src, 1);
  1030. }
  1031. stm32_cryp_write(cryp, CRYP_DIN, *(u32 *)d8);
  1032. cryp->total_in = tag_size;
  1033. }
  1034. }
  1035. }
  1036. static void stm32_cryp_irq_write_gcm_padded_data(struct stm32_cryp *cryp)
  1037. {
  1038. int err;
  1039. u32 cfg, tmp[AES_BLOCK_32];
  1040. size_t total_in_ori = cryp->total_in;
  1041. struct scatterlist *out_sg_ori = cryp->out_sg;
  1042. unsigned int i;
  1043. /* 'Special workaround' procedure described in the datasheet */
  1044. /* a) disable ip */
  1045. stm32_cryp_write(cryp, CRYP_IMSCR, 0);
  1046. cfg = stm32_cryp_read(cryp, CRYP_CR);
  1047. cfg &= ~CR_CRYPEN;
  1048. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1049. /* b) Update IV1R */
  1050. stm32_cryp_write(cryp, CRYP_IV1RR, cryp->gcm_ctr - 2);
  1051. /* c) change mode to CTR */
  1052. cfg &= ~CR_ALGO_MASK;
  1053. cfg |= CR_AES_CTR;
  1054. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1055. /* a) enable IP */
  1056. cfg |= CR_CRYPEN;
  1057. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1058. /* b) pad and write the last block */
  1059. stm32_cryp_irq_write_block(cryp);
  1060. cryp->total_in = total_in_ori;
  1061. err = stm32_cryp_wait_output(cryp);
  1062. if (err) {
  1063. dev_err(cryp->dev, "Timeout (write gcm header)\n");
  1064. return stm32_cryp_finish_req(cryp, err);
  1065. }
  1066. /* c) get and store encrypted data */
  1067. stm32_cryp_irq_read_data(cryp);
  1068. scatterwalk_map_and_copy(tmp, out_sg_ori,
  1069. cryp->total_in_save - total_in_ori,
  1070. total_in_ori, 0);
  1071. /* d) change mode back to AES GCM */
  1072. cfg &= ~CR_ALGO_MASK;
  1073. cfg |= CR_AES_GCM;
  1074. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1075. /* e) change phase to Final */
  1076. cfg &= ~CR_PH_MASK;
  1077. cfg |= CR_PH_FINAL;
  1078. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1079. /* f) write padded data */
  1080. for (i = 0; i < AES_BLOCK_32; i++) {
  1081. if (cryp->total_in)
  1082. stm32_cryp_write(cryp, CRYP_DIN, tmp[i]);
  1083. else
  1084. stm32_cryp_write(cryp, CRYP_DIN, 0);
  1085. cryp->total_in -= min_t(size_t, sizeof(u32), cryp->total_in);
  1086. }
  1087. /* g) Empty fifo out */
  1088. err = stm32_cryp_wait_output(cryp);
  1089. if (err) {
  1090. dev_err(cryp->dev, "Timeout (write gcm header)\n");
  1091. return stm32_cryp_finish_req(cryp, err);
  1092. }
  1093. for (i = 0; i < AES_BLOCK_32; i++)
  1094. stm32_cryp_read(cryp, CRYP_DOUT);
  1095. /* h) run the he normal Final phase */
  1096. stm32_cryp_finish_req(cryp, 0);
  1097. }
  1098. static void stm32_cryp_irq_set_npblb(struct stm32_cryp *cryp)
  1099. {
  1100. u32 cfg, payload_bytes;
  1101. /* disable ip, set NPBLB and reneable ip */
  1102. cfg = stm32_cryp_read(cryp, CRYP_CR);
  1103. cfg &= ~CR_CRYPEN;
  1104. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1105. payload_bytes = is_decrypt(cryp) ? cryp->total_in - cryp->authsize :
  1106. cryp->total_in;
  1107. cfg |= (cryp->hw_blocksize - payload_bytes) << CR_NBPBL_SHIFT;
  1108. cfg |= CR_CRYPEN;
  1109. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1110. }
  1111. static void stm32_cryp_irq_write_ccm_padded_data(struct stm32_cryp *cryp)
  1112. {
  1113. int err = 0;
  1114. u32 cfg, iv1tmp;
  1115. u32 cstmp1[AES_BLOCK_32], cstmp2[AES_BLOCK_32], tmp[AES_BLOCK_32];
  1116. size_t last_total_out, total_in_ori = cryp->total_in;
  1117. struct scatterlist *out_sg_ori = cryp->out_sg;
  1118. unsigned int i;
  1119. /* 'Special workaround' procedure described in the datasheet */
  1120. cryp->flags |= FLG_CCM_PADDED_WA;
  1121. /* a) disable ip */
  1122. stm32_cryp_write(cryp, CRYP_IMSCR, 0);
  1123. cfg = stm32_cryp_read(cryp, CRYP_CR);
  1124. cfg &= ~CR_CRYPEN;
  1125. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1126. /* b) get IV1 from CRYP_CSGCMCCM7 */
  1127. iv1tmp = stm32_cryp_read(cryp, CRYP_CSGCMCCM0R + 7 * 4);
  1128. /* c) Load CRYP_CSGCMCCMxR */
  1129. for (i = 0; i < ARRAY_SIZE(cstmp1); i++)
  1130. cstmp1[i] = stm32_cryp_read(cryp, CRYP_CSGCMCCM0R + i * 4);
  1131. /* d) Write IV1R */
  1132. stm32_cryp_write(cryp, CRYP_IV1RR, iv1tmp);
  1133. /* e) change mode to CTR */
  1134. cfg &= ~CR_ALGO_MASK;
  1135. cfg |= CR_AES_CTR;
  1136. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1137. /* a) enable IP */
  1138. cfg |= CR_CRYPEN;
  1139. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1140. /* b) pad and write the last block */
  1141. stm32_cryp_irq_write_block(cryp);
  1142. cryp->total_in = total_in_ori;
  1143. err = stm32_cryp_wait_output(cryp);
  1144. if (err) {
  1145. dev_err(cryp->dev, "Timeout (wite ccm padded data)\n");
  1146. return stm32_cryp_finish_req(cryp, err);
  1147. }
  1148. /* c) get and store decrypted data */
  1149. last_total_out = cryp->total_out;
  1150. stm32_cryp_irq_read_data(cryp);
  1151. memset(tmp, 0, sizeof(tmp));
  1152. scatterwalk_map_and_copy(tmp, out_sg_ori,
  1153. cryp->total_out_save - last_total_out,
  1154. last_total_out, 0);
  1155. /* d) Load again CRYP_CSGCMCCMxR */
  1156. for (i = 0; i < ARRAY_SIZE(cstmp2); i++)
  1157. cstmp2[i] = stm32_cryp_read(cryp, CRYP_CSGCMCCM0R + i * 4);
  1158. /* e) change mode back to AES CCM */
  1159. cfg &= ~CR_ALGO_MASK;
  1160. cfg |= CR_AES_CCM;
  1161. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1162. /* f) change phase to header */
  1163. cfg &= ~CR_PH_MASK;
  1164. cfg |= CR_PH_HEADER;
  1165. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1166. /* g) XOR and write padded data */
  1167. for (i = 0; i < ARRAY_SIZE(tmp); i++) {
  1168. tmp[i] ^= cstmp1[i];
  1169. tmp[i] ^= cstmp2[i];
  1170. stm32_cryp_write(cryp, CRYP_DIN, tmp[i]);
  1171. }
  1172. /* h) wait for completion */
  1173. err = stm32_cryp_wait_busy(cryp);
  1174. if (err)
  1175. dev_err(cryp->dev, "Timeout (wite ccm padded data)\n");
  1176. /* i) run the he normal Final phase */
  1177. stm32_cryp_finish_req(cryp, err);
  1178. }
  1179. static void stm32_cryp_irq_write_data(struct stm32_cryp *cryp)
  1180. {
  1181. if (unlikely(!cryp->total_in)) {
  1182. dev_warn(cryp->dev, "No more data to process\n");
  1183. return;
  1184. }
  1185. if (unlikely(cryp->total_in < AES_BLOCK_SIZE &&
  1186. (stm32_cryp_get_hw_mode(cryp) == CR_AES_GCM) &&
  1187. is_encrypt(cryp))) {
  1188. /* Padding for AES GCM encryption */
  1189. if (cryp->caps->padding_wa)
  1190. /* Special case 1 */
  1191. return stm32_cryp_irq_write_gcm_padded_data(cryp);
  1192. /* Setting padding bytes (NBBLB) */
  1193. stm32_cryp_irq_set_npblb(cryp);
  1194. }
  1195. if (unlikely((cryp->total_in - cryp->authsize < AES_BLOCK_SIZE) &&
  1196. (stm32_cryp_get_hw_mode(cryp) == CR_AES_CCM) &&
  1197. is_decrypt(cryp))) {
  1198. /* Padding for AES CCM decryption */
  1199. if (cryp->caps->padding_wa)
  1200. /* Special case 2 */
  1201. return stm32_cryp_irq_write_ccm_padded_data(cryp);
  1202. /* Setting padding bytes (NBBLB) */
  1203. stm32_cryp_irq_set_npblb(cryp);
  1204. }
  1205. if (is_aes(cryp) && is_ctr(cryp))
  1206. stm32_cryp_check_ctr_counter(cryp);
  1207. stm32_cryp_irq_write_block(cryp);
  1208. }
  1209. static void stm32_cryp_irq_write_gcm_header(struct stm32_cryp *cryp)
  1210. {
  1211. int err;
  1212. unsigned int i, j;
  1213. u32 cfg, *src;
  1214. src = sg_virt(cryp->in_sg) + _walked_in;
  1215. for (i = 0; i < AES_BLOCK_32; i++) {
  1216. stm32_cryp_write(cryp, CRYP_DIN, *src);
  1217. src = stm32_cryp_next_in(cryp, src, sizeof(u32));
  1218. cryp->total_in -= min_t(size_t, sizeof(u32), cryp->total_in);
  1219. /* Check if whole header written */
  1220. if ((cryp->total_in_save - cryp->total_in) ==
  1221. cryp->areq->assoclen) {
  1222. /* Write padding if needed */
  1223. for (j = i + 1; j < AES_BLOCK_32; j++)
  1224. stm32_cryp_write(cryp, CRYP_DIN, 0);
  1225. /* Wait for completion */
  1226. err = stm32_cryp_wait_busy(cryp);
  1227. if (err) {
  1228. dev_err(cryp->dev, "Timeout (gcm header)\n");
  1229. return stm32_cryp_finish_req(cryp, err);
  1230. }
  1231. if (stm32_cryp_get_input_text_len(cryp)) {
  1232. /* Phase 3 : payload */
  1233. cfg = stm32_cryp_read(cryp, CRYP_CR);
  1234. cfg &= ~CR_CRYPEN;
  1235. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1236. cfg &= ~CR_PH_MASK;
  1237. cfg |= CR_PH_PAYLOAD;
  1238. cfg |= CR_CRYPEN;
  1239. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1240. } else {
  1241. /* Phase 4 : tag */
  1242. stm32_cryp_write(cryp, CRYP_IMSCR, 0);
  1243. stm32_cryp_finish_req(cryp, 0);
  1244. }
  1245. break;
  1246. }
  1247. if (!cryp->total_in)
  1248. break;
  1249. }
  1250. }
  1251. static void stm32_cryp_irq_write_ccm_header(struct stm32_cryp *cryp)
  1252. {
  1253. int err;
  1254. unsigned int i = 0, j, k;
  1255. u32 alen, cfg, *src;
  1256. u8 d8[4];
  1257. src = sg_virt(cryp->in_sg) + _walked_in;
  1258. alen = cryp->areq->assoclen;
  1259. if (!_walked_in) {
  1260. if (cryp->areq->assoclen <= 65280) {
  1261. /* Write first u32 of B1 */
  1262. d8[0] = (alen >> 8) & 0xFF;
  1263. d8[1] = alen & 0xFF;
  1264. d8[2] = *((u8 *)src);
  1265. src = stm32_cryp_next_in(cryp, src, 1);
  1266. d8[3] = *((u8 *)src);
  1267. src = stm32_cryp_next_in(cryp, src, 1);
  1268. stm32_cryp_write(cryp, CRYP_DIN, *(u32 *)d8);
  1269. i++;
  1270. cryp->total_in -= min_t(size_t, 2, cryp->total_in);
  1271. } else {
  1272. /* Build the two first u32 of B1 */
  1273. d8[0] = 0xFF;
  1274. d8[1] = 0xFE;
  1275. d8[2] = alen & 0xFF000000;
  1276. d8[3] = alen & 0x00FF0000;
  1277. stm32_cryp_write(cryp, CRYP_DIN, *(u32 *)d8);
  1278. i++;
  1279. d8[0] = alen & 0x0000FF00;
  1280. d8[1] = alen & 0x000000FF;
  1281. d8[2] = *((u8 *)src);
  1282. src = stm32_cryp_next_in(cryp, src, 1);
  1283. d8[3] = *((u8 *)src);
  1284. src = stm32_cryp_next_in(cryp, src, 1);
  1285. stm32_cryp_write(cryp, CRYP_DIN, *(u32 *)d8);
  1286. i++;
  1287. cryp->total_in -= min_t(size_t, 2, cryp->total_in);
  1288. }
  1289. }
  1290. /* Write next u32 */
  1291. for (; i < AES_BLOCK_32; i++) {
  1292. /* Build an u32 */
  1293. memset(d8, 0, sizeof(u32));
  1294. for (k = 0; k < sizeof(u32); k++) {
  1295. d8[k] = *((u8 *)src);
  1296. src = stm32_cryp_next_in(cryp, src, 1);
  1297. cryp->total_in -= min_t(size_t, 1, cryp->total_in);
  1298. if ((cryp->total_in_save - cryp->total_in) == alen)
  1299. break;
  1300. }
  1301. stm32_cryp_write(cryp, CRYP_DIN, *(u32 *)d8);
  1302. if ((cryp->total_in_save - cryp->total_in) == alen) {
  1303. /* Write padding if needed */
  1304. for (j = i + 1; j < AES_BLOCK_32; j++)
  1305. stm32_cryp_write(cryp, CRYP_DIN, 0);
  1306. /* Wait for completion */
  1307. err = stm32_cryp_wait_busy(cryp);
  1308. if (err) {
  1309. dev_err(cryp->dev, "Timeout (ccm header)\n");
  1310. return stm32_cryp_finish_req(cryp, err);
  1311. }
  1312. if (stm32_cryp_get_input_text_len(cryp)) {
  1313. /* Phase 3 : payload */
  1314. cfg = stm32_cryp_read(cryp, CRYP_CR);
  1315. cfg &= ~CR_CRYPEN;
  1316. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1317. cfg &= ~CR_PH_MASK;
  1318. cfg |= CR_PH_PAYLOAD;
  1319. cfg |= CR_CRYPEN;
  1320. stm32_cryp_write(cryp, CRYP_CR, cfg);
  1321. } else {
  1322. /* Phase 4 : tag */
  1323. stm32_cryp_write(cryp, CRYP_IMSCR, 0);
  1324. stm32_cryp_finish_req(cryp, 0);
  1325. }
  1326. break;
  1327. }
  1328. }
  1329. }
  1330. static irqreturn_t stm32_cryp_irq_thread(int irq, void *arg)
  1331. {
  1332. struct stm32_cryp *cryp = arg;
  1333. u32 ph;
  1334. if (cryp->irq_status & MISR_OUT)
  1335. /* Output FIFO IRQ: read data */
  1336. if (unlikely(stm32_cryp_irq_read_data(cryp))) {
  1337. /* All bytes processed, finish */
  1338. stm32_cryp_write(cryp, CRYP_IMSCR, 0);
  1339. stm32_cryp_finish_req(cryp, 0);
  1340. return IRQ_HANDLED;
  1341. }
  1342. if (cryp->irq_status & MISR_IN) {
  1343. if (is_gcm(cryp)) {
  1344. ph = stm32_cryp_read(cryp, CRYP_CR) & CR_PH_MASK;
  1345. if (unlikely(ph == CR_PH_HEADER))
  1346. /* Write Header */
  1347. stm32_cryp_irq_write_gcm_header(cryp);
  1348. else
  1349. /* Input FIFO IRQ: write data */
  1350. stm32_cryp_irq_write_data(cryp);
  1351. cryp->gcm_ctr++;
  1352. } else if (is_ccm(cryp)) {
  1353. ph = stm32_cryp_read(cryp, CRYP_CR) & CR_PH_MASK;
  1354. if (unlikely(ph == CR_PH_HEADER))
  1355. /* Write Header */
  1356. stm32_cryp_irq_write_ccm_header(cryp);
  1357. else
  1358. /* Input FIFO IRQ: write data */
  1359. stm32_cryp_irq_write_data(cryp);
  1360. } else {
  1361. /* Input FIFO IRQ: write data */
  1362. stm32_cryp_irq_write_data(cryp);
  1363. }
  1364. }
  1365. return IRQ_HANDLED;
  1366. }
  1367. static irqreturn_t stm32_cryp_irq(int irq, void *arg)
  1368. {
  1369. struct stm32_cryp *cryp = arg;
  1370. cryp->irq_status = stm32_cryp_read(cryp, CRYP_MISR);
  1371. return IRQ_WAKE_THREAD;
  1372. }
  1373. static struct crypto_alg crypto_algs[] = {
  1374. {
  1375. .cra_name = "ecb(aes)",
  1376. .cra_driver_name = "stm32-ecb-aes",
  1377. .cra_priority = 200,
  1378. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1379. CRYPTO_ALG_ASYNC,
  1380. .cra_blocksize = AES_BLOCK_SIZE,
  1381. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1382. .cra_alignmask = 0xf,
  1383. .cra_type = &crypto_ablkcipher_type,
  1384. .cra_module = THIS_MODULE,
  1385. .cra_init = stm32_cryp_cra_init,
  1386. .cra_ablkcipher = {
  1387. .min_keysize = AES_MIN_KEY_SIZE,
  1388. .max_keysize = AES_MAX_KEY_SIZE,
  1389. .setkey = stm32_cryp_aes_setkey,
  1390. .encrypt = stm32_cryp_aes_ecb_encrypt,
  1391. .decrypt = stm32_cryp_aes_ecb_decrypt,
  1392. }
  1393. },
  1394. {
  1395. .cra_name = "cbc(aes)",
  1396. .cra_driver_name = "stm32-cbc-aes",
  1397. .cra_priority = 200,
  1398. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1399. CRYPTO_ALG_ASYNC,
  1400. .cra_blocksize = AES_BLOCK_SIZE,
  1401. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1402. .cra_alignmask = 0xf,
  1403. .cra_type = &crypto_ablkcipher_type,
  1404. .cra_module = THIS_MODULE,
  1405. .cra_init = stm32_cryp_cra_init,
  1406. .cra_ablkcipher = {
  1407. .min_keysize = AES_MIN_KEY_SIZE,
  1408. .max_keysize = AES_MAX_KEY_SIZE,
  1409. .ivsize = AES_BLOCK_SIZE,
  1410. .setkey = stm32_cryp_aes_setkey,
  1411. .encrypt = stm32_cryp_aes_cbc_encrypt,
  1412. .decrypt = stm32_cryp_aes_cbc_decrypt,
  1413. }
  1414. },
  1415. {
  1416. .cra_name = "ctr(aes)",
  1417. .cra_driver_name = "stm32-ctr-aes",
  1418. .cra_priority = 200,
  1419. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1420. CRYPTO_ALG_ASYNC,
  1421. .cra_blocksize = 1,
  1422. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1423. .cra_alignmask = 0xf,
  1424. .cra_type = &crypto_ablkcipher_type,
  1425. .cra_module = THIS_MODULE,
  1426. .cra_init = stm32_cryp_cra_init,
  1427. .cra_ablkcipher = {
  1428. .min_keysize = AES_MIN_KEY_SIZE,
  1429. .max_keysize = AES_MAX_KEY_SIZE,
  1430. .ivsize = AES_BLOCK_SIZE,
  1431. .setkey = stm32_cryp_aes_setkey,
  1432. .encrypt = stm32_cryp_aes_ctr_encrypt,
  1433. .decrypt = stm32_cryp_aes_ctr_decrypt,
  1434. }
  1435. },
  1436. {
  1437. .cra_name = "ecb(des)",
  1438. .cra_driver_name = "stm32-ecb-des",
  1439. .cra_priority = 200,
  1440. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1441. CRYPTO_ALG_ASYNC,
  1442. .cra_blocksize = DES_BLOCK_SIZE,
  1443. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1444. .cra_alignmask = 0xf,
  1445. .cra_type = &crypto_ablkcipher_type,
  1446. .cra_module = THIS_MODULE,
  1447. .cra_init = stm32_cryp_cra_init,
  1448. .cra_ablkcipher = {
  1449. .min_keysize = DES_BLOCK_SIZE,
  1450. .max_keysize = DES_BLOCK_SIZE,
  1451. .setkey = stm32_cryp_des_setkey,
  1452. .encrypt = stm32_cryp_des_ecb_encrypt,
  1453. .decrypt = stm32_cryp_des_ecb_decrypt,
  1454. }
  1455. },
  1456. {
  1457. .cra_name = "cbc(des)",
  1458. .cra_driver_name = "stm32-cbc-des",
  1459. .cra_priority = 200,
  1460. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1461. CRYPTO_ALG_ASYNC,
  1462. .cra_blocksize = DES_BLOCK_SIZE,
  1463. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1464. .cra_alignmask = 0xf,
  1465. .cra_type = &crypto_ablkcipher_type,
  1466. .cra_module = THIS_MODULE,
  1467. .cra_init = stm32_cryp_cra_init,
  1468. .cra_ablkcipher = {
  1469. .min_keysize = DES_BLOCK_SIZE,
  1470. .max_keysize = DES_BLOCK_SIZE,
  1471. .ivsize = DES_BLOCK_SIZE,
  1472. .setkey = stm32_cryp_des_setkey,
  1473. .encrypt = stm32_cryp_des_cbc_encrypt,
  1474. .decrypt = stm32_cryp_des_cbc_decrypt,
  1475. }
  1476. },
  1477. {
  1478. .cra_name = "ecb(des3_ede)",
  1479. .cra_driver_name = "stm32-ecb-des3",
  1480. .cra_priority = 200,
  1481. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1482. CRYPTO_ALG_ASYNC,
  1483. .cra_blocksize = DES_BLOCK_SIZE,
  1484. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1485. .cra_alignmask = 0xf,
  1486. .cra_type = &crypto_ablkcipher_type,
  1487. .cra_module = THIS_MODULE,
  1488. .cra_init = stm32_cryp_cra_init,
  1489. .cra_ablkcipher = {
  1490. .min_keysize = 3 * DES_BLOCK_SIZE,
  1491. .max_keysize = 3 * DES_BLOCK_SIZE,
  1492. .setkey = stm32_cryp_tdes_setkey,
  1493. .encrypt = stm32_cryp_tdes_ecb_encrypt,
  1494. .decrypt = stm32_cryp_tdes_ecb_decrypt,
  1495. }
  1496. },
  1497. {
  1498. .cra_name = "cbc(des3_ede)",
  1499. .cra_driver_name = "stm32-cbc-des3",
  1500. .cra_priority = 200,
  1501. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  1502. CRYPTO_ALG_ASYNC,
  1503. .cra_blocksize = DES_BLOCK_SIZE,
  1504. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1505. .cra_alignmask = 0xf,
  1506. .cra_type = &crypto_ablkcipher_type,
  1507. .cra_module = THIS_MODULE,
  1508. .cra_init = stm32_cryp_cra_init,
  1509. .cra_ablkcipher = {
  1510. .min_keysize = 3 * DES_BLOCK_SIZE,
  1511. .max_keysize = 3 * DES_BLOCK_SIZE,
  1512. .ivsize = DES_BLOCK_SIZE,
  1513. .setkey = stm32_cryp_tdes_setkey,
  1514. .encrypt = stm32_cryp_tdes_cbc_encrypt,
  1515. .decrypt = stm32_cryp_tdes_cbc_decrypt,
  1516. }
  1517. },
  1518. };
  1519. static struct aead_alg aead_algs[] = {
  1520. {
  1521. .setkey = stm32_cryp_aes_aead_setkey,
  1522. .setauthsize = stm32_cryp_aes_gcm_setauthsize,
  1523. .encrypt = stm32_cryp_aes_gcm_encrypt,
  1524. .decrypt = stm32_cryp_aes_gcm_decrypt,
  1525. .init = stm32_cryp_aes_aead_init,
  1526. .ivsize = 12,
  1527. .maxauthsize = AES_BLOCK_SIZE,
  1528. .base = {
  1529. .cra_name = "gcm(aes)",
  1530. .cra_driver_name = "stm32-gcm-aes",
  1531. .cra_priority = 200,
  1532. .cra_flags = CRYPTO_ALG_ASYNC,
  1533. .cra_blocksize = 1,
  1534. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1535. .cra_alignmask = 0xf,
  1536. .cra_module = THIS_MODULE,
  1537. },
  1538. },
  1539. {
  1540. .setkey = stm32_cryp_aes_aead_setkey,
  1541. .setauthsize = stm32_cryp_aes_ccm_setauthsize,
  1542. .encrypt = stm32_cryp_aes_ccm_encrypt,
  1543. .decrypt = stm32_cryp_aes_ccm_decrypt,
  1544. .init = stm32_cryp_aes_aead_init,
  1545. .ivsize = AES_BLOCK_SIZE,
  1546. .maxauthsize = AES_BLOCK_SIZE,
  1547. .base = {
  1548. .cra_name = "ccm(aes)",
  1549. .cra_driver_name = "stm32-ccm-aes",
  1550. .cra_priority = 200,
  1551. .cra_flags = CRYPTO_ALG_ASYNC,
  1552. .cra_blocksize = 1,
  1553. .cra_ctxsize = sizeof(struct stm32_cryp_ctx),
  1554. .cra_alignmask = 0xf,
  1555. .cra_module = THIS_MODULE,
  1556. },
  1557. },
  1558. };
  1559. static const struct stm32_cryp_caps f7_data = {
  1560. .swap_final = true,
  1561. .padding_wa = true,
  1562. };
  1563. static const struct stm32_cryp_caps mp1_data = {
  1564. .swap_final = false,
  1565. .padding_wa = false,
  1566. };
  1567. static const struct of_device_id stm32_dt_ids[] = {
  1568. { .compatible = "st,stm32f756-cryp", .data = &f7_data},
  1569. { .compatible = "st,stm32mp1-cryp", .data = &mp1_data},
  1570. {},
  1571. };
  1572. MODULE_DEVICE_TABLE(of, stm32_dt_ids);
  1573. static int stm32_cryp_probe(struct platform_device *pdev)
  1574. {
  1575. struct device *dev = &pdev->dev;
  1576. struct stm32_cryp *cryp;
  1577. struct resource *res;
  1578. struct reset_control *rst;
  1579. int irq, ret;
  1580. cryp = devm_kzalloc(dev, sizeof(*cryp), GFP_KERNEL);
  1581. if (!cryp)
  1582. return -ENOMEM;
  1583. cryp->caps = of_device_get_match_data(dev);
  1584. if (!cryp->caps)
  1585. return -ENODEV;
  1586. cryp->dev = dev;
  1587. mutex_init(&cryp->lock);
  1588. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1589. cryp->regs = devm_ioremap_resource(dev, res);
  1590. if (IS_ERR(cryp->regs))
  1591. return PTR_ERR(cryp->regs);
  1592. irq = platform_get_irq(pdev, 0);
  1593. if (irq < 0) {
  1594. dev_err(dev, "Cannot get IRQ resource\n");
  1595. return irq;
  1596. }
  1597. ret = devm_request_threaded_irq(dev, irq, stm32_cryp_irq,
  1598. stm32_cryp_irq_thread, IRQF_ONESHOT,
  1599. dev_name(dev), cryp);
  1600. if (ret) {
  1601. dev_err(dev, "Cannot grab IRQ\n");
  1602. return ret;
  1603. }
  1604. cryp->clk = devm_clk_get(dev, NULL);
  1605. if (IS_ERR(cryp->clk)) {
  1606. dev_err(dev, "Could not get clock\n");
  1607. return PTR_ERR(cryp->clk);
  1608. }
  1609. ret = clk_prepare_enable(cryp->clk);
  1610. if (ret) {
  1611. dev_err(cryp->dev, "Failed to enable clock\n");
  1612. return ret;
  1613. }
  1614. rst = devm_reset_control_get(dev, NULL);
  1615. if (!IS_ERR(rst)) {
  1616. reset_control_assert(rst);
  1617. udelay(2);
  1618. reset_control_deassert(rst);
  1619. }
  1620. platform_set_drvdata(pdev, cryp);
  1621. spin_lock(&cryp_list.lock);
  1622. list_add(&cryp->list, &cryp_list.dev_list);
  1623. spin_unlock(&cryp_list.lock);
  1624. /* Initialize crypto engine */
  1625. cryp->engine = crypto_engine_alloc_init(dev, 1);
  1626. if (!cryp->engine) {
  1627. dev_err(dev, "Could not init crypto engine\n");
  1628. ret = -ENOMEM;
  1629. goto err_engine1;
  1630. }
  1631. ret = crypto_engine_start(cryp->engine);
  1632. if (ret) {
  1633. dev_err(dev, "Could not start crypto engine\n");
  1634. goto err_engine2;
  1635. }
  1636. ret = crypto_register_algs(crypto_algs, ARRAY_SIZE(crypto_algs));
  1637. if (ret) {
  1638. dev_err(dev, "Could not register algs\n");
  1639. goto err_algs;
  1640. }
  1641. ret = crypto_register_aeads(aead_algs, ARRAY_SIZE(aead_algs));
  1642. if (ret)
  1643. goto err_aead_algs;
  1644. dev_info(dev, "Initialized\n");
  1645. return 0;
  1646. err_aead_algs:
  1647. crypto_unregister_algs(crypto_algs, ARRAY_SIZE(crypto_algs));
  1648. err_algs:
  1649. err_engine2:
  1650. crypto_engine_exit(cryp->engine);
  1651. err_engine1:
  1652. spin_lock(&cryp_list.lock);
  1653. list_del(&cryp->list);
  1654. spin_unlock(&cryp_list.lock);
  1655. clk_disable_unprepare(cryp->clk);
  1656. return ret;
  1657. }
  1658. static int stm32_cryp_remove(struct platform_device *pdev)
  1659. {
  1660. struct stm32_cryp *cryp = platform_get_drvdata(pdev);
  1661. if (!cryp)
  1662. return -ENODEV;
  1663. crypto_unregister_aeads(aead_algs, ARRAY_SIZE(aead_algs));
  1664. crypto_unregister_algs(crypto_algs, ARRAY_SIZE(crypto_algs));
  1665. crypto_engine_exit(cryp->engine);
  1666. spin_lock(&cryp_list.lock);
  1667. list_del(&cryp->list);
  1668. spin_unlock(&cryp_list.lock);
  1669. clk_disable_unprepare(cryp->clk);
  1670. return 0;
  1671. }
  1672. static struct platform_driver stm32_cryp_driver = {
  1673. .probe = stm32_cryp_probe,
  1674. .remove = stm32_cryp_remove,
  1675. .driver = {
  1676. .name = DRIVER_NAME,
  1677. .of_match_table = stm32_dt_ids,
  1678. },
  1679. };
  1680. module_platform_driver(stm32_cryp_driver);
  1681. MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
  1682. MODULE_DESCRIPTION("STMicrolectronics STM32 CRYP hardware driver");
  1683. MODULE_LICENSE("GPL");