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.

chacha_merged_ecrypt.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Definitions for types and macros used in chacha_merged.c. Taken from
  2. * supercop.
  3. */
  4. #include <limits.h>
  5. typedef struct
  6. {
  7. u32 input[16]; /* could be compressed */
  8. /*
  9. * [edit]
  10. *
  11. * Put here all state variable needed during the encryption process.
  12. */
  13. } ECRYPT_ctx;
  14. #if (UCHAR_MAX / 0xFFFFU > 0xFFFFU)
  15. #ifndef I32T
  16. #define I32T char
  17. #define U32C(v) (v##U)
  18. #endif
  19. #endif
  20. #if (USHRT_MAX / 0xFFFFU > 0xFFFFU)
  21. #ifndef I32T
  22. #define I32T short
  23. #define U32C(v) (v##U)
  24. #endif
  25. #endif
  26. #if (UINT_MAX / 0xFFFFU > 0xFFFFU)
  27. #ifndef I32T
  28. #define I32T int
  29. #define U32C(v) (v##U)
  30. #endif
  31. #endif
  32. #if (ULONG_MAX / 0xFFFFUL > 0xFFFFUL)
  33. #ifndef I32T
  34. #define I32T long
  35. #define U32C(v) (v##UL)
  36. #endif
  37. #endif
  38. #define U8C(v) (v ## U)
  39. #define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
  40. #define U8V(v) ((u8)(v) & U8C(0xFF))
  41. #if (defined(WIN32) && defined(_MSC_VER))
  42. #include <stdlib.h>
  43. #pragma intrinsic(_lrotl) /* compile rotations "inline" */
  44. #define ROTL32(v, n) _lrotl(v, n)
  45. #else
  46. #define ROTL32(v, n) \
  47. (U32V((v) << (n)) | ((v) >> (32 - (n))))
  48. #endif
  49. #if ECRYPT_LITTLE_ENDIAN
  50. #define U32TO32_LITTLE(v) (v)
  51. #endif
  52. #ifdef ECRYPT_BIG_ENDIAN
  53. #define SWAP32(v) \
  54. ((ROTL32(v, 8) & U32C(0x00FF00FF)) | \
  55. (ROTL32(v, 24) & U32C(0xFF00FF00)))
  56. #define U32TO32_LITTLE(v) SWAP32(v)
  57. #endif
  58. #ifdef U32TO32_LITTLE
  59. #define U8TO32_LITTLE(p) U32TO32_LITTLE(((u32*)(p))[0])
  60. #define U32TO8_LITTLE(p, v) (((u32*)(p))[0] = U32TO32_LITTLE(v))
  61. #else
  62. #define U8TO32_LITTLE(p) \
  63. (((u32)((p)[0]) ) | \
  64. ((u32)((p)[1]) << 8) | \
  65. ((u32)((p)[2]) << 16) | \
  66. ((u32)((p)[3]) << 24))
  67. #define U32TO8_LITTLE(p, v) \
  68. do { \
  69. (p)[0] = U8V((v) ); \
  70. (p)[1] = U8V((v) >> 8); \
  71. (p)[2] = U8V((v) >> 16); \
  72. (p)[3] = U8V((v) >> 24); \
  73. } while (0)
  74. #endif
  75. /*
  76. * The LITTLE endian machines:
  77. */
  78. #if defined(__ultrix) /* Older MIPS */
  79. #define ECRYPT_LITTLE_ENDIAN
  80. #elif defined(__alpha) /* Alpha */
  81. #define ECRYPT_LITTLE_ENDIAN
  82. #elif defined(i386) /* x86 (gcc) */
  83. #define ECRYPT_LITTLE_ENDIAN
  84. #elif defined(__i386) /* x86 (gcc) */
  85. #define ECRYPT_LITTLE_ENDIAN
  86. #elif defined(__x86_64) /* x86_64 (gcc) */
  87. #define ECRYPT_LITTLE_ENDIAN
  88. #elif defined(_M_IX86) /* x86 (MSC, Borland) */
  89. #define ECRYPT_LITTLE_ENDIAN
  90. #elif defined(_MSC_VER) /* x86 (surely MSC) */
  91. #define ECRYPT_LITTLE_ENDIAN
  92. #elif defined(__INTEL_COMPILER) /* x86 (surely Intel compiler icl.exe) */
  93. #define ECRYPT_LITTLE_ENDIAN
  94. /*
  95. * The BIG endian machines:
  96. */
  97. #elif defined(__sparc) /* Newer Sparc's */
  98. #define ECRYPT_BIG_ENDIAN
  99. #elif defined(__powerpc__) /* PowerPC */
  100. #define ECRYPT_BIG_ENDIAN
  101. #elif defined(__ppc__) /* PowerPC */
  102. #define ECRYPT_BIG_ENDIAN
  103. #elif defined(__hppa) /* HP-PA */
  104. #define ECRYPT_BIG_ENDIAN
  105. /*
  106. * Finally machines with UNKNOWN endianness:
  107. */
  108. #elif defined (_AIX) /* RS6000 */
  109. #define ECRYPT_UNKNOWN
  110. #elif defined(__aux) /* 68K */
  111. #define ECRYPT_UNKNOWN
  112. #elif defined(__dgux) /* 88K (but P6 in latest boxes) */
  113. #define ECRYPT_UNKNOWN
  114. #elif defined(__sgi) /* Newer MIPS */
  115. #define ECRYPT_UNKNOWN
  116. #else /* Any other processor */
  117. #define ECRYPT_UNKNOWN
  118. #endif