VirtualBox

Changeset 51851 in vbox


Ignore:
Timestamp:
Jul 3, 2014 2:01:28 PM (11 years ago)
Author:
vboxsync
Message:

Renamed hash implementations to fit better into the build system, supplying the missing OpenSSL-based MD5. VBoxRT uses the OpenSSL variants, all other libraries uses the alternatives. Also removed stupid OpenSSL dependencies in RTSha1Digest.cpp and RTSha256Digest.cpp.

Location:
trunk
Files:
1 added
5 edited
9 moved

Legend:

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

    r44528 r51851  
    4747 * MD5 hash algorithm context.
    4848 */
    49 typedef struct RTMD5CONTEXT
     49typedef union RTMD5CONTEXT
    5050{
    51     uint32_t in[16];
    52     uint32_t buf[4];
    53     uint32_t bits[2];
     51    uint64_t            u64BetterAlignment;
     52    uint8_t             abPadding[(4 + 6 + 16 + 1) * sizeof(uint32_t)];
     53    /** Context used by md5-alt.cpp. */
     54    struct
     55    {
     56        uint32_t        in[16];
     57        uint32_t        buf[4];
     58        uint32_t        bits[2];
     59    } AltPrivate;
     60#ifdef RT_MD5_OPENSSL_PRIVATE_CONTEXT
     61    /** Context used by md5-openssl.cpp. */
     62    MD5_CTX         OsslPrivate;
     63#endif
    5464} RTMD5CONTEXT;
    55 
    5665/** Pointer to MD5 hash algorithm context. */
    5766typedef RTMD5CONTEXT *PRTMD5CONTEXT;
  • trunk/src/VBox/HostDrivers/Support/Makefile.kmk

    r51843 r51851  
    267267        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/taf-init.cpp \
    268268        $(VBOX_PATH_RUNTIME_SRC)/common/crypto/taf-sanity.cpp \
    269         $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md2-alt.cpp \
    270         $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md5-alt.cpp \
    271         $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha1-alt.cpp \
    272         $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha256-alt.cpp \
    273         $(VBOX_PATH_RUNTIME_SRC)/common/checksum/sha512-alt.cpp \
     269        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-md2.cpp \
     270        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-md5.cpp \
     271        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-sha1.cpp \
     272        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-sha256.cpp \
     273        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/alt-sha512.cpp \
    274274        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md2str.cpp \
    275275        $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md5str.cpp \
  • trunk/src/VBox/Runtime/Makefile.kmk

    r51843 r51851  
    301301        common/checksum/crc32c.cpp \
    302302        common/checksum/crc64.cpp \
    303         common/checksum/md2-alt.cpp \
     303        common/checksum/alt-md2.cpp \
     304        common/checksum/alt-md5.cpp \
     305        common/checksum/alt-sha1.cpp \
     306        common/checksum/alt-sha256.cpp \
     307        common/checksum/alt-sha512.cpp \
    304308        common/checksum/md2str.cpp \
    305         common/checksum/md5-alt.cpp \
    306309        common/checksum/md5str.cpp \
    307310        common/checksum/ipv4.cpp \
     
    313316        common/checksum/RTSha1Digest.cpp \
    314317        common/checksum/RTSha256Digest.cpp \
    315         common/checksum/sha1-alt.cpp \
    316318        common/checksum/sha1str.cpp \
    317         common/checksum/sha256-alt.cpp \
    318319        common/checksum/sha256str.cpp \
    319         common/checksum/sha512-alt.cpp \
    320320        common/checksum/sha512str.cpp \
    321321        common/checksum/x509.cpp \
     
    12141214        common/time/timesupA.asm \
    12151215        common/time/timesup.cpp \
    1216         common/checksum/RTSha1Digest.cpp \
    1217         common/checksum/RTSha256Digest.cpp \
    1218         common/checksum/sha% \
    1219         common/checksum/md2% \
    12201216        common/checksum/x509.cpp \
    12211217        generic/RTLogWriteUser-generic.cpp \
     
    14251421VBoxRT_SOURCES                := \
    14261422        VBox/VBoxRTDeps.cpp \
    1427         $(filter-out common/checksum/crc32.cpp, $(RuntimeR3_SOURCES)) \
     1423        $(filter-out common/checksum/crc32.cpp, \
     1424                $(patsubst common/checksum/alt-%,common/checksum/openssl-%,$(RuntimeR3_SOURCES))) \
    14281425        common/checksum/crc32-zlib.cpp \
    14291426        common/misc/aiomgr.cpp
     
    18401837        common/alloc/heapsimple.cpp \
    18411838        common/alloc/heapoffset.cpp \
     1839        common/checksum/alt-md5.cpp \
    18421840        common/checksum/crc32.cpp \
    18431841        common/checksum/crc64.cpp \
    1844         common/checksum/md5-alt.cpp \
    18451842        common/checksum/ipv4.cpp \
    18461843        common/checksum/ipv6.cpp \
     
    20362033        common/crypto/taf-init.cpp \
    20372034        common/crypto/taf-sanity.cpp \
    2038         common/checksum/md2-alt.cpp \
    2039         common/checksum/sha1-alt.cpp \
    2040         common/checksum/sha256-alt.cpp \
    2041         common/checksum/sha512-alt.cpp \
     2035        common/checksum/alt-md2.cpp \
     2036        common/checksum/alt-sha1.cpp \
     2037        common/checksum/alt-sha256.cpp \
     2038        common/checksum/alt-sha512.cpp \
    20422039        common/checksum/md2str.cpp \
    20432040        common/checksum/md5str.cpp \
     
    23382335 RuntimeRC_INCS          = include
    23392336 RuntimeRC_SOURCES      := \
     2337        common/checksum/alt-md5.cpp \
    23402338        common/checksum/crc32.cpp \
    23412339        common/checksum/crc64.cpp \
    2342         common/checksum/md5-alt.cpp \
    23432340        common/log/log.cpp \
    23442341        common/log/logellipsis.cpp \
  • trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp

    r32569 r51851  
    22/** @file
    33 * IPRT - SHA1 digest creation
     4 *
     5 * @todo Replace this with generic RTCrDigest based implementation. Too much
     6 *       stupid code duplication.
    47 */
    58
     
    3841#include <iprt/file.h>
    3942
    40 #include <openssl/sha.h>
    41 
    4243
    4344RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
     
    5152    *ppszDigest = NULL;
    5253
    53     /* Initialize OpenSSL. */
    54     SHA_CTX ctx;
    55     if (!SHA1_Init(&ctx))
    56         return VERR_INTERNAL_ERROR;
     54    /* Initialize the hash context. */
     55    RTSHA1CONTEXT Ctx;
     56    RTSha1Init(&Ctx);
    5757
    5858    /* Buffer size for progress callback */
    59     double rdMulti = 100.0 / cbBuf;
     59    double rdMulti = 100.0 / (cbBuf ? cbBuf : 1);
    6060
    6161    /* Working buffer */
     
    6363
    6464    /* Process the memory in blocks */
    65     size_t cbRead;
    6665    size_t cbReadTotal = 0;
    6766    for (;;)
    6867    {
    69         cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
    70         if(!SHA1_Update(&ctx, pvTmp, cbRead))
    71         {
    72             rc = VERR_INTERNAL_ERROR;
    73             break;
    74         }
     68        size_t cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
     69        RTSha1Update(&Ctx, pvTmp, cbRead);
    7570        cbReadTotal += cbRead;
    7671        pvTmp += cbRead;
     
    9085    {
    9186        /* Finally calculate & format the SHA1 sum */
    92         unsigned char auchDig[RTSHA1_HASH_SIZE];
    93         if (!SHA1_Final(auchDig, &ctx))
    94             return VERR_INTERNAL_ERROR;
     87        uint8_t abHash[RTSHA1_HASH_SIZE];
     88        RTSha1Final(&Ctx, abHash);
    9589
    9690        char *pszDigest;
     
    9892        if (RT_SUCCESS(rc))
    9993        {
    100             rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);
     94            rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1);
    10195            if (RT_SUCCESS(rc))
    10296                *ppszDigest = pszDigest;
     
    118112    *ppszDigest = NULL;
    119113
    120     /* Initialize OpenSSL. */
    121     SHA_CTX ctx;
    122     if (!SHA1_Init(&ctx))
    123         return VERR_INTERNAL_ERROR;
    124 
    125114    /* Open the file to calculate a SHA1 sum of */
    126115    RTFILE hFile;
     
    140129            return rc;
    141130        }
    142         rdMulti = 100.0 / cbFile;
     131        rdMulti = 100.0 / (cbFile ? cbFile : 1);
    143132    }
    144133
     
    153142    }
    154143
     144    /* Initialize the hash context. */
     145    RTSHA1CONTEXT Ctx;
     146    RTSha1Init(&Ctx);
     147
    155148    /* Read that file in blocks */
    156     size_t cbRead;
    157149    size_t cbReadTotal = 0;
    158150    for (;;)
    159151    {
     152        size_t cbRead;
    160153        rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
    161154        if (RT_FAILURE(rc) || !cbRead)
    162155            break;
    163         if(!SHA1_Update(&ctx, pvBuf, cbRead))
    164         {
    165             rc = VERR_INTERNAL_ERROR;
    166             break;
    167         }
     156        RTSha1Update(&Ctx, pvBuf, cbRead);
    168157        cbReadTotal += cbRead;
    169158
     
    183172
    184173    /* Finally calculate & format the SHA1 sum */
    185     unsigned char auchDig[RTSHA1_HASH_SIZE];
    186     if (!SHA1_Final(auchDig, &ctx))
    187         return VERR_INTERNAL_ERROR;
     174    uint8_t abHash[RTSHA1_HASH_SIZE];
     175    RTSha1Final(&Ctx, abHash);
    188176
    189177    char *pszDigest;
     
    191179    if (RT_SUCCESS(rc))
    192180    {
    193         rc = RTSha1ToString(auchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);
     181        rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1);
    194182        if (RT_SUCCESS(rc))
    195183            *ppszDigest = pszDigest;
  • trunk/src/VBox/Runtime/common/checksum/RTSha256Digest.cpp

    r45234 r51851  
    11/** @file
    22 * IPRT - SHA256 digest creation
     3 *
     4 * @todo Replace this with generic RTCrDigest based implementation. Too much
     5 *       stupid code duplication.
    36 */
    47
    58/*
    6  * Copyright (C) 2009-2013 Oracle Corporation
     9 * Copyright (C) 2009-2014 Oracle Corporation
    710 *
    811 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3740#include <iprt/file.h>
    3841
    39 #include <openssl/sha.h>
    40 
    4142
    4243RTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
     
    5051    *ppszDigest = NULL;
    5152
    52     /* Initialize OpenSSL. */
    53     SHA256_CTX ctx;
    54     if (!SHA256_Init(&ctx))
    55         return VERR_INTERNAL_ERROR;
     53    /* Initialize the hash context. */
     54    RTSHA256CONTEXT Ctx;
     55    RTSha256Init(&Ctx);
    5656
    5757    /* Buffer size for progress callback */
    58     double rdMulti = 100.0 / cbBuf;
     58    double rdMulti = 100.0 / (cbBuf ? cbBuf : 1);
    5959
    6060    /* Working buffer */
     
    6262
    6363    /* Process the memory in blocks */
    64     size_t cbRead;
    6564    size_t cbReadTotal = 0;
    6665    for (;;)
    6766    {
    68         cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
    69         if(!SHA256_Update(&ctx, pvTmp, cbRead))
    70         {
    71             rc = VERR_INTERNAL_ERROR;
    72             break;
    73         }
     67        size_t cbRead = RT_MIN(cbBuf - cbReadTotal, _1M);
     68        RTSha256Update(&Ctx, pvTmp, cbRead);
    7469        cbReadTotal += cbRead;
    7570        pvTmp += cbRead;
     
    8984    {
    9085        /* Finally calculate & format the SHA256 sum */
    91         unsigned char auchDig[RTSHA256_HASH_SIZE];
    92         if (!SHA256_Final(auchDig, &ctx))
    93             return VERR_INTERNAL_ERROR;
     86        uint8_t abHash[RTSHA256_HASH_SIZE];
     87        RTSha256Final(&Ctx, abHash);
    9488
    9589        char *pszDigest;
     
    9791        if (RT_SUCCESS(rc))
    9892        {
    99             rc = RTSha256ToString(auchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);
     93            rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
    10094            if (RT_SUCCESS(rc))
    10195                *ppszDigest = pszDigest;
     
    117111    *ppszDigest = NULL;
    118112
    119     /* Initialize OpenSSL. */
    120     SHA256_CTX ctx;
    121     if (!SHA256_Init(&ctx))
    122         return VERR_INTERNAL_ERROR;
     113    /* Initialize the hash context. */
     114    RTSHA256CONTEXT Ctx;
     115    RTSha256Init(&Ctx);
    123116
    124117    /* Open the file to calculate a SHA256 sum of */
     
    139132            return rc;
    140133        }
    141         rdMulti = 100.0 / cbFile;
     134        rdMulti = 100.0 / (cbFile ? cbFile : 1);
    142135    }
    143136
     
    153146
    154147    /* Read that file in blocks */
    155     size_t cbRead;
    156148    size_t cbReadTotal = 0;
    157149    for (;;)
    158150    {
     151        size_t cbRead;
    159152        rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
    160153        if (RT_FAILURE(rc) || !cbRead)
    161154            break;
    162         if(!SHA256_Update(&ctx, pvBuf, cbRead))
    163         {
    164             rc = VERR_INTERNAL_ERROR;
    165             break;
    166         }
     155        RTSha256Update(&Ctx, pvBuf, cbRead);
    167156        cbReadTotal += cbRead;
    168157
     
    182171
    183172    /* Finally calculate & format the SHA256 sum */
    184     unsigned char auchDig[RTSHA256_HASH_SIZE];
    185     if (!SHA256_Final(auchDig, &ctx))
    186         return VERR_INTERNAL_ERROR;
     173    uint8_t abHash[RTSHA256_HASH_SIZE];
     174    RTSha256Final(&Ctx, abHash);
    187175
    188176    char *pszDigest;
     
    190178    if (RT_SUCCESS(rc))
    191179    {
    192         rc = RTSha256ToString(auchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);
     180        rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1);
    193181        if (RT_SUCCESS(rc))
    194182            *ppszDigest = pszDigest;
  • trunk/src/VBox/Runtime/common/checksum/alt-md5.cpp

    r51843 r51851  
    212212 * initialization constants.
    213213 */
    214 RTDECL(void) RTMd5Init(PRTMD5CONTEXT ctx)
    215 {
    216     ctx->buf[0] = 0x67452301;
    217     ctx->buf[1] = 0xefcdab89;
    218     ctx->buf[2] = 0x98badcfe;
    219     ctx->buf[3] = 0x10325476;
    220 
    221     ctx->bits[0] = 0;
    222     ctx->bits[1] = 0;
     214RTDECL(void) RTMd5Init(PRTMD5CONTEXT pCtx)
     215{
     216    pCtx->AltPrivate.buf[0] = 0x67452301;
     217    pCtx->AltPrivate.buf[1] = 0xefcdab89;
     218    pCtx->AltPrivate.buf[2] = 0x98badcfe;
     219    pCtx->AltPrivate.buf[3] = 0x10325476;
     220
     221    pCtx->AltPrivate.bits[0] = 0;
     222    pCtx->AltPrivate.bits[1] = 0;
    223223}
    224224RT_EXPORT_SYMBOL(RTMd5Init);
     
    229229 * of bytes.
    230230 */
    231 RTDECL(void) RTMd5Update(PRTMD5CONTEXT ctx, const void *pvBuf, size_t len)
     231RTDECL(void) RTMd5Update(PRTMD5CONTEXT pCtx, const void *pvBuf, size_t len)
    232232{
    233233    const uint8_t  *buf = (const uint8_t *)pvBuf;
     
    235235
    236236    /* Update bitcount */
    237     t = ctx->bits[0];
    238     if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
    239     ctx->bits[1]++; /* Carry from low to high */
    240     ctx->bits[1] += (uint32_t)(len >> 29);
     237    t = pCtx->AltPrivate.bits[0];
     238    if ((pCtx->AltPrivate.bits[0] = t + ((uint32_t) len << 3)) < t)
     239    pCtx->AltPrivate.bits[1]++; /* Carry from low to high */
     240    pCtx->AltPrivate.bits[1] += (uint32_t)(len >> 29);
    241241
    242242    t = (t >> 3) & 0x3f;        /* Bytes already in shsInfo->data */
     
    245245    if (t)
    246246    {
    247         uint8_t *p = (uint8_t *) ctx->in + t;
     247        uint8_t *p = (uint8_t *) pCtx->AltPrivate.in + t;
    248248
    249249        t = 64 - t;
     
    254254        }
    255255        memcpy(p, buf, t);
    256         rtMd5ByteReverse(ctx->in, 16);
    257         rtMd5Transform(ctx->buf, ctx->in);
     256        rtMd5ByteReverse(pCtx->AltPrivate.in, 16);
     257        rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
    258258        buf += t;
    259259        len -= t;
     
    265265    {
    266266        while (len >= 64) {
    267             rtMd5Transform(ctx->buf, (uint32_t const *)buf);
     267            rtMd5Transform(pCtx->AltPrivate.buf, (uint32_t const *)buf);
    268268            buf += 64;
    269269            len -= 64;
     
    274274    {
    275275        while (len >= 64) {
    276             memcpy(ctx->in, buf, 64);
    277             rtMd5ByteReverse(ctx->in, 16);
    278             rtMd5Transform(ctx->buf, ctx->in);
     276            memcpy(pCtx->AltPrivate.in, buf, 64);
     277            rtMd5ByteReverse(pCtx->AltPrivate.in, 16);
     278            rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
    279279            buf += 64;
    280280            len -= 64;
     
    283283
    284284    /* Handle any remaining bytes of data */
    285     memcpy(ctx->in, buf, len);
     285    memcpy(pCtx->AltPrivate.in, buf, len);
    286286}
    287287RT_EXPORT_SYMBOL(RTMd5Update);
     
    292292 * 1 0* (64-bit count of bits processed, MSB-first)
    293293 */
    294 RTDECL(void) RTMd5Final(uint8_t digest[16], PRTMD5CONTEXT ctx)
     294RTDECL(void) RTMd5Final(uint8_t digest[16], PRTMD5CONTEXT pCtx)
    295295{
    296296    unsigned int count;
     
    298298
    299299    /* Compute number of bytes mod 64 */
    300     count = (ctx->bits[0] >> 3) & 0x3F;
     300    count = (pCtx->AltPrivate.bits[0] >> 3) & 0x3F;
    301301
    302302    /* Set the first char of padding to 0x80.  This is safe since there is
    303303       always at least one byte free */
    304     p = (uint8_t *)ctx->in + count;
     304    p = (uint8_t *)pCtx->AltPrivate.in + count;
    305305    *p++ = 0x80;
    306306
     
    313313        /* Two lots of padding:  Pad the first block to 64 bytes */
    314314        memset(p, 0, count);
    315         rtMd5ByteReverse(ctx->in, 16);
    316         rtMd5Transform(ctx->buf, ctx->in);
     315        rtMd5ByteReverse(pCtx->AltPrivate.in, 16);
     316        rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
    317317
    318318        /* Now fill the next block with 56 bytes */
    319         memset(ctx->in, 0, 56);
     319        memset(pCtx->AltPrivate.in, 0, 56);
    320320    }
    321321    else
     
    324324        memset(p, 0, count - 8);
    325325    }
    326     rtMd5ByteReverse(ctx->in, 14);
     326    rtMd5ByteReverse(pCtx->AltPrivate.in, 14);
    327327
    328328    /* Append length in bits and transform */
    329     ctx->in[14] = ctx->bits[0];
    330     ctx->in[15] = ctx->bits[1];
    331 
    332     rtMd5Transform(ctx->buf, ctx->in);
    333     rtMd5ByteReverse(ctx->buf, 4);
    334     memcpy(digest, ctx->buf, 16);
    335     memset(ctx, 0, sizeof(*ctx));        /* In case it's sensitive */
     329    pCtx->AltPrivate.in[14] = pCtx->AltPrivate.bits[0];
     330    pCtx->AltPrivate.in[15] = pCtx->AltPrivate.bits[1];
     331
     332    rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
     333    rtMd5ByteReverse(pCtx->AltPrivate.buf, 4);
     334    memcpy(digest, pCtx->AltPrivate.buf, 16);
     335    memset(pCtx, 0, sizeof(*pCtx));        /* In case it's sensitive */
    336336}
    337337RT_EXPORT_SYMBOL(RTMd5Final);
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