src/test/SDL_test_md5.c
changeset 6713 fa17a97389ed
child 6885 700f1b25f77f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/SDL_test_md5.c	Wed Nov 28 19:19:57 2012 -0800
     1.3 @@ -0,0 +1,336 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/*
    1.26 + ***********************************************************************
    1.27 + ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
    1.28 + ** Created: 2/17/90 RLR                                              **
    1.29 + ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. **
    1.30 + ***********************************************************************
    1.31 + */
    1.32 +
    1.33 +/*
    1.34 + ***********************************************************************
    1.35 + ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
    1.36 + **                                                                   **
    1.37 + ** License to copy and use this software is granted provided that    **
    1.38 + ** it is identified as the "RSA Data Security, Inc. MD5 Message-     **
    1.39 + ** Digest Algorithm" in all material mentioning or referencing this  **
    1.40 + ** software or this function.                                        **
    1.41 + **                                                                   **
    1.42 + ** License is also granted to make and use derivative works          **
    1.43 + ** provided that such works are identified as "derived from the RSA  **
    1.44 + ** Data Security, Inc. MD5 Message-Digest Algorithm" in all          **
    1.45 + ** material mentioning or referencing the derived work.              **
    1.46 + **                                                                   **
    1.47 + ** RSA Data Security, Inc. makes no representations concerning       **
    1.48 + ** either the merchantability of this software or the suitability    **
    1.49 + ** of this software for any particular purpose.  It is provided "as  **
    1.50 + ** is" without express or implied warranty of any kind.              **
    1.51 + **                                                                   **
    1.52 + ** These notices must be retained in any copies of any part of this  **
    1.53 + ** documentation and/or software.                                    **
    1.54 + ***********************************************************************
    1.55 + */
    1.56 +
    1.57 +#include "SDL_config.h"
    1.58 +
    1.59 +#include "SDL_test.h"
    1.60 +
    1.61 +/* Forward declaration of static helper function */
    1.62 +static void SDLTest_Md5Transform(MD5UINT4 * buf, MD5UINT4 * in);
    1.63 +
    1.64 +static unsigned char MD5PADDING[64] = {
    1.65 +  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.66 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.67 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.68 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.69 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.70 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.71 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    1.72 +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    1.73 +};
    1.74 +
    1.75 +/* F, G, H and I are basic MD5 functions */
    1.76 +#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
    1.77 +#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
    1.78 +#define H(x, y, z) ((x) ^ (y) ^ (z))
    1.79 +#define I(x, y, z) ((y) ^ ((x) | (~z)))
    1.80 +
    1.81 +/* ROTATE_LEFT rotates x left n bits */
    1.82 +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
    1.83 +
    1.84 +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
    1.85 +
    1.86 +/* Rotation is separate from addition to prevent recomputation */
    1.87 +#define FF(a, b, c, d, x, s, ac) \
    1.88 +  {(a) += F ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
    1.89 +   (a) = ROTATE_LEFT ((a), (s)); \
    1.90 +   (a) += (b); \
    1.91 +  }
    1.92 +#define GG(a, b, c, d, x, s, ac) \
    1.93 +  {(a) += G ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
    1.94 +   (a) = ROTATE_LEFT ((a), (s)); \
    1.95 +   (a) += (b); \
    1.96 +  }
    1.97 +#define HH(a, b, c, d, x, s, ac) \
    1.98 +  {(a) += H ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
    1.99 +   (a) = ROTATE_LEFT ((a), (s)); \
   1.100 +   (a) += (b); \
   1.101 +  }
   1.102 +#define II(a, b, c, d, x, s, ac) \
   1.103 +  {(a) += I ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
   1.104 +   (a) = ROTATE_LEFT ((a), (s)); \
   1.105 +   (a) += (b); \
   1.106 +  }
   1.107 +
   1.108 +/* 
   1.109 +  The routine MD5Init initializes the message-digest context
   1.110 +  mdContext. All fields are set to zero.
   1.111 +*/
   1.112 +
   1.113 +void SDLTest_Md5Init(SDLTest_Md5Context * mdContext)
   1.114 +{
   1.115 +  if (mdContext==NULL) return;
   1.116 +
   1.117 +  mdContext->i[0] = mdContext->i[1] = (MD5UINT4) 0;
   1.118 +
   1.119 +  /*
   1.120 +   * Load magic initialization constants.
   1.121 +   */
   1.122 +  mdContext->buf[0] = (MD5UINT4) 0x67452301;
   1.123 +  mdContext->buf[1] = (MD5UINT4) 0xefcdab89;
   1.124 +  mdContext->buf[2] = (MD5UINT4) 0x98badcfe;
   1.125 +  mdContext->buf[3] = (MD5UINT4) 0x10325476;
   1.126 +}
   1.127 +
   1.128 +/* 
   1.129 + The routine MD5Update updates the message-digest context to
   1.130 + account for the presence of each of the characters inBuf[0..inLen-1]
   1.131 + in the message whose digest is being computed.
   1.132 +*/
   1.133 +
   1.134 +void SDLTest_Md5Update(SDLTest_Md5Context * mdContext, unsigned char *inBuf,
   1.135 +		  unsigned int inLen)
   1.136 +{
   1.137 +  MD5UINT4  in[16];
   1.138 +  int       mdi;
   1.139 +  unsigned int i, ii;
   1.140 +
   1.141 +  if (mdContext == NULL) return;
   1.142 +  if (inBuf == NULL || inLen < 1) return;
   1.143 +
   1.144 +  /*
   1.145 +   * compute number of bytes mod 64 
   1.146 +   */
   1.147 +  mdi = (int) ((mdContext->i[0] >> 3) & 0x3F);
   1.148 +
   1.149 +  /*
   1.150 +   * update number of bits 
   1.151 +   */
   1.152 +  if ((mdContext->i[0] + ((MD5UINT4) inLen << 3)) < mdContext->i[0])
   1.153 +    mdContext->i[1]++;
   1.154 +  mdContext->i[0] += ((MD5UINT4) inLen << 3);
   1.155 +  mdContext->i[1] += ((MD5UINT4) inLen >> 29);
   1.156 +
   1.157 +  while (inLen--) {
   1.158 +    /*
   1.159 +     * add new character to buffer, increment mdi 
   1.160 +     */
   1.161 +    mdContext->in[mdi++] = *inBuf++;
   1.162 +
   1.163 +    /*
   1.164 +     * transform if necessary 
   1.165 +     */
   1.166 +    if (mdi == 0x40) {
   1.167 +      for (i = 0, ii = 0; i < 16; i++, ii += 4)
   1.168 +	in[i] = (((MD5UINT4) mdContext->in[ii + 3]) << 24) |
   1.169 +	  (((MD5UINT4) mdContext->in[ii + 2]) << 16) |
   1.170 +	  (((MD5UINT4) mdContext->in[ii + 1]) << 8) |
   1.171 +	  ((MD5UINT4) mdContext->in[ii]);
   1.172 +      SDLTest_Md5Transform(mdContext->buf, in);
   1.173 +      mdi = 0;
   1.174 +    }
   1.175 +  }
   1.176 +}
   1.177 +
   1.178 +/* 
   1.179 + The routine MD5Final terminates the message-digest computation and
   1.180 + ends with the desired message digest in mdContext->digest[0...15].
   1.181 +*/
   1.182 +
   1.183 +void SDLTest_Md5Final(SDLTest_Md5Context * mdContext)
   1.184 +{
   1.185 +  MD5UINT4  in[16];
   1.186 +  int       mdi;
   1.187 +  unsigned int i, ii;
   1.188 +  unsigned int padLen;
   1.189 +
   1.190 +  if (mdContext == NULL) return;
   1.191 +
   1.192 +  /*
   1.193 +   * save number of bits 
   1.194 +   */
   1.195 +  in[14] = mdContext->i[0];
   1.196 +  in[15] = mdContext->i[1];
   1.197 +
   1.198 +  /*
   1.199 +   * compute number of bytes mod 64 
   1.200 +   */
   1.201 +  mdi = (int) ((mdContext->i[0] >> 3) & 0x3F);
   1.202 +
   1.203 +  /*
   1.204 +   * pad out to 56 mod 64 
   1.205 +   */
   1.206 +  padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
   1.207 +  SDLTest_Md5Update(mdContext, MD5PADDING, padLen);
   1.208 +
   1.209 +  /*
   1.210 +   * append length in bits and transform 
   1.211 +   */
   1.212 +  for (i = 0, ii = 0; i < 14; i++, ii += 4)
   1.213 +    in[i] = (((MD5UINT4) mdContext->in[ii + 3]) << 24) |
   1.214 +      (((MD5UINT4) mdContext->in[ii + 2]) << 16) |
   1.215 +      (((MD5UINT4) mdContext->in[ii + 1]) << 8) |
   1.216 +      ((MD5UINT4) mdContext->in[ii]);
   1.217 +  SDLTest_Md5Transform(mdContext->buf, in);
   1.218 +
   1.219 +  /*
   1.220 +   * store buffer in digest 
   1.221 +   */
   1.222 +  for (i = 0, ii = 0; i < 4; i++, ii += 4) {
   1.223 +    mdContext->digest[ii] = (unsigned char) (mdContext->buf[i] & 0xFF);
   1.224 +    mdContext->digest[ii + 1] =
   1.225 +      (unsigned char) ((mdContext->buf[i] >> 8) & 0xFF);
   1.226 +    mdContext->digest[ii + 2] =
   1.227 +      (unsigned char) ((mdContext->buf[i] >> 16) & 0xFF);
   1.228 +    mdContext->digest[ii + 3] =
   1.229 +      (unsigned char) ((mdContext->buf[i] >> 24) & 0xFF);
   1.230 +  }
   1.231 +}
   1.232 +
   1.233 +/* Basic MD5 step. Transforms buf based on in.
   1.234 + */
   1.235 +static void SDLTest_Md5Transform(MD5UINT4 * buf, MD5UINT4 * in)
   1.236 +{
   1.237 +  MD5UINT4  a = buf[0], b = buf[1], c = buf[2], d = buf[3];
   1.238 +
   1.239 +  /*
   1.240 +   * Round 1 
   1.241 +   */
   1.242 +#define S11 7
   1.243 +#define S12 12
   1.244 +#define S13 17
   1.245 +#define S14 22
   1.246 +  FF(a, b, c, d, in[0], S11, 3614090360u);	/* 1 */
   1.247 +  FF(d, a, b, c, in[1], S12, 3905402710u);	/* 2 */
   1.248 +  FF(c, d, a, b, in[2], S13, 606105819u);	/* 3 */
   1.249 +  FF(b, c, d, a, in[3], S14, 3250441966u);	/* 4 */
   1.250 +  FF(a, b, c, d, in[4], S11, 4118548399u);	/* 5 */
   1.251 +  FF(d, a, b, c, in[5], S12, 1200080426u);	/* 6 */
   1.252 +  FF(c, d, a, b, in[6], S13, 2821735955u);	/* 7 */
   1.253 +  FF(b, c, d, a, in[7], S14, 4249261313u);	/* 8 */
   1.254 +  FF(a, b, c, d, in[8], S11, 1770035416u);	/* 9 */
   1.255 +  FF(d, a, b, c, in[9], S12, 2336552879u);	/* 10 */
   1.256 +  FF(c, d, a, b, in[10], S13, 4294925233u);	/* 11 */
   1.257 +  FF(b, c, d, a, in[11], S14, 2304563134u);	/* 12 */
   1.258 +  FF(a, b, c, d, in[12], S11, 1804603682u);	/* 13 */
   1.259 +  FF(d, a, b, c, in[13], S12, 4254626195u);	/* 14 */
   1.260 +  FF(c, d, a, b, in[14], S13, 2792965006u);	/* 15 */
   1.261 +  FF(b, c, d, a, in[15], S14, 1236535329u);	/* 16 */
   1.262 +
   1.263 +  /*
   1.264 +   * Round 2 
   1.265 +   */
   1.266 +#define S21 5
   1.267 +#define S22 9
   1.268 +#define S23 14
   1.269 +#define S24 20
   1.270 +  GG(a, b, c, d, in[1], S21, 4129170786u);	/* 17 */
   1.271 +  GG(d, a, b, c, in[6], S22, 3225465664u);	/* 18 */
   1.272 +  GG(c, d, a, b, in[11], S23, 643717713u);	/* 19 */
   1.273 +  GG(b, c, d, a, in[0], S24, 3921069994u);	/* 20 */
   1.274 +  GG(a, b, c, d, in[5], S21, 3593408605u);	/* 21 */
   1.275 +  GG(d, a, b, c, in[10], S22, 38016083u);	/* 22 */
   1.276 +  GG(c, d, a, b, in[15], S23, 3634488961u);	/* 23 */
   1.277 +  GG(b, c, d, a, in[4], S24, 3889429448u);	/* 24 */
   1.278 +  GG(a, b, c, d, in[9], S21, 568446438u);	/* 25 */
   1.279 +  GG(d, a, b, c, in[14], S22, 3275163606u);	/* 26 */
   1.280 +  GG(c, d, a, b, in[3], S23, 4107603335u);	/* 27 */
   1.281 +  GG(b, c, d, a, in[8], S24, 1163531501u);	/* 28 */
   1.282 +  GG(a, b, c, d, in[13], S21, 2850285829u);	/* 29 */
   1.283 +  GG(d, a, b, c, in[2], S22, 4243563512u);	/* 30 */
   1.284 +  GG(c, d, a, b, in[7], S23, 1735328473u);	/* 31 */
   1.285 +  GG(b, c, d, a, in[12], S24, 2368359562u);	/* 32 */
   1.286 +
   1.287 +  /*
   1.288 +   * Round 3 
   1.289 +   */
   1.290 +#define S31 4
   1.291 +#define S32 11
   1.292 +#define S33 16
   1.293 +#define S34 23
   1.294 +  HH(a, b, c, d, in[5], S31, 4294588738u);	/* 33 */
   1.295 +  HH(d, a, b, c, in[8], S32, 2272392833u);	/* 34 */
   1.296 +  HH(c, d, a, b, in[11], S33, 1839030562u);	/* 35 */
   1.297 +  HH(b, c, d, a, in[14], S34, 4259657740u);	/* 36 */
   1.298 +  HH(a, b, c, d, in[1], S31, 2763975236u);	/* 37 */
   1.299 +  HH(d, a, b, c, in[4], S32, 1272893353u);	/* 38 */
   1.300 +  HH(c, d, a, b, in[7], S33, 4139469664u);	/* 39 */
   1.301 +  HH(b, c, d, a, in[10], S34, 3200236656u);	/* 40 */
   1.302 +  HH(a, b, c, d, in[13], S31, 681279174u);	/* 41 */
   1.303 +  HH(d, a, b, c, in[0], S32, 3936430074u);	/* 42 */
   1.304 +  HH(c, d, a, b, in[3], S33, 3572445317u);	/* 43 */
   1.305 +  HH(b, c, d, a, in[6], S34, 76029189u);	/* 44 */
   1.306 +  HH(a, b, c, d, in[9], S31, 3654602809u);	/* 45 */
   1.307 +  HH(d, a, b, c, in[12], S32, 3873151461u);	/* 46 */
   1.308 +  HH(c, d, a, b, in[15], S33, 530742520u);	/* 47 */
   1.309 +  HH(b, c, d, a, in[2], S34, 3299628645u);	/* 48 */
   1.310 +
   1.311 +  /*
   1.312 +   * Round 4 
   1.313 +   */
   1.314 +#define S41 6
   1.315 +#define S42 10
   1.316 +#define S43 15
   1.317 +#define S44 21
   1.318 +  II(a, b, c, d, in[0], S41, 4096336452u);	/* 49 */
   1.319 +  II(d, a, b, c, in[7], S42, 1126891415u);	/* 50 */
   1.320 +  II(c, d, a, b, in[14], S43, 2878612391u);	/* 51 */
   1.321 +  II(b, c, d, a, in[5], S44, 4237533241u);	/* 52 */
   1.322 +  II(a, b, c, d, in[12], S41, 1700485571u);	/* 53 */
   1.323 +  II(d, a, b, c, in[3], S42, 2399980690u);	/* 54 */
   1.324 +  II(c, d, a, b, in[10], S43, 4293915773u);	/* 55 */
   1.325 +  II(b, c, d, a, in[1], S44, 2240044497u);	/* 56 */
   1.326 +  II(a, b, c, d, in[8], S41, 1873313359u);	/* 57 */
   1.327 +  II(d, a, b, c, in[15], S42, 4264355552u);	/* 58 */
   1.328 +  II(c, d, a, b, in[6], S43, 2734768916u);	/* 59 */
   1.329 +  II(b, c, d, a, in[13], S44, 1309151649u);	/* 60 */
   1.330 +  II(a, b, c, d, in[4], S41, 4149444226u);	/* 61 */
   1.331 +  II(d, a, b, c, in[11], S42, 3174756917u);	/* 62 */
   1.332 +  II(c, d, a, b, in[2], S43, 718787259u);	/* 63 */
   1.333 +  II(b, c, d, a, in[9], S44, 3951481745u);	/* 64 */
   1.334 +
   1.335 +  buf[0] += a;
   1.336 +  buf[1] += b;
   1.337 +  buf[2] += c;
   1.338 +  buf[3] += d;
   1.339 +}