xref: /third_party/openssl/crypto/rc5/rc5_local.h (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdlib.h>
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#undef c2l
13e1051a39Sopenharmony_ci#define c2l(c,l)        (l =((unsigned long)(*((c)++)))    , \
14e1051a39Sopenharmony_ci                         l|=((unsigned long)(*((c)++)))<< 8L, \
15e1051a39Sopenharmony_ci                         l|=((unsigned long)(*((c)++)))<<16L, \
16e1051a39Sopenharmony_ci                         l|=((unsigned long)(*((c)++)))<<24L)
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per c2l */
19e1051a39Sopenharmony_ci#undef c2ln
20e1051a39Sopenharmony_ci#define c2ln(c,l1,l2,n) { \
21e1051a39Sopenharmony_ci                        c+=n; \
22e1051a39Sopenharmony_ci                        l1=l2=0; \
23e1051a39Sopenharmony_ci                        switch (n) { \
24e1051a39Sopenharmony_ci                        case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
25e1051a39Sopenharmony_ci                        /* fall thru */                               \
26e1051a39Sopenharmony_ci                        case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
27e1051a39Sopenharmony_ci                        /* fall thru */                               \
28e1051a39Sopenharmony_ci                        case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
29e1051a39Sopenharmony_ci                        /* fall thru */                               \
30e1051a39Sopenharmony_ci                        case 5: l2|=((unsigned long)(*(--(c))));      \
31e1051a39Sopenharmony_ci                        /* fall thru */                               \
32e1051a39Sopenharmony_ci                        case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
33e1051a39Sopenharmony_ci                        /* fall thru */                               \
34e1051a39Sopenharmony_ci                        case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
35e1051a39Sopenharmony_ci                        /* fall thru */                               \
36e1051a39Sopenharmony_ci                        case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
37e1051a39Sopenharmony_ci                        /* fall thru */                               \
38e1051a39Sopenharmony_ci                        case 1: l1|=((unsigned long)(*(--(c))));      \
39e1051a39Sopenharmony_ci                                } \
40e1051a39Sopenharmony_ci                        }
41e1051a39Sopenharmony_ci
42e1051a39Sopenharmony_ci#undef l2c
43e1051a39Sopenharmony_ci#define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
44e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
45e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
46e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>>24L)&0xff))
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per l2c */
49e1051a39Sopenharmony_ci#undef l2cn
50e1051a39Sopenharmony_ci#define l2cn(l1,l2,c,n) { \
51e1051a39Sopenharmony_ci                        c+=n; \
52e1051a39Sopenharmony_ci                        switch (n) { \
53e1051a39Sopenharmony_ci                        case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
54e1051a39Sopenharmony_ci                        /* fall thru */                                     \
55e1051a39Sopenharmony_ci                        case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
56e1051a39Sopenharmony_ci                        /* fall thru */                                     \
57e1051a39Sopenharmony_ci                        case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
58e1051a39Sopenharmony_ci                        /* fall thru */                                     \
59e1051a39Sopenharmony_ci                        case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
60e1051a39Sopenharmony_ci                        /* fall thru */                                     \
61e1051a39Sopenharmony_ci                        case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
62e1051a39Sopenharmony_ci                        /* fall thru */                                     \
63e1051a39Sopenharmony_ci                        case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
64e1051a39Sopenharmony_ci                        /* fall thru */                                     \
65e1051a39Sopenharmony_ci                        case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
66e1051a39Sopenharmony_ci                        /* fall thru */                                     \
67e1051a39Sopenharmony_ci                        case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
68e1051a39Sopenharmony_ci                                } \
69e1051a39Sopenharmony_ci                        }
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per n2l */
72e1051a39Sopenharmony_ci#define n2ln(c,l1,l2,n) { \
73e1051a39Sopenharmony_ci                        c+=n; \
74e1051a39Sopenharmony_ci                        l1=l2=0; \
75e1051a39Sopenharmony_ci                        switch (n) { \
76e1051a39Sopenharmony_ci                        case 8: l2 =((unsigned long)(*(--(c))))    ; \
77e1051a39Sopenharmony_ci                        /* fall thru */                              \
78e1051a39Sopenharmony_ci                        case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
79e1051a39Sopenharmony_ci                        /* fall thru */                              \
80e1051a39Sopenharmony_ci                        case 6: l2|=((unsigned long)(*(--(c))))<<16; \
81e1051a39Sopenharmony_ci                        /* fall thru */                              \
82e1051a39Sopenharmony_ci                        case 5: l2|=((unsigned long)(*(--(c))))<<24; \
83e1051a39Sopenharmony_ci                        /* fall thru */                              \
84e1051a39Sopenharmony_ci                        case 4: l1 =((unsigned long)(*(--(c))))    ; \
85e1051a39Sopenharmony_ci                        /* fall thru */                              \
86e1051a39Sopenharmony_ci                        case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
87e1051a39Sopenharmony_ci                        /* fall thru */                              \
88e1051a39Sopenharmony_ci                        case 2: l1|=((unsigned long)(*(--(c))))<<16; \
89e1051a39Sopenharmony_ci                        /* fall thru */                              \
90e1051a39Sopenharmony_ci                        case 1: l1|=((unsigned long)(*(--(c))))<<24; \
91e1051a39Sopenharmony_ci                                } \
92e1051a39Sopenharmony_ci                        }
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per l2n */
95e1051a39Sopenharmony_ci#define l2nn(l1,l2,c,n) { \
96e1051a39Sopenharmony_ci                        c+=n; \
97e1051a39Sopenharmony_ci                        switch (n) { \
98e1051a39Sopenharmony_ci                        case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
99e1051a39Sopenharmony_ci                        /* fall thru */                                    \
100e1051a39Sopenharmony_ci                        case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
101e1051a39Sopenharmony_ci                        /* fall thru */                                    \
102e1051a39Sopenharmony_ci                        case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
103e1051a39Sopenharmony_ci                        /* fall thru */                                    \
104e1051a39Sopenharmony_ci                        case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
105e1051a39Sopenharmony_ci                        /* fall thru */                                    \
106e1051a39Sopenharmony_ci                        case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
107e1051a39Sopenharmony_ci                        /* fall thru */                                    \
108e1051a39Sopenharmony_ci                        case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
109e1051a39Sopenharmony_ci                        /* fall thru */                                    \
110e1051a39Sopenharmony_ci                        case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
111e1051a39Sopenharmony_ci                        /* fall thru */                                    \
112e1051a39Sopenharmony_ci                        case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
113e1051a39Sopenharmony_ci                                } \
114e1051a39Sopenharmony_ci                        }
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_ci#undef n2l
117e1051a39Sopenharmony_ci#define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
118e1051a39Sopenharmony_ci                         l|=((unsigned long)(*((c)++)))<<16L, \
119e1051a39Sopenharmony_ci                         l|=((unsigned long)(*((c)++)))<< 8L, \
120e1051a39Sopenharmony_ci                         l|=((unsigned long)(*((c)++))))
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_ci#undef l2n
123e1051a39Sopenharmony_ci#define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
124e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
125e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
126e1051a39Sopenharmony_ci                         *((c)++)=(unsigned char)(((l)     )&0xff))
127e1051a39Sopenharmony_ci
128e1051a39Sopenharmony_ci#if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER))
129e1051a39Sopenharmony_ci# define ROTATE_l32(a,n)     _lrotl(a,n)
130e1051a39Sopenharmony_ci# define ROTATE_r32(a,n)     _lrotr(a,n)
131e1051a39Sopenharmony_ci#elif defined(__ICC)
132e1051a39Sopenharmony_ci# define ROTATE_l32(a,n)     _rotl(a,n)
133e1051a39Sopenharmony_ci# define ROTATE_r32(a,n)     _rotr(a,n)
134e1051a39Sopenharmony_ci#elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
135e1051a39Sopenharmony_ci# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
136e1051a39Sopenharmony_ci#  define ROTATE_l32(a,n)       ({ register unsigned int ret;   \
137e1051a39Sopenharmony_ci                                        asm ("roll %%cl,%0"     \
138e1051a39Sopenharmony_ci                                                : "=r"(ret)     \
139e1051a39Sopenharmony_ci                                                : "c"(n),"0"((unsigned int)(a)) \
140e1051a39Sopenharmony_ci                                                : "cc");        \
141e1051a39Sopenharmony_ci                                        ret;                    \
142e1051a39Sopenharmony_ci                                })
143e1051a39Sopenharmony_ci#  define ROTATE_r32(a,n)       ({ register unsigned int ret;   \
144e1051a39Sopenharmony_ci                                        asm ("rorl %%cl,%0"     \
145e1051a39Sopenharmony_ci                                                : "=r"(ret)     \
146e1051a39Sopenharmony_ci                                                : "c"(n),"0"((unsigned int)(a)) \
147e1051a39Sopenharmony_ci                                                : "cc");        \
148e1051a39Sopenharmony_ci                                        ret;                    \
149e1051a39Sopenharmony_ci                                })
150e1051a39Sopenharmony_ci# endif
151e1051a39Sopenharmony_ci#endif
152e1051a39Sopenharmony_ci#ifndef ROTATE_l32
153e1051a39Sopenharmony_ci# define ROTATE_l32(a,n)     (((a)<<(n&0x1f))|(((a)&0xffffffff)>>((32-n)&0x1f)))
154e1051a39Sopenharmony_ci#endif
155e1051a39Sopenharmony_ci#ifndef ROTATE_r32
156e1051a39Sopenharmony_ci# define ROTATE_r32(a,n)     (((a)<<((32-n)&0x1f))|(((a)&0xffffffff)>>(n&0x1f)))
157e1051a39Sopenharmony_ci#endif
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_ci#define RC5_32_MASK     0xffffffffL
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci#define RC5_16_P        0xB7E1
162e1051a39Sopenharmony_ci#define RC5_16_Q        0x9E37
163e1051a39Sopenharmony_ci#define RC5_32_P        0xB7E15163L
164e1051a39Sopenharmony_ci#define RC5_32_Q        0x9E3779B9L
165e1051a39Sopenharmony_ci#define RC5_64_P        0xB7E151628AED2A6BLL
166e1051a39Sopenharmony_ci#define RC5_64_Q        0x9E3779B97F4A7C15LL
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ci#define E_RC5_32(a,b,s,n) \
169e1051a39Sopenharmony_ci        a^=b; \
170e1051a39Sopenharmony_ci        a=ROTATE_l32(a,b); \
171e1051a39Sopenharmony_ci        a+=s[n]; \
172e1051a39Sopenharmony_ci        a&=RC5_32_MASK; \
173e1051a39Sopenharmony_ci        b^=a; \
174e1051a39Sopenharmony_ci        b=ROTATE_l32(b,a); \
175e1051a39Sopenharmony_ci        b+=s[n+1]; \
176e1051a39Sopenharmony_ci        b&=RC5_32_MASK;
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_ci#define D_RC5_32(a,b,s,n) \
179e1051a39Sopenharmony_ci        b-=s[n+1]; \
180e1051a39Sopenharmony_ci        b&=RC5_32_MASK; \
181e1051a39Sopenharmony_ci        b=ROTATE_r32(b,a); \
182e1051a39Sopenharmony_ci        b^=a; \
183e1051a39Sopenharmony_ci        a-=s[n]; \
184e1051a39Sopenharmony_ci        a&=RC5_32_MASK; \
185e1051a39Sopenharmony_ci        a=ROTATE_r32(a,b); \
186e1051a39Sopenharmony_ci        a^=b;
187