00001
00002
00003
00004
00005 #define SHA_2
00006 #define SHA_256
00007 #define SHA_384
00008 #define SHA_512
00009
00010 #include <string.h>
00011 #include <stdlib.h>
00012
00013 #include "sha2.h"
00014
00015 #if defined(__GNU_LIBRARY__)
00016 # include <byteswap.h>
00017 # include <endian.h>
00018 #elif defined(__CRYPTLIB__)
00019 # if defined( INC_ALL )
00020 # include "crypt.h"
00021 # elif defined( INC_CHILD )
00022 # include "../crypt.h"
00023 # else
00024 # include "crypt.h"
00025 # endif
00026 # if defined(DATA_LITTLEENDIAN)
00027 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00028 # else
00029 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00030 # endif
00031 #elif defined(_MSC_VER)
00032 # include <stdlib.h>
00033 #elif !defined(WIN32)
00034 # include <stdlib.h>
00035 # if !defined (_ENDIAN_H)
00036 # include <sys/param.h>
00037 # else
00038 # include _ENDIAN_H
00039 # endif
00040 #endif
00041
00042 #define SHA_LITTLE_ENDIAN 1234
00043 #define SHA_BIG_ENDIAN 4321
00044
00045 #if !defined(PLATFORM_BYTE_ORDER)
00046 #if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
00047 # if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
00048 # if defined(BYTE_ORDER)
00049 # if (BYTE_ORDER == LITTLE_ENDIAN)
00050 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00051 # elif (BYTE_ORDER == BIG_ENDIAN)
00052 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00053 # endif
00054 # endif
00055 # elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
00056 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00057 # elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
00058 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00059 # endif
00060 #elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
00061 # if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
00062 # if defined(_BYTE_ORDER)
00063 # if (_BYTE_ORDER == _LITTLE_ENDIAN)
00064 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00065 # elif (_BYTE_ORDER == _BIG_ENDIAN)
00066 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00067 # endif
00068 # endif
00069 # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
00070 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00071 # elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
00072 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00073 # endif
00074 #elif 0
00075 #define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00076 #elif 0
00077 #define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00078 #elif (('1234' >> 24) == '1')
00079 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00080 #elif (('4321' >> 24) == '1')
00081 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00082 #endif
00083 #endif
00084
00085 #if !defined(PLATFORM_BYTE_ORDER)
00086 # error Please set undetermined byte order (lines 159 or 161 of sha2.c).
00087 #endif
00088
00089 #ifdef _MSC_VER
00090 #pragma intrinsic(memcpy)
00091 #endif
00092
00093 #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
00094
00095 #if !defined(bswap_32)
00096 #define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)
00097 #endif
00098
00099 #if (PLATFORM_BYTE_ORDER == SHA_LITTLE_ENDIAN)
00100 #define SWAP_BYTES
00101 #else
00102 #undef SWAP_BYTES
00103 #endif
00104
00105 #if defined(SHA_2) || defined(SHA_256)
00106
00107 #define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
00108
00109 #if defined(SWAP_BYTES)
00110 #define bsw_32(p,n) { int _i = (n); while(_i--) p[_i] = bswap_32(p[_i]); }
00111 #else
00112 #define bsw_32(p,n)
00113 #endif
00114
00115 #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
00116 #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
00117
00118 #define s256_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
00119 #define s256_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
00120 #define g256_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
00121 #define g256_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
00122
00123 #define h2(i) ctx->wbuf[i & 15] += \
00124 g256_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g256_0(ctx->wbuf[(i + 1) & 15])
00125
00126 #define h2_cycle(i,j) \
00127 v[(7 - i) & 7] += (j ? h2(i) : ctx->wbuf[i & 15]) + k256[i + j] \
00128 + s256_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \
00129 v[(3 - i) & 7] += v[(7 - i) & 7]; \
00130 v[(7 - i) & 7] += s256_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])
00131
00132 const sha2_32t k256[64] =
00133 { n_u32(428a2f98), n_u32(71374491), n_u32(b5c0fbcf), n_u32(e9b5dba5),
00134 n_u32(3956c25b), n_u32(59f111f1), n_u32(923f82a4), n_u32(ab1c5ed5),
00135 n_u32(d807aa98), n_u32(12835b01), n_u32(243185be), n_u32(550c7dc3),
00136 n_u32(72be5d74), n_u32(80deb1fe), n_u32(9bdc06a7), n_u32(c19bf174),
00137 n_u32(e49b69c1), n_u32(efbe4786), n_u32(0fc19dc6), n_u32(240ca1cc),
00138 n_u32(2de92c6f), n_u32(4a7484aa), n_u32(5cb0a9dc), n_u32(76f988da),
00139 n_u32(983e5152), n_u32(a831c66d), n_u32(b00327c8), n_u32(bf597fc7),
00140 n_u32(c6e00bf3), n_u32(d5a79147), n_u32(06ca6351), n_u32(14292967),
00141 n_u32(27b70a85), n_u32(2e1b2138), n_u32(4d2c6dfc), n_u32(53380d13),
00142 n_u32(650a7354), n_u32(766a0abb), n_u32(81c2c92e), n_u32(92722c85),
00143 n_u32(a2bfe8a1), n_u32(a81a664b), n_u32(c24b8b70), n_u32(c76c51a3),
00144 n_u32(d192e819), n_u32(d6990624), n_u32(f40e3585), n_u32(106aa070),
00145 n_u32(19a4c116), n_u32(1e376c08), n_u32(2748774c), n_u32(34b0bcb5),
00146 n_u32(391c0cb3), n_u32(4ed8aa4a), n_u32(5b9cca4f), n_u32(682e6ff3),
00147 n_u32(748f82ee), n_u32(78a5636f), n_u32(84c87814), n_u32(8cc70208),
00148 n_u32(90befffa), n_u32(a4506ceb), n_u32(bef9a3f7), n_u32(c67178f2),
00149 };
00150
00151 const sha2_32t i256[8] =
00152 {
00153 n_u32(6a09e667), n_u32(bb67ae85), n_u32(3c6ef372), n_u32(a54ff53a),
00154 n_u32(510e527f), n_u32(9b05688c), n_u32(1f83d9ab), n_u32(5be0cd19)
00155 };
00156
00157 void sha256_begin(sha256_ctx ctx[1])
00158 {
00159 ctx->count[0] = ctx->count[1] = 0;
00160 memcpy(ctx->hash, i256, 8 * sizeof(sha2_32t));
00161 }
00162
00163 void sha256_compile(sha256_ctx ctx[1])
00164 { sha2_32t v[8], j;
00165
00166 memcpy(v, ctx->hash, 8 * sizeof(sha2_32t));
00167
00168 for(j = 0; j < 64; j += 16)
00169 {
00170 h2_cycle( 0, j); h2_cycle( 1, j); h2_cycle( 2, j); h2_cycle( 3, j);
00171 h2_cycle( 4, j); h2_cycle( 5, j); h2_cycle( 6, j); h2_cycle( 7, j);
00172 h2_cycle( 8, j); h2_cycle( 9, j); h2_cycle(10, j); h2_cycle(11, j);
00173 h2_cycle(12, j); h2_cycle(13, j); h2_cycle(14, j); h2_cycle(15, j);
00174 }
00175
00176 ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
00177 ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
00178 }
00179
00180 void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
00181 { sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA256_MASK),
00182 space = SHA256_BLOCK_SIZE - pos;
00183 const unsigned char *sp = data;
00184
00185 if((ctx->count[0] += len) < len)
00186 ++(ctx->count[1]);
00187
00188 while(len >= space)
00189 {
00190 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
00191 sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;
00192 bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)
00193 sha256_compile(ctx);
00194 }
00195
00196 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
00197 }
00198
00199 static sha2_32t m1[4] =
00200 {
00201 n_u32(00000000), n_u32(ff000000), n_u32(ffff0000), n_u32(ffffff00)
00202 };
00203
00204 static sha2_32t b1[4] =
00205 {
00206 n_u32(80000000), n_u32(00800000), n_u32(00008000), n_u32(00000080)
00207 };
00208
00209 void sha256_end(unsigned char hval[], sha256_ctx ctx[1])
00210 { sha2_32t i = (sha2_32t)(ctx->count[0] & SHA256_MASK);
00211
00212 bsw_32(ctx->wbuf, (i + 3) >> 2)
00213
00214
00215
00216
00217
00218
00219 ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & m1[i & 3]) | b1[i & 3];
00220
00221
00222
00223
00224 if(i > SHA256_BLOCK_SIZE - 9)
00225 {
00226 if(i < 60) ctx->wbuf[15] = 0;
00227 sha256_compile(ctx);
00228 i = 0;
00229 }
00230 else
00231 i = (i >> 2) + 1;
00232
00233 while(i < 14)
00234 ctx->wbuf[i++] = 0;
00235
00236
00237
00238
00239
00240
00241 ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
00242 ctx->wbuf[15] = ctx->count[0] << 3;
00243
00244 sha256_compile(ctx);
00245
00246
00247
00248 for(i = 0; i < SHA256_DIGEST_SIZE; ++i)
00249 hval[i] = (unsigned char)(ctx->hash[i >> 2] >> 8 * (~i & 3));
00250 }
00251
00252 void sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
00253 { sha256_ctx cx[1];
00254
00255 sha256_begin(cx); sha256_hash(data, len, cx); sha256_end(hval, cx);
00256 }
00257
00258 #endif
00259
00260 #if defined(SHA_2) || defined(SHA_384) || defined(SHA_512)
00261
00262 #define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
00263
00264 #define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))
00265
00266 #if !defined(bswap_64)
00267 #define bswap_64(x) (((sha2_64t)(bswap_32((sha2_32t)(x)))) << 32 | bswap_32((sha2_32t)((x) >> 32)))
00268 #endif
00269
00270 #if defined(SWAP_BYTES)
00271 #define bsw_64(p,n) { int _i = (n); while(_i--) p[_i] = bswap_64(p[_i]); }
00272 #else
00273 #define bsw_64(p,n)
00274 #endif
00275
00276 #define s512_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
00277 #define s512_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
00278 #define g512_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))
00279 #define g512_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))
00280
00281 #define h5(i) ctx->wbuf[i & 15] += \
00282 g512_1(ctx->wbuf[(i + 14) & 15]) + ctx->wbuf[(i + 9) & 15] + g512_0(ctx->wbuf[(i + 1) & 15])
00283
00284 #define h5_cycle(i,j) \
00285 v[(7 - i) & 7] += (j ? h5(i) : ctx->wbuf[i & 15]) + k512[i + j] \
00286 + s512_1(v[(4 - i) & 7]) + ch(v[(4 - i) & 7], v[(5 - i) & 7], v[(6 - i) & 7]); \
00287 v[(3 - i) & 7] += v[(7 - i) & 7]; \
00288 v[(7 - i) & 7] += s512_0(v[(0 - i) & 7]) + maj(v[(0 - i) & 7], v[(1 - i) & 7], v[(2 - i) & 7])
00289
00290 const sha2_64t k512[80] =
00291 {
00292 n_u64(428a2f98d728ae22), n_u64(7137449123ef65cd),
00293 n_u64(b5c0fbcfec4d3b2f), n_u64(e9b5dba58189dbbc),
00294 n_u64(3956c25bf348b538), n_u64(59f111f1b605d019),
00295 n_u64(923f82a4af194f9b), n_u64(ab1c5ed5da6d8118),
00296 n_u64(d807aa98a3030242), n_u64(12835b0145706fbe),
00297 n_u64(243185be4ee4b28c), n_u64(550c7dc3d5ffb4e2),
00298 n_u64(72be5d74f27b896f), n_u64(80deb1fe3b1696b1),
00299 n_u64(9bdc06a725c71235), n_u64(c19bf174cf692694),
00300 n_u64(e49b69c19ef14ad2), n_u64(efbe4786384f25e3),
00301 n_u64(0fc19dc68b8cd5b5), n_u64(240ca1cc77ac9c65),
00302 n_u64(2de92c6f592b0275), n_u64(4a7484aa6ea6e483),
00303 n_u64(5cb0a9dcbd41fbd4), n_u64(76f988da831153b5),
00304 n_u64(983e5152ee66dfab), n_u64(a831c66d2db43210),
00305 n_u64(b00327c898fb213f), n_u64(bf597fc7beef0ee4),
00306 n_u64(c6e00bf33da88fc2), n_u64(d5a79147930aa725),
00307 n_u64(06ca6351e003826f), n_u64(142929670a0e6e70),
00308 n_u64(27b70a8546d22ffc), n_u64(2e1b21385c26c926),
00309 n_u64(4d2c6dfc5ac42aed), n_u64(53380d139d95b3df),
00310 n_u64(650a73548baf63de), n_u64(766a0abb3c77b2a8),
00311 n_u64(81c2c92e47edaee6), n_u64(92722c851482353b),
00312 n_u64(a2bfe8a14cf10364), n_u64(a81a664bbc423001),
00313 n_u64(c24b8b70d0f89791), n_u64(c76c51a30654be30),
00314 n_u64(d192e819d6ef5218), n_u64(d69906245565a910),
00315 n_u64(f40e35855771202a), n_u64(106aa07032bbd1b8),
00316 n_u64(19a4c116b8d2d0c8), n_u64(1e376c085141ab53),
00317 n_u64(2748774cdf8eeb99), n_u64(34b0bcb5e19b48a8),
00318 n_u64(391c0cb3c5c95a63), n_u64(4ed8aa4ae3418acb),
00319 n_u64(5b9cca4f7763e373), n_u64(682e6ff3d6b2b8a3),
00320 n_u64(748f82ee5defb2fc), n_u64(78a5636f43172f60),
00321 n_u64(84c87814a1f0ab72), n_u64(8cc702081a6439ec),
00322 n_u64(90befffa23631e28), n_u64(a4506cebde82bde9),
00323 n_u64(bef9a3f7b2c67915), n_u64(c67178f2e372532b),
00324 n_u64(ca273eceea26619c), n_u64(d186b8c721c0c207),
00325 n_u64(eada7dd6cde0eb1e), n_u64(f57d4f7fee6ed178),
00326 n_u64(06f067aa72176fba), n_u64(0a637dc5a2c898a6),
00327 n_u64(113f9804bef90dae), n_u64(1b710b35131c471b),
00328 n_u64(28db77f523047d84), n_u64(32caab7b40c72493),
00329 n_u64(3c9ebe0a15c9bebc), n_u64(431d67c49c100d4c),
00330 n_u64(4cc5d4becb3e42b6), n_u64(597f299cfc657e2a),
00331 n_u64(5fcb6fab3ad6faec), n_u64(6c44198c4a475817)
00332 };
00333
00334 void sha512_compile(sha512_ctx ctx[1])
00335 { sha2_64t v[8];
00336 sha2_32t j;
00337
00338 memcpy(v, ctx->hash, 8 * sizeof(sha2_64t));
00339
00340 for(j = 0; j < 80; j += 16)
00341 {
00342 h5_cycle( 0, j); h5_cycle( 1, j); h5_cycle( 2, j); h5_cycle( 3, j);
00343 h5_cycle( 4, j); h5_cycle( 5, j); h5_cycle( 6, j); h5_cycle( 7, j);
00344 h5_cycle( 8, j); h5_cycle( 9, j); h5_cycle(10, j); h5_cycle(11, j);
00345 h5_cycle(12, j); h5_cycle(13, j); h5_cycle(14, j); h5_cycle(15, j);
00346 }
00347
00348 ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
00349 ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
00350 }
00351
00352 void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
00353 { sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA512_MASK),
00354 space = SHA512_BLOCK_SIZE - pos;
00355 const unsigned char *sp = data;
00356
00357 if((ctx->count[0] += len) < len)
00358 ++(ctx->count[1]);
00359
00360 while(len >= space)
00361 {
00362 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
00363 sp += space; len -= space; space = SHA512_BLOCK_SIZE; pos = 0;
00364 bsw_64(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);
00365 sha512_compile(ctx);
00366 }
00367
00368 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
00369 }
00370
00371 static sha2_64t m2[8] =
00372 {
00373 n_u64(0000000000000000), n_u64(ff00000000000000),
00374 n_u64(ffff000000000000), n_u64(ffffff0000000000),
00375 n_u64(ffffffff00000000), n_u64(ffffffffff000000),
00376 n_u64(ffffffffffff0000), n_u64(ffffffffffffff00)
00377 };
00378
00379 static sha2_64t b2[8] =
00380 {
00381 n_u64(8000000000000000), n_u64(0080000000000000),
00382 n_u64(0000800000000000), n_u64(0000008000000000),
00383 n_u64(0000000080000000), n_u64(0000000000800000),
00384 n_u64(0000000000008000), n_u64(0000000000000080)
00385 };
00386
00387 static void sha_end(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
00388 { sha2_32t i = (sha2_32t)(ctx->count[0] & SHA512_MASK);
00389
00390 bsw_64(ctx->wbuf, (i + 7) >> 3);
00391
00392
00393
00394
00395
00396
00397
00398 ctx->wbuf[i >> 3] = (ctx->wbuf[i >> 3] & m2[i & 7]) | b2[i & 7];
00399
00400
00401
00402
00403 if(i > SHA512_BLOCK_SIZE - 17)
00404 {
00405 if(i < 120) ctx->wbuf[15] = 0;
00406 sha512_compile(ctx);
00407 i = 0;
00408 }
00409 else
00410 i = (i >> 3) + 1;
00411
00412 while(i < 14)
00413 ctx->wbuf[i++] = 0;
00414
00415
00416
00417
00418
00419
00420 ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);
00421 ctx->wbuf[15] = ctx->count[0] << 3;
00422
00423 sha512_compile(ctx);
00424
00425
00426
00427 for(i = 0; i < hlen; ++i)
00428 hval[i] = (unsigned char)(ctx->hash[i >> 3] >> 8 * (~i & 7));
00429 }
00430
00431 #endif
00432
00433 #if defined(SHA_2) || defined(SHA_384)
00434
00435 const sha2_64t i384[80] =
00436 {
00437 n_u64(cbbb9d5dc1059ed8), n_u64(629a292a367cd507),
00438 n_u64(9159015a3070dd17), n_u64(152fecd8f70e5939),
00439 n_u64(67332667ffc00b31), n_u64(8eb44a8768581511),
00440 n_u64(db0c2e0d64f98fa7), n_u64(47b5481dbefa4fa4)
00441 };
00442
00443 void sha384_begin(sha384_ctx ctx[1])
00444 {
00445 ctx->count[0] = ctx->count[1] = 0;
00446 memcpy(ctx->hash, i384, 8 * sizeof(sha2_64t));
00447 }
00448
00449 void sha384_end(unsigned char hval[], sha384_ctx ctx[1])
00450 {
00451 sha_end(hval, ctx, SHA384_DIGEST_SIZE);
00452 }
00453
00454 void sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
00455 { sha384_ctx cx[1];
00456
00457 sha384_begin(cx); sha384_hash(data, len, cx); sha384_end(hval, cx);
00458 }
00459
00460 #endif
00461
00462 #if defined(SHA_2) || defined(SHA_512)
00463
00464 const sha2_64t i512[80] =
00465 {
00466 n_u64(6a09e667f3bcc908), n_u64(bb67ae8584caa73b),
00467 n_u64(3c6ef372fe94f82b), n_u64(a54ff53a5f1d36f1),
00468 n_u64(510e527fade682d1), n_u64(9b05688c2b3e6c1f),
00469 n_u64(1f83d9abfb41bd6b), n_u64(5be0cd19137e2179)
00470 };
00471
00472 void sha512_begin(sha512_ctx ctx[1])
00473 {
00474 ctx->count[0] = ctx->count[1] = 0;
00475 memcpy(ctx->hash, i512, 8 * sizeof(sha2_64t));
00476 }
00477
00478 void sha512_end(unsigned char hval[], sha512_ctx ctx[1])
00479 {
00480 sha_end(hval, ctx, SHA512_DIGEST_SIZE);
00481 }
00482
00483 void sha512(unsigned char hval[], const unsigned char data[], unsigned long len)
00484 { sha512_ctx cx[1];
00485
00486 sha512_begin(cx); sha512_hash(data, len, cx); sha512_end(hval, cx);
00487 }
00488
00489 #endif
00490
00491 #if defined(SHA_2)
00492
00493 #define CTX_256(x) ((x)->uu->ctx256)
00494 #define CTX_384(x) ((x)->uu->ctx512)
00495 #define CTX_512(x) ((x)->uu->ctx512)
00496
00497 int sha2_begin(unsigned long len, sha2_ctx ctx[1])
00498 { unsigned long l = len;
00499 switch(len)
00500 {
00501 case 256: l = len >> 3;
00502 case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
00503 memcpy(CTX_256(ctx)->hash, i256, 32); break;
00504 case 384: l = len >> 3;
00505 case 48: CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;
00506 memcpy(CTX_384(ctx)->hash, i384, 64); break;
00507 case 512: l = len >> 3;
00508 case 64: CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;
00509 memcpy(CTX_512(ctx)->hash, i512, 64); break;
00510 default: return SHA2_BAD;
00511 }
00512
00513 ctx->sha2_len = l; return SHA2_GOOD;
00514 }
00515
00516 void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
00517 {
00518 switch(ctx->sha2_len)
00519 {
00520 case 32: sha256_hash(data, len, CTX_256(ctx)); return;
00521 case 48: sha384_hash(data, len, CTX_384(ctx)); return;
00522 case 64: sha512_hash(data, len, CTX_512(ctx)); return;
00523 }
00524 }
00525
00526 void sha2_end(unsigned char hval[], sha2_ctx ctx[1])
00527 {
00528 switch(ctx->sha2_len)
00529 {
00530 case 32: sha256_end(hval, CTX_256(ctx)); return;
00531 case 48: sha_end(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;
00532 case 64: sha_end(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;
00533 }
00534 }
00535
00536 int sha2(unsigned char hval[], unsigned long size,
00537 const unsigned char data[], unsigned long len)
00538 { sha2_ctx cx[1];
00539
00540 if(sha2_begin(size, cx) == SHA2_GOOD)
00541 {
00542 sha2_hash(data, len, cx); sha2_end(hval, cx); return SHA2_GOOD;
00543 }
00544 else
00545 return SHA2_BAD;
00546 }
00547
00548 #endif