rc5_locl.h revision 2139:6243c3338933
1279377Simp/* crypto/rc5/rc5_locl.h */ 2279377Simp/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3279377Simp * All rights reserved. 4279377Simp * 5279377Simp * This package is an SSL implementation written 6279377Simp * by Eric Young (eay@cryptsoft.com). 7279377Simp * The implementation was written so as to conform with Netscapes SSL. 8279377Simp * 9279377Simp * This library is free for commercial and non-commercial use as long as 10279377Simp * the following conditions are aheared to. The following conditions 11279377Simp * apply to all code found in this distribution, be it the RC4, RSA, 12279377Simp * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13279377Simp * included with this distribution is covered by the same copyright terms 14279377Simp * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15279377Simp * 16279377Simp * Copyright remains Eric Young's, and as such any Copyright notices in 17279377Simp * the code are not to be removed. 18279377Simp * If this package is used in a product, Eric Young should be given attribution 19279377Simp * as the author of the parts of the library used. 20279377Simp * This can be in the form of a textual message at program startup or 21279377Simp * in documentation (online or textual) provided with the package. 22279377Simp * 23279377Simp * Redistribution and use in source and binary forms, with or without 24279377Simp * modification, are permitted provided that the following conditions 25279377Simp * are met: 26279377Simp * 1. Redistributions of source code must retain the copyright 27279377Simp * notice, this list of conditions and the following disclaimer. 28279377Simp * 2. Redistributions in binary form must reproduce the above copyright 29279377Simp * notice, this list of conditions and the following disclaimer in the 30279377Simp * documentation and/or other materials provided with the distribution. 31279377Simp * 3. All advertising materials mentioning features or use of this software 32279377Simp * must display the following acknowledgement: 33279377Simp * "This product includes cryptographic software written by 34279377Simp * Eric Young (eay@cryptsoft.com)" 35279377Simp * The word 'cryptographic' can be left out if the rouines from the library 36279377Simp * being used are not cryptographic related :-). 37279377Simp * 4. If you include any Windows specific code (or a derivative thereof) from 38279377Simp * the apps directory (application code) you must include an acknowledgement: 39279377Simp * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40279377Simp * 41279377Simp * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44279377Simp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51279377Simp * SUCH DAMAGE. 52279377Simp * 53279377Simp * The licence and distribution terms for any publically available version or 54279377Simp * derivative of this code cannot be changed. i.e. this code cannot simply be 55279377Simp * copied and put under another distribution licence 56279377Simp * [including the GNU Public Licence.] 57279377Simp */ 58279377Simp 59279377Simp#include <stdlib.h> 60279377Simp 61279377Simp#undef c2l 62279377Simp#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 63279377Simp l|=((unsigned long)(*((c)++)))<< 8L, \ 64279377Simp l|=((unsigned long)(*((c)++)))<<16L, \ 65279377Simp l|=((unsigned long)(*((c)++)))<<24L) 66279377Simp 67279377Simp/* NOTE - c is not incremented as per c2l */ 68279377Simp#undef c2ln 69279377Simp#define c2ln(c,l1,l2,n) { \ 70279377Simp c+=n; \ 71279377Simp l1=l2=0; \ 72279377Simp switch (n) { \ 73279377Simp case 8: l2 =((unsigned long)(*(--(c))))<<24L; \ 74279377Simp case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 75279377Simp case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 76279377Simp case 5: l2|=((unsigned long)(*(--(c)))); \ 77279377Simp case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 78279377Simp case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 79279377Simp case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 80279377Simp case 1: l1|=((unsigned long)(*(--(c)))); \ 81279377Simp } \ 82279377Simp } 83279377Simp 84279377Simp#undef l2c 85279377Simp#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 86279377Simp *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 87279377Simp *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 88279377Simp *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 89279377Simp 90279377Simp/* NOTE - c is not incremented as per l2c */ 91279377Simp#undef l2cn 92279377Simp#define l2cn(l1,l2,c,n) { \ 93279377Simp c+=n; \ 94279377Simp switch (n) { \ 95279377Simp case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 96279377Simp case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 97279377Simp case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 98279377Simp case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 99279377Simp case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 100279377Simp case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 101279377Simp case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 102279377Simp case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 103279377Simp } \ 104279377Simp } 105279377Simp 106279377Simp/* NOTE - c is not incremented as per n2l */ 107279377Simp#define n2ln(c,l1,l2,n) { \ 108279377Simp c+=n; \ 109279377Simp l1=l2=0; \ 110279377Simp switch (n) { \ 111279377Simp case 8: l2 =((unsigned long)(*(--(c)))) ; \ 112279377Simp case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 113279377Simp case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 114279377Simp case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 115279377Simp case 4: l1 =((unsigned long)(*(--(c)))) ; \ 116279377Simp case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 117279377Simp case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 118279377Simp case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 119279377Simp } \ 120279377Simp } 121279377Simp 122279377Simp/* NOTE - c is not incremented as per l2n */ 123279377Simp#define l2nn(l1,l2,c,n) { \ 124279377Simp c+=n; \ 125279377Simp switch (n) { \ 126279377Simp case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 127279377Simp case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 128279377Simp case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 129279377Simp case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 130279377Simp case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 131279377Simp case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 132279377Simp case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 133279377Simp case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 134279377Simp } \ 135279377Simp } 136279377Simp 137279377Simp#undef n2l 138279377Simp#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 139279377Simp l|=((unsigned long)(*((c)++)))<<16L, \ 140279377Simp l|=((unsigned long)(*((c)++)))<< 8L, \ 141279377Simp l|=((unsigned long)(*((c)++)))) 142279377Simp 143279377Simp#undef l2n 144279377Simp#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 145279377Simp *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 146279377Simp *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 147279377Simp *((c)++)=(unsigned char)(((l) )&0xff)) 148279377Simp 149279377Simp#if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) || defined(__ICC) 150279377Simp#define ROTATE_l32(a,n) _lrotl(a,n) 151279377Simp#define ROTATE_r32(a,n) _lrotr(a,n) 152279377Simp#elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) 153279377Simp# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) 154279377Simp# define ROTATE_l32(a,n) ({ register unsigned int ret; \ 155279377Simp __asm__ ("roll %%cl,%0" \ 156279377Simp : "=r"(ret) \ 157279377Simp : "c"(n),"0"(a) \ 158279377Simp : "cc"); \ 159279377Simp ret; \ 160279377Simp }) 161279377Simp# define ROTATE_r32(a,n) ({ register unsigned int ret; \ 162279377Simp __asm__ ("rorl %%cl,%0" \ 163279377Simp : "=r"(ret) \ 164279377Simp : "c"(n),"0"(a) \ 165279377Simp : "cc"); \ 166279377Simp ret; \ 167279377Simp }) 168279377Simp# endif 169279377Simp#endif 170279377Simp#ifndef ROTATE_l32 171279377Simp#define ROTATE_l32(a,n) (((a)<<(n&0x1f))|(((a)&0xffffffff)>>(32-(n&0x1f)))) 172279377Simp#endif 173279377Simp#ifndef ROTATE_r32 174279377Simp#define ROTATE_r32(a,n) (((a)<<(32-(n&0x1f)))|(((a)&0xffffffff)>>(n&0x1f))) 175279377Simp#endif 176279377Simp 177279377Simp#define RC5_32_MASK 0xffffffffL 178279377Simp 179279377Simp#define RC5_16_P 0xB7E1 180279377Simp#define RC5_16_Q 0x9E37 181279377Simp#define RC5_32_P 0xB7E15163L 182279377Simp#define RC5_32_Q 0x9E3779B9L 183279377Simp#define RC5_64_P 0xB7E151628AED2A6BLL 184279377Simp#define RC5_64_Q 0x9E3779B97F4A7C15LL 185279377Simp 186279377Simp#define E_RC5_32(a,b,s,n) \ 187279377Simp a^=b; \ 188279377Simp a=ROTATE_l32(a,b); \ 189279377Simp a+=s[n]; \ 190279377Simp a&=RC5_32_MASK; \ 191279377Simp b^=a; \ 192279377Simp b=ROTATE_l32(b,a); \ 193279377Simp b+=s[n+1]; \ 194279377Simp b&=RC5_32_MASK; 195279377Simp 196279377Simp#define D_RC5_32(a,b,s,n) \ 197279377Simp b-=s[n+1]; \ 198279377Simp b&=RC5_32_MASK; \ 199279377Simp b=ROTATE_r32(b,a); \ 200279377Simp b^=a; \ 201279377Simp a-=s[n]; \ 202279377Simp a&=RC5_32_MASK; \ 203279377Simp a=ROTATE_r32(a,b); \ 204279377Simp a^=b; 205279377Simp 206279377Simp 207279377Simp 208279377Simp