sha512.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* sha512.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <crypto/sha2_large_common.h>
  19. #include <crypto/sha512.h>
  20. void sha512_nextBlock (sha512_ctx_t* ctx, const void* block){
  21. sha2_large_common_nextBlock(ctx, block);
  22. }
  23. void sha512_lastBlock(sha512_ctx_t* ctx, const void* block, uint16_t length_b){
  24. sha2_large_common_lastBlock(ctx, block, length_b);
  25. }
  26. static const
  27. uint64_t sha512_init_values[8] = {
  28. 0x6a09e667f3bcc908LL, 0xbb67ae8584caa73bLL, 0x3c6ef372fe94f82bLL, 0xa54ff53a5f1d36f1LL,
  29. 0x510e527fade682d1LL, 0x9b05688c2b3e6c1fLL, 0x1f83d9abfb41bd6bLL, 0x5be0cd19137e2179LL
  30. };
  31. void sha512_init(sha512_ctx_t* ctx){
  32. ctx->length = 0;
  33. memcpy(ctx->h, sha512_init_values, 8*8);
  34. }
  35. void sha512_ctx2hash(void* dest, const sha512_ctx_t* ctx){
  36. uint8_t i=8, j, *s = (uint8_t*)(ctx->h);
  37. do{
  38. j=7;
  39. do{
  40. *((uint8_t*)dest) = s[j];
  41. dest = (uint8_t*)dest + 1;
  42. }while(j--);
  43. s += 8;
  44. }while(--i);
  45. }
  46. void sha512(void* dest, const void* msg, uint32_t length_b){
  47. sha512_ctx_t ctx;
  48. sha512_init(&ctx);
  49. while(length_b >= 1024){
  50. sha512_nextBlock(&ctx, msg);
  51. msg = (uint8_t*)msg + 1024/8;
  52. length_b -= 1024;
  53. }
  54. sha512_lastBlock(&ctx, msg, length_b);
  55. sha512_ctx2hash(dest, &ctx);
  56. }