Path: blob/master/libs/tomcrypt/src/hashes/sha2/sha512_256.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_256.c10SHA512/256 hash included in sha512.c11*/1213#include "tomcrypt.h"1415#if defined(LTC_SHA512_256) && defined(LTC_SHA512)1617const struct ltc_hash_descriptor sha512_256_desc =18{19"sha512-256",2016,2132,22128,2324/* OID */25{ 2, 16, 840, 1, 101, 3, 4, 2, 6, },269,2728&sha512_256_init,29&sha512_process,30&sha512_256_done,31&sha512_256_test,32NULL33};3435/**36Initialize the hash state37@param md The hash state you wish to initialize38@return CRYPT_OK if successful39*/40int sha512_256_init(hash_state * md)41{42LTC_ARGCHK(md != NULL);4344md->sha512.curlen = 0;45md->sha512.length = 0;46md->sha512.state[0] = CONST64(0x22312194FC2BF72C);47md->sha512.state[1] = CONST64(0x9F555FA3C84C64C2);48md->sha512.state[2] = CONST64(0x2393B86B6F53B151);49md->sha512.state[3] = CONST64(0x963877195940EABD);50md->sha512.state[4] = CONST64(0x96283EE2A88EFFE3);51md->sha512.state[5] = CONST64(0xBE5E1E2553863992);52md->sha512.state[6] = CONST64(0x2B0199FC2C85B8AA);53md->sha512.state[7] = CONST64(0x0EB72DDC81C52CA2);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_256_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, 32);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_256_test(void)87{88#ifndef LTC_TEST89return CRYPT_NOP;90#else91static const struct {92const char *msg;93unsigned char hash[32];94} tests[] = {95{ "abc",96{ 0x53, 0x04, 0x8E, 0x26, 0x81, 0x94, 0x1E, 0xF9,970x9B, 0x2E, 0x29, 0xB7, 0x6B, 0x4C, 0x7D, 0xAB,980xE4, 0xC2, 0xD0, 0xC6, 0x34, 0xFC, 0x6D, 0x46,990xE0, 0xE2, 0xF1, 0x31, 0x07, 0xE7, 0xAF, 0x23 }100},101{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",102{ 0x39, 0x28, 0xE1, 0x84, 0xFB, 0x86, 0x90, 0xF8,1030x40, 0xDA, 0x39, 0x88, 0x12, 0x1D, 0x31, 0xBE,1040x65, 0xCB, 0x9D, 0x3E, 0xF8, 0x3E, 0xE6, 0x14,1050x6F, 0xEA, 0xC8, 0x61, 0xE1, 0x9B, 0x56, 0x3A }106},107};108109int i;110unsigned char tmp[32];111hash_state md;112113for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {114sha512_256_init(&md);115sha512_256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));116sha512_256_done(&md, tmp);117if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-265", i)) {118return CRYPT_FAIL_TESTVECTOR;119}120}121return CRYPT_OK;122#endif123}124125#endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */126127128