| 1 |
/* VAES/AVX2 AMD64 accelerated AES for Libgcrypt |
| 2 |
* Copyright (C) 2021,2026 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 |
|
| 21 |
#include <config.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
|
| 25 |
#include "types.h" /* for byte and u32 typedefs */ |
| 26 |
#include "g10lib.h" |
| 27 |
#include "cipher.h" |
| 28 |
#include "bufhelp.h" |
| 29 |
#include "rijndael-internal.h" |
| 30 |
#include "./cipher-internal.h" |
| 31 |
|
| 32 |
|
| 33 |
#ifdef USE_VAES |
| 34 |
|
| 35 |
|
| 36 |
# ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS |
| 37 |
# define ASM_FUNC_ABI __attribute__((sysv_abi)) |
| 38 |
# else |
| 39 |
# define ASM_FUNC_ABI |
| 40 |
# endif |
| 41 |
|
| 42 |
|
| 43 |
extern void _gcry_aes_aesni_prepare_decryption (RIJNDAEL_context *ctx); |
| 44 |
|
| 45 |
|
| 46 |
extern void _gcry_vaes_avx2_cbc_dec_amd64 (const void *keysched, |
| 47 |
unsigned char *iv, |
| 48 |
void *outbuf_arg, |
| 49 |
const void *inbuf_arg, |
| 50 |
size_t nblocks, |
| 51 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 52 |
|
| 53 |
extern void _gcry_vaes_avx2_cfb_dec_amd64 (const void *keysched, |
| 54 |
unsigned char *iv, |
| 55 |
void *outbuf_arg, |
| 56 |
const void *inbuf_arg, |
| 57 |
size_t nblocks, |
| 58 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 59 |
|
| 60 |
extern void _gcry_vaes_avx2_ctr_enc_amd64 (const void *keysched, |
| 61 |
unsigned char *ctr, |
| 62 |
void *outbuf_arg, |
| 63 |
const void *inbuf_arg, |
| 64 |
size_t nblocks, |
| 65 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 66 |
|
| 67 |
extern void _gcry_vaes_avx2_ctr32le_enc_amd64 (const void *keysched, |
| 68 |
unsigned char *ctr, |
| 69 |
void *outbuf_arg, |
| 70 |
const void *inbuf_arg, |
| 71 |
size_t nblocks, |
| 72 |
unsigned int nrounds) |
| 73 |
ASM_FUNC_ABI; |
| 74 |
|
| 75 |
extern size_t _gcry_vaes_avx2_ocb_crypt_amd64 (const void *keysched, |
| 76 |
unsigned int blkn, |
| 77 |
void *outbuf_arg, |
| 78 |
const void *inbuf_arg, |
| 79 |
size_t nblocks, |
| 80 |
unsigned int nrounds, |
| 81 |
unsigned char *offset, |
| 82 |
unsigned char *checksum, |
| 83 |
unsigned char *L_table, |
| 84 |
int enc_dec_auth) ASM_FUNC_ABI; |
| 85 |
|
| 86 |
extern void _gcry_vaes_avx2_xts_crypt_amd64 (const void *keysched, |
| 87 |
unsigned char *tweak, |
| 88 |
void *outbuf_arg, |
| 89 |
const void *inbuf_arg, |
| 90 |
size_t nblocks, |
| 91 |
unsigned int nrounds, |
| 92 |
int encrypt) ASM_FUNC_ABI; |
| 93 |
|
| 94 |
extern void _gcry_vaes_avx2_ecb_crypt_amd64 (const void *keysched, |
| 95 |
int encrypt, |
| 96 |
void *outbuf_arg, |
| 97 |
const void *inbuf_arg, |
| 98 |
size_t nblocks, |
| 99 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 100 |
|
| 101 |
|
| 102 |
#ifdef USE_VAES_AVX512 |
| 103 |
extern void _gcry_vaes_avx512_cbc_dec_amd64 (const void *keysched, |
| 104 |
unsigned char *iv, |
| 105 |
void *outbuf_arg, |
| 106 |
const void *inbuf_arg, |
| 107 |
size_t nblocks, |
| 108 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 109 |
|
| 110 |
extern void _gcry_vaes_avx512_cfb_dec_amd64 (const void *keysched, |
| 111 |
unsigned char *iv, |
| 112 |
void *outbuf_arg, |
| 113 |
const void *inbuf_arg, |
| 114 |
size_t nblocks, |
| 115 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 116 |
|
| 117 |
extern void _gcry_vaes_avx512_ctr_enc_amd64 (const void *keysched, |
| 118 |
unsigned char *ctr, |
| 119 |
void *outbuf_arg, |
| 120 |
const void *inbuf_arg, |
| 121 |
size_t nblocks, |
| 122 |
unsigned int nrounds) ASM_FUNC_ABI; |
| 123 |
|
| 124 |
extern void _gcry_vaes_avx512_ctr32le_enc_amd64 (const void *keysched, |
| 125 |
unsigned char *ctr, |
| 126 |
void *outbuf_arg, |
| 127 |
const void *inbuf_arg, |
| 128 |
size_t nblocks, |
| 129 |
unsigned int nrounds) |
| 130 |
ASM_FUNC_ABI; |
| 131 |
|
| 132 |
extern size_t |
| 133 |
_gcry_vaes_avx512_ocb_aligned_crypt_amd64 (const void *keysched, |
| 134 |
unsigned int blkn, |
| 135 |
void *outbuf_arg, |
| 136 |
const void *inbuf_arg, |
| 137 |
size_t nblocks, |
| 138 |
unsigned int nrounds, |
| 139 |
unsigned char *offset, |
| 140 |
unsigned char *checksum, |
| 141 |
unsigned char *L_table, |
| 142 |
int enc_dec_auth) ASM_FUNC_ABI; |
| 143 |
|
| 144 |
extern void _gcry_vaes_avx512_xts_crypt_amd64 (const void *keysched, |
| 145 |
unsigned char *tweak, |
| 146 |
void *outbuf_arg, |
| 147 |
const void *inbuf_arg, |
| 148 |
size_t nblocks, |
| 149 |
unsigned int nrounds, |
| 150 |
int encrypt) ASM_FUNC_ABI; |
| 151 |
|
| 152 |
extern void _gcry_vaes_avx512_ecb_crypt_amd64 (const void *keysched, |
| 153 |
int encrypt, |
| 154 |
void *outbuf_arg, |
| 155 |
const void *inbuf_arg, |
| 156 |
size_t nblocks, |
| 157 |
unsigned int nrounds) |
| 158 |
ASM_FUNC_ABI; |
| 159 |
#endif |
| 160 |
|
| 161 |
|
| 162 |
void |
| 163 |
_gcry_aes_vaes_ecb_crypt (void *context, void *outbuf, |
| 164 |
const void *inbuf, size_t nblocks, |
| 165 |
int encrypt) |
| 166 |
{ |
| 167 |
RIJNDAEL_context *ctx = context; |
| 168 |
const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32; |
| 169 |
unsigned int nrounds = ctx->rounds; |
| 170 |
|
| 171 |
if (!encrypt && !ctx->decryption_prepared) |
| 172 |
{ |
| 173 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 174 |
ctx->decryption_prepared = 1; |
| 175 |
} |
| 176 |
|
| 177 |
#ifdef USE_VAES_AVX512 |
| 178 |
if (ctx->use_vaes_avx512) |
| 179 |
{ |
| 180 |
_gcry_vaes_avx512_ecb_crypt_amd64 (keysched, encrypt, outbuf, inbuf, |
| 181 |
nblocks, nrounds); |
| 182 |
return; |
| 183 |
} |
| 184 |
#endif |
| 185 |
|
| 186 |
_gcry_vaes_avx2_ecb_crypt_amd64 (keysched, encrypt, outbuf, inbuf, |
| 187 |
nblocks, nrounds); |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
_gcry_aes_vaes_cbc_dec (void *context, unsigned char *iv, |
| 192 |
void *outbuf, const void *inbuf, |
| 193 |
size_t nblocks) |
| 194 |
{ |
| 195 |
RIJNDAEL_context *ctx = context; |
| 196 |
const void *keysched = ctx->keyschdec32; |
| 197 |
unsigned int nrounds = ctx->rounds; |
| 198 |
|
| 199 |
if (!ctx->decryption_prepared) |
| 200 |
{ |
| 201 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 202 |
ctx->decryption_prepared = 1; |
| 203 |
} |
| 204 |
|
| 205 |
#ifdef USE_VAES_AVX512 |
| 206 |
if (ctx->use_vaes_avx512) |
| 207 |
{ |
| 208 |
_gcry_vaes_avx512_cbc_dec_amd64 (keysched, iv, outbuf, inbuf, |
| 209 |
nblocks, nrounds); |
| 210 |
return; |
| 211 |
} |
| 212 |
#endif |
| 213 |
|
| 214 |
_gcry_vaes_avx2_cbc_dec_amd64 (keysched, iv, outbuf, inbuf, nblocks, nrounds); |
| 215 |
} |
| 216 |
|
| 217 |
void |
| 218 |
_gcry_aes_vaes_cfb_dec (void *context, unsigned char *iv, |
| 219 |
void *outbuf, const void *inbuf, |
| 220 |
size_t nblocks) |
| 221 |
{ |
| 222 |
RIJNDAEL_context *ctx = context; |
| 223 |
const void *keysched = ctx->keyschenc32; |
| 224 |
unsigned int nrounds = ctx->rounds; |
| 225 |
|
| 226 |
#ifdef USE_VAES_AVX512 |
| 227 |
if (ctx->use_vaes_avx512) |
| 228 |
{ |
| 229 |
_gcry_vaes_avx512_cfb_dec_amd64 (keysched, iv, outbuf, inbuf, |
| 230 |
nblocks, nrounds); |
| 231 |
return; |
| 232 |
} |
| 233 |
#endif |
| 234 |
|
| 235 |
_gcry_vaes_avx2_cfb_dec_amd64 (keysched, iv, outbuf, inbuf, nblocks, nrounds); |
| 236 |
} |
| 237 |
|
| 238 |
void |
| 239 |
_gcry_aes_vaes_ctr_enc (void *context, unsigned char *iv, |
| 240 |
void *outbuf, const void *inbuf, |
| 241 |
size_t nblocks) |
| 242 |
{ |
| 243 |
RIJNDAEL_context *ctx = context; |
| 244 |
const void *keysched = ctx->keyschenc32; |
| 245 |
unsigned int nrounds = ctx->rounds; |
| 246 |
|
| 247 |
#ifdef USE_VAES_AVX512 |
| 248 |
if (ctx->use_vaes_avx512) |
| 249 |
{ |
| 250 |
_gcry_vaes_avx512_ctr_enc_amd64 (keysched, iv, outbuf, inbuf, |
| 251 |
nblocks, nrounds); |
| 252 |
return; |
| 253 |
} |
| 254 |
#endif |
| 255 |
|
| 256 |
_gcry_vaes_avx2_ctr_enc_amd64 (keysched, iv, outbuf, inbuf, nblocks, nrounds); |
| 257 |
} |
| 258 |
|
| 259 |
void |
| 260 |
_gcry_aes_vaes_ctr32le_enc (void *context, unsigned char *iv, |
| 261 |
void *outbuf, const void *inbuf, |
| 262 |
size_t nblocks) |
| 263 |
{ |
| 264 |
RIJNDAEL_context *ctx = context; |
| 265 |
const void *keysched = ctx->keyschenc32; |
| 266 |
unsigned int nrounds = ctx->rounds; |
| 267 |
|
| 268 |
#ifdef USE_VAES_AVX512 |
| 269 |
if (ctx->use_vaes_avx512) |
| 270 |
{ |
| 271 |
_gcry_vaes_avx512_ctr32le_enc_amd64 (keysched, iv, outbuf, inbuf, |
| 272 |
nblocks, nrounds); |
| 273 |
return; |
| 274 |
} |
| 275 |
#endif |
| 276 |
|
| 277 |
_gcry_vaes_avx2_ctr32le_enc_amd64 (keysched, iv, outbuf, inbuf, nblocks, |
| 278 |
nrounds); |
| 279 |
} |
| 280 |
|
| 281 |
size_t |
| 282 |
_gcry_aes_vaes_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg, |
| 283 |
const void *inbuf_arg, size_t nblocks, |
| 284 |
int encrypt) |
| 285 |
{ |
| 286 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 287 |
const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32; |
| 288 |
unsigned char *outbuf = outbuf_arg; |
| 289 |
const unsigned char *inbuf = inbuf_arg; |
| 290 |
unsigned int nrounds = ctx->rounds; |
| 291 |
u64 blkn = c->u_mode.ocb.data_nblocks; |
| 292 |
|
| 293 |
if (!encrypt && !ctx->decryption_prepared) |
| 294 |
{ |
| 295 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 296 |
ctx->decryption_prepared = 1; |
| 297 |
} |
| 298 |
|
| 299 |
#ifdef USE_VAES_AVX512 |
| 300 |
if (ctx->use_vaes_avx512 && nblocks >= 32) |
| 301 |
{ |
| 302 |
/* Get number of blocks to align nblk to 32 for L-array optimization. */ |
| 303 |
unsigned int num_to_align = (-blkn) & 31; |
| 304 |
if (nblocks - num_to_align >= 32) |
| 305 |
{ |
| 306 |
if (num_to_align) |
| 307 |
{ |
| 308 |
_gcry_vaes_avx2_ocb_crypt_amd64 (keysched, (unsigned int)blkn, |
| 309 |
outbuf, inbuf, num_to_align, |
| 310 |
nrounds, c->u_iv.iv, |
| 311 |
c->u_ctr.ctr, c->u_mode.ocb.L[0], |
| 312 |
encrypt); |
| 313 |
blkn += num_to_align; |
| 314 |
outbuf += num_to_align * BLOCKSIZE; |
| 315 |
inbuf += num_to_align * BLOCKSIZE; |
| 316 |
nblocks -= num_to_align; |
| 317 |
} |
| 318 |
|
| 319 |
c->u_mode.ocb.data_nblocks = blkn + nblocks; |
| 320 |
|
| 321 |
return _gcry_vaes_avx512_ocb_aligned_crypt_amd64 (keysched, |
| 322 |
(unsigned int)blkn, |
| 323 |
outbuf, inbuf, |
| 324 |
nblocks, |
| 325 |
nrounds, c->u_iv.iv, |
| 326 |
c->u_ctr.ctr, |
| 327 |
c->u_mode.ocb.L[0], |
| 328 |
encrypt); |
| 329 |
} |
| 330 |
} |
| 331 |
#endif |
| 332 |
|
| 333 |
c->u_mode.ocb.data_nblocks = blkn + nblocks; |
| 334 |
|
| 335 |
return _gcry_vaes_avx2_ocb_crypt_amd64 (keysched, (unsigned int)blkn, outbuf, |
| 336 |
inbuf, nblocks, nrounds, c->u_iv.iv, |
| 337 |
c->u_ctr.ctr, c->u_mode.ocb.L[0], |
| 338 |
encrypt); |
| 339 |
} |
| 340 |
|
| 341 |
size_t |
| 342 |
_gcry_aes_vaes_ocb_auth (gcry_cipher_hd_t c, const void *inbuf_arg, |
| 343 |
size_t nblocks) |
| 344 |
{ |
| 345 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 346 |
const void *keysched = ctx->keyschenc32; |
| 347 |
const unsigned char *inbuf = inbuf_arg; |
| 348 |
unsigned int nrounds = ctx->rounds; |
| 349 |
u64 blkn = c->u_mode.ocb.aad_nblocks; |
| 350 |
|
| 351 |
#ifdef USE_VAES_AVX512 |
| 352 |
if (ctx->use_vaes_avx512 && nblocks >= 32) |
| 353 |
{ |
| 354 |
/* Get number of blocks to align nblk to 32 for L-array optimization. */ |
| 355 |
unsigned int num_to_align = (-blkn) & 31; |
| 356 |
if (nblocks - num_to_align >= 32) |
| 357 |
{ |
| 358 |
if (num_to_align) |
| 359 |
{ |
| 360 |
_gcry_vaes_avx2_ocb_crypt_amd64 (keysched, (unsigned int)blkn, |
| 361 |
NULL, inbuf, num_to_align, |
| 362 |
nrounds, |
| 363 |
c->u_mode.ocb.aad_offset, |
| 364 |
c->u_mode.ocb.aad_sum, |
| 365 |
c->u_mode.ocb.L[0], 2); |
| 366 |
blkn += num_to_align; |
| 367 |
inbuf += num_to_align * BLOCKSIZE; |
| 368 |
nblocks -= num_to_align; |
| 369 |
} |
| 370 |
|
| 371 |
c->u_mode.ocb.aad_nblocks = blkn + nblocks; |
| 372 |
|
| 373 |
return _gcry_vaes_avx512_ocb_aligned_crypt_amd64 ( |
| 374 |
keysched, (unsigned int)blkn, NULL, inbuf, |
| 375 |
nblocks, nrounds, c->u_mode.ocb.aad_offset, |
| 376 |
c->u_mode.ocb.aad_sum, c->u_mode.ocb.L[0], 2); |
| 377 |
} |
| 378 |
} |
| 379 |
#endif |
| 380 |
|
| 381 |
c->u_mode.ocb.aad_nblocks = blkn + nblocks; |
| 382 |
|
| 383 |
return _gcry_vaes_avx2_ocb_crypt_amd64 (keysched, (unsigned int)blkn, NULL, |
| 384 |
inbuf, nblocks, nrounds, |
| 385 |
c->u_mode.ocb.aad_offset, |
| 386 |
c->u_mode.ocb.aad_sum, |
| 387 |
c->u_mode.ocb.L[0], 2); |
| 388 |
} |
| 389 |
|
| 390 |
void |
| 391 |
_gcry_aes_vaes_xts_crypt (void *context, unsigned char *tweak, |
| 392 |
void *outbuf, const void *inbuf, |
| 393 |
size_t nblocks, int encrypt) |
| 394 |
{ |
| 395 |
RIJNDAEL_context *ctx = context; |
| 396 |
const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32; |
| 397 |
unsigned int nrounds = ctx->rounds; |
| 398 |
|
| 399 |
if (!encrypt && !ctx->decryption_prepared) |
| 400 |
{ |
| 401 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 402 |
ctx->decryption_prepared = 1; |
| 403 |
} |
| 404 |
|
| 405 |
#ifdef USE_VAES_AVX512 |
| 406 |
if (ctx->use_vaes_avx512) |
| 407 |
{ |
| 408 |
_gcry_vaes_avx512_xts_crypt_amd64 (keysched, tweak, outbuf, inbuf, |
| 409 |
nblocks, nrounds, encrypt); |
| 410 |
return; |
| 411 |
} |
| 412 |
#endif |
| 413 |
|
| 414 |
_gcry_vaes_avx2_xts_crypt_amd64 (keysched, tweak, outbuf, inbuf, nblocks, |
| 415 |
nrounds, encrypt); |
| 416 |
} |
| 417 |
|
| 418 |
#endif /* USE_VAES */ |