1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
11 SHA512/256 hash included in sha512.c
16 #if defined(LTC_SHA512_256) && defined(LTC_SHA512)
18 const struct ltc_hash_descriptor sha512_256_desc =
26 { 2, 16, 840, 1, 101, 3, 4, 2, 6, },
37 Initialize the hash state
38 @param md The hash state you wish to initialize
39 @return CRYPT_OK if successful
41 int sha512_256_init(hash_state * md)
43 LTC_ARGCHK(md != NULL);
45 md->sha512.curlen = 0;
46 md->sha512.length = 0;
47 md->sha512.state[0] = CONST64(0x22312194FC2BF72C);
48 md->sha512.state[1] = CONST64(0x9F555FA3C84C64C2);
49 md->sha512.state[2] = CONST64(0x2393B86B6F53B151);
50 md->sha512.state[3] = CONST64(0x963877195940EABD);
51 md->sha512.state[4] = CONST64(0x96283EE2A88EFFE3);
52 md->sha512.state[5] = CONST64(0xBE5E1E2553863992);
53 md->sha512.state[6] = CONST64(0x2B0199FC2C85B8AA);
54 md->sha512.state[7] = CONST64(0x0EB72DDC81C52CA2);
59 Terminate the hash to get the digest
60 @param md The hash state
61 @param out [out] The destination of the hash (48 bytes)
62 @return CRYPT_OK if successful
64 int sha512_256_done(hash_state * md, unsigned char *out)
66 unsigned char buf[64];
68 LTC_ARGCHK(md != NULL);
69 LTC_ARGCHK(out != NULL);
71 if (md->sha512.curlen >= sizeof(md->sha512.buf)) {
72 return CRYPT_INVALID_ARG;
76 XMEMCPY(out, buf, 32);
77 #ifdef LTC_CLEAN_STACK
78 zeromem(buf, sizeof(buf));
85 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
87 int sha512_256_test(void)
94 unsigned char hash[32];
97 { 0x53, 0x04, 0x8E, 0x26, 0x81, 0x94, 0x1E, 0xF9,
98 0x9B, 0x2E, 0x29, 0xB7, 0x6B, 0x4C, 0x7D, 0xAB,
99 0xE4, 0xC2, 0xD0, 0xC6, 0x34, 0xFC, 0x6D, 0x46,
100 0xE0, 0xE2, 0xF1, 0x31, 0x07, 0xE7, 0xAF, 0x23 }
102 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
103 { 0x39, 0x28, 0xE1, 0x84, 0xFB, 0x86, 0x90, 0xF8,
104 0x40, 0xDA, 0x39, 0x88, 0x12, 0x1D, 0x31, 0xBE,
105 0x65, 0xCB, 0x9D, 0x3E, 0xF8, 0x3E, 0xE6, 0x14,
106 0x6F, 0xEA, 0xC8, 0x61, 0xE1, 0x9B, 0x56, 0x3A }
111 unsigned char tmp[32];
114 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
115 sha512_256_init(&md);
116 sha512_256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
117 sha512_256_done(&md, tmp);
118 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA512-265", i)) {
119 return CRYPT_FAIL_TESTVECTOR;
126 #endif /* defined(LTC_SHA384) && defined(LTC_SHA512) */
128 /* ref: $Format:%D$ */
129 /* git commit: $Format:%H$ */
130 /* commit time: $Format:%ai$ */