Path: blob/master/libs/tomcrypt/src/hashes/sha2/sha384.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 sha384.c10LTC_SHA384 hash included in sha512.c, Tom St Denis11*/1213#include "tomcrypt.h"1415#if defined(LTC_SHA384) && defined(LTC_SHA512)1617const struct ltc_hash_descriptor sha384_desc =18{19"sha384",204,2148,22128,2324/* OID */25{ 2, 16, 840, 1, 101, 3, 4, 2, 2, },269,2728&sha384_init,29&sha512_process,30&sha384_done,31&sha384_test,32NULL33};3435/**36Initialize the hash state37@param md The hash state you wish to initialize38@return CRYPT_OK if successful39*/40int sha384_init(hash_state * md)41{42LTC_ARGCHK(md != NULL);4344md->sha512.curlen = 0;45md->sha512.length = 0;46md->sha512.state[0] = CONST64(0xcbbb9d5dc1059ed8);47md->sha512.state[1] = CONST64(0x629a292a367cd507);48md->sha512.state[2] = CONST64(0x9159015a3070dd17);49md->sha512.state[3] = CONST64(0x152fecd8f70e5939);50md->sha512.state[4] = CONST64(0x67332667ffc00b31);51md->sha512.state[5] = CONST64(0x8eb44a8768581511);52md->sha512.state[6] = CONST64(0xdb0c2e0d64f98fa7);53md->sha512.state[7] = CONST64(0x47b5481dbefa4fa4);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 sha384_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, 48);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 sha384_test(void)87{88#ifndef LTC_TEST89return CRYPT_NOP;90#else91static const struct {92const char *msg;93unsigned char hash[48];94} tests[] = {95{ "abc",96{ 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,970xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,980x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,990x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,1000x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,1010x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7 }102},103{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",104{ 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,1050x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,1060x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,1070x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,1080xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,1090x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39 }110},111};112113int i;114unsigned char tmp[48];115hash_state md;116117for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {118sha384_init(&md);119sha384_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));120sha384_done(&md, tmp);121if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA384", i)) {122return CRYPT_FAIL_TESTVECTOR;123}124}125return CRYPT_OK;126#endif127}128129#endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */130131132