Home | History | Annotate | Download | only in impl
      1 /*
      2    BLAKE2 reference source code package - optimized C implementations
      3 
      4    Copyright 2012, Samuel Neves <sneves (at) 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 
      8    - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
      9    - OpenSSL license   : https://www.openssl.org/source/license.html
     10    - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
     11 
     12    More information about the BLAKE2 hash function can be found at
     13    https://blake2.net.
     14 */
     15 #pragma once
     16 #ifndef __BLAKE2S_ROUND_H__
     17 #define __BLAKE2S_ROUND_H__
     18 
     19 #define LOADU(p)  _mm_loadu_si128( (const __m128i *)(p) )
     20 #define STOREU(p,r) _mm_storeu_si128((__m128i *)(p), r)
     21 
     22 #define TOF(reg) _mm_castsi128_ps((reg))
     23 #define TOI(reg) _mm_castps_si128((reg))
     24 
     25 #define LIKELY(x) __builtin_expect((x),1)
     26 
     27 
     28 /* Microarchitecture-specific macros */
     29 #ifndef HAVE_XOP
     30 #ifdef HAVE_SSSE3
     31 #define _mm_roti_epi32(r, c) ( \
     32                 (8==-(c)) ? _mm_shuffle_epi8(r,r8) \
     33               : (16==-(c)) ? _mm_shuffle_epi8(r,r16) \
     34               : _mm_xor_si128(_mm_srli_epi32( (r), -(c) ),_mm_slli_epi32( (r), 32-(-(c)) )) )
     35 #else
     36 #define _mm_roti_epi32(r, c) _mm_xor_si128(_mm_srli_epi32( (r), -(c) ),_mm_slli_epi32( (r), 32-(-(c)) ))
     37 #endif
     38 #else
     39 /* ... */
     40 #endif
     41 
     42 
     43 #define G1(row1,row2,row3,row4,buf) \
     44   row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \
     45   row4 = _mm_xor_si128( row4, row1 ); \
     46   row4 = _mm_roti_epi32(row4, -16); \
     47   row3 = _mm_add_epi32( row3, row4 );   \
     48   row2 = _mm_xor_si128( row2, row3 ); \
     49   row2 = _mm_roti_epi32(row2, -12);
     50 
     51 #define G2(row1,row2,row3,row4,buf) \
     52   row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \
     53   row4 = _mm_xor_si128( row4, row1 ); \
     54   row4 = _mm_roti_epi32(row4, -8); \
     55   row3 = _mm_add_epi32( row3, row4 );   \
     56   row2 = _mm_xor_si128( row2, row3 ); \
     57   row2 = _mm_roti_epi32(row2, -7);
     58 
     59 #define DIAGONALIZE(row1,row2,row3,row4) \
     60   row4 = _mm_shuffle_epi32( row4, _MM_SHUFFLE(2,1,0,3) ); \
     61   row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(1,0,3,2) ); \
     62   row2 = _mm_shuffle_epi32( row2, _MM_SHUFFLE(0,3,2,1) );
     63 
     64 #define UNDIAGONALIZE(row1,row2,row3,row4) \
     65   row4 = _mm_shuffle_epi32( row4, _MM_SHUFFLE(0,3,2,1) ); \
     66   row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(1,0,3,2) ); \
     67   row2 = _mm_shuffle_epi32( row2, _MM_SHUFFLE(2,1,0,3) );
     68 
     69 #if defined(HAVE_XOP)
     70 #include "blake2s-load-xop.h"
     71 #elif defined(HAVE_SSE41)
     72 #include "blake2s-load-sse41.h"
     73 #else
     74 #include "blake2s-load-sse2.h"
     75 #endif
     76 
     77 #define ROUND(r)  \
     78   LOAD_MSG_ ##r ##_1(buf1); \
     79   G1(row1,row2,row3,row4,buf1); \
     80   LOAD_MSG_ ##r ##_2(buf2); \
     81   G2(row1,row2,row3,row4,buf2); \
     82   DIAGONALIZE(row1,row2,row3,row4); \
     83   LOAD_MSG_ ##r ##_3(buf3); \
     84   G1(row1,row2,row3,row4,buf3); \
     85   LOAD_MSG_ ##r ##_4(buf4); \
     86   G2(row1,row2,row3,row4,buf4); \
     87   UNDIAGONALIZE(row1,row2,row3,row4); \
     88 
     89 #endif
     90 
     91