helpers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright The Mbed TLS Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <test/constant_flow.h>
  18. #include <test/helpers.h>
  19. #include <test/macros.h>
  20. #include <string.h>
  21. /*----------------------------------------------------------------------------*/
  22. /* Static global variables */
  23. #if defined(MBEDTLS_PLATFORM_C)
  24. static mbedtls_platform_context platform_ctx;
  25. #endif
  26. mbedtls_test_info_t mbedtls_test_info;
  27. /*----------------------------------------------------------------------------*/
  28. /* Helper Functions */
  29. int mbedtls_test_platform_setup(void)
  30. {
  31. int ret = 0;
  32. #if defined(MBEDTLS_PLATFORM_C)
  33. ret = mbedtls_platform_setup(&platform_ctx);
  34. #endif /* MBEDTLS_PLATFORM_C */
  35. return ret;
  36. }
  37. void mbedtls_test_platform_teardown(void)
  38. {
  39. #if defined(MBEDTLS_PLATFORM_C)
  40. mbedtls_platform_teardown(&platform_ctx);
  41. #endif /* MBEDTLS_PLATFORM_C */
  42. }
  43. int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
  44. {
  45. if ((c >= '0') && (c <= '9')) {
  46. *uc = c - '0';
  47. } else if ((c >= 'a') && (c <= 'f')) {
  48. *uc = c - 'a' + 10;
  49. } else if ((c >= 'A') && (c <= 'F')) {
  50. *uc = c - 'A' + 10;
  51. } else {
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. void mbedtls_test_fail(const char *test, int line_no, const char *filename)
  57. {
  58. if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
  59. /* We've already recorded the test as having failed. Don't
  60. * overwrite any previous information about the failure. */
  61. return;
  62. }
  63. mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
  64. mbedtls_test_info.test = test;
  65. mbedtls_test_info.line_no = line_no;
  66. mbedtls_test_info.filename = filename;
  67. }
  68. void mbedtls_test_skip(const char *test, int line_no, const char *filename)
  69. {
  70. mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
  71. mbedtls_test_info.test = test;
  72. mbedtls_test_info.line_no = line_no;
  73. mbedtls_test_info.filename = filename;
  74. }
  75. void mbedtls_test_set_step(unsigned long step)
  76. {
  77. mbedtls_test_info.step = step;
  78. }
  79. #if defined(MBEDTLS_BIGNUM_C)
  80. unsigned mbedtls_test_case_uses_negative_0 = 0;
  81. #endif
  82. void mbedtls_test_info_reset(void)
  83. {
  84. mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
  85. mbedtls_test_info.step = (unsigned long) (-1);
  86. mbedtls_test_info.test = 0;
  87. mbedtls_test_info.line_no = 0;
  88. mbedtls_test_info.filename = 0;
  89. memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
  90. memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
  91. #if defined(MBEDTLS_BIGNUM_C)
  92. mbedtls_test_case_uses_negative_0 = 0;
  93. #endif
  94. }
  95. int mbedtls_test_equal(const char *test, int line_no, const char *filename,
  96. unsigned long long value1, unsigned long long value2)
  97. {
  98. TEST_CF_PUBLIC(&value1, sizeof(value1));
  99. TEST_CF_PUBLIC(&value2, sizeof(value2));
  100. if (value1 == value2) {
  101. return 1;
  102. }
  103. if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
  104. /* We've already recorded the test as having failed. Don't
  105. * overwrite any previous information about the failure. */
  106. return 0;
  107. }
  108. mbedtls_test_fail(test, line_no, filename);
  109. (void) mbedtls_snprintf(mbedtls_test_info.line1,
  110. sizeof(mbedtls_test_info.line1),
  111. "lhs = 0x%016llx = %lld",
  112. value1, (long long) value1);
  113. (void) mbedtls_snprintf(mbedtls_test_info.line2,
  114. sizeof(mbedtls_test_info.line2),
  115. "rhs = 0x%016llx = %lld",
  116. value2, (long long) value2);
  117. return 0;
  118. }
  119. int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
  120. unsigned long long value1, unsigned long long value2)
  121. {
  122. TEST_CF_PUBLIC(&value1, sizeof(value1));
  123. TEST_CF_PUBLIC(&value2, sizeof(value2));
  124. if (value1 <= value2) {
  125. return 1;
  126. }
  127. if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
  128. /* We've already recorded the test as having failed. Don't
  129. * overwrite any previous information about the failure. */
  130. return 0;
  131. }
  132. mbedtls_test_fail(test, line_no, filename);
  133. (void) mbedtls_snprintf(mbedtls_test_info.line1,
  134. sizeof(mbedtls_test_info.line1),
  135. "lhs = 0x%016llx = %llu",
  136. value1, value1);
  137. (void) mbedtls_snprintf(mbedtls_test_info.line2,
  138. sizeof(mbedtls_test_info.line2),
  139. "rhs = 0x%016llx = %llu",
  140. value2, value2);
  141. return 0;
  142. }
  143. int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
  144. long long value1, long long value2)
  145. {
  146. TEST_CF_PUBLIC(&value1, sizeof(value1));
  147. TEST_CF_PUBLIC(&value2, sizeof(value2));
  148. if (value1 <= value2) {
  149. return 1;
  150. }
  151. if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
  152. /* We've already recorded the test as having failed. Don't
  153. * overwrite any previous information about the failure. */
  154. return 0;
  155. }
  156. mbedtls_test_fail(test, line_no, filename);
  157. (void) mbedtls_snprintf(mbedtls_test_info.line1,
  158. sizeof(mbedtls_test_info.line1),
  159. "lhs = 0x%016llx = %lld",
  160. (unsigned long long) value1, value1);
  161. (void) mbedtls_snprintf(mbedtls_test_info.line2,
  162. sizeof(mbedtls_test_info.line2),
  163. "rhs = 0x%016llx = %lld",
  164. (unsigned long long) value2, value2);
  165. return 0;
  166. }
  167. int mbedtls_test_unhexify(unsigned char *obuf,
  168. size_t obufmax,
  169. const char *ibuf,
  170. size_t *len)
  171. {
  172. unsigned char uc, uc2;
  173. *len = strlen(ibuf);
  174. /* Must be even number of bytes. */
  175. if ((*len) & 1) {
  176. return -1;
  177. }
  178. *len /= 2;
  179. if ((*len) > obufmax) {
  180. return -1;
  181. }
  182. while (*ibuf != 0) {
  183. if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
  184. return -1;
  185. }
  186. if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
  187. return -1;
  188. }
  189. *(obuf++) = (uc << 4) | uc2;
  190. }
  191. return 0;
  192. }
  193. void mbedtls_test_hexify(unsigned char *obuf,
  194. const unsigned char *ibuf,
  195. int len)
  196. {
  197. unsigned char l, h;
  198. while (len != 0) {
  199. h = *ibuf / 16;
  200. l = *ibuf % 16;
  201. if (h < 10) {
  202. *obuf++ = '0' + h;
  203. } else {
  204. *obuf++ = 'a' + h - 10;
  205. }
  206. if (l < 10) {
  207. *obuf++ = '0' + l;
  208. } else {
  209. *obuf++ = 'a' + l - 10;
  210. }
  211. ++ibuf;
  212. len--;
  213. }
  214. }
  215. unsigned char *mbedtls_test_zero_alloc(size_t len)
  216. {
  217. void *p;
  218. size_t actual_len = (len != 0) ? len : 1;
  219. p = mbedtls_calloc(1, actual_len);
  220. TEST_HELPER_ASSERT(p != NULL);
  221. memset(p, 0x00, actual_len);
  222. return p;
  223. }
  224. unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
  225. {
  226. unsigned char *obuf;
  227. size_t len;
  228. *olen = strlen(ibuf) / 2;
  229. if (*olen == 0) {
  230. return mbedtls_test_zero_alloc(*olen);
  231. }
  232. obuf = mbedtls_calloc(1, *olen);
  233. TEST_HELPER_ASSERT(obuf != NULL);
  234. TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
  235. return obuf;
  236. }
  237. int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
  238. uint32_t a_len, uint32_t b_len)
  239. {
  240. int ret = 0;
  241. uint32_t i = 0;
  242. if (a_len != b_len) {
  243. return -1;
  244. }
  245. for (i = 0; i < a_len; i++) {
  246. if (a[i] != b[i]) {
  247. ret = -1;
  248. break;
  249. }
  250. }
  251. return ret;
  252. }
  253. #if defined(MBEDTLS_TEST_HOOKS)
  254. void mbedtls_test_err_add_check(int high, int low,
  255. const char *file, int line)
  256. {
  257. /* Error codes are always negative (a value of zero is a success) however
  258. * their positive opposites can be easier to understand. The following
  259. * examples given in comments have been made positive for ease of
  260. * understanding. The structure of an error code is such:
  261. *
  262. * shhhhhhhhlllllll
  263. *
  264. * s = sign bit.
  265. * h = high level error code (includes high level module ID (bits 12..14)
  266. * and module-dependent error code (bits 7..11)).
  267. * l = low level error code.
  268. */
  269. if (high > -0x1000 && high != 0) {
  270. /* high < 0001000000000000
  271. * No high level module ID bits are set.
  272. */
  273. mbedtls_test_fail("'high' is not a high-level error code",
  274. line, file);
  275. } else if (high < -0x7F80) {
  276. /* high > 0111111110000000
  277. * Error code is greater than the largest allowed high level module ID.
  278. */
  279. mbedtls_test_fail("'high' error code is greater than 15 bits",
  280. line, file);
  281. } else if ((high & 0x7F) != 0) {
  282. /* high & 0000000001111111
  283. * Error code contains low level error code bits.
  284. */
  285. mbedtls_test_fail("'high' contains a low-level error code",
  286. line, file);
  287. } else if (low < -0x007F) {
  288. /* low > 0000000001111111
  289. * Error code contains high or module level error code bits.
  290. */
  291. mbedtls_test_fail("'low' error code is greater than 7 bits",
  292. line, file);
  293. } else if (low > 0) {
  294. mbedtls_test_fail("'low' error code is greater than zero",
  295. line, file);
  296. }
  297. }
  298. #endif /* MBEDTLS_TEST_HOOKS */