mbedtls:How mbedtls work at PC and embeded device when getrandom

mbedtls:How mbedtls work at PC and embeded device when getrandom

mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy;
/*we need to init the entropy context*/
mbedtls_entropy_init(&entropy);

mbedtls_ctr_drbg_init(&ctr_drbg);

ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
                            (const unsigned char *) pers, strlen(pers));
if (ret != 0)
{
    goto cleanup;
}


void mbedtls_entropy_init( mbedtls_entropy_context *ctx )
{
...
    #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
        mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL,
                                    MBEDTLS_ENTROPY_MIN_PLATFORM,
                                    MBEDTLS_ENTROPY_SOURCE_STRONG );
    #endif
    #if defined(MBEDTLS_TIMING_C)
        mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL,
                                    MBEDTLS_ENTROPY_MIN_HARDCLOCK,
                                    MBEDTLS_ENTROPY_SOURCE_WEAK );
    #endif
...
}

int mbedtls_platform_entropy_poll( void *data,
                           unsigned char *output, size_t len, size_t *olen )
{
    FILE *file;
    size_t read_len;
    ((void) data);

/*If we run mbedtls at PC, mbedtls will run at this branch*/
#if defined(HAVE_GETRANDOM)
    if( has_getrandom == -1 )
        has_getrandom = ( check_version_3_17_plus() == 0 );

    if( has_getrandom )
    {
        int ret;

        if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
            return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );

        *olen = ret;
        return( 0 );
    }
#endif /* HAVE_GETRANDOM */

    *olen = 0;

    /*If we run mbedtls at a embeded device, we must fisrt to make sure we have /dev/urandom or /dev/random to get entropy*/
    file = fopen( "/dev/urandom", "rb" );
    if( file == NULL )
        return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );

    read_len = fread( output, 1, len, file );
    if( read_len != len )
    {
        fclose( file );
        return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
    }

    fclose( file );
    *olen = len;

    return( 0 );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值