generic_sum.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * generic message digest layer demonstration program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
  22. #include "mbedtls/md.h"
  23. #include <stdio.h>
  24. #include <string.h>
  25. #endif
  26. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
  27. int main(void)
  28. {
  29. mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
  30. mbedtls_exit(0);
  31. }
  32. #else
  33. static int generic_wrapper(const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum)
  34. {
  35. int ret = mbedtls_md_file(md_info, filename, sum);
  36. if (ret == 1) {
  37. mbedtls_fprintf(stderr, "failed to open: %s\n", filename);
  38. }
  39. if (ret == 2) {
  40. mbedtls_fprintf(stderr, "failed to read: %s\n", filename);
  41. }
  42. return ret;
  43. }
  44. static int generic_print(const mbedtls_md_info_t *md_info, char *filename)
  45. {
  46. int i;
  47. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  48. if (generic_wrapper(md_info, filename, sum) != 0) {
  49. return 1;
  50. }
  51. for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
  52. mbedtls_printf("%02x", sum[i]);
  53. }
  54. mbedtls_printf(" %s\n", filename);
  55. return 0;
  56. }
  57. static int generic_check(const mbedtls_md_info_t *md_info, char *filename)
  58. {
  59. int i;
  60. size_t n;
  61. FILE *f;
  62. int nb_err1, nb_err2;
  63. int nb_tot1, nb_tot2;
  64. unsigned char sum[MBEDTLS_MD_MAX_SIZE];
  65. char line[1024];
  66. char diff;
  67. #if defined(__clang_analyzer__)
  68. char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
  69. #else
  70. char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
  71. #endif
  72. if ((f = fopen(filename, "rb")) == NULL) {
  73. mbedtls_printf("failed to open: %s\n", filename);
  74. return 1;
  75. }
  76. nb_err1 = nb_err2 = 0;
  77. nb_tot1 = nb_tot2 = 0;
  78. memset(line, 0, sizeof(line));
  79. n = sizeof(line);
  80. while (fgets(line, (int) n - 1, f) != NULL) {
  81. n = strlen(line);
  82. if (n < (size_t) 2 * mbedtls_md_get_size(md_info) + 4) {
  83. mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name(md_info));
  84. continue;
  85. }
  86. if (line[2 * mbedtls_md_get_size(md_info)] != ' ' ||
  87. line[2 * mbedtls_md_get_size(md_info) + 1] != ' ') {
  88. mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name(md_info));
  89. continue;
  90. }
  91. if (line[n - 1] == '\n') {
  92. n--; line[n] = '\0';
  93. }
  94. if (line[n - 1] == '\r') {
  95. n--; line[n] = '\0';
  96. }
  97. nb_tot1++;
  98. if (generic_wrapper(md_info, line + 2 + 2 * mbedtls_md_get_size(md_info), sum) != 0) {
  99. nb_err1++;
  100. continue;
  101. }
  102. nb_tot2++;
  103. for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
  104. sprintf(buf + i * 2, "%02x", sum[i]);
  105. }
  106. /* Use constant-time buffer comparison */
  107. diff = 0;
  108. for (i = 0; i < 2 * mbedtls_md_get_size(md_info); i++) {
  109. diff |= line[i] ^ buf[i];
  110. }
  111. if (diff != 0) {
  112. nb_err2++;
  113. mbedtls_fprintf(stderr, "wrong checksum: %s\n", line + 66);
  114. }
  115. n = sizeof(line);
  116. }
  117. if (nb_err1 != 0) {
  118. mbedtls_printf("WARNING: %d (out of %d) input files could "
  119. "not be read\n", nb_err1, nb_tot1);
  120. }
  121. if (nb_err2 != 0) {
  122. mbedtls_printf("WARNING: %d (out of %d) computed checksums did "
  123. "not match\n", nb_err2, nb_tot2);
  124. }
  125. fclose(f);
  126. return nb_err1 != 0 || nb_err2 != 0;
  127. }
  128. int main(int argc, char *argv[])
  129. {
  130. int ret = 1, i;
  131. int exit_code = MBEDTLS_EXIT_FAILURE;
  132. const mbedtls_md_info_t *md_info;
  133. mbedtls_md_context_t md_ctx;
  134. mbedtls_md_init(&md_ctx);
  135. if (argc < 2) {
  136. const int *list;
  137. mbedtls_printf("print mode: generic_sum <mbedtls_md> <file> <file> ...\n");
  138. mbedtls_printf("check mode: generic_sum <mbedtls_md> -c <checksum file>\n");
  139. mbedtls_printf("\nAvailable message digests:\n");
  140. list = mbedtls_md_list();
  141. while (*list) {
  142. md_info = mbedtls_md_info_from_type(*list);
  143. mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
  144. list++;
  145. }
  146. mbedtls_exit(exit_code);
  147. }
  148. /*
  149. * Read the MD from the command line
  150. */
  151. md_info = mbedtls_md_info_from_string(argv[1]);
  152. if (md_info == NULL) {
  153. mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[1]);
  154. mbedtls_exit(exit_code);
  155. }
  156. if (mbedtls_md_setup(&md_ctx, md_info, 0)) {
  157. mbedtls_fprintf(stderr, "Failed to initialize context.\n");
  158. mbedtls_exit(exit_code);
  159. }
  160. ret = 0;
  161. if (argc == 4 && strcmp("-c", argv[2]) == 0) {
  162. ret |= generic_check(md_info, argv[3]);
  163. goto exit;
  164. }
  165. for (i = 2; i < argc; i++) {
  166. ret |= generic_print(md_info, argv[i]);
  167. }
  168. if (ret == 0) {
  169. exit_code = MBEDTLS_EXIT_SUCCESS;
  170. }
  171. exit:
  172. mbedtls_md_free(&md_ctx);
  173. mbedtls_exit(exit_code);
  174. }
  175. #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */