Miscellaneous changes.
[cumulus.git] / sha1.cc
1 /* sha1.cc - Functions to compute SHA1 message digest of data streams
2    according to the NIST specification FIPS-180-1.
3
4    Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
5    Copyright (C) 2006 Michael Vrable
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* Written by Scott G. Miller
22    Credits:
23       Robert Klep <robert@ilse.nl>  -- Expansion function fix
24
25    Modified by Michael Vrable <mvrable@cs.ucsd.edu> to simplify the interface
26    and add an object-oriented wrapper.  Original code (in C) taken from GNU
27    coreutils (Debian package 5.97-5).
28 */
29
30 #include "sha1.h"
31
32 #include <stddef.h>
33 #include <string.h>
34 #include <arpa/inet.h>
35
36 #include <string.h>
37
38 using std::string;
39
40 /* SWAP does an endian swap on architectures that are little-endian,
41    as SHA1 needs some data in a big-endian form.  */
42 #define SWAP(n) htonl(n)
43
44 #define BLOCKSIZE 4096
45 #if BLOCKSIZE % 64 != 0
46 # error "invalid BLOCKSIZE"
47 #endif
48
49 /* This array contains the bytes used to pad the buffer to the next
50    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
51 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
52
53
54 /*
55   Takes a pointer to a 160 bit block of data (five 32 bit ints) and
56   intializes it to the start constants of the SHA1 algorithm.  This
57   must be called before using hash in the call to sha1_hash.
58 */
59 void
60 sha1_init_ctx (struct sha1_ctx *ctx)
61 {
62   ctx->A = 0x67452301;
63   ctx->B = 0xefcdab89;
64   ctx->C = 0x98badcfe;
65   ctx->D = 0x10325476;
66   ctx->E = 0xc3d2e1f0;
67
68   ctx->total[0] = ctx->total[1] = 0;
69   ctx->buflen = 0;
70 }
71
72 /* Put result from CTX in first 20 bytes following RESBUF.  The result
73    must be in little endian byte order.
74
75    IMPORTANT: On some systems it is required that RESBUF is correctly
76    aligned for a 32 bits value.  */
77 void *
78 sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
79 {
80   ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
81   ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
82   ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
83   ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
84   ((md5_uint32 *) resbuf)[4] = SWAP (ctx->E);
85
86   return resbuf;
87 }
88
89 /* Process the remaining bytes in the internal buffer and the usual
90    prolog according to the standard and write the result to RESBUF.
91
92    IMPORTANT: On some systems it is required that RESBUF is correctly
93    aligned for a 32 bits value.  */
94 void *
95 sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
96 {
97   /* Take yet unprocessed bytes into account.  */
98   md5_uint32 bytes = ctx->buflen;
99   size_t pad;
100
101   /* Now count remaining bytes.  */
102   ctx->total[0] += bytes;
103   if (ctx->total[0] < bytes)
104     ++ctx->total[1];
105
106   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
107   memcpy (&ctx->buffer[bytes], fillbuf, pad);
108
109   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
110   *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
111   *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
112                                                     (ctx->total[0] >> 29));
113
114   /* Process last bytes.  */
115   sha1_process_block (ctx->buffer, bytes + pad + 8, ctx);
116
117   return sha1_read_ctx (ctx, resbuf);
118 }
119
120 void
121 sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
122 {
123   /* When we already have some bits in our internal buffer concatenate
124      both inputs first.  */
125   if (ctx->buflen != 0)
126     {
127       size_t left_over = ctx->buflen;
128       size_t add = 128 - left_over > len ? len : 128 - left_over;
129
130       memcpy (&ctx->buffer[left_over], buffer, add);
131       ctx->buflen += add;
132
133       if (ctx->buflen > 64)
134         {
135           sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
136
137           ctx->buflen &= 63;
138           /* The regions in the following copy operation cannot overlap.  */
139           memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
140                   ctx->buflen);
141         }
142
143       buffer = (const char *) buffer + add;
144       len -= add;
145     }
146
147   /* Process available complete blocks.  */
148   if (len >= 64)
149     {
150 #if !_STRING_ARCH_unaligned
151 # define alignof(type) offsetof (struct { char c; type x; }, x)
152 # define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0)
153       if (UNALIGNED_P (buffer))
154         while (len > 64)
155           {
156             sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
157             buffer = (const char *) buffer + 64;
158             len -= 64;
159           }
160       else
161 #endif
162         {
163           sha1_process_block (buffer, len & ~63, ctx);
164           buffer = (const char *) buffer + (len & ~63);
165           len &= 63;
166         }
167     }
168
169   /* Move remaining bytes in internal buffer.  */
170   if (len > 0)
171     {
172       size_t left_over = ctx->buflen;
173
174       memcpy (&ctx->buffer[left_over], buffer, len);
175       left_over += len;
176       if (left_over >= 64)
177         {
178           sha1_process_block (ctx->buffer, 64, ctx);
179           left_over -= 64;
180           memcpy (ctx->buffer, &ctx->buffer[64], left_over);
181         }
182       ctx->buflen = left_over;
183     }
184 }
185
186 /* --- Code below is the primary difference between md5.c and sha1.c --- */
187
188 /* SHA1 round constants */
189 #define K1 0x5a827999L
190 #define K2 0x6ed9eba1L
191 #define K3 0x8f1bbcdcL
192 #define K4 0xca62c1d6L
193
194 /* Round functions.  Note that F2 is the same as F4.  */
195 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
196 #define F2(B,C,D) (B ^ C ^ D)
197 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
198 #define F4(B,C,D) (B ^ C ^ D)
199
200 /* Process LEN bytes of BUFFER, accumulating context into CTX.
201    It is assumed that LEN % 64 == 0.
202    Most of this code comes from GnuPG's cipher/sha1.c.  */
203
204 void
205 sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
206 {
207   const md5_uint32 *words = (const md5_uint32 *)buffer;
208   size_t nwords = len / sizeof (md5_uint32);
209   const md5_uint32 *endp = words + nwords;
210   md5_uint32 x[16];
211   md5_uint32 a = ctx->A;
212   md5_uint32 b = ctx->B;
213   md5_uint32 c = ctx->C;
214   md5_uint32 d = ctx->D;
215   md5_uint32 e = ctx->E;
216
217   /* First increment the byte count.  RFC 1321 specifies the possible
218      length of the file up to 2^64 bits.  Here we only compute the
219      number of bytes.  Do a double word increment.  */
220   ctx->total[0] += len;
221   if (ctx->total[0] < len)
222     ++ctx->total[1];
223
224 #define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
225
226 #define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
227                     ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
228                , (x[I&0x0f] = rol(tm, 1)) )
229
230 #define R(A,B,C,D,E,F,K,M)  do { E += rol( A, 5 )     \
231                                       + F( B, C, D )  \
232                                       + K             \
233                                       + M;            \
234                                  B = rol( B, 30 );    \
235                                } while(0)
236
237   while (words < endp)
238     {
239       md5_uint32 tm;
240       int t;
241       for (t = 0; t < 16; t++)
242         {
243           x[t] = SWAP (*words);
244           words++;
245         }
246
247       R( a, b, c, d, e, F1, K1, x[ 0] );
248       R( e, a, b, c, d, F1, K1, x[ 1] );
249       R( d, e, a, b, c, F1, K1, x[ 2] );
250       R( c, d, e, a, b, F1, K1, x[ 3] );
251       R( b, c, d, e, a, F1, K1, x[ 4] );
252       R( a, b, c, d, e, F1, K1, x[ 5] );
253       R( e, a, b, c, d, F1, K1, x[ 6] );
254       R( d, e, a, b, c, F1, K1, x[ 7] );
255       R( c, d, e, a, b, F1, K1, x[ 8] );
256       R( b, c, d, e, a, F1, K1, x[ 9] );
257       R( a, b, c, d, e, F1, K1, x[10] );
258       R( e, a, b, c, d, F1, K1, x[11] );
259       R( d, e, a, b, c, F1, K1, x[12] );
260       R( c, d, e, a, b, F1, K1, x[13] );
261       R( b, c, d, e, a, F1, K1, x[14] );
262       R( a, b, c, d, e, F1, K1, x[15] );
263       R( e, a, b, c, d, F1, K1, M(16) );
264       R( d, e, a, b, c, F1, K1, M(17) );
265       R( c, d, e, a, b, F1, K1, M(18) );
266       R( b, c, d, e, a, F1, K1, M(19) );
267       R( a, b, c, d, e, F2, K2, M(20) );
268       R( e, a, b, c, d, F2, K2, M(21) );
269       R( d, e, a, b, c, F2, K2, M(22) );
270       R( c, d, e, a, b, F2, K2, M(23) );
271       R( b, c, d, e, a, F2, K2, M(24) );
272       R( a, b, c, d, e, F2, K2, M(25) );
273       R( e, a, b, c, d, F2, K2, M(26) );
274       R( d, e, a, b, c, F2, K2, M(27) );
275       R( c, d, e, a, b, F2, K2, M(28) );
276       R( b, c, d, e, a, F2, K2, M(29) );
277       R( a, b, c, d, e, F2, K2, M(30) );
278       R( e, a, b, c, d, F2, K2, M(31) );
279       R( d, e, a, b, c, F2, K2, M(32) );
280       R( c, d, e, a, b, F2, K2, M(33) );
281       R( b, c, d, e, a, F2, K2, M(34) );
282       R( a, b, c, d, e, F2, K2, M(35) );
283       R( e, a, b, c, d, F2, K2, M(36) );
284       R( d, e, a, b, c, F2, K2, M(37) );
285       R( c, d, e, a, b, F2, K2, M(38) );
286       R( b, c, d, e, a, F2, K2, M(39) );
287       R( a, b, c, d, e, F3, K3, M(40) );
288       R( e, a, b, c, d, F3, K3, M(41) );
289       R( d, e, a, b, c, F3, K3, M(42) );
290       R( c, d, e, a, b, F3, K3, M(43) );
291       R( b, c, d, e, a, F3, K3, M(44) );
292       R( a, b, c, d, e, F3, K3, M(45) );
293       R( e, a, b, c, d, F3, K3, M(46) );
294       R( d, e, a, b, c, F3, K3, M(47) );
295       R( c, d, e, a, b, F3, K3, M(48) );
296       R( b, c, d, e, a, F3, K3, M(49) );
297       R( a, b, c, d, e, F3, K3, M(50) );
298       R( e, a, b, c, d, F3, K3, M(51) );
299       R( d, e, a, b, c, F3, K3, M(52) );
300       R( c, d, e, a, b, F3, K3, M(53) );
301       R( b, c, d, e, a, F3, K3, M(54) );
302       R( a, b, c, d, e, F3, K3, M(55) );
303       R( e, a, b, c, d, F3, K3, M(56) );
304       R( d, e, a, b, c, F3, K3, M(57) );
305       R( c, d, e, a, b, F3, K3, M(58) );
306       R( b, c, d, e, a, F3, K3, M(59) );
307       R( a, b, c, d, e, F4, K4, M(60) );
308       R( e, a, b, c, d, F4, K4, M(61) );
309       R( d, e, a, b, c, F4, K4, M(62) );
310       R( c, d, e, a, b, F4, K4, M(63) );
311       R( b, c, d, e, a, F4, K4, M(64) );
312       R( a, b, c, d, e, F4, K4, M(65) );
313       R( e, a, b, c, d, F4, K4, M(66) );
314       R( d, e, a, b, c, F4, K4, M(67) );
315       R( c, d, e, a, b, F4, K4, M(68) );
316       R( b, c, d, e, a, F4, K4, M(69) );
317       R( a, b, c, d, e, F4, K4, M(70) );
318       R( e, a, b, c, d, F4, K4, M(71) );
319       R( d, e, a, b, c, F4, K4, M(72) );
320       R( c, d, e, a, b, F4, K4, M(73) );
321       R( b, c, d, e, a, F4, K4, M(74) );
322       R( a, b, c, d, e, F4, K4, M(75) );
323       R( e, a, b, c, d, F4, K4, M(76) );
324       R( d, e, a, b, c, F4, K4, M(77) );
325       R( c, d, e, a, b, F4, K4, M(78) );
326       R( b, c, d, e, a, F4, K4, M(79) );
327
328       a = ctx->A += a;
329       b = ctx->B += b;
330       c = ctx->C += c;
331       d = ctx->D += d;
332       e = ctx->E += e;
333     }
334 }
335
336 /* ---- Object-Oriented Wrapper */
337 SHA1Checksum::SHA1Checksum()
338 {
339     sha1_init_ctx(&ctx);
340 }
341
342 SHA1Checksum::~SHA1Checksum()
343 {
344 }
345
346 void SHA1Checksum::process(const void *data, size_t len)
347 {
348     sha1_process_bytes(data, len, &ctx);
349 }
350
351 const uint8_t *SHA1Checksum::checksum()
352 {
353     sha1_finish_ctx(&ctx, resbuf);
354     return (const uint8_t *)resbuf;
355 }
356
357 string SHA1Checksum::checksum_str()
358 {
359     uint8_t resbuf[20];
360     char hexbuf[4];
361     string result = "sha1=";
362
363     sha1_finish_ctx(&ctx, resbuf);
364
365     for (int i = 0; i < 20; i++) {
366         sprintf(hexbuf, "%02x", resbuf[i]);
367         result += hexbuf;
368     }
369
370     return result;
371 }