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