MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rabbit.cpp
1 /*
2  Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 
20 #include "runtime.hpp"
21 #include "rabbit.hpp"
22 
23 
24 
25 namespace TaoCrypt {
26 
27 
28 #define U32V(x) (word32)(x)
29 
30 
31 #ifdef BIG_ENDIAN_ORDER
32  #define LITTLE32(x) ByteReverse((word32)x)
33 #else
34  #define LITTLE32(x) (x)
35 #endif
36 
37 
38 // local
39 namespace {
40 
41 
42 /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */
43 /* the upper 32 bits XOR the lower 32 bits */
44 word32 RABBIT_g_func(word32 x)
45 {
46  /* Temporary variables */
47  word32 a, b, h, l;
48 
49  /* Construct high and low argument for squaring */
50  a = x&0xFFFF;
51  b = x>>16;
52 
53  /* Calculate high and low result of squaring */
54  h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
55  l = x*x;
56 
57  /* Return high XOR low */
58  return U32V(h^l);
59 }
60 
61 
62 } // namespace local
63 
64 
65 /* Calculate the next internal state */
66 void Rabbit::NextState(RabbitCtx which)
67 {
68  /* Temporary variables */
69  word32 g[8], c_old[8], i;
70 
71  Ctx* ctx;
72 
73  if (which == Master)
74  ctx = &masterCtx_;
75  else
76  ctx = &workCtx_;
77 
78  /* Save old counter values */
79  for (i=0; i<8; i++)
80  c_old[i] = ctx->c[i];
81 
82  /* Calculate new counter values */
83  ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry);
84  ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0]));
85  ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1]));
86  ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2]));
87  ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3]));
88  ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4]));
89  ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5]));
90  ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6]));
91  ctx->carry = (ctx->c[7] < c_old[7]);
92 
93  /* Calculate the g-values */
94  for (i=0;i<8;i++)
95  g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
96 
97  /* Calculate new state values */
98  ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16));
99  ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]);
100  ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16));
101  ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]);
102  ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16));
103  ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]);
104  ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16));
105  ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]);
106 }
107 
108 
109 /* IV setup */
110 void Rabbit::SetIV(const byte* iv)
111 {
112  /* Temporary variables */
113  word32 i0, i1, i2, i3, i;
114 
115  /* Generate four subvectors */
116  i0 = LITTLE32(*(word32*)(iv+0));
117  i2 = LITTLE32(*(word32*)(iv+4));
118  i1 = (i0>>16) | (i2&0xFFFF0000);
119  i3 = (i2<<16) | (i0&0x0000FFFF);
120 
121  /* Modify counter values */
122  workCtx_.c[0] = masterCtx_.c[0] ^ i0;
123  workCtx_.c[1] = masterCtx_.c[1] ^ i1;
124  workCtx_.c[2] = masterCtx_.c[2] ^ i2;
125  workCtx_.c[3] = masterCtx_.c[3] ^ i3;
126  workCtx_.c[4] = masterCtx_.c[4] ^ i0;
127  workCtx_.c[5] = masterCtx_.c[5] ^ i1;
128  workCtx_.c[6] = masterCtx_.c[6] ^ i2;
129  workCtx_.c[7] = masterCtx_.c[7] ^ i3;
130 
131  /* Copy state variables */
132  for (i=0; i<8; i++)
133  workCtx_.x[i] = masterCtx_.x[i];
134  workCtx_.carry = masterCtx_.carry;
135 
136  /* Iterate the system four times */
137  for (i=0; i<4; i++)
138  NextState(Work);
139 }
140 
141 
142 /* Key setup */
143 void Rabbit::SetKey(const byte* key, const byte* iv)
144 {
145  /* Temporary variables */
146  word32 k0, k1, k2, k3, i;
147 
148  /* Generate four subkeys */
149  k0 = LITTLE32(*(word32*)(key+ 0));
150  k1 = LITTLE32(*(word32*)(key+ 4));
151  k2 = LITTLE32(*(word32*)(key+ 8));
152  k3 = LITTLE32(*(word32*)(key+12));
153 
154  /* Generate initial state variables */
155  masterCtx_.x[0] = k0;
156  masterCtx_.x[2] = k1;
157  masterCtx_.x[4] = k2;
158  masterCtx_.x[6] = k3;
159  masterCtx_.x[1] = U32V(k3<<16) | (k2>>16);
160  masterCtx_.x[3] = U32V(k0<<16) | (k3>>16);
161  masterCtx_.x[5] = U32V(k1<<16) | (k0>>16);
162  masterCtx_.x[7] = U32V(k2<<16) | (k1>>16);
163 
164  /* Generate initial counter values */
165  masterCtx_.c[0] = rotlFixed(k2, 16);
166  masterCtx_.c[2] = rotlFixed(k3, 16);
167  masterCtx_.c[4] = rotlFixed(k0, 16);
168  masterCtx_.c[6] = rotlFixed(k1, 16);
169  masterCtx_.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF);
170  masterCtx_.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF);
171  masterCtx_.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF);
172  masterCtx_.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF);
173 
174  /* Clear carry bit */
175  masterCtx_.carry = 0;
176 
177  /* Iterate the system four times */
178  for (i=0; i<4; i++)
179  NextState(Master);
180 
181  /* Modify the counters */
182  for (i=0; i<8; i++)
183  masterCtx_.c[i] ^= masterCtx_.x[(i+4)&0x7];
184 
185  /* Copy master instance to work instance */
186  for (i=0; i<8; i++) {
187  workCtx_.x[i] = masterCtx_.x[i];
188  workCtx_.c[i] = masterCtx_.c[i];
189  }
190  workCtx_.carry = masterCtx_.carry;
191 
192  if (iv) SetIV(iv);
193 }
194 
195 
196 /* Encrypt/decrypt a message of any size */
197 void Rabbit::Process(byte* output, const byte* input, word32 msglen)
198 {
199  /* Temporary variables */
200  word32 i;
201  byte buffer[16];
202 
203  /* Encrypt/decrypt all full blocks */
204  while (msglen >= 16) {
205  /* Iterate the system */
206  NextState(Work);
207 
208  /* Encrypt/decrypt 16 bytes of data */
209  *(word32*)(output+ 0) = *(word32*)(input+ 0) ^
210  LITTLE32(workCtx_.x[0] ^ (workCtx_.x[5]>>16) ^
211  U32V(workCtx_.x[3]<<16));
212  *(word32*)(output+ 4) = *(word32*)(input+ 4) ^
213  LITTLE32(workCtx_.x[2] ^ (workCtx_.x[7]>>16) ^
214  U32V(workCtx_.x[5]<<16));
215  *(word32*)(output+ 8) = *(word32*)(input+ 8) ^
216  LITTLE32(workCtx_.x[4] ^ (workCtx_.x[1]>>16) ^
217  U32V(workCtx_.x[7]<<16));
218  *(word32*)(output+12) = *(word32*)(input+12) ^
219  LITTLE32(workCtx_.x[6] ^ (workCtx_.x[3]>>16) ^
220  U32V(workCtx_.x[1]<<16));
221 
222  /* Increment pointers and decrement length */
223  input += 16;
224  output += 16;
225  msglen -= 16;
226  }
227 
228  /* Encrypt/decrypt remaining data */
229  if (msglen) {
230  /* Iterate the system */
231  NextState(Work);
232 
233  /* Generate 16 bytes of pseudo-random data */
234  *(word32*)(buffer+ 0) = LITTLE32(workCtx_.x[0] ^
235  (workCtx_.x[5]>>16) ^ U32V(workCtx_.x[3]<<16));
236  *(word32*)(buffer+ 4) = LITTLE32(workCtx_.x[2] ^
237  (workCtx_.x[7]>>16) ^ U32V(workCtx_.x[5]<<16));
238  *(word32*)(buffer+ 8) = LITTLE32(workCtx_.x[4] ^
239  (workCtx_.x[1]>>16) ^ U32V(workCtx_.x[7]<<16));
240  *(word32*)(buffer+12) = LITTLE32(workCtx_.x[6] ^
241  (workCtx_.x[3]>>16) ^ U32V(workCtx_.x[1]<<16));
242 
243  /* Encrypt/decrypt the data */
244  for (i=0; i<msglen; i++)
245  output[i] = input[i] ^ buffer[i];
246  }
247 }
248 
249 
250 } // namespace