cc_cipher.c 39 KB

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