sha384-internal.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SHA-384 hash implementation and interface functions
  3. * Copyright (c) 2015, Pali Rohár <pali.rohar@gmail.com>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "sha384_i.h"
  11. #include "crypto.h"
  12. /**
  13. * sha384_vector - SHA384 hash for data vector
  14. * @num_elem: Number of elements in the data vector
  15. * @addr: Pointers to the data areas
  16. * @len: Lengths of the data blocks
  17. * @mac: Buffer for the hash
  18. * Returns: 0 on success, -1 of failure
  19. */
  20. int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  21. u8 *mac)
  22. {
  23. struct sha384_state ctx;
  24. size_t i;
  25. sha384_init(&ctx);
  26. for (i = 0; i < num_elem; i++)
  27. if (sha384_process(&ctx, addr[i], len[i]))
  28. return -1;
  29. if (sha384_done(&ctx, mac))
  30. return -1;
  31. return 0;
  32. }
  33. /* ===== start - public domain SHA384 implementation ===== */
  34. /* This is based on SHA384 implementation in LibTomCrypt that was released into
  35. * public domain by Tom St Denis. */
  36. #define CONST64(n) n ## ULL
  37. /**
  38. Initialize the hash state
  39. @param md The hash state you wish to initialize
  40. @return CRYPT_OK if successful
  41. */
  42. void sha384_init(struct sha384_state *md)
  43. {
  44. md->curlen = 0;
  45. md->length = 0;
  46. md->state[0] = CONST64(0xcbbb9d5dc1059ed8);
  47. md->state[1] = CONST64(0x629a292a367cd507);
  48. md->state[2] = CONST64(0x9159015a3070dd17);
  49. md->state[3] = CONST64(0x152fecd8f70e5939);
  50. md->state[4] = CONST64(0x67332667ffc00b31);
  51. md->state[5] = CONST64(0x8eb44a8768581511);
  52. md->state[6] = CONST64(0xdb0c2e0d64f98fa7);
  53. md->state[7] = CONST64(0x47b5481dbefa4fa4);
  54. }
  55. int sha384_process(struct sha384_state *md, const unsigned char *in,
  56. unsigned long inlen)
  57. {
  58. return sha512_process(md, in, inlen);
  59. }
  60. /**
  61. Terminate the hash to get the digest
  62. @param md The hash state
  63. @param out [out] The destination of the hash (48 bytes)
  64. @return CRYPT_OK if successful
  65. */
  66. int sha384_done(struct sha384_state *md, unsigned char *out)
  67. {
  68. unsigned char buf[64];
  69. if (md->curlen >= sizeof(md->buf))
  70. return -1;
  71. if (sha512_done(md, buf) != 0)
  72. return -1;
  73. os_memcpy(out, buf, 48);
  74. return 0;
  75. }
  76. /* ===== end - public domain SHA384 implementation ===== */