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