MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hc128.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 "hc128.hpp"
22 
23 
24 
25 namespace TaoCrypt {
26 
27 
28 
29 
30 #ifdef BIG_ENDIAN_ORDER
31  #define LITTLE32(x) ByteReverse((word32)x)
32 #else
33  #define LITTLE32(x) (x)
34 #endif
35 
36 
37 /*h1 function*/
38 #define h1(x, y) { \
39  byte a,c; \
40  a = (byte) (x); \
41  c = (byte) ((x) >> 16); \
42  y = (T_[512+a])+(T_[512+256+c]); \
43 }
44 
45 /*h2 function*/
46 #define h2(x, y) { \
47  byte a,c; \
48  a = (byte) (x); \
49  c = (byte) ((x) >> 16); \
50  y = (T_[a])+(T_[256+c]); \
51 }
52 
53 /*one step of HC-128, update P and generate 32 bits keystream*/
54 #define step_P(u,v,a,b,c,d,n){ \
55  word32 tem0,tem1,tem2,tem3; \
56  h1((X_[(d)]),tem3); \
57  tem0 = rotrFixed((T_[(v)]),23); \
58  tem1 = rotrFixed((X_[(c)]),10); \
59  tem2 = rotrFixed((X_[(b)]),8); \
60  (T_[(u)]) += tem2+(tem0 ^ tem1); \
61  (X_[(a)]) = (T_[(u)]); \
62  (n) = tem3 ^ (T_[(u)]) ; \
63 }
64 
65 /*one step of HC-128, update Q and generate 32 bits keystream*/
66 #define step_Q(u,v,a,b,c,d,n){ \
67  word32 tem0,tem1,tem2,tem3; \
68  h2((Y_[(d)]),tem3); \
69  tem0 = rotrFixed((T_[(v)]),(32-23)); \
70  tem1 = rotrFixed((Y_[(c)]),(32-10)); \
71  tem2 = rotrFixed((Y_[(b)]),(32-8)); \
72  (T_[(u)]) += tem2 + (tem0 ^ tem1); \
73  (Y_[(a)]) = (T_[(u)]); \
74  (n) = tem3 ^ (T_[(u)]) ; \
75 }
76 
77 
78 /*16 steps of HC-128, generate 512 bits keystream*/
79 void HC128::GenerateKeystream(word32* keystream)
80 {
81  word32 cc,dd;
82  cc = counter1024_ & 0x1ff;
83  dd = (cc+16)&0x1ff;
84 
85  if (counter1024_ < 512)
86  {
87  counter1024_ = (counter1024_ + 16) & 0x3ff;
88  step_P(cc+0, cc+1, 0, 6, 13,4, keystream[0]);
89  step_P(cc+1, cc+2, 1, 7, 14,5, keystream[1]);
90  step_P(cc+2, cc+3, 2, 8, 15,6, keystream[2]);
91  step_P(cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
92  step_P(cc+4, cc+5, 4, 10,1, 8, keystream[4]);
93  step_P(cc+5, cc+6, 5, 11,2, 9, keystream[5]);
94  step_P(cc+6, cc+7, 6, 12,3, 10,keystream[6]);
95  step_P(cc+7, cc+8, 7, 13,4, 11,keystream[7]);
96  step_P(cc+8, cc+9, 8, 14,5, 12,keystream[8]);
97  step_P(cc+9, cc+10,9, 15,6, 13,keystream[9]);
98  step_P(cc+10,cc+11,10,0, 7, 14,keystream[10]);
99  step_P(cc+11,cc+12,11,1, 8, 15,keystream[11]);
100  step_P(cc+12,cc+13,12,2, 9, 0, keystream[12]);
101  step_P(cc+13,cc+14,13,3, 10,1, keystream[13]);
102  step_P(cc+14,cc+15,14,4, 11,2, keystream[14]);
103  step_P(cc+15,dd+0, 15,5, 12,3, keystream[15]);
104  }
105  else
106  {
107  counter1024_ = (counter1024_ + 16) & 0x3ff;
108  step_Q(512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
109  step_Q(512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
110  step_Q(512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
111  step_Q(512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
112  step_Q(512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
113  step_Q(512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
114  step_Q(512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
115  step_Q(512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
116  step_Q(512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
117  step_Q(512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
118  step_Q(512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
119  step_Q(512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
120  step_Q(512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
121  step_Q(512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
122  step_Q(512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
123  step_Q(512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
124  }
125 }
126 
127 
128 /* The following defines the initialization functions */
129 #define f1(x) (rotrFixed((x),7) ^ rotrFixed((x),18) ^ ((x) >> 3))
130 #define f2(x) (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
131 
132 /*update table P*/
133 #define update_P(u,v,a,b,c,d){ \
134  word32 tem0,tem1,tem2,tem3; \
135  tem0 = rotrFixed((T_[(v)]),23); \
136  tem1 = rotrFixed((X_[(c)]),10); \
137  tem2 = rotrFixed((X_[(b)]),8); \
138  h1((X_[(d)]),tem3); \
139  (T_[(u)]) = ((T_[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
140  (X_[(a)]) = (T_[(u)]); \
141 }
142 
143 /*update table Q*/
144 #define update_Q(u,v,a,b,c,d){ \
145  word32 tem0,tem1,tem2,tem3; \
146  tem0 = rotrFixed((T_[(v)]),(32-23)); \
147  tem1 = rotrFixed((Y_[(c)]),(32-10)); \
148  tem2 = rotrFixed((Y_[(b)]),(32-8)); \
149  h2((Y_[(d)]),tem3); \
150  (T_[(u)]) = ((T_[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
151  (Y_[(a)]) = (T_[(u)]); \
152 }
153 
154 /*16 steps of HC-128, without generating keystream, */
155 /*but use the outputs to update P and Q*/
156 void HC128::SetupUpdate() /*each time 16 steps*/
157 {
158  word32 cc,dd;
159  cc = counter1024_ & 0x1ff;
160  dd = (cc+16)&0x1ff;
161 
162  if (counter1024_ < 512)
163  {
164  counter1024_ = (counter1024_ + 16) & 0x3ff;
165  update_P(cc+0, cc+1, 0, 6, 13, 4);
166  update_P(cc+1, cc+2, 1, 7, 14, 5);
167  update_P(cc+2, cc+3, 2, 8, 15, 6);
168  update_P(cc+3, cc+4, 3, 9, 0, 7);
169  update_P(cc+4, cc+5, 4, 10,1, 8);
170  update_P(cc+5, cc+6, 5, 11,2, 9);
171  update_P(cc+6, cc+7, 6, 12,3, 10);
172  update_P(cc+7, cc+8, 7, 13,4, 11);
173  update_P(cc+8, cc+9, 8, 14,5, 12);
174  update_P(cc+9, cc+10,9, 15,6, 13);
175  update_P(cc+10,cc+11,10,0, 7, 14);
176  update_P(cc+11,cc+12,11,1, 8, 15);
177  update_P(cc+12,cc+13,12,2, 9, 0);
178  update_P(cc+13,cc+14,13,3, 10, 1);
179  update_P(cc+14,cc+15,14,4, 11, 2);
180  update_P(cc+15,dd+0, 15,5, 12, 3);
181  }
182  else
183  {
184  counter1024_ = (counter1024_ + 16) & 0x3ff;
185  update_Q(512+cc+0, 512+cc+1, 0, 6, 13, 4);
186  update_Q(512+cc+1, 512+cc+2, 1, 7, 14, 5);
187  update_Q(512+cc+2, 512+cc+3, 2, 8, 15, 6);
188  update_Q(512+cc+3, 512+cc+4, 3, 9, 0, 7);
189  update_Q(512+cc+4, 512+cc+5, 4, 10,1, 8);
190  update_Q(512+cc+5, 512+cc+6, 5, 11,2, 9);
191  update_Q(512+cc+6, 512+cc+7, 6, 12,3, 10);
192  update_Q(512+cc+7, 512+cc+8, 7, 13,4, 11);
193  update_Q(512+cc+8, 512+cc+9, 8, 14,5, 12);
194  update_Q(512+cc+9, 512+cc+10,9, 15,6, 13);
195  update_Q(512+cc+10,512+cc+11,10,0, 7, 14);
196  update_Q(512+cc+11,512+cc+12,11,1, 8, 15);
197  update_Q(512+cc+12,512+cc+13,12,2, 9, 0);
198  update_Q(512+cc+13,512+cc+14,13,3, 10, 1);
199  update_Q(512+cc+14,512+cc+15,14,4, 11, 2);
200  update_Q(512+cc+15,512+dd+0, 15,5, 12, 3);
201  }
202 }
203 
204 
205 /* for the 128-bit key: key[0]...key[15]
206 * key[0] is the least significant byte of ctx->key[0] (K_0);
207 * key[3] is the most significant byte of ctx->key[0] (K_0);
208 * ...
209 * key[12] is the least significant byte of ctx->key[3] (K_3)
210 * key[15] is the most significant byte of ctx->key[3] (K_3)
211 *
212 * for the 128-bit iv: iv[0]...iv[15]
213 * iv[0] is the least significant byte of ctx->iv[0] (IV_0);
214 * iv[3] is the most significant byte of ctx->iv[0] (IV_0);
215 * ...
216 * iv[12] is the least significant byte of ctx->iv[3] (IV_3)
217 * iv[15] is the most significant byte of ctx->iv[3] (IV_3)
218 */
219 
220 
221 
222 void HC128::SetIV(const byte* iv)
223 {
224  word32 i;
225 
226  for (i = 0; i < (128 >> 5); i++)
227  iv_[i] = LITTLE32(((word32*)iv)[i]);
228 
229  for (; i < 8; i++) iv_[i] = iv_[i-4];
230 
231  /* expand the key and IV into the table T */
232  /* (expand the key and IV into the table P and Q) */
233 
234  for (i = 0; i < 8; i++) T_[i] = key_[i];
235  for (i = 8; i < 16; i++) T_[i] = iv_[i-8];
236 
237  for (i = 16; i < (256+16); i++)
238  T_[i] = f2(T_[i-2]) + T_[i-7] + f1(T_[i-15]) + T_[i-16]+i;
239 
240  for (i = 0; i < 16; i++) T_[i] = T_[256+i];
241 
242  for (i = 16; i < 1024; i++)
243  T_[i] = f2(T_[i-2]) + T_[i-7] + f1(T_[i-15]) + T_[i-16]+256+i;
244 
245  /* initialize counter1024, X and Y */
246  counter1024_ = 0;
247  for (i = 0; i < 16; i++) X_[i] = T_[512-16+i];
248  for (i = 0; i < 16; i++) Y_[i] = T_[512+512-16+i];
249 
250  /* run the cipher 1024 steps before generating the output */
251  for (i = 0; i < 64; i++) SetupUpdate();
252 }
253 
254 
255 void HC128::SetKey(const byte* key, const byte* iv)
256 {
257  word32 i;
258 
259  /* Key size in bits 128 */
260  for (i = 0; i < (128 >> 5); i++)
261  key_[i] = LITTLE32(((word32*)key)[i]);
262 
263  for ( ; i < 8 ; i++) key_[i] = key_[i-4];
264 
265  SetIV(iv);
266 }
267 
268 
269 /* The following defines the encryption of data stream */
270 void HC128::Process(byte* output, const byte* input, word32 msglen)
271 {
272  word32 i, keystream[16];
273 
274  for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
275  {
276  GenerateKeystream(keystream);
277 
278  /* unroll loop */
279  ((word32*)output)[0] = ((word32*)input)[0] ^ LITTLE32(keystream[0]);
280  ((word32*)output)[1] = ((word32*)input)[1] ^ LITTLE32(keystream[1]);
281  ((word32*)output)[2] = ((word32*)input)[2] ^ LITTLE32(keystream[2]);
282  ((word32*)output)[3] = ((word32*)input)[3] ^ LITTLE32(keystream[3]);
283  ((word32*)output)[4] = ((word32*)input)[4] ^ LITTLE32(keystream[4]);
284  ((word32*)output)[5] = ((word32*)input)[5] ^ LITTLE32(keystream[5]);
285  ((word32*)output)[6] = ((word32*)input)[6] ^ LITTLE32(keystream[6]);
286  ((word32*)output)[7] = ((word32*)input)[7] ^ LITTLE32(keystream[7]);
287  ((word32*)output)[8] = ((word32*)input)[8] ^ LITTLE32(keystream[8]);
288  ((word32*)output)[9] = ((word32*)input)[9] ^ LITTLE32(keystream[9]);
289  ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
290  ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
291  ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
292  ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
293  ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
294  ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
295  }
296 
297  if (msglen > 0)
298  {
299  GenerateKeystream(keystream);
300 
301 #ifdef BIG_ENDIAN_ORDER
302  {
303  word32 wordsLeft = msglen / sizeof(word32);
304  if (msglen % sizeof(word32)) wordsLeft++;
305 
306  ByteReverse(keystream, keystream, wordsLeft * sizeof(word32));
307  }
308 #endif
309 
310  for (i = 0; i < msglen; i++)
311  output[i] = input[i] ^ ((byte*)keystream)[i];
312  }
313 
314 }
315 
316 
317 } // namespace