Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / programs / hash / generic_sum.c
1 /*
2  *  generic message digest layer demonstration program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_fprintf         fprintf
34 #define mbedtls_printf          printf
35 #define mbedtls_exit            exit
36 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39
40 #if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
41 #include "mbedtls/md.h"
42
43 #include <stdio.h>
44 #include <string.h>
45 #endif
46
47 #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
48 int main( void )
49 {
50     mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
51     return( 0 );
52 }
53 #else
54
55 #if defined(MBEDTLS_CHECK_PARAMS)
56 #include "mbedtls/platform_util.h"
57 void mbedtls_param_failed( const char *failure_condition,
58                            const char *file,
59                            int line )
60 {
61     mbedtls_printf( "%s:%i: Input param failed - %s\n",
62                     file, line, failure_condition );
63     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
64 }
65 #endif
66
67 static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
68 {
69     int ret = mbedtls_md_file( md_info, filename, sum );
70
71     if( ret == 1 )
72         mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
73
74     if( ret == 2 )
75         mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
76
77     return( ret );
78 }
79
80 static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
81 {
82     int i;
83     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
84
85     if( generic_wrapper( md_info, filename, sum ) != 0 )
86         return( 1 );
87
88     for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
89         mbedtls_printf( "%02x", sum[i] );
90
91     mbedtls_printf( "  %s\n", filename );
92     return( 0 );
93 }
94
95 static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
96 {
97     int i;
98     size_t n;
99     FILE *f;
100     int nb_err1, nb_err2;
101     int nb_tot1, nb_tot2;
102     unsigned char sum[MBEDTLS_MD_MAX_SIZE];
103     char line[1024];
104     char diff;
105 #if defined(__clang_analyzer__)
106     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
107 #else
108     char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
109 #endif
110
111     if( ( f = fopen( filename, "rb" ) ) == NULL )
112     {
113         mbedtls_printf( "failed to open: %s\n", filename );
114         return( 1 );
115     }
116
117     nb_err1 = nb_err2 = 0;
118     nb_tot1 = nb_tot2 = 0;
119
120     memset( line, 0, sizeof( line ) );
121
122     n = sizeof( line );
123
124     while( fgets( line, (int) n - 1, f ) != NULL )
125     {
126         n = strlen( line );
127
128         if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
129         {
130             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
131             continue;
132         }
133
134         if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
135         {
136             mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
137             continue;
138         }
139
140         if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
141         if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
142
143         nb_tot1++;
144
145         if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
146         {
147             nb_err1++;
148             continue;
149         }
150
151         nb_tot2++;
152
153         for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
154             sprintf( buf + i * 2, "%02x", sum[i] );
155
156         /* Use constant-time buffer comparison */
157         diff = 0;
158         for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
159             diff |= line[i] ^ buf[i];
160
161         if( diff != 0 )
162         {
163             nb_err2++;
164             mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
165         }
166
167         n = sizeof( line );
168     }
169
170     if( nb_err1 != 0 )
171     {
172         mbedtls_printf( "WARNING: %d (out of %d) input files could "
173                 "not be read\n", nb_err1, nb_tot1 );
174     }
175
176     if( nb_err2 != 0 )
177     {
178         mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
179                 "not match\n", nb_err2, nb_tot2 );
180     }
181
182     fclose( f );
183
184     return( nb_err1 != 0 || nb_err2 != 0 );
185 }
186
187 int main( int argc, char *argv[] )
188 {
189     int ret = 1, i;
190     int exit_code = MBEDTLS_EXIT_FAILURE;
191     const mbedtls_md_info_t *md_info;
192     mbedtls_md_context_t md_ctx;
193
194     mbedtls_md_init( &md_ctx );
195
196     if( argc == 1 )
197     {
198         const int *list;
199
200         mbedtls_printf( "print mode:  generic_sum <mbedtls_md> <file> <file> ...\n" );
201         mbedtls_printf( "check mode:  generic_sum <mbedtls_md> -c <checksum file>\n" );
202
203         mbedtls_printf( "\nAvailable message digests:\n" );
204         list = mbedtls_md_list();
205         while( *list )
206         {
207             md_info = mbedtls_md_info_from_type( *list );
208             mbedtls_printf( "  %s\n", mbedtls_md_get_name( md_info ) );
209             list++;
210         }
211
212 #if defined(_WIN32)
213         mbedtls_printf( "\n  Press Enter to exit this program.\n" );
214         fflush( stdout ); getchar();
215 #endif
216
217         return( exit_code );
218     }
219
220     /*
221      * Read the MD from the command line
222      */
223     md_info = mbedtls_md_info_from_string( argv[1] );
224     if( md_info == NULL )
225     {
226         mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
227         return( exit_code );
228     }
229     if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
230     {
231         mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
232         return( exit_code );
233     }
234
235     ret = 0;
236     if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
237     {
238         ret |= generic_check( md_info, argv[3] );
239         goto exit;
240     }
241
242     for( i = 2; i < argc; i++ )
243         ret |= generic_print( md_info, argv[i] );
244
245     if ( ret == 0 )
246         exit_code = MBEDTLS_EXIT_SUCCESS;
247
248 exit:
249     mbedtls_md_free( &md_ctx );
250
251     return( exit_code );
252 }
253 #endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */