Add test for -nameout output
[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         if (!EVP_Digest(test[i], strlen(test[i]), md, NULL, EVP_sha1(),
52             NULL)) {
53             printf("EVP_Digest() error\n");
54             err++;
55             goto err;
56         }
57         p = pt(md);
58         if (strcmp(p, (char *)*R) != 0) {
59             printf("error calculating SHA1 on '%s'\n", test[i]);
60             printf("got %s instead of %s\n", p, *R);
61             err++;
62         } else
63             printf("test %d ok\n", i + 1);
64         R++;
65     }
66
67     memset(buf, 'a', 1000);
68 #ifdef CHARSET_EBCDIC
69     ebcdic2ascii(buf, buf, 1000);
70 #endif                         /* CHARSET_EBCDIC */
71     if (!EVP_DigestInit_ex(c, EVP_sha1(), NULL)) {
72         printf("EVP_DigestInit_ex() error\n");
73         err++;
74         goto err;
75     }
76     for (i = 0; i < 1000; i++) {
77         if (!EVP_DigestUpdate(c, buf, 1000)) {
78             printf("EVP_DigestUpdate() error\n");
79             err++;
80             goto err;
81         }
82     }
83     if (!EVP_DigestFinal_ex(c, md, NULL)) {
84             printf("EVP_DigestFinal() error\n");
85             err++;
86             goto err;
87     }
88     p = pt(md);
89
90     r = bigret;
91     if (strcmp(p, r) != 0) {
92         printf("error calculating SHA1 on 'a' * 1000\n");
93         printf("got %s instead of %s\n", p, r);
94         err++;
95     } else
96         printf("test 3 ok\n");
97  err:
98     EVP_MD_CTX_free(c);
99     EXIT(err);
100     return (0);
101 }
102
103 static char *pt(unsigned char *md)
104 {
105     int i;
106     static char buf[80];
107
108     for (i = 0; i < SHA_DIGEST_LENGTH; i++)
109         sprintf(&(buf[i * 2]), "%02x", md[i]);
110     return (buf);
111 }