sha1_neon_glue.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using
  3. * ARM NEON instructions.
  4. *
  5. * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  6. *
  7. * This file is based on sha1_generic.c and sha1_ssse3_glue.c:
  8. * Copyright (c) Alan Smithee.
  9. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  10. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  11. * Copyright (c) Mathias Krause <minipli@googlemail.com>
  12. * Copyright (c) Chandramouli Narayanan <mouli@linux.intel.com>
  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 Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. */
  20. #include <crypto/internal/hash.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.h>
  24. #include <linux/cryptohash.h>
  25. #include <linux/types.h>
  26. #include <crypto/sha.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/neon.h>
  29. #include <asm/simd.h>
  30. #include <asm/crypto/sha1.h>
  31. asmlinkage void sha1_transform_neon(void *state_h, const char *data,
  32. unsigned int rounds);
  33. static int sha1_neon_init(struct shash_desc *desc)
  34. {
  35. struct sha1_state *sctx = shash_desc_ctx(desc);
  36. *sctx = (struct sha1_state){
  37. .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  38. };
  39. return 0;
  40. }
  41. static int __sha1_neon_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len, unsigned int partial)
  43. {
  44. struct sha1_state *sctx = shash_desc_ctx(desc);
  45. unsigned int done = 0;
  46. sctx->count += len;
  47. if (partial) {
  48. done = SHA1_BLOCK_SIZE - partial;
  49. memcpy(sctx->buffer + partial, data, done);
  50. sha1_transform_neon(sctx->state, sctx->buffer, 1);
  51. }
  52. if (len - done >= SHA1_BLOCK_SIZE) {
  53. const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
  54. sha1_transform_neon(sctx->state, data + done, rounds);
  55. done += rounds * SHA1_BLOCK_SIZE;
  56. }
  57. memcpy(sctx->buffer, data + done, len - done);
  58. return 0;
  59. }
  60. static int sha1_neon_update(struct shash_desc *desc, const u8 *data,
  61. unsigned int len)
  62. {
  63. struct sha1_state *sctx = shash_desc_ctx(desc);
  64. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  65. int res;
  66. /* Handle the fast case right here */
  67. if (partial + len < SHA1_BLOCK_SIZE) {
  68. sctx->count += len;
  69. memcpy(sctx->buffer + partial, data, len);
  70. return 0;
  71. }
  72. if (!may_use_simd()) {
  73. res = sha1_update_arm(desc, data, len);
  74. } else {
  75. kernel_neon_begin();
  76. res = __sha1_neon_update(desc, data, len, partial);
  77. kernel_neon_end();
  78. }
  79. return res;
  80. }
  81. /* Add padding and return the message digest. */
  82. static int sha1_neon_final(struct shash_desc *desc, u8 *out)
  83. {
  84. struct sha1_state *sctx = shash_desc_ctx(desc);
  85. unsigned int i, index, padlen;
  86. __be32 *dst = (__be32 *)out;
  87. __be64 bits;
  88. static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
  89. bits = cpu_to_be64(sctx->count << 3);
  90. /* Pad out to 56 mod 64 and append length */
  91. index = sctx->count % SHA1_BLOCK_SIZE;
  92. padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
  93. if (!may_use_simd()) {
  94. sha1_update_arm(desc, padding, padlen);
  95. sha1_update_arm(desc, (const u8 *)&bits, sizeof(bits));
  96. } else {
  97. kernel_neon_begin();
  98. /* We need to fill a whole block for __sha1_neon_update() */
  99. if (padlen <= 56) {
  100. sctx->count += padlen;
  101. memcpy(sctx->buffer + index, padding, padlen);
  102. } else {
  103. __sha1_neon_update(desc, padding, padlen, index);
  104. }
  105. __sha1_neon_update(desc, (const u8 *)&bits, sizeof(bits), 56);
  106. kernel_neon_end();
  107. }
  108. /* Store state in digest */
  109. for (i = 0; i < 5; i++)
  110. dst[i] = cpu_to_be32(sctx->state[i]);
  111. /* Wipe context */
  112. memset(sctx, 0, sizeof(*sctx));
  113. return 0;
  114. }
  115. static int sha1_neon_export(struct shash_desc *desc, void *out)
  116. {
  117. struct sha1_state *sctx = shash_desc_ctx(desc);
  118. memcpy(out, sctx, sizeof(*sctx));
  119. return 0;
  120. }
  121. static int sha1_neon_import(struct shash_desc *desc, const void *in)
  122. {
  123. struct sha1_state *sctx = shash_desc_ctx(desc);
  124. memcpy(sctx, in, sizeof(*sctx));
  125. return 0;
  126. }
  127. static struct shash_alg alg = {
  128. .digestsize = SHA1_DIGEST_SIZE,
  129. .init = sha1_neon_init,
  130. .update = sha1_neon_update,
  131. .final = sha1_neon_final,
  132. .export = sha1_neon_export,
  133. .import = sha1_neon_import,
  134. .descsize = sizeof(struct sha1_state),
  135. .statesize = sizeof(struct sha1_state),
  136. .base = {
  137. .cra_name = "sha1",
  138. .cra_driver_name = "sha1-neon",
  139. .cra_priority = 250,
  140. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  141. .cra_blocksize = SHA1_BLOCK_SIZE,
  142. .cra_module = THIS_MODULE,
  143. }
  144. };
  145. static int __init sha1_neon_mod_init(void)
  146. {
  147. if (!cpu_has_neon())
  148. return -ENODEV;
  149. return crypto_register_shash(&alg);
  150. }
  151. static void __exit sha1_neon_mod_fini(void)
  152. {
  153. crypto_unregister_shash(&alg);
  154. }
  155. module_init(sha1_neon_mod_init);
  156. module_exit(sha1_neon_mod_fini);
  157. MODULE_LICENSE("GPL");
  158. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, NEON accelerated");
  159. MODULE_ALIAS("sha1");