Debian Codegraph @

cipher/bufhelp.h

raw
1 /* bufhelp.h - Some buffer manipulation helpers
2 * Copyright (C) 2012-2017 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef GCRYPT_BUFHELP_H
20 #define GCRYPT_BUFHELP_H
21
22
23 #include "g10lib.h"
24 #include "bithelp.h"
25 #include "const-time.h"
26
27
28 #undef BUFHELP_UNALIGNED_ACCESS
29 #if defined(HAVE_GCC_ATTRIBUTE_PACKED) && \
30 defined(HAVE_GCC_ATTRIBUTE_ALIGNED) && \
31 defined(HAVE_GCC_ATTRIBUTE_MAY_ALIAS)
32 /* Compiler is supports attributes needed for automatically issuing unaligned
33 memory access instructions.
34 */
35 # define BUFHELP_UNALIGNED_ACCESS 1
36 #endif
37
38
39 #ifndef BUFHELP_UNALIGNED_ACCESS
40
41 /* Functions for loading and storing unaligned u32 values of different
42 endianness. */
43 static inline u32 buf_get_be32(const void *_buf)
44 {
45 const byte *in = _buf;
46 return ((u32)in[0] << 24) | ((u32)in[1] << 16) | \
47 ((u32)in[2] << 8) | (u32)in[3];
48 }
49
50 static inline u32 buf_get_le32(const void *_buf)
51 {
52 const byte *in = _buf;
53 return ((u32)in[3] << 24) | ((u32)in[2] << 16) | \
54 ((u32)in[1] << 8) | (u32)in[0];
55 }
56
57 static inline void buf_put_be32(void *_buf, u32 val)
58 {
59 byte *out = _buf;
60 out[0] = val >> 24;
61 out[1] = val >> 16;
62 out[2] = val >> 8;
63 out[3] = val;
64 }
65
66 static inline void buf_put_le32(void *_buf, u32 val)
67 {
68 byte *out = _buf;
69 out[3] = val >> 24;
70 out[2] = val >> 16;
71 out[1] = val >> 8;
72 out[0] = val;
73 }
74
75
76 /* Functions for loading and storing unaligned u64 values of different
77 endianness. */
78 static inline u64 buf_get_be64(const void *_buf)
79 {
80 const byte *in = _buf;
81 return ((u64)in[0] << 56) | ((u64)in[1] << 48) | \
82 ((u64)in[2] << 40) | ((u64)in[3] << 32) | \
83 ((u64)in[4] << 24) | ((u64)in[5] << 16) | \
84 ((u64)in[6] << 8) | (u64)in[7];
85 }
86
87 static inline u64 buf_get_le64(const void *_buf)
88 {
89 const byte *in = _buf;
90 return ((u64)in[7] << 56) | ((u64)in[6] << 48) | \
91 ((u64)in[5] << 40) | ((u64)in[4] << 32) | \
92 ((u64)in[3] << 24) | ((u64)in[2] << 16) | \
93 ((u64)in[1] << 8) | (u64)in[0];
94 }
95
96 static inline void buf_put_be64(void *_buf, u64 val)
97 {
98 byte *out = _buf;
99 out[0] = val >> 56;
100 out[1] = val >> 48;
101 out[2] = val >> 40;
102 out[3] = val >> 32;
103 out[4] = val >> 24;
104 out[5] = val >> 16;
105 out[6] = val >> 8;
106 out[7] = val;
107 }
108
109 static inline void buf_put_le64(void *_buf, u64 val)
110 {
111 byte *out = _buf;
112 out[7] = val >> 56;
113 out[6] = val >> 48;
114 out[5] = val >> 40;
115 out[4] = val >> 32;
116 out[3] = val >> 24;
117 out[2] = val >> 16;
118 out[1] = val >> 8;
119 out[0] = val;
120 }
121
122 #else /*BUFHELP_UNALIGNED_ACCESS*/
123
124 typedef struct bufhelp_u32_s
125 {
126 u32 a;
127 } __attribute__((packed, aligned(1), may_alias)) bufhelp_u32_t;
128
129 /* Functions for loading and storing unaligned u32 values of different
130 endianness. */
131 static inline u32 buf_get_be32(const void *_buf)
132 {
133 return be_bswap32(((const bufhelp_u32_t *)_buf)->a);
134 }
135
136 static inline u32 buf_get_le32(const void *_buf)
137 {
138 return le_bswap32(((const bufhelp_u32_t *)_buf)->a);
139 }
140
141 static inline void buf_put_be32(void *_buf, u32 val)
142 {
143 bufhelp_u32_t *out = _buf;
144 out->a = be_bswap32(val);
145 }
146
147 static inline void buf_put_le32(void *_buf, u32 val)
148 {
149 bufhelp_u32_t *out = _buf;
150 out->a = le_bswap32(val);
151 }
152
153
154 typedef struct bufhelp_u64_s
155 {
156 u64 a;
157 } __attribute__((packed, aligned(1), may_alias)) bufhelp_u64_t;
158
159 /* Functions for loading and storing unaligned u64 values of different
160 endianness. */
161 static inline u64 buf_get_be64(const void *_buf)
162 {
163 return be_bswap64(((const bufhelp_u64_t *)_buf)->a);
164 }
165
166 static inline u64 buf_get_le64(const void *_buf)
167 {
168 return le_bswap64(((const bufhelp_u64_t *)_buf)->a);
169 }
170
171 static inline void buf_put_be64(void *_buf, u64 val)
172 {
173 bufhelp_u64_t *out = _buf;
174 out->a = be_bswap64(val);
175 }
176
177 static inline void buf_put_le64(void *_buf, u64 val)
178 {
179 bufhelp_u64_t *out = _buf;
180 out->a = le_bswap64(val);
181 }
182
183 #endif /*BUFHELP_UNALIGNED_ACCESS*/
184
185
186 /* Host-endian get/put macros */
187 #ifdef WORDS_BIGENDIAN
188 # define buf_get_he32 buf_get_be32
189 # define buf_put_he32 buf_put_be32
190 # define buf_get_he64 buf_get_be64
191 # define buf_put_he64 buf_put_be64
192 #else
193 # define buf_get_he32 buf_get_le32
194 # define buf_put_he32 buf_put_le32
195 # define buf_get_he64 buf_get_le64
196 # define buf_put_he64 buf_put_le64
197 #endif
198
199
200
201 /* Optimized function for small buffer copying */
202 static inline void
203 buf_cpy(void *_dst, const void *_src, size_t len)
204 {
205 byte *dst = _dst;
206 const byte *src = _src;
207
208 #if __GNUC__ >= 4
209 if (!__builtin_constant_p (len))
210 {
211 if (UNLIKELY(len == 0))
212 return;
213 memcpy(_dst, _src, len);
214 return;
215 }
216 #endif
217
218 while (len >= sizeof(u64))
219 {
220 buf_put_he64(dst, buf_get_he64(src));
221 dst += sizeof(u64);
222 src += sizeof(u64);
223 len -= sizeof(u64);
224 }
225
226 if (len >= sizeof(u32))
227 {
228 buf_put_he32(dst, buf_get_he32(src));
229 dst += sizeof(u32);
230 src += sizeof(u32);
231 len -= sizeof(u32);
232 }
233
234 /* Handle tail. */
235 for (; len; len--)
236 *dst++ = *src++;
237 }
238
239
240 /* Optimized function for buffer xoring */
241 static inline void
242 buf_xor(void *_dst, const void *_src1, const void *_src2, size_t len)
243 {
244 byte *dst = _dst;
245 const byte *src1 = _src1;
246 const byte *src2 = _src2;
247
248 while (len >= sizeof(u64))
249 {
250 buf_put_he64(dst, buf_get_he64(src1) ^ buf_get_he64(src2));
251 dst += sizeof(u64);
252 src1 += sizeof(u64);
253 src2 += sizeof(u64);
254 len -= sizeof(u64);
255 }
256
257 if (len > sizeof(u32))
258 {
259 buf_put_he32(dst, buf_get_he32(src1) ^ buf_get_he32(src2));
260 dst += sizeof(u32);
261 src1 += sizeof(u32);
262 src2 += sizeof(u32);
263 len -= sizeof(u32);
264 }
265
266 /* Handle tail. */
267 for (; len; len--)
268 *dst++ = *src1++ ^ *src2++;
269 }
270
271
272 /* Optimized function for buffer xoring with two destination buffers. Used
273 mainly by CFB mode encryption. */
274 static inline void
275 buf_xor_2dst(void *_dst1, void *_dst2, const void *_src, size_t len)
276 {
277 byte *dst1 = _dst1;
278 byte *dst2 = _dst2;
279 const byte *src = _src;
280
281 while (len >= sizeof(u64))
282 {
283 u64 temp = buf_get_he64(dst2) ^ buf_get_he64(src);
284 buf_put_he64(dst2, temp);
285 buf_put_he64(dst1, temp);
286 dst2 += sizeof(u64);
287 dst1 += sizeof(u64);
288 src += sizeof(u64);
289 len -= sizeof(u64);
290 }
291
292 if (len >= sizeof(u32))
293 {
294 u32 temp = buf_get_he32(dst2) ^ buf_get_he32(src);
295 buf_put_he32(dst2, temp);
296 buf_put_he32(dst1, temp);
297 dst2 += sizeof(u32);
298 dst1 += sizeof(u32);
299 src += sizeof(u32);
300 len -= sizeof(u32);
301 }
302
303 /* Handle tail. */
304 for (; len; len--)
305 *dst1++ = (*dst2++ ^= *src++);
306 }
307
308
309 /* Optimized function for combined buffer xoring and copying. Used by mainly
310 CBC mode decryption. */
311 static inline void
312 buf_xor_n_copy_2(void *_dst_xor, const void *_src_xor, void *_srcdst_cpy,
313 const void *_src_cpy, size_t len)
314 {
315 byte *dst_xor = _dst_xor;
316 byte *srcdst_cpy = _srcdst_cpy;
317 const byte *src_xor = _src_xor;
318 const byte *src_cpy = _src_cpy;
319
320 while (len >= sizeof(u64))
321 {
322 u64 temp = buf_get_he64(src_cpy);
323 buf_put_he64(dst_xor, buf_get_he64(srcdst_cpy) ^ buf_get_he64(src_xor));
324 buf_put_he64(srcdst_cpy, temp);
325 dst_xor += sizeof(u64);
326 srcdst_cpy += sizeof(u64);
327 src_xor += sizeof(u64);
328 src_cpy += sizeof(u64);
329 len -= sizeof(u64);
330 }
331
332 if (len >= sizeof(u32))
333 {
334 u32 temp = buf_get_he32(src_cpy);
335 buf_put_he32(dst_xor, buf_get_he32(srcdst_cpy) ^ buf_get_he32(src_xor));
336 buf_put_he32(srcdst_cpy, temp);
337 dst_xor += sizeof(u32);
338 srcdst_cpy += sizeof(u32);
339 src_xor += sizeof(u32);
340 src_cpy += sizeof(u32);
341 len -= sizeof(u32);
342 }
343
344 /* Handle tail. */
345 for (; len; len--)
346 {
347 byte temp = *src_cpy++;
348 *dst_xor++ = *srcdst_cpy ^ *src_xor++;
349 *srcdst_cpy++ = temp;
350 }
351 }
352
353
354 /* Optimized function for combined buffer xoring and copying. Used by mainly
355 CFB mode decryption. */
356 static inline void
357 buf_xor_n_copy(void *_dst_xor, void *_srcdst_cpy, const void *_src, size_t len)
358 {
359 buf_xor_n_copy_2(_dst_xor, _src, _srcdst_cpy, _src, len);
360 }
361
362
363 /* Constant-time compare of two buffers. Returns 1 if buffers are equal,
364 and 0 if buffers differ. */
365 static inline int
366 buf_eq_const(const void *a, const void *b, size_t len)
367 {
368 return ct_memequal (a, b, len);
369 }
370
371
372 #endif /*GCRYPT_BUFHELP_H*/