| /* |
| * sha512.c --- The sha512 algorithm self tests |
| * |
| * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> |
| * (copied from libtomcrypt and then relicensed under GPLv2) |
| * (and later copied from e2fsprogs) |
| * |
| * %Begin-Header% |
| * This file may be redistributed under the terms of the GNU Library |
| * General Public License, version 2. |
| * %End-Header% |
| */ |
| #ifndef __LIBFROG_SHA512SELFTEST_H__ |
| #define __LIBFROG_SHA512SELFTEST_H__ |
| |
| static const struct { |
| char *msg; |
| unsigned char hash[64]; |
| } sha512_tests[] = { |
| { "", |
| { 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, |
| 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, |
| 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, |
| 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, |
| 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, |
| 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, |
| 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, |
| 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e } |
| }, |
| { "abc", |
| { 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, |
| 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, |
| 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, |
| 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, |
| 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, |
| 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, |
| 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, |
| 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f } |
| }, |
| { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
| { 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, |
| 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, |
| 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, |
| 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, |
| 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, |
| 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, |
| 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, |
| 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09 } |
| }, |
| { "The quick brown fox jumps over the lazy dog.\n", |
| { 0x02, 0x0d, 0xa0, 0xf4, 0xd8, 0xa4, 0xc8, 0xbf, |
| 0xbc, 0x98, 0x27, 0x40, 0x27, 0x74, 0x00, 0x61, |
| 0xd7, 0xdf, 0x52, 0xee, 0x07, 0x09, 0x1e, 0xd6, |
| 0x59, 0x5a, 0x08, 0x3e, 0x0f, 0x45, 0x32, 0x7b, |
| 0xbe, 0x59, 0x42, 0x43, 0x12, 0xd8, 0x6f, 0x21, |
| 0x8b, 0x74, 0xed, 0x2e, 0x25, 0x50, 0x7a, 0xba, |
| 0xf5, 0xc7, 0xa5, 0xfc, 0xf4, 0xca, 0xfc, 0xf9, |
| 0x53, 0x8b, 0x70, 0x58, 0x08, 0xfd, 0x55, 0xec } |
| }, |
| }; |
| |
| /* Don't print anything to stdout. */ |
| #define SHA512TEST_QUIET (1U << 0) |
| |
| static int sha512_test(unsigned int flags) |
| { |
| int i; |
| int errors = 0; |
| unsigned char tmp[64]; |
| |
| for (i = 0; i < (int)(sizeof(sha512_tests) / sizeof(sha512_tests[0])); i++) { |
| unsigned char *msg = (unsigned char *) sha512_tests[i].msg; |
| int len = strlen(sha512_tests[i].msg); |
| |
| sha512(msg, len, tmp); |
| if (memcmp(tmp, sha512_tests[i].hash, 64) != 0) |
| errors++; |
| } |
| |
| if (!(flags & SHA512TEST_QUIET) && errors) |
| printf("sha512: %d self tests failed\n", errors); |
| |
| return errors; |
| } |
| |
| #endif /* __LIBFROG_SHA512SELFTEST_H__ */ |