padlock-sha.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Support for VIA PadLock hardware crypto engine.
  6. *
  7. * Copyright (c) 2006 Michal Ludvig <[email protected]>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <crypto/padlock.h>
  11. #include <crypto/sha1.h>
  12. #include <crypto/sha2.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/scatterlist.h>
  20. #include <asm/cpu_device_id.h>
  21. #include <asm/fpu/api.h>
  22. struct padlock_sha_desc {
  23. struct shash_desc fallback;
  24. };
  25. struct padlock_sha_ctx {
  26. struct crypto_shash *fallback;
  27. };
  28. static int padlock_sha_init(struct shash_desc *desc)
  29. {
  30. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  31. struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
  32. dctx->fallback.tfm = ctx->fallback;
  33. return crypto_shash_init(&dctx->fallback);
  34. }
  35. static int padlock_sha_update(struct shash_desc *desc,
  36. const u8 *data, unsigned int length)
  37. {
  38. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  39. return crypto_shash_update(&dctx->fallback, data, length);
  40. }
  41. static int padlock_sha_export(struct shash_desc *desc, void *out)
  42. {
  43. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  44. return crypto_shash_export(&dctx->fallback, out);
  45. }
  46. static int padlock_sha_import(struct shash_desc *desc, const void *in)
  47. {
  48. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  49. struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
  50. dctx->fallback.tfm = ctx->fallback;
  51. return crypto_shash_import(&dctx->fallback, in);
  52. }
  53. static inline void padlock_output_block(uint32_t *src,
  54. uint32_t *dst, size_t count)
  55. {
  56. while (count--)
  57. *dst++ = swab32(*src++);
  58. }
  59. static int padlock_sha1_finup(struct shash_desc *desc, const u8 *in,
  60. unsigned int count, u8 *out)
  61. {
  62. /* We can't store directly to *out as it may be unaligned. */
  63. /* BTW Don't reduce the buffer size below 128 Bytes!
  64. * PadLock microcode needs it that big. */
  65. char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  66. ((aligned(STACK_ALIGN)));
  67. char *result = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  68. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  69. struct sha1_state state;
  70. unsigned int space;
  71. unsigned int leftover;
  72. int err;
  73. err = crypto_shash_export(&dctx->fallback, &state);
  74. if (err)
  75. goto out;
  76. if (state.count + count > ULONG_MAX)
  77. return crypto_shash_finup(&dctx->fallback, in, count, out);
  78. leftover = ((state.count - 1) & (SHA1_BLOCK_SIZE - 1)) + 1;
  79. space = SHA1_BLOCK_SIZE - leftover;
  80. if (space) {
  81. if (count > space) {
  82. err = crypto_shash_update(&dctx->fallback, in, space) ?:
  83. crypto_shash_export(&dctx->fallback, &state);
  84. if (err)
  85. goto out;
  86. count -= space;
  87. in += space;
  88. } else {
  89. memcpy(state.buffer + leftover, in, count);
  90. in = state.buffer;
  91. count += leftover;
  92. state.count &= ~(SHA1_BLOCK_SIZE - 1);
  93. }
  94. }
  95. memcpy(result, &state.state, SHA1_DIGEST_SIZE);
  96. asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
  97. : \
  98. : "c"((unsigned long)state.count + count), \
  99. "a"((unsigned long)state.count), \
  100. "S"(in), "D"(result));
  101. padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
  102. out:
  103. return err;
  104. }
  105. static int padlock_sha1_final(struct shash_desc *desc, u8 *out)
  106. {
  107. u8 buf[4];
  108. return padlock_sha1_finup(desc, buf, 0, out);
  109. }
  110. static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,
  111. unsigned int count, u8 *out)
  112. {
  113. /* We can't store directly to *out as it may be unaligned. */
  114. /* BTW Don't reduce the buffer size below 128 Bytes!
  115. * PadLock microcode needs it that big. */
  116. char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  117. ((aligned(STACK_ALIGN)));
  118. char *result = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  119. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  120. struct sha256_state state;
  121. unsigned int space;
  122. unsigned int leftover;
  123. int err;
  124. err = crypto_shash_export(&dctx->fallback, &state);
  125. if (err)
  126. goto out;
  127. if (state.count + count > ULONG_MAX)
  128. return crypto_shash_finup(&dctx->fallback, in, count, out);
  129. leftover = ((state.count - 1) & (SHA256_BLOCK_SIZE - 1)) + 1;
  130. space = SHA256_BLOCK_SIZE - leftover;
  131. if (space) {
  132. if (count > space) {
  133. err = crypto_shash_update(&dctx->fallback, in, space) ?:
  134. crypto_shash_export(&dctx->fallback, &state);
  135. if (err)
  136. goto out;
  137. count -= space;
  138. in += space;
  139. } else {
  140. memcpy(state.buf + leftover, in, count);
  141. in = state.buf;
  142. count += leftover;
  143. state.count &= ~(SHA1_BLOCK_SIZE - 1);
  144. }
  145. }
  146. memcpy(result, &state.state, SHA256_DIGEST_SIZE);
  147. asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
  148. : \
  149. : "c"((unsigned long)state.count + count), \
  150. "a"((unsigned long)state.count), \
  151. "S"(in), "D"(result));
  152. padlock_output_block((uint32_t *)result, (uint32_t *)out, 8);
  153. out:
  154. return err;
  155. }
  156. static int padlock_sha256_final(struct shash_desc *desc, u8 *out)
  157. {
  158. u8 buf[4];
  159. return padlock_sha256_finup(desc, buf, 0, out);
  160. }
  161. static int padlock_init_tfm(struct crypto_shash *hash)
  162. {
  163. const char *fallback_driver_name = crypto_shash_alg_name(hash);
  164. struct padlock_sha_ctx *ctx = crypto_shash_ctx(hash);
  165. struct crypto_shash *fallback_tfm;
  166. /* Allocate a fallback and abort if it failed. */
  167. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  168. CRYPTO_ALG_NEED_FALLBACK);
  169. if (IS_ERR(fallback_tfm)) {
  170. printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
  171. fallback_driver_name);
  172. return PTR_ERR(fallback_tfm);
  173. }
  174. ctx->fallback = fallback_tfm;
  175. hash->descsize += crypto_shash_descsize(fallback_tfm);
  176. return 0;
  177. }
  178. static void padlock_exit_tfm(struct crypto_shash *hash)
  179. {
  180. struct padlock_sha_ctx *ctx = crypto_shash_ctx(hash);
  181. crypto_free_shash(ctx->fallback);
  182. }
  183. static struct shash_alg sha1_alg = {
  184. .digestsize = SHA1_DIGEST_SIZE,
  185. .init = padlock_sha_init,
  186. .update = padlock_sha_update,
  187. .finup = padlock_sha1_finup,
  188. .final = padlock_sha1_final,
  189. .export = padlock_sha_export,
  190. .import = padlock_sha_import,
  191. .init_tfm = padlock_init_tfm,
  192. .exit_tfm = padlock_exit_tfm,
  193. .descsize = sizeof(struct padlock_sha_desc),
  194. .statesize = sizeof(struct sha1_state),
  195. .base = {
  196. .cra_name = "sha1",
  197. .cra_driver_name = "sha1-padlock",
  198. .cra_priority = PADLOCK_CRA_PRIORITY,
  199. .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  200. .cra_blocksize = SHA1_BLOCK_SIZE,
  201. .cra_ctxsize = sizeof(struct padlock_sha_ctx),
  202. .cra_module = THIS_MODULE,
  203. }
  204. };
  205. static struct shash_alg sha256_alg = {
  206. .digestsize = SHA256_DIGEST_SIZE,
  207. .init = padlock_sha_init,
  208. .update = padlock_sha_update,
  209. .finup = padlock_sha256_finup,
  210. .final = padlock_sha256_final,
  211. .export = padlock_sha_export,
  212. .import = padlock_sha_import,
  213. .init_tfm = padlock_init_tfm,
  214. .exit_tfm = padlock_exit_tfm,
  215. .descsize = sizeof(struct padlock_sha_desc),
  216. .statesize = sizeof(struct sha256_state),
  217. .base = {
  218. .cra_name = "sha256",
  219. .cra_driver_name = "sha256-padlock",
  220. .cra_priority = PADLOCK_CRA_PRIORITY,
  221. .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
  222. .cra_blocksize = SHA256_BLOCK_SIZE,
  223. .cra_ctxsize = sizeof(struct padlock_sha_ctx),
  224. .cra_module = THIS_MODULE,
  225. }
  226. };
  227. /* Add two shash_alg instance for hardware-implemented *
  228. * multiple-parts hash supported by VIA Nano Processor.*/
  229. static int padlock_sha1_init_nano(struct shash_desc *desc)
  230. {
  231. struct sha1_state *sctx = shash_desc_ctx(desc);
  232. *sctx = (struct sha1_state){
  233. .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  234. };
  235. return 0;
  236. }
  237. static int padlock_sha1_update_nano(struct shash_desc *desc,
  238. const u8 *data, unsigned int len)
  239. {
  240. struct sha1_state *sctx = shash_desc_ctx(desc);
  241. unsigned int partial, done;
  242. const u8 *src;
  243. /*The PHE require the out buffer must 128 bytes and 16-bytes aligned*/
  244. u8 buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  245. ((aligned(STACK_ALIGN)));
  246. u8 *dst = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  247. partial = sctx->count & 0x3f;
  248. sctx->count += len;
  249. done = 0;
  250. src = data;
  251. memcpy(dst, (u8 *)(sctx->state), SHA1_DIGEST_SIZE);
  252. if ((partial + len) >= SHA1_BLOCK_SIZE) {
  253. /* Append the bytes in state's buffer to a block to handle */
  254. if (partial) {
  255. done = -partial;
  256. memcpy(sctx->buffer + partial, data,
  257. done + SHA1_BLOCK_SIZE);
  258. src = sctx->buffer;
  259. asm volatile (".byte 0xf3,0x0f,0xa6,0xc8"
  260. : "+S"(src), "+D"(dst) \
  261. : "a"((long)-1), "c"((unsigned long)1));
  262. done += SHA1_BLOCK_SIZE;
  263. src = data + done;
  264. }
  265. /* Process the left bytes from the input data */
  266. if (len - done >= SHA1_BLOCK_SIZE) {
  267. asm volatile (".byte 0xf3,0x0f,0xa6,0xc8"
  268. : "+S"(src), "+D"(dst)
  269. : "a"((long)-1),
  270. "c"((unsigned long)((len - done) / SHA1_BLOCK_SIZE)));
  271. done += ((len - done) - (len - done) % SHA1_BLOCK_SIZE);
  272. src = data + done;
  273. }
  274. partial = 0;
  275. }
  276. memcpy((u8 *)(sctx->state), dst, SHA1_DIGEST_SIZE);
  277. memcpy(sctx->buffer + partial, src, len - done);
  278. return 0;
  279. }
  280. static int padlock_sha1_final_nano(struct shash_desc *desc, u8 *out)
  281. {
  282. struct sha1_state *state = (struct sha1_state *)shash_desc_ctx(desc);
  283. unsigned int partial, padlen;
  284. __be64 bits;
  285. static const u8 padding[64] = { 0x80, };
  286. bits = cpu_to_be64(state->count << 3);
  287. /* Pad out to 56 mod 64 */
  288. partial = state->count & 0x3f;
  289. padlen = (partial < 56) ? (56 - partial) : ((64+56) - partial);
  290. padlock_sha1_update_nano(desc, padding, padlen);
  291. /* Append length field bytes */
  292. padlock_sha1_update_nano(desc, (const u8 *)&bits, sizeof(bits));
  293. /* Swap to output */
  294. padlock_output_block((uint32_t *)(state->state), (uint32_t *)out, 5);
  295. return 0;
  296. }
  297. static int padlock_sha256_init_nano(struct shash_desc *desc)
  298. {
  299. struct sha256_state *sctx = shash_desc_ctx(desc);
  300. *sctx = (struct sha256_state){
  301. .state = { SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3, \
  302. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7},
  303. };
  304. return 0;
  305. }
  306. static int padlock_sha256_update_nano(struct shash_desc *desc, const u8 *data,
  307. unsigned int len)
  308. {
  309. struct sha256_state *sctx = shash_desc_ctx(desc);
  310. unsigned int partial, done;
  311. const u8 *src;
  312. /*The PHE require the out buffer must 128 bytes and 16-bytes aligned*/
  313. u8 buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  314. ((aligned(STACK_ALIGN)));
  315. u8 *dst = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  316. partial = sctx->count & 0x3f;
  317. sctx->count += len;
  318. done = 0;
  319. src = data;
  320. memcpy(dst, (u8 *)(sctx->state), SHA256_DIGEST_SIZE);
  321. if ((partial + len) >= SHA256_BLOCK_SIZE) {
  322. /* Append the bytes in state's buffer to a block to handle */
  323. if (partial) {
  324. done = -partial;
  325. memcpy(sctx->buf + partial, data,
  326. done + SHA256_BLOCK_SIZE);
  327. src = sctx->buf;
  328. asm volatile (".byte 0xf3,0x0f,0xa6,0xd0"
  329. : "+S"(src), "+D"(dst)
  330. : "a"((long)-1), "c"((unsigned long)1));
  331. done += SHA256_BLOCK_SIZE;
  332. src = data + done;
  333. }
  334. /* Process the left bytes from input data*/
  335. if (len - done >= SHA256_BLOCK_SIZE) {
  336. asm volatile (".byte 0xf3,0x0f,0xa6,0xd0"
  337. : "+S"(src), "+D"(dst)
  338. : "a"((long)-1),
  339. "c"((unsigned long)((len - done) / 64)));
  340. done += ((len - done) - (len - done) % 64);
  341. src = data + done;
  342. }
  343. partial = 0;
  344. }
  345. memcpy((u8 *)(sctx->state), dst, SHA256_DIGEST_SIZE);
  346. memcpy(sctx->buf + partial, src, len - done);
  347. return 0;
  348. }
  349. static int padlock_sha256_final_nano(struct shash_desc *desc, u8 *out)
  350. {
  351. struct sha256_state *state =
  352. (struct sha256_state *)shash_desc_ctx(desc);
  353. unsigned int partial, padlen;
  354. __be64 bits;
  355. static const u8 padding[64] = { 0x80, };
  356. bits = cpu_to_be64(state->count << 3);
  357. /* Pad out to 56 mod 64 */
  358. partial = state->count & 0x3f;
  359. padlen = (partial < 56) ? (56 - partial) : ((64+56) - partial);
  360. padlock_sha256_update_nano(desc, padding, padlen);
  361. /* Append length field bytes */
  362. padlock_sha256_update_nano(desc, (const u8 *)&bits, sizeof(bits));
  363. /* Swap to output */
  364. padlock_output_block((uint32_t *)(state->state), (uint32_t *)out, 8);
  365. return 0;
  366. }
  367. static int padlock_sha_export_nano(struct shash_desc *desc,
  368. void *out)
  369. {
  370. int statesize = crypto_shash_statesize(desc->tfm);
  371. void *sctx = shash_desc_ctx(desc);
  372. memcpy(out, sctx, statesize);
  373. return 0;
  374. }
  375. static int padlock_sha_import_nano(struct shash_desc *desc,
  376. const void *in)
  377. {
  378. int statesize = crypto_shash_statesize(desc->tfm);
  379. void *sctx = shash_desc_ctx(desc);
  380. memcpy(sctx, in, statesize);
  381. return 0;
  382. }
  383. static struct shash_alg sha1_alg_nano = {
  384. .digestsize = SHA1_DIGEST_SIZE,
  385. .init = padlock_sha1_init_nano,
  386. .update = padlock_sha1_update_nano,
  387. .final = padlock_sha1_final_nano,
  388. .export = padlock_sha_export_nano,
  389. .import = padlock_sha_import_nano,
  390. .descsize = sizeof(struct sha1_state),
  391. .statesize = sizeof(struct sha1_state),
  392. .base = {
  393. .cra_name = "sha1",
  394. .cra_driver_name = "sha1-padlock-nano",
  395. .cra_priority = PADLOCK_CRA_PRIORITY,
  396. .cra_blocksize = SHA1_BLOCK_SIZE,
  397. .cra_module = THIS_MODULE,
  398. }
  399. };
  400. static struct shash_alg sha256_alg_nano = {
  401. .digestsize = SHA256_DIGEST_SIZE,
  402. .init = padlock_sha256_init_nano,
  403. .update = padlock_sha256_update_nano,
  404. .final = padlock_sha256_final_nano,
  405. .export = padlock_sha_export_nano,
  406. .import = padlock_sha_import_nano,
  407. .descsize = sizeof(struct sha256_state),
  408. .statesize = sizeof(struct sha256_state),
  409. .base = {
  410. .cra_name = "sha256",
  411. .cra_driver_name = "sha256-padlock-nano",
  412. .cra_priority = PADLOCK_CRA_PRIORITY,
  413. .cra_blocksize = SHA256_BLOCK_SIZE,
  414. .cra_module = THIS_MODULE,
  415. }
  416. };
  417. static const struct x86_cpu_id padlock_sha_ids[] = {
  418. X86_MATCH_FEATURE(X86_FEATURE_PHE, NULL),
  419. {}
  420. };
  421. MODULE_DEVICE_TABLE(x86cpu, padlock_sha_ids);
  422. static int __init padlock_init(void)
  423. {
  424. int rc = -ENODEV;
  425. struct cpuinfo_x86 *c = &cpu_data(0);
  426. struct shash_alg *sha1;
  427. struct shash_alg *sha256;
  428. if (!x86_match_cpu(padlock_sha_ids) || !boot_cpu_has(X86_FEATURE_PHE_EN))
  429. return -ENODEV;
  430. /* Register the newly added algorithm module if on *
  431. * VIA Nano processor, or else just do as before */
  432. if (c->x86_model < 0x0f) {
  433. sha1 = &sha1_alg;
  434. sha256 = &sha256_alg;
  435. } else {
  436. sha1 = &sha1_alg_nano;
  437. sha256 = &sha256_alg_nano;
  438. }
  439. rc = crypto_register_shash(sha1);
  440. if (rc)
  441. goto out;
  442. rc = crypto_register_shash(sha256);
  443. if (rc)
  444. goto out_unreg1;
  445. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for SHA1/SHA256 algorithms.\n");
  446. return 0;
  447. out_unreg1:
  448. crypto_unregister_shash(sha1);
  449. out:
  450. printk(KERN_ERR PFX "VIA PadLock SHA1/SHA256 initialization failed.\n");
  451. return rc;
  452. }
  453. static void __exit padlock_fini(void)
  454. {
  455. struct cpuinfo_x86 *c = &cpu_data(0);
  456. if (c->x86_model >= 0x0f) {
  457. crypto_unregister_shash(&sha1_alg_nano);
  458. crypto_unregister_shash(&sha256_alg_nano);
  459. } else {
  460. crypto_unregister_shash(&sha1_alg);
  461. crypto_unregister_shash(&sha256_alg);
  462. }
  463. }
  464. module_init(padlock_init);
  465. module_exit(padlock_fini);
  466. MODULE_DESCRIPTION("VIA PadLock SHA1/SHA256 algorithms support.");
  467. MODULE_LICENSE("GPL");
  468. MODULE_AUTHOR("Michal Ludvig");
  469. MODULE_ALIAS_CRYPTO("sha1-all");
  470. MODULE_ALIAS_CRYPTO("sha256-all");
  471. MODULE_ALIAS_CRYPTO("sha1-padlock");
  472. MODULE_ALIAS_CRYPTO("sha256-padlock");