<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>dbus-md5.c Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.2.15 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="modules.html">Modules</a> <a class="qindex" href="annotated.html">Data Structures</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Data Fields</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>dbus-md5.c</h1><div class="fragment"><pre>00001 <font class="comment">/* -*- mode: C; c-file-style: "gnu" -*- */</font> 00002 <font class="comment">/* dbus-md5.c md5 implementation (based on L Peter Deutsch implementation)</font> 00003 <font class="comment"> *</font> 00004 <font class="comment"> * Copyright (C) 2003 Red Hat Inc.</font> 00005 <font class="comment"> * Copyright (C) 1999, 2000 Aladdin Enterprises. All rights reserved.</font> 00006 <font class="comment"> *</font> 00007 <font class="comment"> * This software is provided 'as-is', without any express or implied</font> 00008 <font class="comment"> * warranty. In no event will the authors be held liable for any damages</font> 00009 <font class="comment"> * arising from the use of this software.</font> 00010 <font class="comment"> *</font> 00011 <font class="comment"> * Permission is granted to anyone to use this software for any purpose,</font> 00012 <font class="comment"> * including commercial applications, and to alter it and redistribute it</font> 00013 <font class="comment"> * freely, subject to the following restrictions:</font> 00014 <font class="comment"> *</font> 00015 <font class="comment"> * 1. The origin of this software must not be misrepresented; you must not</font> 00016 <font class="comment"> * claim that you wrote the original software. If you use this software</font> 00017 <font class="comment"> * in a product, an acknowledgment in the product documentation would be</font> 00018 <font class="comment"> * appreciated but is not required.</font> 00019 <font class="comment"> * 2. Altered source versions must be plainly marked as such, and must not be</font> 00020 <font class="comment"> * misrepresented as being the original software.</font> 00021 <font class="comment"> * 3. This notice may not be removed or altered from any source distribution.</font> 00022 <font class="comment"> *</font> 00023 <font class="comment"> * L. Peter Deutsch</font> 00024 <font class="comment"> * ghost@aladdin.com</font> 00025 <font class="comment"> */</font> 00026 <font class="comment">/*</font> 00027 <font class="comment"> * Independent implementation of MD5 (RFC 1321).</font> 00028 <font class="comment"> *</font> 00029 <font class="comment"> * This code implements the MD5 Algorithm defined in RFC 1321.</font> 00030 <font class="comment"> * It is derived directly from the text of the RFC and not from the</font> 00031 <font class="comment"> * reference implementation.</font> 00032 <font class="comment"> *</font> 00033 <font class="comment"> * The original and principal author of md5.c is L. Peter Deutsch</font> 00034 <font class="comment"> * <ghost@aladdin.com>.</font> 00035 <font class="comment"> */</font> 00036 00037 <font class="preprocessor">#include "dbus-internals.h"</font> 00038 <font class="preprocessor">#include "dbus-md5.h"</font> 00039 <font class="preprocessor">#include <string.h></font> 00040 00062 <font class="preprocessor">#ifndef DOXYGEN_SHOULD_SKIP_THIS</font> 00063 <font class="preprocessor"></font><font class="comment">/*</font> 00064 <font class="comment"> * For reference, here is the program that computed the T values.</font> 00065 <font class="comment"> */</font> 00066 <font class="preprocessor">#ifdef COMPUTE_T_VALUES</font> 00067 <font class="preprocessor"></font><font class="preprocessor">#include <math.h></font> 00068 <font class="keywordtype">int</font> 00069 main(<font class="keywordtype">int</font> argc, <font class="keywordtype">char</font> **argv) 00070 { 00071 <font class="keywordtype">int</font> i; 00072 <font class="keywordflow">for</font> (i = 1; i <= 64; ++i) 00073 { 00074 <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> v = (<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>)(4294967296.0 * fabs(sin((<font class="keywordtype">double</font>)i))); 00075 00076 <font class="comment">/*</font> 00077 <font class="comment"> * The following nonsense is only to avoid compiler warnings about</font> 00078 <font class="comment"> * "integer constant is unsigned in ANSI C, signed with -traditional".</font> 00079 <font class="comment"> */</font> 00080 <font class="keywordflow">if</font> (v >> 31) 00081 { 00082 printf(<font class="stringliteral">"#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n"</font>, i, 00083 v, (<font class="keywordtype">unsigned</font> <font class="keywordtype">long</font>)(<font class="keywordtype">unsigned</font> <font class="keywordtype">int</font>)(~v)); 00084 } <font class="keywordflow">else</font> { 00085 printf(<font class="stringliteral">"#define T%d 0x%08lx\n"</font>, i, v); 00086 } 00087 } 00088 <font class="keywordflow">return</font> 0; 00089 } 00090 <font class="preprocessor">#endif </font><font class="comment">/* COMPUTE_T_VALUES */</font> 00091 <font class="comment">/*</font> 00092 <font class="comment"> * End of T computation program.</font> 00093 <font class="comment"> */</font> 00094 00095 <font class="preprocessor">#define T_MASK ((dbus_uint32_t)~0)</font> 00096 <font class="preprocessor"></font><font class="preprocessor">#define T1 </font><font class="comment">/* 0xd76aa478 */</font> (T_MASK ^ 0x28955b87) 00097 <font class="preprocessor">#define T2 </font><font class="comment">/* 0xe8c7b756 */</font> (T_MASK ^ 0x173848a9) 00098 <font class="preprocessor">#define T3 0x242070db</font> 00099 <font class="preprocessor"></font><font class="preprocessor">#define T4 </font><font class="comment">/* 0xc1bdceee */</font> (T_MASK ^ 0x3e423111) 00100 <font class="preprocessor">#define T5 </font><font class="comment">/* 0xf57c0faf */</font> (T_MASK ^ 0x0a83f050) 00101 <font class="preprocessor">#define T6 0x4787c62a</font> 00102 <font class="preprocessor"></font><font class="preprocessor">#define T7 </font><font class="comment">/* 0xa8304613 */</font> (T_MASK ^ 0x57cfb9ec) 00103 <font class="preprocessor">#define T8 </font><font class="comment">/* 0xfd469501 */</font> (T_MASK ^ 0x02b96afe) 00104 <font class="preprocessor">#define T9 0x698098d8</font> 00105 <font class="preprocessor"></font><font class="preprocessor">#define T10 </font><font class="comment">/* 0x8b44f7af */</font> (T_MASK ^ 0x74bb0850) 00106 <font class="preprocessor">#define T11 </font><font class="comment">/* 0xffff5bb1 */</font> (T_MASK ^ 0x0000a44e) 00107 <font class="preprocessor">#define T12 </font><font class="comment">/* 0x895cd7be */</font> (T_MASK ^ 0x76a32841) 00108 <font class="preprocessor">#define T13 0x6b901122</font> 00109 <font class="preprocessor"></font><font class="preprocessor">#define T14 </font><font class="comment">/* 0xfd987193 */</font> (T_MASK ^ 0x02678e6c) 00110 <font class="preprocessor">#define T15 </font><font class="comment">/* 0xa679438e */</font> (T_MASK ^ 0x5986bc71) 00111 <font class="preprocessor">#define T16 0x49b40821</font> 00112 <font class="preprocessor"></font><font class="preprocessor">#define T17 </font><font class="comment">/* 0xf61e2562 */</font> (T_MASK ^ 0x09e1da9d) 00113 <font class="preprocessor">#define T18 </font><font class="comment">/* 0xc040b340 */</font> (T_MASK ^ 0x3fbf4cbf) 00114 <font class="preprocessor">#define T19 0x265e5a51</font> 00115 <font class="preprocessor"></font><font class="preprocessor">#define T20 </font><font class="comment">/* 0xe9b6c7aa */</font> (T_MASK ^ 0x16493855) 00116 <font class="preprocessor">#define T21 </font><font class="comment">/* 0xd62f105d */</font> (T_MASK ^ 0x29d0efa2) 00117 <font class="preprocessor">#define T22 0x02441453</font> 00118 <font class="preprocessor"></font><font class="preprocessor">#define T23 </font><font class="comment">/* 0xd8a1e681 */</font> (T_MASK ^ 0x275e197e) 00119 <font class="preprocessor">#define T24 </font><font class="comment">/* 0xe7d3fbc8 */</font> (T_MASK ^ 0x182c0437) 00120 <font class="preprocessor">#define T25 0x21e1cde6</font> 00121 <font class="preprocessor"></font><font class="preprocessor">#define T26 </font><font class="comment">/* 0xc33707d6 */</font> (T_MASK ^ 0x3cc8f829) 00122 <font class="preprocessor">#define T27 </font><font class="comment">/* 0xf4d50d87 */</font> (T_MASK ^ 0x0b2af278) 00123 <font class="preprocessor">#define T28 0x455a14ed</font> 00124 <font class="preprocessor"></font><font class="preprocessor">#define T29 </font><font class="comment">/* 0xa9e3e905 */</font> (T_MASK ^ 0x561c16fa) 00125 <font class="preprocessor">#define T30 </font><font class="comment">/* 0xfcefa3f8 */</font> (T_MASK ^ 0x03105c07) 00126 <font class="preprocessor">#define T31 0x676f02d9</font> 00127 <font class="preprocessor"></font><font class="preprocessor">#define T32 </font><font class="comment">/* 0x8d2a4c8a */</font> (T_MASK ^ 0x72d5b375) 00128 <font class="preprocessor">#define T33 </font><font class="comment">/* 0xfffa3942 */</font> (T_MASK ^ 0x0005c6bd) 00129 <font class="preprocessor">#define T34 </font><font class="comment">/* 0x8771f681 */</font> (T_MASK ^ 0x788e097e) 00130 <font class="preprocessor">#define T35 0x6d9d6122</font> 00131 <font class="preprocessor"></font><font class="preprocessor">#define T36 </font><font class="comment">/* 0xfde5380c */</font> (T_MASK ^ 0x021ac7f3) 00132 <font class="preprocessor">#define T37 </font><font class="comment">/* 0xa4beea44 */</font> (T_MASK ^ 0x5b4115bb) 00133 <font class="preprocessor">#define T38 0x4bdecfa9</font> 00134 <font class="preprocessor"></font><font class="preprocessor">#define T39 </font><font class="comment">/* 0xf6bb4b60 */</font> (T_MASK ^ 0x0944b49f) 00135 <font class="preprocessor">#define T40 </font><font class="comment">/* 0xbebfbc70 */</font> (T_MASK ^ 0x4140438f) 00136 <font class="preprocessor">#define T41 0x289b7ec6</font> 00137 <font class="preprocessor"></font><font class="preprocessor">#define T42 </font><font class="comment">/* 0xeaa127fa */</font> (T_MASK ^ 0x155ed805) 00138 <font class="preprocessor">#define T43 </font><font class="comment">/* 0xd4ef3085 */</font> (T_MASK ^ 0x2b10cf7a) 00139 <font class="preprocessor">#define T44 0x04881d05</font> 00140 <font class="preprocessor"></font><font class="preprocessor">#define T45 </font><font class="comment">/* 0xd9d4d039 */</font> (T_MASK ^ 0x262b2fc6) 00141 <font class="preprocessor">#define T46 </font><font class="comment">/* 0xe6db99e5 */</font> (T_MASK ^ 0x1924661a) 00142 <font class="preprocessor">#define T47 0x1fa27cf8</font> 00143 <font class="preprocessor"></font><font class="preprocessor">#define T48 </font><font class="comment">/* 0xc4ac5665 */</font> (T_MASK ^ 0x3b53a99a) 00144 <font class="preprocessor">#define T49 </font><font class="comment">/* 0xf4292244 */</font> (T_MASK ^ 0x0bd6ddbb) 00145 <font class="preprocessor">#define T50 0x432aff97</font> 00146 <font class="preprocessor"></font><font class="preprocessor">#define T51 </font><font class="comment">/* 0xab9423a7 */</font> (T_MASK ^ 0x546bdc58) 00147 <font class="preprocessor">#define T52 </font><font class="comment">/* 0xfc93a039 */</font> (T_MASK ^ 0x036c5fc6) 00148 <font class="preprocessor">#define T53 0x655b59c3</font> 00149 <font class="preprocessor"></font><font class="preprocessor">#define T54 </font><font class="comment">/* 0x8f0ccc92 */</font> (T_MASK ^ 0x70f3336d) 00150 <font class="preprocessor">#define T55 </font><font class="comment">/* 0xffeff47d */</font> (T_MASK ^ 0x00100b82) 00151 <font class="preprocessor">#define T56 </font><font class="comment">/* 0x85845dd1 */</font> (T_MASK ^ 0x7a7ba22e) 00152 <font class="preprocessor">#define T57 0x6fa87e4f</font> 00153 <font class="preprocessor"></font><font class="preprocessor">#define T58 </font><font class="comment">/* 0xfe2ce6e0 */</font> (T_MASK ^ 0x01d3191f) 00154 <font class="preprocessor">#define T59 </font><font class="comment">/* 0xa3014314 */</font> (T_MASK ^ 0x5cfebceb) 00155 <font class="preprocessor">#define T60 0x4e0811a1</font> 00156 <font class="preprocessor"></font><font class="preprocessor">#define T61 </font><font class="comment">/* 0xf7537e82 */</font> (T_MASK ^ 0x08ac817d) 00157 <font class="preprocessor">#define T62 </font><font class="comment">/* 0xbd3af235 */</font> (T_MASK ^ 0x42c50dca) 00158 <font class="preprocessor">#define T63 0x2ad7d2bb</font> 00159 <font class="preprocessor"></font><font class="preprocessor">#define T64 </font><font class="comment">/* 0xeb86d391 */</font> (T_MASK ^ 0x14792c6e) 00160 <font class="preprocessor">#endif </font><font class="comment">/* DOXYGEN_SHOULD_SKIP_THIS */</font> 00161 00162 <font class="keyword">static</font> <font class="keywordtype">void</font> 00163 md5_process(<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context, <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *data <font class="comment">/*[64]*/</font>) 00164 { 00165 dbus_uint32_t 00166 a = context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[0], b = context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[1], 00167 c = context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[2], d = context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[3]; 00168 dbus_uint32_t t; 00169 00170 <font class="preprocessor">#ifdef WORDS_BIGENDIAN</font> 00171 <font class="preprocessor"></font> <font class="comment">/*</font> 00172 <font class="comment"> * On big-endian machines, we must arrange the bytes in the right</font> 00173 <font class="comment"> * order. (This also works on machines of unknown byte order.)</font> 00174 <font class="comment"> */</font> 00175 dbus_uint32_t X[16]; 00176 <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *xp = data; 00177 <font class="keywordtype">int</font> i; 00178 00179 <font class="keywordflow">for</font> (i = 0; i < 16; ++i, xp += 4) 00180 X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); 00181 00182 <font class="preprocessor">#else </font><font class="comment">/* !WORDS_BIGENDIAN */</font> 00183 <font class="comment">/*</font> 00184 <font class="comment"> * On little-endian machines, we can process properly aligned data</font> 00185 <font class="comment"> * without copying it.</font> 00186 <font class="comment"> */</font> 00187 dbus_uint32_t xbuf[16]; 00188 <font class="keyword">const</font> dbus_uint32_t *X; 00189 00190 <font class="keywordflow">if</font> (!((data - (<font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *)0) & 3)) 00191 { 00192 <font class="comment">/* data are properly aligned */</font> 00193 X = (<font class="keyword">const</font> dbus_uint32_t *)data; 00194 } 00195 <font class="keywordflow">else</font> 00196 { 00197 <font class="comment">/* not aligned */</font> 00198 memcpy(xbuf, data, 64); 00199 X = xbuf; 00200 } 00201 <font class="preprocessor">#endif</font> 00202 <font class="preprocessor"></font> 00203 <font class="preprocessor">#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))</font> 00204 <font class="preprocessor"></font> 00205 <font class="comment">/* Round 1. */</font> 00206 <font class="comment">/* Let [abcd k s i] denote the operation</font> 00207 <font class="comment"> a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */</font> 00208 <font class="preprocessor">#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))</font> 00209 <font class="preprocessor"></font><font class="preprocessor">#define SET(a, b, c, d, k, s, Ti) \</font> 00210 <font class="preprocessor"> t = a + F(b,c,d) + X[k] + Ti; \</font> 00211 <font class="preprocessor"> a = ROTATE_LEFT(t, s) + b</font> 00212 <font class="preprocessor"></font> <font class="comment">/* Do the following 16 operations. */</font> 00213 SET(a, b, c, d, 0, 7, T1); 00214 SET(d, a, b, c, 1, 12, T2); 00215 SET(c, d, a, b, 2, 17, T3); 00216 SET(b, c, d, a, 3, 22, T4); 00217 SET(a, b, c, d, 4, 7, T5); 00218 SET(d, a, b, c, 5, 12, T6); 00219 SET(c, d, a, b, 6, 17, T7); 00220 SET(b, c, d, a, 7, 22, T8); 00221 SET(a, b, c, d, 8, 7, T9); 00222 SET(d, a, b, c, 9, 12, T10); 00223 SET(c, d, a, b, 10, 17, T11); 00224 SET(b, c, d, a, 11, 22, T12); 00225 SET(a, b, c, d, 12, 7, T13); 00226 SET(d, a, b, c, 13, 12, T14); 00227 SET(c, d, a, b, 14, 17, T15); 00228 SET(b, c, d, a, 15, 22, T16); 00229 <font class="preprocessor">#undef SET</font> 00230 <font class="preprocessor"></font> 00231 <font class="comment">/* Round 2. */</font> 00232 <font class="comment">/* Let [abcd k s i] denote the operation</font> 00233 <font class="comment"> a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */</font> 00234 <font class="preprocessor">#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))</font> 00235 <font class="preprocessor"></font><font class="preprocessor">#define SET(a, b, c, d, k, s, Ti) \</font> 00236 <font class="preprocessor"> t = a + G(b,c,d) + X[k] + Ti; \</font> 00237 <font class="preprocessor"> a = ROTATE_LEFT(t, s) + b</font> 00238 <font class="preprocessor"></font> <font class="comment">/* Do the following 16 operations. */</font> 00239 SET(a, b, c, d, 1, 5, T17); 00240 SET(d, a, b, c, 6, 9, T18); 00241 SET(c, d, a, b, 11, 14, T19); 00242 SET(b, c, d, a, 0, 20, T20); 00243 SET(a, b, c, d, 5, 5, T21); 00244 SET(d, a, b, c, 10, 9, T22); 00245 SET(c, d, a, b, 15, 14, T23); 00246 SET(b, c, d, a, 4, 20, T24); 00247 SET(a, b, c, d, 9, 5, T25); 00248 SET(d, a, b, c, 14, 9, T26); 00249 SET(c, d, a, b, 3, 14, T27); 00250 SET(b, c, d, a, 8, 20, T28); 00251 SET(a, b, c, d, 13, 5, T29); 00252 SET(d, a, b, c, 2, 9, T30); 00253 SET(c, d, a, b, 7, 14, T31); 00254 SET(b, c, d, a, 12, 20, T32); 00255 <font class="preprocessor">#undef SET</font> 00256 <font class="preprocessor"></font> 00257 <font class="comment">/* Round 3. */</font> 00258 <font class="comment">/* Let [abcd k s t] denote the operation</font> 00259 <font class="comment"> a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */</font> 00260 <font class="preprocessor">#define H(x, y, z) ((x) ^ (y) ^ (z))</font> 00261 <font class="preprocessor"></font><font class="preprocessor">#define SET(a, b, c, d, k, s, Ti) \</font> 00262 <font class="preprocessor"> t = a + H(b,c,d) + X[k] + Ti; \</font> 00263 <font class="preprocessor"> a = ROTATE_LEFT(t, s) + b</font> 00264 <font class="preprocessor"></font> <font class="comment">/* Do the following 16 operations. */</font> 00265 SET(a, b, c, d, 5, 4, T33); 00266 SET(d, a, b, c, 8, 11, T34); 00267 SET(c, d, a, b, 11, 16, T35); 00268 SET(b, c, d, a, 14, 23, T36); 00269 SET(a, b, c, d, 1, 4, T37); 00270 SET(d, a, b, c, 4, 11, T38); 00271 SET(c, d, a, b, 7, 16, T39); 00272 SET(b, c, d, a, 10, 23, T40); 00273 SET(a, b, c, d, 13, 4, T41); 00274 SET(d, a, b, c, 0, 11, T42); 00275 SET(c, d, a, b, 3, 16, T43); 00276 SET(b, c, d, a, 6, 23, T44); 00277 SET(a, b, c, d, 9, 4, T45); 00278 SET(d, a, b, c, 12, 11, T46); 00279 SET(c, d, a, b, 15, 16, T47); 00280 SET(b, c, d, a, 2, 23, T48); 00281 <font class="preprocessor">#undef SET</font> 00282 <font class="preprocessor"></font> 00283 <font class="comment">/* Round 4. */</font> 00284 <font class="comment">/* Let [abcd k s t] denote the operation</font> 00285 <font class="comment"> a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */</font> 00286 <font class="preprocessor">#define I(x, y, z) ((y) ^ ((x) | ~(z)))</font> 00287 <font class="preprocessor"></font><font class="preprocessor">#define SET(a, b, c, d, k, s, Ti) \</font> 00288 <font class="preprocessor"> t = a + I(b,c,d) + X[k] + Ti; \</font> 00289 <font class="preprocessor"> a = ROTATE_LEFT(t, s) + b</font> 00290 <font class="preprocessor"></font> <font class="comment">/* Do the following 16 operations. */</font> 00291 SET(a, b, c, d, 0, 6, T49); 00292 SET(d, a, b, c, 7, 10, T50); 00293 SET(c, d, a, b, 14, 15, T51); 00294 SET(b, c, d, a, 5, 21, T52); 00295 SET(a, b, c, d, 12, 6, T53); 00296 SET(d, a, b, c, 3, 10, T54); 00297 SET(c, d, a, b, 10, 15, T55); 00298 SET(b, c, d, a, 1, 21, T56); 00299 SET(a, b, c, d, 8, 6, T57); 00300 SET(d, a, b, c, 15, 10, T58); 00301 SET(c, d, a, b, 6, 15, T59); 00302 SET(b, c, d, a, 13, 21, T60); 00303 SET(a, b, c, d, 4, 6, T61); 00304 SET(d, a, b, c, 11, 10, T62); 00305 SET(c, d, a, b, 2, 15, T63); 00306 SET(b, c, d, a, 9, 21, T64); 00307 <font class="preprocessor">#undef SET</font> 00308 <font class="preprocessor"></font> 00309 <font class="comment">/* Then perform the following additions. (That is increment each</font> 00310 <font class="comment"> of the four registers by the value it had before this block</font> 00311 <font class="comment"> was started.) */</font> 00312 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[0] += a; 00313 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[1] += b; 00314 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[2] += c; 00315 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[3] += d; 00316 } 00317 00318 <font class="keyword">static</font> <font class="keywordtype">void</font> 00319 md5_init (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context) 00320 { 00321 context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[0] = context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[1] = 0; 00322 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[0] = 0x67452301; 00323 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[1] = <font class="comment">/*0xefcdab89*/</font> T_MASK ^ 0x10325476; 00324 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[2] = <font class="comment">/*0x98badcfe*/</font> T_MASK ^ 0x67452301; 00325 context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[3] = 0x10325476; 00326 } 00327 00328 <font class="keyword">static</font> <font class="keywordtype">void</font> 00329 md5_append (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context, <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *data, <font class="keywordtype">int</font> nbytes) 00330 { 00331 <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *p = data; 00332 <font class="keywordtype">int</font> left = nbytes; 00333 <font class="keywordtype">int</font> offset = (context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[0] >> 3) & 63; 00334 dbus_uint32_t nbits = (dbus_uint32_t)(nbytes << 3); 00335 00336 <font class="keywordflow">if</font> (nbytes <= 0) 00337 <font class="keywordflow">return</font>; 00338 00339 <font class="comment">/* Update the message length. */</font> 00340 context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[1] += nbytes >> 29; 00341 context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[0] += nbits; 00342 <font class="keywordflow">if</font> (context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[0] < nbits) 00343 context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[1]++; 00344 00345 <font class="comment">/* Process an initial partial block. */</font> 00346 <font class="keywordflow">if</font> (offset) 00347 { 00348 <font class="keywordtype">int</font> copy = (offset + nbytes > 64 ? 64 - offset : nbytes); 00349 00350 memcpy(context-><a class="code" href="structDBusMD5Context.html#m2">buf</a> + offset, p, copy); 00351 <font class="keywordflow">if</font> (offset + copy < 64) 00352 <font class="keywordflow">return</font>; 00353 p += copy; 00354 left -= copy; 00355 md5_process(context, context-><a class="code" href="structDBusMD5Context.html#m2">buf</a>); 00356 } 00357 00358 <font class="comment">/* Process full blocks. */</font> 00359 <font class="keywordflow">for</font> (; left >= 64; p += 64, left -= 64) 00360 md5_process(context, p); 00361 00362 <font class="comment">/* Process a final partial block. */</font> 00363 <font class="keywordflow">if</font> (left) 00364 memcpy(context-><a class="code" href="structDBusMD5Context.html#m2">buf</a>, p, left); 00365 } 00366 00367 <font class="keyword">static</font> <font class="keywordtype">void</font> 00368 md5_finish (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context, <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> digest[16]) 00369 { 00370 <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> pad[64] = { 00371 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00372 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00373 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 00374 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 00375 }; 00376 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> data[8]; 00377 <font class="keywordtype">int</font> i; 00378 00379 <font class="comment">/* Save the length before padding. */</font> 00380 <font class="keywordflow">for</font> (i = 0; i < 8; ++i) 00381 data[i] = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>)(context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[i >> 2] >> ((i & 3) << 3)); 00382 <font class="comment">/* Pad to 56 bytes mod 64. */</font> 00383 md5_append(context, pad, ((55 - (context-><a class="code" href="structDBusMD5Context.html#m0">count</a>[0] >> 3)) & 63) + 1); 00384 <font class="comment">/* Append the length. */</font> 00385 md5_append(context, data, 8); 00386 <font class="keywordflow">for</font> (i = 0; i < 16; ++i) 00387 digest[i] = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>)(context-><a class="code" href="structDBusMD5Context.html#m1">abcd</a>[i >> 2] >> ((i & 3) << 3)); 00388 } 00389 <font class="comment">/* End of internals */</font> 00391 00403 <font class="keywordtype">void</font> <a name="l00404"></a><a class="code" href="group__DBusMD5.html#a0">00404</a> _dbus_md5_init (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context) 00405 { 00406 md5_init (context); 00407 } 00408 00409 00416 <font class="keywordtype">void</font> <a name="l00417"></a><a class="code" href="group__DBusMD5.html#a1">00417</a> _dbus_md5_update (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context, 00418 <font class="keyword">const</font> <a class="code" href="structDBusString.html">DBusString</a> *data) 00419 { 00420 <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> inputLen; 00421 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *input; 00422 00423 _dbus_string_get_const_data (data, (<font class="keyword">const</font> <font class="keywordtype">char</font>**) &input); 00424 inputLen = _dbus_string_get_length (data); 00425 00426 md5_append (context, input, inputLen); 00427 } 00428 00440 dbus_bool_t <a name="l00441"></a><a class="code" href="group__DBusMD5.html#a2">00441</a> _dbus_md5_final (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> *context, 00442 <a class="code" href="structDBusString.html">DBusString</a> *results) 00443 { 00444 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> digest[16]; 00445 00446 md5_finish (context, digest); 00447 00448 <font class="keywordflow">if</font> (!_dbus_string_append_len (results, digest, 16)) 00449 <font class="keywordflow">return</font> FALSE; 00450 00451 <font class="comment">/* some kind of security paranoia, though it seems pointless</font> 00452 <font class="comment"> * to me given the nonzeroed stuff flying around</font> 00453 <font class="comment"> */</font> 00454 memset ((<font class="keywordtype">void</font>*)context, <font class="charliteral">'\0'</font>, <font class="keyword">sizeof</font> (<a class="code" href="structDBusMD5Context.html">DBusMD5Context</a>)); 00455 00456 <font class="keywordflow">return</font> TRUE; 00457 } 00458 00467 dbus_bool_t <a name="l00468"></a><a class="code" href="group__DBusMD5.html#a3">00468</a> _dbus_md5_compute (<font class="keyword">const</font> <a class="code" href="structDBusString.html">DBusString</a> *data, 00469 <a class="code" href="structDBusString.html">DBusString</a> *ascii_output) 00470 { 00471 <a class="code" href="structDBusMD5Context.html">DBusMD5Context</a> context; 00472 <a class="code" href="structDBusString.html">DBusString</a> digest; 00473 00474 _dbus_md5_init (&context); 00475 00476 _dbus_md5_update (&context, data); 00477 00478 <font class="keywordflow">if</font> (!_dbus_string_init (&digest)) 00479 <font class="keywordflow">return</font> FALSE; 00480 00481 <font class="keywordflow">if</font> (!_dbus_md5_final (&context, &digest)) 00482 <font class="keywordflow">goto</font> error; 00483 00484 <font class="keywordflow">if</font> (!_dbus_string_hex_encode (&digest, 0, ascii_output, 00485 _dbus_string_get_length (ascii_output))) 00486 <font class="keywordflow">goto</font> error; 00487 00488 _dbus_string_free (&digest); 00489 00490 <font class="keywordflow">return</font> TRUE; 00491 00492 error: 00493 _dbus_string_free (&digest); 00494 <font class="keywordflow">return</font> FALSE; 00495 } 00496 <font class="comment">/* end of exported functions */</font> 00498 00499 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font> 00500 <font class="preprocessor"></font><font class="preprocessor">#include "dbus-test.h"</font> 00501 <font class="preprocessor">#include <stdio.h></font> 00502 00503 <font class="keyword">static</font> dbus_bool_t 00504 check_md5_binary (<font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *input, 00505 <font class="keywordtype">int</font> input_len, 00506 <font class="keyword">const</font> <font class="keywordtype">char</font> *expected) 00507 { 00508 <a class="code" href="structDBusString.html">DBusString</a> input_str; 00509 <a class="code" href="structDBusString.html">DBusString</a> expected_str; 00510 <a class="code" href="structDBusString.html">DBusString</a> results; 00511 00512 _dbus_string_init_const_len (&input_str, input, input_len); 00513 _dbus_string_init_const (&expected_str, expected); 00514 00515 <font class="keywordflow">if</font> (!_dbus_string_init (&results)) 00516 _dbus_assert_not_reached (<font class="stringliteral">"no memory for md5 results"</font>); 00517 00518 <font class="keywordflow">if</font> (!_dbus_md5_compute (&input_str, &results)) 00519 _dbus_assert_not_reached (<font class="stringliteral">"no memory for md5 results"</font>); 00520 00521 <font class="keywordflow">if</font> (!_dbus_string_equal (&expected_str, &results)) 00522 { 00523 <font class="keyword">const</font> <font class="keywordtype">char</font> *s; 00524 _dbus_string_get_const_data (&results, &s); 00525 _dbus_warn (<font class="stringliteral">"Expected hash %s got %s for md5 sum\n"</font>, 00526 expected, s); 00527 _dbus_string_free (&results); 00528 <font class="keywordflow">return</font> FALSE; 00529 } 00530 00531 _dbus_string_free (&results); 00532 <font class="keywordflow">return</font> TRUE; 00533 } 00534 00535 <font class="keyword">static</font> dbus_bool_t 00536 check_md5_str (<font class="keyword">const</font> <font class="keywordtype">char</font> *input, 00537 <font class="keyword">const</font> <font class="keywordtype">char</font> *expected) 00538 { 00539 <font class="keywordflow">return</font> check_md5_binary (input, strlen (input), expected); 00540 } 00541 00548 dbus_bool_t <a name="l00549"></a><a class="code" href="group__DBusMD5Internals.html#a4">00549</a> _dbus_md5_test (<font class="keywordtype">void</font>) 00550 { 00551 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> all_bytes[256]; 00552 <font class="keywordtype">int</font> i; 00553 00554 i = 0; 00555 <font class="keywordflow">while</font> (i < 256) 00556 { 00557 all_bytes[i] = i; 00558 ++i; 00559 } 00560 00561 <font class="keywordflow">if</font> (!check_md5_binary (all_bytes, 256, 00562 <font class="stringliteral">"e2c865db4162bed963bfaa9ef6ac18f0"</font>)) 00563 <font class="keywordflow">return</font> FALSE; 00564 00565 <font class="preprocessor">#define CHECK(input,expected) if (!check_md5_str (input, expected)) return FALSE</font> 00566 <font class="preprocessor"></font> 00567 CHECK (<font class="stringliteral">""</font>, <font class="stringliteral">"d41d8cd98f00b204e9800998ecf8427e"</font>); 00568 CHECK (<font class="stringliteral">"a"</font>, <font class="stringliteral">"0cc175b9c0f1b6a831c399e269772661"</font>); 00569 CHECK (<font class="stringliteral">"abc"</font>, <font class="stringliteral">"900150983cd24fb0d6963f7d28e17f72"</font>); 00570 CHECK (<font class="stringliteral">"message digest"</font>, <font class="stringliteral">"f96b697d7cb7938d525a2f31aaf161d0"</font>); 00571 CHECK (<font class="stringliteral">"abcdefghijklmnopqrstuvwxyz"</font>, <font class="stringliteral">"c3fcd3d76192e4007dfb496cca67e13b"</font>); 00572 CHECK (<font class="stringliteral">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"</font>, 00573 <font class="stringliteral">"d174ab98d277d9f5a5611c2c9f419d9f"</font>); 00574 CHECK (<font class="stringliteral">"12345678901234567890123456789012345678901234567890123456789012345678901234567890"</font>, 00575 <font class="stringliteral">"57edf4a22be3c955ac49da2e2107b67a"</font>); 00576 00577 <font class="keywordflow">return</font> TRUE; 00578 } 00579 00580 <font class="preprocessor">#endif </font><font class="comment">/* DBUS_BUILD_TESTS */</font> </pre></div><hr><address align="right"><small>Generated on Wed Jun 9 05:01:25 2004 for D-BUS by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.15 </small></address> </body> </html>