|
| 1 | +.. _optee_secure_storage: |
| 2 | + |
| 3 | +********************* |
| 4 | +OP-TEE Secure Storage |
| 5 | +********************* |
| 6 | + |
| 7 | +There is often the need to store sensitive data securely, and guarantee |
| 8 | +confidentiality and integrity. This data includes cryptographic keys, |
| 9 | +certificates and other application-specific or general-purpose data. |
| 10 | + |
| 11 | +To guarantee such security, OP-TEE offers 2 Secure Storage mechanisms: |
| 12 | + |
| 13 | +#. **REE FS:** OP-TEE stores data in normal world's filesystem, as encrypted |
| 14 | + binary blobs. |
| 15 | +#. **RPMB:** OP-TEE stores data in a special partition of compliant storage |
| 16 | + devices such as embedded Multimedia Card (eMMC) and Universal Flash Storage |
| 17 | + (UFS). |
| 18 | + |
| 19 | +REE FS |
| 20 | +====== |
| 21 | + |
| 22 | +**REE FS** stands for *Rich Execution Environment Filesystem*; that is, Linux's |
| 23 | +filesystem on TI SoCs. When the TA needs to store any data in REE FS mode, |
| 24 | +OP-TEE first encrypts and stores the data. This is done through the TEE (Trusted |
| 25 | +Execution Environment) Linux driver using the user-space process |
| 26 | +`tee-supplicant`. |
| 27 | + |
| 28 | +While REE FS guarantees confidentiality of the data (that is, only the TA that |
| 29 | +encrypted it can decrypt it), it does not offer any protection against |
| 30 | +denial-of-service attacks (that is, if attacker compromises Linux root, the attacker can |
| 31 | +delete these blobs). Further, unless the configuration enables RPMB, there's no |
| 32 | +protection offered against replay attacks. |
| 33 | + |
| 34 | +In TI SDK, REE FS is enabled by default. SDK disables RPMB by-default, therefore |
| 35 | +OP-TEE does not offer protection against replay attacks. The encrypted binary blobs are |
| 36 | +stored at ``/var/lib/tee/``. |
| 37 | + |
| 38 | +RPMB |
| 39 | +==== |
| 40 | + |
| 41 | +**RPMB** stands for *Replay Protection Memory Block*. Storage devices such as |
| 42 | +the eMMC on TI SoCs contain a special "RPMB partition". This partition uses an |
| 43 | +"Authentication Key" to authenticate each write. Further, it maintains a counter |
| 44 | +to count the number of write operation, thereby ensuring protection against |
| 45 | +replay attacks. |
| 46 | + |
| 47 | +RPMB does not guarantee confidentiality. Data from the RPMB partition can be |
| 48 | +read by any process without requiring the Authentication Key. Therefore, |
| 49 | +to ensure confidentiality, the application must enable encryption or use RPMB in |
| 50 | +conjunction with REE FS. |
| 51 | + |
| 52 | +For more information on compiling OP-TEE with RPMB enabled, refer: :ref:`optee-rpmb-flags`. |
| 53 | + |
| 54 | +.. _optee-client-rpmb: |
| 55 | + |
| 56 | +OP-TEE Client and RPMB Emulation Mode |
| 57 | +------------------------------------- |
| 58 | + |
| 59 | +The optee-client repository offers a user-space process called |
| 60 | +"tee-supplicant", in ``/usr/sbin/``. This process runs as a systemd service |
| 61 | +by-default. All OP-TEE operations on secure storage (regardless of whether they |
| 62 | +are REE_FS or RPMB) go through tee-supplicant. |
| 63 | + |
| 64 | +OP-TEE client has a flag "RPMB_EMU". When set to `1`, tee-supplicant |
| 65 | +emulates RPMB instead of using the real RPMB on the eMMC. This is useful for |
| 66 | +testing and development activities. This "emulated RPMB" exists only in volatile |
| 67 | +memory, so it is reset at each boot. |
| 68 | + |
| 69 | +One use-case for "emulated RPMB" is to test the programming of the |
| 70 | +Authentication Key, using ``CFG_RPMB_WRITE_KEY=y`` option. Since this is a one-time |
| 71 | +and irreversible operation on the real RPMB, it is safer to test it on the |
| 72 | +emulated one, which is reset at every boot. |
| 73 | + |
| 74 | +Therefore, to use real RPMB, re-compile optee-client with RPMB_EMU=0 and copy |
| 75 | +the tee-supplicant to ``/usr/sbin/``. For more details, see: :ref:`optee-client-build`. |
| 76 | + |
| 77 | + |
| 78 | +Programming the Authentication Key |
| 79 | +---------------------------------- |
| 80 | + |
| 81 | +Initially, on a new TI board, there's no key programmed into the eMMC for the |
| 82 | +RPMB. |
| 83 | + |
| 84 | +There are 2 ways to program the key: |
| 85 | + |
| 86 | +#. Use tools such ``mmc-utils`` to write the key. |
| 87 | +#. Use the ``CFG_RPMB_WRITE_KEY=y`` flag while compiling OP-TEE. |
| 88 | + |
| 89 | +The recommended approach is to use 3rd party tools such as |
| 90 | +``mmc-utils``. ``CFG_RPMB_WRITE_KEY=y`` makes OP-TEE send the key to the normal |
| 91 | +world in plain text. Therefore always disable it in production builds. |
| 92 | + |
| 93 | +If ``CFG_RPMB_WRITE_KEY=y`` is being used for programming the key in factory, |
| 94 | +then 2 sets of OP-TEE binaries are required: |
| 95 | + |
| 96 | +#. the first, with ``CFG_RPMB_WRITE_KEY=y``, to program the key. |
| 97 | +#. the second, with ``CFG_RPMB_WRITE_KEY=n``, for use in production. |
| 98 | + |
| 99 | +.. warning:: |
| 100 | + |
| 101 | + If configuration enables ``CFG_RPMB_WRITE_KEY``, OP-TEE sends the key in |
| 102 | + plain text to Linux normal world. Therefore, ensure that it is **never** |
| 103 | + enabled in production builds. To avoid accidents, the recommended way is to |
| 104 | + **never** use ``CFG_RPMB_WRITE_KEY=y`` at all, and instead to just rely on |
| 105 | + third-party tools like ``mmc-utils``. |
| 106 | + |
| 107 | +.. warning:: |
| 108 | + |
| 109 | + The RPMB Authentication Key is one-time programmable only. Once written, it |
| 110 | + cannot be re-written or modified. Thus if the user writes the wrong key, or |
| 111 | + loses the key, the RPMB partition becomes un-usable. Be careful in writing |
| 112 | + the key. |
| 113 | + |
| 114 | + If configuration enables ``CFG_RPMB_WRITE_KEY``, and tee-supplicant operates |
| 115 | + in emulated RPMB mode, then OP-TEE programs the key to the emulated RPMB, |
| 116 | + which resets at each boot. This is good for testing and development phase. |
| 117 | + |
| 118 | + However, if configuration has RPMB_EMU=0 with ``CFG_RPMB_WRITE_KEY``, or you |
| 119 | + are using other tools, then the key is programmed to the real RPMB, therefore |
| 120 | + be careful! |
| 121 | + |
| 122 | + |
| 123 | +OP-TEE Secure Storage Example |
| 124 | +***************************** |
| 125 | + |
| 126 | +To show secure storage in OP-TEE, use the |
| 127 | +``optee_examples_secure_storage`` binary in the filesystem. |
| 128 | + |
| 129 | +.. code:: |
| 130 | +
|
| 131 | + optee_examples_secure_storage |
| 132 | +
|
| 133 | +This example reads and writes raw data from and to the secure storage. This |
| 134 | +secure storage could be either REE_FS or RPMB or both, based on |
| 135 | +configuration. By default, it is REE_FS. |
0 commit comments