aboutsummaryrefslogtreecommitdiffhomepage
path: root/emulator.ipynb
blob: 3a1f32a2a96d61fb258dcf22e0cf129dcc3a1fbb (plain) (blame)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Emulation and leakage simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook demonstrates the functionality of the `EmulatorTarget` class, which can emulate **pyecsca** generated C implementations for `STM32F3` target using Rainbow as a basis as well as simulate side-channel leakage. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialisation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecsca.ec.mult import LTRMultiplier\n",
    "from pyecsca.ec.mod import mod\n",
    "from pyecsca.ec.point import Point, InfinityPoint\n",
    "from pyecsca.ec.model import ShortWeierstrassModel\n",
    "from pyecsca.ec.curve import EllipticCurve\n",
    "from pyecsca.ec.params import DomainParameters\n",
    "from pyecsca.ec.key_generation import KeyGeneration\n",
    "from pyecsca.ec.key_agreement import ECDH_SHA1\n",
    "from pyecsca.ec.configuration import *\n",
    "from pyecsca.codegen.client import EmulatorTarget\n",
    "from pyecsca.codegen.common import Platform\n",
    "from pyecsca.codegen.common import DeviceConfiguration\n",
    "from pyecsca.codegen.builder import render\n",
    "from pyecsca.sca.trace import Trace\n",
    "from pyecsca.sca.trace.plot import plot_trace\n",
    "from pyecsca.sca.trace.process import rolling_mean\n",
    "\n",
    "\n",
    "from rainbow import TraceConfig\n",
    "from rainbow.leakage_models import HammingWeight\n",
    "\n",
    "from binascii import hexlify\n",
    "from random import randbytes, randint\n",
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from subprocess import run\n",
    "from os.path import join\n",
    "from copy import copy "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We first define the elliptic curve parameters we are going to be using for the demonstration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ShortWeierstrassModel()\n",
    "coords = model.coordinates[\"projective\"]\n",
    "p = 0xd7d1247f\n",
    "a = mod(0xa4a44016, p)\n",
    "b = mod(0x73f76716, p)\n",
    "n = 0xd7d2a475\n",
    "h = 1\n",
    "gx, gy, gz = mod(0x54eed6d7, p), mod(0x6f1e55ac, p), mod(1, p)\n",
    "generator = Point(coords, X=gx, Y=gy, Z=gz)\n",
    "neutral = InfinityPoint(coords)\n",
    "\n",
    "curve = EllipticCurve(model, coords, p, neutral, {\"a\": a, \"b\": b})\n",
    "params = DomainParameters(curve, generator, n, h)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create and initialize an instance of the `EmulatorTarget` class with the above EC parameters and the `TraceConfig` class instance, which configures the simulated leakage trace to contain the Hamming Weight of the emulator's register values. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = EmulatorTarget(model, coords, trace_config=TraceConfig(register=HammingWeight()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We generate code and build it using **pyecsca** (for more details see `codegen.ipynb` notebook) and load the resulting binary into the emulator. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "platform = Platform.STM32F3\n",
    "hash_type = HashType.SHA1\n",
    "mod_rand = RandomMod.REDUCE\n",
    "mult = Multiplication.BASE\n",
    "sqr = Squaring.BASE\n",
    "red = Reduction.BASE\n",
    "inv = Inversion.GCD\n",
    "\n",
    "model = ShortWeierstrassModel()\n",
    "coords = model.coordinates[\"projective\"]\n",
    "add = coords.formulas[\"add-1998-cmo\"]\n",
    "dbl = coords.formulas[\"dbl-1998-cmo\"]\n",
    "scl = coords.formulas[\"z\"]\n",
    "formulas = [add, dbl, scl]\n",
    "scalarmult = LTRMultiplier(add, dbl, scl)\n",
    "\n",
    "config = DeviceConfiguration(model, coords, formulas, scalarmult, \n",
    "\t\t\t\t\t\t\t hash_type, mod_rand, mult, sqr, red,\n",
    "\t\t\t\t\t\t\t inv, platform, True, True, True)\n",
    "\n",
    "directory, elf_name, hex_name = render(config)\n",
    "\n",
    "run([\"make\"], cwd=directory)\n",
    "join(directory, hex_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target.connect(binary=join(directory, elf_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the emulated functions to work correctly, we need to set the parameters of the curve in the emulator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target.set_params(params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Emulator functionality"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Scalar multiplication"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Perform scalar multiplication on given point with given scalar and compare with pyecsca."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scalar = randint(128, 255)\n",
    "point = params.curve.affine_random().to_model(coords, params.curve)\n",
    "emulatorResult = target.scalar_mult(scalar, point)\n",
    "print(emulatorResult)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use pyecsca to validate correctness of the emulator result. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "generator = params.generator\n",
    "model = params.curve.model\n",
    "coords = params.curve.coordinate_model\n",
    "add = coords.formulas[\"add-1998-cmo\"]\n",
    "dbl = coords.formulas[\"dbl-1998-cmo\"]\n",
    "scl = coords.formulas[\"z\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mult_sm = LTRMultiplier(add, dbl, scl)\n",
    "mult_sm.init(params, point)\n",
    "\n",
    "pyecscaResult = mult_sm.multiply(scalar)\n",
    "print(pyecscaResult)\n",
    "print(emulatorResult.equals(pyecscaResult))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Key generation"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate private and public key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed_bytes = randbytes(32)\n",
    "target.init_prng(seed_bytes)\n",
    "priv, pub = target.generate()\n",
    "pub = pub.to_model(coords, params.curve)\n",
    "\n",
    "print(\"private key:\", priv)\n",
    "print(\"public key:\", pub)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We check if we generated valid key pair using pyecsca."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(params.curve.is_on_curve(pub))\n",
    "pyecscaPub = params.curve.affine_multiply(params.generator.to_affine(), priv).to_model(coords, params.curve)\n",
    "print(pyecscaPub)\n",
    "print(pub.equals(pyecscaPub))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setting private and public key"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to emulate **ECDH** and **ECDSA** algorithms, the emulator needs private and public keys set. This can be done by methods below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Before private:\", target.privkey)\n",
    "print(\"Before public:\", target.pubkey)\n",
    "\n",
    "target.set_privkey(priv)\n",
    "target.set_pubkey(pub)\n",
    "\n",
    "print(\"After private:\", target.privkey)\n",
    "print(\"After public:\", target.pubkey)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ECDH"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Perform key agreement using ECDH."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "other_priv, other_pub = target.generate()\n",
    "other_pub = other_pub.to_model(coords, params.curve)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shared_secret = target.ecdh(pub)\n",
    "print(\"shared secret:\", hexlify(shared_secret))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check the result is correct using pyecsca."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mult_ecdh = LTRMultiplier(add, dbl, scl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecdh_a = ECDH_SHA1(copy(mult_ecdh), params, pub, other_priv)\n",
    "ecdh_b = ECDH_SHA1(copy(mult_ecdh), params, other_pub, priv)\n",
    "ecdh_a_result = ecdh_a.perform()\n",
    "ecdh_b_result = ecdh_b.perform()\n",
    "print(hexlify(ecdh_a_result))\n",
    "print(hexlify(ecdh_b_result)) \n",
    "print(ecdh_a_result == ecdh_b_result == shared_secret)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ECDSA"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Perform signing over given data and verify the signature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"text\"\n",
    "signed_message = target.ecdsa_sign(message.encode())\n",
    "res = target.ecdsa_verify(message.encode(), bytes(signed_message))\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"text1\"\n",
    "signed_message = target.ecdsa_sign(message.encode())\n",
    "message = \"text2\"\n",
    "res = target.ecdsa_verify(message.encode(), bytes(signed_message))\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Leakage simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While the `EmulatorTarget` performs the above methods, it simulates leakage. The leakage trace is stored in `self.trace` variable. In our case, the trace will contain dictionaries of type `{\"type\": \"code\", \"register\": x}`, where `x` is Hamming Weight of the current register value. For other configurations of the trace, see https://github.com/Ledger-Donjon/rainbow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Leakage trace of scalar multiplication"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We perform scalar multiplication and look at the sample of the leakage trace in `EmulatorTarget`'s `trace` variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scalar = 229\n",
    "point = params.curve.affine_random().to_model(coords, params.curve)\n",
    "target.trace = []\n",
    "emulatorResult = target.scalar_mult(scalar, point)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(target.trace[0:10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use the pyecsca's functionality of working with leakage traces, we transform the trace from dictionary to pyecsca's `Trace` using `EmulatorTarget`'s `process_trace` method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trace = target.transform_trace()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(trace[0:10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now visualize what the whole trace looks like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hv.extension(\"bokeh\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_trace(trace).opts(width=950, height=600)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SPA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now analyze the trace and try to gain information about the execution of the algorithm and/or recover the secret scalar."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We apply rolling mean to the trace to smooth it out, reduce noise and make the actions executed during the algorithm better identifiable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spa_trace = rolling_mean(trace, 3000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see repeated patterns in the resulting trace. Since the emulation consists of performing either point addition or point doubling repeadetly, we can map their execution to the repeated patterns. We know that the doubling operation will be performed for each bit of the scalar, while the addition operation will be performed only when the currently processed bit of the scalar is equal to one. Using this knowledge, we can see in the trace that the order of executed operations is as follows: *dbl-add-dbl-add-dbl-dbl-dbl-add-dbl-dbl-add*. This means that the scalar used equals *11100101 = 229*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_trace(spa_trace).opts(width=950, height=600)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}