Path: blob/master/libs/tomcrypt/src/hashes/sha2/sha224.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8/**9@param sha224.c10LTC_SHA-224 new NIST standard based off of LTC_SHA-256 truncated to 224 bits (Tom St Denis)11*/1213#include "tomcrypt.h"1415#if defined(LTC_SHA224) && defined(LTC_SHA256)1617const struct ltc_hash_descriptor sha224_desc =18{19"sha224",2010,2128,2264,2324/* OID */25{ 2, 16, 840, 1, 101, 3, 4, 2, 4, },269,2728&sha224_init,29&sha256_process,30&sha224_done,31&sha224_test,32NULL33};3435/* init the sha256 er... sha224 state ;-) */36/**37Initialize the hash state38@param md The hash state you wish to initialize39@return CRYPT_OK if successful40*/41int sha224_init(hash_state * md)42{43LTC_ARGCHK(md != NULL);4445md->sha256.curlen = 0;46md->sha256.length = 0;47md->sha256.state[0] = 0xc1059ed8UL;48md->sha256.state[1] = 0x367cd507UL;49md->sha256.state[2] = 0x3070dd17UL;50md->sha256.state[3] = 0xf70e5939UL;51md->sha256.state[4] = 0xffc00b31UL;52md->sha256.state[5] = 0x68581511UL;53md->sha256.state[6] = 0x64f98fa7UL;54md->sha256.state[7] = 0xbefa4fa4UL;55return CRYPT_OK;56}5758/**59Terminate the hash to get the digest60@param md The hash state61@param out [out] The destination of the hash (28 bytes)62@return CRYPT_OK if successful63*/64int sha224_done(hash_state * md, unsigned char *out)65{66unsigned char buf[32];67int err;6869LTC_ARGCHK(md != NULL);70LTC_ARGCHK(out != NULL);7172err = sha256_done(md, buf);73XMEMCPY(out, buf, 28);74#ifdef LTC_CLEAN_STACK75zeromem(buf, sizeof(buf));76#endif77return err;78}7980/**81Self-test the hash82@return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled83*/84int sha224_test(void)85{86#ifndef LTC_TEST87return CRYPT_NOP;88#else89static const struct {90const char *msg;91unsigned char hash[28];92} tests[] = {93{ "abc",94{ 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8,950x22, 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2,960x55, 0xb3, 0x2a, 0xad, 0xbc, 0xe4, 0xbd,970xa0, 0xb3, 0xf7, 0xe3, 0x6c, 0x9d, 0xa7 }98},99{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",100{ 0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76,1010xcc, 0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89,1020x01, 0x50, 0xb0, 0xc6, 0x45, 0x5c, 0xb4,1030xf5, 0x8b, 0x19, 0x52, 0x52, 0x25, 0x25 }104},105};106107int i;108unsigned char tmp[28];109hash_state md;110111for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {112sha224_init(&md);113sha224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));114sha224_done(&md, tmp);115if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA224", i)) {116return CRYPT_FAIL_TESTVECTOR;117}118}119return CRYPT_OK;120#endif121}122123#endif /* defined(LTC_SHA224) && defined(LTC_SHA256) */124125126