octeon-sha1.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Cryptographic API.
  3. *
  4. * SHA1 Secure Hash Algorithm.
  5. *
  6. * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
  7. *
  8. * Based on crypto/sha1_generic.c, which is:
  9. *
  10. * Copyright (c) Alan Smithee.
  11. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  12. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. */
  19. #include <linux/mm.h>
  20. #include <crypto/sha.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/module.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/octeon/octeon.h>
  26. #include <crypto/internal/hash.h>
  27. #include "octeon-crypto.h"
  28. /*
  29. * We pass everything as 64-bit. OCTEON can handle misaligned data.
  30. */
  31. static void octeon_sha1_store_hash(struct sha1_state *sctx)
  32. {
  33. u64 *hash = (u64 *)sctx->state;
  34. union {
  35. u32 word[2];
  36. u64 dword;
  37. } hash_tail = { { sctx->state[4], } };
  38. write_octeon_64bit_hash_dword(hash[0], 0);
  39. write_octeon_64bit_hash_dword(hash[1], 1);
  40. write_octeon_64bit_hash_dword(hash_tail.dword, 2);
  41. memzero_explicit(&hash_tail.word[0], sizeof(hash_tail.word[0]));
  42. }
  43. static void octeon_sha1_read_hash(struct sha1_state *sctx)
  44. {
  45. u64 *hash = (u64 *)sctx->state;
  46. union {
  47. u32 word[2];
  48. u64 dword;
  49. } hash_tail;
  50. hash[0] = read_octeon_64bit_hash_dword(0);
  51. hash[1] = read_octeon_64bit_hash_dword(1);
  52. hash_tail.dword = read_octeon_64bit_hash_dword(2);
  53. sctx->state[4] = hash_tail.word[0];
  54. memzero_explicit(&hash_tail.dword, sizeof(hash_tail.dword));
  55. }
  56. static void octeon_sha1_transform(const void *_block)
  57. {
  58. const u64 *block = _block;
  59. write_octeon_64bit_block_dword(block[0], 0);
  60. write_octeon_64bit_block_dword(block[1], 1);
  61. write_octeon_64bit_block_dword(block[2], 2);
  62. write_octeon_64bit_block_dword(block[3], 3);
  63. write_octeon_64bit_block_dword(block[4], 4);
  64. write_octeon_64bit_block_dword(block[5], 5);
  65. write_octeon_64bit_block_dword(block[6], 6);
  66. octeon_sha1_start(block[7]);
  67. }
  68. static int octeon_sha1_init(struct shash_desc *desc)
  69. {
  70. struct sha1_state *sctx = shash_desc_ctx(desc);
  71. sctx->state[0] = SHA1_H0;
  72. sctx->state[1] = SHA1_H1;
  73. sctx->state[2] = SHA1_H2;
  74. sctx->state[3] = SHA1_H3;
  75. sctx->state[4] = SHA1_H4;
  76. sctx->count = 0;
  77. return 0;
  78. }
  79. static void __octeon_sha1_update(struct sha1_state *sctx, const u8 *data,
  80. unsigned int len)
  81. {
  82. unsigned int partial;
  83. unsigned int done;
  84. const u8 *src;
  85. partial = sctx->count % SHA1_BLOCK_SIZE;
  86. sctx->count += len;
  87. done = 0;
  88. src = data;
  89. if ((partial + len) >= SHA1_BLOCK_SIZE) {
  90. if (partial) {
  91. done = -partial;
  92. memcpy(sctx->buffer + partial, data,
  93. done + SHA1_BLOCK_SIZE);
  94. src = sctx->buffer;
  95. }
  96. do {
  97. octeon_sha1_transform(src);
  98. done += SHA1_BLOCK_SIZE;
  99. src = data + done;
  100. } while (done + SHA1_BLOCK_SIZE <= len);
  101. partial = 0;
  102. }
  103. memcpy(sctx->buffer + partial, src, len - done);
  104. }
  105. static int octeon_sha1_update(struct shash_desc *desc, const u8 *data,
  106. unsigned int len)
  107. {
  108. struct sha1_state *sctx = shash_desc_ctx(desc);
  109. struct octeon_cop2_state state;
  110. unsigned long flags;
  111. /*
  112. * Small updates never reach the crypto engine, so the generic sha1 is
  113. * faster because of the heavyweight octeon_crypto_enable() /
  114. * octeon_crypto_disable().
  115. */
  116. if ((sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE)
  117. return crypto_sha1_update(desc, data, len);
  118. flags = octeon_crypto_enable(&state);
  119. octeon_sha1_store_hash(sctx);
  120. __octeon_sha1_update(sctx, data, len);
  121. octeon_sha1_read_hash(sctx);
  122. octeon_crypto_disable(&state, flags);
  123. return 0;
  124. }
  125. static int octeon_sha1_final(struct shash_desc *desc, u8 *out)
  126. {
  127. struct sha1_state *sctx = shash_desc_ctx(desc);
  128. static const u8 padding[64] = { 0x80, };
  129. struct octeon_cop2_state state;
  130. __be32 *dst = (__be32 *)out;
  131. unsigned int pad_len;
  132. unsigned long flags;
  133. unsigned int index;
  134. __be64 bits;
  135. int i;
  136. /* Save number of bits. */
  137. bits = cpu_to_be64(sctx->count << 3);
  138. /* Pad out to 56 mod 64. */
  139. index = sctx->count & 0x3f;
  140. pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
  141. flags = octeon_crypto_enable(&state);
  142. octeon_sha1_store_hash(sctx);
  143. __octeon_sha1_update(sctx, padding, pad_len);
  144. /* Append length (before padding). */
  145. __octeon_sha1_update(sctx, (const u8 *)&bits, sizeof(bits));
  146. octeon_sha1_read_hash(sctx);
  147. octeon_crypto_disable(&state, flags);
  148. /* Store state in digest */
  149. for (i = 0; i < 5; i++)
  150. dst[i] = cpu_to_be32(sctx->state[i]);
  151. /* Zeroize sensitive information. */
  152. memset(sctx, 0, sizeof(*sctx));
  153. return 0;
  154. }
  155. static int octeon_sha1_export(struct shash_desc *desc, void *out)
  156. {
  157. struct sha1_state *sctx = shash_desc_ctx(desc);
  158. memcpy(out, sctx, sizeof(*sctx));
  159. return 0;
  160. }
  161. static int octeon_sha1_import(struct shash_desc *desc, const void *in)
  162. {
  163. struct sha1_state *sctx = shash_desc_ctx(desc);
  164. memcpy(sctx, in, sizeof(*sctx));
  165. return 0;
  166. }
  167. static struct shash_alg octeon_sha1_alg = {
  168. .digestsize = SHA1_DIGEST_SIZE,
  169. .init = octeon_sha1_init,
  170. .update = octeon_sha1_update,
  171. .final = octeon_sha1_final,
  172. .export = octeon_sha1_export,
  173. .import = octeon_sha1_import,
  174. .descsize = sizeof(struct sha1_state),
  175. .statesize = sizeof(struct sha1_state),
  176. .base = {
  177. .cra_name = "sha1",
  178. .cra_driver_name= "octeon-sha1",
  179. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  180. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  181. .cra_blocksize = SHA1_BLOCK_SIZE,
  182. .cra_module = THIS_MODULE,
  183. }
  184. };
  185. static int __init octeon_sha1_mod_init(void)
  186. {
  187. if (!octeon_has_crypto())
  188. return -ENOTSUPP;
  189. return crypto_register_shash(&octeon_sha1_alg);
  190. }
  191. static void __exit octeon_sha1_mod_fini(void)
  192. {
  193. crypto_unregister_shash(&octeon_sha1_alg);
  194. }
  195. module_init(octeon_sha1_mod_init);
  196. module_exit(octeon_sha1_mod_fini);
  197. MODULE_LICENSE("GPL");
  198. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (OCTEON)");
  199. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");