YACA: Digest ASCII test vectors
[platform/core/test/security-tests.git] / src / yaca / yaca-test-digest.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Dariusz Michaluk (d.michaluk@samsumg.com)
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author Dariusz Michaluk (d.michaluk@samsumg.com)
22  */
23
24 #include <map>
25 #include <vector>
26
27 #include "dpl/test/test_runner.h"
28 #include "yaca-test-common.h"
29 #include "yaca-test-vector.h"
30
31 #include <yaca_digest.h>
32
33 using namespace std;
34
35 namespace {
36 const vector<char> DATA = random_buffer(1024);
37
38 void digest_length_test(yaca_digest_algorithm_e algo, size_t expected_bit_len)
39 {
40     auto ctx_ptr = digest_init(algo);
41     size_t output_len;
42
43     YACA_SUCCESS(yaca_context_get_output_length(ctx_ptr.get(), 0, &output_len));
44
45     YACA_ASSERT_MSG(output_len == expected_bit_len / 8,
46                     "Digest " << digest2str(algo) << " produces " << output_len * 8 <<
47                     " bit digest. Expected " << expected_bit_len << " bits.");
48 }
49
50 } // anonymous namespace
51
52 RUNNER_TEST_GROUP_INIT(T2000_YACA_DIGEST);
53
54 RUNNER_TEST(T2010_yaca_digest_initialize_invalid_param, YacaTest)
55 {
56     YACA_INVALID_PARAM(yaca_digest_initialize(nullptr, YACA_DIGEST_MD5));
57
58     yaca_context_h ctx;
59     YACA_INVALID_PARAM(yaca_digest_initialize(
60             &ctx,
61             static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_MD5-1)));
62
63     YACA_INVALID_PARAM(yaca_digest_initialize(
64             &ctx,
65             static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_SHA512+1)));
66 }
67
68 RUNNER_TEST(T2020_yaca_digest_update_invalid_param, YacaTest)
69 {
70     YACA_INVALID_PARAM(yaca_digest_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size()));
71
72     {
73         auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
74         auto ctx_ptr = hmac_init(YACA_DIGEST_MD5, key_ptr);
75
76         YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
77     }
78
79     auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
80
81     YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), NULL, 42));
82     YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), DATA.data(), 0));
83 }
84
85 RUNNER_TEST(T2030_yaca_digest_finalize_invalid_param, YacaTest)
86 {
87     char output[256];
88     size_t out_len;
89     YACA_INVALID_PARAM(yaca_digest_finalize(YACA_CONTEXT_NULL, output, &out_len));
90
91     {
92         auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
93         auto ctx_ptr = hmac_init(YACA_DIGEST_MD5, key_ptr);
94
95         YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), output, &out_len));
96     }
97
98     auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
99     YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
100
101     YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), nullptr, &out_len));
102     YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), output, nullptr));
103 }
104
105 RUNNER_TEST(T2040_yaca_digest_get_output_length_invalid_param, YacaTest)
106 {
107     auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
108
109     YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 0, nullptr));
110
111     size_t out_len;
112     YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 42, &out_len));
113 }
114
115 RUNNER_TEST(T2050_yaca_digest_get_output_length, YacaTest)
116 {
117     digest_length_test(YACA_DIGEST_MD5,    128);
118     digest_length_test(YACA_DIGEST_SHA1,   160);
119     digest_length_test(YACA_DIGEST_SHA224, 224);
120     digest_length_test(YACA_DIGEST_SHA256, 256);
121     digest_length_test(YACA_DIGEST_SHA384, 384);
122     digest_length_test(YACA_DIGEST_SHA512, 512);
123 }
124
125 RUNNER_TEST(T2060_yaca_digest_test_vectors, YacaTest)
126 {
127     auto tvv = loadTestVector("digest_ascii.txt");
128
129     for (const auto& tv : tvv) {
130         yaca_digest_algorithm_e algo;
131         size_t repeats;
132         std::string input;
133         Buffer expected;
134
135         tv.get("algo", algo);
136         tv.get("repeats", repeats);
137         tv.get("input", input);
138         tv.get("output", expected);
139
140         auto ctx_ptr = digest_init(algo);
141         size_t out_len;
142
143         out_len = get_output_length(ctx_ptr);
144
145         if (input.size() > 0) {
146             for (size_t i = 0; i < repeats; i++)
147                 YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(),
148                                                 input.c_str(),
149                                                 input.size()));
150         }
151
152         Buffer output(out_len);
153         YACA_SUCCESS(yaca_digest_finalize(ctx_ptr.get(), output.data(), &out_len));
154         RUNNER_ASSERT_MSG(output.size() >= out_len,
155                           "Length returned from yaca_digest_finalize() (" << out_len <<
156                           ") is greater than the one returned from yaca_context_get_output_length() ("
157                           << output.size() << ")");
158         output.resize(out_len);
159
160         YACA_ASSERT_MSG(output.size() == expected.size(),
161                         " Digest calculated for \""  << truncate_str(input, 16) << "\" with "
162                         << digest2str(algo) << " is " << output.size() * 8
163                         <<" bits long. Expected " << expected.size() * 8 << " bits");
164
165         int ret = yaca_memcmp(output.data(), expected.data(), output.size());
166         YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
167                         "Digest calculated for \"" << truncate_str(input, 16) << "\" with "
168                         << digest2str(algo) << " is different than expected");
169     }
170 }