Esempio n. 1
0
def test_cmac_subkeys():
    k = binascii.unhexlify("8195088CE6C393708EBBE6C7914ECB0B")
    kx = binascii.unhexlify("2D22571A33B2965A9B49FF4395A43046")

    k0 = LRP.eval_lrp(LRP.generate_plaintexts(k),
                      LRP.generate_updated_keys(k)[0], b"\x00" * 16, True)
    assert (_Element(k0) * _Element(4)).encode().hex() == kx.hex()
Esempio n. 2
0
def execute_test(KEY, IV, FINALIZE, UPDATEDKEY, RES):
    KEY = binascii.unhexlify(KEY)
    RES = binascii.unhexlify(RES)
    FINALIZE = FINALIZE == 1

    assert LRP.eval_lrp(LRP.generate_plaintexts(KEY), LRP.generate_updated_keys(KEY)[UPDATEDKEY], IV, FINALIZE)\
        .hex().upper() == RES.hex().upper()
Esempio n. 3
0
def test_lricb_dec():
    key = binascii.unhexlify("E0C4935FF0C254CD2CEF8FDDC32460CF")
    ct = binascii.unhexlify(
        "FCBBACAA4F29182464F99DE41085266F480E863E487BAAF687B43ED1ECE0D623")

    lrp = LRP(key, 0, b"\xC3\x31\x5D\xBF", pad=True)
    pt = lrp.decrypt(ct)
    assert pt.hex().upper() == "012D7F1653CAF6503C6AB0C1010E8CB0"
Esempio n. 4
0
def test_vectors_generate_updated_keys():
    uk = LRP.generate_updated_keys(
        b"\x56\x78\x26\xB8\xDA\x8E\x76\x84\x32\xA9\x54\x8D\xBE\x4A\xA3\xA0")
    assert uk[
        0] == b"\x16\x3D\x14\xED\x24\xED\x93\x53\x73\x56\x8E\xC5\x21\xE9\x6C\xF4"
    assert uk[
        2] == b"\xFE\x30\xAB\x50\x46\x7E\x61\x78\x3B\xFE\x6B\x5E\x05\x60\x16\x0E"
Esempio n. 5
0
def test_vectors_generate_plaintexts():
    p = LRP.generate_plaintexts(
        b"\x56\x78\x26\xB8\xDA\x8E\x76\x84\x32\xA9\x54\x8D\xBE\x4A\xA3\xA0")
    assert p[
        0] == b"\xAC\x20\xD3\x9F\x53\x41\xFE\x98\xDF\xCA\x21\xDA\x86\xBA\x79\x14"
    assert p[
        15] == b"\x71\xB4\x44\xAF\x25\x7A\x93\x21\x53\x11\xD7\x58\xDD\x33\x32\x47"
Esempio n. 6
0
def test_cmac():
    k = binascii.unhexlify("8195088CE6C393708EBBE6C7914ECB0B")
    lrp = LRP(k, 0, b"\x00" * 16, True)
    assert lrp.cmac(binascii.unhexlify("BBD5B85772C7")).hex() \
        == "AD8595E0B49C5C0DB18E77355F5AAFF6".lower()

    k = binascii.unhexlify("E2F84A0B0AF40EFEB3EEA215A436605C")
    lrp = LRP(k, 0, b"\x00" * 16, True)
    assert lrp.cmac(binascii.unhexlify("8BF1DDA9FE445560A4F4EB9CE0")).hex() \
        == "D04382DF71BC293FEC4BB10BDB13805F".lower()

    k = binascii.unhexlify("5AA9F6C6DE5138113DF5D6B6C77D5D52")
    lrp = LRP(k, 0, b"\x00" * 16, True)
    assert lrp.cmac(binascii.unhexlify("A4434D740C2CB665FE5396959189383F")).hex() \
        == "8B43ADF767E46B692E8F24E837CB5EFC".lower()
Esempio n. 7
0
def test_lrp_sdm():
    key = binascii.unhexlify("00000000000000000000000000000000")

    # suppose that our NDEF URI payload is:
    # https://AAE1508939ECF6FF26BCE407959AB1A5EC022819A35CD293x5E3DB82C19E3865F
    # (this is just an example)
    # settings: LRP mode, encrypted PICCData mirroring with CMAC
    # with SDM MAC input offset: 7, SDM MAC offset: 56, PICC Data offset: 7
    # the dynamic part is:
    msg = "AAE1508939ECF6FF26BCE407959AB1A5EC022819A35CD293x5E3DB82C19E3865F"

    # break it into pieces
    iv = msg[:16]
    picc_data, cmac = msg[16:].split('x')

    # decrypt our message
    lrp = LRP(key, 0, binascii.unhexlify(iv), pad=False)
    decrypted_msg = lrp.decrypt(binascii.unhexlify(picc_data))
    assert decrypted_msg.hex() == "c7042e1d222a63806a000016e2ca89d1"

    # create session key
    # SV = 00h || 01h || 00h || 80h [ || UID] [ || SDMReadCtr] [ || ZeroPadding] || 1Eh || E1h
    svstream = io.BytesIO()
    svstream.write(b"\x00\x01\x00\x80")
    svstream.write(decrypted_msg[1:11])  # UID || SDMReadCtr

    while (svstream.getbuffer().nbytes + 2) % 16 != 0:
        svstream.write(b"\x00")

    svstream.write(b"\x1E\xE1")

    assert svstream.getbuffer().nbytes % 16 == 0

    # generate master key
    lrp = LRP(key, 0)
    master_key = lrp.cmac(svstream.getvalue())

    assert master_key.hex() == "99c2fd9c885c2ca3c9089c20057310c0"

    # generate actual MAC_LRP
    mac_obj = LRP(master_key, 0)
    # everything in hex since PICCData till the MAC offset
    msg_no_cmac = (msg.split('x')[0] + 'x').encode('ascii')
    full_tag = mac_obj.cmac(msg_no_cmac)
    short_tag = bytes(bytearray([full_tag[i] for i in range(16)
                                 if i % 2 == 1])).hex()

    assert short_tag == cmac.lower()
Esempio n. 8
0
 def __init__(self, precision, depth, channel_depth):
     '''Create a new mobile-agent with precision as a multiplier for
        channel_depth, and the depth as it's starting location.'''
     self.precision = precision  # The precision of the movement.
     self.depth = depth  # The current depth, or starting depth.
     self.channel_depth = channel_depth  # The depth of the channel.
     self.action = 1  # The default action is do nothing.
     self.last_action = 1
     self.lrp = LRP(3)  # Use a 3-action LRP to determine direction.
     # action 0 is dive, action 1 is do-nothing, action 2 is surface.
     self.current_to = -1  # Initially the worst possible timeout.
     self.best_to = -1  # Best timeout is the shortest timeout.
Esempio n. 9
0
def test_eval_lrp():
    p = LRP.generate_plaintexts(
        binascii.unhexlify("567826B8DA8E768432A9548DBE4AA3A0"))
    uk = LRP.generate_updated_keys(
        binascii.unhexlify("567826B8DA8E768432A9548DBE4AA3A0"))
    assert LRP.eval_lrp(p, uk[2], b"\x13\x59", final=True).hex() \
        == "1ba2c0c578996bc497dd181c6885a9dd"

    p = LRP.generate_plaintexts(
        binascii.unhexlify("88B95581002057A93E421EFE4076338B"))
    uk = LRP.generate_updated_keys(
        binascii.unhexlify("88B95581002057A93E421EFE4076338B"))
    assert LRP.eval_lrp(p, uk[2], b"\x77\x29\x9D", final=True).hex() \
        == "E9C04556A214AC3297B83E4BDF46F142".lower()

    p = LRP.generate_plaintexts(
        binascii.unhexlify("9AFF3EF56FFEC3153B1CADB48B445409"))
    uk = LRP.generate_updated_keys(
        binascii.unhexlify("9AFF3EF56FFEC3153B1CADB48B445409"))
    assert LRP.eval_lrp(p, uk[3], b"\x4B\x07\x3B\x24\x7C\xD4\x8F\x7E\x0A", final=False).hex() \
        == "909415E5C8BE77563050F2227E17C0E4".lower()
def execute_test_vec(KEY, IV, USEPADDING, PT, CT):
    KEY = binascii.unhexlify(KEY)
    USEPADDING = not not USEPADDING
    PT = binascii.unhexlify(PT)
    CT = binascii.unhexlify(CT)
    IV = binascii.unhexlify(IV)

    lrp = LRP(KEY, 0, IV, USEPADDING)
    assert lrp.encrypt(PT).hex().upper() == CT.hex().upper()
    lrp = LRP(KEY, 0, IV, USEPADDING)
    assert lrp.decrypt(CT).hex().upper() == PT.hex().upper()
Esempio n. 11
0
 def __init__(self, precision, depth, channel_depth):
     '''Create a new mobile-agent with precision as a multiplier for
        channel_depth, and the depth as it's starting location.'''
     self.precision = precision  # The precision of the movement.
     self.depth = depth  # The current depth, or starting depth.
     self.starting_depth = depth  # The starting depth for each iteration.
     self.channel_depth = channel_depth  # The depth of the channel.
     self.action = 1  # The default action is do nothing.
     self.last_action = 1
     self.lrp = LRP(3)  # Use a 3-action LRP to determine direction.
     # action 0 is dive, action 1 is do-nothing, action 2 is surface.
     self.current_to = -1  # Initially the worst possible timeout.
     self.best_to = -1  # Best timeout is the shortest timeout.
     # best_to and current_to are probabilities on [0,1].
     # In order to do better movements we need to store information
     # about consecutive movement.
     self.consecutive = 0
     self.threshold = pow(2, 3)
     self.move_count = 0
     self.mean_movement = []
Esempio n. 12
0
def test_lrp_sdm_with_enc_file():
    key = binascii.unhexlify("00000000000000000000000000000000")

    # suppose our NDEF URI payload is:
    # https://any.domain/?m=65628ED36888CF9C84797E43ECACF114C6ED9A5E101EB592
    # x4ADE304B5AB9474CB40AFFCAB0607A85x87E287E8135BFC06

    msg = "65628ED36888CF9C84797E43ECACF114C6ED9A5E101EB592x4ADE304B5AB9474CB40AFFCAB0607A85x87E287E8135BFC06"

    # break into pieces
    iv = msg[:16]
    picc_data, enc_file_data, cmac = msg[16:].split('x')

    # decrypt our message
    lrp = LRP(key, 0, binascii.unhexlify(iv), pad=False)
    decrypted_msg = lrp.decrypt(binascii.unhexlify(picc_data))
    assert decrypted_msg.hex() == "c7042e1d222a63807b00002993571635"

    # create session key
    # SV = 00h || 01h || 00h || 80h [ || UID] [ || SDMReadCtr] [ || ZeroPadding] || 1Eh || E1h
    svstream = io.BytesIO()
    svstream.write(b"\x00\x01\x00\x80")
    svstream.write(decrypted_msg[1:11])  # UID || SDMReadCtr

    while (svstream.getbuffer().nbytes + 2) % 16 != 0:
        svstream.write(b"\x00")

    svstream.write(b"\x1E\xE1")

    assert svstream.getbuffer().nbytes % 16 == 0

    # generate master key
    lrp = LRP(key, 0)
    master_key = lrp.cmac(svstream.getvalue())

    assert master_key.hex() == "817133dd9fff11b94fc2fb107aa4d971"

    # generate actual MAC_LRP
    mac_obj = LRP(master_key, 0)
    # everything in hex since PICCData till the MAC offset
    msg_no_cmac = "any.domain/?m=65628ED36888CF9C84797E43ECACF114C6ED9A5E101EB592" \
                  "x4ADE304B5AB9474CB40AFFCAB0607A85x".encode('ascii')
    full_tag = mac_obj.cmac(msg_no_cmac)
    short_tag = bytes(bytearray([full_tag[i] for i in range(16)
                                 if i % 2 == 1])).hex()

    assert cmac == short_tag.upper()

    # IV = SDMReadCtr || 00 00 00
    enc_obj = LRP(master_key,
                  1,
                  r=decrypted_msg[8:11] + b"\x00" * 3,
                  pad=False)
    assert enc_obj.decrypt(binascii.unhexlify(enc_file_data)).decode(
        'ascii') == "0102030400000000"
Esempio n. 13
0
from pinger import Pinger
import numpy as np

# Define the number of discrete depths between the surface and seabed.
num_actions = 6
n = 10000
interval = 1
time_between = (n / interval) - 1

# Define the environment with the number of discrete depths for the detectable
# object.
env = Environment(num_actions)
# Define the LRI automata with the same number of actions. This number does
# not correspond to the number of receivers on the array. It is merely the
# representation of the array's ability to detect the object at that depth.
lrp = LRP(num_actions)  # The learning automata.
# The most probable depth that the object exists at, as calculated by the
# learner.
bestdepth = np.zeros(num_actions)
# Define the Markovian Switching Environment that will feed probabilities to
# the Pinger object.
Es = [[0.1, 0.2, 0.4, 0.2, 0.01, 0.09], [0, 0, 0.8, 0.1, 0, 0.1],
      [0, 0, 0, 1, 0, 0], [0.1, 0.1, 0.6, 0.05, 0.01, 0.04]]
mse = MSE(Es)

det_obj = Pinger(mse.env_now())  # Create the detectable object.

# Run 5 individual experiments experiments.
for k in range(len(Es)):
    # Generate an ensemble of n experiments
    for j in range(n):
def execute_test(KEY, Kx, MSG, MAC):
    lrp = LRP(binascii.unhexlify(KEY), 0)
    assert lrp.cmac(binascii.unhexlify(MSG)).hex().upper() == MAC.upper()
from lrp import Linear_Reward_Penalty as LRP
from environment import Environment
from pinger import Pinger
import tune_lrp as tune
import numpy as np

test_lrp = LRP(5)
penaly_probs = [0.3, 0.1, 0.1, 0.1, 0.4]
penalizer = Pinger(np.array(penaly_probs))
env = Environment(5)
a = tune.find_optimal_a(test_lrp, env, penalizer)
print("The value for a after tuning is " + str(test_lrp.a))
b = tune.find_optimal_b(test_lrp, env, penalizer)
print("The value for b after tuning is " + str(test_lrp.b))
test_lrp.a = a
test_lrp.b = b
n = 10000
bestdepth = np.zeros(5)
for j in range(n):
    # reset the action probabilities.
    test_lrp.reset_actions()
    # Run a single experiment. Terminate if it reaches 10000 iterations.
    while (True):
        # Define m as the next action predicting the depth of the object.
        m = test_lrp.next_action()
        # Define req as the next detectable object depth.
        req = penalizer.request()
        # reward if m = req.
        resp = env.response(m, req)
        if (not resp):
            test_lrp.do_reward(m)