chcr_algo.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * This file is part of the Chelsio T6 Crypto driver for Linux.
  3. *
  4. * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. */
  35. #ifndef __CHCR_ALGO_H__
  36. #define __CHCR_ALGO_H__
  37. /* Crypto key context */
  38. #define KEY_CONTEXT_CTX_LEN_S 24
  39. #define KEY_CONTEXT_CTX_LEN_M 0xff
  40. #define KEY_CONTEXT_CTX_LEN_V(x) ((x) << KEY_CONTEXT_CTX_LEN_S)
  41. #define KEY_CONTEXT_CTX_LEN_G(x) \
  42. (((x) >> KEY_CONTEXT_CTX_LEN_S) & KEY_CONTEXT_CTX_LEN_M)
  43. #define KEY_CONTEXT_DUAL_CK_S 12
  44. #define KEY_CONTEXT_DUAL_CK_M 0x1
  45. #define KEY_CONTEXT_DUAL_CK_V(x) ((x) << KEY_CONTEXT_DUAL_CK_S)
  46. #define KEY_CONTEXT_DUAL_CK_G(x) \
  47. (((x) >> KEY_CONTEXT_DUAL_CK_S) & KEY_CONTEXT_DUAL_CK_M)
  48. #define KEY_CONTEXT_DUAL_CK_F KEY_CONTEXT_DUAL_CK_V(1U)
  49. #define KEY_CONTEXT_SALT_PRESENT_S 10
  50. #define KEY_CONTEXT_SALT_PRESENT_M 0x1
  51. #define KEY_CONTEXT_SALT_PRESENT_V(x) ((x) << KEY_CONTEXT_SALT_PRESENT_S)
  52. #define KEY_CONTEXT_SALT_PRESENT_G(x) \
  53. (((x) >> KEY_CONTEXT_SALT_PRESENT_S) & \
  54. KEY_CONTEXT_SALT_PRESENT_M)
  55. #define KEY_CONTEXT_SALT_PRESENT_F KEY_CONTEXT_SALT_PRESENT_V(1U)
  56. #define KEY_CONTEXT_VALID_S 0
  57. #define KEY_CONTEXT_VALID_M 0x1
  58. #define KEY_CONTEXT_VALID_V(x) ((x) << KEY_CONTEXT_VALID_S)
  59. #define KEY_CONTEXT_VALID_G(x) \
  60. (((x) >> KEY_CONTEXT_VALID_S) & \
  61. KEY_CONTEXT_VALID_M)
  62. #define KEY_CONTEXT_VALID_F KEY_CONTEXT_VALID_V(1U)
  63. #define KEY_CONTEXT_CK_SIZE_S 6
  64. #define KEY_CONTEXT_CK_SIZE_M 0xf
  65. #define KEY_CONTEXT_CK_SIZE_V(x) ((x) << KEY_CONTEXT_CK_SIZE_S)
  66. #define KEY_CONTEXT_CK_SIZE_G(x) \
  67. (((x) >> KEY_CONTEXT_CK_SIZE_S) & KEY_CONTEXT_CK_SIZE_M)
  68. #define KEY_CONTEXT_MK_SIZE_S 2
  69. #define KEY_CONTEXT_MK_SIZE_M 0xf
  70. #define KEY_CONTEXT_MK_SIZE_V(x) ((x) << KEY_CONTEXT_MK_SIZE_S)
  71. #define KEY_CONTEXT_MK_SIZE_G(x) \
  72. (((x) >> KEY_CONTEXT_MK_SIZE_S) & KEY_CONTEXT_MK_SIZE_M)
  73. #define KEY_CONTEXT_OPAD_PRESENT_S 11
  74. #define KEY_CONTEXT_OPAD_PRESENT_M 0x1
  75. #define KEY_CONTEXT_OPAD_PRESENT_V(x) ((x) << KEY_CONTEXT_OPAD_PRESENT_S)
  76. #define KEY_CONTEXT_OPAD_PRESENT_G(x) \
  77. (((x) >> KEY_CONTEXT_OPAD_PRESENT_S) & \
  78. KEY_CONTEXT_OPAD_PRESENT_M)
  79. #define KEY_CONTEXT_OPAD_PRESENT_F KEY_CONTEXT_OPAD_PRESENT_V(1U)
  80. #define CHCR_HASH_MAX_DIGEST_SIZE 64
  81. #define CHCR_MAX_SHA_DIGEST_SIZE 64
  82. #define IPSEC_TRUNCATED_ICV_SIZE 12
  83. #define TLS_TRUNCATED_HMAC_SIZE 10
  84. #define CBCMAC_DIGEST_SIZE 16
  85. #define MAX_HASH_NAME 20
  86. #define SHA1_INIT_STATE_5X4B 5
  87. #define SHA256_INIT_STATE_8X4B 8
  88. #define SHA512_INIT_STATE_8X8B 8
  89. #define SHA1_INIT_STATE SHA1_INIT_STATE_5X4B
  90. #define SHA224_INIT_STATE SHA256_INIT_STATE_8X4B
  91. #define SHA256_INIT_STATE SHA256_INIT_STATE_8X4B
  92. #define SHA384_INIT_STATE SHA512_INIT_STATE_8X8B
  93. #define SHA512_INIT_STATE SHA512_INIT_STATE_8X8B
  94. #define DUMMY_BYTES 16
  95. #define IPAD_DATA 0x36363636
  96. #define OPAD_DATA 0x5c5c5c5c
  97. #define TRANSHDR_SIZE(alignedkctx_len)\
  98. (sizeof(struct ulptx_idata) +\
  99. sizeof(struct ulp_txpkt) +\
  100. sizeof(struct fw_crypto_lookaside_wr) +\
  101. sizeof(struct cpl_tx_sec_pdu) +\
  102. (alignedkctx_len))
  103. #define CIPHER_TRANSHDR_SIZE(alignedkctx_len, sge_pairs) \
  104. (TRANSHDR_SIZE(alignedkctx_len) + sge_pairs +\
  105. sizeof(struct cpl_rx_phys_dsgl))
  106. #define HASH_TRANSHDR_SIZE(alignedkctx_len)\
  107. (TRANSHDR_SIZE(alignedkctx_len) + DUMMY_BYTES)
  108. #define SEC_CPL_OFFSET (sizeof(struct fw_crypto_lookaside_wr) + \
  109. sizeof(struct ulp_txpkt) + \
  110. sizeof(struct ulptx_idata))
  111. #define FILL_SEC_CPL_OP_IVINSR(id, len, hldr, ofst) \
  112. htonl( \
  113. CPL_TX_SEC_PDU_OPCODE_V(CPL_TX_SEC_PDU) | \
  114. CPL_TX_SEC_PDU_RXCHID_V((id)) | \
  115. CPL_TX_SEC_PDU_ACKFOLLOWS_V(0) | \
  116. CPL_TX_SEC_PDU_ULPTXLPBK_V(1) | \
  117. CPL_TX_SEC_PDU_CPLLEN_V((len)) | \
  118. CPL_TX_SEC_PDU_PLACEHOLDER_V((hldr)) | \
  119. CPL_TX_SEC_PDU_IVINSRTOFST_V((ofst)))
  120. #define FILL_SEC_CPL_CIPHERSTOP_HI(a_start, a_stop, c_start, c_stop_hi) \
  121. htonl( \
  122. CPL_TX_SEC_PDU_AADSTART_V((a_start)) | \
  123. CPL_TX_SEC_PDU_AADSTOP_V((a_stop)) | \
  124. CPL_TX_SEC_PDU_CIPHERSTART_V((c_start)) | \
  125. CPL_TX_SEC_PDU_CIPHERSTOP_HI_V((c_stop_hi)))
  126. #define FILL_SEC_CPL_AUTHINSERT(c_stop_lo, a_start, a_stop, a_inst) \
  127. htonl( \
  128. CPL_TX_SEC_PDU_CIPHERSTOP_LO_V((c_stop_lo)) | \
  129. CPL_TX_SEC_PDU_AUTHSTART_V((a_start)) | \
  130. CPL_TX_SEC_PDU_AUTHSTOP_V((a_stop)) | \
  131. CPL_TX_SEC_PDU_AUTHINSERT_V((a_inst)))
  132. #define FILL_SEC_CPL_SCMD0_SEQNO(ctrl, seq, cmode, amode, opad, size, nivs) \
  133. htonl( \
  134. SCMD_SEQ_NO_CTRL_V(0) | \
  135. SCMD_STATUS_PRESENT_V(0) | \
  136. SCMD_PROTO_VERSION_V(CHCR_SCMD_PROTO_VERSION_GENERIC) | \
  137. SCMD_ENC_DEC_CTRL_V((ctrl)) | \
  138. SCMD_CIPH_AUTH_SEQ_CTRL_V((seq)) | \
  139. SCMD_CIPH_MODE_V((cmode)) | \
  140. SCMD_AUTH_MODE_V((amode)) | \
  141. SCMD_HMAC_CTRL_V((opad)) | \
  142. SCMD_IV_SIZE_V((size)) | \
  143. SCMD_NUM_IVS_V((nivs)))
  144. #define FILL_SEC_CPL_IVGEN_HDRLEN(last, more, ctx_in, mac, ivdrop, len) htonl( \
  145. SCMD_ENB_DBGID_V(0) | \
  146. SCMD_IV_GEN_CTRL_V(0) | \
  147. SCMD_LAST_FRAG_V((last)) | \
  148. SCMD_MORE_FRAGS_V((more)) | \
  149. SCMD_TLS_COMPPDU_V(0) | \
  150. SCMD_KEY_CTX_INLINE_V((ctx_in)) | \
  151. SCMD_TLS_FRAG_ENABLE_V(0) | \
  152. SCMD_MAC_ONLY_V((mac)) | \
  153. SCMD_AADIVDROP_V((ivdrop)) | \
  154. SCMD_HDR_LEN_V((len)))
  155. #define FILL_KEY_CTX_HDR(ck_size, mk_size, d_ck, opad, ctx_len) \
  156. htonl(KEY_CONTEXT_VALID_V(1) | \
  157. KEY_CONTEXT_CK_SIZE_V((ck_size)) | \
  158. KEY_CONTEXT_MK_SIZE_V(mk_size) | \
  159. KEY_CONTEXT_DUAL_CK_V((d_ck)) | \
  160. KEY_CONTEXT_OPAD_PRESENT_V((opad)) | \
  161. KEY_CONTEXT_SALT_PRESENT_V(1) | \
  162. KEY_CONTEXT_CTX_LEN_V((ctx_len)))
  163. #define FILL_WR_OP_CCTX_SIZE(len, ctx_len) \
  164. htonl( \
  165. FW_CRYPTO_LOOKASIDE_WR_OPCODE_V( \
  166. FW_CRYPTO_LOOKASIDE_WR) | \
  167. FW_CRYPTO_LOOKASIDE_WR_COMPL_V(0) | \
  168. FW_CRYPTO_LOOKASIDE_WR_IMM_LEN_V((len)) | \
  169. FW_CRYPTO_LOOKASIDE_WR_CCTX_LOC_V(1) | \
  170. FW_CRYPTO_LOOKASIDE_WR_CCTX_SIZE_V((ctx_len)))
  171. #define FILL_WR_RX_Q_ID(cid, qid, wr_iv) \
  172. htonl( \
  173. FW_CRYPTO_LOOKASIDE_WR_RX_CHID_V((cid)) | \
  174. FW_CRYPTO_LOOKASIDE_WR_RX_Q_ID_V((qid)) | \
  175. FW_CRYPTO_LOOKASIDE_WR_LCB_V(0) | \
  176. FW_CRYPTO_LOOKASIDE_WR_IV_V((wr_iv)))
  177. #define FILL_ULPTX_CMD_DEST(cid) \
  178. htonl(ULPTX_CMD_V(ULP_TX_PKT) | \
  179. ULP_TXPKT_DEST_V(0) | \
  180. ULP_TXPKT_DATAMODIFY_V(0) | \
  181. ULP_TXPKT_CHANNELID_V((cid)) | \
  182. ULP_TXPKT_RO_V(1) | \
  183. ULP_TXPKT_FID_V(0))
  184. #define KEYCTX_ALIGN_PAD(bs) ({unsigned int _bs = (bs);\
  185. _bs == SHA1_DIGEST_SIZE ? 12 : 0; })
  186. #define FILL_PLD_SIZE_HASH_SIZE(payload_sgl_len, sgl_lengths, total_frags) \
  187. htonl(FW_CRYPTO_LOOKASIDE_WR_PLD_SIZE_V(payload_sgl_len ? \
  188. sgl_lengths[total_frags] : 0) |\
  189. FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(0))
  190. #define FILL_LEN_PKD(calc_tx_flits_ofld, skb) \
  191. htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP((\
  192. calc_tx_flits_ofld(skb) * 8), 16)))
  193. #define FILL_CMD_MORE(immdatalen) htonl(ULPTX_CMD_V(ULP_TX_SC_IMM) |\
  194. ULP_TX_SC_MORE_V((immdatalen) ? 0 : 1))
  195. #define MAX_NK 8
  196. #define CRYPTO_MAX_IMM_TX_PKT_LEN 256
  197. struct algo_param {
  198. unsigned int auth_mode;
  199. unsigned int mk_size;
  200. unsigned int result_size;
  201. };
  202. struct hash_wr_param {
  203. unsigned int opad_needed;
  204. unsigned int more;
  205. unsigned int last;
  206. struct algo_param alg_prm;
  207. unsigned int sg_len;
  208. unsigned int bfr_len;
  209. u64 scmd1;
  210. };
  211. enum {
  212. AES_KEYLENGTH_128BIT = 128,
  213. AES_KEYLENGTH_192BIT = 192,
  214. AES_KEYLENGTH_256BIT = 256
  215. };
  216. enum {
  217. KEYLENGTH_3BYTES = 3,
  218. KEYLENGTH_4BYTES = 4,
  219. KEYLENGTH_6BYTES = 6,
  220. KEYLENGTH_8BYTES = 8
  221. };
  222. enum {
  223. NUMBER_OF_ROUNDS_10 = 10,
  224. NUMBER_OF_ROUNDS_12 = 12,
  225. NUMBER_OF_ROUNDS_14 = 14,
  226. };
  227. /*
  228. * CCM defines values of 4, 6, 8, 10, 12, 14, and 16 octets,
  229. * where they indicate the size of the integrity check value (ICV)
  230. */
  231. enum {
  232. AES_CCM_ICV_4 = 4,
  233. AES_CCM_ICV_6 = 6,
  234. AES_CCM_ICV_8 = 8,
  235. AES_CCM_ICV_10 = 10,
  236. AES_CCM_ICV_12 = 12,
  237. AES_CCM_ICV_14 = 14,
  238. AES_CCM_ICV_16 = 16
  239. };
  240. struct hash_op_params {
  241. unsigned char mk_size;
  242. unsigned char pad_align;
  243. unsigned char auth_mode;
  244. char hash_name[MAX_HASH_NAME];
  245. unsigned short block_size;
  246. unsigned short word_size;
  247. unsigned short ipad_size;
  248. };
  249. struct phys_sge_pairs {
  250. __be16 len[8];
  251. __be64 addr[8];
  252. };
  253. struct phys_sge_parm {
  254. unsigned int nents;
  255. unsigned int obsize;
  256. unsigned short qid;
  257. unsigned char align;
  258. };
  259. struct crypto_result {
  260. struct completion completion;
  261. int err;
  262. };
  263. static const u32 sha1_init[SHA1_DIGEST_SIZE / 4] = {
  264. SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4,
  265. };
  266. static const u32 sha224_init[SHA256_DIGEST_SIZE / 4] = {
  267. SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
  268. SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7,
  269. };
  270. static const u32 sha256_init[SHA256_DIGEST_SIZE / 4] = {
  271. SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
  272. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7,
  273. };
  274. static const u64 sha384_init[SHA512_DIGEST_SIZE / 8] = {
  275. SHA384_H0, SHA384_H1, SHA384_H2, SHA384_H3,
  276. SHA384_H4, SHA384_H5, SHA384_H6, SHA384_H7,
  277. };
  278. static const u64 sha512_init[SHA512_DIGEST_SIZE / 8] = {
  279. SHA512_H0, SHA512_H1, SHA512_H2, SHA512_H3,
  280. SHA512_H4, SHA512_H5, SHA512_H6, SHA512_H7,
  281. };
  282. static inline void copy_hash_init_values(char *key, int digestsize)
  283. {
  284. u8 i;
  285. __be32 *dkey = (__be32 *)key;
  286. u64 *ldkey = (u64 *)key;
  287. __be64 *sha384 = (__be64 *)sha384_init;
  288. __be64 *sha512 = (__be64 *)sha512_init;
  289. switch (digestsize) {
  290. case SHA1_DIGEST_SIZE:
  291. for (i = 0; i < SHA1_INIT_STATE; i++)
  292. dkey[i] = cpu_to_be32(sha1_init[i]);
  293. break;
  294. case SHA224_DIGEST_SIZE:
  295. for (i = 0; i < SHA224_INIT_STATE; i++)
  296. dkey[i] = cpu_to_be32(sha224_init[i]);
  297. break;
  298. case SHA256_DIGEST_SIZE:
  299. for (i = 0; i < SHA256_INIT_STATE; i++)
  300. dkey[i] = cpu_to_be32(sha256_init[i]);
  301. break;
  302. case SHA384_DIGEST_SIZE:
  303. for (i = 0; i < SHA384_INIT_STATE; i++)
  304. ldkey[i] = be64_to_cpu(sha384[i]);
  305. break;
  306. case SHA512_DIGEST_SIZE:
  307. for (i = 0; i < SHA512_INIT_STATE; i++)
  308. ldkey[i] = be64_to_cpu(sha512[i]);
  309. break;
  310. }
  311. }
  312. static const u8 sgl_lengths[20] = {
  313. 0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 13, 13, 14, 15
  314. };
  315. /* Number of len fields(8) * size of one addr field */
  316. #define PHYSDSGL_MAX_LEN_SIZE 16
  317. static inline u16 get_space_for_phys_dsgl(unsigned int sgl_entr)
  318. {
  319. /* len field size + addr field size */
  320. return ((sgl_entr >> 3) + ((sgl_entr % 8) ?
  321. 1 : 0)) * PHYSDSGL_MAX_LEN_SIZE +
  322. (sgl_entr << 3) + ((sgl_entr % 2 ? 1 : 0) << 3);
  323. }
  324. /* The AES s-transform matrix (s-box). */
  325. static const u8 aes_sbox[256] = {
  326. 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215,
  327. 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175,
  328. 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165,
  329. 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7,
  330. 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90,
  331. 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32,
  332. 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170,
  333. 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81,
  334. 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243,
  335. 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100,
  336. 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184,
  337. 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194,
  338. 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78,
  339. 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166,
  340. 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102,
  341. 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248,
  342. 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
  343. 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84,
  344. 187, 22
  345. };
  346. static u32 aes_ks_subword(const u32 w)
  347. {
  348. u8 bytes[4];
  349. *(u32 *)(&bytes[0]) = w;
  350. bytes[0] = aes_sbox[bytes[0]];
  351. bytes[1] = aes_sbox[bytes[1]];
  352. bytes[2] = aes_sbox[bytes[2]];
  353. bytes[3] = aes_sbox[bytes[3]];
  354. return *(u32 *)(&bytes[0]);
  355. }
  356. static u32 round_constant[11] = {
  357. 0x01000000, 0x02000000, 0x04000000, 0x08000000,
  358. 0x10000000, 0x20000000, 0x40000000, 0x80000000,
  359. 0x1B000000, 0x36000000, 0x6C000000
  360. };
  361. /* dec_key - OUTPUT - Reverse round key
  362. * key - INPUT - key
  363. * keylength - INPUT - length of the key in number of bits
  364. */
  365. static inline void get_aes_decrypt_key(unsigned char *dec_key,
  366. const unsigned char *key,
  367. unsigned int keylength)
  368. {
  369. u32 temp;
  370. u32 w_ring[MAX_NK];
  371. int i, j, k;
  372. u8 nr, nk;
  373. switch (keylength) {
  374. case AES_KEYLENGTH_128BIT:
  375. nk = KEYLENGTH_4BYTES;
  376. nr = NUMBER_OF_ROUNDS_10;
  377. break;
  378. case AES_KEYLENGTH_192BIT:
  379. nk = KEYLENGTH_6BYTES;
  380. nr = NUMBER_OF_ROUNDS_12;
  381. break;
  382. case AES_KEYLENGTH_256BIT:
  383. nk = KEYLENGTH_8BYTES;
  384. nr = NUMBER_OF_ROUNDS_14;
  385. break;
  386. default:
  387. return;
  388. }
  389. for (i = 0; i < nk; i++ )
  390. w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
  391. i = 0;
  392. temp = w_ring[nk - 1];
  393. while(i + nk < (nr + 1) * 4) {
  394. if(!(i % nk)) {
  395. /* RotWord(temp) */
  396. temp = (temp << 8) | (temp >> 24);
  397. temp = aes_ks_subword(temp);
  398. temp ^= round_constant[i / nk];
  399. }
  400. else if (nk == 8 && (i % 4 == 0))
  401. temp = aes_ks_subword(temp);
  402. w_ring[i % nk] ^= temp;
  403. temp = w_ring[i % nk];
  404. i++;
  405. }
  406. i--;
  407. for (k = 0, j = i % nk; k < nk; k++) {
  408. *((u32 *)dec_key + k) = htonl(w_ring[j]);
  409. j--;
  410. if(j < 0)
  411. j += nk;
  412. }
  413. }
  414. #endif /* __CHCR_ALGO_H__ */