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