CMAC的简单介绍

CMAC是一种使用对称密钥加密算法,如AES或DES,进行消息认证的方法。它涉及将消息分块并使用CBC模式处理,通过生成子密钥K1和K2对每个消息块Mi进行加密。在处理过程中,会根据块的长度B和特定常量Rb进行操作。CMAC的实现通常分为init、update和dofinal三个阶段,分别用于初始化、处理中间消息块和最终块的加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、简介
CMAC:基于对称密钥分组加密算法的消息认证码。
对称密钥:相当于采用对称算法来加密,如AES、DES等;
分组加密:就是采用分组的方式,将消息分成对应算法对应大小的块;
C:采用的是CBC模式;

2、符号
B:加密块的长度;
Rb:生成子密钥的常量字符串,AES中:0x87(128位),DES中:0x1B(64位);
K:加密算法中的密钥;
K1、K2:通过K生成的子密钥1和子密钥2;
Mi:消息被划分成的第几个消息块;

3、步骤
1、生成子密钥;
2、获取B的长度,Rb等处理;
3、对分块消息处理;
4、根据是否需要填充采用不同密钥处理;

4、图示
在这里插入图片描述
5、其他
对于分成init,update,dofinal三部分处理的话,即init部分可以处理不变的部分,生成两个子密钥,
根据加密算法采用哪种Rb、B的长度是多大。update部分即可以对第一块到最后一块的前一块做处理,即可处理不同地址传过来的消息。
最后一块因为有两种情况,可以放到dofinal中处理,当然dofinal中也可处理所有的块。还是根据自己理解的来划分即可。

详细介绍了AES-CMAC算法的原理与实现,附有C语言写的样例程序。 以下是原文的introduction: The National Institute of Standards and Technology (NIST) has recently specified the Cipher-based Message Authentication Code(CMAC). CMAC [NIST-CMAC] is a keyed hash function that is based on a symmetric key block cipher, such as the Advanced Encryption Standard [NIST-AES]. CMAC is equivalent to the One-Key CBC MAC1 (OMAC1) submitted by Iwata and Kurosawa [OMAC1a, OMAC1b]. OMAC1 is an improvement of the eXtended Cipher Block Chaining mode (XCBC) submitted by Black and Rogaway [XCBCa, XCBCb], which itself is an improvement of the basic Cipher Block Chaining-Message Authentication Code (CBC-MAC). XCBC efficiently addresses the security deficiencies of CBC-MAC, and OMAC1 efficiently reduces the key size of XCBC. AES-CMAC provides stronger assurance of data integrity than a checksum or an error-detecting code. The verification of a checksum or an error-detecting code detects only accidental modifications of the data, while CMAC is designed to detect intentional, unauthorized modifications of the data, as well as accidental modifications. AES-CMAC achieves a security goal similar to that of HMAC [RFC-HMAC]. Since AES-CMAC is based on a symmetric key block cipher, AES, and HMAC is based on a hash function, such as SHA-1, AES-CMAC is appropriate for information systems in which AES is more readily available than a hash function. This memo specifies the authentication algorithm based on CMAC with AES-128. This new authentication algorithm is named AES-CMAC.
### CMAC算法介绍 CMAC(Cipher-based Message Authentication Code),即基于分组密码的消息认证码,是一种用于确保消息完整性和真实性验证的密码学机制[^1]。这种算法利用对称密钥加密技术来生成固定长度的校验值,通常称为标签或MAC。 #### 特点 - **安全性高**:由于采用了强健的分组加密标准如AES作为底层运算器; - **效率良好**:计算过程相对简单快速,在硬件和软件环境中都能高效执行; - **适用广泛**:适用于多种应用场景下的数据保护需求。 对于特定于AES的变体——AES-CMAC而言,其设计遵循NIST SP800-38B规范,并且已经被标准化为RFC 4493的一部分。它能够接受任意长度的数据输入并输出一个定长的二进制字符串作为消息摘要[^2]。 ### Python实现AES-CMAC算法示例 下面给出一段Python代码片段展示如何使用`cryptography`库来创建和验证AES-CMAC: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend import os def aes_cmac(key: bytes, message: bytes) -> bytes: from cryptography.hazmat.primitives.constant_time import bytes_eq from cryptography.hazmat.primitives.cmacecmac import CMac cmac = CMac( algorithm=algorithms.AES(key), backend=default_backend() ) cmac.update(message) return cmac.finalize() if __name__ == "__main__": key = b'sixteen byte key' # 密钥应随机生成且保密存储 data = b"Sample plaintext to be authenticated" mac_tag = aes_cmac(key=key, message=data) print(f"AES-CMAC Tag: {mac_tag.hex()}") ``` 此段程序定义了一个名为`aes_cmac()`的功能函数,该函数接收两个参数:一个是用来初始化CMAC对象的秘密密钥;另一个是要对其进行身份验证的信息流。最后调用了`.finalize()`方法获取最终的结果。 为了完成整个流程,还需要在接收端重复相同的步骤以重新计算MAC并与原始接收到的那个比较一致性。只有当两者完全匹配时才能确认信息未遭篡改[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值