padlock-sha.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/algapi.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/cryptohash.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/scatterlist.h>
  24. #include "padlock.h"
  25. #define SHA1_DEFAULT_FALLBACK "sha1-generic"
  26. #define SHA256_DEFAULT_FALLBACK "sha256-generic"
  27. struct padlock_sha_ctx {
  28. char *data;
  29. size_t used;
  30. int bypass;
  31. void (*f_sha_padlock)(const char *in, char *out, int count);
  32. struct hash_desc fallback;
  33. };
  34. static inline struct padlock_sha_ctx *ctx(struct crypto_tfm *tfm)
  35. {
  36. return crypto_tfm_ctx(tfm);
  37. }
  38. /* We'll need aligned address on the stack */
  39. #define NEAREST_ALIGNED(ptr) \
  40. ((void *)ALIGN((size_t)(ptr), PADLOCK_ALIGNMENT))
  41. static struct crypto_alg sha1_alg, sha256_alg;
  42. static void padlock_sha_bypass(struct crypto_tfm *tfm)
  43. {
  44. if (ctx(tfm)->bypass)
  45. return;
  46. crypto_hash_init(&ctx(tfm)->fallback);
  47. if (ctx(tfm)->data && ctx(tfm)->used) {
  48. struct scatterlist sg;
  49. sg_set_buf(&sg, ctx(tfm)->data, ctx(tfm)->used);
  50. crypto_hash_update(&ctx(tfm)->fallback, &sg, sg.length);
  51. }
  52. ctx(tfm)->used = 0;
  53. ctx(tfm)->bypass = 1;
  54. }
  55. static void padlock_sha_init(struct crypto_tfm *tfm)
  56. {
  57. ctx(tfm)->used = 0;
  58. ctx(tfm)->bypass = 0;
  59. }
  60. static void padlock_sha_update(struct crypto_tfm *tfm,
  61. const uint8_t *data, unsigned int length)
  62. {
  63. /* Our buffer is always one page. */
  64. if (unlikely(!ctx(tfm)->bypass &&
  65. (ctx(tfm)->used + length > PAGE_SIZE)))
  66. padlock_sha_bypass(tfm);
  67. if (unlikely(ctx(tfm)->bypass)) {
  68. struct scatterlist sg;
  69. sg_set_buf(&sg, (uint8_t *)data, length);
  70. crypto_hash_update(&ctx(tfm)->fallback, &sg, length);
  71. return;
  72. }
  73. memcpy(ctx(tfm)->data + ctx(tfm)->used, data, length);
  74. ctx(tfm)->used += length;
  75. }
  76. static inline void padlock_output_block(uint32_t *src,
  77. uint32_t *dst, size_t count)
  78. {
  79. while (count--)
  80. *dst++ = swab32(*src++);
  81. }
  82. static void padlock_do_sha1(const char *in, char *out, int count)
  83. {
  84. /* We can't store directly to *out as it may be unaligned. */
  85. /* BTW Don't reduce the buffer size below 128 Bytes!
  86. * PadLock microcode needs it that big. */
  87. char buf[128+16];
  88. char *result = NEAREST_ALIGNED(buf);
  89. ((uint32_t *)result)[0] = SHA1_H0;
  90. ((uint32_t *)result)[1] = SHA1_H1;
  91. ((uint32_t *)result)[2] = SHA1_H2;
  92. ((uint32_t *)result)[3] = SHA1_H3;
  93. ((uint32_t *)result)[4] = SHA1_H4;
  94. asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
  95. : "+S"(in), "+D"(result)
  96. : "c"(count), "a"(0));
  97. padlock_output_block((uint32_t *)result, (uint32_t *)out, 5);
  98. }
  99. static void padlock_do_sha256(const char *in, char *out, int count)
  100. {
  101. /* We can't store directly to *out as it may be unaligned. */
  102. /* BTW Don't reduce the buffer size below 128 Bytes!
  103. * PadLock microcode needs it that big. */
  104. char buf[128+16];
  105. char *result = NEAREST_ALIGNED(buf);
  106. ((uint32_t *)result)[0] = SHA256_H0;
  107. ((uint32_t *)result)[1] = SHA256_H1;
  108. ((uint32_t *)result)[2] = SHA256_H2;
  109. ((uint32_t *)result)[3] = SHA256_H3;
  110. ((uint32_t *)result)[4] = SHA256_H4;
  111. ((uint32_t *)result)[5] = SHA256_H5;
  112. ((uint32_t *)result)[6] = SHA256_H6;
  113. ((uint32_t *)result)[7] = SHA256_H7;
  114. asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
  115. : "+S"(in), "+D"(result)
  116. : "c"(count), "a"(0));
  117. padlock_output_block((uint32_t *)result, (uint32_t *)out, 8);
  118. }
  119. static void padlock_sha_final(struct crypto_tfm *tfm, uint8_t *out)
  120. {
  121. if (unlikely(ctx(tfm)->bypass)) {
  122. crypto_hash_final(&ctx(tfm)->fallback, out);
  123. ctx(tfm)->bypass = 0;
  124. return;
  125. }
  126. /* Pass the input buffer to PadLock microcode... */
  127. ctx(tfm)->f_sha_padlock(ctx(tfm)->data, out, ctx(tfm)->used);
  128. ctx(tfm)->used = 0;
  129. }
  130. static int padlock_cra_init(struct crypto_tfm *tfm)
  131. {
  132. const char *fallback_driver_name = tfm->__crt_alg->cra_name;
  133. struct crypto_hash *fallback_tfm;
  134. /* For now we'll allocate one page. This
  135. * could eventually be configurable one day. */
  136. ctx(tfm)->data = (char *)__get_free_page(GFP_KERNEL);
  137. if (!ctx(tfm)->data)
  138. return -ENOMEM;
  139. /* Allocate a fallback and abort if it failed. */
  140. fallback_tfm = crypto_alloc_hash(fallback_driver_name, 0,
  141. CRYPTO_ALG_ASYNC |
  142. CRYPTO_ALG_NEED_FALLBACK);
  143. if (IS_ERR(fallback_tfm)) {
  144. printk(KERN_WARNING PFX "Fallback driver '%s' could not be loaded!\n",
  145. fallback_driver_name);
  146. free_page((unsigned long)(ctx(tfm)->data));
  147. return PTR_ERR(fallback_tfm);
  148. }
  149. ctx(tfm)->fallback.tfm = fallback_tfm;
  150. return 0;
  151. }
  152. static int padlock_sha1_cra_init(struct crypto_tfm *tfm)
  153. {
  154. ctx(tfm)->f_sha_padlock = padlock_do_sha1;
  155. return padlock_cra_init(tfm);
  156. }
  157. static int padlock_sha256_cra_init(struct crypto_tfm *tfm)
  158. {
  159. ctx(tfm)->f_sha_padlock = padlock_do_sha256;
  160. return padlock_cra_init(tfm);
  161. }
  162. static void padlock_cra_exit(struct crypto_tfm *tfm)
  163. {
  164. if (ctx(tfm)->data) {
  165. free_page((unsigned long)(ctx(tfm)->data));
  166. ctx(tfm)->data = NULL;
  167. }
  168. crypto_free_hash(ctx(tfm)->fallback.tfm);
  169. ctx(tfm)->fallback.tfm = NULL;
  170. }
  171. static struct crypto_alg sha1_alg = {
  172. .cra_name = "sha1",
  173. .cra_driver_name = "sha1-padlock",
  174. .cra_priority = PADLOCK_CRA_PRIORITY,
  175. .cra_flags = CRYPTO_ALG_TYPE_DIGEST |
  176. CRYPTO_ALG_NEED_FALLBACK,
  177. .cra_blocksize = SHA1_BLOCK_SIZE,
  178. .cra_ctxsize = sizeof(struct padlock_sha_ctx),
  179. .cra_module = THIS_MODULE,
  180. .cra_list = LIST_HEAD_INIT(sha1_alg.cra_list),
  181. .cra_init = padlock_sha1_cra_init,
  182. .cra_exit = padlock_cra_exit,
  183. .cra_u = {
  184. .digest = {
  185. .dia_digestsize = SHA1_DIGEST_SIZE,
  186. .dia_init = padlock_sha_init,
  187. .dia_update = padlock_sha_update,
  188. .dia_final = padlock_sha_final,
  189. }
  190. }
  191. };
  192. static struct crypto_alg sha256_alg = {
  193. .cra_name = "sha256",
  194. .cra_driver_name = "sha256-padlock",
  195. .cra_priority = PADLOCK_CRA_PRIORITY,
  196. .cra_flags = CRYPTO_ALG_TYPE_DIGEST |
  197. CRYPTO_ALG_NEED_FALLBACK,
  198. .cra_blocksize = SHA256_BLOCK_SIZE,
  199. .cra_ctxsize = sizeof(struct padlock_sha_ctx),
  200. .cra_module = THIS_MODULE,
  201. .cra_list = LIST_HEAD_INIT(sha256_alg.cra_list),
  202. .cra_init = padlock_sha256_cra_init,
  203. .cra_exit = padlock_cra_exit,
  204. .cra_u = {
  205. .digest = {
  206. .dia_digestsize = SHA256_DIGEST_SIZE,
  207. .dia_init = padlock_sha_init,
  208. .dia_update = padlock_sha_update,
  209. .dia_final = padlock_sha_final,
  210. }
  211. }
  212. };
  213. static int __init padlock_init(void)
  214. {
  215. int rc = -ENODEV;
  216. if (!cpu_has_phe) {
  217. printk(KERN_ERR PFX "VIA PadLock Hash Engine not detected.\n");
  218. return -ENODEV;
  219. }
  220. if (!cpu_has_phe_enabled) {
  221. printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
  222. return -ENODEV;
  223. }
  224. rc = crypto_register_alg(&sha1_alg);
  225. if (rc)
  226. goto out;
  227. rc = crypto_register_alg(&sha256_alg);
  228. if (rc)
  229. goto out_unreg1;
  230. printk(KERN_NOTICE PFX "Using VIA PadLock ACE for SHA1/SHA256 algorithms.\n");
  231. return 0;
  232. out_unreg1:
  233. crypto_unregister_alg(&sha1_alg);
  234. out:
  235. printk(KERN_ERR PFX "VIA PadLock SHA1/SHA256 initialization failed.\n");
  236. return rc;
  237. }
  238. static void __exit padlock_fini(void)
  239. {
  240. crypto_unregister_alg(&sha1_alg);
  241. crypto_unregister_alg(&sha256_alg);
  242. }
  243. module_init(padlock_init);
  244. module_exit(padlock_fini);
  245. MODULE_DESCRIPTION("VIA PadLock SHA1/SHA256 algorithms support.");
  246. MODULE_LICENSE("GPL");
  247. MODULE_AUTHOR("Michal Ludvig");
  248. MODULE_ALIAS("sha1");
  249. MODULE_ALIAS("sha256");
  250. MODULE_ALIAS("sha1-padlock");
  251. MODULE_ALIAS("sha256-padlock");