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
11 * Take a look at "roll %%cl" part... rotates by constant use fewer registers,
12 * and on many Intel CPUs rotates by %cl are slower: they take 2 cycles, not 1.
15 /******************************************************************************/
17 /******************************************************************************/
19 /******************************************************************************/
21 /* instrinsic rotate */
23 #pragma intrinsic(_lrotr,_lrotl)
24 #define ROR(x,n) _lrotr(x,n)
25 #define ROL(x,n) _lrotl(x,n)
27 /******************************************************************************/
28 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && \
29 !defined(INTEL_CC) && !defined(PS_NO_ASM)
31 static inline unsigned ROL(unsigned word, int i)
39 static inline unsigned ROR(unsigned word, int i)
47 /******************************************************************************/
50 /* rotates the hard way */
52 ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | \
53 (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & \
56 ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
57 ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
59 #endif /* 32-bit Rotates */
60 /******************************************************************************/
62 #ifdef HAVE_NATIVE_INT64
64 #define CONST64(n) n ## ui64
66 #define CONST64(n) n ## ULL
70 /******************************************************************************/
74 #if defined (ENDIAN_NEUTRAL)
75 #define STORE32L(x, y) { \
76 (y)[3] = (unsigned char)(((x)>>24)&255); \
77 (y)[2] = (unsigned char)(((x)>>16)&255); \
78 (y)[1] = (unsigned char)(((x)>>8)&255); \
79 (y)[0] = (unsigned char)((x)&255); \
82 #define LOAD32L(x, y) { \
83 x = ((unsigned long)((y)[3] & 255)<<24) | \
84 ((unsigned long)((y)[2] & 255)<<16) | \
85 ((unsigned long)((y)[1] & 255)<<8) | \
86 ((unsigned long)((y)[0] & 255)); \
89 #define STORE64L(x, y) { \
90 (y)[7] = (unsigned char)(((x)>>56)&255); \
91 (y)[6] = (unsigned char)(((x)>>48)&255); \
92 (y)[5] = (unsigned char)(((x)>>40)&255); \
93 (y)[4] = (unsigned char)(((x)>>32)&255); \
94 (y)[3] = (unsigned char)(((x)>>24)&255); \
95 (y)[2] = (unsigned char)(((x)>>16)&255); \
96 (y)[1] = (unsigned char)(((x)>>8)&255); \
97 (y)[0] = (unsigned char)((x)&255); \
100 #define LOAD64L(x, y) { \
101 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
102 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
103 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
104 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
107 #define STORE32H(x, y) { \
108 (y)[0] = (unsigned char)(((x)>>24)&255); \
109 (y)[1] = (unsigned char)(((x)>>16)&255); \
110 (y)[2] = (unsigned char)(((x)>>8)&255); \
111 (y)[3] = (unsigned char)((x)&255); \
114 #define LOAD32H(x, y) { \
115 x = ((unsigned long)((y)[0] & 255)<<24) | \
116 ((unsigned long)((y)[1] & 255)<<16) | \
117 ((unsigned long)((y)[2] & 255)<<8) | \
118 ((unsigned long)((y)[3] & 255)); \
121 #define STORE64H(x, y) { \
122 (y)[0] = (unsigned char)(((x)>>56)&255); \
123 (y)[1] = (unsigned char)(((x)>>48)&255); \
124 (y)[2] = (unsigned char)(((x)>>40)&255); \
125 (y)[3] = (unsigned char)(((x)>>32)&255); \
126 (y)[4] = (unsigned char)(((x)>>24)&255); \
127 (y)[5] = (unsigned char)(((x)>>16)&255); \
128 (y)[6] = (unsigned char)(((x)>>8)&255); \
129 (y)[7] = (unsigned char)((x)&255); \
132 #define LOAD64H(x, y) { \
133 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
134 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
135 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
136 (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); \
139 #endif /* ENDIAN_NEUTRAL */
142 #define STORE32H(x, y) { \
143 (y)[0] = (unsigned char)(((x)>>24)&255); \
144 (y)[1] = (unsigned char)(((x)>>16)&255); \
145 (y)[2] = (unsigned char)(((x)>>8)&255); \
146 (y)[3] = (unsigned char)((x)&255); \
149 #define LOAD32H(x, y) { \
150 x = ((unsigned long)((y)[0] & 255)<<24) | \
151 ((unsigned long)((y)[1] & 255)<<16) | \
152 ((unsigned long)((y)[2] & 255)<<8) | \
153 ((unsigned long)((y)[3] & 255)); \
156 #define STORE64H(x, y) { \
157 (y)[0] = (unsigned char)(((x)>>56)&255); \
158 (y)[1] = (unsigned char)(((x)>>48)&255); \
159 (y)[2] = (unsigned char)(((x)>>40)&255); \
160 (y)[3] = (unsigned char)(((x)>>32)&255); \
161 (y)[4] = (unsigned char)(((x)>>24)&255); \
162 (y)[5] = (unsigned char)(((x)>>16)&255); \
163 (y)[6] = (unsigned char)(((x)>>8)&255); \
164 (y)[7] = (unsigned char)((x)&255); \
167 #define LOAD64H(x, y) { \
168 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48) | \
169 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32) | \
170 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16) | \
171 (((uint64)((y)[6] & 255))<<8)|(((uint64)((y)[7] & 255))); }
173 #ifdef ENDIAN_32BITWORD
174 #define STORE32L(x, y) { \
175 unsigned long __t = (x); memcpy(y, &__t, 4); \
178 #define LOAD32L(x, y) memcpy(&(x), y, 4);
180 #define STORE64L(x, y) { \
181 (y)[7] = (unsigned char)(((x)>>56)&255); \
182 (y)[6] = (unsigned char)(((x)>>48)&255); \
183 (y)[5] = (unsigned char)(((x)>>40)&255); \
184 (y)[4] = (unsigned char)(((x)>>32)&255); \
185 (y)[3] = (unsigned char)(((x)>>24)&255); \
186 (y)[2] = (unsigned char)(((x)>>16)&255); \
187 (y)[1] = (unsigned char)(((x)>>8)&255); \
188 (y)[0] = (unsigned char)((x)&255); \
191 #define LOAD64L(x, y) { \
192 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48)| \
193 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32)| \
194 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16)| \
195 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
198 #else /* 64-bit words then */
199 #define STORE32L(x, y) \
200 { unsigned long __t = (x); memcpy(y, &__t, 4); }
202 #define LOAD32L(x, y) \
203 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
205 #define STORE64L(x, y) \
206 { uint64 __t = (x); memcpy(y, &__t, 8); }
208 #define LOAD64L(x, y) \
209 { memcpy(&(x), y, 8); }
211 #endif /* ENDIAN_64BITWORD */
212 #endif /* ENDIAN_LITTLE */
215 #define STORE32L(x, y) { \
216 (y)[3] = (unsigned char)(((x)>>24)&255); \
217 (y)[2] = (unsigned char)(((x)>>16)&255); \
218 (y)[1] = (unsigned char)(((x)>>8)&255); \
219 (y)[0] = (unsigned char)((x)&255); \
222 #define LOAD32L(x, y) { \
223 x = ((unsigned long)((y)[3] & 255)<<24) | \
224 ((unsigned long)((y)[2] & 255)<<16) | \
225 ((unsigned long)((y)[1] & 255)<<8) | \
226 ((unsigned long)((y)[0] & 255)); \
229 #define STORE64L(x, y) { \
230 (y)[7] = (unsigned char)(((x)>>56)&255); \
231 (y)[6] = (unsigned char)(((x)>>48)&255); \
232 (y)[5] = (unsigned char)(((x)>>40)&255); \
233 (y)[4] = (unsigned char)(((x)>>32)&255); \
234 (y)[3] = (unsigned char)(((x)>>24)&255); \
235 (y)[2] = (unsigned char)(((x)>>16)&255); \
236 (y)[1] = (unsigned char)(((x)>>8)&255); \
237 (y)[0] = (unsigned char)((x)&255); \
240 #define LOAD64L(x, y) { \
241 x = (((uint64)((y)[7] & 255))<<56)|(((uint64)((y)[6] & 255))<<48) | \
242 (((uint64)((y)[5] & 255))<<40)|(((uint64)((y)[4] & 255))<<32) | \
243 (((uint64)((y)[3] & 255))<<24)|(((uint64)((y)[2] & 255))<<16) | \
244 (((uint64)((y)[1] & 255))<<8)|(((uint64)((y)[0] & 255))); \
247 #ifdef ENDIAN_32BITWORD
248 #define STORE32H(x, y) \
249 { unsigned int __t = (x); memcpy(y, &__t, 4); }
251 #define LOAD32H(x, y) memcpy(&(x), y, 4);
253 #define STORE64H(x, y) { \
254 (y)[0] = (unsigned char)(((x)>>56)&255); \
255 (y)[1] = (unsigned char)(((x)>>48)&255); \
256 (y)[2] = (unsigned char)(((x)>>40)&255); \
257 (y)[3] = (unsigned char)(((x)>>32)&255); \
258 (y)[4] = (unsigned char)(((x)>>24)&255); \
259 (y)[5] = (unsigned char)(((x)>>16)&255); \
260 (y)[6] = (unsigned char)(((x)>>8)&255); \
261 (y)[7] = (unsigned char)((x)&255); \
264 #define LOAD64H(x, y) { \
265 x = (((uint64)((y)[0] & 255))<<56)|(((uint64)((y)[1] & 255))<<48)| \
266 (((uint64)((y)[2] & 255))<<40)|(((uint64)((y)[3] & 255))<<32)| \
267 (((uint64)((y)[4] & 255))<<24)|(((uint64)((y)[5] & 255))<<16)| \
268 (((uint64)((y)[6] & 255))<<8)| (((uint64)((y)[7] & 255))); \
271 #else /* 64-bit words then */
273 #define STORE32H(x, y) \
274 { unsigned long __t = (x); memcpy(y, &__t, 4); }
276 #define LOAD32H(x, y) \
277 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
279 #define STORE64H(x, y) \
280 { uint64 __t = (x); memcpy(y, &__t, 8); }
282 #define LOAD64H(x, y) \
283 { memcpy(&(x), y, 8); }
285 #endif /* ENDIAN_64BITWORD */
286 #endif /* ENDIAN_BIG */
288 #ifdef HAVE_NATIVE_INT64
289 #define ROL64c(x, y) \
290 ( (((x)<<((uint64)(y)&63)) | \
291 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
293 #define ROR64c(x, y) \
294 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((uint64)(y)&CONST64(63))) | \
295 ((x)<<((uint64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
296 #endif /* HAVE_NATIVE_INT64 */
297 /******************************************************************************/
301 /* The part below is taken almost verbatim from matrixssl-3-7-2b-open/crypto/symmetric/.
302 * Changes are flagged with //bbox
307 * @version 33ef80f (HEAD, tag: MATRIXSSL-3-7-2-OPEN, tag: MATRIXSSL-3-7-2-COMM, origin/master, origin/HEAD, master)
309 * Header for internal symmetric key cryptography support.
312 * Copyright (c) 2013-2015 INSIDE Secure Corporation
313 * Copyright (c) PeerSec Networks, 2002-2011
314 * All Rights Reserved
316 * The latest version of this code is available at http://www.matrixssl.org
318 * This software is open source; you can redistribute it and/or modify
319 * it under the terms of the GNU General Public License as published by
320 * the Free Software Foundation; either version 2 of the License, or
321 * (at your option) any later version.
323 * This General Public License does NOT permit incorporating this software
324 * into proprietary programs. If you are unable to comply with the GPL, a
325 * commercial license for this software may be purchased from INSIDE at
326 * http://www.insidesecure.com/eng/Company/Locations
328 * This program is distributed in WITHOUT ANY WARRANTY; without even the
329 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
330 * See the GNU General Public License for more details.
332 * You should have received a copy of the GNU General Public License
333 * along with this program; if not, write to the Free Software
334 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
335 * http://www.gnu.org/copyleft/gpl.html
337 /******************************************************************************/
339 #ifndef _h_PS_SYMMETRIC
340 #define _h_PS_SYMMETRIC
342 /******************************************************************************/
344 /******************************************************************************/
347 #ifndef USE_AES_CBC_EXTERNAL
349 uint32 eK[64], dK[64];
355 unsigned char IV[16];
357 #if defined(USE_AES_GCM) || defined(USE_AES_CCM)
358 unsigned char EncCtr[16];
359 unsigned char CtrBlock[16];
362 unsigned char gInit[16];
364 unsigned char Hash_SubKey[16];
365 uint32 ProcessedBitCount[4];
366 uint32 InputBufferCount;
367 uint32 OutputBufferCount;
370 unsigned char Buffer[128];
371 uint32 BufferAlignment;
373 #endif /* USE_AES_GCM */
375 uint32_t ccmTagTemp[16 / sizeof(uint32_t)]; /* 32 */
378 /* Used for formatting IV. */
379 uint8_t Temporary[16];
380 /* Used for processing Mac. */
383 #endif /* USE_AES_CCM */
385 #endif /* USE_AES_CBC_EXTERNAL */
390 #define SSL_IDEA_KEY_LEN 16
391 #define SSL_IDEA_IV_LEN 8
392 #define SSL_IDEA_BLOCK_LEN 8
395 uint16 key_schedule[52];
401 short for_encryption;
405 /******************************************************************************/
407 /******************************************************************************/
409 /******************************************************************************/
410 #define SSL_SEED_KEY_LEN 16
411 #define SSL_SEED_IV_LEN 16
415 uint32 K[32], dK[32];
420 unsigned char IV[16];
424 #endif /* USE_SEED */
425 /******************************************************************************/
427 /******************************************************************************/
428 #if defined(USE_3DES) || defined(USE_DES)
429 /******************************************************************************/
430 #define DES3_KEY_LEN 24
431 #define DES3_IV_LEN 8
432 #define DES_KEY_LEN 8
435 uint32 ek[3][32], dk[3][32];
439 A block cipher CBC structure
447 #endif /* USE_3DES || USE_DES */
448 /******************************************************************************/
450 /******************************************************************************/
453 unsigned char state[256];
458 #endif /* USE_ARC4 */
459 /******************************************************************************/
471 /******************************************************************************/
472 /* Universal types and defines */
473 /******************************************************************************/
474 #define MAXBLOCKSIZE 24
497 #define byte(x, n) (((x) >> (8 * (n))) & 255)
499 #endif /* _h_PS_SYMMETRIC */
500 /******************************************************************************/