blake2-impl.h 4.1 KB

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