Commit e6e77d354f0ebb9ddb2261126ec228c785ef4f33
Committed by
Tom Rini
1 parent
b0fe6abd71
Exists in
master
and in
54 other branches
Implement verify option for sha1sum command
Loosely based on CONFIG_CRC32_VERIFY. The sum to verify against can be in memory, in a variable, or the last parameter to the function directly. Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
Showing 1 changed file with 105 additions and 0 deletions Side-by-side Diff
common/cmd_sha1sum.c
1 | 1 | /* |
2 | + * (C) Copyright 2011 | |
3 | + * Joe Hershberger, National Instruments, joe.hershberger@ni.com | |
4 | + * | |
2 | 5 | * (C) Copyright 2000 |
3 | 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
4 | 7 | * |
... | ... | @@ -25,6 +28,96 @@ |
25 | 28 | #include <command.h> |
26 | 29 | #include <sha1.h> |
27 | 30 | |
31 | +#ifdef CONFIG_SHA1SUM_VERIFY | |
32 | +static int parse_verify_sum(char *verify_str, u8 *vsum) | |
33 | +{ | |
34 | + if (*verify_str == '*') { | |
35 | + u8 *ptr; | |
36 | + | |
37 | + ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16); | |
38 | + memcpy(vsum, ptr, 20); | |
39 | + } else { | |
40 | + unsigned int i; | |
41 | + char *vsum_str; | |
42 | + | |
43 | + if (strlen(verify_str) == 40) | |
44 | + vsum_str = verify_str; | |
45 | + else { | |
46 | + vsum_str = getenv(verify_str); | |
47 | + if (vsum_str == NULL || strlen(vsum_str) != 40) | |
48 | + return 1; | |
49 | + } | |
50 | + | |
51 | + for (i = 0; i < 20; i++) { | |
52 | + char *nullp = vsum_str + (i + 1) * 2; | |
53 | + char end = *nullp; | |
54 | + | |
55 | + *nullp = '\0'; | |
56 | + *(u8 *)(vsum + i) = | |
57 | + simple_strtoul(vsum_str + (i * 2), NULL, 16); | |
58 | + *nullp = end; | |
59 | + } | |
60 | + } | |
61 | + return 0; | |
62 | +} | |
63 | + | |
64 | +int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
65 | +{ | |
66 | + ulong addr, len; | |
67 | + unsigned int i; | |
68 | + u8 output[20]; | |
69 | + u8 vsum[20]; | |
70 | + int verify = 0; | |
71 | + int ac; | |
72 | + char * const *av; | |
73 | + | |
74 | + if (argc < 3) | |
75 | + return CMD_RET_USAGE; | |
76 | + | |
77 | + av = argv + 1; | |
78 | + ac = argc - 1; | |
79 | + if (strcmp(*av, "-v") == 0) { | |
80 | + verify = 1; | |
81 | + av++; | |
82 | + ac--; | |
83 | + if (ac < 3) | |
84 | + return CMD_RET_USAGE; | |
85 | + } | |
86 | + | |
87 | + addr = simple_strtoul(*av++, NULL, 16); | |
88 | + len = simple_strtoul(*av++, NULL, 16); | |
89 | + | |
90 | + sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1); | |
91 | + | |
92 | + if (!verify) { | |
93 | + printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1); | |
94 | + for (i = 0; i < 20; i++) | |
95 | + printf("%02x", output[i]); | |
96 | + printf("\n"); | |
97 | + } else { | |
98 | + char *verify_str = *av++; | |
99 | + | |
100 | + if (parse_verify_sum(verify_str, vsum)) { | |
101 | + printf("ERROR: %s does not contain a valid SHA1 sum\n", | |
102 | + verify_str); | |
103 | + return 1; | |
104 | + } | |
105 | + if (memcmp(output, vsum, 20) != 0) { | |
106 | + printf("SHA1 for %08lx ... %08lx ==> ", addr, | |
107 | + addr + len - 1); | |
108 | + for (i = 0; i < 20; i++) | |
109 | + printf("%02x", output[i]); | |
110 | + printf(" != "); | |
111 | + for (i = 0; i < 20; i++) | |
112 | + printf("%02x", vsum[i]); | |
113 | + printf(" ** ERROR **\n"); | |
114 | + return 1; | |
115 | + } | |
116 | + } | |
117 | + | |
118 | + return 0; | |
119 | +} | |
120 | +#else | |
28 | 121 | static int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
29 | 122 | { |
30 | 123 | unsigned long addr, len; |
31 | 124 | |
32 | 125 | |
33 | 126 | |
... | ... | @@ -45,10 +138,22 @@ |
45 | 138 | |
46 | 139 | return 0; |
47 | 140 | } |
141 | +#endif | |
48 | 142 | |
143 | +#ifdef CONFIG_SHA1SUM_VERIFY | |
49 | 144 | U_BOOT_CMD( |
145 | + sha1sum, 5, 1, do_sha1sum, | |
146 | + "compute SHA1 message digest", | |
147 | + "address count\n" | |
148 | + " - compute SHA1 message digest\n" | |
149 | + "sha1sum -v address count [*]sum\n" | |
150 | + " - verify sha1sum of memory area" | |
151 | +); | |
152 | +#else | |
153 | +U_BOOT_CMD( | |
50 | 154 | sha1sum, 3, 1, do_sha1sum, |
51 | 155 | "compute SHA1 message digest", |
52 | 156 | "address count" |
53 | 157 | ); |
158 | +#endif |