logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0002-Use-portable-int-pack-unpack-routines.patch (2377B)


  1. From 503ef43e485e76f015b95f3c24233e691ff55e32 Mon Sep 17 00:00:00 2001
  2. From: Michael Forney <mforney@mforney.org>
  3. Date: Wed, 1 Apr 2026 15:17:55 -0700
  4. Subject: [PATCH] Use portable int pack/unpack routines
  5. These compile to the exact same assembly on gcc with -O2.
  6. ---
  7. src/curve25519.c | 38 +++++++++++---------------------------
  8. 1 file changed, 11 insertions(+), 27 deletions(-)
  9. diff --git a/src/curve25519.c b/src/curve25519.c
  10. index d3bc005..e67591c 100644
  11. --- a/src/curve25519.c
  12. +++ b/src/curve25519.c
  13. @@ -8,16 +8,6 @@
  14. #include <stdint.h>
  15. #include <string.h>
  16. -#ifndef __BYTE_ORDER__
  17. -#include <sys/param.h>
  18. -#if !defined(BYTE_ORDER) || !defined(BIG_ENDIAN) || !defined(LITTLE_ENDIAN)
  19. -#error "Unable to determine endianness."
  20. -#endif
  21. -#define __BYTE_ORDER__ BYTE_ORDER
  22. -#define __ORDER_BIG_ENDIAN__ BIG_ENDIAN
  23. -#define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN
  24. -#endif
  25. -
  26. #ifdef __linux__
  27. #include <linux/types.h>
  28. typedef __u64 u64;
  29. @@ -30,15 +20,6 @@ typedef uint32_t u32, __le32;
  30. typedef uint8_t u8;
  31. typedef int64_t s64;
  32. #endif
  33. -#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  34. -#define le64_to_cpup(a) __builtin_bswap64(*(a))
  35. -#define le32_to_cpup(a) __builtin_bswap32(*(a))
  36. -#define cpu_to_le64(a) __builtin_bswap64(a)
  37. -#else
  38. -#define le64_to_cpup(a) (*(a))
  39. -#define le32_to_cpup(a) (*(a))
  40. -#define cpu_to_le64(a) (a)
  41. -#endif
  42. #ifndef __unused
  43. #define __unused __attribute__((unused))
  44. #endif
  45. @@ -57,20 +38,23 @@ typedef int64_t s64;
  46. static __always_inline __unused __le32 get_unaligned_le32(const u8 *a)
  47. {
  48. - __le32 l;
  49. - __builtin_memcpy(&l, a, sizeof(l));
  50. - return le32_to_cpup(&l);
  51. + return (__le32)a[0] | (__le32)a[1] << 8 | (__le32)a[2] << 16 | (__le32)a[3] << 24;
  52. }
  53. static __always_inline __unused __le64 get_unaligned_le64(const u8 *a)
  54. {
  55. - __le64 l;
  56. - __builtin_memcpy(&l, a, sizeof(l));
  57. - return le64_to_cpup(&l);
  58. + return (__le64)a[0] | (__le64)a[1] << 8 | (__le64)a[2] << 16 | (__le64)a[3] << 24 |
  59. + (__le64)a[4] << 32 | (__le64)a[5] << 40 | (__le64)a[6] << 48 | (__le64)a[7] << 56;
  60. }
  61. static __always_inline __unused void put_unaligned_le64(u64 s, u8 *d)
  62. {
  63. - __le64 l = cpu_to_le64(s);
  64. - __builtin_memcpy(d, &l, sizeof(l));
  65. + d[0] = s;
  66. + d[1] = s >> 8;
  67. + d[2] = s >> 16;
  68. + d[3] = s >> 24;
  69. + d[4] = s >> 32;
  70. + d[5] = s >> 40;
  71. + d[6] = s >> 48;
  72. + d[7] = s >> 56;
  73. }
  74. static noinline void memzero_explicit(void *s, size_t count)
  75. --
  76. 2.49.0