w64wrapper.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <tests/unit.h>
  26. #ifdef WOLFSSL_W64_WRAPPER
  27. #ifndef NO_INLINE
  28. #define WOLFSSL_MISC_INCLUDED
  29. #include <wolfcrypt/src/misc.c>
  30. #else
  31. #include <wolfssl/wolfcrypt/misc.h>
  32. #endif
  33. int w64wrapper_test(void)
  34. {
  35. w64wrapper a, b, c;
  36. byte wrap, raw[8];
  37. a = w64From32(0x01020304, 0x05060708);
  38. #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_W64_WRAPPER_TEST)
  39. if (a.n != 0x0102030405060708LL)
  40. return -1;
  41. #else
  42. if (a.n[0] != 0x01020304 || a.n[1] != 0x05060708)
  43. return -1;
  44. #endif /* WORD64_AVAILABLE && WOLFSSL_W64_WRAPPER_TEST */
  45. if (w64GetLow32(a) != 0x05060708)
  46. return -2;
  47. if (w64GetHigh32(a) != 0x01020304)
  48. return -3;
  49. w64SetLow32(&a, 0xabcdefff);
  50. if (w64GetLow32(a) != 0xabcdefff || w64GetHigh32(a) != 0x01020304)
  51. return -4;
  52. a = w64From32(0,0);
  53. w64Increment(&a);
  54. if (w64GetLow32(a) != 1 || w64GetHigh32(a) != 0)
  55. return -5;
  56. a = w64From32(0, 0xffffffff);
  57. w64Increment(&a);
  58. if (w64GetLow32(a) != 0 || w64GetHigh32(a) != 1)
  59. return -6;
  60. a = w64From32(0,1);
  61. w64Decrement(&a);
  62. if (w64GetLow32(a) != 0 || w64GetHigh32(a) != 0)
  63. return -7;
  64. a = w64From32(1,0);
  65. w64Decrement(&a);
  66. if (w64GetLow32(a) != 0xffffffff || w64GetHigh32(a) != 0)
  67. return -8;
  68. a = w64From32(0xabcdef, 0xdeed);
  69. b = w64From32(0xabcdef, 0xdeed);
  70. if (!w64Equal(a, b))
  71. return -9;
  72. a = w64From32(1, 1);
  73. b = w64From32(0, 1);
  74. if (w64Equal(a, b))
  75. return -10;
  76. wrap = 0;
  77. a = w64From32(0x0, 0x1);
  78. b = w64Add32(a, 0x1, &wrap);
  79. if (w64GetLow32(b) != 0x2 || w64GetHigh32(b) != 0x0 || wrap)
  80. return -11;
  81. wrap = 0;
  82. a = w64From32(0x0, 0xffffffff);
  83. b = w64Add32(a, 0x1, &wrap);
  84. if (w64GetLow32(b) != 0x0 || w64GetHigh32(b) != 0x01 || wrap)
  85. return -12;
  86. wrap = 0;
  87. a = w64From32(0xffffffff, 0xffffffff);
  88. b = w64Add32(a, 0x1, &wrap);
  89. if (w64GetLow32(b) != 0x0 || w64GetHigh32(b) != 0x00 || !wrap)
  90. return -13;
  91. wrap = 0;
  92. a = w64From32(0x0, 0x1);
  93. b = w64Sub32(a, 0x1, &wrap);
  94. if (w64GetLow32(b) != 0x0 || w64GetHigh32(b) != 0x00 || wrap)
  95. return -14;
  96. wrap = 0;
  97. a = w64From32(0xffffffff, 0x0);
  98. b = w64Sub32(a, 0x1, &wrap);
  99. if (w64GetLow32(b) != 0xffffffff ||
  100. w64GetHigh32(b) != 0xfffffffe || wrap)
  101. return -15;
  102. wrap = 0;
  103. a = w64From32(0x0, 0x0);
  104. b = w64Sub32(a, 0x1, &wrap);
  105. if (w64GetLow32(b) != 0xffffffff ||
  106. w64GetHigh32(b) != 0xffffffff || !wrap)
  107. return -16;
  108. a = w64From32(0x0, 0x0);
  109. b = w64From32(0x0, 0x0);
  110. if (w64GT(a,b) || w64GT(b,a) || !w64GTE(a,b) || w64LT(a,b) || w64LT(b,a))
  111. return -17;
  112. a = w64From32(0x0, 0x1);
  113. b = w64From32(0x0, 0x0);
  114. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  115. w64LT(a, b) || !w64LT(b, a))
  116. return -18;
  117. a = w64From32(0x1, 0x0);
  118. b = w64From32(0x0, 0x0);
  119. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  120. !w64LT(b, a) || w64LT(a, b))
  121. return -19;
  122. a = w64From32(0x1, 0x0);
  123. b = w64From32(0x1, 0x0);
  124. if (w64GT(a,b) || w64GT(b,a) || !w64GTE(a,b) || w64LT(a,b))
  125. return -20;
  126. a = w64From32(0x1, 0x1);
  127. b = w64From32(0x1, 0x0);
  128. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  129. w64LT(a, b) || !w64LT(b, a))
  130. return -21;
  131. a = w64From32(0x2, 0x1);
  132. b = w64From32(0x1, 0x3);
  133. if (!w64GT(a, b) || w64GT(b, a) || !w64GTE(a, b) || w64GTE(b, a) ||
  134. w64LT(a, b) || !w64LT(b, a))
  135. return -22;
  136. a = w64From32(0x0, 0x0);
  137. if (!w64IsZero(a))
  138. return -23;
  139. a = w64From32(0x01020304, 0x05060708);
  140. c64toa(&a, raw);
  141. if (raw[0] != 0x01
  142. ||raw[1] != 0x02
  143. ||raw[2] != 0x03
  144. ||raw[3] != 0x04
  145. ||raw[4] != 0x05
  146. ||raw[5] != 0x06
  147. ||raw[6] != 0x07
  148. ||raw[7] != 0x08) {
  149. return -24;
  150. }
  151. b = w64From32(0x0,0x0);
  152. ato64(raw, &b);
  153. if (w64GetHigh32(b) != 0x01020304 || w64GetLow32(b) != 0x05060708)
  154. return -25;
  155. w64Zero(&b);
  156. if (w64GetHigh32(b) != 0x0 || w64GetLow32(b) != 0x0)
  157. return -26;
  158. b = w64From32(0x0, 0xffffffff);
  159. w64Increment(&b);
  160. if (w64GetHigh32(b) != 0x1 || w64GetLow32(b) != 0x0)
  161. return -27;
  162. b = w64From32(0xffffffff, 0xffffffff);
  163. w64Increment(&b);
  164. if (w64GetHigh32(b) != 0x0 || w64GetLow32(b) != 0x0)
  165. return -28;
  166. b = w64From32(0xffffffff, 0x0);
  167. w64Decrement(&b);
  168. if (w64GetHigh32(b) != 0xfffffffe || w64GetLow32(b) != 0xffffffff)
  169. return -29;
  170. a = w64From32(0x01, 0x20);
  171. b = w64From32(0x01, 0x10);
  172. c = w64Sub(a,b);
  173. if (w64GetHigh32(c) != 0x0 || w64GetLow32(c) != 0x10)
  174. return -30;
  175. c = w64Sub(b,a);
  176. if (w64GetHigh32(c) != 0xffffffff || w64GetLow32(c) != 0xfffffff0)
  177. return -31;
  178. a = w64From32(0x01, 0x10);
  179. b = w64From32(0x00, 0x20);
  180. c = w64Sub(a,b);
  181. if (w64GetHigh32(c) != 0x00000000 || w64GetLow32(c) != 0xfffffff0)
  182. return -32;
  183. return 0;
  184. }
  185. #endif /* WOLFSSL_W64_WRAPPER */