sha256_neon_glue.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Glue code for the SHA256 Secure Hash Algorithm assembly implementation
  3. * using NEON instructions.
  4. *
  5. * Copyright © 2015 Google Inc.
  6. *
  7. * This file is based on sha512_neon_glue.c:
  8. * Copyright © 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/internal/hash.h>
  17. #include <linux/cryptohash.h>
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <crypto/sha.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/simd.h>
  24. #include <asm/neon.h>
  25. #include "sha256_glue.h"
  26. asmlinkage void sha256_block_data_order_neon(u32 *digest, const void *data,
  27. unsigned int num_blks);
  28. static int __sha256_neon_update(struct shash_desc *desc, const u8 *data,
  29. unsigned int len, unsigned int partial)
  30. {
  31. struct sha256_state *sctx = shash_desc_ctx(desc);
  32. unsigned int done = 0;
  33. sctx->count += len;
  34. if (partial) {
  35. done = SHA256_BLOCK_SIZE - partial;
  36. memcpy(sctx->buf + partial, data, done);
  37. sha256_block_data_order_neon(sctx->state, sctx->buf, 1);
  38. }
  39. if (len - done >= SHA256_BLOCK_SIZE) {
  40. const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
  41. sha256_block_data_order_neon(sctx->state, data + done, rounds);
  42. done += rounds * SHA256_BLOCK_SIZE;
  43. }
  44. memcpy(sctx->buf, data + done, len - done);
  45. return 0;
  46. }
  47. static int sha256_neon_update(struct shash_desc *desc, const u8 *data,
  48. unsigned int len)
  49. {
  50. struct sha256_state *sctx = shash_desc_ctx(desc);
  51. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  52. int res;
  53. /* Handle the fast case right here */
  54. if (partial + len < SHA256_BLOCK_SIZE) {
  55. sctx->count += len;
  56. memcpy(sctx->buf + partial, data, len);
  57. return 0;
  58. }
  59. if (!may_use_simd()) {
  60. res = __sha256_update(desc, data, len, partial);
  61. } else {
  62. kernel_neon_begin();
  63. res = __sha256_neon_update(desc, data, len, partial);
  64. kernel_neon_end();
  65. }
  66. return res;
  67. }
  68. /* Add padding and return the message digest. */
  69. static int sha256_neon_final(struct shash_desc *desc, u8 *out)
  70. {
  71. struct sha256_state *sctx = shash_desc_ctx(desc);
  72. unsigned int i, index, padlen;
  73. __be32 *dst = (__be32 *)out;
  74. __be64 bits;
  75. static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
  76. /* save number of bits */
  77. bits = cpu_to_be64(sctx->count << 3);
  78. /* Pad out to 56 mod 64 and append length */
  79. index = sctx->count % SHA256_BLOCK_SIZE;
  80. padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56)-index);
  81. if (!may_use_simd()) {
  82. sha256_update(desc, padding, padlen);
  83. sha256_update(desc, (const u8 *)&bits, sizeof(bits));
  84. } else {
  85. kernel_neon_begin();
  86. /* We need to fill a whole block for __sha256_neon_update() */
  87. if (padlen <= 56) {
  88. sctx->count += padlen;
  89. memcpy(sctx->buf + index, padding, padlen);
  90. } else {
  91. __sha256_neon_update(desc, padding, padlen, index);
  92. }
  93. __sha256_neon_update(desc, (const u8 *)&bits,
  94. sizeof(bits), 56);
  95. kernel_neon_end();
  96. }
  97. /* Store state in digest */
  98. for (i = 0; i < 8; i++)
  99. dst[i] = cpu_to_be32(sctx->state[i]);
  100. /* Wipe context */
  101. memzero_explicit(sctx, sizeof(*sctx));
  102. return 0;
  103. }
  104. static int sha224_neon_final(struct shash_desc *desc, u8 *out)
  105. {
  106. u8 D[SHA256_DIGEST_SIZE];
  107. sha256_neon_final(desc, D);
  108. memcpy(out, D, SHA224_DIGEST_SIZE);
  109. memzero_explicit(D, SHA256_DIGEST_SIZE);
  110. return 0;
  111. }
  112. struct shash_alg sha256_neon_algs[] = { {
  113. .digestsize = SHA256_DIGEST_SIZE,
  114. .init = sha256_init,
  115. .update = sha256_neon_update,
  116. .final = sha256_neon_final,
  117. .export = sha256_export,
  118. .import = sha256_import,
  119. .descsize = sizeof(struct sha256_state),
  120. .statesize = sizeof(struct sha256_state),
  121. .base = {
  122. .cra_name = "sha256",
  123. .cra_driver_name = "sha256-neon",
  124. .cra_priority = 250,
  125. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  126. .cra_blocksize = SHA256_BLOCK_SIZE,
  127. .cra_module = THIS_MODULE,
  128. }
  129. }, {
  130. .digestsize = SHA224_DIGEST_SIZE,
  131. .init = sha224_init,
  132. .update = sha256_neon_update,
  133. .final = sha224_neon_final,
  134. .export = sha256_export,
  135. .import = sha256_import,
  136. .descsize = sizeof(struct sha256_state),
  137. .statesize = sizeof(struct sha256_state),
  138. .base = {
  139. .cra_name = "sha224",
  140. .cra_driver_name = "sha224-neon",
  141. .cra_priority = 250,
  142. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  143. .cra_blocksize = SHA224_BLOCK_SIZE,
  144. .cra_module = THIS_MODULE,
  145. }
  146. } };