mbedtls how to generate cmac

If you want to generate cmac, you must first open the Macro of CMAC.
The modify as below show.

weihanwu@weihanwu-OptiPlex-990:~/mbedtls/mbedtls/programs$ git ../include/mbedtls/config.h
git: '../include/mbedtls/config.h' is not a git command. See 'git --help'.
weihanwu@weihanwu-OptiPlex-990:~/mbedtls/mbedtls/programs$ git diff ../include/mbedtls/config.h
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
index 058969b73..0256b4fa6 100644
--- a/include/mbedtls/config.h
+++ b/include/mbedtls/config.h
@@ -2326,7 +2326,7 @@
  * Requires: MBEDTLS_AES_C or MBEDTLS_DES_C
  *
  */
-//#define MBEDTLS_CMAC_C
+#define MBEDTLS_CMAC_C

 /**
  * \def MBEDTLS_CTR_DRBG_C
weihanwu@weihanwu-OptiPlex-990:~/mbedtls/mbedtls/programs$

The code of how to generate cmac.

int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
                         const unsigned char *key, size_t keylen,
                         const unsigned char *input, size_t ilen,
                         unsigned char *output )
{
    mbedtls_cipher_context_t ctx;
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;

    if( cipher_info == NULL || key == NULL || input == NULL || output == NULL )
        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );

    mbedtls_cipher_init( &ctx );

    if( ( ret = mbedtls_cipher_setup( &ctx, cipher_info ) ) != 0 )
        goto exit;

    ret = mbedtls_cipher_cmac_starts( &ctx, key, keylen );
    if( ret != 0 )
        goto exit;

    ret = mbedtls_cipher_cmac_update( &ctx, input, ilen );
    if( ret != 0 )
        goto exit;

    ret = mbedtls_cipher_cmac_finish( &ctx, output );

exit:
    mbedtls_cipher_free( &ctx );

    return( ret );
}


int ret = -1;
const mbedtls_cipher_info_t *cipher_info;
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_256_ECB );

if ( cipher_info == NULL )
{
    /* Failing at this point must be due to a build issue */
    ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
    goto cleanup;
}

/*MacValue store the cmac*/
ret = mbedtls_cipher_cmac( cipher_info, Key, 256, Message, Mlen, MacValue );

dump_buf("The cmac value is: ", MacValue, 16);
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值