octeon-sha256.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * SHA-224 and SHA-256 Secure Hash Algorithm.
  6. *
  7. * Adapted for OCTEON by Aaro Koskinen <[email protected]>.
  8. *
  9. * Based on crypto/sha256_generic.c, which is:
  10. *
  11. * Copyright (c) Jean-Luc Cooke <[email protected]>
  12. * Copyright (c) Andrew McDonald <[email protected]>
  13. * Copyright (c) 2002 James Morris <[email protected]>
  14. * SHA224 Support Copyright 2007 Intel Corporation <[email protected]>
  15. */
  16. #include <linux/mm.h>
  17. #include <crypto/sha2.h>
  18. #include <crypto/sha256_base.h>
  19. #include <linux/init.h>
  20. #include <linux/types.h>
  21. #include <linux/module.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/octeon/octeon.h>
  24. #include <crypto/internal/hash.h>
  25. #include "octeon-crypto.h"
  26. /*
  27. * We pass everything as 64-bit. OCTEON can handle misaligned data.
  28. */
  29. static void octeon_sha256_store_hash(struct sha256_state *sctx)
  30. {
  31. u64 *hash = (u64 *)sctx->state;
  32. write_octeon_64bit_hash_dword(hash[0], 0);
  33. write_octeon_64bit_hash_dword(hash[1], 1);
  34. write_octeon_64bit_hash_dword(hash[2], 2);
  35. write_octeon_64bit_hash_dword(hash[3], 3);
  36. }
  37. static void octeon_sha256_read_hash(struct sha256_state *sctx)
  38. {
  39. u64 *hash = (u64 *)sctx->state;
  40. hash[0] = read_octeon_64bit_hash_dword(0);
  41. hash[1] = read_octeon_64bit_hash_dword(1);
  42. hash[2] = read_octeon_64bit_hash_dword(2);
  43. hash[3] = read_octeon_64bit_hash_dword(3);
  44. }
  45. static void octeon_sha256_transform(const void *_block)
  46. {
  47. const u64 *block = _block;
  48. write_octeon_64bit_block_dword(block[0], 0);
  49. write_octeon_64bit_block_dword(block[1], 1);
  50. write_octeon_64bit_block_dword(block[2], 2);
  51. write_octeon_64bit_block_dword(block[3], 3);
  52. write_octeon_64bit_block_dword(block[4], 4);
  53. write_octeon_64bit_block_dword(block[5], 5);
  54. write_octeon_64bit_block_dword(block[6], 6);
  55. octeon_sha256_start(block[7]);
  56. }
  57. static void __octeon_sha256_update(struct sha256_state *sctx, const u8 *data,
  58. unsigned int len)
  59. {
  60. unsigned int partial;
  61. unsigned int done;
  62. const u8 *src;
  63. partial = sctx->count % SHA256_BLOCK_SIZE;
  64. sctx->count += len;
  65. done = 0;
  66. src = data;
  67. if ((partial + len) >= SHA256_BLOCK_SIZE) {
  68. if (partial) {
  69. done = -partial;
  70. memcpy(sctx->buf + partial, data,
  71. done + SHA256_BLOCK_SIZE);
  72. src = sctx->buf;
  73. }
  74. do {
  75. octeon_sha256_transform(src);
  76. done += SHA256_BLOCK_SIZE;
  77. src = data + done;
  78. } while (done + SHA256_BLOCK_SIZE <= len);
  79. partial = 0;
  80. }
  81. memcpy(sctx->buf + partial, src, len - done);
  82. }
  83. static int octeon_sha256_update(struct shash_desc *desc, const u8 *data,
  84. unsigned int len)
  85. {
  86. struct sha256_state *sctx = shash_desc_ctx(desc);
  87. struct octeon_cop2_state state;
  88. unsigned long flags;
  89. /*
  90. * Small updates never reach the crypto engine, so the generic sha256 is
  91. * faster because of the heavyweight octeon_crypto_enable() /
  92. * octeon_crypto_disable().
  93. */
  94. if ((sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  95. return crypto_sha256_update(desc, data, len);
  96. flags = octeon_crypto_enable(&state);
  97. octeon_sha256_store_hash(sctx);
  98. __octeon_sha256_update(sctx, data, len);
  99. octeon_sha256_read_hash(sctx);
  100. octeon_crypto_disable(&state, flags);
  101. return 0;
  102. }
  103. static int octeon_sha256_final(struct shash_desc *desc, u8 *out)
  104. {
  105. struct sha256_state *sctx = shash_desc_ctx(desc);
  106. static const u8 padding[64] = { 0x80, };
  107. struct octeon_cop2_state state;
  108. __be32 *dst = (__be32 *)out;
  109. unsigned int pad_len;
  110. unsigned long flags;
  111. unsigned int index;
  112. __be64 bits;
  113. int i;
  114. /* Save number of bits. */
  115. bits = cpu_to_be64(sctx->count << 3);
  116. /* Pad out to 56 mod 64. */
  117. index = sctx->count & 0x3f;
  118. pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
  119. flags = octeon_crypto_enable(&state);
  120. octeon_sha256_store_hash(sctx);
  121. __octeon_sha256_update(sctx, padding, pad_len);
  122. /* Append length (before padding). */
  123. __octeon_sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
  124. octeon_sha256_read_hash(sctx);
  125. octeon_crypto_disable(&state, flags);
  126. /* Store state in digest */
  127. for (i = 0; i < 8; i++)
  128. dst[i] = cpu_to_be32(sctx->state[i]);
  129. /* Zeroize sensitive information. */
  130. memset(sctx, 0, sizeof(*sctx));
  131. return 0;
  132. }
  133. static int octeon_sha224_final(struct shash_desc *desc, u8 *hash)
  134. {
  135. u8 D[SHA256_DIGEST_SIZE];
  136. octeon_sha256_final(desc, D);
  137. memcpy(hash, D, SHA224_DIGEST_SIZE);
  138. memzero_explicit(D, SHA256_DIGEST_SIZE);
  139. return 0;
  140. }
  141. static int octeon_sha256_export(struct shash_desc *desc, void *out)
  142. {
  143. struct sha256_state *sctx = shash_desc_ctx(desc);
  144. memcpy(out, sctx, sizeof(*sctx));
  145. return 0;
  146. }
  147. static int octeon_sha256_import(struct shash_desc *desc, const void *in)
  148. {
  149. struct sha256_state *sctx = shash_desc_ctx(desc);
  150. memcpy(sctx, in, sizeof(*sctx));
  151. return 0;
  152. }
  153. static struct shash_alg octeon_sha256_algs[2] = { {
  154. .digestsize = SHA256_DIGEST_SIZE,
  155. .init = sha256_base_init,
  156. .update = octeon_sha256_update,
  157. .final = octeon_sha256_final,
  158. .export = octeon_sha256_export,
  159. .import = octeon_sha256_import,
  160. .descsize = sizeof(struct sha256_state),
  161. .statesize = sizeof(struct sha256_state),
  162. .base = {
  163. .cra_name = "sha256",
  164. .cra_driver_name= "octeon-sha256",
  165. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  166. .cra_blocksize = SHA256_BLOCK_SIZE,
  167. .cra_module = THIS_MODULE,
  168. }
  169. }, {
  170. .digestsize = SHA224_DIGEST_SIZE,
  171. .init = sha224_base_init,
  172. .update = octeon_sha256_update,
  173. .final = octeon_sha224_final,
  174. .descsize = sizeof(struct sha256_state),
  175. .base = {
  176. .cra_name = "sha224",
  177. .cra_driver_name= "octeon-sha224",
  178. .cra_blocksize = SHA224_BLOCK_SIZE,
  179. .cra_module = THIS_MODULE,
  180. }
  181. } };
  182. static int __init octeon_sha256_mod_init(void)
  183. {
  184. if (!octeon_has_crypto())
  185. return -ENOTSUPP;
  186. return crypto_register_shashes(octeon_sha256_algs,
  187. ARRAY_SIZE(octeon_sha256_algs));
  188. }
  189. static void __exit octeon_sha256_mod_fini(void)
  190. {
  191. crypto_unregister_shashes(octeon_sha256_algs,
  192. ARRAY_SIZE(octeon_sha256_algs));
  193. }
  194. module_init(octeon_sha256_mod_init);
  195. module_exit(octeon_sha256_mod_fini);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm (OCTEON)");
  198. MODULE_AUTHOR("Aaro Koskinen <[email protected]>");