8488b437e69b9aff09378383bf002aafd71fecb8
[oweals/busybox.git] / networking / tls_symmetric.h
1 /*
2  * Copyright (C) 2017 Denys Vlasenko
3  *
4  * Licensed under GPLv2, see file LICENSE in this source tree.
5  */
6
7
8 /* The part below is a section of matrixssl-3-7-2b-open/crypto/cryptolib.h
9  * Changes are flagged with //bbox
10  */
11
12 /******************************************************************************/
13 /* 32-bit Rotates */
14 /******************************************************************************/
15 #if defined(_MSC_VER)
16 /******************************************************************************/
17
18 /* instrinsic rotate */
19 #include <stdlib.h>
20 #pragma intrinsic(_lrotr,_lrotl)
21 #define ROR(x,n) _lrotr(x,n)
22 #define ROL(x,n) _lrotl(x,n)
23
24 /******************************************************************************/
25 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && \
26                 !defined(INTEL_CC) && !defined(PS_NO_ASM)
27
28 static ALWAYS_INLINE unsigned ROL(unsigned word, int i)
29 {
30  if (__builtin_constant_p(i)) //box
31    // Rotates by constant use fewer registers,
32    // and on many Intel CPUs rotates by %cl take 2 cycles, not 1.
33    asm ("roll %2,%0" //box
34           :"=r" (word)
35           :"0" (word),"i" (i));
36  else  //box
37    asm ("roll %%cl,%0"
38           :"=r" (word)
39           :"0" (word),"c" (i));
40    return word;
41 }
42
43 static ALWAYS_INLINE unsigned ROR(unsigned word, int i)
44 {
45  if (__builtin_constant_p(i)) //box
46    asm ("rorl %2,%0" //box
47           :"=r" (word)
48           :"0" (word),"i" (i));
49  else //box
50    asm ("rorl %%cl,%0"
51           :"=r" (word)
52           :"0" (word),"c" (i));
53    return word;
54 }
55
56 /******************************************************************************/
57 #else
58
59 /* rotates the hard way */
60 #define ROL(x, y) \
61         ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | \
62         (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & \
63         0xFFFFFFFFUL)
64 #define ROR(x, y) \
65         ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
66         ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
67
68 #endif /* 32-bit Rotates */
69 /******************************************************************************/
70
71 #ifdef HAVE_NATIVE_INT64
72 #ifdef _MSC_VER
73         #define CONST64(n) n ## ui64
74 #else
75         #define CONST64(n) n ## ULL
76 #endif
77 #endif
78
79 /******************************************************************************/
80 /*
81         Endian helper macros
82  */
83 #if defined (ENDIAN_NEUTRAL)
84 #define STORE32L(x, y) { \
85 (y)[3] = (unsigned char)(((x)>>24)&255); \
86 (y)[2] = (unsigned char)(((x)>>16)&255);  \
87 (y)[1] = (unsigned char)(((x)>>8)&255); \
88 (y)[0] = (unsigned char)((x)&255); \
89 }
90
91 #define LOAD32L(x, y) { \
92 x = ((unsigned long)((y)[3] & 255)<<24) | \
93 ((unsigned long)((y)[2] & 255)<<16) | \
94 ((unsigned long)((y)[1] & 255)<<8)  | \
95 ((unsigned long)((y)[0] & 255)); \
96 }
97
98 #define STORE64L(x, y) { \
99 (y)[7] = (unsigned char)(((x)>>56)&255); \
100 (y)[6] = (unsigned char)(((x)>>48)&255); \
101 (y)[5] = (unsigned char)(((x)>>40)&255); \
102 (y)[4] = (unsigned char)(((x)>>32)&255); \
103 (y)[3] = (unsigned char)(((x)>>24)&255); \
104 (y)[2] = (unsigned char)(((x)>>16)&255); \
105 (y)[1] = (unsigned char)(((x)>>8)&255); \
106 (y)[0] = (unsigned char)((x)&255); \
107 }
108
109 #define LOAD64L(x, y) { \
110 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
111 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
112 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
113 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
114 }
115
116 #define STORE32H(x, y) { \
117 (y)[0] = (unsigned char)(((x)>>24)&255); \
118 (y)[1] = (unsigned char)(((x)>>16)&255); \
119 (y)[2] = (unsigned char)(((x)>>8)&255); \
120 (y)[3] = (unsigned char)((x)&255); \
121 }
122
123 #define LOAD32H(x, y) { \
124 x = ((unsigned long)((y)[0] & 255)<<24) | \
125 ((unsigned long)((y)[1] & 255)<<16) | \
126 ((unsigned long)((y)[2] & 255)<<8)  | \
127 ((unsigned long)((y)[3] & 255)); \
128 }
129
130 #define STORE64H(x, y) { \
131 (y)[0] = (unsigned char)(((x)>>56)&255); \
132 (y)[1] = (unsigned char)(((x)>>48)&255); \
133 (y)[2] = (unsigned char)(((x)>>40)&255); \
134 (y)[3] = (unsigned char)(((x)>>32)&255); \
135 (y)[4] = (unsigned char)(((x)>>24)&255); \
136 (y)[5] = (unsigned char)(((x)>>16)&255); \
137 (y)[6] = (unsigned char)(((x)>>8)&255); \
138 (y)[7] = (unsigned char)((x)&255); \
139 }
140
141 #define LOAD64H(x, y) { \
142 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
143 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
144 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
145 (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); \
146 }
147
148 #endif /* ENDIAN_NEUTRAL */
149
150 #ifdef ENDIAN_LITTLE
151 #define STORE32H(x, y) { \
152 (y)[0] = (unsigned char)(((x)>>24)&255); \
153 (y)[1] = (unsigned char)(((x)>>16)&255); \
154 (y)[2] = (unsigned char)(((x)>>8)&255); \
155 (y)[3] = (unsigned char)((x)&255); \
156 }
157
158 #define LOAD32H(x, y) { \
159 x = ((unsigned long)((y)[0] & 255)<<24) | \
160 ((unsigned long)((y)[1] & 255)<<16) | \
161 ((unsigned long)((y)[2] & 255)<<8)  | \
162 ((unsigned long)((y)[3] & 255)); \
163 }
164
165 #define STORE64H(x, y) { \
166 (y)[0] = (unsigned char)(((x)>>56)&255); \
167 (y)[1] = (unsigned char)(((x)>>48)&255); \
168 (y)[2] = (unsigned char)(((x)>>40)&255); \
169 (y)[3] = (unsigned char)(((x)>>32)&255); \
170 (y)[4] = (unsigned char)(((x)>>24)&255); \
171 (y)[5] = (unsigned char)(((x)>>16)&255); \
172 (y)[6] = (unsigned char)(((x)>>8)&255); \
173 (y)[7] = (unsigned char)((x)&255); \
174 }
175
176 #define LOAD64H(x, y) { \
177 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
178 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
179 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
180 (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); }
181
182 #ifdef ENDIAN_32BITWORD
183 #define STORE32L(x, y) { \
184 unsigned long __t = (x); memcpy(y, &__t, 4); \
185 }
186
187 #define LOAD32L(x, y)  memcpy(&(x), y, 4);
188
189 #define STORE64L(x, y) { \
190 (y)[7] = (unsigned char)(((x)>>56)&255); \
191 (y)[6] = (unsigned char)(((x)>>48)&255); \
192 (y)[5] = (unsigned char)(((x)>>40)&255); \
193 (y)[4] = (unsigned char)(((x)>>32)&255); \
194 (y)[3] = (unsigned char)(((x)>>24)&255); \
195 (y)[2] = (unsigned char)(((x)>>16)&255); \
196 (y)[1] = (unsigned char)(((x)>>8)&255); \
197 (y)[0] = (unsigned char)((x)&255); \
198 }
199
200 #define LOAD64L(x, y) { \
201 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
202 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
203 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
204 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
205 }
206
207 #else /* 64-bit words then  */
208 #define STORE32L(x, y) \
209 { unsigned long __t = (x); memcpy(y, &__t, 4); }
210
211 #define LOAD32L(x, y) \
212 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
213
214 #define STORE64L(x, y) \
215 { uint64 __t = (x); memcpy(y, &__t, 8); }
216
217 #define LOAD64L(x, y) \
218 { memcpy(&(x), y, 8); }
219
220 #endif /* ENDIAN_64BITWORD */
221 #endif /* ENDIAN_LITTLE */
222
223 #ifdef ENDIAN_BIG
224 #define STORE32L(x, y) { \
225 (y)[3] = (unsigned char)(((x)>>24)&255); \
226 (y)[2] = (unsigned char)(((x)>>16)&255); \
227 (y)[1] = (unsigned char)(((x)>>8)&255); \
228 (y)[0] = (unsigned char)((x)&255); \
229 }
230
231 #define LOAD32L(x, y) { \
232 x = ((unsigned long)((y)[3] & 255)<<24) | \
233 ((unsigned long)((y)[2] & 255)<<16) | \
234 ((unsigned long)((y)[1] & 255)<<8)  | \
235 ((unsigned long)((y)[0] & 255)); \
236 }
237
238 #define STORE64L(x, y) { \
239 (y)[7] = (unsigned char)(((x)>>56)&255); \
240 (y)[6] = (unsigned char)(((x)>>48)&255); \
241 (y)[5] = (unsigned char)(((x)>>40)&255); \
242 (y)[4] = (unsigned char)(((x)>>32)&255); \
243 (y)[3] = (unsigned char)(((x)>>24)&255); \
244 (y)[2] = (unsigned char)(((x)>>16)&255); \
245 (y)[1] = (unsigned char)(((x)>>8)&255); \
246 (y)[0] = (unsigned char)((x)&255); \
247 }
248
249 #define LOAD64L(x, y) { \
250 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48) | \
251 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32) | \
252 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16) | \
253 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
254 }
255
256 #ifdef ENDIAN_32BITWORD
257 #define STORE32H(x, y) \
258 { unsigned int __t = (x); memcpy(y, &__t, 4); }
259
260 #define LOAD32H(x, y) memcpy(&(x), y, 4);
261
262 #define STORE64H(x, y) { \
263 (y)[0] = (unsigned char)(((x)>>56)&255); \
264 (y)[1] = (unsigned char)(((x)>>48)&255); \
265 (y)[2] = (unsigned char)(((x)>>40)&255); \
266 (y)[3] = (unsigned char)(((x)>>32)&255); \
267 (y)[4] = (unsigned char)(((x)>>24)&255); \
268 (y)[5] = (unsigned char)(((x)>>16)&255); \
269 (y)[6] = (unsigned char)(((x)>>8)&255); \
270 (y)[7] = (unsigned char)((x)&255); \
271 }
272
273 #define LOAD64H(x, y) { \
274 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48)| \
275 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32)| \
276 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16)| \
277 (((uint64)((y)[6] & 255))<<8)| (((uint64)((y)[7] & 255))); \
278 }
279
280 #else /* 64-bit words then  */
281
282 #define STORE32H(x, y) \
283 { unsigned long __t = (x); memcpy(y, &__t, 4); }
284
285 #define LOAD32H(x, y) \
286 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
287
288 #define STORE64H(x, y) \
289 { uint64 __t = (x); memcpy(y, &__t, 8); }
290
291 #define LOAD64H(x, y) \
292 { memcpy(&(x), y, 8); }
293
294 #endif /* ENDIAN_64BITWORD */
295 #endif /* ENDIAN_BIG */
296
297 #ifdef HAVE_NATIVE_INT64
298 #define ROL64c(x, y) \
299 ( (((x)<<((uint64)(y)&63)) | \
300 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
301
302 #define ROR64c(x, y) \
303 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)(y)&CONST64(63))) | \
304 ((x)<<((uint64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
305 #endif /* HAVE_NATIVE_INT64 */
306 /******************************************************************************/
307
308
309
310 /* The part below is taken almost verbatim from matrixssl-3-7-2b-open/crypto/symmetric/.
311  * Changes are flagged with //bbox
312  */
313
314 /**
315  *      @file    symmetric.h
316  *      @version 33ef80f (HEAD, tag: MATRIXSSL-3-7-2-OPEN, tag: MATRIXSSL-3-7-2-COMM, origin/master, origin/HEAD, master)
317  *
318  *      Header for internal symmetric key cryptography support.
319  */
320 /*
321  *      Copyright (c) 2013-2015 INSIDE Secure Corporation
322  *      Copyright (c) PeerSec Networks, 2002-2011
323  *      All Rights Reserved
324  *
325  *      The latest version of this code is available at http://www.matrixssl.org
326  *
327  *      This software is open source; you can redistribute it and/or modify
328  *      it under the terms of the GNU General Public License as published by
329  *      the Free Software Foundation; either version 2 of the License, or
330  *      (at your option) any later version.
331  *
332  *      This General Public License does NOT permit incorporating this software
333  *      into proprietary programs.  If you are unable to comply with the GPL, a
334  *      commercial license for this software may be purchased from INSIDE at
335  *      http://www.insidesecure.com/eng/Company/Locations
336  *
337  *      This program is distributed in WITHOUT ANY WARRANTY; without even the
338  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
339  *      See the GNU General Public License for more details.
340  *
341  *      You should have received a copy of the GNU General Public License
342  *      along with this program; if not, write to the Free Software
343  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
344  *      http://www.gnu.org/copyleft/gpl.html
345  */
346 /******************************************************************************/
347
348 #ifndef _h_PS_SYMMETRIC
349 #define _h_PS_SYMMETRIC
350
351 /******************************************************************************/
352 #ifdef USE_AES
353 /******************************************************************************/
354
355
356 #ifndef USE_AES_CBC_EXTERNAL
357 typedef struct {
358         uint32 eK[64], dK[64];
359         int32 Nr;
360 } psAesKey_t;
361
362 typedef struct {
363         int32                   blocklen;
364         unsigned char   IV[16];
365         psAesKey_t              key;
366 #if defined(USE_AES_GCM) || defined(USE_AES_CCM)
367         unsigned char   EncCtr[16];
368         unsigned char   CtrBlock[16];
369 #endif
370 #ifdef USE_AES_GCM
371         unsigned char   gInit[16];
372         uint32                  TagTemp[4];
373         unsigned char   Hash_SubKey[16];
374         uint32                  ProcessedBitCount[4];
375         uint32                  InputBufferCount;
376         uint32                  OutputBufferCount;
377         union
378         {
379                 unsigned char Buffer[128];
380                 uint32 BufferAlignment;
381         } Input;
382 #endif /* USE_AES_GCM */
383 #ifdef USE_AES_CCM
384         uint32_t ccmTagTemp[16 / sizeof(uint32_t)]; /* 32 */
385         union
386         {
387                 /* Used for formatting IV. */
388                 uint8_t Temporary[16];
389                 /* Used for processing Mac. */
390                 uint8_t Y0[16];
391         } u; /* 48 */
392 #endif /* USE_AES_CCM */
393 } psAesCipher_t;
394 #endif /* USE_AES_CBC_EXTERNAL */
395
396 #endif /* USE_AES */
397
398 #ifdef USE_IDEA
399 #define SSL_IDEA_KEY_LEN        16
400 #define SSL_IDEA_IV_LEN         8
401 #define SSL_IDEA_BLOCK_LEN      8
402
403 typedef struct {
404         uint16  key_schedule[52];
405 } psIdeaKey_t;
406
407 typedef struct {
408         psIdeaKey_t             key;
409         uint32                  IV[2];
410         short                   for_encryption;
411         short                   inverted;
412 } idea_CBC;
413 #endif
414 /******************************************************************************/
415
416 /******************************************************************************/
417 #ifdef USE_SEED
418 /******************************************************************************/
419 #define SSL_SEED_KEY_LEN        16
420 #define SSL_SEED_IV_LEN         16
421
422
423 typedef struct {
424         uint32 K[32], dK[32];
425 } psSeedKey_t;
426
427 typedef struct {
428         int32                   blocklen;
429         unsigned char   IV[16];
430         psSeedKey_t             key;
431 } seed_CBC;
432
433 #endif /* USE_SEED */
434 /******************************************************************************/
435
436 /******************************************************************************/
437 #if defined(USE_3DES) || defined(USE_DES)
438 /******************************************************************************/
439 #define DES3_KEY_LEN    24
440 #define DES3_IV_LEN             8
441 #define DES_KEY_LEN             8
442
443 typedef struct {
444         uint32 ek[3][32], dk[3][32];
445 } psDes3Key_t;
446
447 /*
448         A block cipher CBC structure
449  */
450 typedef struct {
451         int32                           blocklen;
452         unsigned char           IV[8];
453         psDes3Key_t                     key;
454 } des3_CBC;
455
456 #endif /* USE_3DES || USE_DES */
457 /******************************************************************************/
458
459 /******************************************************************************/
460 #ifdef USE_ARC4
461 typedef struct {
462         unsigned char   state[256];
463         uint32  byteCount;
464         unsigned char   x;
465         unsigned char   y;
466 } psRc4Key_t;
467 #endif /* USE_ARC4 */
468 /******************************************************************************/
469 #ifdef USE_RC2
470 typedef struct {
471         unsigned xkey[64];
472 } psRc2Key_t;
473
474 typedef struct {
475         int32                           blocklen;
476         unsigned char           IV[8];
477         psRc2Key_t                      key;
478 } rc2_CBC;
479 #endif /* USE_RC2 */
480 /******************************************************************************/
481 /*      Universal types and defines */
482 /******************************************************************************/
483 #define MAXBLOCKSIZE    24
484
485 typedef union {
486 #ifdef USE_RC2
487         rc2_CBC         rc2;
488 #endif
489 #ifdef USE_ARC4
490         psRc4Key_t      arc4;
491 #endif
492 #ifdef USE_3DES
493         des3_CBC        des3;
494 #endif
495 #ifdef USE_AES
496         psAesCipher_t   aes;
497 #endif
498 #ifdef USE_SEED
499         seed_CBC        seed;
500 #endif
501 #ifdef USE_IDEA
502         idea_CBC        idea;
503 #endif
504 } psCipherContext_t;
505
506 #define byte(x, n) (((x) >> (8 * (n))) & 255)
507
508 #endif /* _h_PS_SYMMETRIC */
509 /******************************************************************************/