Skip to content

Commit 45afe97

Browse files
committed
Add OP-TEE heap, RNG and time platform support under WOLFSSL_OPTEE
1 parent 5c3e356 commit 45afe97

8 files changed

Lines changed: 133 additions & 2 deletions

File tree

‎src/internal.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11998,6 +11998,20 @@ ProtocolVersion MakeDTLSv1_3(void)
1199811998
}
1199911999
#endif
1200012000

12001+
#elif defined(WOLFSSL_OPTEE)
12002+
word32 LowResTimer(void)
12003+
{
12004+
TEE_Time now;
12005+
12006+
/* TEE_GetSystemTime is monotonic for the life of the trusted
12007+
* application and is not the normal world's clock, so it cannot be
12008+
* steered from outside. It has no relation to the epoch, which is all
12009+
* this needs. */
12010+
TEE_GetSystemTime(&now);
12011+
12012+
return (word32)now.seconds;
12013+
}
12014+
1200112015
#elif defined(TIME_OVERRIDES)
1200212016
#if !defined(NO_ASN) && !defined(NO_ASN_TIME)
1200312017
/* use same asn time overrides unless user wants tick override above */

‎src/tls13.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,20 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
21172117
}
21182118
#endif
21192119

2120+
#elif defined(WOLFSSL_OPTEE)
2121+
word32 TimeNowInMilliseconds(void)
2122+
{
2123+
TEE_Time now;
2124+
2125+
/* TEE_GetSystemTime is monotonic for the life of the trusted
2126+
* application and is not the normal world's clock, so it cannot be
2127+
* steered from outside. It has no relation to the epoch, which is all
2128+
* this needs. */
2129+
TEE_GetSystemTime(&now);
2130+
2131+
return (word32)((now.seconds * 1000) + now.millis);
2132+
}
2133+
21202134
#elif defined(TIME_OVERRIDES)
21212135
#if !defined(NO_ASN) && !defined(NO_ASN_TIME)
21222136
word32 TimeNowInMilliseconds(void)
@@ -2400,6 +2414,20 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
24002414
}
24012415
#endif
24022416

2417+
#elif defined(WOLFSSL_OPTEE)
2418+
sword64 TimeNowInMilliseconds(void)
2419+
{
2420+
TEE_Time now;
2421+
2422+
/* TEE_GetSystemTime is monotonic for the life of the trusted
2423+
* application and is not the normal world's clock, so it cannot be
2424+
* steered from outside. It has no relation to the epoch, which is all
2425+
* this needs. */
2426+
TEE_GetSystemTime(&now);
2427+
2428+
return ((sword64)now.seconds * 1000) + (sword64)now.millis;
2429+
}
2430+
24032431
#elif defined(TIME_OVERRIDES)
24042432
#if !defined(NO_ASN) && !defined(NO_ASN_TIME)
24052433
sword64 TimeNowInMilliseconds(void)

‎wolfcrypt/benchmark/benchmark.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19013,6 +19013,22 @@ void bench_mldsaKeySign(byte level)
1901319013
return ret;
1901419014
#endif
1901519015
}
19016+
#elif defined(WOLFSSL_OPTEE)
19017+
19018+
double current_time(int reset)
19019+
{
19020+
TEE_Time now;
19021+
19022+
(void)reset;
19023+
19024+
/* Monotonic for the life of the trusted application, millisecond
19025+
* granularity. Short operations need enough iterations that the
19026+
* resolution does not dominate. */
19027+
TEE_GetSystemTime(&now);
19028+
19029+
return (double)now.seconds + ((double)now.millis / 1000.0);
19030+
}
19031+
1901619032
#elif defined(WOLFSSL_ZEPHYR)
1901719033

1901819034
#include <time.h>

‎wolfcrypt/src/random.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ This library contains implementation for the random number generator.
221221
#elif defined(WOLFSSL_DEOS)
222222
#elif defined(MICRIUM)
223223
#elif defined(WOLFSSL_NUCLEUS)
224+
#elif defined(WOLFSSL_OPTEE)
225+
/* for TEE_GenerateRandom */
226+
#include <tee_internal_api.h>
224227
#elif defined(WOLFSSL_PB)
225228
#elif defined(WOLFSSL_ZEPHYR)
226229
#elif defined(WOLFSSL_TELIT_M2MB)
@@ -10570,6 +10573,21 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
1057010573
return 0;
1057110574
}
1057210575

10576+
#elif defined(WOLFSSL_OPTEE)
10577+
10578+
/* OP-TEE trusted application. TEE_GenerateRandom is the secure world's
10579+
* own entropy source, so it seeds the DRBG here rather than replacing it;
10580+
* a build that would rather use it directly can still set
10581+
* CUSTOM_RAND_GENERATE_BLOCK. */
10582+
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
10583+
{
10584+
(void)os;
10585+
10586+
TEE_GenerateRandom(output, (size_t)sz);
10587+
10588+
return 0;
10589+
}
10590+
1057310591
#elif defined(WOLFSSL_PB)
1057410592

1057510593
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)

‎wolfcrypt/src/wc_port.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4922,6 +4922,25 @@ time_t fsl_time(time_t* t)
49224922
}
49234923
#endif
49244924

4925+
#if defined(WOLFSSL_OPTEE)
4926+
4927+
time_t optee_time(time_t* timer)
4928+
{
4929+
TEE_Time reeTime;
4930+
4931+
/* Seconds since the epoch, from the rich OS. See the note on XTIME in
4932+
* wc_port.h: the normal world owns this clock, so it is not a trusted
4933+
* time source. */
4934+
TEE_GetREETime(&reeTime);
4935+
4936+
if (timer != NULL)
4937+
*timer = (time_t)reeTime.seconds;
4938+
4939+
return (time_t)reeTime.seconds;
4940+
}
4941+
4942+
#endif /* WOLFSSL_OPTEE */
4943+
49254944
#if defined(MICRIUM)
49264945

49274946
time_t micrium_time(time_t* timer)

‎wolfssl/wolfcrypt/settings.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,8 +3525,11 @@
35253525
#define NO_WRITEV
35263526
#define NO_MAIN_DRIVER
35273527
#define SINGLE_THREADED
3528-
#define NO_ASN_TIME /* no wall clock in a TA */
3529-
#define USER_TICKS
3528+
/* No trusted wall clock in a trusted application: the only one belongs to
3529+
* the normal world. The timers come from TEE_GetSystemTime, which is
3530+
* monotonic and outside the normal world's reach, so USER_TICKS is not
3531+
* needed; a project can still define it to supply its own. */
3532+
#define NO_ASN_TIME
35303533
#endif /* WOLFSSL_OPTEE */
35313534

35323535
/* FreeScale MMCAU hardware crypto has 4 byte alignment.

‎wolfssl/wolfcrypt/types.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,25 @@ enum {
833833
#endif
834834
#define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
835835

836+
#elif defined(WOLFSSL_OPTEE)
837+
/* An OP-TEE trusted application links libutee rather than a C library and
838+
* has no std heap; the TEE Internal Core API supplies the allocator.
839+
* TEE_MALLOC_FILL_ZERO so an allocation never carries another one's
840+
* bytes, which matters more here than the cost: a trusted application
841+
* holds key material. */
842+
#include <tee_internal_api.h>
843+
#define XMALLOC(s, h, t) ((void)(h), (void)(t), \
844+
TEE_Malloc((size_t)(s), TEE_MALLOC_FILL_ZERO))
845+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
846+
#define XFREE(p, h, t) do { (void)(h); (void)(t); \
847+
TEE_Free(p); } while (0)
848+
#else
849+
#define XFREE(p, h, t) do { void* xp = (p); (void)(h); (void)(t); \
850+
if (xp != NULL) TEE_Free(xp); } while (0)
851+
#endif
852+
#define XREALLOC(p, n, h, t) ((void)(h), (void)(t), \
853+
TEE_Realloc((p), (size_t)(n)))
854+
836855
#elif defined(NO_WOLFSSL_MEMORY)
837856
#ifdef WOLFSSL_NO_MALLOC
838857
/* this platform does not support heap use */

‎wolfssl/wolfcrypt/wc_port.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,20 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
17481748
extern time_t deos_time(time_t* timer);
17491749
#define XTIME(t1) deos_time((t1))
17501750
#endif
1751+
#elif defined(WOLFSSL_OPTEE)
1752+
#include <time.h>
1753+
#include <tee_internal_api.h>
1754+
/* A trusted application has no trusted wall clock. TEE_GetREETime is the
1755+
* normal world's, so it is attacker influenced and fit for logging and
1756+
* freshness, not for deciding certificate validity. That is why the
1757+
* WOLFSSL_OPTEE block in settings.h sets NO_ASN_TIME; a port that turns
1758+
* that off is choosing to trust the rich OS clock. */
1759+
#ifndef XTIME
1760+
extern time_t optee_time(time_t* timer);
1761+
#define XTIME(t1) optee_time((t1))
1762+
#endif
1763+
#define WOLFSSL_GMTIME
1764+
17511765
#elif defined(MICRIUM)
17521766
#include <clk.h>
17531767
#include <time.h>

0 commit comments

Comments
 (0)