[squash]Build works with/out NO_ENGINE and NO_AFALG
[openssl.git] / test / sha1test.c
1 /*
2  * Copyright 1995-2017 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
13 #include "../e_os.h"
14 #include "test_main.h"
15 #include "testutil.h"
16
17 #include <openssl/evp.h>
18 #include <openssl/sha.h>
19
20 #ifdef CHARSET_EBCDIC
21 # include <openssl/ebcdic.h>
22 #endif
23
24 static char test[][80] = {
25     { "abc" },
26     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }
27 };
28
29 static char *ret[] = {
30     "a9993e364706816aba3e25717850c26c9cd0d89d",
31     "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
32 };
33
34 static char *bigret = "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
35
36 static char *pt(unsigned char *md)
37 {
38     int i;
39     static char buf[80];
40
41     for (i = 0; i < SHA_DIGEST_LENGTH; i++)
42         sprintf(&(buf[i * 2]), "%02x", md[i]);
43     return buf;
44 }
45
46 static int test_sha1(int i)
47 {
48     EVP_MD_CTX *c;
49     unsigned char md[SHA_DIGEST_LENGTH];
50     const size_t tlen = strlen(test[i]);
51     int testresult = 0;
52
53     c = EVP_MD_CTX_new();
54     if (!TEST_ptr(c))
55         goto end;
56
57 # ifdef CHARSET_EBCDIC
58     ebcdic2ascii(test[i], test[i], tlen);
59 # endif
60     if (!TEST_true(EVP_Digest(test[i], tlen, md, NULL, EVP_sha1(), NULL))
61         || !TEST_str_eq(pt(md), ret[i]))
62         goto end;
63
64     testresult = 1;
65  end:
66     EVP_MD_CTX_free(c);
67     return testresult;
68 }
69
70 static int test_sha1_big(void)
71 {
72     static unsigned char buf[1000];
73     EVP_MD_CTX *c;
74     unsigned char md[SHA_DIGEST_LENGTH];
75     int i, testresult = 0;
76
77     c = EVP_MD_CTX_new();
78     if (!TEST_ptr(c))
79         goto end;
80
81     memset(buf, 'a', 1000);
82 #ifdef CHARSET_EBCDIC
83     ebcdic2ascii(buf, buf, 1000);
84 #endif                         /* CHARSET_EBCDIC */
85     if (!TEST_true(EVP_DigestInit_ex(c, EVP_sha1(), NULL)))
86         goto end;
87     for (i = 0; i < 1000; i++)
88         if (!TEST_true(EVP_DigestUpdate(c, buf, 1000)))
89             goto end;
90     if (!TEST_true(EVP_DigestFinal_ex(c, md, NULL)))
91         goto end;
92
93     if (!TEST_str_eq(pt(md), bigret))
94         goto end;
95
96     testresult = 1;
97  end:
98     EVP_MD_CTX_free(c);
99     return testresult;
100 }
101
102 void register_tests(void)
103 {
104     ADD_ALL_TESTS(test_sha1, OSSL_NELEM(test));
105     ADD_TEST(test_sha1_big);
106 }