Botan: src/utils/xor_buf.h Source File

Botan 1.10.16

xor_buf.h
Go to the documentation of this file.
1 /*
2 * XOR operations
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_XOR_BUF_H__
9 #define BOTAN_XOR_BUF_H__
10
11 #include <botan/types.h>
12
13 namespace Botan {
14
15 /**
16 * XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length
17 * @param out the input/output buffer
18 * @param in the read-only input buffer
19 * @param length the length of the buffers
20 */
21 inline void xor_buf(byte out[], const byte in[], size_t length)
22 {
23 while(length >= 8)
24 {
25 #if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
26 *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in);
27 #else
28 out[0] ^= in[0]; out[1] ^= in[1];
29 out[2] ^= in[2]; out[3] ^= in[3];
30 out[4] ^= in[4]; out[5] ^= in[5];
31 out[6] ^= in[6]; out[7] ^= in[7];
32 #endif
33
34 out += 8; in += 8; length -= 8;
35 }
36
37 for(size_t i = 0; i != length; ++i)
38 out[i] ^= in[i];
39 }
40
41 /**
42 * XOR arrays. Postcondition out[i] = in[i] ^ in2[i] forall i = 0...length
43 * @param out the output buffer
44 * @param in the first input buffer
45 * @param in2 the second output buffer
46 * @param length the length of the three buffers
47 */
48 inline void xor_buf(byte out[],
49 const byte in[],
50 const byte in2[],
51 size_t length)
52 {
53 while(length >= 8)
54 {
55 #if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
56 *reinterpret_cast<u64bit*>(out) =
57 *reinterpret_cast<const u64bit*>(in) ^
58 *reinterpret_cast<const u64bit*>(in2);
59 #else
60 out[0] = in[0] ^ in2[0]; out[1] = in[1] ^ in2[1];
61 out[2] = in[2] ^ in2[2]; out[3] = in[3] ^ in2[3];
62 out[4] = in[4] ^ in2[4]; out[5] = in[5] ^ in2[5];
63 out[6] = in[6] ^ in2[6]; out[7] = in[7] ^ in2[7];
64 #endif
65
66 in += 8; in2 += 8; out += 8; length -= 8;
67 }
68
69 for(size_t i = 0; i != length; ++i)
70 out[i] = in[i] ^ in2[i];
71 }
72
73 }
74
75 #endif
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan
Definition: algo_base.h:14
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21