idea_local.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 1995-2024 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. #define idea_mul(r,a,b,ul) \
  10. ul=(unsigned long)a*b; \
  11. if (ul != 0) { \
  12. r=(ul&0xffff)-(ul>>16); \
  13. r-=((r)>>16); \
  14. } else { \
  15. r=(-(int)a-b+1); /* assuming a or b is 0 and in range */ \
  16. }
  17. /* NOTE - c is not incremented as per n2l */
  18. #define n2ln(c,l1,l2,n) { \
  19. c+=n; \
  20. l1=l2=0; \
  21. switch (n) { \
  22. case 8: l2 =((unsigned long)(*(--(c)))) ; \
  23. /* fall through */ \
  24. case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
  25. /* fall through */ \
  26. case 6: l2|=((unsigned long)(*(--(c))))<<16; \
  27. /* fall through */ \
  28. case 5: l2|=((unsigned long)(*(--(c))))<<24; \
  29. /* fall through */ \
  30. case 4: l1 =((unsigned long)(*(--(c)))) ; \
  31. /* fall through */ \
  32. case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
  33. /* fall through */ \
  34. case 2: l1|=((unsigned long)(*(--(c))))<<16; \
  35. /* fall through */ \
  36. case 1: l1|=((unsigned long)(*(--(c))))<<24; \
  37. } \
  38. }
  39. /* NOTE - c is not incremented as per l2n */
  40. #define l2nn(l1,l2,c,n) { \
  41. c+=n; \
  42. switch (n) { \
  43. case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
  44. /* fall through */ \
  45. case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
  46. /* fall through */ \
  47. case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
  48. /* fall through */ \
  49. case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
  50. /* fall through */ \
  51. case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
  52. /* fall through */ \
  53. case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
  54. /* fall through */ \
  55. case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
  56. /* fall through */ \
  57. case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
  58. } \
  59. }
  60. #undef n2l
  61. #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
  62. l|=((unsigned long)(*((c)++)))<<16L, \
  63. l|=((unsigned long)(*((c)++)))<< 8L, \
  64. l|=((unsigned long)(*((c)++))))
  65. #undef l2n
  66. #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
  67. *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  68. *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  69. *((c)++)=(unsigned char)(((l) )&0xff))
  70. #undef s2n
  71. #define s2n(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  72. *((c)++)=(unsigned char)(((l)>> 8L)&0xff))
  73. #undef n2s
  74. #define n2s(c,l) (l =((IDEA_INT)(*((c)++)))<< 8L, \
  75. l|=((IDEA_INT)(*((c)++))) )
  76. #define E_IDEA(num) \
  77. x1&=0xffff; \
  78. idea_mul(x1,x1,*p,ul); p++; \
  79. x2+= *(p++); \
  80. x3+= *(p++); \
  81. x4&=0xffff; \
  82. idea_mul(x4,x4,*p,ul); p++; \
  83. t0=(x1^x3)&0xffff; \
  84. idea_mul(t0,t0,*p,ul); p++; \
  85. t1=(t0+(x2^x4))&0xffff; \
  86. idea_mul(t1,t1,*p,ul); p++; \
  87. t0+=t1; \
  88. x1^=t1; \
  89. x4^=t0; \
  90. ul=x2^t0; /* do the swap to x3 */ \
  91. x2=x3^t1; \
  92. x3=ul;