alt-md5.cpp revision ed6697871be690c5826c252d598ea9965c2816b4
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * IPRT - MD5 message digest functions, alternative implementation.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * Copyright (C) 2006-2013 Oracle Corporation
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * available from http://www.virtualbox.org. This file is free software;
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * you can redistribute it and/or modify it under the terms of the GNU
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * General Public License (GPL) as published by the Free Software
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * The contents of this file may alternatively be used under the terms
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * of the Common Development and Distribution License Version 1.0
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * VirtualBox OSE distribution, in which case the provisions of the
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * CDDL are applicable instead of those of the GPL.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * You may elect to license modified versions of this file under the
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * terms and conditions of either the GPL or the CDDL or both.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync/* The code is virtually unchanged from the original version (see copyright
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * notice below). Most changes are related to the function names and data
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * types - in order to fit the code in the IPRT naming style. */
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * This code implements the MD5 message-digest algorithm.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * The algorithm is due to Ron Rivest. This code was
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * written by Colin Plumb in 1993, no copyright is claimed.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * This code is in the public domain; do with it what you wish.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * Equivalent code is available from RSA Data Security, Inc.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * This code has been tested against that, and is equivalent,
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * except that you don't need to include two pages of legalese
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * with every copy.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * To compute the message digest of a chunk of bytes, declare an
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * RTMD5CONTEXT structure, pass it to MD5Init, call MD5Update as
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * needed on buffers full of bytes, and then call MD5Final, which
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * will fill a supplied 16-byte array with the digest.
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync/*******************************************************************************
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync* Header Files *
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync*******************************************************************************/
14ebfaa1c57c3239ff757d0034253ff19647b276vboxsync# include <iprt/asm.h> /* RT_LE2H_U32 uses ASMByteSwapU32. */
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync/*******************************************************************************
14ebfaa1c57c3239ff757d0034253ff19647b276vboxsync* Defined Constants And Macros *
14ebfaa1c57c3239ff757d0034253ff19647b276vboxsync*******************************************************************************/
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync/* The four core functions - F1 is optimized somewhat */
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync/* #define F1(x, y, z) (x & y | ~x & z) */
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync# define F1(x, y, z) (z ^ (x & (y ^ z)))
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync# define F3(x, y, z) (x ^ y ^ z)
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync# define F4(x, y, z) (y ^ (x | ~z))
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync#else /* gcc 4.0.1 (x86) benefits from the explicitness of F1() here. */
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsyncDECL_FORCE_INLINE(uint32_t) F1(uint32_t x, uint32_t y, uint32_t z)
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync register uint32_t r = y ^ z;
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsyncDECL_FORCE_INLINE(uint32_t) F3(uint32_t x, uint32_t y, uint32_t z)
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync register uint32_t r = x ^ y;
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsyncDECL_FORCE_INLINE(uint32_t) F4(uint32_t x, uint32_t y, uint32_t z)
b41f65ee355ae1d4b24473c6d6d70c653497429avboxsync register uint32_t r = ~z;
7dfb99974324af3e0dbbae9902d016927e113925vboxsync/* This is the central step in the MD5 algorithm. */
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * The core of the MD5 algorithm, this alters an existing MD5 hash to reflect
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * the addition of 16 longwords of new data. RTMd5Update blocks the data and
5f076b675789f7dd23040096fa3fdc636bd4ea10vboxsync * converts bytes into longwords for this routine.
7dfb99974324af3e0dbbae9902d016927e113925vboxsyncstatic void rtMd5Transform(uint32_t buf[4], uint32_t const in[16])
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync /* fn, w, x, y, z, data, s) */
#ifdef RT_BIG_ENDIAN
uint32_t t;
t = *buf;
t = RT_LE2H_U32(t);
*buf = t;
buf++;
} while (--longs);
uint32_t t;
if (len < t)
buf += t;
len -= t;
#ifndef RT_BIG_ENDIAN
unsigned int count;
uint8_t *p;