| 1 |
/* cipher-internal.h - Internal defs for cipher.c |
| 2 |
* Copyright (C) 2011 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 <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef G10_CIPHER_INTERNAL_H |
| 21 |
#define G10_CIPHER_INTERNAL_H |
| 22 |
|
| 23 |
#include "./poly1305-internal.h" |
| 24 |
|
| 25 |
|
| 26 |
/* The maximum supported size of a block in bytes. */ |
| 27 |
#define MAX_BLOCKSIZE 16 |
| 28 |
|
| 29 |
/* The length for an OCB block. Although OCB supports any block |
| 30 |
length it does not make sense to use a 64 bit blocklen (and cipher) |
| 31 |
because this reduces the security margin to an unacceptable state. |
| 32 |
Thus we require a cipher with 128 bit blocklength. */ |
| 33 |
#define OCB_BLOCK_LEN (128/8) |
| 34 |
|
| 35 |
/* The size of the pre-computed L table for OCB. This takes the same |
| 36 |
size as the table used for GCM and thus we don't save anything by |
| 37 |
not using such a table. */ |
| 38 |
#define OCB_L_TABLE_SIZE 16 |
| 39 |
|
| 40 |
|
| 41 |
/* Check the above constants. */ |
| 42 |
#if OCB_BLOCK_LEN > MAX_BLOCKSIZE |
| 43 |
# error OCB_BLOCKLEN > MAX_BLOCKSIZE |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
/* Magic values for the context structure. */ |
| 49 |
#define CTX_MAGIC_NORMAL 0x24091964 |
| 50 |
#define CTX_MAGIC_SECURE 0x46919042 |
| 51 |
|
| 52 |
/* Try to use 16 byte aligned cipher context for better performance. |
| 53 |
We use the aligned attribute, thus it is only possible to implement |
| 54 |
this with gcc. */ |
| 55 |
#undef NEED_16BYTE_ALIGNED_CONTEXT |
| 56 |
#ifdef HAVE_GCC_ATTRIBUTE_ALIGNED |
| 57 |
# define NEED_16BYTE_ALIGNED_CONTEXT 1 |
| 58 |
#endif |
| 59 |
|
| 60 |
/* Undef this symbol to trade GCM speed for 256 bytes of memory per context */ |
| 61 |
#define GCM_USE_TABLES 1 |
| 62 |
|
| 63 |
|
| 64 |
/* GCM_USE_INTEL_PCLMUL indicates whether to compile GCM with Intel PCLMUL |
| 65 |
code. */ |
| 66 |
#undef GCM_USE_INTEL_PCLMUL |
| 67 |
#if defined(ENABLE_PCLMUL_SUPPORT) && defined(GCM_USE_TABLES) |
| 68 |
# if ((defined(__i386__) && SIZEOF_UNSIGNED_LONG == 4) || defined(__x86_64__)) |
| 69 |
# if __GNUC__ >= 4 |
| 70 |
# define GCM_USE_INTEL_PCLMUL 1 |
| 71 |
# endif |
| 72 |
# endif |
| 73 |
#endif /* GCM_USE_INTEL_PCLMUL */ |
| 74 |
|
| 75 |
/* GCM_USE_INTEL_VPCLMUL_AVX2 indicates whether to compile GCM with Intel |
| 76 |
VPCLMUL/AVX2 code. */ |
| 77 |
#undef GCM_USE_INTEL_VPCLMUL_AVX2 |
| 78 |
#if defined(__x86_64__) && defined(GCM_USE_INTEL_PCLMUL) && \ |
| 79 |
defined(ENABLE_AVX2_SUPPORT) && defined(HAVE_GCC_INLINE_ASM_VAES_VPCLMUL) |
| 80 |
# define GCM_USE_INTEL_VPCLMUL_AVX2 1 |
| 81 |
#endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */ |
| 82 |
|
| 83 |
/* GCM_USE_INTEL_VPCLMUL_AVX512 indicates whether to compile GCM with Intel |
| 84 |
VPCLMUL/AVX512 code. */ |
| 85 |
#undef GCM_USE_INTEL_VPCLMUL_AVX512 |
| 86 |
#if defined(__x86_64__) && defined(GCM_USE_INTEL_VPCLMUL_AVX2) && \ |
| 87 |
defined(ENABLE_AVX512_SUPPORT) && defined(HAVE_GCC_INLINE_ASM_AVX512) |
| 88 |
# define GCM_USE_INTEL_VPCLMUL_AVX512 1 |
| 89 |
#endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */ |
| 90 |
|
| 91 |
/* GCM_USE_ARM_PMULL indicates whether to compile GCM with ARMv8 PMULL code. */ |
| 92 |
#undef GCM_USE_ARM_PMULL |
| 93 |
#if defined(ENABLE_ARM_CRYPTO_SUPPORT) && defined(GCM_USE_TABLES) |
| 94 |
# if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \ |
| 95 |
&& defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \ |
| 96 |
&& defined(HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO) |
| 97 |
# define GCM_USE_ARM_PMULL 1 |
| 98 |
# elif defined(__AARCH64EL__) && \ |
| 99 |
defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) && \ |
| 100 |
defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO) |
| 101 |
# define GCM_USE_ARM_PMULL 1 |
| 102 |
# endif |
| 103 |
#endif /* GCM_USE_ARM_PMULL */ |
| 104 |
|
| 105 |
/* GCM_USE_ARM_NEON indicates whether to compile GCM with ARMv7 NEON code. */ |
| 106 |
#undef GCM_USE_ARM_NEON |
| 107 |
#if defined(GCM_USE_TABLES) |
| 108 |
#if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) && \ |
| 109 |
defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) && \ |
| 110 |
defined(HAVE_GCC_INLINE_ASM_NEON) |
| 111 |
# define GCM_USE_ARM_NEON 1 |
| 112 |
#endif |
| 113 |
#endif /* GCM_USE_ARM_NEON */ |
| 114 |
|
| 115 |
/* GCM_USE_AARCH64 indicates whether to compile GCM with AArch64 SIMD code. */ |
| 116 |
#undef GCM_USE_AARCH64 |
| 117 |
#if defined(__AARCH64EL__) && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) |
| 118 |
# define GCM_USE_AARCH64 1 |
| 119 |
#endif |
| 120 |
|
| 121 |
/* GCM_USE_S390X_CRYPTO indicates whether to enable zSeries code. */ |
| 122 |
#undef GCM_USE_S390X_CRYPTO |
| 123 |
#if defined(HAVE_GCC_INLINE_ASM_S390X) |
| 124 |
# define GCM_USE_S390X_CRYPTO 1 |
| 125 |
#endif /* GCM_USE_S390X_CRYPTO */ |
| 126 |
|
| 127 |
/* GCM_USE_PPC_VPMSUM indicates whether to compile GCM with PPC Power 8 |
| 128 |
* polynomial multiplication instruction. */ |
| 129 |
#undef GCM_USE_PPC_VPMSUM |
| 130 |
#if defined(GCM_USE_TABLES) |
| 131 |
#if defined(ENABLE_PPC_CRYPTO_SUPPORT) && defined(__powerpc64__) && \ |
| 132 |
defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \ |
| 133 |
defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && __GNUC__ >= 4 |
| 134 |
# define GCM_USE_PPC_VPMSUM 1 |
| 135 |
# define NEED_16BYTE_ALIGNED_CONTEXT 1 /* this also aligns gcm_table */ |
| 136 |
#endif |
| 137 |
#endif /* GCM_USE_PPC_VPMSUM */ |
| 138 |
|
| 139 |
/* GCM_USE_RISCV_ZBB_ZBC indicates whether to compile GCM with RISC-V Zbb+Zbc |
| 140 |
* code. */ |
| 141 |
#undef GCM_USE_RISCV_ZBB_ZBC |
| 142 |
#if defined (__riscv) && (__riscv_xlen == 64) && \ |
| 143 |
defined(HAVE_GCC_INLINE_ASM_RISCV) |
| 144 |
# define GCM_USE_RISCV_ZBB_ZBC 1 |
| 145 |
#endif |
| 146 |
|
| 147 |
/* GCM_USE_RISCV_V_ZVKG indicates whether to enable RISC-V vector Zvkg |
| 148 |
* code. */ |
| 149 |
#undef GCM_USE_RISCV_ZVKG |
| 150 |
#if defined (__riscv) && \ |
| 151 |
defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS) && \ |
| 152 |
defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_CRYPTO_INTRINSICS) |
| 153 |
# define GCM_USE_RISCV_ZVKG 1 |
| 154 |
#endif |
| 155 |
|
| 156 |
typedef unsigned int (*ghash_fn_t) (gcry_cipher_hd_t c, byte *result, |
| 157 |
const byte *buf, size_t nblocks); |
| 158 |
|
| 159 |
|
| 160 |
/* A structure with function pointers for mode operations. */ |
| 161 |
typedef struct cipher_mode_ops |
| 162 |
{ |
| 163 |
gcry_err_code_t (*encrypt)(gcry_cipher_hd_t c, unsigned char *outbuf, |
| 164 |
size_t outbuflen, const unsigned char *inbuf, |
| 165 |
size_t inbuflen); |
| 166 |
gcry_err_code_t (*decrypt)(gcry_cipher_hd_t c, unsigned char *outbuf, |
| 167 |
size_t outbuflen, const unsigned char *inbuf, |
| 168 |
size_t inbuflen); |
| 169 |
gcry_err_code_t (*setiv)(gcry_cipher_hd_t c, const unsigned char *iv, |
| 170 |
size_t ivlen); |
| 171 |
|
| 172 |
gcry_err_code_t (*authenticate)(gcry_cipher_hd_t c, |
| 173 |
const unsigned char *abuf, size_t abuflen); |
| 174 |
gcry_err_code_t (*get_tag)(gcry_cipher_hd_t c, unsigned char *outtag, |
| 175 |
size_t taglen); |
| 176 |
gcry_err_code_t (*check_tag)(gcry_cipher_hd_t c, const unsigned char *intag, |
| 177 |
size_t taglen); |
| 178 |
} cipher_mode_ops_t; |
| 179 |
|
| 180 |
|
| 181 |
/* A structure with function pointers for bulk operations. The cipher |
| 182 |
algorithm setkey function initializes them when bulk operations are |
| 183 |
available and the actual encryption routines use them if they are |
| 184 |
not NULL. */ |
| 185 |
typedef struct cipher_bulk_ops |
| 186 |
{ |
| 187 |
void (*ecb_crypt)(void *context, void *outbuf_arg, const void *inbuf_arg, |
| 188 |
size_t nblocks, int encrypt); |
| 189 |
void (*cfb_enc)(void *context, unsigned char *iv, void *outbuf_arg, |
| 190 |
const void *inbuf_arg, size_t nblocks); |
| 191 |
void (*cfb_dec)(void *context, unsigned char *iv, void *outbuf_arg, |
| 192 |
const void *inbuf_arg, size_t nblocks); |
| 193 |
void (*cbc_enc)(void *context, unsigned char *iv, void *outbuf_arg, |
| 194 |
const void *inbuf_arg, size_t nblocks, int cbc_mac); |
| 195 |
void (*cbc_dec)(void *context, unsigned char *iv, void *outbuf_arg, |
| 196 |
const void *inbuf_arg, size_t nblocks); |
| 197 |
void (*ofb_enc)(void *context, unsigned char *iv, void *outbuf_arg, |
| 198 |
const void *inbuf_arg, size_t nblocks); |
| 199 |
void (*ctr_enc)(void *context, unsigned char *iv, void *outbuf_arg, |
| 200 |
const void *inbuf_arg, size_t nblocks); |
| 201 |
void (*ctr32le_enc)(void *context, unsigned char *iv, void *outbuf_arg, |
| 202 |
const void *inbuf_arg, size_t nblocks); |
| 203 |
size_t (*ocb_crypt)(gcry_cipher_hd_t c, void *outbuf_arg, |
| 204 |
const void *inbuf_arg, size_t nblocks, int encrypt); |
| 205 |
size_t (*ocb_auth)(gcry_cipher_hd_t c, const void *abuf_arg, size_t nblocks); |
| 206 |
void (*xts_crypt)(void *context, unsigned char *tweak, void *outbuf_arg, |
| 207 |
const void *inbuf_arg, size_t nblocks, int encrypt); |
| 208 |
size_t (*gcm_crypt)(gcry_cipher_hd_t c, void *outbuf_arg, |
| 209 |
const void *inbuf_arg, size_t nblocks, int encrypt); |
| 210 |
} cipher_bulk_ops_t; |
| 211 |
|
| 212 |
|
| 213 |
/* A VIA processor with the Padlock engine as well as the Intel AES_NI |
| 214 |
instructions require an alignment of most data on a 16 byte |
| 215 |
boundary. Because we trick out the compiler while allocating the |
| 216 |
context, the align attribute as used in rijndael.c does not work on |
| 217 |
its own. Thus we need to make sure that the entire context |
| 218 |
structure is a aligned on that boundary. We achieve this by |
| 219 |
defining a new type and use that instead of our usual alignment |
| 220 |
type. */ |
| 221 |
typedef union |
| 222 |
{ |
| 223 |
PROPERLY_ALIGNED_TYPE foo; |
| 224 |
#ifdef NEED_16BYTE_ALIGNED_CONTEXT |
| 225 |
char bar[16] __attribute__ ((aligned (16))); |
| 226 |
#endif |
| 227 |
char c[1]; |
| 228 |
} cipher_context_alignment_t; |
| 229 |
|
| 230 |
|
| 231 |
/* Storage structure for CMAC, for CMAC and EAX modes. */ |
| 232 |
typedef struct { |
| 233 |
/* The initialization vector. Also contains tag after finalization. */ |
| 234 |
union { |
| 235 |
cipher_context_alignment_t iv_align; |
| 236 |
unsigned char iv[MAX_BLOCKSIZE]; |
| 237 |
} u_iv; |
| 238 |
|
| 239 |
/* Subkeys for tag creation, not cleared by gcry_cipher_reset. */ |
| 240 |
unsigned char subkeys[2][MAX_BLOCKSIZE]; |
| 241 |
|
| 242 |
/* Space to save partial input lengths for MAC. */ |
| 243 |
unsigned char macbuf[MAX_BLOCKSIZE]; |
| 244 |
|
| 245 |
int mac_unused; /* Number of unprocessed bytes in MACBUF. */ |
| 246 |
unsigned int tag:1; /* Set to 1 if tag has been finalized. */ |
| 247 |
} gcry_cmac_context_t; |
| 248 |
|
| 249 |
|
| 250 |
/* The handle structure. */ |
| 251 |
struct gcry_cipher_handle |
| 252 |
{ |
| 253 |
int magic; |
| 254 |
size_t actual_handle_size; /* Allocated size of this handle. */ |
| 255 |
size_t handle_offset; /* Offset to the malloced block. */ |
| 256 |
gcry_cipher_spec_t *spec; |
| 257 |
|
| 258 |
/* The algorithm id. This is a hack required because the module |
| 259 |
interface does not easily allow retrieving this value. */ |
| 260 |
int algo; |
| 261 |
|
| 262 |
/* A structure with function pointers for mode operations. */ |
| 263 |
cipher_mode_ops_t mode_ops; |
| 264 |
|
| 265 |
/* A structure with function pointers for bulk operations. Due to |
| 266 |
limitations of the module system (we don't want to change the |
| 267 |
API) we need to keep these function pointers here. */ |
| 268 |
cipher_bulk_ops_t bulk; |
| 269 |
|
| 270 |
int mode; |
| 271 |
unsigned int flags; |
| 272 |
|
| 273 |
struct { |
| 274 |
int geniv_method; |
| 275 |
unsigned char fixed[MAX_BLOCKSIZE]; |
| 276 |
unsigned char dynamic[MAX_BLOCKSIZE]; |
| 277 |
size_t fixed_iv_len; |
| 278 |
size_t dynamic_iv_len; |
| 279 |
} aead; |
| 280 |
|
| 281 |
struct { |
| 282 |
unsigned int key:1; /* Set to 1 if a key has been set. */ |
| 283 |
unsigned int iv:1; /* Set to 1 if a IV has been set. */ |
| 284 |
unsigned int tag:1; /* Set to 1 if a tag is finalized. */ |
| 285 |
unsigned int finalize:1; /* Next encrypt/decrypt has the final data. */ |
| 286 |
unsigned int allow_weak_key:1; /* Set to 1 if weak keys are allowed. */ |
| 287 |
} marks; |
| 288 |
|
| 289 |
/* The initialization vector. For best performance we make sure |
| 290 |
that it is properly aligned. In particular some implementations |
| 291 |
of bulk operations expect an 16 byte aligned IV. IV is also used |
| 292 |
to store CBC-MAC in CCM mode; counter IV is stored in U_CTR. For |
| 293 |
OCB mode it is used for the offset value. */ |
| 294 |
union { |
| 295 |
cipher_context_alignment_t iv_align; |
| 296 |
unsigned char iv[MAX_BLOCKSIZE]; |
| 297 |
} u_iv; |
| 298 |
|
| 299 |
/* The counter for CTR mode. This field is also used by AESWRAP and |
| 300 |
thus we can't use the U_IV union. For OCB mode it is used for |
| 301 |
the checksum. */ |
| 302 |
union { |
| 303 |
cipher_context_alignment_t iv_align; |
| 304 |
unsigned char ctr[MAX_BLOCKSIZE]; |
| 305 |
} u_ctr; |
| 306 |
|
| 307 |
/* Space to save an IV or CTR for chaining operations. */ |
| 308 |
unsigned char lastiv[MAX_BLOCKSIZE]; |
| 309 |
int unused; /* Number of unused bytes in LASTIV. */ |
| 310 |
|
| 311 |
union { |
| 312 |
/* Mode specific storage for CCM mode. */ |
| 313 |
struct { |
| 314 |
u64 encryptlen; |
| 315 |
u64 aadlen; |
| 316 |
unsigned int authlen; |
| 317 |
|
| 318 |
/* Space to save partial input lengths for MAC. */ |
| 319 |
unsigned char macbuf[GCRY_CCM_BLOCK_LEN]; |
| 320 |
int mac_unused; /* Number of unprocessed bytes in MACBUF. */ |
| 321 |
|
| 322 |
unsigned char s0[GCRY_CCM_BLOCK_LEN]; |
| 323 |
|
| 324 |
unsigned int nonce:1; /* Set to 1 if nonce has been set. */ |
| 325 |
unsigned int lengths:1; /* Set to 1 if CCM length parameters has been |
| 326 |
processed. */ |
| 327 |
} ccm; |
| 328 |
|
| 329 |
/* Mode specific storage for Poly1305 mode. */ |
| 330 |
struct { |
| 331 |
/* byte counter for AAD. */ |
| 332 |
u32 aadcount[2]; |
| 333 |
|
| 334 |
/* byte counter for data. */ |
| 335 |
u32 datacount[2]; |
| 336 |
|
| 337 |
unsigned int aad_finalized:1; |
| 338 |
unsigned int bytecount_over_limits:1; |
| 339 |
|
| 340 |
poly1305_context_t ctx; |
| 341 |
} poly1305; |
| 342 |
|
| 343 |
/* Mode specific storage for CMAC mode. */ |
| 344 |
gcry_cmac_context_t cmac; |
| 345 |
|
| 346 |
/* Mode specific storage for EAX mode. */ |
| 347 |
struct { |
| 348 |
/* CMAC for header (AAD). */ |
| 349 |
gcry_cmac_context_t cmac_header; |
| 350 |
|
| 351 |
/* CMAC for ciphertext. */ |
| 352 |
gcry_cmac_context_t cmac_ciphertext; |
| 353 |
} eax; |
| 354 |
|
| 355 |
/* Mode specific storage for GCM mode and GCM-SIV mode. */ |
| 356 |
struct { |
| 357 |
/* The interim tag for GCM mode. */ |
| 358 |
union { |
| 359 |
cipher_context_alignment_t iv_align; |
| 360 |
unsigned char tag[MAX_BLOCKSIZE]; |
| 361 |
} u_tag; |
| 362 |
|
| 363 |
/* Space to save partial input lengths for MAC. */ |
| 364 |
unsigned char macbuf[GCRY_GCM_BLOCK_LEN]; |
| 365 |
int mac_unused; /* Number of unprocessed bytes in MACBUF. */ |
| 366 |
|
| 367 |
/* byte counters for GCM */ |
| 368 |
u32 aadlen[2]; |
| 369 |
u32 datalen[2]; |
| 370 |
|
| 371 |
/* encrypted tag counter */ |
| 372 |
unsigned char tagiv[MAX_BLOCKSIZE]; |
| 373 |
|
| 374 |
unsigned int ghash_data_finalized:1; |
| 375 |
unsigned int ghash_aad_finalized:1; |
| 376 |
|
| 377 |
unsigned int datalen_over_limits:1; |
| 378 |
unsigned int disallow_encryption_because_of_setiv_in_fips_mode:1; |
| 379 |
|
| 380 |
/* --- Following members are not cleared in gcry_cipher_reset --- */ |
| 381 |
|
| 382 |
/* GHASH multiplier from key. */ |
| 383 |
union { |
| 384 |
cipher_context_alignment_t iv_align; |
| 385 |
unsigned char key[MAX_BLOCKSIZE]; |
| 386 |
} u_ghash_key; |
| 387 |
|
| 388 |
/* Pre-calculated table for GCM. */ |
| 389 |
#ifdef GCM_USE_TABLES |
| 390 |
#if (SIZEOF_UNSIGNED_LONG == 8 || defined(__x86_64__)) |
| 391 |
#define GCM_TABLES_USE_U64 1 |
| 392 |
u64 gcm_table[4 * 16]; |
| 393 |
#else |
| 394 |
#undef GCM_TABLES_USE_U64 |
| 395 |
u32 gcm_table[8 * 16]; |
| 396 |
#endif |
| 397 |
#endif |
| 398 |
|
| 399 |
/* GHASH implementation in use. */ |
| 400 |
ghash_fn_t ghash_fn; |
| 401 |
|
| 402 |
/* POLYVAL implementation in use (GCM-SIV). */ |
| 403 |
ghash_fn_t polyval_fn; |
| 404 |
|
| 405 |
/* Key length used for GCM-SIV key generating key. */ |
| 406 |
unsigned int siv_keylen; |
| 407 |
|
| 408 |
/* Flags for accelerated implementations. */ |
| 409 |
unsigned int hw_impl_flags; |
| 410 |
} gcm; |
| 411 |
|
| 412 |
/* Mode specific storage for OCB mode. */ |
| 413 |
struct { |
| 414 |
/* --- Following members are not cleared in gcry_cipher_reset --- */ |
| 415 |
|
| 416 |
/* Helper variables and pre-computed table of L values. */ |
| 417 |
unsigned char L_star[OCB_BLOCK_LEN]; |
| 418 |
unsigned char L_dollar[OCB_BLOCK_LEN]; |
| 419 |
unsigned char L0L1[OCB_BLOCK_LEN]; |
| 420 |
unsigned char L[OCB_L_TABLE_SIZE][OCB_BLOCK_LEN]; |
| 421 |
|
| 422 |
/* --- Following members are cleared in gcry_cipher_reset --- */ |
| 423 |
|
| 424 |
/* The tag is valid if marks.tag has been set. */ |
| 425 |
unsigned char tag[OCB_BLOCK_LEN]; |
| 426 |
|
| 427 |
/* A buffer to hold the offset for the AAD processing. */ |
| 428 |
unsigned char aad_offset[OCB_BLOCK_LEN]; |
| 429 |
|
| 430 |
/* A buffer to hold the current sum of AAD processing. We can't |
| 431 |
use tag here because tag may already hold the preprocessed |
| 432 |
checksum of the data. */ |
| 433 |
unsigned char aad_sum[OCB_BLOCK_LEN]; |
| 434 |
|
| 435 |
/* A buffer to store AAD data not yet processed. */ |
| 436 |
unsigned char aad_leftover[OCB_BLOCK_LEN]; |
| 437 |
|
| 438 |
/* Number of data/aad blocks processed so far. */ |
| 439 |
u64 data_nblocks; |
| 440 |
u64 aad_nblocks; |
| 441 |
|
| 442 |
/* Number of valid bytes in AAD_LEFTOVER. */ |
| 443 |
unsigned char aad_nleftover; |
| 444 |
|
| 445 |
/* Length of the tag. Fixed for now but may eventually be |
| 446 |
specified using a set of gcry_cipher_flags. */ |
| 447 |
unsigned char taglen; |
| 448 |
|
| 449 |
/* Flags indicating that the final data/aad block has been |
| 450 |
processed. */ |
| 451 |
unsigned int data_finalized:1; |
| 452 |
unsigned int aad_finalized:1; |
| 453 |
} ocb; |
| 454 |
|
| 455 |
/* Mode specific storage for XTS mode. */ |
| 456 |
struct { |
| 457 |
/* Pointer to tweak cipher context, allocated after actual |
| 458 |
* cipher context. */ |
| 459 |
char *tweak_context; |
| 460 |
} xts; |
| 461 |
|
| 462 |
/* Mode specific storage for SIV mode. */ |
| 463 |
struct { |
| 464 |
/* Tag used for decryption. */ |
| 465 |
unsigned char dec_tag[GCRY_SIV_BLOCK_LEN]; |
| 466 |
|
| 467 |
/* S2V state. */ |
| 468 |
unsigned char s2v_d[GCRY_SIV_BLOCK_LEN]; |
| 469 |
|
| 470 |
/* Number of AAD elements processed. */ |
| 471 |
unsigned int aad_count:8; |
| 472 |
|
| 473 |
/* Flags for SIV state. */ |
| 474 |
unsigned int dec_tag_set:1; |
| 475 |
|
| 476 |
/* --- Following members are not cleared in gcry_cipher_reset --- */ |
| 477 |
|
| 478 |
/* S2V CMAC state. */ |
| 479 |
gcry_cmac_context_t s2v_cmac; |
| 480 |
unsigned char s2v_zero_block[GCRY_SIV_BLOCK_LEN]; |
| 481 |
|
| 482 |
/* Pointer to CTR cipher context, allocated after actual |
| 483 |
* cipher context. */ |
| 484 |
char *ctr_context; |
| 485 |
} siv; |
| 486 |
|
| 487 |
/* Mode specific storage for WRAP mode. */ |
| 488 |
struct { |
| 489 |
unsigned char plen[4]; |
| 490 |
} wrap; |
| 491 |
} u_mode; |
| 492 |
|
| 493 |
/* What follows are two contexts of the cipher in use. The first |
| 494 |
one needs to be aligned well enough for the cipher operation |
| 495 |
whereas the second one is a copy created by cipher_setkey and |
| 496 |
used by cipher_reset. That second copy has no need for proper |
| 497 |
aligment because it is only accessed by memcpy. */ |
| 498 |
cipher_context_alignment_t context; |
| 499 |
}; |
| 500 |
|
| 501 |
|
| 502 |
/*-- cipher-cbc.c --*/ |
| 503 |
gcry_err_code_t _gcry_cipher_cbc_encrypt |
| 504 |
/* */ (gcry_cipher_hd_t c, |
| 505 |
unsigned char *outbuf, size_t outbuflen, |
| 506 |
const unsigned char *inbuf, size_t inbuflen); |
| 507 |
gcry_err_code_t _gcry_cipher_cbc_decrypt |
| 508 |
/* */ (gcry_cipher_hd_t c, |
| 509 |
unsigned char *outbuf, size_t outbuflen, |
| 510 |
const unsigned char *inbuf, size_t inbuflen); |
| 511 |
gcry_err_code_t _gcry_cipher_cbc_cts_encrypt |
| 512 |
/* */ (gcry_cipher_hd_t c, |
| 513 |
unsigned char *outbuf, size_t outbuflen, |
| 514 |
const unsigned char *inbuf, size_t inbuflen); |
| 515 |
gcry_err_code_t _gcry_cipher_cbc_cts_decrypt |
| 516 |
/* */ (gcry_cipher_hd_t c, |
| 517 |
unsigned char *outbuf, size_t outbuflen, |
| 518 |
const unsigned char *inbuf, size_t inbuflen); |
| 519 |
|
| 520 |
/*-- cipher-cfb.c --*/ |
| 521 |
gcry_err_code_t _gcry_cipher_cfb_encrypt |
| 522 |
/* */ (gcry_cipher_hd_t c, |
| 523 |
unsigned char *outbuf, size_t outbuflen, |
| 524 |
const unsigned char *inbuf, size_t inbuflen); |
| 525 |
gcry_err_code_t _gcry_cipher_cfb_decrypt |
| 526 |
/* */ (gcry_cipher_hd_t c, |
| 527 |
unsigned char *outbuf, size_t outbuflen, |
| 528 |
const unsigned char *inbuf, size_t inbuflen); |
| 529 |
gcry_err_code_t _gcry_cipher_cfb8_encrypt |
| 530 |
/* */ (gcry_cipher_hd_t c, |
| 531 |
unsigned char *outbuf, size_t outbuflen, |
| 532 |
const unsigned char *inbuf, size_t inbuflen); |
| 533 |
gcry_err_code_t _gcry_cipher_cfb8_decrypt |
| 534 |
/* */ (gcry_cipher_hd_t c, |
| 535 |
unsigned char *outbuf, size_t outbuflen, |
| 536 |
const unsigned char *inbuf, size_t inbuflen); |
| 537 |
|
| 538 |
|
| 539 |
/*-- cipher-ofb.c --*/ |
| 540 |
gcry_err_code_t _gcry_cipher_ofb_encrypt |
| 541 |
/* */ (gcry_cipher_hd_t c, |
| 542 |
unsigned char *outbuf, size_t outbuflen, |
| 543 |
const unsigned char *inbuf, size_t inbuflen); |
| 544 |
|
| 545 |
/*-- cipher-ctr.c --*/ |
| 546 |
gcry_err_code_t _gcry_cipher_ctr_encrypt_ctx |
| 547 |
/* */ (gcry_cipher_hd_t c, |
| 548 |
unsigned char *outbuf, size_t outbuflen, |
| 549 |
const unsigned char *inbuf, size_t inbuflen, |
| 550 |
void *algo_context); |
| 551 |
gcry_err_code_t _gcry_cipher_ctr_encrypt |
| 552 |
/* */ (gcry_cipher_hd_t c, |
| 553 |
unsigned char *outbuf, size_t outbuflen, |
| 554 |
const unsigned char *inbuf, size_t inbuflen); |
| 555 |
|
| 556 |
|
| 557 |
/*-- cipher-aeswrap.c --*/ |
| 558 |
gcry_err_code_t _gcry_cipher_keywrap_encrypt |
| 559 |
/* */ (gcry_cipher_hd_t c, |
| 560 |
byte *outbuf, size_t outbuflen, |
| 561 |
const byte *inbuf, size_t inbuflen); |
| 562 |
gcry_err_code_t _gcry_cipher_keywrap_encrypt_padding |
| 563 |
/* */ (gcry_cipher_hd_t c, |
| 564 |
byte *outbuf, size_t outbuflen, |
| 565 |
const byte *inbuf, size_t inbuflen); |
| 566 |
gcry_err_code_t _gcry_cipher_keywrap_decrypt_auto |
| 567 |
/* */ (gcry_cipher_hd_t c, |
| 568 |
byte *outbuf, size_t outbuflen, |
| 569 |
const byte *inbuf, size_t inbuflen); |
| 570 |
|
| 571 |
|
| 572 |
/*-- cipher-ccm.c --*/ |
| 573 |
gcry_err_code_t _gcry_cipher_ccm_encrypt |
| 574 |
/* */ (gcry_cipher_hd_t c, |
| 575 |
unsigned char *outbuf, size_t outbuflen, |
| 576 |
const unsigned char *inbuf, size_t inbuflen); |
| 577 |
gcry_err_code_t _gcry_cipher_ccm_decrypt |
| 578 |
/* */ (gcry_cipher_hd_t c, |
| 579 |
unsigned char *outbuf, size_t outbuflen, |
| 580 |
const unsigned char *inbuf, size_t inbuflen); |
| 581 |
gcry_err_code_t _gcry_cipher_ccm_set_nonce |
| 582 |
/* */ (gcry_cipher_hd_t c, const unsigned char *nonce, |
| 583 |
size_t noncelen); |
| 584 |
gcry_err_code_t _gcry_cipher_ccm_authenticate |
| 585 |
/* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen); |
| 586 |
gcry_err_code_t _gcry_cipher_ccm_set_lengths |
| 587 |
/* */ (gcry_cipher_hd_t c, u64 encryptedlen, u64 aadlen, u64 taglen); |
| 588 |
gcry_err_code_t _gcry_cipher_ccm_get_tag |
| 589 |
/* */ (gcry_cipher_hd_t c, |
| 590 |
unsigned char *outtag, size_t taglen); |
| 591 |
gcry_err_code_t _gcry_cipher_ccm_check_tag |
| 592 |
/* */ (gcry_cipher_hd_t c, |
| 593 |
const unsigned char *intag, size_t taglen); |
| 594 |
|
| 595 |
|
| 596 |
/*-- cipher-cmac.c --*/ |
| 597 |
gcry_err_code_t _gcry_cmac_generate_subkeys |
| 598 |
/* */ (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx); |
| 599 |
gcry_err_code_t _gcry_cmac_write |
| 600 |
/* */ (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx, |
| 601 |
const byte * inbuf, size_t inlen); |
| 602 |
gcry_err_code_t _gcry_cmac_final |
| 603 |
/* */ (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx); |
| 604 |
void _gcry_cmac_reset (gcry_cmac_context_t *ctx); |
| 605 |
|
| 606 |
|
| 607 |
/*-- cipher-eax.c --*/ |
| 608 |
gcry_err_code_t _gcry_cipher_eax_encrypt |
| 609 |
/* */ (gcry_cipher_hd_t c, |
| 610 |
unsigned char *outbuf, size_t outbuflen, |
| 611 |
const unsigned char *inbuf, size_t inbuflen); |
| 612 |
gcry_err_code_t _gcry_cipher_eax_decrypt |
| 613 |
/* */ (gcry_cipher_hd_t c, |
| 614 |
unsigned char *outbuf, size_t outbuflen, |
| 615 |
const unsigned char *inbuf, size_t inbuflen); |
| 616 |
gcry_err_code_t _gcry_cipher_eax_set_nonce |
| 617 |
/* */ (gcry_cipher_hd_t c, |
| 618 |
const unsigned char *nonce, size_t noncelen); |
| 619 |
gcry_err_code_t _gcry_cipher_eax_authenticate |
| 620 |
/* */ (gcry_cipher_hd_t c, |
| 621 |
const unsigned char *aadbuf, size_t aadbuflen); |
| 622 |
gcry_err_code_t _gcry_cipher_eax_get_tag |
| 623 |
/* */ (gcry_cipher_hd_t c, |
| 624 |
unsigned char *outtag, size_t taglen); |
| 625 |
gcry_err_code_t _gcry_cipher_eax_check_tag |
| 626 |
/* */ (gcry_cipher_hd_t c, |
| 627 |
const unsigned char *intag, size_t taglen); |
| 628 |
gcry_err_code_t _gcry_cipher_eax_setkey |
| 629 |
/* */ (gcry_cipher_hd_t c); |
| 630 |
|
| 631 |
|
| 632 |
/*-- cipher-gcm.c --*/ |
| 633 |
gcry_err_code_t _gcry_cipher_gcm_encrypt |
| 634 |
/* */ (gcry_cipher_hd_t c, |
| 635 |
unsigned char *outbuf, size_t outbuflen, |
| 636 |
const unsigned char *inbuf, size_t inbuflen); |
| 637 |
gcry_err_code_t _gcry_cipher_gcm_decrypt |
| 638 |
/* */ (gcry_cipher_hd_t c, |
| 639 |
unsigned char *outbuf, size_t outbuflen, |
| 640 |
const unsigned char *inbuf, size_t inbuflen); |
| 641 |
gcry_err_code_t _gcry_cipher_gcm_setiv |
| 642 |
/* */ (gcry_cipher_hd_t c, |
| 643 |
const unsigned char *iv, size_t ivlen); |
| 644 |
gcry_err_code_t _gcry_cipher_gcm_authenticate |
| 645 |
/* */ (gcry_cipher_hd_t c, |
| 646 |
const unsigned char *aadbuf, size_t aadbuflen); |
| 647 |
gcry_err_code_t _gcry_cipher_gcm_get_tag |
| 648 |
/* */ (gcry_cipher_hd_t c, |
| 649 |
unsigned char *outtag, size_t taglen); |
| 650 |
gcry_err_code_t _gcry_cipher_gcm_check_tag |
| 651 |
/* */ (gcry_cipher_hd_t c, |
| 652 |
const unsigned char *intag, size_t taglen); |
| 653 |
void _gcry_cipher_gcm_setkey |
| 654 |
/* */ (gcry_cipher_hd_t c); |
| 655 |
void _gcry_cipher_gcm_setupM |
| 656 |
/* */ (gcry_cipher_hd_t c); |
| 657 |
|
| 658 |
|
| 659 |
/*-- cipher-poly1305.c --*/ |
| 660 |
gcry_err_code_t _gcry_cipher_poly1305_encrypt |
| 661 |
/* */ (gcry_cipher_hd_t c, |
| 662 |
unsigned char *outbuf, size_t outbuflen, |
| 663 |
const unsigned char *inbuf, size_t inbuflen); |
| 664 |
gcry_err_code_t _gcry_cipher_poly1305_decrypt |
| 665 |
/* */ (gcry_cipher_hd_t c, |
| 666 |
unsigned char *outbuf, size_t outbuflen, |
| 667 |
const unsigned char *inbuf, size_t inbuflen); |
| 668 |
gcry_err_code_t _gcry_cipher_poly1305_setiv |
| 669 |
/* */ (gcry_cipher_hd_t c, |
| 670 |
const unsigned char *iv, size_t ivlen); |
| 671 |
gcry_err_code_t _gcry_cipher_poly1305_authenticate |
| 672 |
/* */ (gcry_cipher_hd_t c, |
| 673 |
const unsigned char *aadbuf, size_t aadbuflen); |
| 674 |
gcry_err_code_t _gcry_cipher_poly1305_get_tag |
| 675 |
/* */ (gcry_cipher_hd_t c, |
| 676 |
unsigned char *outtag, size_t taglen); |
| 677 |
gcry_err_code_t _gcry_cipher_poly1305_check_tag |
| 678 |
/* */ (gcry_cipher_hd_t c, |
| 679 |
const unsigned char *intag, size_t taglen); |
| 680 |
void _gcry_cipher_poly1305_setkey |
| 681 |
/* */ (gcry_cipher_hd_t c); |
| 682 |
|
| 683 |
|
| 684 |
/*-- chacha20.c --*/ |
| 685 |
gcry_err_code_t _gcry_chacha20_poly1305_encrypt |
| 686 |
/* */ (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf, |
| 687 |
size_t length); |
| 688 |
gcry_err_code_t _gcry_chacha20_poly1305_decrypt |
| 689 |
/* */ (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf, |
| 690 |
size_t length); |
| 691 |
|
| 692 |
|
| 693 |
/*-- cipher-ocb.c --*/ |
| 694 |
gcry_err_code_t _gcry_cipher_ocb_encrypt |
| 695 |
/* */ (gcry_cipher_hd_t c, |
| 696 |
unsigned char *outbuf, size_t outbuflen, |
| 697 |
const unsigned char *inbuf, size_t inbuflen); |
| 698 |
gcry_err_code_t _gcry_cipher_ocb_decrypt |
| 699 |
/* */ (gcry_cipher_hd_t c, |
| 700 |
unsigned char *outbuf, size_t outbuflen, |
| 701 |
const unsigned char *inbuf, size_t inbuflen); |
| 702 |
gcry_err_code_t _gcry_cipher_ocb_set_nonce |
| 703 |
/* */ (gcry_cipher_hd_t c, const unsigned char *nonce, |
| 704 |
size_t noncelen); |
| 705 |
gcry_err_code_t _gcry_cipher_ocb_authenticate |
| 706 |
/* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen); |
| 707 |
gcry_err_code_t _gcry_cipher_ocb_get_tag |
| 708 |
/* */ (gcry_cipher_hd_t c, |
| 709 |
unsigned char *outtag, size_t taglen); |
| 710 |
gcry_err_code_t _gcry_cipher_ocb_check_tag |
| 711 |
/* */ (gcry_cipher_hd_t c, |
| 712 |
const unsigned char *intag, size_t taglen); |
| 713 |
void _gcry_cipher_ocb_setkey |
| 714 |
/* */ (gcry_cipher_hd_t c); |
| 715 |
|
| 716 |
|
| 717 |
/*-- cipher-xts.c --*/ |
| 718 |
gcry_err_code_t _gcry_cipher_xts_encrypt |
| 719 |
/* */ (gcry_cipher_hd_t c, unsigned char *outbuf, size_t outbuflen, |
| 720 |
const unsigned char *inbuf, size_t inbuflen); |
| 721 |
gcry_err_code_t _gcry_cipher_xts_decrypt |
| 722 |
/* */ (gcry_cipher_hd_t c, unsigned char *outbuf, size_t outbuflen, |
| 723 |
const unsigned char *inbuf, size_t inbuflen); |
| 724 |
|
| 725 |
|
| 726 |
/*-- cipher-siv.c --*/ |
| 727 |
gcry_err_code_t _gcry_cipher_siv_encrypt |
| 728 |
/* */ (gcry_cipher_hd_t c, |
| 729 |
unsigned char *outbuf, size_t outbuflen, |
| 730 |
const unsigned char *inbuf, size_t inbuflen); |
| 731 |
gcry_err_code_t _gcry_cipher_siv_decrypt |
| 732 |
/* */ (gcry_cipher_hd_t c, |
| 733 |
unsigned char *outbuf, size_t outbuflen, |
| 734 |
const unsigned char *inbuf, size_t inbuflen); |
| 735 |
gcry_err_code_t _gcry_cipher_siv_set_nonce |
| 736 |
/* */ (gcry_cipher_hd_t c, const unsigned char *nonce, |
| 737 |
size_t noncelen); |
| 738 |
gcry_err_code_t _gcry_cipher_siv_authenticate |
| 739 |
/* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen); |
| 740 |
gcry_err_code_t _gcry_cipher_siv_set_decryption_tag |
| 741 |
/* */ (gcry_cipher_hd_t c, const byte *tag, size_t taglen); |
| 742 |
gcry_err_code_t _gcry_cipher_siv_get_tag |
| 743 |
/* */ (gcry_cipher_hd_t c, |
| 744 |
unsigned char *outtag, size_t taglen); |
| 745 |
gcry_err_code_t _gcry_cipher_siv_check_tag |
| 746 |
/* */ (gcry_cipher_hd_t c, |
| 747 |
const unsigned char *intag, size_t taglen); |
| 748 |
gcry_err_code_t _gcry_cipher_siv_setkey |
| 749 |
/* */ (gcry_cipher_hd_t c, |
| 750 |
const unsigned char *ctrkey, size_t ctrkeylen); |
| 751 |
|
| 752 |
|
| 753 |
/*-- cipher-gcm-siv.c --*/ |
| 754 |
gcry_err_code_t _gcry_cipher_gcm_siv_encrypt |
| 755 |
/* */ (gcry_cipher_hd_t c, |
| 756 |
unsigned char *outbuf, size_t outbuflen, |
| 757 |
const unsigned char *inbuf, size_t inbuflen); |
| 758 |
gcry_err_code_t _gcry_cipher_gcm_siv_decrypt |
| 759 |
/* */ (gcry_cipher_hd_t c, |
| 760 |
unsigned char *outbuf, size_t outbuflen, |
| 761 |
const unsigned char *inbuf, size_t inbuflen); |
| 762 |
gcry_err_code_t _gcry_cipher_gcm_siv_set_nonce |
| 763 |
/* */ (gcry_cipher_hd_t c, const unsigned char *nonce, |
| 764 |
size_t noncelen); |
| 765 |
gcry_err_code_t _gcry_cipher_gcm_siv_authenticate |
| 766 |
/* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen); |
| 767 |
gcry_err_code_t _gcry_cipher_gcm_siv_set_decryption_tag |
| 768 |
/* */ (gcry_cipher_hd_t c, const byte *tag, size_t taglen); |
| 769 |
gcry_err_code_t _gcry_cipher_gcm_siv_get_tag |
| 770 |
/* */ (gcry_cipher_hd_t c, |
| 771 |
unsigned char *outtag, size_t taglen); |
| 772 |
gcry_err_code_t _gcry_cipher_gcm_siv_check_tag |
| 773 |
/* */ (gcry_cipher_hd_t c, |
| 774 |
const unsigned char *intag, size_t taglen); |
| 775 |
gcry_err_code_t _gcry_cipher_gcm_siv_setkey |
| 776 |
/* */ (gcry_cipher_hd_t c, unsigned int keylen); |
| 777 |
|
| 778 |
|
| 779 |
/* Return the L-value for block N. Note: 'cipher_ocb.c' ensures that N |
| 780 |
* will never be multiple of 65536 (1 << OCB_L_TABLE_SIZE), thus N can |
| 781 |
* be directly passed to _gcry_ctz() function and resulting index will |
| 782 |
* never overflow the table. */ |
| 783 |
static inline const unsigned char * |
| 784 |
ocb_get_l (gcry_cipher_hd_t c, u64 n) |
| 785 |
{ |
| 786 |
unsigned long ntz; |
| 787 |
|
| 788 |
#if ((defined(__i386__) || defined(__x86_64__)) && __GNUC__ >= 4) |
| 789 |
/* Assumes that N != 0. */ |
| 790 |
asm ("rep;bsfl %k[low], %k[ntz]\n\t" |
| 791 |
: [ntz] "=r" (ntz) |
| 792 |
: [low] "r" ((unsigned long)n) |
| 793 |
: "cc"); |
| 794 |
#else |
| 795 |
ntz = _gcry_ctz_no_zero (n); |
| 796 |
#endif |
| 797 |
|
| 798 |
return c->u_mode.ocb.L[ntz]; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
/* Return bit-shift of blocksize. */ |
| 803 |
static inline unsigned int _gcry_blocksize_shift(gcry_cipher_hd_t c) |
| 804 |
{ |
| 805 |
/* Only blocksizes 8 and 16 are used. Return value in such way |
| 806 |
* that compiler can optimize calling functions based on this. */ |
| 807 |
return c->spec->blocksize == 8 ? 3 : 4; |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
/* Optimized function for adding value to cipher block. */ |
| 812 |
static inline void |
| 813 |
cipher_block_add(void *_dstsrc, unsigned int add, size_t blocksize) |
| 814 |
{ |
| 815 |
byte *dstsrc = _dstsrc; |
| 816 |
u64 s[2]; |
| 817 |
|
| 818 |
if (blocksize == 8) |
| 819 |
{ |
| 820 |
buf_put_be64(dstsrc + 0, buf_get_be64(dstsrc + 0) + add); |
| 821 |
} |
| 822 |
else /* blocksize == 16 */ |
| 823 |
{ |
| 824 |
s[0] = buf_get_be64(dstsrc + 8); |
| 825 |
s[1] = buf_get_be64(dstsrc + 0); |
| 826 |
s[0] += add; |
| 827 |
s[1] += (s[0] < add); |
| 828 |
buf_put_be64(dstsrc + 8, s[0]); |
| 829 |
buf_put_be64(dstsrc + 0, s[1]); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
/* Optimized function for cipher block copying */ |
| 835 |
static inline void |
| 836 |
cipher_block_cpy(void *_dst, const void *_src, size_t blocksize) |
| 837 |
{ |
| 838 |
byte *dst = _dst; |
| 839 |
const byte *src = _src; |
| 840 |
u64 s[2]; |
| 841 |
|
| 842 |
if (blocksize == 8) |
| 843 |
{ |
| 844 |
buf_put_he64(dst + 0, buf_get_he64(src + 0)); |
| 845 |
} |
| 846 |
else /* blocksize == 16 */ |
| 847 |
{ |
| 848 |
s[0] = buf_get_he64(src + 0); |
| 849 |
s[1] = buf_get_he64(src + 8); |
| 850 |
buf_put_he64(dst + 0, s[0]); |
| 851 |
buf_put_he64(dst + 8, s[1]); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
/* Optimized function for cipher block xoring */ |
| 857 |
static inline void |
| 858 |
cipher_block_xor(void *_dst, const void *_src1, const void *_src2, |
| 859 |
size_t blocksize) |
| 860 |
{ |
| 861 |
byte *dst = _dst; |
| 862 |
const byte *src1 = _src1; |
| 863 |
const byte *src2 = _src2; |
| 864 |
u64 s1[2]; |
| 865 |
u64 s2[2]; |
| 866 |
|
| 867 |
if (blocksize == 8) |
| 868 |
{ |
| 869 |
buf_put_he64(dst + 0, buf_get_he64(src1 + 0) ^ buf_get_he64(src2 + 0)); |
| 870 |
} |
| 871 |
else /* blocksize == 16 */ |
| 872 |
{ |
| 873 |
s1[0] = buf_get_he64(src1 + 0); |
| 874 |
s1[1] = buf_get_he64(src1 + 8); |
| 875 |
s2[0] = buf_get_he64(src2 + 0); |
| 876 |
s2[1] = buf_get_he64(src2 + 8); |
| 877 |
buf_put_he64(dst + 0, s1[0] ^ s2[0]); |
| 878 |
buf_put_he64(dst + 8, s1[1] ^ s2[1]); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
/* Optimized function for in-place cipher block xoring */ |
| 884 |
static inline void |
| 885 |
cipher_block_xor_1(void *_dst, const void *_src, size_t blocksize) |
| 886 |
{ |
| 887 |
cipher_block_xor (_dst, _dst, _src, blocksize); |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
/* Optimized function for cipher block xoring with two destination cipher |
| 892 |
blocks. Used mainly by CFB mode encryption. */ |
| 893 |
static inline void |
| 894 |
cipher_block_xor_2dst(void *_dst1, void *_dst2, const void *_src, |
| 895 |
size_t blocksize) |
| 896 |
{ |
| 897 |
byte *dst1 = _dst1; |
| 898 |
byte *dst2 = _dst2; |
| 899 |
const byte *src = _src; |
| 900 |
u64 d2[2]; |
| 901 |
u64 s[2]; |
| 902 |
|
| 903 |
if (blocksize == 8) |
| 904 |
{ |
| 905 |
d2[0] = buf_get_he64(dst2 + 0) ^ buf_get_he64(src + 0); |
| 906 |
buf_put_he64(dst2 + 0, d2[0]); |
| 907 |
buf_put_he64(dst1 + 0, d2[0]); |
| 908 |
} |
| 909 |
else /* blocksize == 16 */ |
| 910 |
{ |
| 911 |
s[0] = buf_get_he64(src + 0); |
| 912 |
s[1] = buf_get_he64(src + 8); |
| 913 |
d2[0] = buf_get_he64(dst2 + 0); |
| 914 |
d2[1] = buf_get_he64(dst2 + 8); |
| 915 |
d2[0] = d2[0] ^ s[0]; |
| 916 |
d2[1] = d2[1] ^ s[1]; |
| 917 |
buf_put_he64(dst2 + 0, d2[0]); |
| 918 |
buf_put_he64(dst2 + 8, d2[1]); |
| 919 |
buf_put_he64(dst1 + 0, d2[0]); |
| 920 |
buf_put_he64(dst1 + 8, d2[1]); |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
/* Optimized function for combined cipher block xoring and copying. |
| 926 |
Used by mainly CBC mode decryption. */ |
| 927 |
static inline void |
| 928 |
cipher_block_xor_n_copy_2(void *_dst_xor, const void *_src_xor, |
| 929 |
void *_srcdst_cpy, const void *_src_cpy, |
| 930 |
size_t blocksize) |
| 931 |
{ |
| 932 |
byte *dst_xor = _dst_xor; |
| 933 |
byte *srcdst_cpy = _srcdst_cpy; |
| 934 |
const byte *src_xor = _src_xor; |
| 935 |
const byte *src_cpy = _src_cpy; |
| 936 |
u64 sc[2]; |
| 937 |
u64 sx[2]; |
| 938 |
u64 sdc[2]; |
| 939 |
|
| 940 |
if (blocksize == 8) |
| 941 |
{ |
| 942 |
sc[0] = buf_get_he64(src_cpy + 0); |
| 943 |
buf_put_he64(dst_xor + 0, |
| 944 |
buf_get_he64(srcdst_cpy + 0) ^ buf_get_he64(src_xor + 0)); |
| 945 |
buf_put_he64(srcdst_cpy + 0, sc[0]); |
| 946 |
} |
| 947 |
else /* blocksize == 16 */ |
| 948 |
{ |
| 949 |
sc[0] = buf_get_he64(src_cpy + 0); |
| 950 |
sc[1] = buf_get_he64(src_cpy + 8); |
| 951 |
sx[0] = buf_get_he64(src_xor + 0); |
| 952 |
sx[1] = buf_get_he64(src_xor + 8); |
| 953 |
sdc[0] = buf_get_he64(srcdst_cpy + 0); |
| 954 |
sdc[1] = buf_get_he64(srcdst_cpy + 8); |
| 955 |
sx[0] ^= sdc[0]; |
| 956 |
sx[1] ^= sdc[1]; |
| 957 |
buf_put_he64(dst_xor + 0, sx[0]); |
| 958 |
buf_put_he64(dst_xor + 8, sx[1]); |
| 959 |
buf_put_he64(srcdst_cpy + 0, sc[0]); |
| 960 |
buf_put_he64(srcdst_cpy + 8, sc[1]); |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
/* Optimized function for combined cipher block byte-swapping. */ |
| 966 |
static inline void |
| 967 |
cipher_block_bswap (void *_dst_bswap, const void *_src_bswap, |
| 968 |
size_t blocksize) |
| 969 |
{ |
| 970 |
byte *dst_bswap = _dst_bswap; |
| 971 |
const byte *src_bswap = _src_bswap; |
| 972 |
u64 t[2]; |
| 973 |
|
| 974 |
if (blocksize == 8) |
| 975 |
{ |
| 976 |
buf_put_le64(dst_bswap, buf_get_be64(src_bswap)); |
| 977 |
} |
| 978 |
else |
| 979 |
{ |
| 980 |
t[0] = buf_get_be64(src_bswap + 0); |
| 981 |
t[1] = buf_get_be64(src_bswap + 8); |
| 982 |
buf_put_le64(dst_bswap + 8, t[0]); |
| 983 |
buf_put_le64(dst_bswap + 0, t[1]); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
/* Optimized function for combined cipher block xoring and copying. |
| 989 |
Used by mainly CFB mode decryption. */ |
| 990 |
static inline void |
| 991 |
cipher_block_xor_n_copy(void *_dst_xor, void *_srcdst_cpy, const void *_src, |
| 992 |
size_t blocksize) |
| 993 |
{ |
| 994 |
cipher_block_xor_n_copy_2(_dst_xor, _src, _srcdst_cpy, _src, blocksize); |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
#endif /*G10_CIPHER_INTERNAL_H*/ |