3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <u-boot/md5.h>
16 * Store the resulting sum to an address or variable
18 static void store_result(const u8 *sum, const char *dest)
25 ptr = (u8 *)simple_strtoul(dest + 1, NULL, 16);
26 for (i = 0; i < 16; i++)
30 char *str_ptr = str_output;
32 for (i = 0; i < 16; i++) {
33 sprintf(str_ptr, "%02x", sum[i]);
37 setenv(dest, str_output);
41 #ifdef CONFIG_MD5SUM_VERIFY
42 static int parse_verify_sum(char *verify_str, u8 *vsum)
44 if (*verify_str == '*') {
47 ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
48 memcpy(vsum, ptr, 16);
53 if (strlen(verify_str) == 32)
54 vsum_str = verify_str;
56 vsum_str = getenv(verify_str);
57 if (vsum_str == NULL || strlen(vsum_str) != 32)
61 for (i = 0; i < 16; i++) {
62 char *nullp = vsum_str + (i + 1) * 2;
67 simple_strtoul(vsum_str + (i * 2), NULL, 16);
74 int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
89 if (strcmp(*av, "-v") == 0) {
97 addr = simple_strtoul(*av++, NULL, 16);
98 len = simple_strtoul(*av++, NULL, 16);
100 md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
103 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
104 for (i = 0; i < 16; i++)
105 printf("%02x", output[i]);
109 store_result(output, *av);
111 char *verify_str = *av++;
113 if (parse_verify_sum(verify_str, vsum)) {
114 printf("ERROR: %s does not contain a valid md5 sum\n",
118 if (memcmp(output, vsum, 16) != 0) {
119 printf("md5 for %08lx ... %08lx ==> ", addr,
121 for (i = 0; i < 16; i++)
122 printf("%02x", output[i]);
124 for (i = 0; i < 16; i++)
125 printf("%02x", vsum[i]);
126 printf(" ** ERROR **\n");
134 static int do_md5sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
136 unsigned long addr, len;
141 return CMD_RET_USAGE;
143 addr = simple_strtoul(argv[1], NULL, 16);
144 len = simple_strtoul(argv[2], NULL, 16);
146 md5_wd((unsigned char *) addr, len, output, CHUNKSZ_MD5);
147 printf("md5 for %08lx ... %08lx ==> ", addr, addr + len - 1);
148 for (i = 0; i < 16; i++)
149 printf("%02x", output[i]);
153 store_result(output, argv[3]);
159 #ifdef CONFIG_MD5SUM_VERIFY
161 md5sum, 5, 1, do_md5sum,
162 "compute MD5 message digest",
163 "address count [[*]sum]\n"
164 " - compute MD5 message digest [save to sum]\n"
165 "md5sum -v address count [*]sum\n"
166 " - verify md5sum of memory area"
170 md5sum, 4, 1, do_md5sum,
171 "compute MD5 message digest",
172 "address count [[*]sum]\n"
173 " - compute MD5 message digest [save to sum]"