Debian Codegraph @

tests/API/crypt/test_crapi_mdigest.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #if (defined OPENSCAP_ENABLE_MD5 && defined OPENSCAP_ENABLE_SHA1)
28
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <crapi/crapi.h>
39 #include <crapi/digest.h>
40
41 static int mem2hex (uint8_t *mem, size_t mlen, char *str, size_t slen)
42 {
43 const char ch[] = "0123456789abcdef";
44 register size_t i;
45
46 if (slen < (mlen * 2) + 1) {
47 errno = ENOBUFS;
48 return (-1);
49 }
50
51 for (i = 0; i < mlen; ++i) {
52 str[i*2 ] = ch[(mem[i] & 0xf0) >> 4];
53 str[i*2+1] = ch[(mem[i] & 0x0f)];
54 }
55
56 str[i*2] = '\0';
57
58 return (0);
59 }
60
61 int main (int argc, char *argv[])
62 {
63 uint8_t md5_dst[16];
64 size_t md5_dstlen = sizeof md5_dst;
65
66 uint8_t sha1_dst[20];
67 size_t sha1_dstlen = sizeof sha1_dst;
68
69 uint8_t sha256_dst[32];
70 size_t sha256_dstlen = sizeof sha256_dst;
71
72 char *orig_md5sum, comp_md5sum[(sizeof md5_dst * 2) + 1];
73 char *orig_sha1sum, comp_sha1sum[(sizeof sha1_dst * 2) + 1];
74 char *orig_sha256sum, comp_sha256sum[(sizeof sha256_dst * 2) + 1];
75 char *filename;
76 int fd;
77
78 if (argc != 5) {
79 fprintf (stderr, "Usage: %s <file> <md5sum> <sha1sum> <sha256sum>\n", argv[0]);
80 return (1);
81 }
82
83 filename = argv[1];
84 orig_md5sum = argv[2];
85 orig_sha1sum = argv[3];
86 orig_sha256sum = argv[4];
87
88 if (crapi_init (NULL) != 0) {
89 fprintf (stderr, "crapi_init() != 0\n");
90 abort ();
91 }
92
93 fd = open (filename, O_RDONLY);
94
95 if (fd < 0) {
96 perror ("open");
97 return (2);
98 }
99
100 if (crapi_mdigest_fd (fd, 3,
101 CRAPI_DIGEST_MD5, &md5_dst, &md5_dstlen,
102 CRAPI_DIGEST_SHA1, &sha1_dst, &sha1_dstlen,
103 CRAPI_DIGEST_SHA256, &sha256_dst, &sha256_dstlen) != 0)
104 {
105 fprintf (stderr, "crapi_digest() != 0\n");
106 abort ();
107 }
108
109 mem2hex (md5_dst, md5_dstlen, comp_md5sum, sizeof comp_md5sum);
110 mem2hex (sha1_dst, sha1_dstlen, comp_sha1sum, sizeof comp_sha1sum);
111 mem2hex (sha256_dst, sha256_dstlen, comp_sha256sum, sizeof comp_sha256sum);
112
113 if (strcmp (orig_md5sum, comp_md5sum) != 0) {
114 fprintf (stderr, "crapi_digest::MD5(%s) != %s (== %s)\n", filename, orig_md5sum, comp_md5sum);
115 abort ();
116 }
117
118 if (strcmp (orig_sha1sum, comp_sha1sum) != 0) {
119 fprintf (stderr, "crapi_digest::SHA1(%s) != %s (== %s)\n", filename, orig_sha1sum, comp_sha1sum);
120 abort ();
121 }
122
123 if (strcmp (orig_sha256sum, comp_sha256sum) != 0) {
124 fprintf (stderr, "crapi_digest::SHA256(%s) != %s (== %s)\n", filename, orig_sha256sum, comp_sha256sum);
125 abort ();
126 }
127
128 close (fd);
129
130 return (0);
131 }
132
133 #else
134
135 int main (int argc, char *argv[])
136 {
137 return 1;
138 }
139
140 #endif