sha384.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* included in sha512.c */
  12. const struct _hash_descriptor sha384_desc =
  13. {
  14. "sha384",
  15. 4,
  16. 48,
  17. 128,
  18. /* DER identifier */
  19. { 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86,
  20. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
  21. 0x00, 0x04, 0x30 },
  22. 19,
  23. &sha384_init,
  24. &sha512_process,
  25. &sha384_done,
  26. &sha384_test
  27. };
  28. void sha384_init(hash_state * md)
  29. {
  30. _ARGCHK(md != NULL);
  31. md->sha512.curlen = 0;
  32. md->sha512.length = 0;
  33. md->sha512.state[0] = CONST64(0xcbbb9d5dc1059ed8);
  34. md->sha512.state[1] = CONST64(0x629a292a367cd507);
  35. md->sha512.state[2] = CONST64(0x9159015a3070dd17);
  36. md->sha512.state[3] = CONST64(0x152fecd8f70e5939);
  37. md->sha512.state[4] = CONST64(0x67332667ffc00b31);
  38. md->sha512.state[5] = CONST64(0x8eb44a8768581511);
  39. md->sha512.state[6] = CONST64(0xdb0c2e0d64f98fa7);
  40. md->sha512.state[7] = CONST64(0x47b5481dbefa4fa4);
  41. }
  42. int sha384_done(hash_state * md, unsigned char *hash)
  43. {
  44. unsigned char buf[64];
  45. _ARGCHK(md != NULL);
  46. _ARGCHK(hash != NULL);
  47. if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
  48. return CRYPT_INVALID_ARG;
  49. }
  50. sha512_done(md, buf);
  51. XMEMCPY(hash, buf, 48);
  52. #ifdef CLEAN_STACK
  53. zeromem(buf, sizeof(buf));
  54. #endif
  55. return CRYPT_OK;
  56. }
  57. int sha384_test(void)
  58. {
  59. #ifndef LTC_TEST
  60. return CRYPT_NOP;
  61. #else
  62. static const struct {
  63. char *msg;
  64. unsigned char hash[48];
  65. } tests[] = {
  66. { "abc",
  67. { 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
  68. 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
  69. 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
  70. 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
  71. 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
  72. 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 }
  73. },
  74. { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
  75. { 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
  76. 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
  77. 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
  78. 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
  79. 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
  80. 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 }
  81. },
  82. };
  83. int i;
  84. unsigned char tmp[48];
  85. hash_state md;
  86. for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
  87. sha384_init(&md);
  88. sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
  89. sha384_done(&md, tmp);
  90. if (memcmp(tmp, tests[i].hash, 48) != 0) {
  91. return CRYPT_FAIL_TESTVECTOR;
  92. }
  93. }
  94. return CRYPT_OK;
  95. #endif
  96. }