sha2.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005-2007, 2009, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*      $FreeBSD: src/sys/crypto/sha2/sha2.c,v 1.2.2.2 2002/03/05 08:36:47 ume Exp $    */
00020 /*      $KAME: sha2.c,v 1.8 2001/11/08 01:07:52 itojun Exp $    */
00021 
00022 /*
00023  * sha2.c
00024  *
00025  * Version 1.0.0beta1
00026  *
00027  * Written by Aaron D. Gifford <me@aarongifford.com>
00028  *
00029  * Copyright 2000 Aaron D. Gifford.  All rights reserved.
00030  *
00031  * Redistribution and use in source and binary forms, with or without
00032  * modification, are permitted provided that the following conditions
00033  * are met:
00034  * 1. Redistributions of source code must retain the above copyright
00035  *    notice, this list of conditions and the following disclaimer.
00036  * 2. Redistributions in binary form must reproduce the above copyright
00037  *    notice, this list of conditions and the following disclaimer in the
00038  *    documentation and/or other materials provided with the distribution.
00039  * 3. Neither the name of the copyright holder nor the names of contributors
00040  *    may be used to endorse or promote products derived from this software
00041  *    without specific prior written permission.
00042  *
00043  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
00044  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00045  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00046  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
00047  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00048  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00049  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00050  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00051  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00052  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00053  * SUCH DAMAGE.
00054  *
00055  */
00056 
00057 
00058 #include <config.h>
00059 
00060 #include <isc/assertions.h>
00061 #include <isc/platform.h>
00062 #include <isc/sha2.h>
00063 #include <isc/string.h>
00064 #include <isc/util.h>
00065 
00066 #if PKCS11CRYPTO
00067 #include <pk11/internal.h>
00068 #include <pk11/pk11.h>
00069 #endif
00070 
00071 #ifdef ISC_PLATFORM_OPENSSLHASH
00072 
00073 void
00074 isc_sha224_init(isc_sha224_t *context) {
00075         if (context == (isc_sha224_t *)0) {
00076                 return;
00077         }
00078         RUNTIME_CHECK(EVP_DigestInit(context, EVP_sha224()) == 1);
00079 }
00080 
00081 void
00082 isc_sha224_invalidate(isc_sha224_t *context) {
00083         EVP_MD_CTX_cleanup(context);
00084 }
00085 
00086 void
00087 isc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {
00088         if (len == 0U) {
00089                 /* Calling with no data is valid - we do nothing */
00090                 return;
00091         }
00092 
00093         /* Sanity check: */
00094         REQUIRE(context != (isc_sha224_t *)0 && data != (isc_uint8_t*)0);
00095 
00096         RUNTIME_CHECK(EVP_DigestUpdate(context,
00097                                        (const void *) data, len) == 1);
00098 }
00099 
00100 void
00101 isc_sha224_final(isc_uint8_t digest[], isc_sha224_t *context) {
00102         /* Sanity check: */
00103         REQUIRE(context != (isc_sha224_t *)0);
00104 
00105         /* If no digest buffer is passed, we don't bother doing this: */
00106         if (digest != (isc_uint8_t*)0) {
00107                 RUNTIME_CHECK(EVP_DigestFinal(context, digest, NULL) == 1);
00108         } else {
00109                 EVP_MD_CTX_cleanup(context);
00110         }
00111 }
00112 
00113 void
00114 isc_sha256_init(isc_sha256_t *context) {
00115         if (context == (isc_sha256_t *)0) {
00116                 return;
00117         }
00118         RUNTIME_CHECK(EVP_DigestInit(context, EVP_sha256()) == 1);
00119 }
00120 
00121 void
00122 isc_sha256_invalidate(isc_sha256_t *context) {
00123         EVP_MD_CTX_cleanup(context);
00124 }
00125 
00126 void
00127 isc_sha256_update(isc_sha256_t *context, const isc_uint8_t *data, size_t len) {
00128         if (len == 0U) {
00129                 /* Calling with no data is valid - we do nothing */
00130                 return;
00131         }
00132 
00133         /* Sanity check: */
00134         REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);
00135 
00136         RUNTIME_CHECK(EVP_DigestUpdate(context,
00137                                        (const void *) data, len) == 1);
00138 }
00139 
00140 void
00141 isc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {
00142         /* Sanity check: */
00143         REQUIRE(context != (isc_sha256_t *)0);
00144 
00145         /* If no digest buffer is passed, we don't bother doing this: */
00146         if (digest != (isc_uint8_t*)0) {
00147                 RUNTIME_CHECK(EVP_DigestFinal(context, digest, NULL) == 1);
00148         } else {
00149                 EVP_MD_CTX_cleanup(context);
00150         }
00151 }
00152 
00153 void
00154 isc_sha512_init(isc_sha512_t *context) {
00155         if (context == (isc_sha512_t *)0) {
00156                 return;
00157         }
00158         RUNTIME_CHECK(EVP_DigestInit(context, EVP_sha512()) == 1);
00159 }
00160 
00161 void
00162 isc_sha512_invalidate(isc_sha512_t *context) {
00163         EVP_MD_CTX_cleanup(context);
00164 }
00165 
00166 void isc_sha512_update(isc_sha512_t *context, const isc_uint8_t *data, size_t len) {
00167         if (len == 0U) {
00168                 /* Calling with no data is valid - we do nothing */
00169                 return;
00170         }
00171 
00172         /* Sanity check: */
00173         REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
00174 
00175         RUNTIME_CHECK(EVP_DigestUpdate(context,
00176                                        (const void *) data, len) == 1);
00177 }
00178 
00179 void isc_sha512_final(isc_uint8_t digest[], isc_sha512_t *context) {
00180         /* Sanity check: */
00181         REQUIRE(context != (isc_sha512_t *)0);
00182 
00183         /* If no digest buffer is passed, we don't bother doing this: */
00184         if (digest != (isc_uint8_t*)0) {
00185                 RUNTIME_CHECK(EVP_DigestFinal(context, digest, NULL) == 1);
00186         } else {
00187                 EVP_MD_CTX_cleanup(context);
00188         }
00189 }
00190 
00191 void
00192 isc_sha384_init(isc_sha384_t *context) {
00193         if (context == (isc_sha384_t *)0) {
00194                 return;
00195         }
00196         RUNTIME_CHECK(EVP_DigestInit(context, EVP_sha384()) == 1);
00197 }
00198 
00199 void
00200 isc_sha384_invalidate(isc_sha384_t *context) {
00201         EVP_MD_CTX_cleanup(context);
00202 }
00203 
00204 void
00205 isc_sha384_update(isc_sha384_t *context, const isc_uint8_t* data, size_t len) {
00206         if (len == 0U) {
00207                 /* Calling with no data is valid - we do nothing */
00208                 return;
00209         }
00210 
00211         /* Sanity check: */
00212         REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
00213 
00214         RUNTIME_CHECK(EVP_DigestUpdate(context,
00215                                        (const void *) data, len) == 1);
00216 }
00217 
00218 void
00219 isc_sha384_final(isc_uint8_t digest[], isc_sha384_t *context) {
00220         /* Sanity check: */
00221         REQUIRE(context != (isc_sha384_t *)0);
00222 
00223         /* If no digest buffer is passed, we don't bother doing this: */
00224         if (digest != (isc_uint8_t*)0) {
00225                 RUNTIME_CHECK(EVP_DigestFinal(context, digest, NULL) == 1);
00226         } else {
00227                 EVP_MD_CTX_cleanup(context);
00228         }
00229 }
00230 
00231 #elif PKCS11CRYPTO
00232 
00233 void
00234 isc_sha224_init(isc_sha224_t *context) {
00235         CK_RV rv;
00236         CK_MECHANISM mech = { CKM_SHA224, NULL, 0 };
00237 
00238         if (context == (isc_sha224_t *)0) {
00239                 return;
00240         }
00241         RUNTIME_CHECK(pk11_get_session(context, OP_DIGEST, ISC_TRUE, ISC_FALSE,
00242                                        ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
00243         PK11_FATALCHECK(pkcs_C_DigestInit, (context->session, &mech));
00244 }
00245 
00246 void
00247 isc_sha224_invalidate(isc_sha224_t *context) {
00248         CK_BYTE garbage[ISC_SHA224_DIGESTLENGTH];
00249         CK_ULONG len = ISC_SHA224_DIGESTLENGTH;
00250 
00251         if (context->handle == NULL)
00252                 return;
00253         (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00254         memset(garbage, 0, sizeof(garbage));
00255         pk11_return_session(context);
00256 }
00257 
00258 void
00259 isc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {
00260         CK_RV rv;
00261         CK_BYTE_PTR pPart;
00262 
00263         if (len == 0U) {
00264                 /* Calling with no data is valid - we do nothing */
00265                 return;
00266         }
00267 
00268         /* Sanity check: */
00269         REQUIRE(context != (isc_sha224_t *)0 && data != (isc_uint8_t*)0);
00270 
00271         DE_CONST(data, pPart);
00272         PK11_FATALCHECK(pkcs_C_DigestUpdate,
00273                         (context->session, pPart, (CK_ULONG) len));
00274 }
00275 
00276 void
00277 isc_sha224_final(isc_uint8_t digest[], isc_sha224_t *context) {
00278         CK_RV rv;
00279         CK_ULONG len = ISC_SHA224_DIGESTLENGTH;
00280 
00281         /* Sanity check: */
00282         REQUIRE(context != (isc_sha224_t *)0);
00283 
00284         /* If no digest buffer is passed, we don't bother doing this: */
00285         if (digest != (isc_uint8_t*)0) {
00286                 PK11_FATALCHECK(pkcs_C_DigestFinal,
00287                                 (context->session,
00288                                  (CK_BYTE_PTR) digest,
00289                                  &len));
00290         } else {
00291                 CK_BYTE garbage[ISC_SHA224_DIGESTLENGTH];
00292 
00293                 (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00294                 memset(garbage, 0, sizeof(garbage));
00295         }
00296         pk11_return_session(context);
00297 }
00298 
00299 void
00300 isc_sha256_init(isc_sha256_t *context) {
00301         CK_RV rv;
00302         CK_MECHANISM mech = { CKM_SHA256, NULL, 0 };
00303 
00304         if (context == (isc_sha256_t *)0) {
00305                 return;
00306         }
00307         RUNTIME_CHECK(pk11_get_session(context, OP_DIGEST, ISC_TRUE, ISC_FALSE,
00308                                        ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
00309         PK11_FATALCHECK(pkcs_C_DigestInit, (context->session, &mech));
00310 }
00311 
00312 void
00313 isc_sha256_invalidate(isc_sha256_t *context) {
00314         CK_BYTE garbage[ISC_SHA256_DIGESTLENGTH];
00315         CK_ULONG len = ISC_SHA256_DIGESTLENGTH;
00316 
00317         if (context->handle == NULL)
00318                 return;
00319         (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00320         memset(garbage, 0, sizeof(garbage));
00321         pk11_return_session(context);
00322 }
00323 
00324 void
00325 isc_sha256_update(isc_sha256_t *context, const isc_uint8_t* data, size_t len) {
00326         CK_RV rv;
00327         CK_BYTE_PTR pPart;
00328 
00329         if (len == 0U) {
00330                 /* Calling with no data is valid - we do nothing */
00331                 return;
00332         }
00333 
00334         /* Sanity check: */
00335         REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);
00336 
00337         DE_CONST(data, pPart);
00338         PK11_FATALCHECK(pkcs_C_DigestUpdate,
00339                         (context->session, pPart, (CK_ULONG) len));
00340 }
00341 
00342 void
00343 isc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {
00344         CK_RV rv;
00345         CK_ULONG len = ISC_SHA256_DIGESTLENGTH;
00346 
00347         /* Sanity check: */
00348         REQUIRE(context != (isc_sha256_t *)0);
00349 
00350         /* If no digest buffer is passed, we don't bother doing this: */
00351         if (digest != (isc_uint8_t*)0) {
00352                 PK11_FATALCHECK(pkcs_C_DigestFinal,
00353                                 (context->session,
00354                                  (CK_BYTE_PTR) digest,
00355                                  &len));
00356         } else {
00357                 CK_BYTE garbage[ISC_SHA256_DIGESTLENGTH];
00358 
00359                 (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00360                 memset(garbage, 0, sizeof(garbage));
00361         }
00362         pk11_return_session(context);
00363 }
00364 
00365 void
00366 isc_sha512_init(isc_sha512_t *context) {
00367         CK_RV rv;
00368         CK_MECHANISM mech = { CKM_SHA512, NULL, 0 };
00369 
00370         if (context == (isc_sha512_t *)0) {
00371                 return;
00372         }
00373         RUNTIME_CHECK(pk11_get_session(context, OP_DIGEST, ISC_TRUE, ISC_FALSE,
00374                                        ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
00375         PK11_FATALCHECK(pkcs_C_DigestInit, (context->session, &mech));
00376 }
00377 
00378 void
00379 isc_sha512_invalidate(isc_sha512_t *context) {
00380         CK_BYTE garbage[ISC_SHA512_DIGESTLENGTH];
00381         CK_ULONG len = ISC_SHA512_DIGESTLENGTH;
00382 
00383         if (context->handle == NULL)
00384                 return;
00385         (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00386         memset(garbage, 0, sizeof(garbage));
00387         pk11_return_session(context);
00388 }
00389 
00390 void
00391 isc_sha512_update(isc_sha512_t *context, const isc_uint8_t* data, size_t len) {
00392         CK_RV rv;
00393         CK_BYTE_PTR pPart;
00394 
00395         if (len == 0U) {
00396                 /* Calling with no data is valid - we do nothing */
00397                 return;
00398         }
00399 
00400         /* Sanity check: */
00401         REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
00402 
00403         DE_CONST(data, pPart);
00404         PK11_FATALCHECK(pkcs_C_DigestUpdate,
00405                         (context->session, pPart, (CK_ULONG) len));
00406 }
00407 
00408 void
00409 isc_sha512_final(isc_uint8_t digest[], isc_sha512_t *context) {
00410         CK_RV rv;
00411         CK_ULONG len = ISC_SHA512_DIGESTLENGTH;
00412 
00413         /* Sanity check: */
00414         REQUIRE(context != (isc_sha512_t *)0);
00415 
00416         /* If no digest buffer is passed, we don't bother doing this: */
00417         if (digest != (isc_uint8_t*)0) {
00418                 PK11_FATALCHECK(pkcs_C_DigestFinal,
00419                                 (context->session,
00420                                  (CK_BYTE_PTR) digest,
00421                                  &len));
00422         } else {
00423                 CK_BYTE garbage[ISC_SHA512_DIGESTLENGTH];
00424 
00425                 (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00426                 memset(garbage, 0, sizeof(garbage));
00427         }
00428         pk11_return_session(context);
00429 }
00430 
00431 void
00432 isc_sha384_init(isc_sha384_t *context) {
00433         CK_RV rv;
00434         CK_MECHANISM mech = { CKM_SHA384, NULL, 0 };
00435 
00436         if (context == (isc_sha384_t *)0) {
00437                 return;
00438         }
00439         RUNTIME_CHECK(pk11_get_session(context, OP_DIGEST, ISC_TRUE, ISC_FALSE,
00440                                        ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
00441         PK11_FATALCHECK(pkcs_C_DigestInit, (context->session, &mech));
00442 }
00443 
00444 void
00445 isc_sha384_invalidate(isc_sha384_t *context) {
00446         CK_BYTE garbage[ISC_SHA384_DIGESTLENGTH];
00447         CK_ULONG len = ISC_SHA384_DIGESTLENGTH;
00448 
00449         if (context->handle == NULL)
00450                 return;
00451         (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00452         memset(garbage, 0, sizeof(garbage));
00453         pk11_return_session(context);
00454 }
00455 
00456 void
00457 isc_sha384_update(isc_sha384_t *context, const isc_uint8_t* data, size_t len) {
00458         CK_RV rv;
00459         CK_BYTE_PTR pPart;
00460 
00461         if (len == 0U) {
00462                 /* Calling with no data is valid - we do nothing */
00463                 return;
00464         }
00465 
00466         /* Sanity check: */
00467         REQUIRE(context != (isc_sha384_t *)0 && data != (isc_uint8_t*)0);
00468 
00469         DE_CONST(data, pPart);
00470         PK11_FATALCHECK(pkcs_C_DigestUpdate,
00471                         (context->session, pPart, (CK_ULONG) len));
00472 }
00473 
00474 void
00475 isc_sha384_final(isc_uint8_t digest[], isc_sha384_t *context) {
00476         CK_RV rv;
00477         CK_ULONG len = ISC_SHA384_DIGESTLENGTH;
00478 
00479         /* Sanity check: */
00480         REQUIRE(context != (isc_sha384_t *)0);
00481 
00482         /* If no digest buffer is passed, we don't bother doing this: */
00483         if (digest != (isc_uint8_t*)0) {
00484                 PK11_FATALCHECK(pkcs_C_DigestFinal,
00485                                 (context->session,
00486                                  (CK_BYTE_PTR) digest,
00487                                  &len));
00488         } else {
00489                 CK_BYTE garbage[ISC_SHA384_DIGESTLENGTH];
00490 
00491                 (void) pkcs_C_DigestFinal(context->session, garbage, &len);
00492                 memset(garbage, 0, sizeof(garbage));
00493         }
00494         pk11_return_session(context);
00495 }
00496 
00497 #else
00498 
00499 /*
00500  * UNROLLED TRANSFORM LOOP NOTE:
00501  * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
00502  * loop version for the hash transform rounds (defined using macros
00503  * later in this file).  Either define on the command line, for example:
00504  *
00505  *   cc -DISC_SHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
00506  *
00507  * or define below:
00508  *
00509  *   \#define ISC_SHA2_UNROLL_TRANSFORM
00510  *
00511  */
00512 
00513 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
00514 /*
00515  * BYTE_ORDER NOTE:
00516  *
00517  * Please make sure that your system defines BYTE_ORDER.  If your
00518  * architecture is little-endian, make sure it also defines
00519  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
00520  * equivalent.
00521  *
00522  * If your system does not define the above, then you can do so by
00523  * hand like this:
00524  *
00525  *   \#define LITTLE_ENDIAN 1234
00526  *   \#define BIG_ENDIAN    4321
00527  *
00528  * And for little-endian machines, add:
00529  *
00530  *   \#define BYTE_ORDER LITTLE_ENDIAN
00531  *
00532  * Or for big-endian machines:
00533  *
00534  *   \#define BYTE_ORDER BIG_ENDIAN
00535  *
00536  * The FreeBSD machine this was written on defines BYTE_ORDER
00537  * appropriately by including <sys/types.h> (which in turn includes
00538  * <machine/endian.h> where the appropriate definitions are actually
00539  * made).
00540  */
00541 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
00542 #ifndef BYTE_ORDER
00543 #ifndef BIG_ENDIAN
00544 #define BIG_ENDIAN 4321
00545 #endif
00546 #ifndef LITTLE_ENDIAN
00547 #define LITTLE_ENDIAN 1234
00548 #endif
00549 #ifdef WORDS_BIGENDIAN
00550 #define BYTE_ORDER BIG_ENDIAN
00551 #else
00552 #define BYTE_ORDER LITTLE_ENDIAN
00553 #endif
00554 #else
00555 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
00556 #endif
00557 #endif
00558 
00559 /*** SHA-256/384/512 Various Length Definitions ***********************/
00560 /* NOTE: Most of these are in sha2.h */
00561 #define ISC_SHA256_SHORT_BLOCK_LENGTH   (ISC_SHA256_BLOCK_LENGTH - 8)
00562 #define ISC_SHA384_SHORT_BLOCK_LENGTH   (ISC_SHA384_BLOCK_LENGTH - 16)
00563 #define ISC_SHA512_SHORT_BLOCK_LENGTH   (ISC_SHA512_BLOCK_LENGTH - 16)
00564 
00565 
00566 /*** ENDIAN REVERSAL MACROS *******************************************/
00567 #if BYTE_ORDER == LITTLE_ENDIAN
00568 #define REVERSE32(w,x)  { \
00569         isc_uint32_t tmp = (w); \
00570         tmp = (tmp >> 16) | (tmp << 16); \
00571         (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
00572 }
00573 #ifdef WIN32
00574 #define REVERSE64(w,x)  { \
00575         isc_uint64_t tmp = (w); \
00576         tmp = (tmp >> 32) | (tmp << 32); \
00577         tmp = ((tmp & 0xff00ff00ff00ff00UL) >> 8) | \
00578               ((tmp & 0x00ff00ff00ff00ffUL) << 8); \
00579         (x) = ((tmp & 0xffff0000ffff0000UL) >> 16) | \
00580               ((tmp & 0x0000ffff0000ffffUL) << 16); \
00581 }
00582 #else
00583 #define REVERSE64(w,x)  { \
00584         isc_uint64_t tmp = (w); \
00585         tmp = (tmp >> 32) | (tmp << 32); \
00586         tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
00587               ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
00588         (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
00589               ((tmp & 0x0000ffff0000ffffULL) << 16); \
00590 }
00591 #endif
00592 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
00593 
00594 /*
00595  * Macro for incrementally adding the unsigned 64-bit integer n to the
00596  * unsigned 128-bit integer (represented using a two-element array of
00597  * 64-bit words):
00598  */
00599 #define ADDINC128(w,n)  { \
00600         (w)[0] += (isc_uint64_t)(n); \
00601         if ((w)[0] < (n)) { \
00602                 (w)[1]++; \
00603         } \
00604 }
00605 
00606 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
00607 /*
00608  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
00609  *
00610  *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
00611  *   S is a ROTATION) because the SHA-256/384/512 description document
00612  *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
00613  *   same "backwards" definition.
00614  */
00615 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
00616 #define R(b,x)          ((x) >> (b))
00617 /* 32-bit Rotate-right (used in SHA-256): */
00618 #define S32(b,x)        (((x) >> (b)) | ((x) << (32 - (b))))
00619 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
00620 #define S64(b,x)        (((x) >> (b)) | ((x) << (64 - (b))))
00621 
00622 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
00623 #define Ch(x,y,z)       (((x) & (y)) ^ ((~(x)) & (z)))
00624 #define Maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
00625 
00626 /* Four of six logical functions used in SHA-256: */
00627 #define Sigma0_256(x)   (S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
00628 #define Sigma1_256(x)   (S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
00629 #define sigma0_256(x)   (S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
00630 #define sigma1_256(x)   (S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
00631 
00632 /* Four of six logical functions used in SHA-384 and SHA-512: */
00633 #define Sigma0_512(x)   (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
00634 #define Sigma1_512(x)   (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
00635 #define sigma0_512(x)   (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
00636 #define sigma1_512(x)   (S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
00637 
00638 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
00639 /* NOTE: These should not be accessed directly from outside this
00640  * library -- they are intended for private internal visibility/use
00641  * only.
00642  */
00643 void isc_sha512_last(isc_sha512_t *);
00644 void isc_sha256_transform(isc_sha256_t *, const isc_uint32_t*);
00645 void isc_sha512_transform(isc_sha512_t *, const isc_uint64_t*);
00646 
00647 
00648 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
00649 /* Hash constant words K for SHA-224 and SHA-256: */
00650 static const isc_uint32_t K256[64] = {
00651         0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
00652         0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
00653         0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
00654         0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
00655         0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
00656         0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
00657         0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
00658         0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
00659         0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
00660         0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
00661         0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
00662         0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
00663         0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
00664         0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
00665         0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
00666         0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
00667 };
00668 
00669 /* Initial hash value H for SHA-224: */
00670 static const isc_uint32_t sha224_initial_hash_value[8] = {
00671         0xc1059ed8UL,
00672         0x367cd507UL,
00673         0x3070dd17UL,
00674         0xf70e5939UL,
00675         0xffc00b31UL,
00676         0x68581511UL,
00677         0x64f98fa7UL,
00678         0xbefa4fa4UL
00679 };
00680 
00681 /* Initial hash value H for SHA-256: */
00682 static const isc_uint32_t sha256_initial_hash_value[8] = {
00683         0x6a09e667UL,
00684         0xbb67ae85UL,
00685         0x3c6ef372UL,
00686         0xa54ff53aUL,
00687         0x510e527fUL,
00688         0x9b05688cUL,
00689         0x1f83d9abUL,
00690         0x5be0cd19UL
00691 };
00692 
00693 #ifdef WIN32
00694 /* Hash constant words K for SHA-384 and SHA-512: */
00695 static const isc_uint64_t K512[80] = {
00696         0x428a2f98d728ae22UL, 0x7137449123ef65cdUL,
00697         0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL,
00698         0x3956c25bf348b538UL, 0x59f111f1b605d019UL,
00699         0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL,
00700         0xd807aa98a3030242UL, 0x12835b0145706fbeUL,
00701         0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL,
00702         0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL,
00703         0x9bdc06a725c71235UL, 0xc19bf174cf692694UL,
00704         0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL,
00705         0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL,
00706         0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL,
00707         0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL,
00708         0x983e5152ee66dfabUL, 0xa831c66d2db43210UL,
00709         0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL,
00710         0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
00711         0x06ca6351e003826fUL, 0x142929670a0e6e70UL,
00712         0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL,
00713         0x4d2c6dfc5ac42aedUL, 0x53380d139d95b3dfUL,
00714         0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL,
00715         0x81c2c92e47edaee6UL, 0x92722c851482353bUL,
00716         0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL,
00717         0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL,
00718         0xd192e819d6ef5218UL, 0xd69906245565a910UL,
00719         0xf40e35855771202aUL, 0x106aa07032bbd1b8UL,
00720         0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL,
00721         0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL,
00722         0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL,
00723         0x5b9cca4f7763e373UL, 0x682e6ff3d6b2b8a3UL,
00724         0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL,
00725         0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
00726         0x90befffa23631e28UL, 0xa4506cebde82bde9UL,
00727         0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL,
00728         0xca273eceea26619cUL, 0xd186b8c721c0c207UL,
00729         0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL,
00730         0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL,
00731         0x113f9804bef90daeUL, 0x1b710b35131c471bUL,
00732         0x28db77f523047d84UL, 0x32caab7b40c72493UL,
00733         0x3c9ebe0a15c9bebcUL, 0x431d67c49c100d4cUL,
00734         0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL,
00735         0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL
00736 };
00737 
00738 /* Initial hash value H for SHA-384: */
00739 static const isc_uint64_t sha384_initial_hash_value[8] = {
00740         0xcbbb9d5dc1059ed8UL,
00741         0x629a292a367cd507UL,
00742         0x9159015a3070dd17UL,
00743         0x152fecd8f70e5939UL,
00744         0x67332667ffc00b31UL,
00745         0x8eb44a8768581511UL,
00746         0xdb0c2e0d64f98fa7UL,
00747         0x47b5481dbefa4fa4UL
00748 };
00749 
00750 /* Initial hash value H for SHA-512: */
00751 static const isc_uint64_t sha512_initial_hash_value[8] = {
00752         0x6a09e667f3bcc908U,
00753         0xbb67ae8584caa73bUL,
00754         0x3c6ef372fe94f82bUL,
00755         0xa54ff53a5f1d36f1UL,
00756         0x510e527fade682d1UL,
00757         0x9b05688c2b3e6c1fUL,
00758         0x1f83d9abfb41bd6bUL,
00759         0x5be0cd19137e2179UL
00760 };
00761 #else
00762 /* Hash constant words K for SHA-384 and SHA-512: */
00763 static const isc_uint64_t K512[80] = {
00764         0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
00765         0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
00766         0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
00767         0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
00768         0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
00769         0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
00770         0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
00771         0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
00772         0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
00773         0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
00774         0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
00775         0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
00776         0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
00777         0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
00778         0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
00779         0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
00780         0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
00781         0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
00782         0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
00783         0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
00784         0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
00785         0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
00786         0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
00787         0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
00788         0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
00789         0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
00790         0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
00791         0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
00792         0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
00793         0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
00794         0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
00795         0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
00796         0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
00797         0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
00798         0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
00799         0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
00800         0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
00801         0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
00802         0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
00803         0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
00804 };
00805 
00806 /* Initial hash value H for SHA-384: */
00807 static const isc_uint64_t sha384_initial_hash_value[8] = {
00808         0xcbbb9d5dc1059ed8ULL,
00809         0x629a292a367cd507ULL,
00810         0x9159015a3070dd17ULL,
00811         0x152fecd8f70e5939ULL,
00812         0x67332667ffc00b31ULL,
00813         0x8eb44a8768581511ULL,
00814         0xdb0c2e0d64f98fa7ULL,
00815         0x47b5481dbefa4fa4ULL
00816 };
00817 
00818 /* Initial hash value H for SHA-512: */
00819 static const isc_uint64_t sha512_initial_hash_value[8] = {
00820         0x6a09e667f3bcc908ULL,
00821         0xbb67ae8584caa73bULL,
00822         0x3c6ef372fe94f82bULL,
00823         0xa54ff53a5f1d36f1ULL,
00824         0x510e527fade682d1ULL,
00825         0x9b05688c2b3e6c1fULL,
00826         0x1f83d9abfb41bd6bULL,
00827         0x5be0cd19137e2179ULL
00828 };
00829 #endif
00830 
00831 
00832 /*** SHA-224: *********************************************************/
00833 void
00834 isc_sha224_init(isc_sha224_t *context) {
00835         if (context == (isc_sha256_t *)0) {
00836                 return;
00837         }
00838         memmove(context->state, sha224_initial_hash_value,
00839                 ISC_SHA256_DIGESTLENGTH);
00840         memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);
00841         context->bitcount = 0;
00842 }
00843 
00844 void
00845 isc_sha224_invalidate(isc_sha224_t *context) {
00846         memset(context, 0, sizeof(isc_sha224_t));
00847 }
00848 
00849 void
00850 isc_sha224_update(isc_sha224_t *context, const isc_uint8_t* data, size_t len) {
00851         isc_sha256_update((isc_sha256_t *)context, data, len);
00852 }
00853 
00854 void
00855 isc_sha224_final(isc_uint8_t digest[], isc_sha224_t *context) {
00856         isc_uint8_t sha256_digest[ISC_SHA256_DIGESTLENGTH];
00857         isc_sha256_final(sha256_digest, (isc_sha256_t *)context);
00858         memmove(digest, sha256_digest, ISC_SHA224_DIGESTLENGTH);
00859         memset(sha256_digest, 0, ISC_SHA256_DIGESTLENGTH);
00860 }
00861 
00862 /*** SHA-256: *********************************************************/
00863 void
00864 isc_sha256_init(isc_sha256_t *context) {
00865         if (context == (isc_sha256_t *)0) {
00866                 return;
00867         }
00868         memmove(context->state, sha256_initial_hash_value,
00869                ISC_SHA256_DIGESTLENGTH);
00870         memset(context->buffer, 0, ISC_SHA256_BLOCK_LENGTH);
00871         context->bitcount = 0;
00872 }
00873 
00874 void
00875 isc_sha256_invalidate(isc_sha256_t *context) {
00876         memset(context, 0, sizeof(isc_sha256_t));
00877 }
00878 
00879 #ifdef ISC_SHA2_UNROLL_TRANSFORM
00880 
00881 /* Unrolled SHA-256 round macros: */
00882 
00883 #if BYTE_ORDER == LITTLE_ENDIAN
00884 
00885 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
00886         REVERSE32(*data++, W256[j]); \
00887         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
00888              K256[j] + W256[j]; \
00889         (d) += T1; \
00890         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
00891         j++
00892 
00893 
00894 #else /* BYTE_ORDER == LITTLE_ENDIAN */
00895 
00896 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h)       \
00897         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
00898              K256[j] + (W256[j] = *data++); \
00899         (d) += T1; \
00900         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
00901         j++
00902 
00903 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
00904 
00905 #define ROUND256(a,b,c,d,e,f,g,h)       \
00906         s0 = W256[(j+1)&0x0f]; \
00907         s0 = sigma0_256(s0); \
00908         s1 = W256[(j+14)&0x0f]; \
00909         s1 = sigma1_256(s1); \
00910         T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
00911              (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
00912         (d) += T1; \
00913         (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
00914         j++
00915 
00916 void isc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {
00917         isc_uint32_t    a, b, c, d, e, f, g, h, s0, s1;
00918         isc_uint32_t    T1, *W256;
00919         int             j;
00920 
00921         W256 = (isc_uint32_t*)context->buffer;
00922 
00923         /* Initialize registers with the prev. intermediate value */
00924         a = context->state[0];
00925         b = context->state[1];
00926         c = context->state[2];
00927         d = context->state[3];
00928         e = context->state[4];
00929         f = context->state[5];
00930         g = context->state[6];
00931         h = context->state[7];
00932 
00933         j = 0;
00934         do {
00935                 /* Rounds 0 to 15 (unrolled): */
00936                 ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
00937                 ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
00938                 ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
00939                 ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
00940                 ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
00941                 ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
00942                 ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
00943                 ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
00944         } while (j < 16);
00945 
00946         /* Now for the remaining rounds to 64: */
00947         do {
00948                 ROUND256(a,b,c,d,e,f,g,h);
00949                 ROUND256(h,a,b,c,d,e,f,g);
00950                 ROUND256(g,h,a,b,c,d,e,f);
00951                 ROUND256(f,g,h,a,b,c,d,e);
00952                 ROUND256(e,f,g,h,a,b,c,d);
00953                 ROUND256(d,e,f,g,h,a,b,c);
00954                 ROUND256(c,d,e,f,g,h,a,b);
00955                 ROUND256(b,c,d,e,f,g,h,a);
00956         } while (j < 64);
00957 
00958         /* Compute the current intermediate hash value */
00959         context->state[0] += a;
00960         context->state[1] += b;
00961         context->state[2] += c;
00962         context->state[3] += d;
00963         context->state[4] += e;
00964         context->state[5] += f;
00965         context->state[6] += g;
00966         context->state[7] += h;
00967 
00968         /* Clean up */
00969         a = b = c = d = e = f = g = h = T1 = 0;
00970         /* Avoid compiler warnings */
00971         POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
00972         POST(g); POST(h); POST(T1);
00973 }
00974 
00975 #else /* ISC_SHA2_UNROLL_TRANSFORM */
00976 
00977 void
00978 isc_sha256_transform(isc_sha256_t *context, const isc_uint32_t* data) {
00979         isc_uint32_t    a, b, c, d, e, f, g, h, s0, s1;
00980         isc_uint32_t    T1, T2, *W256;
00981         int             j;
00982 
00983         W256 = (isc_uint32_t*)context->buffer;
00984 
00985         /* Initialize registers with the prev. intermediate value */
00986         a = context->state[0];
00987         b = context->state[1];
00988         c = context->state[2];
00989         d = context->state[3];
00990         e = context->state[4];
00991         f = context->state[5];
00992         g = context->state[6];
00993         h = context->state[7];
00994 
00995         j = 0;
00996         do {
00997 #if BYTE_ORDER == LITTLE_ENDIAN
00998                 /* Copy data while converting to host byte order */
00999                 REVERSE32(*data++,W256[j]);
01000                 /* Apply the SHA-256 compression function to update a..h */
01001                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
01002 #else /* BYTE_ORDER == LITTLE_ENDIAN */
01003                 /* Apply the SHA-256 compression function to update a..h with copy */
01004                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
01005 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
01006                 T2 = Sigma0_256(a) + Maj(a, b, c);
01007                 h = g;
01008                 g = f;
01009                 f = e;
01010                 e = d + T1;
01011                 d = c;
01012                 c = b;
01013                 b = a;
01014                 a = T1 + T2;
01015 
01016                 j++;
01017         } while (j < 16);
01018 
01019         do {
01020                 /* Part of the message block expansion: */
01021                 s0 = W256[(j+1)&0x0f];
01022                 s0 = sigma0_256(s0);
01023                 s1 = W256[(j+14)&0x0f];
01024                 s1 = sigma1_256(s1);
01025 
01026                 /* Apply the SHA-256 compression function to update a..h */
01027                 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
01028                      (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
01029                 T2 = Sigma0_256(a) + Maj(a, b, c);
01030                 h = g;
01031                 g = f;
01032                 f = e;
01033                 e = d + T1;
01034                 d = c;
01035                 c = b;
01036                 b = a;
01037                 a = T1 + T2;
01038 
01039                 j++;
01040         } while (j < 64);
01041 
01042         /* Compute the current intermediate hash value */
01043         context->state[0] += a;
01044         context->state[1] += b;
01045         context->state[2] += c;
01046         context->state[3] += d;
01047         context->state[4] += e;
01048         context->state[5] += f;
01049         context->state[6] += g;
01050         context->state[7] += h;
01051 
01052         /* Clean up */
01053         a = b = c = d = e = f = g = h = T1 = T2 = 0;
01054         /* Avoid compiler warnings */
01055         POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
01056         POST(g); POST(h); POST(T1); POST(T2);
01057 }
01058 
01059 #endif /* ISC_SHA2_UNROLL_TRANSFORM */
01060 
01061 void
01062 isc_sha256_update(isc_sha256_t *context, const isc_uint8_t *data, size_t len) {
01063         unsigned int    freespace, usedspace;
01064 
01065         if (len == 0U) {
01066                 /* Calling with no data is valid - we do nothing */
01067                 return;
01068         }
01069 
01070         /* Sanity check: */
01071         REQUIRE(context != (isc_sha256_t *)0 && data != (isc_uint8_t*)0);
01072 
01073         usedspace = (unsigned int)((context->bitcount >> 3) %
01074                                    ISC_SHA256_BLOCK_LENGTH);
01075         if (usedspace > 0) {
01076                 /* Calculate how much free space is available in the buffer */
01077                 freespace = ISC_SHA256_BLOCK_LENGTH - usedspace;
01078 
01079                 if (len >= freespace) {
01080                         /* Fill the buffer completely and process it */
01081                         memmove(&context->buffer[usedspace], data, freespace);
01082                         context->bitcount += freespace << 3;
01083                         len -= freespace;
01084                         data += freespace;
01085                         isc_sha256_transform(context,
01086                                              (isc_uint32_t*)context->buffer);
01087                 } else {
01088                         /* The buffer is not yet full */
01089                         memmove(&context->buffer[usedspace], data, len);
01090                         context->bitcount += len << 3;
01091                         /* Clean up: */
01092                         usedspace = freespace = 0;
01093                         /* Avoid compiler warnings: */
01094                         POST(usedspace); POST(freespace);
01095                         return;
01096                 }
01097         }
01098         while (len >= ISC_SHA256_BLOCK_LENGTH) {
01099                 /* Process as many complete blocks as we can */
01100                 memmove(context->buffer, data, ISC_SHA256_BLOCK_LENGTH);
01101                 isc_sha256_transform(context, (isc_uint32_t*)context->buffer);
01102                 context->bitcount += ISC_SHA256_BLOCK_LENGTH << 3;
01103                 len -= ISC_SHA256_BLOCK_LENGTH;
01104                 data += ISC_SHA256_BLOCK_LENGTH;
01105         }
01106         if (len > 0U) {
01107                 /* There's left-overs, so save 'em */
01108                 memmove(context->buffer, data, len);
01109                 context->bitcount += len << 3;
01110         }
01111         /* Clean up: */
01112         usedspace = freespace = 0;
01113         /* Avoid compiler warnings: */
01114         POST(usedspace); POST(freespace);
01115 }
01116 
01117 void
01118 isc_sha256_final(isc_uint8_t digest[], isc_sha256_t *context) {
01119         isc_uint32_t    *d = (isc_uint32_t*)digest;
01120         unsigned int    usedspace;
01121 
01122         /* Sanity check: */
01123         REQUIRE(context != (isc_sha256_t *)0);
01124 
01125         /* If no digest buffer is passed, we don't bother doing this: */
01126         if (digest != (isc_uint8_t*)0) {
01127                 usedspace = (unsigned int)((context->bitcount >> 3) %
01128                                            ISC_SHA256_BLOCK_LENGTH);
01129 #if BYTE_ORDER == LITTLE_ENDIAN
01130                 /* Convert FROM host byte order */
01131                 REVERSE64(context->bitcount,context->bitcount);
01132 #endif
01133                 if (usedspace > 0) {
01134                         /* Begin padding with a 1 bit: */
01135                         context->buffer[usedspace++] = 0x80;
01136 
01137                         if (usedspace <= ISC_SHA256_SHORT_BLOCK_LENGTH) {
01138                                 /* Set-up for the last transform: */
01139                                 memset(&context->buffer[usedspace], 0,
01140                                        ISC_SHA256_SHORT_BLOCK_LENGTH - usedspace);
01141                         } else {
01142                                 if (usedspace < ISC_SHA256_BLOCK_LENGTH) {
01143                                         memset(&context->buffer[usedspace], 0,
01144                                                ISC_SHA256_BLOCK_LENGTH -
01145                                                usedspace);
01146                                 }
01147                                 /* Do second-to-last transform: */
01148                                 isc_sha256_transform(context,
01149                                                (isc_uint32_t*)context->buffer);
01150 
01151                                 /* And set-up for the last transform: */
01152                                 memset(context->buffer, 0,
01153                                        ISC_SHA256_SHORT_BLOCK_LENGTH);
01154                         }
01155                 } else {
01156                         /* Set-up for the last transform: */
01157                         memset(context->buffer, 0, ISC_SHA256_SHORT_BLOCK_LENGTH);
01158 
01159                         /* Begin padding with a 1 bit: */
01160                         *context->buffer = 0x80;
01161                 }
01162                 /* Set the bit count: */
01163                 *(isc_uint64_t*)&context->buffer[ISC_SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
01164 
01165                 /* Final transform: */
01166                 isc_sha256_transform(context, (isc_uint32_t*)context->buffer);
01167 
01168 #if BYTE_ORDER == LITTLE_ENDIAN
01169                 {
01170                         /* Convert TO host byte order */
01171                         int     j;
01172                         for (j = 0; j < 8; j++) {
01173                                 REVERSE32(context->state[j],context->state[j]);
01174                                 *d++ = context->state[j];
01175                         }
01176                 }
01177 #else
01178                 memmove(d, context->state, ISC_SHA256_DIGESTLENGTH);
01179 #endif
01180         }
01181 
01182         /* Clean up state data: */
01183         memset(context, 0, sizeof(*context));
01184         usedspace = 0;
01185         POST(usedspace);
01186 }
01187 
01188 /*** SHA-512: *********************************************************/
01189 void
01190 isc_sha512_init(isc_sha512_t *context) {
01191         if (context == (isc_sha512_t *)0) {
01192                 return;
01193         }
01194         memmove(context->state, sha512_initial_hash_value,
01195                 ISC_SHA512_DIGESTLENGTH);
01196         memset(context->buffer, 0, ISC_SHA512_BLOCK_LENGTH);
01197         context->bitcount[0] = context->bitcount[1] =  0;
01198 }
01199 
01200 void
01201 isc_sha512_invalidate(isc_sha512_t *context) {
01202         memset(context, 0, sizeof(isc_sha512_t));
01203 }
01204 
01205 #ifdef ISC_SHA2_UNROLL_TRANSFORM
01206 
01207 /* Unrolled SHA-512 round macros: */
01208 #if BYTE_ORDER == LITTLE_ENDIAN
01209 
01210 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
01211         REVERSE64(*data++, W512[j]); \
01212         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
01213              K512[j] + W512[j]; \
01214         (d) += T1, \
01215         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
01216         j++
01217 
01218 
01219 #else /* BYTE_ORDER == LITTLE_ENDIAN */
01220 
01221 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h)       \
01222         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
01223              K512[j] + (W512[j] = *data++); \
01224         (d) += T1; \
01225         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
01226         j++
01227 
01228 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
01229 
01230 #define ROUND512(a,b,c,d,e,f,g,h)       \
01231         s0 = W512[(j+1)&0x0f]; \
01232         s0 = sigma0_512(s0); \
01233         s1 = W512[(j+14)&0x0f]; \
01234         s1 = sigma1_512(s1); \
01235         T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
01236              (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
01237         (d) += T1; \
01238         (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
01239         j++
01240 
01241 void isc_sha512_transform(isc_sha512_t *context, const isc_uint64_t* data) {
01242         isc_uint64_t    a, b, c, d, e, f, g, h, s0, s1;
01243         isc_uint64_t    T1, *W512 = (isc_uint64_t*)context->buffer;
01244         int             j;
01245 
01246         /* Initialize registers with the prev. intermediate value */
01247         a = context->state[0];
01248         b = context->state[1];
01249         c = context->state[2];
01250         d = context->state[3];
01251         e = context->state[4];
01252         f = context->state[5];
01253         g = context->state[6];
01254         h = context->state[7];
01255 
01256         j = 0;
01257         do {
01258                 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
01259                 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
01260                 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
01261                 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
01262                 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
01263                 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
01264                 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
01265                 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
01266         } while (j < 16);
01267 
01268         /* Now for the remaining rounds up to 79: */
01269         do {
01270                 ROUND512(a,b,c,d,e,f,g,h);
01271                 ROUND512(h,a,b,c,d,e,f,g);
01272                 ROUND512(g,h,a,b,c,d,e,f);
01273                 ROUND512(f,g,h,a,b,c,d,e);
01274                 ROUND512(e,f,g,h,a,b,c,d);
01275                 ROUND512(d,e,f,g,h,a,b,c);
01276                 ROUND512(c,d,e,f,g,h,a,b);
01277                 ROUND512(b,c,d,e,f,g,h,a);
01278         } while (j < 80);
01279 
01280         /* Compute the current intermediate hash value */
01281         context->state[0] += a;
01282         context->state[1] += b;
01283         context->state[2] += c;
01284         context->state[3] += d;
01285         context->state[4] += e;
01286         context->state[5] += f;
01287         context->state[6] += g;
01288         context->state[7] += h;
01289 
01290         /* Clean up */
01291         a = b = c = d = e = f = g = h = T1 = 0;
01292         /* Avoid compiler warnings */
01293         POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
01294         POST(g); POST(h); POST(T1);
01295 }
01296 
01297 #else /* ISC_SHA2_UNROLL_TRANSFORM */
01298 
01299 void
01300 isc_sha512_transform(isc_sha512_t *context, const isc_uint64_t* data) {
01301         isc_uint64_t    a, b, c, d, e, f, g, h, s0, s1;
01302         isc_uint64_t    T1, T2, *W512 = (isc_uint64_t*)context->buffer;
01303         int             j;
01304 
01305         /* Initialize registers with the prev. intermediate value */
01306         a = context->state[0];
01307         b = context->state[1];
01308         c = context->state[2];
01309         d = context->state[3];
01310         e = context->state[4];
01311         f = context->state[5];
01312         g = context->state[6];
01313         h = context->state[7];
01314 
01315         j = 0;
01316         do {
01317 #if BYTE_ORDER == LITTLE_ENDIAN
01318                 /* Convert TO host byte order */
01319                 REVERSE64(*data++, W512[j]);
01320                 /* Apply the SHA-512 compression function to update a..h */
01321                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
01322 #else /* BYTE_ORDER == LITTLE_ENDIAN */
01323                 /* Apply the SHA-512 compression function to update a..h with copy */
01324                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
01325 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
01326                 T2 = Sigma0_512(a) + Maj(a, b, c);
01327                 h = g;
01328                 g = f;
01329                 f = e;
01330                 e = d + T1;
01331                 d = c;
01332                 c = b;
01333                 b = a;
01334                 a = T1 + T2;
01335 
01336                 j++;
01337         } while (j < 16);
01338 
01339         do {
01340                 /* Part of the message block expansion: */
01341                 s0 = W512[(j+1)&0x0f];
01342                 s0 = sigma0_512(s0);
01343                 s1 = W512[(j+14)&0x0f];
01344                 s1 =  sigma1_512(s1);
01345 
01346                 /* Apply the SHA-512 compression function to update a..h */
01347                 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
01348                      (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
01349                 T2 = Sigma0_512(a) + Maj(a, b, c);
01350                 h = g;
01351                 g = f;
01352                 f = e;
01353                 e = d + T1;
01354                 d = c;
01355                 c = b;
01356                 b = a;
01357                 a = T1 + T2;
01358 
01359                 j++;
01360         } while (j < 80);
01361 
01362         /* Compute the current intermediate hash value */
01363         context->state[0] += a;
01364         context->state[1] += b;
01365         context->state[2] += c;
01366         context->state[3] += d;
01367         context->state[4] += e;
01368         context->state[5] += f;
01369         context->state[6] += g;
01370         context->state[7] += h;
01371 
01372         /* Clean up */
01373         a = b = c = d = e = f = g = h = T1 = T2 = 0;
01374         /* Avoid compiler warnings */
01375         POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
01376         POST(g); POST(h); POST(T1); POST(T2);
01377 }
01378 
01379 #endif /* ISC_SHA2_UNROLL_TRANSFORM */
01380 
01381 void isc_sha512_update(isc_sha512_t *context, const isc_uint8_t *data, size_t len) {
01382         unsigned int    freespace, usedspace;
01383 
01384         if (len == 0U) {
01385                 /* Calling with no data is valid - we do nothing */
01386                 return;
01387         }
01388 
01389         /* Sanity check: */
01390         REQUIRE(context != (isc_sha512_t *)0 && data != (isc_uint8_t*)0);
01391 
01392         usedspace = (unsigned int)((context->bitcount[0] >> 3) %
01393                                    ISC_SHA512_BLOCK_LENGTH);
01394         if (usedspace > 0) {
01395                 /* Calculate how much free space is available in the buffer */
01396                 freespace = ISC_SHA512_BLOCK_LENGTH - usedspace;
01397 
01398                 if (len >= freespace) {
01399                         /* Fill the buffer completely and process it */
01400                         memmove(&context->buffer[usedspace], data, freespace);
01401                         ADDINC128(context->bitcount, freespace << 3);
01402                         len -= freespace;
01403                         data += freespace;
01404                         isc_sha512_transform(context,
01405                                              (isc_uint64_t*)context->buffer);
01406                 } else {
01407                         /* The buffer is not yet full */
01408                         memmove(&context->buffer[usedspace], data, len);
01409                         ADDINC128(context->bitcount, len << 3);
01410                         /* Clean up: */
01411                         usedspace = freespace = 0;
01412                         /* Avoid compiler warnings: */
01413                         POST(usedspace); POST(freespace);
01414                         return;
01415                 }
01416         }
01417         while (len >= ISC_SHA512_BLOCK_LENGTH) {
01418                 /* Process as many complete blocks as we can */
01419                 memmove(context->buffer, data, ISC_SHA512_BLOCK_LENGTH);
01420                 isc_sha512_transform(context, (isc_uint64_t*)context->buffer);
01421                 ADDINC128(context->bitcount, ISC_SHA512_BLOCK_LENGTH << 3);
01422                 len -= ISC_SHA512_BLOCK_LENGTH;
01423                 data += ISC_SHA512_BLOCK_LENGTH;
01424         }
01425         if (len > 0U) {
01426                 /* There's left-overs, so save 'em */
01427                 memmove(context->buffer, data, len);
01428                 ADDINC128(context->bitcount, len << 3);
01429         }
01430         /* Clean up: */
01431         usedspace = freespace = 0;
01432         /* Avoid compiler warnings: */
01433         POST(usedspace); POST(freespace);
01434 }
01435 
01436 void isc_sha512_last(isc_sha512_t *context) {
01437         unsigned int    usedspace;
01438 
01439         usedspace = (unsigned int)((context->bitcount[0] >> 3) %
01440                                     ISC_SHA512_BLOCK_LENGTH);
01441 #if BYTE_ORDER == LITTLE_ENDIAN
01442         /* Convert FROM host byte order */
01443         REVERSE64(context->bitcount[0],context->bitcount[0]);
01444         REVERSE64(context->bitcount[1],context->bitcount[1]);
01445 #endif
01446         if (usedspace > 0) {
01447                 /* Begin padding with a 1 bit: */
01448                 context->buffer[usedspace++] = 0x80;
01449 
01450                 if (usedspace <= ISC_SHA512_SHORT_BLOCK_LENGTH) {
01451                         /* Set-up for the last transform: */
01452                         memset(&context->buffer[usedspace], 0,
01453                                ISC_SHA512_SHORT_BLOCK_LENGTH - usedspace);
01454                 } else {
01455                         if (usedspace < ISC_SHA512_BLOCK_LENGTH) {
01456                                 memset(&context->buffer[usedspace], 0,
01457                                        ISC_SHA512_BLOCK_LENGTH - usedspace);
01458                         }
01459                         /* Do second-to-last transform: */
01460                         isc_sha512_transform(context,
01461                                             (isc_uint64_t*)context->buffer);
01462 
01463                         /* And set-up for the last transform: */
01464                         memset(context->buffer, 0, ISC_SHA512_BLOCK_LENGTH - 2);
01465                 }
01466         } else {
01467                 /* Prepare for final transform: */
01468                 memset(context->buffer, 0, ISC_SHA512_SHORT_BLOCK_LENGTH);
01469 
01470                 /* Begin padding with a 1 bit: */
01471                 *context->buffer = 0x80;
01472         }
01473         /* Store the length of input data (in bits): */
01474         *(isc_uint64_t*)&context->buffer[ISC_SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
01475         *(isc_uint64_t*)&context->buffer[ISC_SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
01476 
01477         /* Final transform: */
01478         isc_sha512_transform(context, (isc_uint64_t*)context->buffer);
01479 }
01480 
01481 void isc_sha512_final(isc_uint8_t digest[], isc_sha512_t *context) {
01482         isc_uint64_t    *d = (isc_uint64_t*)digest;
01483 
01484         /* Sanity check: */
01485         REQUIRE(context != (isc_sha512_t *)0);
01486 
01487         /* If no digest buffer is passed, we don't bother doing this: */
01488         if (digest != (isc_uint8_t*)0) {
01489                 isc_sha512_last(context);
01490 
01491                 /* Save the hash data for output: */
01492 #if BYTE_ORDER == LITTLE_ENDIAN
01493                 {
01494                         /* Convert TO host byte order */
01495                         int     j;
01496                         for (j = 0; j < 8; j++) {
01497                                 REVERSE64(context->state[j],context->state[j]);
01498                                 *d++ = context->state[j];
01499                         }
01500                 }
01501 #else
01502                 memmove(d, context->state, ISC_SHA512_DIGESTLENGTH);
01503 #endif
01504         }
01505 
01506         /* Zero out state data */
01507         memset(context, 0, sizeof(*context));
01508 }
01509 
01510 
01511 /*** SHA-384: *********************************************************/
01512 void
01513 isc_sha384_init(isc_sha384_t *context) {
01514         if (context == (isc_sha384_t *)0) {
01515                 return;
01516         }
01517         memmove(context->state, sha384_initial_hash_value,
01518                 ISC_SHA512_DIGESTLENGTH);
01519         memset(context->buffer, 0, ISC_SHA384_BLOCK_LENGTH);
01520         context->bitcount[0] = context->bitcount[1] = 0;
01521 }
01522 
01523 void
01524 isc_sha384_invalidate(isc_sha384_t *context) {
01525         memset(context, 0, sizeof(isc_sha384_t));
01526 }
01527 
01528 void
01529 isc_sha384_update(isc_sha384_t *context, const isc_uint8_t* data, size_t len) {
01530         isc_sha512_update((isc_sha512_t *)context, data, len);
01531 }
01532 
01533 void
01534 isc_sha384_final(isc_uint8_t digest[], isc_sha384_t *context) {
01535         isc_uint64_t    *d = (isc_uint64_t*)digest;
01536 
01537         /* Sanity check: */
01538         REQUIRE(context != (isc_sha384_t *)0);
01539 
01540         /* If no digest buffer is passed, we don't bother doing this: */
01541         if (digest != (isc_uint8_t*)0) {
01542                 isc_sha512_last((isc_sha512_t *)context);
01543 
01544                 /* Save the hash data for output: */
01545 #if BYTE_ORDER == LITTLE_ENDIAN
01546                 {
01547                         /* Convert TO host byte order */
01548                         int     j;
01549                         for (j = 0; j < 6; j++) {
01550                                 REVERSE64(context->state[j],context->state[j]);
01551                                 *d++ = context->state[j];
01552                         }
01553                 }
01554 #else
01555                 memmove(d, context->state, ISC_SHA384_DIGESTLENGTH);
01556 #endif
01557         }
01558 
01559         /* Zero out state data */
01560         memset(context, 0, sizeof(*context));
01561 }
01562 #endif /* !ISC_PLATFORM_OPENSSLHASH */
01563 
01564 /*
01565  * Constant used by SHA256/384/512_End() functions for converting the
01566  * digest to a readable hexadecimal character string:
01567  */
01568 static const char *sha2_hex_digits = "0123456789abcdef";
01569 
01570 char *
01571 isc_sha224_end(isc_sha224_t *context, char buffer[]) {
01572         isc_uint8_t     digest[ISC_SHA224_DIGESTLENGTH], *d = digest;
01573         unsigned int    i;
01574 
01575         /* Sanity check: */
01576         REQUIRE(context != (isc_sha224_t *)0);
01577 
01578         if (buffer != (char*)0) {
01579                 isc_sha224_final(digest, context);
01580 
01581                 for (i = 0; i < ISC_SHA224_DIGESTLENGTH; i++) {
01582                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
01583                         *buffer++ = sha2_hex_digits[*d & 0x0f];
01584                         d++;
01585                 }
01586                 *buffer = (char)0;
01587         } else {
01588 #ifdef ISC_PLATFORM_OPENSSLHASH
01589                 EVP_MD_CTX_cleanup(context);
01590 #elif PKCS11CRYPTO
01591                 pk11_return_session(context);
01592 #else
01593                 memset(context, 0, sizeof(*context));
01594 #endif
01595         }
01596         memset(digest, 0, ISC_SHA224_DIGESTLENGTH);
01597         return buffer;
01598 }
01599 
01600 char *
01601 isc_sha224_data(const isc_uint8_t *data, size_t len,
01602                 char digest[ISC_SHA224_DIGESTSTRINGLENGTH])
01603 {
01604         isc_sha224_t context;
01605 
01606         isc_sha224_init(&context);
01607         isc_sha224_update(&context, data, len);
01608         return (isc_sha224_end(&context, digest));
01609 }
01610 
01611 char *
01612 isc_sha256_end(isc_sha256_t *context, char buffer[]) {
01613         isc_uint8_t     digest[ISC_SHA256_DIGESTLENGTH], *d = digest;
01614         unsigned int    i;
01615 
01616         /* Sanity check: */
01617         REQUIRE(context != (isc_sha256_t *)0);
01618 
01619         if (buffer != (char*)0) {
01620                 isc_sha256_final(digest, context);
01621 
01622                 for (i = 0; i < ISC_SHA256_DIGESTLENGTH; i++) {
01623                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
01624                         *buffer++ = sha2_hex_digits[*d & 0x0f];
01625                         d++;
01626                 }
01627                 *buffer = (char)0;
01628         } else {
01629 #ifdef ISC_PLATFORM_OPENSSLHASH
01630                 EVP_MD_CTX_cleanup(context);
01631 #elif PKCS11CRYPTO
01632                 pk11_return_session(context);
01633 #else
01634                 memset(context, 0, sizeof(*context));
01635 #endif
01636         }
01637         memset(digest, 0, ISC_SHA256_DIGESTLENGTH);
01638         return buffer;
01639 }
01640 
01641 char *
01642 isc_sha256_data(const isc_uint8_t* data, size_t len,
01643                 char digest[ISC_SHA256_DIGESTSTRINGLENGTH])
01644 {
01645         isc_sha256_t context;
01646 
01647         isc_sha256_init(&context);
01648         isc_sha256_update(&context, data, len);
01649         return (isc_sha256_end(&context, digest));
01650 }
01651 
01652 char *
01653 isc_sha512_end(isc_sha512_t *context, char buffer[]) {
01654         isc_uint8_t     digest[ISC_SHA512_DIGESTLENGTH], *d = digest;
01655         unsigned int    i;
01656 
01657         /* Sanity check: */
01658         REQUIRE(context != (isc_sha512_t *)0);
01659 
01660         if (buffer != (char*)0) {
01661                 isc_sha512_final(digest, context);
01662 
01663                 for (i = 0; i < ISC_SHA512_DIGESTLENGTH; i++) {
01664                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
01665                         *buffer++ = sha2_hex_digits[*d & 0x0f];
01666                         d++;
01667                 }
01668                 *buffer = (char)0;
01669         } else {
01670 #ifdef ISC_PLATFORM_OPENSSLHASH
01671                 EVP_MD_CTX_cleanup(context);
01672 #elif PKCS11CRYPTO
01673                 pk11_return_session(context);
01674 #else
01675                 memset(context, 0, sizeof(*context));
01676 #endif
01677         }
01678         memset(digest, 0, ISC_SHA512_DIGESTLENGTH);
01679         return buffer;
01680 }
01681 
01682 char *
01683 isc_sha512_data(const isc_uint8_t *data, size_t len,
01684                 char digest[ISC_SHA512_DIGESTSTRINGLENGTH])
01685 {
01686         isc_sha512_t    context;
01687 
01688         isc_sha512_init(&context);
01689         isc_sha512_update(&context, data, len);
01690         return (isc_sha512_end(&context, digest));
01691 }
01692 
01693 char *
01694 isc_sha384_end(isc_sha384_t *context, char buffer[]) {
01695         isc_uint8_t     digest[ISC_SHA384_DIGESTLENGTH], *d = digest;
01696         unsigned int    i;
01697 
01698         /* Sanity check: */
01699         REQUIRE(context != (isc_sha384_t *)0);
01700 
01701         if (buffer != (char*)0) {
01702                 isc_sha384_final(digest, context);
01703 
01704                 for (i = 0; i < ISC_SHA384_DIGESTLENGTH; i++) {
01705                         *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
01706                         *buffer++ = sha2_hex_digits[*d & 0x0f];
01707                         d++;
01708                 }
01709                 *buffer = (char)0;
01710         } else {
01711 #ifdef ISC_PLATFORM_OPENSSLHASH
01712                 EVP_MD_CTX_cleanup(context);
01713 #elif PKCS11CRYPTO
01714                 pk11_return_session(context);
01715 #else
01716                 memset(context, 0, sizeof(*context));
01717 #endif
01718         }
01719         memset(digest, 0, ISC_SHA384_DIGESTLENGTH);
01720         return buffer;
01721 }
01722 
01723 char *
01724 isc_sha384_data(const isc_uint8_t *data, size_t len,
01725                 char digest[ISC_SHA384_DIGESTSTRINGLENGTH])
01726 {
01727         isc_sha384_t context;
01728 
01729         isc_sha384_init(&context);
01730         isc_sha384_update(&context, data, len);
01731         return (isc_sha384_end(&context, digest));
01732 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha