crypto4xx_alg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. {
  77. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  78. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  79. __le32 iv[AES_IV_SIZE];
  80. if (ivlen)
  81. crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
  82. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  83. req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
  84. ctx->sa_len, 0, NULL);
  85. }
  86. int crypto4xx_encrypt_noiv(struct skcipher_request *req)
  87. {
  88. return crypto4xx_crypt(req, 0, false);
  89. }
  90. int crypto4xx_encrypt_iv(struct skcipher_request *req)
  91. {
  92. return crypto4xx_crypt(req, AES_IV_SIZE, false);
  93. }
  94. int crypto4xx_decrypt_noiv(struct skcipher_request *req)
  95. {
  96. return crypto4xx_crypt(req, 0, true);
  97. }
  98. int crypto4xx_decrypt_iv(struct skcipher_request *req)
  99. {
  100. return crypto4xx_crypt(req, AES_IV_SIZE, true);
  101. }
  102. /**
  103. * AES Functions
  104. */
  105. static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
  106. const u8 *key,
  107. unsigned int keylen,
  108. unsigned char cm,
  109. u8 fb)
  110. {
  111. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  112. struct dynamic_sa_ctl *sa;
  113. int rc;
  114. if (keylen != AES_KEYSIZE_256 &&
  115. keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
  116. crypto_skcipher_set_flags(cipher,
  117. CRYPTO_TFM_RES_BAD_KEY_LEN);
  118. return -EINVAL;
  119. }
  120. /* Create SA */
  121. if (ctx->sa_in || ctx->sa_out)
  122. crypto4xx_free_sa(ctx);
  123. rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
  124. if (rc)
  125. return rc;
  126. /* Setup SA */
  127. sa = ctx->sa_in;
  128. set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_CBC ?
  129. SA_SAVE_IV : SA_NOT_SAVE_IV),
  130. SA_LOAD_HASH_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. return 0;
  148. }
  149. int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
  150. const u8 *key, unsigned int keylen)
  151. {
  152. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
  153. CRYPTO_FEEDBACK_MODE_NO_FB);
  154. }
  155. int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
  156. const u8 *key, unsigned int keylen)
  157. {
  158. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
  159. CRYPTO_FEEDBACK_MODE_128BIT_CFB);
  160. }
  161. int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
  162. const u8 *key, unsigned int keylen)
  163. {
  164. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
  165. CRYPTO_FEEDBACK_MODE_NO_FB);
  166. }
  167. int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
  168. const u8 *key, unsigned int keylen)
  169. {
  170. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
  171. CRYPTO_FEEDBACK_MODE_64BIT_OFB);
  172. }
  173. int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
  174. const u8 *key, unsigned int keylen)
  175. {
  176. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  177. int rc;
  178. rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
  179. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  180. if (rc)
  181. return rc;
  182. ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
  183. CTR_RFC3686_NONCE_SIZE]);
  184. return 0;
  185. }
  186. int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
  187. {
  188. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  189. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  190. __le32 iv[AES_IV_SIZE / 4] = {
  191. ctx->iv_nonce,
  192. cpu_to_le32p((u32 *) req->iv),
  193. cpu_to_le32p((u32 *) (req->iv + 4)),
  194. cpu_to_le32(1) };
  195. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  196. req->cryptlen, iv, AES_IV_SIZE,
  197. ctx->sa_out, ctx->sa_len, 0, NULL);
  198. }
  199. int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
  200. {
  201. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  202. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  203. __le32 iv[AES_IV_SIZE / 4] = {
  204. ctx->iv_nonce,
  205. cpu_to_le32p((u32 *) req->iv),
  206. cpu_to_le32p((u32 *) (req->iv + 4)),
  207. cpu_to_le32(1) };
  208. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  209. req->cryptlen, iv, AES_IV_SIZE,
  210. ctx->sa_out, ctx->sa_len, 0, NULL);
  211. }
  212. static int
  213. crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
  214. {
  215. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  216. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  217. size_t iv_len = crypto_skcipher_ivsize(cipher);
  218. unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
  219. unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
  220. AES_BLOCK_SIZE;
  221. /*
  222. * The hardware uses only the last 32-bits as the counter while the
  223. * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
  224. * the whole IV is a counter. So fallback if the counter is going to
  225. * overlow.
  226. */
  227. if (counter + nblks < counter) {
  228. struct skcipher_request *subreq = skcipher_request_ctx(req);
  229. int ret;
  230. skcipher_request_set_tfm(subreq, ctx->sw_cipher.cipher);
  231. skcipher_request_set_callback(subreq, req->base.flags,
  232. NULL, NULL);
  233. skcipher_request_set_crypt(subreq, req->src, req->dst,
  234. req->cryptlen, req->iv);
  235. ret = encrypt ? crypto_skcipher_encrypt(subreq)
  236. : crypto_skcipher_decrypt(subreq);
  237. skcipher_request_zero(subreq);
  238. return ret;
  239. }
  240. return encrypt ? crypto4xx_encrypt_iv(req)
  241. : crypto4xx_decrypt_iv(req);
  242. }
  243. static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
  244. struct crypto_skcipher *cipher,
  245. const u8 *key,
  246. unsigned int keylen)
  247. {
  248. int rc;
  249. crypto_skcipher_clear_flags(ctx->sw_cipher.cipher,
  250. CRYPTO_TFM_REQ_MASK);
  251. crypto_skcipher_set_flags(ctx->sw_cipher.cipher,
  252. crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  253. rc = crypto_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
  254. crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
  255. crypto_skcipher_set_flags(cipher,
  256. crypto_skcipher_get_flags(ctx->sw_cipher.cipher) &
  257. CRYPTO_TFM_RES_MASK);
  258. return rc;
  259. }
  260. int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
  261. const u8 *key, unsigned int keylen)
  262. {
  263. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  264. int rc;
  265. rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
  266. if (rc)
  267. return rc;
  268. return crypto4xx_setkey_aes(cipher, key, keylen,
  269. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  270. }
  271. int crypto4xx_encrypt_ctr(struct skcipher_request *req)
  272. {
  273. return crypto4xx_ctr_crypt(req, true);
  274. }
  275. int crypto4xx_decrypt_ctr(struct skcipher_request *req)
  276. {
  277. return crypto4xx_ctr_crypt(req, false);
  278. }
  279. static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
  280. unsigned int len,
  281. bool is_ccm, bool decrypt)
  282. {
  283. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  284. /* authsize has to be a multiple of 4 */
  285. if (aead->authsize & 3)
  286. return true;
  287. /*
  288. * hardware does not handle cases where plaintext
  289. * is less than a block.
  290. */
  291. if (len < AES_BLOCK_SIZE)
  292. return true;
  293. /* assoc len needs to be a multiple of 4 and <= 1020 */
  294. if (req->assoclen & 0x3 || req->assoclen > 1020)
  295. return true;
  296. /* CCM supports only counter field length of 2 and 4 bytes */
  297. if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
  298. return true;
  299. return false;
  300. }
  301. static int crypto4xx_aead_fallback(struct aead_request *req,
  302. struct crypto4xx_ctx *ctx, bool do_decrypt)
  303. {
  304. struct aead_request *subreq = aead_request_ctx(req);
  305. aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
  306. aead_request_set_callback(subreq, req->base.flags,
  307. req->base.complete, req->base.data);
  308. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  309. req->iv);
  310. aead_request_set_ad(subreq, req->assoclen);
  311. return do_decrypt ? crypto_aead_decrypt(subreq) :
  312. crypto_aead_encrypt(subreq);
  313. }
  314. static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
  315. struct crypto_aead *cipher,
  316. const u8 *key,
  317. unsigned int keylen)
  318. {
  319. int rc;
  320. crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
  321. crypto_aead_set_flags(ctx->sw_cipher.aead,
  322. crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  323. rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
  324. crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
  325. crypto_aead_set_flags(cipher,
  326. crypto_aead_get_flags(ctx->sw_cipher.aead) &
  327. CRYPTO_TFM_RES_MASK);
  328. return rc;
  329. }
  330. /**
  331. * AES-CCM Functions
  332. */
  333. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
  334. unsigned int keylen)
  335. {
  336. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  337. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  338. struct dynamic_sa_ctl *sa;
  339. int rc = 0;
  340. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  341. if (rc)
  342. return rc;
  343. if (ctx->sa_in || ctx->sa_out)
  344. crypto4xx_free_sa(ctx);
  345. rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
  346. if (rc)
  347. return rc;
  348. /* Setup SA */
  349. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  350. sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
  351. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  352. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  353. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  354. SA_CIPHER_ALG_AES,
  355. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  356. SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
  357. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  358. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  359. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  360. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  361. SA_NOT_COPY_HDR);
  362. sa->sa_command_1.bf.key_len = keylen >> 3;
  363. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
  364. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  365. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  366. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  367. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  368. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  369. SA_CIPHER_ALG_AES,
  370. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  371. SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
  372. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  373. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  374. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  375. SA_COPY_PAD, SA_COPY_PAYLOAD,
  376. SA_NOT_COPY_HDR);
  377. sa->sa_command_1.bf.key_len = keylen >> 3;
  378. return 0;
  379. }
  380. static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
  381. {
  382. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  383. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  384. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  385. __le32 iv[16];
  386. u32 tmp_sa[SA_AES128_CCM_LEN + 4];
  387. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
  388. unsigned int len = req->cryptlen;
  389. if (decrypt)
  390. len -= crypto_aead_authsize(aead);
  391. if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
  392. return crypto4xx_aead_fallback(req, ctx, decrypt);
  393. memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
  394. sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
  395. if (req->iv[0] == 1) {
  396. /* CRYPTO_MODE_AES_ICM */
  397. sa->sa_command_1.bf.crypto_mode9_8 = 1;
  398. }
  399. iv[3] = cpu_to_le32(0);
  400. crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
  401. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  402. len, iv, sizeof(iv),
  403. sa, ctx->sa_len, req->assoclen, rctx->dst);
  404. }
  405. int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
  406. {
  407. return crypto4xx_crypt_aes_ccm(req, false);
  408. }
  409. int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
  410. {
  411. return crypto4xx_crypt_aes_ccm(req, true);
  412. }
  413. int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
  414. unsigned int authsize)
  415. {
  416. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  417. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  418. return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
  419. }
  420. /**
  421. * AES-GCM Functions
  422. */
  423. static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
  424. {
  425. switch (keylen) {
  426. case 16:
  427. case 24:
  428. case 32:
  429. return 0;
  430. default:
  431. return -EINVAL;
  432. }
  433. }
  434. static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
  435. unsigned int keylen)
  436. {
  437. struct crypto_cipher *aes_tfm = NULL;
  438. uint8_t src[16] = { 0 };
  439. int rc = 0;
  440. aes_tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC |
  441. CRYPTO_ALG_NEED_FALLBACK);
  442. if (IS_ERR(aes_tfm)) {
  443. rc = PTR_ERR(aes_tfm);
  444. pr_warn("could not load aes cipher driver: %d\n", rc);
  445. return rc;
  446. }
  447. rc = crypto_cipher_setkey(aes_tfm, key, keylen);
  448. if (rc) {
  449. pr_err("setkey() failed: %d\n", rc);
  450. goto out;
  451. }
  452. crypto_cipher_encrypt_one(aes_tfm, src, src);
  453. crypto4xx_memcpy_to_le32(hash_start, src, 16);
  454. out:
  455. crypto_free_cipher(aes_tfm);
  456. return rc;
  457. }
  458. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  459. const u8 *key, unsigned int keylen)
  460. {
  461. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  462. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  463. struct dynamic_sa_ctl *sa;
  464. int rc = 0;
  465. if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
  466. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  467. return -EINVAL;
  468. }
  469. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  470. if (rc)
  471. return rc;
  472. if (ctx->sa_in || ctx->sa_out)
  473. crypto4xx_free_sa(ctx);
  474. rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
  475. if (rc)
  476. return rc;
  477. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  478. sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
  479. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  480. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  481. SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
  482. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  483. SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
  484. DIR_INBOUND);
  485. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  486. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  487. SA_SEQ_MASK_ON, SA_MC_DISABLE,
  488. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  489. SA_NOT_COPY_HDR);
  490. sa->sa_command_1.bf.key_len = keylen >> 3;
  491. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  492. key, keylen);
  493. rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
  494. key, keylen);
  495. if (rc) {
  496. pr_err("GCM hash key setting failed = %d\n", rc);
  497. goto err;
  498. }
  499. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  500. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  501. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  502. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
  503. return 0;
  504. err:
  505. crypto4xx_free_sa(ctx);
  506. return rc;
  507. }
  508. static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
  509. bool decrypt)
  510. {
  511. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  512. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  513. __le32 iv[4];
  514. unsigned int len = req->cryptlen;
  515. if (decrypt)
  516. len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
  517. if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
  518. return crypto4xx_aead_fallback(req, ctx, decrypt);
  519. crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
  520. iv[3] = cpu_to_le32(1);
  521. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  522. len, iv, sizeof(iv),
  523. decrypt ? ctx->sa_in : ctx->sa_out,
  524. ctx->sa_len, req->assoclen, rctx->dst);
  525. }
  526. int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
  527. {
  528. return crypto4xx_crypt_aes_gcm(req, false);
  529. }
  530. int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
  531. {
  532. return crypto4xx_crypt_aes_gcm(req, true);
  533. }
  534. /**
  535. * HASH SHA1 Functions
  536. */
  537. static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
  538. unsigned int sa_len,
  539. unsigned char ha,
  540. unsigned char hm)
  541. {
  542. struct crypto_alg *alg = tfm->__crt_alg;
  543. struct crypto4xx_alg *my_alg;
  544. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  545. struct dynamic_sa_hash160 *sa;
  546. int rc;
  547. my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
  548. alg.u.hash);
  549. ctx->dev = my_alg->dev;
  550. /* Create SA */
  551. if (ctx->sa_in || ctx->sa_out)
  552. crypto4xx_free_sa(ctx);
  553. rc = crypto4xx_alloc_sa(ctx, sa_len);
  554. if (rc)
  555. return rc;
  556. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  557. sizeof(struct crypto4xx_ctx));
  558. sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
  559. set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  560. SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
  561. SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
  562. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  563. SA_OPCODE_HASH, DIR_INBOUND);
  564. set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
  565. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  566. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  567. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  568. SA_NOT_COPY_HDR);
  569. /* Need to zero hash digest in SA */
  570. memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
  571. memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
  572. return 0;
  573. }
  574. int crypto4xx_hash_init(struct ahash_request *req)
  575. {
  576. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  577. int ds;
  578. struct dynamic_sa_ctl *sa;
  579. sa = ctx->sa_in;
  580. ds = crypto_ahash_digestsize(
  581. __crypto_ahash_cast(req->base.tfm));
  582. sa->sa_command_0.bf.digest_len = ds >> 2;
  583. sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
  584. return 0;
  585. }
  586. int crypto4xx_hash_update(struct ahash_request *req)
  587. {
  588. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  589. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  590. struct scatterlist dst;
  591. unsigned int ds = crypto_ahash_digestsize(ahash);
  592. sg_init_one(&dst, req->result, ds);
  593. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  594. req->nbytes, NULL, 0, ctx->sa_in,
  595. ctx->sa_len, 0, NULL);
  596. }
  597. int crypto4xx_hash_final(struct ahash_request *req)
  598. {
  599. return 0;
  600. }
  601. int crypto4xx_hash_digest(struct ahash_request *req)
  602. {
  603. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  604. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  605. struct scatterlist dst;
  606. unsigned int ds = crypto_ahash_digestsize(ahash);
  607. sg_init_one(&dst, req->result, ds);
  608. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  609. req->nbytes, NULL, 0, ctx->sa_in,
  610. ctx->sa_len, 0, NULL);
  611. }
  612. /**
  613. * SHA1 Algorithm
  614. */
  615. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
  616. {
  617. return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
  618. SA_HASH_MODE_HASH);
  619. }