|
| 1 | +from communication.sca_aes_commands import OTAES |
| 2 | +from communication.sca_prng_commands import OTPRNG |
| 3 | +from communication.sca_trigger_commands import OTTRIGGER |
| 4 | +from communication.chip import * |
| 5 | +from communication.dut import DUT |
| 6 | +import time |
| 7 | + |
| 8 | +def char_aes_single_encrypt(opentitantool, iterations, masking, key, text): |
| 9 | + target = DUT() |
| 10 | + reset_target(opentitantool) |
| 11 | + # Clear the output from the reset |
| 12 | + target.dump_all() |
| 13 | + |
| 14 | + aessca = OTAES(target, "ujson") |
| 15 | + # Initialize our chip and catch its output |
| 16 | + device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0) |
| 17 | + |
| 18 | + # Check whether we enable the masking |
| 19 | + if masking: |
| 20 | + lfsr_seed = 1 |
| 21 | + else: |
| 22 | + lfsr_seed = 0 |
| 23 | + aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little")) |
| 24 | + |
| 25 | + # Set the trigger |
| 26 | + triggersca = OTTRIGGER(target, "ujson") |
| 27 | + triggersca.select_trigger(0) |
| 28 | + |
| 29 | + aessca.key_set(key) |
| 30 | + |
| 31 | + for _ in range(iterations): |
| 32 | + aessca.single_encrypt(text) |
| 33 | + response = target.read_response() |
| 34 | + return response |
| 35 | + |
| 36 | +def char_aes_batch_alternative_encrypt(opentitantool, iterations, num_segments, masking, key, text): |
| 37 | + target = DUT() |
| 38 | + reset_target(opentitantool) |
| 39 | + # Clear the output from the reset |
| 40 | + target.dump_all() |
| 41 | + |
| 42 | + aessca = OTAES(target, "ujson") |
| 43 | + # Initialize our chip and catch its output |
| 44 | + device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0) |
| 45 | + |
| 46 | + # Check whether we enable the masking |
| 47 | + if masking: |
| 48 | + lfsr_seed = 1 |
| 49 | + else: |
| 50 | + lfsr_seed = 0 |
| 51 | + aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little")) |
| 52 | + |
| 53 | + # Set the trigger |
| 54 | + triggersca = OTTRIGGER(target, "ujson") |
| 55 | + triggersca.select_trigger(0) |
| 56 | + |
| 57 | + aessca.key_set(key, len(key)) |
| 58 | + aessca.batch_plaintext_set(text, len(text)) |
| 59 | + |
| 60 | + for _ in range(iterations): |
| 61 | + aessca.batch_alternative_encrypt(num_segments) |
| 62 | + response = target.read_response() |
| 63 | + return response |
| 64 | + |
| 65 | +def char_aes_batch_data_fvsr_encrypt(opentitantool, iterations, num_segments, masking): |
| 66 | + target = DUT() |
| 67 | + reset_target(opentitantool) |
| 68 | + # Clear the output from the reset |
| 69 | + target.dump_all() |
| 70 | + |
| 71 | + aessca = OTAES(target, "ujson") |
| 72 | + # Initialize our chip and catch its output |
| 73 | + device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0) |
| 74 | + |
| 75 | + # Check whether we enable the masking |
| 76 | + if masking: |
| 77 | + lfsr_seed = 1 |
| 78 | + else: |
| 79 | + lfsr_seed = 0 |
| 80 | + aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little")) |
| 81 | + |
| 82 | + # Set the trigger |
| 83 | + triggersca = OTTRIGGER(target, "ujson") |
| 84 | + triggersca.select_trigger(0) |
| 85 | + |
| 86 | + # Set the internal prng |
| 87 | + ot_prng = OTPRNG(target=target, protocol="ujson") |
| 88 | + ot_prng.seed_prng([0,0,0,0]) |
| 89 | + |
| 90 | + # Generate plaintexts and keys for first batch. |
| 91 | + aessca.start_fvsr_batch_generate(2) |
| 92 | + |
| 93 | + for _ in range(iterations): |
| 94 | + aessca.fvsr_data_batch_encrypt(num_segments) |
| 95 | + response = target.read_response() |
| 96 | + return response |
| 97 | + |
| 98 | +def char_aes_batch_key_fvsr_encrypt(opentitantool, iterations, num_segments, masking): |
| 99 | + target = DUT() |
| 100 | + reset_target(opentitantool) |
| 101 | + # Clear the output from the reset |
| 102 | + target.dump_all() |
| 103 | + |
| 104 | + aessca = OTAES(target, "ujson") |
| 105 | + # Initialize our chip and catch its output |
| 106 | + device_id, owner_page, boot_log, boot_measurements, version = aessca.init(0) |
| 107 | + |
| 108 | + # Check whether we enable the masking |
| 109 | + if masking: |
| 110 | + lfsr_seed = 1 |
| 111 | + else: |
| 112 | + lfsr_seed = 0 |
| 113 | + aessca.seed_lfsr(lfsr_seed.to_bytes(4, "little")) |
| 114 | + |
| 115 | + # Set the trigger |
| 116 | + triggersca = OTTRIGGER(target, "ujson") |
| 117 | + triggersca.select_trigger(0) |
| 118 | + |
| 119 | + # Set the internal prng |
| 120 | + ot_prng = OTPRNG(target=target, protocol="ujson") |
| 121 | + ot_prng.seed_prng([0,0,0,0]) |
| 122 | + |
| 123 | + # Generate plaintexts and keys for first batch. |
| 124 | + aessca.start_fvsr_batch_generate(1) |
| 125 | + aessca.write_fvsr_batch_generate(num_segments) |
| 126 | + |
| 127 | + for _ in range(iterations): |
| 128 | + aessca.fvsr_key_batch_encrypt(num_segments) |
| 129 | + response = target.read_response() |
| 130 | + return response |
| 131 | + |
0 commit comments