VirtualBox

Changeset 51828 in vbox


Ignore:
Timestamp:
Jul 3, 2014 3:04:39 AM (11 years ago)
Author:
vboxsync
Message:

Added alternative SHA-1 code. Extended asm.h with 32-bit and 64-bit rotate methods. Hope the latter compiles well with gcc, if not I will fix it after some sleep.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/asm.h

    r49724 r51828  
    6666# pragma intrinsic(_InterlockedCompareExchange)
    6767# pragma intrinsic(_InterlockedCompareExchange64)
     68# pragma intrinsic(_rotl)
     69# pragma intrinsic(_rotr)
     70# pragma intrinsic(_rotl64)
     71# pragma intrinsic(_rotr64)
    6872# ifdef RT_ARCH_AMD64
    6973#  pragma intrinsic(__stosq)
     
    48114815
    48124816
     4817/**
     4818 * Rotate 32-bit unsigned value to the left by @a cShift.
     4819 *
     4820 * @returns Rotated value.
     4821 * @param   u32                 The value to rotate.
     4822 * @param   cShift              How many bits to rotate by.
     4823 */
     4824DECLINLINE(uint32_t) ASMRotateLeftU32(uint32_t u32, uint32_t cShift)
     4825{
     4826#if RT_INLINE_ASM_USES_INTRIN
     4827    return _rotl(u32, cShift);
     4828#elif RT_INLINE_ASM_GNU_STYLE && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
     4829    __asm__ __volatile__("roll %1, %0" : "=r" (u32) : "Ir" (cShift), "0" (u32));
     4830#else
     4831    cShift &= 31;
     4832    return (u32 << cShift) | (u32 >> (32 - cShift));
     4833#endif
     4834}
     4835
     4836
     4837/**
     4838 * Rotate 32-bit unsigned value to the right by @a cShift.
     4839 *
     4840 * @returns Rotated value.
     4841 * @param   u32                 The value to rotate.
     4842 * @param   cShift              How many bits to rotate by.
     4843 */
     4844DECLINLINE(uint32_t) ASMRotateRightU32(uint32_t u32, uint32_t cShift)
     4845{
     4846#if RT_INLINE_ASM_USES_INTRIN
     4847    return _rotr(u32, cShift);
     4848#elif RT_INLINE_ASM_GNU_STYLE && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
     4849    __asm__ __volatile__("rorl %1, %0" : "=r" (u32) : "Ir" (cShift), "0" (u32));
     4850#else
     4851    cShift &= 31;
     4852    return (u32 >> cShift) | (u32 << (32 - cShift));
     4853#endif
     4854}
     4855
     4856
     4857/**
     4858 * Rotate 64-bit unsigned value to the left by @a cShift.
     4859 *
     4860 * @returns Rotated value.
     4861 * @param   u64                 The value to rotate.
     4862 * @param   cShift              How many bits to rotate by.
     4863 */
     4864DECLINLINE(uint64_t) ASMRotateLeftU64(uint64_t u64, uint32_t cShift)
     4865{
     4866#if RT_INLINE_ASM_USES_INTRIN
     4867    return _rotl64(u64, cShift);
     4868#elif RT_INLINE_ASM_GNU_STYLE && defined(RT_ARCH_AMD64)
     4869    __asm__ __volatile__("rolq %1, %0" : "=r" (u64) : "Ir" (cShift), "0" (u64));
     4870#elif RT_INLINE_ASM_GNU_STYLE && defined(RT_ARCH_X86)
     4871    uint32_t uSpill;
     4872    __asm__ __volatile__("testb $0x20, %%cl\n\t"        /* if (cShift >= 0x20) { swap(u64.hi, u64lo); cShift -= 0x20; } */
     4873                         "jz    1f\n\t"
     4874                         "xchgl %%eax, %%edx\n\t"
     4875                         "1:\n\t"
     4876                         "andb  $0x1f, %%cl\n\t"        /* if (cShift & 0x1f) { */
     4877                         "jz    2f\n\t"
     4878                         "movl  %%edx, %2\n\t"          /*   save the hi value in %3. */
     4879                         "shldl %%cl,%%eax,%%edx\n\t"   /*   shift the hi value left, feeding MSBits from the low value. */
     4880                         "shldl %%cl,%2,%%eax\n\t"      /*   shift the lo value left, feeding MSBits from the saved hi value. */
     4881                         "2:\n\t"                       /* } */
     4882                         : "=A" (u64), "=c" (cShift), "=r" (uSpill)
     4883                         : "0" (u64),
     4884                           "1" (cShift));
     4885    return u64;
     4886#else
     4887    cShift &= 63;
     4888    return (u64 << cShift) | (u64 >> (64 - cShift));
     4889#endif
     4890}
     4891
     4892
     4893/**
     4894 * Rotate 64-bit unsigned value to the right by @a cShift.
     4895 *
     4896 * @returns Rotated value.
     4897 * @param   u64                 The value to rotate.
     4898 * @param   cShift              How many bits to rotate by.
     4899 */
     4900DECLINLINE(uint64_t) ASMRotateRightU64(uint64_t u64, uint32_t cShift)
     4901{
     4902#if RT_INLINE_ASM_USES_INTRIN
     4903    return _rotr64(u64, cShift);
     4904#elif RT_INLINE_ASM_GNU_STYLE && defined(RT_ARCH_AMD64)
     4905    __asm__ __volatile__("rorq %1, %0" : "=r" (u64) : "Ir" (cShift), "0" (u64));
     4906#elif RT_INLINE_ASM_GNU_STYLE && defined(RT_ARCH_X86)
     4907    uint32_t uSpill;
     4908    __asm__ __volatile__("testb $0x20, %%cl\n\t"        /* if (cShift >= 0x20) { swap(u64.hi, u64lo); cShift -= 0x20; } */
     4909                         "jz    1f\n\t"
     4910                         "xchgl %%eax, %%edx\n\t"
     4911                         "1:\n\t"
     4912                         "andb  $0x1f, %%cl\n\t"        /* if (cShift & 0x1f) { */
     4913                         "jz    2f\n\t"
     4914                         "movl  %%edx, %2\n\t"          /*   save the hi value in %3. */
     4915                         "shrdl %%cl,%%eax,%%edx\n\t"   /*   shift the hi value right, feeding LSBits from the low value. */
     4916                         "shrdl %%cl,%2,%%eax\n\t"      /*   shift the lo value right, feeding LSBits from the saved hi value. */
     4917                         "2:\n\t"                       /* } */
     4918                         : "=A" (u64), "=c" (cShift), "=r" (uSpill)
     4919                         : "0" (u64),
     4920                           "1" (cShift));
     4921    return u64;
     4922#else
     4923    cShift &= 63;
     4924    return (u64 >> cShift) | (u64 << (64 - cShift));
     4925#endif
     4926}
     4927
    48134928/** @} */
    48144929
  • trunk/include/iprt/sha.h

    r48934 r51828  
    4646typedef union RTSHA1CONTEXT
    4747{
    48     uint8_t abPadding[ARCH_BITS == 32 ? 96 : 128];
     48    uint64_t                u64BetterAlignment;
     49    uint8_t                 abPadding[8 + (5 + 80) * 4 + 4];
    4950#ifdef RT_SHA1_PRIVATE_CONTEXT
    50     SHA_CTX Private;
     51    SHA_CTX                 Private;
     52#endif
     53#ifdef RT_SHA1_PRIVATE_ALT_CONTEXT
     54    RTSHA1ALTPRIVATECTX     AltPrivate;
    5155#endif
    5256} RTSHA1CONTEXT;
     
    149153typedef union RTSHA256CONTEXT
    150154{
    151     uint8_t abPadding[ARCH_BITS == 32 ? 112 : 160];
     155    uint64_t                u64BetterAlignment;
     156    uint8_t                 abPadding[ARCH_BITS == 32 ? 112 : 160];
    152157#ifdef RT_SHA256_PRIVATE_CONTEXT
    153     SHA256_CTX Private;
     158    SHA256_CTX              Private;
    154159#endif
    155160} RTSHA256CONTEXT;
     
    253258typedef union RTSHA512CONTEXT
    254259{
    255     uint8_t abPadding[ARCH_BITS == 32 ? 216 : 256];
     260    uint64_t                u64BetterAlignment;
     261    uint8_t                 abPadding[ARCH_BITS == 32 ? 216 : 256];
    256262#ifdef RT_SHA512_PRIVATE_CONTEXT
    257     SHA512_CTX Private;
     263    SHA512_CTX              Private;
    258264#endif
    259265} RTSHA512CONTEXT;
  • trunk/src/VBox/HostDrivers/Support/Makefile.kmk

    r51825 r51828  
    269269        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md2-alt.cpp \
    270270        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md5.cpp \
    271         $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha1.cpp \
     271        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha1-alt.cpp \
    272272        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha256.cpp \
    273273        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha512.cpp \
  • trunk/src/VBox/Runtime/Makefile.kmk

    r51821 r51828  
    313313        common/checksum/RTSha1Digest.cpp \
    314314        common/checksum/RTSha256Digest.cpp \
    315         common/checksum/sha1.cpp \
     315        common/checksum/sha1-alt.cpp \
    316316        common/checksum/sha1str.cpp \
    317317        common/checksum/sha256.cpp \
     
    20372037        common/crypto/taf-sanity.cpp \
    20382038        common/checksum/md2-alt.cpp \
    2039         common/checksum/sha1.cpp \
     2039        common/checksum/sha1-alt.cpp \
    20402040        common/checksum/sha256.cpp \
    20412041        common/checksum/sha512.cpp \
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette