crypto4xx_alg.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/aes.h>
  29. #include <crypto/sha.h>
  30. #include "crypto4xx_reg_def.h"
  31. #include "crypto4xx_sa.h"
  32. #include "crypto4xx_core.h"
  33. void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
  34. u32 save_iv, u32 ld_h, u32 ld_iv, u32 hdr_proc,
  35. u32 h, u32 c, u32 pad_type, u32 op_grp, u32 op,
  36. u32 dir)
  37. {
  38. sa->sa_command_0.w = 0;
  39. sa->sa_command_0.bf.save_hash_state = save_h;
  40. sa->sa_command_0.bf.save_iv = save_iv;
  41. sa->sa_command_0.bf.load_hash_state = ld_h;
  42. sa->sa_command_0.bf.load_iv = ld_iv;
  43. sa->sa_command_0.bf.hdr_proc = hdr_proc;
  44. sa->sa_command_0.bf.hash_alg = h;
  45. sa->sa_command_0.bf.cipher_alg = c;
  46. sa->sa_command_0.bf.pad_type = pad_type & 3;
  47. sa->sa_command_0.bf.extend_pad = pad_type >> 2;
  48. sa->sa_command_0.bf.op_group = op_grp;
  49. sa->sa_command_0.bf.opcode = op;
  50. sa->sa_command_0.bf.dir = dir;
  51. }
  52. void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm, u32 hmac_mc,
  53. u32 cfb, u32 esn, u32 sn_mask, u32 mute,
  54. u32 cp_pad, u32 cp_pay, u32 cp_hdr)
  55. {
  56. sa->sa_command_1.w = 0;
  57. sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
  58. sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
  59. sa->sa_command_1.bf.feedback_mode = cfb,
  60. sa->sa_command_1.bf.sa_rev = 1;
  61. sa->sa_command_1.bf.extended_seq_num = esn;
  62. sa->sa_command_1.bf.seq_num_mask = sn_mask;
  63. sa->sa_command_1.bf.mutable_bit_proc = mute;
  64. sa->sa_command_1.bf.copy_pad = cp_pad;
  65. sa->sa_command_1.bf.copy_payload = cp_pay;
  66. sa->sa_command_1.bf.copy_hdr = cp_hdr;
  67. }
  68. int crypto4xx_encrypt(struct ablkcipher_request *req)
  69. {
  70. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  71. ctx->direction = DIR_OUTBOUND;
  72. ctx->hash_final = 0;
  73. ctx->is_hash = 0;
  74. ctx->pd_ctl = 0x1;
  75. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  76. req->nbytes, req->info,
  77. get_dynamic_sa_iv_size(ctx));
  78. }
  79. int crypto4xx_decrypt(struct ablkcipher_request *req)
  80. {
  81. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  82. ctx->direction = DIR_INBOUND;
  83. ctx->hash_final = 0;
  84. ctx->is_hash = 0;
  85. ctx->pd_ctl = 1;
  86. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  87. req->nbytes, req->info,
  88. get_dynamic_sa_iv_size(ctx));
  89. }
  90. /**
  91. * AES Functions
  92. */
  93. static int crypto4xx_setkey_aes(struct crypto_ablkcipher *cipher,
  94. const u8 *key,
  95. unsigned int keylen,
  96. unsigned char cm,
  97. u8 fb)
  98. {
  99. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  100. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  101. struct dynamic_sa_ctl *sa;
  102. int rc;
  103. if (keylen != AES_KEYSIZE_256 &&
  104. keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
  105. crypto_ablkcipher_set_flags(cipher,
  106. CRYPTO_TFM_RES_BAD_KEY_LEN);
  107. return -EINVAL;
  108. }
  109. /* Create SA */
  110. if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
  111. crypto4xx_free_sa(ctx);
  112. rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
  113. if (rc)
  114. return rc;
  115. if (ctx->state_record_dma_addr == 0) {
  116. rc = crypto4xx_alloc_state_record(ctx);
  117. if (rc) {
  118. crypto4xx_free_sa(ctx);
  119. return rc;
  120. }
  121. }
  122. /* Setup SA */
  123. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  124. ctx->hash_final = 0;
  125. set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, SA_NOT_SAVE_IV,
  126. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  127. SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
  128. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  129. SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
  130. DIR_INBOUND);
  131. set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
  132. fb, SA_EXTENDED_SN_OFF,
  133. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  134. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  135. SA_NOT_COPY_HDR);
  136. crypto4xx_memcpy_le(ctx->sa_in + get_dynamic_sa_offset_key_field(ctx),
  137. key, keylen);
  138. sa->sa_contents = SA_AES_CONTENTS | (keylen << 2);
  139. sa->sa_command_1.bf.key_len = keylen >> 3;
  140. ctx->is_hash = 0;
  141. ctx->direction = DIR_INBOUND;
  142. memcpy(ctx->sa_in + get_dynamic_sa_offset_state_ptr_field(ctx),
  143. (void *)&ctx->state_record_dma_addr, 4);
  144. ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(ctx);
  145. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  146. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  147. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  148. return 0;
  149. }
  150. int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
  151. const u8 *key, unsigned int keylen)
  152. {
  153. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
  154. CRYPTO_FEEDBACK_MODE_NO_FB);
  155. }
  156. /**
  157. * HASH SHA1 Functions
  158. */
  159. static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
  160. unsigned int sa_len,
  161. unsigned char ha,
  162. unsigned char hm)
  163. {
  164. struct crypto_alg *alg = tfm->__crt_alg;
  165. struct crypto4xx_alg *my_alg = crypto_alg_to_crypto4xx_alg(alg);
  166. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  167. struct dynamic_sa_ctl *sa;
  168. struct dynamic_sa_hash160 *sa_in;
  169. int rc;
  170. ctx->dev = my_alg->dev;
  171. ctx->is_hash = 1;
  172. ctx->hash_final = 0;
  173. /* Create SA */
  174. if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
  175. crypto4xx_free_sa(ctx);
  176. rc = crypto4xx_alloc_sa(ctx, sa_len);
  177. if (rc)
  178. return rc;
  179. if (ctx->state_record_dma_addr == 0) {
  180. crypto4xx_alloc_state_record(ctx);
  181. if (!ctx->state_record_dma_addr) {
  182. crypto4xx_free_sa(ctx);
  183. return -ENOMEM;
  184. }
  185. }
  186. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  187. sizeof(struct crypto4xx_ctx));
  188. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  189. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  190. SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
  191. SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
  192. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  193. SA_OPCODE_HASH, DIR_INBOUND);
  194. set_dynamic_sa_command_1(sa, 0, SA_HASH_MODE_HASH,
  195. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  196. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  197. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  198. SA_NOT_COPY_HDR);
  199. ctx->direction = DIR_INBOUND;
  200. sa->sa_contents = SA_HASH160_CONTENTS;
  201. sa_in = (struct dynamic_sa_hash160 *) ctx->sa_in;
  202. /* Need to zero hash digest in SA */
  203. memset(sa_in->inner_digest, 0, sizeof(sa_in->inner_digest));
  204. memset(sa_in->outer_digest, 0, sizeof(sa_in->outer_digest));
  205. sa_in->state_ptr = ctx->state_record_dma_addr;
  206. ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(ctx);
  207. return 0;
  208. }
  209. int crypto4xx_hash_init(struct ahash_request *req)
  210. {
  211. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  212. int ds;
  213. struct dynamic_sa_ctl *sa;
  214. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  215. ds = crypto_ahash_digestsize(
  216. __crypto_ahash_cast(req->base.tfm));
  217. sa->sa_command_0.bf.digest_len = ds >> 2;
  218. sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
  219. ctx->is_hash = 1;
  220. ctx->direction = DIR_INBOUND;
  221. return 0;
  222. }
  223. int crypto4xx_hash_update(struct ahash_request *req)
  224. {
  225. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  226. ctx->is_hash = 1;
  227. ctx->hash_final = 0;
  228. ctx->pd_ctl = 0x11;
  229. ctx->direction = DIR_INBOUND;
  230. return crypto4xx_build_pd(&req->base, ctx, req->src,
  231. (struct scatterlist *) req->result,
  232. req->nbytes, NULL, 0);
  233. }
  234. int crypto4xx_hash_final(struct ahash_request *req)
  235. {
  236. return 0;
  237. }
  238. int crypto4xx_hash_digest(struct ahash_request *req)
  239. {
  240. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  241. ctx->hash_final = 1;
  242. ctx->pd_ctl = 0x11;
  243. ctx->direction = DIR_INBOUND;
  244. return crypto4xx_build_pd(&req->base, ctx, req->src,
  245. (struct scatterlist *) req->result,
  246. req->nbytes, NULL, 0);
  247. }
  248. /**
  249. * SHA1 Algorithm
  250. */
  251. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
  252. {
  253. return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
  254. SA_HASH_MODE_HASH);
  255. }