sha1_glue.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes.
  2. *
  3. * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
  4. *
  5. * Copyright (c) Alan Smithee.
  6. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  7. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  8. * Copyright (c) Mathias Krause <minipli@googlemail.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 sha1_sparc64_transform(u32 *digest, const char *data,
  22. unsigned int rounds);
  23. static int sha1_sparc64_init(struct shash_desc *desc)
  24. {
  25. struct sha1_state *sctx = shash_desc_ctx(desc);
  26. *sctx = (struct sha1_state){
  27. .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  28. };
  29. return 0;
  30. }
  31. static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data,
  32. unsigned int len, unsigned int partial)
  33. {
  34. unsigned int done = 0;
  35. sctx->count += len;
  36. if (partial) {
  37. done = SHA1_BLOCK_SIZE - partial;
  38. memcpy(sctx->buffer + partial, data, done);
  39. sha1_sparc64_transform(sctx->state, sctx->buffer, 1);
  40. }
  41. if (len - done >= SHA1_BLOCK_SIZE) {
  42. const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
  43. sha1_sparc64_transform(sctx->state, data + done, rounds);
  44. done += rounds * SHA1_BLOCK_SIZE;
  45. }
  46. memcpy(sctx->buffer, data + done, len - done);
  47. }
  48. static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data,
  49. unsigned int len)
  50. {
  51. struct sha1_state *sctx = shash_desc_ctx(desc);
  52. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  53. /* Handle the fast case right here */
  54. if (partial + len < SHA1_BLOCK_SIZE) {
  55. sctx->count += len;
  56. memcpy(sctx->buffer + partial, data, len);
  57. } else
  58. __sha1_sparc64_update(sctx, data, len, partial);
  59. return 0;
  60. }
  61. /* Add padding and return the message digest. */
  62. static int sha1_sparc64_final(struct shash_desc *desc, u8 *out)
  63. {
  64. struct sha1_state *sctx = shash_desc_ctx(desc);
  65. unsigned int i, index, padlen;
  66. __be32 *dst = (__be32 *)out;
  67. __be64 bits;
  68. static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
  69. bits = cpu_to_be64(sctx->count << 3);
  70. /* Pad out to 56 mod 64 and append length */
  71. index = sctx->count % SHA1_BLOCK_SIZE;
  72. padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
  73. /* We need to fill a whole block for __sha1_sparc64_update() */
  74. if (padlen <= 56) {
  75. sctx->count += padlen;
  76. memcpy(sctx->buffer + index, padding, padlen);
  77. } else {
  78. __sha1_sparc64_update(sctx, padding, padlen, index);
  79. }
  80. __sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
  81. /* Store state in digest */
  82. for (i = 0; i < 5; i++)
  83. dst[i] = cpu_to_be32(sctx->state[i]);
  84. /* Wipe context */
  85. memset(sctx, 0, sizeof(*sctx));
  86. return 0;
  87. }
  88. static int sha1_sparc64_export(struct shash_desc *desc, void *out)
  89. {
  90. struct sha1_state *sctx = shash_desc_ctx(desc);
  91. memcpy(out, sctx, sizeof(*sctx));
  92. return 0;
  93. }
  94. static int sha1_sparc64_import(struct shash_desc *desc, const void *in)
  95. {
  96. struct sha1_state *sctx = shash_desc_ctx(desc);
  97. memcpy(sctx, in, sizeof(*sctx));
  98. return 0;
  99. }
  100. static struct shash_alg alg = {
  101. .digestsize = SHA1_DIGEST_SIZE,
  102. .init = sha1_sparc64_init,
  103. .update = sha1_sparc64_update,
  104. .final = sha1_sparc64_final,
  105. .export = sha1_sparc64_export,
  106. .import = sha1_sparc64_import,
  107. .descsize = sizeof(struct sha1_state),
  108. .statesize = sizeof(struct sha1_state),
  109. .base = {
  110. .cra_name = "sha1",
  111. .cra_driver_name= "sha1-sparc64",
  112. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  113. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  114. .cra_blocksize = SHA1_BLOCK_SIZE,
  115. .cra_module = THIS_MODULE,
  116. }
  117. };
  118. static bool __init sparc64_has_sha1_opcode(void)
  119. {
  120. unsigned long cfr;
  121. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  122. return false;
  123. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  124. if (!(cfr & CFR_SHA1))
  125. return false;
  126. return true;
  127. }
  128. static int __init sha1_sparc64_mod_init(void)
  129. {
  130. if (sparc64_has_sha1_opcode()) {
  131. pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n");
  132. return crypto_register_shash(&alg);
  133. }
  134. pr_info("sparc64 sha1 opcode not available.\n");
  135. return -ENODEV;
  136. }
  137. static void __exit sha1_sparc64_mod_fini(void)
  138. {
  139. crypto_unregister_shash(&alg);
  140. }
  141. module_init(sha1_sparc64_mod_init);
  142. module_exit(sha1_sparc64_mod_fini);
  143. MODULE_LICENSE("GPL");
  144. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated");
  145. MODULE_ALIAS_CRYPTO("sha1");
  146. #include "crop_devid.c"