2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file yaca-test-vector.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
24 #include <yaca_types.h>
26 #include <dpl/test/test_runner.h>
28 #include "yaca-test-vector.h"
32 Buffer hex2bin(const std::string &hex_str)
34 RUNNER_ASSERT_MSG(hex_str.size() % 2 == 0,
35 "Length of the provided string is not divisable by 2");
37 /* convert a string representing hex to an array of bytes */
38 Buffer data(hex_str.size() / 2);
39 for (size_t i = 0; i < hex_str.size(); i += 2) {
40 std::string byte_str = hex_str.substr(i, 2);
44 /* Will throw if it can't handle it and that's fine */
45 ss << std::hex << byte_str;
48 RUNNER_ASSERT_MSG(byte == (byte & 0xff), "Byte value out of range");
50 data[i / 2] = static_cast<char>(byte);
56 std::unordered_map<std::string, yaca_key_format_e> str2keyformat = {
57 std::make_pair("DEFAULT", YACA_KEY_FORMAT_DEFAULT),
58 std::make_pair("PKCS8", YACA_KEY_FORMAT_PKCS8)
61 std::unordered_map<std::string, yaca_key_file_format_e> str2keyfileformat = {
62 std::make_pair("RAW", YACA_KEY_FILE_FORMAT_RAW),
63 std::make_pair("BASE64", YACA_KEY_FILE_FORMAT_BASE64),
64 std::make_pair("PEM", YACA_KEY_FILE_FORMAT_PEM),
65 std::make_pair("DER", YACA_KEY_FILE_FORMAT_DER)
68 std::unordered_map<std::string, yaca_key_type_e> str2keytype = {
69 std::make_pair("SYMMETRIC", YACA_KEY_TYPE_SYMMETRIC),
70 std::make_pair("DES", YACA_KEY_TYPE_DES),
71 std::make_pair("IV", YACA_KEY_TYPE_IV),
72 std::make_pair("RSA_PRIV", YACA_KEY_TYPE_RSA_PRIV),
73 std::make_pair("RSA_PUB", YACA_KEY_TYPE_RSA_PUB),
74 std::make_pair("DSA_PRIV", YACA_KEY_TYPE_DSA_PRIV),
75 std::make_pair("DSA_PUB", YACA_KEY_TYPE_DSA_PUB),
76 std::make_pair("DSA_PARAMS", YACA_KEY_TYPE_DSA_PARAMS),
77 std::make_pair("DH_PRIV", YACA_KEY_TYPE_DH_PRIV),
78 std::make_pair("DH_PUB", YACA_KEY_TYPE_DH_PUB),
79 std::make_pair("DH_PARAMS", YACA_KEY_TYPE_DH_PARAMS),
80 std::make_pair("EC_PRIV", YACA_KEY_TYPE_EC_PRIV),
81 std::make_pair("EC_PUB", YACA_KEY_TYPE_EC_PUB),
82 std::make_pair("EC_PARAMS", YACA_KEY_TYPE_EC_PARAMS)
85 std::unordered_map<std::string, yaca_digest_algorithm_e> str2digest = {
86 std::make_pair("MD5", YACA_DIGEST_MD5),
87 std::make_pair("SHA1", YACA_DIGEST_SHA1),
88 std::make_pair("SHA224", YACA_DIGEST_SHA224),
89 std::make_pair("SHA256", YACA_DIGEST_SHA256),
90 std::make_pair("SHA384", YACA_DIGEST_SHA384),
91 std::make_pair("SHA512", YACA_DIGEST_SHA512)
94 std::unordered_map<std::string, yaca_encrypt_algorithm_e> str2encrypt = {
95 std::make_pair("AES", YACA_ENCRYPT_AES),
96 std::make_pair("DES", YACA_ENCRYPT_UNSAFE_DES),
97 std::make_pair("3DES_2TDEA", YACA_ENCRYPT_UNSAFE_3DES_2TDEA),
98 std::make_pair("3DES_3TDEA", YACA_ENCRYPT_3DES_3TDEA),
99 std::make_pair("RC2", YACA_ENCRYPT_UNSAFE_RC2),
100 std::make_pair("RC4", YACA_ENCRYPT_UNSAFE_RC4),
101 std::make_pair("CAST5", YACA_ENCRYPT_CAST5)
104 std::unordered_map<std::string, yaca_block_cipher_mode_e> str2bcm = {
105 std::make_pair("NONE", YACA_BCM_NONE),
106 std::make_pair("ECB", YACA_BCM_ECB),
107 std::make_pair("CTR", YACA_BCM_CTR),
108 std::make_pair("CBC", YACA_BCM_CBC),
109 std::make_pair("GCM", YACA_BCM_GCM),
110 std::make_pair("CFB", YACA_BCM_CFB),
111 std::make_pair("CFB1", YACA_BCM_CFB1),
112 std::make_pair("CFB8", YACA_BCM_CFB8),
113 std::make_pair("OFB", YACA_BCM_OFB),
114 std::make_pair("CCM", YACA_BCM_CCM),
115 std::make_pair("WRAP", YACA_BCM_WRAP)
118 std::unordered_map<std::string, yaca_kdf_e> str2kdf = {
119 std::make_pair("X942", YACA_KDF_X942),
120 std::make_pair("X962", YACA_KDF_X962)
123 } // anonymous namespace
125 void TestVector::add(const std::string &key, const std::string &val)
127 RUNNER_ASSERT_MSG(m_data.find(key) == m_data.end(),
128 "The key " << key << " already exists in this test vector");
132 void TestVector::get(const std::string &key, std::string &val) const
137 void TestVector::get(const std::string &key, Buffer &val) const
139 val = hex2bin(value(key));
142 void TestVector::get(const std::string &key, yaca_key_format_e &val) const
144 val = str2keyformat.at(value(key));
147 void TestVector::get(const std::string &key, yaca_key_file_format_e &val) const
149 val = str2keyfileformat.at(value(key));
152 void TestVector::get(const std::string &key, yaca_key_type_e &val) const
154 val = str2keytype.at(value(key));
157 void TestVector::get(const std::string &key, yaca_digest_algorithm_e &val) const
159 val = str2digest.at(value(key));
162 void TestVector::get(const std::string &key, yaca_encrypt_algorithm_e &val) const
164 val = str2encrypt.at(value(key));
167 void TestVector::get(const std::string &key, yaca_block_cipher_mode_e &val) const
169 val = str2bcm.at(value(key));
172 void TestVector::get(const std::string &key, yaca_kdf_e &val) const
174 val = str2kdf.at(value(key));
177 void TestVector::reset()
182 bool TestVector::empty() const
184 return m_data.empty();
187 std::string TestVector::value(const std::string &key) const
189 auto it = m_data.find(key);
190 RUNNER_ASSERT_MSG(it != m_data.end(),
191 "No entry for key \"" << key << "\" in current test vector");
195 TestVectorVector loadTestVector(const std::string &filename)
197 std::string path_local = std::string("./test-vectors/") + filename;
198 std::string path_shared = std::string(YACA_TEST_DIR"/test-vectors/") + filename;
200 TestVectorVector tvv;
202 std::ifstream ifs(path_local, std::ifstream::in);
204 ifs.open(path_shared, std::ifstream::in);
206 RUNNER_ASSERT_MSG(ifs, "Failed to open " << filename);
209 while (std::getline(ifs, line)) {
210 /* truncate blank chars from the beginning of the line */
212 for (i = 0; i < line.size(); ++i)
213 if (!isblank(line[i]))
215 line = line.substr(i);
218 /* next test vector */
224 size_t equals = line.find('=');
225 bool comment = (line[0] == '#');
227 /* ignore lines without '=' */
228 if (!comment && equals != std::string::npos) {
229 std::string key = line.substr(0, equals);
230 std::string value = line.substr(equals + 1);
238 RUNNER_ASSERT_MSG(ifs.eof(), "Failed to read " << filename);