打印

使用CMAC算法

[复制链接]
3125|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sun_qiao_999|  楼主 | 2015-3-20 08:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
mac, ST, os, TE, ni
我们最近需要使用CMAC算法,但是我们需求是通过一串数据和Key算出MAC,
请问用ST的CMAC库如何实现这个需求?类似下面的功能,下面的功能是使用openssl是实现的,我们需要用ST库来重写这个函数。

S32 HOST_CMAC_Get(U8* pKey, U8 keySizeInBytes,  U8* pMsg, U32 msgLen, U8* pMac)
{
    int ret;
    size_t size;

    ret = HOST_CMAC_Init(pKey, keySizeInBytes);
    if (ret == HOST_CRYPTO_OK)
    {
        ret = CMAC_Update(cmacCtx, pMsg, msgLen);
        if (ret == HOST_CRYPTO_OK)
        {
            ret = CMAC_Final(cmacCtx, pMac, &size);
        }
    }

    if (ret != ERR_MEMORY)
    {
        CMAC_CTX_free(cmacCtx);
    }

    return ret;   
}
沙发
sun_qiao_999|  楼主 | 2015-3-20 08:39 | 只看该作者
应用芯片是STM32L162系列

使用特权

评论回复
板凳
sun_qiao_999|  楼主 | 2015-3-20 08:55 | 只看该作者
在#include "CMAC/aes_cmac.h文件中找到下面这个例子,
但是这个例子只能做16整数的字节的算法CMAC!问题:
1,如果我要做任意字节的数据来算CMAC,怎么实现?
2,我们需要替换OPENSSL中的,CMAC_Init,CMAC_Update,CMAC_Final三个函数!

/*!  
* \page Tutorial_AES_CMAC AES-CMAC Tutorial
*
* Please remember that before starting to call the Encryption function the context \b requires user
* initialization. The Key Size, Tag Size, Flags member must be initialized prior to calling the
*  init function.
* Before the last call to AES_CMAC_Encrypt_Append the flag E_SK_FINAL_APPEND must be set in the context.
* Look at the each function documentation to see what is needed prior of calling.
*
* The API functions to be used are:
*  - \ref AES_CMAC_Encrypt_Init initialize an \ref AESCMACctx_stt context with key, tag size, flags.
*  - \ref AES_CMAC_Encrypt_Append process the input. It doesn't produce any output.
*    It can be called multiple times but the input size must be multiple of 16.
*    A last append call is allowed with any, positive, input size, but prior to that the flag
*    E_SK_FINAL_APPEND must be set.
*  - \ref AES_CMAC_Encrypt_Finish can be called only one time for the finalization process and it will generate
*         the authentication TAG
*
* Here follows an example of AES-128-CMAC authentication, it will process a NIST test vector and generate the
* authentication TAG.
*
* \code
* #include <stdio.h>
* #include "crypto.h"
*
* int main()
* {
*   const uint8_t KEY_CMAC[] ={0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c};  
*   const uint8_t PLAIN_TEXT_CMAC[] = {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
*                                     0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
*                                     0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
*                                     0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10};  
*   uint8_t     TagAesCmac[16] = {0,};
*   int32_t     TagSize=0;
*   uint32_t    error_status = 0;
*
*   AESCMACctx_stt tst_AesCmac;
*   
*   // Set flag field to default value
*   tst_AesCmac.mFlags = E_SK_DEFAULT;
*
*   // Set key size to 16 (correspondng to AES-128)
*   tst_AesCmac.mKeySize = 16;
*
*   // Pointer to original Key buffer
*   tst_AesCmac.pmKey = (uint8_t *) KEY_CMAC;
*
*   // Size of returned authentication TAG
*   tst_AesCmac.mTagSize = 16;
*
*   // Initialize the operation, by passing the key and IV
*   error_status = AES_CMAC_Encrypt_Init(&tst_AesCmac);
*
*   // check for initialization errors
*   if(error_status == AES_SUCCESS)
*   {
*     // Message is 4 blocks of 16 bytes each, do first append of 16 bytes     
*     error_status = AES_CMAC_Encrypt_Append(&tst_AesCmac, (uint8_t *) PLAIN_TEXT_CMAC, 16);
*     if(error_status == AES_SUCCESS)
*     {
*       // Do second append of 32 bytes
*       error_status = AES_CMAC_Encrypt_Append(&tst_AesCmac, (uint8_t *) PLAIN_TEXT_CMAC + 16, 32);
*     }            
*     if(error_status == AES_SUCCESS)
*     {
*       // Do last append of the remaining 16 bytes, but first set the flag
*      tst_AesCmac.mFlags |= E_SK_FINAL_APPEND;
*      error_status = AES_CMAC_Encrypt_Append(&tst_AesCmac, (uint8_t *) PLAIN_TEXT_CMAC + 16 + 32, 16);
*      if(error_status == AES_SUCCESS)
*      {
*        // Do the Finalization, write the TAG at the end of the encrypted message
*        error_status = AES_CMAC_Encrypt_Finish(&tst_AesCmac, (uint8_t*) TagAesCmac, &TagSize);
*      }
*    }   
*  }
*  printf("Returned: %d\n", error_status);
*  print_buffer("Returned TAG:",TagAesCmac,TagSize);  
*  return(0);
* }
* \endcode
*/

使用特权

评论回复
地板
airwill| | 2015-3-20 09:39 | 只看该作者
现成的算法固定了 16 的整数个, 你想改成任意字节
有个简单的办法, 填满 16 的整数倍个字节再计算.

使用特权

评论回复
5
sun_qiao_999|  楼主 | 2015-3-20 09:49 | 只看该作者
本帖最后由 sun_qiao_999 于 2015-3-20 10:39 编辑
airwill 发表于 2015-3-20 09:39
现成的算法固定了 16 的整数个, 你想改成任意字节
有个简单的办法, 填满 16 的整数倍个字节再计算. ...

是填满吗?

使用特权

评论回复
6
sun_qiao_999|  楼主 | 2015-3-20 10:37 | 只看该作者
airwill 发表于 2015-3-20 09:39
现成的算法固定了 16 的整数个, 你想改成任意字节
有个简单的办法, 填满 16 的整数倍个字节再计算. ...

是随便填数,还是要按照什么算法来填呢?ST是否有此规范呢?谢谢

使用特权

评论回复
7
airwill| | 2015-3-20 14:19 | 只看该作者
我不知道有没有这个规范, 在有些 AES 算法里也会强制填满 key 的空间.
你可以先默认填0或固定值的办法.

使用特权

评论回复
8
松翰ic软件开发| | 2015-3-20 18:26 | 只看该作者
牛,来学习下.

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

14

帖子

0

粉丝