| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
#include <stdio.h> |
| 5 |
#include "sha1.h" |
| 6 |
|
| 7 |
#include <limits.h> /* to get info about range unsigned int */ |
| 8 |
|
| 9 |
#if ULONG_MAX == 4294967295U |
| 10 |
typedef unsigned long int Card32; |
| 11 |
#elif UINT_MAX == 4294967295U |
| 12 |
typedef unsigned int Card32; |
| 13 |
#else |
| 14 |
#error "There must be a way to get an unsigned 32 bit integer" |
| 15 |
#endif |
| 16 |
|
| 17 |
typedef struct sha1_ctx { |
| 18 |
Card32 H[5]; |
| 19 |
Card32 count_hi; |
| 20 |
Card32 count_lo; |
| 21 |
unsigned char c[64]; |
| 22 |
Card32 count_in; |
| 23 |
} sha1_ctx; |
| 24 |
|
| 25 |
void sha1_process(sha1_ctx *ctx) { |
| 26 |
Card32 A, B, C, D, E, W[80], TEMP, t; |
| 27 |
|
| 28 |
for (t = 0; t < 16; t++) |
| 29 |
W[t] = (((Card32)(ctx->c[t * 4])) << 24) + |
| 30 |
(((Card32)(ctx->c[t * 4 + 1])) << 16) + |
| 31 |
(((Card32)(ctx->c[t * 4 + 2])) << 8) + |
| 32 |
((Card32)(ctx->c[t * 4 + 3])); |
| 33 |
for (t = 16; t < 80; t++) { |
| 34 |
W[t] = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]; |
| 35 |
W[t] = (W[t] << 1) | (W[t] >> 31); |
| 36 |
} |
| 37 |
A = ctx->H[0]; |
| 38 |
B = ctx->H[1]; |
| 39 |
C = ctx->H[2]; |
| 40 |
D = ctx->H[3]; |
| 41 |
E = ctx->H[4]; |
| 42 |
for (t = 0; t < 20; t++) { |
| 43 |
TEMP = ((A << 5) | (A >> 27)) + |
| 44 |
((B & C) | ((~B) & D)) + |
| 45 |
E + W[t] + 0x5a827999; |
| 46 |
E = D; |
| 47 |
D = C; |
| 48 |
C = ((B << 30) | (B >> 2)); |
| 49 |
B = A; |
| 50 |
A = TEMP; |
| 51 |
} |
| 52 |
for (t = 20; t < 40; t++) { |
| 53 |
TEMP = ((A << 5) | (A >> 27)) + |
| 54 |
(B ^ C ^ D) + |
| 55 |
E + W[t] + 0x6ed9eba1; |
| 56 |
E = D; |
| 57 |
D = C; |
| 58 |
C = ((B << 30) | (B >> 2)); |
| 59 |
B = A; |
| 60 |
A = TEMP; |
| 61 |
} |
| 62 |
for (t = 40; t < 60; t++) { |
| 63 |
TEMP = ((A << 5) | (A >> 27)) + |
| 64 |
((B & C) | (B & D) | (C & D)) + |
| 65 |
E + W[t] + 0x8f1bbcdc; |
| 66 |
E = D; |
| 67 |
D = C; |
| 68 |
C = ((B << 30) | (B >> 2)); |
| 69 |
B = A; |
| 70 |
A = TEMP; |
| 71 |
} |
| 72 |
for (t = 60; t < 80; t++) { |
| 73 |
TEMP = ((A << 5) | (A >> 27)) + |
| 74 |
(B ^ C ^ D) + |
| 75 |
E + W[t] + 0xca62c1d6; |
| 76 |
E = D; |
| 77 |
D = C; |
| 78 |
C = ((B << 30) | (B >> 2)); |
| 79 |
B = A; |
| 80 |
A = TEMP; |
| 81 |
} |
| 82 |
|
| 83 |
ctx->H[0] += A; |
| 84 |
ctx->H[1] += B; |
| 85 |
ctx->H[2] += C; |
| 86 |
ctx->H[3] += D; |
| 87 |
ctx->H[4] += E; |
| 88 |
} |
| 89 |
|
| 90 |
sha1_ctx *sha1_init(sha1_malloc p, void *hook) { |
| 91 |
sha1_ctx *c; |
| 92 |
int i; |
| 93 |
|
| 94 |
#ifdef _MSC_VER |
| 95 |
#pragma warning(push) |
| 96 |
#pragma warning(disable : 4127) |
| 97 |
#endif |
| 98 |
if (sizeof(Card32) != 4) |
| 99 |
return NULL; |
| 100 |
#ifdef _MSC_VER |
| 101 |
#pragma warning(pop) |
| 102 |
#endif |
| 103 |
|
| 104 |
c = (sha1_ctx *)p(sizeof(sha1_ctx), hook); |
| 105 |
if (c == NULL) |
| 106 |
return NULL; |
| 107 |
c->H[0] = 0x67452301; |
| 108 |
c->H[1] = 0xefcdab89; |
| 109 |
c->H[2] = 0x98badcfe; |
| 110 |
c->H[3] = 0x10325476; |
| 111 |
c->H[4] = 0xc3d2e1f0; |
| 112 |
c->count_hi = 0; |
| 113 |
c->count_lo = 0; |
| 114 |
c->count_in = 0; |
| 115 |
for (i = 0; i < 64; i++) |
| 116 |
c->c[i] = 0; |
| 117 |
return c; |
| 118 |
} |
| 119 |
|
| 120 |
int sha1_update(sha1_pctx ctx, unsigned char *b, size_t len) { |
| 121 |
Card32 old_lo; |
| 122 |
if (ctx == NULL) |
| 123 |
return 1; |
| 124 |
if (len == 0) |
| 125 |
return 0; |
| 126 |
if (b == NULL) |
| 127 |
return 1; |
| 128 |
while (len--) { |
| 129 |
old_lo = ctx->count_lo; |
| 130 |
ctx->count_lo += 8; /* count is bits, not bytes */ |
| 131 |
if (old_lo > ctx->count_lo) /* overflow */ |
| 132 |
ctx->count_hi++; /* once in 4 billion bits */ |
| 133 |
ctx->c[ctx->count_in++] = *b++; |
| 134 |
if (ctx->count_in == 64) { |
| 135 |
sha1_process(ctx); |
| 136 |
ctx->count_in = 0; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
int sha1_finalize(sha1_pctx ctx, sha1_free p, sha1_hash hash, void *hook) { |
| 144 |
int i; |
| 145 |
if (ctx == NULL) |
| 146 |
return 1; |
| 147 |
if (p == NULL) |
| 148 |
return 1; |
| 149 |
if (hash == NULL) |
| 150 |
return 1; |
| 151 |
ctx->c[ctx->count_in++] = 0x80; /* pad bit */ |
| 152 |
if (ctx->count_in < 57) { /* count will fit in this buffer */ |
| 153 |
for (i = ctx->count_in; i < 56; i++) |
| 154 |
ctx->c[i] = 0; /* zero fill remainder of buffer */ |
| 155 |
} else { /* count doesn't fit. process this buffer and put count in next */ |
| 156 |
for (i = ctx->count_in; i < 64; i++) |
| 157 |
ctx->c[i] = 0; /* zero fill remainder of buffer */ |
| 158 |
sha1_process(ctx); |
| 159 |
for (i = 0; i < 56; i++) |
| 160 |
ctx->c[i] = 0; /* zero fill the buffer (less count */ |
| 161 |
} |
| 162 |
ctx->c[56] = (unsigned char)(ctx->count_hi >> 24); |
| 163 |
ctx->c[57] = (unsigned char)(ctx->count_hi >> 16); |
| 164 |
ctx->c[58] = (unsigned char)(ctx->count_hi >> 8); |
| 165 |
ctx->c[59] = (unsigned char)(ctx->count_hi); |
| 166 |
ctx->c[60] = (unsigned char)(ctx->count_lo >> 24); |
| 167 |
ctx->c[61] = (unsigned char)(ctx->count_lo >> 16); |
| 168 |
ctx->c[62] = (unsigned char)(ctx->count_lo >> 8); |
| 169 |
ctx->c[63] = (unsigned char)(ctx->count_lo); |
| 170 |
sha1_process(ctx); |
| 171 |
for (i = 0; i < 5; i++) { |
| 172 |
hash[i * 4] = (unsigned char)(ctx->H[i] >> 24); |
| 173 |
hash[i * 4 + 1] = (unsigned char)(ctx->H[i] >> 16); |
| 174 |
hash[i * 4 + 2] = (unsigned char)(ctx->H[i] >> 8); |
| 175 |
hash[i * 4 + 3] = (unsigned char)(ctx->H[i]); |
| 176 |
} |
| 177 |
p(ctx, hook); |
| 178 |
return 0; |
| 179 |
} |