Initial commit
[keepassx] / src / crypto / sha256.cpp
1 /***************************************************************************
2  *   Copyright (C) 2001-2003 by Christophe Devine                          *
3  *   Copyright (C) 2005-2006 by Tarek Saidi                                *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; version 2 of the License.               *
8
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <string.h>
22 #include "sha256.h"
23
24 #define GET_qquint32(n,b,i)                       \
25 {                                               \
26     (n) = ( (quint32) (b)[(i)    ] << 24 )       \
27         | ( (quint32) (b)[(i) + 1] << 16 )       \
28         | ( (quint32) (b)[(i) + 2] <<  8 )       \
29         | ( (quint32) (b)[(i) + 3]       );      \
30 }
31
32 #define PUT_qquint32(n,b,i)                       \
33 {                                               \
34     (b)[(i)    ] = (quint8) ( (n) >> 24 );       \
35     (b)[(i) + 1] = (quint8) ( (n) >> 16 );       \
36     (b)[(i) + 2] = (quint8) ( (n) >>  8 );       \
37     (b)[(i) + 3] = (quint8) ( (n)       );       \
38 }
39
40 void SHA256::hashBuffer(const void* input, void* digest, quint32 length){
41         sha256_context ctx;
42         sha256_starts(&ctx);
43         sha256_update(&ctx,(quint8*)input,length);
44         sha256_finish(&ctx,(quint8*)digest);
45         overwriteCtx(&ctx);
46 }
47
48 void SHA256::overwriteCtx(sha256_context* ctx) {
49         ctx->total[0] = 0;
50         ctx->total[1] = 0;
51         
52         for (int i=0; i<8; i++) {
53                 ctx->state[i] = 0;
54         }
55         
56         for (int i=0; i<8; i++) {
57                 ctx->buffer[i] = 0;
58         }
59 }
60
61 void sha256_starts( sha256_context *ctx )
62 {
63     ctx->total[0] = 0;
64     ctx->total[1] = 0;
65
66     ctx->state[0] = 0x6A09E667;
67     ctx->state[1] = 0xBB67AE85;
68     ctx->state[2] = 0x3C6EF372;
69     ctx->state[3] = 0xA54FF53A;
70     ctx->state[4] = 0x510E527F;
71     ctx->state[5] = 0x9B05688C;
72     ctx->state[6] = 0x1F83D9AB;
73     ctx->state[7] = 0x5BE0CD19;
74 }
75
76 void sha256_process( sha256_context *ctx, const quint8 data[64] )
77 {
78     quint32 temp1, temp2, W[64];
79     quint32 A, B, C, D, E, F, G, H;
80
81     GET_qquint32( W[0],  data,  0 );
82     GET_qquint32( W[1],  data,  4 );
83     GET_qquint32( W[2],  data,  8 );
84     GET_qquint32( W[3],  data, 12 );
85     GET_qquint32( W[4],  data, 16 );
86     GET_qquint32( W[5],  data, 20 );
87     GET_qquint32( W[6],  data, 24 );
88     GET_qquint32( W[7],  data, 28 );
89     GET_qquint32( W[8],  data, 32 );
90     GET_qquint32( W[9],  data, 36 );
91     GET_qquint32( W[10], data, 40 );
92     GET_qquint32( W[11], data, 44 );
93     GET_qquint32( W[12], data, 48 );
94     GET_qquint32( W[13], data, 52 );
95     GET_qquint32( W[14], data, 56 );
96     GET_qquint32( W[15], data, 60 );
97
98 #define  SHR(x,n) ((x & 0xFFFFFFFF) >> n)
99 #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
100
101 #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^  SHR(x, 3))
102 #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^  SHR(x,10))
103
104 #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
105 #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
106
107 #define F0(x,y,z) ((x & y) | (z & (x | y)))
108 #define F1(x,y,z) (z ^ (x & (y ^ z)))
109
110 #define R(t)                                    \
111 (                                               \
112     W[t] = S1(W[t -  2]) + W[t -  7] +          \
113            S0(W[t - 15]) + W[t - 16]            \
114 )
115
116 #define P(a,b,c,d,e,f,g,h,x,K)                  \
117 {                                               \
118     temp1 = h + S3(e) + F1(e,f,g) + K + x;      \
119     temp2 = S2(a) + F0(a,b,c);                  \
120     d += temp1; h = temp1 + temp2;              \
121 }
122
123     A = ctx->state[0];
124     B = ctx->state[1];
125     C = ctx->state[2];
126     D = ctx->state[3];
127     E = ctx->state[4];
128     F = ctx->state[5];
129     G = ctx->state[6];
130     H = ctx->state[7];
131
132     P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
133     P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
134     P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
135     P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
136     P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
137     P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
138     P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
139     P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
140     P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
141     P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
142     P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
143     P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
144     P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
145     P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
146     P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
147     P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
148     P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
149     P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
150     P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
151     P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
152     P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
153     P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
154     P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
155     P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
156     P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
157     P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
158     P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
159     P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
160     P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
161     P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
162     P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
163     P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
164     P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
165     P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
166     P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
167     P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
168     P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
169     P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
170     P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
171     P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
172     P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
173     P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
174     P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
175     P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
176     P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
177     P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
178     P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
179     P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
180     P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
181     P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
182     P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
183     P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
184     P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
185     P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
186     P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
187     P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
188     P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
189     P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
190     P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
191     P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
192     P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
193     P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
194     P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
195     P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
196
197     ctx->state[0] += A;
198     ctx->state[1] += B;
199     ctx->state[2] += C;
200     ctx->state[3] += D;
201     ctx->state[4] += E;
202     ctx->state[5] += F;
203     ctx->state[6] += G;
204     ctx->state[7] += H;
205 }
206
207 void sha256_update( sha256_context *ctx, const quint8 *input, quint32 length )
208 {
209     quint32 left, fill;
210
211     if( ! length ) return;
212
213     left = ctx->total[0] & 0x3F;
214     fill = 64 - left;
215
216     ctx->total[0] += length;
217     ctx->total[0] &= 0xFFFFFFFF;
218
219     if( ctx->total[0] < length )
220         ctx->total[1]++;
221
222     if( left && length >= fill )
223     {
224         memcpy( (void *) (ctx->buffer + left),
225                 (void *) input, fill );
226         sha256_process( ctx, ctx->buffer );
227         length -= fill;
228         input  += fill;
229         left = 0;
230     }
231
232     while( length >= 64 )
233     {
234         sha256_process( ctx, input );
235         length -= 64;
236         input  += 64;
237     }
238
239     if( length )
240     {
241         memcpy( (void *) (ctx->buffer + left),
242                 (void *) input, length );
243     }
244 }
245
246 static quint8 sha256_padding[64] =
247 {
248  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
249     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
252 };
253
254 void sha256_finish( sha256_context *ctx, quint8 digest[32] )
255 {
256     quint32 last, padn;
257     quint32 high, low;
258     quint8 msglen[8];
259
260     high = ( ctx->total[0] >> 29 )
261          | ( ctx->total[1] <<  3 );
262     low  = ( ctx->total[0] <<  3 );
263
264     PUT_qquint32( high, msglen, 0 );
265     PUT_qquint32( low,  msglen, 4 );
266
267     last = ctx->total[0] & 0x3F;
268     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
269
270     sha256_update( ctx, sha256_padding, padn );
271     sha256_update( ctx, msglen, 8 );
272
273     PUT_qquint32( ctx->state[0], digest,  0 );
274     PUT_qquint32( ctx->state[1], digest,  4 );
275     PUT_qquint32( ctx->state[2], digest,  8 );
276     PUT_qquint32( ctx->state[3], digest, 12 );
277     PUT_qquint32( ctx->state[4], digest, 16 );
278     PUT_qquint32( ctx->state[5], digest, 20 );
279     PUT_qquint32( ctx->state[6], digest, 24 );
280     PUT_qquint32( ctx->state[7], digest, 28 );
281 }