Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / yaca / yaca-test-vector.cpp
1 /*
2  *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       yaca-test-vector.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <fstream>
23
24 #include <yaca_types.h>
25
26 #include <dpl/test/test_runner.h>
27
28 #include "yaca-test-vector.h"
29
30 namespace {
31
32 Buffer hex2bin(const std::string &hex_str)
33 {
34     RUNNER_ASSERT_MSG(hex_str.size() % 2 == 0,
35                       "Length of the provided string is not divisable by 2");
36
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);
41         std::stringstream ss;
42         int byte;
43
44         /* Will throw if it can't handle it and that's fine */
45         ss << std::hex << byte_str;
46         ss >> byte;
47
48         RUNNER_ASSERT_MSG(byte == (byte & 0xff), "Byte value out of range");
49
50         data[i / 2] = static_cast<char>(byte);
51     }
52
53     return data;
54 }
55
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)
59 };
60
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)
66 };
67
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)
83 };
84
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)
92 };
93
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)
102 };
103
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)
116 };
117
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)
121 };
122
123 } // anonymous namespace
124
125 void TestVector::add(const std::string &key, const std::string &val)
126 {
127     RUNNER_ASSERT_MSG(m_data.find(key) == m_data.end(),
128                       "The key " << key << " already exists in this test vector");
129     m_data[key] = val;
130 }
131
132 void TestVector::get(const std::string &key, std::string &val) const
133 {
134     val = value(key);
135 }
136
137 void TestVector::get(const std::string &key, Buffer &val) const
138 {
139     val = hex2bin(value(key));
140 }
141
142 void TestVector::get(const std::string &key, yaca_key_format_e &val) const
143 {
144     val = str2keyformat.at(value(key));
145 }
146
147 void TestVector::get(const std::string &key, yaca_key_file_format_e &val) const
148 {
149     val = str2keyfileformat.at(value(key));
150 }
151
152 void TestVector::get(const std::string &key, yaca_key_type_e &val) const
153 {
154     val = str2keytype.at(value(key));
155 }
156
157 void TestVector::get(const std::string &key, yaca_digest_algorithm_e &val) const
158 {
159     val = str2digest.at(value(key));
160 }
161
162 void TestVector::get(const std::string &key, yaca_encrypt_algorithm_e &val) const
163 {
164     val = str2encrypt.at(value(key));
165 }
166
167 void TestVector::get(const std::string &key, yaca_block_cipher_mode_e &val) const
168 {
169     val = str2bcm.at(value(key));
170 }
171
172 void TestVector::get(const std::string &key, yaca_kdf_e &val) const
173 {
174     val = str2kdf.at(value(key));
175 }
176
177 void TestVector::reset()
178 {
179     m_data.clear();
180 }
181
182 bool TestVector::empty() const
183 {
184     return m_data.empty();
185 }
186
187 std::string TestVector::value(const std::string &key) const
188 {
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");
192     return it->second;
193 }
194
195 TestVectorVector loadTestVector(const std::string &filename)
196 {
197     std::string path_local = std::string("./test-vectors/") + filename;
198     std::string path_shared = std::string(YACA_TEST_DIR"/test-vectors/") + filename;
199
200     TestVectorVector tvv;
201
202     std::ifstream ifs(path_local, std::ifstream::in);
203     if (!ifs)
204         ifs.open(path_shared, std::ifstream::in);
205
206     RUNNER_ASSERT_MSG(ifs, "Failed to open " << filename);
207     std::string line;
208     TestVector tv;
209     while (std::getline(ifs, line)) {
210         /* truncate blank chars from the beginning of the line */
211         size_t i;
212         for (i = 0; i < line.size(); ++i)
213             if (!isblank(line[i]))
214                 break;
215         line = line.substr(i);
216
217         if (line.empty()) {
218             /* next test vector */
219             if (!tv.empty()) {
220                 tvv.push_back(tv);
221                 tv.reset();
222             }
223         } else {
224             size_t equals = line.find('=');
225             bool comment = (line[0] == '#');
226
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);
231                 tv.add(key, value);
232             }
233         }
234
235         if (ifs.eof())
236             break;
237     }
238     RUNNER_ASSERT_MSG(ifs.eof(), "Failed to read " << filename);
239     if (!tv.empty())
240         tvv.push_back(tv);
241     return tvv;
242 }