You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

107 lines
3.0 KiB

  1. /**
  2. * @file decaf/common.h
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief Common utility headers for Decaf library.
  10. */
  11. #ifndef __DECAF_COMMON_H__
  12. #define __DECAF_COMMON_H__ 1
  13. #include <stdint.h>
  14. #include <sys/types.h>
  15. /* Goldilocks' build flags default to hidden and stripping executables. */
  16. /** @cond internal */
  17. #if defined(DOXYGEN) && !defined(__attribute__)
  18. #define __attribute__((x))
  19. #endif
  20. #define API_VIS __attribute__((visibility("default")))
  21. #define NOINLINE __attribute__((noinline))
  22. #define WARN_UNUSED __attribute__((warn_unused_result))
  23. #define NONNULL1 __attribute__((nonnull(1)))
  24. #define NONNULL2 __attribute__((nonnull(1,2)))
  25. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  26. #define NONNULL13 __attribute__((nonnull(1,3)))
  27. #define NONNULL134 __attribute__((nonnull(1,3,4)))
  28. #define NONNULL4 __attribute__((nonnull(1,2,3,4)))
  29. #define NONNULL5 __attribute__((nonnull(1,2,3,4,5)))
  30. #define INLINE inline __attribute__((always_inline))
  31. #define UNUSED __attribute__((unused))
  32. /** @endcond */
  33. /* Internal word types.
  34. *
  35. * Somewhat tricky. This could be decided separately per platform. However,
  36. * the structs do need to be all the same size and alignment on a given
  37. * platform to support dynamic linking, since even if you header was built
  38. * with eg arch_neon, you might end up linking a library built with arch_arm32.
  39. */
  40. #if (defined(__ILP64__) || defined(__amd64__) || defined(__x86_64__) || (((__UINT_FAST32_MAX__)>>30)>>30)) \
  41. && !defined(DECAF_FORCE_32_BIT)
  42. #define DECAF_WORD_BITS 64
  43. typedef uint64_t decaf_word_t, decaf_bool_t;
  44. typedef __uint128_t decaf_dword_t;
  45. #else
  46. #define DECAF_WORD_BITS 32
  47. typedef uint32_t decaf_word_t, decaf_bool_t;
  48. typedef uint64_t decaf_dword_t;
  49. #endif
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /** DECAF_TRUE = -1 so that DECAF_TRUE & x = x */
  54. static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
  55. /* Success or failure */
  56. // FIXME: deploy project-wide
  57. typedef enum {
  58. DECAF_SUCCESS = -1,
  59. DECAF_FAILURE = 0
  60. } decaf_error_t;
  61. /** Return success if x is true */
  62. static __inline__ __attribute__((unused,always_inline))
  63. decaf_error_t
  64. decaf_succeed_if(decaf_bool_t x) {
  65. return (decaf_error_t)x;
  66. }
  67. /** Return DECAF_TRUE iff x == DECAF_SUCCESS */
  68. static __inline__ __attribute__((unused,always_inline))
  69. decaf_bool_t
  70. decaf_successful(decaf_error_t e) {
  71. decaf_dword_t w = ((decaf_word_t)e) ^ ((decaf_word_t)DECAF_SUCCESS);
  72. return (w-1)>>DECAF_WORD_BITS;
  73. }
  74. /**
  75. * @brief Overwrite data with zeros. Uses memset_s if available.
  76. */
  77. void decaf_bzero (
  78. void *data,
  79. size_t size
  80. ) NONNULL1 API_VIS;
  81. /**
  82. * @brief Compare two buffers, returning DECAF_TRUE if they are equal.
  83. */
  84. decaf_bool_t decaf_memeq (
  85. const void *data1,
  86. const void *data2,
  87. size_t size
  88. ) NONNULL2 WARN_UNUSED API_VIS;
  89. #ifdef __cplusplus
  90. } /* extern "C" */
  91. #endif
  92. #endif /* __DECAF_COMMON_H__ */