1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/sha.h>
12*e0c4386eSCy Schubert #include "testutil.h"
13*e0c4386eSCy Schubert
test_static_sha_common(const char * input,size_t length,const unsigned char * out,unsigned char * (* md)(const unsigned char * d,size_t n,unsigned char * md))14*e0c4386eSCy Schubert static int test_static_sha_common(const char *input, size_t length,
15*e0c4386eSCy Schubert const unsigned char *out,
16*e0c4386eSCy Schubert unsigned char *(*md)(const unsigned char *d,
17*e0c4386eSCy Schubert size_t n,
18*e0c4386eSCy Schubert unsigned char *md))
19*e0c4386eSCy Schubert {
20*e0c4386eSCy Schubert unsigned char buf[EVP_MAX_MD_SIZE], *sbuf;
21*e0c4386eSCy Schubert const unsigned char *in = (unsigned char *)input;
22*e0c4386eSCy Schubert const size_t in_len = strlen(input);
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert sbuf = (*md)(in, in_len, buf);
25*e0c4386eSCy Schubert if (!TEST_ptr(sbuf)
26*e0c4386eSCy Schubert || !TEST_ptr_eq(sbuf, buf)
27*e0c4386eSCy Schubert || !TEST_mem_eq(sbuf, length, out, length))
28*e0c4386eSCy Schubert return 0;
29*e0c4386eSCy Schubert sbuf = (*md)(in, in_len, NULL);
30*e0c4386eSCy Schubert if (!TEST_ptr(sbuf)
31*e0c4386eSCy Schubert || !TEST_ptr_ne(sbuf, buf)
32*e0c4386eSCy Schubert || !TEST_mem_eq(sbuf, length, out, length))
33*e0c4386eSCy Schubert return 0;
34*e0c4386eSCy Schubert return 1;
35*e0c4386eSCy Schubert }
36*e0c4386eSCy Schubert
test_static_sha1(void)37*e0c4386eSCy Schubert static int test_static_sha1(void)
38*e0c4386eSCy Schubert {
39*e0c4386eSCy Schubert static const unsigned char output[SHA_DIGEST_LENGTH] = {
40*e0c4386eSCy Schubert 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
41*e0c4386eSCy Schubert 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
42*e0c4386eSCy Schubert 0x9c, 0xd0, 0xd8, 0x9d
43*e0c4386eSCy Schubert };
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert return test_static_sha_common("abc", SHA_DIGEST_LENGTH, output, &SHA1);
46*e0c4386eSCy Schubert }
47*e0c4386eSCy Schubert
test_static_sha224(void)48*e0c4386eSCy Schubert static int test_static_sha224(void)
49*e0c4386eSCy Schubert {
50*e0c4386eSCy Schubert static const unsigned char output[SHA224_DIGEST_LENGTH] = {
51*e0c4386eSCy Schubert 0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
52*e0c4386eSCy Schubert 0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
53*e0c4386eSCy Schubert 0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
54*e0c4386eSCy Schubert 0xe3, 0x6c, 0x9d, 0xa7
55*e0c4386eSCy Schubert };
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert return test_static_sha_common("abc", SHA224_DIGEST_LENGTH, output, &SHA224);
58*e0c4386eSCy Schubert }
59*e0c4386eSCy Schubert
test_static_sha256(void)60*e0c4386eSCy Schubert static int test_static_sha256(void)
61*e0c4386eSCy Schubert {
62*e0c4386eSCy Schubert static const unsigned char output[SHA256_DIGEST_LENGTH] = {
63*e0c4386eSCy Schubert 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
64*e0c4386eSCy Schubert 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
65*e0c4386eSCy Schubert 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
66*e0c4386eSCy Schubert 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
67*e0c4386eSCy Schubert };
68*e0c4386eSCy Schubert
69*e0c4386eSCy Schubert return test_static_sha_common("abc", SHA256_DIGEST_LENGTH, output, &SHA256);
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert
test_static_sha384(void)72*e0c4386eSCy Schubert static int test_static_sha384(void)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert static const unsigned char output[SHA384_DIGEST_LENGTH] = {
75*e0c4386eSCy Schubert 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
76*e0c4386eSCy Schubert 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
77*e0c4386eSCy Schubert 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
78*e0c4386eSCy Schubert 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
79*e0c4386eSCy Schubert 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
80*e0c4386eSCy Schubert 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
81*e0c4386eSCy Schubert };
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert return test_static_sha_common("abc", SHA384_DIGEST_LENGTH, output, &SHA384);
84*e0c4386eSCy Schubert }
85*e0c4386eSCy Schubert
test_static_sha512(void)86*e0c4386eSCy Schubert static int test_static_sha512(void)
87*e0c4386eSCy Schubert {
88*e0c4386eSCy Schubert static const unsigned char output[SHA512_DIGEST_LENGTH] = {
89*e0c4386eSCy Schubert 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
90*e0c4386eSCy Schubert 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
91*e0c4386eSCy Schubert 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
92*e0c4386eSCy Schubert 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
93*e0c4386eSCy Schubert 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
94*e0c4386eSCy Schubert 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
95*e0c4386eSCy Schubert 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
96*e0c4386eSCy Schubert 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
97*e0c4386eSCy Schubert };
98*e0c4386eSCy Schubert
99*e0c4386eSCy Schubert return test_static_sha_common("abc", SHA512_DIGEST_LENGTH, output, &SHA512);
100*e0c4386eSCy Schubert }
101*e0c4386eSCy Schubert
setup_tests(void)102*e0c4386eSCy Schubert int setup_tests(void)
103*e0c4386eSCy Schubert {
104*e0c4386eSCy Schubert ADD_TEST(test_static_sha1);
105*e0c4386eSCy Schubert ADD_TEST(test_static_sha224);
106*e0c4386eSCy Schubert ADD_TEST(test_static_sha256);
107*e0c4386eSCy Schubert ADD_TEST(test_static_sha384);
108*e0c4386eSCy Schubert ADD_TEST(test_static_sha512);
109*e0c4386eSCy Schubert return 1;
110*e0c4386eSCy Schubert }
111