当前位置:   article > 正文

mbedtls移植之CMAC算法_mbedtls cmac

mbedtls cmac

一、 mbedtls简介

MbedTLS是一个开源、可移植、易使用、可读性高的SSL库,实现了常所用的加解密算法、X.509证书操作以及TLS协议操作。MbedTLS各功能模块独立性高、耦合度低,可以通过配置宏定义进行功能裁剪,非常适合对空间和效率要求高的嵌入式系统。

二、CMAC算法简介

消息认证码Message Authentication Code(MAC)是一种完整性校验技术。对一个任意长度的消息,通过在发送者和接收者之间共享的密钥,基于MAC算法,可生成MAC值。接收者只需校验传递过的MAC值,即可检验消息的完整性是否有效,从而判断消息是否被篡改过。常用的MAC算法有CMAC、HMAC、GMAC,本文主要介绍CMAC算法。CMAC基于对称密钥的分组算法,例如基于AES算法的CMAC,成为AES-CMAC。AES-CMAC类似AES-CBC算法,总体流程如下图
在这里插入图片描述

详细算法说明请参考RFC4439或NIST.SP.800-38B。

三、实现

3.1 移植MbedTLS代码

移植自mbedTLS 2.16版本
需移植的文件如下:
在这里插入图片描述

修改config.h文件

#ifndef MBEDTLS_CONFIG_H
#define MBEDTLS_CONFIG_H

#define MBEDTLS_ERROR_C

#define MBEDTLS_AES_C
#define MBEDTLS_CMAC_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_CIPHER_MODE_CBC

#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.2 引入头文件

引入相应的头文件

#include <stdio.h>
#include <time.h>
#include <string.h>
#include "../crypto/mbedtls/cipher.h"
#include "../crypto/mbedtls/cmac.h"
#include "../crypto/mbedtls/error.h"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.3 计算CMAC

实例为从一个文件中读取信息并计算AES-CMAC,其中使用到的key设置为字符串“1234567890ABCDEF”,key的长度必须为16字节的unsigned char。需要计算的消息通过调用mbedtls_cipher_cmac_update不断输入,直到全部输入完成。

int get_cmac_from_file(){
    mbedtls_cipher_context_t cipher_context;
    unsigned char error[100];
    const unsigned char *key="1234567890ABCDEF";
    unsigned char output_cmac[16];
    const char *file_name = "D:\\tmp\\crypto\\rsa\\music.mp3";


    mbedtls_cipher_init(&cipher_context);
    const mbedtls_cipher_info_t *cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
    if(cipher_info == NULL){
        printf("failed to get cipher info\n");
        return -1;
    }

    int result = mbedtls_cipher_setup(&cipher_context,cipher_info);
    if(result != 0){
        printf("failed to set cipher info:");
        mbedtls_strerror(result,error,sizeof(error));
        printf("%s\n",error);
        mbedtls_cipher_free(&cipher_context);
        return -2;
    }

    result = mbedtls_cipher_cmac_starts(&cipher_context,key,128);
    if(result != 0){
        printf("failed to set start:");
        mbedtls_strerror(result,error,sizeof(error));
        printf("%s\n",error);
        mbedtls_cipher_free(&cipher_context);
        return -3;
    }
    //读取文件
    FILE *input_file = fopen(file_name,"rb");
    unsigned char read_buffer[1024];
    size_t read_num;
    while (1){
        read_num = fread(read_buffer,sizeof(char),sizeof(read_buffer),input_file);
        result = mbedtls_cipher_cmac_update(&cipher_context,read_buffer,read_num);
        if(result != 0){
            printf("failed to set update:");
            mbedtls_strerror(result,error,sizeof(error));
            printf("%s\n",error);
            mbedtls_cipher_free(&cipher_context);
            fclose(input_file);
            return -3;
        }
        if(read_num < 1024){
            break;
        }
    }
    fclose(input_file);

    result = mbedtls_cipher_cmac_finish(&cipher_context,output_cmac);
    if(result != 0){
        printf("failed to set finish:");
        mbedtls_strerror(result,error,sizeof(error));
        printf("%s\n",error);
        mbedtls_cipher_free(&cipher_context);
        return -3;
    }
    printf("cmac:\n");
    for(int i=0;i<16;i++){
        printf("%02x",output_cmac[i]);
    }
    printf("\n");


    mbedtls_cipher_free(&cipher_context);
    return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

3.4 运行效果

在这里插入图片描述
使用openssl进行验证,计算出的mac值是一致的。
在这里插入图片描述

四、总结

AES-CMAC算法是最常用的CMAC算法,且通过mbedtls移植也很方便。若需要继续精简空间,可不需要error.c以及删除相关.c文件中未使用的函数,例如aes.c中的mbedtls_aes_decrypt。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/176557
推荐阅读
相关标签
  

闽ICP备14008679号