2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Dariusz Michaluk (d.michaluk@samsumg.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@samsumg.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 vector<char> 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);
43 YACA_SUCCESS(yaca_context_get_output_length(ctx_ptr.get(), 0, &output_len));
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.");
50 } // anonymous namespace
52 RUNNER_TEST_GROUP_INIT(T2000_YACA_DIGEST);
54 RUNNER_TEST(T2010_yaca_digest_initialize_invalid_param, YacaTest)
56 YACA_INVALID_PARAM(yaca_digest_initialize(nullptr, YACA_DIGEST_MD5));
59 YACA_INVALID_PARAM(yaca_digest_initialize(
61 static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_MD5-1)));
63 YACA_INVALID_PARAM(yaca_digest_initialize(
65 static_cast<yaca_digest_algorithm_e>(YACA_DIGEST_SHA512+1)));
68 RUNNER_TEST(T2020_yaca_digest_update_invalid_param, YacaTest)
70 YACA_INVALID_PARAM(yaca_digest_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size()));
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);
76 YACA_INVALID_PARAM(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
79 auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
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));
85 RUNNER_TEST(T2030_yaca_digest_finalize_invalid_param, YacaTest)
89 YACA_INVALID_PARAM(yaca_digest_finalize(YACA_CONTEXT_NULL, output, &out_len));
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);
95 YACA_INVALID_PARAM(yaca_digest_finalize(ctx_ptr.get(), output, &out_len));
98 auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
99 YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(), DATA.data(), DATA.size()));
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));
105 RUNNER_TEST(T2040_yaca_digest_get_output_length_invalid_param, YacaTest)
107 auto ctx_ptr = digest_init(YACA_DIGEST_MD5);
109 YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 0, nullptr));
112 YACA_INVALID_PARAM(yaca_context_get_output_length(ctx_ptr.get(), 42, &out_len));
115 RUNNER_TEST(T2050_yaca_digest_get_output_length, YacaTest)
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);
125 RUNNER_TEST(T2060_yaca_digest_test_vectors, YacaTest)
127 auto tvv = loadTestVector("digest_ascii.txt");
129 for (const auto& tv : tvv) {
130 yaca_digest_algorithm_e algo;
135 tv.get("algo", algo);
136 tv.get("repeats", repeats);
137 tv.get("input", input);
138 tv.get("output", expected);
140 auto ctx_ptr = digest_init(algo);
143 out_len = get_output_length(ctx_ptr);
145 if (input.size() > 0) {
146 for (size_t i = 0; i < repeats; i++)
147 YACA_SUCCESS(yaca_digest_update(ctx_ptr.get(),
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);
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");
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");