memxor.c 7.93 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4
/* memxor.c
 *
 */

5 6 7
/* nettle, low-level cryptographics library
 *
 * Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
Niels Möller's avatar
Niels Möller committed
8
 * Copyright (C) 2010 Niels Möller
9 10 11 12 13 14 15 16 17 18 19 20 21
 *  
 * The nettle library 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.
 * 
 * The nettle library 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 the nettle library; see the file COPYING.LIB.  If not, write to
22 23
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
24 25
 */

Niels Möller's avatar
Niels Möller committed
26 27
/* Implementation inspired by memcmp in glibc, contributed to the FSF
   by Torbjorn Granlund.
28
 */
Niels Möller's avatar
Niels Möller committed
29

30
#if HAVE_CONFIG_H
31
# include "config.h"
32 33
#endif

34 35
#include <limits.h>

Niels Möller's avatar
Niels Möller committed
36 37
#include "memxor.h"

38 39 40
/* For uintptr_t */
#include "nettle-types.h"

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
typedef unsigned long int word_t;

#if SIZEOF_LONG & (SIZEOF_LONG - 1)
#error Word size must be a power of two
#endif

#define ALIGN_OFFSET(p) ((uintptr_t) (p) % sizeof(word_t))

#ifndef WORDS_BIGENDIAN
#define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
#else
#define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
#endif

#define WORD_T_THRESH 16

/* XOR word-aligned areas. n is the number of words, not bytes. */
static void
memxor_common_alignment (word_t *dst, const word_t *src, size_t n)
{
Niels Möller's avatar
Niels Möller committed
61 62 63
  /* FIXME: Require n > 0? */
  /* FIXME: Unroll four times, like memcmp? Probably not worth the
     effort. */
64

Niels Möller's avatar
Niels Möller committed
65
  if (n & 1)
66
    {
Niels Möller's avatar
Niels Möller committed
67 68 69 70 71 72 73
      *dst++ ^= *src++;
      n--;
    }
  for (; n >= 2; dst += 2, src += 2, n -= 2)
    {
      dst[0] ^= src[0];
      dst[1] ^= src[1];
74 75 76
    }
}

Niels Möller's avatar
Niels Möller committed
77
/* XOR *un-aligned* src-area onto aligned dst area. n is number of
78 79 80
   words, not bytes. Assumes we can read complete words at the start
   and end of the src operand. */
static void
81
memxor_different_alignment (word_t *dst, const char *src, size_t n)
82 83 84 85 86 87 88 89 90 91
{
  size_t i;
  int shl, shr;
  const word_t *src_word;
  unsigned offset = ALIGN_OFFSET (src);
  word_t s0, s1;

  shl = CHAR_BIT * offset;
  shr = CHAR_BIT * (sizeof(word_t) - offset);

Niels Möller's avatar
Niels Möller committed
92
  src_word = (const word_t *) ((uintptr_t) src & -SIZEOF_LONG);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  /* FIXME: Unroll four times, like memcmp? */
  i = n & 1;
  s0 = src_word[i];
  if (i)
    {
      s1 = src_word[0];
      dst[0] ^= MERGE (s1, shl, s0, shr);
    }

  for (; i < n; i += 2)
    {
      s1 = src_word[i+1];
      dst[i] ^= MERGE(s0, shl, s1, shr);
      s0 = src_word[i+2];
      dst[i+1] ^= MERGE(s1, shl, s0, shr);
    }
}

Niels Möller's avatar
Niels Möller committed
112 113 114
/* Performance, Intel SU1400 (x86_64): 0.25 cycles/byte aligned, 0.45
   cycles/byte unaligned. */

115 116
/* XOR LEN bytes starting at SRCADDR onto DESTADDR. Result undefined
   if the source overlaps with the destination. Return DESTADDR. */
117 118
void *
memxor(void *dst_in, const void *src_in, size_t n)
119
{
120 121
  char *dst = dst_in;
  const char *src = src_in;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

  if (n >= WORD_T_THRESH)
    {
      /* There are at least some bytes to compare.  No need to test
	 for N == 0 in this alignment loop.  */
      while (ALIGN_OFFSET (dst))
	{
	  *dst++ ^= *src++;
	  n--;
	}
      if (ALIGN_OFFSET (src))
	memxor_different_alignment ((word_t *) dst, src, n / sizeof(word_t));
      else
	memxor_common_alignment ((word_t *) dst, (const word_t *) src, n / sizeof(word_t));

Niels Möller's avatar
Niels Möller committed
137 138 139
      dst += n & -SIZEOF_LONG;
      src += n & -SIZEOF_LONG;
      n = n & (SIZEOF_LONG - 1);
140 141 142 143
    }
  for (; n > 0; n--)
    *dst++ ^= *src++;

144
  return dst_in;
145
}
Niels Möller's avatar
Niels Möller committed
146 147 148 149 150 151


/* XOR word-aligned areas. n is the number of words, not bytes. */
static void
memxor3_common_alignment (word_t *dst,
			  const word_t *a, const word_t *b, size_t n)
Niels Möller's avatar
Niels Möller committed
152
{
Niels Möller's avatar
Niels Möller committed
153 154 155 156
  /* FIXME: Require n > 0? */
  while (n-- > 0)
    dst[n] = a[n] ^ b[n];
}
Niels Möller's avatar
Niels Möller committed
157

Niels Möller's avatar
Niels Möller committed
158 159
static void
memxor3_different_alignment_b (word_t *dst,
160
			       const word_t *a, const char *b, unsigned offset, size_t n)
Niels Möller's avatar
Niels Möller committed
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
{
  int shl, shr;
  const word_t *b_word;

  word_t s0, s1;

  shl = CHAR_BIT * offset;
  shr = CHAR_BIT * (sizeof(word_t) - offset);

  b_word = (const word_t *) ((uintptr_t) b & -SIZEOF_LONG);

  if (n & 1)
    {
      n--;
      s1 = b_word[n];
      s0 = b_word[n+1];
      dst[n] = a[n] ^ MERGE (s1, shl, s0, shr);
    }
  else
    s1 = b_word[n];
  
  while (n > 0)
    {
      n -= 2;
      s0 = b_word[n+1]; 
      dst[n+1] = a[n+1] ^ MERGE(s0, shl, s1, shr);
      s1 = b_word[n];
      dst[n] = a[n] ^ MERGE(s1, shl, s0, shr);
    }
Niels Möller's avatar
Niels Möller committed
190
}
191

Niels Möller's avatar
Niels Möller committed
192 193
static void
memxor3_different_alignment_ab (word_t *dst,
194
				const char *a, const char *b,
Niels Möller's avatar
Niels Möller committed
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
				unsigned offset, size_t n)
{
  int shl, shr;
  const word_t *a_word;
  const word_t *b_word;
  
  word_t s0, s1;

  shl = CHAR_BIT * offset;
  shr = CHAR_BIT * (sizeof(word_t) - offset);

  a_word = (const word_t *) ((uintptr_t) a & -SIZEOF_LONG);
  b_word = (const word_t *) ((uintptr_t) b & -SIZEOF_LONG);

  if (n & 1)
    {
      n--;
      s1 = a_word[n] ^ b_word[n];
      s0 = a_word[n+1] ^ b_word[n+1];
      dst[n] = MERGE (s1, shl, s0, shr);
    }
  else    
    s1 = a_word[n] ^ b_word[n];
  
  while (n > 0)
    {
      n -= 2;
      s0 = a_word[n+1] ^ b_word[n+1]; 
      dst[n+1] = MERGE(s0, shl, s1, shr);
      s1 = a_word[n] ^ b_word[n];
      dst[n] = MERGE(s1, shl, s0, shr);
    }
}

static void
memxor3_different_alignment_all (word_t *dst,
231
				 const char *a, const char *b,
Niels Möller's avatar
Niels Möller committed
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
				 unsigned a_offset, unsigned b_offset,
				 size_t n)
{
  int al, ar, bl, br;
  const word_t *a_word;
  const word_t *b_word;
  
  word_t a0, a1, b0, b1;

  al = CHAR_BIT * a_offset;
  ar = CHAR_BIT * (sizeof(word_t) - a_offset);
  bl = CHAR_BIT * b_offset;
  br = CHAR_BIT * (sizeof(word_t) - b_offset);

  a_word = (const word_t *) ((uintptr_t) a & -SIZEOF_LONG);
  b_word = (const word_t *) ((uintptr_t) b & -SIZEOF_LONG);

  if (n & 1)
    {
      n--;
      a1 = a_word[n]; a0 = a_word[n+1];
      b1 = b_word[n]; b0 = b_word[n+1];
      
      dst[n] = MERGE (a1, al, a0, ar) ^ MERGE (b1, bl, b0, br);
    }
  else    
    {
      a1 = a_word[n];
      b1 = b_word[n];
    }
  
  while (n > 0)
    {
      n -= 2;
      a0 = a_word[n+1]; b0 = b_word[n+1]; 
      dst[n+1] = MERGE(a0, al, a1, ar) ^ MERGE(b0, bl, b1, br);
      a1 = a_word[n]; b1 = b_word[n];
      dst[n] = MERGE(a1, al, a0, ar) ^ MERGE(b1, bl, b0, br);
    }
}

/* Current implementation processes data in descending order, to
   support overlapping operation with one of the sources overlapping
   the start of the destination area. This feature is used only
   internally by cbc decrypt, and it is not advertised or documented
   to nettle users. */
278 279
void *
memxor3(void *dst_in, const void *a_in, const void *b_in, size_t n)
280
{
281 282 283 284
  char *dst = dst_in;
  const char *a = a_in;
  const char *b = b_in;

Niels Möller's avatar
Niels Möller committed
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
  if (n >= WORD_T_THRESH)
    {
      unsigned i;
      unsigned a_offset;
      unsigned b_offset;
      size_t nwords;

      for (i = ALIGN_OFFSET(dst + n); i > 0; i--)
	{
	  n--;
	  dst[n] = a[n] ^ b[n];
	}

      a_offset = ALIGN_OFFSET(a + n);
      b_offset = ALIGN_OFFSET(b + n);

      nwords = n / sizeof (word_t);
      n %= sizeof (word_t);

      if (a_offset == b_offset)
	{
	  if (!a_offset)
	    memxor3_common_alignment((word_t *) (dst + n),
				     (const word_t *) (a + n),
				     (const word_t *) (b + n), nwords);
	  else
	    memxor3_different_alignment_ab((word_t *) (dst + n),
					   a + n, b + n, a_offset,
					   nwords);
	}
      else if (!a_offset)
	memxor3_different_alignment_b((word_t *) (dst + n),
				      (const word_t *) (a + n), b + n,
				      b_offset, nwords);
      else if (!b_offset)
	memxor3_different_alignment_b((word_t *) (dst + n),
				      (const word_t *) (b + n), a + n,
				      a_offset, nwords);
      else
	memxor3_different_alignment_all((word_t *) (dst + n), a + n, b + n,
					a_offset, b_offset, nwords);
					
    }
  while (n-- > 0)
    dst[n] = a[n] ^ b[n];
330 331 332

  return dst;
}