]>
Commit | Line | Data |
---|---|---|
be68129d JR |
1 | From 3fbec7d4db59bbd24a9a768182000a1d004c5bc5 Mon Sep 17 00:00:00 2001 |
2 | From: Tiago Vignatti <tiago.vignatti@nokia.com> | |
3 | Date: Wed, 24 Mar 2010 17:27:43 +0200 | |
4 | Subject: [PATCH] Revert "Revert "Render: Use built-in SHA1 library"" | |
5 | ||
6 | This reverts commit a39377cbcbd3091095efbeab25bec18ae520147e. | |
7 | ||
8 | Conflicts: | |
9 | ||
10 | configure.ac | |
11 | include/dix-config.h.in | |
12 | render/glyph.c | |
13 | ||
14 | ||
15 | Ok ok ok, it's the revert of a revert! Buhhh... | |
16 | ||
17 | Once upon a time, back in 2007, Carl Worth was trying to boost render | |
18 | (4c6abe1c). He prefered to use a "strong hash" to compare glyphs (19b3b1fd) | |
19 | and used openssl library for this. Further, for the same purpose, people | |
20 | started to set other SHA1 implementations in autoconf. And a lot of | |
21 | alternatives appeared - six, to be precise. In the mean time, John Tapsell | |
22 | commit a builtin implementation of SHA1. In the same day, Keith Packard | |
23 | reverted, stating "X.org should not be providing a custom SHA1 | |
24 | implementation." (a39377cb). Now, users ended up with Xorg setting the default | |
25 | as the openssl's one (libcrypto), which takes 88 kB of xserver's private RSS. | |
26 | Besides that, we have a ridiculous "configure dot fucking ac stanza to work | |
27 | out which lib to use is almost as long as sha1.c was", to quote daniels. | |
28 | ||
29 | My simple argument against Keith's decision is simple: we can save 316 kB of | |
30 | RSS in a standalone Xorg call. Therefore, I'm in favor to keep our own very | |
31 | simple and shiny SHA1 implementation. | |
32 | ||
33 | --- | |
34 | I'm not comfortable yet to send this patch around without see if there's any | |
35 | regressions on render or eventually get some ack from Carl. | |
36 | ||
37 | ||
38 | configure.ac | 89 +------------------------ | |
39 | include/dix-config.h.in | 15 ---- | |
40 | os/Makefile.am | 1 - | |
41 | os/xsha1.c | 168 --------------------------------------------- | |
42 | render/Makefile.am | 3 + | |
43 | render/glyph.c | 25 ++----- | |
44 | render/sha1.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++ | |
45 | render/sha1.h | 63 +++++++++++++++++ | |
46 | 8 files changed, 248 insertions(+), 289 deletions(-) | |
47 | delete mode 100644 os/xsha1.c | |
48 | create mode 100644 render/sha1.c | |
49 | create mode 100644 render/sha1.h | |
50 | ||
51 | diff --git a/configure.ac b/configure.ac | |
52 | index 3e8ea10..ef21aa0 100644 | |
53 | --- a/configure.ac | |
54 | +++ b/configure.ac | |
55 | @@ -1305,7 +1305,7 @@ AC_DEFINE(BIGREQS, 1, [Support BigRequests extension]) | |
56 | ||
57 | if test "x$SPECIAL_DTRACE_OBJECTS" = "xyes" ; then | |
58 | DIX_LIB='$(top_builddir)/dix/dix.O' | |
59 | - OS_LIB='$(top_builddir)/os/os.O $(SHA1_LIBS)' | |
60 | + OS_LIB='$(top_builddir)/os/os.O' | |
61 | else | |
62 | DIX_LIB='$(top_builddir)/dix/libdix.la' | |
63 | OS_LIB='$(top_builddir)/os/libos.la' | |
1bbb6133 | 64 | @@ -1349,99 +1349,6 @@ |
be68129d JR |
65 | MIEXT_SHADOW_LIB='$(top_builddir)/miext/shadow/libshadow.la' |
66 | CORE_INCS='-I$(top_srcdir)/include -I$(top_builddir)/include' | |
67 | ||
68 | -# SHA1 hashing | |
69 | -AC_ARG_WITH([sha1], | |
70 | - [AS_HELP_STRING([--with-sha1=libc|libmd|libgcrypt|libcrypto|libsha1|CommonCrypto], | |
71 | - [choose SHA1 implementation])]) | |
72 | -AC_CHECK_FUNC([SHA1Init], [HAVE_SHA1_IN_LIBC=yes]) | |
73 | -if test "x$with_sha1" = x && test "x$HAVE_SHA1_IN_LIBC" = xyes; then | |
74 | - with_sha1=libc | |
75 | -fi | |
76 | -if test "x$with_sha1" = xlibc && test "x$HAVE_SHA1_IN_LIBC" != xyes; then | |
77 | - AC_MSG_ERROR([libc requested but not found]) | |
78 | -fi | |
79 | -if test "x$with_sha1" = xlibc; then | |
80 | - AC_DEFINE([HAVE_SHA1_IN_LIBC], [1], | |
81 | - [Use libc SHA1 functions]) | |
82 | - SHA1_LIBS="" | |
83 | -fi | |
84 | -AC_CHECK_FUNC([CC_SHA1_Init], [HAVE_SHA1_IN_COMMONCRYPTO=yes]) | |
85 | -if test "x$with_sha1" = x && test "x$HAVE_SHA1_IN_COMMONCRYPTO" = xyes; then | |
86 | - with_sha1=CommonCrypto | |
87 | -fi | |
88 | -if test "x$with_sha1" = xCommonCrypto && test "x$HAVE_SHA1_IN_COMMONCRYPTO" != xyes; then | |
89 | - AC_MSG_ERROR([CommonCrypto requested but not found]) | |
90 | -fi | |
91 | -if test "x$with_sha1" = xCommonCrypto; then | |
92 | - AC_DEFINE([HAVE_SHA1_IN_COMMONCRYPTO], [1], | |
93 | - [Use CommonCrypto SHA1 functions]) | |
94 | - SHA1_LIBS="" | |
95 | -fi | |
96 | -AC_CHECK_LIB([md], [SHA1Init], [HAVE_LIBMD=yes]) | |
97 | -if test "x$with_sha1" = x && test "x$HAVE_LIBMD" = xyes; then | |
98 | - with_sha1=libmd | |
99 | -fi | |
100 | -if test "x$with_sha1" = xlibmd && test "x$HAVE_LIBMD" != xyes; then | |
101 | - AC_MSG_ERROR([libmd requested but not found]) | |
102 | -fi | |
103 | -if test "x$with_sha1" = xlibmd; then | |
104 | - AC_DEFINE([HAVE_SHA1_IN_LIBMD], [1], | |
105 | - [Use libmd SHA1 functions]) | |
106 | - SHA1_LIBS=-lmd | |
107 | -fi | |
be68129d JR |
108 | -AC_CHECK_LIB([sha1], [sha1_begin], [HAVE_LIBSHA1=yes]) |
109 | -if test "x$with_sha1" = x && test "x$HAVE_LIBSHA1" = xyes; then | |
110 | - with_sha1=libsha1 | |
111 | -fi | |
1bbb6133 AM |
112 | -if test "x$with_sha1" = xlibsha1 && test "x$HAVE_LIBSHA1" != xyes; then |
113 | - AC_MSG_ERROR([libsha1 requested but not found]) | |
114 | -fi | |
be68129d JR |
115 | -if test "x$with_sha1" = xlibsha1; then |
116 | - AC_DEFINE([HAVE_SHA1_IN_LIBSHA1], [1], | |
117 | - [Use libsha1 for SHA1]) | |
118 | - SHA1_LIBS=-lsha1 | |
119 | -fi | |
1bbb6133 AM |
120 | -AC_CHECK_LIB([gcrypt], [gcry_md_open], [HAVE_LIBGCRYPT=yes]) |
121 | -if test "x$with_sha1" = x && test "x$HAVE_LIBGCRYPT" = xyes; then | |
122 | - with_sha1=libgcrypt | |
123 | -fi | |
124 | -if test "x$with_sha1" = xlibgcrypt && test "x$HAVE_LIBGCRYPT" != xyes; then | |
125 | - AC_MSG_ERROR([libgcrypt requested but not found]) | |
126 | -fi | |
127 | -if test "x$with_sha1" = xlibgcrypt; then | |
128 | - AC_DEFINE([HAVE_SHA1_IN_LIBGCRYPT], [1], | |
129 | - [Use libgcrypt SHA1 functions]) | |
130 | - SHA1_LIBS=-lgcrypt | |
131 | -fi | |
be68129d JR |
132 | -# We don't need all of the OpenSSL libraries, just libcrypto |
133 | -AC_CHECK_LIB([crypto], [SHA1_Init], [HAVE_LIBCRYPTO=yes]) | |
134 | -PKG_CHECK_MODULES([OPENSSL], [openssl], [HAVE_OPENSSL_PKC=yes], | |
135 | - [HAVE_OPENSSL_PKC=no]) | |
136 | -if test "x$HAVE_LIBCRYPTO" = xyes || test "x$HAVE_OPENSSL_PKC" = xyes; then | |
137 | - if test "x$with_sha1" = x; then | |
138 | - with_sha1=libcrypto | |
139 | - fi | |
140 | -else | |
141 | - if test "x$with_sha1" = xlibcrypto; then | |
142 | - AC_MSG_ERROR([OpenSSL libcrypto requested but not found]) | |
143 | - fi | |
144 | -fi | |
145 | -if test "x$with_sha1" = xlibcrypto; then | |
146 | - if test "x$HAVE_LIBCRYPTO" = xyes; then | |
147 | - SHA1_LIBS=-lcrypto | |
148 | - else | |
149 | - SHA1_LIBS="$OPENSSL_LIBS" | |
150 | - SHA1_CFLAGS="$OPENSSL_CFLAGS" | |
151 | - fi | |
152 | -fi | |
153 | -AC_MSG_CHECKING([for SHA1 implementation]) | |
154 | -if test "x$with_sha1" = x; then | |
155 | - AC_MSG_ERROR([No suitable SHA1 implementation found]) | |
156 | -fi | |
157 | -AC_MSG_RESULT([$with_sha1]) | |
158 | -AC_SUBST(SHA1_LIBS) | |
159 | -AC_SUBST(SHA1_CFLAGS) | |
160 | - | |
161 | PKG_CHECK_MODULES([XSERVERCFLAGS], [$REQUIRED_MODULES $REQUIRED_LIBS]) | |
162 | PKG_CHECK_MODULES([XSERVERLIBS], [$REQUIRED_LIBS]) | |
163 | ||
164 | diff --git a/include/dix-config.h.in b/include/dix-config.h.in | |
165 | index 058c8fd..d6e99a5 100644 | |
166 | --- a/include/dix-config.h.in | |
167 | +++ b/include/dix-config.h.in | |
168 | @@ -160,21 +160,6 @@ | |
169 | /* Define to 1 if you have the <rpcsvc/dbm.h> header file. */ | |
170 | #undef HAVE_RPCSVC_DBM_H | |
171 | ||
172 | -/* Define to use libc SHA1 functions */ | |
173 | -#undef HAVE_SHA1_IN_LIBC | |
174 | - | |
175 | -/* Define to use CommonCrypto SHA1 functions */ | |
176 | -#undef HAVE_SHA1_IN_COMMONCRYPTO | |
177 | - | |
178 | -/* Define to use libmd SHA1 functions */ | |
179 | -#undef HAVE_SHA1_IN_LIBMD | |
180 | - | |
181 | -/* Define to use libgcrypt SHA1 functions */ | |
182 | -#undef HAVE_SHA1_IN_LIBGCRYPT | |
183 | - | |
184 | -/* Define to use libsha1 for SHA1 */ | |
185 | -#undef HAVE_SHA1_IN_LIBSHA1 | |
186 | - | |
187 | /* Define to 1 if you have the `shmctl64' function. */ | |
188 | #undef HAVE_SHMCTL64 | |
189 | ||
190 | diff --git a/os/Makefile.am b/os/Makefile.am | |
191 | index 66a4a0f..b8c1636 100644 | |
192 | --- a/os/Makefile.am | |
193 | +++ b/os/Makefile.am | |
194 | @@ -22,7 +22,6 @@ libos_la_SOURCES = \ | |
195 | strcasecmp.c \ | |
196 | strcasestr.c \ | |
197 | xdmauth.c \ | |
198 | - xsha1.c \ | |
199 | xstrans.c \ | |
200 | xprintf.c \ | |
201 | $(XORG_SRCS) | |
202 | diff --git a/os/xsha1.c b/os/xsha1.c | |
203 | deleted file mode 100644 | |
204 | index 355862f..0000000 | |
205 | --- a/os/xsha1.c | |
206 | +++ /dev/null | |
207 | @@ -1,168 +0,0 @@ | |
208 | -#ifdef HAVE_DIX_CONFIG_H | |
209 | -#include <dix-config.h> | |
210 | -#endif | |
211 | - | |
212 | -#include "os.h" | |
213 | -#include "xsha1.h" | |
214 | - | |
215 | -#if defined(HAVE_SHA1_IN_LIBMD) /* Use libmd for SHA1 */ \ | |
216 | - || defined(HAVE_SHA1_IN_LIBC) /* Use libc for SHA1 */ | |
217 | - | |
218 | -# include <sha1.h> | |
219 | - | |
220 | -void *x_sha1_init(void) | |
221 | -{ | |
222 | - SHA1_CTX *ctx = xalloc(sizeof(*ctx)); | |
223 | - if (!ctx) | |
224 | - return NULL; | |
225 | - SHA1Init(ctx); | |
226 | - return ctx; | |
227 | -} | |
228 | - | |
229 | -int x_sha1_update(void *ctx, void *data, int size) | |
230 | -{ | |
231 | - SHA1_CTX *sha1_ctx = ctx; | |
232 | - SHA1Update(sha1_ctx, data, size); | |
233 | - return 1; | |
234 | -} | |
235 | - | |
236 | -int x_sha1_final(void *ctx, unsigned char result[20]) | |
237 | -{ | |
238 | - SHA1_CTX *sha1_ctx = ctx; | |
239 | - SHA1Final(result, sha1_ctx); | |
240 | - xfree(sha1_ctx); | |
241 | - return 1; | |
242 | -} | |
243 | - | |
244 | -#elif defined(HAVE_SHA1_IN_COMMONCRYPTO) /* Use CommonCrypto for SHA1 */ | |
245 | - | |
246 | -#include <CommonCrypto/CommonDigest.h> | |
247 | - | |
248 | -void *x_sha1_init(void) | |
249 | -{ | |
250 | - CC_SHA1_CTX *ctx = xalloc(sizeof(*ctx)); | |
251 | - if (!ctx) | |
252 | - return NULL; | |
253 | - CC_SHA1_Init(ctx); | |
254 | - return ctx; | |
255 | -} | |
256 | - | |
257 | -int x_sha1_update(void *ctx, void *data, int size) | |
258 | -{ | |
259 | - CC_SHA1_CTX *sha1_ctx = ctx; | |
260 | - CC_SHA1_Update(sha1_ctx, data, size); | |
261 | - return 1; | |
262 | -} | |
263 | - | |
264 | -int x_sha1_final(void *ctx, unsigned char result[20]) | |
265 | -{ | |
266 | - CC_SHA1_CTX *sha1_ctx = ctx; | |
267 | - CC_SHA1_Final(result, sha1_ctx); | |
268 | - xfree(sha1_ctx); | |
269 | - return 1; | |
270 | -} | |
271 | - | |
272 | -#elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */ | |
273 | - | |
274 | -# include <gcrypt.h> | |
275 | - | |
276 | -void *x_sha1_init(void) | |
277 | -{ | |
278 | - static int init; | |
279 | - gcry_md_hd_t h; | |
280 | - gcry_error_t err; | |
281 | - | |
282 | - if (!init) { | |
283 | - if (!gcry_check_version(NULL)) | |
284 | - return NULL; | |
285 | - gcry_control(GCRYCTL_DISABLE_SECMEM, 0); | |
286 | - gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); | |
287 | - init = 1; | |
288 | - } | |
289 | - | |
290 | - err = gcry_md_open(&h, GCRY_MD_SHA1, 0); | |
291 | - if (err) | |
292 | - return NULL; | |
293 | - return h; | |
294 | -} | |
295 | - | |
296 | -int x_sha1_update(void *ctx, void *data, int size) | |
297 | -{ | |
298 | - gcry_md_hd_t h = ctx; | |
299 | - gcry_md_write(h, data, size); | |
300 | - return 1; | |
301 | -} | |
302 | - | |
303 | -int x_sha1_final(void *ctx, unsigned char result[20]) | |
304 | -{ | |
305 | - gcry_md_hd_t h = ctx; | |
306 | - memcpy(result, gcry_md_read(h, GCRY_MD_SHA1), 20); | |
307 | - gcry_md_close(h); | |
308 | - return 1; | |
309 | -} | |
310 | - | |
311 | -#elif defined(HAVE_SHA1_IN_LIBSHA1) /* Use libsha1 */ | |
312 | - | |
313 | -# include <libsha1.h> | |
314 | - | |
315 | -void *x_sha1_init(void) | |
316 | -{ | |
317 | - sha1_ctx *ctx = xalloc(sizeof(*ctx)); | |
318 | - if(!ctx) | |
319 | - return NULL; | |
320 | - sha1_begin(ctx); | |
321 | - return ctx; | |
322 | -} | |
323 | - | |
324 | -int x_sha1_update(void *ctx, void *data, int size) | |
325 | -{ | |
326 | - sha1_hash(data, size, ctx); | |
327 | - return 1; | |
328 | -} | |
329 | - | |
330 | -int x_sha1_final(void *ctx, unsigned char result[20]) | |
331 | -{ | |
332 | - sha1_end(result, ctx); | |
333 | - xfree(ctx); | |
334 | - return 1; | |
335 | -} | |
336 | - | |
337 | -#else /* Use OpenSSL's libcrypto */ | |
338 | - | |
339 | -# include <stddef.h> /* buggy openssl/sha.h wants size_t */ | |
340 | -# include <openssl/sha.h> | |
341 | - | |
342 | -void *x_sha1_init(void) | |
343 | -{ | |
344 | - int ret; | |
345 | - SHA_CTX *ctx = xalloc(sizeof(*ctx)); | |
346 | - if (!ctx) | |
347 | - return NULL; | |
348 | - ret = SHA1_Init(ctx); | |
349 | - if (!ret) { | |
350 | - xfree(ctx); | |
351 | - return NULL; | |
352 | - } | |
353 | - return ctx; | |
354 | -} | |
355 | - | |
356 | -int x_sha1_update(void *ctx, void *data, int size) | |
357 | -{ | |
358 | - int ret; | |
359 | - SHA_CTX *sha_ctx = ctx; | |
360 | - ret = SHA1_Update(sha_ctx, data, size); | |
361 | - if (!ret) | |
362 | - xfree(sha_ctx); | |
363 | - return ret; | |
364 | -} | |
365 | - | |
366 | -int x_sha1_final(void *ctx, unsigned char result[20]) | |
367 | -{ | |
368 | - int ret; | |
369 | - SHA_CTX *sha_ctx = ctx; | |
370 | - ret = SHA1_Final(result, sha_ctx); | |
371 | - xfree(sha_ctx); | |
372 | - return ret; | |
373 | -} | |
374 | - | |
375 | -#endif | |
376 | diff --git a/render/Makefile.am b/render/Makefile.am | |
377 | index 216c613..bb46702 100644 | |
378 | --- a/render/Makefile.am | |
379 | +++ b/render/Makefile.am | |
380 | @@ -14,8 +14,11 @@ librender_la_SOURCES = \ | |
381 | mitri.c \ | |
382 | picture.c \ | |
383 | render.c \ | |
384 | + sha1.c \ | |
385 | renderedge.c | |
386 | ||
387 | if XORG | |
388 | sdk_HEADERS = picture.h mipict.h glyphstr.h picturestr.h renderedge.h | |
389 | endif | |
390 | + | |
391 | +EXTRA_DIST = sha1.h | |
392 | diff --git a/render/glyph.c b/render/glyph.c | |
393 | index 0b864ad..e14530a 100644 | |
394 | --- a/render/glyph.c | |
395 | +++ b/render/glyph.c | |
396 | @@ -26,8 +26,7 @@ | |
397 | #include <dix-config.h> | |
398 | #endif | |
399 | ||
400 | -#include "xsha1.h" | |
401 | - | |
402 | +#include "sha1.h" | |
403 | #include "misc.h" | |
404 | #include "scrnintstr.h" | |
405 | #include "os.h" | |
406 | @@ -193,21 +192,13 @@ HashGlyph (xGlyphInfo *gi, | |
407 | unsigned long size, | |
408 | unsigned char sha1[20]) | |
409 | { | |
410 | - void *ctx = x_sha1_init(); | |
411 | - int success; | |
412 | - | |
413 | - if (!ctx) | |
414 | - return BadAlloc; | |
415 | - | |
416 | - success = x_sha1_update(ctx, gi, sizeof(xGlyphInfo)); | |
417 | - if (!success) | |
418 | - return BadAlloc; | |
419 | - success = x_sha1_update(ctx, bits, size); | |
420 | - if (!success) | |
421 | - return BadAlloc; | |
422 | - success = x_sha1_final(ctx, sha1); | |
423 | - if (!success) | |
424 | - return BadAlloc; | |
425 | + SHA1_CTX ctx; | |
426 | + | |
427 | + SHA1Init (&ctx); | |
428 | + SHA1Update (&ctx, gi, sizeof (xGlyphInfo)); | |
429 | + SHA1Update (&ctx, bits, size); | |
430 | + SHA1Final (sha1, &ctx); | |
431 | + | |
432 | return Success; | |
433 | } | |
434 | ||
435 | diff --git a/render/sha1.c b/render/sha1.c | |
436 | new file mode 100644 | |
437 | index 0000000..820eb2a | |
438 | --- /dev/null | |
439 | +++ b/render/sha1.c | |
440 | @@ -0,0 +1,173 @@ | |
441 | +/* | |
442 | + * SHA-1 in C | |
443 | + * By Steve Reid <steve@edmweb.com> | |
444 | + * 100% Public Domain | |
445 | + * | |
446 | + * Test Vectors (from FIPS PUB 180-1) | |
447 | + * "abc" | |
448 | + * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D | |
449 | + * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" | |
450 | + * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 | |
451 | + * A million repetitions of "a" | |
452 | + * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F | |
453 | + */ | |
454 | + | |
455 | +#include <sys/param.h> | |
456 | +#include <string.h> | |
457 | +#include <sha1.h> | |
458 | + | |
459 | +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) | |
460 | + | |
461 | +/* | |
462 | + * blk0() and blk() perform the initial expand. | |
463 | + * I got the idea of expanding during the round function from SSLeay | |
464 | + */ | |
465 | +#if BYTE_ORDER == LITTLE_ENDIAN | |
466 | +# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ | |
467 | + |(rol(block->l[i],8)&0x00FF00FF)) | |
468 | +#else | |
469 | +# define blk0(i) block->l[i] | |
470 | +#endif | |
471 | +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ | |
472 | + ^block->l[(i+2)&15]^block->l[i&15],1)) | |
473 | + | |
474 | +/* | |
475 | + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 | |
476 | + */ | |
477 | +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); | |
478 | +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); | |
479 | +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); | |
480 | +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); | |
481 | +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); | |
482 | + | |
483 | +/* | |
484 | + * Hash a single 512-bit block. This is the core of the algorithm. | |
485 | + */ | |
486 | +void | |
487 | +SHA1Transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCK_LENGTH]) | |
488 | +{ | |
489 | + uint32_t a, b, c, d, e; | |
490 | + uint8_t workspace[SHA1_BLOCK_LENGTH]; | |
491 | + typedef union { | |
492 | + uint8_t c[64]; | |
493 | + uint32_t l[16]; | |
494 | + } CHAR64LONG16; | |
495 | + CHAR64LONG16 *block = (CHAR64LONG16 *)workspace; | |
496 | + | |
497 | + (void)memcpy(block, buffer, SHA1_BLOCK_LENGTH); | |
498 | + | |
499 | + /* Copy context->state[] to working vars */ | |
500 | + a = state[0]; | |
501 | + b = state[1]; | |
502 | + c = state[2]; | |
503 | + d = state[3]; | |
504 | + e = state[4]; | |
505 | + | |
506 | + /* 4 rounds of 20 operations each. Loop unrolled. */ | |
507 | + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); | |
508 | + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); | |
509 | + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); | |
510 | + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); | |
511 | + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); | |
512 | + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); | |
513 | + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); | |
514 | + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); | |
515 | + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); | |
516 | + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); | |
517 | + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); | |
518 | + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); | |
519 | + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); | |
520 | + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); | |
521 | + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); | |
522 | + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); | |
523 | + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); | |
524 | + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); | |
525 | + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); | |
526 | + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); | |
527 | + | |
528 | + /* Add the working vars back into context.state[] */ | |
529 | + state[0] += a; | |
530 | + state[1] += b; | |
531 | + state[2] += c; | |
532 | + state[3] += d; | |
533 | + state[4] += e; | |
534 | + | |
535 | + /* Wipe variables */ | |
536 | + a = b = c = d = e = 0; | |
537 | +} | |
538 | + | |
539 | + | |
540 | +/* | |
541 | + * SHA1Init - Initialize new context | |
542 | + */ | |
543 | +void | |
544 | +SHA1Init(SHA1_CTX *context) | |
545 | +{ | |
546 | + | |
547 | + /* SHA1 initialization constants */ | |
548 | + context->count = 0; | |
549 | + context->state[0] = 0x67452301; | |
550 | + context->state[1] = 0xEFCDAB89; | |
551 | + context->state[2] = 0x98BADCFE; | |
552 | + context->state[3] = 0x10325476; | |
553 | + context->state[4] = 0xC3D2E1F0; | |
554 | +} | |
555 | + | |
556 | + | |
557 | +/* | |
558 | + * Run your data through this. | |
559 | + */ | |
560 | +void | |
561 | +SHA1Update(SHA1_CTX *context, const uint8_t *data, size_t len) | |
562 | +{ | |
563 | + size_t i, j; | |
564 | + | |
565 | + j = (size_t)((context->count >> 3) & 63); | |
566 | + context->count += (len << 3); | |
567 | + if ((j + len) > 63) { | |
568 | + (void)memcpy(&context->buffer[j], data, (i = 64-j)); | |
569 | + SHA1Transform(context->state, context->buffer); | |
570 | + for ( ; i + 63 < len; i += 64) | |
571 | + SHA1Transform(context->state, (uint8_t *)&data[i]); | |
572 | + j = 0; | |
573 | + } else { | |
574 | + i = 0; | |
575 | + } | |
576 | + (void)memcpy(&context->buffer[j], &data[i], len - i); | |
577 | +} | |
578 | + | |
579 | + | |
580 | +/* | |
581 | + * Add padding and return the message digest. | |
582 | + */ | |
583 | +void | |
584 | +SHA1Pad(SHA1_CTX *context) | |
585 | +{ | |
586 | + uint8_t finalcount[8]; | |
587 | + uint i; | |
588 | + | |
589 | + for (i = 0; i < 8; i++) { | |
590 | + finalcount[i] = (uint8_t)((context->count >> | |
591 | + ((7 - (i & 7)) * 8)) & 255); /* Endian independent */ | |
592 | + } | |
593 | + SHA1Update(context, (uint8_t *)"\200", 1); | |
594 | + while ((context->count & 504) != 448) | |
595 | + SHA1Update(context, (uint8_t *)"\0", 1); | |
596 | + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ | |
597 | +} | |
598 | + | |
599 | +void | |
600 | +SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context) | |
601 | +{ | |
602 | + uint i; | |
603 | + | |
604 | + SHA1Pad(context); | |
605 | + if (digest) { | |
606 | + for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { | |
607 | + digest[i] = (uint8_t) | |
608 | + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); | |
609 | + } | |
610 | + memset(context, 0, sizeof(*context)); | |
611 | + } | |
612 | +} | |
613 | + | |
614 | diff --git a/render/sha1.h b/render/sha1.h | |
615 | new file mode 100644 | |
616 | index 0000000..ace7d97 | |
617 | --- /dev/null | |
618 | +++ b/render/sha1.h | |
619 | @@ -0,0 +1,63 @@ | |
620 | +/* | |
621 | + * SHA-1 in C | |
622 | + * By Steve Reid <steve@edmweb.com> | |
623 | + * 100% Public Domain | |
624 | + */ | |
625 | + | |
626 | +#ifndef _SHA1_H | |
627 | +#define _SHA1_H | |
628 | + | |
629 | +#include <stdint.h> | |
630 | +#include <stddef.h> | |
631 | +#include <unistd.h> | |
632 | + | |
633 | + | |
634 | +#define SHA1_BLOCK_LENGTH 64 | |
635 | +#define SHA1_DIGEST_LENGTH 20 | |
636 | +#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1) | |
637 | + | |
638 | +typedef struct { | |
639 | + uint32_t state[5]; | |
640 | + uint64_t count; | |
641 | + uint8_t buffer[SHA1_BLOCK_LENGTH]; | |
642 | +} SHA1_CTX; | |
643 | + | |
644 | +#include <sys/cdefs.h> | |
645 | + | |
646 | +__BEGIN_DECLS | |
647 | +void SHA1Init(SHA1_CTX *); | |
648 | +void SHA1Pad(SHA1_CTX *); | |
649 | +void SHA1Transform(uint32_t [5], const uint8_t [SHA1_BLOCK_LENGTH]) | |
650 | + __attribute__((__bounded__(__minbytes__,1,5))) | |
651 | + __attribute__((__bounded__(__minbytes__,2,SHA1_BLOCK_LENGTH))); | |
652 | +void SHA1Update(SHA1_CTX *, const uint8_t *, size_t) | |
653 | + __attribute__((__bounded__(__string__,2,3))); | |
654 | +void SHA1Final(uint8_t [SHA1_DIGEST_LENGTH], SHA1_CTX *) | |
655 | + __attribute__((__bounded__(__minbytes__,1,SHA1_DIGEST_LENGTH))); | |
656 | +char *SHA1End(SHA1_CTX *, char *) | |
657 | + __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH))); | |
658 | +char *SHA1File(const char *, char *) | |
659 | + __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH))); | |
660 | +char *SHA1FileChunk(const char *, char *, off_t, off_t) | |
661 | + __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH))); | |
662 | +char *SHA1Data(const uint8_t *, size_t, char *) | |
663 | + __attribute__((__bounded__(__string__,1,2))) | |
664 | + __attribute__((__bounded__(__minbytes__,3,SHA1_DIGEST_STRING_LENGTH))); | |
665 | +__END_DECLS | |
666 | + | |
667 | +#define HTONDIGEST(x) do { \ | |
668 | + x[0] = htonl(x[0]); \ | |
669 | + x[1] = htonl(x[1]); \ | |
670 | + x[2] = htonl(x[2]); \ | |
671 | + x[3] = htonl(x[3]); \ | |
672 | + x[4] = htonl(x[4]); } while (0) | |
673 | + | |
674 | +#define NTOHDIGEST(x) do { \ | |
675 | + x[0] = ntohl(x[0]); \ | |
676 | + x[1] = ntohl(x[1]); \ | |
677 | + x[2] = ntohl(x[2]); \ | |
678 | + x[3] = ntohl(x[3]); \ | |
679 | + x[4] = ntohl(x[4]); } while (0) | |
680 | + | |
681 | +#endif /* _SHA1_H */ | |
682 | + | |
683 | -- | |
684 | 1.6.0.4 | |
685 |