ReactOS 0.4.15-dev-7953-g1f49173
md.c
Go to the documentation of this file.
1
51#if !defined(MBEDTLS_CONFIG_FILE)
52#include "mbedtls/config.h"
53#else
54#include MBEDTLS_CONFIG_FILE
55#endif
56
57#if defined(MBEDTLS_MD_C)
58
59#include "mbedtls/md.h"
60#include "mbedtls/md_internal.h"
62
63#if defined(MBEDTLS_PLATFORM_C)
64#include "mbedtls/platform.h"
65#else
66#include <stdlib.h>
67#define mbedtls_calloc calloc
68#define mbedtls_free free
69#endif
70
71#include <string.h>
72
73#if defined(MBEDTLS_FS_IO)
74#include <stdio.h>
75#endif
76
77/*
78 * Reminder: update profiles in x509_crt.c when adding a new hash!
79 */
80static const int supported_digests[] = {
81
82#if defined(MBEDTLS_SHA512_C)
85#endif
86
87#if defined(MBEDTLS_SHA256_C)
90#endif
91
92#if defined(MBEDTLS_SHA1_C)
94#endif
95
96#if defined(MBEDTLS_RIPEMD160_C)
98#endif
99
100#if defined(MBEDTLS_MD5_C)
102#endif
103
104#if defined(MBEDTLS_MD4_C)
106#endif
107
108#if defined(MBEDTLS_MD2_C)
110#endif
111
113};
114
115const int *mbedtls_md_list( void )
116{
117 return( supported_digests );
118}
119
120const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name )
121{
122 if( NULL == md_name )
123 return( NULL );
124
125 /* Get the appropriate digest information */
126#if defined(MBEDTLS_MD2_C)
127 if( !strcmp( "MD2", md_name ) )
129#endif
130#if defined(MBEDTLS_MD4_C)
131 if( !strcmp( "MD4", md_name ) )
133#endif
134#if defined(MBEDTLS_MD5_C)
135 if( !strcmp( "MD5", md_name ) )
137#endif
138#if defined(MBEDTLS_RIPEMD160_C)
139 if( !strcmp( "RIPEMD160", md_name ) )
141#endif
142#if defined(MBEDTLS_SHA1_C)
143 if( !strcmp( "SHA1", md_name ) || !strcmp( "SHA", md_name ) )
145#endif
146#if defined(MBEDTLS_SHA256_C)
147 if( !strcmp( "SHA224", md_name ) )
149 if( !strcmp( "SHA256", md_name ) )
151#endif
152#if defined(MBEDTLS_SHA512_C)
153 if( !strcmp( "SHA384", md_name ) )
155 if( !strcmp( "SHA512", md_name ) )
157#endif
158 return( NULL );
159}
160
162{
163 switch( md_type )
164 {
165#if defined(MBEDTLS_MD2_C)
166 case MBEDTLS_MD_MD2:
167 return( &mbedtls_md2_info );
168#endif
169#if defined(MBEDTLS_MD4_C)
170 case MBEDTLS_MD_MD4:
171 return( &mbedtls_md4_info );
172#endif
173#if defined(MBEDTLS_MD5_C)
174 case MBEDTLS_MD_MD5:
175 return( &mbedtls_md5_info );
176#endif
177#if defined(MBEDTLS_RIPEMD160_C)
179 return( &mbedtls_ripemd160_info );
180#endif
181#if defined(MBEDTLS_SHA1_C)
182 case MBEDTLS_MD_SHA1:
183 return( &mbedtls_sha1_info );
184#endif
185#if defined(MBEDTLS_SHA256_C)
187 return( &mbedtls_sha224_info );
189 return( &mbedtls_sha256_info );
190#endif
191#if defined(MBEDTLS_SHA512_C)
193 return( &mbedtls_sha384_info );
195 return( &mbedtls_sha512_info );
196#endif
197 default:
198 return( NULL );
199 }
200}
201
203{
204 memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
205}
206
208{
209 if( ctx == NULL || ctx->md_info == NULL )
210 return;
211
212 if( ctx->md_ctx != NULL )
213 ctx->md_info->ctx_free_func( ctx->md_ctx );
214
215 if( ctx->hmac_ctx != NULL )
216 {
217 mbedtls_platform_zeroize( ctx->hmac_ctx,
218 2 * ctx->md_info->block_size );
219 mbedtls_free( ctx->hmac_ctx );
220 }
221
223}
224
227{
228 if( dst == NULL || dst->md_info == NULL ||
229 src == NULL || src->md_info == NULL ||
230 dst->md_info != src->md_info )
231 {
233 }
234
235 dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
236
237 return( 0 );
238}
239
240#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
242{
243 return mbedtls_md_setup( ctx, md_info, 1 );
244}
245#endif
246
247int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac )
248{
249 if( md_info == NULL || ctx == NULL )
251
252 if( ( ctx->md_ctx = md_info->ctx_alloc_func() ) == NULL )
254
255 if( hmac != 0 )
256 {
257 ctx->hmac_ctx = mbedtls_calloc( 2, md_info->block_size );
258 if( ctx->hmac_ctx == NULL )
259 {
260 md_info->ctx_free_func( ctx->md_ctx );
262 }
263 }
264
265 ctx->md_info = md_info;
266
267 return( 0 );
268}
269
271{
272 if( ctx == NULL || ctx->md_info == NULL )
274
275 return( ctx->md_info->starts_func( ctx->md_ctx ) );
276}
277
278int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
279{
280 if( ctx == NULL || ctx->md_info == NULL )
282
283 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
284}
285
286int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output )
287{
288 if( ctx == NULL || ctx->md_info == NULL )
290
291 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
292}
293
294int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
295 unsigned char *output )
296{
297 if( md_info == NULL )
299
300 return( md_info->digest_func( input, ilen, output ) );
301}
302
303#if defined(MBEDTLS_FS_IO)
304int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
305{
306 int ret;
307 FILE *f;
308 size_t n;
310 unsigned char buf[1024];
311
312 if( md_info == NULL )
314
315 if( ( f = fopen( path, "rb" ) ) == NULL )
317
319
320 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
321 goto cleanup;
322
323 if( ( ret = md_info->starts_func( ctx.md_ctx ) ) != 0 )
324 goto cleanup;
325
326 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
327 if( ( ret = md_info->update_func( ctx.md_ctx, buf, n ) ) != 0 )
328 goto cleanup;
329
330 if( ferror( f ) != 0 )
332 else
333 ret = md_info->finish_func( ctx.md_ctx, output );
334
335cleanup:
336 mbedtls_platform_zeroize( buf, sizeof( buf ) );
337 fclose( f );
339
340 return( ret );
341}
342#endif /* MBEDTLS_FS_IO */
343
344int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
345{
346 int ret;
347 unsigned char sum[MBEDTLS_MD_MAX_SIZE];
348 unsigned char *ipad, *opad;
349 size_t i;
350
351 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
353
354 if( keylen > (size_t) ctx->md_info->block_size )
355 {
356 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
357 goto cleanup;
358 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
359 goto cleanup;
360 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
361 goto cleanup;
362
363 keylen = ctx->md_info->size;
364 key = sum;
365 }
366
367 ipad = (unsigned char *) ctx->hmac_ctx;
368 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
369
370 memset( ipad, 0x36, ctx->md_info->block_size );
371 memset( opad, 0x5C, ctx->md_info->block_size );
372
373 for( i = 0; i < keylen; i++ )
374 {
375 ipad[i] = (unsigned char)( ipad[i] ^ key[i] );
376 opad[i] = (unsigned char)( opad[i] ^ key[i] );
377 }
378
379 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
380 goto cleanup;
381 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
382 ctx->md_info->block_size ) ) != 0 )
383 goto cleanup;
384
385cleanup:
386 mbedtls_platform_zeroize( sum, sizeof( sum ) );
387
388 return( ret );
389}
390
391int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen )
392{
393 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
395
396 return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
397}
398
399int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output )
400{
401 int ret;
402 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
403 unsigned char *opad;
404
405 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
407
408 opad = (unsigned char *) ctx->hmac_ctx + ctx->md_info->block_size;
409
410 if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
411 return( ret );
412 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
413 return( ret );
414 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
415 ctx->md_info->block_size ) ) != 0 )
416 return( ret );
417 if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
418 ctx->md_info->size ) ) != 0 )
419 return( ret );
420 return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
421}
422
424{
425 int ret;
426 unsigned char *ipad;
427
428 if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
430
431 ipad = (unsigned char *) ctx->hmac_ctx;
432
433 if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
434 return( ret );
435 return( ctx->md_info->update_func( ctx->md_ctx, ipad,
436 ctx->md_info->block_size ) );
437}
438
439int mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
440 const unsigned char *key, size_t keylen,
441 const unsigned char *input, size_t ilen,
442 unsigned char *output )
443{
445 int ret;
446
447 if( md_info == NULL )
449
451
452 if( ( ret = mbedtls_md_setup( &ctx, md_info, 1 ) ) != 0 )
453 goto cleanup;
454
455 if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
456 goto cleanup;
457 if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
458 goto cleanup;
459 if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
460 goto cleanup;
461
462cleanup:
464
465 return( ret );
466}
467
468int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data )
469{
470 if( ctx == NULL || ctx->md_info == NULL )
472
473 return( ctx->md_info->process_func( ctx->md_ctx, data ) );
474}
475
476unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
477{
478 if( md_info == NULL )
479 return( 0 );
480
481 return md_info->size;
482}
483
485{
486 if( md_info == NULL )
487 return( MBEDTLS_MD_NONE );
488
489 return md_info->type;
490}
491
492const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info )
493{
494 if( md_info == NULL )
495 return( NULL );
496
497 return md_info->name;
498}
499
500#endif /* MBEDTLS_MD_C */
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define NULL
Definition: types.h:112
static void cleanup(void)
Definition: main.c:1335
unsigned char
Definition: typeof.h:29
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble n
Definition: glext.h:7729
GLenum src
Definition: glext.h:6340
GLfloat f
Definition: glext.h:7540
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLenum dst
Definition: glext.h:6340
GLenum GLenum GLenum input
Definition: glext.h:9031
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_ _CRTIMP int __cdecl ferror(_In_ FILE *_File)
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
#define f
Definition: ke_i.h:83
This file contains the generic message-digest wrapper.
const int * mbedtls_md_list(void)
This function returns the list of digests supported by the generic digest module.
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
This function selects the message digest algorithm to use, and allocates internal structures.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:83
@ MBEDTLS_MD_SHA512
Definition: md.h:92
@ MBEDTLS_MD_MD5
Definition: md.h:87
@ MBEDTLS_MD_RIPEMD160
Definition: md.h:93
@ MBEDTLS_MD_SHA384
Definition: md.h:91
@ MBEDTLS_MD_NONE
Definition: md.h:84
@ MBEDTLS_MD_SHA256
Definition: md.h:90
@ MBEDTLS_MD_SHA224
Definition: md.h:89
@ MBEDTLS_MD_SHA1
Definition: md.h:88
@ MBEDTLS_MD_MD4
Definition: md.h:86
@ MBEDTLS_MD_MD2
Definition: md.h:85
int mbedtls_md_starts(mbedtls_md_context_t *ctx)
This function starts a message-digest computation.
int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the full generic HMAC on the input buffer with the provided key.
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the message-digest of a buffer, with respect to a configurable message-diges...
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
This function prepares to authenticate a new message with the same key as the previous HMAC operation...
const char * mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
This function extracts the message-digest name from the message-digest information structure.
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info) MBEDTLS_DEPRECATED
This function selects the message digest algorithm to use, and allocates internal structures.
int mbedtls_md_clone(mbedtls_md_context_t *dst, const mbedtls_md_context_t *src)
This function clones the state of an message-digest context.
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
This function extracts the message-digest type from the message-digest information structure.
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
This function finishes the HMAC operation, and writes the result to the output buffer.
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing message-digest computation.
#define MBEDTLS_ERR_MD_ALLOC_FAILED
Definition: md.h:65
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing HMAC computation.
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
This function sets the HMAC key and prepares to authenticate a new message.
#define MBEDTLS_ERR_MD_FILE_IO_ERROR
Definition: md.h:66
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data)
#define MBEDTLS_MD_MAX_SIZE
Definition: md.h:97
void mbedtls_md_init(mbedtls_md_context_t *ctx)
This function initializes a message-digest context without binding it to a particular message-digest ...
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
This function finishes the digest operation, and writes the result to the output buffer.
const mbedtls_md_info_t * mbedtls_md_info_from_string(const char *md_name)
This function returns the message-digest information associated with the given digest name.
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
This function extracts the message-digest size from the message-digest information structure.
#define MBEDTLS_ERR_MD_BAD_INPUT_DATA
Definition: md.h:64
void mbedtls_md_free(mbedtls_md_context_t *ctx)
This function clears the internal structure of ctx and frees any embedded internal structure,...
Message digest wrappers.
const mbedtls_md_info_t mbedtls_sha384_info
const mbedtls_md_info_t mbedtls_sha1_info
const mbedtls_md_info_t mbedtls_ripemd160_info
const mbedtls_md_info_t mbedtls_sha256_info
const mbedtls_md_info_t mbedtls_sha224_info
const mbedtls_md_info_t mbedtls_md5_info
const mbedtls_md_info_t mbedtls_sha512_info
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
Definition: platform_util.c:98
Common and shared functions used by multiple modules in the Mbed TLS library.
static int sum(int x_, int y_)
Definition: ptr2_test.cpp:35
#define mbedtls_md_info_from_type
Configuration options (set of defines)
This file contains the definitions and functions of the Mbed TLS platform abstraction layer.
#define mbedtls_free
Definition: platform.h:168
#define mbedtls_calloc
Definition: platform.h:169
#define memset(x, y, z)
Definition: compat.h:39
Definition: copy.c:22
int(* digest_func)(const unsigned char *input, size_t ilen, unsigned char *output)
Definition: md_internal.h:96
int(* update_func)(void *ctx, const unsigned char *input, size_t ilen)
Definition: md_internal.h:90
const char * name
Definition: md_internal.h:78
void(* ctx_free_func)(void *ctx)
Definition: md_internal.h:103
mbedtls_md_type_t type
Definition: md_internal.h:75
void *(* ctx_alloc_func)(void)
Definition: md_internal.h:100
int(* finish_func)(void *ctx, unsigned char *output)
Definition: md_internal.h:93
int(* starts_func)(void *ctx)
Definition: md_internal.h:87
int ret