Imported Upstream version 1.8.7
[debian/sudo] / plugins / sudoers / regress / parser / check_digest.c
1 /*
2  * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #ifdef STDC_HEADERS
21 # include <stdlib.h>
22 # include <stddef.h>
23 #else
24 # ifdef HAVE_STDLIB_H
25 #  include <stdlib.h>
26 # endif
27 #endif /* STDC_HEADERS */
28 #ifdef HAVE_STRING_H
29 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
30 #  include <memory.h>
31 # endif
32 # include <string.h>
33 #endif /* HAVE_STRING_H */
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif /* HAVE_STRINGS_H */
37 #if defined(HAVE_STDINT_H)
38 # include <stdint.h>
39 #elif defined(HAVE_INTTYPES_H)
40 # include <inttypes.h>
41 #endif
42
43 #include "missing.h"
44 #include "sha2.h"
45
46 __dso_public int main(int argc, char *argv[]);
47
48 static struct digest_function {
49     const char *digest_name;
50     const int digest_len;
51     void (*init)(SHA2_CTX *);
52     void (*update)(SHA2_CTX *, const unsigned char *, size_t);
53     void (*final)(unsigned char *, SHA2_CTX *);
54 } digest_functions[] = {
55     {
56         "SHA224",
57         SHA224_DIGEST_LENGTH,
58         SHA224Init,
59         SHA224Update,
60         SHA224Final
61     }, {
62         "SHA256",
63         SHA256_DIGEST_LENGTH,
64         SHA256Init,
65         SHA256Update,
66         SHA256Final
67     }, {
68         "SHA384",
69         SHA384_DIGEST_LENGTH,
70         SHA384Init,
71         SHA384Update,
72         SHA384Final
73     }, {
74         "SHA512",
75         SHA512_DIGEST_LENGTH,
76         SHA512Init,
77         SHA512Update,
78         SHA512Final
79     }, {
80         NULL
81     }
82 };
83
84 int
85 main(int argc, char *argv[])
86 {
87     SHA2_CTX ctx;
88     int i, j;
89     struct digest_function *func;
90     unsigned char digest[SHA512_DIGEST_LENGTH];
91     static const char hex[] = "0123456789abcdef";
92     unsigned char buf[1000];
93     unsigned const char *test_strings[] = {
94         "",
95         "a",
96         "abc",
97         "message digest",
98         "abcdefghijklmnopqrstuvwxyz",
99         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
100         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
101             "0123456789",
102         "12345678901234567890123456789012345678901234567890123456789"
103             "012345678901234567890",
104     };
105
106     for (func = digest_functions; func->digest_name != NULL; func++) {
107         for (i = 0; i < 8; i++) {
108             func->init(&ctx);
109             func->update(&ctx, test_strings[i], strlen(test_strings[i]));
110             func->final(digest, &ctx);
111             printf("%s (\"%s\") = ", func->digest_name, test_strings[i]);
112             for (j = 0; j < func->digest_len; j++) {
113                 putchar(hex[digest[j] >> 4]);
114                 putchar(hex[digest[j] & 0x0f]);
115             }
116             putchar('\n');
117         }
118
119         /* Simulate a string of a million 'a' characters. */
120         memset(buf, 'a', sizeof(buf));
121         func->init(&ctx);
122         for (i = 0; i < 1000; i++) {
123             func->update(&ctx, buf, sizeof(buf));
124         }
125         func->final(digest, &ctx);
126         printf("%s (one million 'a' characters) = ", func->digest_name);
127         for (j = 0; j < func->digest_len; j++) {
128             putchar(hex[digest[j] >> 4]);
129             putchar(hex[digest[j] & 0x0f]);
130         }
131         putchar('\n');
132     }
133     exit(0);
134 }