Changeset 51851 in vbox
- Timestamp:
- Jul 3, 2014 2:01:28 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
- 9 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/md5.h
r44528 r51851 47 47 * MD5 hash algorithm context. 48 48 */ 49 typedef structRTMD5CONTEXT49 typedef union RTMD5CONTEXT 50 50 { 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 54 64 } RTMD5CONTEXT; 55 56 65 /** Pointer to MD5 hash algorithm context. */ 57 66 typedef RTMD5CONTEXT *PRTMD5CONTEXT; -
trunk/src/VBox/HostDrivers/Support/Makefile.kmk
r51843 r51851 267 267 $(VBOX_PATH_RUNTIME_SRC)/common/crypto/taf-init.cpp \ 268 268 $(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 \ 274 274 $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md2str.cpp \ 275 275 $(VBOX_PATH_RUNTIME_SRC)/common/checksum/md5str.cpp \ -
trunk/src/VBox/Runtime/Makefile.kmk
r51843 r51851 301 301 common/checksum/crc32c.cpp \ 302 302 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 \ 304 308 common/checksum/md2str.cpp \ 305 common/checksum/md5-alt.cpp \306 309 common/checksum/md5str.cpp \ 307 310 common/checksum/ipv4.cpp \ … … 313 316 common/checksum/RTSha1Digest.cpp \ 314 317 common/checksum/RTSha256Digest.cpp \ 315 common/checksum/sha1-alt.cpp \316 318 common/checksum/sha1str.cpp \ 317 common/checksum/sha256-alt.cpp \318 319 common/checksum/sha256str.cpp \ 319 common/checksum/sha512-alt.cpp \320 320 common/checksum/sha512str.cpp \ 321 321 common/checksum/x509.cpp \ … … 1214 1214 common/time/timesupA.asm \ 1215 1215 common/time/timesup.cpp \ 1216 common/checksum/RTSha1Digest.cpp \1217 common/checksum/RTSha256Digest.cpp \1218 common/checksum/sha% \1219 common/checksum/md2% \1220 1216 common/checksum/x509.cpp \ 1221 1217 generic/RTLogWriteUser-generic.cpp \ … … 1425 1421 VBoxRT_SOURCES := \ 1426 1422 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))) \ 1428 1425 common/checksum/crc32-zlib.cpp \ 1429 1426 common/misc/aiomgr.cpp … … 1840 1837 common/alloc/heapsimple.cpp \ 1841 1838 common/alloc/heapoffset.cpp \ 1839 common/checksum/alt-md5.cpp \ 1842 1840 common/checksum/crc32.cpp \ 1843 1841 common/checksum/crc64.cpp \ 1844 common/checksum/md5-alt.cpp \1845 1842 common/checksum/ipv4.cpp \ 1846 1843 common/checksum/ipv6.cpp \ … … 2036 2033 common/crypto/taf-init.cpp \ 2037 2034 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 \ 2042 2039 common/checksum/md2str.cpp \ 2043 2040 common/checksum/md5str.cpp \ … … 2338 2335 RuntimeRC_INCS = include 2339 2336 RuntimeRC_SOURCES := \ 2337 common/checksum/alt-md5.cpp \ 2340 2338 common/checksum/crc32.cpp \ 2341 2339 common/checksum/crc64.cpp \ 2342 common/checksum/md5-alt.cpp \2343 2340 common/log/log.cpp \ 2344 2341 common/log/logellipsis.cpp \ -
trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp
r32569 r51851 2 2 /** @file 3 3 * IPRT - SHA1 digest creation 4 * 5 * @todo Replace this with generic RTCrDigest based implementation. Too much 6 * stupid code duplication. 4 7 */ 5 8 … … 38 41 #include <iprt/file.h> 39 42 40 #include <openssl/sha.h>41 42 43 43 44 RTR3DECL(int) RTSha1Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser) … … 51 52 *ppszDigest = NULL; 52 53 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); 57 57 58 58 /* Buffer size for progress callback */ 59 double rdMulti = 100.0 / cbBuf;59 double rdMulti = 100.0 / (cbBuf ? cbBuf : 1); 60 60 61 61 /* Working buffer */ … … 63 63 64 64 /* Process the memory in blocks */ 65 size_t cbRead;66 65 size_t cbReadTotal = 0; 67 66 for (;;) 68 67 { 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); 75 70 cbReadTotal += cbRead; 76 71 pvTmp += cbRead; … … 90 85 { 91 86 /* 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); 95 89 96 90 char *pszDigest; … … 98 92 if (RT_SUCCESS(rc)) 99 93 { 100 rc = RTSha1ToString(a uchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);94 rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1); 101 95 if (RT_SUCCESS(rc)) 102 96 *ppszDigest = pszDigest; … … 118 112 *ppszDigest = NULL; 119 113 120 /* Initialize OpenSSL. */121 SHA_CTX ctx;122 if (!SHA1_Init(&ctx))123 return VERR_INTERNAL_ERROR;124 125 114 /* Open the file to calculate a SHA1 sum of */ 126 115 RTFILE hFile; … … 140 129 return rc; 141 130 } 142 rdMulti = 100.0 / cbFile;131 rdMulti = 100.0 / (cbFile ? cbFile : 1); 143 132 } 144 133 … … 153 142 } 154 143 144 /* Initialize the hash context. */ 145 RTSHA1CONTEXT Ctx; 146 RTSha1Init(&Ctx); 147 155 148 /* Read that file in blocks */ 156 size_t cbRead;157 149 size_t cbReadTotal = 0; 158 150 for (;;) 159 151 { 152 size_t cbRead; 160 153 rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead); 161 154 if (RT_FAILURE(rc) || !cbRead) 162 155 break; 163 if(!SHA1_Update(&ctx, pvBuf, cbRead)) 164 { 165 rc = VERR_INTERNAL_ERROR; 166 break; 167 } 156 RTSha1Update(&Ctx, pvBuf, cbRead); 168 157 cbReadTotal += cbRead; 169 158 … … 183 172 184 173 /* 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); 188 176 189 177 char *pszDigest; … … 191 179 if (RT_SUCCESS(rc)) 192 180 { 193 rc = RTSha1ToString(a uchDig, pszDigest, RTSHA1_DIGEST_LEN + 1);181 rc = RTSha1ToString(abHash, pszDigest, RTSHA1_DIGEST_LEN + 1); 194 182 if (RT_SUCCESS(rc)) 195 183 *ppszDigest = pszDigest; -
trunk/src/VBox/Runtime/common/checksum/RTSha256Digest.cpp
r45234 r51851 1 1 /** @file 2 2 * IPRT - SHA256 digest creation 3 * 4 * @todo Replace this with generic RTCrDigest based implementation. Too much 5 * stupid code duplication. 3 6 */ 4 7 5 8 /* 6 * Copyright (C) 2009-201 3Oracle Corporation9 * Copyright (C) 2009-2014 Oracle Corporation 7 10 * 8 11 * This file is part of VirtualBox Open Source Edition (OSE), as … … 37 40 #include <iprt/file.h> 38 41 39 #include <openssl/sha.h>40 41 42 42 43 RTR3DECL(int) RTSha256Digest(void* pvBuf, size_t cbBuf, char **ppszDigest, PFNRTPROGRESS pfnProgressCallback, void *pvUser) … … 50 51 *ppszDigest = NULL; 51 52 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); 56 56 57 57 /* Buffer size for progress callback */ 58 double rdMulti = 100.0 / cbBuf;58 double rdMulti = 100.0 / (cbBuf ? cbBuf : 1); 59 59 60 60 /* Working buffer */ … … 62 62 63 63 /* Process the memory in blocks */ 64 size_t cbRead;65 64 size_t cbReadTotal = 0; 66 65 for (;;) 67 66 { 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); 74 69 cbReadTotal += cbRead; 75 70 pvTmp += cbRead; … … 89 84 { 90 85 /* 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); 94 88 95 89 char *pszDigest; … … 97 91 if (RT_SUCCESS(rc)) 98 92 { 99 rc = RTSha256ToString(a uchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);93 rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1); 100 94 if (RT_SUCCESS(rc)) 101 95 *ppszDigest = pszDigest; … … 117 111 *ppszDigest = NULL; 118 112 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); 123 116 124 117 /* Open the file to calculate a SHA256 sum of */ … … 139 132 return rc; 140 133 } 141 rdMulti = 100.0 / cbFile;134 rdMulti = 100.0 / (cbFile ? cbFile : 1); 142 135 } 143 136 … … 153 146 154 147 /* Read that file in blocks */ 155 size_t cbRead;156 148 size_t cbReadTotal = 0; 157 149 for (;;) 158 150 { 151 size_t cbRead; 159 152 rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead); 160 153 if (RT_FAILURE(rc) || !cbRead) 161 154 break; 162 if(!SHA256_Update(&ctx, pvBuf, cbRead)) 163 { 164 rc = VERR_INTERNAL_ERROR; 165 break; 166 } 155 RTSha256Update(&Ctx, pvBuf, cbRead); 167 156 cbReadTotal += cbRead; 168 157 … … 182 171 183 172 /* 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); 187 175 188 176 char *pszDigest; … … 190 178 if (RT_SUCCESS(rc)) 191 179 { 192 rc = RTSha256ToString(a uchDig, pszDigest, RTSHA256_DIGEST_LEN + 1);180 rc = RTSha256ToString(abHash, pszDigest, RTSHA256_DIGEST_LEN + 1); 193 181 if (RT_SUCCESS(rc)) 194 182 *ppszDigest = pszDigest; -
trunk/src/VBox/Runtime/common/checksum/alt-md5.cpp
r51843 r51851 212 212 * initialization constants. 213 213 */ 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;214 RTDECL(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; 223 223 } 224 224 RT_EXPORT_SYMBOL(RTMd5Init); … … 229 229 * of bytes. 230 230 */ 231 RTDECL(void) RTMd5Update(PRTMD5CONTEXT ctx, const void *pvBuf, size_t len)231 RTDECL(void) RTMd5Update(PRTMD5CONTEXT pCtx, const void *pvBuf, size_t len) 232 232 { 233 233 const uint8_t *buf = (const uint8_t *)pvBuf; … … 235 235 236 236 /* 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); 241 241 242 242 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ … … 245 245 if (t) 246 246 { 247 uint8_t *p = (uint8_t *) ctx->in + t;247 uint8_t *p = (uint8_t *) pCtx->AltPrivate.in + t; 248 248 249 249 t = 64 - t; … … 254 254 } 255 255 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); 258 258 buf += t; 259 259 len -= t; … … 265 265 { 266 266 while (len >= 64) { 267 rtMd5Transform( ctx->buf, (uint32_t const *)buf);267 rtMd5Transform(pCtx->AltPrivate.buf, (uint32_t const *)buf); 268 268 buf += 64; 269 269 len -= 64; … … 274 274 { 275 275 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); 279 279 buf += 64; 280 280 len -= 64; … … 283 283 284 284 /* Handle any remaining bytes of data */ 285 memcpy( ctx->in, buf, len);285 memcpy(pCtx->AltPrivate.in, buf, len); 286 286 } 287 287 RT_EXPORT_SYMBOL(RTMd5Update); … … 292 292 * 1 0* (64-bit count of bits processed, MSB-first) 293 293 */ 294 RTDECL(void) RTMd5Final(uint8_t digest[16], PRTMD5CONTEXT ctx)294 RTDECL(void) RTMd5Final(uint8_t digest[16], PRTMD5CONTEXT pCtx) 295 295 { 296 296 unsigned int count; … … 298 298 299 299 /* Compute number of bytes mod 64 */ 300 count = ( ctx->bits[0] >> 3) & 0x3F;300 count = (pCtx->AltPrivate.bits[0] >> 3) & 0x3F; 301 301 302 302 /* Set the first char of padding to 0x80. This is safe since there is 303 303 always at least one byte free */ 304 p = (uint8_t *) ctx->in + count;304 p = (uint8_t *)pCtx->AltPrivate.in + count; 305 305 *p++ = 0x80; 306 306 … … 313 313 /* Two lots of padding: Pad the first block to 64 bytes */ 314 314 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); 317 317 318 318 /* Now fill the next block with 56 bytes */ 319 memset( ctx->in, 0, 56);319 memset(pCtx->AltPrivate.in, 0, 56); 320 320 } 321 321 else … … 324 324 memset(p, 0, count - 8); 325 325 } 326 rtMd5ByteReverse( ctx->in, 14);326 rtMd5ByteReverse(pCtx->AltPrivate.in, 14); 327 327 328 328 /* 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 */ 336 336 } 337 337 RT_EXPORT_SYMBOL(RTMd5Final);
Note:
See TracChangeset
for help on using the changeset viewer.