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