1 /* 2 BLAKE2 reference source code package - optimized C implementations 3 4 Copyright 2012, Samuel Neves <sneves@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 #ifndef BLAKE2S_ROUND_H 16 #define BLAKE2S_ROUND_H 17 18 #define LOADU(p) _mm_loadu_si128( (const __m128i *)(p) ) 19 #define STOREU(p,r) _mm_storeu_si128((__m128i *)(p), r) 20 21 #define TOF(reg) _mm_castsi128_ps((reg)) 22 #define TOI(reg) _mm_castps_si128((reg)) 23 24 #define LIKELY(x) __builtin_expect((x),1) 25 26 27 /* Microarchitecture-specific macros */ 28 #ifndef HAVE_XOP 29 #ifdef HAVE_SSSE3 30 #define _mm_roti_epi32(r, c) ( \ 31 (8==-(c)) ? _mm_shuffle_epi8(r,r8) \ 32 : (16==-(c)) ? _mm_shuffle_epi8(r,r16) \ 33 : _mm_xor_si128(_mm_srli_epi32( (r), -(c) ),_mm_slli_epi32( (r), 32-(-(c)) )) ) 34 #else 35 #define _mm_roti_epi32(r, c) _mm_xor_si128(_mm_srli_epi32( (r), -(c) ),_mm_slli_epi32( (r), 32-(-(c)) )) 36 #endif 37 #else 38 /* ... */ 39 #endif 40 41 42 #define G1(row1,row2,row3,row4,buf) \ 43 row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \ 44 row4 = _mm_xor_si128( row4, row1 ); \ 45 row4 = _mm_roti_epi32(row4, -16); \ 46 row3 = _mm_add_epi32( row3, row4 ); \ 47 row2 = _mm_xor_si128( row2, row3 ); \ 48 row2 = _mm_roti_epi32(row2, -12); 49 50 #define G2(row1,row2,row3,row4,buf) \ 51 row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \ 52 row4 = _mm_xor_si128( row4, row1 ); \ 53 row4 = _mm_roti_epi32(row4, -8); \ 54 row3 = _mm_add_epi32( row3, row4 ); \ 55 row2 = _mm_xor_si128( row2, row3 ); \ 56 row2 = _mm_roti_epi32(row2, -7); 57 58 #define DIAGONALIZE(row1,row2,row3,row4) \ 59 row4 = _mm_shuffle_epi32( row4, _MM_SHUFFLE(2,1,0,3) ); \ 60 row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(1,0,3,2) ); \ 61 row2 = _mm_shuffle_epi32( row2, _MM_SHUFFLE(0,3,2,1) ); 62 63 #define UNDIAGONALIZE(row1,row2,row3,row4) \ 64 row4 = _mm_shuffle_epi32( row4, _MM_SHUFFLE(0,3,2,1) ); \ 65 row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(1,0,3,2) ); \ 66 row2 = _mm_shuffle_epi32( row2, _MM_SHUFFLE(2,1,0,3) ); 67 68 #if defined(HAVE_XOP) 69 #include "blake2s-load-xop.h" 70 #elif defined(HAVE_SSE41) 71 #include "blake2s-load-sse41.h" 72 #else 73 #include "blake2s-load-sse2.h" 74 #endif 75 76 #define ROUND(r) \ 77 LOAD_MSG_ ##r ##_1(buf1); \ 78 G1(row1,row2,row3,row4,buf1); \ 79 LOAD_MSG_ ##r ##_2(buf2); \ 80 G2(row1,row2,row3,row4,buf2); \ 81 DIAGONALIZE(row1,row2,row3,row4); \ 82 LOAD_MSG_ ##r ##_3(buf3); \ 83 G1(row1,row2,row3,row4,buf3); \ 84 LOAD_MSG_ ##r ##_4(buf4); \ 85 G2(row1,row2,row3,row4,buf4); \ 86 UNDIAGONALIZE(row1,row2,row3,row4); \ 87 88 #endif 89