Disclaimer: It took me a solid 4-5 hours of looking for an answer and after figuring it out I decided to post it here for people in the same place.
OP-TEE is quite a good environment to develop TAs and CAs, however, there is no straightforward method of acquiring a datetime formatted properly. There is no struct tm
either. Therefore, it made me wonder how do I get a datetime format in OP-TEE TAs?
What I spent a long time trying was to utilize the already supported mbedTLS
libraries which, for a newcomer, would seem like they do support getting datetime format. After all, they do have gmtime
which is supposed to return this value.
However, unfortunately, the gmtime
and relevant functions have no implementation for the platform OP-TEE on ARMv8. That's a pretty tough luck.
So how do you get UTC time in an OP-TEE TA?
All OP-TEE development for ARMv8 is done using C. However, it lacks major libc support. Practically, it has very little libraries (e.g. string.h) which are skimmed down versions from the original libc corresponding libraries.
With that, the provided
<time.h>
in the OP-TEE contains nothing but atypedef
fortime_t
and that's it.The problem can be broken down to two sections:
This is an interesting problem, and while the straight forward solution is to simply do this:
This solution may not be satisfactory for many people who would not want to rely on REE's (Rich Execution Environment, AKA the vulnerable environment) count of epochs. This can be problematic for security sensitive operations where you need time to be legit and with no room for REE to modify it to perform a certain attack.
In the case described above, you will have to obtain the epochs from a hardware clock which will depend on the hardware board you are developing the TA on. You can even retrieve it from location devices which also return UTC time in NMEA statements. While it may not be 100% precise to the second, it may just be enough. If you need very high precision, you will need to get it from a hardware device securely.
Either way, you have to figure out how to get the epochs on your own. This answer focuses on the second part: getting the datetime.
gmtime
which does not exist in the OP-TEE. It has no implementation. And you need a minimalist implementation to keep things simple.Luckily, I was able to find this answer. Which links to
newlib
libraries that develop for Free BSD which are ideal for embedded systems. Hence why it's useful here!I was able to put it together from their implementation and you can use it here:
gmtime_r.h:
gmtime_r.c:
You can put both of these files in your TA folder, and make sure you add
gmtime.c
in the sources list in thesub.mk
. And finally, in the TA itself you can use it:This will print the time and date in a proper format.
Currently, I have not yet ported the implementation of
strftime
, but soon I will do that as well which will auto-format thetm
struct and add the1900
totm_year
and1
totm_mon
.In the meantime, I hope this finds someone in need.