sha2.c

Go to the documentation of this file.
00001 /*      $FreeBSD: src/sys/crypto/sha2/sha2.c,v 1.2.2.2 2002/03/05 08:36:47 ume Exp $    */
00002 /*      $KAME: sha2.c,v 1.8 2001/11/08 01:07:52 itojun Exp $    */
00003 
00004 /*
00005  * sha2.c
00006  *
00007  * Version 1.0.0beta1
00008  *
00009  * Written by Aaron D. Gifford <me@aarongifford.com>
00010  *
00011  * Copyright 2000 Aaron D. Gifford.  All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions
00015  * are met:
00016  * 1. Redistributions of source code must retain the above copyright
00017  *    notice, this list of conditions and the following disclaimer.
00018  * 2. Redistributions in binary form must reproduce the above copyright
00019  *    notice, this list of conditions and the following disclaimer in the
00020  *    documentation and/or other materials provided with the distribution.
00021  * 3. Neither the name of the copyright holder nor the names of contributors
00022  *    may be used to endorse or promote products derived from this software
00023  *    without specific prior written permission.
00024  * 
00025  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00031  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00032  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00034  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035  * SUCH DAMAGE.
00036  *
00037  */
00038 
00039 #include <sys/types.h>
00040 /* #include <sys/time.h> */
00041 /* #include <sys/systm.h> */
00042 /* #include <machine/endian.h> */
00043 #include "sha2.h"
00044 
00045 /*
00046  * ASSERT NOTE:
00047  * Some sanity checking code is included using assert().  On my FreeBSD
00048  * system, this additional code can be removed by compiling with NDEBUG
00049  * defined.  Check your own systems manpage on assert() to see how to
00050  * compile WITHOUT the sanity checking code on your system.
00051  *
00052  * UNROLLED TRANSFORM LOOP NOTE:
00053  * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
00054  * loop version for the hash transform rounds (defined using macros
00055  * later in this file).  Either define on the command line, for example:
00056  *
00057  *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
00058  *
00059  * or define below:
00060  *
00061  *   #define SHA2_UNROLL_TRANSFORM
00062  *
00063  */
00064 
00065 #if defined(__bsdi__) || defined(__FreeBSD__)
00066 #define assert(x)
00067 #endif
00068 
00069 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
00070 /*
00071  * SHA2_BYTE_ORDER NOTE:
00072  *
00073  * Please make sure that your system defines SHA2_BYTE_ORDER.  If your
00074  * architecture is little-endian, make sure it also defines
00075  * SHA2_LITTLE_ENDIAN and that the two (SHA2_BYTE_ORDER and SHA2_LITTLE_ENDIAN) are
00076  * equivilent.
00077  *
00078  * If your system does not define the above, then you can do so by
00079  * hand like this:
00080  *
00081  *   #define SHA2_LITTLE_ENDIAN 1234
00082  *   #define SHA2_BIG_ENDIAN    4321
00083  *
00084  * And for little-endian machines, add:
00085  *
00086  *   #define SHA2_BYTE_ORDER SHA2_LITTLE_ENDIAN 
00087  *
00088  * Or for big-endian machines:
00089  *
00090  *   #define SHA2_BYTE_ORDER SHA2_BIG_ENDIAN
00091  *
00092  * The FreeBSD machine this was written on defines BYTE_ORDER
00093  * appropriately by including <sys/types.h> (which in turn includes
00094  * <machine/endian.h> where the appropriate definitions are actually
00095  * made).
00096  */
00097 #if !defined(SHA2_BYTE_ORDER) || (SHA2_BYTE_ORDER != SHA2_LITTLE_ENDIAN && SHA2_BYTE_ORDER != SHA2_BIG_ENDIAN)
00098 #error Define SHA2_BYTE_ORDER to be equal to either SHA2_LITTLE_ENDIAN or SHA2_BIG_ENDIAN
00099 #endif
00100 
00101 /*
00102  * Define the followingsha2_* types to types of the correct length on
00103  * the native archtecture.   Most BSD systems and Linux define u_intXX_t
00104  * types.  Machines with very recent ANSI C headers, can use the
00105  * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
00106  * during compile or in the sha.h header file.
00107  *
00108  * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
00109  * will need to define these three typedefs below (and the appropriate
00110  * ones in sha.h too) by hand according to their system architecture.
00111  *
00112  * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
00113  * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
00114  */
00115 #if 0 /*def SHA2_USE_INTTYPES_H*/
00116 
00117 typedef uint8_t  sha2_byte;     /* Exactly 1 byte */
00118 typedef uint32_t sha2_word32;   /* Exactly 4 bytes */
00119 typedef uint64_t sha2_word64;   /* Exactly 8 bytes */
00120 
00121 #else /* SHA2_USE_INTTYPES_H */
00122 
00123 typedef u_int8_t  sha2_byte;    /* Exactly 1 byte */
00124 typedef u_int32_t sha2_word32;  /* Exactly 4 bytes */
00125 typedef u_int64_t sha2_word64;  /* Exactly 8 bytes */
00126 
00127 #endif /* SHA2_USE_INTTYPES_H */
00128 
00129 /*** SHA-256/384/512 Various Length Definitions ***********************/
00130 /* NOTE: Most of these are in sha2.h */
00131 #define SHA256_SHORT_BLOCK_LENGTH       (SHA256_BLOCK_LENGTH - 8)
00132 #define SHA384_SHORT_BLOCK_LENGTH       (SHA384_BLOCK_LENGTH - 16)
00133 #define SHA512_SHORT_BLOCK_LENGTH       (SHA512_BLOCK_LENGTH - 16)
00134 
00135 /*** ENDIAN REVERSAL MACROS *******************************************/
00136 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00137 #define REVERSE32(w,x)  { \
00138         sha2_word32 tmp = (w); \
00139         tmp = (tmp >> 16) | (tmp << 16); \
00140         (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
00141 }
00142 #define REVERSE64(w,x)  { \
00143         sha2_word64 tmp = (w); \
00144         tmp = (tmp >> 32) | (tmp << 32); \
00145         tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
00146               ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
00147         (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
00148               ((tmp & 0x0000ffff0000ffffULL) << 16); \
00149 }
00150 #if MINIX_64BIT
00151 #undef REVERSE64
00152 #define REVERSE64(w,x)  { \
00153         u32_t hi, lo; \
00154         REVERSE32(ex64hi((w)), lo); \
00155         REVERSE32(ex64lo((w)), hi); \
00156         (x) = make64(lo, hi); \
00157 }
00158 #endif /* MINIX_64BIT */
00159 #endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00160 
00161 /*
00162  * Macro for incrementally adding the unsigned 64-bit integer n to the
00163  * unsigned 128-bit integer (represented using a two-element array of
00164  * 64-bit words):
00165  */
00166 #define ADDINC128(w,n)  { \
00167         (w)[0] += (sha2_word64)(n); \
00168         if ((w)[0] < (n)) { \
00169                 (w)[1]++; \
00170         } \
00171 }
00172 
00173 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
00174 /*
00175  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
00176  *
00177  *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
00178  *   S is a ROTATION) because the SHA-256/384/512 description document
00179  *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
00180  *   same "backwards" definition.
00181  */
00182 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
00183 #define R(b,x)          ((x) >> (b))
00184 /* 32-bit Rotate-right (used in SHA-256): */
00185 #define S32(b,x)        (((x) >> (b)) | ((x) << (32 - (b))))
00186 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
00187 #define S64(b,x)        (((x) >> (b)) | ((x) << (64 - (b))))
00188 
00189 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
00190 #define Ch(x,y,z)       (((x) & (y)) ^ ((~(x)) & (z)))
00191 #define Maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
00192 
00193 /* Four of six logical functions used in SHA-256: */
00194 #define Sigma0_256(x)   (S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
00195 #define Sigma1_256(x)   (S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
00196 #define sigma0_256(x)   (S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
00197 #define sigma1_256(x)   (S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
00198 
00199 /* Four of six logical functions used in SHA-384 and SHA-512: */
00200 #define Sigma0_512(x)   (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
00201 #define Sigma1_512(x)   (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
00202 #define sigma0_512(x)   (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
00203 #define sigma1_512(x)   (S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
00204 
00205 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
00206 /* NOTE: These should not be accessed directly from outside this
00207  * library -- they are intended for private internal visibility/use
00208  * only.
00209  */
00210 void SHA512_Last(SHA512_CTX*);
00211 void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
00212 void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
00213 
00214 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
00215 /* Hash constant words K for SHA-256: */
00216 const static sha2_word32 K256[64] = {
00217         0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
00218         0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
00219         0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
00220         0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
00221         0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
00222         0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
00223         0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
00224         0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
00225         0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
00226         0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
00227         0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
00228         0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
00229         0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
00230         0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
00231         0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
00232         0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
00233 };
00234 
00235 /* Initial hash value H for SHA-256: */
00236 const static sha2_word32 sha256_initial_hash_value[8] = {
00237         0x6a09e667UL,
00238         0xbb67ae85UL,
00239         0x3c6ef372UL,
00240         0xa54ff53aUL,
00241         0x510e527fUL,
00242         0x9b05688cUL,
00243         0x1f83d9abUL,
00244         0x5be0cd19UL
00245 };
00246 
00247 #if !NO_64BIT
00248 /* Hash constant words K for SHA-384 and SHA-512: */
00249 const static sha2_word64 K512[80] = {
00250         0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
00251         0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
00252         0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
00253         0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
00254         0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
00255         0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
00256         0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
00257         0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
00258         0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
00259         0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
00260         0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
00261         0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
00262         0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
00263         0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
00264         0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
00265         0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
00266         0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
00267         0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
00268         0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
00269         0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
00270         0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
00271         0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
00272         0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
00273         0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
00274         0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
00275         0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
00276         0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
00277         0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
00278         0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
00279         0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
00280         0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
00281         0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
00282         0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
00283         0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
00284         0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
00285         0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
00286         0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
00287         0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
00288         0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
00289         0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
00290 };
00291 
00292 /* Initial hash value H for SHA-384 */
00293 const static sha2_word64 sha384_initial_hash_value[8] = {
00294         0xcbbb9d5dc1059ed8ULL,
00295         0x629a292a367cd507ULL,
00296         0x9159015a3070dd17ULL,
00297         0x152fecd8f70e5939ULL,
00298         0x67332667ffc00b31ULL,
00299         0x8eb44a8768581511ULL,
00300         0xdb0c2e0d64f98fa7ULL,
00301         0x47b5481dbefa4fa4ULL
00302 };
00303 
00304 /* Initial hash value H for SHA-512 */
00305 const static sha2_word64 sha512_initial_hash_value[8] = {
00306         0x6a09e667f3bcc908ULL,
00307         0xbb67ae8584caa73bULL,
00308         0x3c6ef372fe94f82bULL,
00309         0xa54ff53a5f1d36f1ULL,
00310         0x510e527fade682d1ULL,
00311         0x9b05688c2b3e6c1fULL,
00312         0x1f83d9abfb41bd6bULL,
00313         0x5be0cd19137e2179ULL
00314 };
00315 #endif /* !NO_64BIT */
00316 
00317 /*
00318  * Constant used by SHA256/384/512_End() functions for converting the
00319  * digest to a readable hexadecimal character string:
00320  */
00321 static const char *sha2_hex_digits = "0123456789abcdef";
00322 
00323 /*** SHA-256: *********************************************************/
00324 void SHA256_Init(SHA256_CTX* context) {
00325         if (context == (SHA256_CTX*)0) {
00326                 return;
00327         }
00328         bcopy(sha256_initial_hash_value, context->state, SHA256_DIGEST_LENGTH);
00329         bzero(context->buffer, SHA256_BLOCK_LENGTH);
00330 #if MINIX_64BIT
00331         context->bitcount= cvu64(0);
00332 #else /* !MINIX_64BIT */
00333         context->bitcount = 0;
00334 #endif /* MINIX_64BIT */
00335 }
00336 
00337 #ifdef SHA2_UNROLL_TRANSFORM
00338 
00339 /* Unrolled SHA-256 round macros: */
00340 
00341 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00342 
00343 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
00344         REVERSE32(*data++, W256[j]); \
00345         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
00346              K256[j] + W256[j]; \
00347         (d) += T1; \
00348         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
00349         j++
00350 
00351 #else /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00352 
00353 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
00354         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
00355              K256[j] + (W256[j] = *data++); \
00356         (d) += T1; \
00357         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
00358         j++
00359 
00360 #endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00361 
00362 #define ROUND256(a,b,c,d,e,f,g,h)       \
00363         s0 = W256[(j+1)&0x0f]; \
00364         s0 = sigma0_256(s0); \
00365         s1 = W256[(j+14)&0x0f]; \
00366         s1 = sigma1_256(s1); \
00367         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
00368              (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
00369         (d) += T1; \
00370         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
00371         j++
00372 
00373 void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
00374         sha2_word32     a, b, c, d, e, f, g, h, s0, s1;
00375         sha2_word32     T1, *W256;
00376         int             j;
00377 
00378         W256 = (sha2_word32*)context->buffer;
00379 
00380         /* Initialize registers with the prev. intermediate value */
00381         a = context->state[0];
00382         b = context->state[1];
00383         c = context->state[2];
00384         d = context->state[3];
00385         e = context->state[4];
00386         f = context->state[5];
00387         g = context->state[6];
00388         h = context->state[7];
00389 
00390         j = 0;
00391         do {
00392                 /* Rounds 0 to 15 (unrolled): */
00393                 ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
00394                 ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
00395                 ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
00396                 ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
00397                 ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
00398                 ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
00399                 ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
00400                 ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
00401         } while (j < 16);
00402 
00403         /* Now for the remaining rounds to 64: */
00404         do {
00405                 ROUND256(a,b,c,d,e,f,g,h);
00406                 ROUND256(h,a,b,c,d,e,f,g);
00407                 ROUND256(g,h,a,b,c,d,e,f);
00408                 ROUND256(f,g,h,a,b,c,d,e);
00409                 ROUND256(e,f,g,h,a,b,c,d);
00410                 ROUND256(d,e,f,g,h,a,b,c);
00411                 ROUND256(c,d,e,f,g,h,a,b);
00412                 ROUND256(b,c,d,e,f,g,h,a);
00413         } while (j < 64);
00414 
00415         /* Compute the current intermediate hash value */
00416         context->state[0] += a;
00417         context->state[1] += b;
00418         context->state[2] += c;
00419         context->state[3] += d;
00420         context->state[4] += e;
00421         context->state[5] += f;
00422         context->state[6] += g;
00423         context->state[7] += h;
00424 
00425         /* Clean up */
00426         a = b = c = d = e = f = g = h = T1 = 0;
00427 }
00428 
00429 #else /* SHA2_UNROLL_TRANSFORM */
00430 
00431 void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
00432         sha2_word32     a, b, c, d, e, f, g, h, s0, s1;
00433         sha2_word32     T1, T2, *W256;
00434         int             j;
00435 
00436         W256 = (sha2_word32*)context->buffer;
00437 
00438         /* Initialize registers with the prev. intermediate value */
00439         a = context->state[0];
00440         b = context->state[1];
00441         c = context->state[2];
00442         d = context->state[3];
00443         e = context->state[4];
00444         f = context->state[5];
00445         g = context->state[6];
00446         h = context->state[7];
00447 
00448         j = 0;
00449         do {
00450 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00451                 /* Copy data while converting to host byte order */
00452                 REVERSE32(*data++,W256[j]);
00453                 /* Apply the SHA-256 compression function to update a..h */
00454                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
00455 #else /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00456                 /* Apply the SHA-256 compression function to update a..h with copy */
00457                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
00458 #endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00459                 T2 = Sigma0_256(a) + Maj(a, b, c);
00460                 h = g;
00461                 g = f;
00462                 f = e;
00463                 e = d + T1;
00464                 d = c;
00465                 c = b;
00466                 b = a;
00467                 a = T1 + T2;
00468 
00469                 j++;
00470         } while (j < 16);
00471 
00472         do {
00473                 /* Part of the message block expansion: */
00474                 s0 = W256[(j+1)&0x0f];
00475                 s0 = sigma0_256(s0);
00476                 s1 = W256[(j+14)&0x0f]; 
00477                 s1 = sigma1_256(s1);
00478 
00479                 /* Apply the SHA-256 compression function to update a..h */
00480                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + 
00481                      (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
00482                 T2 = Sigma0_256(a) + Maj(a, b, c);
00483                 h = g;
00484                 g = f;
00485                 f = e;
00486                 e = d + T1;
00487                 d = c;
00488                 c = b;
00489                 b = a;
00490                 a = T1 + T2;
00491 
00492                 j++;
00493         } while (j < 64);
00494 
00495         /* Compute the current intermediate hash value */
00496         context->state[0] += a;
00497         context->state[1] += b;
00498         context->state[2] += c;
00499         context->state[3] += d;
00500         context->state[4] += e;
00501         context->state[5] += f;
00502         context->state[6] += g;
00503         context->state[7] += h;
00504 
00505         /* Clean up */
00506         a = b = c = d = e = f = g = h = T1 = T2 = 0;
00507 }
00508 
00509 #endif /* SHA2_UNROLL_TRANSFORM */
00510 
00511 void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
00512         unsigned int    freespace, usedspace;
00513 
00514         if (len == 0) {
00515                 /* Calling with no data is valid - we do nothing */
00516                 return;
00517         }
00518 
00519         /* Sanity check: */
00520         assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
00521 
00522 #if MINIX_64BIT
00523         usedspace= rem64u(context->bitcount, SHA256_BLOCK_LENGTH*8)/8;
00524 #else /* !MINIX_64BIT */
00525         usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
00526 #endif /* MINIX_64BIT */
00527         if (usedspace > 0) {
00528                 /* Calculate how much free space is available in the buffer */
00529                 freespace = SHA256_BLOCK_LENGTH - usedspace;
00530 
00531                 if (len >= freespace) {
00532                         /* Fill the buffer completely and process it */
00533                         bcopy(data, &context->buffer[usedspace], freespace);
00534 #if MINIX_64BIT
00535                         context->bitcount= add64u(context->bitcount,
00536                                 freespace << 3);
00537 #else /* !MINIX_64BIT */
00538                         context->bitcount += freespace << 3;
00539 #endif /* MINIX_64BIT */
00540                         len -= freespace;
00541                         data += freespace;
00542                         SHA256_Transform(context, (sha2_word32*)context->buffer);
00543                 } else {
00544                         /* The buffer is not yet full */
00545                         bcopy(data, &context->buffer[usedspace], len);
00546 #if MINIX_64BIT
00547                         context->bitcount= add64u(context->bitcount, len << 3);
00548 #else /* !MINIX_64BIT */
00549                         context->bitcount += len << 3;
00550 #endif /* MINIX_64BIT */
00551                         /* Clean up: */
00552                         usedspace = freespace = 0;
00553                         return;
00554                 }
00555         }
00556         while (len >= SHA256_BLOCK_LENGTH) {
00557                 /* Process as many complete blocks as we can */
00558                 SHA256_Transform(context, (const sha2_word32*)data);
00559 #if MINIX_64BIT
00560                 context->bitcount= add64u(context->bitcount,
00561                         SHA256_BLOCK_LENGTH << 3);
00562 #else /* !MINIX_64BIT */
00563                 context->bitcount += SHA256_BLOCK_LENGTH << 3;
00564 #endif /* MINIX_64BIT */
00565                 len -= SHA256_BLOCK_LENGTH;
00566                 data += SHA256_BLOCK_LENGTH;
00567         }
00568         if (len > 0) {
00569                 /* There's left-overs, so save 'em */
00570                 bcopy(data, context->buffer, len);
00571 #if MINIX_64BIT
00572                 context->bitcount= add64u(context->bitcount, len << 3);
00573 #else /* !MINIX_64BIT */
00574                 context->bitcount += len << 3;
00575 #endif /* MINIX_64BIT */
00576         }
00577         /* Clean up: */
00578         usedspace = freespace = 0;
00579 }
00580 
00581 void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
00582         sha2_word32     *d = (sha2_word32*)digest;
00583         unsigned int    usedspace;
00584 
00585         /* Sanity check: */
00586         assert(context != (SHA256_CTX*)0);
00587 
00588         /* If no digest buffer is passed, we don't bother doing this: */
00589         if (digest != (sha2_byte*)0) {
00590 #if MINIX_64BIT
00591                 usedspace= rem64u(context->bitcount, SHA256_BLOCK_LENGTH*8)/8;
00592 #else /* !MINIX_64BIT */
00593                 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
00594 #endif /* MINIX_64BIT */
00595 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00596                 /* Convert FROM host byte order */
00597                 REVERSE64(context->bitcount,context->bitcount);
00598 #endif
00599                 if (usedspace > 0) {
00600                         /* Begin padding with a 1 bit: */
00601                         context->buffer[usedspace++] = 0x80;
00602 
00603                         if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
00604                                 /* Set-up for the last transform: */
00605                                 bzero(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
00606                         } else {
00607                                 if (usedspace < SHA256_BLOCK_LENGTH) {
00608                                         bzero(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
00609                                 }
00610                                 /* Do second-to-last transform: */
00611                                 SHA256_Transform(context, (sha2_word32*)context->buffer);
00612 
00613                                 /* And set-up for the last transform: */
00614                                 bzero(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
00615                         }
00616                 } else {
00617                         /* Set-up for the last transform: */
00618                         bzero(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
00619 
00620                         /* Begin padding with a 1 bit: */
00621                         *context->buffer = 0x80;
00622                 }
00623                 /* Set the bit count: */
00624                 *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
00625 
00626                 /* Final transform: */
00627                 SHA256_Transform(context, (sha2_word32*)context->buffer);
00628 
00629 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00630                 {
00631                         /* Convert TO host byte order */
00632                         int     j;
00633                         for (j = 0; j < 8; j++) {
00634                                 REVERSE32(context->state[j],context->state[j]);
00635                                 *d++ = context->state[j];
00636                         }
00637                 }
00638 #else
00639                 bcopy(context->state, d, SHA256_DIGEST_LENGTH);
00640 #endif
00641         }
00642 
00643         /* Clean up state data: */
00644         bzero(context, sizeof(context));
00645         usedspace = 0;
00646 }
00647 
00648 char *SHA256_End(SHA256_CTX* context, char buffer[]) {
00649         sha2_byte       digest[SHA256_DIGEST_LENGTH], *d = digest;
00650         int             i;
00651 
00652         /* Sanity check: */
00653         assert(context != (SHA256_CTX*)0);
00654 
00655         if (buffer != (char*)0) {
00656                 SHA256_Final(digest, context);
00657 
00658                 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
00659                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
00660                         *buffer++ = sha2_hex_digits[*d & 0x0f];
00661                         d++;
00662                 }
00663                 *buffer = (char)0;
00664         } else {
00665                 bzero(context, sizeof(context));
00666         }
00667         bzero(digest, SHA256_DIGEST_LENGTH);
00668         return buffer;
00669 }
00670 
00671 char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
00672         SHA256_CTX      context;
00673 
00674         SHA256_Init(&context);
00675         SHA256_Update(&context, data, len);
00676         return SHA256_End(&context, digest);
00677 }
00678 
00679 #if !NO_64BIT
00680 
00681 /*** SHA-512: *********************************************************/
00682 void SHA512_Init(SHA512_CTX* context) {
00683         if (context == (SHA512_CTX*)0) {
00684                 return;
00685         }
00686         bcopy(sha512_initial_hash_value, context->state, SHA512_DIGEST_LENGTH);
00687         bzero(context->buffer, SHA512_BLOCK_LENGTH);
00688         context->bitcount[0] = context->bitcount[1] =  0;
00689 }
00690 
00691 #ifdef SHA2_UNROLL_TRANSFORM
00692 
00693 /* Unrolled SHA-512 round macros: */
00694 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00695 
00696 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
00697         REVERSE64(*data++, W512[j]); \
00698         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
00699              K512[j] + W512[j]; \
00700         (d) += T1, \
00701         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
00702         j++
00703 
00704 #else /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00705 
00706 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
00707         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
00708              K512[j] + (W512[j] = *data++); \
00709         (d) += T1; \
00710         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
00711         j++
00712 
00713 #endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00714 
00715 #define ROUND512(a,b,c,d,e,f,g,h)       \
00716         s0 = W512[(j+1)&0x0f]; \
00717         s0 = sigma0_512(s0); \
00718         s1 = W512[(j+14)&0x0f]; \
00719         s1 = sigma1_512(s1); \
00720         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
00721              (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
00722         (d) += T1; \
00723         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
00724         j++
00725 
00726 void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
00727         sha2_word64     a, b, c, d, e, f, g, h, s0, s1;
00728         sha2_word64     T1, *W512 = (sha2_word64*)context->buffer;
00729         int             j;
00730 
00731         /* Initialize registers with the prev. intermediate value */
00732         a = context->state[0];
00733         b = context->state[1];
00734         c = context->state[2];
00735         d = context->state[3];
00736         e = context->state[4];
00737         f = context->state[5];
00738         g = context->state[6];
00739         h = context->state[7];
00740 
00741         j = 0;
00742         do {
00743                 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
00744                 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
00745                 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
00746                 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
00747                 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
00748                 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
00749                 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
00750                 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
00751         } while (j < 16);
00752 
00753         /* Now for the remaining rounds up to 79: */
00754         do {
00755                 ROUND512(a,b,c,d,e,f,g,h);
00756                 ROUND512(h,a,b,c,d,e,f,g);
00757                 ROUND512(g,h,a,b,c,d,e,f);
00758                 ROUND512(f,g,h,a,b,c,d,e);
00759                 ROUND512(e,f,g,h,a,b,c,d);
00760                 ROUND512(d,e,f,g,h,a,b,c);
00761                 ROUND512(c,d,e,f,g,h,a,b);
00762                 ROUND512(b,c,d,e,f,g,h,a);
00763         } while (j < 80);
00764 
00765         /* Compute the current intermediate hash value */
00766         context->state[0] += a;
00767         context->state[1] += b;
00768         context->state[2] += c;
00769         context->state[3] += d;
00770         context->state[4] += e;
00771         context->state[5] += f;
00772         context->state[6] += g;
00773         context->state[7] += h;
00774 
00775         /* Clean up */
00776         a = b = c = d = e = f = g = h = T1 = 0;
00777 }
00778 
00779 #else /* SHA2_UNROLL_TRANSFORM */
00780 
00781 void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
00782         sha2_word64     a, b, c, d, e, f, g, h, s0, s1;
00783         sha2_word64     T1, T2, *W512 = (sha2_word64*)context->buffer;
00784         int             j;
00785 
00786         /* Initialize registers with the prev. intermediate value */
00787         a = context->state[0];
00788         b = context->state[1];
00789         c = context->state[2];
00790         d = context->state[3];
00791         e = context->state[4];
00792         f = context->state[5];
00793         g = context->state[6];
00794         h = context->state[7];
00795 
00796         j = 0;
00797         do {
00798 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00799                 /* Convert TO host byte order */
00800                 REVERSE64(*data++, W512[j]);
00801                 /* Apply the SHA-512 compression function to update a..h */
00802                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
00803 #else /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00804                 /* Apply the SHA-512 compression function to update a..h with copy */
00805                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
00806 #endif /* SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN */
00807                 T2 = Sigma0_512(a) + Maj(a, b, c);
00808                 h = g;
00809                 g = f;
00810                 f = e;
00811                 e = d + T1;
00812                 d = c;
00813                 c = b;
00814                 b = a;
00815                 a = T1 + T2;
00816 
00817                 j++;
00818         } while (j < 16);
00819 
00820         do {
00821                 /* Part of the message block expansion: */
00822                 s0 = W512[(j+1)&0x0f];
00823                 s0 = sigma0_512(s0);
00824                 s1 = W512[(j+14)&0x0f];
00825                 s1 =  sigma1_512(s1);
00826 
00827                 /* Apply the SHA-512 compression function to update a..h */
00828                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
00829                      (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
00830                 T2 = Sigma0_512(a) + Maj(a, b, c);
00831                 h = g;
00832                 g = f;
00833                 f = e;
00834                 e = d + T1;
00835                 d = c;
00836                 c = b;
00837                 b = a;
00838                 a = T1 + T2;
00839 
00840                 j++;
00841         } while (j < 80);
00842 
00843         /* Compute the current intermediate hash value */
00844         context->state[0] += a;
00845         context->state[1] += b;
00846         context->state[2] += c;
00847         context->state[3] += d;
00848         context->state[4] += e;
00849         context->state[5] += f;
00850         context->state[6] += g;
00851         context->state[7] += h;
00852 
00853         /* Clean up */
00854         a = b = c = d = e = f = g = h = T1 = T2 = 0;
00855 }
00856 
00857 #endif /* SHA2_UNROLL_TRANSFORM */
00858 
00859 void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
00860         unsigned int    freespace, usedspace;
00861 
00862         if (len == 0) {
00863                 /* Calling with no data is valid - we do nothing */
00864                 return;
00865         }
00866 
00867         /* Sanity check: */
00868         assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
00869 
00870         usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
00871         if (usedspace > 0) {
00872                 /* Calculate how much free space is available in the buffer */
00873                 freespace = SHA512_BLOCK_LENGTH - usedspace;
00874 
00875                 if (len >= freespace) {
00876                         /* Fill the buffer completely and process it */
00877                         bcopy(data, &context->buffer[usedspace], freespace);
00878                         ADDINC128(context->bitcount, freespace << 3);
00879                         len -= freespace;
00880                         data += freespace;
00881                         SHA512_Transform(context, (sha2_word64*)context->buffer);
00882                 } else {
00883                         /* The buffer is not yet full */
00884                         bcopy(data, &context->buffer[usedspace], len);
00885                         ADDINC128(context->bitcount, len << 3);
00886                         /* Clean up: */
00887                         usedspace = freespace = 0;
00888                         return;
00889                 }
00890         }
00891         while (len >= SHA512_BLOCK_LENGTH) {
00892                 /* Process as many complete blocks as we can */
00893                 SHA512_Transform(context, (const sha2_word64*)data);
00894                 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
00895                 len -= SHA512_BLOCK_LENGTH;
00896                 data += SHA512_BLOCK_LENGTH;
00897         }
00898         if (len > 0) {
00899                 /* There's left-overs, so save 'em */
00900                 bcopy(data, context->buffer, len);
00901                 ADDINC128(context->bitcount, len << 3);
00902         }
00903         /* Clean up: */
00904         usedspace = freespace = 0;
00905 }
00906 
00907 void SHA512_Last(SHA512_CTX* context) {
00908         unsigned int    usedspace;
00909 
00910         usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
00911 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00912         /* Convert FROM host byte order */
00913         REVERSE64(context->bitcount[0],context->bitcount[0]);
00914         REVERSE64(context->bitcount[1],context->bitcount[1]);
00915 #endif
00916         if (usedspace > 0) {
00917                 /* Begin padding with a 1 bit: */
00918                 context->buffer[usedspace++] = 0x80;
00919 
00920                 if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
00921                         /* Set-up for the last transform: */
00922                         bzero(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
00923                 } else {
00924                         if (usedspace < SHA512_BLOCK_LENGTH) {
00925                                 bzero(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
00926                         }
00927                         /* Do second-to-last transform: */
00928                         SHA512_Transform(context, (sha2_word64*)context->buffer);
00929 
00930                         /* And set-up for the last transform: */
00931                         bzero(context->buffer, SHA512_BLOCK_LENGTH - 2);
00932                 }
00933         } else {
00934                 /* Prepare for final transform: */
00935                 bzero(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
00936 
00937                 /* Begin padding with a 1 bit: */
00938                 *context->buffer = 0x80;
00939         }
00940         /* Store the length of input data (in bits): */
00941         *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
00942         *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
00943 
00944         /* Final transform: */
00945         SHA512_Transform(context, (sha2_word64*)context->buffer);
00946 }
00947 
00948 void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
00949         sha2_word64     *d = (sha2_word64*)digest;
00950 
00951         /* Sanity check: */
00952         assert(context != (SHA512_CTX*)0);
00953 
00954         /* If no digest buffer is passed, we don't bother doing this: */
00955         if (digest != (sha2_byte*)0) {
00956                 SHA512_Last(context);
00957 
00958                 /* Save the hash data for output: */
00959 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
00960                 {
00961                         /* Convert TO host byte order */
00962                         int     j;
00963                         for (j = 0; j < 8; j++) {
00964                                 REVERSE64(context->state[j],context->state[j]);
00965                                 *d++ = context->state[j];
00966                         }
00967                 }
00968 #else
00969                 bcopy(context->state, d, SHA512_DIGEST_LENGTH);
00970 #endif
00971         }
00972 
00973         /* Zero out state data */
00974         bzero(context, sizeof(context));
00975 }
00976 
00977 char *SHA512_End(SHA512_CTX* context, char buffer[]) {
00978         sha2_byte       digest[SHA512_DIGEST_LENGTH], *d = digest;
00979         int             i;
00980 
00981         /* Sanity check: */
00982         assert(context != (SHA512_CTX*)0);
00983 
00984         if (buffer != (char*)0) {
00985                 SHA512_Final(digest, context);
00986 
00987                 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
00988                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
00989                         *buffer++ = sha2_hex_digits[*d & 0x0f];
00990                         d++;
00991                 }
00992                 *buffer = (char)0;
00993         } else {
00994                 bzero(context, sizeof(context));
00995         }
00996         bzero(digest, SHA512_DIGEST_LENGTH);
00997         return buffer;
00998 }
00999 
01000 char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
01001         SHA512_CTX      context;
01002 
01003         SHA512_Init(&context);
01004         SHA512_Update(&context, data, len);
01005         return SHA512_End(&context, digest);
01006 }
01007 
01008 /*** SHA-384: *********************************************************/
01009 void SHA384_Init(SHA384_CTX* context) {
01010         if (context == (SHA384_CTX*)0) {
01011                 return;
01012         }
01013         bcopy(sha384_initial_hash_value, context->state, SHA512_DIGEST_LENGTH);
01014         bzero(context->buffer, SHA384_BLOCK_LENGTH);
01015         context->bitcount[0] = context->bitcount[1] = 0;
01016 }
01017 
01018 void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
01019         SHA512_Update((SHA512_CTX*)context, data, len);
01020 }
01021 
01022 void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
01023         sha2_word64     *d = (sha2_word64*)digest;
01024 
01025         /* Sanity check: */
01026         assert(context != (SHA384_CTX*)0);
01027 
01028         /* If no digest buffer is passed, we don't bother doing this: */
01029         if (digest != (sha2_byte*)0) {
01030                 SHA512_Last((SHA512_CTX*)context);
01031 
01032                 /* Save the hash data for output: */
01033 #if SHA2_BYTE_ORDER == SHA2_LITTLE_ENDIAN
01034                 {
01035                         /* Convert TO host byte order */
01036                         int     j;
01037                         for (j = 0; j < 6; j++) {
01038                                 REVERSE64(context->state[j],context->state[j]);
01039                                 *d++ = context->state[j];
01040                         }
01041                 }
01042 #else
01043                 bcopy(context->state, d, SHA384_DIGEST_LENGTH);
01044 #endif
01045         }
01046 
01047         /* Zero out state data */
01048         bzero(context, sizeof(context));
01049 }
01050 
01051 char *SHA384_End(SHA384_CTX* context, char buffer[]) {
01052         sha2_byte       digest[SHA384_DIGEST_LENGTH], *d = digest;
01053         int             i;
01054 
01055         /* Sanity check: */
01056         assert(context != (SHA384_CTX*)0);
01057 
01058         if (buffer != (char*)0) {
01059                 SHA384_Final(digest, context);
01060 
01061                 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
01062                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
01063                         *buffer++ = sha2_hex_digits[*d & 0x0f];
01064                         d++;
01065                 }
01066                 *buffer = (char)0;
01067         } else {
01068                 bzero(context, sizeof(context));
01069         }
01070         bzero(digest, SHA384_DIGEST_LENGTH);
01071         return buffer;
01072 }
01073 
01074 char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
01075         SHA384_CTX      context;
01076 
01077         SHA384_Init(&context);
01078         SHA384_Update(&context, data, len);
01079         return SHA384_End(&context, digest);
01080 }
01081 
01082 #endif /* !NO_64BIT */
01083 
01084 /*
01085  * $PchId: sha2.c,v 1.1 2005/06/28 14:29:23 philip Exp $
01086  */

Generated on Fri Apr 14 22:57:18 2006 for minix by  doxygen 1.4.6