qat_algs.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) 2014 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. qat-linux@intel.com
  15. BSD LICENSE
  16. Copyright(c) 2014 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/slab.h>
  43. #include <linux/crypto.h>
  44. #include <crypto/internal/aead.h>
  45. #include <crypto/aes.h>
  46. #include <crypto/sha.h>
  47. #include <crypto/hash.h>
  48. #include <crypto/hmac.h>
  49. #include <crypto/algapi.h>
  50. #include <crypto/authenc.h>
  51. #include <linux/dma-mapping.h>
  52. #include "adf_accel_devices.h"
  53. #include "adf_transport.h"
  54. #include "adf_common_drv.h"
  55. #include "qat_crypto.h"
  56. #include "icp_qat_hw.h"
  57. #include "icp_qat_fw.h"
  58. #include "icp_qat_fw_la.h"
  59. #define QAT_AES_HW_CONFIG_ENC(alg, mode) \
  60. ICP_QAT_HW_CIPHER_CONFIG_BUILD(mode, alg, \
  61. ICP_QAT_HW_CIPHER_NO_CONVERT, \
  62. ICP_QAT_HW_CIPHER_ENCRYPT)
  63. #define QAT_AES_HW_CONFIG_DEC(alg, mode) \
  64. ICP_QAT_HW_CIPHER_CONFIG_BUILD(mode, alg, \
  65. ICP_QAT_HW_CIPHER_KEY_CONVERT, \
  66. ICP_QAT_HW_CIPHER_DECRYPT)
  67. static DEFINE_MUTEX(algs_lock);
  68. static unsigned int active_devs;
  69. struct qat_alg_buf {
  70. uint32_t len;
  71. uint32_t resrvd;
  72. uint64_t addr;
  73. } __packed;
  74. struct qat_alg_buf_list {
  75. uint64_t resrvd;
  76. uint32_t num_bufs;
  77. uint32_t num_mapped_bufs;
  78. struct qat_alg_buf bufers[];
  79. } __packed __aligned(64);
  80. /* Common content descriptor */
  81. struct qat_alg_cd {
  82. union {
  83. struct qat_enc { /* Encrypt content desc */
  84. struct icp_qat_hw_cipher_algo_blk cipher;
  85. struct icp_qat_hw_auth_algo_blk hash;
  86. } qat_enc_cd;
  87. struct qat_dec { /* Decrytp content desc */
  88. struct icp_qat_hw_auth_algo_blk hash;
  89. struct icp_qat_hw_cipher_algo_blk cipher;
  90. } qat_dec_cd;
  91. };
  92. } __aligned(64);
  93. struct qat_alg_aead_ctx {
  94. struct qat_alg_cd *enc_cd;
  95. struct qat_alg_cd *dec_cd;
  96. dma_addr_t enc_cd_paddr;
  97. dma_addr_t dec_cd_paddr;
  98. struct icp_qat_fw_la_bulk_req enc_fw_req;
  99. struct icp_qat_fw_la_bulk_req dec_fw_req;
  100. struct crypto_shash *hash_tfm;
  101. enum icp_qat_hw_auth_algo qat_hash_alg;
  102. struct qat_crypto_instance *inst;
  103. };
  104. struct qat_alg_ablkcipher_ctx {
  105. struct icp_qat_hw_cipher_algo_blk *enc_cd;
  106. struct icp_qat_hw_cipher_algo_blk *dec_cd;
  107. dma_addr_t enc_cd_paddr;
  108. dma_addr_t dec_cd_paddr;
  109. struct icp_qat_fw_la_bulk_req enc_fw_req;
  110. struct icp_qat_fw_la_bulk_req dec_fw_req;
  111. struct qat_crypto_instance *inst;
  112. struct crypto_tfm *tfm;
  113. spinlock_t lock; /* protects qat_alg_ablkcipher_ctx struct */
  114. };
  115. static int qat_get_inter_state_size(enum icp_qat_hw_auth_algo qat_hash_alg)
  116. {
  117. switch (qat_hash_alg) {
  118. case ICP_QAT_HW_AUTH_ALGO_SHA1:
  119. return ICP_QAT_HW_SHA1_STATE1_SZ;
  120. case ICP_QAT_HW_AUTH_ALGO_SHA256:
  121. return ICP_QAT_HW_SHA256_STATE1_SZ;
  122. case ICP_QAT_HW_AUTH_ALGO_SHA512:
  123. return ICP_QAT_HW_SHA512_STATE1_SZ;
  124. default:
  125. return -EFAULT;
  126. };
  127. return -EFAULT;
  128. }
  129. static int qat_alg_do_precomputes(struct icp_qat_hw_auth_algo_blk *hash,
  130. struct qat_alg_aead_ctx *ctx,
  131. const uint8_t *auth_key,
  132. unsigned int auth_keylen)
  133. {
  134. SHASH_DESC_ON_STACK(shash, ctx->hash_tfm);
  135. struct sha1_state sha1;
  136. struct sha256_state sha256;
  137. struct sha512_state sha512;
  138. int block_size = crypto_shash_blocksize(ctx->hash_tfm);
  139. int digest_size = crypto_shash_digestsize(ctx->hash_tfm);
  140. char ipad[block_size];
  141. char opad[block_size];
  142. __be32 *hash_state_out;
  143. __be64 *hash512_state_out;
  144. int i, offset;
  145. memset(ipad, 0, block_size);
  146. memset(opad, 0, block_size);
  147. shash->tfm = ctx->hash_tfm;
  148. shash->flags = 0x0;
  149. if (auth_keylen > block_size) {
  150. int ret = crypto_shash_digest(shash, auth_key,
  151. auth_keylen, ipad);
  152. if (ret)
  153. return ret;
  154. memcpy(opad, ipad, digest_size);
  155. } else {
  156. memcpy(ipad, auth_key, auth_keylen);
  157. memcpy(opad, auth_key, auth_keylen);
  158. }
  159. for (i = 0; i < block_size; i++) {
  160. char *ipad_ptr = ipad + i;
  161. char *opad_ptr = opad + i;
  162. *ipad_ptr ^= HMAC_IPAD_VALUE;
  163. *opad_ptr ^= HMAC_OPAD_VALUE;
  164. }
  165. if (crypto_shash_init(shash))
  166. return -EFAULT;
  167. if (crypto_shash_update(shash, ipad, block_size))
  168. return -EFAULT;
  169. hash_state_out = (__be32 *)hash->sha.state1;
  170. hash512_state_out = (__be64 *)hash_state_out;
  171. switch (ctx->qat_hash_alg) {
  172. case ICP_QAT_HW_AUTH_ALGO_SHA1:
  173. if (crypto_shash_export(shash, &sha1))
  174. return -EFAULT;
  175. for (i = 0; i < digest_size >> 2; i++, hash_state_out++)
  176. *hash_state_out = cpu_to_be32(*(sha1.state + i));
  177. break;
  178. case ICP_QAT_HW_AUTH_ALGO_SHA256:
  179. if (crypto_shash_export(shash, &sha256))
  180. return -EFAULT;
  181. for (i = 0; i < digest_size >> 2; i++, hash_state_out++)
  182. *hash_state_out = cpu_to_be32(*(sha256.state + i));
  183. break;
  184. case ICP_QAT_HW_AUTH_ALGO_SHA512:
  185. if (crypto_shash_export(shash, &sha512))
  186. return -EFAULT;
  187. for (i = 0; i < digest_size >> 3; i++, hash512_state_out++)
  188. *hash512_state_out = cpu_to_be64(*(sha512.state + i));
  189. break;
  190. default:
  191. return -EFAULT;
  192. }
  193. if (crypto_shash_init(shash))
  194. return -EFAULT;
  195. if (crypto_shash_update(shash, opad, block_size))
  196. return -EFAULT;
  197. offset = round_up(qat_get_inter_state_size(ctx->qat_hash_alg), 8);
  198. hash_state_out = (__be32 *)(hash->sha.state1 + offset);
  199. hash512_state_out = (__be64 *)hash_state_out;
  200. switch (ctx->qat_hash_alg) {
  201. case ICP_QAT_HW_AUTH_ALGO_SHA1:
  202. if (crypto_shash_export(shash, &sha1))
  203. return -EFAULT;
  204. for (i = 0; i < digest_size >> 2; i++, hash_state_out++)
  205. *hash_state_out = cpu_to_be32(*(sha1.state + i));
  206. break;
  207. case ICP_QAT_HW_AUTH_ALGO_SHA256:
  208. if (crypto_shash_export(shash, &sha256))
  209. return -EFAULT;
  210. for (i = 0; i < digest_size >> 2; i++, hash_state_out++)
  211. *hash_state_out = cpu_to_be32(*(sha256.state + i));
  212. break;
  213. case ICP_QAT_HW_AUTH_ALGO_SHA512:
  214. if (crypto_shash_export(shash, &sha512))
  215. return -EFAULT;
  216. for (i = 0; i < digest_size >> 3; i++, hash512_state_out++)
  217. *hash512_state_out = cpu_to_be64(*(sha512.state + i));
  218. break;
  219. default:
  220. return -EFAULT;
  221. }
  222. memzero_explicit(ipad, block_size);
  223. memzero_explicit(opad, block_size);
  224. return 0;
  225. }
  226. static void qat_alg_init_common_hdr(struct icp_qat_fw_comn_req_hdr *header)
  227. {
  228. header->hdr_flags =
  229. ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
  230. header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_LA;
  231. header->comn_req_flags =
  232. ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_CD_FLD_TYPE_64BIT_ADR,
  233. QAT_COMN_PTR_TYPE_SGL);
  234. ICP_QAT_FW_LA_PARTIAL_SET(header->serv_specif_flags,
  235. ICP_QAT_FW_LA_PARTIAL_NONE);
  236. ICP_QAT_FW_LA_CIPH_IV_FLD_FLAG_SET(header->serv_specif_flags,
  237. ICP_QAT_FW_CIPH_IV_16BYTE_DATA);
  238. ICP_QAT_FW_LA_PROTO_SET(header->serv_specif_flags,
  239. ICP_QAT_FW_LA_NO_PROTO);
  240. ICP_QAT_FW_LA_UPDATE_STATE_SET(header->serv_specif_flags,
  241. ICP_QAT_FW_LA_NO_UPDATE_STATE);
  242. }
  243. static int qat_alg_aead_init_enc_session(struct crypto_aead *aead_tfm,
  244. int alg,
  245. struct crypto_authenc_keys *keys,
  246. int mode)
  247. {
  248. struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(aead_tfm);
  249. unsigned int digestsize = crypto_aead_authsize(aead_tfm);
  250. struct qat_enc *enc_ctx = &ctx->enc_cd->qat_enc_cd;
  251. struct icp_qat_hw_cipher_algo_blk *cipher = &enc_ctx->cipher;
  252. struct icp_qat_hw_auth_algo_blk *hash =
  253. (struct icp_qat_hw_auth_algo_blk *)((char *)enc_ctx +
  254. sizeof(struct icp_qat_hw_auth_setup) + keys->enckeylen);
  255. struct icp_qat_fw_la_bulk_req *req_tmpl = &ctx->enc_fw_req;
  256. struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req_tmpl->cd_pars;
  257. struct icp_qat_fw_comn_req_hdr *header = &req_tmpl->comn_hdr;
  258. void *ptr = &req_tmpl->cd_ctrl;
  259. struct icp_qat_fw_cipher_cd_ctrl_hdr *cipher_cd_ctrl = ptr;
  260. struct icp_qat_fw_auth_cd_ctrl_hdr *hash_cd_ctrl = ptr;
  261. /* CD setup */
  262. cipher->aes.cipher_config.val = QAT_AES_HW_CONFIG_ENC(alg, mode);
  263. memcpy(cipher->aes.key, keys->enckey, keys->enckeylen);
  264. hash->sha.inner_setup.auth_config.config =
  265. ICP_QAT_HW_AUTH_CONFIG_BUILD(ICP_QAT_HW_AUTH_MODE1,
  266. ctx->qat_hash_alg, digestsize);
  267. hash->sha.inner_setup.auth_counter.counter =
  268. cpu_to_be32(crypto_shash_blocksize(ctx->hash_tfm));
  269. if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
  270. return -EFAULT;
  271. /* Request setup */
  272. qat_alg_init_common_hdr(header);
  273. header->service_cmd_id = ICP_QAT_FW_LA_CMD_CIPHER_HASH;
  274. ICP_QAT_FW_LA_DIGEST_IN_BUFFER_SET(header->serv_specif_flags,
  275. ICP_QAT_FW_LA_DIGEST_IN_BUFFER);
  276. ICP_QAT_FW_LA_RET_AUTH_SET(header->serv_specif_flags,
  277. ICP_QAT_FW_LA_RET_AUTH_RES);
  278. ICP_QAT_FW_LA_CMP_AUTH_SET(header->serv_specif_flags,
  279. ICP_QAT_FW_LA_NO_CMP_AUTH_RES);
  280. cd_pars->u.s.content_desc_addr = ctx->enc_cd_paddr;
  281. cd_pars->u.s.content_desc_params_sz = sizeof(struct qat_alg_cd) >> 3;
  282. /* Cipher CD config setup */
  283. cipher_cd_ctrl->cipher_key_sz = keys->enckeylen >> 3;
  284. cipher_cd_ctrl->cipher_state_sz = AES_BLOCK_SIZE >> 3;
  285. cipher_cd_ctrl->cipher_cfg_offset = 0;
  286. ICP_QAT_FW_COMN_CURR_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_CIPHER);
  287. ICP_QAT_FW_COMN_NEXT_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_AUTH);
  288. /* Auth CD config setup */
  289. hash_cd_ctrl->hash_cfg_offset = ((char *)hash - (char *)cipher) >> 3;
  290. hash_cd_ctrl->hash_flags = ICP_QAT_FW_AUTH_HDR_FLAG_NO_NESTED;
  291. hash_cd_ctrl->inner_res_sz = digestsize;
  292. hash_cd_ctrl->final_sz = digestsize;
  293. switch (ctx->qat_hash_alg) {
  294. case ICP_QAT_HW_AUTH_ALGO_SHA1:
  295. hash_cd_ctrl->inner_state1_sz =
  296. round_up(ICP_QAT_HW_SHA1_STATE1_SZ, 8);
  297. hash_cd_ctrl->inner_state2_sz =
  298. round_up(ICP_QAT_HW_SHA1_STATE2_SZ, 8);
  299. break;
  300. case ICP_QAT_HW_AUTH_ALGO_SHA256:
  301. hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA256_STATE1_SZ;
  302. hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA256_STATE2_SZ;
  303. break;
  304. case ICP_QAT_HW_AUTH_ALGO_SHA512:
  305. hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA512_STATE1_SZ;
  306. hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA512_STATE2_SZ;
  307. break;
  308. default:
  309. break;
  310. }
  311. hash_cd_ctrl->inner_state2_offset = hash_cd_ctrl->hash_cfg_offset +
  312. ((sizeof(struct icp_qat_hw_auth_setup) +
  313. round_up(hash_cd_ctrl->inner_state1_sz, 8)) >> 3);
  314. ICP_QAT_FW_COMN_CURR_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_AUTH);
  315. ICP_QAT_FW_COMN_NEXT_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_DRAM_WR);
  316. return 0;
  317. }
  318. static int qat_alg_aead_init_dec_session(struct crypto_aead *aead_tfm,
  319. int alg,
  320. struct crypto_authenc_keys *keys,
  321. int mode)
  322. {
  323. struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(aead_tfm);
  324. unsigned int digestsize = crypto_aead_authsize(aead_tfm);
  325. struct qat_dec *dec_ctx = &ctx->dec_cd->qat_dec_cd;
  326. struct icp_qat_hw_auth_algo_blk *hash = &dec_ctx->hash;
  327. struct icp_qat_hw_cipher_algo_blk *cipher =
  328. (struct icp_qat_hw_cipher_algo_blk *)((char *)dec_ctx +
  329. sizeof(struct icp_qat_hw_auth_setup) +
  330. roundup(crypto_shash_digestsize(ctx->hash_tfm), 8) * 2);
  331. struct icp_qat_fw_la_bulk_req *req_tmpl = &ctx->dec_fw_req;
  332. struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req_tmpl->cd_pars;
  333. struct icp_qat_fw_comn_req_hdr *header = &req_tmpl->comn_hdr;
  334. void *ptr = &req_tmpl->cd_ctrl;
  335. struct icp_qat_fw_cipher_cd_ctrl_hdr *cipher_cd_ctrl = ptr;
  336. struct icp_qat_fw_auth_cd_ctrl_hdr *hash_cd_ctrl = ptr;
  337. struct icp_qat_fw_la_auth_req_params *auth_param =
  338. (struct icp_qat_fw_la_auth_req_params *)
  339. ((char *)&req_tmpl->serv_specif_rqpars +
  340. sizeof(struct icp_qat_fw_la_cipher_req_params));
  341. /* CD setup */
  342. cipher->aes.cipher_config.val = QAT_AES_HW_CONFIG_DEC(alg, mode);
  343. memcpy(cipher->aes.key, keys->enckey, keys->enckeylen);
  344. hash->sha.inner_setup.auth_config.config =
  345. ICP_QAT_HW_AUTH_CONFIG_BUILD(ICP_QAT_HW_AUTH_MODE1,
  346. ctx->qat_hash_alg,
  347. digestsize);
  348. hash->sha.inner_setup.auth_counter.counter =
  349. cpu_to_be32(crypto_shash_blocksize(ctx->hash_tfm));
  350. if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen))
  351. return -EFAULT;
  352. /* Request setup */
  353. qat_alg_init_common_hdr(header);
  354. header->service_cmd_id = ICP_QAT_FW_LA_CMD_HASH_CIPHER;
  355. ICP_QAT_FW_LA_DIGEST_IN_BUFFER_SET(header->serv_specif_flags,
  356. ICP_QAT_FW_LA_DIGEST_IN_BUFFER);
  357. ICP_QAT_FW_LA_RET_AUTH_SET(header->serv_specif_flags,
  358. ICP_QAT_FW_LA_NO_RET_AUTH_RES);
  359. ICP_QAT_FW_LA_CMP_AUTH_SET(header->serv_specif_flags,
  360. ICP_QAT_FW_LA_CMP_AUTH_RES);
  361. cd_pars->u.s.content_desc_addr = ctx->dec_cd_paddr;
  362. cd_pars->u.s.content_desc_params_sz = sizeof(struct qat_alg_cd) >> 3;
  363. /* Cipher CD config setup */
  364. cipher_cd_ctrl->cipher_key_sz = keys->enckeylen >> 3;
  365. cipher_cd_ctrl->cipher_state_sz = AES_BLOCK_SIZE >> 3;
  366. cipher_cd_ctrl->cipher_cfg_offset =
  367. (sizeof(struct icp_qat_hw_auth_setup) +
  368. roundup(crypto_shash_digestsize(ctx->hash_tfm), 8) * 2) >> 3;
  369. ICP_QAT_FW_COMN_CURR_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_CIPHER);
  370. ICP_QAT_FW_COMN_NEXT_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_DRAM_WR);
  371. /* Auth CD config setup */
  372. hash_cd_ctrl->hash_cfg_offset = 0;
  373. hash_cd_ctrl->hash_flags = ICP_QAT_FW_AUTH_HDR_FLAG_NO_NESTED;
  374. hash_cd_ctrl->inner_res_sz = digestsize;
  375. hash_cd_ctrl->final_sz = digestsize;
  376. switch (ctx->qat_hash_alg) {
  377. case ICP_QAT_HW_AUTH_ALGO_SHA1:
  378. hash_cd_ctrl->inner_state1_sz =
  379. round_up(ICP_QAT_HW_SHA1_STATE1_SZ, 8);
  380. hash_cd_ctrl->inner_state2_sz =
  381. round_up(ICP_QAT_HW_SHA1_STATE2_SZ, 8);
  382. break;
  383. case ICP_QAT_HW_AUTH_ALGO_SHA256:
  384. hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA256_STATE1_SZ;
  385. hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA256_STATE2_SZ;
  386. break;
  387. case ICP_QAT_HW_AUTH_ALGO_SHA512:
  388. hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA512_STATE1_SZ;
  389. hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA512_STATE2_SZ;
  390. break;
  391. default:
  392. break;
  393. }
  394. hash_cd_ctrl->inner_state2_offset = hash_cd_ctrl->hash_cfg_offset +
  395. ((sizeof(struct icp_qat_hw_auth_setup) +
  396. round_up(hash_cd_ctrl->inner_state1_sz, 8)) >> 3);
  397. auth_param->auth_res_sz = digestsize;
  398. ICP_QAT_FW_COMN_CURR_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_AUTH);
  399. ICP_QAT_FW_COMN_NEXT_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_CIPHER);
  400. return 0;
  401. }
  402. static void qat_alg_ablkcipher_init_com(struct qat_alg_ablkcipher_ctx *ctx,
  403. struct icp_qat_fw_la_bulk_req *req,
  404. struct icp_qat_hw_cipher_algo_blk *cd,
  405. const uint8_t *key, unsigned int keylen)
  406. {
  407. struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req->cd_pars;
  408. struct icp_qat_fw_comn_req_hdr *header = &req->comn_hdr;
  409. struct icp_qat_fw_cipher_cd_ctrl_hdr *cd_ctrl = (void *)&req->cd_ctrl;
  410. memcpy(cd->aes.key, key, keylen);
  411. qat_alg_init_common_hdr(header);
  412. header->service_cmd_id = ICP_QAT_FW_LA_CMD_CIPHER;
  413. cd_pars->u.s.content_desc_params_sz =
  414. sizeof(struct icp_qat_hw_cipher_algo_blk) >> 3;
  415. /* Cipher CD config setup */
  416. cd_ctrl->cipher_key_sz = keylen >> 3;
  417. cd_ctrl->cipher_state_sz = AES_BLOCK_SIZE >> 3;
  418. cd_ctrl->cipher_cfg_offset = 0;
  419. ICP_QAT_FW_COMN_CURR_ID_SET(cd_ctrl, ICP_QAT_FW_SLICE_CIPHER);
  420. ICP_QAT_FW_COMN_NEXT_ID_SET(cd_ctrl, ICP_QAT_FW_SLICE_DRAM_WR);
  421. }
  422. static void qat_alg_ablkcipher_init_enc(struct qat_alg_ablkcipher_ctx *ctx,
  423. int alg, const uint8_t *key,
  424. unsigned int keylen, int mode)
  425. {
  426. struct icp_qat_hw_cipher_algo_blk *enc_cd = ctx->enc_cd;
  427. struct icp_qat_fw_la_bulk_req *req = &ctx->enc_fw_req;
  428. struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req->cd_pars;
  429. qat_alg_ablkcipher_init_com(ctx, req, enc_cd, key, keylen);
  430. cd_pars->u.s.content_desc_addr = ctx->enc_cd_paddr;
  431. enc_cd->aes.cipher_config.val = QAT_AES_HW_CONFIG_ENC(alg, mode);
  432. }
  433. static void qat_alg_ablkcipher_init_dec(struct qat_alg_ablkcipher_ctx *ctx,
  434. int alg, const uint8_t *key,
  435. unsigned int keylen, int mode)
  436. {
  437. struct icp_qat_hw_cipher_algo_blk *dec_cd = ctx->dec_cd;
  438. struct icp_qat_fw_la_bulk_req *req = &ctx->dec_fw_req;
  439. struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req->cd_pars;
  440. qat_alg_ablkcipher_init_com(ctx, req, dec_cd, key, keylen);
  441. cd_pars->u.s.content_desc_addr = ctx->dec_cd_paddr;
  442. if (mode != ICP_QAT_HW_CIPHER_CTR_MODE)
  443. dec_cd->aes.cipher_config.val =
  444. QAT_AES_HW_CONFIG_DEC(alg, mode);
  445. else
  446. dec_cd->aes.cipher_config.val =
  447. QAT_AES_HW_CONFIG_ENC(alg, mode);
  448. }
  449. static int qat_alg_validate_key(int key_len, int *alg, int mode)
  450. {
  451. if (mode != ICP_QAT_HW_CIPHER_XTS_MODE) {
  452. switch (key_len) {
  453. case AES_KEYSIZE_128:
  454. *alg = ICP_QAT_HW_CIPHER_ALGO_AES128;
  455. break;
  456. case AES_KEYSIZE_192:
  457. *alg = ICP_QAT_HW_CIPHER_ALGO_AES192;
  458. break;
  459. case AES_KEYSIZE_256:
  460. *alg = ICP_QAT_HW_CIPHER_ALGO_AES256;
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. } else {
  466. switch (key_len) {
  467. case AES_KEYSIZE_128 << 1:
  468. *alg = ICP_QAT_HW_CIPHER_ALGO_AES128;
  469. break;
  470. case AES_KEYSIZE_256 << 1:
  471. *alg = ICP_QAT_HW_CIPHER_ALGO_AES256;
  472. break;
  473. default:
  474. return -EINVAL;
  475. }
  476. }
  477. return 0;
  478. }
  479. static int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key,
  480. unsigned int keylen, int mode)
  481. {
  482. struct crypto_authenc_keys keys;
  483. int alg;
  484. if (crypto_authenc_extractkeys(&keys, key, keylen))
  485. goto bad_key;
  486. if (qat_alg_validate_key(keys.enckeylen, &alg, mode))
  487. goto bad_key;
  488. if (qat_alg_aead_init_enc_session(tfm, alg, &keys, mode))
  489. goto error;
  490. if (qat_alg_aead_init_dec_session(tfm, alg, &keys, mode))
  491. goto error;
  492. memzero_explicit(&keys, sizeof(keys));
  493. return 0;
  494. bad_key:
  495. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  496. memzero_explicit(&keys, sizeof(keys));
  497. return -EINVAL;
  498. error:
  499. memzero_explicit(&keys, sizeof(keys));
  500. return -EFAULT;
  501. }
  502. static int qat_alg_ablkcipher_init_sessions(struct qat_alg_ablkcipher_ctx *ctx,
  503. const uint8_t *key,
  504. unsigned int keylen,
  505. int mode)
  506. {
  507. int alg;
  508. if (qat_alg_validate_key(keylen, &alg, mode))
  509. goto bad_key;
  510. qat_alg_ablkcipher_init_enc(ctx, alg, key, keylen, mode);
  511. qat_alg_ablkcipher_init_dec(ctx, alg, key, keylen, mode);
  512. return 0;
  513. bad_key:
  514. crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  515. return -EINVAL;
  516. }
  517. static int qat_alg_aead_setkey(struct crypto_aead *tfm, const uint8_t *key,
  518. unsigned int keylen)
  519. {
  520. struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm);
  521. struct device *dev;
  522. if (ctx->enc_cd) {
  523. /* rekeying */
  524. dev = &GET_DEV(ctx->inst->accel_dev);
  525. memset(ctx->enc_cd, 0, sizeof(*ctx->enc_cd));
  526. memset(ctx->dec_cd, 0, sizeof(*ctx->dec_cd));
  527. memset(&ctx->enc_fw_req, 0, sizeof(ctx->enc_fw_req));
  528. memset(&ctx->dec_fw_req, 0, sizeof(ctx->dec_fw_req));
  529. } else {
  530. /* new key */
  531. int node = get_current_node();
  532. struct qat_crypto_instance *inst =
  533. qat_crypto_get_instance_node(node);
  534. if (!inst) {
  535. return -EINVAL;
  536. }
  537. dev = &GET_DEV(inst->accel_dev);
  538. ctx->inst = inst;
  539. ctx->enc_cd = dma_zalloc_coherent(dev, sizeof(*ctx->enc_cd),
  540. &ctx->enc_cd_paddr,
  541. GFP_ATOMIC);
  542. if (!ctx->enc_cd) {
  543. return -ENOMEM;
  544. }
  545. ctx->dec_cd = dma_zalloc_coherent(dev, sizeof(*ctx->dec_cd),
  546. &ctx->dec_cd_paddr,
  547. GFP_ATOMIC);
  548. if (!ctx->dec_cd) {
  549. goto out_free_enc;
  550. }
  551. }
  552. if (qat_alg_aead_init_sessions(tfm, key, keylen,
  553. ICP_QAT_HW_CIPHER_CBC_MODE))
  554. goto out_free_all;
  555. return 0;
  556. out_free_all:
  557. memset(ctx->dec_cd, 0, sizeof(struct qat_alg_cd));
  558. dma_free_coherent(dev, sizeof(struct qat_alg_cd),
  559. ctx->dec_cd, ctx->dec_cd_paddr);
  560. ctx->dec_cd = NULL;
  561. out_free_enc:
  562. memset(ctx->enc_cd, 0, sizeof(struct qat_alg_cd));
  563. dma_free_coherent(dev, sizeof(struct qat_alg_cd),
  564. ctx->enc_cd, ctx->enc_cd_paddr);
  565. ctx->enc_cd = NULL;
  566. return -ENOMEM;
  567. }
  568. static void qat_alg_free_bufl(struct qat_crypto_instance *inst,
  569. struct qat_crypto_request *qat_req)
  570. {
  571. struct device *dev = &GET_DEV(inst->accel_dev);
  572. struct qat_alg_buf_list *bl = qat_req->buf.bl;
  573. struct qat_alg_buf_list *blout = qat_req->buf.blout;
  574. dma_addr_t blp = qat_req->buf.blp;
  575. dma_addr_t blpout = qat_req->buf.bloutp;
  576. size_t sz = qat_req->buf.sz;
  577. size_t sz_out = qat_req->buf.sz_out;
  578. int i;
  579. for (i = 0; i < bl->num_bufs; i++)
  580. dma_unmap_single(dev, bl->bufers[i].addr,
  581. bl->bufers[i].len, DMA_BIDIRECTIONAL);
  582. dma_unmap_single(dev, blp, sz, DMA_TO_DEVICE);
  583. kfree(bl);
  584. if (blp != blpout) {
  585. /* If out of place operation dma unmap only data */
  586. int bufless = blout->num_bufs - blout->num_mapped_bufs;
  587. for (i = bufless; i < blout->num_bufs; i++) {
  588. dma_unmap_single(dev, blout->bufers[i].addr,
  589. blout->bufers[i].len,
  590. DMA_BIDIRECTIONAL);
  591. }
  592. dma_unmap_single(dev, blpout, sz_out, DMA_TO_DEVICE);
  593. kfree(blout);
  594. }
  595. }
  596. static int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst,
  597. struct scatterlist *sgl,
  598. struct scatterlist *sglout,
  599. struct qat_crypto_request *qat_req)
  600. {
  601. struct device *dev = &GET_DEV(inst->accel_dev);
  602. int i, sg_nctr = 0;
  603. int n = sg_nents(sgl);
  604. struct qat_alg_buf_list *bufl;
  605. struct qat_alg_buf_list *buflout = NULL;
  606. dma_addr_t blp;
  607. dma_addr_t bloutp = 0;
  608. struct scatterlist *sg;
  609. size_t sz_out, sz = sizeof(struct qat_alg_buf_list) +
  610. ((1 + n) * sizeof(struct qat_alg_buf));
  611. if (unlikely(!n))
  612. return -EINVAL;
  613. bufl = kzalloc_node(sz, GFP_ATOMIC,
  614. dev_to_node(&GET_DEV(inst->accel_dev)));
  615. if (unlikely(!bufl))
  616. return -ENOMEM;
  617. blp = dma_map_single(dev, bufl, sz, DMA_TO_DEVICE);
  618. if (unlikely(dma_mapping_error(dev, blp)))
  619. goto err_in;
  620. for_each_sg(sgl, sg, n, i) {
  621. int y = sg_nctr;
  622. if (!sg->length)
  623. continue;
  624. bufl->bufers[y].addr = dma_map_single(dev, sg_virt(sg),
  625. sg->length,
  626. DMA_BIDIRECTIONAL);
  627. bufl->bufers[y].len = sg->length;
  628. if (unlikely(dma_mapping_error(dev, bufl->bufers[y].addr)))
  629. goto err_in;
  630. sg_nctr++;
  631. }
  632. bufl->num_bufs = sg_nctr;
  633. qat_req->buf.bl = bufl;
  634. qat_req->buf.blp = blp;
  635. qat_req->buf.sz = sz;
  636. /* Handle out of place operation */
  637. if (sgl != sglout) {
  638. struct qat_alg_buf *bufers;
  639. n = sg_nents(sglout);
  640. sz_out = sizeof(struct qat_alg_buf_list) +
  641. ((1 + n) * sizeof(struct qat_alg_buf));
  642. sg_nctr = 0;
  643. buflout = kzalloc_node(sz_out, GFP_ATOMIC,
  644. dev_to_node(&GET_DEV(inst->accel_dev)));
  645. if (unlikely(!buflout))
  646. goto err_in;
  647. bloutp = dma_map_single(dev, buflout, sz_out, DMA_TO_DEVICE);
  648. if (unlikely(dma_mapping_error(dev, bloutp)))
  649. goto err_out;
  650. bufers = buflout->bufers;
  651. for_each_sg(sglout, sg, n, i) {
  652. int y = sg_nctr;
  653. if (!sg->length)
  654. continue;
  655. bufers[y].addr = dma_map_single(dev, sg_virt(sg),
  656. sg->length,
  657. DMA_BIDIRECTIONAL);
  658. if (unlikely(dma_mapping_error(dev, bufers[y].addr)))
  659. goto err_out;
  660. bufers[y].len = sg->length;
  661. sg_nctr++;
  662. }
  663. buflout->num_bufs = sg_nctr;
  664. buflout->num_mapped_bufs = sg_nctr;
  665. qat_req->buf.blout = buflout;
  666. qat_req->buf.bloutp = bloutp;
  667. qat_req->buf.sz_out = sz_out;
  668. } else {
  669. /* Otherwise set the src and dst to the same address */
  670. qat_req->buf.bloutp = qat_req->buf.blp;
  671. qat_req->buf.sz_out = 0;
  672. }
  673. return 0;
  674. err_out:
  675. n = sg_nents(sglout);
  676. for (i = 0; i < n; i++)
  677. if (!dma_mapping_error(dev, buflout->bufers[i].addr))
  678. dma_unmap_single(dev, buflout->bufers[i].addr,
  679. buflout->bufers[i].len,
  680. DMA_BIDIRECTIONAL);
  681. if (!dma_mapping_error(dev, bloutp))
  682. dma_unmap_single(dev, bloutp, sz_out, DMA_TO_DEVICE);
  683. kfree(buflout);
  684. err_in:
  685. n = sg_nents(sgl);
  686. for (i = 0; i < n; i++)
  687. if (!dma_mapping_error(dev, bufl->bufers[i].addr))
  688. dma_unmap_single(dev, bufl->bufers[i].addr,
  689. bufl->bufers[i].len,
  690. DMA_BIDIRECTIONAL);
  691. if (!dma_mapping_error(dev, blp))
  692. dma_unmap_single(dev, blp, sz, DMA_TO_DEVICE);
  693. kfree(bufl);
  694. dev_err(dev, "Failed to map buf for dma\n");
  695. return -ENOMEM;
  696. }
  697. static void qat_aead_alg_callback(struct icp_qat_fw_la_resp *qat_resp,
  698. struct qat_crypto_request *qat_req)
  699. {
  700. struct qat_alg_aead_ctx *ctx = qat_req->aead_ctx;
  701. struct qat_crypto_instance *inst = ctx->inst;
  702. struct aead_request *areq = qat_req->aead_req;
  703. uint8_t stat_filed = qat_resp->comn_resp.comn_status;
  704. int res = 0, qat_res = ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(stat_filed);
  705. qat_alg_free_bufl(inst, qat_req);
  706. if (unlikely(qat_res != ICP_QAT_FW_COMN_STATUS_FLAG_OK))
  707. res = -EBADMSG;
  708. areq->base.complete(&areq->base, res);
  709. }
  710. static void qat_ablkcipher_alg_callback(struct icp_qat_fw_la_resp *qat_resp,
  711. struct qat_crypto_request *qat_req)
  712. {
  713. struct qat_alg_ablkcipher_ctx *ctx = qat_req->ablkcipher_ctx;
  714. struct qat_crypto_instance *inst = ctx->inst;
  715. struct ablkcipher_request *areq = qat_req->ablkcipher_req;
  716. uint8_t stat_filed = qat_resp->comn_resp.comn_status;
  717. int res = 0, qat_res = ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(stat_filed);
  718. qat_alg_free_bufl(inst, qat_req);
  719. if (unlikely(qat_res != ICP_QAT_FW_COMN_STATUS_FLAG_OK))
  720. res = -EINVAL;
  721. areq->base.complete(&areq->base, res);
  722. }
  723. void qat_alg_callback(void *resp)
  724. {
  725. struct icp_qat_fw_la_resp *qat_resp = resp;
  726. struct qat_crypto_request *qat_req =
  727. (void *)(__force long)qat_resp->opaque_data;
  728. qat_req->cb(qat_resp, qat_req);
  729. }
  730. static int qat_alg_aead_dec(struct aead_request *areq)
  731. {
  732. struct crypto_aead *aead_tfm = crypto_aead_reqtfm(areq);
  733. struct crypto_tfm *tfm = crypto_aead_tfm(aead_tfm);
  734. struct qat_alg_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  735. struct qat_crypto_request *qat_req = aead_request_ctx(areq);
  736. struct icp_qat_fw_la_cipher_req_params *cipher_param;
  737. struct icp_qat_fw_la_auth_req_params *auth_param;
  738. struct icp_qat_fw_la_bulk_req *msg;
  739. int digst_size = crypto_aead_authsize(aead_tfm);
  740. int ret, ctr = 0;
  741. ret = qat_alg_sgl_to_bufl(ctx->inst, areq->src, areq->dst, qat_req);
  742. if (unlikely(ret))
  743. return ret;
  744. msg = &qat_req->req;
  745. *msg = ctx->dec_fw_req;
  746. qat_req->aead_ctx = ctx;
  747. qat_req->aead_req = areq;
  748. qat_req->cb = qat_aead_alg_callback;
  749. qat_req->req.comn_mid.opaque_data = (uint64_t)(__force long)qat_req;
  750. qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp;
  751. qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp;
  752. cipher_param = (void *)&qat_req->req.serv_specif_rqpars;
  753. cipher_param->cipher_length = areq->cryptlen - digst_size;
  754. cipher_param->cipher_offset = areq->assoclen;
  755. memcpy(cipher_param->u.cipher_IV_array, areq->iv, AES_BLOCK_SIZE);
  756. auth_param = (void *)((uint8_t *)cipher_param + sizeof(*cipher_param));
  757. auth_param->auth_off = 0;
  758. auth_param->auth_len = areq->assoclen + cipher_param->cipher_length;
  759. do {
  760. ret = adf_send_message(ctx->inst->sym_tx, (uint32_t *)msg);
  761. } while (ret == -EAGAIN && ctr++ < 10);
  762. if (ret == -EAGAIN) {
  763. qat_alg_free_bufl(ctx->inst, qat_req);
  764. return -EBUSY;
  765. }
  766. return -EINPROGRESS;
  767. }
  768. static int qat_alg_aead_enc(struct aead_request *areq)
  769. {
  770. struct crypto_aead *aead_tfm = crypto_aead_reqtfm(areq);
  771. struct crypto_tfm *tfm = crypto_aead_tfm(aead_tfm);
  772. struct qat_alg_aead_ctx *ctx = crypto_tfm_ctx(tfm);
  773. struct qat_crypto_request *qat_req = aead_request_ctx(areq);
  774. struct icp_qat_fw_la_cipher_req_params *cipher_param;
  775. struct icp_qat_fw_la_auth_req_params *auth_param;
  776. struct icp_qat_fw_la_bulk_req *msg;
  777. uint8_t *iv = areq->iv;
  778. int ret, ctr = 0;
  779. ret = qat_alg_sgl_to_bufl(ctx->inst, areq->src, areq->dst, qat_req);
  780. if (unlikely(ret))
  781. return ret;
  782. msg = &qat_req->req;
  783. *msg = ctx->enc_fw_req;
  784. qat_req->aead_ctx = ctx;
  785. qat_req->aead_req = areq;
  786. qat_req->cb = qat_aead_alg_callback;
  787. qat_req->req.comn_mid.opaque_data = (uint64_t)(__force long)qat_req;
  788. qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp;
  789. qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp;
  790. cipher_param = (void *)&qat_req->req.serv_specif_rqpars;
  791. auth_param = (void *)((uint8_t *)cipher_param + sizeof(*cipher_param));
  792. memcpy(cipher_param->u.cipher_IV_array, iv, AES_BLOCK_SIZE);
  793. cipher_param->cipher_length = areq->cryptlen;
  794. cipher_param->cipher_offset = areq->assoclen;
  795. auth_param->auth_off = 0;
  796. auth_param->auth_len = areq->assoclen + areq->cryptlen;
  797. do {
  798. ret = adf_send_message(ctx->inst->sym_tx, (uint32_t *)msg);
  799. } while (ret == -EAGAIN && ctr++ < 10);
  800. if (ret == -EAGAIN) {
  801. qat_alg_free_bufl(ctx->inst, qat_req);
  802. return -EBUSY;
  803. }
  804. return -EINPROGRESS;
  805. }
  806. static int qat_alg_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  807. const u8 *key, unsigned int keylen,
  808. int mode)
  809. {
  810. struct qat_alg_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  811. struct device *dev;
  812. spin_lock(&ctx->lock);
  813. if (ctx->enc_cd) {
  814. /* rekeying */
  815. dev = &GET_DEV(ctx->inst->accel_dev);
  816. memset(ctx->enc_cd, 0, sizeof(*ctx->enc_cd));
  817. memset(ctx->dec_cd, 0, sizeof(*ctx->dec_cd));
  818. memset(&ctx->enc_fw_req, 0, sizeof(ctx->enc_fw_req));
  819. memset(&ctx->dec_fw_req, 0, sizeof(ctx->dec_fw_req));
  820. } else {
  821. /* new key */
  822. int node = get_current_node();
  823. struct qat_crypto_instance *inst =
  824. qat_crypto_get_instance_node(node);
  825. if (!inst) {
  826. spin_unlock(&ctx->lock);
  827. return -EINVAL;
  828. }
  829. dev = &GET_DEV(inst->accel_dev);
  830. ctx->inst = inst;
  831. ctx->enc_cd = dma_zalloc_coherent(dev, sizeof(*ctx->enc_cd),
  832. &ctx->enc_cd_paddr,
  833. GFP_ATOMIC);
  834. if (!ctx->enc_cd) {
  835. spin_unlock(&ctx->lock);
  836. return -ENOMEM;
  837. }
  838. ctx->dec_cd = dma_zalloc_coherent(dev, sizeof(*ctx->dec_cd),
  839. &ctx->dec_cd_paddr,
  840. GFP_ATOMIC);
  841. if (!ctx->dec_cd) {
  842. spin_unlock(&ctx->lock);
  843. goto out_free_enc;
  844. }
  845. }
  846. spin_unlock(&ctx->lock);
  847. if (qat_alg_ablkcipher_init_sessions(ctx, key, keylen, mode))
  848. goto out_free_all;
  849. return 0;
  850. out_free_all:
  851. memset(ctx->dec_cd, 0, sizeof(*ctx->dec_cd));
  852. dma_free_coherent(dev, sizeof(*ctx->dec_cd),
  853. ctx->dec_cd, ctx->dec_cd_paddr);
  854. ctx->dec_cd = NULL;
  855. out_free_enc:
  856. memset(ctx->enc_cd, 0, sizeof(*ctx->enc_cd));
  857. dma_free_coherent(dev, sizeof(*ctx->enc_cd),
  858. ctx->enc_cd, ctx->enc_cd_paddr);
  859. ctx->enc_cd = NULL;
  860. return -ENOMEM;
  861. }
  862. static int qat_alg_ablkcipher_cbc_setkey(struct crypto_ablkcipher *tfm,
  863. const u8 *key, unsigned int keylen)
  864. {
  865. return qat_alg_ablkcipher_setkey(tfm, key, keylen,
  866. ICP_QAT_HW_CIPHER_CBC_MODE);
  867. }
  868. static int qat_alg_ablkcipher_ctr_setkey(struct crypto_ablkcipher *tfm,
  869. const u8 *key, unsigned int keylen)
  870. {
  871. return qat_alg_ablkcipher_setkey(tfm, key, keylen,
  872. ICP_QAT_HW_CIPHER_CTR_MODE);
  873. }
  874. static int qat_alg_ablkcipher_xts_setkey(struct crypto_ablkcipher *tfm,
  875. const u8 *key, unsigned int keylen)
  876. {
  877. return qat_alg_ablkcipher_setkey(tfm, key, keylen,
  878. ICP_QAT_HW_CIPHER_XTS_MODE);
  879. }
  880. static int qat_alg_ablkcipher_encrypt(struct ablkcipher_request *req)
  881. {
  882. struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
  883. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(atfm);
  884. struct qat_alg_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  885. struct qat_crypto_request *qat_req = ablkcipher_request_ctx(req);
  886. struct icp_qat_fw_la_cipher_req_params *cipher_param;
  887. struct icp_qat_fw_la_bulk_req *msg;
  888. int ret, ctr = 0;
  889. ret = qat_alg_sgl_to_bufl(ctx->inst, req->src, req->dst, qat_req);
  890. if (unlikely(ret))
  891. return ret;
  892. msg = &qat_req->req;
  893. *msg = ctx->enc_fw_req;
  894. qat_req->ablkcipher_ctx = ctx;
  895. qat_req->ablkcipher_req = req;
  896. qat_req->cb = qat_ablkcipher_alg_callback;
  897. qat_req->req.comn_mid.opaque_data = (uint64_t)(__force long)qat_req;
  898. qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp;
  899. qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp;
  900. cipher_param = (void *)&qat_req->req.serv_specif_rqpars;
  901. cipher_param->cipher_length = req->nbytes;
  902. cipher_param->cipher_offset = 0;
  903. memcpy(cipher_param->u.cipher_IV_array, req->info, AES_BLOCK_SIZE);
  904. do {
  905. ret = adf_send_message(ctx->inst->sym_tx, (uint32_t *)msg);
  906. } while (ret == -EAGAIN && ctr++ < 10);
  907. if (ret == -EAGAIN) {
  908. qat_alg_free_bufl(ctx->inst, qat_req);
  909. return -EBUSY;
  910. }
  911. return -EINPROGRESS;
  912. }
  913. static int qat_alg_ablkcipher_decrypt(struct ablkcipher_request *req)
  914. {
  915. struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
  916. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(atfm);
  917. struct qat_alg_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  918. struct qat_crypto_request *qat_req = ablkcipher_request_ctx(req);
  919. struct icp_qat_fw_la_cipher_req_params *cipher_param;
  920. struct icp_qat_fw_la_bulk_req *msg;
  921. int ret, ctr = 0;
  922. ret = qat_alg_sgl_to_bufl(ctx->inst, req->src, req->dst, qat_req);
  923. if (unlikely(ret))
  924. return ret;
  925. msg = &qat_req->req;
  926. *msg = ctx->dec_fw_req;
  927. qat_req->ablkcipher_ctx = ctx;
  928. qat_req->ablkcipher_req = req;
  929. qat_req->cb = qat_ablkcipher_alg_callback;
  930. qat_req->req.comn_mid.opaque_data = (uint64_t)(__force long)qat_req;
  931. qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp;
  932. qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp;
  933. cipher_param = (void *)&qat_req->req.serv_specif_rqpars;
  934. cipher_param->cipher_length = req->nbytes;
  935. cipher_param->cipher_offset = 0;
  936. memcpy(cipher_param->u.cipher_IV_array, req->info, AES_BLOCK_SIZE);
  937. do {
  938. ret = adf_send_message(ctx->inst->sym_tx, (uint32_t *)msg);
  939. } while (ret == -EAGAIN && ctr++ < 10);
  940. if (ret == -EAGAIN) {
  941. qat_alg_free_bufl(ctx->inst, qat_req);
  942. return -EBUSY;
  943. }
  944. return -EINPROGRESS;
  945. }
  946. static int qat_alg_aead_init(struct crypto_aead *tfm,
  947. enum icp_qat_hw_auth_algo hash,
  948. const char *hash_name)
  949. {
  950. struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm);
  951. ctx->hash_tfm = crypto_alloc_shash(hash_name, 0, 0);
  952. if (IS_ERR(ctx->hash_tfm))
  953. return PTR_ERR(ctx->hash_tfm);
  954. ctx->qat_hash_alg = hash;
  955. crypto_aead_set_reqsize(tfm, sizeof(struct qat_crypto_request));
  956. return 0;
  957. }
  958. static int qat_alg_aead_sha1_init(struct crypto_aead *tfm)
  959. {
  960. return qat_alg_aead_init(tfm, ICP_QAT_HW_AUTH_ALGO_SHA1, "sha1");
  961. }
  962. static int qat_alg_aead_sha256_init(struct crypto_aead *tfm)
  963. {
  964. return qat_alg_aead_init(tfm, ICP_QAT_HW_AUTH_ALGO_SHA256, "sha256");
  965. }
  966. static int qat_alg_aead_sha512_init(struct crypto_aead *tfm)
  967. {
  968. return qat_alg_aead_init(tfm, ICP_QAT_HW_AUTH_ALGO_SHA512, "sha512");
  969. }
  970. static void qat_alg_aead_exit(struct crypto_aead *tfm)
  971. {
  972. struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm);
  973. struct qat_crypto_instance *inst = ctx->inst;
  974. struct device *dev;
  975. crypto_free_shash(ctx->hash_tfm);
  976. if (!inst)
  977. return;
  978. dev = &GET_DEV(inst->accel_dev);
  979. if (ctx->enc_cd) {
  980. memset(ctx->enc_cd, 0, sizeof(struct qat_alg_cd));
  981. dma_free_coherent(dev, sizeof(struct qat_alg_cd),
  982. ctx->enc_cd, ctx->enc_cd_paddr);
  983. }
  984. if (ctx->dec_cd) {
  985. memset(ctx->dec_cd, 0, sizeof(struct qat_alg_cd));
  986. dma_free_coherent(dev, sizeof(struct qat_alg_cd),
  987. ctx->dec_cd, ctx->dec_cd_paddr);
  988. }
  989. qat_crypto_put_instance(inst);
  990. }
  991. static int qat_alg_ablkcipher_init(struct crypto_tfm *tfm)
  992. {
  993. struct qat_alg_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  994. spin_lock_init(&ctx->lock);
  995. tfm->crt_ablkcipher.reqsize = sizeof(struct qat_crypto_request);
  996. ctx->tfm = tfm;
  997. return 0;
  998. }
  999. static void qat_alg_ablkcipher_exit(struct crypto_tfm *tfm)
  1000. {
  1001. struct qat_alg_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
  1002. struct qat_crypto_instance *inst = ctx->inst;
  1003. struct device *dev;
  1004. if (!inst)
  1005. return;
  1006. dev = &GET_DEV(inst->accel_dev);
  1007. if (ctx->enc_cd) {
  1008. memset(ctx->enc_cd, 0,
  1009. sizeof(struct icp_qat_hw_cipher_algo_blk));
  1010. dma_free_coherent(dev,
  1011. sizeof(struct icp_qat_hw_cipher_algo_blk),
  1012. ctx->enc_cd, ctx->enc_cd_paddr);
  1013. }
  1014. if (ctx->dec_cd) {
  1015. memset(ctx->dec_cd, 0,
  1016. sizeof(struct icp_qat_hw_cipher_algo_blk));
  1017. dma_free_coherent(dev,
  1018. sizeof(struct icp_qat_hw_cipher_algo_blk),
  1019. ctx->dec_cd, ctx->dec_cd_paddr);
  1020. }
  1021. qat_crypto_put_instance(inst);
  1022. }
  1023. static struct aead_alg qat_aeads[] = { {
  1024. .base = {
  1025. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1026. .cra_driver_name = "qat_aes_cbc_hmac_sha1",
  1027. .cra_priority = 4001,
  1028. .cra_flags = CRYPTO_ALG_ASYNC,
  1029. .cra_blocksize = AES_BLOCK_SIZE,
  1030. .cra_ctxsize = sizeof(struct qat_alg_aead_ctx),
  1031. .cra_module = THIS_MODULE,
  1032. },
  1033. .init = qat_alg_aead_sha1_init,
  1034. .exit = qat_alg_aead_exit,
  1035. .setkey = qat_alg_aead_setkey,
  1036. .decrypt = qat_alg_aead_dec,
  1037. .encrypt = qat_alg_aead_enc,
  1038. .ivsize = AES_BLOCK_SIZE,
  1039. .maxauthsize = SHA1_DIGEST_SIZE,
  1040. }, {
  1041. .base = {
  1042. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1043. .cra_driver_name = "qat_aes_cbc_hmac_sha256",
  1044. .cra_priority = 4001,
  1045. .cra_flags = CRYPTO_ALG_ASYNC,
  1046. .cra_blocksize = AES_BLOCK_SIZE,
  1047. .cra_ctxsize = sizeof(struct qat_alg_aead_ctx),
  1048. .cra_module = THIS_MODULE,
  1049. },
  1050. .init = qat_alg_aead_sha256_init,
  1051. .exit = qat_alg_aead_exit,
  1052. .setkey = qat_alg_aead_setkey,
  1053. .decrypt = qat_alg_aead_dec,
  1054. .encrypt = qat_alg_aead_enc,
  1055. .ivsize = AES_BLOCK_SIZE,
  1056. .maxauthsize = SHA256_DIGEST_SIZE,
  1057. }, {
  1058. .base = {
  1059. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  1060. .cra_driver_name = "qat_aes_cbc_hmac_sha512",
  1061. .cra_priority = 4001,
  1062. .cra_flags = CRYPTO_ALG_ASYNC,
  1063. .cra_blocksize = AES_BLOCK_SIZE,
  1064. .cra_ctxsize = sizeof(struct qat_alg_aead_ctx),
  1065. .cra_module = THIS_MODULE,
  1066. },
  1067. .init = qat_alg_aead_sha512_init,
  1068. .exit = qat_alg_aead_exit,
  1069. .setkey = qat_alg_aead_setkey,
  1070. .decrypt = qat_alg_aead_dec,
  1071. .encrypt = qat_alg_aead_enc,
  1072. .ivsize = AES_BLOCK_SIZE,
  1073. .maxauthsize = SHA512_DIGEST_SIZE,
  1074. } };
  1075. static struct crypto_alg qat_algs[] = { {
  1076. .cra_name = "cbc(aes)",
  1077. .cra_driver_name = "qat_aes_cbc",
  1078. .cra_priority = 4001,
  1079. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1080. .cra_blocksize = AES_BLOCK_SIZE,
  1081. .cra_ctxsize = sizeof(struct qat_alg_ablkcipher_ctx),
  1082. .cra_alignmask = 0,
  1083. .cra_type = &crypto_ablkcipher_type,
  1084. .cra_module = THIS_MODULE,
  1085. .cra_init = qat_alg_ablkcipher_init,
  1086. .cra_exit = qat_alg_ablkcipher_exit,
  1087. .cra_u = {
  1088. .ablkcipher = {
  1089. .setkey = qat_alg_ablkcipher_cbc_setkey,
  1090. .decrypt = qat_alg_ablkcipher_decrypt,
  1091. .encrypt = qat_alg_ablkcipher_encrypt,
  1092. .min_keysize = AES_MIN_KEY_SIZE,
  1093. .max_keysize = AES_MAX_KEY_SIZE,
  1094. .ivsize = AES_BLOCK_SIZE,
  1095. },
  1096. },
  1097. }, {
  1098. .cra_name = "ctr(aes)",
  1099. .cra_driver_name = "qat_aes_ctr",
  1100. .cra_priority = 4001,
  1101. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1102. .cra_blocksize = AES_BLOCK_SIZE,
  1103. .cra_ctxsize = sizeof(struct qat_alg_ablkcipher_ctx),
  1104. .cra_alignmask = 0,
  1105. .cra_type = &crypto_ablkcipher_type,
  1106. .cra_module = THIS_MODULE,
  1107. .cra_init = qat_alg_ablkcipher_init,
  1108. .cra_exit = qat_alg_ablkcipher_exit,
  1109. .cra_u = {
  1110. .ablkcipher = {
  1111. .setkey = qat_alg_ablkcipher_ctr_setkey,
  1112. .decrypt = qat_alg_ablkcipher_decrypt,
  1113. .encrypt = qat_alg_ablkcipher_encrypt,
  1114. .min_keysize = AES_MIN_KEY_SIZE,
  1115. .max_keysize = AES_MAX_KEY_SIZE,
  1116. .ivsize = AES_BLOCK_SIZE,
  1117. },
  1118. },
  1119. }, {
  1120. .cra_name = "xts(aes)",
  1121. .cra_driver_name = "qat_aes_xts",
  1122. .cra_priority = 4001,
  1123. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1124. .cra_blocksize = AES_BLOCK_SIZE,
  1125. .cra_ctxsize = sizeof(struct qat_alg_ablkcipher_ctx),
  1126. .cra_alignmask = 0,
  1127. .cra_type = &crypto_ablkcipher_type,
  1128. .cra_module = THIS_MODULE,
  1129. .cra_init = qat_alg_ablkcipher_init,
  1130. .cra_exit = qat_alg_ablkcipher_exit,
  1131. .cra_u = {
  1132. .ablkcipher = {
  1133. .setkey = qat_alg_ablkcipher_xts_setkey,
  1134. .decrypt = qat_alg_ablkcipher_decrypt,
  1135. .encrypt = qat_alg_ablkcipher_encrypt,
  1136. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1137. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1138. .ivsize = AES_BLOCK_SIZE,
  1139. },
  1140. },
  1141. } };
  1142. int qat_algs_register(void)
  1143. {
  1144. int ret = 0, i;
  1145. mutex_lock(&algs_lock);
  1146. if (++active_devs != 1)
  1147. goto unlock;
  1148. for (i = 0; i < ARRAY_SIZE(qat_algs); i++)
  1149. qat_algs[i].cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
  1150. ret = crypto_register_algs(qat_algs, ARRAY_SIZE(qat_algs));
  1151. if (ret)
  1152. goto unlock;
  1153. for (i = 0; i < ARRAY_SIZE(qat_aeads); i++)
  1154. qat_aeads[i].base.cra_flags = CRYPTO_ALG_ASYNC;
  1155. ret = crypto_register_aeads(qat_aeads, ARRAY_SIZE(qat_aeads));
  1156. if (ret)
  1157. goto unreg_algs;
  1158. unlock:
  1159. mutex_unlock(&algs_lock);
  1160. return ret;
  1161. unreg_algs:
  1162. crypto_unregister_algs(qat_algs, ARRAY_SIZE(qat_algs));
  1163. goto unlock;
  1164. }
  1165. void qat_algs_unregister(void)
  1166. {
  1167. mutex_lock(&algs_lock);
  1168. if (--active_devs != 0)
  1169. goto unlock;
  1170. crypto_unregister_aeads(qat_aeads, ARRAY_SIZE(qat_aeads));
  1171. crypto_unregister_algs(qat_algs, ARRAY_SIZE(qat_algs));
  1172. unlock:
  1173. mutex_unlock(&algs_lock);
  1174. }