md5-glue.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Glue code for MD5 implementation for PPC assembler
  3. *
  4. * Based on generic implementation.
  5. *
  6. * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  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/byteorder.h>
  22. extern void ppc_md5_transform(u32 *state, const u8 *src, u32 blocks);
  23. static inline void ppc_md5_clear_context(struct md5_state *sctx)
  24. {
  25. int count = sizeof(struct md5_state) >> 2;
  26. u32 *ptr = (u32 *)sctx;
  27. /* make sure we can clear the fast way */
  28. BUILD_BUG_ON(sizeof(struct md5_state) % 4);
  29. do { *ptr++ = 0; } while (--count);
  30. }
  31. static int ppc_md5_init(struct shash_desc *desc)
  32. {
  33. struct md5_state *sctx = shash_desc_ctx(desc);
  34. sctx->hash[0] = MD5_H0;
  35. sctx->hash[1] = MD5_H1;
  36. sctx->hash[2] = MD5_H2;
  37. sctx->hash[3] = MD5_H3;
  38. sctx->byte_count = 0;
  39. return 0;
  40. }
  41. static int ppc_md5_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct md5_state *sctx = shash_desc_ctx(desc);
  45. const unsigned int offset = sctx->byte_count & 0x3f;
  46. unsigned int avail = 64 - offset;
  47. const u8 *src = data;
  48. sctx->byte_count += len;
  49. if (avail > len) {
  50. memcpy((char *)sctx->block + offset, src, len);
  51. return 0;
  52. }
  53. if (offset) {
  54. memcpy((char *)sctx->block + offset, src, avail);
  55. ppc_md5_transform(sctx->hash, (const u8 *)sctx->block, 1);
  56. len -= avail;
  57. src += avail;
  58. }
  59. if (len > 63) {
  60. ppc_md5_transform(sctx->hash, src, len >> 6);
  61. src += len & ~0x3f;
  62. len &= 0x3f;
  63. }
  64. memcpy((char *)sctx->block, src, len);
  65. return 0;
  66. }
  67. static int ppc_md5_final(struct shash_desc *desc, u8 *out)
  68. {
  69. struct md5_state *sctx = shash_desc_ctx(desc);
  70. const unsigned int offset = sctx->byte_count & 0x3f;
  71. const u8 *src = (const u8 *)sctx->block;
  72. u8 *p = (u8 *)src + offset;
  73. int padlen = 55 - offset;
  74. __le64 *pbits = (__le64 *)((char *)sctx->block + 56);
  75. __le32 *dst = (__le32 *)out;
  76. *p++ = 0x80;
  77. if (padlen < 0) {
  78. memset(p, 0x00, padlen + sizeof (u64));
  79. ppc_md5_transform(sctx->hash, src, 1);
  80. p = (char *)sctx->block;
  81. padlen = 56;
  82. }
  83. memset(p, 0, padlen);
  84. *pbits = cpu_to_le64(sctx->byte_count << 3);
  85. ppc_md5_transform(sctx->hash, src, 1);
  86. dst[0] = cpu_to_le32(sctx->hash[0]);
  87. dst[1] = cpu_to_le32(sctx->hash[1]);
  88. dst[2] = cpu_to_le32(sctx->hash[2]);
  89. dst[3] = cpu_to_le32(sctx->hash[3]);
  90. ppc_md5_clear_context(sctx);
  91. return 0;
  92. }
  93. static int ppc_md5_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 ppc_md5_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 = ppc_md5_init,
  108. .update = ppc_md5_update,
  109. .final = ppc_md5_final,
  110. .export = ppc_md5_export,
  111. .import = ppc_md5_import,
  112. .descsize = sizeof(struct md5_state),
  113. .statesize = sizeof(struct md5_state),
  114. .base = {
  115. .cra_name = "md5",
  116. .cra_driver_name= "md5-ppc",
  117. .cra_priority = 200,
  118. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  119. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  120. .cra_module = THIS_MODULE,
  121. }
  122. };
  123. static int __init ppc_md5_mod_init(void)
  124. {
  125. return crypto_register_shash(&alg);
  126. }
  127. static void __exit ppc_md5_mod_fini(void)
  128. {
  129. crypto_unregister_shash(&alg);
  130. }
  131. module_init(ppc_md5_mod_init);
  132. module_exit(ppc_md5_mod_fini);
  133. MODULE_LICENSE("GPL");
  134. MODULE_DESCRIPTION("MD5 Secure Hash Algorithm, PPC assembler");
  135. MODULE_ALIAS_CRYPTO("md5");
  136. MODULE_ALIAS_CRYPTO("md5-ppc");