def get_traces(n_traces, n_samples, FHplaintext, FHciphertext, FHtraces, direction):
	global verbose
	print ("Starting capture... ")
	for i in range(0, n_traces):
		# start the scope
		scope_run()
		if(i==0): # wait five seconds the first time to prevent unusable measurements
			time.sleep(5)
		if(verbose):print ("Trace number: %8d/%d" %(i+1,n_traces))
		# generate a new random data vector
		data = [random.randint(0,255) for x in range(16)]
		ciphertext_data = hl2bs(data)
		# send data to the card to be decrypted
		plaintext_data = sc_decrypt(data)
		# get the power trace
		scope_get_trace()
		# save trace, plain-text and cipher-text into an hdf5 file
		if(direction == 0):
			hdf5_add_data(bs2hl(ciphertext_data), bs2hl(plaintext_data), i, FHplaintext, FHciphertext, FHtraces)
		else:
			hdf5_add_data(bs2hl(plaintext_data), bs2hl(ciphertext_data), i, FHplaintext, FHciphertext, FHtraces)
	print ('Done!')
Example #2
0
def writeStudentCourse(cardResquestSc,
                       cardResquestSam,
                       courseIndex,
                       currentAttendance,
                       addedValue=10):
    serviceSc = cardResquestSc.waitforcard()

    start = time.time()
    serviceSc.connection.connect()

    serviceSam = cardResquestSam.waitforcard()
    serviceSam.connection.connect()
    # MF Sam call
    apdu = Card.SELECT + Card.MF_SC
    data = __transmit(serviceSam, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # DF Sam call
    apdu = Card.SELECT + Sam.DF_SAM
    data = __transmit(serviceSam, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # EF Sam call
    apdu = Card.SELECT + Sam.EF_SAM
    data = __transmit(serviceSam, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GU Sc call
    apdu = Sam.GU_SC
    data = __transmit(serviceSc, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # LK and WK call
    apdu = Sam.LK_SAM + data.response + Sam.WK_SAM
    data = __transmit(serviceSam, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # MF sc call
    apdu = Card.SELECT + Card.MF_SC
    data = __transmit(serviceSc, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # DF sc call
    apdu = Card.SELECT + Student.DF_SC
    data = __transmit(serviceSc, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GC sc call
    apdu = Card.GC_SC + Sam.WK_SAM
    data = __transmit(serviceSc, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GR sc call
    apdu = Card.GR_SC + [data.sw2]
    data = __transmit(serviceSc, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GC sam call
    apdu = Sam.GC_SAM + data.response
    data = __transmit(serviceSam, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GR sam call
    apdu = Sam.GR_SAM + [data.sw2]
    data = __transmit(serviceSam, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # MUA SC call
    apdu = Sam.MUA + data.response
    data = __transmit(serviceSc, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GR SC call
    apdu = Card.GR_SC + [data.sw2]
    data = __transmit(serviceSc, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # MUA sam call
    apdu = Sam.MUA + data.response
    data = __transmit(serviceSam, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # EF Sc call
    apdu = Card.SELECT + Student.Course.EF_SC
    data = __transmit(serviceSc, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # Write to card BEGIN HERE
    # SAM encript
    attendanceInHex = bs2hl(str(currentAttendance + addedValue))
    attendanceInHex = [0x30] * (3 - len(
        attendanceInHex)) + attendanceInHex  # make sure 3 digit to write

    apdu = Sam.ENCRIPT + Student.Course.LENGTH_WRITE + attendanceInHex  ####### TO DO
    data = __transmit(serviceSam, apdu, Card.OPEN_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    # GR sam
    lengthu = data.sw2
    apdu = Sam.GR_SAM + [data.sw2]
    data = __transmit(serviceSam, apdu, Card.READ_SUCCESS)

    if (not data.isSuccess): return False  # wrong card

    apdu = Sam.WRITE2 + [0x00, courseIndex * 9 + 6] + [
        lengthu + 1
    ] + Student.Course.LENGTH_WRITE + data.response
    data = __transmit(serviceSc, apdu, Card.READ_SUCCESS)

    end = time.time()
    print 'writeStudentCourse:time,', (end - start)

    serviceSc.connection.disconnect()
    serviceSam.connection.disconnect()
    return data.isSuccess
Example #3
0
## Form the command to be sent to the card:
DECRYPT_KEY = [0x88, 0x10, 0, 0, len(DATA)] + DATA + [0x10]
GET_RESPONSE = [0x88, 0xc0, 0x00, 0x00, 0x10]

## First, we send the DECRYPT_KEY command.
## This triggers the decryption in the card. The blue
## light will flash, and the toggle output will show
## a spike.
apdu = DECRYPT_KEY
print 'sending ' + toHexString(apdu)
response, sw1, sw2 = cardservice.connection.transmit( apdu )
print 'response: ', response, ' status words: ', "%x %x" % (sw1, sw2)
## There will be no response here, but the card answers with sw1=0x61, sw2=0x10,
## indicating that there are 16 (=0x10) bytes to read now.

## Now we fetch the decrypted chunk key using the GET_RESPONSE command:
apdu = GET_RESPONSE
print 'sending ' + toHexString(apdu)
response, sw1, sw2 = cardservice.connection.transmit( apdu )
print 'response: ', response, ' status words: ', "%x %x" % (sw1, sw2)

## If we want to check if an assumed key is correct, we calculate the
## result for this assumed key and print it:
ASSUMED_KEY = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
aes_device = Crypto.Cipher.AES.new( toASCIIString( ASSUMED_KEY ) , Crypto.Cipher.AES.MODE_ECB )
response = aes_device.decrypt( toASCIIString( DATA ) )
print 'assumed:  ', bs2hl( response )
## That's it.