crypto4xx_alg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AMCC SoC PPC4xx Crypto Driver
  4. *
  5. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  6. * All rights reserved. James Hsiao <[email protected]>
  7. *
  8. * This file implements the Linux crypto algorithms.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/spinlock_types.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/crypto.h>
  15. #include <linux/hash.h>
  16. #include <crypto/internal/hash.h>
  17. #include <linux/dma-mapping.h>
  18. #include <crypto/algapi.h>
  19. #include <crypto/aead.h>
  20. #include <crypto/aes.h>
  21. #include <crypto/gcm.h>
  22. #include <crypto/sha1.h>
  23. #include <crypto/ctr.h>
  24. #include <crypto/skcipher.h>
  25. #include "crypto4xx_reg_def.h"
  26. #include "crypto4xx_core.h"
  27. #include "crypto4xx_sa.h"
  28. static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
  29. u32 save_iv, u32 ld_h, u32 ld_iv,
  30. u32 hdr_proc, u32 h, u32 c, u32 pad_type,
  31. u32 op_grp, u32 op, u32 dir)
  32. {
  33. sa->sa_command_0.w = 0;
  34. sa->sa_command_0.bf.save_hash_state = save_h;
  35. sa->sa_command_0.bf.save_iv = save_iv;
  36. sa->sa_command_0.bf.load_hash_state = ld_h;
  37. sa->sa_command_0.bf.load_iv = ld_iv;
  38. sa->sa_command_0.bf.hdr_proc = hdr_proc;
  39. sa->sa_command_0.bf.hash_alg = h;
  40. sa->sa_command_0.bf.cipher_alg = c;
  41. sa->sa_command_0.bf.pad_type = pad_type & 3;
  42. sa->sa_command_0.bf.extend_pad = pad_type >> 2;
  43. sa->sa_command_0.bf.op_group = op_grp;
  44. sa->sa_command_0.bf.opcode = op;
  45. sa->sa_command_0.bf.dir = dir;
  46. }
  47. static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
  48. u32 hmac_mc, u32 cfb, u32 esn,
  49. u32 sn_mask, u32 mute, u32 cp_pad,
  50. u32 cp_pay, u32 cp_hdr)
  51. {
  52. sa->sa_command_1.w = 0;
  53. sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
  54. sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
  55. sa->sa_command_1.bf.feedback_mode = cfb;
  56. sa->sa_command_1.bf.sa_rev = 1;
  57. sa->sa_command_1.bf.hmac_muting = hmac_mc;
  58. sa->sa_command_1.bf.extended_seq_num = esn;
  59. sa->sa_command_1.bf.seq_num_mask = sn_mask;
  60. sa->sa_command_1.bf.mutable_bit_proc = mute;
  61. sa->sa_command_1.bf.copy_pad = cp_pad;
  62. sa->sa_command_1.bf.copy_payload = cp_pay;
  63. sa->sa_command_1.bf.copy_hdr = cp_hdr;
  64. }
  65. static inline int crypto4xx_crypt(struct skcipher_request *req,
  66. const unsigned int ivlen, bool decrypt,
  67. bool check_blocksize)
  68. {
  69. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  70. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  71. __le32 iv[AES_IV_SIZE];
  72. if (check_blocksize && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE))
  73. return -EINVAL;
  74. if (ivlen)
  75. crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
  76. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  77. req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
  78. ctx->sa_len, 0, NULL);
  79. }
  80. int crypto4xx_encrypt_noiv_block(struct skcipher_request *req)
  81. {
  82. return crypto4xx_crypt(req, 0, false, true);
  83. }
  84. int crypto4xx_encrypt_iv_stream(struct skcipher_request *req)
  85. {
  86. return crypto4xx_crypt(req, AES_IV_SIZE, false, false);
  87. }
  88. int crypto4xx_decrypt_noiv_block(struct skcipher_request *req)
  89. {
  90. return crypto4xx_crypt(req, 0, true, true);
  91. }
  92. int crypto4xx_decrypt_iv_stream(struct skcipher_request *req)
  93. {
  94. return crypto4xx_crypt(req, AES_IV_SIZE, true, false);
  95. }
  96. int crypto4xx_encrypt_iv_block(struct skcipher_request *req)
  97. {
  98. return crypto4xx_crypt(req, AES_IV_SIZE, false, true);
  99. }
  100. int crypto4xx_decrypt_iv_block(struct skcipher_request *req)
  101. {
  102. return crypto4xx_crypt(req, AES_IV_SIZE, true, true);
  103. }
  104. /*
  105. * AES Functions
  106. */
  107. static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
  108. const u8 *key,
  109. unsigned int keylen,
  110. unsigned char cm,
  111. u8 fb)
  112. {
  113. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  114. struct dynamic_sa_ctl *sa;
  115. int rc;
  116. if (keylen != AES_KEYSIZE_256 && keylen != AES_KEYSIZE_192 &&
  117. keylen != AES_KEYSIZE_128)
  118. return -EINVAL;
  119. /* Create SA */
  120. if (ctx->sa_in || ctx->sa_out)
  121. crypto4xx_free_sa(ctx);
  122. rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
  123. if (rc)
  124. return rc;
  125. /* Setup SA */
  126. sa = ctx->sa_in;
  127. set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
  128. SA_NOT_SAVE_IV : SA_SAVE_IV),
  129. SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
  130. SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
  131. SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
  132. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  133. SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
  134. DIR_INBOUND);
  135. set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
  136. fb, SA_EXTENDED_SN_OFF,
  137. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  138. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  139. SA_NOT_COPY_HDR);
  140. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  141. key, keylen);
  142. sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
  143. sa->sa_command_1.bf.key_len = keylen >> 3;
  144. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  145. sa = ctx->sa_out;
  146. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  147. /*
  148. * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
  149. * it's the DIR_(IN|OUT)BOUND that matters
  150. */
  151. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
  152. return 0;
  153. }
  154. int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
  155. const u8 *key, unsigned int keylen)
  156. {
  157. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
  158. CRYPTO_FEEDBACK_MODE_NO_FB);
  159. }
  160. int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
  161. const u8 *key, unsigned int keylen)
  162. {
  163. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
  164. CRYPTO_FEEDBACK_MODE_128BIT_CFB);
  165. }
  166. int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
  167. const u8 *key, unsigned int keylen)
  168. {
  169. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
  170. CRYPTO_FEEDBACK_MODE_NO_FB);
  171. }
  172. int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
  173. const u8 *key, unsigned int keylen)
  174. {
  175. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
  176. CRYPTO_FEEDBACK_MODE_64BIT_OFB);
  177. }
  178. int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
  179. const u8 *key, unsigned int keylen)
  180. {
  181. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  182. int rc;
  183. rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
  184. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  185. if (rc)
  186. return rc;
  187. ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
  188. CTR_RFC3686_NONCE_SIZE]);
  189. return 0;
  190. }
  191. int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
  192. {
  193. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  194. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  195. __le32 iv[AES_IV_SIZE / 4] = {
  196. ctx->iv_nonce,
  197. cpu_to_le32p((u32 *) req->iv),
  198. cpu_to_le32p((u32 *) (req->iv + 4)),
  199. cpu_to_le32(1) };
  200. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  201. req->cryptlen, iv, AES_IV_SIZE,
  202. ctx->sa_out, ctx->sa_len, 0, NULL);
  203. }
  204. int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
  205. {
  206. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  207. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  208. __le32 iv[AES_IV_SIZE / 4] = {
  209. ctx->iv_nonce,
  210. cpu_to_le32p((u32 *) req->iv),
  211. cpu_to_le32p((u32 *) (req->iv + 4)),
  212. cpu_to_le32(1) };
  213. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  214. req->cryptlen, iv, AES_IV_SIZE,
  215. ctx->sa_out, ctx->sa_len, 0, NULL);
  216. }
  217. static int
  218. crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
  219. {
  220. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  221. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  222. size_t iv_len = crypto_skcipher_ivsize(cipher);
  223. unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
  224. unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
  225. AES_BLOCK_SIZE;
  226. /*
  227. * The hardware uses only the last 32-bits as the counter while the
  228. * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
  229. * the whole IV is a counter. So fallback if the counter is going to
  230. * overlow.
  231. */
  232. if (counter + nblks < counter) {
  233. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
  234. int ret;
  235. skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
  236. skcipher_request_set_callback(subreq, req->base.flags,
  237. NULL, NULL);
  238. skcipher_request_set_crypt(subreq, req->src, req->dst,
  239. req->cryptlen, req->iv);
  240. ret = encrypt ? crypto_skcipher_encrypt(subreq)
  241. : crypto_skcipher_decrypt(subreq);
  242. skcipher_request_zero(subreq);
  243. return ret;
  244. }
  245. return encrypt ? crypto4xx_encrypt_iv_stream(req)
  246. : crypto4xx_decrypt_iv_stream(req);
  247. }
  248. static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
  249. struct crypto_skcipher *cipher,
  250. const u8 *key,
  251. unsigned int keylen)
  252. {
  253. crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
  254. CRYPTO_TFM_REQ_MASK);
  255. crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
  256. crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  257. return crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
  258. }
  259. int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
  260. const u8 *key, unsigned int keylen)
  261. {
  262. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  263. int rc;
  264. rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
  265. if (rc)
  266. return rc;
  267. return crypto4xx_setkey_aes(cipher, key, keylen,
  268. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  269. }
  270. int crypto4xx_encrypt_ctr(struct skcipher_request *req)
  271. {
  272. return crypto4xx_ctr_crypt(req, true);
  273. }
  274. int crypto4xx_decrypt_ctr(struct skcipher_request *req)
  275. {
  276. return crypto4xx_ctr_crypt(req, false);
  277. }
  278. static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
  279. unsigned int len,
  280. bool is_ccm, bool decrypt)
  281. {
  282. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  283. /* authsize has to be a multiple of 4 */
  284. if (aead->authsize & 3)
  285. return true;
  286. /*
  287. * hardware does not handle cases where plaintext
  288. * is less than a block.
  289. */
  290. if (len < AES_BLOCK_SIZE)
  291. return true;
  292. /* assoc len needs to be a multiple of 4 and <= 1020 */
  293. if (req->assoclen & 0x3 || req->assoclen > 1020)
  294. return true;
  295. /* CCM supports only counter field length of 2 and 4 bytes */
  296. if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
  297. return true;
  298. return false;
  299. }
  300. static int crypto4xx_aead_fallback(struct aead_request *req,
  301. struct crypto4xx_ctx *ctx, bool do_decrypt)
  302. {
  303. struct aead_request *subreq = aead_request_ctx(req);
  304. aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
  305. aead_request_set_callback(subreq, req->base.flags,
  306. req->base.complete, req->base.data);
  307. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  308. req->iv);
  309. aead_request_set_ad(subreq, req->assoclen);
  310. return do_decrypt ? crypto_aead_decrypt(subreq) :
  311. crypto_aead_encrypt(subreq);
  312. }
  313. static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
  314. struct crypto_aead *cipher,
  315. const u8 *key,
  316. unsigned int keylen)
  317. {
  318. crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
  319. crypto_aead_set_flags(ctx->sw_cipher.aead,
  320. crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  321. return crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
  322. }
  323. /*
  324. * AES-CCM Functions
  325. */
  326. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
  327. unsigned int keylen)
  328. {
  329. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  330. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  331. struct dynamic_sa_ctl *sa;
  332. int rc = 0;
  333. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  334. if (rc)
  335. return rc;
  336. if (ctx->sa_in || ctx->sa_out)
  337. crypto4xx_free_sa(ctx);
  338. rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
  339. if (rc)
  340. return rc;
  341. /* Setup SA */
  342. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  343. sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
  344. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  345. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  346. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  347. SA_CIPHER_ALG_AES,
  348. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  349. SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
  350. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  351. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  352. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  353. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  354. SA_NOT_COPY_HDR);
  355. sa->sa_command_1.bf.key_len = keylen >> 3;
  356. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
  357. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  358. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  359. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  360. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  361. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  362. SA_CIPHER_ALG_AES,
  363. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  364. SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
  365. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  366. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  367. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  368. SA_COPY_PAD, SA_COPY_PAYLOAD,
  369. SA_NOT_COPY_HDR);
  370. sa->sa_command_1.bf.key_len = keylen >> 3;
  371. return 0;
  372. }
  373. static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
  374. {
  375. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  376. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  377. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  378. __le32 iv[16];
  379. u32 tmp_sa[SA_AES128_CCM_LEN + 4];
  380. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
  381. unsigned int len = req->cryptlen;
  382. if (decrypt)
  383. len -= crypto_aead_authsize(aead);
  384. if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
  385. return crypto4xx_aead_fallback(req, ctx, decrypt);
  386. memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
  387. sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
  388. if (req->iv[0] == 1) {
  389. /* CRYPTO_MODE_AES_ICM */
  390. sa->sa_command_1.bf.crypto_mode9_8 = 1;
  391. }
  392. iv[3] = cpu_to_le32(0);
  393. crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
  394. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  395. len, iv, sizeof(iv),
  396. sa, ctx->sa_len, req->assoclen, rctx->dst);
  397. }
  398. int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
  399. {
  400. return crypto4xx_crypt_aes_ccm(req, false);
  401. }
  402. int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
  403. {
  404. return crypto4xx_crypt_aes_ccm(req, true);
  405. }
  406. int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
  407. unsigned int authsize)
  408. {
  409. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  410. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  411. return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
  412. }
  413. /*
  414. * AES-GCM Functions
  415. */
  416. static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
  417. {
  418. switch (keylen) {
  419. case 16:
  420. case 24:
  421. case 32:
  422. return 0;
  423. default:
  424. return -EINVAL;
  425. }
  426. }
  427. static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
  428. unsigned int keylen)
  429. {
  430. struct crypto_aes_ctx ctx;
  431. uint8_t src[16] = { 0 };
  432. int rc;
  433. rc = aes_expandkey(&ctx, key, keylen);
  434. if (rc) {
  435. pr_err("aes_expandkey() failed: %d\n", rc);
  436. return rc;
  437. }
  438. aes_encrypt(&ctx, src, src);
  439. crypto4xx_memcpy_to_le32(hash_start, src, 16);
  440. memzero_explicit(&ctx, sizeof(ctx));
  441. return 0;
  442. }
  443. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  444. const u8 *key, unsigned int keylen)
  445. {
  446. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  447. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  448. struct dynamic_sa_ctl *sa;
  449. int rc = 0;
  450. if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0)
  451. return -EINVAL;
  452. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  453. if (rc)
  454. return rc;
  455. if (ctx->sa_in || ctx->sa_out)
  456. crypto4xx_free_sa(ctx);
  457. rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
  458. if (rc)
  459. return rc;
  460. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  461. sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
  462. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  463. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  464. SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
  465. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  466. SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
  467. DIR_INBOUND);
  468. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  469. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  470. SA_SEQ_MASK_ON, SA_MC_DISABLE,
  471. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  472. SA_NOT_COPY_HDR);
  473. sa->sa_command_1.bf.key_len = keylen >> 3;
  474. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  475. key, keylen);
  476. rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
  477. key, keylen);
  478. if (rc) {
  479. pr_err("GCM hash key setting failed = %d\n", rc);
  480. goto err;
  481. }
  482. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  483. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  484. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  485. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
  486. return 0;
  487. err:
  488. crypto4xx_free_sa(ctx);
  489. return rc;
  490. }
  491. static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
  492. bool decrypt)
  493. {
  494. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  495. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  496. __le32 iv[4];
  497. unsigned int len = req->cryptlen;
  498. if (decrypt)
  499. len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
  500. if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
  501. return crypto4xx_aead_fallback(req, ctx, decrypt);
  502. crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
  503. iv[3] = cpu_to_le32(1);
  504. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  505. len, iv, sizeof(iv),
  506. decrypt ? ctx->sa_in : ctx->sa_out,
  507. ctx->sa_len, req->assoclen, rctx->dst);
  508. }
  509. int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
  510. {
  511. return crypto4xx_crypt_aes_gcm(req, false);
  512. }
  513. int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
  514. {
  515. return crypto4xx_crypt_aes_gcm(req, true);
  516. }
  517. /*
  518. * HASH SHA1 Functions
  519. */
  520. static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
  521. unsigned int sa_len,
  522. unsigned char ha,
  523. unsigned char hm)
  524. {
  525. struct crypto_alg *alg = tfm->__crt_alg;
  526. struct crypto4xx_alg *my_alg;
  527. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  528. struct dynamic_sa_hash160 *sa;
  529. int rc;
  530. my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
  531. alg.u.hash);
  532. ctx->dev = my_alg->dev;
  533. /* Create SA */
  534. if (ctx->sa_in || ctx->sa_out)
  535. crypto4xx_free_sa(ctx);
  536. rc = crypto4xx_alloc_sa(ctx, sa_len);
  537. if (rc)
  538. return rc;
  539. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  540. sizeof(struct crypto4xx_ctx));
  541. sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
  542. set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  543. SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
  544. SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
  545. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  546. SA_OPCODE_HASH, DIR_INBOUND);
  547. set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
  548. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  549. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  550. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  551. SA_NOT_COPY_HDR);
  552. /* Need to zero hash digest in SA */
  553. memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
  554. memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
  555. return 0;
  556. }
  557. int crypto4xx_hash_init(struct ahash_request *req)
  558. {
  559. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  560. int ds;
  561. struct dynamic_sa_ctl *sa;
  562. sa = ctx->sa_in;
  563. ds = crypto_ahash_digestsize(
  564. __crypto_ahash_cast(req->base.tfm));
  565. sa->sa_command_0.bf.digest_len = ds >> 2;
  566. sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
  567. return 0;
  568. }
  569. int crypto4xx_hash_update(struct ahash_request *req)
  570. {
  571. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  572. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  573. struct scatterlist dst;
  574. unsigned int ds = crypto_ahash_digestsize(ahash);
  575. sg_init_one(&dst, req->result, ds);
  576. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  577. req->nbytes, NULL, 0, ctx->sa_in,
  578. ctx->sa_len, 0, NULL);
  579. }
  580. int crypto4xx_hash_final(struct ahash_request *req)
  581. {
  582. return 0;
  583. }
  584. int crypto4xx_hash_digest(struct ahash_request *req)
  585. {
  586. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  587. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  588. struct scatterlist dst;
  589. unsigned int ds = crypto_ahash_digestsize(ahash);
  590. sg_init_one(&dst, req->result, ds);
  591. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  592. req->nbytes, NULL, 0, ctx->sa_in,
  593. ctx->sa_len, 0, NULL);
  594. }
  595. /*
  596. * SHA1 Algorithm
  597. */
  598. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
  599. {
  600. return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
  601. SA_HASH_MODE_HASH);
  602. }