00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
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
00090 return;
00091 }
00092
00093
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
00103 REQUIRE(context != (isc_sha224_t *)0);
00104
00105
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
00130 return;
00131 }
00132
00133
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
00143 REQUIRE(context != (isc_sha256_t *)0);
00144
00145
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
00169 return;
00170 }
00171
00172
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
00181 REQUIRE(context != (isc_sha512_t *)0);
00182
00183
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
00208 return;
00209 }
00210
00211
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
00221 REQUIRE(context != (isc_sha384_t *)0);
00222
00223
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
00265 return;
00266 }
00267
00268
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
00282 REQUIRE(context != (isc_sha224_t *)0);
00283
00284
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
00331 return;
00332 }
00333
00334
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
00348 REQUIRE(context != (isc_sha256_t *)0);
00349
00350
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
00397 return;
00398 }
00399
00400
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
00414 REQUIRE(context != (isc_sha512_t *)0);
00415
00416
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
00463 return;
00464 }
00465
00466
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
00480 REQUIRE(context != (isc_sha384_t *)0);
00481
00482
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
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
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
00560
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
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
00593
00594
00595
00596
00597
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
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 #define R(b,x) ((x) >> (b))
00617
00618 #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
00619
00620 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
00621
00622
00623 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
00624 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
00625
00626
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
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
00639
00640
00641
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
00649
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
00969 a = b = c = d = e = f = g = h = T1 = 0;
00970
00971 POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
00972 POST(g); POST(h); POST(T1);
00973 }
00974
00975 #else
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
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
00999 REVERSE32(*data++,W256[j]);
01000
01001 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
01002 #else
01003
01004 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
01005 #endif
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
01021 s0 = W256[(j+1)&0x0f];
01022 s0 = sigma0_256(s0);
01023 s1 = W256[(j+14)&0x0f];
01024 s1 = sigma1_256(s1);
01025
01026
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
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
01053 a = b = c = d = e = f = g = h = T1 = T2 = 0;
01054
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
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
01067 return;
01068 }
01069
01070
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
01077 freespace = ISC_SHA256_BLOCK_LENGTH - usedspace;
01078
01079 if (len >= freespace) {
01080
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
01089 memmove(&context->buffer[usedspace], data, len);
01090 context->bitcount += len << 3;
01091
01092 usedspace = freespace = 0;
01093
01094 POST(usedspace); POST(freespace);
01095 return;
01096 }
01097 }
01098 while (len >= ISC_SHA256_BLOCK_LENGTH) {
01099
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
01108 memmove(context->buffer, data, len);
01109 context->bitcount += len << 3;
01110 }
01111
01112 usedspace = freespace = 0;
01113
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
01123 REQUIRE(context != (isc_sha256_t *)0);
01124
01125
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
01131 REVERSE64(context->bitcount,context->bitcount);
01132 #endif
01133 if (usedspace > 0) {
01134
01135 context->buffer[usedspace++] = 0x80;
01136
01137 if (usedspace <= ISC_SHA256_SHORT_BLOCK_LENGTH) {
01138
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
01148 isc_sha256_transform(context,
01149 (isc_uint32_t*)context->buffer);
01150
01151
01152 memset(context->buffer, 0,
01153 ISC_SHA256_SHORT_BLOCK_LENGTH);
01154 }
01155 } else {
01156
01157 memset(context->buffer, 0, ISC_SHA256_SHORT_BLOCK_LENGTH);
01158
01159
01160 *context->buffer = 0x80;
01161 }
01162
01163 *(isc_uint64_t*)&context->buffer[ISC_SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
01164
01165
01166 isc_sha256_transform(context, (isc_uint32_t*)context->buffer);
01167
01168 #if BYTE_ORDER == LITTLE_ENDIAN
01169 {
01170
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
01183 memset(context, 0, sizeof(*context));
01184 usedspace = 0;
01185 POST(usedspace);
01186 }
01187
01188
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
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
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
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
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
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
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
01291 a = b = c = d = e = f = g = h = T1 = 0;
01292
01293 POST(a); POST(b); POST(c); POST(d); POST(e); POST(f);
01294 POST(g); POST(h); POST(T1);
01295 }
01296
01297 #else
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
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
01319 REVERSE64(*data++, W512[j]);
01320
01321 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
01322 #else
01323
01324 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
01325 #endif
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
01341 s0 = W512[(j+1)&0x0f];
01342 s0 = sigma0_512(s0);
01343 s1 = W512[(j+14)&0x0f];
01344 s1 = sigma1_512(s1);
01345
01346
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
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
01373 a = b = c = d = e = f = g = h = T1 = T2 = 0;
01374
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
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
01386 return;
01387 }
01388
01389
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
01396 freespace = ISC_SHA512_BLOCK_LENGTH - usedspace;
01397
01398 if (len >= freespace) {
01399
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
01408 memmove(&context->buffer[usedspace], data, len);
01409 ADDINC128(context->bitcount, len << 3);
01410
01411 usedspace = freespace = 0;
01412
01413 POST(usedspace); POST(freespace);
01414 return;
01415 }
01416 }
01417 while (len >= ISC_SHA512_BLOCK_LENGTH) {
01418
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
01427 memmove(context->buffer, data, len);
01428 ADDINC128(context->bitcount, len << 3);
01429 }
01430
01431 usedspace = freespace = 0;
01432
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
01443 REVERSE64(context->bitcount[0],context->bitcount[0]);
01444 REVERSE64(context->bitcount[1],context->bitcount[1]);
01445 #endif
01446 if (usedspace > 0) {
01447
01448 context->buffer[usedspace++] = 0x80;
01449
01450 if (usedspace <= ISC_SHA512_SHORT_BLOCK_LENGTH) {
01451
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
01460 isc_sha512_transform(context,
01461 (isc_uint64_t*)context->buffer);
01462
01463
01464 memset(context->buffer, 0, ISC_SHA512_BLOCK_LENGTH - 2);
01465 }
01466 } else {
01467
01468 memset(context->buffer, 0, ISC_SHA512_SHORT_BLOCK_LENGTH);
01469
01470
01471 *context->buffer = 0x80;
01472 }
01473
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
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
01485 REQUIRE(context != (isc_sha512_t *)0);
01486
01487
01488 if (digest != (isc_uint8_t*)0) {
01489 isc_sha512_last(context);
01490
01491
01492 #if BYTE_ORDER == LITTLE_ENDIAN
01493 {
01494
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
01507 memset(context, 0, sizeof(*context));
01508 }
01509
01510
01511
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
01538 REQUIRE(context != (isc_sha384_t *)0);
01539
01540
01541 if (digest != (isc_uint8_t*)0) {
01542 isc_sha512_last((isc_sha512_t *)context);
01543
01544
01545 #if BYTE_ORDER == LITTLE_ENDIAN
01546 {
01547
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
01560 memset(context, 0, sizeof(*context));
01561 }
01562 #endif
01563
01564
01565
01566
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
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
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
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
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 }