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