Crypto++
misc.cpp
1 // misc.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #ifndef CRYPTOPP_IMPORTS
6 
7 #include "misc.h"
8 #include "words.h"
9 #include <new>
10 
11 #if defined(CRYPTOPP_MEMALIGN_AVAILABLE) || defined(CRYPTOPP_MM_MALLOC_AVAILABLE) || defined(QNX)
12 #include <malloc.h>
13 #endif
14 
15 NAMESPACE_BEGIN(CryptoPP)
16 
17 void xorbuf(byte *buf, const byte *mask, size_t count)
18 {
19  size_t i;
20 
21  if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
22  {
23  if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
24  {
25  for (i=0; i<count/8; i++)
26  ((word64*)buf)[i] ^= ((word64*)mask)[i];
27  count -= 8*i;
28  if (!count)
29  return;
30  buf += 8*i;
31  mask += 8*i;
32  }
33 
34  for (i=0; i<count/4; i++)
35  ((word32*)buf)[i] ^= ((word32*)mask)[i];
36  count -= 4*i;
37  if (!count)
38  return;
39  buf += 4*i;
40  mask += 4*i;
41  }
42 
43  for (i=0; i<count; i++)
44  buf[i] ^= mask[i];
45 }
46 
47 void xorbuf(byte *output, const byte *input, const byte *mask, size_t count)
48 {
49  size_t i;
50 
51  if (IsAligned<word32>(output) && IsAligned<word32>(input) && IsAligned<word32>(mask))
52  {
53  if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(output) && IsAligned<word64>(input) && IsAligned<word64>(mask))
54  {
55  for (i=0; i<count/8; i++)
56  ((word64*)output)[i] = ((word64*)input)[i] ^ ((word64*)mask)[i];
57  count -= 8*i;
58  if (!count)
59  return;
60  output += 8*i;
61  input += 8*i;
62  mask += 8*i;
63  }
64 
65  for (i=0; i<count/4; i++)
66  ((word32*)output)[i] = ((word32*)input)[i] ^ ((word32*)mask)[i];
67  count -= 4*i;
68  if (!count)
69  return;
70  output += 4*i;
71  input += 4*i;
72  mask += 4*i;
73  }
74 
75  for (i=0; i<count; i++)
76  output[i] = input[i] ^ mask[i];
77 }
78 
79 bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
80 {
81  size_t i;
82  byte acc8 = 0;
83 
84  if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
85  {
86  word32 acc32 = 0;
87  if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
88  {
89  word64 acc64 = 0;
90  for (i=0; i<count/8; i++)
91  acc64 |= ((word64*)buf)[i] ^ ((word64*)mask)[i];
92  count -= 8*i;
93  if (!count)
94  return acc64 == 0;
95  buf += 8*i;
96  mask += 8*i;
97  acc32 = word32(acc64) | word32(acc64>>32);
98  }
99 
100  for (i=0; i<count/4; i++)
101  acc32 |= ((word32*)buf)[i] ^ ((word32*)mask)[i];
102  count -= 4*i;
103  if (!count)
104  return acc32 == 0;
105  buf += 4*i;
106  mask += 4*i;
107  acc8 = byte(acc32) | byte(acc32>>8) | byte(acc32>>16) | byte(acc32>>24);
108  }
109 
110  for (i=0; i<count; i++)
111  acc8 |= buf[i] ^ mask[i];
112  return acc8 == 0;
113 }
114 
115 #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
116 using std::new_handler;
117 using std::set_new_handler;
118 #endif
119 
120 void CallNewHandler()
121 {
122  new_handler newHandler = set_new_handler(NULL);
123  if (newHandler)
124  set_new_handler(newHandler);
125 
126  if (newHandler)
127  newHandler();
128  else
129  throw std::bad_alloc();
130 }
131 
132 #if CRYPTOPP_BOOL_ALIGN16_ENABLED
133 
134 void * AlignedAllocate(size_t size)
135 {
136  byte *p;
137 #ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
138  while (!(p = (byte *)_mm_malloc(size, 16)))
139 #elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
140  while (!(p = (byte *)memalign(16, size)))
141 #elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
142  while (!(p = (byte *)malloc(size)))
143 #else
144  while (!(p = (byte *)malloc(size + 16)))
145 #endif
146  CallNewHandler();
147 
148 #ifdef CRYPTOPP_NO_ALIGNED_ALLOC
149  size_t adjustment = 16-((size_t)p%16);
150  p += adjustment;
151  p[-1] = (byte)adjustment;
152 #endif
153 
154  assert(IsAlignedOn(p, 16));
155  return p;
156 }
157 
158 void AlignedDeallocate(void *p)
159 {
160 #ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
161  _mm_free(p);
162 #elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
163  p = (byte *)p - ((byte *)p)[-1];
164  free(p);
165 #else
166  free(p);
167 #endif
168 }
169 
170 #endif
171 
172 void * UnalignedAllocate(size_t size)
173 {
174  void *p;
175  while (!(p = malloc(size)))
176  CallNewHandler();
177  return p;
178 }
179 
180 void UnalignedDeallocate(void *p)
181 {
182  free(p);
183 }
184 
185 NAMESPACE_END
186 
187 #endif