blake2-impl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Argon2 reference source code package - reference C implementations
  3. *
  4. * Copyright 2015
  5. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  6. *
  7. * You may use this work under the terms of a Creative Commons CC0 1.0
  8. * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  9. * these licenses can be found at:
  10. *
  11. * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  12. * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * You should have received a copy of both of these licenses along with this
  15. * software. If not, they may be obtained at the above URLs.
  16. */
  17. #ifndef PORTABLE_BLAKE2_IMPL_H
  18. #define PORTABLE_BLAKE2_IMPL_H
  19. #include <stdint.h>
  20. #include <string.h>
  21. #if defined(_MSC_VER)
  22. #define BLAKE2_INLINE __inline
  23. #elif defined(__GNUC__) || defined(__clang__)
  24. #define BLAKE2_INLINE __inline__
  25. #else
  26. #define BLAKE2_INLINE
  27. #endif
  28. /* Argon2 Team - Begin Code */
  29. /*
  30. Not an exhaustive list, but should cover the majority of modern platforms
  31. Additionally, the code will always be correct---this is only a performance
  32. tweak.
  33. */
  34. #if (defined(__BYTE_ORDER__) && \
  35. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
  36. defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
  37. defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) || \
  38. defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || \
  39. defined(_M_ARM)
  40. #define NATIVE_LITTLE_ENDIAN
  41. #endif
  42. /* Argon2 Team - End Code */
  43. static BLAKE2_INLINE uint32_t load32(const void *src) {
  44. #if defined(NATIVE_LITTLE_ENDIAN)
  45. uint32_t w;
  46. memcpy(&w, src, sizeof w);
  47. return w;
  48. #else
  49. const uint8_t *p = (const uint8_t *)src;
  50. uint32_t w = *p++;
  51. w |= (uint32_t)(*p++) << 8;
  52. w |= (uint32_t)(*p++) << 16;
  53. w |= (uint32_t)(*p++) << 24;
  54. return w;
  55. #endif
  56. }
  57. static BLAKE2_INLINE uint64_t load64(const void *src) {
  58. #if defined(NATIVE_LITTLE_ENDIAN)
  59. uint64_t w;
  60. memcpy(&w, src, sizeof w);
  61. return w;
  62. #else
  63. const uint8_t *p = (const uint8_t *)src;
  64. uint64_t w = *p++;
  65. w |= (uint64_t)(*p++) << 8;
  66. w |= (uint64_t)(*p++) << 16;
  67. w |= (uint64_t)(*p++) << 24;
  68. w |= (uint64_t)(*p++) << 32;
  69. w |= (uint64_t)(*p++) << 40;
  70. w |= (uint64_t)(*p++) << 48;
  71. w |= (uint64_t)(*p++) << 56;
  72. return w;
  73. #endif
  74. }
  75. static BLAKE2_INLINE void store32(void *dst, uint32_t w) {
  76. #if defined(NATIVE_LITTLE_ENDIAN)
  77. memcpy(dst, &w, sizeof w);
  78. #else
  79. uint8_t *p = (uint8_t *)dst;
  80. *p++ = (uint8_t)w;
  81. w >>= 8;
  82. *p++ = (uint8_t)w;
  83. w >>= 8;
  84. *p++ = (uint8_t)w;
  85. w >>= 8;
  86. *p++ = (uint8_t)w;
  87. #endif
  88. }
  89. static BLAKE2_INLINE void store64(void *dst, uint64_t w) {
  90. #if defined(NATIVE_LITTLE_ENDIAN)
  91. memcpy(dst, &w, sizeof w);
  92. #else
  93. uint8_t *p = (uint8_t *)dst;
  94. *p++ = (uint8_t)w;
  95. w >>= 8;
  96. *p++ = (uint8_t)w;
  97. w >>= 8;
  98. *p++ = (uint8_t)w;
  99. w >>= 8;
  100. *p++ = (uint8_t)w;
  101. w >>= 8;
  102. *p++ = (uint8_t)w;
  103. w >>= 8;
  104. *p++ = (uint8_t)w;
  105. w >>= 8;
  106. *p++ = (uint8_t)w;
  107. w >>= 8;
  108. *p++ = (uint8_t)w;
  109. #endif
  110. }
  111. static BLAKE2_INLINE uint64_t load48(const void *src) {
  112. const uint8_t *p = (const uint8_t *)src;
  113. uint64_t w = *p++;
  114. w |= (uint64_t)(*p++) << 8;
  115. w |= (uint64_t)(*p++) << 16;
  116. w |= (uint64_t)(*p++) << 24;
  117. w |= (uint64_t)(*p++) << 32;
  118. w |= (uint64_t)(*p++) << 40;
  119. return w;
  120. }
  121. static BLAKE2_INLINE void store48(void *dst, uint64_t w) {
  122. uint8_t *p = (uint8_t *)dst;
  123. *p++ = (uint8_t)w;
  124. w >>= 8;
  125. *p++ = (uint8_t)w;
  126. w >>= 8;
  127. *p++ = (uint8_t)w;
  128. w >>= 8;
  129. *p++ = (uint8_t)w;
  130. w >>= 8;
  131. *p++ = (uint8_t)w;
  132. w >>= 8;
  133. *p++ = (uint8_t)w;
  134. }
  135. static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c) {
  136. return (w >> c) | (w << (32 - c));
  137. }
  138. static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c) {
  139. return (w >> c) | (w << (64 - c));
  140. }
  141. void clear_internal_memory(void *v, size_t n);
  142. #endif