1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 #include "tomcrypt_private.h"
5
6 /**
7 @file compare_testvector.c
8 Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel
9 */
10
11 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
s_print_hex(const char * what,const void * v,const unsigned long l)12 static void s_print_hex(const char* what, const void* v, const unsigned long l)
13 {
14 const unsigned char* p = v;
15 unsigned long x, y = 0, z;
16 fprintf(stderr, "%s contents: \n", what);
17 for (x = 0; x < l; ) {
18 fprintf(stderr, "%02X ", p[x]);
19 if (!(++x % 16) || x == l) {
20 if((x % 16) != 0) {
21 z = 16 - (x % 16);
22 if(z >= 8)
23 fprintf(stderr, " ");
24 for (; z != 0; --z) {
25 fprintf(stderr, " ");
26 }
27 }
28 fprintf(stderr, " | ");
29 for(; y < x; y++) {
30 if((y % 8) == 0)
31 fprintf(stderr, " ");
32 if(isgraph(p[y]))
33 fprintf(stderr, "%c", p[y]);
34 else
35 fprintf(stderr, ".");
36 }
37 fprintf(stderr, "\n");
38 }
39 else if((x % 8) == 0) {
40 fprintf(stderr, " ");
41 }
42 }
43 }
44 #endif
45
46 /**
47 Compare two test-vectors
48
49 @param is The data as it is
50 @param is_len The length of is
51 @param should The data as it should
52 @param should_len The length of should
53 @param what The type of the data
54 @param which The iteration count
55 @return 0 on equality, -1 or 1 on difference
56 */
compare_testvector(const void * is,const unsigned long is_len,const void * should,const unsigned long should_len,const char * what,int which)57 int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
58 {
59 int res = 0;
60 if(is_len != should_len) {
61 res = is_len > should_len ? -1 : 1;
62 } else {
63 res = XMEMCMP(is, should, is_len);
64 }
65 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
66 if (res != 0) {
67 fprintf(stderr, "Testvector #%i(0x%x) of %s failed:\n", which, which, what);
68 s_print_hex("SHOULD", should, should_len);
69 s_print_hex("IS ", is, is_len);
70 #if LTC_TEST_DBG > 1
71 } else {
72 fprintf(stderr, "Testvector #%i(0x%x) of %s passed!\n", which, which, what);
73 #endif
74 }
75 #else
76 LTC_UNUSED_PARAM(which);
77 LTC_UNUSED_PARAM(what);
78 #endif
79
80 return res;
81 }
82