Path: blob/master/libs/tomcrypt/src/hashes/sha2/sha512_224.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 sha512_224.c10SHA512/224 hash included in sha512.c11*/1213#include "tomcrypt.h"1415#if defined(LTC_SHA512_224) && defined(LTC_SHA512)1617const struct ltc_hash_descriptor sha512_224_desc =18{19"sha512-224",2015,2128,22128,2324/* OID */25{ 2, 16, 840, 1, 101, 3, 4, 2, 5, },269,2728&sha512_224_init,29&sha512_process,30&sha512_224_done,31&sha512_224_test,32NULL33};3435/**36Initialize the hash state37@param md The hash state you wish to initialize38@return CRYPT_OK if successful39*/40int sha512_224_init(hash_state * md)41{42LTC_ARGCHK(md != NULL);4344md->sha512.curlen = 0;45md->sha512.length = 0;46md->sha512.state[0] = CONST64(0x8C3D37C819544DA2);47md->sha512.state[1] = CONST64(0x73E1996689DCD4D6);48md->sha512.state[2] = CONST64(0x1DFAB7AE32FF9C82);49md->sha512.state[3] = CONST64(0x679DD514582F9FCF);50md->sha512.state[4] = CONST64(0x0F6D2B697BD44DA8);51md->sha512.state[5] = CONST64(0x77E36F7304C48942);52md->sha512.state[6] = CONST64(0x3F9D85A86A1D36C8);53md->sha512.state[7] = CONST64(0x1112E6AD91D692A1);54return CRYPT_OK;55}5657/**58Terminate the hash to get the digest59@param md The hash state60@param out [out] The destination of the hash (48 bytes)61@return CRYPT_OK if successful62*/63int sha512_224_done(hash_state * md, unsigned char *out)64{65unsigned char buf[64];6667LTC_ARGCHK(md != NULL);68LTC_ARGCHK(out != NULL);6970if (md->sha512.curlen >= sizeof(md->sha512.buf)) {71return CRYPT_INVALID_ARG;72}7374sha512_done(md, buf);75XMEMCPY(out, buf, 28);76#ifdef LTC_CLEAN_STACK77zeromem(buf, sizeof(buf));78#endif79return CRYPT_OK;80}8182/**83Self-test the hash84@return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled85*/86int sha512_224_test(void)87{88#ifndef LTC_TEST89return CRYPT_NOP;90#else91static const struct {92const char *msg;93unsigned char hash[28];94} tests[] = {95{ "abc",96{ 0x46, 0x34, 0x27, 0x0F, 0x70, 0x7B, 0x6A, 0x54,970xDA, 0xAE, 0x75, 0x30, 0x46, 0x08, 0x42, 0xE2,980x0E, 0x37, 0xED, 0x26, 0x5C, 0xEE, 0xE9, 0xA4,990x3E, 0x89, 0x24, 0xAA }100},101{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",102{ 0x23, 0xFE, 0xC5, 0xBB, 0x94, 0xD6, 0x0B, 0x23,1030x30, 0x81, 0x92, 0x64, 0x0B, 0x0C, 0x45, 0x33,1040x35, 0xD6, 0x64, 0x73, 0x4F, 0xE4, 0x0E, 0x72,1050x68, 0x67, 0x4A, 0xF9 }106},107};108109int i;110unsigned char tmp[28];111hash_state md;112113for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {114sha512_224_init(&md);115sha512_224_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));116sha512_224_done(&md, tmp);117if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-224", i)) {118return CRYPT_FAIL_TESTVECTOR;119}120}121return CRYPT_OK;122#endif123}124125#endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */126127128