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