From f40c79272f7892d4b16108593aeb1c6f80616987 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 16 Jul 2020 21:02:02 +0300 Subject: [PATCH 01/15] tinycrypt: use fast integer types Signed-off-by: Kevin Bracey --- include/tinycrypt/ecc.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/tinycrypt/ecc.h b/include/tinycrypt/ecc.h index f2a5ad67d..cdb5762a2 100644 --- a/include/tinycrypt/ecc.h +++ b/include/tinycrypt/ecc.h @@ -98,10 +98,10 @@ extern "C" { #endif /* defining data types to store word and bit counts: */ -typedef int8_t wordcount_t; -typedef int16_t bitcount_t; +typedef int_fast8_t wordcount_t; +typedef int_fast16_t bitcount_t; /* defining data type for comparison result: */ -typedef int8_t cmpresult_t; +typedef int_fast8_t cmpresult_t; /* defining data type to store ECC coordinate/point in 32bits words: */ typedef unsigned int uECC_word_t; /* defining data type to store an ECC coordinate/point in 64bits words: */ From 1959c18c69a1ebe2f841457f8747edfa9ca6463a Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Thu, 16 Jul 2020 21:03:19 +0300 Subject: [PATCH 02/15] tinycrypt: Add ARM assembler optimisations For ARM Compiler 5 only, provide assembler versions of vli_add, vli_sub, vli_rshift1 and muladd. Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 210 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index aeb6447f3..01db4e6ec 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -69,6 +69,16 @@ #include #include "mbedtls/platform_util.h" +#ifdef __CC_ARM +#pragma diag_suppress 667 // strict diagnostic: "asm" function is nonstandard +#endif + +#if defined MBEDTLS_HAVE_ASM +#ifndef asm +#define asm __asm +#endif +#endif + /* Parameters for curve NIST P-256 aka secp256r1 */ const uECC_word_t curve_p[NUM_ECC_WORDS] = { BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), @@ -322,6 +332,62 @@ uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond) /* Computes result = left - right, returning borrow, in constant time. * Can modify in place. */ +#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +__asm uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, + const uECC_word_t *right) +{ +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +#if defined __thumb__ && __TARGET_ARCH_THUMB < 4 + PUSH {r4-r6,lr} + FRAME PUSH {r4-r6,lr} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + SUBS r3,r5 + SBCS r4,r6 + STMIA r0!,{r3,r4} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + SBCS r3,r5 + SBCS r4,r6 + STMIA r0!,{r3,r4} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + SBCS r3,r5 + SBCS r4,r6 + STMIA r0!,{r3,r4} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + SBCS r3,r5 + SBCS r4,r6 + STMIA r0!,{r3,r4} + SBCS r0,r0 // r0 := r0 - r0 - borrow = -borrow + RSBS r0,r0,#0 // r0 := borrow + POP {r4-r6,pc} +#else + PUSH {r4-r8,lr} + FRAME PUSH {r4-r8,lr} + LDMIA r1!,{r3-r6} + LDMIA r2!,{r7,r8,r12,lr} + SUBS r3,r7 + SBCS r4,r8 + SBCS r5,r12 + SBCS r6,lr + STMIA r0!,{r3-r6} + LDMIA r1!,{r3-r6} + LDMIA r2!,{r7,r8,r12,lr} + SBCS r3,r7 + SBCS r4,r8 + SBCS r5,r12 + SBCS r6,lr + STMIA r0!,{r3-r6} + SBCS r0,r0 // r0 := r0 - r0 - borrow = -borrow + RSBS r0,r0,#0 // r0 := borrow + POP {r4-r8,pc} +#endif +} +#else uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { @@ -336,9 +402,66 @@ uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, } return borrow; } +#endif /* Computes result = left + right, returning carry, in constant time. * Can modify in place. */ +#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +static __asm uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, + const uECC_word_t *right) +{ +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +#if defined __thumb__ && __TARGET_ARCH_THUMB < 4 + PUSH {r4-r6,lr} + FRAME PUSH {r4-r6,lr} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + ADDS r3,r5 + ADCS r4,r6 + STMIA r0!,{r3,r4} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + ADCS r3,r5 + ADCS r4,r6 + STMIA r0!,{r3,r4} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + ADCS r3,r5 + ADCS r4,r6 + STMIA r0!,{r3,r4} + LDMIA r1!,{r3,r4} + LDMIA r2!,{r5,r6} + ADCS r3,r5 + ADCS r4,r6 + STMIA r0!,{r3,r4} + MOVS r0,#0 // does not affect C flag + ADCS r0,r0 // r0 := 0 + 0 + C = carry + POP {r4-r6,pc} +#else + PUSH {r4-r8,lr} + FRAME PUSH {r4-r8,lr} + LDMIA r1!,{r3-r6} + LDMIA r2!,{r7,r8,r12,lr} + ADDS r3,r7 + ADCS r4,r8 + ADCS r5,r12 + ADCS r6,lr + STMIA r0!,{r3-r6} + LDMIA r1!,{r3-r6} + LDMIA r2!,{r7,r8,r12,lr} + ADCS r3,r7 + ADCS r4,r8 + ADCS r5,r12 + ADCS r6,lr + STMIA r0!,{r3-r6} + MOVS r0,#0 // does not affect C flag + ADCS r0,r0 // r0 := 0 + 0 + C = carry + POP {r4-r8,pc} +#endif +} +#else static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { @@ -352,6 +475,7 @@ static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, } return carry; } +#endif cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right) { @@ -362,6 +486,46 @@ cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right) } /* Computes vli = vli >> 1. */ +#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +static __asm void uECC_vli_rshift1(uECC_word_t *vli) +{ +#if defined __thumb__ && __TARGET_ARCH_THUMB < 4 +// RRX instruction is not available, so although we +// can use C flag, it's not that effective. Does at +// least save one working register, meaning we don't need stack + MOVS r3,#0 // initial carry = 0 + MOVS r2,#__cpp(4 * (NUM_ECC_WORDS - 1)) +01 LDR r1,[r0,r2] + LSRS r1,r1,#1 // r2 = word >> 1 + ORRS r1,r3 // merge in the previous carry + STR r1,[r0,r2] + ADCS r3,r3 // put C into bottom bit of r3 + LSLS r3,r3,#31 // shift it up to the top ready for next word + SUBS r2,r2,#4 + BPL %B01 + BX lr +#else +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +// Smooth multiword operation, lots of 32-bit instructions + ADDS r0,#32 + LDMDB r0,{r1-r3,ip} + LSRS ip,ip,#1 + RRXS r3,r3 + RRXS r2,r2 + RRXS r1,r1 + STMDB r0!,{r1-r3,ip} + LDMDB r0,{r1-r3,ip} + RRXS ip,ip + RRXS r3,r3 + RRXS r2,r2 + RRX r1,r1 + STMDB r0!,{r1-r3,ip} + BX lr +#endif +} +#else static void uECC_vli_rshift1(uECC_word_t *vli) { uECC_word_t *end = vli; @@ -374,6 +538,7 @@ static void uECC_vli_rshift1(uECC_word_t *vli) carry = temp << (uECC_WORD_BITS - 1); } } +#endif /* Compute a * b + r, where r is a double-word with high-order word r1 and * low-order word r0, and store the result in the same double-word (r1, r0), @@ -385,6 +550,50 @@ static void uECC_vli_rshift1(uECC_word_t *vli) * [out] r0, r1: low and high-order words of the result * [out] r2: carry */ +#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, + uECC_word_t *r1, uECC_word_t *r2) +{ +#if defined __thumb__ && __TARGET_ARCH_THUMB < 4 + IMPORT __ARM_common_ll_muluu + PRESERVE8 + // have to save r4 to keep stack 8-byte aligned, but then + // may as well make use of it - helps a bit having it + PUSH {r2,r3,r4,lr} + FRAME SAVE {r4,lr},-8 + FRAME ADDRESS sp,16 + BL __ARM_common_ll_muluu + POP {r2,r3} + FRAME ADDRESS sp,8 + LDR r4,[r2] + ADDS r4,r0 + STR r4,[r2] + LDR r4,[r3] + ADCS r4,r1 + STR r4,[r3] + LDR r3,[sp,#8] + LDR r4,[r3] + MOVS r0,#0 + ADCS r4,r0 + STR r4,[r3] + POP {r4,pc} +#else + UMULL r0,r1,r0,r1 + LDR ip,[r2] + ADDS r0,r0,ip + STR r0,[r2] + LDR r0,[r3] + ADCS r0,r1 + STR r0,[r3] + LDR r3,[sp,#0] + LDR r0,[r3] + ADC r0,r0,#0 + STR r0,[r3] + BX lr +#endif + +} +#else static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, uECC_word_t *r1, uECC_word_t *r2) { @@ -397,6 +606,7 @@ static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, *r0 = (uECC_word_t)r01; } +#endif /* State for implementing random delays in uECC_vli_mult_rnd(). * From 84f31d3eaa194970bb12ea22c482aa3e25e352b0 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Tue, 29 Sep 2020 17:51:04 +0300 Subject: [PATCH 03/15] Optimise muladd more Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 173 ++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 01db4e6ec..26577848d 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -540,71 +540,75 @@ static void uECC_vli_rshift1(uECC_word_t *vli) } #endif -/* Compute a * b + r, where r is a double-word with high-order word r1 and - * low-order word r0, and store the result in the same double-word (r1, r0), - * with the carry bit stored in r2. +/* Compute a * b + r, where r is a triple-word with high-order word r[2] and + * low-order word r[0], and store the result in the same triple-word. * - * (r2, r1, r0) = a * b + (r1, r0): + * r[2..0] = a * b + r[2..0]: * [in] a, b: operands to be multiplied - * [in] r0, r1: low and high-order words of operand to add - * [out] r0, r1: low and high-order words of the result - * [out] r2: carry + * [in] r: 3 words of operand to add + * [out] r: 3 words of result */ #if defined MBEDTLS_HAVE_ASM && defined __CC_ARM -static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, - uECC_word_t *r1, uECC_word_t *r2) +static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) { #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 - IMPORT __ARM_common_ll_muluu - PRESERVE8 - // have to save r4 to keep stack 8-byte aligned, but then - // may as well make use of it - helps a bit having it - PUSH {r2,r3,r4,lr} - FRAME SAVE {r4,lr},-8 - FRAME ADDRESS sp,16 - BL __ARM_common_ll_muluu - POP {r2,r3} - FRAME ADDRESS sp,8 - LDR r4,[r2] - ADDS r4,r0 - STR r4,[r2] - LDR r4,[r3] + PUSH {r4-r5} + FRAME PUSH {r4-r5} + // __ARM_common_mul_uu replacement - inline, faster, don't touch R2 + // Separate operands into halfwords + UXTH r3,r0 // r3 := a.lo + LSRS r4,r0,#16 // r4 := a.hi + UXTH r5,r1 // r5 := b.lo + LSRS r1,r1,#16 // r1 := b.hi + // Multiply halfword pairs + MOVS r0,r3 + MULS r0,r5,r0 // r0 := a.lo * b.lo + MULS r3,r1,r3 // r3 := a.lo * b.hi + MULS r5,r4,r5 // r5 := a.hi * b.lo + MULS r1,r4,r1 // r1 := a.hi * b.hi + // Split, shift and add a.lo * b.hi + LSRS r4,r3,#16 // r4 := (a.lo * b.hi).hi + LSLS r3,r3,#16 // r3 := (a.lo * b.hi).lo + ADDS r0,r0,r3 // r0 := a.lo * b.lo + (a.lo * b.hi).lo + ADCS r1,r4 // r1 := a.hi * b.hi + (a.lo * b.hi).hi + carry + // Split, shift and add a.hi * b.lo + LSRS r4,r5,#16 // r4 := (a.hi * b.lo).hi + LSLS r5,r5,#16 // r5 := (a.hi * b.lo).lo + ADDS r0,r0,r5 // r0 := a.lo * b.lo + (a.lo * b.hi).lo + (a.hi * b.lo).lo + ADCS r1,r4 // r1 := a.hi * b.hi + (a.lo * b.hi).hi + (a.hi * b.lo).hi + carries + // Finally add r[] + LDMIA r2!,{r3,r4,r5} + ADDS r3,r3,r0 ADCS r4,r1 - STR r4,[r3] - LDR r3,[sp,#8] - LDR r4,[r3] MOVS r0,#0 - ADCS r4,r0 - STR r4,[r3] - POP {r4,pc} + ADCS r5,r0 + SUBS r2,#12 + STMIA r2!,{r3,r4,r5} + POP {r4-r5} + FRAME POP {r4-r5} + BX lr #else - UMULL r0,r1,r0,r1 - LDR ip,[r2] - ADDS r0,r0,ip - STR r0,[r2] - LDR r0,[r3] - ADCS r0,r1 - STR r0,[r3] - LDR r3,[sp,#0] - LDR r0,[r3] - ADC r0,r0,#0 - STR r0,[r3] + UMULL r3,ip,r0,r1 // pre-ARMv6 requires Rd[Lo|Hi] != Rn + LDMIA r2,{r0,r1} + ADDS r0,r0,r3 + LDR r3,[r2,#8] + ADCS r1,r1,ip + ADC r3,r3,#0 + STMIA r2!,{r0,r1,r3} BX lr #endif } #else -static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, - uECC_word_t *r1, uECC_word_t *r2) +static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) { uECC_dword_t p = (uECC_dword_t)a * b; - uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; + uECC_dword_t r01 = ((uECC_dword_t)(r[1]) << uECC_WORD_BITS) | r[0]; r01 += p; - *r2 += (r01 < p); - *r1 = r01 >> uECC_WORD_BITS; - *r0 = (uECC_word_t)r01; - + r[2] += (r01 < p); + r[1] = r01 >> uECC_WORD_BITS; + r[0] = (uECC_word_t)r01; } #endif @@ -663,16 +667,14 @@ static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, ecc_wait_state_t *s) { - uECC_word_t r0 = 0; - uECC_word_t r1 = 0; - uECC_word_t r2 = 0; + uECC_word_t r[3] = { 0, 0, 0 }; wordcount_t i, k; const uint8_t num_words = NUM_ECC_WORDS; /* Fetch 8 bit worth of delay from the state; 0 if we have no state */ uint8_t delays = s ? s->delays[s->i++] : 0; - uECC_word_t rr0 = 0, rr1 = 0; - volatile uECC_word_t r; + uECC_word_t rr[3] = { 0, 0, 0 }; + volatile uECC_word_t rdummy; /* Mimic start of next loop: k in [0, 3] */ k = 0 + (delays & 0x03); @@ -680,24 +682,23 @@ static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, /* k = 0 -> i in [1, 0] -> 0 extra muladd; * k = 3 -> i in [1, 3] -> 3 extra muladd */ for (i = 1; i <= k; ++i) { - muladd(left[i], right[k - i], &rr0, &rr1, &r2); + muladd(left[i], right[k - i], rr); } - r = rr0; - rr0 = rr1; - rr1 = r2; - r2 = 0; + rdummy = rr[0]; + rr[0] = rr[1]; + rr[1] = rr[2]; + rr[2] = 0; /* Compute each digit of result in sequence, maintaining the carries. */ for (k = 0; k < num_words; ++k) { - for (i = 0; i <= k; ++i) { - muladd(left[i], right[k - i], &r0, &r1, &r2); + muladd(left[i], right[k - i], r); } - result[k] = r0; - r0 = r1; - r1 = r2; - r2 = 0; + result[k] = r[0]; + r[0] = r[1]; + r[1] = r[2]; + r[2] = 0; } /* Mimic end of previous loop: k in [4, 7] */ @@ -706,12 +707,12 @@ static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, /* k = 4 -> i in [5, 4] -> 0 extra muladd; * k = 7 -> i in [5, 7] -> 3 extra muladd */ for (i = 5; i <= k; ++i) { - muladd(left[i], right[k - i], &rr0, &rr1, &r2); + muladd(left[i], right[k - i], rr); } - r = rr0; - rr0 = rr1; - rr1 = r2; - r2 = 0; + rdummy = rr[0]; + rr[0] = rr[1]; + rr[1] = rr[2]; + rr[2] = 0; /* Mimic start of next loop: k in [8, 11] */ k = 11 - (delays & 0x03); @@ -719,25 +720,25 @@ static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, /* k = 8 -> i in [5, 7] -> 3 extra muladd; * k = 11 -> i in [8, 7] -> 0 extra muladd */ for (i = (k + 5) - num_words; i < num_words; ++i) { - muladd(left[i], right[k - i], &rr0, &rr1, &r2); + muladd(left[i], right[k - i], rr); } - r = rr0; - rr0 = rr1; - rr1 = r2; - r2 = 0; + rdummy = rr[0]; + rr[0] = rr[1]; + rr[1] = rr[2]; + rr[2] = 0; for (k = num_words; k < num_words * 2 - 1; ++k) { for (i = (k + 1) - num_words; i < num_words; ++i) { - muladd(left[i], right[k - i], &r0, &r1, &r2); + muladd(left[i], right[k - i], r); } - result[k] = r0; - r0 = r1; - r1 = r2; - r2 = 0; + result[k] = r[0]; + r[0] = r[1]; + r[1] = r[2]; + r[2] = 0; } - result[num_words * 2 - 1] = r0; + result[num_words * 2 - 1] = r[0]; /* Mimic end of previous loop: k in [12, 15] */ k = 15 - (delays & 0x03); @@ -745,15 +746,15 @@ static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, /* k = 12 -> i in [5, 7] -> 3 extra muladd; * k = 15 -> i in [8, 7] -> 0 extra muladd */ for (i = (k + 1) - num_words; i < num_words; ++i) { - muladd(left[i], right[k - i], &rr0, &rr1, &r2); + muladd(left[i], right[k - i], rr); } - r = rr0; - rr0 = rr1; - rr1 = r2; - r2 = 0; + rdummy = rr[0]; + rr[0] = rr[1]; + rr[1] = rr[2]; + rr[2] = 0; - /* avoid warning that r is set but not used */ - (void) r; + /* avoid warning that rdummy is set but not used */ + (void) rdummy; } void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left, From e0f88d514028ef6ac4030a2ac512e38a72c5d772 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Wed, 30 Sep 2020 12:52:15 +0300 Subject: [PATCH 04/15] Add GCC/Clang muladd assembly Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 26577848d..422d01255 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -597,7 +597,63 @@ static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) STMIA r2!,{r0,r1,r3} BX lr #endif - +} +#elif defined MBEDTLS_HAVE_ASM && (defined __GNUC__ || defined __clang__) && defined __arm__ +static __attribute__((__naked__)) +void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) +{ + asm( +#if defined __thumb__ && !defined(__thumb2__) + ".syntax unified \n\t" + "PUSH {r4-r5} \n\t" + ".cfi_remember_state \n\t" + ".cfi_adjust_cfa_offset 8 \n\t" + ".cfi_rel_offset r4,0 \n\t" + ".cfi_rel_offset r5,4 \n\t" + // __ARM_common_mul_uu replacement - inline, faster, don't touch R2 + // Separate operands into halfwords + "UXTH r3,r0 \n\t" // r3 := a.lo + "LSRS r4,r0,#16 \n\t" // r4 := a.hi + "UXTH r5,r1 \n\t" // r5 := b.lo + "LSRS r1,r1,#16 \n\t" // r1 := b.hi + // Multiply halfword pairs + "MOVS r0,r3 \n\t" + "MULS r0,r5,r0 \n\t" // r0 := a.lo * b.lo + "MULS r3,r1,r3 \n\t" // r3 := a.lo * b.hi + "MULS r5,r4,r5 \n\t" // r5 := a.hi * b.lo + "MULS r1,r4,r1 \n\t" // r1 := a.hi * b.hi + // Split, shift and add a.lo * b.hi + "LSRS r4,r3,#16 \n\t" // r4 := (a.lo * b.hi).hi + "LSLS r3,r3,#16 \n\t" // r3 := (a.lo * b.hi).lo + "ADDS r0,r0,r3 \n\t" // r0 := a.lo * b.lo + (a.lo * b.hi).lo + "ADCS r1,r4 \n\t" // r1 := a.hi * b.hi + (a.lo * b.hi).hi + carry + // Split, shift and add a.hi * b.lo + "LSRS r4,r5,#16 \n\t" // r4 := (a.hi * b.lo).hi + "LSLS r5,r5,#16 \n\t" // r5 := (a.hi * b.lo).lo + "ADDS r0,r0,r5 \n\t" // r0 := a.lo * b.lo + (a.lo * b.hi).lo + (a.hi * b.lo).lo + "ADCS r1,r4 \n\t" // r1 := a.hi * b.hi + (a.lo * b.hi).hi + (a.hi * b.lo).hi + carries + // Finally add r[] + "LDMIA r2!,{r3,r4,r5} \n\t" + "ADDS r3,r3,r0 \n\t" + "ADCS r4,r1 \n\t" + "MOVS r0,#0 \n\t" + "ADCS r5,r0 \n\t" + "SUBS r2,#12 \n\t" + "STMIA r2!,{r3,r4,r5} \n\t" + "POP {r4-r5} \n\t" + ".cfi_restore_state \n\t" + "BX lr \n\t" +#else + "UMULL r3,ip,r0,r1 \n\t" // pre-ARMv6 requires Rd[Lo|Hi] != Rn + "LDMIA r2,{r0,r1} \n\t" + "ADDS r0,r0,r3 \n\t" + "LDR r3,[r2,#8] \n\t" + "ADCS r1,r1,ip \n\t" + "ADC r3,r3,#0 \n\t" + "STMIA r2!,{r0,r1,r3} \n\t" + "BX lr \n\t" +#endif + ); } #else static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) From 0a1884bba4d4f3bdfbff1cd4a6d304a522387cc2 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Fri, 2 Oct 2020 14:44:40 +0300 Subject: [PATCH 05/15] Use GCC extended assembler Naked functions and basic assembler don't seem to work reliably - basic assembler is assumed to not change registers or memory, and there does not appear to be any special handling of naked functions in this regard. Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 422d01255..b9986e493 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -598,18 +598,15 @@ static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) BX lr #endif } -#elif defined MBEDTLS_HAVE_ASM && (defined __GNUC__ || defined __clang__) && defined __arm__ -static __attribute__((__naked__)) -void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) +#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) { - asm( + register uECC_word_t r0 asm ("r0") = a; + register uECC_word_t r1 asm ("r1") = b; + register uECC_word_t *r2 asm ("r2") = r; + asm volatile ( #if defined __thumb__ && !defined(__thumb2__) ".syntax unified \n\t" - "PUSH {r4-r5} \n\t" - ".cfi_remember_state \n\t" - ".cfi_adjust_cfa_offset 8 \n\t" - ".cfi_rel_offset r4,0 \n\t" - ".cfi_rel_offset r5,4 \n\t" // __ARM_common_mul_uu replacement - inline, faster, don't touch R2 // Separate operands into halfwords "UXTH r3,r0 \n\t" // r3 := a.lo @@ -640,9 +637,9 @@ void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) "ADCS r5,r0 \n\t" "SUBS r2,#12 \n\t" "STMIA r2!,{r3,r4,r5} \n\t" - "POP {r4-r5} \n\t" - ".cfi_restore_state \n\t" - "BX lr \n\t" + : "+r" (r0), "+r" (r1), "+r" (r2) + : + : "r3", "r4", "r5", "ip", "cc", "memory" #else "UMULL r3,ip,r0,r1 \n\t" // pre-ARMv6 requires Rd[Lo|Hi] != Rn "LDMIA r2,{r0,r1} \n\t" @@ -651,7 +648,9 @@ void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) "ADCS r1,r1,ip \n\t" "ADC r3,r3,#0 \n\t" "STMIA r2!,{r0,r1,r3} \n\t" - "BX lr \n\t" + : "+r" (r0), "+r" (r1), "+r" (r2) + : + : "r3", "ip", "cc", "memory" #endif ); } From 06060330d80043854e7760220130f75ae742d235 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Fri, 2 Oct 2020 17:43:12 +0300 Subject: [PATCH 06/15] Add GCC vli_rshift1 assembly Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index b9986e493..40256dc31 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -525,6 +525,32 @@ static __asm void uECC_vli_rshift1(uECC_word_t *vli) BX lr #endif } +#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ && defined __thumb2__ +static void uECC_vli_rshift1(uECC_word_t *vli) +{ + register uECC_word_t *r0 asm ("r0") = vli; +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif + asm volatile ( + "ADDS r0,#32 \n\t" + "LDMDB r0,{r1-r3,ip} \n\t" + "LSRS ip,ip,#1 \n\t" + "RRXS r3,r3 \n\t" + "RRXS r2,r2 \n\t" + "RRXS r1,r1 \n\t" + "STMDB r0!,{r1-r3,ip} \n\t" + "LDMDB r0,{r1-r3,ip} \n\t" + "RRXS ip,ip \n\t" + "RRXS r3,r3 \n\t" + "RRXS r2,r2 \n\t" + "RRX r1,r1 \n\t" + "STMDB r0!,{r1-r3,ip} \n\t" + : "+r" (r0) + : + : "r1", "r2", "r3", "ip", "cc", "memory" + ); +} #else static void uECC_vli_rshift1(uECC_word_t *vli) { From 4aea62569be7060bd3be42f31cc47b2f13ab9a42 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Fri, 9 Oct 2020 12:54:28 +0300 Subject: [PATCH 07/15] More GCC assembly Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 40256dc31..edb6e051d 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -387,6 +387,68 @@ __asm uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, POP {r4-r8,pc} #endif } +#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, + const uECC_word_t *right) +{ +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif + register uECC_word_t *r0 asm ("r0") = result; + register const uECC_word_t *r1 asm ("r1") = left; + register const uECC_word_t *r2 asm ("r2") = right; + asm volatile ( +#if defined __thumb__ && !defined __thumb2__ + ".syntax unified \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "SUBS r3,r5 \n\t" + "SBCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "SBCS r3,r5 \n\t" + "SBCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "SBCS r3,r5 \n\t" + "SBCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "SBCS r3,r5 \n\t" + "SBCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "SBCS r0,r0 \n\t" // r0 := r0 - r0 - borrow = -borrow + "RSBS r0,r0,#0 \n\t" // r0 := borrow + : "+r" (r0), "+r" (r1), "+r" (r2) + : + : "r3", "r4", "r5", "r6", "cc", "memory" +#else + "LDMIA r1!,{r3-r6} \n\t" + "LDMIA r2!,{r7,r8,r12,lr} \n\t" + "SUBS r3,r7 \n\t" + "SBCS r4,r8 \n\t" + "SBCS r5,r12 \n\t" + "SBCS r6,lr \n\t" + "STMIA r0!,{r3-r6} \n\t" + "LDMIA r1!,{r3-r6} \n\t" + "LDMIA r2!,{r7,r8,r12,lr} \n\t" + "SBCS r3,r7 \n\t" + "SBCS r4,r8 \n\t" + "SBCS r5,r12 \n\t" + "SBCS r6,lr \n\t" + "STMIA r0!,{r3-r6} \n\t" + "SBCS r0,r0 \n\t" // r0 := r0 - r0 - borrow = -borrow + "RSBS r0,r0,#0 \n\t" // r0 := borrow + : "+r" (r0), "+r" (r1), "+r" (r2) + : + : "r3", "r4", "r5", "r6", "r7", "r8", "r12", "lr", "cc", "memory" +#endif + ); + return (uECC_word_t) r0; +} #else uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) @@ -461,6 +523,66 @@ static __asm uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *le POP {r4-r8,pc} #endif } +#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, + const uECC_word_t *right) +{ + register uECC_word_t *r0 asm ("r0") = result; + register const uECC_word_t *r1 asm ("r1") = left; + register const uECC_word_t *r2 asm ("r2") = right; + + asm volatile ( +#if defined __thumb__ && !defined __thumb2__ + ".syntax unified \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "ADDS r3,r5 \n\t" + "ADCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "ADCS r3,r5 \n\t" + "ADCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "ADCS r3,r5 \n\t" + "ADCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "LDMIA r1!,{r3,r4} \n\t" + "LDMIA r2!,{r5,r6} \n\t" + "ADCS r3,r5 \n\t" + "ADCS r4,r6 \n\t" + "STMIA r0!,{r3,r4} \n\t" + "MOVS r0,#0 \n\t" // does not affect C flag + "ADCS r0,r0 \n\t" // r0 := 0 + 0 + C = carry + : "+r" (r0), "+r" (r1), "+r" (r2) + : + : "r3", "r4", "r5", "r6", "cc", "memory" +#else + "LDMIA r1!,{r3-r6} \n\t" + "LDMIA r2!,{r7,r8,r12,lr} \n\t" + "ADDS r3,r7 \n\t" + "ADCS r4,r8 \n\t" + "ADCS r5,r12 \n\t" + "ADCS r6,lr \n\t" + "STMIA r0!,{r3-r6} \n\t" + "LDMIA r1!,{r3-r6} \n\t" + "LDMIA r2!,{r7,r8,r12,lr} \n\t" + "ADCS r3,r7 \n\t" + "ADCS r4,r8 \n\t" + "ADCS r5,r12 \n\t" + "ADCS r6,lr \n\t" + "STMIA r0!,{r3-r6} \n\t" + "MOVS r0,#0 \n\t" // does not affect C flag + "ADCS r0,r0 \n\t" // r0 := 0 + 0 + C = carry + : "+r" (r0), "+r" (r1), "+r" (r2) + : + : "r3", "r4", "r5", "r6", "r7", "r8", "r12", "lr", "cc", "memory" +#endif + ); + return (uECC_word_t) r0; +} #else static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) From 3be252e252ef648a6c763162956f0213c732c884 Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Fri, 9 Oct 2020 12:54:48 +0300 Subject: [PATCH 08/15] vli_isZero and vli_clear assembly Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index edb6e051d..ee9195718 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -213,6 +213,63 @@ int uECC_curve_public_key_size(void) return 2 * NUM_ECC_BYTES; } +#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +__asm void uECC_vli_clear(uECC_word_t *vli) +{ +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +#if !defined __thumb__ || __TARGET_ARCH_THUMB < 4 + MOVS r1,#0 + MOVS r2,#0 + STMIA r0!,{r1,r2} + STMIA r0!,{r1,r2} + STMIA r0!,{r1,r2} + STMIA r0!,{r1,r2} + BX lr +#else + MOVS r1,#0 + STRD r1,r1,[r0,#0] // Only Thumb2 STRD can store same reg twice, not ARM + STRD r1,r1,[r0,#8] + STRD r1,r1,[r0,#16] + STRD r1,r1,[r0,#24] + BX lr +#endif +} +#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +void uECC_vli_clear(uECC_word_t *vli) +{ +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +#if !defined __thumb__ || !defined __thumb2__ + register uECC_word_t *r0 asm("r0") = vli; + register uECC_word_t r1 asm("r1") = 0; + register uECC_word_t r2 asm("r2") = 0; + asm volatile ( + ".syntax unified \n\t" + "STMIA r0!,{r1,r2} \n\t" + "STMIA r0!,{r1,r2} \n\t" + "STMIA r0!,{r1,r2} \n\t" + "STMIA r0!,{r1,r2} \n\t" + : "+r" (r0) + : "r" (r1), "r" (r2) + : "memory" +#else + register uECC_word_t *r0 asm("r0") = vli; + register uECC_word_t r1 asm("r1") = 0; + asm volatile ( + "STRD r1,r1,[r0,#0] \n\t" // Only Thumb2 STRD can store same reg twice, not ARM + "STRD r1,r1,[r0,#8] \n\t" + "STRD r1,r1,[r0,#16] \n\t" + "STRD r1,r1,[r0,#24] \n\t" + : + : "r" (r0), "r" (r1) + : "memory" +#endif + ); +} +#else void uECC_vli_clear(uECC_word_t *vli) { wordcount_t i; @@ -220,7 +277,111 @@ void uECC_vli_clear(uECC_word_t *vli) vli[i] = 0; } } +#endif +#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +__asm uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) +{ +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +#if defined __thumb__ && __TARGET_ARCH_THUMB < 4 + LDMIA r0!,{r1,r2,r3} + ORRS r1,r2 + ORRS r1,r3 + LDMIA r0!,{r2,r3} + ORRS r1,r2 + ORRS r1,r3 + LDMIA r0,{r0,r2,r3} + ORRS r1,r0 + ORRS r1,r2 + ORRS r1,r3 + RSBS r1,r1,#0 // C set if zero + MOVS r0,#0 + ADCS r0,r0 + BX lr +#else + LDMIA r0!,{r1,r2,r3,ip} + ORRS r1,r2 + ORRS r1,r3 + ORRS r1,ip + LDMIA r0,{r0,r2,r3,ip} + ORRS r1,r0 + ORRS r1,r2 + ORRS r1,r3 + ORRS r1,ip +#ifdef __ARM_FEATURE_CLZ + CLZ r0,r1 // 32 if zero + LSRS r0,r0,#5 +#else + RSBS r1,r1,#0 // C set if zero + MOVS r0,#0 + ADCS r0,r0 +#endif + BX lr +#endif +} +#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) +{ + uECC_word_t ret; +#if NUM_ECC_WORDS != 8 +#error adjust ARM assembly to handle NUM_ECC_WORDS != 8 +#endif +#if defined __thumb__ && !defined __thumb2__ + register uECC_word_t r1 asm ("r1"); + register uECC_word_t r2 asm ("r2"); + register uECC_word_t r3 asm ("r3"); + asm volatile ( + ".syntax unified \n\t" + "LDMIA %[vli]!,{%[r1],%[r2],%[r3]} \n\t" + "ORRS %[r1],%[r2] \n\t" + "ORRS %[r1],%[r3] \n\t" + "LDMIA %[vli]!,{%[r2],%[r3]} \n\t" + "ORRS %[r1],%[r2] \n\t" + "ORRS %[r1],%[r3] \n\t" + "LDMIA %[vli],{%[vli],%[r2],%[r3]} \n\t" + "ORRS %[r1],%[vli] \n\t" + "ORRS %[r1],%[r2] \n\t" + "ORRS %[r1],%[r3] \n\t" + "RSBS %[r1],%[r1],#0 \n\t" // C set if zero + "MOVS %[ret],#0 \n\t" + "ADCS %[ret],r0 \n\t" + : [ret]"=r" (ret), [r1]"=r" (r1), [r2]"=r" (r2), [r3]"=r" (r3) + : [vli]"[ret]" (vli) + : "cc", "memory" + ); +#else + register uECC_word_t r1 asm ("r1"); + register uECC_word_t r2 asm ("r2"); + register uECC_word_t r3 asm ("r3"); + register uECC_word_t ip asm ("ip"); + asm volatile ( + "LDMIA %[vli]!,{%[r1],%[r2],%[r3],%[ip]}\n\t" + "ORRS %[r1],%[r2] \n\t" + "ORRS %[r1],%[r3] \n\t" + "ORRS %[r1],%[ip] \n\t" + "LDMIA %[vli],{%[vli],%[r2],%[r3],%[ip]}\n\t" + "ORRS %[r1],%[vli] \n\t" + "ORRS %[r1],%[r2] \n\t" + "ORRS %[r1],%[r3] \n\t" + "ORRS %[r1],%[ip] \n\t" +#if __ARM_ARCH >= 5 + "CLZ %[ret],%[r1] \n\t" // r0 = 32 if zero + "LSRS %[ret],%[ret],#5 \n\t" +#else + "RSBS %[r1],%[r1],#0 \n\t" // C set if zero + "MOVS %[ret],#0 \n\t" + "ADCS %[ret],r0 \n\t" +#endif + : [ret]"=r" (ret), [r1]"=r" (r1), [r2]"=r" (r2), [r3]"=r" (r3), [ip]"=r" (ip) + : [vli]"[ret]" (vli) + : "cc", "memory" + ); +#endif + return ret; +} +#else uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) { uECC_word_t bits = 0; @@ -230,6 +391,7 @@ uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) } return (bits == 0); } +#endif uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit) { From 99c64e142eadb8823190f84a6547da8e9c3504ae Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Tue, 6 Oct 2020 12:25:28 +0300 Subject: [PATCH 09/15] Use builtin CLZ Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index ee9195718..3407aaf96 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -424,9 +424,13 @@ bitcount_t uECC_vli_numBits(const uECC_word_t *vli) } digit = vli[num_digits - 1]; +#if defined __GNUC__ || defined __clang__ || defined __CC_ARM + i = uECC_WORD_BITS - __builtin_clz(digit); +#else for (i = 0; digit; ++i) { digit >>= 1; } +#endif return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); } From 045e576e02c27a60fc94760a8213828fa367476b Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Tue, 6 Oct 2020 12:25:58 +0300 Subject: [PATCH 10/15] Adjust fixed-time arithmetic to not use boolean ops Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 3407aaf96..d48c3eeca 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -493,7 +493,7 @@ uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right) uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond) { - return (p_true*(cond)) | (p_false*(!cond)); + return (p_true*(cond)) | (p_false*(cond ^ 1)); } /* Computes result = left - right, returning borrow, in constant time. @@ -768,9 +768,9 @@ static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right) { uECC_word_t tmp[NUM_ECC_WORDS]; - uECC_word_t neg = !!uECC_vli_sub(tmp, left, right); + uECC_word_t neg = uECC_vli_sub(tmp, left, right); uECC_word_t equal = uECC_vli_isZero(tmp); - return (!equal - 2 * neg); + return ((equal ^ 1) - 2 * neg); } /* Computes vli = vli >> 1. */ From 8b2014bdb6285adf9f704d36acd0d20227635a3d Mon Sep 17 00:00:00 2001 From: Kevin Bracey Date: Wed, 7 Oct 2020 16:53:00 +0300 Subject: [PATCH 11/15] Add .syntax divided markers Old GCC (pre-5?) natively use divided syntax, so we must put it back after changing it. More recent GCCs natively use unified syntax, and themselves are surrounding our blocks with divided/unified directives for backwards compatibility, so we don't need to restore for those. Signed-off-by: Kevin Bracey --- tinycrypt/ecc.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index d48c3eeca..cfc527b2f 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -252,6 +252,7 @@ void uECC_vli_clear(uECC_word_t *vli) "STMIA r0!,{r1,r2} \n\t" "STMIA r0!,{r1,r2} \n\t" "STMIA r0!,{r1,r2} \n\t" + ".syntax divided \n\t" : "+r" (r0) : "r" (r1), "r" (r2) : "memory" @@ -347,6 +348,7 @@ uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) "RSBS %[r1],%[r1],#0 \n\t" // C set if zero "MOVS %[ret],#0 \n\t" "ADCS %[ret],r0 \n\t" + ".syntax divided \n\t" : [ret]"=r" (ret), [r1]"=r" (r1), [r2]"=r" (r2), [r3]"=r" (r3) : [vli]"[ret]" (vli) : "cc", "memory" @@ -588,6 +590,7 @@ uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, "STMIA r0!,{r3,r4} \n\t" "SBCS r0,r0 \n\t" // r0 := r0 - r0 - borrow = -borrow "RSBS r0,r0,#0 \n\t" // r0 := borrow + ".syntax divided \n\t" : "+r" (r0), "+r" (r1), "+r" (r2) : : "r3", "r4", "r5", "r6", "cc", "memory" @@ -722,6 +725,7 @@ static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, "STMIA r0!,{r3,r4} \n\t" "MOVS r0,#0 \n\t" // does not affect C flag "ADCS r0,r0 \n\t" // r0 := 0 + 0 + C = carry + ".syntax divided \n\t" : "+r" (r0), "+r" (r1), "+r" (r2) : : "r3", "r4", "r5", "r6", "cc", "memory" @@ -951,6 +955,7 @@ static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) "ADCS r5,r0 \n\t" "SUBS r2,#12 \n\t" "STMIA r2!,{r3,r4,r5} \n\t" + ".syntax divided \n\t" : "+r" (r0), "+r" (r1), "+r" (r2) : : "r3", "r4", "r5", "ip", "cc", "memory" From 7e62c31097723d8cffa056362c06f5af24e9f813 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Wed, 14 Oct 2020 12:02:40 +0200 Subject: [PATCH 12/15] Guard the tinycrypt code with MBEDTLS_USE_TINYCRYPT Signed-off-by: Andrzej Kurek --- tinycrypt/ecc.c | 2 ++ tinycrypt/ecc_dh.c | 2 ++ tinycrypt/ecc_dsa.c | 2 ++ 3 files changed, 6 insertions(+) diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index cfc527b2f..6c944d33f 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -63,6 +63,7 @@ #include MBEDTLS_CONFIG_FILE #endif +#if defined(MBEDTLS_USE_TINYCRYPT) #include #include "mbedtls/platform_util.h" #include "mbedtls/sha256.h" @@ -1853,3 +1854,4 @@ int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key) return ret; } +#endif /* MBEDTLS_USE_TINYCRYPT */ diff --git a/tinycrypt/ecc_dh.c b/tinycrypt/ecc_dh.c index 6285cf357..b6b1898d3 100644 --- a/tinycrypt/ecc_dh.c +++ b/tinycrypt/ecc_dh.c @@ -66,6 +66,7 @@ #include MBEDTLS_CONFIG_FILE #endif +#if defined(MBEDTLS_USE_TINYCRYPT) #include #include #include @@ -195,3 +196,4 @@ int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key, return UECC_FAULT_DETECTED; } +#endif /* MBEDTLS_USE_TINYCRYPT */ diff --git a/tinycrypt/ecc_dsa.c b/tinycrypt/ecc_dsa.c index d432a2e6a..fe3952c2d 100644 --- a/tinycrypt/ecc_dsa.c +++ b/tinycrypt/ecc_dsa.c @@ -64,6 +64,7 @@ #include MBEDTLS_CONFIG_FILE #endif +#if defined(MBEDTLS_USE_TINYCRYPT) #include #include #include "mbedtls/platform_util.h" @@ -314,3 +315,4 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash, return UECC_FAILURE; } +#endif /* MBEDTLS_USE_TINYCRYPT */ From db0e50ea7004f244f572b3bc567d39ea9e720521 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Wed, 14 Oct 2020 12:24:20 +0200 Subject: [PATCH 13/15] Introduce MBEDTLS_OPTIMIZE_TINYCRYPT_ASM Make the ASM optimizations in tinycrypt optional. Signed-off-by: Andrzej Kurek --- configs/baremetal.h | 2 ++ include/mbedtls/check_config.h | 6 ++++++ include/mbedtls/config.h | 15 +++++++++++++++ library/version_features.c | 3 +++ programs/ssl/query_config.c | 8 ++++++++ scripts/config.pl | 2 ++ tinycrypt/ecc.c | 28 ++++++++++++++-------------- 7 files changed, 50 insertions(+), 14 deletions(-) diff --git a/configs/baremetal.h b/configs/baremetal.h index 9fa39189d..628153795 100644 --- a/configs/baremetal.h +++ b/configs/baremetal.h @@ -116,6 +116,8 @@ MBEDTLS_SSL_EXTENDED_MS_ENFORCE_ENABLED #define MBEDTLS_USE_TINYCRYPT +#define MBEDTLS_HAVE_ASM +#define MBEDTLS_OPTIMIZE_TINYCRYPT_ASM /* X.509 CRT parsing */ #define MBEDTLS_X509_USE_C diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h index 5e2a661d3..f08aea503 100644 --- a/include/mbedtls/check_config.h +++ b/include/mbedtls/check_config.h @@ -130,6 +130,12 @@ #error "MBEDTLS_USE_TINYCRYPT defined, but not all prerequesites" #endif +#if defined(MBEDTLS_OPTIMIZE_TINYCRYPT_ASM) && \ + ( !defined(MBEDTLS_HAVE_ASM) || \ + !defined(MBEDTLS_USE_TINYCRYPT) ) +#error "MBEDTLS_OPTIMIZE_TINYCRYPT_ASM defined, but not all prerequesites" +#endif + #if defined(MBEDTLS_NIST_KW_C) && \ ( !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_CIPHER_C) ) #error "MBEDTLS_NIST_KW_C defined, but not all prerequisites" diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 06cdde9f6..3587bbaa4 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -2702,6 +2702,21 @@ */ //#define MBEDTLS_USE_TINYCRYPT +/** + * \def MBEDTLS_OPTIMIZE_TINYCRYPT_ASM + * + * Optimize TinyCrypt operations using assembly. + * Add T32/A32 assembly for core tinycrypt/microecc routines, for ARMC5 and GCC; + * Use fast integer types to avoid frequent narrowing instructions; + * Use __builtin_clz and avoid boolean ops. + * + * Requires: MBEDTLS_USE_TINYCRYPT + * MBEDTLS_HAVE_ASM + * + * Module: tinycrypt/ecc.c + */ +//#define MBEDTLS_OPTIMIZE_TINYCRYPT_ASM + /** * \def MBEDTLS_ENTROPY_C * diff --git a/library/version_features.c b/library/version_features.c index 2ef9d1291..1a226c872 100644 --- a/library/version_features.c +++ b/library/version_features.c @@ -678,6 +678,9 @@ static const char *features[] = { #if defined(MBEDTLS_USE_TINYCRYPT) "MBEDTLS_USE_TINYCRYPT", #endif /* MBEDTLS_USE_TINYCRYPT */ +#if defined(MBEDTLS_OPTIMIZE_TINYCRYPT_ASM) + "MBEDTLS_OPTIMIZE_TINYCRYPT_ASM", +#endif /* MBEDTLS_OPTIMIZE_TINYCRYPT_ASM */ #if defined(MBEDTLS_ENTROPY_C) "MBEDTLS_ENTROPY_C", #endif /* MBEDTLS_ENTROPY_C */ diff --git a/programs/ssl/query_config.c b/programs/ssl/query_config.c index ac0ef2ee4..ba4b59650 100644 --- a/programs/ssl/query_config.c +++ b/programs/ssl/query_config.c @@ -1850,6 +1850,14 @@ int query_config( const char *config ) } #endif /* MBEDTLS_USE_TINYCRYPT */ +#if defined(MBEDTLS_OPTIMIZE_TINYCRYPT_ASM) + if( strcmp( "MBEDTLS_OPTIMIZE_TINYCRYPT_ASM", config ) == 0 ) + { + MACRO_EXPANSION_TO_STR( MBEDTLS_OPTIMIZE_TINYCRYPT_ASM ); + return( 0 ); + } +#endif /* MBEDTLS_OPTIMIZE_TINYCRYPT_ASM */ + #if defined(MBEDTLS_ENTROPY_C) if( strcmp( "MBEDTLS_ENTROPY_C", config ) == 0 ) { diff --git a/scripts/config.pl b/scripts/config.pl index 1c3422edf..0a65ef438 100755 --- a/scripts/config.pl +++ b/scripts/config.pl @@ -59,6 +59,7 @@ # MBEDTLS_CTR_DRBG_USE_128_BIT_KEY # MBEDTLS_SSL_TRANSFORM_OPTIMIZE_CIPHERS # MBEDTLS_VALIDATE_SSL_KEYS_INTEGRITY +# MBEDTLS_OPTIMIZE_TINYCRYPT_ASM # and any symbol beginning _ALT # # The baremetal configuration excludes options that require a library or @@ -144,6 +145,7 @@ MBEDTLS_AES_SCA_COUNTERMEASURES MBEDTLS_CTR_DRBG_USE_128_BIT_KEY MBEDTLS_SSL_TRANSFORM_OPTIMIZE_CIPHERS MBEDTLS_VALIDATE_SSL_KEYS_INTEGRITY +MBEDTLS_OPTIMIZE_TINYCRYPT_ASM _ALT\s*$ ); diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 6c944d33f..79ecc8cec 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -70,15 +70,15 @@ #include #include "mbedtls/platform_util.h" +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM #ifdef __CC_ARM #pragma diag_suppress 667 // strict diagnostic: "asm" function is nonstandard #endif -#if defined MBEDTLS_HAVE_ASM #ifndef asm #define asm __asm #endif -#endif +#endif /* MBEDTLS_OPTIMIZE_TINYCRYPT_ASM */ /* Parameters for curve NIST P-256 aka secp256r1 */ const uECC_word_t curve_p[NUM_ECC_WORDS] = { @@ -214,7 +214,7 @@ int uECC_curve_public_key_size(void) return 2 * NUM_ECC_BYTES; } -#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __CC_ARM __asm void uECC_vli_clear(uECC_word_t *vli) { #if NUM_ECC_WORDS != 8 @@ -237,7 +237,7 @@ __asm void uECC_vli_clear(uECC_word_t *vli) BX lr #endif } -#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +#elif defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __GNUC__ && defined __arm__ void uECC_vli_clear(uECC_word_t *vli) { #if NUM_ECC_WORDS != 8 @@ -281,7 +281,7 @@ void uECC_vli_clear(uECC_word_t *vli) } #endif -#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __CC_ARM __asm uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) { #if NUM_ECC_WORDS != 8 @@ -323,7 +323,7 @@ __asm uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) BX lr #endif } -#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +#elif defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __GNUC__ && defined __arm__ uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) { uECC_word_t ret; @@ -501,7 +501,7 @@ uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond) /* Computes result = left - right, returning borrow, in constant time. * Can modify in place. */ -#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __CC_ARM __asm uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { @@ -556,7 +556,7 @@ __asm uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, POP {r4-r8,pc} #endif } -#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +#elif defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __GNUC__ && defined __arm__ uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { @@ -638,7 +638,7 @@ uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, /* Computes result = left + right, returning carry, in constant time. * Can modify in place. */ -#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __CC_ARM static __asm uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { @@ -693,7 +693,7 @@ static __asm uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *le POP {r4-r8,pc} #endif } -#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +#elif defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __GNUC__ && defined __arm__ static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { @@ -779,7 +779,7 @@ cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right) } /* Computes vli = vli >> 1. */ -#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __CC_ARM static __asm void uECC_vli_rshift1(uECC_word_t *vli) { #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 @@ -818,7 +818,7 @@ static __asm void uECC_vli_rshift1(uECC_word_t *vli) BX lr #endif } -#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ && defined __thumb2__ +#elif defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __GNUC__ && defined __arm__ && defined __thumb2__ static void uECC_vli_rshift1(uECC_word_t *vli) { register uECC_word_t *r0 asm ("r0") = vli; @@ -867,7 +867,7 @@ static void uECC_vli_rshift1(uECC_word_t *vli) * [in] r: 3 words of operand to add * [out] r: 3 words of result */ -#if defined MBEDTLS_HAVE_ASM && defined __CC_ARM +#if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __CC_ARM static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) { #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 @@ -917,7 +917,7 @@ static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) BX lr #endif } -#elif defined MBEDTLS_HAVE_ASM && defined __GNUC__ && defined __arm__ +#elif defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM && defined __GNUC__ && defined __arm__ static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) { register uECC_word_t r0 asm ("r0") = a; From b04208151b9b2edb95afcd1a66f39669ce116d53 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Wed, 14 Oct 2020 19:42:23 +0200 Subject: [PATCH 14/15] tinycrypt: make asm optimizations optional for baremetal Disable asm optimizations for strict armcc baremetal builds. Signed-off-by: Andrzej Kurek --- configs/baremetal.h | 5 +++-- tinycrypt/ecc.c | 4 ---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/configs/baremetal.h b/configs/baremetal.h index 628153795..7168976e5 100644 --- a/configs/baremetal.h +++ b/configs/baremetal.h @@ -117,8 +117,9 @@ #define MBEDTLS_USE_TINYCRYPT #define MBEDTLS_HAVE_ASM -#define MBEDTLS_OPTIMIZE_TINYCRYPT_ASM - +#if !( defined(__STRICT_ANSI__) && defined(__CC_ARM) ) + #define MBEDTLS_OPTIMIZE_TINYCRYPT_ASM +#endif /* X.509 CRT parsing */ #define MBEDTLS_X509_USE_C #define MBEDTLS_X509_CRT_PARSE_C diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 79ecc8cec..1328ea2d1 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -71,10 +71,6 @@ #include "mbedtls/platform_util.h" #if defined MBEDTLS_OPTIMIZE_TINYCRYPT_ASM -#ifdef __CC_ARM -#pragma diag_suppress 667 // strict diagnostic: "asm" function is nonstandard -#endif - #ifndef asm #define asm __asm #endif From d66d13eaa9a4af7a3575dc7985c9a7cbed2d5d4f Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Thu, 15 Oct 2020 09:54:49 +0200 Subject: [PATCH 15/15] Add a no-strict build to armcc baremetal tests in all.sh Signed-off-by: Andrzej Kurek --- tests/scripts/all.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 5439b854b..7ae142502 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1634,15 +1634,21 @@ component_build_arm_none_eabi_gcc_no_64bit_multiplication () { } component_build_armcc () { - msg "build: ARM Compiler 5" + msg "build: ARM Compiler 5 strict" scripts/config.pl baremetal make CC="$ARMC5_CC" AR="$ARMC5_AR" WARNING_CFLAGS='--strict --c99' lib - msg "size: ARM Compiler 5" + msg "size: ARM Compiler 5 strict" "$ARMC5_FROMELF" -z library/*.o make clean + msg "build: ARM Compiler 5 non strict" + make CC="$ARMC5_CC" AR="$ARMC5_AR" WARNING_CFLAGS='--c99' lib + + msg "size: ARM Compiler 5 non strict" + "$ARMC5_FROMELF" -z library/*.o + # ARM Compiler 6 - Target ARMv7-A armc6_build_test "--target=arm-arm-none-eabi -march=armv7-a"