octeon-sha512.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Cryptographic API.
  3. *
  4. * SHA-512 and SHA-384 Secure Hash Algorithm.
  5. *
  6. * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
  7. *
  8. * Based on crypto/sha512_generic.c, which is:
  9. *
  10. * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
  11. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  12. * Copyright (c) 2003 Kyle McMartin <kyle@debian.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
  16. * Free Software Foundation; either version 2, or (at your option) any
  17. * 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_sha512_store_hash(struct sha512_state *sctx)
  32. {
  33. write_octeon_64bit_hash_sha512(sctx->state[0], 0);
  34. write_octeon_64bit_hash_sha512(sctx->state[1], 1);
  35. write_octeon_64bit_hash_sha512(sctx->state[2], 2);
  36. write_octeon_64bit_hash_sha512(sctx->state[3], 3);
  37. write_octeon_64bit_hash_sha512(sctx->state[4], 4);
  38. write_octeon_64bit_hash_sha512(sctx->state[5], 5);
  39. write_octeon_64bit_hash_sha512(sctx->state[6], 6);
  40. write_octeon_64bit_hash_sha512(sctx->state[7], 7);
  41. }
  42. static void octeon_sha512_read_hash(struct sha512_state *sctx)
  43. {
  44. sctx->state[0] = read_octeon_64bit_hash_sha512(0);
  45. sctx->state[1] = read_octeon_64bit_hash_sha512(1);
  46. sctx->state[2] = read_octeon_64bit_hash_sha512(2);
  47. sctx->state[3] = read_octeon_64bit_hash_sha512(3);
  48. sctx->state[4] = read_octeon_64bit_hash_sha512(4);
  49. sctx->state[5] = read_octeon_64bit_hash_sha512(5);
  50. sctx->state[6] = read_octeon_64bit_hash_sha512(6);
  51. sctx->state[7] = read_octeon_64bit_hash_sha512(7);
  52. }
  53. static void octeon_sha512_transform(const void *_block)
  54. {
  55. const u64 *block = _block;
  56. write_octeon_64bit_block_sha512(block[0], 0);
  57. write_octeon_64bit_block_sha512(block[1], 1);
  58. write_octeon_64bit_block_sha512(block[2], 2);
  59. write_octeon_64bit_block_sha512(block[3], 3);
  60. write_octeon_64bit_block_sha512(block[4], 4);
  61. write_octeon_64bit_block_sha512(block[5], 5);
  62. write_octeon_64bit_block_sha512(block[6], 6);
  63. write_octeon_64bit_block_sha512(block[7], 7);
  64. write_octeon_64bit_block_sha512(block[8], 8);
  65. write_octeon_64bit_block_sha512(block[9], 9);
  66. write_octeon_64bit_block_sha512(block[10], 10);
  67. write_octeon_64bit_block_sha512(block[11], 11);
  68. write_octeon_64bit_block_sha512(block[12], 12);
  69. write_octeon_64bit_block_sha512(block[13], 13);
  70. write_octeon_64bit_block_sha512(block[14], 14);
  71. octeon_sha512_start(block[15]);
  72. }
  73. static int octeon_sha512_init(struct shash_desc *desc)
  74. {
  75. struct sha512_state *sctx = shash_desc_ctx(desc);
  76. sctx->state[0] = SHA512_H0;
  77. sctx->state[1] = SHA512_H1;
  78. sctx->state[2] = SHA512_H2;
  79. sctx->state[3] = SHA512_H3;
  80. sctx->state[4] = SHA512_H4;
  81. sctx->state[5] = SHA512_H5;
  82. sctx->state[6] = SHA512_H6;
  83. sctx->state[7] = SHA512_H7;
  84. sctx->count[0] = sctx->count[1] = 0;
  85. return 0;
  86. }
  87. static int octeon_sha384_init(struct shash_desc *desc)
  88. {
  89. struct sha512_state *sctx = shash_desc_ctx(desc);
  90. sctx->state[0] = SHA384_H0;
  91. sctx->state[1] = SHA384_H1;
  92. sctx->state[2] = SHA384_H2;
  93. sctx->state[3] = SHA384_H3;
  94. sctx->state[4] = SHA384_H4;
  95. sctx->state[5] = SHA384_H5;
  96. sctx->state[6] = SHA384_H6;
  97. sctx->state[7] = SHA384_H7;
  98. sctx->count[0] = sctx->count[1] = 0;
  99. return 0;
  100. }
  101. static void __octeon_sha512_update(struct sha512_state *sctx, const u8 *data,
  102. unsigned int len)
  103. {
  104. unsigned int part_len;
  105. unsigned int index;
  106. unsigned int i;
  107. /* Compute number of bytes mod 128. */
  108. index = sctx->count[0] % SHA512_BLOCK_SIZE;
  109. /* Update number of bytes. */
  110. if ((sctx->count[0] += len) < len)
  111. sctx->count[1]++;
  112. part_len = SHA512_BLOCK_SIZE - index;
  113. /* Transform as many times as possible. */
  114. if (len >= part_len) {
  115. memcpy(&sctx->buf[index], data, part_len);
  116. octeon_sha512_transform(sctx->buf);
  117. for (i = part_len; i + SHA512_BLOCK_SIZE <= len;
  118. i += SHA512_BLOCK_SIZE)
  119. octeon_sha512_transform(&data[i]);
  120. index = 0;
  121. } else {
  122. i = 0;
  123. }
  124. /* Buffer remaining input. */
  125. memcpy(&sctx->buf[index], &data[i], len - i);
  126. }
  127. static int octeon_sha512_update(struct shash_desc *desc, const u8 *data,
  128. unsigned int len)
  129. {
  130. struct sha512_state *sctx = shash_desc_ctx(desc);
  131. struct octeon_cop2_state state;
  132. unsigned long flags;
  133. /*
  134. * Small updates never reach the crypto engine, so the generic sha512 is
  135. * faster because of the heavyweight octeon_crypto_enable() /
  136. * octeon_crypto_disable().
  137. */
  138. if ((sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
  139. return crypto_sha512_update(desc, data, len);
  140. flags = octeon_crypto_enable(&state);
  141. octeon_sha512_store_hash(sctx);
  142. __octeon_sha512_update(sctx, data, len);
  143. octeon_sha512_read_hash(sctx);
  144. octeon_crypto_disable(&state, flags);
  145. return 0;
  146. }
  147. static int octeon_sha512_final(struct shash_desc *desc, u8 *hash)
  148. {
  149. struct sha512_state *sctx = shash_desc_ctx(desc);
  150. static u8 padding[128] = { 0x80, };
  151. struct octeon_cop2_state state;
  152. __be64 *dst = (__be64 *)hash;
  153. unsigned int pad_len;
  154. unsigned long flags;
  155. unsigned int index;
  156. __be64 bits[2];
  157. int i;
  158. /* Save number of bits. */
  159. bits[1] = cpu_to_be64(sctx->count[0] << 3);
  160. bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
  161. /* Pad out to 112 mod 128. */
  162. index = sctx->count[0] & 0x7f;
  163. pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
  164. flags = octeon_crypto_enable(&state);
  165. octeon_sha512_store_hash(sctx);
  166. __octeon_sha512_update(sctx, padding, pad_len);
  167. /* Append length (before padding). */
  168. __octeon_sha512_update(sctx, (const u8 *)bits, sizeof(bits));
  169. octeon_sha512_read_hash(sctx);
  170. octeon_crypto_disable(&state, flags);
  171. /* Store state in digest. */
  172. for (i = 0; i < 8; i++)
  173. dst[i] = cpu_to_be64(sctx->state[i]);
  174. /* Zeroize sensitive information. */
  175. memset(sctx, 0, sizeof(struct sha512_state));
  176. return 0;
  177. }
  178. static int octeon_sha384_final(struct shash_desc *desc, u8 *hash)
  179. {
  180. u8 D[64];
  181. octeon_sha512_final(desc, D);
  182. memcpy(hash, D, 48);
  183. memzero_explicit(D, 64);
  184. return 0;
  185. }
  186. static struct shash_alg octeon_sha512_algs[2] = { {
  187. .digestsize = SHA512_DIGEST_SIZE,
  188. .init = octeon_sha512_init,
  189. .update = octeon_sha512_update,
  190. .final = octeon_sha512_final,
  191. .descsize = sizeof(struct sha512_state),
  192. .base = {
  193. .cra_name = "sha512",
  194. .cra_driver_name= "octeon-sha512",
  195. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  196. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  197. .cra_blocksize = SHA512_BLOCK_SIZE,
  198. .cra_module = THIS_MODULE,
  199. }
  200. }, {
  201. .digestsize = SHA384_DIGEST_SIZE,
  202. .init = octeon_sha384_init,
  203. .update = octeon_sha512_update,
  204. .final = octeon_sha384_final,
  205. .descsize = sizeof(struct sha512_state),
  206. .base = {
  207. .cra_name = "sha384",
  208. .cra_driver_name= "octeon-sha384",
  209. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  210. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  211. .cra_blocksize = SHA384_BLOCK_SIZE,
  212. .cra_module = THIS_MODULE,
  213. }
  214. } };
  215. static int __init octeon_sha512_mod_init(void)
  216. {
  217. if (!octeon_has_crypto())
  218. return -ENOTSUPP;
  219. return crypto_register_shashes(octeon_sha512_algs,
  220. ARRAY_SIZE(octeon_sha512_algs));
  221. }
  222. static void __exit octeon_sha512_mod_fini(void)
  223. {
  224. crypto_unregister_shashes(octeon_sha512_algs,
  225. ARRAY_SIZE(octeon_sha512_algs));
  226. }
  227. module_init(octeon_sha512_mod_init);
  228. module_exit(octeon_sha512_mod_fini);
  229. MODULE_LICENSE("GPL");
  230. MODULE_DESCRIPTION("SHA-512 and SHA-384 Secure Hash Algorithms (OCTEON)");
  231. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");