Python-ELMO is a Python library which offers an encapsulation of the binary tool ELMO, in order to manipulate it easily in Python and SageMath script.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
pirms 4 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Python-ELMO
  2. _Python-ELMO_ is a Python library which proposes an encapsulation of the project _ELMO_, a statistical leakage simulator for the ARM M0 family.
  3. [MOW17] **Towards Practical Tools for Side
  4. Channel Aware Software Engineering : ’Grey Box’ Modelling for Instruction Leakages**
  5. by _David McCann, Elisabeth Oswald et Carolyn Whitnall_.
  6. https://www.usenix.org/conference/usenixsecurity17/technical-sessions/presentation/mccann
  7. **ELMO GitHub**: https://github.com/sca-research/ELMO
  8. ## Requirements
  9. To use _Python-ELMO_, you need at least Python3.5 and ```numpy```.
  10. The library will install and compile ELMO. So, you need the GCC compiler collection and the command/utility ```make``` (for more details, see the documentation of ELMO). On Ubuntu/Debian,
  11. ```bash
  12. sudo apt install build-essential
  13. ```
  14. To use ELMO on a leaking binary program, you need to compile the C implementations to binary programs (a ".bin" file). "ELMO is not linked to any ARM specific tools, so users should be fine to utilise whatever they want for this purpose. A minimal working platform for compiling your code into an ARM Thumb binary would be to use the GNU ARM Embedded Toolchain (tested version: arm-none-eabi-gcc version 7.3.1 20180622, it can be downloaded from https://developer.arm.com/open-source/gnu-toolchain/gnu-rm).", see the [documentation of ELMO](https://github.com/sca-research/ELMO) for more details.
  15. ## Installation
  16. First, download _Python-ELMO_.
  17. ```bash
  18. git clone https://github.com/ThFeneuil/python-elmo
  19. ```
  20. And then, install ELMO thanks to the installation script. It will use Internet to download the [ELMO project](https://github.com/sca-research/ELMO).
  21. ```bash
  22. python setup.py install
  23. ```
  24. ## Usage
  25. ### Create a new simulation project
  26. What is a _simulation project_ ? It is a project to simulate the power traces of _one_ binary program. It includes
  27. - A Python class to manage the project;
  28. - The C program which will be compile to have the binary program for the analysis;
  29. - A linker script where the configuration of the simulated device are defined.
  30. To start a new project, you can use the following function.
  31. ```python
  32. from elmo import create_simulation
  33. create_simulation(
  34. 'dilithium', # The (relative) path of the project
  35. 'DilithiumSimulation' # The classname of the simulation
  36. )
  37. ```
  38. This function will create a repository ```dilithium``` with all the complete squeleton of the project. In this repository, you can find:
  39. - The file ```project.c``` where you must put the leaking code;
  40. - The file ```projectclass.py``` where there is the class of the simulation which will enable you to generate traces of the project in Python scripts;
  41. - A ```Makefile``` ready to be used with a compiler _arm-none-eabi-gcc_.
  42. Usually a leaking code runs challenges, one challenge giving a power trace. A challenge is the execution of a code with a specific set of data. This set of data is given in the input of the leakage simulation. For example, one can imagine that the leaking code is a symmetric encryption and one wants to study its power leakage according to the message and the secret key. Then, a challenge is the simulation of the leakage for a specific message and for a specific secret key.
  43. So, the classical form of ```project.c``` is the following one:
  44. - It gets a number of challenges with ```readbyte```.
  45. - Then, it loops for each challenge.
  46. - For the challenge, load the specific set of data with ```readbyte```.
  47. - Start the record of the power leakage (start a power trace) with ```starttrigger```.
  48. - Realise the leaking operations with the loaded set of data.
  49. - Stop the record of the power leakage (end a power trace) with ```endtrigger```.
  50. - Eventually output some data with ```printbyte```.
  51. - Indicate to ELMO tool that the simulation is finished with ```endprogram```.
  52. The file ```projectclass.py``` contains a subclass of ```SimulationProject```. It is the description of the ```project.c``` file for the ELMO tool, in order to correctly realise the simulation. It also provides methods to manage the simulation (see following sections).
  53. - The classmethod ```get_binary_path(cl)``` must return the relative path of the leakage binary (```project.c``` correctly compiled).
  54. - The method ```set_input_for_each_challenge(self, input, challenge)``` must write a ```challenge``` in ```input``` using the function ```write```.
  55. Many methods of ```SimulationProject``` can be rewritten in the subclass if necessary. For example, in the case where your ```project.c``` doesn't run challenges, you can rewrite the method ```set_input(self, input)```.
  56. Important! Don't forget that _ELMO_ (and so _Python-ELMO_) needs a **compiled** version of ```project.c``` (see the "Requirements" section for more details). The provided ```Makefile``` is here to help you to compile.
  57. ### List all the available simulation
  58. ```python
  59. from elmo import search_simulations
  60. search_simulations('.')
  61. ```
  62. ```text
  63. {'DilithiumSimulation': <class 'DilithiumSimulation'>,
  64. 'KyberNTTSimulation': <class 'KyberNTTSimulation'>}
  65. ```
  66. _Python-ELMO_ offers a example project to you in the repository ```projects/Examples``` of the module. This example is a project to generate traces of the execution of the NTT implemented in the cryptosystem [Kyber](https://pq-crystals.org/kyber/).
  67. ### Use a simulation project
  68. Warning! Before using it, you have to compile your project thanks to the provided Makefile.
  69. ```python
  70. from elmo import get_simulation
  71. KyberNTTSimulation = get_simulation('KyberNTTSimulation')
  72. simulation = KyberNTTSimulation()
  73. challenges = simulation.get_random_challenges(10)
  74. simulation.set_challenges(challenges)
  75. simulation.run() # Launch the simulation
  76. traces = simulation.get_traces()
  77. # And now, I can draw and analyse the traces
  78. ```
  79. ### Use a simulation project thanks to a server
  80. Sometimes, it is impossible to run the simulation thanks the simple method ```run``` of the project class. Indeed, sometimes the Python script is executed in the environment where _Python-ELMO_ cannot launch the ELMO tool. For example, it is the case where _Python-ELMO_ is used in SageMath on Windows. On Windows, SageMath installation relies on the Cygwin POSIX emulation system and it can be a problem.
  81. To offer a solution, _Python-ELMO_ can be used thanks to a client-server link. The idea is you must launch the following script which will listen (by default) at port 5000 in localhost.
  82. ```bash
  83. python -m elmo run-server
  84. ```
  85. And after, you can manipulate the projects as described in the previous section by replacing ```run``` to ```run_online```.
  86. ```python
  87. from elmo.manage import get_simulation
  88. KyberNTTSimulation = get_simulation('KyberNTTSimulation')
  89. simulation = KyberNTTSimulation()
  90. challenges = simulation.get_random_challenges(10)
  91. simulation.set_challenges(challenges)
  92. simulation.run_online() # Launch the simulation THANKS TO A SERVER
  93. traces = simulation.get_traces()
  94. # And now, I can draw and analyse the traces
  95. ```
  96. Warning! Using the ```run_online``` method doesn't exempt you from compiling the project with the provided Makefile.
  97. ### Use the ELMO Engine
  98. The engine exploits the model of ELMO to directly give the power consumption of an assembler instruction. In the model, to have the power consumption of an assembler instruction, it needs
  99. - the type and the operands of the previous assembler instruction
  100. - the type and the operands of the current assembler instruction
  101. - the type of the next assembler instruction
  102. The type of the instructions are:
  103. - ```EOR``` for ADD(1-4), AND, CMP, CPY, EOR, MOV, ORR, ROR, SUB;
  104. - ```LSL``` for LSL(2), LSR(2);
  105. - ```STR``` for STR, STRB, STRH;
  106. - ```LDR``` for LDR, LDRB, LDRH;
  107. - ```MUL``` for MUL;
  108. - ```OTHER``` for the other instructions.
  109. ```python
  110. from elmo.engine import ELMOEngine, Instr
  111. engine = ELMOEngine()
  112. for i in range(0, 256):
  113. engine.add_point(
  114. (Instr.LDR, Instr.MUL, Instr.OTHER), # Types of the previous, current and next instructions
  115. (0x0000, i), # Operands of the previous instructions
  116. (0x2BAC, i) # Operands of the current instructions
  117. )
  118. engine.run() # Compute the power consumption of all these points
  119. power = engine.power # Numpy 1D array with an entry for each previous point
  120. engine.reset_points() # Reset the engine to study other points
  121. ```
  122. ## Limitations
  123. Since the [ELMO project](https://github.com/sca-research/ELMO) takes its inputs and outputs from files, _Python-ELMO_ **can not** manage simultaneous runs.
  124. ## Licences
  125. [MIT](LICENCE.txt)