Ignore:
Timestamp:
Feb 7, 2019, 8:36:33 AM (5 years ago)
Author:
coas-nagasima
Message:

wolfsslを3.15.7にバージョンアップ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/misc.c

    r337 r372  
    4747/* Check for if compiling misc.c when not needed. */
    4848#if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE)
     49    #ifndef WOLFSSL_IGNORE_FILE_WARN
    4950    #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined)
     51    #endif
    5052
    5153#else
     
    6567    #pragma intrinsic(_lrotl, _lrotr)
    6668
    67     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
     69    STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
    6870    {
    6971        return y ? _lrotl(x, y) : x;
    7072    }
    7173
    72     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
     74    STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
    7375    {
    7476        return y ? _lrotr(x, y) : x;
     
    7779#else /* generic */
    7880
    79     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
     81    STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
    8082    {
    8183        return (x << y) | (x >> (sizeof(y) * 8 - y));
     
    8385
    8486
    85     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
     87    STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
    8688    {
    8789        return (x >> y) | (x << (sizeof(y) * 8 - y));
     
    9193
    9294
    93 STATIC INLINE word32 ByteReverseWord32(word32 value)
     95STATIC WC_INLINE word32 ByteReverseWord32(word32 value)
    9496{
    9597#ifdef PPC_INTRINSICS
     
    100102#elif defined(KEIL_INTRINSICS)
    101103    return (word32)__rev(value);
     104#elif defined(WOLF_ALLOW_BUILTIN) && \
     105        defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
     106    return (word32)__builtin_bswap32(value);
    102107#elif defined(FAST_ROTATE)
    103108    /* 5 instructions with rotate instruction, 9 without */
     
    112117
    113118
    114 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
     119STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
    115120                                    word32 byteCount)
    116121{
     
    123128
    124129
    125 #ifdef WORD64_AVAILABLE
    126 
    127 
    128 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
     130#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS)
     131
     132
     133STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y)
    129134{
    130135    return (x << y) | (x >> (sizeof(y) * 8 - y));
     
    132137
    133138
    134 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
     139STATIC WC_INLINE word64 rotrFixed64(word64 x, word64 y)
    135140{
    136141    return (x >> y) | (x << (sizeof(y) * 8 - y));
     
    138143
    139144
    140 STATIC INLINE word64 ByteReverseWord64(word64 value)
    141 {
    142 #if defined(WOLFCRYPT_SLOW_WORD64)
    143         return (word64)(ByteReverseWord32((word32)value)) << 32 |
    144                     ByteReverseWord32((word32)(value>>32));
     145STATIC WC_INLINE word64 ByteReverseWord64(word64 value)
     146{
     147#if defined(WOLF_ALLOW_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
     148    return (word64)__builtin_bswap64(value);
     149#elif defined(WOLFCRYPT_SLOW_WORD64)
     150        return (word64)((word64)ByteReverseWord32((word32) value)) << 32 |
     151                    (word64)ByteReverseWord32((word32)(value   >> 32));
    145152#else
    146153        value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
     
    153160
    154161
    155 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
     162STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in,
    156163                                      word32 byteCount)
    157164{
     
    163170}
    164171
    165 #endif /* WORD64_AVAILABLE */
    166 
    167 
    168 STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
     172#endif /* WORD64_AVAILABLE && !WOLFSSL_NO_WORD64_OPS */
     173
     174#ifndef WOLFSSL_NO_XOR_OPS
     175STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
    169176{
    170177    word32 i;
     
    174181
    175182
    176 STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)
     183STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
    177184{
    178185    if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0)
     
    187194    }
    188195}
    189 
    190 
     196#endif
     197
     198#ifndef WOLFSSL_NO_FORCE_ZERO
    191199/* Make sure compiler doesn't skip */
    192 STATIC INLINE void ForceZero(const void* mem, word32 len)
     200STATIC WC_INLINE void ForceZero(const void* mem, word32 len)
    193201{
    194202    volatile byte* z = (volatile byte*)mem;
    195 #ifdef WOLFSSL_X86_64_BUILD
     203
     204#if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE)
    196205    volatile word64* w;
    197 
     206    #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS
     207        word32 l = (sizeof(word64) - ((size_t)z & (sizeof(word64)-1))) &
     208                                                             (sizeof(word64)-1);
     209
     210        if (len < l) l = len;
     211        len -= l;
     212        while (l--) *z++ = 0;
     213    #endif
    198214    for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w))
    199215        *w++ = 0;
    200216    z = (volatile byte*)w;
    201217#endif
     218
    202219    while (len--) *z++ = 0;
    203220}
    204 
    205 
     221#endif
     222
     223
     224#ifndef WOLFSSL_NO_CONST_CMP
    206225/* check all length bytes for equality, return 0 on success */
    207 STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length)
     226STATIC WC_INLINE int ConstantCompare(const byte* a, const byte* b, int length)
    208227{
    209228    int i;
     
    216235    return compareSum;
    217236}
     237#endif
    218238
    219239
     
    223243        #define min min
    224244    #endif
    225     STATIC INLINE word32 min(word32 a, word32 b)
     245    STATIC WC_INLINE word32 min(word32 a, word32 b)
    226246    {
    227247        return a > b ? b : a;
     
    234254        #define max max
    235255    #endif
    236     STATIC INLINE word32 max(word32 a, word32 b)
     256    STATIC WC_INLINE word32 max(word32 a, word32 b)
    237257    {
    238258        return a > b ? a : b;
     
    240260#endif /* !WOLFSSL_HAVE_MAX */
    241261
     262#ifndef WOLFSSL_NO_INT_ENCODE
    242263/* converts a 32 bit integer to 24 bit */
    243 STATIC INLINE void c32to24(word32 in, word24 out)
     264STATIC WC_INLINE void c32to24(word32 in, word24 out)
    244265{
    245266    out[0] = (in >> 16) & 0xff;
     
    249270
    250271/* convert 16 bit integer to opaque */
    251 STATIC INLINE void c16toa(word16 u16, byte* c)
    252 {
    253     c[0] = (u16 >> 8) & 0xff;
    254     c[1] =  u16 & 0xff;
     272STATIC WC_INLINE void c16toa(word16 wc_u16, byte* c)
     273{
     274    c[0] = (wc_u16 >> 8) & 0xff;
     275    c[1] =  wc_u16 & 0xff;
    255276}
    256277
    257278/* convert 32 bit integer to opaque */
    258 STATIC INLINE void c32toa(word32 u32, byte* c)
    259 {
    260     c[0] = (u32 >> 24) & 0xff;
    261     c[1] = (u32 >> 16) & 0xff;
    262     c[2] = (u32 >>  8) & 0xff;
    263     c[3] =  u32 & 0xff;
    264 }
    265 
     279STATIC WC_INLINE void c32toa(word32 wc_u32, byte* c)
     280{
     281    c[0] = (wc_u32 >> 24) & 0xff;
     282    c[1] = (wc_u32 >> 16) & 0xff;
     283    c[2] = (wc_u32 >>  8) & 0xff;
     284    c[3] =  wc_u32 & 0xff;
     285}
     286#endif
     287
     288#ifndef WOLFSSL_NO_INT_DECODE
    266289/* convert a 24 bit integer into a 32 bit one */
    267 STATIC INLINE void c24to32(const word24 u24, word32* u32)
    268 {
    269     *u32 = (u24[0] << 16) | (u24[1] << 8) | u24[2];
     290STATIC WC_INLINE void c24to32(const word24 wc_u24, word32* wc_u32)
     291{
     292    *wc_u32 = (wc_u24[0] << 16) | (wc_u24[1] << 8) | wc_u24[2];
    270293}
    271294
    272295
    273296/* convert opaque to 24 bit integer */
    274 STATIC INLINE void ato24(const byte* c, word32* u24)
    275 {
    276     *u24 = (c[0] << 16) | (c[1] << 8) | c[2];
     297STATIC WC_INLINE void ato24(const byte* c, word32* wc_u24)
     298{
     299    *wc_u24 = (c[0] << 16) | (c[1] << 8) | c[2];
    277300}
    278301
    279302/* convert opaque to 16 bit integer */
    280 STATIC INLINE void ato16(const byte* c, word16* u16)
    281 {
    282     *u16 = (word16) ((c[0] << 8) | (c[1]));
     303STATIC WC_INLINE void ato16(const byte* c, word16* wc_u16)
     304{
     305    *wc_u16 = (word16) ((c[0] << 8) | (c[1]));
    283306}
    284307
    285308/* convert opaque to 32 bit integer */
    286 STATIC INLINE void ato32(const byte* c, word32* u32)
    287 {
    288     *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
    289 }
    290 
    291 
    292 STATIC INLINE word32 btoi(byte b)
     309STATIC WC_INLINE void ato32(const byte* c, word32* wc_u32)
     310{
     311    *wc_u32 = ((word32)c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
     312}
     313
     314
     315STATIC WC_INLINE word32 btoi(byte b)
    293316{
    294317    return (word32)(b - 0x30);
    295318}
    296 
     319#endif
     320
     321
     322#ifndef WOLFSSL_NO_CT_OPS
     323/* Constant time - mask set when a > b. */
     324STATIC WC_INLINE byte ctMaskGT(int a, int b)
     325{
     326    return (((word32)a - b - 1) >> 31) - 1;
     327}
     328
     329/* Constant time - mask set when a >= b. */
     330STATIC WC_INLINE byte ctMaskGTE(int a, int b)
     331{
     332    return (((word32)a - b    ) >> 31) - 1;
     333}
     334
     335/* Constant time - mask set when a < b. */
     336STATIC WC_INLINE byte ctMaskLT(int a, int b)
     337{
     338    return (((word32)b - a - 1) >> 31) - 1;
     339}
     340
     341/* Constant time - mask set when a <= b. */
     342STATIC WC_INLINE byte ctMaskLTE(int a, int b)
     343{
     344    return (((word32)b - a    ) >> 31) - 1;
     345}
     346
     347/* Constant time - mask set when a == b. */
     348STATIC WC_INLINE byte ctMaskEq(int a, int b)
     349{
     350    return 0 - (a == b);
     351}
     352
     353/* Constant time - mask set when a != b. */
     354STATIC WC_INLINE byte ctMaskNotEq(int a, int b)
     355{
     356    return 0 - (a != b);
     357}
     358
     359/* Constant time - select a when mask is set and b otherwise. */
     360STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b)
     361{
     362    return (b & ((byte)~(word32)m)) | (a & m);
     363}
     364
     365/* Constant time - select integer a when mask is set and integer b otherwise. */
     366STATIC WC_INLINE int ctMaskSelInt(byte m, int a, int b)
     367{
     368    return (b & (~(signed int)(signed char)m)) |
     369           (a & ( (signed int)(signed char)m));
     370}
     371
     372/* Constant time - bit set when a <= b. */
     373STATIC WC_INLINE byte ctSetLTE(int a, int b)
     374{
     375    return ((word32)a - b - 1) >> 31;
     376}
     377#endif
    297378
    298379
Note: See TracChangeset for help on using the changeset viewer.