cmd_sha1sum.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * (C) Copyright 2011
  3. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  4. *
  5. * (C) Copyright 2000
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <sha1.h>
  29. #ifdef CONFIG_SHA1SUM_VERIFY
  30. static int parse_verify_sum(char *verify_str, u8 *vsum)
  31. {
  32. if (*verify_str == '*') {
  33. u8 *ptr;
  34. ptr = (u8 *)simple_strtoul(verify_str + 1, NULL, 16);
  35. memcpy(vsum, ptr, 20);
  36. } else {
  37. unsigned int i;
  38. char *vsum_str;
  39. if (strlen(verify_str) == 40)
  40. vsum_str = verify_str;
  41. else {
  42. vsum_str = getenv(verify_str);
  43. if (vsum_str == NULL || strlen(vsum_str) != 40)
  44. return 1;
  45. }
  46. for (i = 0; i < 20; i++) {
  47. char *nullp = vsum_str + (i + 1) * 2;
  48. char end = *nullp;
  49. *nullp = '\0';
  50. *(u8 *)(vsum + i) =
  51. simple_strtoul(vsum_str + (i * 2), NULL, 16);
  52. *nullp = end;
  53. }
  54. }
  55. return 0;
  56. }
  57. int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  58. {
  59. ulong addr, len;
  60. unsigned int i;
  61. u8 output[20];
  62. u8 vsum[20];
  63. int verify = 0;
  64. int ac;
  65. char * const *av;
  66. if (argc < 3)
  67. return CMD_RET_USAGE;
  68. av = argv + 1;
  69. ac = argc - 1;
  70. if (strcmp(*av, "-v") == 0) {
  71. verify = 1;
  72. av++;
  73. ac--;
  74. if (ac < 3)
  75. return CMD_RET_USAGE;
  76. }
  77. addr = simple_strtoul(*av++, NULL, 16);
  78. len = simple_strtoul(*av++, NULL, 16);
  79. sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
  80. if (!verify) {
  81. printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  82. for (i = 0; i < 20; i++)
  83. printf("%02x", output[i]);
  84. printf("\n");
  85. } else {
  86. char *verify_str = *av++;
  87. if (parse_verify_sum(verify_str, vsum)) {
  88. printf("ERROR: %s does not contain a valid SHA1 sum\n",
  89. verify_str);
  90. return 1;
  91. }
  92. if (memcmp(output, vsum, 20) != 0) {
  93. printf("SHA1 for %08lx ... %08lx ==> ", addr,
  94. addr + len - 1);
  95. for (i = 0; i < 20; i++)
  96. printf("%02x", output[i]);
  97. printf(" != ");
  98. for (i = 0; i < 20; i++)
  99. printf("%02x", vsum[i]);
  100. printf(" ** ERROR **\n");
  101. return 1;
  102. }
  103. }
  104. return 0;
  105. }
  106. #else
  107. static int do_sha1sum(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  108. {
  109. unsigned long addr, len;
  110. unsigned int i;
  111. u8 output[20];
  112. if (argc < 3)
  113. return CMD_RET_USAGE;
  114. addr = simple_strtoul(argv[1], NULL, 16);
  115. len = simple_strtoul(argv[2], NULL, 16);
  116. sha1_csum_wd((unsigned char *) addr, len, output, CHUNKSZ_SHA1);
  117. printf("SHA1 for %08lx ... %08lx ==> ", addr, addr + len - 1);
  118. for (i = 0; i < 20; i++)
  119. printf("%02x", output[i]);
  120. printf("\n");
  121. return 0;
  122. }
  123. #endif
  124. #ifdef CONFIG_SHA1SUM_VERIFY
  125. U_BOOT_CMD(
  126. sha1sum, 5, 1, do_sha1sum,
  127. "compute SHA1 message digest",
  128. "address count\n"
  129. " - compute SHA1 message digest\n"
  130. "sha1sum -v address count [*]sum\n"
  131. " - verify sha1sum of memory area"
  132. );
  133. #else
  134. U_BOOT_CMD(
  135. sha1sum, 3, 1, do_sha1sum,
  136. "compute SHA1 message digest",
  137. "address count"
  138. );
  139. #endif