Merge branch 'nether' into tizen
[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@samsung.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@samsung.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 Buffer 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 = get_output_length(ctx_ptr);
42
43     YACA_ASSERT_MSG(output_len == expected_bit_len / 8,
44                     "Digest " << digest2str(algo) << " produces " << output_len * 8 <<
45                     " bit digest. Expected " << expected_bit_len << " bits.");
46 }
47
48 void check_test_vector(yaca_digest_algorithm_e algo,
49                        size_t repeats,
50                        const Buffer& input,
51                        const Buffer& expected)
52 {
53     auto ctx_ptr = digest_init(algo);
54     size_t out_len;
55
56     out_len = get_output_length(ctx_ptr);
57
58     /* check the test vector */
59     if (input.size() > 0) {
60         for (size_t i = 0; i < repeats; i++)
61             YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(),
62                                             input.data(),
63                                             input.size()));
64     }
65
66     Buffer output(out_len);
67     YACA_SUCCESS(yaca_digest_finalize(ctx_ptr.get(), output.data(), &out_len));
68     RUNNER_ASSERT_MSG(output.size() >= out_len,
69                       "Length returned from yaca_digest_finalize() (" << out_len <<
70                       ") is greater than the one returned from yaca_context_get_output_length() ("
71                       << output.size() << ")");
72     output.resize(out_len);
73
74     YACA_ASSERT_MSG(output.size() == expected.size(),
75                     "Digest calculated for \""  <<
76                     truncate_str(std::string(input.data(), input.size()), 16)
77                     << "\" with " << digest2str(algo) << " is " << output.size() * 8
78                     <<" bits long. Expected " << expected.size() * 8 << " bits");
79
80     int ret = yaca_memcmp(output.data(), expected.data(), output.size());
81     YACA_ASSERT_MSG(YACA_ERROR_NONE == ret,
82                     "Digest calculated for \"" <<
83                     truncate_str(std::string(input.data(), input.size()), 16)
84                     << "\" with " << digest2str(algo) << " is different than expected");
85 }
86
87 } // anonymous namespace
88
89 RUNNER_TEST_GROUP_INIT(T2000_YACA_DIGEST);
90
91 RUNNER_TEST(T2010_yaca_digest_initialize_invalid_param, YacaTest)
92 {
93     YACA_INVALID_PARAM(yaca_digest_initialize(nullptr, YACA_DIGEST_MD5));
94
95     yaca_context_h ctx;
96     YACA_INVALID_PARAM(yaca_digest_initialize(
97             &ctx,
98             static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_MD5-1)));
99
100     YACA_INVALID_PARAM(yaca_digest_initialize(
101             &ctx,
102             static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_SHA512+1)));
103 }
104
105 RUNNER_TEST(T2020_yaca_digest_update_invalid_param, YacaTest)
106 {
107     YACA_INVALID_PARAM(yaca_digest_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size()));
108
109     {
110         auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
111         auto ctx_ptr = hmac_init(YACA_DIGEST_MD5, key_ptr);
112
113         YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
114     }
115
116     auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
117
118     YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), NULL, 42));
119     YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), DATA.data(), 0));
120 }
121
122 RUNNER_TEST(T2030_yaca_digest_finalize_invalid_param, YacaTest)
123 {
124     char output[256];
125     size_t out_len;
126     YACA_INVALID_PARAM(yaca_digest_finalize(YACA_CONTEXT_NULL, output, &out_len));
127
128     {
129         auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
130         auto ctx_ptr = hmac_init(YACA_DIGEST_MD5, key_ptr);
131
132         YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), output, &out_len));
133     }
134
135     auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
136     YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
137
138     YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), nullptr, &out_len));
139     YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), output, nullptr));
140 }
141
142 RUNNER_TEST(T2040_yaca_digest_get_output_length_invalid_param, YacaTest)
143 {
144     auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
145
146     YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 0, nullptr));
147
148     size_t out_len;
149     YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 42, &out_len));
150 }
151
152 RUNNER_TEST(T2045_yaca_digest_call_order_invalid_param)
153 {
154     char output[512];
155     size_t out_len;
156     CtxPtr ctx = digest_init(YACA_DIGEST_SHA512);
157
158     YACA_SUCCESS(yaca_digest_update(ctx.get(), DATA.data(), DATA.size()));
159     YACA_SUCCESS(yaca_digest_finalize(ctx.get(), output, &out_len));
160
161     YACA_INVALID_PARAM(yaca_digest_update(ctx.get(), DATA.data(), DATA.size()));
162 }
163
164 RUNNER_TEST(T2050_yaca_digest_get_output_length, YacaTest)
165 {
166     digest_length_test(YACA_DIGEST_MD5,    128);
167     digest_length_test(YACA_DIGEST_SHA1,   160);
168     digest_length_test(YACA_DIGEST_SHA224, 224);
169     digest_length_test(YACA_DIGEST_SHA256, 256);
170     digest_length_test(YACA_DIGEST_SHA384, 384);
171     digest_length_test(YACA_DIGEST_SHA512, 512);
172 }
173
174 RUNNER_TEST(T2060_yaca_digest_test_ascii_vectors, YacaTest)
175 {
176     auto tvv = loadTestVector("digest_ascii.txt");
177
178     for (const auto& tv : tvv) {
179         yaca_digest_algorithm_e algo;
180         size_t repeats;
181         std::string input;
182         Buffer expected;
183
184         tv.get("algo", algo);
185         tv.get("repeats", repeats);
186         tv.get("input", input);
187         tv.get("output", expected);
188
189         check_test_vector(algo, repeats, Buffer(input.begin(), input.end()), expected);
190     }
191 }
192
193 RUNNER_TEST(T2070_yaca_digest_test_binary_vectors, YacaTest)
194 {
195     auto tvv = loadTestVector("digest_binary.txt");
196
197     for (const auto& tv : tvv) {
198         yaca_digest_algorithm_e algo;
199         size_t repeats;
200         Buffer input;
201         Buffer expected;
202
203         tv.get("algo", algo);
204         tv.get("repeats", repeats);
205         tv.get("input", input);
206         tv.get("output", expected);
207
208         check_test_vector(algo, repeats, input, expected);
209     }
210 }