sha256_glue.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Glue code for SHA256 hashing optimized for sparc64 crypto opcodes.
  2. *
  3. * This is based largely upon crypto/sha256_generic.c
  4. *
  5. * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
  6. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  7. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  8. * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <crypto/internal/hash.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/cryptohash.h>
  16. #include <linux/types.h>
  17. #include <crypto/sha.h>
  18. #include <asm/pstate.h>
  19. #include <asm/elf.h>
  20. #include "opcodes.h"
  21. asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data,
  22. unsigned int rounds);
  23. static int sha224_sparc64_init(struct shash_desc *desc)
  24. {
  25. struct sha256_state *sctx = shash_desc_ctx(desc);
  26. sctx->state[0] = SHA224_H0;
  27. sctx->state[1] = SHA224_H1;
  28. sctx->state[2] = SHA224_H2;
  29. sctx->state[3] = SHA224_H3;
  30. sctx->state[4] = SHA224_H4;
  31. sctx->state[5] = SHA224_H5;
  32. sctx->state[6] = SHA224_H6;
  33. sctx->state[7] = SHA224_H7;
  34. sctx->count = 0;
  35. return 0;
  36. }
  37. static int sha256_sparc64_init(struct shash_desc *desc)
  38. {
  39. struct sha256_state *sctx = shash_desc_ctx(desc);
  40. sctx->state[0] = SHA256_H0;
  41. sctx->state[1] = SHA256_H1;
  42. sctx->state[2] = SHA256_H2;
  43. sctx->state[3] = SHA256_H3;
  44. sctx->state[4] = SHA256_H4;
  45. sctx->state[5] = SHA256_H5;
  46. sctx->state[6] = SHA256_H6;
  47. sctx->state[7] = SHA256_H7;
  48. sctx->count = 0;
  49. return 0;
  50. }
  51. static void __sha256_sparc64_update(struct sha256_state *sctx, const u8 *data,
  52. unsigned int len, unsigned int partial)
  53. {
  54. unsigned int done = 0;
  55. sctx->count += len;
  56. if (partial) {
  57. done = SHA256_BLOCK_SIZE - partial;
  58. memcpy(sctx->buf + partial, data, done);
  59. sha256_sparc64_transform(sctx->state, sctx->buf, 1);
  60. }
  61. if (len - done >= SHA256_BLOCK_SIZE) {
  62. const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
  63. sha256_sparc64_transform(sctx->state, data + done, rounds);
  64. done += rounds * SHA256_BLOCK_SIZE;
  65. }
  66. memcpy(sctx->buf, data + done, len - done);
  67. }
  68. static int sha256_sparc64_update(struct shash_desc *desc, const u8 *data,
  69. unsigned int len)
  70. {
  71. struct sha256_state *sctx = shash_desc_ctx(desc);
  72. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  73. /* Handle the fast case right here */
  74. if (partial + len < SHA256_BLOCK_SIZE) {
  75. sctx->count += len;
  76. memcpy(sctx->buf + partial, data, len);
  77. } else
  78. __sha256_sparc64_update(sctx, data, len, partial);
  79. return 0;
  80. }
  81. static int sha256_sparc64_final(struct shash_desc *desc, u8 *out)
  82. {
  83. struct sha256_state *sctx = shash_desc_ctx(desc);
  84. unsigned int i, index, padlen;
  85. __be32 *dst = (__be32 *)out;
  86. __be64 bits;
  87. static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
  88. bits = cpu_to_be64(sctx->count << 3);
  89. /* Pad out to 56 mod 64 and append length */
  90. index = sctx->count % SHA256_BLOCK_SIZE;
  91. padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56) - index);
  92. /* We need to fill a whole block for __sha256_sparc64_update() */
  93. if (padlen <= 56) {
  94. sctx->count += padlen;
  95. memcpy(sctx->buf + index, padding, padlen);
  96. } else {
  97. __sha256_sparc64_update(sctx, padding, padlen, index);
  98. }
  99. __sha256_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
  100. /* Store state in digest */
  101. for (i = 0; i < 8; i++)
  102. dst[i] = cpu_to_be32(sctx->state[i]);
  103. /* Wipe context */
  104. memset(sctx, 0, sizeof(*sctx));
  105. return 0;
  106. }
  107. static int sha224_sparc64_final(struct shash_desc *desc, u8 *hash)
  108. {
  109. u8 D[SHA256_DIGEST_SIZE];
  110. sha256_sparc64_final(desc, D);
  111. memcpy(hash, D, SHA224_DIGEST_SIZE);
  112. memzero_explicit(D, SHA256_DIGEST_SIZE);
  113. return 0;
  114. }
  115. static int sha256_sparc64_export(struct shash_desc *desc, void *out)
  116. {
  117. struct sha256_state *sctx = shash_desc_ctx(desc);
  118. memcpy(out, sctx, sizeof(*sctx));
  119. return 0;
  120. }
  121. static int sha256_sparc64_import(struct shash_desc *desc, const void *in)
  122. {
  123. struct sha256_state *sctx = shash_desc_ctx(desc);
  124. memcpy(sctx, in, sizeof(*sctx));
  125. return 0;
  126. }
  127. static struct shash_alg sha256 = {
  128. .digestsize = SHA256_DIGEST_SIZE,
  129. .init = sha256_sparc64_init,
  130. .update = sha256_sparc64_update,
  131. .final = sha256_sparc64_final,
  132. .export = sha256_sparc64_export,
  133. .import = sha256_sparc64_import,
  134. .descsize = sizeof(struct sha256_state),
  135. .statesize = sizeof(struct sha256_state),
  136. .base = {
  137. .cra_name = "sha256",
  138. .cra_driver_name= "sha256-sparc64",
  139. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  140. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  141. .cra_blocksize = SHA256_BLOCK_SIZE,
  142. .cra_module = THIS_MODULE,
  143. }
  144. };
  145. static struct shash_alg sha224 = {
  146. .digestsize = SHA224_DIGEST_SIZE,
  147. .init = sha224_sparc64_init,
  148. .update = sha256_sparc64_update,
  149. .final = sha224_sparc64_final,
  150. .descsize = sizeof(struct sha256_state),
  151. .base = {
  152. .cra_name = "sha224",
  153. .cra_driver_name= "sha224-sparc64",
  154. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  155. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  156. .cra_blocksize = SHA224_BLOCK_SIZE,
  157. .cra_module = THIS_MODULE,
  158. }
  159. };
  160. static bool __init sparc64_has_sha256_opcode(void)
  161. {
  162. unsigned long cfr;
  163. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  164. return false;
  165. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  166. if (!(cfr & CFR_SHA256))
  167. return false;
  168. return true;
  169. }
  170. static int __init sha256_sparc64_mod_init(void)
  171. {
  172. if (sparc64_has_sha256_opcode()) {
  173. int ret = crypto_register_shash(&sha224);
  174. if (ret < 0)
  175. return ret;
  176. ret = crypto_register_shash(&sha256);
  177. if (ret < 0) {
  178. crypto_unregister_shash(&sha224);
  179. return ret;
  180. }
  181. pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n");
  182. return 0;
  183. }
  184. pr_info("sparc64 sha256 opcode not available.\n");
  185. return -ENODEV;
  186. }
  187. static void __exit sha256_sparc64_mod_fini(void)
  188. {
  189. crypto_unregister_shash(&sha224);
  190. crypto_unregister_shash(&sha256);
  191. }
  192. module_init(sha256_sparc64_mod_init);
  193. module_exit(sha256_sparc64_mod_fini);
  194. MODULE_LICENSE("GPL");
  195. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, sparc64 sha256 opcode accelerated");
  196. MODULE_ALIAS_CRYPTO("sha224");
  197. MODULE_ALIAS_CRYPTO("sha256");
  198. #include "crop_devid.c"