2 * Copyright 2009 Colin Percival, 2011 ArtForz, 2012-2013 pooler
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * This file was originally written by Colin Percival as part of the Tarsnap
27 * online backup system.
34 #include <openssl/evp.h>
36 #include <emmintrin.h>
38 static inline uint32_t le32dec(const void *pp)
40 const uint8_t *p = (uint8_t const *)pp;
41 return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
42 ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
45 static inline void le32enc(void *pp, uint32_t x)
47 uint8_t *p = (uint8_t *)pp;
49 p[1] = (x >> 8) & 0xff;
50 p[2] = (x >> 16) & 0xff;
51 p[3] = (x >> 24) & 0xff;
54 static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4])
56 __m128i X0, X1, X2, X3;
60 X0 = B[0] = _mm_xor_si128(B[0], Bx[0]);
61 X1 = B[1] = _mm_xor_si128(B[1], Bx[1]);
62 X2 = B[2] = _mm_xor_si128(B[2], Bx[2]);
63 X3 = B[3] = _mm_xor_si128(B[3], Bx[3]);
65 for (i = 0; i < 8; i += 2) {
66 /* Operate on "columns". */
67 T = _mm_add_epi32(X0, X3);
68 X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
69 X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
70 T = _mm_add_epi32(X1, X0);
71 X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
72 X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
73 T = _mm_add_epi32(X2, X1);
74 X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
75 X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
76 T = _mm_add_epi32(X3, X2);
77 X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
78 X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
81 X1 = _mm_shuffle_epi32(X1, 0x93);
82 X2 = _mm_shuffle_epi32(X2, 0x4E);
83 X3 = _mm_shuffle_epi32(X3, 0x39);
85 /* Operate on "rows". */
86 T = _mm_add_epi32(X0, X1);
87 X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
88 X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
89 T = _mm_add_epi32(X3, X0);
90 X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
91 X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
92 T = _mm_add_epi32(X2, X3);
93 X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
94 X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
95 T = _mm_add_epi32(X1, X2);
96 X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
97 X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
100 X1 = _mm_shuffle_epi32(X1, 0x39);
101 X2 = _mm_shuffle_epi32(X2, 0x4E);
102 X3 = _mm_shuffle_epi32(X3, 0x93);
105 B[0] = _mm_add_epi32(B[0], X0);
106 B[1] = _mm_add_epi32(B[1], X1);
107 B[2] = _mm_add_epi32(B[2], X2);
108 B[3] = _mm_add_epi32(B[3], X3);
111 uint256 scrypt_blockhash__sse2(const uint8_t* input)
114 uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
123 V = (__m128i *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
125 void *const tmp = const_cast<uint8_t*>(input);
126 PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, input, 80, 1, EVP_sha256(), 128, B);
128 for (k = 0; k < 2; k++) {
129 for (i = 0; i < 16; i++) {
130 X.u32[k * 16 + i] = le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
134 for (i = 0; i < 1024; i++) {
135 for (k = 0; k < 8; k++)
136 V[i * 8 + k] = X.i128[k];
137 xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
138 xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
140 for (i = 0; i < 1024; i++) {
141 j = 8 * (X.u32[16] & 1023);
142 for (k = 0; k < 8; k++)
143 X.i128[k] = _mm_xor_si128(X.i128[k], V[j + k]);
144 xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
145 xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
148 for (k = 0; k < 2; k++) {
149 for (i = 0; i < 16; i++) {
150 le32enc(&B[(k * 16 + (i * 5 % 16)) * 4], X.u32[k * 16 + i]);
154 PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, B, 128, 1, EVP_sha256(), 32, (unsigned char*)&result);