| 1 |
/* VAES/AVX2 i386 accelerated AES for Libgcrypt |
| 2 |
* Copyright (C) 2023 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_I386 |
| 34 |
|
| 35 |
|
| 36 |
extern void _gcry_aes_aesni_prepare_decryption(RIJNDAEL_context *ctx); |
| 37 |
|
| 38 |
|
| 39 |
extern void _gcry_vaes_avx2_cbc_dec_i386 (const void *keysched, |
| 40 |
unsigned char *iv, |
| 41 |
void *outbuf_arg, |
| 42 |
const void *inbuf_arg, |
| 43 |
size_t nblocks, |
| 44 |
unsigned int nrounds); |
| 45 |
|
| 46 |
extern void _gcry_vaes_avx2_cfb_dec_i386 (const void *keysched, |
| 47 |
unsigned char *iv, |
| 48 |
void *outbuf_arg, |
| 49 |
const void *inbuf_arg, |
| 50 |
size_t nblocks, |
| 51 |
unsigned int nrounds); |
| 52 |
|
| 53 |
extern void _gcry_vaes_avx2_ctr_enc_i386 (const void *keysched, |
| 54 |
unsigned char *ctr, |
| 55 |
void *outbuf_arg, |
| 56 |
const void *inbuf_arg, |
| 57 |
size_t nblocks, |
| 58 |
unsigned int nrounds); |
| 59 |
|
| 60 |
extern void _gcry_vaes_avx2_ctr32le_enc_i386 (const void *keysched, |
| 61 |
unsigned char *ctr, |
| 62 |
void *outbuf_arg, |
| 63 |
const void *inbuf_arg, |
| 64 |
size_t nblocks, |
| 65 |
unsigned int nrounds); |
| 66 |
|
| 67 |
extern size_t _gcry_vaes_avx2_ocb_crypt_i386 (const void *keysched, |
| 68 |
void *outbuf_arg, |
| 69 |
const void *inbuf_arg, |
| 70 |
size_t nblocks, |
| 71 |
unsigned int nrounds, |
| 72 |
unsigned char *offset, |
| 73 |
unsigned char *checksum, |
| 74 |
unsigned int blkn, |
| 75 |
const void *L_table, |
| 76 |
int enc_dec_or_auth); |
| 77 |
|
| 78 |
extern void _gcry_vaes_avx2_xts_crypt_i386 (const void *keysched, |
| 79 |
unsigned char *tweak, |
| 80 |
void *outbuf_arg, |
| 81 |
const void *inbuf_arg, |
| 82 |
size_t nblocks, |
| 83 |
unsigned int nrounds, |
| 84 |
int encrypt); |
| 85 |
|
| 86 |
extern void _gcry_vaes_avx2_ecb_crypt_i386 (const void *keysched, |
| 87 |
int encrypt, |
| 88 |
void *outbuf_arg, |
| 89 |
const void *inbuf_arg, |
| 90 |
size_t nblocks, |
| 91 |
unsigned int nrounds); |
| 92 |
|
| 93 |
|
| 94 |
void |
| 95 |
_gcry_aes_vaes_ecb_crypt (void *context, void *outbuf, |
| 96 |
const void *inbuf, size_t nblocks, |
| 97 |
int encrypt) |
| 98 |
{ |
| 99 |
RIJNDAEL_context *ctx = context; |
| 100 |
const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32; |
| 101 |
unsigned int nrounds = ctx->rounds; |
| 102 |
|
| 103 |
if (!encrypt && !ctx->decryption_prepared) |
| 104 |
{ |
| 105 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 106 |
ctx->decryption_prepared = 1; |
| 107 |
} |
| 108 |
|
| 109 |
_gcry_vaes_avx2_ecb_crypt_i386 (keysched, encrypt, outbuf, inbuf, |
| 110 |
nblocks, nrounds); |
| 111 |
} |
| 112 |
|
| 113 |
void |
| 114 |
_gcry_aes_vaes_cbc_dec (void *context, unsigned char *iv, |
| 115 |
void *outbuf, const void *inbuf, |
| 116 |
size_t nblocks) |
| 117 |
{ |
| 118 |
RIJNDAEL_context *ctx = context; |
| 119 |
const void *keysched = ctx->keyschdec32; |
| 120 |
unsigned int nrounds = ctx->rounds; |
| 121 |
|
| 122 |
if (!ctx->decryption_prepared) |
| 123 |
{ |
| 124 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 125 |
ctx->decryption_prepared = 1; |
| 126 |
} |
| 127 |
|
| 128 |
_gcry_vaes_avx2_cbc_dec_i386 (keysched, iv, outbuf, inbuf, nblocks, nrounds); |
| 129 |
} |
| 130 |
|
| 131 |
void |
| 132 |
_gcry_aes_vaes_cfb_dec (void *context, unsigned char *iv, |
| 133 |
void *outbuf, const void *inbuf, |
| 134 |
size_t nblocks) |
| 135 |
{ |
| 136 |
RIJNDAEL_context *ctx = context; |
| 137 |
const void *keysched = ctx->keyschenc32; |
| 138 |
unsigned int nrounds = ctx->rounds; |
| 139 |
|
| 140 |
_gcry_vaes_avx2_cfb_dec_i386 (keysched, iv, outbuf, inbuf, nblocks, nrounds); |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
_gcry_aes_vaes_ctr_enc (void *context, unsigned char *iv, |
| 145 |
void *outbuf, const void *inbuf, |
| 146 |
size_t nblocks) |
| 147 |
{ |
| 148 |
RIJNDAEL_context *ctx = context; |
| 149 |
const void *keysched = ctx->keyschenc32; |
| 150 |
unsigned int nrounds = ctx->rounds; |
| 151 |
|
| 152 |
_gcry_vaes_avx2_ctr_enc_i386 (keysched, iv, outbuf, inbuf, nblocks, nrounds); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
_gcry_aes_vaes_ctr32le_enc (void *context, unsigned char *iv, |
| 157 |
void *outbuf, const void *inbuf, |
| 158 |
size_t nblocks) |
| 159 |
{ |
| 160 |
RIJNDAEL_context *ctx = context; |
| 161 |
const void *keysched = ctx->keyschenc32; |
| 162 |
unsigned int nrounds = ctx->rounds; |
| 163 |
|
| 164 |
_gcry_vaes_avx2_ctr32le_enc_i386 (keysched, iv, outbuf, inbuf, nblocks, |
| 165 |
nrounds); |
| 166 |
} |
| 167 |
|
| 168 |
size_t |
| 169 |
_gcry_aes_vaes_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg, |
| 170 |
const void *inbuf_arg, size_t nblocks, |
| 171 |
int encrypt) |
| 172 |
{ |
| 173 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 174 |
const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32; |
| 175 |
unsigned char *outbuf = outbuf_arg; |
| 176 |
const unsigned char *inbuf = inbuf_arg; |
| 177 |
u64 blkn; |
| 178 |
|
| 179 |
if (!encrypt && !ctx->decryption_prepared) |
| 180 |
{ |
| 181 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 182 |
ctx->decryption_prepared = 1; |
| 183 |
} |
| 184 |
|
| 185 |
blkn = c->u_mode.ocb.data_nblocks; |
| 186 |
c->u_mode.ocb.data_nblocks = blkn + nblocks; |
| 187 |
|
| 188 |
return _gcry_vaes_avx2_ocb_crypt_i386 (keysched, outbuf, inbuf, nblocks, |
| 189 |
ctx->rounds, c->u_iv.iv, c->u_ctr.ctr, |
| 190 |
(unsigned int)blkn, |
| 191 |
&c->u_mode.ocb.L[0], encrypt); |
| 192 |
} |
| 193 |
|
| 194 |
size_t |
| 195 |
_gcry_aes_vaes_ocb_auth (gcry_cipher_hd_t c, const void *inbuf_arg, |
| 196 |
size_t nblocks) |
| 197 |
{ |
| 198 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 199 |
const void *keysched = ctx->keyschenc32; |
| 200 |
const unsigned char *inbuf = inbuf_arg; |
| 201 |
u64 blkn = c->u_mode.ocb.aad_nblocks; |
| 202 |
|
| 203 |
c->u_mode.ocb.aad_nblocks = blkn + nblocks; |
| 204 |
|
| 205 |
return _gcry_vaes_avx2_ocb_crypt_i386 (keysched, NULL, inbuf, nblocks, |
| 206 |
ctx->rounds, c->u_mode.ocb.aad_offset, |
| 207 |
c->u_mode.ocb.aad_sum, |
| 208 |
(unsigned int)blkn, |
| 209 |
&c->u_mode.ocb.L[0], 2); |
| 210 |
} |
| 211 |
|
| 212 |
void |
| 213 |
_gcry_aes_vaes_xts_crypt (void *context, unsigned char *tweak, |
| 214 |
void *outbuf, const void *inbuf, |
| 215 |
size_t nblocks, int encrypt) |
| 216 |
{ |
| 217 |
RIJNDAEL_context *ctx = context; |
| 218 |
const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32; |
| 219 |
unsigned int nrounds = ctx->rounds; |
| 220 |
|
| 221 |
if (!encrypt && !ctx->decryption_prepared) |
| 222 |
{ |
| 223 |
_gcry_aes_aesni_prepare_decryption (ctx); |
| 224 |
ctx->decryption_prepared = 1; |
| 225 |
} |
| 226 |
|
| 227 |
_gcry_vaes_avx2_xts_crypt_i386 (keysched, tweak, outbuf, inbuf, nblocks, |
| 228 |
nrounds, encrypt); |
| 229 |
} |
| 230 |
|
| 231 |
#endif /* USE_VAES_I386 */ |