| OLD | NEW |
| 1 /* | 1 /* |
| 2 * sha512.c - implementation of SHA224, SHA256, SHA384 and SHA512 | 2 * sha512.c - implementation of SHA224, SHA256, SHA384 and SHA512 |
| 3 * | 3 * |
| 4 * This Source Code Form is subject to the terms of the Mozilla Public | 4 * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 * License, v. 2.0. If a copy of the MPL was not distributed with this | 5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 7 /* $Id: sha512.c,v 1.21 2012/07/27 20:00:39 wtc%google.com Exp $ */ | 7 /* $Id: sha512.c,v 1.23 2013/02/06 00:41:13 wtc%google.com Exp $ */ |
| 8 | 8 |
| 9 #ifdef FREEBL_NO_DEPEND | 9 #ifdef FREEBL_NO_DEPEND |
| 10 #include "stubs.h" | 10 #include "stubs.h" |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include "prcpucfg.h" | 13 #include "prcpucfg.h" |
| 14 #if defined(NSS_X86) || defined(SHA_NO_LONG_LONG) | 14 #if defined(NSS_X86) || defined(SHA_NO_LONG_LONG) |
| 15 #define NOUNROLL512 1 | 15 #define NOUNROLL512 1 |
| 16 #undef HAVE_LONG_LONG | 16 #undef HAVE_LONG_LONG |
| 17 #endif | 17 #endif |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 BYTESWAP4(H[5]); | 455 BYTESWAP4(H[5]); |
| 456 BYTESWAP4(H[6]); | 456 BYTESWAP4(H[6]); |
| 457 BYTESWAP4(H[7]); | 457 BYTESWAP4(H[7]); |
| 458 #endif | 458 #endif |
| 459 padLen = PR_MIN(SHA256_LENGTH, maxDigestLen); | 459 padLen = PR_MIN(SHA256_LENGTH, maxDigestLen); |
| 460 memcpy(digest, H, padLen); | 460 memcpy(digest, H, padLen); |
| 461 if (digestLen) | 461 if (digestLen) |
| 462 *digestLen = padLen; | 462 *digestLen = padLen; |
| 463 } | 463 } |
| 464 | 464 |
| 465 void |
| 466 SHA256_EndRaw(SHA256Context *ctx, unsigned char *digest, |
| 467 unsigned int *digestLen, unsigned int maxDigestLen) |
| 468 { |
| 469 PRUint32 h[8]; |
| 470 unsigned int len; |
| 471 #ifdef SWAP4MASK |
| 472 PRUint32 t1; |
| 473 #endif |
| 474 |
| 475 memcpy(h, ctx->h, sizeof(h)); |
| 476 |
| 477 #if defined(IS_LITTLE_ENDIAN) |
| 478 BYTESWAP4(h[0]); |
| 479 BYTESWAP4(h[1]); |
| 480 BYTESWAP4(h[2]); |
| 481 BYTESWAP4(h[3]); |
| 482 BYTESWAP4(h[4]); |
| 483 BYTESWAP4(h[5]); |
| 484 BYTESWAP4(h[6]); |
| 485 BYTESWAP4(h[7]); |
| 486 #endif |
| 487 |
| 488 len = PR_MIN(SHA256_LENGTH, maxDigestLen); |
| 489 memcpy(digest, h, len); |
| 490 if (digestLen) |
| 491 *digestLen = len; |
| 492 } |
| 493 |
| 465 SECStatus | 494 SECStatus |
| 466 SHA256_HashBuf(unsigned char *dest, const unsigned char *src, | 495 SHA256_HashBuf(unsigned char *dest, const unsigned char *src, |
| 467 uint32 src_length) | 496 uint32 src_length) |
| 468 { | 497 { |
| 469 SHA256Context ctx; | 498 SHA256Context ctx; |
| 470 unsigned int outLen; | 499 unsigned int outLen; |
| 471 | 500 |
| 472 SHA256_Begin(&ctx); | 501 SHA256_Begin(&ctx); |
| 473 SHA256_Update(&ctx, src, src_length); | 502 SHA256_Update(&ctx, src, src_length); |
| 474 SHA256_End(&ctx, dest, &outLen, SHA256_LENGTH); | 503 SHA256_End(&ctx, dest, &outLen, SHA256_LENGTH); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 } | 578 } |
| 550 | 579 |
| 551 void | 580 void |
| 552 SHA224_End(SHA256Context *ctx, unsigned char *digest, | 581 SHA224_End(SHA256Context *ctx, unsigned char *digest, |
| 553 unsigned int *digestLen, unsigned int maxDigestLen) | 582 unsigned int *digestLen, unsigned int maxDigestLen) |
| 554 { | 583 { |
| 555 unsigned int maxLen = SHA_MIN(maxDigestLen, SHA224_LENGTH); | 584 unsigned int maxLen = SHA_MIN(maxDigestLen, SHA224_LENGTH); |
| 556 SHA256_End(ctx, digest, digestLen, maxLen); | 585 SHA256_End(ctx, digest, digestLen, maxLen); |
| 557 } | 586 } |
| 558 | 587 |
| 588 void |
| 589 SHA224_EndRaw(SHA256Context *ctx, unsigned char *digest, |
| 590 unsigned int *digestLen, unsigned int maxDigestLen) |
| 591 { |
| 592 unsigned int maxLen = SHA_MIN(maxDigestLen, SHA224_LENGTH); |
| 593 SHA256_EndRaw(ctx, digest, digestLen, maxLen); |
| 594 } |
| 595 |
| 559 SECStatus | 596 SECStatus |
| 560 SHA224_HashBuf(unsigned char *dest, const unsigned char *src, | 597 SHA224_HashBuf(unsigned char *dest, const unsigned char *src, |
| 561 uint32 src_length) | 598 uint32 src_length) |
| 562 { | 599 { |
| 563 SHA256Context ctx; | 600 SHA256Context ctx; |
| 564 unsigned int outLen; | 601 unsigned int outLen; |
| 565 | 602 |
| 566 SHA224_Begin(&ctx); | 603 SHA224_Begin(&ctx); |
| 567 SHA256_Update(&ctx, src, src_length); | 604 SHA256_Update(&ctx, src, src_length); |
| 568 SHA256_End(&ctx, dest, &outLen, SHA224_LENGTH); | 605 SHA256_End(&ctx, dest, &outLen, SHA224_LENGTH); |
| (...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1221 BYTESWAP8(H[5]); | 1258 BYTESWAP8(H[5]); |
| 1222 BYTESWAP8(H[6]); | 1259 BYTESWAP8(H[6]); |
| 1223 BYTESWAP8(H[7]); | 1260 BYTESWAP8(H[7]); |
| 1224 #endif | 1261 #endif |
| 1225 padLen = PR_MIN(SHA512_LENGTH, maxDigestLen); | 1262 padLen = PR_MIN(SHA512_LENGTH, maxDigestLen); |
| 1226 memcpy(digest, H, padLen); | 1263 memcpy(digest, H, padLen); |
| 1227 if (digestLen) | 1264 if (digestLen) |
| 1228 *digestLen = padLen; | 1265 *digestLen = padLen; |
| 1229 } | 1266 } |
| 1230 | 1267 |
| 1268 void |
| 1269 SHA512_EndRaw(SHA512Context *ctx, unsigned char *digest, |
| 1270 unsigned int *digestLen, unsigned int maxDigestLen) |
| 1271 { |
| 1272 #if defined(HAVE_LONG_LONG) |
| 1273 PRUint64 t1; |
| 1274 #else |
| 1275 PRUint32 t1; |
| 1276 #endif |
| 1277 PRUint64 h[8]; |
| 1278 unsigned int len; |
| 1279 |
| 1280 memcpy(h, ctx->h, sizeof(h)); |
| 1281 |
| 1282 #if defined(IS_LITTLE_ENDIAN) |
| 1283 BYTESWAP8(h[0]); |
| 1284 BYTESWAP8(h[1]); |
| 1285 BYTESWAP8(h[2]); |
| 1286 BYTESWAP8(h[3]); |
| 1287 BYTESWAP8(h[4]); |
| 1288 BYTESWAP8(h[5]); |
| 1289 BYTESWAP8(h[6]); |
| 1290 BYTESWAP8(h[7]); |
| 1291 #endif |
| 1292 len = PR_MIN(SHA512_LENGTH, maxDigestLen); |
| 1293 memcpy(digest, h, len); |
| 1294 if (digestLen) |
| 1295 *digestLen = len; |
| 1296 } |
| 1297 |
| 1231 SECStatus | 1298 SECStatus |
| 1232 SHA512_HashBuf(unsigned char *dest, const unsigned char *src, | 1299 SHA512_HashBuf(unsigned char *dest, const unsigned char *src, |
| 1233 uint32 src_length) | 1300 uint32 src_length) |
| 1234 { | 1301 { |
| 1235 SHA512Context ctx; | 1302 SHA512Context ctx; |
| 1236 unsigned int outLen; | 1303 unsigned int outLen; |
| 1237 | 1304 |
| 1238 SHA512_Begin(&ctx); | 1305 SHA512_Begin(&ctx); |
| 1239 SHA512_Update(&ctx, src, src_length); | 1306 SHA512_Update(&ctx, src, src_length); |
| 1240 SHA512_End(&ctx, dest, &outLen, SHA512_LENGTH); | 1307 SHA512_End(&ctx, dest, &outLen, SHA512_LENGTH); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1329 } | 1396 } |
| 1330 | 1397 |
| 1331 void | 1398 void |
| 1332 SHA384_End(SHA384Context *ctx, unsigned char *digest, | 1399 SHA384_End(SHA384Context *ctx, unsigned char *digest, |
| 1333 unsigned int *digestLen, unsigned int maxDigestLen) | 1400 unsigned int *digestLen, unsigned int maxDigestLen) |
| 1334 { | 1401 { |
| 1335 unsigned int maxLen = SHA_MIN(maxDigestLen, SHA384_LENGTH); | 1402 unsigned int maxLen = SHA_MIN(maxDigestLen, SHA384_LENGTH); |
| 1336 SHA512_End(ctx, digest, digestLen, maxLen); | 1403 SHA512_End(ctx, digest, digestLen, maxLen); |
| 1337 } | 1404 } |
| 1338 | 1405 |
| 1406 void |
| 1407 SHA384_EndRaw(SHA384Context *ctx, unsigned char *digest, |
| 1408 unsigned int *digestLen, unsigned int maxDigestLen) |
| 1409 { |
| 1410 unsigned int maxLen = SHA_MIN(maxDigestLen, SHA384_LENGTH); |
| 1411 SHA512_EndRaw(ctx, digest, digestLen, maxLen); |
| 1412 } |
| 1413 |
| 1339 SECStatus | 1414 SECStatus |
| 1340 SHA384_HashBuf(unsigned char *dest, const unsigned char *src, | 1415 SHA384_HashBuf(unsigned char *dest, const unsigned char *src, |
| 1341 uint32 src_length) | 1416 uint32 src_length) |
| 1342 { | 1417 { |
| 1343 SHA512Context ctx; | 1418 SHA512Context ctx; |
| 1344 unsigned int outLen; | 1419 unsigned int outLen; |
| 1345 | 1420 |
| 1346 SHA384_Begin(&ctx); | 1421 SHA384_Begin(&ctx); |
| 1347 SHA512_Update(&ctx, src, src_length); | 1422 SHA512_Update(&ctx, src, src_length); |
| 1348 SHA512_End(&ctx, dest, &outLen, SHA384_LENGTH); | 1423 SHA512_End(&ctx, dest, &outLen, SHA384_LENGTH); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1515 } | 1590 } |
| 1516 printf("done\n"); | 1591 printf("done\n"); |
| 1517 } | 1592 } |
| 1518 return 0; | 1593 return 0; |
| 1519 } | 1594 } |
| 1520 | 1595 |
| 1521 void *PORT_Alloc(size_t len) { return malloc(len); } | 1596 void *PORT_Alloc(size_t len) { return malloc(len); } |
| 1522 void PORT_Free(void *ptr) { free(ptr); } | 1597 void PORT_Free(void *ptr) { free(ptr); } |
| 1523 void PORT_ZFree(void *ptr, size_t len) { memset(ptr, 0, len); free(ptr); } | 1598 void PORT_ZFree(void *ptr, size_t len) { memset(ptr, 0, len); free(ptr); } |
| 1524 #endif | 1599 #endif |
| OLD | NEW |