blob: afb0ae570923b231a82257c100b27b4b398aef22 [file] [log] [blame]
Paul Bakkerfb6c7e22011-01-21 10:21:11 +00001/*
2 * generic message digest layer demonstration program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakkerfb6c7e22011-01-21 10:21:11 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010032#include <stdlib.h>
33#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010035#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010036#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +010037#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/md.h"
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000042
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <stdio.h>
44#include <string.h>
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000048int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Andrzej Kurek825ebd42020-05-18 11:47:25 -040051 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000052}
53#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010054
Jarno Lamsa642596e2019-10-04 12:52:42 +030055#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
56int mbedtls_hardware_poll( void *data, unsigned char *output,
57 size_t len, size_t *olen )
58{
59 size_t i;
60 (void) data;
61 for( i = 0; i < len; ++i )
62 output[i] = rand();
63 *olen = len;
64 return( 0 );
65}
66#endif
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010067
Hanno Beckera5cedbc2019-07-17 11:21:02 +010068static int generic_wrapper( mbedtls_md_handle_t md_info, char *filename, unsigned char *sum )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 int ret = mbedtls_md_file( md_info, filename, sum );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000071
72 if( ret == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000074
75 if( ret == 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000077
78 return( ret );
79}
80
Hanno Beckera5cedbc2019-07-17 11:21:02 +010081static int generic_print( mbedtls_md_handle_t md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000082{
83 int i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000085
86 if( generic_wrapper( md_info, filename, sum ) != 0 )
87 return( 1 );
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
90 mbedtls_printf( "%02x", sum[i] );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 mbedtls_printf( " %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000093 return( 0 );
94}
95
Hanno Beckera5cedbc2019-07-17 11:21:02 +010096static int generic_check( mbedtls_md_handle_t md_info, char *filename )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +000097{
98 int i;
99 size_t n;
100 FILE *f;
101 int nb_err1, nb_err2;
102 int nb_tot1, nb_tot2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100104 char line[1024];
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100105 char diff;
Paul Bakkerd1fe7aa2016-05-12 12:46:02 +0100106#if defined(__clang_analyzer__)
107 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
108#else
109 char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
110#endif
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000111
112 if( ( f = fopen( filename, "rb" ) ) == NULL )
113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf( "failed to open: %s\n", filename );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000115 return( 1 );
116 }
117
118 nb_err1 = nb_err2 = 0;
119 nb_tot1 = nb_tot2 = 0;
120
121 memset( line, 0, sizeof( line ) );
122
123 n = sizeof( line );
124
Paul Bakker23986e52011-04-24 08:57:21 +0000125 while( fgets( line, (int) n - 1, f ) != NULL )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000126 {
127 n = strlen( line );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000132 continue;
133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000138 continue;
139 }
140
141 if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
142 if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
143
144 nb_tot1++;
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000147 {
148 nb_err1++;
149 continue;
150 }
151
152 nb_tot2++;
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000155 sprintf( buf + i * 2, "%02x", sum[i] );
156
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100157 /* Use constant-time buffer comparison */
158 diff = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
Manuel Pégourié-Gonnard291f9af2013-10-28 12:51:32 +0100160 diff |= line[i] ^ buf[i];
161
162 if( diff != 0 )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000163 {
164 nb_err2++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000166 }
167
168 n = sizeof( line );
169 }
170
171 if( nb_err1 != 0 )
172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( "WARNING: %d (out of %d) input files could "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000174 "not be read\n", nb_err1, nb_tot1 );
175 }
176
177 if( nb_err2 != 0 )
178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000180 "not match\n", nb_err2, nb_tot2 );
181 }
182
Paul Bakker64abd832014-02-06 15:03:06 +0100183 fclose( f );
184
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000185 return( nb_err1 != 0 || nb_err2 != 0 );
186}
187
188int main( int argc, char *argv[] )
189{
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100190 int ret = 1, i;
191 int exit_code = MBEDTLS_EXIT_FAILURE;
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100192 mbedtls_md_handle_t md_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_md_context_t md_ctx;
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_md_init( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000196
197 if( argc == 1 )
198 {
199 const int *list;
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
202 mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_printf( "\nAvailable message digests:\n" );
205 list = mbedtls_md_list();
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000206 while( *list )
207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 md_info = mbedtls_md_info_from_type( *list );
209 mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000210 list++;
211 }
212
Paul Bakkercce9d772011-11-18 14:26:47 +0000213#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( "\n Press Enter to exit this program.\n" );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000215 fflush( stdout ); getchar();
216#endif
217
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400218 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000219 }
220
221 /*
222 * Read the MD from the command line
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 md_info = mbedtls_md_info_from_string( argv[1] );
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100225 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400228 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000229 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400233 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000234 }
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000235
236 ret = 0;
237 if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
238 {
239 ret |= generic_check( md_info, argv[3] );
240 goto exit;
241 }
242
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000243 for( i = 2; i < argc; i++ )
244 ret |= generic_print( md_info, argv[i] );
245
Andres Amaya Garciadabd78f2018-04-29 22:35:36 +0100246 if ( ret == 0 )
247 exit_code = MBEDTLS_EXIT_SUCCESS;
248
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000249exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_md_free( &md_ctx );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000251
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400252 mbedtls_exit( exit_code );
Paul Bakkerfb6c7e22011-01-21 10:21:11 +0000253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */