Skip to content

Commit 3f58629

Browse files
authored
Merge pull request #12121 from yosuke-wolfssl/fix/f-thumb-sp
Fix Thumb-1 sp_div_word
2 parents dacb8b5 + e9cbae4 commit 3f58629

2 files changed

Lines changed: 184 additions & 19 deletions

File tree

‎wolfcrypt/src/sp_int.c‎

Lines changed: 130 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,29 +3033,123 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
30333033
sp_int_digit d)
30343034
{
30353035
__asm__ __volatile__ (
3036-
#if defined(__clang__) || defined(WOLFSSL_KEIL)
3037-
"lsrs r3, %[d], #24\n\t"
3036+
/* Shift d so that top bit is set. */
3037+
"movs r3, #0\n\t"
3038+
#ifdef WOLFSSL_KEIL
3039+
"lsrs r6, %[d], #16\n\t"
3040+
"subs r6, r6, #1\n\t"
3041+
"sbcs r6, r6, r6\n\t"
3042+
"movs r5, #16\n\t"
3043+
"ands r5, r5, r6\n\t"
3044+
"lsls %[d], %[d], r5\n\t"
3045+
"adds r3, r3, r5\n\t"
3046+
"lsrs r6, %[d], #24\n\t"
3047+
"subs r6, r6, #1\n\t"
3048+
"sbcs r6, r6, r6\n\t"
3049+
"movs r5, #8\n\t"
3050+
"ands r5, r5, r6\n\t"
3051+
"lsls %[d], %[d], r5\n\t"
3052+
"adds r3, r3, r5\n\t"
3053+
"lsrs r6, %[d], #28\n\t"
3054+
"subs r6, r6, #1\n\t"
3055+
"sbcs r6, r6, r6\n\t"
3056+
"movs r5, #4\n\t"
3057+
"ands r5, r5, r6\n\t"
3058+
"lsls %[d], %[d], r5\n\t"
3059+
"adds r3, r3, r5\n\t"
3060+
"lsrs r6, %[d], #30\n\t"
3061+
"subs r6, r6, #1\n\t"
3062+
"sbcs r6, r6, r6\n\t"
3063+
"movs r5, #2\n\t"
3064+
"ands r5, r5, r6\n\t"
3065+
"lsls %[d], %[d], r5\n\t"
3066+
"adds r3, r3, r5\n\t"
3067+
"lsrs r6, %[d], #31\n\t"
3068+
"subs r6, r6, #1\n\t"
3069+
"sbcs r6, r6, r6\n\t"
3070+
"movs r5, #1\n\t"
3071+
"ands r5, r5, r6\n\t"
3072+
"lsls %[d], %[d], r5\n\t"
3073+
"adds r3, r3, r5\n\t"
3074+
#elif defined(__clang__)
3075+
"lsrs r6, %[d], #16\n\t"
3076+
"subs r6, r6, #1\n\t"
3077+
"sbcs r6, r6\n\t"
3078+
"movs r5, #16\n\t"
3079+
"ands r5, r6\n\t"
3080+
"lsls %[d], %[d], r5\n\t"
3081+
"adds r3, r3, r5\n\t"
3082+
"lsrs r6, %[d], #24\n\t"
3083+
"subs r6, r6, #1\n\t"
3084+
"sbcs r6, r6\n\t"
3085+
"movs r5, #8\n\t"
3086+
"ands r5, r6\n\t"
3087+
"lsls %[d], %[d], r5\n\t"
3088+
"adds r3, r3, r5\n\t"
3089+
"lsrs r6, %[d], #28\n\t"
3090+
"subs r6, r6, #1\n\t"
3091+
"sbcs r6, r6\n\t"
3092+
"movs r5, #4\n\t"
3093+
"ands r5, r6\n\t"
3094+
"lsls %[d], %[d], r5\n\t"
3095+
"adds r3, r3, r5\n\t"
3096+
"lsrs r6, %[d], #30\n\t"
3097+
"subs r6, r6, #1\n\t"
3098+
"sbcs r6, r6\n\t"
3099+
"movs r5, #2\n\t"
3100+
"ands r5, r6\n\t"
3101+
"lsls %[d], %[d], r5\n\t"
3102+
"adds r3, r3, r5\n\t"
3103+
"lsrs r6, %[d], #31\n\t"
3104+
"subs r6, r6, #1\n\t"
3105+
"sbcs r6, r6\n\t"
3106+
"movs r5, #1\n\t"
3107+
"ands r5, r6\n\t"
3108+
"lsls %[d], %[d], r5\n\t"
3109+
"adds r3, r3, r5\n\t"
30383110
#else
3039-
"lsr r3, %[d], #24\n\t"
3111+
"lsr r6, %[d], #16\n\t"
3112+
"sub r6, r6, #1\n\t"
3113+
"sbc r6, r6\n\t"
3114+
"movs r5, #16\n\t"
3115+
"and r5, r6\n\t"
3116+
"lsl %[d], %[d], r5\n\t"
3117+
"add r3, r3, r5\n\t"
3118+
"lsr r6, %[d], #24\n\t"
3119+
"sub r6, r6, #1\n\t"
3120+
"sbc r6, r6\n\t"
3121+
"movs r5, #8\n\t"
3122+
"and r5, r6\n\t"
3123+
"lsl %[d], %[d], r5\n\t"
3124+
"add r3, r3, r5\n\t"
3125+
"lsr r6, %[d], #28\n\t"
3126+
"sub r6, r6, #1\n\t"
3127+
"sbc r6, r6\n\t"
3128+
"movs r5, #4\n\t"
3129+
"and r5, r6\n\t"
3130+
"lsl %[d], %[d], r5\n\t"
3131+
"add r3, r3, r5\n\t"
3132+
"lsr r6, %[d], #30\n\t"
3133+
"sub r6, r6, #1\n\t"
3134+
"sbc r6, r6\n\t"
3135+
"movs r5, #2\n\t"
3136+
"and r5, r6\n\t"
3137+
"lsl %[d], %[d], r5\n\t"
3138+
"add r3, r3, r5\n\t"
3139+
"lsr r6, %[d], #31\n\t"
3140+
"sub r6, r6, #1\n\t"
3141+
"sbc r6, r6\n\t"
3142+
"movs r5, #1\n\t"
3143+
"and r5, r6\n\t"
3144+
"lsl %[d], %[d], r5\n\t"
3145+
"add r3, r3, r5\n\t"
30403146
#endif
3041-
"beq 2%=f\n\t"
3042-
"\n1%=:\n\t"
3043-
"movs r3, #0\n\t"
3044-
"b 3%=f\n\t"
3045-
"\n2%=:\n\t"
3046-
"mov r3, #8\n\t"
3047-
"\n3%=:\n\t"
30483147
"movs r4, #31\n\t"
30493148
#if defined(__clang__) || defined(WOLFSSL_KEIL)
30503149
"subs r4, r4, r3\n\t"
30513150
#else
30523151
"sub r4, r4, r3\n\t"
30533152
#endif
3054-
#if defined(__clang__) || defined(WOLFSSL_KEIL)
3055-
"lsls %[d], %[d], r3\n\t"
3056-
#else
3057-
"lsl %[d], %[d], r3\n\t"
3058-
#endif
30593153
#if defined(__clang__) || defined(WOLFSSL_KEIL)
30603154
"lsls %[hi], %[hi], r3\n\t"
30613155
#else
@@ -3299,7 +3393,6 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
32993393
#else
33003394
"sub %[hi], %[hi], r4\n\t"
33013395
#endif
3302-
"movs r4, %[hi]\n\t"
33033396
"mov %[hi], r9\n\t"
33043397
#ifdef WOLFSSL_KEIL
33053398
"sbcs %[hi], %[hi], r5\n\t"
@@ -3546,11 +3639,29 @@ static WC_INLINE sp_int_digit sp_div_word(sp_int_digit hi, sp_int_digit lo,
35463639
#else
35473640
"add r3, r3, r5\n\t"
35483641
#endif
3549-
"movs r6, %[d]\n\t"
3642+
/* Add 1 to result if bottom half of difference is >= d. */
3643+
"movs r5, %[d]\n\t"
3644+
#if defined(__clang__) || defined(WOLFSSL_KEIL)
3645+
"subs r5, r5, r4\n\t"
3646+
#else
3647+
"sub r5, r5, r4\n\t"
3648+
#endif
3649+
#ifdef WOLFSSL_KEIL
3650+
"sbcs r6, r6, r6\n\t"
3651+
#elif defined(__clang__)
3652+
"sbcs r6, r6\n\t"
3653+
#else
3654+
"sbc r6, r6\n\t"
3655+
#endif
3656+
#if defined(__clang__) || defined(WOLFSSL_KEIL)
3657+
"subs r3, r3, r6\n\t"
3658+
#else
3659+
"sub r3, r3, r6\n\t"
3660+
#endif
35503661
#if defined(__clang__) || defined(WOLFSSL_KEIL)
3551-
"subs r6, r6, r4\n\t"
3662+
"subs r5, r5, #1\n\t"
35523663
#else
3553-
"sub r6, r6, r4\n\t"
3664+
"sub r5, r5, #1\n\t"
35543665
#endif
35553666
#ifdef WOLFSSL_KEIL
35563667
"sbcs r6, r6, r6\n\t"

‎wolfcrypt/test/test.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97605,6 +97605,55 @@ static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
9760597605
}
9760697606
#endif
9760797607

97608+
#if defined(WOLFSSL_SP_MATH_ALL) && (DIGIT_BIT >= 32) && \
97609+
(!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0))
97610+
/* Divide hi:lo (two 32-bit words) by a 32-bit divisor larger than 16 bits. */
97611+
static wc_test_ret_t mp_test_div_word(mp_int* a, mp_int* r)
97612+
{
97613+
static const struct {
97614+
word32 hi;
97615+
word32 lo;
97616+
word32 d;
97617+
word32 q;
97618+
word32 rem;
97619+
} vec[] = {
97620+
/* Divisor with the top bit set. */
97621+
{ 0x4fbdd7d6, 0xba08af90, 0x85c6dbfc, 0x9898a6c6, 0x11f522a8 },
97622+
/* Exact divisions. */
97623+
{ 0x00008000, 0x00000000, 0x80000000, 0x00010000, 0x00000000 },
97624+
{ 0x7fff7fff, 0x00000000, 0x80008000, 0xfffe0000, 0x00000000 },
97625+
/* Divisors in [2^16, 2^23) and [2^24, 2^31). */
97626+
{ 0x00010002, 0x00000000, 0x00010003, 0xffff0002, 0x0000fffa },
97627+
{ 0x01000000, 0x00000000, 0x01000001, 0xffffff00, 0x00000100 },
97628+
{ 0x40000000, 0x00000000, 0x40000001, 0xfffffffc, 0x00000004 },
97629+
};
97630+
wc_test_ret_t ret;
97631+
mp_digit rem;
97632+
int i;
97633+
97634+
for (i = 0; i < (int)(sizeof(vec) / sizeof(vec[0])); i++) {
97635+
ret = mp_set(a, vec[i].hi);
97636+
if (ret == MP_OKAY)
97637+
ret = mp_mul_2d(a, 32, a);
97638+
if (ret == MP_OKAY)
97639+
ret = mp_add_d(a, vec[i].lo, a);
97640+
if (ret == MP_OKAY)
97641+
ret = mp_div_d(a, vec[i].d, r, &rem);
97642+
if (ret != MP_OKAY)
97643+
return WC_TEST_RET_ENC_EC(ret);
97644+
if ((mp_cmp_d(r, vec[i].q) != MP_EQ) || (rem != vec[i].rem))
97645+
return WC_TEST_RET_ENC_I(i);
97646+
ret = mp_mod_d(a, vec[i].d, &rem);
97647+
if (ret != MP_OKAY)
97648+
return WC_TEST_RET_ENC_EC(ret);
97649+
if (rem != vec[i].rem)
97650+
return WC_TEST_RET_ENC_I(i);
97651+
}
97652+
97653+
return 0;
97654+
}
97655+
#endif
97656+
9760897657
static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1,
9760997658
mp_int* r2, WC_RNG* rng)
9761097659
{
@@ -98902,6 +98951,11 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
9890298951
(defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
9890398952
if ((ret = mp_test_mod_d(a, &rng)) != 0)
9890498953
goto done;
98954+
#endif
98955+
#if defined(WOLFSSL_SP_MATH_ALL) && (DIGIT_BIT >= 32) && \
98956+
(!defined(HAVE_FIPS) || FIPS_VERSION3_GE(7,0,0))
98957+
if ((ret = mp_test_div_word(a, r1)) != 0)
98958+
goto done;
9890598959
#endif
9890698960
if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
9890798961
goto done;

0 commit comments

Comments
 (0)