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