padlock-sha.c 8.1 KB

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