1#include <string.h>
2#include "sha512.h"
3
4static const struct
5{
6 const char *input;
7 const char result[64];
8} tests[] =
9 {
10 /* Test vectors from FIPS 180-2: appendix C.1. */
11 { "abc",
12 "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31"
13 "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a"
14 "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd"
15 "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f" },
16 /* Test vectors from FIPS 180-2: appendix C.2. */
17 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
18 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
19 "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f"
20 "\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18"
21 "\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a"
22 "\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09" },
23 /* Test vectors from the NESSIE project. */
24 { "",
25 "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07"
26 "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce"
27 "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f"
28 "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e" },
29 { "a",
30 "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2"
31 "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53"
32 "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46"
33 "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75" },
34 { "message digest",
35 "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51"
36 "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33"
37 "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20"
38 "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c" },
39 { "abcdefghijklmnopqrstuvwxyz",
40 "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81"
41 "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29"
42 "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59"
43 "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1" },
44 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
45 "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16"
46 "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35"
47 "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0"
48 "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45" },
49 { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
50 "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93"
51 "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4"
52 "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab"
53 "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94" },
54 { "123456789012345678901234567890123456789012345678901234567890"
55 "12345678901234567890",
56 "\x72\xec\x1e\xf1\x12\x4a\x45\xb0\x47\xe8\xb7\xc7\x5a\x93\x21\x95"
57 "\x13\x5b\xb6\x1d\xe2\x4e\xc0\xd1\x91\x40\x42\x24\x6e\x0a\xec\x3a"
58 "\x23\x54\xe0\x93\xd7\x6f\x30\x48\xb4\x56\x76\x43\x46\x90\x0c\xb1"
59 "\x30\xd2\xa4\xfd\x5d\xd1\x6a\xbb\x5e\x30\xbc\xb8\x50\xde\xe8\x43" }
60 };
61
62
63int
64main (void)
65{
66 struct sha512_ctx ctx;
67 char sum[64];
68 int result = 0;
69 int cnt;
70
71 for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt)
72 {
73 __sha512_init_ctx (ctx: &ctx);
74 __sha512_process_bytes (buffer: tests[cnt].input, len: strlen (s: tests[cnt].input),
75 ctx: &ctx);
76 __sha512_finish_ctx (ctx: &ctx, resbuf: sum);
77 if (memcmp (s1: tests[cnt].result, s2: sum, n: 64) != 0)
78 {
79 printf (format: "test %d run %d failed\n", cnt, 1);
80 result = 1;
81 }
82
83 __sha512_init_ctx (ctx: &ctx);
84 for (int i = 0; tests[cnt].input[i] != '\0'; ++i)
85 __sha512_process_bytes (buffer: &tests[cnt].input[i], len: 1, ctx: &ctx);
86 __sha512_finish_ctx (ctx: &ctx, resbuf: sum);
87 if (memcmp (s1: tests[cnt].result, s2: sum, n: 64) != 0)
88 {
89 printf (format: "test %d run %d failed\n", cnt, 2);
90 result = 1;
91 }
92 }
93
94 /* Test vector from FIPS 180-2: appendix C.3. */
95 char buf[1000];
96 memset (s: buf, c: 'a', n: sizeof (buf));
97 __sha512_init_ctx (ctx: &ctx);
98 for (int i = 0; i < 1000; ++i)
99 __sha512_process_bytes (buffer: buf, len: sizeof (buf), ctx: &ctx);
100 __sha512_finish_ctx (ctx: &ctx, resbuf: sum);
101 static const char expected[64] =
102 "\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63"
103 "\x8e\x1f\x98\xb1\x3b\x20\x44\x28\x56\x32\xa8\x03\xaf\xa9\x73\xeb"
104 "\xde\x0f\xf2\x44\x87\x7e\xa6\x0a\x4c\xb0\x43\x2c\xe5\x77\xc3\x1b"
105 "\xeb\x00\x9c\x5c\x2c\x49\xaa\x2e\x4e\xad\xb2\x17\xad\x8c\xc0\x9b";
106 if (memcmp (s1: expected, s2: sum, n: 64) != 0)
107 {
108 printf (format: "test %d failed\n", cnt);
109 result = 1;
110 }
111
112 return result;
113}
114

source code of glibc/crypt/sha512test.c