00001
00002
00003 #include "pch.h"
00004 #include "config.h"
00005
00006 #if CRYPTOPP_MSC_VERSION
00007 # pragma warning(disable: 4189)
00008 # if (CRYPTOPP_MSC_VERSION >= 1400)
00009 # pragma warning(disable: 6237)
00010 # endif
00011 #endif
00012
00013 #ifndef CRYPTOPP_IMPORTS
00014
00015 #include "misc.h"
00016 #include "words.h"
00017 #include "words.h"
00018 #include "stdcpp.h"
00019 #include "integer.h"
00020
00021
00022 #if defined(CRYPTOPP_MEMALIGN_AVAILABLE) || defined(CRYPTOPP_MM_MALLOC_AVAILABLE) || defined(QNX)
00023 # include <malloc.h>
00024 #endif
00025
00026 NAMESPACE_BEGIN(CryptoPP)
00027
00028 void xorbuf(byte *buf, const byte *mask, size_t count)
00029 {
00030 assert(buf != NULL);
00031 assert(mask != NULL);
00032 assert(count > 0);
00033
00034 size_t i=0;
00035 if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
00036 {
00037 if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
00038 {
00039 for (i=0; i<count/8; i++)
00040 ((word64*)buf)[i] ^= ((word64*)mask)[i];
00041 count -= 8*i;
00042 if (!count)
00043 return;
00044 buf += 8*i;
00045 mask += 8*i;
00046 }
00047
00048 for (i=0; i<count/4; i++)
00049 ((word32*)buf)[i] ^= ((word32*)mask)[i];
00050 count -= 4*i;
00051 if (!count)
00052 return;
00053 buf += 4*i;
00054 mask += 4*i;
00055 }
00056
00057 for (i=0; i<count; i++)
00058 buf[i] ^= mask[i];
00059 }
00060
00061 void xorbuf(byte *output, const byte *input, const byte *mask, size_t count)
00062 {
00063 assert(output != NULL);
00064 assert(input != NULL);
00065 assert(count > 0);
00066
00067 size_t i=0;
00068 if (IsAligned<word32>(output) && IsAligned<word32>(input) && IsAligned<word32>(mask))
00069 {
00070 if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(output) && IsAligned<word64>(input) && IsAligned<word64>(mask))
00071 {
00072 for (i=0; i<count/8; i++)
00073 ((word64*)output)[i] = ((word64*)input)[i] ^ ((word64*)mask)[i];
00074 count -= 8*i;
00075 if (!count)
00076 return;
00077 output += 8*i;
00078 input += 8*i;
00079 mask += 8*i;
00080 }
00081
00082 for (i=0; i<count/4; i++)
00083 ((word32*)output)[i] = ((word32*)input)[i] ^ ((word32*)mask)[i];
00084 count -= 4*i;
00085 if (!count)
00086 return;
00087 output += 4*i;
00088 input += 4*i;
00089 mask += 4*i;
00090 }
00091
00092 for (i=0; i<count; i++)
00093 output[i] = input[i] ^ mask[i];
00094 }
00095
00096 bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
00097 {
00098 assert(buf != NULL);
00099 assert(mask != NULL);
00100 assert(count > 0);
00101
00102 size_t i=0;
00103 byte acc8 = 0;
00104
00105 if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
00106 {
00107 word32 acc32 = 0;
00108 if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
00109 {
00110 word64 acc64 = 0;
00111 for (i=0; i<count/8; i++)
00112 acc64 |= ((word64*)buf)[i] ^ ((word64*)mask)[i];
00113 count -= 8*i;
00114 if (!count)
00115 return acc64 == 0;
00116 buf += 8*i;
00117 mask += 8*i;
00118 acc32 = word32(acc64) | word32(acc64>>32);
00119 }
00120
00121 for (i=0; i<count/4; i++)
00122 acc32 |= ((word32*)buf)[i] ^ ((word32*)mask)[i];
00123 count -= 4*i;
00124 if (!count)
00125 return acc32 == 0;
00126 buf += 4*i;
00127 mask += 4*i;
00128 acc8 = byte(acc32) | byte(acc32>>8) | byte(acc32>>16) | byte(acc32>>24);
00129 }
00130
00131 for (i=0; i<count; i++)
00132 acc8 |= buf[i] ^ mask[i];
00133 return acc8 == 0;
00134 }
00135
00136 #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
00137 using std::new_handler;
00138 using std::set_new_handler;
00139 #endif
00140
00141 void CallNewHandler()
00142 {
00143 new_handler newHandler = set_new_handler(NULL);
00144 if (newHandler)
00145 set_new_handler(newHandler);
00146
00147 if (newHandler)
00148 newHandler();
00149 else
00150 throw std::bad_alloc();
00151 }
00152
00153 #if CRYPTOPP_BOOL_ALIGN16
00154
00155 void * AlignedAllocate(size_t size)
00156 {
00157 byte *p;
00158 #if defined(CRYPTOPP_APPLE_ALLOC_AVAILABLE)
00159 while ((p = (byte *)calloc(1, size)) == NULL)
00160 #elif defined(CRYPTOPP_MM_MALLOC_AVAILABLE)
00161 while ((p = (byte *)_mm_malloc(size, 16)) == NULL)
00162 #elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
00163 while ((p = (byte *)memalign(16, size)) == NULL)
00164 #elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
00165 while ((p = (byte *)malloc(size)) == NULL)
00166 #else
00167 while ((p = (byte *)malloc(size + 16)) == NULL)
00168 #endif
00169 CallNewHandler();
00170
00171 #ifdef CRYPTOPP_NO_ALIGNED_ALLOC
00172 size_t adjustment = 16-((size_t)p%16);
00173 p += adjustment;
00174 p[-1] = (byte)adjustment;
00175 #endif
00176
00177 assert(IsAlignedOn(p, 16));
00178 return p;
00179 }
00180
00181 void AlignedDeallocate(void *p)
00182 {
00183 #ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
00184 _mm_free(p);
00185 #elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
00186 p = (byte *)p - ((byte *)p)[-1];
00187 free(p);
00188 #else
00189 free(p);
00190 #endif
00191 }
00192
00193 #endif
00194
00195 void * UnalignedAllocate(size_t size)
00196 {
00197 void *p;
00198 while ((p = malloc(size)) == NULL)
00199 CallNewHandler();
00200 return p;
00201 }
00202
00203 void UnalignedDeallocate(void *p)
00204 {
00205 free(p);
00206 }
00207
00208 NAMESPACE_END
00209
00210 #endif