From d0193ba8ebedff613f0a4e18b0fd712fdcf18819 Mon Sep 17 00:00:00 2001 From: Todd A Ouska Date: Mon, 29 Aug 2011 10:39:40 -0700 Subject: [PATCH] add math library runtime settings check --- ctaocrypt/src/integer.c | 7 +++++++ ctaocrypt/src/tfm.c | 6 ++++++ ctaocrypt/test/test.c | 3 +++ cyassl/ctaocrypt/types.h | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/ctaocrypt/src/integer.c b/ctaocrypt/src/integer.c index 7ae49bfb2..2cf2d6dbd 100644 --- a/ctaocrypt/src/integer.c +++ b/ctaocrypt/src/integer.c @@ -38,6 +38,13 @@ #include +/* math settings check */ +word32 CheckRunTimeSettings(void) +{ + return CTC_SETTINGS; +} + + /* handle up to 6 inits */ int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f) diff --git a/ctaocrypt/src/tfm.c b/ctaocrypt/src/tfm.c index 409a5851a..37bb1d462 100644 --- a/ctaocrypt/src/tfm.c +++ b/ctaocrypt/src/tfm.c @@ -43,6 +43,12 @@ #include /* will define asm MACROS or C ones */ +/* math settings check */ +word32 CheckRunTimeSettings(void) +{ + return CTC_SETTINGS; +} + /* Functions */ diff --git a/ctaocrypt/test/test.c b/ctaocrypt/test/test.c index 3efae3150..972b41cac 100644 --- a/ctaocrypt/test/test.c +++ b/ctaocrypt/test/test.c @@ -131,6 +131,9 @@ void ctaocrypt_test(void* args) int ret = 0; ((func_args*)args)->return_code = -1; /* error state */ + + if (CheckCtcSettings() != 1) + err_sys("Build vs runtime math mismatch\n", -1234); if ( (ret = md5_test()) ) err_sys("MD5 test failed!\n", ret); diff --git a/cyassl/ctaocrypt/types.h b/cyassl/ctaocrypt/types.h index 0de50e797..b588a58b7 100644 --- a/cyassl/ctaocrypt/types.h +++ b/cyassl/ctaocrypt/types.h @@ -190,6 +190,30 @@ enum { }; + +/* settings detection for compile vs runtime math incombatibilities */ +enum { +#if !defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG) + CTC_SETTINGS = 0x0 +#elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG) + CTC_SETTINGS = 0x1 +#elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) + CTC_SETTINGS = 0x2 +#elif defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG) + CTC_SETTINGS = 0x4 +#elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG) + CTC_SETTINGS = 0x8 +#elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) + CTC_SETTINGS = 0x10 +#endif +}; + + +CYASSL_API word32 CheckRunTimeSettings(void); + +#define CheckCtcSettings() (CTC_SETTINGS == CheckRunTimeSettings()) + + #ifdef __cplusplus } /* extern "C" */ #endif