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);
发布了223 篇原创文章 · 获赞 160 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/u014100559/article/details/104194099