2 * Copyright (C) 2017 Denys Vlasenko
4 * Licensed under GPLv2, see file LICENSE in this source tree.
8 /* The part below is a section of matrixssl-3-7-2b-open/crypto/cryptolib.h
9 * Changes are flagged with //bbox
12 /******************************************************************************/
14 /******************************************************************************/
16 /******************************************************************************/
18 /* instrinsic rotate */
20 #pragma intrinsic(_lrotr,_lrotl)
21 #define ROR(x,n) _lrotr(x,n)
22 #define ROL(x,n) _lrotl(x,n)
24 /******************************************************************************/
25 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && \
26 !defined(INTEL_CC) && !defined(PS_NO_ASM)
28 static ALWAYS_INLINE unsigned ROL(unsigned word, int i)
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.
44 static ALWAYS_INLINE unsigned ROR(unsigned word, int i)
46 if (__builtin_constant_p(i)) { //box
58 /******************************************************************************/
61 /* rotates the hard way */
63 ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | \
64 (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & \
67 ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
68 ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
70 #endif /* 32-bit Rotates */
71 /******************************************************************************/
73 #ifdef HAVE_NATIVE_INT64
75 #define CONST64(n) n ## ui64
77 #define CONST64(n) n ## ULL
81 /******************************************************************************/
85 #if defined (ENDIAN_NEUTRAL)
86 #define STORE32L(x, y) { \
87 (y)[3] = (unsigned char)(((x)>>24)&255); \
88 (y)[2] = (unsigned char)(((x)>>16)&255); \
89 (y)[1] = (unsigned char)(((x)>>8)&255); \
90 (y)[0] = (unsigned char)((x)&255); \
93 #define LOAD32L(x, y) { \
94 x = ((unsigned long)((y)[3] & 255)<<24) | \
95 ((unsigned long)((y)[2] & 255)<<16) | \
96 ((unsigned long)((y)[1] & 255)<<8) | \
97 ((unsigned long)((y)[0] & 255)); \
100 #define STORE64L(x, y) { \
101 (y)[7] = (unsigned char)(((x)>>56)&255); \
102 (y)[6] = (unsigned char)(((x)>>48)&255); \
103 (y)[5] = (unsigned char)(((x)>>40)&255); \
104 (y)[4] = (unsigned char)(((x)>>32)&255); \
105 (y)[3] = (unsigned char)(((x)>>24)&255); \
106 (y)[2] = (unsigned char)(((x)>>16)&255); \
107 (y)[1] = (unsigned char)(((x)>>8)&255); \
108 (y)[0] = (unsigned char)((x)&255); \
111 #define LOAD64L(x, y) { \
112 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
113 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
114 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
115 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
118 #define STORE32H(x, y) { \
119 (y)[0] = (unsigned char)(((x)>>24)&255); \
120 (y)[1] = (unsigned char)(((x)>>16)&255); \
121 (y)[2] = (unsigned char)(((x)>>8)&255); \
122 (y)[3] = (unsigned char)((x)&255); \
125 #define LOAD32H(x, y) { \
126 x = ((unsigned long)((y)[0] & 255)<<24) | \
127 ((unsigned long)((y)[1] & 255)<<16) | \
128 ((unsigned long)((y)[2] & 255)<<8) | \
129 ((unsigned long)((y)[3] & 255)); \
132 #define STORE64H(x, y) { \
133 (y)[0] = (unsigned char)(((x)>>56)&255); \
134 (y)[1] = (unsigned char)(((x)>>48)&255); \
135 (y)[2] = (unsigned char)(((x)>>40)&255); \
136 (y)[3] = (unsigned char)(((x)>>32)&255); \
137 (y)[4] = (unsigned char)(((x)>>24)&255); \
138 (y)[5] = (unsigned char)(((x)>>16)&255); \
139 (y)[6] = (unsigned char)(((x)>>8)&255); \
140 (y)[7] = (unsigned char)((x)&255); \
143 #define LOAD64H(x, y) { \
144 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
145 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
146 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
147 (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); \
150 #endif /* ENDIAN_NEUTRAL */
153 #define STORE32H(x, y) { \
154 (y)[0] = (unsigned char)(((x)>>24)&255); \
155 (y)[1] = (unsigned char)(((x)>>16)&255); \
156 (y)[2] = (unsigned char)(((x)>>8)&255); \
157 (y)[3] = (unsigned char)((x)&255); \
160 #define LOAD32H(x, y) { \
161 x = ((unsigned long)((y)[0] & 255)<<24) | \
162 ((unsigned long)((y)[1] & 255)<<16) | \
163 ((unsigned long)((y)[2] & 255)<<8) | \
164 ((unsigned long)((y)[3] & 255)); \
167 #define STORE64H(x, y) { \
168 (y)[0] = (unsigned char)(((x)>>56)&255); \
169 (y)[1] = (unsigned char)(((x)>>48)&255); \
170 (y)[2] = (unsigned char)(((x)>>40)&255); \
171 (y)[3] = (unsigned char)(((x)>>32)&255); \
172 (y)[4] = (unsigned char)(((x)>>24)&255); \
173 (y)[5] = (unsigned char)(((x)>>16)&255); \
174 (y)[6] = (unsigned char)(((x)>>8)&255); \
175 (y)[7] = (unsigned char)((x)&255); \
178 #define LOAD64H(x, y) { \
179 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
180 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
181 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
182 (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); }
184 #ifdef ENDIAN_32BITWORD
185 #define STORE32L(x, y) { \
186 unsigned long __t = (x); memcpy(y, &__t, 4); \
189 #define LOAD32L(x, y) memcpy(&(x), y, 4);
191 #define STORE64L(x, y) { \
192 (y)[7] = (unsigned char)(((x)>>56)&255); \
193 (y)[6] = (unsigned char)(((x)>>48)&255); \
194 (y)[5] = (unsigned char)(((x)>>40)&255); \
195 (y)[4] = (unsigned char)(((x)>>32)&255); \
196 (y)[3] = (unsigned char)(((x)>>24)&255); \
197 (y)[2] = (unsigned char)(((x)>>16)&255); \
198 (y)[1] = (unsigned char)(((x)>>8)&255); \
199 (y)[0] = (unsigned char)((x)&255); \
202 #define LOAD64L(x, y) { \
203 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
204 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
205 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
206 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
209 #else /* 64-bit words then */
210 #define STORE32L(x, y) \
211 { unsigned long __t = (x); memcpy(y, &__t, 4); }
213 #define LOAD32L(x, y) \
214 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
216 #define STORE64L(x, y) \
217 { uint64 __t = (x); memcpy(y, &__t, 8); }
219 #define LOAD64L(x, y) \
220 { memcpy(&(x), y, 8); }
222 #endif /* ENDIAN_64BITWORD */
223 #endif /* ENDIAN_LITTLE */
226 #define STORE32L(x, y) { \
227 (y)[3] = (unsigned char)(((x)>>24)&255); \
228 (y)[2] = (unsigned char)(((x)>>16)&255); \
229 (y)[1] = (unsigned char)(((x)>>8)&255); \
230 (y)[0] = (unsigned char)((x)&255); \
233 #define LOAD32L(x, y) { \
234 x = ((unsigned long)((y)[3] & 255)<<24) | \
235 ((unsigned long)((y)[2] & 255)<<16) | \
236 ((unsigned long)((y)[1] & 255)<<8) | \
237 ((unsigned long)((y)[0] & 255)); \
240 #define STORE64L(x, y) { \
241 (y)[7] = (unsigned char)(((x)>>56)&255); \
242 (y)[6] = (unsigned char)(((x)>>48)&255); \
243 (y)[5] = (unsigned char)(((x)>>40)&255); \
244 (y)[4] = (unsigned char)(((x)>>32)&255); \
245 (y)[3] = (unsigned char)(((x)>>24)&255); \
246 (y)[2] = (unsigned char)(((x)>>16)&255); \
247 (y)[1] = (unsigned char)(((x)>>8)&255); \
248 (y)[0] = (unsigned char)((x)&255); \
251 #define LOAD64L(x, y) { \
252 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48) | \
253 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32) | \
254 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16) | \
255 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
258 #ifdef ENDIAN_32BITWORD
259 #define STORE32H(x, y) \
260 { unsigned int __t = (x); memcpy(y, &__t, 4); }
262 #define LOAD32H(x, y) memcpy(&(x), y, 4);
264 #define STORE64H(x, y) { \
265 (y)[0] = (unsigned char)(((x)>>56)&255); \
266 (y)[1] = (unsigned char)(((x)>>48)&255); \
267 (y)[2] = (unsigned char)(((x)>>40)&255); \
268 (y)[3] = (unsigned char)(((x)>>32)&255); \
269 (y)[4] = (unsigned char)(((x)>>24)&255); \
270 (y)[5] = (unsigned char)(((x)>>16)&255); \
271 (y)[6] = (unsigned char)(((x)>>8)&255); \
272 (y)[7] = (unsigned char)((x)&255); \
275 #define LOAD64H(x, y) { \
276 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48)| \
277 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32)| \
278 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16)| \
279 (((uint64)((y)[6] & 255))<<8)| (((uint64)((y)[7] & 255))); \
282 #else /* 64-bit words then */
284 #define STORE32H(x, y) \
285 { unsigned long __t = (x); memcpy(y, &__t, 4); }
287 #define LOAD32H(x, y) \
288 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
290 #define STORE64H(x, y) \
291 { uint64 __t = (x); memcpy(y, &__t, 8); }
293 #define LOAD64H(x, y) \
294 { memcpy(&(x), y, 8); }
296 #endif /* ENDIAN_64BITWORD */
297 #endif /* ENDIAN_BIG */
299 #ifdef HAVE_NATIVE_INT64
300 #define ROL64c(x, y) \
301 ( (((x)<<((uint64)(y)&63)) | \
302 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
304 #define ROR64c(x, y) \
305 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)(y)&CONST64(63))) | \
306 ((x)<<((uint64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
307 #endif /* HAVE_NATIVE_INT64 */
308 /******************************************************************************/
312 /* The part below is taken almost verbatim from matrixssl-3-7-2b-open/crypto/symmetric/.
313 * Changes are flagged with //bbox
318 * @version 33ef80f (HEAD, tag: MATRIXSSL-3-7-2-OPEN, tag: MATRIXSSL-3-7-2-COMM, origin/master, origin/HEAD, master)
320 * Header for internal symmetric key cryptography support.
323 * Copyright (c) 2013-2015 INSIDE Secure Corporation
324 * Copyright (c) PeerSec Networks, 2002-2011
325 * All Rights Reserved
327 * The latest version of this code is available at http://www.matrixssl.org
329 * This software is open source; you can redistribute it and/or modify
330 * it under the terms of the GNU General Public License as published by
331 * the Free Software Foundation; either version 2 of the License, or
332 * (at your option) any later version.
334 * This General Public License does NOT permit incorporating this software
335 * into proprietary programs. If you are unable to comply with the GPL, a
336 * commercial license for this software may be purchased from INSIDE at
337 * http://www.insidesecure.com/eng/Company/Locations
339 * This program is distributed in WITHOUT ANY WARRANTY; without even the
340 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
341 * See the GNU General Public License for more details.
343 * You should have received a copy of the GNU General Public License
344 * along with this program; if not, write to the Free Software
345 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
346 * http://www.gnu.org/copyleft/gpl.html
348 /******************************************************************************/
350 #ifndef _h_PS_SYMMETRIC
351 #define _h_PS_SYMMETRIC
353 /******************************************************************************/
355 /******************************************************************************/
358 #ifndef USE_AES_CBC_EXTERNAL
360 uint32 eK[64], dK[64];
366 unsigned char IV[16];
368 #if defined(USE_AES_GCM) || defined(USE_AES_CCM)
369 unsigned char EncCtr[16];
370 unsigned char CtrBlock[16];
373 unsigned char gInit[16];
375 unsigned char Hash_SubKey[16];
376 uint32 ProcessedBitCount[4];
377 uint32 InputBufferCount;
378 uint32 OutputBufferCount;
381 unsigned char Buffer[128];
382 uint32 BufferAlignment;
384 #endif /* USE_AES_GCM */
386 uint32_t ccmTagTemp[16 / sizeof(uint32_t)]; /* 32 */
389 /* Used for formatting IV. */
390 uint8_t Temporary[16];
391 /* Used for processing Mac. */
394 #endif /* USE_AES_CCM */
396 #endif /* USE_AES_CBC_EXTERNAL */
401 #define SSL_IDEA_KEY_LEN 16
402 #define SSL_IDEA_IV_LEN 8
403 #define SSL_IDEA_BLOCK_LEN 8
406 uint16 key_schedule[52];
412 short for_encryption;
416 /******************************************************************************/
418 /******************************************************************************/
420 /******************************************************************************/
421 #define SSL_SEED_KEY_LEN 16
422 #define SSL_SEED_IV_LEN 16
426 uint32 K[32], dK[32];
431 unsigned char IV[16];
435 #endif /* USE_SEED */
436 /******************************************************************************/
438 /******************************************************************************/
439 #if defined(USE_3DES) || defined(USE_DES)
440 /******************************************************************************/
441 #define DES3_KEY_LEN 24
442 #define DES3_IV_LEN 8
443 #define DES_KEY_LEN 8
446 uint32 ek[3][32], dk[3][32];
450 A block cipher CBC structure
458 #endif /* USE_3DES || USE_DES */
459 /******************************************************************************/
461 /******************************************************************************/
464 unsigned char state[256];
469 #endif /* USE_ARC4 */
470 /******************************************************************************/
482 /******************************************************************************/
483 /* Universal types and defines */
484 /******************************************************************************/
485 #define MAXBLOCKSIZE 24
508 #define byte(x, n) (((x) >> (8 * (n))) & 255)
510 #endif /* _h_PS_SYMMETRIC */
511 /******************************************************************************/