blake2-impl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. BLAKE2 reference source code package - reference C implementations
  3. Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
  4. To the extent possible under law, the author(s) have dedicated all copyright
  5. and related and neighboring rights to this software to the public domain
  6. worldwide. This software is distributed without any warranty.
  7. You should have received a copy of the CC0 Public Domain Dedication along with
  8. this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  9. */
  10. /* blake2-impl.h
  11. *
  12. * Copyright (C) 2006-2022 wolfSSL Inc.
  13. *
  14. * This file is part of wolfSSL.
  15. *
  16. * wolfSSL is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * wolfSSL is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  29. */
  30. #ifndef WOLFCRYPT_BLAKE2_IMPL_H
  31. #define WOLFCRYPT_BLAKE2_IMPL_H
  32. #include <wolfssl/wolfcrypt/types.h>
  33. static WC_INLINE word32 load32( const void *src )
  34. {
  35. #if defined(LITTLE_ENDIAN_ORDER)
  36. return *( word32 * )( src );
  37. #else
  38. const byte *p = ( byte * )src;
  39. word32 w = *p++;
  40. w |= ( word32 )( *p++ ) << 8;
  41. w |= ( word32 )( *p++ ) << 16;
  42. w |= ( word32 )( *p++ ) << 24;
  43. return w;
  44. #endif
  45. }
  46. static WC_INLINE word64 load64( const void *src )
  47. {
  48. #if defined(LITTLE_ENDIAN_ORDER)
  49. return *( word64 * )( src );
  50. #else
  51. const byte *p = ( byte * )src;
  52. word64 w = *p++;
  53. w |= ( word64 )( *p++ ) << 8;
  54. w |= ( word64 )( *p++ ) << 16;
  55. w |= ( word64 )( *p++ ) << 24;
  56. w |= ( word64 )( *p++ ) << 32;
  57. w |= ( word64 )( *p++ ) << 40;
  58. w |= ( word64 )( *p++ ) << 48;
  59. w |= ( word64 )( *p++ ) << 56;
  60. return w;
  61. #endif
  62. }
  63. static WC_INLINE void store32( void *dst, word32 w )
  64. {
  65. #if defined(LITTLE_ENDIAN_ORDER)
  66. *( word32 * )( dst ) = w;
  67. #else
  68. byte *p = ( byte * )dst;
  69. *p++ = ( byte )w; w >>= 8;
  70. *p++ = ( byte )w; w >>= 8;
  71. *p++ = ( byte )w; w >>= 8;
  72. *p++ = ( byte )w;
  73. #endif
  74. }
  75. static WC_INLINE void store64( void *dst, word64 w )
  76. {
  77. #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_GENERAL_ALIGNMENT)
  78. *( word64 * )( dst ) = w;
  79. #else
  80. byte *p = ( byte * )dst;
  81. *p++ = ( byte )w; w >>= 8;
  82. *p++ = ( byte )w; w >>= 8;
  83. *p++ = ( byte )w; w >>= 8;
  84. *p++ = ( byte )w; w >>= 8;
  85. *p++ = ( byte )w; w >>= 8;
  86. *p++ = ( byte )w; w >>= 8;
  87. *p++ = ( byte )w; w >>= 8;
  88. *p++ = ( byte )w;
  89. #endif
  90. }
  91. static WC_INLINE word64 load48( const void *src )
  92. {
  93. const byte *p = ( const byte * )src;
  94. word64 w = *p++;
  95. w |= ( word64 )( *p++ ) << 8;
  96. w |= ( word64 )( *p++ ) << 16;
  97. w |= ( word64 )( *p++ ) << 24;
  98. w |= ( word64 )( *p++ ) << 32;
  99. w |= ( word64 )( *p++ ) << 40;
  100. return w;
  101. }
  102. static WC_INLINE void store48( void *dst, word64 w )
  103. {
  104. byte *p = ( byte * )dst;
  105. *p++ = ( byte )w; w >>= 8;
  106. *p++ = ( byte )w; w >>= 8;
  107. *p++ = ( byte )w; w >>= 8;
  108. *p++ = ( byte )w; w >>= 8;
  109. *p++ = ( byte )w; w >>= 8;
  110. *p++ = ( byte )w;
  111. }
  112. static WC_INLINE word32 rotl32( const word32 w, const unsigned c )
  113. {
  114. return ( w << c ) | ( w >> ( 32 - c ) );
  115. }
  116. static WC_INLINE word64 rotl64( const word64 w, const unsigned c )
  117. {
  118. return ( w << c ) | ( w >> ( 64 - c ) );
  119. }
  120. static WC_INLINE word32 rotr32( const word32 w, const unsigned c )
  121. {
  122. return ( w >> c ) | ( w << ( 32 - c ) );
  123. }
  124. static WC_INLINE word64 rotr64( const word64 w, const unsigned c )
  125. {
  126. return ( w >> c ) | ( w << ( 64 - c ) );
  127. }
  128. /* prevents compiler optimizing out memset() */
  129. static WC_INLINE void secure_zero_memory( void *v, word64 n )
  130. {
  131. volatile byte *p = ( volatile byte * )v;
  132. while( n-- ) *p++ = 0;
  133. }
  134. #endif /* WOLFCRYPT_BLAKE2_IMPL_H */