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.
- bit_length()[source]¶
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> (37).bit_length() 6
- bit_count()[source]¶
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> (13).bit_count() 3
- to_bytes(length=1, byteorder='big', *, signed=False)[source]¶
Return an array of bytes representing an integer.
- length
Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.
- signed
Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
- from_bytes(byteorder='big', *, signed=False)[source]¶
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.
- signed
Indicates whether two’s complement is used to represent the integer.
- class EmulatorTarget(model, coords, print_config=<Print: 0>, trace_config=<rainbow.rainbow.TraceConfig object>, allow_breakpoints=False)[source]¶
Bases:
Target
-
model:
CurveModel
[source]¶
-
coords:
CoordinateModel
[source]¶
-
params:
Optional
[DomainParameters
][source]¶
-
model:
- 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. SeeDeviceTarget
that usespyecsca.sca.target.chipwhisperer.ChipWhispererTarget
for thar purpose, orHostTarget
that usespyecsca.sca.target.binary.BinaryTarget
.
-
model:
CurveModel
[source]¶
-
coords:
CoordinateModel
[source]¶
-
params:
Optional
[DomainParameters
][source]¶
- 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
]
- scalar_mult(scalar, point)[source]¶
Run scalar multiplication on the target and export the result.
Requires that domain parameters are set up.
- Return type:
- 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
- abstract read(num=0, timeout=0)[source]¶
Read upto
num
bytes or untiltimeout
milliseconds from the target’s serial output.
- recv_msgs(timeout)[source]¶
Receive
SimpleSerialMessage
messages, while waiting uptotimeout
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 atimeout
.- 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
- 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.
- class DeviceTarget(model, coords, platform, **kwargs)[source]¶
Bases:
ImplTarget
,ChipWhispererTarget
A ChipWhisperer-based device target.
- debug()[source]¶
Run the debug command on the target.
Returns the curve model and coordinate model names.
- Return type:
Tuple
[str
,str
]
- 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
]
- read(num=0, timeout=0)[source]¶
Read upto
num
bytes or untiltimeout
milliseconds from the target’s serial output.
- recv_msgs(timeout)[source]¶
Receive
SimpleSerialMessage
messages, while waiting uptotimeout
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:
- send_cmd(cmd, timeout)[source]¶
Send a
SimpleSerialMessage
and receive the response messages that the command produces, within atimeout
.- 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_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]¶
-
coords:
CoordinateModel
[source]¶
-
params:
Optional
[DomainParameters
][source]¶
- 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.
- debug()[source]¶
Run the debug command on the target.
Returns the curve model and coordinate model names.
- Return type:
Tuple
[str
,str
]
- 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
]
- read(num=0, timeout=0)[source]¶
Read upto
num
bytes or untiltimeout
milliseconds from the target’s serial output.
- recv_msgs(timeout)[source]¶
Receive
SimpleSerialMessage
messages, while waiting uptotimeout
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:
- send_cmd(cmd, timeout)[source]¶
Send a
SimpleSerialMessage
and receive the response messages that the command produces, within atimeout
.- 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_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.