xref: /openssl/crypto/rc5/rc5_local.h (revision da1c088f)
1 /*
2  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdlib.h>
11 
12 #undef c2l
13 #define c2l(c,l)        (l =((unsigned long)(*((c)++)))    , \
14                          l|=((unsigned long)(*((c)++)))<< 8L, \
15                          l|=((unsigned long)(*((c)++)))<<16L, \
16                          l|=((unsigned long)(*((c)++)))<<24L)
17 
18 /* NOTE - c is not incremented as per c2l */
19 #undef c2ln
20 #define c2ln(c,l1,l2,n) { \
21                         c+=n; \
22                         l1=l2=0; \
23                         switch (n) { \
24                         case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
25                         /* fall through */                               \
26                         case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
27                         /* fall through */                               \
28                         case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
29                         /* fall through */                               \
30                         case 5: l2|=((unsigned long)(*(--(c))));      \
31                         /* fall through */                               \
32                         case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
33                         /* fall through */                               \
34                         case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
35                         /* fall through */                               \
36                         case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
37                         /* fall through */                               \
38                         case 1: l1|=((unsigned long)(*(--(c))));      \
39                                 } \
40                         }
41 
42 #undef l2c
43 #define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
44                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
45                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
46                          *((c)++)=(unsigned char)(((l)>>24L)&0xff))
47 
48 /* NOTE - c is not incremented as per l2c */
49 #undef l2cn
50 #define l2cn(l1,l2,c,n) { \
51                         c+=n; \
52                         switch (n) { \
53                         case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
54                         /* fall through */                                     \
55                         case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
56                         /* fall through */                                     \
57                         case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
58                         /* fall through */                                     \
59                         case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
60                         /* fall through */                                     \
61                         case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
62                         /* fall through */                                     \
63                         case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
64                         /* fall through */                                     \
65                         case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
66                         /* fall through */                                     \
67                         case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
68                                 } \
69                         }
70 
71 #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER))
72 # define ROTATE_l32(a,n)     _lrotl(a,n)
73 # define ROTATE_r32(a,n)     _lrotr(a,n)
74 #elif defined(__ICC)
75 # define ROTATE_l32(a,n)     _rotl(a,n)
76 # define ROTATE_r32(a,n)     _rotr(a,n)
77 #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
78 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
79 #  define ROTATE_l32(a,n)       ({ register unsigned int ret;   \
80                                         asm ("roll %%cl,%0"     \
81                                                 : "=r"(ret)     \
82                                                 : "c"(n),"0"((unsigned int)(a)) \
83                                                 : "cc");        \
84                                         ret;                    \
85                                 })
86 #  define ROTATE_r32(a,n)       ({ register unsigned int ret;   \
87                                         asm ("rorl %%cl,%0"     \
88                                                 : "=r"(ret)     \
89                                                 : "c"(n),"0"((unsigned int)(a)) \
90                                                 : "cc");        \
91                                         ret;                    \
92                                 })
93 # endif
94 #endif
95 #ifndef ROTATE_l32
96 # define ROTATE_l32(a,n)     (((a)<<(n&0x1f))|(((a)&0xffffffff)>>((32-n)&0x1f)))
97 #endif
98 #ifndef ROTATE_r32
99 # define ROTATE_r32(a,n)     (((a)<<((32-n)&0x1f))|(((a)&0xffffffff)>>(n&0x1f)))
100 #endif
101 
102 #define RC5_32_MASK     0xffffffffL
103 
104 #define RC5_32_P        0xB7E15163L
105 #define RC5_32_Q        0x9E3779B9L
106 
107 #define E_RC5_32(a,b,s,n) \
108         a^=b; \
109         a=ROTATE_l32(a,b); \
110         a+=s[n]; \
111         a&=RC5_32_MASK; \
112         b^=a; \
113         b=ROTATE_l32(b,a); \
114         b+=s[n+1]; \
115         b&=RC5_32_MASK;
116 
117 #define D_RC5_32(a,b,s,n) \
118         b-=s[n+1]; \
119         b&=RC5_32_MASK; \
120         b=ROTATE_r32(b,a); \
121         b^=a; \
122         a-=s[n]; \
123         a&=RC5_32_MASK; \
124         a=ROTATE_r32(a,b); \
125         a^=b;
126