1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2017 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#ifdef OPENSSL_SYS_WIN32 11e1051a39Sopenharmony_ci# include <stdlib.h> 12e1051a39Sopenharmony_ci#endif 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_ci#undef c2l 15e1051a39Sopenharmony_ci#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 16e1051a39Sopenharmony_ci l|=((unsigned long)(*((c)++)))<< 8L, \ 17e1051a39Sopenharmony_ci l|=((unsigned long)(*((c)++)))<<16L, \ 18e1051a39Sopenharmony_ci l|=((unsigned long)(*((c)++)))<<24L) 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per c2l */ 21e1051a39Sopenharmony_ci#undef c2ln 22e1051a39Sopenharmony_ci#define c2ln(c,l1,l2,n) { \ 23e1051a39Sopenharmony_ci c+=n; \ 24e1051a39Sopenharmony_ci l1=l2=0; \ 25e1051a39Sopenharmony_ci switch (n) { \ 26e1051a39Sopenharmony_ci case 8: l2 =((unsigned long)(*(--(c))))<<24L; \ 27e1051a39Sopenharmony_ci case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 28e1051a39Sopenharmony_ci case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 29e1051a39Sopenharmony_ci case 5: l2|=((unsigned long)(*(--(c)))); \ 30e1051a39Sopenharmony_ci case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 31e1051a39Sopenharmony_ci case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 32e1051a39Sopenharmony_ci case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 33e1051a39Sopenharmony_ci case 1: l1|=((unsigned long)(*(--(c)))); \ 34e1051a39Sopenharmony_ci } \ 35e1051a39Sopenharmony_ci } 36e1051a39Sopenharmony_ci 37e1051a39Sopenharmony_ci#undef l2c 38e1051a39Sopenharmony_ci#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 39e1051a39Sopenharmony_ci *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 40e1051a39Sopenharmony_ci *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 41e1051a39Sopenharmony_ci *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 42e1051a39Sopenharmony_ci 43e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per l2c */ 44e1051a39Sopenharmony_ci#undef l2cn 45e1051a39Sopenharmony_ci#define l2cn(l1,l2,c,n) { \ 46e1051a39Sopenharmony_ci c+=n; \ 47e1051a39Sopenharmony_ci switch (n) { \ 48e1051a39Sopenharmony_ci case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 49e1051a39Sopenharmony_ci case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 50e1051a39Sopenharmony_ci case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 51e1051a39Sopenharmony_ci case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 52e1051a39Sopenharmony_ci case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 53e1051a39Sopenharmony_ci case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 54e1051a39Sopenharmony_ci case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 55e1051a39Sopenharmony_ci case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 56e1051a39Sopenharmony_ci } \ 57e1051a39Sopenharmony_ci } 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per n2l */ 60e1051a39Sopenharmony_ci#define n2ln(c,l1,l2,n) { \ 61e1051a39Sopenharmony_ci c+=n; \ 62e1051a39Sopenharmony_ci l1=l2=0; \ 63e1051a39Sopenharmony_ci switch (n) { \ 64e1051a39Sopenharmony_ci case 8: l2 =((unsigned long)(*(--(c)))) ; \ 65e1051a39Sopenharmony_ci /* fall thru */ \ 66e1051a39Sopenharmony_ci case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 67e1051a39Sopenharmony_ci /* fall thru */ \ 68e1051a39Sopenharmony_ci case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 69e1051a39Sopenharmony_ci /* fall thru */ \ 70e1051a39Sopenharmony_ci case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 71e1051a39Sopenharmony_ci /* fall thru */ \ 72e1051a39Sopenharmony_ci case 4: l1 =((unsigned long)(*(--(c)))) ; \ 73e1051a39Sopenharmony_ci /* fall thru */ \ 74e1051a39Sopenharmony_ci case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 75e1051a39Sopenharmony_ci /* fall thru */ \ 76e1051a39Sopenharmony_ci case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 77e1051a39Sopenharmony_ci /* fall thru */ \ 78e1051a39Sopenharmony_ci case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 79e1051a39Sopenharmony_ci } \ 80e1051a39Sopenharmony_ci } 81e1051a39Sopenharmony_ci 82e1051a39Sopenharmony_ci/* NOTE - c is not incremented as per l2n */ 83e1051a39Sopenharmony_ci#define l2nn(l1,l2,c,n) { \ 84e1051a39Sopenharmony_ci c+=n; \ 85e1051a39Sopenharmony_ci switch (n) { \ 86e1051a39Sopenharmony_ci case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 87e1051a39Sopenharmony_ci /* fall thru */ \ 88e1051a39Sopenharmony_ci case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 89e1051a39Sopenharmony_ci /* fall thru */ \ 90e1051a39Sopenharmony_ci case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 91e1051a39Sopenharmony_ci /* fall thru */ \ 92e1051a39Sopenharmony_ci case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 93e1051a39Sopenharmony_ci /* fall thru */ \ 94e1051a39Sopenharmony_ci case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 95e1051a39Sopenharmony_ci /* fall thru */ \ 96e1051a39Sopenharmony_ci case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 97e1051a39Sopenharmony_ci /* fall thru */ \ 98e1051a39Sopenharmony_ci case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 99e1051a39Sopenharmony_ci /* fall thru */ \ 100e1051a39Sopenharmony_ci case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 101e1051a39Sopenharmony_ci } \ 102e1051a39Sopenharmony_ci } 103e1051a39Sopenharmony_ci 104e1051a39Sopenharmony_ci#undef n2l 105e1051a39Sopenharmony_ci#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 106e1051a39Sopenharmony_ci l|=((unsigned long)(*((c)++)))<<16L, \ 107e1051a39Sopenharmony_ci l|=((unsigned long)(*((c)++)))<< 8L, \ 108e1051a39Sopenharmony_ci l|=((unsigned long)(*((c)++)))) 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci#undef l2n 111e1051a39Sopenharmony_ci#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 112e1051a39Sopenharmony_ci *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 113e1051a39Sopenharmony_ci *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 114e1051a39Sopenharmony_ci *((c)++)=(unsigned char)(((l) )&0xff)) 115e1051a39Sopenharmony_ci 116e1051a39Sopenharmony_ci#if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER) 117e1051a39Sopenharmony_ci# define ROTL(a,n) (_lrotl(a,n)) 118e1051a39Sopenharmony_ci#else 119e1051a39Sopenharmony_ci# define ROTL(a,n) ((((a)<<(n))&0xffffffffL)|((a)>>((32-(n))&31))) 120e1051a39Sopenharmony_ci#endif 121e1051a39Sopenharmony_ci 122e1051a39Sopenharmony_ci#define C_M 0x3fc 123e1051a39Sopenharmony_ci#define C_0 22L 124e1051a39Sopenharmony_ci#define C_1 14L 125e1051a39Sopenharmony_ci#define C_2 6L 126e1051a39Sopenharmony_ci#define C_3 2L /* left shift */ 127e1051a39Sopenharmony_ci 128e1051a39Sopenharmony_ci/* The rotate has an extra 16 added to it to help the x86 asm */ 129e1051a39Sopenharmony_ci#if defined(CAST_PTR) 130e1051a39Sopenharmony_ci# define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 131e1051a39Sopenharmony_ci { \ 132e1051a39Sopenharmony_ci int i; \ 133e1051a39Sopenharmony_ci t=(key[n*2] OP1 R)&0xffffffffL; \ 134e1051a39Sopenharmony_ci i=key[n*2+1]; \ 135e1051a39Sopenharmony_ci t=ROTL(t,i); \ 136e1051a39Sopenharmony_ci L^= (((((*(CAST_LONG *)((unsigned char *) \ 137e1051a39Sopenharmony_ci CAST_S_table0+((t>>C_2)&C_M)) OP2 \ 138e1051a39Sopenharmony_ci *(CAST_LONG *)((unsigned char *) \ 139e1051a39Sopenharmony_ci CAST_S_table1+((t<<C_3)&C_M)))&0xffffffffL) OP3 \ 140e1051a39Sopenharmony_ci *(CAST_LONG *)((unsigned char *) \ 141e1051a39Sopenharmony_ci CAST_S_table2+((t>>C_0)&C_M)))&0xffffffffL) OP1 \ 142e1051a39Sopenharmony_ci *(CAST_LONG *)((unsigned char *) \ 143e1051a39Sopenharmony_ci CAST_S_table3+((t>>C_1)&C_M)))&0xffffffffL; \ 144e1051a39Sopenharmony_ci } 145e1051a39Sopenharmony_ci#elif defined(CAST_PTR2) 146e1051a39Sopenharmony_ci# define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 147e1051a39Sopenharmony_ci { \ 148e1051a39Sopenharmony_ci int i; \ 149e1051a39Sopenharmony_ci CAST_LONG u,v,w; \ 150e1051a39Sopenharmony_ci w=(key[n*2] OP1 R)&0xffffffffL; \ 151e1051a39Sopenharmony_ci i=key[n*2+1]; \ 152e1051a39Sopenharmony_ci w=ROTL(w,i); \ 153e1051a39Sopenharmony_ci u=w>>C_2; \ 154e1051a39Sopenharmony_ci v=w<<C_3; \ 155e1051a39Sopenharmony_ci u&=C_M; \ 156e1051a39Sopenharmony_ci v&=C_M; \ 157e1051a39Sopenharmony_ci t= *(CAST_LONG *)((unsigned char *)CAST_S_table0+u); \ 158e1051a39Sopenharmony_ci u=w>>C_0; \ 159e1051a39Sopenharmony_ci t=(t OP2 *(CAST_LONG *)((unsigned char *)CAST_S_table1+v))&0xffffffffL;\ 160e1051a39Sopenharmony_ci v=w>>C_1; \ 161e1051a39Sopenharmony_ci u&=C_M; \ 162e1051a39Sopenharmony_ci v&=C_M; \ 163e1051a39Sopenharmony_ci t=(t OP3 *(CAST_LONG *)((unsigned char *)CAST_S_table2+u)&0xffffffffL);\ 164e1051a39Sopenharmony_ci t=(t OP1 *(CAST_LONG *)((unsigned char *)CAST_S_table3+v)&0xffffffffL);\ 165e1051a39Sopenharmony_ci L^=(t&0xffffffff); \ 166e1051a39Sopenharmony_ci } 167e1051a39Sopenharmony_ci#else 168e1051a39Sopenharmony_ci# define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 169e1051a39Sopenharmony_ci { \ 170e1051a39Sopenharmony_ci CAST_LONG a,b,c,d; \ 171e1051a39Sopenharmony_ci t=(key[n*2] OP1 R)&0xffffffff; \ 172e1051a39Sopenharmony_ci t=ROTL(t,(key[n*2+1])); \ 173e1051a39Sopenharmony_ci a=CAST_S_table0[(t>> 8)&0xff]; \ 174e1051a39Sopenharmony_ci b=CAST_S_table1[(t )&0xff]; \ 175e1051a39Sopenharmony_ci c=CAST_S_table2[(t>>24)&0xff]; \ 176e1051a39Sopenharmony_ci d=CAST_S_table3[(t>>16)&0xff]; \ 177e1051a39Sopenharmony_ci L^=(((((a OP2 b)&0xffffffffL) OP3 c)&0xffffffffL) OP1 d)&0xffffffffL; \ 178e1051a39Sopenharmony_ci } 179e1051a39Sopenharmony_ci#endif 180e1051a39Sopenharmony_ci 181e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table0[256]; 182e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table1[256]; 183e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table2[256]; 184e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table3[256]; 185e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table4[256]; 186e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table5[256]; 187e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table6[256]; 188e1051a39Sopenharmony_ciextern const CAST_LONG CAST_S_table7[256]; 189