sha224.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void 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. }
  41. int sha224_done(hash_state * md, unsigned char *hash)
  42. {
  43. unsigned char buf[32];
  44. int err;
  45. err = sha256_done(md, buf);
  46. XMEMCPY(hash, buf, 28);
  47. #ifdef CLEAN_STACK
  48. zeromem(buf, sizeof(buf));
  49. #endif
  50. return err;
  51. }
  52. int sha224_test(void)
  53. {
  54. #ifndef LTC_TEST
  55. return CRYPT_NOP;
  56. #else
  57. static const struct {
  58. char *msg;
  59. unsigned char hash[28];
  60. } tests[] = {
  61. { "abc",
  62. { 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8,
  63. 0x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2,
  64. 0x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd,
  65. 0xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }
  66. },
  67. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  68. { 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76,
  69. 0xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89,
  70. 0x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4,
  71. 0xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 }
  72. },
  73. };
  74. int i;
  75. unsigned char tmp[28];
  76. hash_state md;
  77. for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
  78. sha224_init(&md);
  79. sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
  80. sha224_done(&md, tmp);
  81. if (memcmp(tmp, tests[i].hash, 28) != 0) {
  82. return CRYPT_FAIL_TESTVECTOR;
  83. }
  84. }
  85. return CRYPT_OK;
  86. #endif
  87. }