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