curve25519.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. */
  5. #include "curve25519.h"
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include "utils.h"
  9. #ifndef __BYTE_ORDER__
  10. #include <sys/param.h>
  11. #if !defined(BYTE_ORDER) || !defined(BIG_ENDIAN) || !defined(LITTLE_ENDIAN)
  12. #error "Unable to determine endianness."
  13. #endif
  14. #define __BYTE_ORDER__ BYTE_ORDER
  15. #define __ORDER_BIG_ENDIAN__ BIG_ENDIAN
  16. #define __ORDER_LITTLE_ENDIAN__ LITTLE_ENDIAN
  17. #endif
  18. #ifdef __linux__
  19. #include <linux/types.h>
  20. typedef __u64 u64;
  21. typedef __u32 u32;
  22. typedef __u8 u8;
  23. typedef __s64 s64;
  24. #else
  25. typedef uint64_t u64, __le64;
  26. typedef uint32_t u32, __le32;
  27. typedef uint8_t u8;
  28. typedef int64_t s64;
  29. #endif
  30. #ifndef __unused
  31. #define __unused __attribute__((unused))
  32. #endif
  33. #ifndef __always_inline
  34. #define __always_inline __inline __attribute__((__always_inline__))
  35. #endif
  36. #ifndef noinline
  37. #define noinline __attribute__((noinline))
  38. #endif
  39. #ifndef __aligned
  40. #define __aligned(x) __attribute__((aligned(x)))
  41. #endif
  42. #ifndef __force
  43. #define __force
  44. #endif
  45. static __always_inline __unused void put_unaligned_le64(u64 s, u8 *d)
  46. {
  47. __le64 l = cpu_to_le64(s);
  48. __builtin_memcpy(d, &l, sizeof(l));
  49. }
  50. static noinline void memzero_explicit(void *s, size_t count)
  51. {
  52. memset(s, 0, count);
  53. asm volatile("": :"r"(s) : "memory");
  54. }
  55. #ifdef __SIZEOF_INT128__
  56. #include "curve25519-hacl64.h"
  57. #else
  58. #include "curve25519-fiat32.h"
  59. #endif
  60. void curve25519_generate_public(uint8_t pub[static CURVE25519_KEY_SIZE], const uint8_t secret[static CURVE25519_KEY_SIZE])
  61. {
  62. static const uint8_t basepoint[CURVE25519_KEY_SIZE] __aligned(sizeof(uintptr_t)) = { 9 };
  63. curve25519(pub, secret, basepoint);
  64. }
  65. void curve25519(uint8_t mypublic[static CURVE25519_KEY_SIZE], const uint8_t secret[static CURVE25519_KEY_SIZE], const uint8_t basepoint[static CURVE25519_KEY_SIZE])
  66. {
  67. curve25519_generic(mypublic, secret, basepoint);
  68. }