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