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