cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
mode_hdr.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
4 
5  LICENSE TERMS
6 
7  The redistribution and use of this software (with or without changes)
8  is allowed without the payment of fees or royalties provided that:
9 
10  1. source code distributions include the above copyright notice, this
11  list of conditions and the following disclaimer;
12 
13  2. binary distributions include the above copyright notice, this list
14  of conditions and the following disclaimer in their documentation;
15 
16  3. the name of the copyright holder is not used to endorse products
17  built using this software without specific written permission.
18 
19  DISCLAIMER
20 
21  This software is provided 'as is' with no explicit or implied warranties
22  in respect of its properties, including, but not limited to, correctness
23  and/or fitness for purpose.
24  ---------------------------------------------------------------------------
25  Issue Date: 07/10/2010
26 
27  This header file is an INTERNAL file which supports mode implementation
28 */
29 
30 #ifndef _MODE_HDR_H
31 #define _MODE_HDR_H
32 
33 #include <string.h>
34 #include <limits.h>
35 
36 #if defined( INC_ALL ) /* pcg */
37  #include "brg_endian.h"
38 #else
39  #include "crypt/brg_endian.h"
40 #endif /* Compiler-specific includes */
41 
42 /* This define sets the units in which buffers are processed. This code
43  can provide significant speed gains if buffers can be processed in
44  32 or 64 bit chunks rather than in bytes. This define sets the units
45  in which buffers will be accessed if possible
46 */
47 #if !defined( UNIT_BITS )
48 # if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
49 # define UNIT_BITS 8
50 # elif defined( _WIN64 )
51 # define UNIT_BITS 64
52 # else
53 # define UNIT_BITS 32
54 # endif
55 #endif
56 
57 #if UNIT_BITS == 64 && !defined( NEED_UINT_64T )
58 # define NEED_UINT_64T
59 #endif
60 
61 #if defined( INC_ALL ) /* pcg */
62  #include "brg_types.h"
63 #else
64  #include "crypt/brg_types.h"
65 #endif /* Compiler-specific includes */
66 
67 /* Use of inlines is preferred but code blocks can also be expanded inline
68  using 'defines'. But the latter approach will typically generate a LOT
69  of code and is not recommended.
70 */
71 #if 1 && !defined( USE_INLINING )
72 # define USE_INLINING
73 #endif
74 
75 #if defined( _MSC_VER )
76 # if _MSC_VER >= 1400
77 # include <stdlib.h>
78 # include <intrin.h>
79 # pragma intrinsic(memset)
80 # pragma intrinsic(memcpy)
81 # define rotl32 _rotl
82 # define rotr32 _rotr
83 # define rotl64 _rotl64
84 # define rotr64 _rotl64
85 # define bswap_16(x) _byteswap_ushort(x)
86 # define bswap_32(x) _byteswap_ulong(x)
87 # define bswap_64(x) _byteswap_uint64(x)
88 # else
89 # define rotl32 _lrotl
90 # define rotr32 _lrotr
91 # endif
92 #endif
93 
94 #if defined( USE_INLINING )
95 # if defined( _MSC_VER )
96 # define mh_decl __inline
97 # elif defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
98 # define mh_decl static inline
99 # else
100 # define mh_decl static
101 # endif
102 #endif
103 
104 #if defined(__cplusplus)
105 extern "C" {
106 #endif
107 
108 #define UI8_PTR(x) UPTR_CAST(x, 8)
109 #define UI16_PTR(x) UPTR_CAST(x, 16)
110 #define UI32_PTR(x) UPTR_CAST(x, 32)
111 #define UI64_PTR(x) UPTR_CAST(x, 64)
112 #define UNIT_PTR(x) UPTR_CAST(x, UNIT_BITS)
113 
114 #define UI8_VAL(x) UNIT_CAST(x, 8)
115 #define UI16_VAL(x) UNIT_CAST(x, 16)
116 #define UI32_VAL(x) UNIT_CAST(x, 32)
117 #define UI64_VAL(x) UNIT_CAST(x, 64)
118 #define UNIT_VAL(x) UNIT_CAST(x, UNIT_BITS)
119 
120 #define BUF_INC (UNIT_BITS >> 3)
121 #define BUF_ADRMASK ((UNIT_BITS >> 3) - 1)
122 
123 #define rep2_u2(f,r,x) f( 0,r,x); f( 1,r,x)
124 #define rep2_u4(f,r,x) f( 0,r,x); f( 1,r,x); f( 2,r,x); f( 3,r,x)
125 #define rep2_u16(f,r,x) f( 0,r,x); f( 1,r,x); f( 2,r,x); f( 3,r,x); \
126  f( 4,r,x); f( 5,r,x); f( 6,r,x); f( 7,r,x); \
127  f( 8,r,x); f( 9,r,x); f(10,r,x); f(11,r,x); \
128  f(12,r,x); f(13,r,x); f(14,r,x); f(15,r,x)
129 
130 #define rep2_d2(f,r,x) f( 1,r,x); f( 0,r,x)
131 #define rep2_d4(f,r,x) f( 3,r,x); f( 2,r,x); f( 1,r,x); f( 0,r,x)
132 #define rep2_d16(f,r,x) f(15,r,x); f(14,r,x); f(13,r,x); f(12,r,x); \
133  f(11,r,x); f(10,r,x); f( 9,r,x); f( 8,r,x); \
134  f( 7,r,x); f( 6,r,x); f( 5,r,x); f( 4,r,x); \
135  f( 3,r,x); f( 2,r,x); f( 1,r,x); f( 0,r,x)
136 
137 #define rep3_u2(f,r,x,y,c) f( 0,r,x,y,c); f( 1,r,x,y,c)
138 #define rep3_u4(f,r,x,y,c) f( 0,r,x,y,c); f( 1,r,x,y,c); f( 2,r,x,y,c); f( 3,r,x,y,c)
139 #define rep3_u16(f,r,x,y,c) f( 0,r,x,y,c); f( 1,r,x,y,c); f( 2,r,x,y,c); f( 3,r,x,y,c); \
140  f( 4,r,x,y,c); f( 5,r,x,y,c); f( 6,r,x,y,c); f( 7,r,x,y,c); \
141  f( 8,r,x,y,c); f( 9,r,x,y,c); f(10,r,x,y,c); f(11,r,x,y,c); \
142  f(12,r,x,y,c); f(13,r,x,y,c); f(14,r,x,y,c); f(15,r,x,y,c)
143 
144 #define rep3_d2(f,r,x,y,c) f( 1,r,x,y,c); f( 0,r,x,y,c)
145 #define rep3_d4(f,r,x,y,c) f( 3,r,x,y,c); f( 2,r,x,y,c); f( 1,r,x,y,c); f( 0,r,x,y,c)
146 #define rep3_d16(f,r,x,y,c) f(15,r,x,y,c); f(14,r,x,y,c); f(13,r,x,y,c); f(12,r,x,y,c); \
147  f(11,r,x,y,c); f(10,r,x,y,c); f( 9,r,x,y,c); f( 8,r,x,y,c); \
148  f( 7,r,x,y,c); f( 6,r,x,y,c); f( 5,r,x,y,c); f( 4,r,x,y,c); \
149  f( 3,r,x,y,c); f( 2,r,x,y,c); f( 1,r,x,y,c); f( 0,r,x,y,c)
150 
151 /* function pointers might be used for fast XOR operations */
152 
153 typedef void (*xor_function)(void* r, const void* p, const void* q);
154 
155 /* left and right rotates on 32 and 64 bit variables */
156 
157 #if !defined( rotl32 ) /* NOTE: 0 <= n <= 32 ASSUMED */
158 mh_decl uint_32t rotl32(uint_32t x, int n)
159 {
160  return (((x) << n) | ((x) >> (32 - n)));
161 }
162 #endif
163 
164 #if !defined( rotr32 ) /* NOTE: 0 <= n <= 32 ASSUMED */
165 mh_decl uint_32t rotr32(uint_32t x, int n)
166 {
167  return (((x) >> n) | ((x) << (32 - n)));
168 }
169 #endif
170 
171 #if ( UNIT_BITS == 64 ) && !defined( rotl64 ) /* NOTE: 0 <= n <= 64 ASSUMED */
172 mh_decl uint_64t rotl64(uint_64t x, int n)
173 {
174  return (((x) << n) | ((x) >> (64 - n)));
175 }
176 #endif
177 
178 #if ( UNIT_BITS == 64 ) && !defined( rotr64 ) /* NOTE: 0 <= n <= 64 ASSUMED */
179 mh_decl uint_64t rotr64(uint_64t x, int n)
180 {
181  return (((x) >> n) | ((x) << (64 - n)));
182 }
183 #endif
184 
185 /* byte order inversions for 16, 32 and 64 bit variables */
186 
187 #if !defined(bswap_16)
188 mh_decl uint_16t bswap_16(uint_16t x)
189 {
190  return (uint_16t)((x >> 8) | (x << 8));
191 }
192 #endif
193 
194 #if !defined(bswap_32)
195 mh_decl uint_32t bswap_32(uint_32t x)
196 {
197  return ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00));
198 }
199 #endif
200 
201 #if ( UNIT_BITS == 64 ) && !defined(bswap_64)
202 mh_decl uint_64t bswap_64(uint_64t x)
203 {
204  return bswap_32((uint_32t)(x >> 32)) | ((uint_64t)bswap_32((uint_32t)x) << 32);
205 }
206 #endif
207 
208 /* support for fast aligned buffer move, xor and byte swap operations -
209  source and destination buffers for move and xor operations must not
210  overlap, those for byte order revesal must either not overlap or
211  must be identical
212 */
213 #define f_copy(n,p,q) p[n] = q[n]
214 #define f_xor(n,r,p,q,c) r[n] = c(p[n] ^ q[n])
215 
216 mh_decl void copy_block(void* p, const void* q)
217 {
218  memcpy(p, q, 16);
219 }
220 
221 mh_decl void copy_block_aligned(void *p, const void *q)
222 {
223 #if UNIT_BITS == 8
224  memcpy(p, q, 16);
225 #elif UNIT_BITS == 32
227 #else
229 #endif
230 }
231 
232 mh_decl void xor_block(void *r, const void* p, const void* q)
233 {
235 }
236 
237 mh_decl void xor_block_aligned(void *r, const void *p, const void *q)
238 {
239 #if UNIT_BITS == 8
241 #elif UNIT_BITS == 32
243 #else
245 #endif
246 }
247 
248 mh_decl void bswap32_block(void *d, const void* s)
249 {
250 #if UNIT_BITS == 8
251  uint_8t t;
252  t = UNIT_PTR(s)[ 0]; UNIT_PTR(d)[ 0] = UNIT_PTR(s)[ 3]; UNIT_PTR(d)[ 3] = t;
253  t = UNIT_PTR(s)[ 1]; UNIT_PTR(d)[ 1] = UNIT_PTR(s)[ 2]; UNIT_PTR(d)[ 2] = t;
254  t = UNIT_PTR(s)[ 4]; UNIT_PTR(d)[ 4] = UNIT_PTR(s)[ 7]; UNIT_PTR(d)[ 7] = t;
255  t = UNIT_PTR(s)[ 5]; UNIT_PTR(d)[ 5] = UNIT_PTR(s)[ 6]; UNIT_PTR(d) [6] = t;
256  t = UNIT_PTR(s)[ 8]; UNIT_PTR(d)[ 8] = UNIT_PTR(s)[11]; UNIT_PTR(d)[12] = t;
257  t = UNIT_PTR(s)[ 9]; UNIT_PTR(d)[ 9] = UNIT_PTR(s)[10]; UNIT_PTR(d)[10] = t;
258  t = UNIT_PTR(s)[12]; UNIT_PTR(d)[12] = UNIT_PTR(s)[15]; UNIT_PTR(d)[15] = t;
259  t = UNIT_PTR(s)[13]; UNIT_PTR(d)[ 3] = UNIT_PTR(s)[14]; UNIT_PTR(d)[14] = t;
260 #elif UNIT_BITS == 32
261  UNIT_PTR(d)[0] = bswap_32(UNIT_PTR(s)[0]); UNIT_PTR(d)[1] = bswap_32(UNIT_PTR(s)[1]);
262  UNIT_PTR(d)[2] = bswap_32(UNIT_PTR(s)[2]); UNIT_PTR(d)[3] = bswap_32(UNIT_PTR(s)[3]);
263 #else
264  UI32_PTR(d)[0] = bswap_32(UI32_PTR(s)[0]); UI32_PTR(d)[1] = bswap_32(UI32_PTR(s)[1]);
265  UI32_PTR(d)[2] = bswap_32(UI32_PTR(s)[2]); UI32_PTR(d)[3] = bswap_32(UI32_PTR(s)[3]);
266 #endif
267 }
268 
269 mh_decl void bswap64_block(void *d, const void* s)
270 {
271 #if UNIT_BITS == 8
272  uint_8t t;
273  t = UNIT_PTR(s)[ 0]; UNIT_PTR(d)[ 0] = UNIT_PTR(s)[ 7]; UNIT_PTR(d)[ 7] = t;
274  t = UNIT_PTR(s)[ 1]; UNIT_PTR(d)[ 1] = UNIT_PTR(s)[ 6]; UNIT_PTR(d)[ 6] = t;
275  t = UNIT_PTR(s)[ 2]; UNIT_PTR(d)[ 2] = UNIT_PTR(s)[ 5]; UNIT_PTR(d)[ 5] = t;
276  t = UNIT_PTR(s)[ 3]; UNIT_PTR(d)[ 3] = UNIT_PTR(s)[ 3]; UNIT_PTR(d) [3] = t;
277  t = UNIT_PTR(s)[ 8]; UNIT_PTR(d)[ 8] = UNIT_PTR(s)[15]; UNIT_PTR(d)[15] = t;
278  t = UNIT_PTR(s)[ 9]; UNIT_PTR(d)[ 9] = UNIT_PTR(s)[14]; UNIT_PTR(d)[14] = t;
279  t = UNIT_PTR(s)[10]; UNIT_PTR(d)[10] = UNIT_PTR(s)[13]; UNIT_PTR(d)[13] = t;
280  t = UNIT_PTR(s)[11]; UNIT_PTR(d)[11] = UNIT_PTR(s)[12]; UNIT_PTR(d)[12] = t;
281 #elif UNIT_BITS == 32
282  uint_32t t;
283  t = bswap_32(UNIT_PTR(s)[0]); UNIT_PTR(d)[0] = bswap_32(UNIT_PTR(s)[1]); UNIT_PTR(d)[1] = t;
284  t = bswap_32(UNIT_PTR(s)[2]); UNIT_PTR(d)[2] = bswap_32(UNIT_PTR(s)[2]); UNIT_PTR(d)[3] = t;
285 #else
286  UNIT_PTR(d)[0] = bswap_64(UNIT_PTR(s)[0]); UNIT_PTR(d)[1] = bswap_64(UNIT_PTR(s)[1]);
287 #endif
288 }
289 
290 mh_decl void bswap128_block(void *d, const void* s)
291 {
292 #if UNIT_BITS == 8
293  uint_8t t;
294  t = UNIT_PTR(s)[0]; UNIT_PTR(d)[0] = UNIT_PTR(s)[15]; UNIT_PTR(d)[15] = t;
295  t = UNIT_PTR(s)[1]; UNIT_PTR(d)[1] = UNIT_PTR(s)[14]; UNIT_PTR(d)[14] = t;
296  t = UNIT_PTR(s)[2]; UNIT_PTR(d)[2] = UNIT_PTR(s)[13]; UNIT_PTR(d)[13] = t;
297  t = UNIT_PTR(s)[3]; UNIT_PTR(d)[3] = UNIT_PTR(s)[12]; UNIT_PTR(d)[12] = t;
298  t = UNIT_PTR(s)[4]; UNIT_PTR(d)[4] = UNIT_PTR(s)[11]; UNIT_PTR(d)[11] = t;
299  t = UNIT_PTR(s)[5]; UNIT_PTR(d)[5] = UNIT_PTR(s)[10]; UNIT_PTR(d)[10] = t;
300  t = UNIT_PTR(s)[6]; UNIT_PTR(d)[6] = UNIT_PTR(s)[ 9]; UNIT_PTR(d)[ 9] = t;
301  t = UNIT_PTR(s)[7]; UNIT_PTR(d)[7] = UNIT_PTR(s)[ 8]; UNIT_PTR(d)[ 8] = t;
302 #elif UNIT_BITS == 32
303  uint_32t t;
304  t = bswap_32(UNIT_PTR(s)[0]); UNIT_PTR(d)[0] = bswap_32(UNIT_PTR(s)[3]); UNIT_PTR(d)[3] = t;
305  t = bswap_32(UNIT_PTR(s)[1]); UNIT_PTR(d)[1] = bswap_32(UNIT_PTR(s)[2]); UNIT_PTR(d)[2] = t;
306 #else
307  uint_64t t;
308  t = bswap_64(UNIT_PTR(s)[0]); UNIT_PTR(d)[0] = bswap_64(UNIT_PTR(s)[1]); UNIT_PTR(d)[1] = t;
309 #endif
310 }
311 
312 /* platform byte order to big or little endian order for 16, 32 and 64 bit variables */
313 
314 #if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
315 
316 # define uint_16t_to_le(x) (x) = bswap_16((x))
317 # define uint_32t_to_le(x) (x) = bswap_32((x))
318 # define uint_64t_to_le(x) (x) = bswap_64((x))
319 # define uint_16t_to_be(x)
320 # define uint_32t_to_be(x)
321 # define uint_64t_to_be(x)
322 
323 #else
324 
325 # define uint_16t_to_le(x)
326 # define uint_32t_to_le(x)
327 # define uint_64t_to_le(x)
328 # define uint_16t_to_be(x) (x) = bswap_16((x))
329 # define uint_32t_to_be(x) (x) = bswap_32((x))
330 # define uint_64t_to_be(x) (x) = bswap_64((x))
331 
332 #endif
333 
334 #if defined(__cplusplus)
335 }
336 #endif
337 
338 #endif