xref: /btstack/test/crypto/aes_cmac_test.py (revision 7aa521df15437e269918f1173a8571ecbf31676e)
1*7aa521dfSMatthias Ringwald#!/usr/bin/env python3
2*7aa521dfSMatthias Ringwald# BlueKitchen GmbH (c) 2019
3*7aa521dfSMatthias Ringwald
4*7aa521dfSMatthias Ringwald# pip3 install pycryptodomex
5*7aa521dfSMatthias Ringwald
6*7aa521dfSMatthias Ringwaldfrom Cryptodome.Cipher import AES
7*7aa521dfSMatthias Ringwaldfrom Cryptodome.Hash   import CMAC
8*7aa521dfSMatthias Ringwald
9*7aa521dfSMatthias Ringwalddef aes_cmac(key, n):
10*7aa521dfSMatthias Ringwald    cobj = CMAC.new(key, ciphermod=AES)
11*7aa521dfSMatthias Ringwald    cobj.update(n)
12*7aa521dfSMatthias Ringwald    return cobj.digest()
13*7aa521dfSMatthias Ringwald
14*7aa521dfSMatthias Ringwalddb_message = bytes.fromhex('010000280018020003280a0300002a04000328020500012a06000028011807000328200800052a090002290a0003280a0b00292b0c000328020d002a2b0e00002808180f000228140016000f1810000328a21100182a12000229130000290000140001280f1815000328021600192a');
15*7aa521dfSMatthias Ringwalddb_hash_expected = bytes.fromhex('F1CA2D48ECF58BAC8A8830BBB9FBA990')
16*7aa521dfSMatthias Ringwalddb_hash_actual = aes_cmac(bytes(16), db_message);
17*7aa521dfSMatthias Ringwaldif db_hash_actual != db_hash_expected:
18*7aa521dfSMatthias Ringwald    print("Expected: " + db_hash_actual.hex())
19*7aa521dfSMatthias Ringwald    print("Actual:   " + db_hash_actual.hex())
20