sha256_glue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
  3. * using optimized ARM assembler and NEON instructions.
  4. *
  5. * Copyright © 2015 Google Inc.
  6. *
  7. * This file is based on sha256_ssse3_glue.c:
  8. * Copyright (C) 2013 Intel Corporation
  9. * Author: Tim Chen <tim.c.chen@linux.intel.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #include <crypto/internal/hash.h>
  18. #include <linux/crypto.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/cryptohash.h>
  23. #include <linux/types.h>
  24. #include <linux/string.h>
  25. #include <crypto/sha.h>
  26. #include <asm/byteorder.h>
  27. #include <asm/simd.h>
  28. #include <asm/neon.h>
  29. #include "sha256_glue.h"
  30. asmlinkage void sha256_block_data_order(u32 *digest, const void *data,
  31. unsigned int num_blks);
  32. int sha256_init(struct shash_desc *desc)
  33. {
  34. struct sha256_state *sctx = shash_desc_ctx(desc);
  35. sctx->state[0] = SHA256_H0;
  36. sctx->state[1] = SHA256_H1;
  37. sctx->state[2] = SHA256_H2;
  38. sctx->state[3] = SHA256_H3;
  39. sctx->state[4] = SHA256_H4;
  40. sctx->state[5] = SHA256_H5;
  41. sctx->state[6] = SHA256_H6;
  42. sctx->state[7] = SHA256_H7;
  43. sctx->count = 0;
  44. return 0;
  45. }
  46. int sha224_init(struct shash_desc *desc)
  47. {
  48. struct sha256_state *sctx = shash_desc_ctx(desc);
  49. sctx->state[0] = SHA224_H0;
  50. sctx->state[1] = SHA224_H1;
  51. sctx->state[2] = SHA224_H2;
  52. sctx->state[3] = SHA224_H3;
  53. sctx->state[4] = SHA224_H4;
  54. sctx->state[5] = SHA224_H5;
  55. sctx->state[6] = SHA224_H6;
  56. sctx->state[7] = SHA224_H7;
  57. sctx->count = 0;
  58. return 0;
  59. }
  60. int __sha256_update(struct shash_desc *desc, const u8 *data, unsigned int len,
  61. unsigned int partial)
  62. {
  63. struct sha256_state *sctx = shash_desc_ctx(desc);
  64. unsigned int done = 0;
  65. sctx->count += len;
  66. if (partial) {
  67. done = SHA256_BLOCK_SIZE - partial;
  68. memcpy(sctx->buf + partial, data, done);
  69. sha256_block_data_order(sctx->state, sctx->buf, 1);
  70. }
  71. if (len - done >= SHA256_BLOCK_SIZE) {
  72. const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
  73. sha256_block_data_order(sctx->state, data + done, rounds);
  74. done += rounds * SHA256_BLOCK_SIZE;
  75. }
  76. memcpy(sctx->buf, data + done, len - done);
  77. return 0;
  78. }
  79. int sha256_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  80. {
  81. struct sha256_state *sctx = shash_desc_ctx(desc);
  82. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  83. /* Handle the fast case right here */
  84. if (partial + len < SHA256_BLOCK_SIZE) {
  85. sctx->count += len;
  86. memcpy(sctx->buf + partial, data, len);
  87. return 0;
  88. }
  89. return __sha256_update(desc, data, len, partial);
  90. }
  91. /* Add padding and return the message digest. */
  92. static int sha256_final(struct shash_desc *desc, u8 *out)
  93. {
  94. struct sha256_state *sctx = shash_desc_ctx(desc);
  95. unsigned int i, index, padlen;
  96. __be32 *dst = (__be32 *)out;
  97. __be64 bits;
  98. static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
  99. /* save number of bits */
  100. bits = cpu_to_be64(sctx->count << 3);
  101. /* Pad out to 56 mod 64 and append length */
  102. index = sctx->count % SHA256_BLOCK_SIZE;
  103. padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56)-index);
  104. /* We need to fill a whole block for __sha256_update */
  105. if (padlen <= 56) {
  106. sctx->count += padlen;
  107. memcpy(sctx->buf + index, padding, padlen);
  108. } else {
  109. __sha256_update(desc, padding, padlen, index);
  110. }
  111. __sha256_update(desc, (const u8 *)&bits, sizeof(bits), 56);
  112. /* Store state in digest */
  113. for (i = 0; i < 8; i++)
  114. dst[i] = cpu_to_be32(sctx->state[i]);
  115. /* Wipe context */
  116. memset(sctx, 0, sizeof(*sctx));
  117. return 0;
  118. }
  119. static int sha224_final(struct shash_desc *desc, u8 *out)
  120. {
  121. u8 D[SHA256_DIGEST_SIZE];
  122. sha256_final(desc, D);
  123. memcpy(out, D, SHA224_DIGEST_SIZE);
  124. memzero_explicit(D, SHA256_DIGEST_SIZE);
  125. return 0;
  126. }
  127. int sha256_export(struct shash_desc *desc, void *out)
  128. {
  129. struct sha256_state *sctx = shash_desc_ctx(desc);
  130. memcpy(out, sctx, sizeof(*sctx));
  131. return 0;
  132. }
  133. int sha256_import(struct shash_desc *desc, const void *in)
  134. {
  135. struct sha256_state *sctx = shash_desc_ctx(desc);
  136. memcpy(sctx, in, sizeof(*sctx));
  137. return 0;
  138. }
  139. static struct shash_alg algs[] = { {
  140. .digestsize = SHA256_DIGEST_SIZE,
  141. .init = sha256_init,
  142. .update = sha256_update,
  143. .final = sha256_final,
  144. .export = sha256_export,
  145. .import = sha256_import,
  146. .descsize = sizeof(struct sha256_state),
  147. .statesize = sizeof(struct sha256_state),
  148. .base = {
  149. .cra_name = "sha256",
  150. .cra_driver_name = "sha256-asm",
  151. .cra_priority = 150,
  152. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  153. .cra_blocksize = SHA256_BLOCK_SIZE,
  154. .cra_module = THIS_MODULE,
  155. }
  156. }, {
  157. .digestsize = SHA224_DIGEST_SIZE,
  158. .init = sha224_init,
  159. .update = sha256_update,
  160. .final = sha224_final,
  161. .export = sha256_export,
  162. .import = sha256_import,
  163. .descsize = sizeof(struct sha256_state),
  164. .statesize = sizeof(struct sha256_state),
  165. .base = {
  166. .cra_name = "sha224",
  167. .cra_driver_name = "sha224-asm",
  168. .cra_priority = 150,
  169. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  170. .cra_blocksize = SHA224_BLOCK_SIZE,
  171. .cra_module = THIS_MODULE,
  172. }
  173. } };
  174. static int __init sha256_mod_init(void)
  175. {
  176. int res = crypto_register_shashes(algs, ARRAY_SIZE(algs));
  177. if (res < 0)
  178. return res;
  179. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
  180. res = crypto_register_shashes(sha256_neon_algs,
  181. ARRAY_SIZE(sha256_neon_algs));
  182. if (res < 0)
  183. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  184. }
  185. return res;
  186. }
  187. static void __exit sha256_mod_fini(void)
  188. {
  189. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  190. if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
  191. crypto_unregister_shashes(sha256_neon_algs,
  192. ARRAY_SIZE(sha256_neon_algs));
  193. }
  194. module_init(sha256_mod_init);
  195. module_exit(sha256_mod_fini);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm (ARM), including NEON");
  198. MODULE_ALIAS("sha256");