Skip to content

Commit

Permalink
ECC: optimize the pool of temporary variables
Browse files Browse the repository at this point in the history
ECC is using a lot (80) temporary variables. These variables
are taken from a static pool, each being of the maximum key size
supported in OP-TEE: 4096bits, times 2 to include
wrapping multiplication in temporary computation.

With the introduction of being able to get temporary variables
of a given size, the current patch optimize the use of the variables
in case of ECC.

Thanks to this patch, the number of temporary variables is back to 50,
and the emulated esram size (QEMU / FVP / HiKey) is back to 200KB.

Note that further optimization can be performed, for ECC and also
for other algorithms (RSA,...).

Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Tested-by: Pascal Brand <pascal.brand@linaro.org> (QEMU platform)
Signed-off-by: Pascal Brand <pascal.brand@st.com>
[jf: pick 6d914f6, keep LTC changes only, use new header paths]
[jf: resolve conflicts, applying 0c6c51d on top of LTC v1.18.2-681-ge6be20bf]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
Pascal Brand authored and jenswi-linaro committed Apr 24, 2024
1 parent b32f841 commit a4281f4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/lib/libtomcrypt/src/headers/tomcrypt_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ typedef struct {
*/
int (*init)(void **a);

/** initialize a bignum
@param size_bits The size of the number we compute on
@param a The number to initialize
@return CRYPT_OK on success
*/
int (*init_size)(int size_bits, void **a);

/** init copy
@param dst The number to initialize and write to
@param src The number to copy from
Expand Down Expand Up @@ -503,6 +510,7 @@ typedef struct {
extern ltc_math_descriptor ltc_mp;

int ltc_init_multi(void **a, ...) LTC_NULL_TERMINATED;
int ltc_init_multi_size(int size_bits, void **a, ...) LTC_NULL_TERMINATED;
void ltc_deinit_multi(void *a, ...) LTC_NULL_TERMINATED;
void ltc_cleanup_multi(void **a, ...) LTC_NULL_TERMINATED;

Expand Down
2 changes: 2 additions & 0 deletions core/lib/libtomcrypt/src/headers/tomcrypt_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const
/* some handy macros */
#define mp_init(a) ltc_mp.init(a)
#define mp_init_multi ltc_init_multi
#define mp_init_size(a, b) ltc_mp.init_size(a, b)
#define mp_init_multi_size ltc_init_multi_size
#define mp_clear(a) ltc_mp.deinit(a)
#define mp_clear_multi ltc_deinit_multi
#define mp_cleanup_multi ltc_cleanup_multi
Expand Down
28 changes: 28 additions & 0 deletions core/lib/libtomcrypt/src/math/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ int ltc_init_multi(void **a, ...)
return CRYPT_OK;
}

int ltc_init_multi_size(int size_bits, void **a, ...)
{
void **cur = a;
int np = 0;
va_list args;

va_start(args, a);
while (cur != NULL) {
if (mp_init_size(size_bits, cur) != CRYPT_OK) {
/* failed */
va_list clean_list;

va_start(clean_list, a);
cur = a;
while (np--) {
mp_clear(*cur);
cur = va_arg(clean_list, void**);
}
va_end(clean_list);
return CRYPT_MEM;
}
++np;
cur = va_arg(args, void**);
}
va_end(args);
return CRYPT_OK;
}

void ltc_deinit_multi(void *a, ...)
{
void *cur = a;
Expand Down
3 changes: 2 additions & 1 deletion core/lib/libtomcrypt/src/pk/ecc/ltc_ecc_points.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ ecc_point *ltc_ecc_new_point(void)
if (p == NULL) {
return NULL;
}
if (mp_init_multi(&p->x, &p->y, &p->z, LTC_NULL) != CRYPT_OK) {
if (mp_init_multi_size(LTC_MAX_ECC * 2,
&p->x, &p->y, &p->z, LTC_NULL) != CRYPT_OK) {
XFREE(p);
return NULL;
}
Expand Down

0 comments on commit a4281f4

Please sign in to comment.