def CCMtestVector(testCase, macSize, key, nonce, addAuth, pt, kct):
            """ CCM test vectors using AES algorithm """
            print '%s %s %s' % ('=' *
                                ((54 - len(testCase)) / 2), testCase, '=' *
                                ((54 - len(testCase)) / 2))
            key, nonce, pt, addAuth, kct = a2b_p(key), a2b_p(nonce), a2b_p(
                pt), a2b_p(addAuth), a2b_p(kct)

            alg = CCM(AES(key, keySize=len(key)),
                      macSize=macSize,
                      nonceSize=len(nonce))

            print 'alg=%s%skeySize=%3d   blockSize=%3d   M=%2d    L=%2d' % (
                alg.baseCipher.name, ' ' * (10 - len(alg.baseCipher.name)),
                alg.keySize, alg.blockSize, alg.M, alg.L)
            print 'key:    %s' % b2a_p(key)[9:]
            print 'nonce:  %s' % b2a_p(nonce)[9:]
            print 'addAuth:%s' % b2a_p(addAuth)[9:]
            print 'pt:     %s' % b2a_p(pt)[9:]
            ct = alg.encrypt(pt, nonce=nonce, addAuthData=addAuth)
            print 'ct:     %s' % b2a_p(ct)[9:]
            print 'kct:    %s' % b2a_p(kct)[9:]
            print '========================================================'
            self.assertEqual(ct, kct)
            dct = alg.decrypt(ct, nonce=nonce, addAuthData=addAuth)
            self.assertEqual(dct, pt)
def generate_mic(data):
    '''
    \brief Generate a Message Integrity Code (MIC) over some given data.
    
    \param[in] data The data to calculate the MIC over, a binary string.
    
    \returns The 32-bit MIC, an integer.
    '''
    #alg       = AES_CBC(key=OTAP_KEY, keySize=len(OTAP_KEY), padding=padWithZeros())
    #outdata   = alg.encrypt(data, iv=OTAP_NONCE)
    alg = CCM(AES(OTAP_KEY), macSize=OTAP_MIC_LEN)
    outdata = alg.encrypt('', OTAP_NONCE, addAuthData=data)

    # the 32-bit MIC is the first 4 bytes of the CBC-MAC result (per CCM spec)
    #return struct.unpack('!L', outdata[-16:-12])[0]
    return struct.unpack('!L', outdata[-OTAP_MIC_LEN:])[0]