crypto4xx_alg.c 21 KB

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