| 1 |
/* Rijndael (AES) for GnuPG - s390x/zSeries AES implementation |
| 2 |
* Copyright (C) 2020 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 "rijndael-internal.h" |
| 23 |
#include "cipher-internal.h" |
| 24 |
#include "bufhelp.h" |
| 25 |
|
| 26 |
#ifdef USE_S390X_CRYPTO |
| 27 |
|
| 28 |
#include "asm-inline-s390x.h" |
| 29 |
|
| 30 |
#define NO_INLINE __attribute__((noinline)) |
| 31 |
|
| 32 |
struct aes_s390x_gcm_params_s |
| 33 |
{ |
| 34 |
u32 reserved[3]; |
| 35 |
u32 counter_value; |
| 36 |
u64 tag[2]; |
| 37 |
u64 hash_subkey[2]; |
| 38 |
u64 total_aad_length; |
| 39 |
u64 total_cipher_length; |
| 40 |
u32 initial_counter_value[4]; |
| 41 |
u64 key[4]; |
| 42 |
}; |
| 43 |
|
| 44 |
#define DECL_QUERY_FUNC(instruction, opcode) \ |
| 45 |
static u128_t instruction ##_query(void) \ |
| 46 |
{ \ |
| 47 |
static u128_t function_codes = 0; \ |
| 48 |
static int initialized = 0; \ |
| 49 |
register unsigned long reg0 asm("0") = 0; \ |
| 50 |
register void *reg1 asm("1") = &function_codes; \ |
| 51 |
u128_t r1, r2; \ |
| 52 |
\ |
| 53 |
if (initialized) \ |
| 54 |
return function_codes; \ |
| 55 |
\ |
| 56 |
asm volatile ("0: .insn rre," #opcode " << 16, %[r1], %[r2]\n\t" \ |
| 57 |
" brc 1,0b\n\t" \ |
| 58 |
: [r1] "=a" (r1), [r2] "=a" (r2) \ |
| 59 |
: [reg0] "r" (reg0), [reg1] "r" (reg1) \ |
| 60 |
: "cc", "memory"); \ |
| 61 |
\ |
| 62 |
initialized = 1; \ |
| 63 |
return function_codes; \ |
| 64 |
} |
| 65 |
|
| 66 |
#define DECL_EXECUTE_FUNC(instruction, opcode, param_const) \ |
| 67 |
static ALWAYS_INLINE size_t \ |
| 68 |
instruction ##_execute(unsigned int func, param_const void *param_block, \ |
| 69 |
void *dst, const void *src, size_t src_len) \ |
| 70 |
{ \ |
| 71 |
register unsigned long reg0 asm("0") = func; \ |
| 72 |
register param_const byte *reg1 asm("1") = param_block; \ |
| 73 |
u128_t r1 = ((u128_t)(uintptr_t)dst << 64); \ |
| 74 |
u128_t r2 = ((u128_t)(uintptr_t)src << 64) | (u64)src_len; \ |
| 75 |
\ |
| 76 |
asm volatile ("0: .insn rre," #opcode " << 16, %[r1], %[r2]\n\t" \ |
| 77 |
" brc 1,0b\n\t" \ |
| 78 |
: [r1] "+a" (r1), [r2] "+a" (r2) \ |
| 79 |
: [func] "r" (reg0), [param_ptr] "r" (reg1) \ |
| 80 |
: "cc", "memory"); \ |
| 81 |
\ |
| 82 |
return (u64)r2; \ |
| 83 |
} |
| 84 |
|
| 85 |
DECL_QUERY_FUNC(km, 0xb92e); |
| 86 |
DECL_QUERY_FUNC(kmc, 0xb92f); |
| 87 |
DECL_QUERY_FUNC(kmac, 0xb91e); |
| 88 |
DECL_QUERY_FUNC(kmf, 0xb92a); |
| 89 |
DECL_QUERY_FUNC(kmo, 0xb92b); |
| 90 |
|
| 91 |
DECL_EXECUTE_FUNC(km, 0xb92e, const); |
| 92 |
DECL_EXECUTE_FUNC(kmc, 0xb92f, ); |
| 93 |
DECL_EXECUTE_FUNC(kmac, 0xb91e, ); |
| 94 |
DECL_EXECUTE_FUNC(kmf, 0xb92a, ); |
| 95 |
DECL_EXECUTE_FUNC(kmo, 0xb92b, ); |
| 96 |
|
| 97 |
static u128_t kma_query(void) |
| 98 |
{ |
| 99 |
static u128_t function_codes = 0; |
| 100 |
static int initialized = 0; |
| 101 |
register unsigned long reg0 asm("0") = 0; |
| 102 |
register void *reg1 asm("1") = &function_codes; |
| 103 |
u128_t r1, r2, r3; |
| 104 |
|
| 105 |
if (initialized) |
| 106 |
return function_codes; |
| 107 |
|
| 108 |
asm volatile ("0: .insn rrf,0xb929 << 16, %[r1], %[r2], %[r3], 0\n\t" |
| 109 |
" brc 1,0b\n\t" |
| 110 |
: [r1] "=a" (r1), [r2] "=a" (r2), [r3] "=a" (r3) |
| 111 |
: [reg0] "r" (reg0), [reg1] "r" (reg1) |
| 112 |
: "cc", "memory"); |
| 113 |
|
| 114 |
initialized = 1; |
| 115 |
return function_codes; |
| 116 |
} |
| 117 |
|
| 118 |
static ALWAYS_INLINE void |
| 119 |
kma_execute(unsigned int func, void *param_block, byte *dst, const byte *src, |
| 120 |
size_t src_len, const byte *aad, size_t aad_len) |
| 121 |
{ |
| 122 |
register unsigned long reg0 asm("0") = func; |
| 123 |
register byte *reg1 asm("1") = param_block; |
| 124 |
u128_t r1 = ((u128_t)(uintptr_t)dst << 64); |
| 125 |
u128_t r2 = ((u128_t)(uintptr_t)src << 64) | (u64)src_len; |
| 126 |
u128_t r3 = ((u128_t)(uintptr_t)aad << 64) | (u64)aad_len; |
| 127 |
|
| 128 |
asm volatile ("0: .insn rrf,0xb929 << 16, %[r1], %[r2], %[r3], 0\n\t" |
| 129 |
" brc 1,0b\n\t" |
| 130 |
: [r1] "+a" (r1), [r2] "+a" (r2), [r3] "+a" (r3), |
| 131 |
[func] "+r" (reg0) |
| 132 |
: [param_ptr] "r" (reg1) |
| 133 |
: "cc", "memory"); |
| 134 |
} |
| 135 |
|
| 136 |
unsigned int _gcry_aes_s390x_encrypt(const RIJNDAEL_context *ctx, |
| 137 |
unsigned char *dst, |
| 138 |
const unsigned char *src) |
| 139 |
{ |
| 140 |
km_execute (ctx->km_func | KM_ENCRYPT, ctx->keyschenc, dst, src, |
| 141 |
BLOCKSIZE); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
unsigned int _gcry_aes_s390x_decrypt(const RIJNDAEL_context *ctx, |
| 146 |
unsigned char *dst, |
| 147 |
const unsigned char *src) |
| 148 |
{ |
| 149 |
km_execute (ctx->km_func | KM_DECRYPT, ctx->keyschenc, dst, src, |
| 150 |
BLOCKSIZE); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
static void aes_s390x_cbc_enc(void *context, unsigned char *iv, |
| 155 |
void *outbuf_arg, const void *inbuf_arg, |
| 156 |
size_t nblocks, int cbc_mac) |
| 157 |
{ |
| 158 |
RIJNDAEL_context *ctx = context; |
| 159 |
byte *out = outbuf_arg; |
| 160 |
const byte *in = inbuf_arg; |
| 161 |
u128_t params[3]; |
| 162 |
|
| 163 |
/* Prepare parameter block. */ |
| 164 |
memcpy (¶ms[0], iv, BLOCKSIZE); |
| 165 |
memcpy (¶ms[1], ctx->keyschenc, 32); |
| 166 |
|
| 167 |
if (cbc_mac) |
| 168 |
{ |
| 169 |
kmac_execute (ctx->kmac_func | KM_ENCRYPT, ¶ms, NULL, in, |
| 170 |
nblocks * BLOCKSIZE); |
| 171 |
memcpy (out, ¶ms[0], BLOCKSIZE); |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
kmc_execute (ctx->kmc_func | KM_ENCRYPT, ¶ms, out, in, |
| 176 |
nblocks * BLOCKSIZE); |
| 177 |
} |
| 178 |
|
| 179 |
/* Update IV with OCV. */ |
| 180 |
memcpy (iv, ¶ms[0], BLOCKSIZE); |
| 181 |
|
| 182 |
wipememory (¶ms, sizeof(params)); |
| 183 |
} |
| 184 |
|
| 185 |
static void aes_s390x_cbc_dec(void *context, unsigned char *iv, |
| 186 |
void *outbuf_arg, const void *inbuf_arg, |
| 187 |
size_t nblocks) |
| 188 |
{ |
| 189 |
RIJNDAEL_context *ctx = context; |
| 190 |
byte *out = outbuf_arg; |
| 191 |
const byte *in = inbuf_arg; |
| 192 |
u128_t params[3]; |
| 193 |
|
| 194 |
/* Prepare parameter block (ICV & key). */ |
| 195 |
memcpy (¶ms[0], iv, BLOCKSIZE); |
| 196 |
memcpy (¶ms[1], ctx->keyschenc, 32); |
| 197 |
|
| 198 |
kmc_execute (ctx->kmc_func | KM_DECRYPT, ¶ms, out, in, |
| 199 |
nblocks * BLOCKSIZE); |
| 200 |
|
| 201 |
/* Update IV with OCV. */ |
| 202 |
memcpy (iv, ¶ms[0], BLOCKSIZE); |
| 203 |
|
| 204 |
wipememory (¶ms, sizeof(params)); |
| 205 |
} |
| 206 |
|
| 207 |
static void aes_s390x_cfb128_enc(void *context, unsigned char *iv, |
| 208 |
void *outbuf_arg, const void *inbuf_arg, |
| 209 |
size_t nblocks) |
| 210 |
{ |
| 211 |
RIJNDAEL_context *ctx = context; |
| 212 |
byte *out = outbuf_arg; |
| 213 |
const byte *in = inbuf_arg; |
| 214 |
unsigned int function; |
| 215 |
u128_t params[3]; |
| 216 |
|
| 217 |
/* Prepare parameter block. */ |
| 218 |
memcpy (¶ms[0], iv, BLOCKSIZE); |
| 219 |
memcpy (¶ms[1], ctx->keyschenc, 32); |
| 220 |
|
| 221 |
function = ctx->kmf_func | KM_ENCRYPT | KMF_LCFB_16; |
| 222 |
kmf_execute (function, ¶ms, out, in, nblocks * BLOCKSIZE); |
| 223 |
|
| 224 |
/* Update IV with OCV. */ |
| 225 |
memcpy (iv, ¶ms[0], BLOCKSIZE); |
| 226 |
|
| 227 |
wipememory (¶ms, sizeof(params)); |
| 228 |
} |
| 229 |
|
| 230 |
static void aes_s390x_cfb128_dec(void *context, unsigned char *iv, |
| 231 |
void *outbuf_arg, const void *inbuf_arg, |
| 232 |
size_t nblocks) |
| 233 |
{ |
| 234 |
RIJNDAEL_context *ctx = context; |
| 235 |
u128_t blocks[64]; |
| 236 |
byte *out = outbuf_arg; |
| 237 |
const byte *in = inbuf_arg; |
| 238 |
size_t max_blocks_used = 0; |
| 239 |
|
| 240 |
/* AES128-CFB128 decryption speed using KMF was observed to be the same as |
| 241 |
* the KMF encryption, ~1.03 cpb. Expection was to see similar performance |
| 242 |
* as for AES128-CBC decryption as decryption for both modes should be |
| 243 |
* parallalizeble (CBC shows ~0.22 cpb). Therefore there is quite a bit |
| 244 |
* of room for improvement and implementation below using KM instruction |
| 245 |
* shows ~0.70 cpb speed, ~30% improvement over KMF instruction. |
| 246 |
*/ |
| 247 |
|
| 248 |
while (nblocks >= 64) |
| 249 |
{ |
| 250 |
/* Copy IV to encrypt buffer, copy (nblocks - 1) input blocks to |
| 251 |
* encrypt buffer and update IV. */ |
| 252 |
asm volatile ("mvc 0(16, %[blocks]), 0(%[iv])\n\t" |
| 253 |
"mvc 16(240, %[blocks]), 0(%[in])\n\t" |
| 254 |
"mvc 256(256, %[blocks]), 240(%[in])\n\t" |
| 255 |
"mvc 512(256, %[blocks]), 496(%[in])\n\t" |
| 256 |
"mvc 768(256, %[blocks]), 752(%[in])\n\t" |
| 257 |
"mvc 0(16, %[iv]), 1008(%[in])\n\t" |
| 258 |
: |
| 259 |
: [in] "a" (in), [out] "a" (out), [blocks] "a" (blocks), |
| 260 |
[iv] "a" (iv) |
| 261 |
: "memory"); |
| 262 |
|
| 263 |
/* Perform encryption of temporary buffer. */ |
| 264 |
km_execute (ctx->km_func | KM_ENCRYPT, ctx->keyschenc, blocks, blocks, |
| 265 |
64 * BLOCKSIZE); |
| 266 |
|
| 267 |
/* Xor encrypt buffer with input blocks and store to output blocks. */ |
| 268 |
asm volatile ("xc 0(256, %[blocks]), 0(%[in])\n\t" |
| 269 |
"xc 256(256, %[blocks]), 256(%[in])\n\t" |
| 270 |
"xc 512(256, %[blocks]), 512(%[in])\n\t" |
| 271 |
"xc 768(256, %[blocks]), 768(%[in])\n\t" |
| 272 |
"mvc 0(256, %[out]), 0(%[blocks])\n\t" |
| 273 |
"mvc 256(256, %[out]), 256(%[blocks])\n\t" |
| 274 |
"mvc 512(256, %[out]), 512(%[blocks])\n\t" |
| 275 |
"mvc 768(256, %[out]), 768(%[blocks])\n\t" |
| 276 |
: |
| 277 |
: [in] "a" (in), [out] "a" (out), [blocks] "a" (blocks) |
| 278 |
: "memory"); |
| 279 |
|
| 280 |
max_blocks_used = 64; |
| 281 |
in += 64 * BLOCKSIZE; |
| 282 |
out += 64 * BLOCKSIZE; |
| 283 |
nblocks -= 64; |
| 284 |
} |
| 285 |
|
| 286 |
if (nblocks) |
| 287 |
{ |
| 288 |
unsigned int pos = 0; |
| 289 |
size_t in_nblocks = nblocks; |
| 290 |
size_t num_in = 0; |
| 291 |
|
| 292 |
max_blocks_used = max_blocks_used < nblocks ? nblocks : max_blocks_used; |
| 293 |
|
| 294 |
/* Copy IV to encrypt buffer. */ |
| 295 |
asm volatile ("mvc 0(16, %[blocks]), 0(%[iv])\n\t" |
| 296 |
: |
| 297 |
: [blocks] "a" (blocks), [iv] "a" (iv) |
| 298 |
: "memory"); |
| 299 |
pos += 1; |
| 300 |
|
| 301 |
#define CFB_MOVE_BLOCKS(block_oper, move_nbytes) \ |
| 302 |
block_oper (in_nblocks - 1 >= move_nbytes / BLOCKSIZE) \ |
| 303 |
{ \ |
| 304 |
unsigned int move_nblocks = move_nbytes / BLOCKSIZE; \ |
| 305 |
asm volatile ("mvc 0(" #move_nbytes ", %[blocks_x]), 0(%[in])\n\t" \ |
| 306 |
: \ |
| 307 |
: [blocks_x] "a" (&blocks[pos]), [in] "a" (in) \ |
| 308 |
: "memory"); \ |
| 309 |
num_in += move_nblocks; \ |
| 310 |
in += move_nblocks * BLOCKSIZE; \ |
| 311 |
pos += move_nblocks; \ |
| 312 |
in_nblocks -= move_nblocks; \ |
| 313 |
} |
| 314 |
|
| 315 |
/* Copy (nblocks - 1) input blocks to encrypt buffer. */ |
| 316 |
CFB_MOVE_BLOCKS(while, 256); |
| 317 |
CFB_MOVE_BLOCKS(if, 128); |
| 318 |
CFB_MOVE_BLOCKS(if, 64); |
| 319 |
CFB_MOVE_BLOCKS(if, 32); |
| 320 |
CFB_MOVE_BLOCKS(if, 16); |
| 321 |
|
| 322 |
#undef CFB_MOVE_BLOCKS |
| 323 |
|
| 324 |
/* Update IV. */ |
| 325 |
asm volatile ("mvc 0(16, %[iv]), 0(%[in])\n\t" |
| 326 |
: |
| 327 |
: [iv] "a" (iv), [in] "a" (in) |
| 328 |
: "memory"); |
| 329 |
num_in += 1; |
| 330 |
in += BLOCKSIZE; |
| 331 |
|
| 332 |
/* Perform encryption of temporary buffer. */ |
| 333 |
km_execute (ctx->km_func | KM_ENCRYPT, ctx->keyschenc, blocks, blocks, |
| 334 |
nblocks * BLOCKSIZE); |
| 335 |
|
| 336 |
/* Xor encrypt buffer with input blocks and store to output blocks. */ |
| 337 |
pos = 0; |
| 338 |
in -= nblocks * BLOCKSIZE; |
| 339 |
|
| 340 |
#define CFB_XOR_BLOCKS(block_oper, xor_nbytes) \ |
| 341 |
block_oper (nblocks >= xor_nbytes / BLOCKSIZE) \ |
| 342 |
{ \ |
| 343 |
unsigned int xor_nblocks = xor_nbytes / BLOCKSIZE; \ |
| 344 |
asm volatile ("xc 0(" #xor_nbytes ", %[blocks_x]), 0(%[in])\n\t" \ |
| 345 |
"mvc 0(" #xor_nbytes ", %[out]), 0(%[blocks_x])\n\t" \ |
| 346 |
: \ |
| 347 |
: [blocks_x] "a" (&blocks[pos]), [out] "a" (out), \ |
| 348 |
[in] "a" (in) \ |
| 349 |
: "memory"); \ |
| 350 |
out += xor_nblocks * BLOCKSIZE; \ |
| 351 |
in += xor_nblocks * BLOCKSIZE; \ |
| 352 |
nblocks -= xor_nblocks; \ |
| 353 |
pos += xor_nblocks; \ |
| 354 |
} |
| 355 |
|
| 356 |
CFB_XOR_BLOCKS(while, 256); |
| 357 |
CFB_XOR_BLOCKS(if, 128); |
| 358 |
CFB_XOR_BLOCKS(if, 64); |
| 359 |
CFB_XOR_BLOCKS(if, 32); |
| 360 |
CFB_XOR_BLOCKS(if, 16); |
| 361 |
|
| 362 |
#undef CFB_XOR_BLOCKS |
| 363 |
} |
| 364 |
|
| 365 |
if (max_blocks_used) |
| 366 |
wipememory (&blocks, max_blocks_used * BLOCKSIZE); |
| 367 |
} |
| 368 |
|
| 369 |
static void aes_s390x_ofb_enc(void *context, unsigned char *iv, |
| 370 |
void *outbuf_arg, const void *inbuf_arg, |
| 371 |
size_t nblocks) |
| 372 |
{ |
| 373 |
RIJNDAEL_context *ctx = context; |
| 374 |
byte *out = outbuf_arg; |
| 375 |
const byte *in = inbuf_arg; |
| 376 |
unsigned int function; |
| 377 |
u128_t params[3]; |
| 378 |
|
| 379 |
/* Prepare parameter block. */ |
| 380 |
memcpy (¶ms[0], iv, BLOCKSIZE); |
| 381 |
memcpy (¶ms[1], ctx->keyschenc, 32); |
| 382 |
|
| 383 |
function = ctx->kmo_func | KM_ENCRYPT; |
| 384 |
kmo_execute (function, ¶ms, out, in, nblocks * BLOCKSIZE); |
| 385 |
|
| 386 |
/* Update IV with OCV. */ |
| 387 |
memcpy (iv, ¶ms[0], BLOCKSIZE); |
| 388 |
|
| 389 |
wipememory (¶ms, sizeof(params)); |
| 390 |
} |
| 391 |
|
| 392 |
static void aes_s390x_ctr128_enc(void *context, unsigned char *ctr, |
| 393 |
void *outbuf_arg, const void *inbuf_arg, |
| 394 |
size_t nblocks) |
| 395 |
{ |
| 396 |
RIJNDAEL_context *ctx = context; |
| 397 |
byte *out = outbuf_arg; |
| 398 |
const byte *in = inbuf_arg; |
| 399 |
unsigned int function; |
| 400 |
struct aes_s390x_gcm_params_s params; |
| 401 |
|
| 402 |
memset (¶ms.hash_subkey, 0, sizeof(params.hash_subkey)); |
| 403 |
memcpy (¶ms.key, ctx->keyschenc, 32); |
| 404 |
|
| 405 |
function = ctx->kma_func | KM_DECRYPT | KMA_HS | KMA_LAAD; |
| 406 |
|
| 407 |
while (nblocks) |
| 408 |
{ |
| 409 |
u64 to_overflow = (u64)0xFFFFFFFFU + 1 - buf_get_be32 (ctr + 12); |
| 410 |
u64 ncurr = nblocks > to_overflow ? to_overflow : nblocks; |
| 411 |
|
| 412 |
/* Prepare parameter block. */ |
| 413 |
memset (¶ms.reserved, 0, sizeof(params.reserved)); |
| 414 |
buf_put_be32 (¶ms.counter_value, buf_get_be32(ctr + 12) - 1); |
| 415 |
memcpy (¶ms.initial_counter_value, ctr, 16); |
| 416 |
params.initial_counter_value[3] = params.counter_value; |
| 417 |
memset (¶ms.tag, 0, sizeof(params.tag)); |
| 418 |
params.total_aad_length = 0; |
| 419 |
params.total_cipher_length = 0; |
| 420 |
|
| 421 |
/* Update counter. */ |
| 422 |
cipher_block_add (ctr, ncurr, BLOCKSIZE); |
| 423 |
if (ncurr == (u64)0xFFFFFFFFU + 1) |
| 424 |
cipher_block_add (ctr, 1, BLOCKSIZE); |
| 425 |
|
| 426 |
/* Perform CTR using KMA-GCM. */ |
| 427 |
kma_execute (function, ¶ms, out, in, ncurr * BLOCKSIZE, NULL, 0); |
| 428 |
|
| 429 |
out += ncurr * BLOCKSIZE; |
| 430 |
in += ncurr * BLOCKSIZE; |
| 431 |
nblocks -= ncurr; |
| 432 |
} |
| 433 |
|
| 434 |
wipememory (¶ms, sizeof(params)); |
| 435 |
} |
| 436 |
|
| 437 |
static size_t aes_s390x_gcm_crypt(gcry_cipher_hd_t c, void *outbuf_arg, |
| 438 |
const void *inbuf_arg, size_t nblocks, |
| 439 |
int encrypt) |
| 440 |
{ |
| 441 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 442 |
byte *out = outbuf_arg; |
| 443 |
const byte *in = inbuf_arg; |
| 444 |
byte *ctr = c->u_ctr.ctr; |
| 445 |
unsigned int function; |
| 446 |
struct aes_s390x_gcm_params_s params; |
| 447 |
|
| 448 |
function = ctx->kma_func | (encrypt ? KM_ENCRYPT : KM_DECRYPT) |
| 449 |
| KMA_HS | KMA_LAAD; |
| 450 |
|
| 451 |
/* Prepare parameter block. */ |
| 452 |
memset (¶ms.reserved, 0, sizeof(params.reserved)); |
| 453 |
buf_put_be32 (¶ms.counter_value, buf_get_be32(ctr + 12) - 1); |
| 454 |
memcpy (¶ms.tag, c->u_mode.gcm.u_tag.tag, 16); |
| 455 |
memcpy (¶ms.hash_subkey, c->u_mode.gcm.u_ghash_key.key, 16); |
| 456 |
params.total_aad_length = 0; |
| 457 |
params.total_cipher_length = 0; |
| 458 |
memcpy (¶ms.initial_counter_value, ctr, 12); |
| 459 |
params.initial_counter_value[3] = params.counter_value; |
| 460 |
memcpy (¶ms.key, ctx->keyschenc, 32); |
| 461 |
|
| 462 |
/* Update counter (CTR32). */ |
| 463 |
buf_put_be32(ctr + 12, buf_get_be32(ctr + 12) + nblocks); |
| 464 |
|
| 465 |
/* Perform KMA-GCM. */ |
| 466 |
kma_execute (function, ¶ms, out, in, nblocks * BLOCKSIZE, NULL, 0); |
| 467 |
|
| 468 |
/* Update tag. */ |
| 469 |
memcpy (c->u_mode.gcm.u_tag.tag, ¶ms.tag, 16); |
| 470 |
|
| 471 |
wipememory (¶ms, sizeof(params)); |
| 472 |
|
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
static void aes_s390x_xts_crypt(void *context, unsigned char *tweak, |
| 477 |
void *outbuf_arg, const void *inbuf_arg, |
| 478 |
size_t nblocks, int encrypt) |
| 479 |
{ |
| 480 |
RIJNDAEL_context *ctx = context; |
| 481 |
byte *out = outbuf_arg; |
| 482 |
const byte *in = inbuf_arg; |
| 483 |
unsigned int function; |
| 484 |
u128_t params[3]; |
| 485 |
u128_t *params_tweak; |
| 486 |
|
| 487 |
if (ctx->rounds < 12) |
| 488 |
{ |
| 489 |
memcpy (¶ms[0], ctx->keyschenc, 16); |
| 490 |
params_tweak = ¶ms[1]; |
| 491 |
memcpy (params_tweak, tweak, BLOCKSIZE); |
| 492 |
} |
| 493 |
else if (ctx->rounds == 12) |
| 494 |
{ |
| 495 |
BUG(); /* KM-XTS-AES-192 not defined. */ |
| 496 |
} |
| 497 |
else |
| 498 |
{ |
| 499 |
memcpy (¶ms[0], ctx->keyschenc, 32); |
| 500 |
params_tweak = ¶ms[2]; |
| 501 |
memcpy (params_tweak, tweak, BLOCKSIZE); |
| 502 |
} |
| 503 |
|
| 504 |
function = ctx->km_func_xts | (encrypt ? KM_ENCRYPT : KM_DECRYPT); |
| 505 |
km_execute (function, ¶ms, out, in, nblocks * BLOCKSIZE); |
| 506 |
|
| 507 |
/* Update tweak with XTSP. */ |
| 508 |
memcpy (tweak, params_tweak, BLOCKSIZE); |
| 509 |
|
| 510 |
wipememory (¶ms, sizeof(params)); |
| 511 |
} |
| 512 |
|
| 513 |
static NO_INLINE void |
| 514 |
aes_s390x_ocb_prepare_Ls (gcry_cipher_hd_t c, u64 blkn, const void *Ls[64], |
| 515 |
const void ***pl) |
| 516 |
{ |
| 517 |
unsigned int n = 64 - (blkn % 64); |
| 518 |
int i; |
| 519 |
|
| 520 |
/* Prepare L pointers. */ |
| 521 |
*pl = &Ls[(63 + n) % 64]; |
| 522 |
for (i = 0; i < 64; i += 8, n = (n + 8) % 64) |
| 523 |
{ |
| 524 |
static const int lastL[8] = { 3, 4, 3, 5, 3, 4, 3, 0 }; |
| 525 |
|
| 526 |
Ls[(0 + n) % 64] = c->u_mode.ocb.L[0]; |
| 527 |
Ls[(1 + n) % 64] = c->u_mode.ocb.L[1]; |
| 528 |
Ls[(2 + n) % 64] = c->u_mode.ocb.L[0]; |
| 529 |
Ls[(3 + n) % 64] = c->u_mode.ocb.L[2]; |
| 530 |
Ls[(4 + n) % 64] = c->u_mode.ocb.L[0]; |
| 531 |
Ls[(5 + n) % 64] = c->u_mode.ocb.L[1]; |
| 532 |
Ls[(6 + n) % 64] = c->u_mode.ocb.L[0]; |
| 533 |
Ls[(7 + n) % 64] = c->u_mode.ocb.L[lastL[i / 8]]; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
static ALWAYS_INLINE const unsigned char * |
| 538 |
aes_s390x_ocb_get_l (gcry_cipher_hd_t c, u64 n) |
| 539 |
{ |
| 540 |
unsigned long ntz = _gcry_ctz (n); |
| 541 |
if (ntz >= OCB_L_TABLE_SIZE) |
| 542 |
{ |
| 543 |
return NULL; /* Not accessed. */ |
| 544 |
} |
| 545 |
return c->u_mode.ocb.L[ntz]; |
| 546 |
} |
| 547 |
|
| 548 |
static NO_INLINE void |
| 549 |
aes_s390x_ocb_checksum (unsigned char *checksum, const void *plainbuf_arg, |
| 550 |
size_t nblks) |
| 551 |
{ |
| 552 |
const char *plainbuf = plainbuf_arg; |
| 553 |
u64 tmp0[2]; |
| 554 |
u64 tmp1[2] = { 0, 0 }; |
| 555 |
u64 tmp2[2] = { 0, 0 }; |
| 556 |
u64 tmp3[2] = { 0, 0 }; |
| 557 |
|
| 558 |
cipher_block_cpy (tmp0, checksum, BLOCKSIZE); |
| 559 |
|
| 560 |
if (nblks >= 4) |
| 561 |
{ |
| 562 |
while (nblks >= 4) |
| 563 |
{ |
| 564 |
/* Checksum_i = Checksum_{i-1} xor P_i */ |
| 565 |
cipher_block_xor_1 (tmp0, plainbuf + 0 * BLOCKSIZE, BLOCKSIZE); |
| 566 |
cipher_block_xor_1 (tmp1, plainbuf + 1 * BLOCKSIZE, BLOCKSIZE); |
| 567 |
cipher_block_xor_1 (tmp2, plainbuf + 2 * BLOCKSIZE, BLOCKSIZE); |
| 568 |
cipher_block_xor_1 (tmp3, plainbuf + 3 * BLOCKSIZE, BLOCKSIZE); |
| 569 |
|
| 570 |
plainbuf += 4 * BLOCKSIZE; |
| 571 |
nblks -= 4; |
| 572 |
} |
| 573 |
|
| 574 |
cipher_block_xor_1 (tmp0, tmp1, BLOCKSIZE); |
| 575 |
cipher_block_xor_1 (tmp2, tmp3, BLOCKSIZE); |
| 576 |
cipher_block_xor_1 (tmp0, tmp2, BLOCKSIZE); |
| 577 |
|
| 578 |
wipememory (tmp1, sizeof(tmp1)); |
| 579 |
wipememory (tmp2, sizeof(tmp2)); |
| 580 |
wipememory (tmp3, sizeof(tmp3)); |
| 581 |
} |
| 582 |
|
| 583 |
while (nblks > 0) |
| 584 |
{ |
| 585 |
/* Checksum_i = Checksum_{i-1} xor P_i */ |
| 586 |
cipher_block_xor_1 (tmp0, plainbuf, BLOCKSIZE); |
| 587 |
|
| 588 |
plainbuf += BLOCKSIZE; |
| 589 |
nblks--; |
| 590 |
} |
| 591 |
|
| 592 |
cipher_block_cpy (checksum, tmp0, BLOCKSIZE); |
| 593 |
|
| 594 |
wipememory (tmp0, sizeof(tmp0)); |
| 595 |
} |
| 596 |
|
| 597 |
static NO_INLINE size_t |
| 598 |
aes_s390x_ocb_enc (gcry_cipher_hd_t c, void *outbuf_arg, |
| 599 |
const void *inbuf_arg, size_t nblocks_arg) |
| 600 |
{ |
| 601 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 602 |
unsigned char *outbuf = outbuf_arg; |
| 603 |
const unsigned char *inbuf = inbuf_arg; |
| 604 |
size_t nblocks = nblocks_arg; |
| 605 |
u128_t blocks[64]; |
| 606 |
u128_t offset; |
| 607 |
size_t max_blocks_used = 0; |
| 608 |
u64 blkn = c->u_mode.ocb.data_nblocks; |
| 609 |
unsigned int function = ctx->km_func | KM_ENCRYPT; |
| 610 |
const void *Ls[64]; |
| 611 |
const void **pl; |
| 612 |
|
| 613 |
aes_s390x_ocb_prepare_Ls (c, blkn, Ls, &pl); |
| 614 |
|
| 615 |
/* Checksumming could be done inline in OCB_INPUT macros, but register |
| 616 |
* pressure becomes too heavy and performance would end up being worse. |
| 617 |
* For decryption, checksumming is part of OCB_OUTPUT macros as |
| 618 |
* output handling is less demanding and can handle the additional |
| 619 |
* computation. */ |
| 620 |
aes_s390x_ocb_checksum (c->u_ctr.ctr, inbuf_arg, nblocks_arg); |
| 621 |
|
| 622 |
cipher_block_cpy (&offset, &c->u_iv.iv, BLOCKSIZE); |
| 623 |
|
| 624 |
#define OCB_INPUT(n) \ |
| 625 |
cipher_block_xor_2dst (&blocks[n], &offset, Ls[n], BLOCKSIZE); \ |
| 626 |
cipher_block_xor (outbuf + (n) * BLOCKSIZE, inbuf + (n) * BLOCKSIZE, \ |
| 627 |
&offset, BLOCKSIZE) |
| 628 |
|
| 629 |
#define OCB_INPUT_4(n) \ |
| 630 |
OCB_INPUT((n) + 0); OCB_INPUT((n) + 1); OCB_INPUT((n) + 2); \ |
| 631 |
OCB_INPUT((n) + 3) |
| 632 |
|
| 633 |
#define OCB_INPUT_16(n) \ |
| 634 |
OCB_INPUT_4((n) + 0); OCB_INPUT_4((n) + 4); OCB_INPUT_4((n) + 8); \ |
| 635 |
OCB_INPUT_4((n) + 12); |
| 636 |
|
| 637 |
#define OCB_OUTPUT(n) \ |
| 638 |
cipher_block_xor_1 (outbuf + (n) * BLOCKSIZE, &blocks[n], BLOCKSIZE) |
| 639 |
|
| 640 |
#define OCB_OUTPUT_4(n) \ |
| 641 |
OCB_OUTPUT((n) + 0); OCB_OUTPUT((n) + 1); OCB_OUTPUT((n) + 2); \ |
| 642 |
OCB_OUTPUT((n) + 3) |
| 643 |
|
| 644 |
#define OCB_OUTPUT_16(n) \ |
| 645 |
OCB_OUTPUT_4((n) + 0); OCB_OUTPUT_4((n) + 4); OCB_OUTPUT_4((n) + 8); \ |
| 646 |
OCB_OUTPUT_4((n) + 12); |
| 647 |
|
| 648 |
while (nblocks >= 64) |
| 649 |
{ |
| 650 |
blkn += 64; |
| 651 |
*pl = aes_s390x_ocb_get_l(c, blkn - blkn % 64); |
| 652 |
|
| 653 |
OCB_INPUT_16(0); |
| 654 |
OCB_INPUT_16(16); |
| 655 |
OCB_INPUT_16(32); |
| 656 |
OCB_INPUT_16(48); |
| 657 |
|
| 658 |
km_execute (function, ctx->keyschenc, outbuf, outbuf, 64 * BLOCKSIZE); |
| 659 |
|
| 660 |
asm volatile ("xc 0(256, %[out]), 0(%[blocks])\n\t" |
| 661 |
"xc 256(256, %[out]), 256(%[blocks])\n\t" |
| 662 |
"xc 512(256, %[out]), 512(%[blocks])\n\t" |
| 663 |
"xc 768(256, %[out]), 768(%[blocks])\n\t" |
| 664 |
: |
| 665 |
: [out] "a" (outbuf), [blocks] "a" (blocks) |
| 666 |
: "memory"); |
| 667 |
|
| 668 |
max_blocks_used = 64; |
| 669 |
inbuf += 64 * BLOCKSIZE; |
| 670 |
outbuf += 64 * BLOCKSIZE; |
| 671 |
nblocks -= 64; |
| 672 |
} |
| 673 |
|
| 674 |
if (nblocks) |
| 675 |
{ |
| 676 |
unsigned int pos = 0; |
| 677 |
|
| 678 |
max_blocks_used = max_blocks_used < nblocks ? nblocks : max_blocks_used; |
| 679 |
|
| 680 |
blkn += nblocks; |
| 681 |
*pl = aes_s390x_ocb_get_l(c, blkn - blkn % 64); |
| 682 |
|
| 683 |
while (nblocks >= 16) |
| 684 |
{ |
| 685 |
OCB_INPUT_16(pos + 0); |
| 686 |
pos += 16; |
| 687 |
nblocks -= 16; |
| 688 |
} |
| 689 |
while (nblocks >= 4) |
| 690 |
{ |
| 691 |
OCB_INPUT_4(pos + 0); |
| 692 |
pos += 4; |
| 693 |
nblocks -= 4; |
| 694 |
} |
| 695 |
if (nblocks >= 2) |
| 696 |
{ |
| 697 |
OCB_INPUT(pos + 0); |
| 698 |
OCB_INPUT(pos + 1); |
| 699 |
pos += 2; |
| 700 |
nblocks -= 2; |
| 701 |
} |
| 702 |
if (nblocks >= 1) |
| 703 |
{ |
| 704 |
OCB_INPUT(pos + 0); |
| 705 |
pos += 1; |
| 706 |
nblocks -= 1; |
| 707 |
} |
| 708 |
|
| 709 |
nblocks = pos; |
| 710 |
pos = 0; |
| 711 |
km_execute (function, ctx->keyschenc, outbuf, outbuf, |
| 712 |
nblocks * BLOCKSIZE); |
| 713 |
|
| 714 |
while (nblocks >= 16) |
| 715 |
{ |
| 716 |
OCB_OUTPUT_16(pos + 0); |
| 717 |
pos += 16; |
| 718 |
nblocks -= 16; |
| 719 |
} |
| 720 |
while (nblocks >= 4) |
| 721 |
{ |
| 722 |
OCB_OUTPUT_4(pos + 0); |
| 723 |
pos += 4; |
| 724 |
nblocks -= 4; |
| 725 |
} |
| 726 |
if (nblocks >= 2) |
| 727 |
{ |
| 728 |
OCB_OUTPUT(pos + 0); |
| 729 |
OCB_OUTPUT(pos + 1); |
| 730 |
pos += 2; |
| 731 |
nblocks -= 2; |
| 732 |
} |
| 733 |
if (nblocks >= 1) |
| 734 |
{ |
| 735 |
OCB_OUTPUT(pos + 0); |
| 736 |
pos += 1; |
| 737 |
nblocks -= 1; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
#undef OCB_INPUT |
| 742 |
#undef OCB_INPUT_4 |
| 743 |
#undef OCB_INPUT_16 |
| 744 |
#undef OCB_OUTPUT |
| 745 |
#undef OCB_OUTPUT_4 |
| 746 |
#undef OCB_OUTPUT_16 |
| 747 |
|
| 748 |
c->u_mode.ocb.data_nblocks = blkn; |
| 749 |
cipher_block_cpy (&c->u_iv.iv, &offset, BLOCKSIZE); |
| 750 |
|
| 751 |
if (max_blocks_used) |
| 752 |
wipememory (&blocks, max_blocks_used * BLOCKSIZE); |
| 753 |
|
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
static NO_INLINE size_t |
| 758 |
aes_s390x_ocb_dec (gcry_cipher_hd_t c, void *outbuf_arg, |
| 759 |
const void *inbuf_arg, size_t nblocks_arg) |
| 760 |
{ |
| 761 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 762 |
unsigned char *outbuf = outbuf_arg; |
| 763 |
const unsigned char *inbuf = inbuf_arg; |
| 764 |
size_t nblocks = nblocks_arg; |
| 765 |
u128_t blocks[64]; |
| 766 |
u128_t offset; |
| 767 |
size_t max_blocks_used = 0; |
| 768 |
u64 blkn = c->u_mode.ocb.data_nblocks; |
| 769 |
unsigned int function = ctx->km_func | KM_DECRYPT; |
| 770 |
const void *Ls[64]; |
| 771 |
const void **pl; |
| 772 |
|
| 773 |
aes_s390x_ocb_prepare_Ls (c, blkn, Ls, &pl); |
| 774 |
|
| 775 |
cipher_block_cpy (&offset, &c->u_iv.iv, BLOCKSIZE); |
| 776 |
|
| 777 |
#define OCB_INPUT(n) \ |
| 778 |
cipher_block_xor_2dst (&blocks[n], &offset, Ls[n], BLOCKSIZE); \ |
| 779 |
cipher_block_xor (outbuf + (n) * BLOCKSIZE, inbuf + (n) * BLOCKSIZE, \ |
| 780 |
&offset, BLOCKSIZE) |
| 781 |
|
| 782 |
#define OCB_INPUT_4(n) \ |
| 783 |
OCB_INPUT((n) + 0); OCB_INPUT((n) + 1); OCB_INPUT((n) + 2); \ |
| 784 |
OCB_INPUT((n) + 3) |
| 785 |
|
| 786 |
#define OCB_INPUT_16(n) \ |
| 787 |
OCB_INPUT_4((n) + 0); OCB_INPUT_4((n) + 4); OCB_INPUT_4((n) + 8); \ |
| 788 |
OCB_INPUT_4((n) + 12); |
| 789 |
|
| 790 |
#define OCB_OUTPUT(n) \ |
| 791 |
cipher_block_xor_1 (outbuf + (n) * BLOCKSIZE, &blocks[n], BLOCKSIZE); |
| 792 |
|
| 793 |
#define OCB_OUTPUT_4(n) \ |
| 794 |
OCB_OUTPUT((n) + 0); OCB_OUTPUT((n) + 1); OCB_OUTPUT((n) + 2); \ |
| 795 |
OCB_OUTPUT((n) + 3) |
| 796 |
|
| 797 |
#define OCB_OUTPUT_16(n) \ |
| 798 |
OCB_OUTPUT_4((n) + 0); OCB_OUTPUT_4((n) + 4); OCB_OUTPUT_4((n) + 8); \ |
| 799 |
OCB_OUTPUT_4((n) + 12); |
| 800 |
|
| 801 |
while (nblocks >= 64) |
| 802 |
{ |
| 803 |
blkn += 64; |
| 804 |
*pl = aes_s390x_ocb_get_l(c, blkn - blkn % 64); |
| 805 |
|
| 806 |
OCB_INPUT_16(0); |
| 807 |
OCB_INPUT_16(16); |
| 808 |
OCB_INPUT_16(32); |
| 809 |
OCB_INPUT_16(48); |
| 810 |
|
| 811 |
km_execute (function, ctx->keyschenc, outbuf, outbuf, 64 * BLOCKSIZE); |
| 812 |
|
| 813 |
asm volatile ("xc 0(256, %[out]), 0(%[blocks])\n\t" |
| 814 |
"xc 256(256, %[out]), 256(%[blocks])\n\t" |
| 815 |
"xc 512(256, %[out]), 512(%[blocks])\n\t" |
| 816 |
"xc 768(256, %[out]), 768(%[blocks])\n\t" |
| 817 |
: |
| 818 |
: [out] "a" (outbuf), [blocks] "a" (blocks) |
| 819 |
: "memory"); |
| 820 |
|
| 821 |
max_blocks_used = 64; |
| 822 |
inbuf += 64 * BLOCKSIZE; |
| 823 |
outbuf += 64 * BLOCKSIZE; |
| 824 |
nblocks -= 64; |
| 825 |
} |
| 826 |
|
| 827 |
if (nblocks) |
| 828 |
{ |
| 829 |
unsigned int pos = 0; |
| 830 |
|
| 831 |
max_blocks_used = max_blocks_used < nblocks ? nblocks : max_blocks_used; |
| 832 |
|
| 833 |
blkn += nblocks; |
| 834 |
*pl = aes_s390x_ocb_get_l(c, blkn - blkn % 64); |
| 835 |
|
| 836 |
while (nblocks >= 16) |
| 837 |
{ |
| 838 |
OCB_INPUT_16(pos + 0); |
| 839 |
pos += 16; |
| 840 |
nblocks -= 16; |
| 841 |
} |
| 842 |
while (nblocks >= 4) |
| 843 |
{ |
| 844 |
OCB_INPUT_4(pos + 0); |
| 845 |
pos += 4; |
| 846 |
nblocks -= 4; |
| 847 |
} |
| 848 |
if (nblocks >= 2) |
| 849 |
{ |
| 850 |
OCB_INPUT(pos + 0); |
| 851 |
OCB_INPUT(pos + 1); |
| 852 |
pos += 2; |
| 853 |
nblocks -= 2; |
| 854 |
} |
| 855 |
if (nblocks >= 1) |
| 856 |
{ |
| 857 |
OCB_INPUT(pos + 0); |
| 858 |
pos += 1; |
| 859 |
nblocks -= 1; |
| 860 |
} |
| 861 |
|
| 862 |
nblocks = pos; |
| 863 |
pos = 0; |
| 864 |
km_execute (function, ctx->keyschenc, outbuf, outbuf, |
| 865 |
nblocks * BLOCKSIZE); |
| 866 |
|
| 867 |
while (nblocks >= 16) |
| 868 |
{ |
| 869 |
OCB_OUTPUT_16(pos + 0); |
| 870 |
pos += 16; |
| 871 |
nblocks -= 16; |
| 872 |
} |
| 873 |
while (nblocks >= 4) |
| 874 |
{ |
| 875 |
OCB_OUTPUT_4(pos + 0); |
| 876 |
pos += 4; |
| 877 |
nblocks -= 4; |
| 878 |
} |
| 879 |
if (nblocks >= 2) |
| 880 |
{ |
| 881 |
OCB_OUTPUT(pos + 0); |
| 882 |
OCB_OUTPUT(pos + 1); |
| 883 |
pos += 2; |
| 884 |
nblocks -= 2; |
| 885 |
} |
| 886 |
if (nblocks >= 1) |
| 887 |
{ |
| 888 |
OCB_OUTPUT(pos + 0); |
| 889 |
pos += 1; |
| 890 |
nblocks -= 1; |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
#undef OCB_INPUT |
| 895 |
#undef OCB_INPUT_4 |
| 896 |
#undef OCB_INPUT_16 |
| 897 |
#undef OCB_OUTPUT |
| 898 |
#undef OCB_OUTPUT_4 |
| 899 |
#undef OCB_OUTPUT_16 |
| 900 |
|
| 901 |
c->u_mode.ocb.data_nblocks = blkn; |
| 902 |
cipher_block_cpy (&c->u_iv.iv, &offset, BLOCKSIZE); |
| 903 |
|
| 904 |
if (max_blocks_used) |
| 905 |
wipememory (&blocks, max_blocks_used * BLOCKSIZE); |
| 906 |
|
| 907 |
aes_s390x_ocb_checksum (c->u_ctr.ctr, outbuf_arg, nblocks_arg); |
| 908 |
|
| 909 |
return 0; |
| 910 |
} |
| 911 |
|
| 912 |
static size_t |
| 913 |
aes_s390x_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg, |
| 914 |
const void *inbuf_arg, size_t nblocks_arg, int encrypt) |
| 915 |
{ |
| 916 |
if (encrypt) |
| 917 |
return aes_s390x_ocb_enc (c, outbuf_arg, inbuf_arg, nblocks_arg); |
| 918 |
else |
| 919 |
return aes_s390x_ocb_dec (c, outbuf_arg, inbuf_arg, nblocks_arg); |
| 920 |
} |
| 921 |
|
| 922 |
static size_t |
| 923 |
aes_s390x_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg, |
| 924 |
size_t nblocks_arg) |
| 925 |
{ |
| 926 |
RIJNDAEL_context *ctx = (void *)&c->context.c; |
| 927 |
const unsigned char *abuf = abuf_arg; |
| 928 |
u128_t blocks[64]; |
| 929 |
u128_t offset; |
| 930 |
size_t max_blocks_used = 0; |
| 931 |
u64 blkn = c->u_mode.ocb.aad_nblocks; |
| 932 |
unsigned int function = ctx->km_func | KM_ENCRYPT; |
| 933 |
const void *Ls[64]; |
| 934 |
const void **pl; |
| 935 |
|
| 936 |
aes_s390x_ocb_prepare_Ls (c, blkn, Ls, &pl); |
| 937 |
|
| 938 |
cipher_block_cpy (&offset, c->u_mode.ocb.aad_offset, BLOCKSIZE); |
| 939 |
|
| 940 |
#define OCB_INPUT(n) \ |
| 941 |
cipher_block_xor_2dst (&blocks[n], &offset, Ls[n], BLOCKSIZE); \ |
| 942 |
cipher_block_xor_1 (&blocks[n], abuf + (n) * BLOCKSIZE, BLOCKSIZE) |
| 943 |
|
| 944 |
#define OCB_INPUT_4(n) \ |
| 945 |
OCB_INPUT((n) + 0); OCB_INPUT((n) + 1); OCB_INPUT((n) + 2); \ |
| 946 |
OCB_INPUT((n) + 3) |
| 947 |
|
| 948 |
#define OCB_INPUT_16(n) \ |
| 949 |
OCB_INPUT_4((n) + 0); OCB_INPUT_4((n) + 4); OCB_INPUT_4((n) + 8); \ |
| 950 |
OCB_INPUT_4((n) + 12); |
| 951 |
|
| 952 |
while (nblocks_arg >= 64) |
| 953 |
{ |
| 954 |
blkn += 64; |
| 955 |
*pl = aes_s390x_ocb_get_l(c, blkn - blkn % 64); |
| 956 |
|
| 957 |
OCB_INPUT_16(0); |
| 958 |
OCB_INPUT_16(16); |
| 959 |
OCB_INPUT_16(32); |
| 960 |
OCB_INPUT_16(48); |
| 961 |
|
| 962 |
km_execute (function, ctx->keyschenc, blocks, blocks, 64 * BLOCKSIZE); |
| 963 |
|
| 964 |
aes_s390x_ocb_checksum (c->u_mode.ocb.aad_sum, blocks, 64); |
| 965 |
|
| 966 |
max_blocks_used = 64; |
| 967 |
abuf += 64 * BLOCKSIZE; |
| 968 |
nblocks_arg -= 64; |
| 969 |
} |
| 970 |
|
| 971 |
if (nblocks_arg > 0) |
| 972 |
{ |
| 973 |
size_t nblocks = nblocks_arg; |
| 974 |
unsigned int pos = 0; |
| 975 |
|
| 976 |
max_blocks_used = max_blocks_used < nblocks ? nblocks : max_blocks_used; |
| 977 |
|
| 978 |
blkn += nblocks; |
| 979 |
*pl = aes_s390x_ocb_get_l(c, blkn - blkn % 64); |
| 980 |
|
| 981 |
while (nblocks >= 16) |
| 982 |
{ |
| 983 |
OCB_INPUT_16(pos + 0); |
| 984 |
pos += 16; |
| 985 |
nblocks -= 16; |
| 986 |
} |
| 987 |
while (nblocks >= 4) |
| 988 |
{ |
| 989 |
OCB_INPUT_4(pos + 0); |
| 990 |
pos += 4; |
| 991 |
nblocks -= 4; |
| 992 |
} |
| 993 |
if (nblocks >= 2) |
| 994 |
{ |
| 995 |
OCB_INPUT(pos + 0); |
| 996 |
OCB_INPUT(pos + 1); |
| 997 |
pos += 2; |
| 998 |
nblocks -= 2; |
| 999 |
} |
| 1000 |
if (nblocks >= 1) |
| 1001 |
{ |
| 1002 |
OCB_INPUT(pos + 0); |
| 1003 |
pos += 1; |
| 1004 |
nblocks -= 1; |
| 1005 |
} |
| 1006 |
|
| 1007 |
nblocks = pos; |
| 1008 |
nblocks_arg -= pos; |
| 1009 |
pos = 0; |
| 1010 |
km_execute (function, ctx->keyschenc, blocks, blocks, |
| 1011 |
nblocks * BLOCKSIZE); |
| 1012 |
|
| 1013 |
aes_s390x_ocb_checksum (c->u_mode.ocb.aad_sum, blocks, nblocks); |
| 1014 |
} |
| 1015 |
|
| 1016 |
#undef OCB_INPUT |
| 1017 |
#undef OCB_INPUT_4 |
| 1018 |
#undef OCB_INPUT_16 |
| 1019 |
|
| 1020 |
c->u_mode.ocb.aad_nblocks = blkn; |
| 1021 |
cipher_block_cpy (c->u_mode.ocb.aad_offset, &offset, BLOCKSIZE); |
| 1022 |
|
| 1023 |
if (max_blocks_used) |
| 1024 |
wipememory (&blocks, max_blocks_used * BLOCKSIZE); |
| 1025 |
|
| 1026 |
return 0; |
| 1027 |
} |
| 1028 |
|
| 1029 |
int _gcry_aes_s390x_setup_acceleration(RIJNDAEL_context *ctx, |
| 1030 |
unsigned int keylen, |
| 1031 |
unsigned int hwfeatures, |
| 1032 |
cipher_bulk_ops_t *bulk_ops) |
| 1033 |
{ |
| 1034 |
unsigned int func; |
| 1035 |
unsigned int func_xts; |
| 1036 |
u128_t func_mask; |
| 1037 |
u128_t func_xts_mask; |
| 1038 |
|
| 1039 |
if (!(hwfeatures & HWF_S390X_MSA)) |
| 1040 |
return 0; |
| 1041 |
|
| 1042 |
switch (keylen) |
| 1043 |
{ |
| 1044 |
default: |
| 1045 |
case 16: |
| 1046 |
func = KM_FUNCTION_AES_128; |
| 1047 |
func_xts = KM_FUNCTION_XTS_AES_128; |
| 1048 |
func_mask = km_function_to_mask(KM_FUNCTION_AES_128); |
| 1049 |
func_xts_mask = km_function_to_mask(KM_FUNCTION_XTS_AES_128); |
| 1050 |
break; |
| 1051 |
case 24: |
| 1052 |
func = KM_FUNCTION_AES_192; |
| 1053 |
func_xts = 0; |
| 1054 |
func_mask = km_function_to_mask(KM_FUNCTION_AES_192); |
| 1055 |
func_xts_mask = 0; /* XTS-AES192 not available. */ |
| 1056 |
break; |
| 1057 |
case 32: |
| 1058 |
func = KM_FUNCTION_AES_256; |
| 1059 |
func_xts = KM_FUNCTION_XTS_AES_256; |
| 1060 |
func_mask = km_function_to_mask(KM_FUNCTION_AES_256); |
| 1061 |
func_xts_mask = km_function_to_mask(KM_FUNCTION_XTS_AES_256); |
| 1062 |
break; |
| 1063 |
} |
| 1064 |
|
| 1065 |
/* Query KM for supported algorithms and check if acceleration for |
| 1066 |
* requested key-length is available. */ |
| 1067 |
if (!(km_query () & func_mask)) |
| 1068 |
return 0; |
| 1069 |
|
| 1070 |
ctx->km_func = func; |
| 1071 |
|
| 1072 |
/* Query KM for supported XTS algorithms. */ |
| 1073 |
if (km_query () & func_xts_mask) |
| 1074 |
ctx->km_func_xts = func_xts; |
| 1075 |
|
| 1076 |
/* Query KMC for supported algorithms. */ |
| 1077 |
if (kmc_query () & func_mask) |
| 1078 |
ctx->kmc_func = func; |
| 1079 |
|
| 1080 |
/* Query KMAC for supported algorithms. */ |
| 1081 |
if (kmac_query () & func_mask) |
| 1082 |
ctx->kmac_func = func; |
| 1083 |
|
| 1084 |
if (hwfeatures & HWF_S390X_MSA_4) |
| 1085 |
{ |
| 1086 |
/* Query KMF for supported algorithms. */ |
| 1087 |
if (kmf_query () & func_mask) |
| 1088 |
ctx->kmf_func = func; |
| 1089 |
|
| 1090 |
/* Query KMO for supported algorithms. */ |
| 1091 |
if (kmo_query () & func_mask) |
| 1092 |
ctx->kmo_func = func; |
| 1093 |
} |
| 1094 |
|
| 1095 |
if (hwfeatures & HWF_S390X_MSA_8) |
| 1096 |
{ |
| 1097 |
/* Query KMA for supported algorithms. */ |
| 1098 |
if (kma_query () & func_mask) |
| 1099 |
ctx->kma_func = func; |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* Setup zSeries bulk encryption/decryption routines. */ |
| 1103 |
|
| 1104 |
if (ctx->km_func) |
| 1105 |
{ |
| 1106 |
bulk_ops->ocb_crypt = aes_s390x_ocb_crypt; |
| 1107 |
bulk_ops->ocb_auth = aes_s390x_ocb_auth; |
| 1108 |
|
| 1109 |
/* CFB128 decryption uses KM instruction, instead of KMF. */ |
| 1110 |
bulk_ops->cfb_dec = aes_s390x_cfb128_dec; |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (ctx->km_func_xts) |
| 1114 |
{ |
| 1115 |
bulk_ops->xts_crypt = aes_s390x_xts_crypt; |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (ctx->kmc_func) |
| 1119 |
{ |
| 1120 |
if(ctx->kmac_func) |
| 1121 |
{ |
| 1122 |
/* Either KMC or KMAC used depending on 'cbc_mac' parameter. */ |
| 1123 |
bulk_ops->cbc_enc = aes_s390x_cbc_enc; |
| 1124 |
} |
| 1125 |
|
| 1126 |
bulk_ops->cbc_dec = aes_s390x_cbc_dec; |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (ctx->kmf_func) |
| 1130 |
{ |
| 1131 |
bulk_ops->cfb_enc = aes_s390x_cfb128_enc; |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (ctx->kmo_func) |
| 1135 |
{ |
| 1136 |
bulk_ops->ofb_enc = aes_s390x_ofb_enc; |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (ctx->kma_func) |
| 1140 |
{ |
| 1141 |
bulk_ops->ctr_enc = aes_s390x_ctr128_enc; |
| 1142 |
|
| 1143 |
if (kimd_query () & km_function_to_mask (KMID_FUNCTION_GHASH)) |
| 1144 |
{ |
| 1145 |
/* KIMD based GHASH implementation is required with AES-GCM |
| 1146 |
* acceleration. */ |
| 1147 |
bulk_ops->gcm_crypt = aes_s390x_gcm_crypt; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
return 1; |
| 1152 |
} |
| 1153 |
|
| 1154 |
void _gcry_aes_s390x_setkey(RIJNDAEL_context *ctx, const byte *key) |
| 1155 |
{ |
| 1156 |
unsigned int keylen = 16 + (ctx->rounds - 10) * 4; |
| 1157 |
memcpy (ctx->keyschenc, key, keylen); |
| 1158 |
} |
| 1159 |
|
| 1160 |
void _gcry_aes_s390x_prepare_decryption(RIJNDAEL_context *ctx) |
| 1161 |
{ |
| 1162 |
/* Do nothing. */ |
| 1163 |
(void)ctx; |
| 1164 |
} |
| 1165 |
|
| 1166 |
#endif /* USE_S390X_CRYPTO */ |