tests: Shut the shell up unless verbose
[openssl.git] / test / sha1test.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include "../e_os.h"
15 #include <openssl/evp.h>
16 #include <openssl/sha.h>
17
18 #ifdef CHARSET_EBCDIC
19 # include <openssl/ebcdic.h>
20 #endif
21
22 static char test[][80] = {
23     { "abc" },
24     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }
25 };
26
27 static char *ret[] = {
28     "a9993e364706816aba3e25717850c26c9cd0d89d",
29     "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
30 };
31
32 static char *bigret = "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
33
34 static char *pt(unsigned char *md);
35 int main(int argc, char *argv[])
36 {
37     unsigned int i;
38     int err = 0;
39     char **R;
40     static unsigned char buf[1000];
41     char *p, *r;
42     EVP_MD_CTX *c;
43     unsigned char md[SHA_DIGEST_LENGTH];
44
45     c = EVP_MD_CTX_new();
46     R = ret;
47     for (i = 0; i < OSSL_NELEM(test); i++) {
48 # ifdef CHARSET_EBCDIC
49         ebcdic2ascii(test[i], test[i], strlen(test[i]));
50 # endif
51         EVP_Digest(test[i], strlen(test[i]), md, NULL, EVP_sha1(), NULL);
52         p = pt(md);
53         if (strcmp(p, (char *)*R) != 0) {
54             printf("error calculating SHA1 on '%s'\n", test[i]);
55             printf("got %s instead of %s\n", p, *R);
56             err++;
57         } else
58             printf("test %d ok\n", i + 1);
59         R++;
60     }
61
62     memset(buf, 'a', 1000);
63 #ifdef CHARSET_EBCDIC
64     ebcdic2ascii(buf, buf, 1000);
65 #endif                         /* CHARSET_EBCDIC */
66     EVP_DigestInit_ex(c, EVP_sha1(), NULL);
67     for (i = 0; i < 1000; i++)
68         EVP_DigestUpdate(c, buf, 1000);
69     EVP_DigestFinal_ex(c, md, NULL);
70     p = pt(md);
71
72     r = bigret;
73     if (strcmp(p, r) != 0) {
74         printf("error calculating SHA1 on 'a' * 1000\n");
75         printf("got %s instead of %s\n", p, r);
76         err++;
77     } else
78         printf("test 3 ok\n");
79
80     EVP_MD_CTX_free(c);
81     EXIT(err);
82     return (0);
83 }
84
85 static char *pt(unsigned char *md)
86 {
87     int i;
88     static char buf[80];
89
90     for (i = 0; i < SHA_DIGEST_LENGTH; i++)
91         sprintf(&(buf[i * 2]), "%02x", md[i]);
92     return (buf);
93 }