rc5_locl.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 1995-2016 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. #include <stdlib.h>
  10. #undef c2l
  11. #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
  12. l|=((unsigned long)(*((c)++)))<< 8L, \
  13. l|=((unsigned long)(*((c)++)))<<16L, \
  14. l|=((unsigned long)(*((c)++)))<<24L)
  15. /* NOTE - c is not incremented as per c2l */
  16. #undef c2ln
  17. #define c2ln(c,l1,l2,n) { \
  18. c+=n; \
  19. l1=l2=0; \
  20. switch (n) { \
  21. case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
  22. /* fall thru */ \
  23. case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
  24. /* fall thru */ \
  25. case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
  26. /* fall thru */ \
  27. case 5: l2|=((unsigned long)(*(--(c)))); \
  28. /* fall thru */ \
  29. case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
  30. /* fall thru */ \
  31. case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
  32. /* fall thru */ \
  33. case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
  34. /* fall thru */ \
  35. case 1: l1|=((unsigned long)(*(--(c)))); \
  36. } \
  37. }
  38. #undef l2c
  39. #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  40. *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  41. *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  42. *((c)++)=(unsigned char)(((l)>>24L)&0xff))
  43. /* NOTE - c is not incremented as per l2c */
  44. #undef l2cn
  45. #define l2cn(l1,l2,c,n) { \
  46. c+=n; \
  47. switch (n) { \
  48. case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
  49. /* fall thru */ \
  50. case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
  51. /* fall thru */ \
  52. case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
  53. /* fall thru */ \
  54. case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
  55. /* fall thru */ \
  56. case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
  57. /* fall thru */ \
  58. case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
  59. /* fall thru */ \
  60. case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
  61. /* fall thru */ \
  62. case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
  63. } \
  64. }
  65. /* NOTE - c is not incremented as per n2l */
  66. #define n2ln(c,l1,l2,n) { \
  67. c+=n; \
  68. l1=l2=0; \
  69. switch (n) { \
  70. case 8: l2 =((unsigned long)(*(--(c)))) ; \
  71. /* fall thru */ \
  72. case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
  73. /* fall thru */ \
  74. case 6: l2|=((unsigned long)(*(--(c))))<<16; \
  75. /* fall thru */ \
  76. case 5: l2|=((unsigned long)(*(--(c))))<<24; \
  77. /* fall thru */ \
  78. case 4: l1 =((unsigned long)(*(--(c)))) ; \
  79. /* fall thru */ \
  80. case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
  81. /* fall thru */ \
  82. case 2: l1|=((unsigned long)(*(--(c))))<<16; \
  83. /* fall thru */ \
  84. case 1: l1|=((unsigned long)(*(--(c))))<<24; \
  85. } \
  86. }
  87. /* NOTE - c is not incremented as per l2n */
  88. #define l2nn(l1,l2,c,n) { \
  89. c+=n; \
  90. switch (n) { \
  91. case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
  92. /* fall thru */ \
  93. case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
  94. /* fall thru */ \
  95. case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
  96. /* fall thru */ \
  97. case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
  98. /* fall thru */ \
  99. case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
  100. /* fall thru */ \
  101. case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
  102. /* fall thru */ \
  103. case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
  104. /* fall thru */ \
  105. case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
  106. } \
  107. }
  108. #undef n2l
  109. #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
  110. l|=((unsigned long)(*((c)++)))<<16L, \
  111. l|=((unsigned long)(*((c)++)))<< 8L, \
  112. l|=((unsigned long)(*((c)++))))
  113. #undef l2n
  114. #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
  115. *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  116. *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  117. *((c)++)=(unsigned char)(((l) )&0xff))
  118. #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER))
  119. # define ROTATE_l32(a,n) _lrotl(a,n)
  120. # define ROTATE_r32(a,n) _lrotr(a,n)
  121. #elif defined(__ICC)
  122. # define ROTATE_l32(a,n) _rotl(a,n)
  123. # define ROTATE_r32(a,n) _rotr(a,n)
  124. #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
  125. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  126. # define ROTATE_l32(a,n) ({ register unsigned int ret; \
  127. asm ("roll %%cl,%0" \
  128. : "=r"(ret) \
  129. : "c"(n),"0"((unsigned int)(a)) \
  130. : "cc"); \
  131. ret; \
  132. })
  133. # define ROTATE_r32(a,n) ({ register unsigned int ret; \
  134. asm ("rorl %%cl,%0" \
  135. : "=r"(ret) \
  136. : "c"(n),"0"((unsigned int)(a)) \
  137. : "cc"); \
  138. ret; \
  139. })
  140. # endif
  141. #endif
  142. #ifndef ROTATE_l32
  143. # define ROTATE_l32(a,n) (((a)<<(n&0x1f))|(((a)&0xffffffff)>>((32-n)&0x1f)))
  144. #endif
  145. #ifndef ROTATE_r32
  146. # define ROTATE_r32(a,n) (((a)<<((32-n)&0x1f))|(((a)&0xffffffff)>>(n&0x1f)))
  147. #endif
  148. #define RC5_32_MASK 0xffffffffL
  149. #define RC5_16_P 0xB7E1
  150. #define RC5_16_Q 0x9E37
  151. #define RC5_32_P 0xB7E15163L
  152. #define RC5_32_Q 0x9E3779B9L
  153. #define RC5_64_P 0xB7E151628AED2A6BLL
  154. #define RC5_64_Q 0x9E3779B97F4A7C15LL
  155. #define E_RC5_32(a,b,s,n) \
  156. a^=b; \
  157. a=ROTATE_l32(a,b); \
  158. a+=s[n]; \
  159. a&=RC5_32_MASK; \
  160. b^=a; \
  161. b=ROTATE_l32(b,a); \
  162. b+=s[n+1]; \
  163. b&=RC5_32_MASK;
  164. #define D_RC5_32(a,b,s,n) \
  165. b-=s[n+1]; \
  166. b&=RC5_32_MASK; \
  167. b=ROTATE_r32(b,a); \
  168. b^=a; \
  169. a-=s[n]; \
  170. a&=RC5_32_MASK; \
  171. a=ROTATE_r32(a,b); \
  172. a^=b;