2
0

cast_local.h 6.0 KB

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