cc_cipher.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <crypto/algapi.h>
  6. #include <crypto/internal/skcipher.h>
  7. #include <crypto/des.h>
  8. #include <crypto/xts.h>
  9. #include <crypto/scatterwalk.h>
  10. #include "cc_driver.h"
  11. #include "cc_lli_defs.h"
  12. #include "cc_buffer_mgr.h"
  13. #include "cc_cipher.h"
  14. #include "cc_request_mgr.h"
  15. #define MAX_ABLKCIPHER_SEQ_LEN 6
  16. #define template_skcipher template_u.skcipher
  17. #define CC_MIN_AES_XTS_SIZE 0x10
  18. #define CC_MAX_AES_XTS_SIZE 0x2000
  19. struct cc_cipher_handle {
  20. struct list_head alg_list;
  21. };
  22. struct cc_user_key_info {
  23. u8 *key;
  24. dma_addr_t key_dma_addr;
  25. };
  26. struct cc_hw_key_info {
  27. enum cc_hw_crypto_key key1_slot;
  28. enum cc_hw_crypto_key key2_slot;
  29. };
  30. struct cc_cipher_ctx {
  31. struct cc_drvdata *drvdata;
  32. int keylen;
  33. int key_round_number;
  34. int cipher_mode;
  35. int flow_mode;
  36. unsigned int flags;
  37. struct cc_user_key_info user;
  38. struct cc_hw_key_info hw;
  39. struct crypto_shash *shash_tfm;
  40. };
  41. static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
  42. static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
  43. {
  44. switch (ctx_p->flow_mode) {
  45. case S_DIN_to_AES:
  46. switch (size) {
  47. case CC_AES_128_BIT_KEY_SIZE:
  48. case CC_AES_192_BIT_KEY_SIZE:
  49. if (ctx_p->cipher_mode != DRV_CIPHER_XTS &&
  50. ctx_p->cipher_mode != DRV_CIPHER_ESSIV &&
  51. ctx_p->cipher_mode != DRV_CIPHER_BITLOCKER)
  52. return 0;
  53. break;
  54. case CC_AES_256_BIT_KEY_SIZE:
  55. return 0;
  56. case (CC_AES_192_BIT_KEY_SIZE * 2):
  57. case (CC_AES_256_BIT_KEY_SIZE * 2):
  58. if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
  59. ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
  60. ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)
  61. return 0;
  62. break;
  63. default:
  64. break;
  65. }
  66. case S_DIN_to_DES:
  67. if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
  68. return 0;
  69. break;
  70. default:
  71. break;
  72. }
  73. return -EINVAL;
  74. }
  75. static int validate_data_size(struct cc_cipher_ctx *ctx_p,
  76. unsigned int size)
  77. {
  78. switch (ctx_p->flow_mode) {
  79. case S_DIN_to_AES:
  80. switch (ctx_p->cipher_mode) {
  81. case DRV_CIPHER_XTS:
  82. if (size >= CC_MIN_AES_XTS_SIZE &&
  83. size <= CC_MAX_AES_XTS_SIZE &&
  84. IS_ALIGNED(size, AES_BLOCK_SIZE))
  85. return 0;
  86. break;
  87. case DRV_CIPHER_CBC_CTS:
  88. if (size >= AES_BLOCK_SIZE)
  89. return 0;
  90. break;
  91. case DRV_CIPHER_OFB:
  92. case DRV_CIPHER_CTR:
  93. return 0;
  94. case DRV_CIPHER_ECB:
  95. case DRV_CIPHER_CBC:
  96. case DRV_CIPHER_ESSIV:
  97. case DRV_CIPHER_BITLOCKER:
  98. if (IS_ALIGNED(size, AES_BLOCK_SIZE))
  99. return 0;
  100. break;
  101. default:
  102. break;
  103. }
  104. break;
  105. case S_DIN_to_DES:
  106. if (IS_ALIGNED(size, DES_BLOCK_SIZE))
  107. return 0;
  108. break;
  109. default:
  110. break;
  111. }
  112. return -EINVAL;
  113. }
  114. static int cc_cipher_init(struct crypto_tfm *tfm)
  115. {
  116. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  117. struct cc_crypto_alg *cc_alg =
  118. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  119. skcipher_alg.base);
  120. struct device *dev = drvdata_to_dev(cc_alg->drvdata);
  121. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  122. int rc = 0;
  123. dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
  124. crypto_tfm_alg_name(tfm));
  125. crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
  126. sizeof(struct cipher_req_ctx));
  127. ctx_p->cipher_mode = cc_alg->cipher_mode;
  128. ctx_p->flow_mode = cc_alg->flow_mode;
  129. ctx_p->drvdata = cc_alg->drvdata;
  130. /* Allocate key buffer, cache line aligned */
  131. ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL);
  132. if (!ctx_p->user.key)
  133. return -ENOMEM;
  134. dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
  135. ctx_p->user.key);
  136. /* Map key buffer */
  137. ctx_p->user.key_dma_addr = dma_map_single(dev, (void *)ctx_p->user.key,
  138. max_key_buf_size,
  139. DMA_TO_DEVICE);
  140. if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
  141. dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
  142. max_key_buf_size, ctx_p->user.key);
  143. return -ENOMEM;
  144. }
  145. dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
  146. max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
  147. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  148. /* Alloc hash tfm for essiv */
  149. ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
  150. if (IS_ERR(ctx_p->shash_tfm)) {
  151. dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
  152. return PTR_ERR(ctx_p->shash_tfm);
  153. }
  154. }
  155. return rc;
  156. }
  157. static void cc_cipher_exit(struct crypto_tfm *tfm)
  158. {
  159. struct crypto_alg *alg = tfm->__crt_alg;
  160. struct cc_crypto_alg *cc_alg =
  161. container_of(alg, struct cc_crypto_alg,
  162. skcipher_alg.base);
  163. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  164. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  165. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  166. dev_dbg(dev, "Clearing context @%p for %s\n",
  167. crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
  168. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  169. /* Free hash tfm for essiv */
  170. crypto_free_shash(ctx_p->shash_tfm);
  171. ctx_p->shash_tfm = NULL;
  172. }
  173. /* Unmap key buffer */
  174. dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
  175. DMA_TO_DEVICE);
  176. dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
  177. &ctx_p->user.key_dma_addr);
  178. /* Free key buffer in context */
  179. kzfree(ctx_p->user.key);
  180. dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
  181. }
  182. struct tdes_keys {
  183. u8 key1[DES_KEY_SIZE];
  184. u8 key2[DES_KEY_SIZE];
  185. u8 key3[DES_KEY_SIZE];
  186. };
  187. static enum cc_hw_crypto_key hw_key_to_cc_hw_key(int slot_num)
  188. {
  189. switch (slot_num) {
  190. case 0:
  191. return KFDE0_KEY;
  192. case 1:
  193. return KFDE1_KEY;
  194. case 2:
  195. return KFDE2_KEY;
  196. case 3:
  197. return KFDE3_KEY;
  198. }
  199. return END_OF_KEYS;
  200. }
  201. static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
  202. unsigned int keylen)
  203. {
  204. struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
  205. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  206. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  207. u32 tmp[DES3_EDE_EXPKEY_WORDS];
  208. struct cc_crypto_alg *cc_alg =
  209. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  210. skcipher_alg.base);
  211. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  212. dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
  213. ctx_p, crypto_tfm_alg_name(tfm), keylen);
  214. dump_byte_array("key", (u8 *)key, keylen);
  215. /* STAT_PHASE_0: Init and sanity checks */
  216. if (validate_keys_sizes(ctx_p, keylen)) {
  217. dev_err(dev, "Unsupported key size %d.\n", keylen);
  218. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  219. return -EINVAL;
  220. }
  221. if (cc_is_hw_key(tfm)) {
  222. /* setting HW key slots */
  223. struct arm_hw_key_info *hki = (struct arm_hw_key_info *)key;
  224. if (ctx_p->flow_mode != S_DIN_to_AES) {
  225. dev_err(dev, "HW key not supported for non-AES flows\n");
  226. return -EINVAL;
  227. }
  228. ctx_p->hw.key1_slot = hw_key_to_cc_hw_key(hki->hw_key1);
  229. if (ctx_p->hw.key1_slot == END_OF_KEYS) {
  230. dev_err(dev, "Unsupported hw key1 number (%d)\n",
  231. hki->hw_key1);
  232. return -EINVAL;
  233. }
  234. if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
  235. ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
  236. ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER) {
  237. if (hki->hw_key1 == hki->hw_key2) {
  238. dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
  239. hki->hw_key1, hki->hw_key2);
  240. return -EINVAL;
  241. }
  242. ctx_p->hw.key2_slot =
  243. hw_key_to_cc_hw_key(hki->hw_key2);
  244. if (ctx_p->hw.key2_slot == END_OF_KEYS) {
  245. dev_err(dev, "Unsupported hw key2 number (%d)\n",
  246. hki->hw_key2);
  247. return -EINVAL;
  248. }
  249. }
  250. ctx_p->keylen = keylen;
  251. dev_dbg(dev, "cc_is_hw_key ret 0");
  252. return 0;
  253. }
  254. /*
  255. * Verify DES weak keys
  256. * Note that we're dropping the expanded key since the
  257. * HW does the expansion on its own.
  258. */
  259. if (ctx_p->flow_mode == S_DIN_to_DES) {
  260. if (keylen == DES3_EDE_KEY_SIZE &&
  261. __des3_ede_setkey(tmp, &tfm->crt_flags, key,
  262. DES3_EDE_KEY_SIZE)) {
  263. dev_dbg(dev, "weak 3DES key");
  264. return -EINVAL;
  265. } else if (!des_ekey(tmp, key) &&
  266. (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_WEAK_KEY)) {
  267. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  268. dev_dbg(dev, "weak DES key");
  269. return -EINVAL;
  270. }
  271. }
  272. if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
  273. xts_check_key(tfm, key, keylen)) {
  274. dev_dbg(dev, "weak XTS key");
  275. return -EINVAL;
  276. }
  277. /* STAT_PHASE_1: Copy key to ctx */
  278. dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
  279. max_key_buf_size, DMA_TO_DEVICE);
  280. memcpy(ctx_p->user.key, key, keylen);
  281. if (keylen == 24)
  282. memset(ctx_p->user.key + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
  283. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  284. /* sha256 for key2 - use sw implementation */
  285. int key_len = keylen >> 1;
  286. int err;
  287. SHASH_DESC_ON_STACK(desc, ctx_p->shash_tfm);
  288. desc->tfm = ctx_p->shash_tfm;
  289. err = crypto_shash_digest(desc, ctx_p->user.key, key_len,
  290. ctx_p->user.key + key_len);
  291. if (err) {
  292. dev_err(dev, "Failed to hash ESSIV key.\n");
  293. return err;
  294. }
  295. }
  296. dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
  297. max_key_buf_size, DMA_TO_DEVICE);
  298. ctx_p->keylen = keylen;
  299. dev_dbg(dev, "return safely");
  300. return 0;
  301. }
  302. static void cc_setup_cipher_desc(struct crypto_tfm *tfm,
  303. struct cipher_req_ctx *req_ctx,
  304. unsigned int ivsize, unsigned int nbytes,
  305. struct cc_hw_desc desc[],
  306. unsigned int *seq_size)
  307. {
  308. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  309. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  310. int cipher_mode = ctx_p->cipher_mode;
  311. int flow_mode = ctx_p->flow_mode;
  312. int direction = req_ctx->gen_ctx.op_type;
  313. dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
  314. unsigned int key_len = ctx_p->keylen;
  315. dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
  316. unsigned int du_size = nbytes;
  317. struct cc_crypto_alg *cc_alg =
  318. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  319. skcipher_alg.base);
  320. if (cc_alg->data_unit)
  321. du_size = cc_alg->data_unit;
  322. switch (cipher_mode) {
  323. case DRV_CIPHER_CBC:
  324. case DRV_CIPHER_CBC_CTS:
  325. case DRV_CIPHER_CTR:
  326. case DRV_CIPHER_OFB:
  327. /* Load cipher state */
  328. hw_desc_init(&desc[*seq_size]);
  329. set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
  330. NS_BIT);
  331. set_cipher_config0(&desc[*seq_size], direction);
  332. set_flow_mode(&desc[*seq_size], flow_mode);
  333. set_cipher_mode(&desc[*seq_size], cipher_mode);
  334. if (cipher_mode == DRV_CIPHER_CTR ||
  335. cipher_mode == DRV_CIPHER_OFB) {
  336. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
  337. } else {
  338. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
  339. }
  340. (*seq_size)++;
  341. /*FALLTHROUGH*/
  342. case DRV_CIPHER_ECB:
  343. /* Load key */
  344. hw_desc_init(&desc[*seq_size]);
  345. set_cipher_mode(&desc[*seq_size], cipher_mode);
  346. set_cipher_config0(&desc[*seq_size], direction);
  347. if (flow_mode == S_DIN_to_AES) {
  348. if (cc_is_hw_key(tfm)) {
  349. set_hw_crypto_key(&desc[*seq_size],
  350. ctx_p->hw.key1_slot);
  351. } else {
  352. set_din_type(&desc[*seq_size], DMA_DLLI,
  353. key_dma_addr, ((key_len == 24) ?
  354. AES_MAX_KEY_SIZE :
  355. key_len), NS_BIT);
  356. }
  357. set_key_size_aes(&desc[*seq_size], key_len);
  358. } else {
  359. /*des*/
  360. set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
  361. key_len, NS_BIT);
  362. set_key_size_des(&desc[*seq_size], key_len);
  363. }
  364. set_flow_mode(&desc[*seq_size], flow_mode);
  365. set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
  366. (*seq_size)++;
  367. break;
  368. case DRV_CIPHER_XTS:
  369. case DRV_CIPHER_ESSIV:
  370. case DRV_CIPHER_BITLOCKER:
  371. /* Load AES key */
  372. hw_desc_init(&desc[*seq_size]);
  373. set_cipher_mode(&desc[*seq_size], cipher_mode);
  374. set_cipher_config0(&desc[*seq_size], direction);
  375. if (cc_is_hw_key(tfm)) {
  376. set_hw_crypto_key(&desc[*seq_size],
  377. ctx_p->hw.key1_slot);
  378. } else {
  379. set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
  380. (key_len / 2), NS_BIT);
  381. }
  382. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  383. set_flow_mode(&desc[*seq_size], flow_mode);
  384. set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
  385. (*seq_size)++;
  386. /* load XEX key */
  387. hw_desc_init(&desc[*seq_size]);
  388. set_cipher_mode(&desc[*seq_size], cipher_mode);
  389. set_cipher_config0(&desc[*seq_size], direction);
  390. if (cc_is_hw_key(tfm)) {
  391. set_hw_crypto_key(&desc[*seq_size],
  392. ctx_p->hw.key2_slot);
  393. } else {
  394. set_din_type(&desc[*seq_size], DMA_DLLI,
  395. (key_dma_addr + (key_len / 2)),
  396. (key_len / 2), NS_BIT);
  397. }
  398. set_xex_data_unit_size(&desc[*seq_size], du_size);
  399. set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
  400. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  401. set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
  402. (*seq_size)++;
  403. /* Set state */
  404. hw_desc_init(&desc[*seq_size]);
  405. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
  406. set_cipher_mode(&desc[*seq_size], cipher_mode);
  407. set_cipher_config0(&desc[*seq_size], direction);
  408. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  409. set_flow_mode(&desc[*seq_size], flow_mode);
  410. set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
  411. CC_AES_BLOCK_SIZE, NS_BIT);
  412. (*seq_size)++;
  413. break;
  414. default:
  415. dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
  416. }
  417. }
  418. static void cc_setup_cipher_data(struct crypto_tfm *tfm,
  419. struct cipher_req_ctx *req_ctx,
  420. struct scatterlist *dst,
  421. struct scatterlist *src, unsigned int nbytes,
  422. void *areq, struct cc_hw_desc desc[],
  423. unsigned int *seq_size)
  424. {
  425. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  426. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  427. unsigned int flow_mode = ctx_p->flow_mode;
  428. switch (ctx_p->flow_mode) {
  429. case S_DIN_to_AES:
  430. flow_mode = DIN_AES_DOUT;
  431. break;
  432. case S_DIN_to_DES:
  433. flow_mode = DIN_DES_DOUT;
  434. break;
  435. default:
  436. dev_err(dev, "invalid flow mode, flow_mode = %d\n", flow_mode);
  437. return;
  438. }
  439. /* Process */
  440. if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
  441. dev_dbg(dev, " data params addr %pad length 0x%X\n",
  442. &sg_dma_address(src), nbytes);
  443. dev_dbg(dev, " data params addr %pad length 0x%X\n",
  444. &sg_dma_address(dst), nbytes);
  445. hw_desc_init(&desc[*seq_size]);
  446. set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
  447. nbytes, NS_BIT);
  448. set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
  449. nbytes, NS_BIT, (!areq ? 0 : 1));
  450. if (areq)
  451. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  452. set_flow_mode(&desc[*seq_size], flow_mode);
  453. (*seq_size)++;
  454. } else {
  455. /* bypass */
  456. dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
  457. &req_ctx->mlli_params.mlli_dma_addr,
  458. req_ctx->mlli_params.mlli_len,
  459. (unsigned int)ctx_p->drvdata->mlli_sram_addr);
  460. hw_desc_init(&desc[*seq_size]);
  461. set_din_type(&desc[*seq_size], DMA_DLLI,
  462. req_ctx->mlli_params.mlli_dma_addr,
  463. req_ctx->mlli_params.mlli_len, NS_BIT);
  464. set_dout_sram(&desc[*seq_size],
  465. ctx_p->drvdata->mlli_sram_addr,
  466. req_ctx->mlli_params.mlli_len);
  467. set_flow_mode(&desc[*seq_size], BYPASS);
  468. (*seq_size)++;
  469. hw_desc_init(&desc[*seq_size]);
  470. set_din_type(&desc[*seq_size], DMA_MLLI,
  471. ctx_p->drvdata->mlli_sram_addr,
  472. req_ctx->in_mlli_nents, NS_BIT);
  473. if (req_ctx->out_nents == 0) {
  474. dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
  475. (unsigned int)ctx_p->drvdata->mlli_sram_addr,
  476. (unsigned int)ctx_p->drvdata->mlli_sram_addr);
  477. set_dout_mlli(&desc[*seq_size],
  478. ctx_p->drvdata->mlli_sram_addr,
  479. req_ctx->in_mlli_nents, NS_BIT,
  480. (!areq ? 0 : 1));
  481. } else {
  482. dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
  483. (unsigned int)ctx_p->drvdata->mlli_sram_addr,
  484. (unsigned int)ctx_p->drvdata->mlli_sram_addr +
  485. (u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
  486. set_dout_mlli(&desc[*seq_size],
  487. (ctx_p->drvdata->mlli_sram_addr +
  488. (LLI_ENTRY_BYTE_SIZE *
  489. req_ctx->in_mlli_nents)),
  490. req_ctx->out_mlli_nents, NS_BIT,
  491. (!areq ? 0 : 1));
  492. }
  493. if (areq)
  494. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  495. set_flow_mode(&desc[*seq_size], flow_mode);
  496. (*seq_size)++;
  497. }
  498. }
  499. static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
  500. {
  501. struct skcipher_request *req = (struct skcipher_request *)cc_req;
  502. struct scatterlist *dst = req->dst;
  503. struct scatterlist *src = req->src;
  504. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  505. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  506. unsigned int ivsize = crypto_skcipher_ivsize(tfm);
  507. cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
  508. kzfree(req_ctx->iv);
  509. /*
  510. * The crypto API expects us to set the req->iv to the last
  511. * ciphertext block. For encrypt, simply copy from the result.
  512. * For decrypt, we must copy from a saved buffer since this
  513. * could be an in-place decryption operation and the src is
  514. * lost by this point.
  515. */
  516. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  517. memcpy(req->iv, req_ctx->backup_info, ivsize);
  518. kzfree(req_ctx->backup_info);
  519. } else if (!err) {
  520. scatterwalk_map_and_copy(req->iv, req->dst,
  521. (req->cryptlen - ivsize),
  522. ivsize, 0);
  523. }
  524. skcipher_request_complete(req, err);
  525. }
  526. static int cc_cipher_process(struct skcipher_request *req,
  527. enum drv_crypto_direction direction)
  528. {
  529. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  530. struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
  531. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  532. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  533. struct scatterlist *dst = req->dst;
  534. struct scatterlist *src = req->src;
  535. unsigned int nbytes = req->cryptlen;
  536. void *iv = req->iv;
  537. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  538. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  539. struct cc_hw_desc desc[MAX_ABLKCIPHER_SEQ_LEN];
  540. struct cc_crypto_req cc_req = {};
  541. int rc, cts_restore_flag = 0;
  542. unsigned int seq_len = 0;
  543. gfp_t flags = cc_gfp_flags(&req->base);
  544. dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
  545. ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  546. "Encrypt" : "Decrypt"), req, iv, nbytes);
  547. /* STAT_PHASE_0: Init and sanity checks */
  548. /* TODO: check data length according to mode */
  549. if (validate_data_size(ctx_p, nbytes)) {
  550. dev_err(dev, "Unsupported data size %d.\n", nbytes);
  551. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
  552. rc = -EINVAL;
  553. goto exit_process;
  554. }
  555. if (nbytes == 0) {
  556. /* No data to process is valid */
  557. rc = 0;
  558. goto exit_process;
  559. }
  560. /* The IV we are handed may be allocted from the stack so
  561. * we must copy it to a DMAable buffer before use.
  562. */
  563. req_ctx->iv = kmemdup(iv, ivsize, flags);
  564. if (!req_ctx->iv) {
  565. rc = -ENOMEM;
  566. goto exit_process;
  567. }
  568. /*For CTS in case of data size aligned to 16 use CBC mode*/
  569. if (((nbytes % AES_BLOCK_SIZE) == 0) &&
  570. ctx_p->cipher_mode == DRV_CIPHER_CBC_CTS) {
  571. ctx_p->cipher_mode = DRV_CIPHER_CBC;
  572. cts_restore_flag = 1;
  573. }
  574. /* Setup request structure */
  575. cc_req.user_cb = (void *)cc_cipher_complete;
  576. cc_req.user_arg = (void *)req;
  577. #ifdef ENABLE_CYCLE_COUNT
  578. cc_req.op_type = (direction == DRV_CRYPTO_DIRECTION_DECRYPT) ?
  579. STAT_OP_TYPE_DECODE : STAT_OP_TYPE_ENCODE;
  580. #endif
  581. /* Setup request context */
  582. req_ctx->gen_ctx.op_type = direction;
  583. /* STAT_PHASE_1: Map buffers */
  584. rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
  585. req_ctx->iv, src, dst, flags);
  586. if (rc) {
  587. dev_err(dev, "map_request() failed\n");
  588. goto exit_process;
  589. }
  590. /* STAT_PHASE_2: Create sequence */
  591. /* Setup processing */
  592. cc_setup_cipher_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
  593. /* Data processing */
  594. cc_setup_cipher_data(tfm, req_ctx, dst, src, nbytes, req, desc,
  595. &seq_len);
  596. /* do we need to generate IV? */
  597. if (req_ctx->is_giv) {
  598. cc_req.ivgen_dma_addr[0] = req_ctx->gen_ctx.iv_dma_addr;
  599. cc_req.ivgen_dma_addr_len = 1;
  600. /* set the IV size (8/16 B long)*/
  601. cc_req.ivgen_size = ivsize;
  602. }
  603. /* STAT_PHASE_3: Lock HW and push sequence */
  604. rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
  605. &req->base);
  606. if (rc != -EINPROGRESS && rc != -EBUSY) {
  607. /* Failed to send the request or request completed
  608. * synchronously
  609. */
  610. cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
  611. }
  612. exit_process:
  613. if (cts_restore_flag)
  614. ctx_p->cipher_mode = DRV_CIPHER_CBC_CTS;
  615. if (rc != -EINPROGRESS && rc != -EBUSY) {
  616. kzfree(req_ctx->backup_info);
  617. kzfree(req_ctx->iv);
  618. }
  619. return rc;
  620. }
  621. static int cc_cipher_encrypt(struct skcipher_request *req)
  622. {
  623. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  624. req_ctx->is_giv = false;
  625. req_ctx->backup_info = NULL;
  626. return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  627. }
  628. static int cc_cipher_decrypt(struct skcipher_request *req)
  629. {
  630. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  631. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  632. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  633. gfp_t flags = cc_gfp_flags(&req->base);
  634. /*
  635. * Allocate and save the last IV sized bytes of the source, which will
  636. * be lost in case of in-place decryption and might be needed for CTS.
  637. */
  638. req_ctx->backup_info = kmalloc(ivsize, flags);
  639. if (!req_ctx->backup_info)
  640. return -ENOMEM;
  641. scatterwalk_map_and_copy(req_ctx->backup_info, req->src,
  642. (req->cryptlen - ivsize), ivsize, 0);
  643. req_ctx->is_giv = false;
  644. return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  645. }
  646. /* Block cipher alg */
  647. static const struct cc_alg_template skcipher_algs[] = {
  648. {
  649. .name = "xts(aes)",
  650. .driver_name = "xts-aes-ccree",
  651. .blocksize = AES_BLOCK_SIZE,
  652. .template_skcipher = {
  653. .setkey = cc_cipher_setkey,
  654. .encrypt = cc_cipher_encrypt,
  655. .decrypt = cc_cipher_decrypt,
  656. .min_keysize = AES_MIN_KEY_SIZE * 2,
  657. .max_keysize = AES_MAX_KEY_SIZE * 2,
  658. .ivsize = AES_BLOCK_SIZE,
  659. },
  660. .cipher_mode = DRV_CIPHER_XTS,
  661. .flow_mode = S_DIN_to_AES,
  662. .min_hw_rev = CC_HW_REV_630,
  663. },
  664. {
  665. .name = "xts512(aes)",
  666. .driver_name = "xts-aes-du512-ccree",
  667. .blocksize = AES_BLOCK_SIZE,
  668. .template_skcipher = {
  669. .setkey = cc_cipher_setkey,
  670. .encrypt = cc_cipher_encrypt,
  671. .decrypt = cc_cipher_decrypt,
  672. .min_keysize = AES_MIN_KEY_SIZE * 2,
  673. .max_keysize = AES_MAX_KEY_SIZE * 2,
  674. .ivsize = AES_BLOCK_SIZE,
  675. },
  676. .cipher_mode = DRV_CIPHER_XTS,
  677. .flow_mode = S_DIN_to_AES,
  678. .data_unit = 512,
  679. .min_hw_rev = CC_HW_REV_712,
  680. },
  681. {
  682. .name = "xts4096(aes)",
  683. .driver_name = "xts-aes-du4096-ccree",
  684. .blocksize = AES_BLOCK_SIZE,
  685. .template_skcipher = {
  686. .setkey = cc_cipher_setkey,
  687. .encrypt = cc_cipher_encrypt,
  688. .decrypt = cc_cipher_decrypt,
  689. .min_keysize = AES_MIN_KEY_SIZE * 2,
  690. .max_keysize = AES_MAX_KEY_SIZE * 2,
  691. .ivsize = AES_BLOCK_SIZE,
  692. },
  693. .cipher_mode = DRV_CIPHER_XTS,
  694. .flow_mode = S_DIN_to_AES,
  695. .data_unit = 4096,
  696. .min_hw_rev = CC_HW_REV_712,
  697. },
  698. {
  699. .name = "essiv(aes)",
  700. .driver_name = "essiv-aes-ccree",
  701. .blocksize = AES_BLOCK_SIZE,
  702. .template_skcipher = {
  703. .setkey = cc_cipher_setkey,
  704. .encrypt = cc_cipher_encrypt,
  705. .decrypt = cc_cipher_decrypt,
  706. .min_keysize = AES_MIN_KEY_SIZE * 2,
  707. .max_keysize = AES_MAX_KEY_SIZE * 2,
  708. .ivsize = AES_BLOCK_SIZE,
  709. },
  710. .cipher_mode = DRV_CIPHER_ESSIV,
  711. .flow_mode = S_DIN_to_AES,
  712. .min_hw_rev = CC_HW_REV_712,
  713. },
  714. {
  715. .name = "essiv512(aes)",
  716. .driver_name = "essiv-aes-du512-ccree",
  717. .blocksize = AES_BLOCK_SIZE,
  718. .template_skcipher = {
  719. .setkey = cc_cipher_setkey,
  720. .encrypt = cc_cipher_encrypt,
  721. .decrypt = cc_cipher_decrypt,
  722. .min_keysize = AES_MIN_KEY_SIZE * 2,
  723. .max_keysize = AES_MAX_KEY_SIZE * 2,
  724. .ivsize = AES_BLOCK_SIZE,
  725. },
  726. .cipher_mode = DRV_CIPHER_ESSIV,
  727. .flow_mode = S_DIN_to_AES,
  728. .data_unit = 512,
  729. .min_hw_rev = CC_HW_REV_712,
  730. },
  731. {
  732. .name = "essiv4096(aes)",
  733. .driver_name = "essiv-aes-du4096-ccree",
  734. .blocksize = AES_BLOCK_SIZE,
  735. .template_skcipher = {
  736. .setkey = cc_cipher_setkey,
  737. .encrypt = cc_cipher_encrypt,
  738. .decrypt = cc_cipher_decrypt,
  739. .min_keysize = AES_MIN_KEY_SIZE * 2,
  740. .max_keysize = AES_MAX_KEY_SIZE * 2,
  741. .ivsize = AES_BLOCK_SIZE,
  742. },
  743. .cipher_mode = DRV_CIPHER_ESSIV,
  744. .flow_mode = S_DIN_to_AES,
  745. .data_unit = 4096,
  746. .min_hw_rev = CC_HW_REV_712,
  747. },
  748. {
  749. .name = "bitlocker(aes)",
  750. .driver_name = "bitlocker-aes-ccree",
  751. .blocksize = AES_BLOCK_SIZE,
  752. .template_skcipher = {
  753. .setkey = cc_cipher_setkey,
  754. .encrypt = cc_cipher_encrypt,
  755. .decrypt = cc_cipher_decrypt,
  756. .min_keysize = AES_MIN_KEY_SIZE * 2,
  757. .max_keysize = AES_MAX_KEY_SIZE * 2,
  758. .ivsize = AES_BLOCK_SIZE,
  759. },
  760. .cipher_mode = DRV_CIPHER_BITLOCKER,
  761. .flow_mode = S_DIN_to_AES,
  762. .min_hw_rev = CC_HW_REV_712,
  763. },
  764. {
  765. .name = "bitlocker512(aes)",
  766. .driver_name = "bitlocker-aes-du512-ccree",
  767. .blocksize = AES_BLOCK_SIZE,
  768. .template_skcipher = {
  769. .setkey = cc_cipher_setkey,
  770. .encrypt = cc_cipher_encrypt,
  771. .decrypt = cc_cipher_decrypt,
  772. .min_keysize = AES_MIN_KEY_SIZE * 2,
  773. .max_keysize = AES_MAX_KEY_SIZE * 2,
  774. .ivsize = AES_BLOCK_SIZE,
  775. },
  776. .cipher_mode = DRV_CIPHER_BITLOCKER,
  777. .flow_mode = S_DIN_to_AES,
  778. .data_unit = 512,
  779. .min_hw_rev = CC_HW_REV_712,
  780. },
  781. {
  782. .name = "bitlocker4096(aes)",
  783. .driver_name = "bitlocker-aes-du4096-ccree",
  784. .blocksize = AES_BLOCK_SIZE,
  785. .template_skcipher = {
  786. .setkey = cc_cipher_setkey,
  787. .encrypt = cc_cipher_encrypt,
  788. .decrypt = cc_cipher_decrypt,
  789. .min_keysize = AES_MIN_KEY_SIZE * 2,
  790. .max_keysize = AES_MAX_KEY_SIZE * 2,
  791. .ivsize = AES_BLOCK_SIZE,
  792. },
  793. .cipher_mode = DRV_CIPHER_BITLOCKER,
  794. .flow_mode = S_DIN_to_AES,
  795. .data_unit = 4096,
  796. .min_hw_rev = CC_HW_REV_712,
  797. },
  798. {
  799. .name = "ecb(aes)",
  800. .driver_name = "ecb-aes-ccree",
  801. .blocksize = AES_BLOCK_SIZE,
  802. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  803. .template_skcipher = {
  804. .setkey = cc_cipher_setkey,
  805. .encrypt = cc_cipher_encrypt,
  806. .decrypt = cc_cipher_decrypt,
  807. .min_keysize = AES_MIN_KEY_SIZE,
  808. .max_keysize = AES_MAX_KEY_SIZE,
  809. .ivsize = 0,
  810. },
  811. .cipher_mode = DRV_CIPHER_ECB,
  812. .flow_mode = S_DIN_to_AES,
  813. .min_hw_rev = CC_HW_REV_630,
  814. },
  815. {
  816. .name = "cbc(aes)",
  817. .driver_name = "cbc-aes-ccree",
  818. .blocksize = AES_BLOCK_SIZE,
  819. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  820. .template_skcipher = {
  821. .setkey = cc_cipher_setkey,
  822. .encrypt = cc_cipher_encrypt,
  823. .decrypt = cc_cipher_decrypt,
  824. .min_keysize = AES_MIN_KEY_SIZE,
  825. .max_keysize = AES_MAX_KEY_SIZE,
  826. .ivsize = AES_BLOCK_SIZE,
  827. },
  828. .cipher_mode = DRV_CIPHER_CBC,
  829. .flow_mode = S_DIN_to_AES,
  830. .min_hw_rev = CC_HW_REV_630,
  831. },
  832. {
  833. .name = "ofb(aes)",
  834. .driver_name = "ofb-aes-ccree",
  835. .blocksize = AES_BLOCK_SIZE,
  836. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  837. .template_skcipher = {
  838. .setkey = cc_cipher_setkey,
  839. .encrypt = cc_cipher_encrypt,
  840. .decrypt = cc_cipher_decrypt,
  841. .min_keysize = AES_MIN_KEY_SIZE,
  842. .max_keysize = AES_MAX_KEY_SIZE,
  843. .ivsize = AES_BLOCK_SIZE,
  844. },
  845. .cipher_mode = DRV_CIPHER_OFB,
  846. .flow_mode = S_DIN_to_AES,
  847. .min_hw_rev = CC_HW_REV_630,
  848. },
  849. {
  850. .name = "cts1(cbc(aes))",
  851. .driver_name = "cts1-cbc-aes-ccree",
  852. .blocksize = AES_BLOCK_SIZE,
  853. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  854. .template_skcipher = {
  855. .setkey = cc_cipher_setkey,
  856. .encrypt = cc_cipher_encrypt,
  857. .decrypt = cc_cipher_decrypt,
  858. .min_keysize = AES_MIN_KEY_SIZE,
  859. .max_keysize = AES_MAX_KEY_SIZE,
  860. .ivsize = AES_BLOCK_SIZE,
  861. },
  862. .cipher_mode = DRV_CIPHER_CBC_CTS,
  863. .flow_mode = S_DIN_to_AES,
  864. .min_hw_rev = CC_HW_REV_630,
  865. },
  866. {
  867. .name = "ctr(aes)",
  868. .driver_name = "ctr-aes-ccree",
  869. .blocksize = 1,
  870. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  871. .template_skcipher = {
  872. .setkey = cc_cipher_setkey,
  873. .encrypt = cc_cipher_encrypt,
  874. .decrypt = cc_cipher_decrypt,
  875. .min_keysize = AES_MIN_KEY_SIZE,
  876. .max_keysize = AES_MAX_KEY_SIZE,
  877. .ivsize = AES_BLOCK_SIZE,
  878. },
  879. .cipher_mode = DRV_CIPHER_CTR,
  880. .flow_mode = S_DIN_to_AES,
  881. .min_hw_rev = CC_HW_REV_630,
  882. },
  883. {
  884. .name = "cbc(des3_ede)",
  885. .driver_name = "cbc-3des-ccree",
  886. .blocksize = DES3_EDE_BLOCK_SIZE,
  887. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  888. .template_skcipher = {
  889. .setkey = cc_cipher_setkey,
  890. .encrypt = cc_cipher_encrypt,
  891. .decrypt = cc_cipher_decrypt,
  892. .min_keysize = DES3_EDE_KEY_SIZE,
  893. .max_keysize = DES3_EDE_KEY_SIZE,
  894. .ivsize = DES3_EDE_BLOCK_SIZE,
  895. },
  896. .cipher_mode = DRV_CIPHER_CBC,
  897. .flow_mode = S_DIN_to_DES,
  898. .min_hw_rev = CC_HW_REV_630,
  899. },
  900. {
  901. .name = "ecb(des3_ede)",
  902. .driver_name = "ecb-3des-ccree",
  903. .blocksize = DES3_EDE_BLOCK_SIZE,
  904. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  905. .template_skcipher = {
  906. .setkey = cc_cipher_setkey,
  907. .encrypt = cc_cipher_encrypt,
  908. .decrypt = cc_cipher_decrypt,
  909. .min_keysize = DES3_EDE_KEY_SIZE,
  910. .max_keysize = DES3_EDE_KEY_SIZE,
  911. .ivsize = 0,
  912. },
  913. .cipher_mode = DRV_CIPHER_ECB,
  914. .flow_mode = S_DIN_to_DES,
  915. .min_hw_rev = CC_HW_REV_630,
  916. },
  917. {
  918. .name = "cbc(des)",
  919. .driver_name = "cbc-des-ccree",
  920. .blocksize = DES_BLOCK_SIZE,
  921. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  922. .template_skcipher = {
  923. .setkey = cc_cipher_setkey,
  924. .encrypt = cc_cipher_encrypt,
  925. .decrypt = cc_cipher_decrypt,
  926. .min_keysize = DES_KEY_SIZE,
  927. .max_keysize = DES_KEY_SIZE,
  928. .ivsize = DES_BLOCK_SIZE,
  929. },
  930. .cipher_mode = DRV_CIPHER_CBC,
  931. .flow_mode = S_DIN_to_DES,
  932. .min_hw_rev = CC_HW_REV_630,
  933. },
  934. {
  935. .name = "ecb(des)",
  936. .driver_name = "ecb-des-ccree",
  937. .blocksize = DES_BLOCK_SIZE,
  938. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  939. .template_skcipher = {
  940. .setkey = cc_cipher_setkey,
  941. .encrypt = cc_cipher_encrypt,
  942. .decrypt = cc_cipher_decrypt,
  943. .min_keysize = DES_KEY_SIZE,
  944. .max_keysize = DES_KEY_SIZE,
  945. .ivsize = 0,
  946. },
  947. .cipher_mode = DRV_CIPHER_ECB,
  948. .flow_mode = S_DIN_to_DES,
  949. .min_hw_rev = CC_HW_REV_630,
  950. },
  951. };
  952. static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
  953. struct device *dev)
  954. {
  955. struct cc_crypto_alg *t_alg;
  956. struct skcipher_alg *alg;
  957. t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
  958. if (!t_alg)
  959. return ERR_PTR(-ENOMEM);
  960. alg = &t_alg->skcipher_alg;
  961. memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
  962. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  963. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  964. tmpl->driver_name);
  965. alg->base.cra_module = THIS_MODULE;
  966. alg->base.cra_priority = CC_CRA_PRIO;
  967. alg->base.cra_blocksize = tmpl->blocksize;
  968. alg->base.cra_alignmask = 0;
  969. alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
  970. alg->base.cra_init = cc_cipher_init;
  971. alg->base.cra_exit = cc_cipher_exit;
  972. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
  973. CRYPTO_ALG_TYPE_SKCIPHER;
  974. t_alg->cipher_mode = tmpl->cipher_mode;
  975. t_alg->flow_mode = tmpl->flow_mode;
  976. t_alg->data_unit = tmpl->data_unit;
  977. return t_alg;
  978. }
  979. int cc_cipher_free(struct cc_drvdata *drvdata)
  980. {
  981. struct cc_crypto_alg *t_alg, *n;
  982. struct cc_cipher_handle *cipher_handle = drvdata->cipher_handle;
  983. if (cipher_handle) {
  984. /* Remove registered algs */
  985. list_for_each_entry_safe(t_alg, n, &cipher_handle->alg_list,
  986. entry) {
  987. crypto_unregister_skcipher(&t_alg->skcipher_alg);
  988. list_del(&t_alg->entry);
  989. kfree(t_alg);
  990. }
  991. kfree(cipher_handle);
  992. drvdata->cipher_handle = NULL;
  993. }
  994. return 0;
  995. }
  996. int cc_cipher_alloc(struct cc_drvdata *drvdata)
  997. {
  998. struct cc_cipher_handle *cipher_handle;
  999. struct cc_crypto_alg *t_alg;
  1000. struct device *dev = drvdata_to_dev(drvdata);
  1001. int rc = -ENOMEM;
  1002. int alg;
  1003. cipher_handle = kmalloc(sizeof(*cipher_handle), GFP_KERNEL);
  1004. if (!cipher_handle)
  1005. return -ENOMEM;
  1006. INIT_LIST_HEAD(&cipher_handle->alg_list);
  1007. drvdata->cipher_handle = cipher_handle;
  1008. /* Linux crypto */
  1009. dev_dbg(dev, "Number of algorithms = %zu\n",
  1010. ARRAY_SIZE(skcipher_algs));
  1011. for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
  1012. if (skcipher_algs[alg].min_hw_rev > drvdata->hw_rev)
  1013. continue;
  1014. dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
  1015. t_alg = cc_create_alg(&skcipher_algs[alg], dev);
  1016. if (IS_ERR(t_alg)) {
  1017. rc = PTR_ERR(t_alg);
  1018. dev_err(dev, "%s alg allocation failed\n",
  1019. skcipher_algs[alg].driver_name);
  1020. goto fail0;
  1021. }
  1022. t_alg->drvdata = drvdata;
  1023. dev_dbg(dev, "registering %s\n",
  1024. skcipher_algs[alg].driver_name);
  1025. rc = crypto_register_skcipher(&t_alg->skcipher_alg);
  1026. dev_dbg(dev, "%s alg registration rc = %x\n",
  1027. t_alg->skcipher_alg.base.cra_driver_name, rc);
  1028. if (rc) {
  1029. dev_err(dev, "%s alg registration failed\n",
  1030. t_alg->skcipher_alg.base.cra_driver_name);
  1031. kfree(t_alg);
  1032. goto fail0;
  1033. } else {
  1034. list_add_tail(&t_alg->entry,
  1035. &cipher_handle->alg_list);
  1036. dev_dbg(dev, "Registered %s\n",
  1037. t_alg->skcipher_alg.base.cra_driver_name);
  1038. }
  1039. }
  1040. return 0;
  1041. fail0:
  1042. cc_cipher_free(drvdata);
  1043. return rc;
  1044. }