3131/*
3232 * Reminder: update profiles in x509_crt.c when adding a new hash!
3333 */
34- static const int32_t supported_digests [] = {
34+ static const mbedtls_md_type_t supported_digests [] = {
3535 MBEDTLS_MD_SHA256 ,
3636 MBEDTLS_MD_NONE
3737};
3838
39- const int32_t * mbedtls_md_list ( void )
39+ const mbedtls_md_type_t * mbedtls_md_list ( void )
4040{
4141 return ( supported_digests );
4242}
4343
4444const mbedtls_md_info_t * mbedtls_md_info_from_type ( mbedtls_md_type_t md_type )
4545{
46- switch ( md_type )
46+ const mbedtls_md_info_t * md_info = NULL ;
47+
48+ switch ( md_type )
4749 {
4850 case MBEDTLS_MD_SHA256 :
49- return ( & mbedtls_sha256_info );
51+ md_info = & mbedtls_sha256_info ;
52+ break ;
5053 default :
51- return ( NULL ) ;
54+ break ;
5255 }
56+
57+ return md_info ;
5358}
5459
5560void mbedtls_md_init ( mbedtls_md_context_t * ctx )
5661{
57- memset ( ctx , 0 , sizeof ( mbedtls_md_context_t ) );
62+ ( void ) memset ( ctx , 0U , sizeof ( mbedtls_md_context_t ) );
5863}
5964
6065void mbedtls_md_free ( mbedtls_md_context_t * ctx )
6166{
62- if ( ctx == NULL )
63- return ;
64-
65- mbedtls_platform_zeroize ( ctx , sizeof ( mbedtls_md_context_t ) );
66- }
67-
68- int32_t mbedtls_md_clone ( mbedtls_md_context_t * dst ,
69- const mbedtls_md_context_t * src )
70- {
71- if ( dst == NULL || dst -> md_info == NULL ||
72- src == NULL || src -> md_info == NULL ||
73- dst -> md_info != src -> md_info )
74- {
75- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
67+ if ( ctx != NULL ) {
68+ (void ) mbedtls_platform_zeroize ( ctx , sizeof ( mbedtls_md_context_t ) );
7669 }
7770
78- dst -> md_info -> clone_func ( dst -> md_ctx , src -> md_ctx );
79-
80- return ( 0 );
71+ return ;
8172}
8273
8374int32_t mbedtls_md_setup ( mbedtls_md_context_t * ctx , const mbedtls_md_info_t * md_info )
8475{
85- if ( md_info == NULL || ctx == NULL )
86- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
87-
88- ctx -> md_info = md_info ;
89-
90- return ( 0 );
91- }
92-
93- int32_t mbedtls_md_starts ( mbedtls_md_context_t * ctx )
94- {
95- if ( ctx == NULL || ctx -> md_info == NULL )
96- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
97-
98- return ( ctx -> md_info -> starts_func ( ctx -> md_ctx ) );
99- }
76+ int32_t ret = 0 ;
10077
101- int32_t mbedtls_md_update ( mbedtls_md_context_t * ctx , const uint8_t * input , size_t ilen )
102- {
103- if ( ctx == NULL || ctx -> md_info == NULL )
104- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
105-
106- return ( ctx -> md_info -> update_func ( ctx -> md_ctx , input , ilen ) );
107- }
108-
109- int32_t mbedtls_md_finish ( mbedtls_md_context_t * ctx , uint8_t * output )
110- {
111- if ( ctx == NULL || ctx -> md_info == NULL )
112- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
113-
114- return ( ctx -> md_info -> finish_func ( ctx -> md_ctx , output ) );
115- }
116-
117- int32_t mbedtls_md ( const mbedtls_md_info_t * md_info , const uint8_t * input , size_t ilen ,
118- uint8_t * output )
119- {
120- if ( md_info == NULL )
121- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
78+ if ( ( md_info == NULL ) || ( ctx == NULL ) ) {
79+ ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
80+ } else {
81+ ctx -> md_info = md_info ;
82+ }
12283
123- return ( md_info -> digest_func ( input , ilen , output ) );
84+ return ( ret );
12485}
12586
12687int32_t mbedtls_md_hmac_starts ( mbedtls_md_context_t * ctx , const uint8_t * key , size_t keylen )
12788{
128- int32_t ret ;
89+ int32_t ret = 0 ;
12990 uint8_t sum [MBEDTLS_MD_MAX_SIZE ];
13091 uint8_t * ipad , * opad ;
92+ const uint8_t * temp_key = key ;
13193 size_t i ;
13294
133- if ( ctx == NULL || ctx -> md_info == NULL || ctx -> hmac_ctx == NULL )
134- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
135-
136- if ( keylen > (size_t ) ctx -> md_info -> block_size )
137- {
138- if ( ( ret = ctx -> md_info -> starts_func ( ctx -> md_ctx ) ) != 0 )
139- goto cleanup ;
140- if ( ( ret = ctx -> md_info -> update_func ( ctx -> md_ctx , key , keylen ) ) != 0 )
141- goto cleanup ;
142- if ( ( ret = ctx -> md_info -> finish_func ( ctx -> md_ctx , sum ) ) != 0 )
143- goto cleanup ;
144-
145- keylen = ctx -> md_info -> size ;
146- key = sum ;
95+ if ( ( ctx == NULL ) || ( ctx -> md_info == NULL ) || ( ctx -> hmac_ctx == NULL ) || ( temp_key == NULL ) ) {
96+ ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
14797 }
14898
149- ipad = (uint8_t * ) ctx -> hmac_ctx ;
150- opad = (uint8_t * ) ctx -> hmac_ctx + ctx -> md_info -> block_size ;
151-
152- memset ( ipad , 0x36 , ctx -> md_info -> block_size );
153- memset ( opad , 0x5C , ctx -> md_info -> block_size );
154-
155- for ( i = 0 ; i < keylen ; i ++ )
156- {
157- ipad [i ] = (uint8_t )( ipad [i ] ^ key [i ] );
158- opad [i ] = (uint8_t )( opad [i ] ^ key [i ] );
99+ if (ret == 0 ) {
100+ if ( keylen > (size_t ) ctx -> md_info -> block_size ) {
101+ ret = ctx -> md_info -> starts_func ( (void * ) ctx -> md_ctx );
102+ if ( ret == 0 ) {
103+ ret = ctx -> md_info -> update_func ( (void * ) ctx -> md_ctx , temp_key , keylen );
104+ if ( ret == 0 ) {
105+ ret = ctx -> md_info -> finish_func ( (void * ) ctx -> md_ctx , sum );
106+ }
107+ }
108+
109+ if ( ret == 0 ) {
110+ keylen = (size_t ) ctx -> md_info -> size ;
111+ temp_key = sum ;
112+ }
113+ }
114+
115+ if ( ret == 0 ) {
116+ ipad = (uint8_t * ) ctx -> hmac_ctx ;
117+ opad = (uint8_t * ) ctx -> hmac_ctx + ctx -> md_info -> block_size ;
118+
119+ (void ) memset ( ipad , 0x36U , (size_t ) ctx -> md_info -> block_size );
120+ (void ) memset ( opad , 0x5CU , (size_t ) ctx -> md_info -> block_size );
121+
122+ for ( i = 0U ; i < keylen ; i ++ ) {
123+ * ( ipad + i ) = (uint8_t )( * ( ipad + i ) ^ * ( temp_key + i ) );
124+ * ( opad + i ) = (uint8_t )( * ( opad + i ) ^ * ( temp_key + i ) );
125+ }
126+
127+ ret = ctx -> md_info -> starts_func ( (void * ) ctx -> md_ctx );
128+ if ( ret == 0 ) {
129+ ret = ctx -> md_info -> update_func ( (void * ) ctx -> md_ctx , ipad ,
130+ ctx -> md_info -> block_size );
131+ }
132+ }
133+ (void ) mbedtls_platform_zeroize ( sum , sizeof ( sum ) );
159134 }
160135
161- if ( ( ret = ctx -> md_info -> starts_func ( ctx -> md_ctx ) ) != 0 )
162- goto cleanup ;
163- if ( ( ret = ctx -> md_info -> update_func ( ctx -> md_ctx , ipad ,
164- ctx -> md_info -> block_size ) ) != 0 )
165- goto cleanup ;
166-
167- cleanup :
168- mbedtls_platform_zeroize ( sum , sizeof ( sum ) );
169-
170136 return ( ret );
171137}
172138
173139int32_t mbedtls_md_hmac_update ( mbedtls_md_context_t * ctx , const uint8_t * input , size_t ilen )
174140{
175- if ( ctx == NULL || ctx -> md_info == NULL || ctx -> hmac_ctx == NULL )
176- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
141+ int32_t ret ;
142+
143+ if ( ( ctx == NULL ) || ( ctx -> md_info == NULL ) || ( ctx -> hmac_ctx == NULL ) ) {
144+ ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
145+ } else {
146+ ret = ctx -> md_info -> update_func ( (void * ) ctx -> md_ctx , input , ilen );
147+ }
177148
178- return ( ctx -> md_info -> update_func ( ctx -> md_ctx , input , ilen ) );
149+ return ( ret );
179150}
180151
181152int32_t mbedtls_md_hmac_finish ( mbedtls_md_context_t * ctx , uint8_t * output )
182153{
183- int32_t ret ;
154+ int32_t ret = 0 ;
184155 uint8_t tmp [MBEDTLS_MD_MAX_SIZE ];
185156 uint8_t * opad ;
186157
187- if ( ctx == NULL || ctx -> md_info == NULL || ctx -> hmac_ctx == NULL )
188- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
189-
190- opad = (uint8_t * ) ctx -> hmac_ctx + ctx -> md_info -> block_size ;
191-
192- if ( ( ret = ctx -> md_info -> finish_func ( ctx -> md_ctx , tmp ) ) != 0 )
193- return ( ret );
194- if ( ( ret = ctx -> md_info -> starts_func ( ctx -> md_ctx ) ) != 0 )
195- return ( ret );
196- if ( ( ret = ctx -> md_info -> update_func ( ctx -> md_ctx , opad ,
197- ctx -> md_info -> block_size ) ) != 0 )
198- return ( ret );
199- if ( ( ret = ctx -> md_info -> update_func ( ctx -> md_ctx , tmp ,
200- ctx -> md_info -> size ) ) != 0 )
201- return ( ret );
202- return ( ctx -> md_info -> finish_func ( ctx -> md_ctx , output ) );
203- }
158+ if ( ( ctx == NULL ) || ( ctx -> md_info == NULL ) || ( ctx -> hmac_ctx == NULL ) ) {
159+ ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
160+ }
204161
205- int32_t mbedtls_md_hmac_reset ( mbedtls_md_context_t * ctx )
206- {
207- int32_t ret ;
208- uint8_t * ipad ;
162+ if ( ret == 0 ) {
163+ opad = (uint8_t * ) ctx -> hmac_ctx + ctx -> md_info -> block_size ;
209164
210- if ( ctx == NULL || ctx -> md_info == NULL || ctx -> hmac_ctx == NULL )
211- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
165+ ret = ctx -> md_info -> finish_func ( (void * ) ctx -> md_ctx , tmp );
166+ if ( ret == 0 ) {
167+ ret = ctx -> md_info -> starts_func ( (void * ) ctx -> md_ctx );
168+ }
169+ }
212170
213- ipad = (uint8_t * ) ctx -> hmac_ctx ;
171+ if ( ret == 0 ) {
172+ ret = ctx -> md_info -> update_func ( (void * ) ctx -> md_ctx , opad ,
173+ ctx -> md_info -> block_size );
174+ if ( ret == 0 ) {
175+ ret = ctx -> md_info -> update_func ( (void * ) ctx -> md_ctx , tmp ,
176+ ctx -> md_info -> size );
177+ }
178+
179+ if ( ret == 0 ) {
180+ ret = ctx -> md_info -> finish_func ( (void * ) ctx -> md_ctx ,
181+ (uint8_t * ) output );
182+ }
183+ }
214184
215- if ( ( ret = ctx -> md_info -> starts_func ( ctx -> md_ctx ) ) != 0 )
216- return ( ret );
217- return ( ctx -> md_info -> update_func ( ctx -> md_ctx , ipad ,
218- ctx -> md_info -> block_size ) );
185+ return ( ret );
219186}
220187
221188int32_t mbedtls_md_hmac ( const mbedtls_md_info_t * md_info ,
@@ -224,49 +191,41 @@ int32_t mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
224191 uint8_t * output )
225192{
226193 mbedtls_md_context_t ctx ;
227- int32_t ret ;
228-
229- if ( md_info == NULL )
230- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
194+ int32_t ret = 0 ;
231195
232- mbedtls_md_init ( & ctx );
196+ if ( md_info == NULL ) {
197+ ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA ;
198+ }
233199
234- if ( ( ret = mbedtls_md_setup ( & ctx , md_info ) ) ! = 0 )
235- goto cleanup ;
200+ if ( ret == 0 ) {
201+ mbedtls_md_init ( & ctx ) ;
236202
237- if ( ( ret = mbedtls_md_hmac_starts ( & ctx , key , keylen ) ) != 0 )
238- goto cleanup ;
239- if ( ( ret = mbedtls_md_hmac_update ( & ctx , input , ilen ) ) != 0 )
240- goto cleanup ;
241- if ( ( ret = mbedtls_md_hmac_finish ( & ctx , output ) ) != 0 )
242- goto cleanup ;
203+ ret = mbedtls_md_setup ( & ctx , md_info );
204+ if ( ret == 0 ) {
205+ ret = mbedtls_md_hmac_starts ( & ctx , key , keylen );
206+ }
243207
244- cleanup :
245- mbedtls_md_free ( & ctx );
208+ if ( ret == 0 ) {
209+ ret = mbedtls_md_hmac_update ( & ctx , input , ilen );
210+ }
246211
247- return ( ret );
248- }
212+ if ( ret == 0 ) {
213+ ret = mbedtls_md_hmac_finish ( & ctx , output );
214+ }
249215
250- int32_t mbedtls_md_process ( mbedtls_md_context_t * ctx , const uint8_t * data )
251- {
252- if ( ctx == NULL || ctx -> md_info == NULL )
253- return ( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
216+ mbedtls_md_free ( & ctx );
217+ }
254218
255- return ( ctx -> md_info -> process_func ( ctx -> md_ctx , data ) );
219+ return ( ret );
256220}
257221
258222uint8_t mbedtls_md_get_size ( const mbedtls_md_info_t * md_info )
259223{
260- if ( md_info == NULL )
261- return ( 0 );
224+ uint8_t ret = 0U ;
262225
263- return md_info -> size ;
264- }
265-
266- mbedtls_md_type_t mbedtls_md_get_type ( const mbedtls_md_info_t * md_info )
267- {
268- if ( md_info == NULL )
269- return ( MBEDTLS_MD_NONE );
226+ if ( md_info != NULL ) {
227+ ret = (uint8_t ) md_info -> size ;
228+ }
270229
271- return md_info -> type ;
230+ return ( ret ) ;
272231}
0 commit comments