2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Dariusz Michaluk (d.michaluk@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @author Dariusz Michaluk (d.michaluk@samsung.com)
27 #include "dpl/test/test_runner.h"
28 #include "yaca-test-common.h"
29 #include "yaca-test-vector.h"
31 #include <yaca_digest.h>
36 const Buffer DATA = random_buffer(1024);
38 void digest_length_test(yaca_digest_algorithm_e algo, size_t expected_bit_len)
40 auto ctx_ptr = digest_init(algo);
41 size_t output_len = get_output_length(ctx_ptr);
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.");
48 void check_test_vector(yaca_digest_algorithm_e algo,
51 const Buffer& expected)
53 auto ctx_ptr = digest_init(algo);
56 out_len = get_output_length(ctx_ptr);
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(),
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);
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");
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");
87 } // anonymous namespace
89 RUNNER_TEST_GROUP_INIT(T2000_YACA_DIGEST);
91 RUNNER_TEST(T2010_yaca_digest_initialize_invalid_param, YacaTest)
93 YACA_INVALID_PARAM(yaca_digest_initialize(nullptr, YACA_DIGEST_MD5));
96 YACA_INVALID_PARAM(yaca_digest_initialize(
98 static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_MD5-1)));
100 YACA_INVALID_PARAM(yaca_digest_initialize(
102 static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_SHA512+1)));
105 RUNNER_TEST(T2020_yaca_digest_update_invalid_param, YacaTest)
107 YACA_INVALID_PARAM(yaca_digest_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size()));
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);
113 YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
116 auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
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));
122 RUNNER_TEST(T2030_yaca_digest_finalize_invalid_param, YacaTest)
126 YACA_INVALID_PARAM(yaca_digest_finalize(YACA_CONTEXT_NULL, output, &out_len));
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);
132 YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), output, &out_len));
135 auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
136 YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
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));
142 RUNNER_TEST(T2040_yaca_digest_get_output_length_invalid_param, YacaTest)
144 auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
146 YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 0, nullptr));
149 YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 42, &out_len));
152 RUNNER_TEST(T2045_yaca_digest_call_order_invalid_param)
156 CtxPtr ctx = digest_init(YACA_DIGEST_SHA512);
158 YACA_SUCCESS(yaca_digest_update(ctx.get(), DATA.data(), DATA.size()));
159 YACA_SUCCESS(yaca_digest_finalize(ctx.get(), output, &out_len));
161 YACA_INVALID_PARAM(yaca_digest_update(ctx.get(), DATA.data(), DATA.size()));
164 RUNNER_TEST(T2050_yaca_digest_get_output_length, YacaTest)
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);
174 RUNNER_TEST(T2060_yaca_digest_test_ascii_vectors, YacaTest)
176 auto tvv = loadTestVector("digest_ascii.txt");
178 for (const auto& tv : tvv) {
179 yaca_digest_algorithm_e algo;
184 tv.get("algo", algo);
185 tv.get("repeats", repeats);
186 tv.get("input", input);
187 tv.get("output", expected);
189 check_test_vector(algo, repeats, Buffer(input.begin(), input.end()), expected);
193 RUNNER_TEST(T2070_yaca_digest_test_binary_vectors, YacaTest)
195 auto tvv = loadTestVector("digest_binary.txt");
197 for (const auto& tv : tvv) {
198 yaca_digest_algorithm_e algo;
203 tv.get("algo", algo);
204 tv.get("repeats", repeats);
205 tv.get("input", input);
206 tv.get("output", expected);
208 check_test_vector(algo, repeats, input, expected);