| 1 |
/* camellia-glue.c - Glue for the Camellia cipher |
| 2 |
* Copyright (C) 2007 Free Software Foundation, Inc. |
| 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 <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
/* I put all the libgcrypt-specific stuff in this file to keep the |
| 22 |
camellia.c/camellia.h files exactly as provided by NTT. If they |
| 23 |
update their code, this should make it easier to bring the changes |
| 24 |
in. - dshaw |
| 25 |
|
| 26 |
There is one small change which needs to be done: Include the |
| 27 |
following code at the top of camellia.h: */ |
| 28 |
#if 0 |
| 29 |
|
| 30 |
/* To use Camellia with libraries it is often useful to keep the name |
| 31 |
* space of the library clean. The following macro is thus useful: |
| 32 |
* |
| 33 |
* #define CAMELLIA_EXT_SYM_PREFIX foo_ |
| 34 |
* |
| 35 |
* This prefixes all external symbols with "foo_". |
| 36 |
*/ |
| 37 |
#ifdef HAVE_CONFIG_H |
| 38 |
#include <config.h> |
| 39 |
#endif |
| 40 |
#ifdef CAMELLIA_EXT_SYM_PREFIX |
| 41 |
#define CAMELLIA_PREFIX1(x,y) x ## y |
| 42 |
#define CAMELLIA_PREFIX2(x,y) CAMELLIA_PREFIX1(x,y) |
| 43 |
#define CAMELLIA_PREFIX(x) CAMELLIA_PREFIX2(CAMELLIA_EXT_SYM_PREFIX,x) |
| 44 |
#define Camellia_Ekeygen CAMELLIA_PREFIX(Camellia_Ekeygen) |
| 45 |
#define Camellia_EncryptBlock CAMELLIA_PREFIX(Camellia_EncryptBlock) |
| 46 |
#define Camellia_DecryptBlock CAMELLIA_PREFIX(Camellia_DecryptBlock) |
| 47 |
#define camellia_decrypt128 CAMELLIA_PREFIX(camellia_decrypt128) |
| 48 |
#define camellia_decrypt256 CAMELLIA_PREFIX(camellia_decrypt256) |
| 49 |
#define camellia_encrypt128 CAMELLIA_PREFIX(camellia_encrypt128) |
| 50 |
#define camellia_encrypt256 CAMELLIA_PREFIX(camellia_encrypt256) |
| 51 |
#define camellia_setup128 CAMELLIA_PREFIX(camellia_setup128) |
| 52 |
#define camellia_setup192 CAMELLIA_PREFIX(camellia_setup192) |
| 53 |
#define camellia_setup256 CAMELLIA_PREFIX(camellia_setup256) |
| 54 |
#endif /*CAMELLIA_EXT_SYM_PREFIX*/ |
| 55 |
|
| 56 |
#endif /* Code sample. */ |
| 57 |
|
| 58 |
|
| 59 |
#include <config.h> |
| 60 |
#include "types.h" |
| 61 |
#include "g10lib.h" |
| 62 |
#include "cipher.h" |
| 63 |
#include "camellia.h" |
| 64 |
#include "bufhelp.h" |
| 65 |
#include "cipher-internal.h" |
| 66 |
#include "bulkhelp.h" |
| 67 |
|
| 68 |
/* Helper macro to force alignment to 16 bytes. */ |
| 69 |
#ifdef HAVE_GCC_ATTRIBUTE_ALIGNED |
| 70 |
# define ATTR_ALIGNED_16 __attribute__ ((aligned (16))) |
| 71 |
#else |
| 72 |
# define ATTR_ALIGNED_16 |
| 73 |
#endif |
| 74 |
|
| 75 |
/* USE_AESNI inidicates whether to compile with Intel AES-NI/AVX code. */ |
| 76 |
#undef USE_AESNI_AVX |
| 77 |
#if defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX_SUPPORT) |
| 78 |
# if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ |
| 79 |
defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) |
| 80 |
# define USE_AESNI_AVX 1 |
| 81 |
# endif |
| 82 |
#endif |
| 83 |
|
| 84 |
/* USE_AESNI_AVX2 inidicates whether to compile with Intel AES-NI/AVX2 code. */ |
| 85 |
#undef USE_AESNI_AVX2 |
| 86 |
#if defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX2_SUPPORT) |
| 87 |
# if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ |
| 88 |
defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) |
| 89 |
# define USE_AESNI_AVX2 1 |
| 90 |
# endif |
| 91 |
#endif |
| 92 |
|
| 93 |
/* USE_VAES_AVX2 inidicates whether to compile with Intel VAES/AVX2 code. */ |
| 94 |
#undef USE_VAES_AVX2 |
| 95 |
#if defined(USE_AESNI_AVX2) && defined(HAVE_GCC_INLINE_ASM_VAES_VPCLMUL) |
| 96 |
# define USE_VAES_AVX2 1 |
| 97 |
#endif |
| 98 |
|
| 99 |
/* USE_GFNI_AVX2 inidicates whether to compile with Intel GFNI/AVX2 code. */ |
| 100 |
#undef USE_GFNI_AVX2 |
| 101 |
#if defined(USE_AESNI_AVX2) && defined(ENABLE_GFNI_SUPPORT) |
| 102 |
# define USE_GFNI_AVX2 1 |
| 103 |
#endif |
| 104 |
|
| 105 |
/* USE_GFNI_AVX512 inidicates whether to compile with Intel GFNI/AVX512 code. */ |
| 106 |
#undef USE_GFNI_AVX512 |
| 107 |
#if defined(USE_GFNI_AVX2) && defined(ENABLE_AVX512_SUPPORT) |
| 108 |
# define USE_GFNI_AVX512 1 |
| 109 |
#endif |
| 110 |
|
| 111 |
/* USE_PPC_CRYPTO indicates whether to enable PowerPC vector crypto |
| 112 |
* accelerated code. */ |
| 113 |
#undef USE_PPC_CRYPTO |
| 114 |
#if !defined(WORDS_BIGENDIAN) && defined(ENABLE_PPC_CRYPTO_SUPPORT) && \ |
| 115 |
defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \ |
| 116 |
defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \ |
| 117 |
(SIZEOF_UNSIGNED_LONG == 8) && (__GNUC__ >= 4) |
| 118 |
# define USE_PPC_CRYPTO 1 |
| 119 |
#endif |
| 120 |
|
| 121 |
/* USE_AARCH64_CE indicates whether to enable ARMv8/CE accelerated code. */ |
| 122 |
#undef USE_AARCH64_CE |
| 123 |
#if defined(__AARCH64EL__) && \ |
| 124 |
defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) && \ |
| 125 |
defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO) && \ |
| 126 |
defined(HAVE_COMPATIBLE_CC_AARCH64_NEON_INTRINSICS) && \ |
| 127 |
(__GNUC__ >= 4) |
| 128 |
# define USE_AARCH64_CE 1 |
| 129 |
#endif |
| 130 |
|
| 131 |
typedef struct |
| 132 |
{ |
| 133 |
KEY_TABLE_TYPE keytable; |
| 134 |
int keybitlength; |
| 135 |
#ifdef USE_AESNI_AVX |
| 136 |
unsigned int use_aesni_avx:1; /* AES-NI/AVX implementation shall be used. */ |
| 137 |
#endif /*USE_AESNI_AVX*/ |
| 138 |
#ifdef USE_AESNI_AVX2 |
| 139 |
unsigned int use_avx2:1; /* If any of AVX2 implementation is enabled. */ |
| 140 |
unsigned int use_aesni_avx2:1;/* AES-NI/AVX2 implementation shall be used. */ |
| 141 |
unsigned int use_vaes_avx2:1; /* VAES/AVX2 implementation shall be used. */ |
| 142 |
unsigned int use_gfni_avx2:1; /* GFNI/AVX2 implementation shall be used. */ |
| 143 |
unsigned int use_gfni_avx512:1; /* GFNI/AVX512 implementation shall be used. */ |
| 144 |
#endif /*USE_AESNI_AVX2*/ |
| 145 |
#ifdef USE_PPC_CRYPTO |
| 146 |
unsigned int use_ppc:1; |
| 147 |
unsigned int use_ppc8:1; |
| 148 |
unsigned int use_ppc9:1; |
| 149 |
#endif /*USE_PPC_CRYPTO*/ |
| 150 |
#ifdef USE_AARCH64_CE |
| 151 |
unsigned int use_aarch64ce:1; |
| 152 |
#endif /*USE_AARCH64_CE*/ |
| 153 |
} CAMELLIA_context; |
| 154 |
|
| 155 |
/* Assembly implementations use SystemV ABI, ABI conversion and additional |
| 156 |
* stack to store XMM6-XMM15 needed on Win64. */ |
| 157 |
#undef ASM_FUNC_ABI |
| 158 |
#undef ASM_EXTRA_STACK |
| 159 |
#if defined(USE_AESNI_AVX) || defined(USE_AESNI_AVX2) |
| 160 |
# ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS |
| 161 |
# define ASM_FUNC_ABI __attribute__((sysv_abi)) |
| 162 |
# define ASM_EXTRA_STACK (10 * 16) |
| 163 |
# else |
| 164 |
# define ASM_FUNC_ABI |
| 165 |
# define ASM_EXTRA_STACK 0 |
| 166 |
# endif |
| 167 |
#endif |
| 168 |
|
| 169 |
#ifdef USE_AESNI_AVX |
| 170 |
/* Assembler implementations of Camellia using AES-NI and AVX. Process data |
| 171 |
in 16 blocks same time. |
| 172 |
*/ |
| 173 |
extern void _gcry_camellia_aesni_avx_ctr_enc(CAMELLIA_context *ctx, |
| 174 |
unsigned char *out, |
| 175 |
const unsigned char *in, |
| 176 |
unsigned char *ctr) ASM_FUNC_ABI; |
| 177 |
|
| 178 |
extern void _gcry_camellia_aesni_avx_cbc_dec(CAMELLIA_context *ctx, |
| 179 |
unsigned char *out, |
| 180 |
const unsigned char *in, |
| 181 |
unsigned char *iv) ASM_FUNC_ABI; |
| 182 |
|
| 183 |
extern void _gcry_camellia_aesni_avx_cfb_dec(CAMELLIA_context *ctx, |
| 184 |
unsigned char *out, |
| 185 |
const unsigned char *in, |
| 186 |
unsigned char *iv) ASM_FUNC_ABI; |
| 187 |
|
| 188 |
extern void _gcry_camellia_aesni_avx_ocb_enc(CAMELLIA_context *ctx, |
| 189 |
unsigned char *out, |
| 190 |
const unsigned char *in, |
| 191 |
unsigned char *offset, |
| 192 |
unsigned char *checksum, |
| 193 |
const u64 Ls[16]) ASM_FUNC_ABI; |
| 194 |
|
| 195 |
extern void _gcry_camellia_aesni_avx_ocb_dec(CAMELLIA_context *ctx, |
| 196 |
unsigned char *out, |
| 197 |
const unsigned char *in, |
| 198 |
unsigned char *offset, |
| 199 |
unsigned char *checksum, |
| 200 |
const u64 Ls[16]) ASM_FUNC_ABI; |
| 201 |
|
| 202 |
extern void _gcry_camellia_aesni_avx_ocb_auth(CAMELLIA_context *ctx, |
| 203 |
const unsigned char *abuf, |
| 204 |
unsigned char *offset, |
| 205 |
unsigned char *checksum, |
| 206 |
const u64 Ls[16]) ASM_FUNC_ABI; |
| 207 |
|
| 208 |
extern void _gcry_camellia_aesni_avx_keygen(CAMELLIA_context *ctx, |
| 209 |
const unsigned char *key, |
| 210 |
unsigned int keylen) ASM_FUNC_ABI; |
| 211 |
|
| 212 |
extern void _gcry_camellia_aesni_avx_ecb_enc(const CAMELLIA_context *ctx, |
| 213 |
unsigned char *out, |
| 214 |
const unsigned char *in) |
| 215 |
ASM_FUNC_ABI; |
| 216 |
|
| 217 |
extern void _gcry_camellia_aesni_avx_ecb_dec(const CAMELLIA_context *ctx, |
| 218 |
unsigned char *out, |
| 219 |
const unsigned char *in) |
| 220 |
ASM_FUNC_ABI; |
| 221 |
|
| 222 |
static const int avx_burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 16 + |
| 223 |
2 * sizeof(void *) + ASM_EXTRA_STACK; |
| 224 |
|
| 225 |
#endif |
| 226 |
|
| 227 |
#ifdef USE_AESNI_AVX2 |
| 228 |
/* Assembler implementations of Camellia using AES-NI and AVX2. Process data |
| 229 |
in 32 blocks same time. |
| 230 |
*/ |
| 231 |
extern void _gcry_camellia_aesni_avx2_ctr_enc(CAMELLIA_context *ctx, |
| 232 |
unsigned char *out, |
| 233 |
const unsigned char *in, |
| 234 |
unsigned char *ctr) ASM_FUNC_ABI; |
| 235 |
|
| 236 |
extern void _gcry_camellia_aesni_avx2_cbc_dec(CAMELLIA_context *ctx, |
| 237 |
unsigned char *out, |
| 238 |
const unsigned char *in, |
| 239 |
unsigned char *iv) ASM_FUNC_ABI; |
| 240 |
|
| 241 |
extern void _gcry_camellia_aesni_avx2_cfb_dec(CAMELLIA_context *ctx, |
| 242 |
unsigned char *out, |
| 243 |
const unsigned char *in, |
| 244 |
unsigned char *iv) ASM_FUNC_ABI; |
| 245 |
|
| 246 |
extern void _gcry_camellia_aesni_avx2_ocb_enc(CAMELLIA_context *ctx, |
| 247 |
unsigned char *out, |
| 248 |
const unsigned char *in, |
| 249 |
unsigned char *offset, |
| 250 |
unsigned char *checksum, |
| 251 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 252 |
|
| 253 |
extern void _gcry_camellia_aesni_avx2_ocb_dec(CAMELLIA_context *ctx, |
| 254 |
unsigned char *out, |
| 255 |
const unsigned char *in, |
| 256 |
unsigned char *offset, |
| 257 |
unsigned char *checksum, |
| 258 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 259 |
|
| 260 |
extern void _gcry_camellia_aesni_avx2_ocb_auth(CAMELLIA_context *ctx, |
| 261 |
const unsigned char *abuf, |
| 262 |
unsigned char *offset, |
| 263 |
unsigned char *checksum, |
| 264 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 265 |
|
| 266 |
extern void _gcry_camellia_aesni_avx2_enc_blk1_32(const CAMELLIA_context *ctx, |
| 267 |
unsigned char *out, |
| 268 |
const unsigned char *in, |
| 269 |
unsigned int nblocks) |
| 270 |
ASM_FUNC_ABI; |
| 271 |
|
| 272 |
extern void _gcry_camellia_aesni_avx2_dec_blk1_32(const CAMELLIA_context *ctx, |
| 273 |
unsigned char *out, |
| 274 |
const unsigned char *in, |
| 275 |
unsigned int nblocks) |
| 276 |
ASM_FUNC_ABI; |
| 277 |
|
| 278 |
static const int avx2_burn_stack_depth = 32 * CAMELLIA_BLOCK_SIZE + 16 + |
| 279 |
2 * sizeof(void *) + ASM_EXTRA_STACK; |
| 280 |
|
| 281 |
#endif |
| 282 |
|
| 283 |
#ifdef USE_VAES_AVX2 |
| 284 |
/* Assembler implementations of Camellia using VAES and AVX2. Process data |
| 285 |
in 32 blocks same time. |
| 286 |
*/ |
| 287 |
extern void _gcry_camellia_vaes_avx2_ctr_enc(CAMELLIA_context *ctx, |
| 288 |
unsigned char *out, |
| 289 |
const unsigned char *in, |
| 290 |
unsigned char *ctr) ASM_FUNC_ABI; |
| 291 |
|
| 292 |
extern void _gcry_camellia_vaes_avx2_cbc_dec(CAMELLIA_context *ctx, |
| 293 |
unsigned char *out, |
| 294 |
const unsigned char *in, |
| 295 |
unsigned char *iv) ASM_FUNC_ABI; |
| 296 |
|
| 297 |
extern void _gcry_camellia_vaes_avx2_cfb_dec(CAMELLIA_context *ctx, |
| 298 |
unsigned char *out, |
| 299 |
const unsigned char *in, |
| 300 |
unsigned char *iv) ASM_FUNC_ABI; |
| 301 |
|
| 302 |
extern void _gcry_camellia_vaes_avx2_ocb_enc(CAMELLIA_context *ctx, |
| 303 |
unsigned char *out, |
| 304 |
const unsigned char *in, |
| 305 |
unsigned char *offset, |
| 306 |
unsigned char *checksum, |
| 307 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 308 |
|
| 309 |
extern void _gcry_camellia_vaes_avx2_ocb_dec(CAMELLIA_context *ctx, |
| 310 |
unsigned char *out, |
| 311 |
const unsigned char *in, |
| 312 |
unsigned char *offset, |
| 313 |
unsigned char *checksum, |
| 314 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 315 |
|
| 316 |
extern void _gcry_camellia_vaes_avx2_ocb_auth(CAMELLIA_context *ctx, |
| 317 |
const unsigned char *abuf, |
| 318 |
unsigned char *offset, |
| 319 |
unsigned char *checksum, |
| 320 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 321 |
|
| 322 |
extern void _gcry_camellia_vaes_avx2_enc_blk1_32(const CAMELLIA_context *ctx, |
| 323 |
unsigned char *out, |
| 324 |
const unsigned char *in, |
| 325 |
unsigned int nblocks) |
| 326 |
ASM_FUNC_ABI; |
| 327 |
|
| 328 |
extern void _gcry_camellia_vaes_avx2_dec_blk1_32(const CAMELLIA_context *ctx, |
| 329 |
unsigned char *out, |
| 330 |
const unsigned char *in, |
| 331 |
unsigned int nblocks) |
| 332 |
ASM_FUNC_ABI; |
| 333 |
#endif |
| 334 |
|
| 335 |
#ifdef USE_GFNI_AVX2 |
| 336 |
/* Assembler implementations of Camellia using GFNI and AVX2. Process data |
| 337 |
in 32 blocks same time. |
| 338 |
*/ |
| 339 |
extern void _gcry_camellia_gfni_avx2_ctr_enc(CAMELLIA_context *ctx, |
| 340 |
unsigned char *out, |
| 341 |
const unsigned char *in, |
| 342 |
unsigned char *ctr) ASM_FUNC_ABI; |
| 343 |
|
| 344 |
extern void _gcry_camellia_gfni_avx2_cbc_dec(CAMELLIA_context *ctx, |
| 345 |
unsigned char *out, |
| 346 |
const unsigned char *in, |
| 347 |
unsigned char *iv) ASM_FUNC_ABI; |
| 348 |
|
| 349 |
extern void _gcry_camellia_gfni_avx2_cfb_dec(CAMELLIA_context *ctx, |
| 350 |
unsigned char *out, |
| 351 |
const unsigned char *in, |
| 352 |
unsigned char *iv) ASM_FUNC_ABI; |
| 353 |
|
| 354 |
extern void _gcry_camellia_gfni_avx2_ocb_enc(CAMELLIA_context *ctx, |
| 355 |
unsigned char *out, |
| 356 |
const unsigned char *in, |
| 357 |
unsigned char *offset, |
| 358 |
unsigned char *checksum, |
| 359 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 360 |
|
| 361 |
extern void _gcry_camellia_gfni_avx2_ocb_dec(CAMELLIA_context *ctx, |
| 362 |
unsigned char *out, |
| 363 |
const unsigned char *in, |
| 364 |
unsigned char *offset, |
| 365 |
unsigned char *checksum, |
| 366 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 367 |
|
| 368 |
extern void _gcry_camellia_gfni_avx2_ocb_auth(CAMELLIA_context *ctx, |
| 369 |
const unsigned char *abuf, |
| 370 |
unsigned char *offset, |
| 371 |
unsigned char *checksum, |
| 372 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 373 |
|
| 374 |
extern void _gcry_camellia_gfni_avx2_enc_blk1_32(const CAMELLIA_context *ctx, |
| 375 |
unsigned char *out, |
| 376 |
const unsigned char *in, |
| 377 |
unsigned int nblocks) |
| 378 |
ASM_FUNC_ABI; |
| 379 |
|
| 380 |
extern void _gcry_camellia_gfni_avx2_dec_blk1_32(const CAMELLIA_context *ctx, |
| 381 |
unsigned char *out, |
| 382 |
const unsigned char *in, |
| 383 |
unsigned int nblocks) |
| 384 |
ASM_FUNC_ABI; |
| 385 |
#endif |
| 386 |
|
| 387 |
#ifdef USE_GFNI_AVX512 |
| 388 |
/* Assembler implementations of Camellia using GFNI and AVX512. Process data |
| 389 |
in 64 blocks same time. |
| 390 |
*/ |
| 391 |
extern void _gcry_camellia_gfni_avx512_ctr_enc(CAMELLIA_context *ctx, |
| 392 |
unsigned char *out, |
| 393 |
const unsigned char *in, |
| 394 |
unsigned char *ctr) ASM_FUNC_ABI; |
| 395 |
|
| 396 |
extern void _gcry_camellia_gfni_avx512_cbc_dec(CAMELLIA_context *ctx, |
| 397 |
unsigned char *out, |
| 398 |
const unsigned char *in, |
| 399 |
unsigned char *iv) ASM_FUNC_ABI; |
| 400 |
|
| 401 |
extern void _gcry_camellia_gfni_avx512_cfb_dec(CAMELLIA_context *ctx, |
| 402 |
unsigned char *out, |
| 403 |
const unsigned char *in, |
| 404 |
unsigned char *iv) ASM_FUNC_ABI; |
| 405 |
|
| 406 |
extern void _gcry_camellia_gfni_avx512_ocb_enc(CAMELLIA_context *ctx, |
| 407 |
unsigned char *out, |
| 408 |
const unsigned char *in, |
| 409 |
unsigned char *offset, |
| 410 |
unsigned char *checksum, |
| 411 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 412 |
|
| 413 |
extern void _gcry_camellia_gfni_avx512_ocb_dec(CAMELLIA_context *ctx, |
| 414 |
unsigned char *out, |
| 415 |
const unsigned char *in, |
| 416 |
unsigned char *offset, |
| 417 |
unsigned char *checksum, |
| 418 |
const u64 Ls[32]) ASM_FUNC_ABI; |
| 419 |
|
| 420 |
extern void _gcry_camellia_gfni_avx512_enc_blk64(const CAMELLIA_context *ctx, |
| 421 |
unsigned char *out, |
| 422 |
const unsigned char *in) |
| 423 |
ASM_FUNC_ABI; |
| 424 |
|
| 425 |
extern void _gcry_camellia_gfni_avx512_dec_blk64(const CAMELLIA_context *ctx, |
| 426 |
unsigned char *out, |
| 427 |
const unsigned char *in) |
| 428 |
ASM_FUNC_ABI; |
| 429 |
|
| 430 |
extern void _gcry_camellia_gfni_avx512_enc_blk1(const CAMELLIA_context *ctx, |
| 431 |
unsigned char *out, |
| 432 |
const unsigned char *in) |
| 433 |
ASM_FUNC_ABI; |
| 434 |
|
| 435 |
extern void _gcry_camellia_gfni_avx512_dec_blk1(const CAMELLIA_context *ctx, |
| 436 |
unsigned char *out, |
| 437 |
const unsigned char *in) |
| 438 |
ASM_FUNC_ABI; |
| 439 |
|
| 440 |
/* Stack not used by AVX512 implementation. */ |
| 441 |
static const int avx512_burn_stack_depth = 0; |
| 442 |
#endif |
| 443 |
|
| 444 |
#ifdef USE_PPC_CRYPTO |
| 445 |
extern void _gcry_camellia_ppc8_encrypt_blk16(const void *key_table, |
| 446 |
void *out, |
| 447 |
const void *in, |
| 448 |
int key_length); |
| 449 |
|
| 450 |
extern void _gcry_camellia_ppc8_decrypt_blk16(const void *key_table, |
| 451 |
void *out, |
| 452 |
const void *in, |
| 453 |
int key_length); |
| 454 |
|
| 455 |
extern void _gcry_camellia_ppc9_encrypt_blk16(const void *key_table, |
| 456 |
void *out, |
| 457 |
const void *in, |
| 458 |
int key_length); |
| 459 |
|
| 460 |
extern void _gcry_camellia_ppc9_decrypt_blk16(const void *key_table, |
| 461 |
void *out, |
| 462 |
const void *in, |
| 463 |
int key_length); |
| 464 |
|
| 465 |
extern void _gcry_camellia_ppc8_keygen(void *key_table, const void *vkey, |
| 466 |
unsigned int keylen); |
| 467 |
|
| 468 |
extern void _gcry_camellia_ppc9_keygen(void *key_table, const void *vkey, |
| 469 |
unsigned int keylen); |
| 470 |
|
| 471 |
void camellia_ppc_enc_blk16(const CAMELLIA_context *ctx, unsigned char *out, |
| 472 |
const unsigned char *in) |
| 473 |
{ |
| 474 |
if (ctx->use_ppc9) |
| 475 |
_gcry_camellia_ppc9_encrypt_blk16 (ctx->keytable, out, in, |
| 476 |
ctx->keybitlength / 8); |
| 477 |
else |
| 478 |
_gcry_camellia_ppc8_encrypt_blk16 (ctx->keytable, out, in, |
| 479 |
ctx->keybitlength / 8); |
| 480 |
} |
| 481 |
|
| 482 |
void camellia_ppc_dec_blk16(const CAMELLIA_context *ctx, unsigned char *out, |
| 483 |
const unsigned char *in) |
| 484 |
{ |
| 485 |
if (ctx->use_ppc9) |
| 486 |
_gcry_camellia_ppc9_decrypt_blk16 (ctx->keytable, out, in, |
| 487 |
ctx->keybitlength / 8); |
| 488 |
else |
| 489 |
_gcry_camellia_ppc8_decrypt_blk16 (ctx->keytable, out, in, |
| 490 |
ctx->keybitlength / 8); |
| 491 |
} |
| 492 |
|
| 493 |
static const int ppc_burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 16 + |
| 494 |
2 * sizeof(void *); |
| 495 |
#endif /*USE_PPC_CRYPTO*/ |
| 496 |
|
| 497 |
#ifdef USE_AARCH64_CE |
| 498 |
extern void _gcry_camellia_aarch64ce_encrypt_blk16(const void *key_table, |
| 499 |
void *out, const void *in, |
| 500 |
int key_length); |
| 501 |
|
| 502 |
extern void _gcry_camellia_aarch64ce_decrypt_blk16(const void *key_table, |
| 503 |
void *out, const void *in, |
| 504 |
int key_length); |
| 505 |
|
| 506 |
extern void _gcry_camellia_aarch64ce_keygen(void *key_table, const void *vkey, |
| 507 |
unsigned int keylen); |
| 508 |
|
| 509 |
void camellia_aarch64ce_enc_blk16(const CAMELLIA_context *ctx, |
| 510 |
unsigned char *out, const unsigned char *in) |
| 511 |
{ |
| 512 |
_gcry_camellia_aarch64ce_encrypt_blk16 (ctx->keytable, out, in, |
| 513 |
ctx->keybitlength / 8); |
| 514 |
} |
| 515 |
|
| 516 |
void camellia_aarch64ce_dec_blk16(const CAMELLIA_context *ctx, |
| 517 |
unsigned char *out, const unsigned char *in) |
| 518 |
{ |
| 519 |
_gcry_camellia_aarch64ce_decrypt_blk16 (ctx->keytable, out, in, |
| 520 |
ctx->keybitlength / 8); |
| 521 |
} |
| 522 |
|
| 523 |
static const int aarch64ce_burn_stack_depth = 16 * CAMELLIA_BLOCK_SIZE + 16 + |
| 524 |
2 * sizeof(void *); |
| 525 |
#endif /*USE_AARCH64_CE*/ |
| 526 |
|
| 527 |
static const char *selftest(void); |
| 528 |
|
| 529 |
static void _gcry_camellia_ctr_enc (void *context, unsigned char *ctr, |
| 530 |
void *outbuf_arg, const void *inbuf_arg, |
| 531 |
size_t nblocks); |
| 532 |
static void _gcry_camellia_cbc_dec (void *context, unsigned char *iv, |
| 533 |
void *outbuf_arg, const void *inbuf_arg, |
| 534 |
size_t nblocks); |
| 535 |
static void _gcry_camellia_cfb_dec (void *context, unsigned char *iv, |
| 536 |
void *outbuf_arg, const void *inbuf_arg, |
| 537 |
size_t nblocks); |
| 538 |
static void _gcry_camellia_xts_crypt (void *context, unsigned char *tweak, |
| 539 |
void *outbuf_arg, const void *inbuf_arg, |
| 540 |
size_t nblocks, int encrypt); |
| 541 |
static void _gcry_camellia_ecb_crypt (void *context, void *outbuf_arg, |
| 542 |
const void *inbuf_arg, size_t nblocks, |
| 543 |
int encrypt); |
| 544 |
static void _gcry_camellia_ctr32le_enc (void *context, unsigned char *ctr, |
| 545 |
void *outbuf_arg, const void *inbuf_arg, |
| 546 |
size_t nblocks); |
| 547 |
static size_t _gcry_camellia_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg, |
| 548 |
const void *inbuf_arg, size_t nblocks, |
| 549 |
int encrypt); |
| 550 |
static size_t _gcry_camellia_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg, |
| 551 |
size_t nblocks); |
| 552 |
|
| 553 |
static gcry_err_code_t |
| 554 |
camellia_setkey(void *c, const byte *key, unsigned keylen, |
| 555 |
cipher_bulk_ops_t *bulk_ops) |
| 556 |
{ |
| 557 |
CAMELLIA_context *ctx=c; |
| 558 |
static int initialized=0; |
| 559 |
static const char *selftest_failed=NULL; |
| 560 |
unsigned int hwf = _gcry_get_hw_features (); |
| 561 |
|
| 562 |
(void)hwf; |
| 563 |
|
| 564 |
if(keylen!=16 && keylen!=24 && keylen!=32) |
| 565 |
return GPG_ERR_INV_KEYLEN; |
| 566 |
|
| 567 |
if(!initialized) |
| 568 |
{ |
| 569 |
initialized=1; |
| 570 |
selftest_failed=selftest(); |
| 571 |
if(selftest_failed) |
| 572 |
log_error("%s\n",selftest_failed); |
| 573 |
} |
| 574 |
|
| 575 |
if(selftest_failed) |
| 576 |
return GPG_ERR_SELFTEST_FAILED; |
| 577 |
|
| 578 |
#ifdef USE_AESNI_AVX |
| 579 |
ctx->use_aesni_avx = (hwf & HWF_INTEL_AESNI) && (hwf & HWF_INTEL_AVX); |
| 580 |
#endif |
| 581 |
#ifdef USE_AESNI_AVX2 |
| 582 |
ctx->use_aesni_avx2 = (hwf & HWF_INTEL_AESNI) && (hwf & HWF_INTEL_AVX2); |
| 583 |
ctx->use_vaes_avx2 = 0; |
| 584 |
ctx->use_gfni_avx2 = 0; |
| 585 |
ctx->use_gfni_avx512 = 0; |
| 586 |
ctx->use_avx2 = ctx->use_aesni_avx2; |
| 587 |
#endif |
| 588 |
#ifdef USE_VAES_AVX2 |
| 589 |
ctx->use_vaes_avx2 = (hwf & HWF_INTEL_VAES_VPCLMUL) && (hwf & HWF_INTEL_AVX2); |
| 590 |
ctx->use_avx2 |= ctx->use_vaes_avx2; |
| 591 |
#endif |
| 592 |
#ifdef USE_GFNI_AVX2 |
| 593 |
ctx->use_gfni_avx2 = (hwf & HWF_INTEL_GFNI) && (hwf & HWF_INTEL_AVX2); |
| 594 |
ctx->use_avx2 |= ctx->use_gfni_avx2; |
| 595 |
#endif |
| 596 |
#ifdef USE_GFNI_AVX512 |
| 597 |
ctx->use_gfni_avx512 = (hwf & HWF_INTEL_GFNI) && (hwf & HWF_INTEL_AVX512); |
| 598 |
#endif |
| 599 |
#ifdef USE_PPC_CRYPTO |
| 600 |
ctx->use_ppc8 = (hwf & HWF_PPC_VCRYPTO) != 0; |
| 601 |
ctx->use_ppc9 = (hwf & HWF_PPC_VCRYPTO) && (hwf & HWF_PPC_ARCH_3_00); |
| 602 |
ctx->use_ppc = ctx->use_ppc8 || ctx->use_ppc9; |
| 603 |
#endif |
| 604 |
#ifdef USE_AARCH64_CE |
| 605 |
ctx->use_aarch64ce = (hwf & HWF_ARM_AES) != 0; |
| 606 |
#endif |
| 607 |
|
| 608 |
ctx->keybitlength=keylen*8; |
| 609 |
|
| 610 |
/* Setup bulk encryption routines. */ |
| 611 |
memset (bulk_ops, 0, sizeof(*bulk_ops)); |
| 612 |
bulk_ops->cbc_dec = _gcry_camellia_cbc_dec; |
| 613 |
bulk_ops->cfb_dec = _gcry_camellia_cfb_dec; |
| 614 |
bulk_ops->ctr_enc = _gcry_camellia_ctr_enc; |
| 615 |
bulk_ops->ocb_crypt = _gcry_camellia_ocb_crypt; |
| 616 |
bulk_ops->ocb_auth = _gcry_camellia_ocb_auth; |
| 617 |
bulk_ops->xts_crypt = _gcry_camellia_xts_crypt; |
| 618 |
bulk_ops->ecb_crypt = _gcry_camellia_ecb_crypt; |
| 619 |
bulk_ops->ctr32le_enc = _gcry_camellia_ctr32le_enc; |
| 620 |
|
| 621 |
if (0) |
| 622 |
{ } |
| 623 |
#ifdef USE_AESNI_AVX |
| 624 |
else if (ctx->use_aesni_avx) |
| 625 |
_gcry_camellia_aesni_avx_keygen(ctx, key, keylen); |
| 626 |
#endif |
| 627 |
#ifdef USE_PPC_CRYPTO |
| 628 |
else if (ctx->use_ppc9) |
| 629 |
_gcry_camellia_ppc9_keygen(ctx->keytable, key, keylen); |
| 630 |
else if (ctx->use_ppc8) |
| 631 |
_gcry_camellia_ppc8_keygen(ctx->keytable, key, keylen); |
| 632 |
#endif |
| 633 |
#ifdef USE_AARCH64_CE |
| 634 |
else if (ctx->use_aarch64ce) |
| 635 |
_gcry_camellia_aarch64ce_keygen(ctx->keytable, key, keylen); |
| 636 |
#endif |
| 637 |
else |
| 638 |
{ |
| 639 |
Camellia_Ekeygen(ctx->keybitlength,key,ctx->keytable); |
| 640 |
_gcry_burn_stack |
| 641 |
((19+34+34)*sizeof(u32)+2*sizeof(void*) /* camellia_setup256 */ |
| 642 |
+(4+32)*sizeof(u32)+2*sizeof(void*) /* camellia_setup192 */ |
| 643 |
+0+sizeof(int)+2*sizeof(void*) /* Camellia_Ekeygen */ |
| 644 |
+3*2*sizeof(void*) /* Function calls. */ |
| 645 |
); |
| 646 |
} |
| 647 |
|
| 648 |
#ifdef USE_GFNI_AVX2 |
| 649 |
if (ctx->use_gfni_avx2) |
| 650 |
{ |
| 651 |
/* Disable AESNI & VAES implementations when GFNI implementation is |
| 652 |
* enabled. */ |
| 653 |
#ifdef USE_AESNI_AVX |
| 654 |
ctx->use_aesni_avx = 0; |
| 655 |
#endif |
| 656 |
#ifdef USE_AESNI_AVX2 |
| 657 |
ctx->use_aesni_avx2 = 0; |
| 658 |
#endif |
| 659 |
#ifdef USE_VAES_AVX2 |
| 660 |
ctx->use_vaes_avx2 = 0; |
| 661 |
#endif |
| 662 |
} |
| 663 |
#endif |
| 664 |
|
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
#ifdef USE_ARM_ASM |
| 669 |
|
| 670 |
/* Assembly implementations of Camellia. */ |
| 671 |
extern void _gcry_camellia_arm_encrypt_block(const KEY_TABLE_TYPE keyTable, |
| 672 |
byte *outbuf, const byte *inbuf, |
| 673 |
const int keybits); |
| 674 |
|
| 675 |
extern void _gcry_camellia_arm_decrypt_block(const KEY_TABLE_TYPE keyTable, |
| 676 |
byte *outbuf, const byte *inbuf, |
| 677 |
const int keybits); |
| 678 |
|
| 679 |
static void Camellia_EncryptBlock(const int keyBitLength, |
| 680 |
const unsigned char *plaintext, |
| 681 |
const KEY_TABLE_TYPE keyTable, |
| 682 |
unsigned char *cipherText) |
| 683 |
{ |
| 684 |
_gcry_camellia_arm_encrypt_block(keyTable, cipherText, plaintext, |
| 685 |
keyBitLength); |
| 686 |
} |
| 687 |
|
| 688 |
static void Camellia_DecryptBlock(const int keyBitLength, |
| 689 |
const unsigned char *cipherText, |
| 690 |
const KEY_TABLE_TYPE keyTable, |
| 691 |
unsigned char *plaintext) |
| 692 |
{ |
| 693 |
_gcry_camellia_arm_decrypt_block(keyTable, plaintext, cipherText, |
| 694 |
keyBitLength); |
| 695 |
} |
| 696 |
|
| 697 |
#ifdef __aarch64__ |
| 698 |
# define CAMELLIA_encrypt_stack_burn_size (0) |
| 699 |
# define CAMELLIA_decrypt_stack_burn_size (0) |
| 700 |
#else |
| 701 |
# define CAMELLIA_encrypt_stack_burn_size (15*4) |
| 702 |
# define CAMELLIA_decrypt_stack_burn_size (15*4) |
| 703 |
#endif |
| 704 |
|
| 705 |
static unsigned int |
| 706 |
camellia_encrypt(void *c, byte *outbuf, const byte *inbuf) |
| 707 |
{ |
| 708 |
CAMELLIA_context *ctx = c; |
| 709 |
Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf); |
| 710 |
return /*burn_stack*/ (CAMELLIA_encrypt_stack_burn_size); |
| 711 |
} |
| 712 |
|
| 713 |
static unsigned int |
| 714 |
camellia_decrypt(void *c, byte *outbuf, const byte *inbuf) |
| 715 |
{ |
| 716 |
CAMELLIA_context *ctx=c; |
| 717 |
Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf); |
| 718 |
return /*burn_stack*/ (CAMELLIA_decrypt_stack_burn_size); |
| 719 |
} |
| 720 |
|
| 721 |
#else /*USE_ARM_ASM*/ |
| 722 |
|
| 723 |
static unsigned int |
| 724 |
camellia_encrypt(void *c, byte *outbuf, const byte *inbuf) |
| 725 |
{ |
| 726 |
CAMELLIA_context *ctx=c; |
| 727 |
|
| 728 |
#ifdef USE_GFNI_AVX512 |
| 729 |
if (ctx->use_gfni_avx512) |
| 730 |
{ |
| 731 |
_gcry_camellia_gfni_avx512_enc_blk1(ctx, outbuf, inbuf); |
| 732 |
return 0; |
| 733 |
} |
| 734 |
#endif |
| 735 |
|
| 736 |
Camellia_EncryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf); |
| 737 |
|
| 738 |
#define CAMELLIA_encrypt_stack_burn_size \ |
| 739 |
(sizeof(int)+2*sizeof(unsigned char *)+sizeof(void*/*KEY_TABLE_TYPE*/) \ |
| 740 |
+4*sizeof(u32)+4*sizeof(u32) \ |
| 741 |
+2*sizeof(u32*)+4*sizeof(u32) \ |
| 742 |
+2*2*sizeof(void*) /* Function calls. */ \ |
| 743 |
) |
| 744 |
|
| 745 |
return /*burn_stack*/ (CAMELLIA_encrypt_stack_burn_size); |
| 746 |
} |
| 747 |
|
| 748 |
static unsigned int |
| 749 |
camellia_decrypt(void *c, byte *outbuf, const byte *inbuf) |
| 750 |
{ |
| 751 |
CAMELLIA_context *ctx=c; |
| 752 |
|
| 753 |
#ifdef USE_GFNI_AVX512 |
| 754 |
if (ctx->use_gfni_avx512) |
| 755 |
{ |
| 756 |
_gcry_camellia_gfni_avx512_dec_blk1(ctx, outbuf, inbuf); |
| 757 |
return 0; |
| 758 |
} |
| 759 |
#endif |
| 760 |
|
| 761 |
Camellia_DecryptBlock(ctx->keybitlength,inbuf,ctx->keytable,outbuf); |
| 762 |
|
| 763 |
#define CAMELLIA_decrypt_stack_burn_size \ |
| 764 |
(sizeof(int)+2*sizeof(unsigned char *)+sizeof(void*/*KEY_TABLE_TYPE*/) \ |
| 765 |
+4*sizeof(u32)+4*sizeof(u32) \ |
| 766 |
+2*sizeof(u32*)+4*sizeof(u32) \ |
| 767 |
+2*2*sizeof(void*) /* Function calls. */ \ |
| 768 |
) |
| 769 |
|
| 770 |
return /*burn_stack*/ (CAMELLIA_decrypt_stack_burn_size); |
| 771 |
} |
| 772 |
|
| 773 |
#endif /*!USE_ARM_ASM*/ |
| 774 |
|
| 775 |
|
| 776 |
static unsigned int |
| 777 |
camellia_encrypt_blk1_32 (void *priv, byte *outbuf, const byte *inbuf, |
| 778 |
size_t num_blks) |
| 779 |
{ |
| 780 |
const CAMELLIA_context *ctx = priv; |
| 781 |
unsigned int stack_burn_size = 0; |
| 782 |
|
| 783 |
gcry_assert (num_blks <= 32); |
| 784 |
|
| 785 |
#ifdef USE_GFNI_AVX2 |
| 786 |
if (ctx->use_gfni_avx2 && num_blks >= 2) |
| 787 |
{ |
| 788 |
/* 2 or more parallel block GFNI processing is faster than |
| 789 |
* generic C implementation. */ |
| 790 |
_gcry_camellia_gfni_avx2_enc_blk1_32 (ctx, outbuf, inbuf, num_blks); |
| 791 |
return avx2_burn_stack_depth; |
| 792 |
} |
| 793 |
#endif |
| 794 |
#ifdef USE_VAES_AVX2 |
| 795 |
if (ctx->use_vaes_avx2 && num_blks >= 4) |
| 796 |
{ |
| 797 |
/* 4 or more parallel block VAES processing is faster than |
| 798 |
* generic C implementation. */ |
| 799 |
_gcry_camellia_vaes_avx2_enc_blk1_32 (ctx, outbuf, inbuf, num_blks); |
| 800 |
return avx2_burn_stack_depth; |
| 801 |
} |
| 802 |
#endif |
| 803 |
#ifdef USE_AESNI_AVX2 |
| 804 |
if (ctx->use_aesni_avx2 && num_blks >= 5) |
| 805 |
{ |
| 806 |
/* 5 or more parallel block AESNI processing is faster than |
| 807 |
* generic C implementation. */ |
| 808 |
_gcry_camellia_aesni_avx2_enc_blk1_32 (ctx, outbuf, inbuf, num_blks); |
| 809 |
return avx2_burn_stack_depth; |
| 810 |
} |
| 811 |
#endif |
| 812 |
#ifdef USE_AESNI_AVX |
| 813 |
while (ctx->use_aesni_avx && num_blks >= 16) |
| 814 |
{ |
| 815 |
_gcry_camellia_aesni_avx_ecb_enc (ctx, outbuf, inbuf); |
| 816 |
stack_burn_size = avx_burn_stack_depth; |
| 817 |
outbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 818 |
inbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 819 |
num_blks -= 16; |
| 820 |
} |
| 821 |
#endif |
| 822 |
#ifdef USE_PPC_CRYPTO |
| 823 |
while (ctx->use_ppc && num_blks >= 16) |
| 824 |
{ |
| 825 |
camellia_ppc_enc_blk16 (ctx, outbuf, inbuf); |
| 826 |
stack_burn_size = ppc_burn_stack_depth; |
| 827 |
outbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 828 |
inbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 829 |
num_blks -= 16; |
| 830 |
} |
| 831 |
#endif |
| 832 |
#ifdef USE_AARCH64_CE |
| 833 |
while (ctx->use_aarch64ce && num_blks >= 16) |
| 834 |
{ |
| 835 |
camellia_aarch64ce_enc_blk16 (ctx, outbuf, inbuf); |
| 836 |
stack_burn_size = aarch64ce_burn_stack_depth; |
| 837 |
outbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 838 |
inbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 839 |
num_blks -= 16; |
| 840 |
} |
| 841 |
#endif |
| 842 |
|
| 843 |
while (num_blks) |
| 844 |
{ |
| 845 |
unsigned int nburn = camellia_encrypt((void *)ctx, outbuf, inbuf); |
| 846 |
stack_burn_size = nburn > stack_burn_size ? nburn : stack_burn_size; |
| 847 |
outbuf += CAMELLIA_BLOCK_SIZE; |
| 848 |
inbuf += CAMELLIA_BLOCK_SIZE; |
| 849 |
num_blks--; |
| 850 |
} |
| 851 |
|
| 852 |
return stack_burn_size; |
| 853 |
} |
| 854 |
|
| 855 |
static unsigned int |
| 856 |
camellia_encrypt_blk1_64 (void *priv, byte *outbuf, const byte *inbuf, |
| 857 |
size_t num_blks) |
| 858 |
{ |
| 859 |
CAMELLIA_context *ctx = priv; |
| 860 |
unsigned int stack_burn_size = 0; |
| 861 |
unsigned int nburn; |
| 862 |
|
| 863 |
gcry_assert (num_blks <= 64); |
| 864 |
|
| 865 |
#ifdef USE_GFNI_AVX512 |
| 866 |
if (num_blks == 64 && ctx->use_gfni_avx512) |
| 867 |
{ |
| 868 |
_gcry_camellia_gfni_avx512_enc_blk64 (ctx, outbuf, inbuf); |
| 869 |
return avx512_burn_stack_depth; |
| 870 |
} |
| 871 |
#endif |
| 872 |
|
| 873 |
do |
| 874 |
{ |
| 875 |
unsigned int curr_blks = num_blks > 32 ? 32 : num_blks; |
| 876 |
nburn = camellia_encrypt_blk1_32 (ctx, outbuf, inbuf, curr_blks); |
| 877 |
stack_burn_size = nburn > stack_burn_size ? nburn : stack_burn_size; |
| 878 |
outbuf += curr_blks * 16; |
| 879 |
inbuf += curr_blks * 16; |
| 880 |
num_blks -= curr_blks; |
| 881 |
} |
| 882 |
while (num_blks > 0); |
| 883 |
|
| 884 |
return stack_burn_size; |
| 885 |
} |
| 886 |
|
| 887 |
static unsigned int |
| 888 |
camellia_decrypt_blk1_32 (void *priv, byte *outbuf, const byte *inbuf, |
| 889 |
size_t num_blks) |
| 890 |
{ |
| 891 |
const CAMELLIA_context *ctx = priv; |
| 892 |
unsigned int stack_burn_size = 0; |
| 893 |
|
| 894 |
gcry_assert (num_blks <= 32); |
| 895 |
|
| 896 |
#ifdef USE_GFNI_AVX2 |
| 897 |
if (ctx->use_gfni_avx2 && num_blks >= 2) |
| 898 |
{ |
| 899 |
/* 2 or more parallel block GFNI processing is faster than |
| 900 |
* generic C implementation. */ |
| 901 |
_gcry_camellia_gfni_avx2_dec_blk1_32 (ctx, outbuf, inbuf, num_blks); |
| 902 |
return avx2_burn_stack_depth; |
| 903 |
} |
| 904 |
#endif |
| 905 |
#ifdef USE_VAES_AVX2 |
| 906 |
if (ctx->use_vaes_avx2 && num_blks >= 4) |
| 907 |
{ |
| 908 |
/* 4 or more parallel block VAES processing is faster than |
| 909 |
* generic C implementation. */ |
| 910 |
_gcry_camellia_vaes_avx2_dec_blk1_32 (ctx, outbuf, inbuf, num_blks); |
| 911 |
return avx2_burn_stack_depth; |
| 912 |
} |
| 913 |
#endif |
| 914 |
#ifdef USE_AESNI_AVX2 |
| 915 |
if (ctx->use_aesni_avx2 && num_blks >= 5) |
| 916 |
{ |
| 917 |
/* 5 or more parallel block AESNI processing is faster than |
| 918 |
* generic C implementation. */ |
| 919 |
_gcry_camellia_aesni_avx2_dec_blk1_32 (ctx, outbuf, inbuf, num_blks); |
| 920 |
return avx2_burn_stack_depth; |
| 921 |
} |
| 922 |
#endif |
| 923 |
#ifdef USE_AESNI_AVX |
| 924 |
while (ctx->use_aesni_avx && num_blks >= 16) |
| 925 |
{ |
| 926 |
_gcry_camellia_aesni_avx_ecb_dec (ctx, outbuf, inbuf); |
| 927 |
stack_burn_size = avx_burn_stack_depth; |
| 928 |
outbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 929 |
inbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 930 |
num_blks -= 16; |
| 931 |
} |
| 932 |
#endif |
| 933 |
#ifdef USE_PPC_CRYPTO |
| 934 |
while (ctx->use_ppc && num_blks >= 16) |
| 935 |
{ |
| 936 |
camellia_ppc_dec_blk16 (ctx, outbuf, inbuf); |
| 937 |
stack_burn_size = ppc_burn_stack_depth; |
| 938 |
outbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 939 |
inbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 940 |
num_blks -= 16; |
| 941 |
} |
| 942 |
#endif |
| 943 |
#ifdef USE_AARCH64_CE |
| 944 |
while (ctx->use_aarch64ce && num_blks >= 16) |
| 945 |
{ |
| 946 |
camellia_aarch64ce_dec_blk16 (ctx, outbuf, inbuf); |
| 947 |
stack_burn_size = aarch64ce_burn_stack_depth; |
| 948 |
outbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 949 |
inbuf += CAMELLIA_BLOCK_SIZE * 16; |
| 950 |
num_blks -= 16; |
| 951 |
} |
| 952 |
#endif |
| 953 |
|
| 954 |
while (num_blks) |
| 955 |
{ |
| 956 |
unsigned int nburn = camellia_decrypt((void *)ctx, outbuf, inbuf); |
| 957 |
stack_burn_size = nburn > stack_burn_size ? nburn : stack_burn_size; |
| 958 |
outbuf += CAMELLIA_BLOCK_SIZE; |
| 959 |
inbuf += CAMELLIA_BLOCK_SIZE; |
| 960 |
num_blks--; |
| 961 |
} |
| 962 |
|
| 963 |
return stack_burn_size; |
| 964 |
} |
| 965 |
|
| 966 |
static unsigned int |
| 967 |
camellia_decrypt_blk1_64 (void *priv, byte *outbuf, const byte *inbuf, |
| 968 |
size_t num_blks) |
| 969 |
{ |
| 970 |
CAMELLIA_context *ctx = priv; |
| 971 |
unsigned int stack_burn_size = 0; |
| 972 |
unsigned int nburn; |
| 973 |
|
| 974 |
gcry_assert (num_blks <= 64); |
| 975 |
|
| 976 |
#ifdef USE_GFNI_AVX512 |
| 977 |
if (num_blks == 64 && ctx->use_gfni_avx512) |
| 978 |
{ |
| 979 |
_gcry_camellia_gfni_avx512_dec_blk64 (ctx, outbuf, inbuf); |
| 980 |
return avx512_burn_stack_depth; |
| 981 |
} |
| 982 |
#endif |
| 983 |
|
| 984 |
do |
| 985 |
{ |
| 986 |
unsigned int curr_blks = num_blks > 32 ? 32 : num_blks; |
| 987 |
nburn = camellia_decrypt_blk1_32 (ctx, outbuf, inbuf, curr_blks); |
| 988 |
stack_burn_size = nburn > stack_burn_size ? nburn : stack_burn_size; |
| 989 |
outbuf += curr_blks * 16; |
| 990 |
inbuf += curr_blks * 16; |
| 991 |
num_blks -= curr_blks; |
| 992 |
} |
| 993 |
while (num_blks > 0); |
| 994 |
|
| 995 |
return stack_burn_size; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
/* Bulk encryption of complete blocks in CTR mode. This function is only |
| 1000 |
intended for the bulk encryption feature of cipher.c. CTR is expected to be |
| 1001 |
of size CAMELLIA_BLOCK_SIZE. */ |
| 1002 |
static void |
| 1003 |
_gcry_camellia_ctr_enc(void *context, unsigned char *ctr, |
| 1004 |
void *outbuf_arg, const void *inbuf_arg, |
| 1005 |
size_t nblocks) |
| 1006 |
{ |
| 1007 |
CAMELLIA_context *ctx = context; |
| 1008 |
unsigned char *outbuf = outbuf_arg; |
| 1009 |
const unsigned char *inbuf = inbuf_arg; |
| 1010 |
int burn_stack_depth = 0; |
| 1011 |
|
| 1012 |
#ifdef USE_GFNI_AVX512 |
| 1013 |
if (ctx->use_gfni_avx512) |
| 1014 |
{ |
| 1015 |
int did_use_gfni_avx512 = 0; |
| 1016 |
|
| 1017 |
/* Process data in 64 block chunks. */ |
| 1018 |
while (nblocks >= 64) |
| 1019 |
{ |
| 1020 |
_gcry_camellia_gfni_avx512_ctr_enc (ctx, outbuf, inbuf, ctr); |
| 1021 |
nblocks -= 64; |
| 1022 |
outbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1023 |
inbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1024 |
did_use_gfni_avx512 = 1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
if (did_use_gfni_avx512) |
| 1028 |
{ |
| 1029 |
if (burn_stack_depth < avx512_burn_stack_depth) |
| 1030 |
burn_stack_depth = avx512_burn_stack_depth; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/* Use generic code to handle smaller chunks... */ |
| 1034 |
} |
| 1035 |
#endif |
| 1036 |
|
| 1037 |
#ifdef USE_AESNI_AVX2 |
| 1038 |
if (ctx->use_avx2) |
| 1039 |
{ |
| 1040 |
int did_use_aesni_avx2 = 0; |
| 1041 |
typeof (&_gcry_camellia_aesni_avx2_ctr_enc) bulk_ctr_fn = |
| 1042 |
_gcry_camellia_aesni_avx2_ctr_enc; |
| 1043 |
|
| 1044 |
#ifdef USE_VAES_AVX2 |
| 1045 |
if (ctx->use_vaes_avx2) |
| 1046 |
bulk_ctr_fn =_gcry_camellia_vaes_avx2_ctr_enc; |
| 1047 |
#endif |
| 1048 |
#ifdef USE_GFNI_AVX2 |
| 1049 |
if (ctx->use_gfni_avx2) |
| 1050 |
bulk_ctr_fn =_gcry_camellia_gfni_avx2_ctr_enc; |
| 1051 |
#endif |
| 1052 |
|
| 1053 |
/* Process data in 32 block chunks. */ |
| 1054 |
while (nblocks >= 32) |
| 1055 |
{ |
| 1056 |
bulk_ctr_fn (ctx, outbuf, inbuf, ctr); |
| 1057 |
nblocks -= 32; |
| 1058 |
outbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1059 |
inbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1060 |
did_use_aesni_avx2 = 1; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (did_use_aesni_avx2) |
| 1064 |
{ |
| 1065 |
if (burn_stack_depth < avx2_burn_stack_depth) |
| 1066 |
burn_stack_depth = avx2_burn_stack_depth; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* Use generic code to handle smaller chunks... */ |
| 1070 |
} |
| 1071 |
#endif |
| 1072 |
|
| 1073 |
#ifdef USE_AESNI_AVX |
| 1074 |
if (ctx->use_aesni_avx) |
| 1075 |
{ |
| 1076 |
int did_use_aesni_avx = 0; |
| 1077 |
|
| 1078 |
/* Process data in 16 block chunks. */ |
| 1079 |
while (nblocks >= 16) |
| 1080 |
{ |
| 1081 |
_gcry_camellia_aesni_avx_ctr_enc(ctx, outbuf, inbuf, ctr); |
| 1082 |
|
| 1083 |
nblocks -= 16; |
| 1084 |
outbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1085 |
inbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1086 |
did_use_aesni_avx = 1; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (did_use_aesni_avx) |
| 1090 |
{ |
| 1091 |
if (burn_stack_depth < avx_burn_stack_depth) |
| 1092 |
burn_stack_depth = avx_burn_stack_depth; |
| 1093 |
} |
| 1094 |
|
| 1095 |
/* Use generic code to handle smaller chunks... */ |
| 1096 |
} |
| 1097 |
#endif |
| 1098 |
|
| 1099 |
/* Process remaining blocks. */ |
| 1100 |
if (nblocks) |
| 1101 |
{ |
| 1102 |
byte tmpbuf[CAMELLIA_BLOCK_SIZE * 32]; |
| 1103 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1104 |
size_t nburn; |
| 1105 |
|
| 1106 |
nburn = bulk_ctr_enc_128(ctx, camellia_encrypt_blk1_32, outbuf, inbuf, |
| 1107 |
nblocks, ctr, tmpbuf, |
| 1108 |
sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, &tmp_used); |
| 1109 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1110 |
|
| 1111 |
wipememory(tmpbuf, tmp_used); |
| 1112 |
} |
| 1113 |
|
| 1114 |
if (burn_stack_depth) |
| 1115 |
_gcry_burn_stack(burn_stack_depth); |
| 1116 |
} |
| 1117 |
|
| 1118 |
/* Bulk decryption of complete blocks in CBC mode. This function is only |
| 1119 |
intended for the bulk encryption feature of cipher.c. */ |
| 1120 |
static void |
| 1121 |
_gcry_camellia_cbc_dec(void *context, unsigned char *iv, |
| 1122 |
void *outbuf_arg, const void *inbuf_arg, |
| 1123 |
size_t nblocks) |
| 1124 |
{ |
| 1125 |
CAMELLIA_context *ctx = context; |
| 1126 |
unsigned char *outbuf = outbuf_arg; |
| 1127 |
const unsigned char *inbuf = inbuf_arg; |
| 1128 |
int burn_stack_depth = 0; |
| 1129 |
|
| 1130 |
#ifdef USE_GFNI_AVX512 |
| 1131 |
if (ctx->use_gfni_avx512) |
| 1132 |
{ |
| 1133 |
int did_use_gfni_avx512 = 0; |
| 1134 |
|
| 1135 |
/* Process data in 64 block chunks. */ |
| 1136 |
while (nblocks >= 64) |
| 1137 |
{ |
| 1138 |
_gcry_camellia_gfni_avx512_cbc_dec (ctx, outbuf, inbuf, iv); |
| 1139 |
nblocks -= 64; |
| 1140 |
outbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1141 |
inbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1142 |
did_use_gfni_avx512 = 1; |
| 1143 |
} |
| 1144 |
|
| 1145 |
if (did_use_gfni_avx512) |
| 1146 |
{ |
| 1147 |
if (burn_stack_depth < avx512_burn_stack_depth) |
| 1148 |
burn_stack_depth = avx512_burn_stack_depth; |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* Use generic code to handle smaller chunks... */ |
| 1152 |
} |
| 1153 |
#endif |
| 1154 |
|
| 1155 |
#ifdef USE_AESNI_AVX2 |
| 1156 |
if (ctx->use_avx2) |
| 1157 |
{ |
| 1158 |
int did_use_aesni_avx2 = 0; |
| 1159 |
typeof (&_gcry_camellia_aesni_avx2_cbc_dec) bulk_cbc_fn = |
| 1160 |
_gcry_camellia_aesni_avx2_cbc_dec; |
| 1161 |
|
| 1162 |
#ifdef USE_VAES_AVX2 |
| 1163 |
if (ctx->use_vaes_avx2) |
| 1164 |
bulk_cbc_fn =_gcry_camellia_vaes_avx2_cbc_dec; |
| 1165 |
#endif |
| 1166 |
#ifdef USE_GFNI_AVX2 |
| 1167 |
if (ctx->use_gfni_avx2) |
| 1168 |
bulk_cbc_fn =_gcry_camellia_gfni_avx2_cbc_dec; |
| 1169 |
#endif |
| 1170 |
|
| 1171 |
/* Process data in 32 block chunks. */ |
| 1172 |
while (nblocks >= 32) |
| 1173 |
{ |
| 1174 |
bulk_cbc_fn (ctx, outbuf, inbuf, iv); |
| 1175 |
nblocks -= 32; |
| 1176 |
outbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1177 |
inbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1178 |
did_use_aesni_avx2 = 1; |
| 1179 |
} |
| 1180 |
|
| 1181 |
if (did_use_aesni_avx2) |
| 1182 |
{ |
| 1183 |
if (burn_stack_depth < avx2_burn_stack_depth) |
| 1184 |
burn_stack_depth = avx2_burn_stack_depth; |
| 1185 |
} |
| 1186 |
|
| 1187 |
/* Use generic code to handle smaller chunks... */ |
| 1188 |
} |
| 1189 |
#endif |
| 1190 |
|
| 1191 |
#ifdef USE_AESNI_AVX |
| 1192 |
if (ctx->use_aesni_avx) |
| 1193 |
{ |
| 1194 |
int did_use_aesni_avx = 0; |
| 1195 |
|
| 1196 |
/* Process data in 16 block chunks. */ |
| 1197 |
while (nblocks >= 16) |
| 1198 |
{ |
| 1199 |
_gcry_camellia_aesni_avx_cbc_dec(ctx, outbuf, inbuf, iv); |
| 1200 |
|
| 1201 |
nblocks -= 16; |
| 1202 |
outbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1203 |
inbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1204 |
did_use_aesni_avx = 1; |
| 1205 |
} |
| 1206 |
|
| 1207 |
if (did_use_aesni_avx) |
| 1208 |
{ |
| 1209 |
if (burn_stack_depth < avx_burn_stack_depth) |
| 1210 |
burn_stack_depth = avx_burn_stack_depth; |
| 1211 |
} |
| 1212 |
|
| 1213 |
/* Use generic code to handle smaller chunks... */ |
| 1214 |
} |
| 1215 |
#endif |
| 1216 |
|
| 1217 |
/* Process remaining blocks. */ |
| 1218 |
if (nblocks) |
| 1219 |
{ |
| 1220 |
byte tmpbuf[CAMELLIA_BLOCK_SIZE * 32]; |
| 1221 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1222 |
size_t nburn; |
| 1223 |
|
| 1224 |
nburn = bulk_cbc_dec_128(ctx, camellia_decrypt_blk1_32, outbuf, inbuf, |
| 1225 |
nblocks, iv, tmpbuf, |
| 1226 |
sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, &tmp_used); |
| 1227 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1228 |
|
| 1229 |
wipememory(tmpbuf, tmp_used); |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (burn_stack_depth) |
| 1233 |
_gcry_burn_stack(burn_stack_depth); |
| 1234 |
} |
| 1235 |
|
| 1236 |
/* Bulk decryption of complete blocks in CFB mode. This function is only |
| 1237 |
intended for the bulk encryption feature of cipher.c. */ |
| 1238 |
static void |
| 1239 |
_gcry_camellia_cfb_dec(void *context, unsigned char *iv, |
| 1240 |
void *outbuf_arg, const void *inbuf_arg, |
| 1241 |
size_t nblocks) |
| 1242 |
{ |
| 1243 |
CAMELLIA_context *ctx = context; |
| 1244 |
unsigned char *outbuf = outbuf_arg; |
| 1245 |
const unsigned char *inbuf = inbuf_arg; |
| 1246 |
int burn_stack_depth = 0; |
| 1247 |
|
| 1248 |
#ifdef USE_GFNI_AVX512 |
| 1249 |
if (ctx->use_gfni_avx512) |
| 1250 |
{ |
| 1251 |
int did_use_gfni_avx512 = 0; |
| 1252 |
|
| 1253 |
/* Process data in 64 block chunks. */ |
| 1254 |
while (nblocks >= 64) |
| 1255 |
{ |
| 1256 |
_gcry_camellia_gfni_avx512_cfb_dec (ctx, outbuf, inbuf, iv); |
| 1257 |
nblocks -= 64; |
| 1258 |
outbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1259 |
inbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1260 |
did_use_gfni_avx512 = 1; |
| 1261 |
} |
| 1262 |
|
| 1263 |
if (did_use_gfni_avx512) |
| 1264 |
{ |
| 1265 |
if (burn_stack_depth < avx512_burn_stack_depth) |
| 1266 |
burn_stack_depth = avx512_burn_stack_depth; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/* Use generic code to handle smaller chunks... */ |
| 1270 |
} |
| 1271 |
#endif |
| 1272 |
|
| 1273 |
#ifdef USE_AESNI_AVX2 |
| 1274 |
if (ctx->use_avx2) |
| 1275 |
{ |
| 1276 |
int did_use_aesni_avx2 = 0; |
| 1277 |
typeof (&_gcry_camellia_aesni_avx2_cfb_dec) bulk_cfb_fn = |
| 1278 |
_gcry_camellia_aesni_avx2_cfb_dec; |
| 1279 |
|
| 1280 |
#ifdef USE_VAES_AVX2 |
| 1281 |
if (ctx->use_vaes_avx2) |
| 1282 |
bulk_cfb_fn =_gcry_camellia_vaes_avx2_cfb_dec; |
| 1283 |
#endif |
| 1284 |
#ifdef USE_GFNI_AVX2 |
| 1285 |
if (ctx->use_gfni_avx2) |
| 1286 |
bulk_cfb_fn =_gcry_camellia_gfni_avx2_cfb_dec; |
| 1287 |
#endif |
| 1288 |
|
| 1289 |
/* Process data in 32 block chunks. */ |
| 1290 |
while (nblocks >= 32) |
| 1291 |
{ |
| 1292 |
bulk_cfb_fn (ctx, outbuf, inbuf, iv); |
| 1293 |
nblocks -= 32; |
| 1294 |
outbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1295 |
inbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1296 |
did_use_aesni_avx2 = 1; |
| 1297 |
} |
| 1298 |
|
| 1299 |
if (did_use_aesni_avx2) |
| 1300 |
{ |
| 1301 |
if (burn_stack_depth < avx2_burn_stack_depth) |
| 1302 |
burn_stack_depth = avx2_burn_stack_depth; |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* Use generic code to handle smaller chunks... */ |
| 1306 |
} |
| 1307 |
#endif |
| 1308 |
|
| 1309 |
#ifdef USE_AESNI_AVX |
| 1310 |
if (ctx->use_aesni_avx) |
| 1311 |
{ |
| 1312 |
int did_use_aesni_avx = 0; |
| 1313 |
|
| 1314 |
/* Process data in 16 block chunks. */ |
| 1315 |
while (nblocks >= 16) |
| 1316 |
{ |
| 1317 |
_gcry_camellia_aesni_avx_cfb_dec(ctx, outbuf, inbuf, iv); |
| 1318 |
|
| 1319 |
nblocks -= 16; |
| 1320 |
outbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1321 |
inbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1322 |
did_use_aesni_avx = 1; |
| 1323 |
} |
| 1324 |
|
| 1325 |
if (did_use_aesni_avx) |
| 1326 |
{ |
| 1327 |
if (burn_stack_depth < avx_burn_stack_depth) |
| 1328 |
burn_stack_depth = avx_burn_stack_depth; |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* Use generic code to handle smaller chunks... */ |
| 1332 |
} |
| 1333 |
#endif |
| 1334 |
|
| 1335 |
/* Process remaining blocks. */ |
| 1336 |
if (nblocks) |
| 1337 |
{ |
| 1338 |
byte tmpbuf[CAMELLIA_BLOCK_SIZE * 32]; |
| 1339 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1340 |
size_t nburn; |
| 1341 |
|
| 1342 |
nburn = bulk_cfb_dec_128(ctx, camellia_encrypt_blk1_32, outbuf, inbuf, |
| 1343 |
nblocks, iv, tmpbuf, |
| 1344 |
sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, &tmp_used); |
| 1345 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1346 |
|
| 1347 |
wipememory(tmpbuf, tmp_used); |
| 1348 |
} |
| 1349 |
|
| 1350 |
if (burn_stack_depth) |
| 1351 |
_gcry_burn_stack(burn_stack_depth); |
| 1352 |
} |
| 1353 |
|
| 1354 |
/* Bulk encryption/decryption in ECB mode. */ |
| 1355 |
static void |
| 1356 |
_gcry_camellia_ecb_crypt (void *context, void *outbuf_arg, |
| 1357 |
const void *inbuf_arg, size_t nblocks, int encrypt) |
| 1358 |
{ |
| 1359 |
CAMELLIA_context *ctx = context; |
| 1360 |
unsigned char *outbuf = outbuf_arg; |
| 1361 |
const unsigned char *inbuf = inbuf_arg; |
| 1362 |
int burn_stack_depth = 0; |
| 1363 |
|
| 1364 |
/* Process remaining blocks. */ |
| 1365 |
if (nblocks) |
| 1366 |
{ |
| 1367 |
size_t nburn; |
| 1368 |
|
| 1369 |
nburn = bulk_ecb_crypt_128(ctx, encrypt ? camellia_encrypt_blk1_64 |
| 1370 |
: camellia_decrypt_blk1_64, |
| 1371 |
outbuf, inbuf, nblocks, 64); |
| 1372 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (burn_stack_depth) |
| 1376 |
_gcry_burn_stack(burn_stack_depth); |
| 1377 |
} |
| 1378 |
|
| 1379 |
/* Bulk encryption/decryption of complete blocks in XTS mode. */ |
| 1380 |
static void |
| 1381 |
_gcry_camellia_xts_crypt (void *context, unsigned char *tweak, |
| 1382 |
void *outbuf_arg, const void *inbuf_arg, |
| 1383 |
size_t nblocks, int encrypt) |
| 1384 |
{ |
| 1385 |
CAMELLIA_context *ctx = context; |
| 1386 |
unsigned char *outbuf = outbuf_arg; |
| 1387 |
const unsigned char *inbuf = inbuf_arg; |
| 1388 |
int burn_stack_depth = 0; |
| 1389 |
|
| 1390 |
/* Process remaining blocks. */ |
| 1391 |
if (nblocks) |
| 1392 |
{ |
| 1393 |
byte tmpbuf[CAMELLIA_BLOCK_SIZE * 64]; |
| 1394 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1395 |
size_t nburn; |
| 1396 |
|
| 1397 |
nburn = bulk_xts_crypt_128(ctx, encrypt ? camellia_encrypt_blk1_64 |
| 1398 |
: camellia_decrypt_blk1_64, |
| 1399 |
outbuf, inbuf, nblocks, tweak, tmpbuf, |
| 1400 |
sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, |
| 1401 |
&tmp_used); |
| 1402 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1403 |
|
| 1404 |
wipememory(tmpbuf, tmp_used); |
| 1405 |
} |
| 1406 |
|
| 1407 |
if (burn_stack_depth) |
| 1408 |
_gcry_burn_stack(burn_stack_depth); |
| 1409 |
} |
| 1410 |
|
| 1411 |
/* Bulk encryption of complete blocks in CTR32LE mode (for GCM-SIV). */ |
| 1412 |
static void |
| 1413 |
_gcry_camellia_ctr32le_enc(void *context, unsigned char *ctr, |
| 1414 |
void *outbuf_arg, const void *inbuf_arg, |
| 1415 |
size_t nblocks) |
| 1416 |
{ |
| 1417 |
CAMELLIA_context *ctx = context; |
| 1418 |
byte *outbuf = outbuf_arg; |
| 1419 |
const byte *inbuf = inbuf_arg; |
| 1420 |
int burn_stack_depth = 0; |
| 1421 |
|
| 1422 |
/* Process remaining blocks. */ |
| 1423 |
if (nblocks) |
| 1424 |
{ |
| 1425 |
byte tmpbuf[64 * CAMELLIA_BLOCK_SIZE]; |
| 1426 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1427 |
size_t nburn; |
| 1428 |
|
| 1429 |
nburn = bulk_ctr32le_enc_128 (ctx, camellia_encrypt_blk1_64, outbuf, |
| 1430 |
inbuf, nblocks, ctr, tmpbuf, |
| 1431 |
sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, |
| 1432 |
&tmp_used); |
| 1433 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1434 |
|
| 1435 |
wipememory (tmpbuf, tmp_used); |
| 1436 |
} |
| 1437 |
|
| 1438 |
if (burn_stack_depth) |
| 1439 |
_gcry_burn_stack (burn_stack_depth); |
| 1440 |
} |
| 1441 |
|
| 1442 |
/* Bulk encryption/decryption of complete blocks in OCB mode. */ |
| 1443 |
static size_t |
| 1444 |
_gcry_camellia_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg, |
| 1445 |
const void *inbuf_arg, size_t nblocks, int encrypt) |
| 1446 |
{ |
| 1447 |
#if defined(USE_PPC_CRYPTO) || defined(USE_AESNI_AVX) || defined(USE_AESNI_AVX2) |
| 1448 |
CAMELLIA_context *ctx = (void *)&c->context.c; |
| 1449 |
unsigned char *outbuf = outbuf_arg; |
| 1450 |
const unsigned char *inbuf = inbuf_arg; |
| 1451 |
int burn_stack_depth = 0; |
| 1452 |
u64 blkn = c->u_mode.ocb.data_nblocks; |
| 1453 |
|
| 1454 |
#else |
| 1455 |
(void)c; |
| 1456 |
(void)outbuf_arg; |
| 1457 |
(void)inbuf_arg; |
| 1458 |
(void)encrypt; |
| 1459 |
#endif |
| 1460 |
|
| 1461 |
#ifdef USE_GFNI_AVX512 |
| 1462 |
if (ctx->use_gfni_avx512) |
| 1463 |
{ |
| 1464 |
int did_use_gfni_avx512 = 0; |
| 1465 |
u64 Ls[64]; |
| 1466 |
u64 *l; |
| 1467 |
|
| 1468 |
if (nblocks >= 64) |
| 1469 |
{ |
| 1470 |
typeof (&_gcry_camellia_gfni_avx512_ocb_dec) bulk_ocb_fn = |
| 1471 |
encrypt ? _gcry_camellia_gfni_avx512_ocb_enc |
| 1472 |
: _gcry_camellia_gfni_avx512_ocb_dec; |
| 1473 |
l = bulk_ocb_prepare_L_pointers_array_blk64 (c, Ls, blkn); |
| 1474 |
|
| 1475 |
/* Process data in 64 block chunks. */ |
| 1476 |
while (nblocks >= 64) |
| 1477 |
{ |
| 1478 |
blkn += 64; |
| 1479 |
*l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 64); |
| 1480 |
|
| 1481 |
bulk_ocb_fn (ctx, outbuf, inbuf, c->u_iv.iv, c->u_ctr.ctr, Ls); |
| 1482 |
|
| 1483 |
nblocks -= 64; |
| 1484 |
outbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1485 |
inbuf += 64 * CAMELLIA_BLOCK_SIZE; |
| 1486 |
did_use_gfni_avx512 = 1; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
if (did_use_gfni_avx512) |
| 1491 |
{ |
| 1492 |
if (burn_stack_depth < avx2_burn_stack_depth) |
| 1493 |
burn_stack_depth = avx2_burn_stack_depth; |
| 1494 |
} |
| 1495 |
|
| 1496 |
/* Use generic code to handle smaller chunks... */ |
| 1497 |
} |
| 1498 |
#endif |
| 1499 |
|
| 1500 |
#ifdef USE_AESNI_AVX2 |
| 1501 |
if (ctx->use_avx2) |
| 1502 |
{ |
| 1503 |
int did_use_aesni_avx2 = 0; |
| 1504 |
u64 Ls[32]; |
| 1505 |
u64 *l; |
| 1506 |
|
| 1507 |
if (nblocks >= 32) |
| 1508 |
{ |
| 1509 |
typeof (&_gcry_camellia_aesni_avx2_ocb_dec) bulk_ocb_fn = |
| 1510 |
encrypt ? _gcry_camellia_aesni_avx2_ocb_enc |
| 1511 |
: _gcry_camellia_aesni_avx2_ocb_dec; |
| 1512 |
|
| 1513 |
#ifdef USE_VAES_AVX2 |
| 1514 |
if (ctx->use_vaes_avx2) |
| 1515 |
bulk_ocb_fn = encrypt ? _gcry_camellia_vaes_avx2_ocb_enc |
| 1516 |
: _gcry_camellia_vaes_avx2_ocb_dec; |
| 1517 |
#endif |
| 1518 |
#ifdef USE_GFNI_AVX2 |
| 1519 |
if (ctx->use_gfni_avx2) |
| 1520 |
bulk_ocb_fn = encrypt ? _gcry_camellia_gfni_avx2_ocb_enc |
| 1521 |
: _gcry_camellia_gfni_avx2_ocb_dec; |
| 1522 |
#endif |
| 1523 |
l = bulk_ocb_prepare_L_pointers_array_blk32 (c, Ls, blkn); |
| 1524 |
|
| 1525 |
/* Process data in 32 block chunks. */ |
| 1526 |
while (nblocks >= 32) |
| 1527 |
{ |
| 1528 |
blkn += 32; |
| 1529 |
*l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 32); |
| 1530 |
|
| 1531 |
bulk_ocb_fn (ctx, outbuf, inbuf, c->u_iv.iv, c->u_ctr.ctr, Ls); |
| 1532 |
|
| 1533 |
nblocks -= 32; |
| 1534 |
outbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1535 |
inbuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1536 |
did_use_aesni_avx2 = 1; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
|
| 1540 |
if (did_use_aesni_avx2) |
| 1541 |
{ |
| 1542 |
if (burn_stack_depth < avx2_burn_stack_depth) |
| 1543 |
burn_stack_depth = avx2_burn_stack_depth; |
| 1544 |
} |
| 1545 |
|
| 1546 |
/* Use generic code to handle smaller chunks... */ |
| 1547 |
} |
| 1548 |
#endif |
| 1549 |
|
| 1550 |
#ifdef USE_AESNI_AVX |
| 1551 |
if (ctx->use_aesni_avx) |
| 1552 |
{ |
| 1553 |
int did_use_aesni_avx = 0; |
| 1554 |
u64 Ls[16]; |
| 1555 |
u64 *l; |
| 1556 |
|
| 1557 |
if (nblocks >= 16) |
| 1558 |
{ |
| 1559 |
l = bulk_ocb_prepare_L_pointers_array_blk16 (c, Ls, blkn); |
| 1560 |
|
| 1561 |
/* Process data in 16 block chunks. */ |
| 1562 |
while (nblocks >= 16) |
| 1563 |
{ |
| 1564 |
blkn += 16; |
| 1565 |
*l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 16); |
| 1566 |
|
| 1567 |
if (encrypt) |
| 1568 |
_gcry_camellia_aesni_avx_ocb_enc(ctx, outbuf, inbuf, c->u_iv.iv, |
| 1569 |
c->u_ctr.ctr, Ls); |
| 1570 |
else |
| 1571 |
_gcry_camellia_aesni_avx_ocb_dec(ctx, outbuf, inbuf, c->u_iv.iv, |
| 1572 |
c->u_ctr.ctr, Ls); |
| 1573 |
|
| 1574 |
nblocks -= 16; |
| 1575 |
outbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1576 |
inbuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1577 |
did_use_aesni_avx = 1; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
if (did_use_aesni_avx) |
| 1582 |
{ |
| 1583 |
if (burn_stack_depth < avx_burn_stack_depth) |
| 1584 |
burn_stack_depth = avx_burn_stack_depth; |
| 1585 |
} |
| 1586 |
|
| 1587 |
/* Use generic code to handle smaller chunks... */ |
| 1588 |
} |
| 1589 |
#endif |
| 1590 |
|
| 1591 |
#if defined(USE_PPC_CRYPTO) || defined(USE_AESNI_AVX) || defined(USE_AESNI_AVX2) |
| 1592 |
/* Process remaining blocks. */ |
| 1593 |
if (nblocks) |
| 1594 |
{ |
| 1595 |
byte tmpbuf[CAMELLIA_BLOCK_SIZE * 32]; |
| 1596 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1597 |
size_t nburn; |
| 1598 |
|
| 1599 |
nburn = bulk_ocb_crypt_128 (c, ctx, encrypt ? camellia_encrypt_blk1_32 |
| 1600 |
: camellia_decrypt_blk1_32, |
| 1601 |
outbuf, inbuf, nblocks, &blkn, encrypt, |
| 1602 |
tmpbuf, sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, |
| 1603 |
&tmp_used); |
| 1604 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1605 |
|
| 1606 |
wipememory(tmpbuf, tmp_used); |
| 1607 |
nblocks = 0; |
| 1608 |
} |
| 1609 |
|
| 1610 |
c->u_mode.ocb.data_nblocks = blkn; |
| 1611 |
|
| 1612 |
if (burn_stack_depth) |
| 1613 |
_gcry_burn_stack (burn_stack_depth + 4 * sizeof(void *)); |
| 1614 |
#endif |
| 1615 |
|
| 1616 |
return nblocks; |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* Bulk authentication of complete blocks in OCB mode. */ |
| 1620 |
static size_t |
| 1621 |
_gcry_camellia_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg, |
| 1622 |
size_t nblocks) |
| 1623 |
{ |
| 1624 |
#if defined(USE_PPC_CRYPTO) || defined(USE_AESNI_AVX) || defined(USE_AESNI_AVX2) |
| 1625 |
CAMELLIA_context *ctx = (void *)&c->context.c; |
| 1626 |
const unsigned char *abuf = abuf_arg; |
| 1627 |
int burn_stack_depth = 0; |
| 1628 |
u64 blkn = c->u_mode.ocb.aad_nblocks; |
| 1629 |
#else |
| 1630 |
(void)c; |
| 1631 |
(void)abuf_arg; |
| 1632 |
#endif |
| 1633 |
|
| 1634 |
#ifdef USE_AESNI_AVX2 |
| 1635 |
if (ctx->use_avx2) |
| 1636 |
{ |
| 1637 |
int did_use_aesni_avx2 = 0; |
| 1638 |
u64 Ls[32]; |
| 1639 |
u64 *l; |
| 1640 |
|
| 1641 |
if (nblocks >= 32) |
| 1642 |
{ |
| 1643 |
typeof (&_gcry_camellia_aesni_avx2_ocb_auth) bulk_auth_fn = |
| 1644 |
_gcry_camellia_aesni_avx2_ocb_auth; |
| 1645 |
|
| 1646 |
#ifdef USE_VAES_AVX2 |
| 1647 |
if (ctx->use_vaes_avx2) |
| 1648 |
bulk_auth_fn = _gcry_camellia_vaes_avx2_ocb_auth; |
| 1649 |
#endif |
| 1650 |
#ifdef USE_GFNI_AVX2 |
| 1651 |
if (ctx->use_gfni_avx2) |
| 1652 |
bulk_auth_fn = _gcry_camellia_gfni_avx2_ocb_auth; |
| 1653 |
#endif |
| 1654 |
|
| 1655 |
l = bulk_ocb_prepare_L_pointers_array_blk32 (c, Ls, blkn); |
| 1656 |
|
| 1657 |
/* Process data in 32 block chunks. */ |
| 1658 |
while (nblocks >= 32) |
| 1659 |
{ |
| 1660 |
blkn += 32; |
| 1661 |
*l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 32); |
| 1662 |
|
| 1663 |
bulk_auth_fn (ctx, abuf, c->u_mode.ocb.aad_offset, |
| 1664 |
c->u_mode.ocb.aad_sum, Ls); |
| 1665 |
|
| 1666 |
nblocks -= 32; |
| 1667 |
abuf += 32 * CAMELLIA_BLOCK_SIZE; |
| 1668 |
did_use_aesni_avx2 = 1; |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
if (did_use_aesni_avx2) |
| 1673 |
{ |
| 1674 |
if (burn_stack_depth < avx2_burn_stack_depth) |
| 1675 |
burn_stack_depth = avx2_burn_stack_depth; |
| 1676 |
} |
| 1677 |
|
| 1678 |
/* Use generic code to handle smaller chunks... */ |
| 1679 |
} |
| 1680 |
#endif |
| 1681 |
|
| 1682 |
#ifdef USE_AESNI_AVX |
| 1683 |
if (ctx->use_aesni_avx) |
| 1684 |
{ |
| 1685 |
int did_use_aesni_avx = 0; |
| 1686 |
u64 Ls[16]; |
| 1687 |
u64 *l; |
| 1688 |
|
| 1689 |
if (nblocks >= 16) |
| 1690 |
{ |
| 1691 |
l = bulk_ocb_prepare_L_pointers_array_blk16 (c, Ls, blkn); |
| 1692 |
|
| 1693 |
/* Process data in 16 block chunks. */ |
| 1694 |
while (nblocks >= 16) |
| 1695 |
{ |
| 1696 |
blkn += 16; |
| 1697 |
*l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 16); |
| 1698 |
|
| 1699 |
_gcry_camellia_aesni_avx_ocb_auth(ctx, abuf, |
| 1700 |
c->u_mode.ocb.aad_offset, |
| 1701 |
c->u_mode.ocb.aad_sum, Ls); |
| 1702 |
|
| 1703 |
nblocks -= 16; |
| 1704 |
abuf += 16 * CAMELLIA_BLOCK_SIZE; |
| 1705 |
did_use_aesni_avx = 1; |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
if (did_use_aesni_avx) |
| 1710 |
{ |
| 1711 |
if (burn_stack_depth < avx_burn_stack_depth) |
| 1712 |
burn_stack_depth = avx_burn_stack_depth; |
| 1713 |
} |
| 1714 |
|
| 1715 |
/* Use generic code to handle smaller chunks... */ |
| 1716 |
} |
| 1717 |
#endif |
| 1718 |
|
| 1719 |
#if defined(USE_PPC_CRYPTO) || defined(USE_AESNI_AVX) || defined(USE_AESNI_AVX2) |
| 1720 |
/* Process remaining blocks. */ |
| 1721 |
if (nblocks) |
| 1722 |
{ |
| 1723 |
byte tmpbuf[CAMELLIA_BLOCK_SIZE * 32]; |
| 1724 |
unsigned int tmp_used = CAMELLIA_BLOCK_SIZE; |
| 1725 |
size_t nburn; |
| 1726 |
|
| 1727 |
nburn = bulk_ocb_auth_128 (c, ctx, camellia_encrypt_blk1_32, |
| 1728 |
abuf, nblocks, &blkn, tmpbuf, |
| 1729 |
sizeof(tmpbuf) / CAMELLIA_BLOCK_SIZE, |
| 1730 |
&tmp_used); |
| 1731 |
burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth; |
| 1732 |
|
| 1733 |
wipememory(tmpbuf, tmp_used); |
| 1734 |
nblocks = 0; |
| 1735 |
} |
| 1736 |
|
| 1737 |
c->u_mode.ocb.aad_nblocks = blkn; |
| 1738 |
|
| 1739 |
if (burn_stack_depth) |
| 1740 |
_gcry_burn_stack (burn_stack_depth + 4 * sizeof(void *)); |
| 1741 |
#endif |
| 1742 |
|
| 1743 |
return nblocks; |
| 1744 |
} |
| 1745 |
|
| 1746 |
|
| 1747 |
static const char * |
| 1748 |
selftest(void) |
| 1749 |
{ |
| 1750 |
CAMELLIA_context ctx; |
| 1751 |
byte scratch[16]; |
| 1752 |
cipher_bulk_ops_t bulk_ops; |
| 1753 |
|
| 1754 |
/* These test vectors are from RFC-3713 */ |
| 1755 |
static const byte plaintext[]= |
| 1756 |
{ |
| 1757 |
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, |
| 1758 |
0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10 |
| 1759 |
}; |
| 1760 |
static const byte key_128[]= |
| 1761 |
{ |
| 1762 |
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, |
| 1763 |
0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10 |
| 1764 |
}; |
| 1765 |
static const byte ciphertext_128[]= |
| 1766 |
{ |
| 1767 |
0x67,0x67,0x31,0x38,0x54,0x96,0x69,0x73, |
| 1768 |
0x08,0x57,0x06,0x56,0x48,0xea,0xbe,0x43 |
| 1769 |
}; |
| 1770 |
static const byte key_192[]= |
| 1771 |
{ |
| 1772 |
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98, |
| 1773 |
0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77 |
| 1774 |
}; |
| 1775 |
static const byte ciphertext_192[]= |
| 1776 |
{ |
| 1777 |
0xb4,0x99,0x34,0x01,0xb3,0xe9,0x96,0xf8, |
| 1778 |
0x4e,0xe5,0xce,0xe7,0xd7,0x9b,0x09,0xb9 |
| 1779 |
}; |
| 1780 |
static const byte key_256[]= |
| 1781 |
{ |
| 1782 |
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba, |
| 1783 |
0x98,0x76,0x54,0x32,0x10,0x00,0x11,0x22,0x33,0x44,0x55, |
| 1784 |
0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff |
| 1785 |
}; |
| 1786 |
static const byte ciphertext_256[]= |
| 1787 |
{ |
| 1788 |
0x9a,0xcc,0x23,0x7d,0xff,0x16,0xd7,0x6c, |
| 1789 |
0x20,0xef,0x7c,0x91,0x9e,0x3a,0x75,0x09 |
| 1790 |
}; |
| 1791 |
|
| 1792 |
camellia_setkey(&ctx,key_128,sizeof(key_128),&bulk_ops); |
| 1793 |
camellia_encrypt(&ctx,scratch,plaintext); |
| 1794 |
if(memcmp(scratch,ciphertext_128,sizeof(ciphertext_128))!=0) |
| 1795 |
return "CAMELLIA-128 test encryption failed."; |
| 1796 |
camellia_decrypt(&ctx,scratch,scratch); |
| 1797 |
if(memcmp(scratch,plaintext,sizeof(plaintext))!=0) |
| 1798 |
return "CAMELLIA-128 test decryption failed."; |
| 1799 |
|
| 1800 |
camellia_setkey(&ctx,key_192,sizeof(key_192),&bulk_ops); |
| 1801 |
camellia_encrypt(&ctx,scratch,plaintext); |
| 1802 |
if(memcmp(scratch,ciphertext_192,sizeof(ciphertext_192))!=0) |
| 1803 |
return "CAMELLIA-192 test encryption failed."; |
| 1804 |
camellia_decrypt(&ctx,scratch,scratch); |
| 1805 |
if(memcmp(scratch,plaintext,sizeof(plaintext))!=0) |
| 1806 |
return "CAMELLIA-192 test decryption failed."; |
| 1807 |
|
| 1808 |
camellia_setkey(&ctx,key_256,sizeof(key_256),&bulk_ops); |
| 1809 |
camellia_encrypt(&ctx,scratch,plaintext); |
| 1810 |
if(memcmp(scratch,ciphertext_256,sizeof(ciphertext_256))!=0) |
| 1811 |
return "CAMELLIA-256 test encryption failed."; |
| 1812 |
camellia_decrypt(&ctx,scratch,scratch); |
| 1813 |
if(memcmp(scratch,plaintext,sizeof(plaintext))!=0) |
| 1814 |
return "CAMELLIA-256 test decryption failed."; |
| 1815 |
|
| 1816 |
return NULL; |
| 1817 |
} |
| 1818 |
|
| 1819 |
/* These oids are from |
| 1820 |
<http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications_oid.html>, |
| 1821 |
retrieved May 1, 2007. */ |
| 1822 |
|
| 1823 |
static const gcry_cipher_oid_spec_t camellia128_oids[] = |
| 1824 |
{ |
| 1825 |
{"1.2.392.200011.61.1.1.1.2", GCRY_CIPHER_MODE_CBC}, |
| 1826 |
{"0.3.4401.5.3.1.9.1", GCRY_CIPHER_MODE_ECB}, |
| 1827 |
{"0.3.4401.5.3.1.9.3", GCRY_CIPHER_MODE_OFB}, |
| 1828 |
{"0.3.4401.5.3.1.9.4", GCRY_CIPHER_MODE_CFB}, |
| 1829 |
{ NULL } |
| 1830 |
}; |
| 1831 |
|
| 1832 |
static const gcry_cipher_oid_spec_t camellia192_oids[] = |
| 1833 |
{ |
| 1834 |
{"1.2.392.200011.61.1.1.1.3", GCRY_CIPHER_MODE_CBC}, |
| 1835 |
{"0.3.4401.5.3.1.9.21", GCRY_CIPHER_MODE_ECB}, |
| 1836 |
{"0.3.4401.5.3.1.9.23", GCRY_CIPHER_MODE_OFB}, |
| 1837 |
{"0.3.4401.5.3.1.9.24", GCRY_CIPHER_MODE_CFB}, |
| 1838 |
{ NULL } |
| 1839 |
}; |
| 1840 |
|
| 1841 |
static const gcry_cipher_oid_spec_t camellia256_oids[] = |
| 1842 |
{ |
| 1843 |
{"1.2.392.200011.61.1.1.1.4", GCRY_CIPHER_MODE_CBC}, |
| 1844 |
{"0.3.4401.5.3.1.9.41", GCRY_CIPHER_MODE_ECB}, |
| 1845 |
{"0.3.4401.5.3.1.9.43", GCRY_CIPHER_MODE_OFB}, |
| 1846 |
{"0.3.4401.5.3.1.9.44", GCRY_CIPHER_MODE_CFB}, |
| 1847 |
{ NULL } |
| 1848 |
}; |
| 1849 |
|
| 1850 |
gcry_cipher_spec_t _gcry_cipher_spec_camellia128 = |
| 1851 |
{ |
| 1852 |
GCRY_CIPHER_CAMELLIA128, {0, 0}, |
| 1853 |
"CAMELLIA128",NULL,camellia128_oids,CAMELLIA_BLOCK_SIZE,128, |
| 1854 |
sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt |
| 1855 |
}; |
| 1856 |
|
| 1857 |
gcry_cipher_spec_t _gcry_cipher_spec_camellia192 = |
| 1858 |
{ |
| 1859 |
GCRY_CIPHER_CAMELLIA192, {0, 0}, |
| 1860 |
"CAMELLIA192",NULL,camellia192_oids,CAMELLIA_BLOCK_SIZE,192, |
| 1861 |
sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt |
| 1862 |
}; |
| 1863 |
|
| 1864 |
gcry_cipher_spec_t _gcry_cipher_spec_camellia256 = |
| 1865 |
{ |
| 1866 |
GCRY_CIPHER_CAMELLIA256, {0, 0}, |
| 1867 |
"CAMELLIA256",NULL,camellia256_oids,CAMELLIA_BLOCK_SIZE,256, |
| 1868 |
sizeof(CAMELLIA_context),camellia_setkey,camellia_encrypt,camellia_decrypt |
| 1869 |
}; |