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)
Пример #2
0
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]
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]
        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 )
Пример #5
0
def decripta(data):
	try:
		aad = binify(data["aad"])
		tk = binify(data["tk"])
		nonce = binify(data["nonce"])
		dati = binify(data["data"])
		src = binify(data["src"])
		dst = binify(data["dst"])
		strlist = []
		strlist.append("-----SOURCE MAC ADDRESS---")
		strlist.append(dump(src))
		strlist.append("------DEST MAC ADDRESS----")
		strlist.append(dump(dst))
		strlist.append("-----------AAD------------")
		strlist.append(dump(aad))
		strlist.append("-----------NONCE----------")
		strlist.append(dump(tk))
		strlist.append("-----------TK-------------")
		strlist.append(dump(nonce))
		strlist.append("-----------DATI CIFRATI----------")
		strlist.append(dump(dati))
		
		longstr = "\n".join(strlist)
		print longstr
		fh.write(longstr)

		decrypter = CCM(AES(tk,len(tk)))
		plainText = decrypter.decrypt(dati,nonce,aad)
		strlist = []
		strlist.append("-----------DATI IN CHIARO----------")
		strlist.append(dump(plainText))
		
		longstr = "\n".join(strlist)
		print longstr
		fh.write(longstr)
	except:
		print '\033[91m' "---CONTROLLO DI INTEGRITA' FALLITO---" '' '\033[0m' "" ''
		fh.write("---CONTROLLO DI INTEGRITA' FALLITO---\n")