octeon-sha512.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * SHA-512 and SHA-384 Secure Hash Algorithm.
  6. *
  7. * Adapted for OCTEON by Aaro Koskinen <[email protected]>.
  8. *
  9. * Based on crypto/sha512_generic.c, which is:
  10. *
  11. * Copyright (c) Jean-Luc Cooke <[email protected]>
  12. * Copyright (c) Andrew McDonald <[email protected]>
  13. * Copyright (c) 2003 Kyle McMartin <[email protected]>
  14. */
  15. #include <linux/mm.h>
  16. #include <crypto/sha2.h>
  17. #include <crypto/sha512_base.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/module.h>
  21. #include <asm/byteorder.h>
  22. #include <asm/octeon/octeon.h>
  23. #include <crypto/internal/hash.h>
  24. #include "octeon-crypto.h"
  25. /*
  26. * We pass everything as 64-bit. OCTEON can handle misaligned data.
  27. */
  28. static void octeon_sha512_store_hash(struct sha512_state *sctx)
  29. {
  30. write_octeon_64bit_hash_sha512(sctx->state[0], 0);
  31. write_octeon_64bit_hash_sha512(sctx->state[1], 1);
  32. write_octeon_64bit_hash_sha512(sctx->state[2], 2);
  33. write_octeon_64bit_hash_sha512(sctx->state[3], 3);
  34. write_octeon_64bit_hash_sha512(sctx->state[4], 4);
  35. write_octeon_64bit_hash_sha512(sctx->state[5], 5);
  36. write_octeon_64bit_hash_sha512(sctx->state[6], 6);
  37. write_octeon_64bit_hash_sha512(sctx->state[7], 7);
  38. }
  39. static void octeon_sha512_read_hash(struct sha512_state *sctx)
  40. {
  41. sctx->state[0] = read_octeon_64bit_hash_sha512(0);
  42. sctx->state[1] = read_octeon_64bit_hash_sha512(1);
  43. sctx->state[2] = read_octeon_64bit_hash_sha512(2);
  44. sctx->state[3] = read_octeon_64bit_hash_sha512(3);
  45. sctx->state[4] = read_octeon_64bit_hash_sha512(4);
  46. sctx->state[5] = read_octeon_64bit_hash_sha512(5);
  47. sctx->state[6] = read_octeon_64bit_hash_sha512(6);
  48. sctx->state[7] = read_octeon_64bit_hash_sha512(7);
  49. }
  50. static void octeon_sha512_transform(const void *_block)
  51. {
  52. const u64 *block = _block;
  53. write_octeon_64bit_block_sha512(block[0], 0);
  54. write_octeon_64bit_block_sha512(block[1], 1);
  55. write_octeon_64bit_block_sha512(block[2], 2);
  56. write_octeon_64bit_block_sha512(block[3], 3);
  57. write_octeon_64bit_block_sha512(block[4], 4);
  58. write_octeon_64bit_block_sha512(block[5], 5);
  59. write_octeon_64bit_block_sha512(block[6], 6);
  60. write_octeon_64bit_block_sha512(block[7], 7);
  61. write_octeon_64bit_block_sha512(block[8], 8);
  62. write_octeon_64bit_block_sha512(block[9], 9);
  63. write_octeon_64bit_block_sha512(block[10], 10);
  64. write_octeon_64bit_block_sha512(block[11], 11);
  65. write_octeon_64bit_block_sha512(block[12], 12);
  66. write_octeon_64bit_block_sha512(block[13], 13);
  67. write_octeon_64bit_block_sha512(block[14], 14);
  68. octeon_sha512_start(block[15]);
  69. }
  70. static void __octeon_sha512_update(struct sha512_state *sctx, const u8 *data,
  71. unsigned int len)
  72. {
  73. unsigned int part_len;
  74. unsigned int index;
  75. unsigned int i;
  76. /* Compute number of bytes mod 128. */
  77. index = sctx->count[0] % SHA512_BLOCK_SIZE;
  78. /* Update number of bytes. */
  79. if ((sctx->count[0] += len) < len)
  80. sctx->count[1]++;
  81. part_len = SHA512_BLOCK_SIZE - index;
  82. /* Transform as many times as possible. */
  83. if (len >= part_len) {
  84. memcpy(&sctx->buf[index], data, part_len);
  85. octeon_sha512_transform(sctx->buf);
  86. for (i = part_len; i + SHA512_BLOCK_SIZE <= len;
  87. i += SHA512_BLOCK_SIZE)
  88. octeon_sha512_transform(&data[i]);
  89. index = 0;
  90. } else {
  91. i = 0;
  92. }
  93. /* Buffer remaining input. */
  94. memcpy(&sctx->buf[index], &data[i], len - i);
  95. }
  96. static int octeon_sha512_update(struct shash_desc *desc, const u8 *data,
  97. unsigned int len)
  98. {
  99. struct sha512_state *sctx = shash_desc_ctx(desc);
  100. struct octeon_cop2_state state;
  101. unsigned long flags;
  102. /*
  103. * Small updates never reach the crypto engine, so the generic sha512 is
  104. * faster because of the heavyweight octeon_crypto_enable() /
  105. * octeon_crypto_disable().
  106. */
  107. if ((sctx->count[0] % SHA512_BLOCK_SIZE) + len < SHA512_BLOCK_SIZE)
  108. return crypto_sha512_update(desc, data, len);
  109. flags = octeon_crypto_enable(&state);
  110. octeon_sha512_store_hash(sctx);
  111. __octeon_sha512_update(sctx, data, len);
  112. octeon_sha512_read_hash(sctx);
  113. octeon_crypto_disable(&state, flags);
  114. return 0;
  115. }
  116. static int octeon_sha512_final(struct shash_desc *desc, u8 *hash)
  117. {
  118. struct sha512_state *sctx = shash_desc_ctx(desc);
  119. static u8 padding[128] = { 0x80, };
  120. struct octeon_cop2_state state;
  121. __be64 *dst = (__be64 *)hash;
  122. unsigned int pad_len;
  123. unsigned long flags;
  124. unsigned int index;
  125. __be64 bits[2];
  126. int i;
  127. /* Save number of bits. */
  128. bits[1] = cpu_to_be64(sctx->count[0] << 3);
  129. bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
  130. /* Pad out to 112 mod 128. */
  131. index = sctx->count[0] & 0x7f;
  132. pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
  133. flags = octeon_crypto_enable(&state);
  134. octeon_sha512_store_hash(sctx);
  135. __octeon_sha512_update(sctx, padding, pad_len);
  136. /* Append length (before padding). */
  137. __octeon_sha512_update(sctx, (const u8 *)bits, sizeof(bits));
  138. octeon_sha512_read_hash(sctx);
  139. octeon_crypto_disable(&state, flags);
  140. /* Store state in digest. */
  141. for (i = 0; i < 8; i++)
  142. dst[i] = cpu_to_be64(sctx->state[i]);
  143. /* Zeroize sensitive information. */
  144. memset(sctx, 0, sizeof(struct sha512_state));
  145. return 0;
  146. }
  147. static int octeon_sha384_final(struct shash_desc *desc, u8 *hash)
  148. {
  149. u8 D[64];
  150. octeon_sha512_final(desc, D);
  151. memcpy(hash, D, 48);
  152. memzero_explicit(D, 64);
  153. return 0;
  154. }
  155. static struct shash_alg octeon_sha512_algs[2] = { {
  156. .digestsize = SHA512_DIGEST_SIZE,
  157. .init = sha512_base_init,
  158. .update = octeon_sha512_update,
  159. .final = octeon_sha512_final,
  160. .descsize = sizeof(struct sha512_state),
  161. .base = {
  162. .cra_name = "sha512",
  163. .cra_driver_name= "octeon-sha512",
  164. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  165. .cra_blocksize = SHA512_BLOCK_SIZE,
  166. .cra_module = THIS_MODULE,
  167. }
  168. }, {
  169. .digestsize = SHA384_DIGEST_SIZE,
  170. .init = sha384_base_init,
  171. .update = octeon_sha512_update,
  172. .final = octeon_sha384_final,
  173. .descsize = sizeof(struct sha512_state),
  174. .base = {
  175. .cra_name = "sha384",
  176. .cra_driver_name= "octeon-sha384",
  177. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  178. .cra_blocksize = SHA384_BLOCK_SIZE,
  179. .cra_module = THIS_MODULE,
  180. }
  181. } };
  182. static int __init octeon_sha512_mod_init(void)
  183. {
  184. if (!octeon_has_crypto())
  185. return -ENOTSUPP;
  186. return crypto_register_shashes(octeon_sha512_algs,
  187. ARRAY_SIZE(octeon_sha512_algs));
  188. }
  189. static void __exit octeon_sha512_mod_fini(void)
  190. {
  191. crypto_unregister_shashes(octeon_sha512_algs,
  192. ARRAY_SIZE(octeon_sha512_algs));
  193. }
  194. module_init(octeon_sha512_mod_init);
  195. module_exit(octeon_sha512_mod_fini);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("SHA-512 and SHA-384 Secure Hash Algorithms (OCTEON)");
  198. MODULE_AUTHOR("Aaro Koskinen <[email protected]>");