1 /* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (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 thru */ \ 26 case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 27 /* fall thru */ \ 28 case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 29 /* fall thru */ \ 30 case 5: l2|=((unsigned long)(*(--(c)))); \ 31 /* fall thru */ \ 32 case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 33 /* fall thru */ \ 34 case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 35 /* fall thru */ \ 36 case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 37 /* fall thru */ \ 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 thru */ \ 55 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 56 /* fall thru */ \ 57 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 58 /* fall thru */ \ 59 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 60 /* fall thru */ \ 61 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 62 /* fall thru */ \ 63 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 64 /* fall thru */ \ 65 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 66 /* fall thru */ \ 67 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 68 } \ 69 } 70 71 /* NOTE - c is not incremented as per n2l */ 72 #define n2ln(c,l1,l2,n) { \ 73 c+=n; \ 74 l1=l2=0; \ 75 switch (n) { \ 76 case 8: l2 =((unsigned long)(*(--(c)))) ; \ 77 /* fall thru */ \ 78 case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 79 /* fall thru */ \ 80 case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 81 /* fall thru */ \ 82 case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 83 /* fall thru */ \ 84 case 4: l1 =((unsigned long)(*(--(c)))) ; \ 85 /* fall thru */ \ 86 case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 87 /* fall thru */ \ 88 case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 89 /* fall thru */ \ 90 case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 91 } \ 92 } 93 94 /* NOTE - c is not incremented as per l2n */ 95 #define l2nn(l1,l2,c,n) { \ 96 c+=n; \ 97 switch (n) { \ 98 case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 99 /* fall thru */ \ 100 case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 101 /* fall thru */ \ 102 case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 103 /* fall thru */ \ 104 case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 105 /* fall thru */ \ 106 case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 107 /* fall thru */ \ 108 case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 109 /* fall thru */ \ 110 case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 111 /* fall thru */ \ 112 case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 113 } \ 114 } 115 116 #undef n2l 117 #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 118 l|=((unsigned long)(*((c)++)))<<16L, \ 119 l|=((unsigned long)(*((c)++)))<< 8L, \ 120 l|=((unsigned long)(*((c)++)))) 121 122 #undef l2n 123 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 124 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 125 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 126 *((c)++)=(unsigned char)(((l) )&0xff)) 127 128 #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) 129 # define ROTATE_l32(a,n) _lrotl(a,n) 130 # define ROTATE_r32(a,n) _lrotr(a,n) 131 #elif defined(__ICC) 132 # define ROTATE_l32(a,n) _rotl(a,n) 133 # define ROTATE_r32(a,n) _rotr(a,n) 134 #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) 135 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) 136 # define ROTATE_l32(a,n) ({ register unsigned int ret; \ 137 asm ("roll %%cl,%0" \ 138 : "=r"(ret) \ 139 : "c"(n),"0"((unsigned int)(a)) \ 140 : "cc"); \ 141 ret; \ 142 }) 143 # define ROTATE_r32(a,n) ({ register unsigned int ret; \ 144 asm ("rorl %%cl,%0" \ 145 : "=r"(ret) \ 146 : "c"(n),"0"((unsigned int)(a)) \ 147 : "cc"); \ 148 ret; \ 149 }) 150 # endif 151 #endif 152 #ifndef ROTATE_l32 153 # define ROTATE_l32(a,n) (((a)<<(n&0x1f))|(((a)&0xffffffff)>>((32-n)&0x1f))) 154 #endif 155 #ifndef ROTATE_r32 156 # define ROTATE_r32(a,n) (((a)<<((32-n)&0x1f))|(((a)&0xffffffff)>>(n&0x1f))) 157 #endif 158 159 #define RC5_32_MASK 0xffffffffL 160 161 #define RC5_16_P 0xB7E1 162 #define RC5_16_Q 0x9E37 163 #define RC5_32_P 0xB7E15163L 164 #define RC5_32_Q 0x9E3779B9L 165 #define RC5_64_P 0xB7E151628AED2A6BLL 166 #define RC5_64_Q 0x9E3779B97F4A7C15LL 167 168 #define E_RC5_32(a,b,s,n) \ 169 a^=b; \ 170 a=ROTATE_l32(a,b); \ 171 a+=s[n]; \ 172 a&=RC5_32_MASK; \ 173 b^=a; \ 174 b=ROTATE_l32(b,a); \ 175 b+=s[n+1]; \ 176 b&=RC5_32_MASK; 177 178 #define D_RC5_32(a,b,s,n) \ 179 b-=s[n+1]; \ 180 b&=RC5_32_MASK; \ 181 b=ROTATE_r32(b,a); \ 182 b^=a; \ 183 a-=s[n]; \ 184 a&=RC5_32_MASK; \ 185 a=ROTATE_r32(a,b); \ 186 a^=b; 187