Botan  1.10.9
xtea_simd.cpp
Go to the documentation of this file.
1 /*
2 * XTEA in SIMD
3 * (C) 2009 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/xtea_simd.h>
9 #include <botan/loadstor.h>
10 #include <botan/internal/simd_32.h>
11 
12 namespace Botan {
13 
14 namespace {
15 
16 void xtea_encrypt_8(const byte in[64], byte out[64], const u32bit EK[64])
17  {
18  SIMD_32 L0 = SIMD_32::load_be(in );
19  SIMD_32 R0 = SIMD_32::load_be(in + 16);
20  SIMD_32 L1 = SIMD_32::load_be(in + 32);
21  SIMD_32 R1 = SIMD_32::load_be(in + 48);
22 
23  SIMD_32::transpose(L0, R0, L1, R1);
24 
25  for(size_t i = 0; i != 32; i += 2)
26  {
27  SIMD_32 K0(EK[2*i ]);
28  SIMD_32 K1(EK[2*i+1]);
29  SIMD_32 K2(EK[2*i+2]);
30  SIMD_32 K3(EK[2*i+3]);
31 
32  L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K0;
33  L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K0;
34 
35  R0 += (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K1;
36  R1 += (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K1;
37 
38  L0 += (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K2;
39  L1 += (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K2;
40 
41  R0 += (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K3;
42  R1 += (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K3;
43  }
44 
45  SIMD_32::transpose(L0, R0, L1, R1);
46 
47  L0.store_be(out);
48  R0.store_be(out + 16);
49  L1.store_be(out + 32);
50  R1.store_be(out + 48);
51  }
52 
53 void xtea_decrypt_8(const byte in[64], byte out[64], const u32bit EK[64])
54  {
55  SIMD_32 L0 = SIMD_32::load_be(in );
56  SIMD_32 R0 = SIMD_32::load_be(in + 16);
57  SIMD_32 L1 = SIMD_32::load_be(in + 32);
58  SIMD_32 R1 = SIMD_32::load_be(in + 48);
59 
60  SIMD_32::transpose(L0, R0, L1, R1);
61 
62  for(size_t i = 0; i != 32; i += 2)
63  {
64  SIMD_32 K0(EK[63 - 2*i]);
65  SIMD_32 K1(EK[62 - 2*i]);
66  SIMD_32 K2(EK[61 - 2*i]);
67  SIMD_32 K3(EK[60 - 2*i]);
68 
69  R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K0;
70  R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K0;
71 
72  L0 -= (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K1;
73  L1 -= (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K1;
74 
75  R0 -= (((L0 << 4) ^ (L0 >> 5)) + L0) ^ K2;
76  R1 -= (((L1 << 4) ^ (L1 >> 5)) + L1) ^ K2;
77 
78  L0 -= (((R0 << 4) ^ (R0 >> 5)) + R0) ^ K3;
79  L1 -= (((R1 << 4) ^ (R1 >> 5)) + R1) ^ K3;
80  }
81 
82  SIMD_32::transpose(L0, R0, L1, R1);
83 
84  L0.store_be(out);
85  R0.store_be(out + 16);
86  L1.store_be(out + 32);
87  R1.store_be(out + 48);
88  }
89 
90 }
91 
92 /*
93 * XTEA Encryption
94 */
95 void XTEA_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const
96  {
97  const u32bit* KS = &(this->get_EK()[0]);
98 
99  while(blocks >= 8)
100  {
101  xtea_encrypt_8(in, out, KS);
102  in += 8 * BLOCK_SIZE;
103  out += 8 * BLOCK_SIZE;
104  blocks -= 8;
105  }
106 
107  if(blocks)
108  XTEA::encrypt_n(in, out, blocks);
109  }
110 
111 /*
112 * XTEA Decryption
113 */
114 void XTEA_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const
115  {
116  const u32bit* KS = &(this->get_EK()[0]);
117 
118  while(blocks >= 8)
119  {
120  xtea_decrypt_8(in, out, KS);
121  in += 8 * BLOCK_SIZE;
122  out += 8 * BLOCK_SIZE;
123  blocks -= 8;
124  }
125 
126  if(blocks)
127  XTEA::decrypt_n(in, out, blocks);
128  }
129 
130 }
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: xtea.cpp:62
T load_be(const byte in[], size_t off)
Definition: loadstor.h:100
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: xtea_simd.cpp:114
#define R0
Definition: asm_x86_64.h:51
unsigned char byte
Definition: types.h:22
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: xtea.cpp:93
#define R1
Definition: asm_x86_64.h:52
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: xtea_simd.cpp:95
unsigned int u32bit
Definition: types.h:32
const SecureVector< u32bit > & get_EK() const
Definition: xtea.h:33