w64wrapper.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* w64wrapper.c w64wrapper unit tests
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include <tests/unit.h>
  22. #ifdef WOLFSSL_W64_WRAPPER
  23. #ifndef NO_INLINE
  24. #define WOLFSSL_MISC_INCLUDED
  25. #include <wolfcrypt/src/misc.c>
  26. #else
  27. #include <wolfssl/wolfcrypt/misc.h>
  28. #endif
  29. int w64wrapper_test(void)
  30. {
  31. w64wrapper a, b, c;
  32. byte wrap, raw[8];
  33. a = w64From32(0x01020304, 0x05060708);
  34. #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_W64_WRAPPER_TEST)
  35. if (a.n != 0x0102030405060708LL)
  36. return -1;
  37. #else
  38. if (a.n[0] != 0x01020304 || a.n[1] != 0x05060708)
  39. return -1;
  40. #endif /* WORD64_AVAILABLE && WOLFSSL_W64_WRAPPER_TEST */
  41. if (w64GetLow32(a) != 0x05060708)
  42. return -2;
  43. if (w64GetHigh32(a) != 0x01020304)
  44. return -3;
  45. w64SetLow32(&a, 0xabcdefff);
  46. if (w64GetLow32(a) != 0xabcdefff || w64GetHigh32(a) != 0x01020304)
  47. return -4;
  48. a = w64From32(0,0);
  49. w64Increment(&a);
  50. if (w64GetLow32(a) != 1 || w64GetHigh32(a) != 0)
  51. return -5;
  52. a = w64From32(0, 0xffffffff);
  53. w64Increment(&a);
  54. if (w64GetLow32(a) != 0 || w64GetHigh32(a) != 1)
  55. return -6;
  56. a = w64From32(0,1);
  57. w64Decrement(&a);
  58. if (w64GetLow32(a) != 0 || w64GetHigh32(a) != 0)
  59. return -7;
  60. a = w64From32(1,0);
  61. w64Decrement(&a);
  62. if (w64GetLow32(a) != 0xffffffff || w64GetHigh32(a) != 0)
  63. return -8;
  64. a = w64From32(0xabcdef, 0xdeed);
  65. b = w64From32(0xabcdef, 0xdeed);
  66. if (!w64Equal(a, b))
  67. return -9;
  68. a = w64From32(1, 1);
  69. b = w64From32(0, 1);
  70. if (w64Equal(a, b))
  71. return -10;
  72. wrap = 0;
  73. a = w64From32(0x0, 0x1);
  74. b = w64Add32(a, 0x1, &wrap);
  75. if (w64GetLow32(b) != 0x2 || w64GetHigh32(b) != 0x0 || wrap)
  76. return -11;
  77. wrap = 0;
  78. a = w64From32(0x0, 0xffffffff);
  79. b = w64Add32(a, 0x1, &wrap);
  80. if (w64GetLow32(b) != 0x0 || w64GetHigh32(b) != 0x01 || wrap)
  81. return -12;
  82. wrap = 0;
  83. a = w64From32(0xffffffff, 0xffffffff);
  84. b = w64Add32(a, 0x1, &wrap);
  85. if (w64GetLow32(b) != 0x0 || w64GetHigh32(b) != 0x00 || !wrap)
  86. return -13;
  87. wrap = 0;
  88. a = w64From32(0x0, 0x1);
  89. b = w64Sub32(a, 0x1, &wrap);
  90. if (w64GetLow32(b) != 0x0 || w64GetHigh32(b) != 0x00 || wrap)
  91. return -14;
  92. wrap = 0;
  93. a = w64From32(0xffffffff, 0x0);
  94. b = w64Sub32(a, 0x1, &wrap);
  95. if (w64GetLow32(b) != 0xffffffff ||
  96. w64GetHigh32(b) != 0xfffffffe || wrap)
  97. return -15;
  98. wrap = 0;
  99. a = w64From32(0x0, 0x0);
  100. b = w64Sub32(a, 0x1, &wrap);
  101. if (w64GetLow32(b) != 0xffffffff ||
  102. w64GetHigh32(b) != 0xffffffff || !wrap)
  103. return -16;
  104. a = w64From32(0x0, 0x0);
  105. b = w64From32(0x0, 0x0);
  106. if (w64GT(a,b) || w64GT(b,a) || !w64GTE(a,b) || w64LT(a,b) || w64LT(b,a))
  107. return -17;
  108. a = w64From32(0x0, 0x1);
  109. b = w64From32(0x0, 0x0);
  110. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  111. w64LT(a, b) || !w64LT(b, a))
  112. return -18;
  113. a = w64From32(0x1, 0x0);
  114. b = w64From32(0x0, 0x0);
  115. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  116. !w64LT(b, a) || w64LT(a, b))
  117. return -19;
  118. a = w64From32(0x1, 0x0);
  119. b = w64From32(0x1, 0x0);
  120. if (w64GT(a,b) || w64GT(b,a) || !w64GTE(a,b) || w64LT(a,b))
  121. return -20;
  122. a = w64From32(0x1, 0x1);
  123. b = w64From32(0x1, 0x0);
  124. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  125. w64LT(a, b) || !w64LT(b, a))
  126. return -21;
  127. a = w64From32(0x2, 0x1);
  128. b = w64From32(0x1, 0x3);
  129. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  130. w64LT(a, b) || !w64LT(b, a))
  131. return -22;
  132. a = w64From32(0x0, 0x0);
  133. if (!w64IsZero(a))
  134. return -23;
  135. a = w64From32(0x01020304, 0x05060708);
  136. c64toa(&a, raw);
  137. if (raw[0] != 0x01
  138. ||raw[1] != 0x02
  139. ||raw[2] != 0x03
  140. ||raw[3] != 0x04
  141. ||raw[4] != 0x05
  142. ||raw[5] != 0x06
  143. ||raw[6] != 0x07
  144. ||raw[7] != 0x08) {
  145. return -24;
  146. }
  147. b = w64From32(0x0,0x0);
  148. ato64(raw, &b);
  149. if (w64GetHigh32(b) != 0x01020304 || w64GetLow32(b) != 0x05060708)
  150. return -25;
  151. w64Zero(&b);
  152. if (w64GetHigh32(b) != 0x0 || w64GetLow32(b) != 0x0)
  153. return -26;
  154. b = w64From32(0x0, 0xffffffff);
  155. w64Increment(&b);
  156. if (w64GetHigh32(b) != 0x1 || w64GetLow32(b) != 0x0)
  157. return -27;
  158. b = w64From32(0xffffffff, 0xffffffff);
  159. w64Increment(&b);
  160. if (w64GetHigh32(b) != 0x0 || w64GetLow32(b) != 0x0)
  161. return -28;
  162. b = w64From32(0xffffffff, 0x0);
  163. w64Decrement(&b);
  164. if (w64GetHigh32(b) != 0xfffffffe || w64GetLow32(b) != 0xffffffff)
  165. return -29;
  166. a = w64From32(0x01, 0x20);
  167. b = w64From32(0x01, 0x10);
  168. c = w64Sub(a,b);
  169. if (w64GetHigh32(c) != 0x0 || w64GetLow32(c) != 0x10)
  170. return -30;
  171. c = w64Sub(b,a);
  172. if (w64GetHigh32(c) != 0xffffffff || w64GetLow32(c) != 0xfffffff0)
  173. return -31;
  174. a = w64From32(0x01, 0x10);
  175. b = w64From32(0x00, 0x20);
  176. c = w64Sub(a,b);
  177. if (w64GetHigh32(c) != 0x00000000 || w64GetLow32(c) != 0xfffffff0)
  178. return -32;
  179. return 0;
  180. }
  181. #endif /* WOLFSSL_W64_WRAPPER */