Ignore:
Timestamp:
Jun 22, 2021, 9:00:19 PM (3 years ago)
Author:
coas-nagasima
Message:

WolfSSLとAzure IoT SDKを更新

Location:
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/misc.c

    r457 r464  
    1919 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
    2020 */
    21 
    22 
     21/*
     22
     23DESCRIPTION
     24This module implements the arithmetic-shift right, left, byte swapping, XOR,
     25masking and clearing memory logic.
     26
     27*/
    2328#ifdef HAVE_CONFIG_H
    2429    #include <config.h>
     
    7883
    7984#else /* generic */
     85/* This routine performs a left circular arithmetic shift of <x> by <y> value. */
    8086
    8187    WC_STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
     
    8490    }
    8591
    86 
     92/* This routine performs a right circular arithmetic shift of <x> by <y> value. */
    8793    WC_STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
    8894    {
     
    9298#endif
    9399
    94 
     100#ifdef WC_RC2
     101
     102/* This routine performs a left circular arithmetic shift of <x> by <y> value */
     103WC_STATIC WC_INLINE word16 rotlFixed16(word16 x, word16 y)
     104{
     105    return (x << y) | (x >> (sizeof(y) * 8 - y));
     106}
     107
     108
     109/* This routine performs a right circular arithmetic shift of <x> by <y> value */
     110WC_STATIC WC_INLINE word16 rotrFixed16(word16 x, word16 y)
     111{
     112    return (x >> y) | (x << (sizeof(y) * 8 - y));
     113}
     114
     115#endif /* WC_RC2 */
     116
     117/* This routine performs a byte swap of 32-bit word value. */
    95118WC_STATIC WC_INLINE word32 ByteReverseWord32(word32 value)
    96119{
     
    105128        defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
    106129    return (word32)__builtin_bswap32(value);
     130#elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
     131      defined(__aarch64__)
     132    __asm__ volatile (
     133        "REV32 %0, %0  \n"
     134        : "+r" (value)
     135        :
     136    );
     137    return value;
     138#elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
     139      (defined(__thumb__) || defined(__arm__))
     140    __asm__ volatile (
     141        "REV %0, %0  \n"
     142        : "+r" (value)
     143        :
     144    );
     145    return value;
    107146#elif defined(FAST_ROTATE)
    108147    /* 5 instructions with rotate instruction, 9 without */
     
    115154#endif
    116155}
    117 
    118 
     156/* This routine performs a byte swap of words array of a given count. */
    119157WC_STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
    120158                                    word32 byteCount)
     
    126164
    127165}
    128 
    129166
    130167#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS)
     
    173210
    174211#ifndef WOLFSSL_NO_XOR_OPS
     212/* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number
     213of wolfssl_words, placing the result in <*r>. */
     214WC_STATIC WC_INLINE void XorWordsOut(wolfssl_word* r, const wolfssl_word* a,
     215                                     const wolfssl_word* b, word32 n)
     216{
     217    word32 i;
     218
     219    for (i = 0; i < n; i++) r[i] = a[i] ^ b[i];
     220}
     221
     222/* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n
     223counts, placing the result in <*buf>. */
     224
     225WC_STATIC WC_INLINE void xorbufout(void*out, const void* buf, const void* mask,
     226                                   word32 count)
     227{
     228    if (((wolfssl_word)out | (wolfssl_word)buf | (wolfssl_word)mask | count) % \
     229                                                         WOLFSSL_WORD_SIZE == 0)
     230        XorWordsOut( (wolfssl_word*)out, (wolfssl_word*)buf,
     231                     (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
     232    else {
     233        word32 i;
     234        byte*       o = (byte*)out;
     235        byte*       b = (byte*)buf;
     236        const byte* m = (const byte*)mask;
     237
     238        for (i = 0; i < count; i++) o[i] = b[i] ^ m[i];
     239    }
     240}
     241
     242/* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number
     243of wolfssl_words, placing the result in <*r>. */
    175244WC_STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
    176245{
     
    180249}
    181250
     251/* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n
     252counts, placing the result in <*buf>. */
    182253
    183254WC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
     
    197268
    198269#ifndef WOLFSSL_NO_FORCE_ZERO
    199 /* Make sure compiler doesn't skip */
     270/* This routine fills the first len bytes of the memory area pointed by mem
     271   with zeros. It ensures compiler optimizations doesn't skip it  */
    200272WC_STATIC WC_INLINE void ForceZero(const void* mem, word32 len)
    201273{
    202274    volatile byte* z = (volatile byte*)mem;
    203275
    204 #if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE)
     276#if (defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_AARCH64_BUILD)) \
     277            && defined(WORD64_AVAILABLE)
    205278    volatile word64* w;
    206279    #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS
     
    243316        #define min min
    244317    #endif
     318    /* returns the smaller of a and b */
    245319    WC_STATIC WC_INLINE word32 min(word32 a, word32 b)
    246320    {
     
    324398WC_STATIC WC_INLINE byte ctMaskGT(int a, int b)
    325399{
    326     return (((word32)a - b - 1) >> 31) - 1;
     400    return (byte)((((word32)a - b - 1) >> 31) - 1);
    327401}
    328402
     
    330404WC_STATIC WC_INLINE byte ctMaskGTE(int a, int b)
    331405{
    332     return (((word32)a - b    ) >> 31) - 1;
     406    return (byte)((((word32)a - b    ) >> 31) - 1);
    333407}
    334408
     
    336410WC_STATIC WC_INLINE int ctMaskIntGTE(int a, int b)
    337411{
    338     return (((word32)a - b    ) >> 31) - 1;
     412    return (int)((((word32)a - b    ) >> 31) - 1);
    339413}
    340414
     
    342416WC_STATIC WC_INLINE byte ctMaskLT(int a, int b)
    343417{
    344     return (((word32)b - a - 1) >> 31) - 1;
     418    return (byte)((((word32)b - a - 1) >> 31) - 1);
    345419}
    346420
     
    348422WC_STATIC WC_INLINE byte ctMaskLTE(int a, int b)
    349423{
    350     return (((word32)b - a    ) >> 31) - 1;
     424    return (byte)((((word32)b - a    ) >> 31) - 1);
    351425}
    352426
     
    354428WC_STATIC WC_INLINE byte ctMaskEq(int a, int b)
    355429{
    356     return (~ctMaskGT(a, b)) & (~ctMaskLT(a, b));
    357 }
    358 
     430    return (byte)(~ctMaskGT(a, b)) & (byte)(~ctMaskLT(a, b));
     431}
     432
     433/* Constant time - sets 16 bit integer mask when a > b */
    359434WC_STATIC WC_INLINE word16 ctMask16GT(int a, int b)
    360435{
    361     return (((word32)a - b - 1) >> 31) - 1;
    362 }
    363 
     436    return (word16)((((word32)a - b - 1) >> 31) - 1);
     437}
     438
     439/* Constant time - sets 16 bit integer mask when a >= b */
     440WC_STATIC WC_INLINE word16 ctMask16GTE(int a, int b)
     441{
     442    return (word16)((((word32)a - b    ) >> 31) - 1);
     443}
     444
     445/* Constant time - sets 16 bit integer mask when a < b. */
    364446WC_STATIC WC_INLINE word16 ctMask16LT(int a, int b)
    365447{
    366     return (((word32)a - b - 1) >> 31) - 1;
    367 }
    368 
     448    return (word16)((((word32)b - a - 1) >> 31) - 1);
     449}
     450
     451/* Constant time - sets 16 bit integer mask when a <= b. */
     452WC_STATIC WC_INLINE word16 ctMask16LTE(int a, int b)
     453{
     454    return (word16)((((word32)b - a    ) >> 31) - 1);
     455}
     456
     457/* Constant time - sets 16 bit integer mask when a == b. */
    369458WC_STATIC WC_INLINE word16 ctMask16Eq(int a, int b)
    370459{
    371     return (~ctMask16GT(a, b)) & (~ctMask16LT(a, b));
     460    return (word16)(~ctMask16GT(a, b)) & (word16)(~ctMask16LT(a, b));
    372461}
    373462
     
    375464WC_STATIC WC_INLINE byte ctMaskNotEq(int a, int b)
    376465{
    377     return ctMaskGT(a, b) | ctMaskLT(a, b);
     466    return (byte)ctMaskGT(a, b) | (byte)ctMaskLT(a, b);
    378467}
    379468
     
    381470WC_STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b)
    382471{
    383     return (b & ((byte)~(word32)m)) | (a & m);
     472    return (byte)((b & ((byte)~(word32)m)) | (a & m));
    384473}
    385474
     
    394483WC_STATIC WC_INLINE byte ctSetLTE(int a, int b)
    395484{
    396     return ((word32)a - b - 1) >> 31;
     485    return (byte)(((word32)a - b - 1) >> 31);
    397486}
    398487#endif
Note: See TracChangeset for help on using the changeset viewer.