1 /*
2  *  generic message digest layer demonstration program
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  */
7 
8 #include "mbedtls/build_info.h"
9 
10 #include "mbedtls/platform.h"
11 
12 #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
13 #include "mbedtls/md.h"
14 
15 #include <stdio.h>
16 #include <string.h>
17 #endif
18 
19 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
main(void)20 int main(void)
21 {
22     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
23     mbedtls_exit(0);
24 }
25 #else
26 
27 
generic_wrapper(const mbedtls_md_info_t * md_info,char * filename,unsigned char * sum)28 static int generic_wrapper(const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum)
29 {
30     int ret = mbedtls_md_file(md_info, filename, sum);
31 
32     if (ret == 1) {
33         mbedtls_fprintf(stderr, "failed to open: %s\n", filename);
34     }
35 
36     if (ret == 2) {
37         mbedtls_fprintf(stderr, "failed to read: %s\n", filename);
38     }
39 
40     return ret;
41 }
42 
generic_print(const mbedtls_md_info_t * md_info,char * filename)43 static int generic_print(const mbedtls_md_info_t *md_info, char *filename)
44 {
45     int i;
46     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
47 
48     if (generic_wrapper(md_info, filename, sum) != 0) {
49         return 1;
50     }
51 
52     for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
53         mbedtls_printf("%02x", sum[i]);
54     }
55 
56     mbedtls_printf("  %s\n", filename);
57     return 0;
58 }
59 
generic_check(const mbedtls_md_info_t * md_info,char * filename)60 static int generic_check(const mbedtls_md_info_t *md_info, char *filename)
61 {
62     int i;
63     size_t n;
64     FILE *f;
65     int nb_err1, nb_err2;
66     int nb_tot1, nb_tot2;
67     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
68     char line[1024];
69     char diff;
70 #if defined(__clang_analyzer__)
71     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
72 #else
73     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
74 #endif
75 
76     if ((f = fopen(filename, "rb")) == NULL) {
77         mbedtls_printf("failed to open: %s\n", filename);
78         return 1;
79     }
80 
81     nb_err1 = nb_err2 = 0;
82     nb_tot1 = nb_tot2 = 0;
83 
84     memset(line, 0, sizeof(line));
85 
86     n = sizeof(line);
87 
88     while (fgets(line, (int) n - 1, f) != NULL) {
89         n = strlen(line);
90 
91         if (n < (size_t) 2 * mbedtls_md_get_size(md_info) + 4) {
92             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name(md_info));
93             continue;
94         }
95 
96         if (line[2 * mbedtls_md_get_size(md_info)] != ' ' ||
97             line[2 * mbedtls_md_get_size(md_info) + 1] != ' ') {
98             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name(md_info));
99             continue;
100         }
101 
102         if (line[n - 1] == '\n') {
103             n--; line[n] = '\0';
104         }
105         if (line[n - 1] == '\r') {
106             n--; line[n] = '\0';
107         }
108 
109         nb_tot1++;
110 
111         if (generic_wrapper(md_info, line + 2 + 2 * mbedtls_md_get_size(md_info), sum) != 0) {
112             nb_err1++;
113             continue;
114         }
115 
116         nb_tot2++;
117 
118         for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
119             sprintf(buf + i * 2, "%02x", sum[i]);
120         }
121 
122         /* Use constant-time buffer comparison */
123         diff = 0;
124         for (i = 0; i < 2 * mbedtls_md_get_size(md_info); i++) {
125             diff |= line[i] ^ buf[i];
126         }
127 
128         if (diff != 0) {
129             nb_err2++;
130             mbedtls_fprintf(stderr, "wrong checksum: %s\n", line + 66);
131         }
132 
133         n = sizeof(line);
134     }
135 
136     if (nb_err1 != 0) {
137         mbedtls_printf("WARNING: %d (out of %d) input files could "
138                        "not be read\n", nb_err1, nb_tot1);
139     }
140 
141     if (nb_err2 != 0) {
142         mbedtls_printf("WARNING: %d (out of %d) computed checksums did "
143                        "not match\n", nb_err2, nb_tot2);
144     }
145 
146     fclose(f);
147 
148     return nb_err1 != 0 || nb_err2 != 0;
149 }
150 
main(int argc,char * argv[])151 int main(int argc, char *argv[])
152 {
153     int ret = 1, i;
154     int exit_code = MBEDTLS_EXIT_FAILURE;
155     const mbedtls_md_info_t *md_info;
156     mbedtls_md_context_t md_ctx;
157 
158     mbedtls_md_init(&md_ctx);
159 
160     if (argc < 2) {
161         const int *list;
162 
163         mbedtls_printf("print mode:  generic_sum <mbedtls_md> <file> <file> ...\n");
164         mbedtls_printf("check mode:  generic_sum <mbedtls_md> -c <checksum file>\n");
165 
166         mbedtls_printf("\nAvailable message digests:\n");
167         list = mbedtls_md_list();
168         while (*list) {
169             md_info = mbedtls_md_info_from_type(*list);
170             mbedtls_printf("  %s\n", mbedtls_md_get_name(md_info));
171             list++;
172         }
173 
174         mbedtls_exit(exit_code);
175     }
176 
177     /*
178      * Read the MD from the command line
179      */
180     md_info = mbedtls_md_info_from_string(argv[1]);
181     if (md_info == NULL) {
182         mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[1]);
183         mbedtls_exit(exit_code);
184     }
185     if (mbedtls_md_setup(&md_ctx, md_info, 0)) {
186         mbedtls_fprintf(stderr, "Failed to initialize context.\n");
187         mbedtls_exit(exit_code);
188     }
189 
190     ret = 0;
191     if (argc == 4 && strcmp("-c", argv[2]) == 0) {
192         ret |= generic_check(md_info, argv[3]);
193         goto exit;
194     }
195 
196     for (i = 2; i < argc; i++) {
197         ret |= generic_print(md_info, argv[i]);
198     }
199 
200     if (ret == 0) {
201         exit_code = MBEDTLS_EXIT_SUCCESS;
202     }
203 
204 exit:
205     mbedtls_md_free(&md_ctx);
206 
207     mbedtls_exit(exit_code);
208 }
209 #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
210