rc5_local.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #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 through */ \
  23. case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
  24. /* fall through */ \
  25. case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
  26. /* fall through */ \
  27. case 5: l2|=((unsigned long)(*(--(c)))); \
  28. /* fall through */ \
  29. case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
  30. /* fall through */ \
  31. case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
  32. /* fall through */ \
  33. case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
  34. /* fall through */ \
  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 through */ \
  50. case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
  51. /* fall through */ \
  52. case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
  53. /* fall through */ \
  54. case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
  55. /* fall through */ \
  56. case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
  57. /* fall through */ \
  58. case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
  59. /* fall through */ \
  60. case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
  61. /* fall through */ \
  62. case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
  63. } \
  64. }
  65. #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER))
  66. # define ROTATE_l32(a,n) _lrotl(a,n)
  67. # define ROTATE_r32(a,n) _lrotr(a,n)
  68. #elif defined(__ICC)
  69. # define ROTATE_l32(a,n) _rotl(a,n)
  70. # define ROTATE_r32(a,n) _rotr(a,n)
  71. #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
  72. # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
  73. # define ROTATE_l32(a,n) ({ register unsigned int ret; \
  74. asm ("roll %%cl,%0" \
  75. : "=r"(ret) \
  76. : "c"(n),"0"((unsigned int)(a)) \
  77. : "cc"); \
  78. ret; \
  79. })
  80. # define ROTATE_r32(a,n) ({ register unsigned int ret; \
  81. asm ("rorl %%cl,%0" \
  82. : "=r"(ret) \
  83. : "c"(n),"0"((unsigned int)(a)) \
  84. : "cc"); \
  85. ret; \
  86. })
  87. # endif
  88. #endif
  89. #ifndef ROTATE_l32
  90. # define ROTATE_l32(a,n) (((a)<<(n&0x1f))|(((a)&0xffffffff)>>((32-n)&0x1f)))
  91. #endif
  92. #ifndef ROTATE_r32
  93. # define ROTATE_r32(a,n) (((a)<<((32-n)&0x1f))|(((a)&0xffffffff)>>(n&0x1f)))
  94. #endif
  95. #define RC5_32_MASK 0xffffffffL
  96. #define RC5_32_P 0xB7E15163L
  97. #define RC5_32_Q 0x9E3779B9L
  98. #define E_RC5_32(a,b,s,n) \
  99. a^=b; \
  100. a=ROTATE_l32(a,b); \
  101. a+=s[n]; \
  102. a&=RC5_32_MASK; \
  103. b^=a; \
  104. b=ROTATE_l32(b,a); \
  105. b+=s[n+1]; \
  106. b&=RC5_32_MASK;
  107. #define D_RC5_32(a,b,s,n) \
  108. b-=s[n+1]; \
  109. b&=RC5_32_MASK; \
  110. b=ROTATE_r32(b,a); \
  111. b^=a; \
  112. a-=s[n]; \
  113. a&=RC5_32_MASK; \
  114. a=ROTATE_r32(a,b); \
  115. a^=b;