octeon-md5.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Cryptographic API.
  3. *
  4. * MD5 Message Digest Algorithm (RFC1321).
  5. *
  6. * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
  7. *
  8. * Based on crypto/md5.c, which is:
  9. *
  10. * Derived from cryptoapi implementation, originally based on the
  11. * public domain implementation written by Colin Plumb in 1993.
  12. *
  13. * Copyright (c) Cryptoapi developers.
  14. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the Free
  18. * Software Foundation; either version 2 of the License, or (at your option)
  19. * any later version.
  20. */
  21. #include <crypto/md5.h>
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <linux/module.h>
  25. #include <linux/string.h>
  26. #include <asm/byteorder.h>
  27. #include <linux/cryptohash.h>
  28. #include <asm/octeon/octeon.h>
  29. #include <crypto/internal/hash.h>
  30. #include "octeon-crypto.h"
  31. /*
  32. * We pass everything as 64-bit. OCTEON can handle misaligned data.
  33. */
  34. static void octeon_md5_store_hash(struct md5_state *ctx)
  35. {
  36. u64 *hash = (u64 *)ctx->hash;
  37. write_octeon_64bit_hash_dword(hash[0], 0);
  38. write_octeon_64bit_hash_dword(hash[1], 1);
  39. }
  40. static void octeon_md5_read_hash(struct md5_state *ctx)
  41. {
  42. u64 *hash = (u64 *)ctx->hash;
  43. hash[0] = read_octeon_64bit_hash_dword(0);
  44. hash[1] = read_octeon_64bit_hash_dword(1);
  45. }
  46. static void octeon_md5_transform(const void *_block)
  47. {
  48. const u64 *block = _block;
  49. write_octeon_64bit_block_dword(block[0], 0);
  50. write_octeon_64bit_block_dword(block[1], 1);
  51. write_octeon_64bit_block_dword(block[2], 2);
  52. write_octeon_64bit_block_dword(block[3], 3);
  53. write_octeon_64bit_block_dword(block[4], 4);
  54. write_octeon_64bit_block_dword(block[5], 5);
  55. write_octeon_64bit_block_dword(block[6], 6);
  56. octeon_md5_start(block[7]);
  57. }
  58. static int octeon_md5_init(struct shash_desc *desc)
  59. {
  60. struct md5_state *mctx = shash_desc_ctx(desc);
  61. mctx->hash[0] = cpu_to_le32(MD5_H0);
  62. mctx->hash[1] = cpu_to_le32(MD5_H1);
  63. mctx->hash[2] = cpu_to_le32(MD5_H2);
  64. mctx->hash[3] = cpu_to_le32(MD5_H3);
  65. mctx->byte_count = 0;
  66. return 0;
  67. }
  68. static int octeon_md5_update(struct shash_desc *desc, const u8 *data,
  69. unsigned int len)
  70. {
  71. struct md5_state *mctx = shash_desc_ctx(desc);
  72. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  73. struct octeon_cop2_state state;
  74. unsigned long flags;
  75. mctx->byte_count += len;
  76. if (avail > len) {
  77. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  78. data, len);
  79. return 0;
  80. }
  81. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data,
  82. avail);
  83. flags = octeon_crypto_enable(&state);
  84. octeon_md5_store_hash(mctx);
  85. octeon_md5_transform(mctx->block);
  86. data += avail;
  87. len -= avail;
  88. while (len >= sizeof(mctx->block)) {
  89. octeon_md5_transform(data);
  90. data += sizeof(mctx->block);
  91. len -= sizeof(mctx->block);
  92. }
  93. octeon_md5_read_hash(mctx);
  94. octeon_crypto_disable(&state, flags);
  95. memcpy(mctx->block, data, len);
  96. return 0;
  97. }
  98. static int octeon_md5_final(struct shash_desc *desc, u8 *out)
  99. {
  100. struct md5_state *mctx = shash_desc_ctx(desc);
  101. const unsigned int offset = mctx->byte_count & 0x3f;
  102. char *p = (char *)mctx->block + offset;
  103. int padding = 56 - (offset + 1);
  104. struct octeon_cop2_state state;
  105. unsigned long flags;
  106. *p++ = 0x80;
  107. flags = octeon_crypto_enable(&state);
  108. octeon_md5_store_hash(mctx);
  109. if (padding < 0) {
  110. memset(p, 0x00, padding + sizeof(u64));
  111. octeon_md5_transform(mctx->block);
  112. p = (char *)mctx->block;
  113. padding = 56;
  114. }
  115. memset(p, 0, padding);
  116. mctx->block[14] = cpu_to_le32(mctx->byte_count << 3);
  117. mctx->block[15] = cpu_to_le32(mctx->byte_count >> 29);
  118. octeon_md5_transform(mctx->block);
  119. octeon_md5_read_hash(mctx);
  120. octeon_crypto_disable(&state, flags);
  121. memcpy(out, mctx->hash, sizeof(mctx->hash));
  122. memset(mctx, 0, sizeof(*mctx));
  123. return 0;
  124. }
  125. static int octeon_md5_export(struct shash_desc *desc, void *out)
  126. {
  127. struct md5_state *ctx = shash_desc_ctx(desc);
  128. memcpy(out, ctx, sizeof(*ctx));
  129. return 0;
  130. }
  131. static int octeon_md5_import(struct shash_desc *desc, const void *in)
  132. {
  133. struct md5_state *ctx = shash_desc_ctx(desc);
  134. memcpy(ctx, in, sizeof(*ctx));
  135. return 0;
  136. }
  137. static struct shash_alg alg = {
  138. .digestsize = MD5_DIGEST_SIZE,
  139. .init = octeon_md5_init,
  140. .update = octeon_md5_update,
  141. .final = octeon_md5_final,
  142. .export = octeon_md5_export,
  143. .import = octeon_md5_import,
  144. .descsize = sizeof(struct md5_state),
  145. .statesize = sizeof(struct md5_state),
  146. .base = {
  147. .cra_name = "md5",
  148. .cra_driver_name= "octeon-md5",
  149. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  150. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  151. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  152. .cra_module = THIS_MODULE,
  153. }
  154. };
  155. static int __init md5_mod_init(void)
  156. {
  157. if (!octeon_has_crypto())
  158. return -ENOTSUPP;
  159. return crypto_register_shash(&alg);
  160. }
  161. static void __exit md5_mod_fini(void)
  162. {
  163. crypto_unregister_shash(&alg);
  164. }
  165. module_init(md5_mod_init);
  166. module_exit(md5_mod_fini);
  167. MODULE_LICENSE("GPL");
  168. MODULE_DESCRIPTION("MD5 Message Digest Algorithm (OCTEON)");
  169. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");