1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
import os
import shutil
import subprocess
import tempfile
from _ast import Pow
from os import path
from os import makedirs
from typing import Optional, List, Set, Mapping, MutableMapping, Any, Tuple
from jinja2 import Environment, PackageLoader
from pkg_resources import resource_filename
from public import public
from pyecsca.ec.configuration import HashType, RandomMod, Reduction, Multiplication, Squaring
from pyecsca.ec.coordinates import CoordinateModel
from pyecsca.ec.formula import (Formula)
from pyecsca.ec.model import CurveModel
from pyecsca.ec.mult import (ScalarMultiplier, LTRMultiplier, RTLMultiplier, CoronMultiplier,
LadderMultiplier, SimpleLadderMultiplier, DifferentialLadderMultiplier,
BinaryNAFMultiplier)
from pyecsca.ec.op import OpType, CodeOp
from pyecsca.codegen.common import Platform, DeviceConfiguration
env = Environment(
loader=PackageLoader("pyecsca.codegen")
)
env.globals["isinstance"] = isinstance
def render_op(op: OpType, result: str, left: str, right: str, mod: str, red: str) -> Optional[str]:
"""
Render an operation `op` (add, sub, neg, ...) into a C string.
The operation will use `left` (optional) and `right` operands, be over `mod` modulus,
use the `red` reduction context and leave the result in `result`. All of these need to
be valid C variable names.
"""
if op == OpType.Add:
return "bn_red_add(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Sub:
return "bn_red_sub(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Neg:
return "bn_red_neg(&{}, &{}, &{}, &{});".format(right, mod, red, result)
elif op == OpType.Mult:
return "bn_red_mul(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Div or op == OpType.Inv:
return "bn_red_div(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Sqr:
return "bn_red_sqr(&{}, &{}, &{}, &{});".format(left, mod, red, result)
elif op == OpType.Pow:
return "bn_red_pow(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Id:
return "bn_copy(&{}, &{});".format(left, result)
else:
print(op, result, left, right, mod)
return None
env.globals["render_op"] = render_op
def render_defs(model: CurveModel, coords: CoordinateModel) -> str:
"""
Render the defs.h file with definitions of the curve and point structures.
These change depending on the number (and name) of curve parameters and number
(and name) of point coordinates.
"""
return env.get_template("defs.h").render(params=model.parameter_names,
variables=coords.variables)
def render_curve_impl(model: CurveModel) -> str:
"""
Render the defs.h file with basic curve operations.
These change depending on the number (and name) of curve parameters.
"""
return env.get_template("curve.c").render(params=model.parameter_names)
def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
renames: Mapping[str, str] = None) -> MutableMapping[Any, Any]:
def rename(name: str):
if renames is not None and name not in outputs:
return renames.get(name, name)
return name
allocations: List[str] = []
initializations = {}
const_mapping = {}
operations = []
frees = []
for op in ops:
if op.result not in allocations:
allocations.append(op.result)
frees.append(op.result)
for param in op.parameters:
if param not in allocations and param not in parameters:
raise ValueError("Should be allocated or parameter: {}".format(param))
for const in op.constants:
name = "c" + str(const)
if name not in allocations:
allocations.append(name)
initializations[name] = const
const_mapping[const] = name
frees.append(name)
operations.append((op.operator, op.result, rename(op.left), rename(op.right)))
mapped = []
for op in operations:
o2 = op[2]
if o2 in const_mapping:
o2 = const_mapping[o2]
o3 = op[3]
if o3 in const_mapping and not (isinstance(op[0], Pow) and o3 == 2):
o3 = const_mapping[o3]
mapped.append((op[0], op[1], o2, o3))
returns = {}
if renames:
for r_from, r_to in renames.items():
if r_from in outputs:
returns[r_from] = r_to
return dict(allocations=allocations,
initializations=initializations,
const_mapping=const_mapping, operations=mapped,
frees=frees, returns=returns)
def render_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
renames: Mapping[str, str] = None) -> str:
namespace = transform_ops(ops, parameters, outputs, renames)
return env.get_template("ops.c").render(namespace)
def render_coords_impl(coords: CoordinateModel) -> str:
ops = []
for s in coords.satisfying:
try:
ops.append(CodeOp(s))
except Exception:
pass
renames = {"x": "out_x", "y": "out_y"}
for variable in coords.variables:
renames[variable] = "point->{}".format(variable)
for param in coords.curve_model.parameter_names:
renames[param] = "curve->{}".format(param)
namespace = transform_ops(ops, coords.curve_model.parameter_names,
coords.curve_model.coordinate_names, renames)
returns = namespace["returns"]
namespace["returns"] = {}
frees = namespace["frees"]
namespace["frees"] = {}
return env.get_template("point.c").render(variables=coords.variables, **namespace,
to_affine_rets=returns, to_affine_frees=frees)
def render_formulas_impl(formulas: Set[Formula]) -> str:
names = {formula.shortname for formula in formulas}
return env.get_template("formulas.c").render(names=names)
def render_formula_impl(formula: Formula, short_circuit: bool = False) -> str:
template = env.get_template(f"formula_{formula.shortname}.c")
inputs = ["one", "other", "diff"]
outputs = ["out_one", "out_other"]
renames = {}
for input in formula.inputs:
var = input[0]
num = int(input[1:]) - formula.input_index
renames[input] = "{}->{}".format(inputs[num], var)
for param in formula.coordinate_model.curve_model.parameter_names:
renames[param] = "curve->{}".format(param)
for output in formula.outputs:
var = output[0]
num = int(output[1:]) - formula.output_index
renames[output] = "{}->{}".format(outputs[num], var)
namespace = transform_ops(formula.code, formula.coordinate_model.curve_model.parameter_names,
formula.outputs, renames)
namespace["short_circuit"] = short_circuit
namespace["formula"] = formula
return template.render(namespace)
def render_scalarmult_impl(scalarmult: ScalarMultiplier) -> str:
return env.get_template("mult.c").render(scalarmult=scalarmult, LTRMultiplier=LTRMultiplier,
RTLMultiplier=RTLMultiplier,
CoronMultiplier=CoronMultiplier,
LadderMultiplier=LadderMultiplier,
SimpleLadderMultiplier=SimpleLadderMultiplier,
DifferentialLadderMultiplier=DifferentialLadderMultiplier,
BinaryNAFMultiplier=BinaryNAFMultiplier)
def render_action() -> str:
return env.get_template("action.c").render()
def render_rand() -> str:
return env.get_template("rand.c").render()
def render_main(model: CurveModel, coords: CoordinateModel, keygen: bool, ecdh: bool,
ecdsa: bool) -> str:
return env.get_template("main.c").render(model=model, coords=coords,
curve_variables=coords.variables,
curve_parameters=model.parameter_names,
keygen=keygen, ecdh=ecdh, ecdsa=ecdsa)
def render_makefile(platform: Platform, hash_type: HashType, mod_rand: RandomMod,
reduction: Reduction, mul: Multiplication, sqr: Squaring) -> str:
return env.get_template("Makefile").render(platform=str(platform), hash_type=str(hash_type),
mod_rand=str(mod_rand), reduction=str(reduction),
mul=str(mul), sqr=str(sqr))
def save_render(dir: str, fname: str, rendered: str):
with open(path.join(dir, fname), "w") as f:
f.write(rendered)
@public
def render(config: DeviceConfiguration) -> Tuple[str, str, str]:
"""
Render the `config` into a temporary directory.
:param config: The configuration to render.
:return: The temporary directory, the elf-file name, the hex-file name.
"""
temp = tempfile.mkdtemp()
symlinks = ["asn1", "bn", "hal", "hash", "prng", "simpleserial", "tommath", "fat.h",
"rand.h", "point.h", "curve.h", "mult.h", "formulas.h", "action.h", "Makefile.inc"]
for sym in symlinks:
os.symlink(resource_filename("pyecsca.codegen", sym), path.join(temp, sym))
gen_dir = path.join(temp, "gen")
makedirs(gen_dir, exist_ok=True)
save_render(temp, "Makefile",
render_makefile(config.platform, config.hash_type, config.mod_rand, config.red, config.mult, config.sqr))
save_render(temp, "main.c",
render_main(config.model, config.coords, config.keygen, config.ecdh, config.ecdsa))
save_render(gen_dir, "defs.h", render_defs(config.model, config.coords))
save_render(gen_dir, "point.c", render_coords_impl(config.coords))
save_render(gen_dir, "formulas.c", render_formulas_impl(config.formulas))
for formula in config.formulas:
save_render(gen_dir, f"formula_{formula.shortname}.c",
render_formula_impl(formula, config.scalarmult.short_circuit))
save_render(gen_dir, "action.c", render_action())
save_render(gen_dir, "rand.c", render_rand())
save_render(gen_dir, "curve.c", render_curve_impl(config.model))
save_render(gen_dir, "mult.c", render_scalarmult_impl(config.scalarmult))
return temp, "pyecsca-codegen-{}.elf".format(
str(config.platform)), "pyecsca-codegen-{}.hex".format(str(config.platform))
@public
def build(dir: str, elf_file: str, hex_file: str, outdir: str, strip: bool = False,
remove: bool = True) -> subprocess.CompletedProcess:
"""
Build a rendered configuration.
:param dir: Directory with a rendered implementation.
:param elf_file: Elf-file name.
:param hex_file: Hex-file name.
:param outdir: Output directory to copy the elf and hex files into.
:param strip: Whether to strip the resulting binary of debug symbols.
:param remove: Whether to remove the original directory after build.
:return: The subprocess that ran the build (make).
"""
res = subprocess.run(["make"], cwd=dir, capture_output=True)
if res.returncode != 0:
raise ValueError("Build failed!")
if strip:
subprocess.run(["make", "strip"], cwd=dir, capture_output=True)
full_elf_path = path.join(dir, elf_file)
full_hex_path = path.join(dir, hex_file)
makedirs(outdir, exist_ok=True)
shutil.copy(full_elf_path, outdir)
shutil.copy(full_hex_path, outdir)
if remove:
shutil.rmtree(dir)
return res
@public
def render_and_build(config: DeviceConfiguration, outdir: str, strip: bool = False,
remove: bool = True) -> Tuple[str, str, str, subprocess.CompletedProcess]:
"""
Render and build a `config` in one go.
:param config: The configuration to build.
:param outdir: Output directory to copy the elf and hex files into.
:param strip: Whether to strip the resulting binary of debug symbols.
:param remove: Whether to remove the original directory after build.
:return: The subprocess that ran the build (make).
"""
dir, elf_file, hex_file = render(config)
res = build(dir, elf_file, hex_file, outdir, strip, remove)
return dir, elf_file, hex_file, res
|