utils.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto library utility functions
  4. *
  5. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #include <crypto/utils.h>
  8. #include <linux/export.h>
  9. #include <linux/module.h>
  10. #include <linux/unaligned.h>
  11. /*
  12. * XOR @len bytes from @src1 and @src2 together, writing the result to @dst
  13. * (which may alias one of the sources). Don't call this directly; call
  14. * crypto_xor() or crypto_xor_cpy() instead.
  15. */
  16. void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len)
  17. {
  18. int relalign = 0;
  19. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
  20. int size = sizeof(unsigned long);
  21. int d = (((unsigned long)dst ^ (unsigned long)src1) |
  22. ((unsigned long)dst ^ (unsigned long)src2)) &
  23. (size - 1);
  24. relalign = d ? 1 << __ffs(d) : size;
  25. /*
  26. * If we care about alignment, process as many bytes as
  27. * needed to advance dst and src to values whose alignments
  28. * equal their relative alignment. This will allow us to
  29. * process the remainder of the input using optimal strides.
  30. */
  31. while (((unsigned long)dst & (relalign - 1)) && len > 0) {
  32. *dst++ = *src1++ ^ *src2++;
  33. len--;
  34. }
  35. }
  36. while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) {
  37. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
  38. u64 l = get_unaligned((u64 *)src1) ^
  39. get_unaligned((u64 *)src2);
  40. put_unaligned(l, (u64 *)dst);
  41. } else {
  42. *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2;
  43. }
  44. dst += 8;
  45. src1 += 8;
  46. src2 += 8;
  47. len -= 8;
  48. }
  49. while (len >= 4 && !(relalign & 3)) {
  50. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
  51. u32 l = get_unaligned((u32 *)src1) ^
  52. get_unaligned((u32 *)src2);
  53. put_unaligned(l, (u32 *)dst);
  54. } else {
  55. *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2;
  56. }
  57. dst += 4;
  58. src1 += 4;
  59. src2 += 4;
  60. len -= 4;
  61. }
  62. while (len >= 2 && !(relalign & 1)) {
  63. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
  64. u16 l = get_unaligned((u16 *)src1) ^
  65. get_unaligned((u16 *)src2);
  66. put_unaligned(l, (u16 *)dst);
  67. } else {
  68. *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2;
  69. }
  70. dst += 2;
  71. src1 += 2;
  72. src2 += 2;
  73. len -= 2;
  74. }
  75. while (len--)
  76. *dst++ = *src1++ ^ *src2++;
  77. }
  78. EXPORT_SYMBOL_GPL(__crypto_xor);
  79. MODULE_DESCRIPTION("Crypto library utility functions");
  80. MODULE_LICENSE("GPL");