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