~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to misc.cpp

  • Committer: noloader
  • Date: 2015-07-16 04:38:08 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:590
Added targets for UBsan and Asan

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include "words.h"
9
9
#include <new>
10
10
 
 
11
#if defined(CRYPTOPP_MEMALIGN_AVAILABLE) || defined(CRYPTOPP_MM_MALLOC_AVAILABLE) || defined(QNX)
 
12
#include <malloc.h>
 
13
#endif
 
14
 
11
15
NAMESPACE_BEGIN(CryptoPP)
12
16
 
 
17
// Vectorization at -O3 requires IsStrictAligned<word64> for GCC 4.8 and above with xorbuf and VerifyBufsEqual.
 
18
// Problems have not been experienced for the word32 variant, but it may aoccur in the future.
 
19
 
13
20
void xorbuf(byte *buf, const byte *mask, size_t count)
14
21
{
15
 
        if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0)
16
 
                XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
17
 
        else
 
22
        size_t i;
 
23
 
 
24
        if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
18
25
        {
19
 
                for (unsigned int i=0; i<count; i++)
20
 
                        buf[i] ^= mask[i];
 
26
                if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsStrictAligned<word64>(buf) && IsStrictAligned<word64>(mask))
 
27
                {
 
28
                        assert(IsAlignedOn(buf, GetStrictAlignmentOf<word64>()));
 
29
                        assert(IsAlignedOn(mask, GetStrictAlignmentOf<word64>()));
 
30
 
 
31
                        for (i=0; i<count/8; i++)
 
32
                                ((word64*)buf)[i] ^= ((word64*)mask)[i];
 
33
                        count -= 8*i;
 
34
                        if (!count)
 
35
                                return;
 
36
                        buf += 8*i;
 
37
                        mask += 8*i;
 
38
                }
 
39
 
 
40
                for (i=0; i<count/4; i++)
 
41
                        ((word32*)buf)[i] ^= ((word32*)mask)[i];
 
42
                count -= 4*i;
 
43
                if (!count)
 
44
                        return;
 
45
                buf += 4*i;
 
46
                mask += 4*i;
21
47
        }
 
48
 
 
49
        for (i=0; i<count; i++)
 
50
                buf[i] ^= mask[i];
22
51
}
23
52
 
24
53
void xorbuf(byte *output, const byte *input, const byte *mask, size_t count)
25
54
{
26
 
        if (((size_t)output | (size_t)input | (size_t)mask | count) % WORD_SIZE == 0)
27
 
                XorWords((word *)output, (const word *)input, (const word *)mask, count/WORD_SIZE);
28
 
        else
29
 
        {
30
 
                for (unsigned int i=0; i<count; i++)
31
 
                        output[i] = input[i] ^ mask[i];
32
 
        }
 
55
        size_t i;
 
56
 
 
57
        if (IsAligned<word32>(output) && IsAligned<word32>(input) && IsAligned<word32>(mask))
 
58
        {
 
59
                if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsStrictAligned<word64>(output) && IsStrictAligned<word64>(input) && IsStrictAligned<word64>(mask))
 
60
                {
 
61
                        assert(IsAlignedOn(output, GetStrictAlignmentOf<word64>()));
 
62
                        assert(IsAlignedOn(input, GetStrictAlignmentOf<word64>()));
 
63
                        assert(IsAlignedOn(mask, GetStrictAlignmentOf<word64>()));
 
64
 
 
65
                        for (i=0; i<count/8; i++)
 
66
                                ((word64*)output)[i] = ((word64*)input)[i] ^ ((word64*)mask)[i];
 
67
                        count -= 8*i;
 
68
                        if (!count)
 
69
                                return;
 
70
                        output += 8*i;
 
71
                        input += 8*i;
 
72
                        mask += 8*i;
 
73
                }
 
74
 
 
75
                for (i=0; i<count/4; i++)
 
76
                        ((word32*)output)[i] = ((word32*)input)[i] ^ ((word32*)mask)[i];
 
77
                count -= 4*i;
 
78
                if (!count)
 
79
                        return;
 
80
                output += 4*i;
 
81
                input += 4*i;
 
82
                mask += 4*i;
 
83
        }
 
84
 
 
85
        for (i=0; i<count; i++)
 
86
                output[i] = input[i] ^ mask[i];
 
87
}
 
88
 
 
89
bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
 
90
{
 
91
        size_t i;
 
92
        byte acc8 = 0;
 
93
 
 
94
        if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
 
95
        {
 
96
                word32 acc32 = 0;
 
97
                if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsStrictAligned<word64>(buf) && IsStrictAligned<word64>(mask))
 
98
                {
 
99
                        assert(IsAlignedOn(buf, GetStrictAlignmentOf<word64>()));
 
100
                        assert(IsAlignedOn(mask, GetStrictAlignmentOf<word64>()));
 
101
 
 
102
                        word64 acc64 = 0;
 
103
                        for (i=0; i<count/8; i++)
 
104
                                acc64 |= ((word64*)buf)[i] ^ ((word64*)mask)[i];
 
105
                        count -= 8*i;
 
106
                        if (!count)
 
107
                                return acc64 == 0;
 
108
                        buf += 8*i;
 
109
                        mask += 8*i;
 
110
                        acc32 = word32(acc64) | word32(acc64>>32);
 
111
                }
 
112
 
 
113
                for (i=0; i<count/4; i++)
 
114
                        acc32 |= ((word32*)buf)[i] ^ ((word32*)mask)[i];
 
115
                count -= 4*i;
 
116
                if (!count)
 
117
                        return acc32 == 0;
 
118
                buf += 4*i;
 
119
                mask += 4*i;
 
120
                acc8 = byte(acc32) | byte(acc32>>8) | byte(acc32>>16) | byte(acc32>>24);
 
121
        }
 
122
 
 
123
        for (i=0; i<count; i++)
 
124
                acc8 |= buf[i] ^ mask[i];
 
125
        return acc8 == 0;
33
126
}
34
127
 
35
128
#if !(defined(_MSC_VER) && (_MSC_VER < 1300))
49
142
                throw std::bad_alloc();
50
143
}
51
144
 
 
145
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
 
146
 
 
147
void * AlignedAllocate(size_t size)
 
148
{
 
149
        byte *p;
 
150
#ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
 
151
        while (!(p = (byte *)_mm_malloc(size, 16)))
 
152
#elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
 
153
        while (!(p = (byte *)memalign(16, size)))
 
154
#elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
 
155
        while (!(p = (byte *)malloc(size)))
 
156
#else
 
157
        while (!(p = (byte *)malloc(size + 16)))
 
158
#endif
 
159
                CallNewHandler();
 
160
 
 
161
#ifdef CRYPTOPP_NO_ALIGNED_ALLOC
 
162
        size_t adjustment = 16-((size_t)p%16);
 
163
        p += adjustment;
 
164
        p[-1] = (byte)adjustment;
 
165
#endif
 
166
 
 
167
        assert(IsAlignedOn(p, 16));
 
168
        return p;
 
169
}
 
170
 
 
171
void AlignedDeallocate(void *p)
 
172
{
 
173
#ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
 
174
        _mm_free(p);
 
175
#elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
 
176
        p = (byte *)p - ((byte *)p)[-1];
 
177
        free(p);
 
178
#else
 
179
        free(p);
 
180
#endif
 
181
}
 
182
 
 
183
#endif
 
184
 
 
185
void * UnalignedAllocate(size_t size)
 
186
{
 
187
        void *p;
 
188
        while (!(p = malloc(size)))
 
189
                CallNewHandler();
 
190
        return p;
 
191
}
 
192
 
 
193
void UnalignedDeallocate(void *p)
 
194
{
 
195
        free(p);
 
196
}
 
197
 
52
198
NAMESPACE_END
53
199
 
54
200
#endif