Annotation of embedaddon/sudo/plugins/sudoers/regress/parser/check_digest.c, revision 1.1.1.2

1.1       misho       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: 
1.1.1.2 ! misho      84: #define NUM_TESTS      8
        !            85: static const char *test_strings[NUM_TESTS] = {
        !            86:     "",
        !            87:     "a",
        !            88:     "abc",
        !            89:     "message digest",
        !            90:     "abcdefghijklmnopqrstuvwxyz",
        !            91:     "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
        !            92:     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
        !            93:     "12345678901234567890123456789012345678901234567890123456789"
        !            94:        "012345678901234567890",
        !            95: };
        !            96: 
1.1       misho      97: int
                     98: main(int argc, char *argv[])
                     99: {
                    100:     SHA2_CTX ctx;
                    101:     int i, j;
                    102:     struct digest_function *func;
                    103:     unsigned char digest[SHA512_DIGEST_LENGTH];
                    104:     static const char hex[] = "0123456789abcdef";
                    105:     unsigned char buf[1000];
                    106: 
                    107:     for (func = digest_functions; func->digest_name != NULL; func++) {
1.1.1.2 ! misho     108:        for (i = 0; i < NUM_TESTS; i++) {
1.1       misho     109:            func->init(&ctx);
1.1.1.2 ! misho     110:            func->update(&ctx, (unsigned char *)test_strings[i],
        !           111:                strlen(test_strings[i]));
1.1       misho     112:            func->final(digest, &ctx);
                    113:            printf("%s (\"%s\") = ", func->digest_name, test_strings[i]);
                    114:            for (j = 0; j < func->digest_len; j++) {
                    115:                putchar(hex[digest[j] >> 4]);
                    116:                putchar(hex[digest[j] & 0x0f]);
                    117:            }
                    118:            putchar('\n');
                    119:        }
                    120: 
                    121:        /* Simulate a string of a million 'a' characters. */
                    122:        memset(buf, 'a', sizeof(buf));
                    123:        func->init(&ctx);
                    124:        for (i = 0; i < 1000; i++) {
                    125:            func->update(&ctx, buf, sizeof(buf));
                    126:        }
                    127:        func->final(digest, &ctx);
                    128:        printf("%s (one million 'a' characters) = ", func->digest_name);
                    129:        for (j = 0; j < func->digest_len; j++) {
                    130:            putchar(hex[digest[j] >> 4]);
                    131:            putchar(hex[digest[j] & 0x0f]);
                    132:        }
                    133:        putchar('\n');
                    134:     }
                    135:     exit(0);
                    136: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>