md5_glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Glue code for MD5 hashing optimized for sparc64 crypto opcodes.
  2. *
  3. * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
  4. * and crypto/md5.c which are:
  5. *
  6. * Copyright (c) Alan Smithee.
  7. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  8. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  9. * Copyright (c) Mathias Krause <minipli@googlemail.com>
  10. * Copyright (c) Cryptoapi developers.
  11. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <crypto/internal/hash.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mm.h>
  18. #include <linux/cryptohash.h>
  19. #include <linux/types.h>
  20. #include <crypto/md5.h>
  21. #include <asm/pstate.h>
  22. #include <asm/elf.h>
  23. #include "opcodes.h"
  24. asmlinkage void md5_sparc64_transform(u32 *digest, const char *data,
  25. unsigned int rounds);
  26. static int md5_sparc64_init(struct shash_desc *desc)
  27. {
  28. struct md5_state *mctx = shash_desc_ctx(desc);
  29. mctx->hash[0] = cpu_to_le32(MD5_H0);
  30. mctx->hash[1] = cpu_to_le32(MD5_H1);
  31. mctx->hash[2] = cpu_to_le32(MD5_H2);
  32. mctx->hash[3] = cpu_to_le32(MD5_H3);
  33. mctx->byte_count = 0;
  34. return 0;
  35. }
  36. static void __md5_sparc64_update(struct md5_state *sctx, const u8 *data,
  37. unsigned int len, unsigned int partial)
  38. {
  39. unsigned int done = 0;
  40. sctx->byte_count += len;
  41. if (partial) {
  42. done = MD5_HMAC_BLOCK_SIZE - partial;
  43. memcpy((u8 *)sctx->block + partial, data, done);
  44. md5_sparc64_transform(sctx->hash, (u8 *)sctx->block, 1);
  45. }
  46. if (len - done >= MD5_HMAC_BLOCK_SIZE) {
  47. const unsigned int rounds = (len - done) / MD5_HMAC_BLOCK_SIZE;
  48. md5_sparc64_transform(sctx->hash, data + done, rounds);
  49. done += rounds * MD5_HMAC_BLOCK_SIZE;
  50. }
  51. memcpy(sctx->block, data + done, len - done);
  52. }
  53. static int md5_sparc64_update(struct shash_desc *desc, const u8 *data,
  54. unsigned int len)
  55. {
  56. struct md5_state *sctx = shash_desc_ctx(desc);
  57. unsigned int partial = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
  58. /* Handle the fast case right here */
  59. if (partial + len < MD5_HMAC_BLOCK_SIZE) {
  60. sctx->byte_count += len;
  61. memcpy((u8 *)sctx->block + partial, data, len);
  62. } else
  63. __md5_sparc64_update(sctx, data, len, partial);
  64. return 0;
  65. }
  66. /* Add padding and return the message digest. */
  67. static int md5_sparc64_final(struct shash_desc *desc, u8 *out)
  68. {
  69. struct md5_state *sctx = shash_desc_ctx(desc);
  70. unsigned int i, index, padlen;
  71. u32 *dst = (u32 *)out;
  72. __le64 bits;
  73. static const u8 padding[MD5_HMAC_BLOCK_SIZE] = { 0x80, };
  74. bits = cpu_to_le64(sctx->byte_count << 3);
  75. /* Pad out to 56 mod 64 and append length */
  76. index = sctx->byte_count % MD5_HMAC_BLOCK_SIZE;
  77. padlen = (index < 56) ? (56 - index) : ((MD5_HMAC_BLOCK_SIZE+56) - index);
  78. /* We need to fill a whole block for __md5_sparc64_update() */
  79. if (padlen <= 56) {
  80. sctx->byte_count += padlen;
  81. memcpy((u8 *)sctx->block + index, padding, padlen);
  82. } else {
  83. __md5_sparc64_update(sctx, padding, padlen, index);
  84. }
  85. __md5_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
  86. /* Store state in digest */
  87. for (i = 0; i < MD5_HASH_WORDS; i++)
  88. dst[i] = sctx->hash[i];
  89. /* Wipe context */
  90. memset(sctx, 0, sizeof(*sctx));
  91. return 0;
  92. }
  93. static int md5_sparc64_export(struct shash_desc *desc, void *out)
  94. {
  95. struct md5_state *sctx = shash_desc_ctx(desc);
  96. memcpy(out, sctx, sizeof(*sctx));
  97. return 0;
  98. }
  99. static int md5_sparc64_import(struct shash_desc *desc, const void *in)
  100. {
  101. struct md5_state *sctx = shash_desc_ctx(desc);
  102. memcpy(sctx, in, sizeof(*sctx));
  103. return 0;
  104. }
  105. static struct shash_alg alg = {
  106. .digestsize = MD5_DIGEST_SIZE,
  107. .init = md5_sparc64_init,
  108. .update = md5_sparc64_update,
  109. .final = md5_sparc64_final,
  110. .export = md5_sparc64_export,
  111. .import = md5_sparc64_import,
  112. .descsize = sizeof(struct md5_state),
  113. .statesize = sizeof(struct md5_state),
  114. .base = {
  115. .cra_name = "md5",
  116. .cra_driver_name= "md5-sparc64",
  117. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  118. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  119. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  120. .cra_module = THIS_MODULE,
  121. }
  122. };
  123. static bool __init sparc64_has_md5_opcode(void)
  124. {
  125. unsigned long cfr;
  126. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  127. return false;
  128. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  129. if (!(cfr & CFR_MD5))
  130. return false;
  131. return true;
  132. }
  133. static int __init md5_sparc64_mod_init(void)
  134. {
  135. if (sparc64_has_md5_opcode()) {
  136. pr_info("Using sparc64 md5 opcode optimized MD5 implementation\n");
  137. return crypto_register_shash(&alg);
  138. }
  139. pr_info("sparc64 md5 opcode not available.\n");
  140. return -ENODEV;
  141. }
  142. static void __exit md5_sparc64_mod_fini(void)
  143. {
  144. crypto_unregister_shash(&alg);
  145. }
  146. module_init(md5_sparc64_mod_init);
  147. module_exit(md5_sparc64_mod_fini);
  148. MODULE_LICENSE("GPL");
  149. MODULE_DESCRIPTION("MD5 Message Digest Algorithm, sparc64 md5 opcode accelerated");
  150. MODULE_ALIAS_CRYPTO("md5");
  151. #include "crop_devid.c"