Percy++
A C++ implementation of Private Information Retrieval (PIR) protocols
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
xor.h
1 // Percy++ Copyright 2007,2012,2013,2014
2 // Ian Goldberg <iang@cs.uwaterloo.ca>,
3 // Casey Devet <cjdevet@uwaterloo.ca>,
4 // Wouter Lueks <wouter@telox.net>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of version 2 of the GNU General Public License as
8 // published by the Free Software Foundation.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // There is a copy of the GNU General Public License in the COPYING file
16 // packaged with this plugin; if you cannot find it, write to the Free
17 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 
20 #ifndef __XOR_H__
21 #define __XOR_H__
22 
23 typedef unsigned long chunk;
24 
25 inline void XOR_equal(unsigned char *dst, const unsigned char *src, size_t len) {
26  chunk *dl = (chunk *)dst;
27  const chunk *sl = (const chunk *)src;
28  size_t ll = len / (8*sizeof(chunk));
29  size_t lr = len % (8*sizeof(chunk));
30  for(size_t i=0; i<ll; i++) {
31  *(dl++) ^= *(sl++);
32  *(dl++) ^= *(sl++);
33  *(dl++) ^= *(sl++);
34  *(dl++) ^= *(sl++);
35  *(dl++) ^= *(sl++);
36  *(dl++) ^= *(sl++);
37  *(dl++) ^= *(sl++);
38  *(dl++) ^= *(sl++);
39  }
40  if (lr) {
41  dst = (unsigned char *)dl;
42  src = (const unsigned char *)sl;
43  for(size_t i=0; i<lr; i++) {
44  *(dst++) ^= *(src++);
45  }
46  }
47 }
48 
49 inline void XOR_memblocks(unsigned char *dst, const unsigned char *src1,
50  const unsigned char *src2, size_t len) {
51  chunk *dl = (chunk *)dst;
52  const chunk *sl1 = (const chunk *)src1;
53  const chunk *sl2 = (const chunk *)src2;
54  size_t ll = len / (8*sizeof(chunk));
55  size_t lr = len % (8*sizeof(chunk));
56  for(size_t i=0; i<ll; i++) {
57  *(dl++) = *(sl1++) ^ *(sl2++);
58  *(dl++) = *(sl1++) ^ *(sl2++);
59  *(dl++) = *(sl1++) ^ *(sl2++);
60  *(dl++) = *(sl1++) ^ *(sl2++);
61  *(dl++) = *(sl1++) ^ *(sl2++);
62  *(dl++) = *(sl1++) ^ *(sl2++);
63  *(dl++) = *(sl1++) ^ *(sl2++);
64  *(dl++) = *(sl1++) ^ *(sl2++);
65  }
66  if (lr) {
67  dst = (unsigned char *)dl;
68  src1 = (const unsigned char *)sl1;
69  src2 = (const unsigned char *)sl2;
70  for(size_t i=0; i<lr; i++) {
71  *(dst++) = *(src1++) ^ *(src2++);
72  }
73  }
74 }
75 
76 #endif
77