padlock-sha.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for VIA PadLock hardware crypto engine.
  5. *
  6. * Copyright (c) 2006 Michal Ludvig <michal@logix.cz>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <crypto/internal/hash.h>
  15. #include <crypto/sha.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/scatterlist.h>
  23. #include <asm/i387.h>
  24. #include "padlock.h"
  25. #ifdef CONFIG_64BIT
  26. #define STACK_ALIGN 16
  27. #else
  28. #define STACK_ALIGN 4
  29. #endif
  30. struct padlock_sha_desc {
  31. struct shash_desc fallback;
  32. };
  33. struct padlock_sha_ctx {
  34. struct crypto_shash *fallback;
  35. };
  36. static int padlock_sha_init(struct shash_desc *desc)
  37. {
  38. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  39. struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
  40. dctx->fallback.tfm = ctx->fallback;
  41. dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  42. return crypto_shash_init(&dctx->fallback);
  43. }
  44. static int padlock_sha_update(struct shash_desc *desc,
  45. const u8 *data, unsigned int length)
  46. {
  47. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  48. dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  49. return crypto_shash_update(&dctx->fallback, data, length);
  50. }
  51. static int padlock_sha_export(struct shash_desc *desc, void *out)
  52. {
  53. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  54. return crypto_shash_export(&dctx->fallback, out);
  55. }
  56. static int padlock_sha_import(struct shash_desc *desc, const void *in)
  57. {
  58. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  59. struct padlock_sha_ctx *ctx = crypto_shash_ctx(desc->tfm);
  60. dctx->fallback.tfm = ctx->fallback;
  61. dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  62. return crypto_shash_import(&dctx->fallback, in);
  63. }
  64. static inline void padlock_output_block(uint32_t *src,
  65. uint32_t *dst, size_t count)
  66. {
  67. while (count--)
  68. *dst++ = swab32(*src++);
  69. }
  70. static int padlock_sha1_finup(struct shash_desc *desc, const u8 *in,
  71. unsigned int count, u8 *out)
  72. {
  73. /* We can't store directly to *out as it may be unaligned. */
  74. /* BTW Don't reduce the buffer size below 128 Bytes!
  75. * PadLock microcode needs it that big. */
  76. char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  77. ((aligned(STACK_ALIGN)));
  78. char *result = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  79. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  80. struct sha1_state state;
  81. unsigned int space;
  82. unsigned int leftover;
  83. int ts_state;
  84. int err;
  85. dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  86. err = crypto_shash_export(&dctx->fallback, &state);
  87. if (err)
  88. goto out;
  89. if (state.count + count > ULONG_MAX)
  90. return crypto_shash_finup(&dctx->fallback, in, count, out);
  91. leftover = ((state.count - 1) & (SHA1_BLOCK_SIZE - 1)) + 1;
  92. space = SHA1_BLOCK_SIZE - leftover;
  93. if (space) {
  94. if (count > space) {
  95. err = crypto_shash_update(&dctx->fallback, in, space) ?:
  96. crypto_shash_export(&dctx->fallback, &state);
  97. if (err)
  98. goto out;
  99. count -= space;
  100. in += space;
  101. } else {
  102. memcpy(state.buffer + leftover, in, count);
  103. in = state.buffer;
  104. count += leftover;
  105. state.count &= ~(SHA1_BLOCK_SIZE - 1);
  106. }
  107. }
  108. memcpy(result, &state.state, SHA1_DIGEST_SIZE);
  109. /* prevent taking the spurious DNA fault with padlock. */
  110. ts_state = irq_ts_save();
  111. asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
  112. : \
  113. : "c"((unsigned long)state.count + count), \
  114. "a"((unsigned long)state.count), \
  115. "S"(in), "D"(result));
  116. irq_ts_restore(ts_state);
  117. padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
  118. out:
  119. return err;
  120. }
  121. static int padlock_sha1_final(struct shash_desc *desc, u8 *out)
  122. {
  123. u8 buf[4];
  124. return padlock_sha1_finup(desc, buf, 0, out);
  125. }
  126. static int padlock_sha256_finup(struct shash_desc *desc, const u8 *in,
  127. unsigned int count, u8 *out)
  128. {
  129. /* We can't store directly to *out as it may be unaligned. */
  130. /* BTW Don't reduce the buffer size below 128 Bytes!
  131. * PadLock microcode needs it that big. */
  132. char buf[128 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
  133. ((aligned(STACK_ALIGN)));
  134. char *result = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
  135. struct padlock_sha_desc *dctx = shash_desc_ctx(desc);
  136. struct sha256_state state;
  137. unsigned int space;
  138. unsigned int leftover;
  139. int ts_state;
  140. int err;
  141. dctx->fallback.flags = desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
  142. err = crypto_shash_export(&dctx->fallback, &state);
  143. if (err)
  144. goto out;
  145. if (state.count + count > ULONG_MAX)
  146. return crypto_shash_finup(&dctx->fallback, in, count, out);
  147. leftover = ((state.count - 1) & (SHA256_BLOCK_SIZE - 1)) + 1;
  148. space = SHA256_BLOCK_SIZE - leftover;
  149. if (space) {
  150. if (count > space) {
  151. err = crypto_shash_update(&dctx->fallback, in, space) ?:
  152. crypto_shash_export(&dctx->fallback, &state);
  153. if (err)
  154. goto out;
  155. count -= space;
  156. in += space;
  157. } else {
  158. memcpy(state.buf + leftover, in, count);
  159. in = state.buf;
  160. count += leftover;
  161. state.count &= ~(SHA1_BLOCK_SIZE - 1);
  162. }
  163. }
  164. memcpy(result, &state.state, SHA256_DIGEST_SIZE);
  165. /* prevent taking the spurious DNA fault with padlock. */
  166. ts_state = irq_ts_save();
  167. asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
  168. : \
  169. : "c"((unsigned long)state.count + count), \
  170. "a"((unsigned long)state.count), \
  171. "S"(in), "D"(result));
  172. irq_ts_restore(ts_state);
  173. padlock_output_block((uint32_t *)result, (uint32_t *)out, 8);
  174. out:
  175. return err;
  176. }
  177. static int padlock_sha256_final(struct shash_desc *desc, u8 *out)
  178. {
  179. u8 buf[4];
  180. return padlock_sha256_finup(desc, buf, 0, out);
  181. }
  182. static int padlock_cra_init(struct crypto_tfm *tfm)
  183. {
  184. struct crypto_shash *hash = __crypto_shash_cast(tfm);
  185. const char *fallback_driver_name = tfm->__crt_alg->cra_name;
  186. struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  187. struct crypto_shash *fallback_tfm;
  188. int err = -ENOMEM;
  189. /* Allocate a fallback and abort if it failed. */
  190. fallback_tfm = crypto_alloc_shash(fallback_driver_name, 0,
  191. CRYPTO_ALG_NEED_FALLBACK);
  192. if (IS_ERR(fallback_tfm)) {
  193. printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
  194. fallback_driver_name);
  195. err = PTR_ERR(fallback_tfm);
  196. goto out;
  197. }
  198. ctx->fallback = fallback_tfm;
  199. hash->descsize += crypto_shash_descsize(fallback_tfm);
  200. return 0;
  201. out:
  202. return err;
  203. }
  204. static void padlock_cra_exit(struct crypto_tfm *tfm)
  205. {
  206. struct padlock_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  207. crypto_free_shash(ctx->fallback);
  208. }
  209. static struct shash_alg sha1_alg = {
  210. .digestsize = SHA1_DIGEST_SIZE,
  211. .init = padlock_sha_init,
  212. .update = padlock_sha_update,
  213. .finup = padlock_sha1_finup,
  214. .final = padlock_sha1_final,
  215. .export = padlock_sha_export,
  216. .import = padlock_sha_import,
  217. .descsize = sizeof(struct padlock_sha_desc),
  218. .statesize = sizeof(struct sha1_state),
  219. .base = {
  220. .cra_name = "sha1",
  221. .cra_driver_name = "sha1-padlock",
  222. .cra_priority = PADLOCK_CRA_PRIORITY,
  223. .cra_flags = CRYPTO_ALG_TYPE_SHASH |
  224. CRYPTO_ALG_NEED_FALLBACK,
  225. .cra_blocksize = SHA1_BLOCK_SIZE,
  226. .cra_ctxsize = sizeof(struct padlock_sha_ctx),
  227. .cra_module = THIS_MODULE,
  228. .cra_init = padlock_cra_init,
  229. .cra_exit = padlock_cra_exit,
  230. }
  231. };
  232. static struct shash_alg sha256_alg = {
  233. .digestsize = SHA256_DIGEST_SIZE,
  234. .init = padlock_sha_init,
  235. .update = padlock_sha_update,
  236. .finup = padlock_sha256_finup,
  237. .final = padlock_sha256_final,
  238. .export = padlock_sha_export,
  239. .import = padlock_sha_import,
  240. .descsize = sizeof(struct padlock_sha_desc),
  241. .statesize = sizeof(struct sha256_state),
  242. .base = {
  243. .cra_name = "sha256",
  244. .cra_driver_name = "sha256-padlock",
  245. .cra_priority = PADLOCK_CRA_PRIORITY,
  246. .cra_flags = CRYPTO_ALG_TYPE_SHASH |
  247. CRYPTO_ALG_NEED_FALLBACK,
  248. .cra_blocksize = SHA256_BLOCK_SIZE,
  249. .cra_ctxsize = sizeof(struct padlock_sha_ctx),
  250. .cra_module = THIS_MODULE,
  251. .cra_init = padlock_cra_init,
  252. .cra_exit = padlock_cra_exit,
  253. }
  254. };
  255. static int __init padlock_init(void)
  256. {
  257. int rc = -ENODEV;
  258. if (!cpu_has_phe) {
  259. printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
  260. return -ENODEV;
  261. }
  262. if (!cpu_has_phe_enabled) {
  263. printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  264. return -ENODEV;
  265. }
  266. rc = crypto_register_shash(&sha1_alg);
  267. if (rc)
  268. goto out;
  269. rc = crypto_register_shash(&sha256_alg);
  270. if (rc)
  271. goto out_unreg1;
  272. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for SHA1/SHA256 algorithms.\n");
  273. return 0;
  274. out_unreg1:
  275. crypto_unregister_shash(&sha1_alg);
  276. out:
  277. printk(KERN_ERR PFX "VIA PadLock SHA1/SHA256 initialization failed.\n");
  278. return rc;
  279. }
  280. static void __exit padlock_fini(void)
  281. {
  282. crypto_unregister_shash(&sha1_alg);
  283. crypto_unregister_shash(&sha256_alg);
  284. }
  285. module_init(padlock_init);
  286. module_exit(padlock_fini);
  287. MODULE_DESCRIPTION("VIA PadLock SHA1/SHA256 algorithms support.");
  288. MODULE_LICENSE("GPL");
  289. MODULE_AUTHOR("Michal Ludvig");
  290. MODULE_ALIAS("sha1-all");
  291. MODULE_ALIAS("sha256-all");
  292. MODULE_ALIAS("sha1-padlock");
  293. MODULE_ALIAS("sha256-padlock");