cc_cipher.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2019 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/internal/des.h>
  8. #include <crypto/xts.h>
  9. #include <crypto/sm4.h>
  10. #include <crypto/scatterwalk.h>
  11. #include "cc_driver.h"
  12. #include "cc_lli_defs.h"
  13. #include "cc_buffer_mgr.h"
  14. #include "cc_cipher.h"
  15. #include "cc_request_mgr.h"
  16. #define MAX_ABLKCIPHER_SEQ_LEN 6
  17. #define template_skcipher template_u.skcipher
  18. struct cc_cipher_handle {
  19. struct list_head alg_list;
  20. };
  21. struct cc_user_key_info {
  22. u8 *key;
  23. dma_addr_t key_dma_addr;
  24. };
  25. struct cc_hw_key_info {
  26. enum cc_hw_crypto_key key1_slot;
  27. enum cc_hw_crypto_key key2_slot;
  28. };
  29. struct cc_cpp_key_info {
  30. u8 slot;
  31. enum cc_cpp_alg alg;
  32. };
  33. enum cc_key_type {
  34. CC_UNPROTECTED_KEY, /* User key */
  35. CC_HW_PROTECTED_KEY, /* HW (FDE) key */
  36. CC_POLICY_PROTECTED_KEY, /* CPP key */
  37. CC_INVALID_PROTECTED_KEY /* Invalid key */
  38. };
  39. struct cc_cipher_ctx {
  40. struct cc_drvdata *drvdata;
  41. int keylen;
  42. int key_round_number;
  43. int cipher_mode;
  44. int flow_mode;
  45. unsigned int flags;
  46. enum cc_key_type key_type;
  47. struct cc_user_key_info user;
  48. union {
  49. struct cc_hw_key_info hw;
  50. struct cc_cpp_key_info cpp;
  51. };
  52. struct crypto_shash *shash_tfm;
  53. };
  54. static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
  55. static inline enum cc_key_type cc_key_type(struct crypto_tfm *tfm)
  56. {
  57. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  58. return ctx_p->key_type;
  59. }
  60. static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
  61. {
  62. switch (ctx_p->flow_mode) {
  63. case S_DIN_to_AES:
  64. switch (size) {
  65. case CC_AES_128_BIT_KEY_SIZE:
  66. case CC_AES_192_BIT_KEY_SIZE:
  67. if (ctx_p->cipher_mode != DRV_CIPHER_XTS &&
  68. ctx_p->cipher_mode != DRV_CIPHER_ESSIV &&
  69. ctx_p->cipher_mode != DRV_CIPHER_BITLOCKER)
  70. return 0;
  71. break;
  72. case CC_AES_256_BIT_KEY_SIZE:
  73. return 0;
  74. case (CC_AES_192_BIT_KEY_SIZE * 2):
  75. case (CC_AES_256_BIT_KEY_SIZE * 2):
  76. if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
  77. ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
  78. ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)
  79. return 0;
  80. break;
  81. default:
  82. break;
  83. }
  84. break;
  85. case S_DIN_to_DES:
  86. if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
  87. return 0;
  88. break;
  89. case S_DIN_to_SM4:
  90. if (size == SM4_KEY_SIZE)
  91. return 0;
  92. default:
  93. break;
  94. }
  95. return -EINVAL;
  96. }
  97. static int validate_data_size(struct cc_cipher_ctx *ctx_p,
  98. unsigned int size)
  99. {
  100. switch (ctx_p->flow_mode) {
  101. case S_DIN_to_AES:
  102. switch (ctx_p->cipher_mode) {
  103. case DRV_CIPHER_XTS:
  104. case DRV_CIPHER_CBC_CTS:
  105. if (size >= AES_BLOCK_SIZE)
  106. return 0;
  107. break;
  108. case DRV_CIPHER_OFB:
  109. case DRV_CIPHER_CTR:
  110. return 0;
  111. case DRV_CIPHER_ECB:
  112. case DRV_CIPHER_CBC:
  113. case DRV_CIPHER_ESSIV:
  114. case DRV_CIPHER_BITLOCKER:
  115. if (IS_ALIGNED(size, AES_BLOCK_SIZE))
  116. return 0;
  117. break;
  118. default:
  119. break;
  120. }
  121. break;
  122. case S_DIN_to_DES:
  123. if (IS_ALIGNED(size, DES_BLOCK_SIZE))
  124. return 0;
  125. break;
  126. case S_DIN_to_SM4:
  127. switch (ctx_p->cipher_mode) {
  128. case DRV_CIPHER_CTR:
  129. return 0;
  130. case DRV_CIPHER_ECB:
  131. case DRV_CIPHER_CBC:
  132. if (IS_ALIGNED(size, SM4_BLOCK_SIZE))
  133. return 0;
  134. default:
  135. break;
  136. }
  137. default:
  138. break;
  139. }
  140. return -EINVAL;
  141. }
  142. static int cc_cipher_init(struct crypto_tfm *tfm)
  143. {
  144. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  145. struct cc_crypto_alg *cc_alg =
  146. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  147. skcipher_alg.base);
  148. struct device *dev = drvdata_to_dev(cc_alg->drvdata);
  149. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  150. dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
  151. crypto_tfm_alg_name(tfm));
  152. crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
  153. sizeof(struct cipher_req_ctx));
  154. ctx_p->cipher_mode = cc_alg->cipher_mode;
  155. ctx_p->flow_mode = cc_alg->flow_mode;
  156. ctx_p->drvdata = cc_alg->drvdata;
  157. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  158. /* Alloc hash tfm for essiv */
  159. ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
  160. if (IS_ERR(ctx_p->shash_tfm)) {
  161. dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
  162. return PTR_ERR(ctx_p->shash_tfm);
  163. }
  164. }
  165. /* Allocate key buffer, cache line aligned */
  166. ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL);
  167. if (!ctx_p->user.key)
  168. goto free_shash;
  169. dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
  170. ctx_p->user.key);
  171. /* Map key buffer */
  172. ctx_p->user.key_dma_addr = dma_map_single(dev, (void *)ctx_p->user.key,
  173. max_key_buf_size,
  174. DMA_TO_DEVICE);
  175. if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
  176. dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
  177. max_key_buf_size, ctx_p->user.key);
  178. goto free_key;
  179. }
  180. dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
  181. max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
  182. return 0;
  183. free_key:
  184. kfree(ctx_p->user.key);
  185. free_shash:
  186. crypto_free_shash(ctx_p->shash_tfm);
  187. return -ENOMEM;
  188. }
  189. static void cc_cipher_exit(struct crypto_tfm *tfm)
  190. {
  191. struct crypto_alg *alg = tfm->__crt_alg;
  192. struct cc_crypto_alg *cc_alg =
  193. container_of(alg, struct cc_crypto_alg,
  194. skcipher_alg.base);
  195. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  196. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  197. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  198. dev_dbg(dev, "Clearing context @%p for %s\n",
  199. crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
  200. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  201. /* Free hash tfm for essiv */
  202. crypto_free_shash(ctx_p->shash_tfm);
  203. ctx_p->shash_tfm = NULL;
  204. }
  205. /* Unmap key buffer */
  206. dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
  207. DMA_TO_DEVICE);
  208. dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
  209. &ctx_p->user.key_dma_addr);
  210. /* Free key buffer in context */
  211. kzfree(ctx_p->user.key);
  212. dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
  213. }
  214. struct tdes_keys {
  215. u8 key1[DES_KEY_SIZE];
  216. u8 key2[DES_KEY_SIZE];
  217. u8 key3[DES_KEY_SIZE];
  218. };
  219. static enum cc_hw_crypto_key cc_slot_to_hw_key(u8 slot_num)
  220. {
  221. switch (slot_num) {
  222. case 0:
  223. return KFDE0_KEY;
  224. case 1:
  225. return KFDE1_KEY;
  226. case 2:
  227. return KFDE2_KEY;
  228. case 3:
  229. return KFDE3_KEY;
  230. }
  231. return END_OF_KEYS;
  232. }
  233. static u8 cc_slot_to_cpp_key(u8 slot_num)
  234. {
  235. return (slot_num - CC_FIRST_CPP_KEY_SLOT);
  236. }
  237. static inline enum cc_key_type cc_slot_to_key_type(u8 slot_num)
  238. {
  239. if (slot_num >= CC_FIRST_HW_KEY_SLOT && slot_num <= CC_LAST_HW_KEY_SLOT)
  240. return CC_HW_PROTECTED_KEY;
  241. else if (slot_num >= CC_FIRST_CPP_KEY_SLOT &&
  242. slot_num <= CC_LAST_CPP_KEY_SLOT)
  243. return CC_POLICY_PROTECTED_KEY;
  244. else
  245. return CC_INVALID_PROTECTED_KEY;
  246. }
  247. static int cc_cipher_sethkey(struct crypto_skcipher *sktfm, const u8 *key,
  248. unsigned int keylen)
  249. {
  250. struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
  251. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  252. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  253. struct cc_hkey_info hki;
  254. dev_dbg(dev, "Setting HW key in context @%p for %s. keylen=%u\n",
  255. ctx_p, crypto_tfm_alg_name(tfm), keylen);
  256. dump_byte_array("key", (u8 *)key, keylen);
  257. /* STAT_PHASE_0: Init and sanity checks */
  258. /* This check the size of the protected key token */
  259. if (keylen != sizeof(hki)) {
  260. dev_err(dev, "Unsupported protected key size %d.\n", keylen);
  261. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  262. return -EINVAL;
  263. }
  264. memcpy(&hki, key, keylen);
  265. /* The real key len for crypto op is the size of the HW key
  266. * referenced by the HW key slot, not the hardware key token
  267. */
  268. keylen = hki.keylen;
  269. if (validate_keys_sizes(ctx_p, keylen)) {
  270. dev_err(dev, "Unsupported key size %d.\n", keylen);
  271. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  272. return -EINVAL;
  273. }
  274. ctx_p->keylen = keylen;
  275. switch (cc_slot_to_key_type(hki.hw_key1)) {
  276. case CC_HW_PROTECTED_KEY:
  277. if (ctx_p->flow_mode == S_DIN_to_SM4) {
  278. dev_err(dev, "Only AES HW protected keys are supported\n");
  279. return -EINVAL;
  280. }
  281. ctx_p->hw.key1_slot = cc_slot_to_hw_key(hki.hw_key1);
  282. if (ctx_p->hw.key1_slot == END_OF_KEYS) {
  283. dev_err(dev, "Unsupported hw key1 number (%d)\n",
  284. hki.hw_key1);
  285. return -EINVAL;
  286. }
  287. if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
  288. ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
  289. ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER) {
  290. if (hki.hw_key1 == hki.hw_key2) {
  291. dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
  292. hki.hw_key1, hki.hw_key2);
  293. return -EINVAL;
  294. }
  295. ctx_p->hw.key2_slot = cc_slot_to_hw_key(hki.hw_key2);
  296. if (ctx_p->hw.key2_slot == END_OF_KEYS) {
  297. dev_err(dev, "Unsupported hw key2 number (%d)\n",
  298. hki.hw_key2);
  299. return -EINVAL;
  300. }
  301. }
  302. ctx_p->key_type = CC_HW_PROTECTED_KEY;
  303. dev_dbg(dev, "HW protected key %d/%d set\n.",
  304. ctx_p->hw.key1_slot, ctx_p->hw.key2_slot);
  305. break;
  306. case CC_POLICY_PROTECTED_KEY:
  307. if (ctx_p->drvdata->hw_rev < CC_HW_REV_713) {
  308. dev_err(dev, "CPP keys not supported in this hardware revision.\n");
  309. return -EINVAL;
  310. }
  311. if (ctx_p->cipher_mode != DRV_CIPHER_CBC &&
  312. ctx_p->cipher_mode != DRV_CIPHER_CTR) {
  313. dev_err(dev, "CPP keys only supported in CBC or CTR modes.\n");
  314. return -EINVAL;
  315. }
  316. ctx_p->cpp.slot = cc_slot_to_cpp_key(hki.hw_key1);
  317. if (ctx_p->flow_mode == S_DIN_to_AES)
  318. ctx_p->cpp.alg = CC_CPP_AES;
  319. else /* Must be SM4 since due to sethkey registration */
  320. ctx_p->cpp.alg = CC_CPP_SM4;
  321. ctx_p->key_type = CC_POLICY_PROTECTED_KEY;
  322. dev_dbg(dev, "policy protected key alg: %d slot: %d.\n",
  323. ctx_p->cpp.alg, ctx_p->cpp.slot);
  324. break;
  325. default:
  326. dev_err(dev, "Unsupported protected key (%d)\n", hki.hw_key1);
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
  332. unsigned int keylen)
  333. {
  334. struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
  335. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  336. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  337. struct cc_crypto_alg *cc_alg =
  338. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  339. skcipher_alg.base);
  340. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  341. dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
  342. ctx_p, crypto_tfm_alg_name(tfm), keylen);
  343. dump_byte_array("key", (u8 *)key, keylen);
  344. /* STAT_PHASE_0: Init and sanity checks */
  345. if (validate_keys_sizes(ctx_p, keylen)) {
  346. dev_err(dev, "Unsupported key size %d.\n", keylen);
  347. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  348. return -EINVAL;
  349. }
  350. ctx_p->key_type = CC_UNPROTECTED_KEY;
  351. /*
  352. * Verify DES weak keys
  353. * Note that we're dropping the expanded key since the
  354. * HW does the expansion on its own.
  355. */
  356. if (ctx_p->flow_mode == S_DIN_to_DES) {
  357. if ((keylen == DES3_EDE_KEY_SIZE &&
  358. verify_skcipher_des3_key(sktfm, key)) ||
  359. verify_skcipher_des_key(sktfm, key)) {
  360. dev_dbg(dev, "weak DES key");
  361. return -EINVAL;
  362. }
  363. }
  364. if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
  365. xts_check_key(tfm, key, keylen)) {
  366. dev_dbg(dev, "weak XTS key");
  367. return -EINVAL;
  368. }
  369. /* STAT_PHASE_1: Copy key to ctx */
  370. dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
  371. max_key_buf_size, DMA_TO_DEVICE);
  372. memcpy(ctx_p->user.key, key, keylen);
  373. if (keylen == 24)
  374. memset(ctx_p->user.key + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
  375. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  376. /* sha256 for key2 - use sw implementation */
  377. int key_len = keylen >> 1;
  378. int err;
  379. SHASH_DESC_ON_STACK(desc, ctx_p->shash_tfm);
  380. desc->tfm = ctx_p->shash_tfm;
  381. err = crypto_shash_digest(desc, ctx_p->user.key, key_len,
  382. ctx_p->user.key + key_len);
  383. if (err) {
  384. dev_err(dev, "Failed to hash ESSIV key.\n");
  385. return err;
  386. }
  387. }
  388. dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
  389. max_key_buf_size, DMA_TO_DEVICE);
  390. ctx_p->keylen = keylen;
  391. dev_dbg(dev, "return safely");
  392. return 0;
  393. }
  394. static int cc_out_setup_mode(struct cc_cipher_ctx *ctx_p)
  395. {
  396. switch (ctx_p->flow_mode) {
  397. case S_DIN_to_AES:
  398. return S_AES_to_DOUT;
  399. case S_DIN_to_DES:
  400. return S_DES_to_DOUT;
  401. case S_DIN_to_SM4:
  402. return S_SM4_to_DOUT;
  403. default:
  404. return ctx_p->flow_mode;
  405. }
  406. }
  407. static void cc_setup_readiv_desc(struct crypto_tfm *tfm,
  408. struct cipher_req_ctx *req_ctx,
  409. unsigned int ivsize, struct cc_hw_desc desc[],
  410. unsigned int *seq_size)
  411. {
  412. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  413. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  414. int cipher_mode = ctx_p->cipher_mode;
  415. int flow_mode = cc_out_setup_mode(ctx_p);
  416. int direction = req_ctx->gen_ctx.op_type;
  417. dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
  418. if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY)
  419. return;
  420. switch (cipher_mode) {
  421. case DRV_CIPHER_ECB:
  422. break;
  423. case DRV_CIPHER_CBC:
  424. case DRV_CIPHER_CBC_CTS:
  425. case DRV_CIPHER_CTR:
  426. case DRV_CIPHER_OFB:
  427. /* Read next IV */
  428. hw_desc_init(&desc[*seq_size]);
  429. set_dout_dlli(&desc[*seq_size], iv_dma_addr, ivsize, NS_BIT, 1);
  430. set_cipher_config0(&desc[*seq_size], direction);
  431. set_flow_mode(&desc[*seq_size], flow_mode);
  432. set_cipher_mode(&desc[*seq_size], cipher_mode);
  433. if (cipher_mode == DRV_CIPHER_CTR ||
  434. cipher_mode == DRV_CIPHER_OFB) {
  435. set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
  436. } else {
  437. set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE0);
  438. }
  439. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  440. (*seq_size)++;
  441. break;
  442. case DRV_CIPHER_XTS:
  443. case DRV_CIPHER_ESSIV:
  444. case DRV_CIPHER_BITLOCKER:
  445. /* IV */
  446. hw_desc_init(&desc[*seq_size]);
  447. set_setup_mode(&desc[*seq_size], SETUP_WRITE_STATE1);
  448. set_cipher_mode(&desc[*seq_size], cipher_mode);
  449. set_cipher_config0(&desc[*seq_size], direction);
  450. set_flow_mode(&desc[*seq_size], flow_mode);
  451. set_dout_dlli(&desc[*seq_size], iv_dma_addr, CC_AES_BLOCK_SIZE,
  452. NS_BIT, 1);
  453. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  454. (*seq_size)++;
  455. break;
  456. default:
  457. dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
  458. }
  459. }
  460. static void cc_setup_state_desc(struct crypto_tfm *tfm,
  461. struct cipher_req_ctx *req_ctx,
  462. unsigned int ivsize, unsigned int nbytes,
  463. struct cc_hw_desc desc[],
  464. unsigned int *seq_size)
  465. {
  466. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  467. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  468. int cipher_mode = ctx_p->cipher_mode;
  469. int flow_mode = ctx_p->flow_mode;
  470. int direction = req_ctx->gen_ctx.op_type;
  471. dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
  472. unsigned int du_size = nbytes;
  473. struct cc_crypto_alg *cc_alg =
  474. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  475. skcipher_alg.base);
  476. if (cc_alg->data_unit)
  477. du_size = cc_alg->data_unit;
  478. switch (cipher_mode) {
  479. case DRV_CIPHER_ECB:
  480. break;
  481. case DRV_CIPHER_CBC:
  482. case DRV_CIPHER_CBC_CTS:
  483. case DRV_CIPHER_CTR:
  484. case DRV_CIPHER_OFB:
  485. /* Load IV */
  486. hw_desc_init(&desc[*seq_size]);
  487. set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
  488. NS_BIT);
  489. set_cipher_config0(&desc[*seq_size], direction);
  490. set_flow_mode(&desc[*seq_size], flow_mode);
  491. set_cipher_mode(&desc[*seq_size], cipher_mode);
  492. if (cipher_mode == DRV_CIPHER_CTR ||
  493. cipher_mode == DRV_CIPHER_OFB) {
  494. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
  495. } else {
  496. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
  497. }
  498. (*seq_size)++;
  499. break;
  500. case DRV_CIPHER_XTS:
  501. case DRV_CIPHER_ESSIV:
  502. case DRV_CIPHER_BITLOCKER:
  503. break;
  504. default:
  505. dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
  506. }
  507. }
  508. static void cc_setup_xex_state_desc(struct crypto_tfm *tfm,
  509. struct cipher_req_ctx *req_ctx,
  510. unsigned int ivsize, unsigned int nbytes,
  511. struct cc_hw_desc desc[],
  512. unsigned int *seq_size)
  513. {
  514. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  515. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  516. int cipher_mode = ctx_p->cipher_mode;
  517. int flow_mode = ctx_p->flow_mode;
  518. int direction = req_ctx->gen_ctx.op_type;
  519. dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
  520. unsigned int key_len = ctx_p->keylen;
  521. dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
  522. unsigned int du_size = nbytes;
  523. struct cc_crypto_alg *cc_alg =
  524. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  525. skcipher_alg.base);
  526. if (cc_alg->data_unit)
  527. du_size = cc_alg->data_unit;
  528. switch (cipher_mode) {
  529. case DRV_CIPHER_ECB:
  530. break;
  531. case DRV_CIPHER_CBC:
  532. case DRV_CIPHER_CBC_CTS:
  533. case DRV_CIPHER_CTR:
  534. case DRV_CIPHER_OFB:
  535. break;
  536. case DRV_CIPHER_XTS:
  537. case DRV_CIPHER_ESSIV:
  538. case DRV_CIPHER_BITLOCKER:
  539. /* load XEX key */
  540. hw_desc_init(&desc[*seq_size]);
  541. set_cipher_mode(&desc[*seq_size], cipher_mode);
  542. set_cipher_config0(&desc[*seq_size], direction);
  543. if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
  544. set_hw_crypto_key(&desc[*seq_size],
  545. ctx_p->hw.key2_slot);
  546. } else {
  547. set_din_type(&desc[*seq_size], DMA_DLLI,
  548. (key_dma_addr + (key_len / 2)),
  549. (key_len / 2), NS_BIT);
  550. }
  551. set_xex_data_unit_size(&desc[*seq_size], du_size);
  552. set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
  553. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  554. set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
  555. (*seq_size)++;
  556. /* Load IV */
  557. hw_desc_init(&desc[*seq_size]);
  558. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
  559. set_cipher_mode(&desc[*seq_size], cipher_mode);
  560. set_cipher_config0(&desc[*seq_size], direction);
  561. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  562. set_flow_mode(&desc[*seq_size], flow_mode);
  563. set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
  564. CC_AES_BLOCK_SIZE, NS_BIT);
  565. (*seq_size)++;
  566. break;
  567. default:
  568. dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
  569. }
  570. }
  571. static int cc_out_flow_mode(struct cc_cipher_ctx *ctx_p)
  572. {
  573. switch (ctx_p->flow_mode) {
  574. case S_DIN_to_AES:
  575. return DIN_AES_DOUT;
  576. case S_DIN_to_DES:
  577. return DIN_DES_DOUT;
  578. case S_DIN_to_SM4:
  579. return DIN_SM4_DOUT;
  580. default:
  581. return ctx_p->flow_mode;
  582. }
  583. }
  584. static void cc_setup_key_desc(struct crypto_tfm *tfm,
  585. struct cipher_req_ctx *req_ctx,
  586. unsigned int nbytes, struct cc_hw_desc desc[],
  587. unsigned int *seq_size)
  588. {
  589. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  590. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  591. int cipher_mode = ctx_p->cipher_mode;
  592. int flow_mode = ctx_p->flow_mode;
  593. int direction = req_ctx->gen_ctx.op_type;
  594. dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
  595. unsigned int key_len = ctx_p->keylen;
  596. unsigned int din_size;
  597. switch (cipher_mode) {
  598. case DRV_CIPHER_CBC:
  599. case DRV_CIPHER_CBC_CTS:
  600. case DRV_CIPHER_CTR:
  601. case DRV_CIPHER_OFB:
  602. case DRV_CIPHER_ECB:
  603. /* Load key */
  604. hw_desc_init(&desc[*seq_size]);
  605. set_cipher_mode(&desc[*seq_size], cipher_mode);
  606. set_cipher_config0(&desc[*seq_size], direction);
  607. if (cc_key_type(tfm) == CC_POLICY_PROTECTED_KEY) {
  608. /* We use the AES key size coding for all CPP algs */
  609. set_key_size_aes(&desc[*seq_size], key_len);
  610. set_cpp_crypto_key(&desc[*seq_size], ctx_p->cpp.slot);
  611. flow_mode = cc_out_flow_mode(ctx_p);
  612. } else {
  613. if (flow_mode == S_DIN_to_AES) {
  614. if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
  615. set_hw_crypto_key(&desc[*seq_size],
  616. ctx_p->hw.key1_slot);
  617. } else {
  618. /* CC_POLICY_UNPROTECTED_KEY
  619. * Invalid keys are filtered out in
  620. * sethkey()
  621. */
  622. din_size = (key_len == 24) ?
  623. AES_MAX_KEY_SIZE : key_len;
  624. set_din_type(&desc[*seq_size], DMA_DLLI,
  625. key_dma_addr, din_size,
  626. NS_BIT);
  627. }
  628. set_key_size_aes(&desc[*seq_size], key_len);
  629. } else {
  630. /*des*/
  631. set_din_type(&desc[*seq_size], DMA_DLLI,
  632. key_dma_addr, key_len, NS_BIT);
  633. set_key_size_des(&desc[*seq_size], key_len);
  634. }
  635. set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
  636. }
  637. set_flow_mode(&desc[*seq_size], flow_mode);
  638. (*seq_size)++;
  639. break;
  640. case DRV_CIPHER_XTS:
  641. case DRV_CIPHER_ESSIV:
  642. case DRV_CIPHER_BITLOCKER:
  643. /* Load AES key */
  644. hw_desc_init(&desc[*seq_size]);
  645. set_cipher_mode(&desc[*seq_size], cipher_mode);
  646. set_cipher_config0(&desc[*seq_size], direction);
  647. if (cc_key_type(tfm) == CC_HW_PROTECTED_KEY) {
  648. set_hw_crypto_key(&desc[*seq_size],
  649. ctx_p->hw.key1_slot);
  650. } else {
  651. set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
  652. (key_len / 2), NS_BIT);
  653. }
  654. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  655. set_flow_mode(&desc[*seq_size], flow_mode);
  656. set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
  657. (*seq_size)++;
  658. break;
  659. default:
  660. dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
  661. }
  662. }
  663. static void cc_setup_mlli_desc(struct crypto_tfm *tfm,
  664. struct cipher_req_ctx *req_ctx,
  665. struct scatterlist *dst, struct scatterlist *src,
  666. unsigned int nbytes, void *areq,
  667. struct cc_hw_desc desc[], unsigned int *seq_size)
  668. {
  669. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  670. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  671. if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
  672. /* bypass */
  673. dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
  674. &req_ctx->mlli_params.mlli_dma_addr,
  675. req_ctx->mlli_params.mlli_len,
  676. (unsigned int)ctx_p->drvdata->mlli_sram_addr);
  677. hw_desc_init(&desc[*seq_size]);
  678. set_din_type(&desc[*seq_size], DMA_DLLI,
  679. req_ctx->mlli_params.mlli_dma_addr,
  680. req_ctx->mlli_params.mlli_len, NS_BIT);
  681. set_dout_sram(&desc[*seq_size],
  682. ctx_p->drvdata->mlli_sram_addr,
  683. req_ctx->mlli_params.mlli_len);
  684. set_flow_mode(&desc[*seq_size], BYPASS);
  685. (*seq_size)++;
  686. }
  687. }
  688. static void cc_setup_flow_desc(struct crypto_tfm *tfm,
  689. struct cipher_req_ctx *req_ctx,
  690. struct scatterlist *dst, struct scatterlist *src,
  691. unsigned int nbytes, struct cc_hw_desc desc[],
  692. unsigned int *seq_size)
  693. {
  694. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  695. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  696. unsigned int flow_mode = cc_out_flow_mode(ctx_p);
  697. bool last_desc = (ctx_p->key_type == CC_POLICY_PROTECTED_KEY ||
  698. ctx_p->cipher_mode == DRV_CIPHER_ECB);
  699. /* Process */
  700. if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
  701. dev_dbg(dev, " data params addr %pad length 0x%X\n",
  702. &sg_dma_address(src), nbytes);
  703. dev_dbg(dev, " data params addr %pad length 0x%X\n",
  704. &sg_dma_address(dst), nbytes);
  705. hw_desc_init(&desc[*seq_size]);
  706. set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
  707. nbytes, NS_BIT);
  708. set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
  709. nbytes, NS_BIT, (!last_desc ? 0 : 1));
  710. if (last_desc)
  711. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  712. set_flow_mode(&desc[*seq_size], flow_mode);
  713. (*seq_size)++;
  714. } else {
  715. hw_desc_init(&desc[*seq_size]);
  716. set_din_type(&desc[*seq_size], DMA_MLLI,
  717. ctx_p->drvdata->mlli_sram_addr,
  718. req_ctx->in_mlli_nents, NS_BIT);
  719. if (req_ctx->out_nents == 0) {
  720. dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
  721. (unsigned int)ctx_p->drvdata->mlli_sram_addr,
  722. (unsigned int)ctx_p->drvdata->mlli_sram_addr);
  723. set_dout_mlli(&desc[*seq_size],
  724. ctx_p->drvdata->mlli_sram_addr,
  725. req_ctx->in_mlli_nents, NS_BIT,
  726. (!last_desc ? 0 : 1));
  727. } else {
  728. dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
  729. (unsigned int)ctx_p->drvdata->mlli_sram_addr,
  730. (unsigned int)ctx_p->drvdata->mlli_sram_addr +
  731. (u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
  732. set_dout_mlli(&desc[*seq_size],
  733. (ctx_p->drvdata->mlli_sram_addr +
  734. (LLI_ENTRY_BYTE_SIZE *
  735. req_ctx->in_mlli_nents)),
  736. req_ctx->out_mlli_nents, NS_BIT,
  737. (!last_desc ? 0 : 1));
  738. }
  739. if (last_desc)
  740. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  741. set_flow_mode(&desc[*seq_size], flow_mode);
  742. (*seq_size)++;
  743. }
  744. }
  745. static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
  746. {
  747. struct skcipher_request *req = (struct skcipher_request *)cc_req;
  748. struct scatterlist *dst = req->dst;
  749. struct scatterlist *src = req->src;
  750. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  751. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  752. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  753. if (err != -EINPROGRESS) {
  754. /* Not a BACKLOG notification */
  755. cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
  756. memcpy(req->iv, req_ctx->iv, ivsize);
  757. kzfree(req_ctx->iv);
  758. }
  759. skcipher_request_complete(req, err);
  760. }
  761. static int cc_cipher_process(struct skcipher_request *req,
  762. enum drv_crypto_direction direction)
  763. {
  764. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  765. struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
  766. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  767. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  768. struct scatterlist *dst = req->dst;
  769. struct scatterlist *src = req->src;
  770. unsigned int nbytes = req->cryptlen;
  771. void *iv = req->iv;
  772. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  773. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  774. struct cc_hw_desc desc[MAX_ABLKCIPHER_SEQ_LEN];
  775. struct cc_crypto_req cc_req = {};
  776. int rc;
  777. unsigned int seq_len = 0;
  778. gfp_t flags = cc_gfp_flags(&req->base);
  779. dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
  780. ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  781. "Encrypt" : "Decrypt"), req, iv, nbytes);
  782. /* STAT_PHASE_0: Init and sanity checks */
  783. /* TODO: check data length according to mode */
  784. if (validate_data_size(ctx_p, nbytes)) {
  785. dev_err(dev, "Unsupported data size %d.\n", nbytes);
  786. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
  787. rc = -EINVAL;
  788. goto exit_process;
  789. }
  790. if (nbytes == 0) {
  791. /* No data to process is valid */
  792. rc = 0;
  793. goto exit_process;
  794. }
  795. /* The IV we are handed may be allocted from the stack so
  796. * we must copy it to a DMAable buffer before use.
  797. */
  798. req_ctx->iv = kmemdup(iv, ivsize, flags);
  799. if (!req_ctx->iv) {
  800. rc = -ENOMEM;
  801. goto exit_process;
  802. }
  803. /* Setup request structure */
  804. cc_req.user_cb = (void *)cc_cipher_complete;
  805. cc_req.user_arg = (void *)req;
  806. /* Setup CPP operation details */
  807. if (ctx_p->key_type == CC_POLICY_PROTECTED_KEY) {
  808. cc_req.cpp.is_cpp = true;
  809. cc_req.cpp.alg = ctx_p->cpp.alg;
  810. cc_req.cpp.slot = ctx_p->cpp.slot;
  811. }
  812. /* Setup request context */
  813. req_ctx->gen_ctx.op_type = direction;
  814. /* STAT_PHASE_1: Map buffers */
  815. rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
  816. req_ctx->iv, src, dst, flags);
  817. if (rc) {
  818. dev_err(dev, "map_request() failed\n");
  819. goto exit_process;
  820. }
  821. /* STAT_PHASE_2: Create sequence */
  822. /* Setup state (IV) */
  823. cc_setup_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
  824. /* Setup MLLI line, if needed */
  825. cc_setup_mlli_desc(tfm, req_ctx, dst, src, nbytes, req, desc, &seq_len);
  826. /* Setup key */
  827. cc_setup_key_desc(tfm, req_ctx, nbytes, desc, &seq_len);
  828. /* Setup state (IV and XEX key) */
  829. cc_setup_xex_state_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
  830. /* Data processing */
  831. cc_setup_flow_desc(tfm, req_ctx, dst, src, nbytes, desc, &seq_len);
  832. /* Read next IV */
  833. cc_setup_readiv_desc(tfm, req_ctx, ivsize, desc, &seq_len);
  834. /* STAT_PHASE_3: Lock HW and push sequence */
  835. rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
  836. &req->base);
  837. if (rc != -EINPROGRESS && rc != -EBUSY) {
  838. /* Failed to send the request or request completed
  839. * synchronously
  840. */
  841. cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
  842. }
  843. exit_process:
  844. if (rc != -EINPROGRESS && rc != -EBUSY) {
  845. kzfree(req_ctx->iv);
  846. }
  847. return rc;
  848. }
  849. static int cc_cipher_encrypt(struct skcipher_request *req)
  850. {
  851. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  852. memset(req_ctx, 0, sizeof(*req_ctx));
  853. return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  854. }
  855. static int cc_cipher_decrypt(struct skcipher_request *req)
  856. {
  857. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  858. memset(req_ctx, 0, sizeof(*req_ctx));
  859. return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  860. }
  861. /* Block cipher alg */
  862. static const struct cc_alg_template skcipher_algs[] = {
  863. {
  864. .name = "xts(paes)",
  865. .driver_name = "xts-paes-ccree",
  866. .blocksize = 1,
  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_XTS,
  876. .flow_mode = S_DIN_to_AES,
  877. .min_hw_rev = CC_HW_REV_630,
  878. .std_body = CC_STD_NIST,
  879. .sec_func = true,
  880. },
  881. {
  882. .name = "xts512(paes)",
  883. .driver_name = "xts-paes-du512-ccree",
  884. .blocksize = 1,
  885. .template_skcipher = {
  886. .setkey = cc_cipher_sethkey,
  887. .encrypt = cc_cipher_encrypt,
  888. .decrypt = cc_cipher_decrypt,
  889. .min_keysize = CC_HW_KEY_SIZE,
  890. .max_keysize = CC_HW_KEY_SIZE,
  891. .ivsize = AES_BLOCK_SIZE,
  892. },
  893. .cipher_mode = DRV_CIPHER_XTS,
  894. .flow_mode = S_DIN_to_AES,
  895. .data_unit = 512,
  896. .min_hw_rev = CC_HW_REV_712,
  897. .std_body = CC_STD_NIST,
  898. .sec_func = true,
  899. },
  900. {
  901. .name = "xts4096(paes)",
  902. .driver_name = "xts-paes-du4096-ccree",
  903. .blocksize = 1,
  904. .template_skcipher = {
  905. .setkey = cc_cipher_sethkey,
  906. .encrypt = cc_cipher_encrypt,
  907. .decrypt = cc_cipher_decrypt,
  908. .min_keysize = CC_HW_KEY_SIZE,
  909. .max_keysize = CC_HW_KEY_SIZE,
  910. .ivsize = AES_BLOCK_SIZE,
  911. },
  912. .cipher_mode = DRV_CIPHER_XTS,
  913. .flow_mode = S_DIN_to_AES,
  914. .data_unit = 4096,
  915. .min_hw_rev = CC_HW_REV_712,
  916. .std_body = CC_STD_NIST,
  917. .sec_func = true,
  918. },
  919. {
  920. .name = "essiv(paes)",
  921. .driver_name = "essiv-paes-ccree",
  922. .blocksize = AES_BLOCK_SIZE,
  923. .template_skcipher = {
  924. .setkey = cc_cipher_sethkey,
  925. .encrypt = cc_cipher_encrypt,
  926. .decrypt = cc_cipher_decrypt,
  927. .min_keysize = CC_HW_KEY_SIZE,
  928. .max_keysize = CC_HW_KEY_SIZE,
  929. .ivsize = AES_BLOCK_SIZE,
  930. },
  931. .cipher_mode = DRV_CIPHER_ESSIV,
  932. .flow_mode = S_DIN_to_AES,
  933. .min_hw_rev = CC_HW_REV_712,
  934. .std_body = CC_STD_NIST,
  935. .sec_func = true,
  936. },
  937. {
  938. .name = "essiv512(paes)",
  939. .driver_name = "essiv-paes-du512-ccree",
  940. .blocksize = AES_BLOCK_SIZE,
  941. .template_skcipher = {
  942. .setkey = cc_cipher_sethkey,
  943. .encrypt = cc_cipher_encrypt,
  944. .decrypt = cc_cipher_decrypt,
  945. .min_keysize = CC_HW_KEY_SIZE,
  946. .max_keysize = CC_HW_KEY_SIZE,
  947. .ivsize = AES_BLOCK_SIZE,
  948. },
  949. .cipher_mode = DRV_CIPHER_ESSIV,
  950. .flow_mode = S_DIN_to_AES,
  951. .data_unit = 512,
  952. .min_hw_rev = CC_HW_REV_712,
  953. .std_body = CC_STD_NIST,
  954. .sec_func = true,
  955. },
  956. {
  957. .name = "essiv4096(paes)",
  958. .driver_name = "essiv-paes-du4096-ccree",
  959. .blocksize = AES_BLOCK_SIZE,
  960. .template_skcipher = {
  961. .setkey = cc_cipher_sethkey,
  962. .encrypt = cc_cipher_encrypt,
  963. .decrypt = cc_cipher_decrypt,
  964. .min_keysize = CC_HW_KEY_SIZE,
  965. .max_keysize = CC_HW_KEY_SIZE,
  966. .ivsize = AES_BLOCK_SIZE,
  967. },
  968. .cipher_mode = DRV_CIPHER_ESSIV,
  969. .flow_mode = S_DIN_to_AES,
  970. .data_unit = 4096,
  971. .min_hw_rev = CC_HW_REV_712,
  972. .std_body = CC_STD_NIST,
  973. .sec_func = true,
  974. },
  975. {
  976. .name = "bitlocker(paes)",
  977. .driver_name = "bitlocker-paes-ccree",
  978. .blocksize = AES_BLOCK_SIZE,
  979. .template_skcipher = {
  980. .setkey = cc_cipher_sethkey,
  981. .encrypt = cc_cipher_encrypt,
  982. .decrypt = cc_cipher_decrypt,
  983. .min_keysize = CC_HW_KEY_SIZE,
  984. .max_keysize = CC_HW_KEY_SIZE,
  985. .ivsize = AES_BLOCK_SIZE,
  986. },
  987. .cipher_mode = DRV_CIPHER_BITLOCKER,
  988. .flow_mode = S_DIN_to_AES,
  989. .min_hw_rev = CC_HW_REV_712,
  990. .std_body = CC_STD_NIST,
  991. .sec_func = true,
  992. },
  993. {
  994. .name = "bitlocker512(paes)",
  995. .driver_name = "bitlocker-paes-du512-ccree",
  996. .blocksize = AES_BLOCK_SIZE,
  997. .template_skcipher = {
  998. .setkey = cc_cipher_sethkey,
  999. .encrypt = cc_cipher_encrypt,
  1000. .decrypt = cc_cipher_decrypt,
  1001. .min_keysize = CC_HW_KEY_SIZE,
  1002. .max_keysize = CC_HW_KEY_SIZE,
  1003. .ivsize = AES_BLOCK_SIZE,
  1004. },
  1005. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1006. .flow_mode = S_DIN_to_AES,
  1007. .data_unit = 512,
  1008. .min_hw_rev = CC_HW_REV_712,
  1009. .std_body = CC_STD_NIST,
  1010. .sec_func = true,
  1011. },
  1012. {
  1013. .name = "bitlocker4096(paes)",
  1014. .driver_name = "bitlocker-paes-du4096-ccree",
  1015. .blocksize = AES_BLOCK_SIZE,
  1016. .template_skcipher = {
  1017. .setkey = cc_cipher_sethkey,
  1018. .encrypt = cc_cipher_encrypt,
  1019. .decrypt = cc_cipher_decrypt,
  1020. .min_keysize = CC_HW_KEY_SIZE,
  1021. .max_keysize = CC_HW_KEY_SIZE,
  1022. .ivsize = AES_BLOCK_SIZE,
  1023. },
  1024. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1025. .flow_mode = S_DIN_to_AES,
  1026. .data_unit = 4096,
  1027. .min_hw_rev = CC_HW_REV_712,
  1028. .std_body = CC_STD_NIST,
  1029. .sec_func = true,
  1030. },
  1031. {
  1032. .name = "ecb(paes)",
  1033. .driver_name = "ecb-paes-ccree",
  1034. .blocksize = AES_BLOCK_SIZE,
  1035. .template_skcipher = {
  1036. .setkey = cc_cipher_sethkey,
  1037. .encrypt = cc_cipher_encrypt,
  1038. .decrypt = cc_cipher_decrypt,
  1039. .min_keysize = CC_HW_KEY_SIZE,
  1040. .max_keysize = CC_HW_KEY_SIZE,
  1041. .ivsize = 0,
  1042. },
  1043. .cipher_mode = DRV_CIPHER_ECB,
  1044. .flow_mode = S_DIN_to_AES,
  1045. .min_hw_rev = CC_HW_REV_712,
  1046. .std_body = CC_STD_NIST,
  1047. .sec_func = true,
  1048. },
  1049. {
  1050. .name = "cbc(paes)",
  1051. .driver_name = "cbc-paes-ccree",
  1052. .blocksize = AES_BLOCK_SIZE,
  1053. .template_skcipher = {
  1054. .setkey = cc_cipher_sethkey,
  1055. .encrypt = cc_cipher_encrypt,
  1056. .decrypt = cc_cipher_decrypt,
  1057. .min_keysize = CC_HW_KEY_SIZE,
  1058. .max_keysize = CC_HW_KEY_SIZE,
  1059. .ivsize = AES_BLOCK_SIZE,
  1060. },
  1061. .cipher_mode = DRV_CIPHER_CBC,
  1062. .flow_mode = S_DIN_to_AES,
  1063. .min_hw_rev = CC_HW_REV_712,
  1064. .std_body = CC_STD_NIST,
  1065. .sec_func = true,
  1066. },
  1067. {
  1068. .name = "ofb(paes)",
  1069. .driver_name = "ofb-paes-ccree",
  1070. .blocksize = AES_BLOCK_SIZE,
  1071. .template_skcipher = {
  1072. .setkey = cc_cipher_sethkey,
  1073. .encrypt = cc_cipher_encrypt,
  1074. .decrypt = cc_cipher_decrypt,
  1075. .min_keysize = CC_HW_KEY_SIZE,
  1076. .max_keysize = CC_HW_KEY_SIZE,
  1077. .ivsize = AES_BLOCK_SIZE,
  1078. },
  1079. .cipher_mode = DRV_CIPHER_OFB,
  1080. .flow_mode = S_DIN_to_AES,
  1081. .min_hw_rev = CC_HW_REV_712,
  1082. .std_body = CC_STD_NIST,
  1083. .sec_func = true,
  1084. },
  1085. {
  1086. .name = "cts(cbc(paes))",
  1087. .driver_name = "cts-cbc-paes-ccree",
  1088. .blocksize = AES_BLOCK_SIZE,
  1089. .template_skcipher = {
  1090. .setkey = cc_cipher_sethkey,
  1091. .encrypt = cc_cipher_encrypt,
  1092. .decrypt = cc_cipher_decrypt,
  1093. .min_keysize = CC_HW_KEY_SIZE,
  1094. .max_keysize = CC_HW_KEY_SIZE,
  1095. .ivsize = AES_BLOCK_SIZE,
  1096. },
  1097. .cipher_mode = DRV_CIPHER_CBC_CTS,
  1098. .flow_mode = S_DIN_to_AES,
  1099. .min_hw_rev = CC_HW_REV_712,
  1100. .std_body = CC_STD_NIST,
  1101. .sec_func = true,
  1102. },
  1103. {
  1104. .name = "ctr(paes)",
  1105. .driver_name = "ctr-paes-ccree",
  1106. .blocksize = 1,
  1107. .template_skcipher = {
  1108. .setkey = cc_cipher_sethkey,
  1109. .encrypt = cc_cipher_encrypt,
  1110. .decrypt = cc_cipher_decrypt,
  1111. .min_keysize = CC_HW_KEY_SIZE,
  1112. .max_keysize = CC_HW_KEY_SIZE,
  1113. .ivsize = AES_BLOCK_SIZE,
  1114. },
  1115. .cipher_mode = DRV_CIPHER_CTR,
  1116. .flow_mode = S_DIN_to_AES,
  1117. .min_hw_rev = CC_HW_REV_712,
  1118. .std_body = CC_STD_NIST,
  1119. .sec_func = true,
  1120. },
  1121. {
  1122. .name = "xts(aes)",
  1123. .driver_name = "xts-aes-ccree",
  1124. .blocksize = 1,
  1125. .template_skcipher = {
  1126. .setkey = cc_cipher_setkey,
  1127. .encrypt = cc_cipher_encrypt,
  1128. .decrypt = cc_cipher_decrypt,
  1129. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1130. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1131. .ivsize = AES_BLOCK_SIZE,
  1132. },
  1133. .cipher_mode = DRV_CIPHER_XTS,
  1134. .flow_mode = S_DIN_to_AES,
  1135. .min_hw_rev = CC_HW_REV_630,
  1136. .std_body = CC_STD_NIST,
  1137. },
  1138. {
  1139. .name = "xts512(aes)",
  1140. .driver_name = "xts-aes-du512-ccree",
  1141. .blocksize = 1,
  1142. .template_skcipher = {
  1143. .setkey = cc_cipher_setkey,
  1144. .encrypt = cc_cipher_encrypt,
  1145. .decrypt = cc_cipher_decrypt,
  1146. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1147. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1148. .ivsize = AES_BLOCK_SIZE,
  1149. },
  1150. .cipher_mode = DRV_CIPHER_XTS,
  1151. .flow_mode = S_DIN_to_AES,
  1152. .data_unit = 512,
  1153. .min_hw_rev = CC_HW_REV_712,
  1154. .std_body = CC_STD_NIST,
  1155. },
  1156. {
  1157. .name = "xts4096(aes)",
  1158. .driver_name = "xts-aes-du4096-ccree",
  1159. .blocksize = 1,
  1160. .template_skcipher = {
  1161. .setkey = cc_cipher_setkey,
  1162. .encrypt = cc_cipher_encrypt,
  1163. .decrypt = cc_cipher_decrypt,
  1164. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1165. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1166. .ivsize = AES_BLOCK_SIZE,
  1167. },
  1168. .cipher_mode = DRV_CIPHER_XTS,
  1169. .flow_mode = S_DIN_to_AES,
  1170. .data_unit = 4096,
  1171. .min_hw_rev = CC_HW_REV_712,
  1172. .std_body = CC_STD_NIST,
  1173. },
  1174. {
  1175. .name = "essiv(aes)",
  1176. .driver_name = "essiv-aes-ccree",
  1177. .blocksize = AES_BLOCK_SIZE,
  1178. .template_skcipher = {
  1179. .setkey = cc_cipher_setkey,
  1180. .encrypt = cc_cipher_encrypt,
  1181. .decrypt = cc_cipher_decrypt,
  1182. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1183. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1184. .ivsize = AES_BLOCK_SIZE,
  1185. },
  1186. .cipher_mode = DRV_CIPHER_ESSIV,
  1187. .flow_mode = S_DIN_to_AES,
  1188. .min_hw_rev = CC_HW_REV_712,
  1189. .std_body = CC_STD_NIST,
  1190. },
  1191. {
  1192. .name = "essiv512(aes)",
  1193. .driver_name = "essiv-aes-du512-ccree",
  1194. .blocksize = AES_BLOCK_SIZE,
  1195. .template_skcipher = {
  1196. .setkey = cc_cipher_setkey,
  1197. .encrypt = cc_cipher_encrypt,
  1198. .decrypt = cc_cipher_decrypt,
  1199. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1200. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1201. .ivsize = AES_BLOCK_SIZE,
  1202. },
  1203. .cipher_mode = DRV_CIPHER_ESSIV,
  1204. .flow_mode = S_DIN_to_AES,
  1205. .data_unit = 512,
  1206. .min_hw_rev = CC_HW_REV_712,
  1207. .std_body = CC_STD_NIST,
  1208. },
  1209. {
  1210. .name = "essiv4096(aes)",
  1211. .driver_name = "essiv-aes-du4096-ccree",
  1212. .blocksize = AES_BLOCK_SIZE,
  1213. .template_skcipher = {
  1214. .setkey = cc_cipher_setkey,
  1215. .encrypt = cc_cipher_encrypt,
  1216. .decrypt = cc_cipher_decrypt,
  1217. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1218. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1219. .ivsize = AES_BLOCK_SIZE,
  1220. },
  1221. .cipher_mode = DRV_CIPHER_ESSIV,
  1222. .flow_mode = S_DIN_to_AES,
  1223. .data_unit = 4096,
  1224. .min_hw_rev = CC_HW_REV_712,
  1225. .std_body = CC_STD_NIST,
  1226. },
  1227. {
  1228. .name = "bitlocker(aes)",
  1229. .driver_name = "bitlocker-aes-ccree",
  1230. .blocksize = AES_BLOCK_SIZE,
  1231. .template_skcipher = {
  1232. .setkey = cc_cipher_setkey,
  1233. .encrypt = cc_cipher_encrypt,
  1234. .decrypt = cc_cipher_decrypt,
  1235. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1236. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1237. .ivsize = AES_BLOCK_SIZE,
  1238. },
  1239. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1240. .flow_mode = S_DIN_to_AES,
  1241. .min_hw_rev = CC_HW_REV_712,
  1242. .std_body = CC_STD_NIST,
  1243. },
  1244. {
  1245. .name = "bitlocker512(aes)",
  1246. .driver_name = "bitlocker-aes-du512-ccree",
  1247. .blocksize = AES_BLOCK_SIZE,
  1248. .template_skcipher = {
  1249. .setkey = cc_cipher_setkey,
  1250. .encrypt = cc_cipher_encrypt,
  1251. .decrypt = cc_cipher_decrypt,
  1252. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1253. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1254. .ivsize = AES_BLOCK_SIZE,
  1255. },
  1256. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1257. .flow_mode = S_DIN_to_AES,
  1258. .data_unit = 512,
  1259. .min_hw_rev = CC_HW_REV_712,
  1260. .std_body = CC_STD_NIST,
  1261. },
  1262. {
  1263. .name = "bitlocker4096(aes)",
  1264. .driver_name = "bitlocker-aes-du4096-ccree",
  1265. .blocksize = AES_BLOCK_SIZE,
  1266. .template_skcipher = {
  1267. .setkey = cc_cipher_setkey,
  1268. .encrypt = cc_cipher_encrypt,
  1269. .decrypt = cc_cipher_decrypt,
  1270. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1271. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1272. .ivsize = AES_BLOCK_SIZE,
  1273. },
  1274. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1275. .flow_mode = S_DIN_to_AES,
  1276. .data_unit = 4096,
  1277. .min_hw_rev = CC_HW_REV_712,
  1278. .std_body = CC_STD_NIST,
  1279. },
  1280. {
  1281. .name = "ecb(aes)",
  1282. .driver_name = "ecb-aes-ccree",
  1283. .blocksize = AES_BLOCK_SIZE,
  1284. .template_skcipher = {
  1285. .setkey = cc_cipher_setkey,
  1286. .encrypt = cc_cipher_encrypt,
  1287. .decrypt = cc_cipher_decrypt,
  1288. .min_keysize = AES_MIN_KEY_SIZE,
  1289. .max_keysize = AES_MAX_KEY_SIZE,
  1290. .ivsize = 0,
  1291. },
  1292. .cipher_mode = DRV_CIPHER_ECB,
  1293. .flow_mode = S_DIN_to_AES,
  1294. .min_hw_rev = CC_HW_REV_630,
  1295. .std_body = CC_STD_NIST,
  1296. },
  1297. {
  1298. .name = "cbc(aes)",
  1299. .driver_name = "cbc-aes-ccree",
  1300. .blocksize = AES_BLOCK_SIZE,
  1301. .template_skcipher = {
  1302. .setkey = cc_cipher_setkey,
  1303. .encrypt = cc_cipher_encrypt,
  1304. .decrypt = cc_cipher_decrypt,
  1305. .min_keysize = AES_MIN_KEY_SIZE,
  1306. .max_keysize = AES_MAX_KEY_SIZE,
  1307. .ivsize = AES_BLOCK_SIZE,
  1308. },
  1309. .cipher_mode = DRV_CIPHER_CBC,
  1310. .flow_mode = S_DIN_to_AES,
  1311. .min_hw_rev = CC_HW_REV_630,
  1312. .std_body = CC_STD_NIST,
  1313. },
  1314. {
  1315. .name = "ofb(aes)",
  1316. .driver_name = "ofb-aes-ccree",
  1317. .blocksize = AES_BLOCK_SIZE,
  1318. .template_skcipher = {
  1319. .setkey = cc_cipher_setkey,
  1320. .encrypt = cc_cipher_encrypt,
  1321. .decrypt = cc_cipher_decrypt,
  1322. .min_keysize = AES_MIN_KEY_SIZE,
  1323. .max_keysize = AES_MAX_KEY_SIZE,
  1324. .ivsize = AES_BLOCK_SIZE,
  1325. },
  1326. .cipher_mode = DRV_CIPHER_OFB,
  1327. .flow_mode = S_DIN_to_AES,
  1328. .min_hw_rev = CC_HW_REV_630,
  1329. .std_body = CC_STD_NIST,
  1330. },
  1331. {
  1332. .name = "cts(cbc(aes))",
  1333. .driver_name = "cts-cbc-aes-ccree",
  1334. .blocksize = AES_BLOCK_SIZE,
  1335. .template_skcipher = {
  1336. .setkey = cc_cipher_setkey,
  1337. .encrypt = cc_cipher_encrypt,
  1338. .decrypt = cc_cipher_decrypt,
  1339. .min_keysize = AES_MIN_KEY_SIZE,
  1340. .max_keysize = AES_MAX_KEY_SIZE,
  1341. .ivsize = AES_BLOCK_SIZE,
  1342. },
  1343. .cipher_mode = DRV_CIPHER_CBC_CTS,
  1344. .flow_mode = S_DIN_to_AES,
  1345. .min_hw_rev = CC_HW_REV_630,
  1346. .std_body = CC_STD_NIST,
  1347. },
  1348. {
  1349. .name = "ctr(aes)",
  1350. .driver_name = "ctr-aes-ccree",
  1351. .blocksize = 1,
  1352. .template_skcipher = {
  1353. .setkey = cc_cipher_setkey,
  1354. .encrypt = cc_cipher_encrypt,
  1355. .decrypt = cc_cipher_decrypt,
  1356. .min_keysize = AES_MIN_KEY_SIZE,
  1357. .max_keysize = AES_MAX_KEY_SIZE,
  1358. .ivsize = AES_BLOCK_SIZE,
  1359. },
  1360. .cipher_mode = DRV_CIPHER_CTR,
  1361. .flow_mode = S_DIN_to_AES,
  1362. .min_hw_rev = CC_HW_REV_630,
  1363. .std_body = CC_STD_NIST,
  1364. },
  1365. {
  1366. .name = "cbc(des3_ede)",
  1367. .driver_name = "cbc-3des-ccree",
  1368. .blocksize = DES3_EDE_BLOCK_SIZE,
  1369. .template_skcipher = {
  1370. .setkey = cc_cipher_setkey,
  1371. .encrypt = cc_cipher_encrypt,
  1372. .decrypt = cc_cipher_decrypt,
  1373. .min_keysize = DES3_EDE_KEY_SIZE,
  1374. .max_keysize = DES3_EDE_KEY_SIZE,
  1375. .ivsize = DES3_EDE_BLOCK_SIZE,
  1376. },
  1377. .cipher_mode = DRV_CIPHER_CBC,
  1378. .flow_mode = S_DIN_to_DES,
  1379. .min_hw_rev = CC_HW_REV_630,
  1380. .std_body = CC_STD_NIST,
  1381. },
  1382. {
  1383. .name = "ecb(des3_ede)",
  1384. .driver_name = "ecb-3des-ccree",
  1385. .blocksize = DES3_EDE_BLOCK_SIZE,
  1386. .template_skcipher = {
  1387. .setkey = cc_cipher_setkey,
  1388. .encrypt = cc_cipher_encrypt,
  1389. .decrypt = cc_cipher_decrypt,
  1390. .min_keysize = DES3_EDE_KEY_SIZE,
  1391. .max_keysize = DES3_EDE_KEY_SIZE,
  1392. .ivsize = 0,
  1393. },
  1394. .cipher_mode = DRV_CIPHER_ECB,
  1395. .flow_mode = S_DIN_to_DES,
  1396. .min_hw_rev = CC_HW_REV_630,
  1397. .std_body = CC_STD_NIST,
  1398. },
  1399. {
  1400. .name = "cbc(des)",
  1401. .driver_name = "cbc-des-ccree",
  1402. .blocksize = DES_BLOCK_SIZE,
  1403. .template_skcipher = {
  1404. .setkey = cc_cipher_setkey,
  1405. .encrypt = cc_cipher_encrypt,
  1406. .decrypt = cc_cipher_decrypt,
  1407. .min_keysize = DES_KEY_SIZE,
  1408. .max_keysize = DES_KEY_SIZE,
  1409. .ivsize = DES_BLOCK_SIZE,
  1410. },
  1411. .cipher_mode = DRV_CIPHER_CBC,
  1412. .flow_mode = S_DIN_to_DES,
  1413. .min_hw_rev = CC_HW_REV_630,
  1414. .std_body = CC_STD_NIST,
  1415. },
  1416. {
  1417. .name = "ecb(des)",
  1418. .driver_name = "ecb-des-ccree",
  1419. .blocksize = DES_BLOCK_SIZE,
  1420. .template_skcipher = {
  1421. .setkey = cc_cipher_setkey,
  1422. .encrypt = cc_cipher_encrypt,
  1423. .decrypt = cc_cipher_decrypt,
  1424. .min_keysize = DES_KEY_SIZE,
  1425. .max_keysize = DES_KEY_SIZE,
  1426. .ivsize = 0,
  1427. },
  1428. .cipher_mode = DRV_CIPHER_ECB,
  1429. .flow_mode = S_DIN_to_DES,
  1430. .min_hw_rev = CC_HW_REV_630,
  1431. .std_body = CC_STD_NIST,
  1432. },
  1433. {
  1434. .name = "cbc(sm4)",
  1435. .driver_name = "cbc-sm4-ccree",
  1436. .blocksize = SM4_BLOCK_SIZE,
  1437. .template_skcipher = {
  1438. .setkey = cc_cipher_setkey,
  1439. .encrypt = cc_cipher_encrypt,
  1440. .decrypt = cc_cipher_decrypt,
  1441. .min_keysize = SM4_KEY_SIZE,
  1442. .max_keysize = SM4_KEY_SIZE,
  1443. .ivsize = SM4_BLOCK_SIZE,
  1444. },
  1445. .cipher_mode = DRV_CIPHER_CBC,
  1446. .flow_mode = S_DIN_to_SM4,
  1447. .min_hw_rev = CC_HW_REV_713,
  1448. .std_body = CC_STD_OSCCA,
  1449. },
  1450. {
  1451. .name = "ecb(sm4)",
  1452. .driver_name = "ecb-sm4-ccree",
  1453. .blocksize = SM4_BLOCK_SIZE,
  1454. .template_skcipher = {
  1455. .setkey = cc_cipher_setkey,
  1456. .encrypt = cc_cipher_encrypt,
  1457. .decrypt = cc_cipher_decrypt,
  1458. .min_keysize = SM4_KEY_SIZE,
  1459. .max_keysize = SM4_KEY_SIZE,
  1460. .ivsize = 0,
  1461. },
  1462. .cipher_mode = DRV_CIPHER_ECB,
  1463. .flow_mode = S_DIN_to_SM4,
  1464. .min_hw_rev = CC_HW_REV_713,
  1465. .std_body = CC_STD_OSCCA,
  1466. },
  1467. {
  1468. .name = "ctr(sm4)",
  1469. .driver_name = "ctr-sm4-ccree",
  1470. .blocksize = SM4_BLOCK_SIZE,
  1471. .template_skcipher = {
  1472. .setkey = cc_cipher_setkey,
  1473. .encrypt = cc_cipher_encrypt,
  1474. .decrypt = cc_cipher_decrypt,
  1475. .min_keysize = SM4_KEY_SIZE,
  1476. .max_keysize = SM4_KEY_SIZE,
  1477. .ivsize = SM4_BLOCK_SIZE,
  1478. },
  1479. .cipher_mode = DRV_CIPHER_CTR,
  1480. .flow_mode = S_DIN_to_SM4,
  1481. .min_hw_rev = CC_HW_REV_713,
  1482. .std_body = CC_STD_OSCCA,
  1483. },
  1484. {
  1485. .name = "cbc(psm4)",
  1486. .driver_name = "cbc-psm4-ccree",
  1487. .blocksize = SM4_BLOCK_SIZE,
  1488. .template_skcipher = {
  1489. .setkey = cc_cipher_sethkey,
  1490. .encrypt = cc_cipher_encrypt,
  1491. .decrypt = cc_cipher_decrypt,
  1492. .min_keysize = CC_HW_KEY_SIZE,
  1493. .max_keysize = CC_HW_KEY_SIZE,
  1494. .ivsize = SM4_BLOCK_SIZE,
  1495. },
  1496. .cipher_mode = DRV_CIPHER_CBC,
  1497. .flow_mode = S_DIN_to_SM4,
  1498. .min_hw_rev = CC_HW_REV_713,
  1499. .std_body = CC_STD_OSCCA,
  1500. .sec_func = true,
  1501. },
  1502. {
  1503. .name = "ctr(psm4)",
  1504. .driver_name = "ctr-psm4-ccree",
  1505. .blocksize = SM4_BLOCK_SIZE,
  1506. .template_skcipher = {
  1507. .setkey = cc_cipher_sethkey,
  1508. .encrypt = cc_cipher_encrypt,
  1509. .decrypt = cc_cipher_decrypt,
  1510. .min_keysize = CC_HW_KEY_SIZE,
  1511. .max_keysize = CC_HW_KEY_SIZE,
  1512. .ivsize = SM4_BLOCK_SIZE,
  1513. },
  1514. .cipher_mode = DRV_CIPHER_CTR,
  1515. .flow_mode = S_DIN_to_SM4,
  1516. .min_hw_rev = CC_HW_REV_713,
  1517. .std_body = CC_STD_OSCCA,
  1518. .sec_func = true,
  1519. },
  1520. };
  1521. static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
  1522. struct device *dev)
  1523. {
  1524. struct cc_crypto_alg *t_alg;
  1525. struct skcipher_alg *alg;
  1526. t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
  1527. if (!t_alg)
  1528. return ERR_PTR(-ENOMEM);
  1529. alg = &t_alg->skcipher_alg;
  1530. memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
  1531. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  1532. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1533. tmpl->driver_name);
  1534. alg->base.cra_module = THIS_MODULE;
  1535. alg->base.cra_priority = CC_CRA_PRIO;
  1536. alg->base.cra_blocksize = tmpl->blocksize;
  1537. alg->base.cra_alignmask = 0;
  1538. alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
  1539. alg->base.cra_init = cc_cipher_init;
  1540. alg->base.cra_exit = cc_cipher_exit;
  1541. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  1542. t_alg->cipher_mode = tmpl->cipher_mode;
  1543. t_alg->flow_mode = tmpl->flow_mode;
  1544. t_alg->data_unit = tmpl->data_unit;
  1545. return t_alg;
  1546. }
  1547. int cc_cipher_free(struct cc_drvdata *drvdata)
  1548. {
  1549. struct cc_crypto_alg *t_alg, *n;
  1550. struct cc_cipher_handle *cipher_handle = drvdata->cipher_handle;
  1551. if (cipher_handle) {
  1552. /* Remove registered algs */
  1553. list_for_each_entry_safe(t_alg, n, &cipher_handle->alg_list,
  1554. entry) {
  1555. crypto_unregister_skcipher(&t_alg->skcipher_alg);
  1556. list_del(&t_alg->entry);
  1557. kfree(t_alg);
  1558. }
  1559. kfree(cipher_handle);
  1560. drvdata->cipher_handle = NULL;
  1561. }
  1562. return 0;
  1563. }
  1564. int cc_cipher_alloc(struct cc_drvdata *drvdata)
  1565. {
  1566. struct cc_cipher_handle *cipher_handle;
  1567. struct cc_crypto_alg *t_alg;
  1568. struct device *dev = drvdata_to_dev(drvdata);
  1569. int rc = -ENOMEM;
  1570. int alg;
  1571. cipher_handle = kmalloc(sizeof(*cipher_handle), GFP_KERNEL);
  1572. if (!cipher_handle)
  1573. return -ENOMEM;
  1574. INIT_LIST_HEAD(&cipher_handle->alg_list);
  1575. drvdata->cipher_handle = cipher_handle;
  1576. /* Linux crypto */
  1577. dev_dbg(dev, "Number of algorithms = %zu\n",
  1578. ARRAY_SIZE(skcipher_algs));
  1579. for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
  1580. if ((skcipher_algs[alg].min_hw_rev > drvdata->hw_rev) ||
  1581. !(drvdata->std_bodies & skcipher_algs[alg].std_body) ||
  1582. (drvdata->sec_disabled && skcipher_algs[alg].sec_func))
  1583. continue;
  1584. dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
  1585. t_alg = cc_create_alg(&skcipher_algs[alg], dev);
  1586. if (IS_ERR(t_alg)) {
  1587. rc = PTR_ERR(t_alg);
  1588. dev_err(dev, "%s alg allocation failed\n",
  1589. skcipher_algs[alg].driver_name);
  1590. goto fail0;
  1591. }
  1592. t_alg->drvdata = drvdata;
  1593. dev_dbg(dev, "registering %s\n",
  1594. skcipher_algs[alg].driver_name);
  1595. rc = crypto_register_skcipher(&t_alg->skcipher_alg);
  1596. dev_dbg(dev, "%s alg registration rc = %x\n",
  1597. t_alg->skcipher_alg.base.cra_driver_name, rc);
  1598. if (rc) {
  1599. dev_err(dev, "%s alg registration failed\n",
  1600. t_alg->skcipher_alg.base.cra_driver_name);
  1601. kfree(t_alg);
  1602. goto fail0;
  1603. } else {
  1604. list_add_tail(&t_alg->entry,
  1605. &cipher_handle->alg_list);
  1606. dev_dbg(dev, "Registered %s\n",
  1607. t_alg->skcipher_alg.base.cra_driver_name);
  1608. }
  1609. }
  1610. return 0;
  1611. fail0:
  1612. cc_cipher_free(drvdata);
  1613. return rc;
  1614. }