/* * HMAC_DRBG update, using optional additional data (10.1.2.2) */ void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len ) { const mbedtls_md_info_t *info; size_t md_len; int /* unsigned char */ rounds; unsigned char sep[1]; unsigned char K[MBEDTLS_MD_MAX_SIZE]; int sep_value; info = ctx->md_ctx.md_info; md_len = mbedtls_md_get_size( info ); rounds = ( add_len != 0 && additional != NULL ) ? 2 : 1; /* rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1; */ for( sep_value = 0; sep_value < rounds; sep_value++ ) { sep[0] = sep_value; /* Step 1 or 4 */ mbedtls_md_hmac_reset( &ctx->md_ctx ); mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); mbedtls_md_hmac_update( &ctx->md_ctx, sep, 1 ); if( rounds == 2 ) mbedtls_md_hmac_update( &ctx->md_ctx, additional, add_len ); mbedtls_md_hmac_finish( &ctx->md_ctx, K ); /* Step 2 or 5 */ mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ); mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ); } }
void computeHmac(const uint8_t* input, size_t inputLen, uint8_t* sig) const { auto ctx = const_cast<mbedtls_md_context_t*>(&m_ctx); mbedtls_md_hmac_update(ctx, input, inputLen); mbedtls_md_hmac_finish(ctx, sig); mbedtls_md_hmac_reset(ctx); }
/* * HMAC_DRBG random function with optional additional data: * 10.1.2.5 (arabic) + 9.3 (Roman) */ int mbedtls_hmac_drbg_random_with_add( void *p_rng, unsigned char *output, size_t out_len, const unsigned char *additional, size_t add_len ) { int ret; mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng; size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info ); size_t left = out_len; unsigned char *out = output; /* II. Check request length */ if( out_len > MBEDTLS_HMAC_DRBG_MAX_REQUEST ) return( MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG ); /* III. Check input length */ if( add_len > MBEDTLS_HMAC_DRBG_MAX_INPUT ) return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG ); /* 1. (aka VII and IX) Check reseed counter and PR */ if( ctx->f_entropy != NULL && /* For no-reseeding instances */ ( ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON || ctx->reseed_counter > ctx->reseed_interval ) ) { if( ( ret = mbedtls_hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 ) return( ret ); add_len = 0; /* VII.4 */ } /* 2. Use additional data if any */ if( additional != NULL && add_len != 0 ) mbedtls_hmac_drbg_update( ctx, additional, add_len ); /* 3, 4, 5. Generate bytes */ while( left != 0 ) { size_t use_len = left > md_len ? md_len : left; mbedtls_md_hmac_reset( &ctx->md_ctx ); mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ); memcpy( out, ctx->V, use_len ); out += use_len; left -= use_len; } /* 6. Update */ mbedtls_hmac_drbg_update( ctx, additional, add_len ); /* 7. Update reseed counter */ ctx->reseed_counter++; /* 8. Done */ return( 0 ); }
/* * HMAC_DRBG update, using optional additional data (10.1.2.2) */ int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len ) { size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info ); unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1; unsigned char sep[1]; unsigned char K[MBEDTLS_MD_MAX_SIZE]; int ret; for( sep[0] = 0; sep[0] < rounds; sep[0]++ ) { /* Step 1 or 4 */ if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 ) goto exit; if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ) ) != 0 ) goto exit; if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, sep, 1 ) ) != 0 ) goto exit; if( rounds == 2 ) { if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, additional, add_len ) ) != 0 ) goto exit; } if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, K ) ) != 0 ) goto exit; /* Step 2 or 5 */ if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ) ) != 0 ) goto exit; if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ) ) != 0 ) goto exit; if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 ) goto exit; } exit: mbedtls_platform_zeroize( K, sizeof( K ) ); return( ret ); }
result_t Digest::digest(obj_ptr<Buffer_base> &retVal) { if (m_iAlgo < 0) return CHECK_ERROR(CALL_E_INVALID_CALL); std::string strBuf; strBuf.resize(mbedtls_md_get_size(m_ctx.md_info)); if (m_bMac) mbedtls_md_hmac_finish(&m_ctx, (unsigned char *) &strBuf[0]); else mbedtls_md_finish(&m_ctx, (unsigned char *) &strBuf[0]); m_iAlgo = -1; mbedtls_md_hmac_reset(&m_ctx); retVal = new Buffer(strBuf); return 0; }
/* * HMAC_DRBG update, using optional additional data (10.1.2.2) */ void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len ) { size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info ); unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1; unsigned char sep[1]; unsigned char K[MBEDTLS_MD_MAX_SIZE]; for( sep[0] = 0; sep[0] < rounds; sep[0]++ ) { /* Step 1 or 4 */ mbedtls_md_hmac_reset( &ctx->md_ctx ); mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); mbedtls_md_hmac_update( &ctx->md_ctx, sep, 1 ); if( rounds == 2 ) mbedtls_md_hmac_update( &ctx->md_ctx, additional, add_len ); mbedtls_md_hmac_finish( &ctx->md_ctx, K ); /* Step 2 or 5 */ mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ); mbedtls_md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ); } }
void hmac_ctx_reset(mbedtls_md_context_t *ctx) { ASSERT(0 == mbedtls_md_hmac_reset(ctx)); }