le2be_macros.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _LE2BE_MACROS_H_
  32. #define _LE2BE_MACROS_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /*
  37. * Endianness translation macros used for SafeId serialization/deserialization
  38. */
  39. // LE<->BE translation of DWORD
  40. #define SwapEndian_DW(dw) ( (((dw) & 0x000000ff) << 24) | (((dw) & 0x0000ff00) << 8) | (((dw) & 0x00ff0000) >> 8) | (((dw) & 0xff000000) >> 24) )
  41. // LE<->BE translation of 8 byte big number
  42. #define SwapEndian_8B(ptr) \
  43. { \
  44. unsigned int Temp = 0; \
  45. unsigned int* p = reinterpret_cast<unsigned int*>(ptr); \
  46. Temp = SwapEndian_DW(p[0]); \
  47. p[0] = SwapEndian_DW(p[1]); \
  48. p[1] = Temp; \
  49. } \
  50. // LE<->BE translation of 10 byte (80 bit) big number
  51. #define SwapEndian_10B(ptr) \
  52. { \
  53. unsigned int Temp1 = 0; \
  54. unsigned int Temp2 = 0; \
  55. unsigned int* p = reinterpret_cast<unsigned int*>(ptr); \
  56. Temp1 = SwapEndian_DW( (unsigned int)( (((unsigned char*)(ptr))[8] << 16 ) | ( ((unsigned char*)(ptr))[9] << 24) ) ); \
  57. Temp2 = SwapEndian_DW(p[1]); \
  58. *(reinterpret_cast<unsigned int*>((unsigned char*)(ptr) + 6)) = SwapEndian_DW(p[0]); \
  59. *(reinterpret_cast<unsigned int*>((unsigned char*)(ptr) + 2)) = Temp2; \
  60. p[0] = (Temp1 & 0x0000ffff) | (p[0] & 0xffff0000); \
  61. }
  62. // LE<->BE translation of 16 byte (128-bit) big number
  63. #define SwapEndian_16B(ptr) \
  64. { \
  65. unsigned int Temp = 0; \
  66. unsigned int* p = reinterpret_cast<unsigned int*>(ptr); \
  67. Temp = SwapEndian_DW(p[0]); \
  68. p[0] = SwapEndian_DW(p[3]); \
  69. p[3] = Temp; \
  70. Temp = SwapEndian_DW(p[1]); \
  71. p[1] = SwapEndian_DW(p[2]); \
  72. p[2] = Temp; \
  73. }
  74. // LE<->BE translation of 32 byte big number
  75. #define SwapEndian_32B(ptr) \
  76. { \
  77. unsigned int Temp = 0; \
  78. unsigned int* p = reinterpret_cast<unsigned int*>(ptr); \
  79. Temp = SwapEndian_DW(p[0]); \
  80. p[0] = SwapEndian_DW(p[7]); \
  81. p[7] = Temp; \
  82. Temp = SwapEndian_DW(p[1]); \
  83. p[1] = SwapEndian_DW(p[6]); \
  84. p[6] = Temp; \
  85. Temp = SwapEndian_DW(p[2]); \
  86. p[2] = SwapEndian_DW(p[5]); \
  87. p[5] = Temp; \
  88. Temp = SwapEndian_DW(p[3]); \
  89. p[3] = SwapEndian_DW(p[4]); \
  90. p[4] = Temp; \
  91. }
  92. // LE<->BE translation of 64 byte big number
  93. #define SwapEndian_64B(ptr) \
  94. { \
  95. unsigned int Temp = 0; \
  96. unsigned int* p = reinterpret_cast<unsigned int*>(ptr); \
  97. Temp = SwapEndian_DW(p[0]); \
  98. p[0] = SwapEndian_DW(p[15]); \
  99. p[15] = Temp; \
  100. Temp = SwapEndian_DW(p[1]); \
  101. p[1] = SwapEndian_DW(p[14]); \
  102. p[14] = Temp; \
  103. Temp = SwapEndian_DW(p[2]); \
  104. p[2] = SwapEndian_DW(p[13]); \
  105. p[13] = Temp; \
  106. Temp = SwapEndian_DW(p[3]); \
  107. p[3] = SwapEndian_DW(p[12]); \
  108. p[12] = Temp; \
  109. Temp = SwapEndian_DW(p[4]); \
  110. p[4] = SwapEndian_DW(p[11]); \
  111. p[11] = Temp; \
  112. Temp = SwapEndian_DW(p[5]); \
  113. p[5] = SwapEndian_DW(p[10]); \
  114. p[10] = Temp; \
  115. Temp = SwapEndian_DW(p[6]); \
  116. p[6] = SwapEndian_DW(p[9]); \
  117. p[9] = Temp; \
  118. Temp = SwapEndian_DW(p[7]); \
  119. p[7] = SwapEndian_DW(p[8]); \
  120. p[8] = Temp; \
  121. }
  122. // LE<->BE translation of 75 byte (600 bit) big number
  123. #define SwapEndian_75B(ptr) \
  124. { \
  125. unsigned int Temp1 = 0; \
  126. unsigned int Temp2 = 0; \
  127. unsigned char i = 0; \
  128. unsigned int* p = reinterpret_cast<unsigned int*>(ptr); \
  129. Temp1 = SwapEndian_DW( (unsigned int)( ( ((unsigned char*)(ptr))[72] << 8 ) | ( ((unsigned char*)(ptr))[73] << 16 ) | ( ((unsigned char*)(ptr))[74] << 24 ) ) ); \
  130. Temp2 = (p[17] & 0xff000000); \
  131. *((unsigned int*)((unsigned char*)(ptr) + 71)) = SwapEndian_DW(p[0]); \
  132. p[0] = Temp1; \
  133. Temp1 = SwapEndian_DW(p[1]); \
  134. *((unsigned int*)((unsigned char*)(ptr) + 3 )) = SwapEndian_DW( ( (p[17] & 0x00ffffff) | Temp2 ) ); \
  135. for(i = 0; i < 8; i++) { \
  136. Temp2 = (p[16 - i] & 0xff000000); \
  137. *((unsigned int*)((unsigned char*)(ptr) + 67 - 4*i)) = Temp1; \
  138. Temp1 = SwapEndian_DW(p[2 + i]); \
  139. *((unsigned int*)((unsigned char*)(ptr) + 7 + 4*i)) = SwapEndian_DW( ( (p[16 - i] & 0x00ffffff) | Temp2 ) ); \
  140. } \
  141. }
  142. #if defined(DEFINE_GPOINTS_IN_LE2BE_MACROS)
  143. /*
  144. * Some structures useful during the conversion of SafeId data
  145. */
  146. typedef struct _G1Point {
  147. unsigned char x[32];
  148. unsigned char y[32];
  149. } G1Point;
  150. typedef struct _G2Point {
  151. unsigned char x0[32];
  152. unsigned char x1[32];
  153. unsigned char x2[32];
  154. unsigned char y0[32];
  155. unsigned char y1[32];
  156. unsigned char y2[32];
  157. } G2Point;
  158. typedef G1Point G3Point;
  159. typedef struct _GTPoint {
  160. unsigned char x0[32];
  161. unsigned char x1[32];
  162. unsigned char x2[32];
  163. unsigned char x3[32];
  164. unsigned char x4[32];
  165. unsigned char x5[32];
  166. } GTPoint;
  167. #endif
  168. #ifdef __cplusplus
  169. }
  170. #endif
  171. #endif