keystone-sa-lld.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Keystone crypto accelerator driver
  3. *
  4. * Copyright (C) 2015, 2016 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Sandeep Nair
  7. * Vitaly Andrianov
  8. *
  9. * Contributors:Tinku Mannan
  10. * Hao Zhang
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #include <linux/types.h>
  22. #include <linux/crypto.h>
  23. #include <linux/cryptohash.h>
  24. #include <crypto/aes.h>
  25. #include <crypto/sha.h>
  26. #include <crypto/md5.h>
  27. #include "keystone-sa.h"
  28. #include "keystone-sa-hlp.h"
  29. /* Byte offset for key in encryption security context */
  30. #define SC_ENC_KEY_OFFSET (1 + 27 + 4)
  31. /* Byte offset for Aux-1 in encryption security context */
  32. #define SC_ENC_AUX1_OFFSET (1 + 27 + 4 + 32)
  33. struct sa_eng_mci_tbl sa_mci_tbl;
  34. /* Perform 16 byte swizzling */
  35. void sa_swiz_128(u8 *in, u8 *out, u16 len)
  36. {
  37. u8 data[16];
  38. int i, j;
  39. for (i = 0; i < len - 15; i += 16) {
  40. memcpy(data, &in[i], 16);
  41. for (j = 0; j < 16; j++)
  42. out[i + j] = data[15 - j];
  43. }
  44. }
  45. /* Convert CRA name to internal algorithm ID */
  46. void sa_conv_calg_to_salg(const char *cra_name, int *ealg_id, int *aalg_id)
  47. {
  48. *ealg_id = SA_EALG_ID_NONE;
  49. *aalg_id = SA_AALG_ID_NONE;
  50. if (!strcmp(cra_name, "authenc(hmac(sha1),cbc(aes))")) {
  51. *ealg_id = SA_EALG_ID_AES_CBC;
  52. *aalg_id = SA_AALG_ID_HMAC_SHA1;
  53. } else if (!strcmp(cra_name, "authenc(hmac(sha256),cbc(aes))")) {
  54. *ealg_id = SA_EALG_ID_AES_CBC;
  55. *aalg_id = SA_AALG_ID_HMAC_SHA2_256;
  56. } else if (!strcmp(cra_name, "authenc(hmac(sha1),ecb(cipher_null))")) {
  57. *ealg_id = SA_EALG_ID_NULL;
  58. *aalg_id = SA_AALG_ID_HMAC_SHA1;
  59. } else if (!strcmp(cra_name, "authenc(hmac(sha1),cbc(des3_ede))")) {
  60. *ealg_id = SA_EALG_ID_3DES_CBC;
  61. *aalg_id = SA_AALG_ID_HMAC_SHA1;
  62. } else if (!strcmp(cra_name, "authenc(xcbc(aes),cbc(aes))")) {
  63. *ealg_id = SA_EALG_ID_AES_CBC;
  64. *aalg_id = SA_AALG_ID_AES_XCBC;
  65. } else if (!strcmp(cra_name, "authenc(xcbc(aes),cbc(des3_ede))")) {
  66. *ealg_id = SA_EALG_ID_3DES_CBC;
  67. *aalg_id = SA_AALG_ID_AES_XCBC;
  68. } else if (!strcmp(cra_name, "rfc4106(gcm(aes))")) {
  69. *ealg_id = SA_EALG_ID_GCM;
  70. } else if (!strcmp(cra_name, "rfc4543(gcm(aes))")) {
  71. *aalg_id = SA_AALG_ID_GMAC;
  72. } else if (!strcmp(cra_name, "cbc(aes)")) {
  73. *ealg_id = SA_EALG_ID_AES_CBC;
  74. } else if (!strcmp(cra_name, "cbc(des3_ede)")) {
  75. *ealg_id = SA_EALG_ID_3DES_CBC;
  76. } else if (!strcmp(cra_name, "hmac(sha1)")) {
  77. *aalg_id = SA_AALG_ID_HMAC_SHA1;
  78. } else if (!strcmp(cra_name, "xcbc(aes)")) {
  79. *aalg_id = SA_AALG_ID_AES_XCBC;
  80. } else
  81. pr_err("%s - unsupported cra_name %s\n", __func__, cra_name);
  82. }
  83. struct sa_eng_info sa_eng_info_tbl[SA_ALG_ID_LAST] = {
  84. [SA_EALG_ID_NONE] = { SA_ENG_ID_NONE, 0},
  85. [SA_EALG_ID_NULL] = { SA_ENG_ID_NONE, 0},
  86. [SA_EALG_ID_AES_CTR] = { SA_ENG_ID_NONE, 0},
  87. [SA_EALG_ID_AES_F8] = { SA_ENG_ID_NONE, 0},
  88. [SA_EALG_ID_AES_CBC] = { SA_ENG_ID_EM1, SA_CTX_ENC_TYPE1_SZ},
  89. [SA_EALG_ID_DES_CBC] = { SA_ENG_ID_EM1, SA_CTX_ENC_TYPE1_SZ},
  90. [SA_EALG_ID_3DES_CBC] = { SA_ENG_ID_EM1, SA_CTX_ENC_TYPE1_SZ},
  91. [SA_EALG_ID_CCM] = { SA_ENG_ID_NONE, 0},
  92. [SA_EALG_ID_GCM] = { SA_ENG_ID_EM1, SA_CTX_ENC_TYPE2_SZ},
  93. [SA_AALG_ID_NULL] = { SA_ENG_ID_NONE, 0},
  94. [SA_AALG_ID_MD5] = { SA_ENG_ID_NONE, 0},
  95. [SA_AALG_ID_SHA1] = { SA_ENG_ID_NONE, 0},
  96. [SA_AALG_ID_SHA2_224] = { SA_ENG_ID_NONE, 0},
  97. [SA_AALG_ID_SHA2_256] = { SA_ENG_ID_NONE, 0},
  98. [SA_AALG_ID_HMAC_MD5] = { SA_ENG_ID_AM1, SA_CTX_AUTH_TYPE2_SZ},
  99. [SA_AALG_ID_HMAC_SHA1] = { SA_ENG_ID_AM1, SA_CTX_AUTH_TYPE2_SZ},
  100. [SA_AALG_ID_HMAC_SHA2_224] = { SA_ENG_ID_NONE, 0},
  101. [SA_AALG_ID_HMAC_SHA2_256] = { SA_ENG_ID_AM1, SA_CTX_AUTH_TYPE2_SZ},
  102. [SA_AALG_ID_GMAC] = { SA_ENG_ID_EM1, SA_CTX_ENC_TYPE2_SZ},
  103. [SA_AALG_ID_CMAC] = {SA_ENG_ID_EM1, SA_CTX_AUTH_TYPE1_SZ},
  104. [SA_AALG_ID_CBC_MAC] = { SA_ENG_ID_NONE, 0},
  105. [SA_AALG_ID_AES_XCBC] = {SA_ENG_ID_EM1, SA_CTX_AUTH_TYPE1_SZ}
  106. };
  107. /* Given an algorithm ID get the engine details */
  108. struct sa_eng_info *sa_get_engine_info(int alg_id)
  109. {
  110. if (alg_id < SA_ALG_ID_LAST)
  111. return &sa_eng_info_tbl[alg_id];
  112. pr_err("%s: unsupported algo\n", __func__);
  113. return &sa_eng_info_tbl[SA_EALG_ID_NONE];
  114. }
  115. /* Given an algorithm get the hash size */
  116. int sa_get_hash_size(u16 aalg_id)
  117. {
  118. int hash_size = 0;
  119. switch (aalg_id) {
  120. case SA_AALG_ID_MD5:
  121. case SA_AALG_ID_HMAC_MD5:
  122. hash_size = MD5_DIGEST_SIZE;
  123. break;
  124. case SA_AALG_ID_SHA1:
  125. case SA_AALG_ID_HMAC_SHA1:
  126. hash_size = SHA1_DIGEST_SIZE;
  127. break;
  128. case SA_AALG_ID_SHA2_224:
  129. case SA_AALG_ID_HMAC_SHA2_224:
  130. hash_size = SHA224_DIGEST_SIZE;
  131. break;
  132. case SA_AALG_ID_SHA2_256:
  133. case SA_AALG_ID_HMAC_SHA2_256:
  134. hash_size = SHA256_DIGEST_SIZE;
  135. break;
  136. case SA_AALG_ID_AES_XCBC:
  137. case SA_AALG_ID_CMAC:
  138. case SA_AALG_ID_GMAC:
  139. hash_size = AES_BLOCK_SIZE;
  140. break;
  141. default:
  142. pr_err("%s: unsupported hash\n", __func__);
  143. break;
  144. }
  145. return hash_size;
  146. }
  147. /* Initialize MD5 digest */
  148. static inline void md5_init(u32 *hash)
  149. {
  150. /* Load magic initialization constants */
  151. hash[0] = 0x67452301;
  152. hash[1] = 0xefcdab89;
  153. hash[2] = 0x98badcfe;
  154. hash[3] = 0x10325476;
  155. }
  156. /* Generate HMAC-MD5 intermediate Hash */
  157. static void sa_hmac_md5_get_pad(const u8 *key, u16 key_sz, u32 *ipad, u32 *opad)
  158. {
  159. u8 k_ipad[MD5_MESSAGE_BYTES];
  160. u8 k_opad[MD5_MESSAGE_BYTES];
  161. int i;
  162. for (i = 0; i < key_sz; i++) {
  163. k_ipad[i] = key[i] ^ 0x36;
  164. k_opad[i] = key[i] ^ 0x5c;
  165. }
  166. /* Instead of XOR with 0 */
  167. for (; i < SHA_MESSAGE_BYTES; i++) {
  168. k_ipad[i] = 0x36;
  169. k_opad[i] = 0x5c;
  170. }
  171. /* SHA-1 on k_ipad */
  172. md5_init(ipad);
  173. md5_transform(ipad, (u32 *)k_ipad);
  174. /* SHA-1 on k_opad */
  175. md5_init(opad);
  176. md5_transform(ipad, (u32 *)k_opad);
  177. }
  178. /* Generate HMAC-SHA1 intermediate Hash */
  179. static
  180. void sa_hmac_sha1_get_pad(const u8 *key, u16 key_sz, u32 *ipad, u32 *opad)
  181. {
  182. u32 ws[SHA_WORKSPACE_WORDS];
  183. u8 k_ipad[SHA_MESSAGE_BYTES];
  184. u8 k_opad[SHA_MESSAGE_BYTES];
  185. int i;
  186. for (i = 0; i < key_sz; i++) {
  187. k_ipad[i] = key[i] ^ 0x36;
  188. k_opad[i] = key[i] ^ 0x5c;
  189. }
  190. /* Instead of XOR with 0 */
  191. for (; i < SHA_MESSAGE_BYTES; i++) {
  192. k_ipad[i] = 0x36;
  193. k_opad[i] = 0x5c;
  194. }
  195. /* SHA-1 on k_ipad */
  196. sha_init(ipad);
  197. sha_transform(ipad, k_ipad, ws);
  198. for (i = 0; i < SHA_DIGEST_WORDS; i++)
  199. ipad[i] = cpu_to_be32(ipad[i]);
  200. /* SHA-1 on k_opad */
  201. sha_init(opad);
  202. sha_transform(opad, k_opad, ws);
  203. for (i = 0; i < SHA_DIGEST_WORDS; i++)
  204. opad[i] = cpu_to_be32(opad[i]);
  205. }
  206. #define ROTATE(a, n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  207. /*
  208. * FIPS specification refers to right rotations, while our ROTATE macro
  209. * is left one. This is why you might notice that rotation coefficients
  210. * differ from those observed in FIPS document by 32-N...
  211. */
  212. #define Sigma0(x) (ROTATE((x), 30) ^ ROTATE((x), 19) ^ ROTATE((x), 10))
  213. #define Sigma1(x) (ROTATE((x), 26) ^ ROTATE((x), 21) ^ ROTATE((x), 7))
  214. #define sigma0(x) (ROTATE((x), 25) ^ ROTATE((x), 14) ^ ((x)>>3))
  215. #define sigma1(x) (ROTATE((x), 15) ^ ROTATE((x), 13) ^ ((x)>>10))
  216. #define CH(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
  217. #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  218. /* SHA256 constants. Values obtained from RFC4634. */
  219. static const u32 K256[64] = {
  220. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  221. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  222. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  223. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  224. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  225. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  226. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  227. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  228. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  229. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  230. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  231. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  232. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  233. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  234. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  235. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  236. };
  237. /* Obtained from TI SA-LLD */
  238. static const u32 mac_last_word_mask[3] = {0xff000000, 0xffff0000, 0xffffff00};
  239. /* Structure used by SHA2 algorithm */
  240. struct sa_sha2_inst_t {
  241. u32 h[8]; /* H Buffers */
  242. u32 nl, nh;
  243. u32 data[16]; /* 32 bit words in a BLOCK */
  244. u16 num;
  245. u16 md_len;
  246. };
  247. /* Initialize SHA2-256 context. */
  248. static inline void sa_sha256_init(struct sa_sha2_inst_t *inst)
  249. {
  250. /* SHA256 initial hash values. Values obtained from RFC4634. */
  251. inst->h[0] = 0x6a09e667;
  252. inst->h[1] = 0xbb67ae85;
  253. inst->h[2] = 0x3c6ef372;
  254. inst->h[3] = 0xa54ff53a;
  255. inst->h[4] = 0x510e527f;
  256. inst->h[5] = 0x9b05688c;
  257. inst->h[6] = 0x1f83d9ab;
  258. inst->h[7] = 0x5be0cd19;
  259. inst->nl = 0;
  260. inst->nh = 0;
  261. inst->num = 0;
  262. inst->md_len = SHA256_DIGEST_SIZE;
  263. }
  264. /* SHA2 block processing function. */
  265. static inline void sha256_block(struct sa_sha2_inst_t *inst, u32 *p)
  266. {
  267. u32 a, b, c, d, e, f, g, h, s0, s1, T1, T2;
  268. u32 X[16];
  269. int i;
  270. a = inst->h[0];
  271. b = inst->h[1];
  272. c = inst->h[2];
  273. d = inst->h[3];
  274. e = inst->h[4];
  275. f = inst->h[5];
  276. g = inst->h[6];
  277. h = inst->h[7];
  278. for (i = 0; i < 16; i++) {
  279. T1 = X[i] = p[i];
  280. T1 += h + Sigma1(e) + CH(e, f, g) + K256[i];
  281. T2 = Sigma0(a) + MAJ(a, b, c);
  282. h = g;
  283. g = f;
  284. f = e;
  285. e = d + T1;
  286. d = c;
  287. c = b;
  288. b = a;
  289. a = T1 + T2;
  290. }
  291. for (; i < 64; i++) {
  292. s0 = X[(i + 1) & 0x0f];
  293. s0 = sigma0(s0);
  294. s1 = X[(i + 14) & 0x0f];
  295. s1 = sigma1(s1);
  296. T1 = X[i & 0xf] += s0 + s1 + X[(i + 9) & 0xf];
  297. T1 += h + Sigma1(e) + CH(e, f, g) + K256[i];
  298. T2 = Sigma0(a) + MAJ(a, b, c);
  299. h = g;
  300. g = f;
  301. f = e;
  302. e = d + T1;
  303. d = c;
  304. c = b;
  305. b = a;
  306. a = T1 + T2;
  307. }
  308. inst->h[0] += a;
  309. inst->h[1] += b;
  310. inst->h[2] += c;
  311. inst->h[3] += d;
  312. inst->h[4] += e;
  313. inst->h[5] += f;
  314. inst->h[6] += g;
  315. inst->h[7] += h;
  316. }
  317. /* SHA2-256 update function. */
  318. static inline void sa_sha256_update(struct sa_sha2_inst_t *inst,
  319. u8 *data, u32 len)
  320. {
  321. u32 *p;
  322. u16 ew, ec, sw;
  323. u32 l;
  324. u32 offset = 0;
  325. if (len == 0)
  326. return;
  327. l = (inst->nl + (len << 3)) & 0xffffffff;
  328. if (l < inst->nl) /* overflow */
  329. inst->nh++;
  330. inst->nh += (len >> 29);
  331. inst->nl = l;
  332. /*
  333. * We now can process the input data in blocks of SHA_CBLOCK
  334. * chars and save the leftovers to inst->data.
  335. */
  336. p = inst->data;
  337. while (len >= SHA256_BLOCK_SIZE) {
  338. for (sw = (SHA256_BLOCK_SIZE/4); sw; sw--, offset += 4) {
  339. *p++ = SA_MK_U32(data[offset], data[offset + 1],
  340. data[offset + 2], data[offset + 3]);
  341. }
  342. p = inst->data;
  343. sha256_block(inst, p);
  344. len -= SHA256_BLOCK_SIZE;
  345. }
  346. ec = (s16)len;
  347. inst->num = ec;
  348. ew = (ec >> 2);
  349. ec &= 0x03;
  350. for (sw = 0; sw < ew; sw++) {
  351. p[sw] = SA_MK_U32(data[offset], data[offset + 1],
  352. data[offset + 2], data[offset + 3]);
  353. }
  354. if (ec) {
  355. p[sw] = (SA_MK_U32(data[offset], data[offset + 1],
  356. data[offset + 2], data[offset + 3])) &
  357. mac_last_word_mask[ec - 1];
  358. }
  359. }
  360. /* Generate HMAC-SHA256 intermediate Hash */
  361. static inline void sa_hmac_sha256_get_pad(const u8 *key, u16 key_sz,
  362. u32 *ipad, u32 *opad)
  363. {
  364. u16 i;
  365. struct sa_sha2_inst_t sha2_inst;
  366. u8 k_ipad[SHA256_BLOCK_SIZE];
  367. u8 k_opad[SHA256_BLOCK_SIZE];
  368. u8 *key1 = (u8 *)key;
  369. /* assumption is that key_sz will be even number always */
  370. /* set up key xor ipad, opad */
  371. for (i = 0; i < key_sz; i++) {
  372. k_ipad[i] = key1[i] ^ 0x36;
  373. k_opad[i] = key1[i] ^ 0x5c;
  374. }
  375. /* Instead of XOR with zero */
  376. for (; i < SHA256_BLOCK_SIZE; i++) {
  377. k_ipad[i] = 0x36;
  378. k_opad[i] = 0x5c;
  379. }
  380. /*
  381. * Perform sha1 on K_ipad
  382. */
  383. /*Init the SHA1 state for 1st pass */
  384. sa_sha256_init(&sha2_inst);
  385. /* start with inner pad k_ipad */
  386. sa_sha256_update(&sha2_inst, (u8 *)k_ipad, SHA256_BLOCK_SIZE);
  387. /* Output the intermediate hash */
  388. for (i = 0; i < 8; i++)
  389. ipad[i] = cpu_to_be32(sha2_inst.h[i]);
  390. /*
  391. * Perform sha1 on K_opad
  392. */
  393. /*Init the SHA1 state for 2nd pass */
  394. sa_sha256_init(&sha2_inst);
  395. /* start with outer pad k_opad */
  396. sa_sha256_update(&sha2_inst, (u8 *)k_opad, SHA256_BLOCK_SIZE);
  397. /* Output the intermediate hash */
  398. for (i = 0; i < 8; i++)
  399. opad[i] = cpu_to_be32(sha2_inst.h[i]);
  400. }
  401. /* Derive GHASH to be used in the GCM algorithm */
  402. static inline void sa_calc_ghash(const u8 *key, u16 key_sz, u8 *ghash)
  403. {
  404. struct AES_KEY enc_key;
  405. if (private_AES_set_encrypt_key(key, key_sz, &enc_key) == -1) {
  406. pr_err("ERROR (%s): failed to set enc key\n", __func__);
  407. return;
  408. }
  409. memset(ghash, 0x00, AES_BLOCK_SIZE);
  410. AES_encrypt(ghash, ghash, &enc_key);
  411. }
  412. /* Derive the inverse key used in AES-CBC decryption operation */
  413. static inline int sa_aes_inv_key(u8 *inv_key, const u8 *key, u16 key_sz)
  414. {
  415. struct crypto_aes_ctx ctx;
  416. int key_pos;
  417. if (crypto_aes_expand_key(&ctx, key, key_sz)) {
  418. pr_err("%s: bad key len(%d)\n", __func__, key_sz);
  419. return -1;
  420. }
  421. /* Refer the implementation of crypto_aes_expand_key()
  422. * to understand the below logic
  423. */
  424. switch (key_sz) {
  425. case AES_KEYSIZE_128:
  426. case AES_KEYSIZE_192:
  427. key_pos = key_sz + 24;
  428. break;
  429. case AES_KEYSIZE_256:
  430. key_pos = key_sz + 24 - 4;
  431. break;
  432. default:
  433. pr_err("%s: bad key len(%d)\n", __func__, key_sz);
  434. return -1;
  435. }
  436. memcpy(inv_key, &ctx.key_enc[key_pos], key_sz);
  437. return 0;
  438. }
  439. /* Set Security context for the encryption engine */
  440. int sa_set_sc_enc(u16 alg_id, const u8 *key, u16 key_sz,
  441. u16 aad_len, u8 enc, u8 *sc_buf)
  442. {
  443. u8 ghash[16]; /* AES block size */
  444. const u8 *mci = NULL;
  445. /* Convert the key size (16/24/32) to the key size index (0/1/2) */
  446. int key_idx = (key_sz >> 3) - 2;
  447. /* Set Encryption mode selector to crypto processing */
  448. sc_buf[0] = 0;
  449. /* Select the mode control instruction */
  450. switch (alg_id) {
  451. case SA_EALG_ID_AES_CBC:
  452. mci = (enc) ? sa_mci_tbl.aes_enc[SA_ENG_ALGO_CBC][key_idx] :
  453. sa_mci_tbl.aes_dec[SA_ENG_ALGO_CBC][key_idx];
  454. break;
  455. case SA_EALG_ID_CCM:
  456. mci = (enc) ? sa_mci_tbl.aes_enc[SA_ENG_ALGO_CCM][key_idx] :
  457. sa_mci_tbl.aes_dec[SA_ENG_ALGO_CCM][key_idx];
  458. break;
  459. case SA_EALG_ID_AES_F8:
  460. mci = sa_mci_tbl.aes_enc[SA_ENG_ALGO_F8][key_idx];
  461. break;
  462. case SA_EALG_ID_AES_CTR:
  463. mci = sa_mci_tbl.aes_enc[SA_ENG_ALGO_CTR][key_idx];
  464. break;
  465. case SA_EALG_ID_GCM:
  466. aad_len = 8; /* Default AAD size is 8 */
  467. mci = (enc) ? sa_mci_tbl.aes_enc[SA_ENG_ALGO_GCM][key_idx] :
  468. sa_mci_tbl.aes_dec[SA_ENG_ALGO_GCM][key_idx];
  469. /* Set AAD length at byte offset 23 in Aux-1 */
  470. sc_buf[SC_ENC_AUX1_OFFSET + 23] = (aad_len << 3);
  471. /* fall through to GMAC for hash */
  472. case SA_AALG_ID_GMAC:
  473. if (alg_id == SA_AALG_ID_GMAC)
  474. mci = sa_mci_tbl.aes_enc[SA_ENG_ALGO_GMAC][key_idx];
  475. sa_calc_ghash(key, (key_sz << 3), ghash);
  476. /* copy GCM Hash in Aux-1 */
  477. memcpy(&sc_buf[SC_ENC_AUX1_OFFSET], ghash, 16);
  478. break;
  479. case SA_AALG_ID_AES_XCBC:
  480. case SA_AALG_ID_CMAC:
  481. mci = sa_mci_tbl.aes_enc[SA_ENG_ALGO_CMAC][key_idx];
  482. break;
  483. case SA_AALG_ID_CBC_MAC:
  484. mci = sa_mci_tbl.aes_enc[SA_ENG_ALGO_CBCMAC][key_idx];
  485. break;
  486. case SA_EALG_ID_3DES_CBC:
  487. mci = (enc) ? sa_mci_tbl._3des_enc[SA_ENG_ALGO_CBC] :
  488. sa_mci_tbl._3des_dec[SA_ENG_ALGO_CBC];
  489. break;
  490. }
  491. /* Set the mode control instructions in security context */
  492. if (mci)
  493. memcpy(&sc_buf[1], mci, 27);
  494. /* For AES-CBC decryption get the inverse key */
  495. if ((alg_id == SA_EALG_ID_AES_CBC) && !enc) {
  496. if (sa_aes_inv_key(&sc_buf[SC_ENC_KEY_OFFSET], key, key_sz))
  497. return -1;
  498. }
  499. /* For AES-XCBC-MAC get the subkey */
  500. else if (alg_id == SA_AALG_ID_AES_XCBC) {
  501. if (sa_aes_xcbc_subkey(&sc_buf[SC_ENC_KEY_OFFSET], NULL,
  502. NULL, key, key_sz))
  503. return -1;
  504. }
  505. /* For all other cases: key is used */
  506. else
  507. memcpy(&sc_buf[SC_ENC_KEY_OFFSET], key, key_sz);
  508. return 0;
  509. }
  510. /* Set Security context for the authentication engine */
  511. void sa_set_sc_auth(u16 alg_id, const u8 *key, u16 key_sz, u8 *sc_buf)
  512. {
  513. u32 ipad[8], opad[8];
  514. u8 mac_sz, keyed_mac = 0;
  515. /* Set Authentication mode selector to hash processing */
  516. sc_buf[0] = 0;
  517. /* Auth SW ctrl word: bit[6]=1 (upload computed hash to TLR section) */
  518. sc_buf[1] = 0x40;
  519. switch (alg_id) {
  520. case SA_AALG_ID_MD5:
  521. /*
  522. * Auth SW ctrl word: bit[4]=1 (basic hash)
  523. * bit[3:0]=1 (MD5 operation)
  524. */
  525. sc_buf[1] |= (0x10 | 0x1);
  526. break;
  527. case SA_AALG_ID_SHA1:
  528. /*
  529. * Auth SW ctrl word: bit[4]=1 (basic hash)
  530. * bit[3:0]=2 (SHA1 operation)
  531. */
  532. sc_buf[1] |= (0x10 | 0x2);
  533. break;
  534. case SA_AALG_ID_SHA2_224:
  535. /*
  536. * Auth SW ctrl word: bit[4]=1 (basic hash)
  537. * bit[3:0]=3 (SHA2-224 operation)
  538. */
  539. sc_buf[1] |= (0x10 | 0x3);
  540. break;
  541. case SA_AALG_ID_SHA2_256:
  542. /*
  543. * Auth SW ctrl word: bit[4]=1 (basic hash)
  544. * bit[3:0]=4 (SHA2-256 operation)
  545. */
  546. sc_buf[1] |= (0x10 | 0x4);
  547. break;
  548. case SA_AALG_ID_HMAC_MD5:
  549. /*
  550. * Auth SW ctrl word: bit[4]=0 (HMAC)
  551. * bit[3:0]=1 (MD5 operation)
  552. */
  553. sc_buf[1] |= 0x1;
  554. keyed_mac = 1;
  555. mac_sz = MD5_DIGEST_SIZE;
  556. sa_hmac_md5_get_pad(key, key_sz, ipad, opad);
  557. break;
  558. case SA_AALG_ID_HMAC_SHA1:
  559. /*
  560. * Auth SW ctrl word: bit[4]=0 (HMAC)
  561. * bit[3:0]=2 (SHA1 operation)
  562. */
  563. sc_buf[1] |= 0x2;
  564. keyed_mac = 1;
  565. mac_sz = SHA1_DIGEST_SIZE;
  566. sa_hmac_sha1_get_pad(key, key_sz, ipad, opad);
  567. break;
  568. case SA_AALG_ID_HMAC_SHA2_224:
  569. /*
  570. * Auth SW ctrl word: bit[4]=0 (HMAC)
  571. * bit[3:0]=3 (SHA2-224 operation)
  572. */
  573. sc_buf[1] |= 0x3;
  574. keyed_mac = 1;
  575. mac_sz = SHA224_DIGEST_SIZE;
  576. break;
  577. case SA_AALG_ID_HMAC_SHA2_256:
  578. /*
  579. * Auth SW ctrl word: bit[4]=0 (HMAC)
  580. * bit[3:0]=4 (SHA2-256 operation)
  581. */
  582. sc_buf[1] |= 0x4;
  583. keyed_mac = 1;
  584. mac_sz = SHA256_DIGEST_SIZE;
  585. sa_hmac_sha256_get_pad(key, key_sz, ipad, opad);
  586. break;
  587. }
  588. /* Copy the keys or ipad/opad */
  589. if (keyed_mac) {
  590. /* Copy ipad to AuthKey */
  591. memcpy(&sc_buf[32], ipad, mac_sz);
  592. /* Copy opad to Aux-1 */
  593. memcpy(&sc_buf[64], opad, mac_sz);
  594. }
  595. }