sha224.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /* SHA-224 new NIST standard based off of SHA-256 truncated to 224 bits */
  12. const struct _hash_descriptor sha224_desc =
  13. {
  14. "sha224",
  15. 10,
  16. 28,
  17. 64,
  18. /* DER identifier (not supported) */
  19. { 0x00 },
  20. 0,
  21. &sha224_init,
  22. &sha256_process,
  23. &sha224_done,
  24. &sha224_test
  25. };
  26. /* init the sha256 er... sha224 state ;-) */
  27. int sha224_init(hash_state * md)
  28. {
  29. _ARGCHK(md != NULL);
  30. md->sha256.curlen = 0;
  31. md->sha256.length = 0;
  32. md->sha256.state[0] = 0xc1059ed8UL;
  33. md->sha256.state[1] = 0x367cd507UL;
  34. md->sha256.state[2] = 0x3070dd17UL;
  35. md->sha256.state[3] = 0xf70e5939UL;
  36. md->sha256.state[4] = 0xffc00b31UL;
  37. md->sha256.state[5] = 0x68581511UL;
  38. md->sha256.state[6] = 0x64f98fa7UL;
  39. md->sha256.state[7] = 0xbefa4fa4UL;
  40. return CRYPT_OK;
  41. }
  42. int sha224_done(hash_state * md, unsigned char *hash)
  43. {
  44. unsigned char buf[32];
  45. int err;
  46. err = sha256_done(md, buf);
  47. XMEMCPY(hash, buf, 28);
  48. #ifdef CLEAN_STACK
  49. zeromem(buf, sizeof(buf));
  50. #endif
  51. return err;
  52. }
  53. int sha224_test(void)
  54. {
  55. #ifndef LTC_TEST
  56. return CRYPT_NOP;
  57. #else
  58. static const struct {
  59. char *msg;
  60. unsigned char hash[28];
  61. } tests[] = {
  62. { "abc",
  63. { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8,
  64. 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2,
  65. 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd,
  66. 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }
  67. },
  68. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  69. { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76,
  70. 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89,
  71. 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4,
  72. 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 }
  73. },
  74. };
  75. int i;
  76. unsigned char tmp[28];
  77. hash_state md;
  78. for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
  79. sha224_init(&md);
  80. sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
  81. sha224_done(&md, tmp);
  82. if (memcmp(tmp, tests[i].hash, 28) != 0) {
  83. return CRYPT_FAIL_TESTVECTOR;
  84. }
  85. }
  86. return CRYPT_OK;
  87. #endif
  88. }