padlock-sha.c 8.1 KB

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