1*61dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*61dff127SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*61dff127SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*61dff127SAndrew Rist * distributed with this work for additional information
6*61dff127SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*61dff127SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*61dff127SAndrew Rist * "License"); you may not use this file except in compliance
9*61dff127SAndrew Rist * with the License. You may obtain a copy of the License at
10*61dff127SAndrew Rist *
11*61dff127SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*61dff127SAndrew Rist *
13*61dff127SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*61dff127SAndrew Rist * software distributed under the License is distributed on an
15*61dff127SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*61dff127SAndrew Rist * KIND, either express or implied. See the License for the
17*61dff127SAndrew Rist * specific language governing permissions and limitations
18*61dff127SAndrew Rist * under the License.
19*61dff127SAndrew Rist *
20*61dff127SAndrew Rist *************************************************************/
21*61dff127SAndrew Rist
22*61dff127SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_bridges.hxx"
27cdf0e10cSrcweir
28cdf0e10cSrcweir
29cdf0e10cSrcweir #ifndef TEST
30cdf0e10cSrcweir #ifndef _SAL_TYPES_H_
31cdf0e10cSrcweir #include <sal/types.h>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #else
34cdf0e10cSrcweir typedef unsigned int sal_uInt32;
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir
37cdf0e10cSrcweir #include <string.h>
38cdf0e10cSrcweir
39cdf0e10cSrcweir /*
40cdf0e10cSrcweir * build a hash for a character buffer using the NIST algorithm
41cdf0e10cSrcweir */
42cdf0e10cSrcweir
43cdf0e10cSrcweir class NIST_Hash
44cdf0e10cSrcweir {
45cdf0e10cSrcweir
46cdf0e10cSrcweir // helper functions
f1(sal_uInt32 x,sal_uInt32 y,sal_uInt32 z)47cdf0e10cSrcweir sal_uInt32 f1( sal_uInt32 x, sal_uInt32 y, sal_uInt32 z )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir return z ^ ( x & ( y ^ z ) );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir
f2(sal_uInt32 x,sal_uInt32 y,sal_uInt32 z)52cdf0e10cSrcweir sal_uInt32 f2( sal_uInt32 x, sal_uInt32 y, sal_uInt32 z )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir return x ^ y ^ z;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
f3(sal_uInt32 x,sal_uInt32 y,sal_uInt32 z)57cdf0e10cSrcweir sal_uInt32 f3( sal_uInt32 x, sal_uInt32 y, sal_uInt32 z )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir return ( x & y ) + ( z & ( x ^ y ) );
60cdf0e10cSrcweir }
61cdf0e10cSrcweir
rotl(sal_uInt32 nValue,sal_uInt32 nBits)62cdf0e10cSrcweir sal_uInt32 rotl( sal_uInt32 nValue, sal_uInt32 nBits )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir return ( nValue << nBits ) | ( nValue >> (32-nBits) );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir
expand_nostore(sal_uInt32 index)67cdf0e10cSrcweir sal_uInt32 expand_nostore( sal_uInt32 index )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir return data[index&15] ^ data[(index-14)&15] ^ data[(index-8)&15] ^ data[(index-3)&15];
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
expand_store(sal_uInt32 index)72cdf0e10cSrcweir sal_uInt32 expand_store( sal_uInt32 index )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir return data[index&15] ^= data[(index-14)&15] ^ data[(index-8)&15] ^ data[(index-3)&15];
75cdf0e10cSrcweir }
76cdf0e10cSrcweir
subRound(sal_uInt32 a,sal_uInt32 & b,sal_uInt32 c,sal_uInt32 d,sal_uInt32 & e,sal_uInt32 constant,sal_uInt32 datum,sal_uInt32 nFunction)77cdf0e10cSrcweir void subRound( sal_uInt32 a, sal_uInt32& b, sal_uInt32 c, sal_uInt32 d, sal_uInt32& e, sal_uInt32 constant, sal_uInt32 datum, sal_uInt32 nFunction )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir e += rotl(a,5);
80cdf0e10cSrcweir switch( nFunction )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir case 1: e += f1( b, c, d );break;
83cdf0e10cSrcweir case 2:
84cdf0e10cSrcweir case 4: e += f2( b, c, d );break;
85cdf0e10cSrcweir case 3: e += f3( b, c, d );break;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir e += constant + datum;
88cdf0e10cSrcweir b = rotl( b, 30 );
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
91cdf0e10cSrcweir void transform();
92cdf0e10cSrcweir void final();
93cdf0e10cSrcweir
94cdf0e10cSrcweir // data members
95cdf0e10cSrcweir sal_uInt32 data[16];
96cdf0e10cSrcweir sal_uInt32 hashdata[5];
97cdf0e10cSrcweir public:
98cdf0e10cSrcweir NIST_Hash( const char* pString, sal_uInt32 nLen );
99cdf0e10cSrcweir
getHash()100cdf0e10cSrcweir sal_uInt32 *getHash() { return hashdata; }
101cdf0e10cSrcweir };
102cdf0e10cSrcweir
transform()103cdf0e10cSrcweir void NIST_Hash::transform()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir // constants
106cdf0e10cSrcweir const sal_uInt32 K2 = 0x5A827999;
107cdf0e10cSrcweir const sal_uInt32 K3 = 0x6ED9EBA1;
108cdf0e10cSrcweir const sal_uInt32 K5 = 0x8F1BBCDC;
109cdf0e10cSrcweir const sal_uInt32 K10 = 0xCA62C1D6;
110cdf0e10cSrcweir
111cdf0e10cSrcweir sal_uInt32 a, b, c, d, e;
112cdf0e10cSrcweir a = hashdata[0];
113cdf0e10cSrcweir b = hashdata[1];
114cdf0e10cSrcweir c = hashdata[2];
115cdf0e10cSrcweir d = hashdata[3];
116cdf0e10cSrcweir e = hashdata[4];
117cdf0e10cSrcweir
118cdf0e10cSrcweir subRound( a, b, c, d, e, K2, data[ 0], 1 );
119cdf0e10cSrcweir subRound( e, a, b, c, d, K2, data[ 1], 1 );
120cdf0e10cSrcweir subRound( d, e, a, b, c, K2, data[ 2], 1 );
121cdf0e10cSrcweir subRound( c, d, e, a, b, K2, data[ 3], 1 );
122cdf0e10cSrcweir subRound( b, c, d, e, a, K2, data[ 4], 1 );
123cdf0e10cSrcweir subRound( a, b, c, d, e, K2, data[ 5], 1 );
124cdf0e10cSrcweir subRound( e, a, b, c, d, K2, data[ 6], 1 );
125cdf0e10cSrcweir subRound( d, e, a, b, c, K2, data[ 7], 1 );
126cdf0e10cSrcweir subRound( c, d, e, a, b, K2, data[ 8], 1 );
127cdf0e10cSrcweir subRound( b, c, d, e, a, K2, data[ 9], 1 );
128cdf0e10cSrcweir subRound( a, b, c, d, e, K2, data[10], 1 );
129cdf0e10cSrcweir subRound( e, a, b, c, d, K2, data[11], 1 );
130cdf0e10cSrcweir subRound( d, e, a, b, c, K2, data[12], 1 );
131cdf0e10cSrcweir subRound( c, d, e, a, b, K2, data[13], 1 );
132cdf0e10cSrcweir subRound( b, c, d, e, a, K2, data[14], 1 );
133cdf0e10cSrcweir subRound( a, b, c, d, e, K2, data[15], 1 );
134cdf0e10cSrcweir subRound( e, a, b, c, d, K2, expand_store( 16 ), 1 );
135cdf0e10cSrcweir subRound( d, e, a, b, c, K2, expand_store( 17 ), 1 );
136cdf0e10cSrcweir subRound( c, d, e, a, b, K2, expand_store( 18 ), 1 );
137cdf0e10cSrcweir subRound( b, c, d, e, a, K2, expand_store( 19 ), 1 );
138cdf0e10cSrcweir
139cdf0e10cSrcweir subRound( a, b, c, d, e, K3, expand_store( 20 ), 2 );
140cdf0e10cSrcweir subRound( e, a, b, c, d, K3, expand_store( 21 ), 2 );
141cdf0e10cSrcweir subRound( d, e, a, b, c, K3, expand_store( 22 ), 2 );
142cdf0e10cSrcweir subRound( c, d, e, a, b, K3, expand_store( 23 ), 2 );
143cdf0e10cSrcweir subRound( b, c, d, e, a, K3, expand_store( 24 ), 2 );
144cdf0e10cSrcweir subRound( a, b, c, d, e, K3, expand_store( 25 ), 2 );
145cdf0e10cSrcweir subRound( e, a, b, c, d, K3, expand_store( 26 ), 2 );
146cdf0e10cSrcweir subRound( d, e, a, b, c, K3, expand_store( 27 ), 2 );
147cdf0e10cSrcweir subRound( c, d, e, a, b, K3, expand_store( 28 ), 2 );
148cdf0e10cSrcweir subRound( b, c, d, e, a, K3, expand_store( 29 ), 2 );
149cdf0e10cSrcweir subRound( a, b, c, d, e, K3, expand_store( 30 ), 2 );
150cdf0e10cSrcweir subRound( e, a, b, c, d, K3, expand_store( 31 ), 2 );
151cdf0e10cSrcweir subRound( d, e, a, b, c, K3, expand_store( 32 ), 2 );
152cdf0e10cSrcweir subRound( c, d, e, a, b, K3, expand_store( 33 ), 2 );
153cdf0e10cSrcweir subRound( b, c, d, e, a, K3, expand_store( 34 ), 2 );
154cdf0e10cSrcweir subRound( a, b, c, d, e, K3, expand_store( 35 ), 2 );
155cdf0e10cSrcweir subRound( e, a, b, c, d, K3, expand_store( 36 ), 2 );
156cdf0e10cSrcweir subRound( d, e, a, b, c, K3, expand_store( 37 ), 2 );
157cdf0e10cSrcweir subRound( c, d, e, a, b, K3, expand_store( 38 ), 2 );
158cdf0e10cSrcweir subRound( b, c, d, e, a, K3, expand_store( 39 ), 2 );
159cdf0e10cSrcweir
160cdf0e10cSrcweir subRound( a, b, c, d, e, K5, expand_store( 40 ), 3 );
161cdf0e10cSrcweir subRound( e, a, b, c, d, K5, expand_store( 41 ), 3 );
162cdf0e10cSrcweir subRound( d, e, a, b, c, K5, expand_store( 42 ), 3 );
163cdf0e10cSrcweir subRound( c, d, e, a, b, K5, expand_store( 43 ), 3 );
164cdf0e10cSrcweir subRound( b, c, d, e, a, K5, expand_store( 44 ), 3 );
165cdf0e10cSrcweir subRound( a, b, c, d, e, K5, expand_store( 45 ), 3 );
166cdf0e10cSrcweir subRound( e, a, b, c, d, K5, expand_store( 46 ), 3 );
167cdf0e10cSrcweir subRound( d, e, a, b, c, K5, expand_store( 47 ), 3 );
168cdf0e10cSrcweir subRound( c, d, e, a, b, K5, expand_store( 48 ), 3 );
169cdf0e10cSrcweir subRound( b, c, d, e, a, K5, expand_store( 49 ), 3 );
170cdf0e10cSrcweir subRound( a, b, c, d, e, K5, expand_store( 50 ), 3 );
171cdf0e10cSrcweir subRound( e, a, b, c, d, K5, expand_store( 51 ), 3 );
172cdf0e10cSrcweir subRound( d, e, a, b, c, K5, expand_store( 52 ), 3 );
173cdf0e10cSrcweir subRound( c, d, e, a, b, K5, expand_store( 53 ), 3 );
174cdf0e10cSrcweir subRound( b, c, d, e, a, K5, expand_store( 54 ), 3 );
175cdf0e10cSrcweir subRound( a, b, c, d, e, K5, expand_store( 55 ), 3 );
176cdf0e10cSrcweir subRound( e, a, b, c, d, K5, expand_store( 56 ), 3 );
177cdf0e10cSrcweir subRound( d, e, a, b, c, K5, expand_store( 57 ), 3 );
178cdf0e10cSrcweir subRound( c, d, e, a, b, K5, expand_store( 58 ), 3 );
179cdf0e10cSrcweir subRound( b, c, d, e, a, K5, expand_store( 59 ), 3 );
180cdf0e10cSrcweir
181cdf0e10cSrcweir subRound( a, b, c, d, e, K10, expand_store( 60 ), 4 );
182cdf0e10cSrcweir subRound( e, a, b, c, d, K10, expand_store( 61 ), 4 );
183cdf0e10cSrcweir subRound( d, e, a, b, c, K10, expand_store( 62 ), 4 );
184cdf0e10cSrcweir subRound( c, d, e, a, b, K10, expand_store( 63 ), 4 );
185cdf0e10cSrcweir subRound( b, c, d, e, a, K10, expand_store( 64 ), 4 );
186cdf0e10cSrcweir subRound( a, b, c, d, e, K10, expand_store( 65 ), 4 );
187cdf0e10cSrcweir subRound( e, a, b, c, d, K10, expand_store( 66 ), 4 );
188cdf0e10cSrcweir subRound( d, e, a, b, c, K10, expand_store( 67 ), 4 );
189cdf0e10cSrcweir subRound( c, d, e, a, b, K10, expand_store( 68 ), 4 );
190cdf0e10cSrcweir subRound( b, c, d, e, a, K10, expand_store( 69 ), 4 );
191cdf0e10cSrcweir subRound( a, b, c, d, e, K10, expand_store( 70 ), 4 );
192cdf0e10cSrcweir subRound( e, a, b, c, d, K10, expand_store( 71 ), 4 );
193cdf0e10cSrcweir subRound( d, e, a, b, c, K10, expand_store( 72 ), 4 );
194cdf0e10cSrcweir subRound( c, d, e, a, b, K10, expand_store( 73 ), 4 );
195cdf0e10cSrcweir subRound( b, c, d, e, a, K10, expand_store( 74 ), 4 );
196cdf0e10cSrcweir subRound( a, b, c, d, e, K10, expand_store( 75 ), 4 );
197cdf0e10cSrcweir subRound( e, a, b, c, d, K10, expand_store( 76 ), 4 );
198cdf0e10cSrcweir subRound( d, e, a, b, c, K10, expand_nostore( 77 ), 4 );
199cdf0e10cSrcweir subRound( c, d, e, a, b, K10, expand_nostore( 78 ), 4 );
200cdf0e10cSrcweir subRound( b, c, d, e, a, K10, expand_nostore( 79 ), 4 );
201cdf0e10cSrcweir
202cdf0e10cSrcweir hashdata[0] += a;
203cdf0e10cSrcweir hashdata[1] += b;
204cdf0e10cSrcweir hashdata[2] += c;
205cdf0e10cSrcweir hashdata[3] += d;
206cdf0e10cSrcweir hashdata[4] += e;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir
209cdf0e10cSrcweir #define BLOCKSIZE sizeof( data )
210cdf0e10cSrcweir
NIST_Hash(const char * pString,sal_uInt32 nLen)211cdf0e10cSrcweir NIST_Hash::NIST_Hash( const char* pString, sal_uInt32 nLen )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir hashdata[0] = 0x67452301;
214cdf0e10cSrcweir hashdata[1] = 0xefcdab89;
215cdf0e10cSrcweir hashdata[2] = 0x98badcfe;
216cdf0e10cSrcweir hashdata[3] = 0x10325476;
217cdf0e10cSrcweir hashdata[4] = 0xc3d2e1f0;
218cdf0e10cSrcweir
219cdf0e10cSrcweir sal_uInt32 nBytes = nLen;
220cdf0e10cSrcweir
221cdf0e10cSrcweir while( nLen >= sizeof( data ) )
222cdf0e10cSrcweir {
223cdf0e10cSrcweir memcpy( data, pString, sizeof( data ) );
224cdf0e10cSrcweir pString += sizeof( data );
225cdf0e10cSrcweir nLen -= sizeof( data );
226cdf0e10cSrcweir transform();
227cdf0e10cSrcweir }
228cdf0e10cSrcweir memcpy( data, pString, nLen );
229cdf0e10cSrcweir ((char*)data)[nLen++] = 0x80;
230cdf0e10cSrcweir if( nLen > sizeof( data ) - 8 )
231cdf0e10cSrcweir {
232cdf0e10cSrcweir memset( ((char*)data)+nLen, 0, sizeof( data ) - nLen );
233cdf0e10cSrcweir transform();
234cdf0e10cSrcweir memset( data, 0, sizeof( data ) - 8 );
235cdf0e10cSrcweir }
236cdf0e10cSrcweir else
237cdf0e10cSrcweir memset( ((char*)data)+nLen, 0, sizeof( data ) - 8 - nLen );
238cdf0e10cSrcweir data[14] = 0;
239cdf0e10cSrcweir data[15] = nBytes << 3;
240cdf0e10cSrcweir transform();
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
243cdf0e10cSrcweir #ifdef TEST
244cdf0e10cSrcweir #include <stdio.h>
main(int argc,const char ** argv)245cdf0e10cSrcweir int main( int argc, const char** argv )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir const char* pHash = argc < 2 ? argv[0] : argv[1];
248cdf0e10cSrcweir
249cdf0e10cSrcweir NIST_Hash aHash( pHash, strlen( pHash ) );
250cdf0e10cSrcweir sal_uInt32* pBits = aHash.getHash();
251cdf0e10cSrcweir
252cdf0e10cSrcweir printf( "text : %s\n"
253cdf0e10cSrcweir "bits : 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
254cdf0e10cSrcweir pHash,
255cdf0e10cSrcweir pBits[0], pBits[1], pBits[2],pBits[3],pBits[4]
256cdf0e10cSrcweir );
257cdf0e10cSrcweir return 0;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir
260cdf0e10cSrcweir #endif
261