octeon-sha256.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Cryptographic API.
  3. *
  4. * SHA-224 and SHA-256 Secure Hash Algorithm.
  5. *
  6. * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>.
  7. *
  8. * Based on crypto/sha256_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) 2002 James Morris <jmorris@intercode.com.au>
  13. * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the Free
  17. * Software Foundation; either version 2 of the License, or (at your option)
  18. * any later version.
  19. */
  20. #include <linux/mm.h>
  21. #include <crypto/sha.h>
  22. #include <linux/init.h>
  23. #include <linux/types.h>
  24. #include <linux/module.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/octeon/octeon.h>
  27. #include <crypto/internal/hash.h>
  28. #include "octeon-crypto.h"
  29. /*
  30. * We pass everything as 64-bit. OCTEON can handle misaligned data.
  31. */
  32. static void octeon_sha256_store_hash(struct sha256_state *sctx)
  33. {
  34. u64 *hash = (u64 *)sctx->state;
  35. write_octeon_64bit_hash_dword(hash[0], 0);
  36. write_octeon_64bit_hash_dword(hash[1], 1);
  37. write_octeon_64bit_hash_dword(hash[2], 2);
  38. write_octeon_64bit_hash_dword(hash[3], 3);
  39. }
  40. static void octeon_sha256_read_hash(struct sha256_state *sctx)
  41. {
  42. u64 *hash = (u64 *)sctx->state;
  43. hash[0] = read_octeon_64bit_hash_dword(0);
  44. hash[1] = read_octeon_64bit_hash_dword(1);
  45. hash[2] = read_octeon_64bit_hash_dword(2);
  46. hash[3] = read_octeon_64bit_hash_dword(3);
  47. }
  48. static void octeon_sha256_transform(const void *_block)
  49. {
  50. const u64 *block = _block;
  51. write_octeon_64bit_block_dword(block[0], 0);
  52. write_octeon_64bit_block_dword(block[1], 1);
  53. write_octeon_64bit_block_dword(block[2], 2);
  54. write_octeon_64bit_block_dword(block[3], 3);
  55. write_octeon_64bit_block_dword(block[4], 4);
  56. write_octeon_64bit_block_dword(block[5], 5);
  57. write_octeon_64bit_block_dword(block[6], 6);
  58. octeon_sha256_start(block[7]);
  59. }
  60. static int octeon_sha224_init(struct shash_desc *desc)
  61. {
  62. struct sha256_state *sctx = shash_desc_ctx(desc);
  63. sctx->state[0] = SHA224_H0;
  64. sctx->state[1] = SHA224_H1;
  65. sctx->state[2] = SHA224_H2;
  66. sctx->state[3] = SHA224_H3;
  67. sctx->state[4] = SHA224_H4;
  68. sctx->state[5] = SHA224_H5;
  69. sctx->state[6] = SHA224_H6;
  70. sctx->state[7] = SHA224_H7;
  71. sctx->count = 0;
  72. return 0;
  73. }
  74. static int octeon_sha256_init(struct shash_desc *desc)
  75. {
  76. struct sha256_state *sctx = shash_desc_ctx(desc);
  77. sctx->state[0] = SHA256_H0;
  78. sctx->state[1] = SHA256_H1;
  79. sctx->state[2] = SHA256_H2;
  80. sctx->state[3] = SHA256_H3;
  81. sctx->state[4] = SHA256_H4;
  82. sctx->state[5] = SHA256_H5;
  83. sctx->state[6] = SHA256_H6;
  84. sctx->state[7] = SHA256_H7;
  85. sctx->count = 0;
  86. return 0;
  87. }
  88. static void __octeon_sha256_update(struct sha256_state *sctx, const u8 *data,
  89. unsigned int len)
  90. {
  91. unsigned int partial;
  92. unsigned int done;
  93. const u8 *src;
  94. partial = sctx->count % SHA256_BLOCK_SIZE;
  95. sctx->count += len;
  96. done = 0;
  97. src = data;
  98. if ((partial + len) >= SHA256_BLOCK_SIZE) {
  99. if (partial) {
  100. done = -partial;
  101. memcpy(sctx->buf + partial, data,
  102. done + SHA256_BLOCK_SIZE);
  103. src = sctx->buf;
  104. }
  105. do {
  106. octeon_sha256_transform(src);
  107. done += SHA256_BLOCK_SIZE;
  108. src = data + done;
  109. } while (done + SHA256_BLOCK_SIZE <= len);
  110. partial = 0;
  111. }
  112. memcpy(sctx->buf + partial, src, len - done);
  113. }
  114. static int octeon_sha256_update(struct shash_desc *desc, const u8 *data,
  115. unsigned int len)
  116. {
  117. struct sha256_state *sctx = shash_desc_ctx(desc);
  118. struct octeon_cop2_state state;
  119. unsigned long flags;
  120. /*
  121. * Small updates never reach the crypto engine, so the generic sha256 is
  122. * faster because of the heavyweight octeon_crypto_enable() /
  123. * octeon_crypto_disable().
  124. */
  125. if ((sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
  126. return crypto_sha256_update(desc, data, len);
  127. flags = octeon_crypto_enable(&state);
  128. octeon_sha256_store_hash(sctx);
  129. __octeon_sha256_update(sctx, data, len);
  130. octeon_sha256_read_hash(sctx);
  131. octeon_crypto_disable(&state, flags);
  132. return 0;
  133. }
  134. static int octeon_sha256_final(struct shash_desc *desc, u8 *out)
  135. {
  136. struct sha256_state *sctx = shash_desc_ctx(desc);
  137. static const u8 padding[64] = { 0x80, };
  138. struct octeon_cop2_state state;
  139. __be32 *dst = (__be32 *)out;
  140. unsigned int pad_len;
  141. unsigned long flags;
  142. unsigned int index;
  143. __be64 bits;
  144. int i;
  145. /* Save number of bits. */
  146. bits = cpu_to_be64(sctx->count << 3);
  147. /* Pad out to 56 mod 64. */
  148. index = sctx->count & 0x3f;
  149. pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
  150. flags = octeon_crypto_enable(&state);
  151. octeon_sha256_store_hash(sctx);
  152. __octeon_sha256_update(sctx, padding, pad_len);
  153. /* Append length (before padding). */
  154. __octeon_sha256_update(sctx, (const u8 *)&bits, sizeof(bits));
  155. octeon_sha256_read_hash(sctx);
  156. octeon_crypto_disable(&state, flags);
  157. /* Store state in digest */
  158. for (i = 0; i < 8; i++)
  159. dst[i] = cpu_to_be32(sctx->state[i]);
  160. /* Zeroize sensitive information. */
  161. memset(sctx, 0, sizeof(*sctx));
  162. return 0;
  163. }
  164. static int octeon_sha224_final(struct shash_desc *desc, u8 *hash)
  165. {
  166. u8 D[SHA256_DIGEST_SIZE];
  167. octeon_sha256_final(desc, D);
  168. memcpy(hash, D, SHA224_DIGEST_SIZE);
  169. memzero_explicit(D, SHA256_DIGEST_SIZE);
  170. return 0;
  171. }
  172. static int octeon_sha256_export(struct shash_desc *desc, void *out)
  173. {
  174. struct sha256_state *sctx = shash_desc_ctx(desc);
  175. memcpy(out, sctx, sizeof(*sctx));
  176. return 0;
  177. }
  178. static int octeon_sha256_import(struct shash_desc *desc, const void *in)
  179. {
  180. struct sha256_state *sctx = shash_desc_ctx(desc);
  181. memcpy(sctx, in, sizeof(*sctx));
  182. return 0;
  183. }
  184. static struct shash_alg octeon_sha256_algs[2] = { {
  185. .digestsize = SHA256_DIGEST_SIZE,
  186. .init = octeon_sha256_init,
  187. .update = octeon_sha256_update,
  188. .final = octeon_sha256_final,
  189. .export = octeon_sha256_export,
  190. .import = octeon_sha256_import,
  191. .descsize = sizeof(struct sha256_state),
  192. .statesize = sizeof(struct sha256_state),
  193. .base = {
  194. .cra_name = "sha256",
  195. .cra_driver_name= "octeon-sha256",
  196. .cra_priority = OCTEON_CR_OPCODE_PRIORITY,
  197. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  198. .cra_blocksize = SHA256_BLOCK_SIZE,
  199. .cra_module = THIS_MODULE,
  200. }
  201. }, {
  202. .digestsize = SHA224_DIGEST_SIZE,
  203. .init = octeon_sha224_init,
  204. .update = octeon_sha256_update,
  205. .final = octeon_sha224_final,
  206. .descsize = sizeof(struct sha256_state),
  207. .base = {
  208. .cra_name = "sha224",
  209. .cra_driver_name= "octeon-sha224",
  210. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  211. .cra_blocksize = SHA224_BLOCK_SIZE,
  212. .cra_module = THIS_MODULE,
  213. }
  214. } };
  215. static int __init octeon_sha256_mod_init(void)
  216. {
  217. if (!octeon_has_crypto())
  218. return -ENOTSUPP;
  219. return crypto_register_shashes(octeon_sha256_algs,
  220. ARRAY_SIZE(octeon_sha256_algs));
  221. }
  222. static void __exit octeon_sha256_mod_fini(void)
  223. {
  224. crypto_unregister_shashes(octeon_sha256_algs,
  225. ARRAY_SIZE(octeon_sha256_algs));
  226. }
  227. module_init(octeon_sha256_mod_init);
  228. module_exit(octeon_sha256_mod_fini);
  229. MODULE_LICENSE("GPL");
  230. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm (OCTEON)");
  231. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");