| 1 |
/* sha256-intel-shaext.S - SHAEXT accelerated SHA-256 transform function |
| 2 |
* Copyright (C) 2018 Jussi Kivilinna <[email protected]> |
| 3 |
* |
| 4 |
* This file is part of Libgcrypt. |
| 5 |
* |
| 6 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#include "types.h" |
| 23 |
|
| 24 |
#if defined(HAVE_GCC_INLINE_ASM_SHAEXT) && \ |
| 25 |
defined(HAVE_GCC_INLINE_ASM_SSE41) && defined(USE_SHA256) && \ |
| 26 |
defined(ENABLE_SHAEXT_SUPPORT) |
| 27 |
|
| 28 |
#if _GCRY_GCC_VERSION >= 40400 /* 4.4 */ |
| 29 |
/* Prevent compiler from issuing SSE instructions between asm blocks. */ |
| 30 |
# pragma GCC target("no-sse") |
| 31 |
#endif |
| 32 |
#if __clang__ |
| 33 |
# pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function) |
| 34 |
#endif |
| 35 |
|
| 36 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 37 |
|
| 38 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 39 |
|
| 40 |
/* Two macros to be called prior and after the use of SHA-EXT |
| 41 |
instructions. There should be no external function calls between |
| 42 |
the use of these macros. There purpose is to make sure that the |
| 43 |
SSE regsiters are cleared and won't reveal any information about |
| 44 |
the key or the data. */ |
| 45 |
#ifdef __WIN64__ |
| 46 |
/* XMM6-XMM15 are callee-saved registers on WIN64. */ |
| 47 |
# define shaext_prepare_variable char win64tmp[2*16] |
| 48 |
# define shaext_prepare_variable_size sizeof(win64tmp) |
| 49 |
# define shaext_prepare() \ |
| 50 |
do { asm volatile ("movdqu %%xmm6, (%0)\n" \ |
| 51 |
"movdqu %%xmm7, (%1)\n" \ |
| 52 |
: \ |
| 53 |
: "r" (&win64tmp[0]), "r" (&win64tmp[16]) \ |
| 54 |
: "memory"); \ |
| 55 |
} while (0) |
| 56 |
# define shaext_cleanup(tmp0,tmp1) \ |
| 57 |
do { asm volatile ("movdqu (%0), %%xmm6\n" \ |
| 58 |
"movdqu (%1), %%xmm7\n" \ |
| 59 |
"pxor %%xmm0, %%xmm0\n" \ |
| 60 |
"pxor %%xmm1, %%xmm1\n" \ |
| 61 |
"pxor %%xmm2, %%xmm2\n" \ |
| 62 |
"pxor %%xmm3, %%xmm3\n" \ |
| 63 |
"pxor %%xmm4, %%xmm4\n" \ |
| 64 |
"pxor %%xmm5, %%xmm5\n" \ |
| 65 |
"movdqa %%xmm0, (%2)\n\t" \ |
| 66 |
"movdqa %%xmm0, (%3)\n\t" \ |
| 67 |
: \ |
| 68 |
: "r" (&win64tmp[0]), "r" (&win64tmp[16]), \ |
| 69 |
"r" (tmp0), "r" (tmp1) \ |
| 70 |
: "memory"); \ |
| 71 |
} while (0) |
| 72 |
#else |
| 73 |
# define shaext_prepare_variable |
| 74 |
# define shaext_prepare_variable_size 0 |
| 75 |
# define shaext_prepare() do { } while (0) |
| 76 |
# define shaext_cleanup(tmp0,tmp1) \ |
| 77 |
do { asm volatile ("pxor %%xmm0, %%xmm0\n" \ |
| 78 |
"pxor %%xmm1, %%xmm1\n" \ |
| 79 |
"pxor %%xmm2, %%xmm2\n" \ |
| 80 |
"pxor %%xmm3, %%xmm3\n" \ |
| 81 |
"pxor %%xmm4, %%xmm4\n" \ |
| 82 |
"pxor %%xmm5, %%xmm5\n" \ |
| 83 |
"pxor %%xmm6, %%xmm6\n" \ |
| 84 |
"pxor %%xmm7, %%xmm7\n" \ |
| 85 |
"movdqa %%xmm0, (%0)\n\t" \ |
| 86 |
"movdqa %%xmm0, (%1)\n\t" \ |
| 87 |
: \ |
| 88 |
: "r" (tmp0), "r" (tmp1) \ |
| 89 |
: "memory"); \ |
| 90 |
} while (0) |
| 91 |
#endif |
| 92 |
|
| 93 |
typedef struct u128_s |
| 94 |
{ |
| 95 |
u32 a, b, c, d; |
| 96 |
} u128_t; |
| 97 |
|
| 98 |
/* |
| 99 |
* Transform nblks*64 bytes (nblks*16 32-bit words) at DATA. |
| 100 |
*/ |
| 101 |
unsigned int ASM_FUNC_ATTR |
| 102 |
_gcry_sha256_transform_intel_shaext(u32 state[8], const unsigned char *data, |
| 103 |
size_t nblks) |
| 104 |
{ |
| 105 |
static const unsigned char bshuf_mask[16] __attribute__ ((aligned (16))) = |
| 106 |
{ 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12 }; |
| 107 |
static const u128_t K[16] __attribute__ ((aligned (16))) = |
| 108 |
{ |
| 109 |
{ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5 }, |
| 110 |
{ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5 }, |
| 111 |
{ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3 }, |
| 112 |
{ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174 }, |
| 113 |
{ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc }, |
| 114 |
{ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da }, |
| 115 |
{ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7 }, |
| 116 |
{ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967 }, |
| 117 |
{ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13 }, |
| 118 |
{ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85 }, |
| 119 |
{ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3 }, |
| 120 |
{ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070 }, |
| 121 |
{ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5 }, |
| 122 |
{ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 }, |
| 123 |
{ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208 }, |
| 124 |
{ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 } |
| 125 |
}; |
| 126 |
char save_buf[2 * 16 + 15]; |
| 127 |
char *abef_save; |
| 128 |
char *cdgh_save; |
| 129 |
shaext_prepare_variable; |
| 130 |
|
| 131 |
if (nblks == 0) |
| 132 |
return 0; |
| 133 |
|
| 134 |
shaext_prepare (); |
| 135 |
|
| 136 |
asm volatile ("" : "=r" (abef_save) : "0" (save_buf) : "memory"); |
| 137 |
abef_save = abef_save + (-(uintptr_t)abef_save & 15); |
| 138 |
cdgh_save = abef_save + 16; |
| 139 |
|
| 140 |
/* byteswap mask => XMM7 */ |
| 141 |
asm volatile ("movdqa %[mask], %%xmm7\n\t" /* Preload mask */ |
| 142 |
: |
| 143 |
: [mask] "m" (*bshuf_mask) |
| 144 |
: "memory"); |
| 145 |
|
| 146 |
/* Load state.. ABEF_SAVE => STATE0 XMM1, CDGH_STATE => STATE1 XMM2 */ |
| 147 |
asm volatile ("movups 16(%[state]), %%xmm1\n\t" /* HGFE (xmm=EFGH) */ |
| 148 |
"movups 0(%[state]), %%xmm0\n\t" /* DCBA (xmm=ABCD) */ |
| 149 |
"movaps %%xmm1, %%xmm2\n\t" |
| 150 |
"shufps $0x11, %%xmm0, %%xmm1\n\t" /* ABEF (xmm=FEBA) */ |
| 151 |
"shufps $0xbb, %%xmm0, %%xmm2\n\t" /* CDGH (xmm=HGDC) */ |
| 152 |
: |
| 153 |
: [state] "r" (state) |
| 154 |
: "memory" ); |
| 155 |
|
| 156 |
/* Load message */ |
| 157 |
asm volatile ("movdqu 0*16(%[data]), %%xmm3\n\t" |
| 158 |
"movdqu 1*16(%[data]), %%xmm4\n\t" |
| 159 |
"movdqu 2*16(%[data]), %%xmm5\n\t" |
| 160 |
"movdqu 3*16(%[data]), %%xmm6\n\t" |
| 161 |
"pshufb %%xmm7, %%xmm3\n\t" |
| 162 |
"pshufb %%xmm7, %%xmm4\n\t" |
| 163 |
"pshufb %%xmm7, %%xmm5\n\t" |
| 164 |
"pshufb %%xmm7, %%xmm6\n\t" |
| 165 |
: |
| 166 |
: [data] "r" (data) |
| 167 |
: "memory" ); |
| 168 |
data += 64; |
| 169 |
|
| 170 |
do |
| 171 |
{ |
| 172 |
/* Save state */ |
| 173 |
asm volatile ("movdqa %%xmm1, (%[abef_save])\n\t" |
| 174 |
"movdqa %%xmm2, (%[cdgh_save])\n\t" |
| 175 |
: |
| 176 |
: [abef_save] "r" (abef_save), [cdgh_save] "r" (cdgh_save) |
| 177 |
: "memory" ); |
| 178 |
|
| 179 |
/* Round 0..3 */ |
| 180 |
asm volatile ("movdqa %%xmm3, %%xmm0\n\t" |
| 181 |
"paddd %[constants], %%xmm0\n\t" |
| 182 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 183 |
"psrldq $8, %%xmm0\n\t" |
| 184 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 185 |
: |
| 186 |
: [constants] "m" (K[0].a) |
| 187 |
: "memory" ); |
| 188 |
|
| 189 |
/* Round 4..7 */ |
| 190 |
asm volatile ("movdqa %%xmm4, %%xmm0\n\t" |
| 191 |
"paddd %[constants], %%xmm0\n\t" |
| 192 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 193 |
"psrldq $8, %%xmm0\n\t" |
| 194 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 195 |
"sha256msg1 %%xmm4, %%xmm3\n\t" |
| 196 |
: |
| 197 |
: [constants] "m" (K[1].a) |
| 198 |
: "memory" ); |
| 199 |
|
| 200 |
/* Round 8..11 */ |
| 201 |
asm volatile ("movdqa %%xmm5, %%xmm0\n\t" |
| 202 |
"paddd %[constants], %%xmm0\n\t" |
| 203 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 204 |
"psrldq $8, %%xmm0\n\t" |
| 205 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 206 |
"sha256msg1 %%xmm5, %%xmm4\n\t" |
| 207 |
: |
| 208 |
: [constants] "m" (K[2].a) |
| 209 |
: "memory" ); |
| 210 |
|
| 211 |
#define ROUND(k, MSG0, MSG1, MSG2, MSG3) \ |
| 212 |
asm volatile ("movdqa %%"MSG0", %%xmm0\n\t" \ |
| 213 |
"paddd %[constants], %%xmm0\n\t" \ |
| 214 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" \ |
| 215 |
"movdqa %%"MSG0", %%xmm7\n\t" \ |
| 216 |
"palignr $4, %%"MSG3", %%xmm7\n\t" \ |
| 217 |
"paddd %%xmm7, %%"MSG1"\n\t" \ |
| 218 |
"sha256msg2 %%"MSG0", %%"MSG1"\n\t" \ |
| 219 |
"psrldq $8, %%xmm0\n\t" \ |
| 220 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" \ |
| 221 |
"sha256msg1 %%"MSG0", %%"MSG3"\n\t" \ |
| 222 |
: \ |
| 223 |
: [constants] "m" (K[k].a) \ |
| 224 |
: "memory" ) |
| 225 |
|
| 226 |
/* Rounds 12..15 to 48..51 */ |
| 227 |
ROUND(3, "xmm6", "xmm3", "xmm4", "xmm5"); |
| 228 |
ROUND(4, "xmm3", "xmm4", "xmm5", "xmm6"); |
| 229 |
ROUND(5, "xmm4", "xmm5", "xmm6", "xmm3"); |
| 230 |
ROUND(6, "xmm5", "xmm6", "xmm3", "xmm4"); |
| 231 |
ROUND(7, "xmm6", "xmm3", "xmm4", "xmm5"); |
| 232 |
ROUND(8, "xmm3", "xmm4", "xmm5", "xmm6"); |
| 233 |
ROUND(9, "xmm4", "xmm5", "xmm6", "xmm3"); |
| 234 |
ROUND(10, "xmm5", "xmm6", "xmm3", "xmm4"); |
| 235 |
ROUND(11, "xmm6", "xmm3", "xmm4", "xmm5"); |
| 236 |
ROUND(12, "xmm3", "xmm4", "xmm5", "xmm6"); |
| 237 |
|
| 238 |
if (--nblks == 0) |
| 239 |
break; |
| 240 |
|
| 241 |
/* Round 52..55 */ |
| 242 |
asm volatile ("movdqa %%xmm4, %%xmm0\n\t" |
| 243 |
"paddd %[constants], %%xmm0\n\t" |
| 244 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 245 |
"movdqa %%xmm4, %%xmm7\n\t" |
| 246 |
"palignr $4, %%xmm3, %%xmm7\n\t" |
| 247 |
"movdqu 0*16(%[data]), %%xmm3\n\t" |
| 248 |
"paddd %%xmm7, %%xmm5\n\t" |
| 249 |
"sha256msg2 %%xmm4, %%xmm5\n\t" |
| 250 |
"psrldq $8, %%xmm0\n\t" |
| 251 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 252 |
: |
| 253 |
: [constants] "m" (K[13].a), [data] "r" (data) |
| 254 |
: "memory" ); |
| 255 |
|
| 256 |
/* Round 56..59 */ |
| 257 |
asm volatile ("movdqa %%xmm5, %%xmm0\n\t" |
| 258 |
"paddd %[constants], %%xmm0\n\t" |
| 259 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 260 |
"movdqa %%xmm5, %%xmm7\n\t" |
| 261 |
"palignr $4, %%xmm4, %%xmm7\n\t" |
| 262 |
"movdqu 1*16(%[data]), %%xmm4\n\t" |
| 263 |
"paddd %%xmm7, %%xmm6\n\t" |
| 264 |
"movdqa %[mask], %%xmm7\n\t" /* Reload mask */ |
| 265 |
"sha256msg2 %%xmm5, %%xmm6\n\t" |
| 266 |
"movdqu 2*16(%[data]), %%xmm5\n\t" |
| 267 |
"psrldq $8, %%xmm0\n\t" |
| 268 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 269 |
: |
| 270 |
: [constants] "m" (K[14].a), [mask] "m" (*bshuf_mask), |
| 271 |
[data] "r" (data) |
| 272 |
: "memory" ); |
| 273 |
|
| 274 |
/* Round 60..63 */ |
| 275 |
asm volatile ("movdqa %%xmm6, %%xmm0\n\t" |
| 276 |
"pshufb %%xmm7, %%xmm3\n\t" |
| 277 |
"movdqu 3*16(%[data]), %%xmm6\n\t" |
| 278 |
"paddd %[constants], %%xmm0\n\t" |
| 279 |
"pshufb %%xmm7, %%xmm4\n\t" |
| 280 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 281 |
"psrldq $8, %%xmm0\n\t" |
| 282 |
"pshufb %%xmm7, %%xmm5\n\t" |
| 283 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 284 |
: |
| 285 |
: [constants] "m" (K[15].a), [data] "r" (data) |
| 286 |
: "memory" ); |
| 287 |
data += 64; |
| 288 |
|
| 289 |
/* Merge states */ |
| 290 |
asm volatile ("paddd (%[abef_save]), %%xmm1\n\t" |
| 291 |
"paddd (%[cdgh_save]), %%xmm2\n\t" |
| 292 |
"pshufb %%xmm7, %%xmm6\n\t" |
| 293 |
: |
| 294 |
: [abef_save] "r" (abef_save), [cdgh_save] "r" (cdgh_save) |
| 295 |
: "memory" ); |
| 296 |
} |
| 297 |
while (1); |
| 298 |
|
| 299 |
/* Round 52..55 */ |
| 300 |
asm volatile ("movdqa %%xmm4, %%xmm0\n\t" |
| 301 |
"paddd %[constants], %%xmm0\n\t" |
| 302 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 303 |
"movdqa %%xmm4, %%xmm7\n\t" |
| 304 |
"palignr $4, %%xmm3, %%xmm7\n\t" |
| 305 |
"paddd %%xmm7, %%xmm5\n\t" |
| 306 |
"sha256msg2 %%xmm4, %%xmm5\n\t" |
| 307 |
"psrldq $8, %%xmm0\n\t" |
| 308 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 309 |
: |
| 310 |
: [constants] "m" (K[13].a) |
| 311 |
: "memory" ); |
| 312 |
|
| 313 |
/* Round 56..59 */ |
| 314 |
asm volatile ("movdqa %%xmm5, %%xmm0\n\t" |
| 315 |
"paddd %[constants], %%xmm0\n\t" |
| 316 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 317 |
"movdqa %%xmm5, %%xmm7\n\t" |
| 318 |
"palignr $4, %%xmm4, %%xmm7\n\t" |
| 319 |
"paddd %%xmm7, %%xmm6\n\t" |
| 320 |
"movdqa %[mask], %%xmm7\n\t" /* Reload mask */ |
| 321 |
"sha256msg2 %%xmm5, %%xmm6\n\t" |
| 322 |
"psrldq $8, %%xmm0\n\t" |
| 323 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 324 |
: |
| 325 |
: [constants] "m" (K[14].a), [mask] "m" (*bshuf_mask) |
| 326 |
: "memory" ); |
| 327 |
|
| 328 |
/* Round 60..63 */ |
| 329 |
asm volatile ("movdqa %%xmm6, %%xmm0\n\t" |
| 330 |
"paddd %[constants], %%xmm0\n\t" |
| 331 |
"sha256rnds2 %%xmm1, %%xmm2\n\t" |
| 332 |
"psrldq $8, %%xmm0\n\t" |
| 333 |
"sha256rnds2 %%xmm2, %%xmm1\n\t" |
| 334 |
: |
| 335 |
: [constants] "m" (K[15].a) |
| 336 |
: "memory" ); |
| 337 |
|
| 338 |
/* Merge states */ |
| 339 |
asm volatile ("paddd (%[abef_save]), %%xmm1\n\t" |
| 340 |
"paddd (%[cdgh_save]), %%xmm2\n\t" |
| 341 |
: |
| 342 |
: [abef_save] "r" (abef_save), [cdgh_save] "r" (cdgh_save) |
| 343 |
: "memory" ); |
| 344 |
|
| 345 |
/* Save state (XMM1=FEBA, XMM2=HGDC) */ |
| 346 |
asm volatile ("movaps %%xmm1, %%xmm0\n\t" |
| 347 |
"shufps $0x11, %%xmm2, %%xmm1\n\t" /* xmm=ABCD */ |
| 348 |
"shufps $0xbb, %%xmm2, %%xmm0\n\t" /* xmm=EFGH */ |
| 349 |
"movups %%xmm1, 16(%[state])\n\t" |
| 350 |
"movups %%xmm0, 0(%[state])\n\t" |
| 351 |
: |
| 352 |
: [state] "r" (state) |
| 353 |
: "memory" ); |
| 354 |
|
| 355 |
shaext_cleanup (abef_save, cdgh_save); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
#if __clang__ |
| 360 |
# pragma clang attribute pop |
| 361 |
#endif |
| 362 |
|
| 363 |
#endif /* HAVE_GCC_INLINE_ASM_SHA_EXT */ |