1

我有一个 CMAC 计算示例,我想在 Python 中重现它,但是我失败了。该示例如下所示:

key = 3ED0920E5E6A0320D823D5987FEAFBB1
msg = CEE9A53E3E463EF1F459635736738962&cmac=

预期的(截断的)CMAC 看起来像这样(注意:截断意味着每隔一个字节被丢弃)

ECC1E7F6C6C73BF6

因此,我尝试使用以下代码重新制定此示例:

from Crypto.Hash import CMAC
from Crypto.Cipher import AES
from binascii import hexlify, unhexlify

def generate_cmac(key, msg):
    """generate a truncated cmac message.
    Inputs: 
    key: 1-dimensional bytearray of arbitrary length
    msg: 1-dimensional bytearray of arbitrary length
    Outputs:
    CMAC: The cmac number
    CMAC_t: Trunacted CMAC"""


    # Generate CMAC via the CMAC algorithm
    cobj = CMAC.new(key=key, ciphermod=AES)
    cobj.update(msg)
    mac_raw = cobj.digest()

    # Truncate by initializing an empty array and assigning every second byte
    mac_truncated = bytearray(8 * b'\x00')
    it2 = 0
    for it in range(len(mac_raw)):
        if it % 2:
            mac_truncated[it2:it2+1] = mac_raw[it:it+1]
            it2 += 1
    return mac_raw, mac_truncated

key = unhexlify('3ED0920E5E6A0320D823D5987FEAFBB1') # The key as in the example
msg = 'CEE9A53E3E463EF1F459635736738962&cmac='      # The msg as in the example
msg_utf = msg.encode('utf-8')
msg_input = hexlify(msg_utf)                        # Trying to get the bytearray
mac, mact_calc = generate_cmac(key, msg_input)      # Calculate the CMAC and truncated CMAC
# However the calculated CMAC does not match the cmac of the example

我的函数generate_cmac()适用于其他情况,为什么不适合这个例子呢?

(如果有人好奇,这个例子来自本文档第 18 页/表 6)

编辑:成功的 cmac 计算示例如下:

mact_expected = unhexlify('94EED9EE65337086')       # as stated in the application note
key = unhexlify('3FB5F6E3A807A03D5E3570ACE393776F') # called K_SesSDMFileReadMAC
msg = []                                            # zero length input
mac, mact_calc = generate_cmac(key, msg)            # mact_expected and mact_calc are the same
assert mact_expected == mact_calc, "Example 1 failed" # This assertion passes
4

1 回答 1

2

TLDR:过度翻译

令我大吃一惊的是,链接的示例确实似乎意味着CEE9A53E3E463EF1F459635736738962&cmac=当它写入时,因为下面的框包含 76 个十六进制字符,用于 ASCII 编码的 38 个字节,即434545394135334533453436334546314634353936333537333637333839363226636d61633d.

但是,我很肯定这不需要像代码那样在 76 字节的调子上进一步进行 hexlified。换句话说,我的赌注是

key = unhexlify('3ED0920E5E6A0320D823D5987FEAFBB1')
msg = 'CEE9A53E3E463EF1F459635736738962&cmac='.encode()
mac, mact_calc = generate_cmac(key, msg)
于 2020-04-06T19:48:59.073 回答