Annotation of /trunk/mkinitrd-magellan/busybox/libbb/sha1.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 5308 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 5308 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Based on shasum from http://www.netsw.org/crypto/hash/ | ||
4 | * Majorly hacked up to use Dr Brian Gladman's sha1 code | ||
5 | * | ||
6 | * Copyright (C) 2002 Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. | ||
7 | * Copyright (C) 2003 Glenn L. McGrath | ||
8 | * Copyright (C) 2003 Erik Andersen | ||
9 | * | ||
10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
11 | * | ||
12 | * --------------------------------------------------------------------------- | ||
13 | * Issue Date: 10/11/2002 | ||
14 | * | ||
15 | * This is a byte oriented version of SHA1 that operates on arrays of bytes | ||
16 | * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor | ||
17 | */ | ||
18 | |||
19 | #include <fcntl.h> | ||
20 | #include <limits.h> | ||
21 | #include <stdio.h> | ||
22 | #include <stdint.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <unistd.h> | ||
26 | |||
27 | #include "libbb.h" | ||
28 | |||
29 | #define SHA1_BLOCK_SIZE 64 | ||
30 | #define SHA1_DIGEST_SIZE 20 | ||
31 | #define SHA1_HASH_SIZE SHA1_DIGEST_SIZE | ||
32 | #define SHA2_GOOD 0 | ||
33 | #define SHA2_BAD 1 | ||
34 | |||
35 | #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) | ||
36 | |||
37 | #define SHA1_MASK (SHA1_BLOCK_SIZE - 1) | ||
38 | |||
39 | /* reverse byte order in 32-bit words */ | ||
40 | #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) | ||
41 | #define parity(x,y,z) ((x) ^ (y) ^ (z)) | ||
42 | #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) | ||
43 | |||
44 | /* A normal version as set out in the FIPS. This version uses */ | ||
45 | /* partial loop unrolling and is optimised for the Pentium 4 */ | ||
46 | #define rnd(f,k) \ | ||
47 | do { \ | ||
48 | t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \ | ||
49 | e = d; d = c; c = rotl32(b, 30); b = t; \ | ||
50 | } while (0) | ||
51 | |||
52 | static void sha1_compile(sha1_ctx_t *ctx) | ||
53 | { | ||
54 | uint32_t w[80], i, a, b, c, d, e, t; | ||
55 | |||
56 | /* note that words are compiled from the buffer into 32-bit */ | ||
57 | /* words in big-endian order so an order reversal is needed */ | ||
58 | /* here on little endian machines */ | ||
59 | for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i) | ||
60 | w[i] = htonl(ctx->wbuf[i]); | ||
61 | |||
62 | for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i) | ||
63 | w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); | ||
64 | |||
65 | a = ctx->hash[0]; | ||
66 | b = ctx->hash[1]; | ||
67 | c = ctx->hash[2]; | ||
68 | d = ctx->hash[3]; | ||
69 | e = ctx->hash[4]; | ||
70 | |||
71 | for (i = 0; i < 20; ++i) { | ||
72 | rnd(ch, 0x5a827999); | ||
73 | } | ||
74 | |||
75 | for (i = 20; i < 40; ++i) { | ||
76 | rnd(parity, 0x6ed9eba1); | ||
77 | } | ||
78 | |||
79 | for (i = 40; i < 60; ++i) { | ||
80 | rnd(maj, 0x8f1bbcdc); | ||
81 | } | ||
82 | |||
83 | for (i = 60; i < 80; ++i) { | ||
84 | rnd(parity, 0xca62c1d6); | ||
85 | } | ||
86 | |||
87 | ctx->hash[0] += a; | ||
88 | ctx->hash[1] += b; | ||
89 | ctx->hash[2] += c; | ||
90 | ctx->hash[3] += d; | ||
91 | ctx->hash[4] += e; | ||
92 | } | ||
93 | |||
94 | void sha1_begin(sha1_ctx_t *ctx) | ||
95 | { | ||
96 | ctx->count[0] = ctx->count[1] = 0; | ||
97 | ctx->hash[0] = 0x67452301; | ||
98 | ctx->hash[1] = 0xefcdab89; | ||
99 | ctx->hash[2] = 0x98badcfe; | ||
100 | ctx->hash[3] = 0x10325476; | ||
101 | ctx->hash[4] = 0xc3d2e1f0; | ||
102 | } | ||
103 | |||
104 | /* SHA1 hash data in an array of bytes into hash buffer and call the */ | ||
105 | /* hash_compile function as required. */ | ||
106 | void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx) | ||
107 | { | ||
108 | uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK); | ||
109 | uint32_t freeb = SHA1_BLOCK_SIZE - pos; | ||
110 | const unsigned char *sp = data; | ||
111 | |||
112 | if ((ctx->count[0] += length) < length) | ||
113 | ++(ctx->count[1]); | ||
114 | |||
115 | while (length >= freeb) { /* tranfer whole blocks while possible */ | ||
116 | memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb); | ||
117 | sp += freeb; | ||
118 | length -= freeb; | ||
119 | freeb = SHA1_BLOCK_SIZE; | ||
120 | pos = 0; | ||
121 | sha1_compile(ctx); | ||
122 | } | ||
123 | |||
124 | memcpy(((unsigned char *) ctx->wbuf) + pos, sp, length); | ||
125 | } | ||
126 | |||
127 | void *sha1_end(void *resbuf, sha1_ctx_t *ctx) | ||
128 | { | ||
129 | /* SHA1 Final padding and digest calculation */ | ||
130 | #if BB_BIG_ENDIAN | ||
131 | static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 }; | ||
132 | static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 }; | ||
133 | #else | ||
134 | static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff }; | ||
135 | static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 }; | ||
136 | #endif | ||
137 | |||
138 | uint8_t *hval = resbuf; | ||
139 | uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK); | ||
140 | |||
141 | /* mask out the rest of any partial 32-bit word and then set */ | ||
142 | /* the next byte to 0x80. On big-endian machines any bytes in */ | ||
143 | /* the buffer will be at the top end of 32 bit words, on little */ | ||
144 | /* endian machines they will be at the bottom. Hence the AND */ | ||
145 | /* and OR masks above are reversed for little endian systems */ | ||
146 | ctx->wbuf[cnt >> 2] = | ||
147 | (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3]; | ||
148 | |||
149 | /* we need 9 or more empty positions, one for the padding byte */ | ||
150 | /* (above) and eight for the length count. If there is not */ | ||
151 | /* enough space pad and empty the buffer */ | ||
152 | if (cnt > SHA1_BLOCK_SIZE - 9) { | ||
153 | if (cnt < 60) | ||
154 | ctx->wbuf[15] = 0; | ||
155 | sha1_compile(ctx); | ||
156 | cnt = 0; | ||
157 | } else /* compute a word index for the empty buffer positions */ | ||
158 | cnt = (cnt >> 2) + 1; | ||
159 | |||
160 | while (cnt < 14) /* and zero pad all but last two positions */ | ||
161 | ctx->wbuf[cnt++] = 0; | ||
162 | |||
163 | /* assemble the eight byte counter in the buffer in big-endian */ | ||
164 | /* format */ | ||
165 | |||
166 | ctx->wbuf[14] = htonl((ctx->count[1] << 3) | (ctx->count[0] >> 29)); | ||
167 | ctx->wbuf[15] = htonl(ctx->count[0] << 3); | ||
168 | |||
169 | sha1_compile(ctx); | ||
170 | |||
171 | /* extract the hash value as bytes in case the hash buffer is */ | ||
172 | /* misaligned for 32-bit words */ | ||
173 | |||
174 | for (i = 0; i < SHA1_DIGEST_SIZE; ++i) | ||
175 | hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3)); | ||
176 | |||
177 | return resbuf; | ||
178 | } |