1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/* bufhelp.h - Some buffer manipulation helpers
* Copyright (C) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
*
* This file is part of Libgcrypt.
*
* Libgcrypt is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Libgcrypt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GCRYPT_BUFHELP_H
#define GCRYPT_BUFHELP_H
#include "bithelp.h"
#undef BUFHELP_FAST_UNALIGNED_ACCESS
#if defined(HAVE_GCC_ATTRIBUTE_PACKED) && \
defined(HAVE_GCC_ATTRIBUTE_ALIGNED) && \
(defined(__i386__) || defined(__x86_64__) || \
(defined(__arm__) && defined(__ARM_FEATURE_UNALIGNED)) || \
defined(__aarch64__))
/* These architectures are able of unaligned memory accesses and can
handle those fast.
*/
# define BUFHELP_FAST_UNALIGNED_ACCESS 1
#endif
#ifdef BUFHELP_FAST_UNALIGNED_ACCESS
/* Define type with one-byte alignment on architectures with fast unaligned
memory accesses.
*/
typedef struct bufhelp_int_s
{
uintptr_t a;
} __attribute__((packed, aligned(1))) bufhelp_int_t;
#else
/* Define type with default alignment for other architectures (unaligned
accessed handled in per byte loops).
*/
typedef struct bufhelp_int_s
{
uintptr_t a;
} bufhelp_int_t;
#endif
/* Optimized function for small buffer copying */
static inline void
buf_cpy(void *_dst, const void *_src, size_t len)
{
#if __GNUC__ >= 4 && (defined(__x86_64__) || defined(__i386__))
/* For AMD64 and i386, memcpy is faster. */
memcpy(_dst, _src, len);
#else
byte *dst = _dst;
const byte *src = _src;
bufhelp_int_t *ldst;
const bufhelp_int_t *lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
if (((uintptr_t)dst | (uintptr_t)src) & longmask)
goto do_bytes;
#endif
ldst = (bufhelp_int_t *)(void *)dst;
lsrc = (const bufhelp_int_t *)(const void *)src;
for (; len >= sizeof(bufhelp_int_t); len -= sizeof(bufhelp_int_t))
(ldst++)->a = (lsrc++)->a;
dst = (byte *)ldst;
src = (const byte *)lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
/* Handle tail. */
for (; len; len--)
*dst++ = *src++;
#endif /*__GNUC__ >= 4 && (__x86_64__ || __i386__)*/
}
/* Optimized function for buffer xoring */
static inline void
buf_xor(void *_dst, const void *_src1, const void *_src2, size_t len)
{
byte *dst = _dst;
const byte *src1 = _src1;
const byte *src2 = _src2;
bufhelp_int_t *ldst;
const bufhelp_int_t *lsrc1, *lsrc2;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
if (((uintptr_t)dst | (uintptr_t)src1 | (uintptr_t)src2) & longmask)
goto do_bytes;
#endif
ldst = (bufhelp_int_t *)(void *)dst;
lsrc1 = (const bufhelp_int_t *)(const void *)src1;
lsrc2 = (const bufhelp_int_t *)(const void *)src2;
for (; len >= sizeof(bufhelp_int_t); len -= sizeof(bufhelp_int_t))
(ldst++)->a = (lsrc1++)->a ^ (lsrc2++)->a;
dst = (byte *)ldst;
src1 = (const byte *)lsrc1;
src2 = (const byte *)lsrc2;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
/* Handle tail. */
for (; len; len--)
*dst++ = *src1++ ^ *src2++;
}
/* Optimized function for in-place buffer xoring. */
static inline void
buf_xor_1(void *_dst, const void *_src, size_t len)
{
byte *dst = _dst;
const byte *src = _src;
bufhelp_int_t *ldst;
const bufhelp_int_t *lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
if (((uintptr_t)dst | (uintptr_t)src) & longmask)
goto do_bytes;
#endif
ldst = (bufhelp_int_t *)(void *)dst;
lsrc = (const bufhelp_int_t *)(const void *)src;
for (; len >= sizeof(bufhelp_int_t); len -= sizeof(bufhelp_int_t))
(ldst++)->a ^= (lsrc++)->a;
dst = (byte *)ldst;
src = (const byte *)lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
/* Handle tail. */
for (; len; len--)
*dst++ ^= *src++;
}
/* Optimized function for buffer xoring with two destination buffers. Used
mainly by CFB mode encryption. */
static inline void
buf_xor_2dst(void *_dst1, void *_dst2, const void *_src, size_t len)
{
byte *dst1 = _dst1;
byte *dst2 = _dst2;
const byte *src = _src;
bufhelp_int_t *ldst1, *ldst2;
const bufhelp_int_t *lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
if (((uintptr_t)src | (uintptr_t)dst1 | (uintptr_t)dst2) & longmask)
goto do_bytes;
#endif
ldst1 = (bufhelp_int_t *)(void *)dst1;
ldst2 = (bufhelp_int_t *)(void *)dst2;
lsrc = (const bufhelp_int_t *)(const void *)src;
for (; len >= sizeof(bufhelp_int_t); len -= sizeof(bufhelp_int_t))
(ldst1++)->a = ((ldst2++)->a ^= (lsrc++)->a);
dst1 = (byte *)ldst1;
dst2 = (byte *)ldst2;
src = (const byte *)lsrc;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
/* Handle tail. */
for (; len; len--)
*dst1++ = (*dst2++ ^= *src++);
}
/* Optimized function for combined buffer xoring and copying. Used by mainly
CBC mode decryption. */
static inline void
buf_xor_n_copy_2(void *_dst_xor, const void *_src_xor, void *_srcdst_cpy,
const void *_src_cpy, size_t len)
{
byte *dst_xor = _dst_xor;
byte *srcdst_cpy = _srcdst_cpy;
const byte *src_xor = _src_xor;
const byte *src_cpy = _src_cpy;
byte temp;
bufhelp_int_t *ldst_xor, *lsrcdst_cpy;
const bufhelp_int_t *lsrc_cpy, *lsrc_xor;
uintptr_t ltemp;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
const unsigned int longmask = sizeof(bufhelp_int_t) - 1;
/* Skip fast processing if buffers are unaligned. */
if (((uintptr_t)src_cpy | (uintptr_t)src_xor | (uintptr_t)dst_xor |
(uintptr_t)srcdst_cpy) & longmask)
goto do_bytes;
#endif
ldst_xor = (bufhelp_int_t *)(void *)dst_xor;
lsrc_xor = (const bufhelp_int_t *)(void *)src_xor;
lsrcdst_cpy = (bufhelp_int_t *)(void *)srcdst_cpy;
lsrc_cpy = (const bufhelp_int_t *)(const void *)src_cpy;
for (; len >= sizeof(bufhelp_int_t); len -= sizeof(bufhelp_int_t))
{
ltemp = (lsrc_cpy++)->a;
(ldst_xor++)->a = (lsrcdst_cpy)->a ^ (lsrc_xor++)->a;
(lsrcdst_cpy++)->a = ltemp;
}
dst_xor = (byte *)ldst_xor;
src_xor = (const byte *)lsrc_xor;
srcdst_cpy = (byte *)lsrcdst_cpy;
src_cpy = (const byte *)lsrc_cpy;
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
do_bytes:
#endif
/* Handle tail. */
for (; len; len--)
{
temp = *src_cpy++;
*dst_xor++ = *srcdst_cpy ^ *src_xor++;
*srcdst_cpy++ = temp;
}
}
/* Optimized function for combined buffer xoring and copying. Used by mainly
CFB mode decryption. */
static inline void
buf_xor_n_copy(void *_dst_xor, void *_srcdst_cpy, const void *_src, size_t len)
{
buf_xor_n_copy_2(_dst_xor, _src, _srcdst_cpy, _src, len);
}
/* Constant-time compare of two buffers. Returns 1 if buffers are equal,
and 0 if buffers differ. */
static inline int
buf_eq_const(const void *_a, const void *_b, size_t len)
{
const byte *a = _a;
const byte *b = _b;
size_t diff, i;
/* Constant-time compare. */
for (i = 0, diff = 0; i < len; i++)
diff -= !!(a[i] - b[i]);
return !diff;
}
#ifndef BUFHELP_FAST_UNALIGNED_ACCESS
/* Functions for loading and storing unaligned u32 values of different
endianness. */
static inline u32 buf_get_be32(const void *_buf)
{
const byte *in = _buf;
return ((u32)in[0] << 24) | ((u32)in[1] << 16) | \
((u32)in[2] << 8) | (u32)in[3];
}
static inline u32 buf_get_le32(const void *_buf)
{
const byte *in = _buf;
return ((u32)in[3] << 24) | ((u32)in[2] << 16) | \
((u32)in[1] << 8) | (u32)in[0];
}
static inline void buf_put_be32(void *_buf, u32 val)
{
byte *out = _buf;
out[0] = val >> 24;
out[1] = val >> 16;
out[2] = val >> 8;
out[3] = val;
}
static inline void buf_put_le32(void *_buf, u32 val)
{
byte *out = _buf;
out[3] = val >> 24;
out[2] = val >> 16;
out[1] = val >> 8;
out[0] = val;
}
/* Functions for loading and storing unaligned u64 values of different
endianness. */
static inline u64 buf_get_be64(const void *_buf)
{
const byte *in = _buf;
return ((u64)in[0] << 56) | ((u64)in[1] << 48) | \
((u64)in[2] << 40) | ((u64)in[3] << 32) | \
((u64)in[4] << 24) | ((u64)in[5] << 16) | \
((u64)in[6] << 8) | (u64)in[7];
}
static inline u64 buf_get_le64(const void *_buf)
{
const byte *in = _buf;
return ((u64)in[7] << 56) | ((u64)in[6] << 48) | \
((u64)in[5] << 40) | ((u64)in[4] << 32) | \
((u64)in[3] << 24) | ((u64)in[2] << 16) | \
((u64)in[1] << 8) | (u64)in[0];
}
static inline void buf_put_be64(void *_buf, u64 val)
{
byte *out = _buf;
out[0] = val >> 56;
out[1] = val >> 48;
out[2] = val >> 40;
out[3] = val >> 32;
out[4] = val >> 24;
out[5] = val >> 16;
out[6] = val >> 8;
out[7] = val;
}
static inline void buf_put_le64(void *_buf, u64 val)
{
byte *out = _buf;
out[7] = val >> 56;
out[6] = val >> 48;
out[5] = val >> 40;
out[4] = val >> 32;
out[3] = val >> 24;
out[2] = val >> 16;
out[1] = val >> 8;
out[0] = val;
}
#else /*BUFHELP_FAST_UNALIGNED_ACCESS*/
typedef struct bufhelp_u32_s
{
u32 a;
} __attribute__((packed, aligned(1))) bufhelp_u32_t;
/* Functions for loading and storing unaligned u32 values of different
endianness. */
static inline u32 buf_get_be32(const void *_buf)
{
return be_bswap32(((const bufhelp_u32_t *)_buf)->a);
}
static inline u32 buf_get_le32(const void *_buf)
{
return le_bswap32(((const bufhelp_u32_t *)_buf)->a);
}
static inline void buf_put_be32(void *_buf, u32 val)
{
bufhelp_u32_t *out = _buf;
out->a = be_bswap32(val);
}
static inline void buf_put_le32(void *_buf, u32 val)
{
bufhelp_u32_t *out = _buf;
out->a = le_bswap32(val);
}
typedef struct bufhelp_u64_s
{
u64 a;
} __attribute__((packed, aligned(1))) bufhelp_u64_t;
/* Functions for loading and storing unaligned u64 values of different
endianness. */
static inline u64 buf_get_be64(const void *_buf)
{
return be_bswap64(((const bufhelp_u64_t *)_buf)->a);
}
static inline u64 buf_get_le64(const void *_buf)
{
return le_bswap64(((const bufhelp_u64_t *)_buf)->a);
}
static inline void buf_put_be64(void *_buf, u64 val)
{
bufhelp_u64_t *out = _buf;
out->a = be_bswap64(val);
}
static inline void buf_put_le64(void *_buf, u64 val)
{
bufhelp_u64_t *out = _buf;
out->a = le_bswap64(val);
}
#endif /*BUFHELP_FAST_UNALIGNED_ACCESS*/
#endif /*GCRYPT_BUFHELP_H*/
|