<!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-sha.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-sha.c</h1><div class="fragment"><pre>00001 <font class="comment">/* -*- mode: C; c-file-style: "gnu" -*- */</font> 00002 <font class="comment">/* dbus-sha.c SHA-1 implementation</font> 00003 <font class="comment"> *</font> 00004 <font class="comment"> * Copyright (C) 2003 Red Hat Inc.</font> 00005 <font class="comment"> * Copyright (C) 1995 A. M. Kuchling</font> 00006 <font class="comment"> *</font> 00007 <font class="comment"> * Licensed under the Academic Free License version 2.0</font> 00008 <font class="comment"> *</font> 00009 <font class="comment"> * This program is free software; you can redistribute it and/or modify</font> 00010 <font class="comment"> * it under the terms of the GNU General Public License as published by</font> 00011 <font class="comment"> * the Free Software Foundation; either version 2 of the License, or</font> 00012 <font class="comment"> * (at your option) any later version.</font> 00013 <font class="comment"> *</font> 00014 <font class="comment"> * This program is distributed in the hope that it will be useful,</font> 00015 <font class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</font> 00016 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</font> 00017 <font class="comment"> * GNU General Public License for more details.</font> 00018 <font class="comment"> *</font> 00019 <font class="comment"> * You should have received a copy of the GNU General Public License</font> 00020 <font class="comment"> * along with this program; if not, write to the Free Software</font> 00021 <font class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</font> 00022 <font class="comment"> *</font> 00023 <font class="comment"> */</font> 00024 00025 <font class="preprocessor">#include "dbus-internals.h"</font> 00026 <font class="preprocessor">#include "dbus-marshal.h"</font> 00027 <font class="preprocessor">#include "dbus-sha.h"</font> 00028 <font class="preprocessor">#include <string.h></font> 00029 00030 <font class="comment">/* The following comments have the history of where this code</font> 00031 <font class="comment"> * comes from. I actually copied it from GNet in GNOME CVS.</font> 00032 <font class="comment"> * - hp@redhat.com</font> 00033 <font class="comment"> */</font> 00034 00035 <font class="comment">/*</font> 00036 <font class="comment"> * sha.h : Implementation of the Secure Hash Algorithm</font> 00037 <font class="comment"> *</font> 00038 <font class="comment"> * Part of the Python Cryptography Toolkit, version 1.0.0</font> 00039 <font class="comment"> *</font> 00040 <font class="comment"> * Copyright (C) 1995, A.M. Kuchling</font> 00041 <font class="comment"> *</font> 00042 <font class="comment"> * Distribute and use freely; there are no restrictions on further</font> 00043 <font class="comment"> * dissemination and usage except those imposed by the laws of your</font> 00044 <font class="comment"> * country of residence.</font> 00045 <font class="comment"> *</font> 00046 <font class="comment"> */</font> 00047 00048 <font class="comment">/* SHA: NIST's Secure Hash Algorithm */</font> 00049 00050 <font class="comment">/* Based on SHA code originally posted to sci.crypt by Peter Gutmann</font> 00051 <font class="comment"> in message <30ajo5$oe8@ccu2.auckland.ac.nz>.</font> 00052 <font class="comment"> Modified to test for endianness on creation of SHA objects by AMK.</font> 00053 <font class="comment"> Also, the original specification of SHA was found to have a weakness</font> 00054 <font class="comment"> by NSA/NIST. This code implements the fixed version of SHA.</font> 00055 <font class="comment">*/</font> 00056 00057 <font class="comment">/* Here's the first paragraph of Peter Gutmann's posting:</font> 00058 <font class="comment"></font> 00059 <font class="comment">The following is my SHA (FIPS 180) code updated to allow use of the "fixed"</font> 00060 <font class="comment">SHA, thanks to Jim Gillogly and an anonymous contributor for the information on</font> 00061 <font class="comment">what's changed in the new version. The fix is a simple change which involves</font> 00062 <font class="comment">adding a single rotate in the initial expansion function. It is unknown</font> 00063 <font class="comment">whether this is an optimal solution to the problem which was discovered in the</font> 00064 <font class="comment">SHA or whether it's simply a bandaid which fixes the problem with a minimum of</font> 00065 <font class="comment">effort (for example the reengineering of a great many Capstone chips).</font> 00066 <font class="comment">*/</font> 00067 00087 <font class="preprocessor">#ifndef DOXYGEN_SHOULD_SKIP_THIS</font> 00088 <font class="preprocessor"></font> 00089 <font class="comment">/* The SHA block size and message digest sizes, in bytes */</font> 00090 00091 <font class="preprocessor">#define SHA_DATASIZE 64</font> 00092 <font class="preprocessor"></font><font class="preprocessor">#define SHA_DIGESTSIZE 20</font> 00093 <font class="preprocessor"></font> 00094 <font class="comment">/* The SHA f()-functions. The f1 and f3 functions can be optimized to</font> 00095 <font class="comment"> save one boolean operation each - thanks to Rich Schroeppel,</font> 00096 <font class="comment"> rcs@cs.arizona.edu for discovering this */</font> 00097 00098 <font class="comment">/*#define f1(x,y,z) ( ( x & y ) | ( ~x & z ) ) // Rounds 0-19 */</font> 00099 <font class="preprocessor">#define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) </font><font class="comment">/* Rounds 0-19 */</font> 00100 <font class="preprocessor">#define f2(x,y,z) ( x ^ y ^ z ) </font><font class="comment">/* Rounds 20-39 */</font> 00101 <font class="comment">/*#define f3(x,y,z) ( ( x & y ) | ( x & z ) | ( y & z ) ) // Rounds 40-59 */</font> 00102 <font class="preprocessor">#define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) </font><font class="comment">/* Rounds 40-59 */</font> 00103 <font class="preprocessor">#define f4(x,y,z) ( x ^ y ^ z ) </font><font class="comment">/* Rounds 60-79 */</font> 00104 00105 <font class="comment">/* The SHA Mysterious Constants */</font> 00106 00107 <font class="preprocessor">#define K1 0x5A827999L </font><font class="comment">/* Rounds 0-19 */</font> 00108 <font class="preprocessor">#define K2 0x6ED9EBA1L </font><font class="comment">/* Rounds 20-39 */</font> 00109 <font class="preprocessor">#define K3 0x8F1BBCDCL </font><font class="comment">/* Rounds 40-59 */</font> 00110 <font class="preprocessor">#define K4 0xCA62C1D6L </font><font class="comment">/* Rounds 60-79 */</font> 00111 00112 <font class="comment">/* SHA initial values */</font> 00113 00114 <font class="preprocessor">#define h0init 0x67452301L</font> 00115 <font class="preprocessor"></font><font class="preprocessor">#define h1init 0xEFCDAB89L</font> 00116 <font class="preprocessor"></font><font class="preprocessor">#define h2init 0x98BADCFEL</font> 00117 <font class="preprocessor"></font><font class="preprocessor">#define h3init 0x10325476L</font> 00118 <font class="preprocessor"></font><font class="preprocessor">#define h4init 0xC3D2E1F0L</font> 00119 <font class="preprocessor"></font> 00120 <font class="comment">/* Note that it may be necessary to add parentheses to these macros if they</font> 00121 <font class="comment"> are to be called with expressions as arguments */</font> 00122 <font class="comment">/* 32-bit rotate left - kludged with shifts */</font> 00123 00124 <font class="preprocessor">#define ROTL(n,X) ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )</font> 00125 <font class="preprocessor"></font> 00126 <font class="comment">/* The initial expanding function. The hash function is defined over an</font> 00127 <font class="comment"> 80-word expanded input array W, where the first 16 are copies of the input</font> 00128 <font class="comment"> data, and the remaining 64 are defined by</font> 00129 <font class="comment"></font> 00130 <font class="comment"> W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ]</font> 00131 <font class="comment"></font> 00132 <font class="comment"> This implementation generates these values on the fly in a circular</font> 00133 <font class="comment"> buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this</font> 00134 <font class="comment"> optimization.</font> 00135 <font class="comment"></font> 00136 <font class="comment"> The updated SHA changes the expanding function by adding a rotate of 1</font> 00137 <font class="comment"> bit. Thanks to Jim Gillogly, jim@rand.org, and an anonymous contributor</font> 00138 <font class="comment"> for this information */</font> 00139 00140 <font class="preprocessor">#define expand(W,i) ( W[ i & 15 ] = ROTL( 1, ( W[ i & 15 ] ^ W[ (i - 14) & 15 ] ^ \</font> 00141 <font class="preprocessor"> W[ (i - 8) & 15 ] ^ W[ (i - 3) & 15 ] ) ) )</font> 00142 <font class="preprocessor"></font> 00143 00144 <font class="comment">/* The prototype SHA sub-round. The fundamental sub-round is:</font> 00145 <font class="comment"></font> 00146 <font class="comment"> a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data;</font> 00147 <font class="comment"> b' = a;</font> 00148 <font class="comment"> c' = ROTL( 30, b );</font> 00149 <font class="comment"> d' = c;</font> 00150 <font class="comment"> e' = d;</font> 00151 <font class="comment"></font> 00152 <font class="comment"> but this is implemented by unrolling the loop 5 times and renaming the</font> 00153 <font class="comment"> variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration.</font> 00154 <font class="comment"> This code is then replicated 20 times for each of the 4 functions, using</font> 00155 <font class="comment"> the next 20 values from the W[] array each time */</font> 00156 00157 <font class="preprocessor">#define subRound(a, b, c, d, e, f, k, data) \</font> 00158 <font class="preprocessor"> ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) )</font> 00159 <font class="preprocessor"></font> 00160 <font class="preprocessor">#endif </font><font class="comment">/* DOXYGEN_SHOULD_SKIP_THIS */</font> 00161 00162 <font class="comment">/* Perform the SHA transformation. Note that this code, like MD5, seems to</font> 00163 <font class="comment"> break some optimizing compilers due to the complexity of the expressions</font> 00164 <font class="comment"> and the size of the basic block. It may be necessary to split it into</font> 00165 <font class="comment"> sections, e.g. based on the four subrounds</font> 00166 <font class="comment"></font> 00167 <font class="comment"> Note that this corrupts the context->data area */</font> 00168 00169 <font class="keyword">static</font> <font class="keywordtype">void</font> 00170 SHATransform(dbus_uint32_t *digest, dbus_uint32_t *data) 00171 { 00172 dbus_uint32_t A, B, C, D, E; <font class="comment">/* Local vars */</font> 00173 dbus_uint32_t eData[16]; <font class="comment">/* Expanded data */</font> 00174 00175 <font class="comment">/* Set up first buffer and local data buffer */</font> 00176 A = digest[0]; 00177 B = digest[1]; 00178 C = digest[2]; 00179 D = digest[3]; 00180 E = digest[4]; 00181 memmove (eData, data, SHA_DATASIZE); 00182 00183 <font class="comment">/* Heavy mangling, in 4 sub-rounds of 20 interations each. */</font> 00184 subRound (A, B, C, D, E, f1, K1, eData[0]); 00185 subRound (E, A, B, C, D, f1, K1, eData[1]); 00186 subRound (D, E, A, B, C, f1, K1, eData[2]); 00187 subRound (C, D, E, A, B, f1, K1, eData[3]); 00188 subRound (B, C, D, E, A, f1, K1, eData[4]); 00189 subRound (A, B, C, D, E, f1, K1, eData[5]); 00190 subRound (E, A, B, C, D, f1, K1, eData[6]); 00191 subRound (D, E, A, B, C, f1, K1, eData[7]); 00192 subRound (C, D, E, A, B, f1, K1, eData[8]); 00193 subRound (B, C, D, E, A, f1, K1, eData[9]); 00194 subRound (A, B, C, D, E, f1, K1, eData[10]); 00195 subRound (E, A, B, C, D, f1, K1, eData[11]); 00196 subRound (D, E, A, B, C, f1, K1, eData[12]); 00197 subRound (C, D, E, A, B, f1, K1, eData[13]); 00198 subRound (B, C, D, E, A, f1, K1, eData[14]); 00199 subRound (A, B, C, D, E, f1, K1, eData[15]); 00200 subRound (E, A, B, C, D, f1, K1, expand ( eData, 16) ); 00201 subRound (D, E, A, B, C, f1, K1, expand ( eData, 17) ); 00202 subRound (C, D, E, A, B, f1, K1, expand ( eData, 18) ); 00203 subRound (B, C, D, E, A, f1, K1, expand ( eData, 19) ); 00204 00205 subRound (A, B, C, D, E, f2, K2, expand ( eData, 20) ); 00206 subRound (E, A, B, C, D, f2, K2, expand ( eData, 21) ); 00207 subRound (D, E, A, B, C, f2, K2, expand ( eData, 22) ); 00208 subRound (C, D, E, A, B, f2, K2, expand ( eData, 23) ); 00209 subRound (B, C, D, E, A, f2, K2, expand ( eData, 24) ); 00210 subRound (A, B, C, D, E, f2, K2, expand ( eData, 25) ); 00211 subRound (E, A, B, C, D, f2, K2, expand ( eData, 26) ); 00212 subRound (D, E, A, B, C, f2, K2, expand ( eData, 27) ); 00213 subRound (C, D, E, A, B, f2, K2, expand ( eData, 28) ); 00214 subRound (B, C, D, E, A, f2, K2, expand ( eData, 29) ); 00215 subRound (A, B, C, D, E, f2, K2, expand ( eData, 30) ); 00216 subRound (E, A, B, C, D, f2, K2, expand ( eData, 31) ); 00217 subRound (D, E, A, B, C, f2, K2, expand ( eData, 32) ); 00218 subRound (C, D, E, A, B, f2, K2, expand ( eData, 33) ); 00219 subRound (B, C, D, E, A, f2, K2, expand ( eData, 34) ); 00220 subRound (A, B, C, D, E, f2, K2, expand ( eData, 35) ); 00221 subRound (E, A, B, C, D, f2, K2, expand ( eData, 36) ); 00222 subRound (D, E, A, B, C, f2, K2, expand ( eData, 37) ); 00223 subRound (C, D, E, A, B, f2, K2, expand ( eData, 38) ); 00224 subRound (B, C, D, E, A, f2, K2, expand ( eData, 39) ); 00225 00226 subRound (A, B, C, D, E, f3, K3, expand ( eData, 40) ); 00227 subRound (E, A, B, C, D, f3, K3, expand ( eData, 41) ); 00228 subRound (D, E, A, B, C, f3, K3, expand ( eData, 42) ); 00229 subRound (C, D, E, A, B, f3, K3, expand ( eData, 43) ); 00230 subRound (B, C, D, E, A, f3, K3, expand ( eData, 44) ); 00231 subRound (A, B, C, D, E, f3, K3, expand ( eData, 45) ); 00232 subRound (E, A, B, C, D, f3, K3, expand ( eData, 46) ); 00233 subRound (D, E, A, B, C, f3, K3, expand ( eData, 47) ); 00234 subRound (C, D, E, A, B, f3, K3, expand ( eData, 48) ); 00235 subRound (B, C, D, E, A, f3, K3, expand ( eData, 49) ); 00236 subRound (A, B, C, D, E, f3, K3, expand ( eData, 50) ); 00237 subRound (E, A, B, C, D, f3, K3, expand ( eData, 51) ); 00238 subRound (D, E, A, B, C, f3, K3, expand ( eData, 52) ); 00239 subRound (C, D, E, A, B, f3, K3, expand ( eData, 53) ); 00240 subRound (B, C, D, E, A, f3, K3, expand ( eData, 54) ); 00241 subRound (A, B, C, D, E, f3, K3, expand ( eData, 55) ); 00242 subRound (E, A, B, C, D, f3, K3, expand ( eData, 56) ); 00243 subRound (D, E, A, B, C, f3, K3, expand ( eData, 57) ); 00244 subRound (C, D, E, A, B, f3, K3, expand ( eData, 58) ); 00245 subRound (B, C, D, E, A, f3, K3, expand ( eData, 59) ); 00246 00247 subRound (A, B, C, D, E, f4, K4, expand ( eData, 60) ); 00248 subRound (E, A, B, C, D, f4, K4, expand ( eData, 61) ); 00249 subRound (D, E, A, B, C, f4, K4, expand ( eData, 62) ); 00250 subRound (C, D, E, A, B, f4, K4, expand ( eData, 63) ); 00251 subRound (B, C, D, E, A, f4, K4, expand ( eData, 64) ); 00252 subRound (A, B, C, D, E, f4, K4, expand ( eData, 65) ); 00253 subRound (E, A, B, C, D, f4, K4, expand ( eData, 66) ); 00254 subRound (D, E, A, B, C, f4, K4, expand ( eData, 67) ); 00255 subRound (C, D, E, A, B, f4, K4, expand ( eData, 68) ); 00256 subRound (B, C, D, E, A, f4, K4, expand ( eData, 69) ); 00257 subRound (A, B, C, D, E, f4, K4, expand ( eData, 70) ); 00258 subRound (E, A, B, C, D, f4, K4, expand ( eData, 71) ); 00259 subRound (D, E, A, B, C, f4, K4, expand ( eData, 72) ); 00260 subRound (C, D, E, A, B, f4, K4, expand ( eData, 73) ); 00261 subRound (B, C, D, E, A, f4, K4, expand ( eData, 74) ); 00262 subRound (A, B, C, D, E, f4, K4, expand ( eData, 75) ); 00263 subRound (E, A, B, C, D, f4, K4, expand ( eData, 76) ); 00264 subRound (D, E, A, B, C, f4, K4, expand ( eData, 77) ); 00265 subRound (C, D, E, A, B, f4, K4, expand ( eData, 78) ); 00266 subRound (B, C, D, E, A, f4, K4, expand ( eData, 79) ); 00267 00268 <font class="comment">/* Build message digest */</font> 00269 digest[0] += A; 00270 digest[1] += B; 00271 digest[2] += C; 00272 digest[3] += D; 00273 digest[4] += E; 00274 } 00275 00276 <font class="comment">/* When run on a little-endian CPU we need to perform byte reversal on an</font> 00277 <font class="comment"> array of longwords. */</font> 00278 00279 <font class="preprocessor">#ifdef WORDS_BIGENDIAN</font> 00280 <font class="preprocessor"></font><font class="preprocessor">#define swap_words(buffer, byte_count)</font> 00281 <font class="preprocessor"></font><font class="preprocessor">#else</font> 00282 <font class="preprocessor"></font><font class="keyword">static</font> <font class="keywordtype">void</font> 00283 swap_words (dbus_uint32_t *buffer, 00284 <font class="keywordtype">int</font> byte_count) 00285 { 00286 byte_count /= <font class="keyword">sizeof</font> (dbus_uint32_t); 00287 <font class="keywordflow">while</font> (byte_count--) 00288 { 00289 *buffer = DBUS_UINT32_SWAP_LE_BE (*buffer); 00290 ++buffer; 00291 } 00292 } 00293 <font class="preprocessor">#endif</font> 00294 <font class="preprocessor"></font> 00295 <font class="keyword">static</font> <font class="keywordtype">void</font> 00296 sha_init (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> *context) 00297 { 00298 <font class="comment">/* Set the h-vars to their initial values */</font> 00299 context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>[0] = h0init; 00300 context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>[1] = h1init; 00301 context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>[2] = h2init; 00302 context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>[3] = h3init; 00303 context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>[4] = h4init; 00304 00305 <font class="comment">/* Initialise bit count */</font> 00306 context-><a class="code" href="structDBusSHAContext.html#m1">count_lo</a> = context-><a class="code" href="structDBusSHAContext.html#m2">count_hi</a> = 0; 00307 } 00308 00309 <font class="keyword">static</font> <font class="keywordtype">void</font> 00310 sha_append (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> *context, 00311 <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *buffer, 00312 <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> count) 00313 { 00314 dbus_uint32_t tmp; 00315 <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> dataCount; 00316 00317 <font class="comment">/* Update bitcount */</font> 00318 tmp = context-><a class="code" href="structDBusSHAContext.html#m1">count_lo</a>; 00319 <font class="keywordflow">if</font> (( context-><a class="code" href="structDBusSHAContext.html#m1">count_lo</a> = tmp + ( ( dbus_uint32_t) count << 3) ) < tmp) 00320 context-><a class="code" href="structDBusSHAContext.html#m2">count_hi</a>++; <font class="comment">/* Carry from low to high */</font> 00321 context-><a class="code" href="structDBusSHAContext.html#m2">count_hi</a> += count >> 29; 00322 00323 <font class="comment">/* Get count of bytes already in data */</font> 00324 dataCount = (int) (tmp >> 3) & 0x3F; 00325 00326 <font class="comment">/* Handle any leading odd-sized chunks */</font> 00327 <font class="keywordflow">if</font> (dataCount) 00328 { 00329 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *p = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *) context-><a class="code" href="structDBusSHAContext.html#m3">data</a> + dataCount; 00330 00331 dataCount = SHA_DATASIZE - dataCount; 00332 <font class="keywordflow">if</font> (count < dataCount) 00333 { 00334 memmove (p, buffer, count); 00335 <font class="keywordflow">return</font>; 00336 } 00337 memmove (p, buffer, dataCount); 00338 swap_words (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, SHA_DATASIZE); 00339 SHATransform (context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>, context-><a class="code" href="structDBusSHAContext.html#m3">data</a>); 00340 buffer += dataCount; 00341 count -= dataCount; 00342 } 00343 00344 <font class="comment">/* Process data in SHA_DATASIZE chunks */</font> 00345 <font class="keywordflow">while</font> (count >= SHA_DATASIZE) 00346 { 00347 memmove (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, buffer, SHA_DATASIZE); 00348 swap_words (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, SHA_DATASIZE); 00349 SHATransform (context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>, context-><a class="code" href="structDBusSHAContext.html#m3">data</a>); 00350 buffer += SHA_DATASIZE; 00351 count -= SHA_DATASIZE; 00352 } 00353 00354 <font class="comment">/* Handle any remaining bytes of data. */</font> 00355 memmove (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, buffer, count); 00356 } 00357 00358 00359 <font class="comment">/* Final wrapup - pad to SHA_DATASIZE-byte boundary with the bit pattern</font> 00360 <font class="comment"> 1 0* (64-bit count of bits processed, MSB-first) */</font> 00361 00362 <font class="keyword">static</font> <font class="keywordtype">void</font> 00363 sha_finish (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> *context, <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> digest[20]) 00364 { 00365 <font class="keywordtype">int</font> count; 00366 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *data_p; 00367 00368 <font class="comment">/* Compute number of bytes mod 64 */</font> 00369 count = (int) context-><a class="code" href="structDBusSHAContext.html#m1">count_lo</a>; 00370 count = (count >> 3) & 0x3F; 00371 00372 <font class="comment">/* Set the first char of padding to 0x80. This is safe since there is</font> 00373 <font class="comment"> always at least one byte free */</font> 00374 data_p = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *) context-><a class="code" href="structDBusSHAContext.html#m3">data</a> + count; 00375 *data_p++ = 0x80; 00376 00377 <font class="comment">/* Bytes of padding needed to make 64 bytes */</font> 00378 count = SHA_DATASIZE - 1 - count; 00379 00380 <font class="comment">/* Pad out to 56 mod 64 */</font> 00381 <font class="keywordflow">if</font> (count < 8) 00382 { 00383 <font class="comment">/* Two lots of padding: Pad the first block to 64 bytes */</font> 00384 memset (data_p, 0, count); 00385 swap_words (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, SHA_DATASIZE); 00386 SHATransform (context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>, context-><a class="code" href="structDBusSHAContext.html#m3">data</a>); 00387 00388 <font class="comment">/* Now fill the next block with 56 bytes */</font> 00389 memset (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, 0, SHA_DATASIZE - 8); 00390 } 00391 <font class="keywordflow">else</font> 00392 <font class="comment">/* Pad block to 56 bytes */</font> 00393 memset (data_p, 0, count - 8); 00394 00395 <font class="comment">/* Append length in bits and transform */</font> 00396 context-><a class="code" href="structDBusSHAContext.html#m3">data</a>[14] = context-><a class="code" href="structDBusSHAContext.html#m2">count_hi</a>; 00397 context-><a class="code" href="structDBusSHAContext.html#m3">data</a>[15] = context-><a class="code" href="structDBusSHAContext.html#m1">count_lo</a>; 00398 00399 swap_words (context-><a class="code" href="structDBusSHAContext.html#m3">data</a>, SHA_DATASIZE - 8); 00400 SHATransform (context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>, context-><a class="code" href="structDBusSHAContext.html#m3">data</a>); 00401 swap_words (context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>, SHA_DIGESTSIZE); 00402 memmove (digest, context-><a class="code" href="structDBusSHAContext.html#m0">digest</a>, SHA_DIGESTSIZE); 00403 } 00404 <font class="comment">/* End of internals */</font> 00406 00418 <font class="keywordtype">void</font> <a name="l00419"></a><a class="code" href="group__DBusSHA.html#a0">00419</a> _dbus_sha_init (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> *context) 00420 { 00421 sha_init (context); 00422 } 00423 00430 <font class="keywordtype">void</font> <a name="l00431"></a><a class="code" href="group__DBusSHA.html#a1">00431</a> _dbus_sha_update (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> *context, 00432 <font class="keyword">const</font> <a class="code" href="structDBusString.html">DBusString</a> *data) 00433 { 00434 <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> inputLen; 00435 <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *input; 00436 00437 input = (<font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>*) _dbus_string_get_const_data (data); 00438 inputLen = _dbus_string_get_length (data); 00439 00440 sha_append (context, input, inputLen); 00441 } 00442 00454 dbus_bool_t <a name="l00455"></a><a class="code" href="group__DBusSHA.html#a2">00455</a> _dbus_sha_final (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> *context, 00456 <a class="code" href="structDBusString.html">DBusString</a> *results) 00457 { 00458 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> digest[20]; 00459 00460 sha_finish (context, digest); 00461 00462 <font class="keywordflow">if</font> (!_dbus_string_append_len (results, digest, 20)) 00463 <font class="keywordflow">return</font> FALSE; 00464 00465 <font class="comment">/* some kind of security paranoia, though it seems pointless</font> 00466 <font class="comment"> * to me given the nonzeroed stuff flying around</font> 00467 <font class="comment"> */</font> 00468 memset ((<font class="keywordtype">void</font>*)context, <font class="charliteral">'\0'</font>, <font class="keyword">sizeof</font> (<a class="code" href="structDBusSHAContext.html">DBusSHAContext</a>)); 00469 00470 <font class="keywordflow">return</font> TRUE; 00471 } 00472 00481 dbus_bool_t <a name="l00482"></a><a class="code" href="group__DBusSHA.html#a3">00482</a> _dbus_sha_compute (<font class="keyword">const</font> <a class="code" href="structDBusString.html">DBusString</a> *data, 00483 <a class="code" href="structDBusString.html">DBusString</a> *ascii_output) 00484 { 00485 <a class="code" href="structDBusSHAContext.html">DBusSHAContext</a> context; 00486 <a class="code" href="structDBusString.html">DBusString</a> digest; 00487 00488 _dbus_sha_init (&context); 00489 00490 _dbus_sha_update (&context, data); 00491 00492 <font class="keywordflow">if</font> (!_dbus_string_init (&digest)) 00493 <font class="keywordflow">return</font> FALSE; 00494 00495 <font class="keywordflow">if</font> (!_dbus_sha_final (&context, &digest)) 00496 <font class="keywordflow">goto</font> error; 00497 00498 <font class="keywordflow">if</font> (!_dbus_string_hex_encode (&digest, 0, ascii_output, 00499 _dbus_string_get_length (ascii_output))) 00500 <font class="keywordflow">goto</font> error; 00501 00502 _dbus_string_free (&digest); 00503 00504 <font class="keywordflow">return</font> TRUE; 00505 00506 error: 00507 _dbus_string_free (&digest); 00508 <font class="keywordflow">return</font> FALSE; 00509 } 00510 <font class="comment">/* end of exported functions */</font> 00512 00513 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font> 00514 <font class="preprocessor"></font><font class="preprocessor">#include "dbus-test.h"</font> 00515 <font class="preprocessor">#include <stdio.h></font> 00516 00517 <font class="keyword">static</font> dbus_bool_t 00518 check_sha_binary (<font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *input, 00519 <font class="keywordtype">int</font> input_len, 00520 <font class="keyword">const</font> <font class="keywordtype">char</font> *expected) 00521 { 00522 <a class="code" href="structDBusString.html">DBusString</a> input_str; 00523 <a class="code" href="structDBusString.html">DBusString</a> expected_str; 00524 <a class="code" href="structDBusString.html">DBusString</a> results; 00525 00526 _dbus_string_init_const_len (&input_str, input, input_len); 00527 _dbus_string_init_const (&expected_str, expected); 00528 00529 <font class="keywordflow">if</font> (!_dbus_string_init (&results)) 00530 _dbus_assert_not_reached (<font class="stringliteral">"no memory for SHA-1 results"</font>); 00531 00532 <font class="keywordflow">if</font> (!_dbus_sha_compute (&input_str, &results)) 00533 _dbus_assert_not_reached (<font class="stringliteral">"no memory for SHA-1 results"</font>); 00534 00535 <font class="keywordflow">if</font> (!_dbus_string_equal (&expected_str, &results)) 00536 { 00537 _dbus_warn (<font class="stringliteral">"Expected hash %s got %s for SHA-1 sum\n"</font>, 00538 expected, 00539 _dbus_string_get_const_data (&results)); 00540 _dbus_string_free (&results); 00541 <font class="keywordflow">return</font> FALSE; 00542 } 00543 00544 _dbus_string_free (&results); 00545 <font class="keywordflow">return</font> TRUE; 00546 } 00547 00548 <font class="keyword">static</font> dbus_bool_t 00549 check_sha_str (<font class="keyword">const</font> <font class="keywordtype">char</font> *input, 00550 <font class="keyword">const</font> <font class="keywordtype">char</font> *expected) 00551 { 00552 <font class="keywordflow">return</font> check_sha_binary (input, strlen (input), expected); 00553 } 00554 00555 <font class="keyword">static</font> dbus_bool_t 00556 decode_compact_string (<font class="keyword">const</font> <a class="code" href="structDBusString.html">DBusString</a> *line, 00557 <a class="code" href="structDBusString.html">DBusString</a> *decoded) 00558 { 00559 <font class="keywordtype">int</font> n_bits; 00560 dbus_bool_t current_b; 00561 <font class="keywordtype">int</font> offset; 00562 <font class="keywordtype">int</font> next; 00563 <font class="keywordtype">long</font> val; 00564 <font class="keywordtype">int</font> length_bytes; 00565 00566 offset = 0; 00567 next = 0; 00568 00569 <font class="keywordflow">if</font> (!_dbus_string_parse_int (line, offset, &val, &next)) 00570 { 00571 fprintf (stderr, <font class="stringliteral">"could not parse length at start of compact string: %s\n"</font>, 00572 _dbus_string_get_const_data (line)); 00573 <font class="keywordflow">return</font> FALSE; 00574 } 00575 00576 _dbus_string_skip_blank (line, next, &next); 00577 00578 offset = next; 00579 <font class="keywordflow">if</font> (!_dbus_string_parse_int (line, offset, &val, &next)) 00580 { 00581 fprintf (stderr, <font class="stringliteral">"could not parse start bit 'b' in compact string: %s\n"</font>, 00582 _dbus_string_get_const_data (line)); 00583 <font class="keywordflow">return</font> FALSE; 00584 } 00585 00586 <font class="keywordflow">if</font> (!(val == 0 || val == 1)) 00587 { 00588 fprintf (stderr, <font class="stringliteral">"the value 'b' must be 0 or 1, see sha-1/Readme.txt\n"</font>); 00589 <font class="keywordflow">return</font> FALSE; 00590 } 00591 00592 _dbus_string_skip_blank (line, next, &next); 00593 00594 current_b = val; 00595 n_bits = 0; 00596 00597 <font class="keywordflow">while</font> (next < _dbus_string_get_length (line)) 00598 { 00599 <font class="keywordtype">int</font> total_bits; 00600 00601 offset = next; 00602 00603 <font class="keywordflow">if</font> (_dbus_string_get_byte (line, offset) == <font class="charliteral">'^'</font>) 00604 <font class="keywordflow">break</font>; 00605 00606 <font class="keywordflow">if</font> (!_dbus_string_parse_int (line, offset, &val, &next)) 00607 { 00608 fprintf (stderr, <font class="stringliteral">"could not parse bit count in compact string\n"</font>); 00609 <font class="keywordflow">return</font> FALSE; 00610 } 00611 00612 <font class="comment">/* We now append "val" copies of "current_b" bits to the string */</font> 00613 total_bits = n_bits + val; 00614 <font class="keywordflow">while</font> (n_bits < total_bits) 00615 { 00616 <font class="keywordtype">int</font> byte_containing_next_bit = n_bits / 8; 00617 <font class="keywordtype">int</font> bit_containing_next_bit = 7 - (n_bits % 8); 00618 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> old_byte; 00619 00620 <font class="keywordflow">if</font> (byte_containing_next_bit >= _dbus_string_get_length (decoded)) 00621 { 00622 <font class="keywordflow">if</font> (!_dbus_string_set_length (decoded, byte_containing_next_bit + 1)) 00623 _dbus_assert_not_reached (<font class="stringliteral">"no memory to extend to next byte"</font>); 00624 } 00625 00626 old_byte = _dbus_string_get_byte (decoded, byte_containing_next_bit); 00627 old_byte |= current_b << bit_containing_next_bit; 00628 00629 <font class="preprocessor">#if 0</font> 00630 <font class="preprocessor"></font> printf (<font class="stringliteral">"Appending bit %d to byte %d at bit %d resulting in byte 0x%x\n"</font>, 00631 current_b, byte_containing_next_bit, 00632 bit_containing_next_bit, old_byte); 00633 <font class="preprocessor">#endif</font> 00634 <font class="preprocessor"></font> 00635 _dbus_string_set_byte (decoded, byte_containing_next_bit, old_byte); 00636 00637 ++n_bits; 00638 } 00639 00640 _dbus_string_skip_blank (line, next, &next); 00641 00642 current_b = !current_b; 00643 } 00644 00645 length_bytes = (n_bits / 8 + ((n_bits % 8) ? 1 : 0)); 00646 00647 <font class="keywordflow">if</font> (_dbus_string_get_length (decoded) != length_bytes) 00648 { 00649 fprintf (stderr, <font class="stringliteral">"Expected length %d bytes %d bits for compact string, got %d bytes\n"</font>, 00650 length_bytes, n_bits, _dbus_string_get_length (decoded)); 00651 <font class="keywordflow">return</font> FALSE; 00652 } 00653 <font class="keywordflow">else</font> 00654 <font class="keywordflow">return</font> TRUE; 00655 } 00656 00657 <font class="keyword">static</font> dbus_bool_t 00658 get_next_expected_result (<a class="code" href="structDBusString.html">DBusString</a> *results, 00659 <a class="code" href="structDBusString.html">DBusString</a> *result) 00660 { 00661 <a class="code" href="structDBusString.html">DBusString</a> line; 00662 dbus_bool_t retval; 00663 00664 retval = FALSE; 00665 00666 <font class="keywordflow">if</font> (!_dbus_string_init (&line)) 00667 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00668 00669 next_iteration: 00670 <font class="keywordflow">while</font> (_dbus_string_pop_line (results, &line)) 00671 { 00672 _dbus_string_delete_leading_blanks (&line); 00673 00674 <font class="keywordflow">if</font> (_dbus_string_get_length (&line) == 0) 00675 <font class="keywordflow">goto</font> next_iteration; 00676 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_dbus_string_starts_with_c_str (&line, <font class="stringliteral">"#"</font>)) 00677 <font class="keywordflow">goto</font> next_iteration; 00678 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_dbus_string_starts_with_c_str (&line, <font class="stringliteral">"H>"</font>)) 00679 { 00680 <font class="comment">/* don't print */</font> 00681 } 00682 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_dbus_string_starts_with_c_str (&line, <font class="stringliteral">"D>"</font>) || 00683 _dbus_string_starts_with_c_str (&line, <font class="stringliteral">"<D"</font>)) 00684 <font class="keywordflow">goto</font> next_iteration; 00685 <font class="keywordflow">else</font> 00686 { 00687 <font class="keywordtype">int</font> i; 00688 00689 <font class="keywordflow">if</font> (!_dbus_string_move (&line, 0, result, 0)) 00690 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00691 00692 i = 0; 00693 <font class="keywordflow">while</font> (i < _dbus_string_get_length (result)) 00694 { 00695 <font class="keywordflow">switch</font> (_dbus_string_get_byte (result, i)) 00696 { 00697 <font class="keywordflow">case</font> <font class="charliteral">'A'</font>: 00698 _dbus_string_set_byte (result, i, <font class="charliteral">'a'</font>); 00699 <font class="keywordflow">break</font>; 00700 <font class="keywordflow">case</font> <font class="charliteral">'B'</font>: 00701 _dbus_string_set_byte (result, i, <font class="charliteral">'b'</font>); 00702 <font class="keywordflow">break</font>; 00703 <font class="keywordflow">case</font> <font class="charliteral">'C'</font>: 00704 _dbus_string_set_byte (result, i, <font class="charliteral">'c'</font>); 00705 <font class="keywordflow">break</font>; 00706 <font class="keywordflow">case</font> <font class="charliteral">'D'</font>: 00707 _dbus_string_set_byte (result, i, <font class="charliteral">'d'</font>); 00708 <font class="keywordflow">break</font>; 00709 <font class="keywordflow">case</font> <font class="charliteral">'E'</font>: 00710 _dbus_string_set_byte (result, i, <font class="charliteral">'e'</font>); 00711 <font class="keywordflow">break</font>; 00712 <font class="keywordflow">case</font> <font class="charliteral">'F'</font>: 00713 _dbus_string_set_byte (result, i, <font class="charliteral">'f'</font>); 00714 <font class="keywordflow">break</font>; 00715 <font class="keywordflow">case</font> <font class="charliteral">'^'</font>: 00716 <font class="keywordflow">case</font> <font class="charliteral">' '</font>: 00717 _dbus_string_delete (result, i, 1); 00718 --i; <font class="comment">/* to offset ++i below */</font> 00719 <font class="keywordflow">break</font>; 00720 } 00721 00722 ++i; 00723 } 00724 00725 <font class="keywordflow">break</font>; 00726 } 00727 } 00728 00729 retval = TRUE; 00730 00731 <font class="comment">/* out: */</font> 00732 _dbus_string_free (&line); 00733 <font class="keywordflow">return</font> retval; 00734 } 00735 00736 <font class="keyword">static</font> dbus_bool_t 00737 process_test_data (<font class="keyword">const</font> <font class="keywordtype">char</font> *test_data_dir) 00738 { 00739 <a class="code" href="structDBusString.html">DBusString</a> tests_file; 00740 <a class="code" href="structDBusString.html">DBusString</a> results_file; 00741 <a class="code" href="structDBusString.html">DBusString</a> tests; 00742 <a class="code" href="structDBusString.html">DBusString</a> results; 00743 <a class="code" href="structDBusString.html">DBusString</a> line; 00744 <a class="code" href="structDBusString.html">DBusString</a> tmp; 00745 <font class="keywordtype">int</font> line_no; 00746 dbus_bool_t retval; 00747 <font class="keywordtype">int</font> success_count; 00748 <a class="code" href="structDBusError.html">DBusError</a> error; 00749 00750 retval = FALSE; 00751 00752 <font class="keywordflow">if</font> (!_dbus_string_init (&tests_file)) 00753 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00754 00755 <font class="keywordflow">if</font> (!_dbus_string_init (&results_file)) 00756 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00757 00758 <font class="keywordflow">if</font> (!_dbus_string_init (&tests)) 00759 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00760 00761 <font class="keywordflow">if</font> (!_dbus_string_init (&results)) 00762 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00763 00764 <font class="keywordflow">if</font> (!_dbus_string_init (&line)) 00765 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00766 00767 <font class="keywordflow">if</font> (!_dbus_string_append (&tests_file, test_data_dir)) 00768 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00769 00770 <font class="keywordflow">if</font> (!_dbus_string_append (&results_file, test_data_dir)) 00771 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00772 00773 _dbus_string_init_const (&tmp, <font class="stringliteral">"sha-1/byte-messages.sha1"</font>); 00774 <font class="keywordflow">if</font> (!_dbus_concat_dir_and_file (&tests_file, &tmp)) 00775 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00776 00777 _dbus_string_init_const (&tmp, <font class="stringliteral">"sha-1/byte-hashes.sha1"</font>); 00778 <font class="keywordflow">if</font> (!_dbus_concat_dir_and_file (&results_file, &tmp)) 00779 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00780 00781 dbus_error_init (&error); 00782 <font class="keywordflow">if</font> (!_dbus_file_get_contents (&tests, &tests_file, &error)) 00783 { 00784 fprintf (stderr, <font class="stringliteral">"could not load test data file %s: %s\n"</font>, 00785 _dbus_string_get_const_data (&tests_file), 00786 error.<a class="code" href="structDBusError.html#m1">message</a>); 00787 dbus_error_free (&error); 00788 <font class="keywordflow">goto</font> out; 00789 } 00790 00791 <font class="keywordflow">if</font> (!_dbus_file_get_contents (&results, &results_file, &error)) 00792 { 00793 fprintf (stderr, <font class="stringliteral">"could not load results data file %s: %s\n"</font>, 00794 _dbus_string_get_const_data (&results_file), error.<a class="code" href="structDBusError.html#m1">message</a>); 00795 dbus_error_free (&error); 00796 <font class="keywordflow">goto</font> out; 00797 } 00798 00799 success_count = 0; 00800 line_no = 0; 00801 next_iteration: 00802 <font class="keywordflow">while</font> (_dbus_string_pop_line (&tests, &line)) 00803 { 00804 line_no += 1; 00805 00806 _dbus_string_delete_leading_blanks (&line); 00807 00808 <font class="keywordflow">if</font> (_dbus_string_get_length (&line) == 0) 00809 <font class="keywordflow">goto</font> next_iteration; 00810 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_dbus_string_starts_with_c_str (&line, <font class="stringliteral">"#"</font>)) 00811 <font class="keywordflow">goto</font> next_iteration; 00812 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_dbus_string_starts_with_c_str (&line, <font class="stringliteral">"H>"</font>)) 00813 { 00814 printf (<font class="stringliteral">"SHA-1: %s\n"</font>, _dbus_string_get_const_data (&line)); 00815 00816 <font class="keywordflow">if</font> (_dbus_string_find (&line, 0, <font class="stringliteral">"Type 3"</font>, NULL)) 00817 { 00818 <font class="comment">/* See sha-1/Readme.txt - the "Type 3" tests are</font> 00819 <font class="comment"> * random seeds, rather than data to be hashed.</font> 00820 <font class="comment"> * we'd have to do a little bit more implementation</font> 00821 <font class="comment"> * to use those tests.</font> 00822 <font class="comment"> */</font> 00823 00824 printf (<font class="stringliteral">" (ending tests due to Type 3 tests seen - this is normal)\n"</font>); 00825 <font class="keywordflow">break</font>; 00826 } 00827 } 00828 <font class="keywordflow">else</font> <font class="keywordflow">if</font> (_dbus_string_starts_with_c_str (&line, <font class="stringliteral">"D>"</font>) || 00829 _dbus_string_starts_with_c_str (&line, <font class="stringliteral">"<D"</font>)) 00830 <font class="keywordflow">goto</font> next_iteration; 00831 <font class="keywordflow">else</font> 00832 { 00833 <a class="code" href="structDBusString.html">DBusString</a> test; 00834 <a class="code" href="structDBusString.html">DBusString</a> result; 00835 <a class="code" href="structDBusString.html">DBusString</a> next_line; 00836 <a class="code" href="structDBusString.html">DBusString</a> expected; 00837 dbus_bool_t success; 00838 00839 success = FALSE; 00840 00841 <font class="keywordflow">if</font> (!_dbus_string_init (&next_line)) 00842 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00843 00844 <font class="keywordflow">if</font> (!_dbus_string_init (&expected)) 00845 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00846 00847 <font class="keywordflow">if</font> (!_dbus_string_init (&test)) 00848 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00849 00850 <font class="keywordflow">if</font> (!_dbus_string_init (&result)) 00851 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00852 00853 <font class="comment">/* the "compact strings" are "^"-terminated not</font> 00854 <font class="comment"> * newline-terminated so readahead to find the</font> 00855 <font class="comment"> * "^"</font> 00856 <font class="comment"> */</font> 00857 <font class="keywordflow">while</font> (!_dbus_string_find (&line, 0, <font class="stringliteral">"^"</font>, NULL) && 00858 _dbus_string_pop_line (&tests, &next_line)) 00859 { 00860 <font class="keywordflow">if</font> (!_dbus_string_append_byte (&line, <font class="charliteral">' '</font>) || 00861 !_dbus_string_move (&next_line, 0, &line, 00862 _dbus_string_get_length (&line))) 00863 _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>); 00864 } 00865 00866 <font class="keywordflow">if</font> (!decode_compact_string (&line, &test)) 00867 { 00868 fprintf (stderr, <font class="stringliteral">"Failed to decode line %d as a compact string\n"</font>, 00869 line_no); 00870 <font class="keywordflow">goto</font> failure; 00871 } 00872 00873 <font class="keywordflow">if</font> (!_dbus_sha_compute (&test, &result)) 00874 _dbus_assert_not_reached (<font class="stringliteral">"no memory for SHA-1 result"</font>); 00875 00876 <font class="keywordflow">if</font> (!get_next_expected_result (&results, &expected)) 00877 { 00878 fprintf (stderr, <font class="stringliteral">"Failed to read an expected result\n"</font>); 00879 <font class="keywordflow">goto</font> failure; 00880 } 00881 00882 <font class="keywordflow">if</font> (!_dbus_string_equal (&result, &expected)) 00883 { 00884 fprintf (stderr, <font class="stringliteral">" for line %d got hash %s expected %s\n"</font>, 00885 line_no, 00886 _dbus_string_get_const_data (&result), 00887 _dbus_string_get_const_data (&expected)); 00888 <font class="keywordflow">goto</font> failure; 00889 } 00890 <font class="keywordflow">else</font> 00891 { 00892 success_count += 1; 00893 } 00894 00895 success = TRUE; 00896 00897 failure: 00898 _dbus_string_free (&test); 00899 _dbus_string_free (&result); 00900 _dbus_string_free (&next_line); 00901 _dbus_string_free (&expected); 00902 00903 <font class="keywordflow">if</font> (!success) 00904 <font class="keywordflow">goto</font> out; 00905 } 00906 } 00907 00908 retval = TRUE; 00909 00910 printf (<font class="stringliteral">"Passed the %d SHA-1 tests in the test file\n"</font>, 00911 success_count); 00912 00913 out: 00914 _dbus_string_free (&tests_file); 00915 _dbus_string_free (&results_file); 00916 _dbus_string_free (&tests); 00917 _dbus_string_free (&results); 00918 _dbus_string_free (&line); 00919 00920 <font class="keywordflow">return</font> retval; 00921 } 00922 00929 dbus_bool_t <a name="l00930"></a><a class="code" href="group__DBusSHAInternals.html#a5">00930</a> _dbus_sha_test (<font class="keyword">const</font> <font class="keywordtype">char</font> *test_data_dir) 00931 { 00932 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> all_bytes[256]; 00933 <font class="keywordtype">int</font> i; 00934 00935 <font class="keywordflow">if</font> (test_data_dir != NULL) 00936 { 00937 <font class="keywordflow">if</font> (!process_test_data (test_data_dir)) 00938 <font class="keywordflow">return</font> FALSE; 00939 } 00940 <font class="keywordflow">else</font> 00941 printf (<font class="stringliteral">"No test data dir\n"</font>); 00942 00943 i = 0; 00944 <font class="keywordflow">while</font> (i < 256) 00945 { 00946 all_bytes[i] = i; 00947 ++i; 00948 } 00949 00950 <font class="keywordflow">if</font> (!check_sha_binary (all_bytes, 256, 00951 <font class="stringliteral">"4916d6bdb7f78e6803698cab32d1586ea457dfc8"</font>)) 00952 <font class="keywordflow">return</font> FALSE; 00953 00954 <font class="preprocessor">#define CHECK(input,expected) if (!check_sha_str (input, expected)) return FALSE</font> 00955 <font class="preprocessor"></font> 00956 CHECK (<font class="stringliteral">""</font>, <font class="stringliteral">"da39a3ee5e6b4b0d3255bfef95601890afd80709"</font>); 00957 CHECK (<font class="stringliteral">"a"</font>, <font class="stringliteral">"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"</font>); 00958 CHECK (<font class="stringliteral">"abc"</font>, <font class="stringliteral">"a9993e364706816aba3e25717850c26c9cd0d89d"</font>); 00959 CHECK (<font class="stringliteral">"message digest"</font>, <font class="stringliteral">"c12252ceda8be8994d5fa0290a47231c1d16aae3"</font>); 00960 CHECK (<font class="stringliteral">"abcdefghijklmnopqrstuvwxyz"</font>, <font class="stringliteral">"32d10c7b8cf96570ca04ce37f2a19d84240d3a89"</font>); 00961 CHECK (<font class="stringliteral">"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"</font>, 00962 <font class="stringliteral">"761c457bf73b14d27e9e9265c46f4b4dda11f940"</font>); 00963 CHECK (<font class="stringliteral">"12345678901234567890123456789012345678901234567890123456789012345678901234567890"</font>, 00964 <font class="stringliteral">"50abf5706a150990a08b2c5ea40fa0e585554732"</font>); 00965 00966 <font class="keywordflow">return</font> TRUE; 00967 } 00968 00969 <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:26 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>