sha1-ce-glue.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
  3. *
  4. * Copyright (C) 2014 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/unaligned.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/sha.h>
  14. #include <linux/cpufeature.h>
  15. #include <linux/crypto.h>
  16. #include <linux/module.h>
  17. MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
  18. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  19. MODULE_LICENSE("GPL v2");
  20. asmlinkage void sha1_ce_transform(int blocks, u8 const *src, u32 *state,
  21. u8 *head, long bytes);
  22. static int sha1_init(struct shash_desc *desc)
  23. {
  24. struct sha1_state *sctx = shash_desc_ctx(desc);
  25. *sctx = (struct sha1_state){
  26. .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  27. };
  28. return 0;
  29. }
  30. static int sha1_update(struct shash_desc *desc, const u8 *data,
  31. unsigned int len)
  32. {
  33. struct sha1_state *sctx = shash_desc_ctx(desc);
  34. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  35. sctx->count += len;
  36. if ((partial + len) >= SHA1_BLOCK_SIZE) {
  37. int blocks;
  38. if (partial) {
  39. int p = SHA1_BLOCK_SIZE - partial;
  40. memcpy(sctx->buffer + partial, data, p);
  41. data += p;
  42. len -= p;
  43. }
  44. blocks = len / SHA1_BLOCK_SIZE;
  45. len %= SHA1_BLOCK_SIZE;
  46. kernel_neon_begin_partial(16);
  47. sha1_ce_transform(blocks, data, sctx->state,
  48. partial ? sctx->buffer : NULL, 0);
  49. kernel_neon_end();
  50. data += blocks * SHA1_BLOCK_SIZE;
  51. partial = 0;
  52. }
  53. if (len)
  54. memcpy(sctx->buffer + partial, data, len);
  55. return 0;
  56. }
  57. static int sha1_final(struct shash_desc *desc, u8 *out)
  58. {
  59. static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
  60. struct sha1_state *sctx = shash_desc_ctx(desc);
  61. __be64 bits = cpu_to_be64(sctx->count << 3);
  62. __be32 *dst = (__be32 *)out;
  63. int i;
  64. u32 padlen = SHA1_BLOCK_SIZE
  65. - ((sctx->count + sizeof(bits)) % SHA1_BLOCK_SIZE);
  66. sha1_update(desc, padding, padlen);
  67. sha1_update(desc, (const u8 *)&bits, sizeof(bits));
  68. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
  69. put_unaligned_be32(sctx->state[i], dst++);
  70. *sctx = (struct sha1_state){};
  71. return 0;
  72. }
  73. static int sha1_finup(struct shash_desc *desc, const u8 *data,
  74. unsigned int len, u8 *out)
  75. {
  76. struct sha1_state *sctx = shash_desc_ctx(desc);
  77. __be32 *dst = (__be32 *)out;
  78. int blocks;
  79. int i;
  80. if (sctx->count || !len || (len % SHA1_BLOCK_SIZE)) {
  81. sha1_update(desc, data, len);
  82. return sha1_final(desc, out);
  83. }
  84. /*
  85. * Use a fast path if the input is a multiple of 64 bytes. In
  86. * this case, there is no need to copy data around, and we can
  87. * perform the entire digest calculation in a single invocation
  88. * of sha1_ce_transform()
  89. */
  90. blocks = len / SHA1_BLOCK_SIZE;
  91. kernel_neon_begin_partial(16);
  92. sha1_ce_transform(blocks, data, sctx->state, NULL, len);
  93. kernel_neon_end();
  94. for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++)
  95. put_unaligned_be32(sctx->state[i], dst++);
  96. *sctx = (struct sha1_state){};
  97. return 0;
  98. }
  99. static int sha1_export(struct shash_desc *desc, void *out)
  100. {
  101. struct sha1_state *sctx = shash_desc_ctx(desc);
  102. struct sha1_state *dst = out;
  103. *dst = *sctx;
  104. return 0;
  105. }
  106. static int sha1_import(struct shash_desc *desc, const void *in)
  107. {
  108. struct sha1_state *sctx = shash_desc_ctx(desc);
  109. struct sha1_state const *src = in;
  110. *sctx = *src;
  111. return 0;
  112. }
  113. static struct shash_alg alg = {
  114. .init = sha1_init,
  115. .update = sha1_update,
  116. .final = sha1_final,
  117. .finup = sha1_finup,
  118. .export = sha1_export,
  119. .import = sha1_import,
  120. .descsize = sizeof(struct sha1_state),
  121. .digestsize = SHA1_DIGEST_SIZE,
  122. .statesize = sizeof(struct sha1_state),
  123. .base = {
  124. .cra_name = "sha1",
  125. .cra_driver_name = "sha1-ce",
  126. .cra_priority = 200,
  127. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  128. .cra_blocksize = SHA1_BLOCK_SIZE,
  129. .cra_module = THIS_MODULE,
  130. }
  131. };
  132. static int __init sha1_ce_mod_init(void)
  133. {
  134. return crypto_register_shash(&alg);
  135. }
  136. static void __exit sha1_ce_mod_fini(void)
  137. {
  138. crypto_unregister_shash(&alg);
  139. }
  140. module_cpu_feature_match(SHA1, sha1_ce_mod_init);
  141. module_exit(sha1_ce_mod_fini);