|
9 | 9 |
|
10 | 10 | #include "sha1.h"
|
11 | 11 |
|
12 |
| -/* Hash one 64-byte block of data */ |
13 |
| -static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data); |
14 |
| - |
15 |
| -void blk_SHA1_Init(blk_SHA_CTX *ctx) |
16 |
| -{ |
17 |
| - ctx->size = 0; |
18 |
| - |
19 |
| - /* Initialize H with the magic constants (see FIPS180 for constants) |
20 |
| - */ |
21 |
| - ctx->H[0] = 0x67452301; |
22 |
| - ctx->H[1] = 0xefcdab89; |
23 |
| - ctx->H[2] = 0x98badcfe; |
24 |
| - ctx->H[3] = 0x10325476; |
25 |
| - ctx->H[4] = 0xc3d2e1f0; |
26 |
| -} |
27 |
| - |
28 |
| - |
29 |
| -void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len) |
30 |
| -{ |
31 |
| - int lenW = ctx->size & 63; |
32 |
| - |
33 |
| - ctx->size += len; |
34 |
| - |
35 |
| - /* Read the data into W and process blocks as they get full |
36 |
| - */ |
37 |
| - if (lenW) { |
38 |
| - int left = 64 - lenW; |
39 |
| - if (len < left) |
40 |
| - left = len; |
41 |
| - memcpy(lenW + (char *)ctx->W, data, left); |
42 |
| - lenW = (lenW + left) & 63; |
43 |
| - len -= left; |
44 |
| - data += left; |
45 |
| - if (lenW) |
46 |
| - return; |
47 |
| - blk_SHA1Block(ctx, ctx->W); |
48 |
| - } |
49 |
| - while (len >= 64) { |
50 |
| - blk_SHA1Block(ctx, data); |
51 |
| - data += 64; |
52 |
| - len -= 64; |
53 |
| - } |
54 |
| - if (len) |
55 |
| - memcpy(ctx->W, data, len); |
56 |
| -} |
57 |
| - |
58 |
| - |
59 |
| -void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx) |
60 |
| -{ |
61 |
| - static const unsigned char pad[64] = { 0x80 }; |
62 |
| - unsigned int padlen[2]; |
63 |
| - int i; |
64 |
| - |
65 |
| - /* Pad with a binary 1 (ie 0x80), then zeroes, then length |
66 |
| - */ |
67 |
| - padlen[0] = htonl(ctx->size >> 29); |
68 |
| - padlen[1] = htonl(ctx->size << 3); |
69 |
| - |
70 |
| - i = ctx->size & 63; |
71 |
| - blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - i))); |
72 |
| - blk_SHA1_Update(ctx, padlen, 8); |
73 |
| - |
74 |
| - /* Output hash |
75 |
| - */ |
76 |
| - for (i = 0; i < 5; i++) |
77 |
| - ((unsigned int *)hashout)[i] = htonl(ctx->H[i]); |
78 |
| -} |
79 |
| - |
80 | 12 | #if defined(__i386__) || defined(__x86_64__)
|
81 | 13 |
|
82 | 14 | #define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; })
|
@@ -136,7 +68,7 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
|
136 | 68 | #define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
|
137 | 69 | #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
|
138 | 70 |
|
139 |
| -static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data) |
| 71 | +static void blk_SHA1_Block(blk_SHA_CTX *ctx, const unsigned int *data) |
140 | 72 | {
|
141 | 73 | unsigned int A,B,C,D,E;
|
142 | 74 | unsigned int array[16];
|
@@ -243,3 +175,62 @@ static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data)
|
243 | 175 | ctx->H[3] += D;
|
244 | 176 | ctx->H[4] += E;
|
245 | 177 | }
|
| 178 | + |
| 179 | +void blk_SHA1_Init(blk_SHA_CTX *ctx) |
| 180 | +{ |
| 181 | + ctx->size = 0; |
| 182 | + |
| 183 | + /* Initialize H with the magic constants (see FIPS180 for constants) */ |
| 184 | + ctx->H[0] = 0x67452301; |
| 185 | + ctx->H[1] = 0xefcdab89; |
| 186 | + ctx->H[2] = 0x98badcfe; |
| 187 | + ctx->H[3] = 0x10325476; |
| 188 | + ctx->H[4] = 0xc3d2e1f0; |
| 189 | +} |
| 190 | + |
| 191 | +void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len) |
| 192 | +{ |
| 193 | + int lenW = ctx->size & 63; |
| 194 | + |
| 195 | + ctx->size += len; |
| 196 | + |
| 197 | + /* Read the data into W and process blocks as they get full */ |
| 198 | + if (lenW) { |
| 199 | + int left = 64 - lenW; |
| 200 | + if (len < left) |
| 201 | + left = len; |
| 202 | + memcpy(lenW + (char *)ctx->W, data, left); |
| 203 | + lenW = (lenW + left) & 63; |
| 204 | + len -= left; |
| 205 | + data += left; |
| 206 | + if (lenW) |
| 207 | + return; |
| 208 | + blk_SHA1_Block(ctx, ctx->W); |
| 209 | + } |
| 210 | + while (len >= 64) { |
| 211 | + blk_SHA1_Block(ctx, data); |
| 212 | + data += 64; |
| 213 | + len -= 64; |
| 214 | + } |
| 215 | + if (len) |
| 216 | + memcpy(ctx->W, data, len); |
| 217 | +} |
| 218 | + |
| 219 | +void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx) |
| 220 | +{ |
| 221 | + static const unsigned char pad[64] = { 0x80 }; |
| 222 | + unsigned int padlen[2]; |
| 223 | + int i; |
| 224 | + |
| 225 | + /* Pad with a binary 1 (ie 0x80), then zeroes, then length */ |
| 226 | + padlen[0] = htonl(ctx->size >> 29); |
| 227 | + padlen[1] = htonl(ctx->size << 3); |
| 228 | + |
| 229 | + i = ctx->size & 63; |
| 230 | + blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - i))); |
| 231 | + blk_SHA1_Update(ctx, padlen, 8); |
| 232 | + |
| 233 | + /* Output hash */ |
| 234 | + for (i = 0; i < 5; i++) |
| 235 | + ((unsigned int *)hashout)[i] = htonl(ctx->H[i]); |
| 236 | +} |
0 commit comments