blake2-impl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. BLAKE2 reference source code package - reference C implementations
  3. Copyright 2012, Samuel Neves <[email protected]>. You may use this under the
  4. terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
  5. your option. The terms of these licenses can be found at:
  6. - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  7. - OpenSSL license : https://www.openssl.org/source/license.html
  8. - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  9. More information about the BLAKE2 hash function can be found at
  10. https://blake2.net.
  11. */
  12. #ifndef BLAKE2_IMPL_H
  13. #define BLAKE2_IMPL_H
  14. #include <stdint.h>
  15. #include <string.h>
  16. #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
  17. #if defined(_MSC_VER)
  18. #define BLAKE2_INLINE __inline
  19. #elif defined(__GNUC__)
  20. #define BLAKE2_INLINE __inline__
  21. #else
  22. #define BLAKE2_INLINE
  23. #endif
  24. #else
  25. #define BLAKE2_INLINE inline
  26. #endif
  27. static BLAKE2_INLINE uint32_t load32( const void *src )
  28. {
  29. #if defined(NATIVE_LITTLE_ENDIAN)
  30. uint32_t w;
  31. memcpy(&w, src, sizeof w);
  32. return w;
  33. #else
  34. const uint8_t *p = ( const uint8_t * )src;
  35. return (( uint32_t )( p[0] ) << 0) |
  36. (( uint32_t )( p[1] ) << 8) |
  37. (( uint32_t )( p[2] ) << 16) |
  38. (( uint32_t )( p[3] ) << 24) ;
  39. #endif
  40. }
  41. static BLAKE2_INLINE uint64_t load64( const void *src )
  42. {
  43. #if defined(NATIVE_LITTLE_ENDIAN)
  44. uint64_t w;
  45. memcpy(&w, src, sizeof w);
  46. return w;
  47. #else
  48. const uint8_t *p = ( const uint8_t * )src;
  49. return (( uint64_t )( p[0] ) << 0) |
  50. (( uint64_t )( p[1] ) << 8) |
  51. (( uint64_t )( p[2] ) << 16) |
  52. (( uint64_t )( p[3] ) << 24) |
  53. (( uint64_t )( p[4] ) << 32) |
  54. (( uint64_t )( p[5] ) << 40) |
  55. (( uint64_t )( p[6] ) << 48) |
  56. (( uint64_t )( p[7] ) << 56) ;
  57. #endif
  58. }
  59. static BLAKE2_INLINE uint16_t load16( const void *src )
  60. {
  61. #if defined(NATIVE_LITTLE_ENDIAN)
  62. uint16_t w;
  63. memcpy(&w, src, sizeof w);
  64. return w;
  65. #else
  66. const uint8_t *p = ( const uint8_t * )src;
  67. return (( uint16_t )( p[0] ) << 0) |
  68. (( uint16_t )( p[1] ) << 8) ;
  69. #endif
  70. }
  71. static BLAKE2_INLINE void store16( void *dst, uint16_t w )
  72. {
  73. #if defined(NATIVE_LITTLE_ENDIAN)
  74. memcpy(dst, &w, sizeof w);
  75. #else
  76. uint8_t *p = ( uint8_t * )dst;
  77. *p++ = ( uint8_t )w; w >>= 8;
  78. *p++ = ( uint8_t )w;
  79. #endif
  80. }
  81. static BLAKE2_INLINE void store32( void *dst, uint32_t w )
  82. {
  83. #if defined(NATIVE_LITTLE_ENDIAN)
  84. memcpy(dst, &w, sizeof w);
  85. #else
  86. uint8_t *p = ( uint8_t * )dst;
  87. p[0] = (uint8_t)(w >> 0);
  88. p[1] = (uint8_t)(w >> 8);
  89. p[2] = (uint8_t)(w >> 16);
  90. p[3] = (uint8_t)(w >> 24);
  91. #endif
  92. }
  93. static BLAKE2_INLINE void store64( void *dst, uint64_t w )
  94. {
  95. #if defined(NATIVE_LITTLE_ENDIAN)
  96. memcpy(dst, &w, sizeof w);
  97. #else
  98. uint8_t *p = ( uint8_t * )dst;
  99. p[0] = (uint8_t)(w >> 0);
  100. p[1] = (uint8_t)(w >> 8);
  101. p[2] = (uint8_t)(w >> 16);
  102. p[3] = (uint8_t)(w >> 24);
  103. p[4] = (uint8_t)(w >> 32);
  104. p[5] = (uint8_t)(w >> 40);
  105. p[6] = (uint8_t)(w >> 48);
  106. p[7] = (uint8_t)(w >> 56);
  107. #endif
  108. }
  109. static BLAKE2_INLINE uint64_t load48( const void *src )
  110. {
  111. const uint8_t *p = ( const uint8_t * )src;
  112. return (( uint64_t )( p[0] ) << 0) |
  113. (( uint64_t )( p[1] ) << 8) |
  114. (( uint64_t )( p[2] ) << 16) |
  115. (( uint64_t )( p[3] ) << 24) |
  116. (( uint64_t )( p[4] ) << 32) |
  117. (( uint64_t )( p[5] ) << 40) ;
  118. }
  119. static BLAKE2_INLINE void store48( void *dst, uint64_t w )
  120. {
  121. uint8_t *p = ( uint8_t * )dst;
  122. p[0] = (uint8_t)(w >> 0);
  123. p[1] = (uint8_t)(w >> 8);
  124. p[2] = (uint8_t)(w >> 16);
  125. p[3] = (uint8_t)(w >> 24);
  126. p[4] = (uint8_t)(w >> 32);
  127. p[5] = (uint8_t)(w >> 40);
  128. }
  129. static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c )
  130. {
  131. return ( w >> c ) | ( w << ( 32 - c ) );
  132. }
  133. static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c )
  134. {
  135. return ( w >> c ) | ( w << ( 64 - c ) );
  136. }
  137. /* prevents compiler optimizing out memset() */
  138. static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n)
  139. {
  140. static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
  141. memset_v(v, 0, n);
  142. }
  143. #endif