2
0

cast_local.h 8.2 KB

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