utils.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Cryptographic utilities
  4. *
  5. * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_UTILS_H
  8. #define _CRYPTO_UTILS_H
  9. #include <linux/unaligned.h>
  10. #include <linux/compiler_attributes.h>
  11. #include <linux/types.h>
  12. void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int size);
  13. static inline void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
  14. {
  15. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
  16. __builtin_constant_p(size) &&
  17. (size % sizeof(unsigned long)) == 0) {
  18. unsigned long *d = (unsigned long *)dst;
  19. unsigned long *s = (unsigned long *)src;
  20. unsigned long l;
  21. while (size > 0) {
  22. l = get_unaligned(d) ^ get_unaligned(s++);
  23. put_unaligned(l, d++);
  24. size -= sizeof(unsigned long);
  25. }
  26. } else {
  27. __crypto_xor(dst, dst, src, size);
  28. }
  29. }
  30. static inline void crypto_xor_cpy(u8 *dst, const u8 *src1, const u8 *src2,
  31. unsigned int size)
  32. {
  33. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
  34. __builtin_constant_p(size) &&
  35. (size % sizeof(unsigned long)) == 0) {
  36. unsigned long *d = (unsigned long *)dst;
  37. unsigned long *s1 = (unsigned long *)src1;
  38. unsigned long *s2 = (unsigned long *)src2;
  39. unsigned long l;
  40. while (size > 0) {
  41. l = get_unaligned(s1++) ^ get_unaligned(s2++);
  42. put_unaligned(l, d++);
  43. size -= sizeof(unsigned long);
  44. }
  45. } else {
  46. __crypto_xor(dst, src1, src2, size);
  47. }
  48. }
  49. noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
  50. /**
  51. * crypto_memneq - Compare two areas of memory without leaking
  52. * timing information.
  53. *
  54. * @a: One area of memory
  55. * @b: Another area of memory
  56. * @size: The size of the area.
  57. *
  58. * Returns 0 when data is equal, 1 otherwise.
  59. */
  60. static inline int crypto_memneq(const void *a, const void *b, size_t size)
  61. {
  62. return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
  63. }
  64. #endif /* _CRYPTO_UTILS_H */