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

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* nettle, low-level cryptographics library
 *
 * Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
 * Copyright (C) 2010 Niels Mller
 *  
 * 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
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

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

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

33 34
#include <limits.h>

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

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
#if 1
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)
{
  size_t i;
  /* FIXME: Unroll four times, like memcmp? */
  i = n & 1;
  if (i)
    dst[0] ^= src[0];

  for (; i < n; i += 2)
    {
      dst[i] ^= src[i];
      dst[i+1] ^= src[i+1];
    }
}

/* XOR *un-aligned* src-area onto aligned dst area. n is numer of
   words, not bytes. Assumes we can read complete words at the start
   and end of the src operand. */
static void
memxor_different_alignment (word_t *dst, const uint8_t *src, size_t n)
{
  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);

  src_word = (const word_t *) (src - offset);

  /* FIXME: Unroll four times, like memcmp? */
  i = n & 1;
  s0 = src_word[i];
  if (i)
    {
      s1 = src_word[0];
      s0 = src_word[1];
      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);
    }
}

/* XOR LEN bytes starting at SRCADDR onto DESTADDR. Result undefined
   if the source overlaps with the destination. Return DESTADDR. */
uint8_t *
memxor(uint8_t *dst, const uint8_t *src, size_t n)
{
  uint8_t *orig_dst = dst;

  if (n >= WORD_T_THRESH)
    {
      size_t left_over;

      /* 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));

      left_over = n % sizeof(word_t);
      dst += n - left_over;
      src += n - left_over;
      n = left_over;
    }
  for (; n > 0; n--)
    *dst++ ^= *src++;

  return orig_dst;
}
#else
141 142
uint8_t *
memxor(uint8_t *dst, const uint8_t *src, size_t n)
Niels Möller's avatar
Niels Möller committed
143 144 145 146 147 148 149
{
  size_t i;
  for (i = 0; i<n; i++)
    dst[i] ^= src[i];

  return dst;
}
150
#endif
151 152 153 154 155 156 157 158 159 160 161

uint8_t *
memxor3(uint8_t *dst, const uint8_t *a, const uint8_t *b, size_t n)
{
  size_t i;
  for (i = 0; i<n; i++)
    dst[i] = a[i] ^ b[i];

  return dst;
}