pyecsca.codegen.client module

Client script.

Use it to interact with the built implementations (and flash them if necessary).

Examples

The following examples use the generated implementation in pyecsca-codegen-HOST.elf for the host architecture, which is assumed to use the Short-Weierstrass curve model with projective coordinates.

The first example generates a keypair and exports it.

$ client --platform HOST --fw ./pyecsca-codegen-HOST.elf shortw projective gen secg/secp128r1
(162938999268550597445809790209592423458, Point([x=111810799217268384317536017529141796945, y=309320541414531923178173772704935971498] in shortw/affine))
0.01743340492248535

The following example does ECDH with the target, which first generates a keypair.

$ client --platform HOST --fw ./pyecsca-codegen-HOST.elf shortw projective ecdh secg/secp128r1 122835813094999453922649270086793500655,326514220558629293368386081113307347349
Keypair: 162938999268550597445809790209592423458, [x=111810799217268384317536017529141796945, y=309320541414531923178173772704935971498]
ECDH result: 30567033074c9169e9355a7b348aa7511c3ae605

The following example signs a message "something" using the target, which first generates a keypair.

$ client --platform HOST --fw ./pyecsca-codegen-HOST.elf shortw projective ecdsa-sign secg/secp128r1 something
Keypair: 162938999268550597445809790209592423458, [x=111810799217268384317536017529141796945, y=309320541414531923178173772704935971498]
Signature: 30250211009dc6d5b03cffe0cbd5e829838ecb59e4021023496ba97cf1d816619fe626de2d07b6

The following example verifies a signature on the message "something" using the target and the provided public key.

$ client --platform HOST --fw ./pyecsca-codegen-HOST.elf shortw projective ecdsa-verify secg/secp128r1 111810799217268384317536017529141796945,309320541414531923178173772704935971498 something_else 30250211009dc6d5b03cffe0cbd5e829838ecb59e40210171c895d2d4f27850ff5f2a375ea22b7
Result: True

Note

The implementation has a PRNG it uses to get randomness. This PRNG starts with a constant state. You can use the --seed option to set a custom state.

class Triggers(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: IntFlag

Actions that the implementation can trigger on.

Given that this is a bit-flag, multiple choices are allowed, in which case the trigger signal will toggle onn each action entry/exit.

add = 1[source]
dadd = 2[source]
dbl = 4[source]
ladd = 8[source]
neg = 16[source]
scl = 32[source]
tpl = 64[source]
mult = 128[source]
keygen = 256[source]
ecdh = 512[source]
ecdsa_sign = 1024[source]
ecdsa_verify = 2048[source]
coord_map = 4096[source]
random_mod = 8192[source]
cmd_init_prng(seed)[source]

Build the init PRNG command.

Return type:

str

cmd_set_params(params)[source]

Build the set parameters command.

Return type:

str

cmd_generate()[source]

Build the generate keypair command.

Return type:

str

cmd_set_privkey(privkey)[source]

Build the set private key command.

Return type:

str

cmd_set_pubkey(pubkey)[source]

Build the set public key command.

Return type:

str

cmd_scalar_mult(scalar, point)[source]

Build the scalar multiplication command.

Return type:

str

cmd_ecdh(pubkey)[source]

Build the ECDH command.

Return type:

str

cmd_ecdsa_sign(data)[source]

Build the ECDSA sign command.

Return type:

str

cmd_ecdsa_verify(data, sig)[source]

Build the ECDSA verify command.

Return type:

str

cmd_set_trigger(actions)[source]

Build the set trigger command.

Return type:

str

cmd_debug()[source]

Build the debug command.

Return type:

str

class EmulatorTarget(model, coords, print_config=<Print: 0>, trace_config=<rainbow.rainbow.TraceConfig object>, allow_breakpoints=False)[source]

Bases: Target

An emulator-based target, using the rainbow emulator.

This target will load the binary in an emulator and run the commands by calling the corresponding functions in the binary. It will also hook the simpleserial_put function to get the output data from the implementation.

Note that this target does not support triggers, as the rainbow emulator does not support GPIOs.

emulator: rainbow_stm32f215[source]

The rainbow emulator instance.

result: list[source]

The result of the last command.

trace: list[source]

The trace collected during the emulation.

model: CurveModel[source]

The curve model.

coords: CoordinateModel[source]

The coordinate model.

seed: Optional[bytes][source]

The PRNG seed, if any.

params: Optional[DomainParameters][source]

The domain parameters, if any.

privkey: Optional[int][source]

The private key, if any.

pubkey: Optional[Point][source]

The public key, if any.

connect(**kwargs)[source]

Connect to the target device.

Return type:

None

set_params(params)[source]
Return type:

None

scalar_mult(scalar, point)[source]
Return type:

Point

init_prng(seed)[source]
Return type:

None

generate()[source]
Return type:

Tuple[int, Point]

set_privkey(privkey)[source]
Return type:

None

set_pubkey(pubkey)[source]
Return type:

None

ecdh(other_pubkey)[source]
Return type:

bytes

ecdsa_sign(data)[source]
Return type:

bytes

ecdsa_verify(data, signature)[source]
Return type:

bool

transform_trace(filter_malloc=True, save_instructions=False)[source]

Transform the collected trace into a pyecsca.sca.trace.Trace object.

Return type:

Trace

set_trigger()[source]
debug()[source]
Return type:

Tuple[str, str]

quit()[source]
disconnect()[source]

Disconnect from the target device.

class ImplTarget(model, coords, **kwargs)[source]

Bases: SimpleSerialTarget

A target that is based on an implementation built by pyecsca-codegen.

This is an abstract class that uses the send_cmd method on the pyecsca.sca.target.simpleserial.SimpleSerialTarget class to send commands to the target. That class in turn requires one to implement the read/write/connect/disconnect methods that communicate with the target somehow. See DeviceTarget that uses pyecsca.sca.target.chipwhisperer.ChipWhispererTarget for thar purpose, or HostTarget that uses pyecsca.sca.target.binary.BinaryTarget.

abstract connect()[source]

Connect to the target device.

abstract read(num=0, timeout=0)[source]

Read upto num bytes or until timeout milliseconds from the target’s serial output.

Parameters:
  • num (int) – The number of bytes to read, 0 for all available.

  • timeout (int) – The timeout in milliseconds.

Return type:

bytes

Returns:

The bytes read.

recv_msgs(timeout)[source]

Receive SimpleSerialMessage messages, while waiting upto timeout seconds.

Parameters:

timeout (int) – How long to wait.

Return type:

Mapping[str, SimpleSerialMessage]

Returns:

The received messages with their char.

send_cmd(cmd, timeout)[source]

Send a SimpleSerialMessage and receive the response messages that the command produces, within a timeout.

Parameters:
  • cmd (SimpleSerialMessage) – The command message to send.

  • timeout (int) – The timeout value to wait for the responses.

Return type:

Mapping[str, SimpleSerialMessage]

Returns:

A mapping of the starting character of the message to the message.

abstract write(data)[source]

Write the data bytes to the target’s serial input.

Parameters:

data (bytes) – The data to write.

Return type:

None

timeout: int[source]

The command timeout, in milliseconds.

model: CurveModel[source]

The curve model.

coords: CoordinateModel[source]

The coordinate model.

seed: Optional[bytes][source]

The PRNG seed, if any.

params: Optional[DomainParameters][source]

The domain parameters, if any.

privkey: Optional[int][source]

The private key, if any.

pubkey: Optional[Point][source]

The public key, if any.

trigger: Optional[Triggers][source]

The trigger actions, if any.

init_prng(seed)[source]

Init the PRNG using the seed.

Return type:

None

set_params(params)[source]

Set the domain parameters on the target.

Return type:

None

generate()[source]

Generate a keypair on the target and set it (and export it).

Requires that domain parameters are set up.

Return type:

Tuple[int, Point]

set_privkey(privkey)[source]

Set the private key on the target.

Return type:

None

set_pubkey(pubkey)[source]

Set the public key on the target.

Return type:

None

scalar_mult(scalar, point)[source]

Run scalar multiplication on the target and export the result.

Requires that domain parameters are set up.

Return type:

Point

ecdh(other_pubkey)[source]

Do ECDH with the target.

Requires that domain parameters are set up, as well as a private key.

Return type:

bytes

ecdsa_sign(data)[source]

Sign a message on the target.

Requires that domain parameters are set up, as well as a private key.

Return type:

bytes

ecdsa_verify(data, signature)[source]

Verify a signature on the target.

Requires that domain parameters are set up, as well as a public key.

Return type:

bool

debug()[source]

Run the debug command on the target.

Returns the curve model and coordinate model names.

Return type:

Tuple[str, str]

set_trigger(actions)[source]

Setup the trigger on the target. :rtype: None

Note

The triggers are not exclusive, and you can set to trigger on multiple actions. However, note that they toggle the trigger signal. For example, if you set up triggers on scalar multiplication and addition (Triggers.mult | Triggers.add) the trigger signal will go high once scalar multiplication starts, then go low during each addition operation and finally go low for good after scalar multiplication ends.

quit()[source]

Turn off the target.

disconnect()[source]

Disconnect from the target device.

class DeviceTarget(model, coords, platform, **kwargs)[source]

Bases: ImplTarget, ChipWhispererTarget

A ChipWhisperer-based device target.

connect()[source]

Connect to the target device.

debug()[source]

Run the debug command on the target.

Returns the curve model and coordinate model names.

Return type:

Tuple[str, str]

disconnect()[source]

Disconnect from the target device.

ecdh(other_pubkey)[source]

Do ECDH with the target.

Requires that domain parameters are set up, as well as a private key.

Return type:

bytes

ecdsa_sign(data)[source]

Sign a message on the target.

Requires that domain parameters are set up, as well as a private key.

Return type:

bytes

ecdsa_verify(data, signature)[source]

Verify a signature on the target.

Requires that domain parameters are set up, as well as a public key.

Return type:

bool

flash(fw_path)[source]

Flash the firmware at fw_path to the target.

Parameters:

fw_path (str) – The path to the firmware blob.

Return type:

bool

Returns:

Whether the flashing was successful.

generate()[source]

Generate a keypair on the target and set it (and export it).

Requires that domain parameters are set up.

Return type:

Tuple[int, Point]

init_prng(seed)[source]

Init the PRNG using the seed.

Return type:

None

quit()[source]

Turn off the target.

read(num=0, timeout=0)[source]

Read upto num bytes or until timeout milliseconds from the target’s serial output.

Parameters:
  • num (int) – The number of bytes to read, 0 for all available.

  • timeout (int) – The timeout in milliseconds.

Return type:

bytes

Returns:

The bytes read.

recv_msgs(timeout)[source]

Receive SimpleSerialMessage messages, while waiting upto timeout seconds.

Parameters:

timeout (int) – How long to wait.

Return type:

Mapping[str, SimpleSerialMessage]

Returns:

The received messages with their char.

reset()[source]
scalar_mult(scalar, point)[source]

Run scalar multiplication on the target and export the result.

Requires that domain parameters are set up.

Return type:

Point

send_cmd(cmd, timeout)[source]

Send a SimpleSerialMessage and receive the response messages that the command produces, within a timeout.

Parameters:
  • cmd (SimpleSerialMessage) – The command message to send.

  • timeout (int) – The timeout value to wait for the responses.

Return type:

Mapping[str, SimpleSerialMessage]

Returns:

A mapping of the starting character of the message to the message.

set_params(params)[source]

Set the domain parameters on the target.

Return type:

None

set_privkey(privkey)[source]

Set the private key on the target.

Return type:

None

set_pubkey(pubkey)[source]

Set the public key on the target.

Return type:

None

set_trigger(actions)[source]

Setup the trigger on the target. :rtype: None

Note

The triggers are not exclusive, and you can set to trigger on multiple actions. However, note that they toggle the trigger signal. For example, if you set up triggers on scalar multiplication and addition (Triggers.mult | Triggers.add) the trigger signal will go high once scalar multiplication starts, then go low during each addition operation and finally go low for good after scalar multiplication ends.

write(data)[source]

Write the data bytes to the target’s serial input.

Parameters:

data (bytes) – The data to write.

Return type:

None

model: CurveModel[source]

The curve model.

coords: CoordinateModel[source]

The coordinate model.

seed: Optional[bytes][source]

The PRNG seed, if any.

params: Optional[DomainParameters][source]

The domain parameters, if any.

privkey: Optional[int][source]

The private key, if any.

pubkey: Optional[Point][source]

The public key, if any.

trigger: Optional[Triggers][source]

The trigger actions, if any.

timeout: int[source]

The command timeout, in milliseconds.

class HostTarget(model, coords, **kwargs)[source]

Bases: ImplTarget, BinaryTarget

A host-based target, will just run the binary on your machine and communicate with it via stdin/stdout.

connect()[source]

Connect to the target device.

debug()[source]

Run the debug command on the target.

Returns the curve model and coordinate model names.

Return type:

Tuple[str, str]

disconnect()[source]

Disconnect from the target device.

ecdh(other_pubkey)[source]

Do ECDH with the target.

Requires that domain parameters are set up, as well as a private key.

Return type:

bytes

ecdsa_sign(data)[source]

Sign a message on the target.

Requires that domain parameters are set up, as well as a private key.

Return type:

bytes

ecdsa_verify(data, signature)[source]

Verify a signature on the target.

Requires that domain parameters are set up, as well as a public key.

Return type:

bool

generate()[source]

Generate a keypair on the target and set it (and export it).

Requires that domain parameters are set up.

Return type:

Tuple[int, Point]

init_prng(seed)[source]

Init the PRNG using the seed.

Return type:

None

process: Optional[Popen] = None[source]
quit()[source]

Turn off the target.

read(num=0, timeout=0)[source]

Read upto num bytes or until timeout milliseconds from the target’s serial output.

Parameters:
  • num (int) – The number of bytes to read, 0 for all available.

  • timeout (int) – The timeout in milliseconds.

Return type:

bytes

Returns:

The bytes read.

recv_msgs(timeout)[source]

Receive SimpleSerialMessage messages, while waiting upto timeout seconds.

Parameters:

timeout (int) – How long to wait.

Return type:

Mapping[str, SimpleSerialMessage]

Returns:

The received messages with their char.

scalar_mult(scalar, point)[source]

Run scalar multiplication on the target and export the result.

Requires that domain parameters are set up.

Return type:

Point

send_cmd(cmd, timeout)[source]

Send a SimpleSerialMessage and receive the response messages that the command produces, within a timeout.

Parameters:
  • cmd (SimpleSerialMessage) – The command message to send.

  • timeout (int) – The timeout value to wait for the responses.

Return type:

Mapping[str, SimpleSerialMessage]

Returns:

A mapping of the starting character of the message to the message.

set_params(params)[source]

Set the domain parameters on the target.

Return type:

None

set_privkey(privkey)[source]

Set the private key on the target.

Return type:

None

set_pubkey(pubkey)[source]

Set the public key on the target.

Return type:

None

set_trigger(actions)[source]

Setup the trigger on the target. :rtype: None

Note

The triggers are not exclusive, and you can set to trigger on multiple actions. However, note that they toggle the trigger signal. For example, if you set up triggers on scalar multiplication and addition (Triggers.mult | Triggers.add) the trigger signal will go high once scalar multiplication starts, then go low during each addition operation and finally go low for good after scalar multiplication ends.

write(data)[source]

Write the data bytes to the target’s serial input.

Parameters:

data (bytes) – The data to write.

Return type:

None

model: CurveModel[source]

The curve model.

coords: CoordinateModel[source]

The coordinate model.

seed: Optional[bytes][source]

The PRNG seed, if any.

params: Optional[DomainParameters][source]

The domain parameters, if any.

privkey: Optional[int][source]

The private key, if any.

pubkey: Optional[Point][source]

The public key, if any.

trigger: Optional[Triggers][source]

The trigger actions, if any.

timeout: int[source]

The command timeout, in milliseconds.

binary: List[str][source]
debug_output: bool[source]