sha384.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2015, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <string.h>
  31. #include "os_port.h"
  32. #include "crypto.h"
  33. /**
  34. * Initialize the SHA384 context
  35. */
  36. void SHA384_Init(SHA384_CTX *ctx)
  37. {
  38. //Set initial hash value
  39. ctx->h_dig.h[0] = 0xCBBB9D5DC1059ED8;
  40. ctx->h_dig.h[1] = 0x629A292A367CD507;
  41. ctx->h_dig.h[2] = 0x9159015A3070DD17;
  42. ctx->h_dig.h[3] = 0x152FECD8F70E5939;
  43. ctx->h_dig.h[4] = 0x67332667FFC00B31;
  44. ctx->h_dig.h[5] = 0x8EB44A8768581511;
  45. ctx->h_dig.h[6] = 0xDB0C2E0D64F98FA7;
  46. ctx->h_dig.h[7] = 0x47B5481DBEFA4FA4;
  47. // Number of bytes in the buffer
  48. ctx->size = 0;
  49. // Total length of the message
  50. ctx->totalSize = 0;
  51. }
  52. /**
  53. * Accepts an array of octets as the next portion of the message.
  54. */
  55. void SHA384_Update(SHA384_CTX *ctx, const uint8_t * msg, int len)
  56. {
  57. // The function is defined in the exact same manner as SHA-512
  58. SHA512_Update(ctx, msg, len);
  59. }
  60. /**
  61. * Return the 384-bit message digest into the user's array
  62. */
  63. void SHA384_Final(uint8_t *digest, SHA384_CTX *ctx)
  64. {
  65. // The function is defined in the exact same manner as SHA-512
  66. SHA512_Final(NULL, ctx);
  67. // Copy the resulting digest
  68. if (digest != NULL)
  69. memcpy(digest, ctx->h_dig.digest, SHA384_SIZE);
  70. }