blob: 0b900ed8206200d2fe38ff6bab8dd0e9c2a5a3f6 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu04020922014-01-22 12:46:53 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Yingdi Yu04020922014-01-22 12:46:53 -080020 */
21
Yingdi Yuf56c68f2014-04-24 21:50:13 -070022#include "security/sec-tpm-file.hpp"
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080023#include "security/key-chain.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070024#include "security/cryptopp.hpp"
Yingdi Yu04020922014-01-22 12:46:53 -080025
Yingdi Yuf56c68f2014-04-24 21:50:13 -070026#include "util/time.hpp"
27
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include <boost/lexical_cast.hpp>
29
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030#include "boost-test.hpp"
31
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032namespace ndn {
Yingdi Yu04020922014-01-22 12:46:53 -080033
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070034BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmFile)
Yingdi Yu04020922014-01-22 12:46:53 -080035
Yingdi Yu7036ce22014-06-19 18:53:37 -070036BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080037{
38 SecTpmFile tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070039
Yingdi Yu5e96e002014-04-23 18:32:15 -070040 Name keyName("/TestSecTpmFile/Delete/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070041 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
42 RsaKeyParams params(2048);
43 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070044
Yingdi Yu28fd32f2014-01-28 19:03:03 -080045 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
46 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070047
Yingdi Yu28fd32f2014-01-28 19:03:03 -080048 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070049
Yingdi Yu28fd32f2014-01-28 19:03:03 -080050 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
51 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
52}
53
Yingdi Yu7036ce22014-06-19 18:53:37 -070054BOOST_AUTO_TEST_CASE(SignVerify)
Yingdi Yu04020922014-01-22 12:46:53 -080055{
56 SecTpmFile tpm;
57
Yingdi Yu5e96e002014-04-23 18:32:15 -070058 Name keyName("/TestSecTpmFile/SignVerify/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070059 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
60 RsaKeyParams params(2048);
61 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070062
Yingdi Yu04020922014-01-22 12:46:53 -080063 Data data("/tmp/test/1");
64 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
65
Yingdi Yu2e57a582014-02-20 23:34:43 -080066 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070067 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
68 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu7036ce22014-06-19 18:53:37 -070069 shared_ptr<PublicKey> publicKey;
70 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu04020922014-01-22 12:46:53 -080071
Yingdi Yu2e57a582014-02-20 23:34:43 -080072 try
73 {
74 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070075
Yingdi Yu7036ce22014-06-19 18:53:37 -070076 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -080077 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -070078 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
79 rsaPublicKey.Load(queue);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070080
Yingdi Yu7036ce22014-06-19 18:53:37 -070081 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
Yingdi Yu2e57a582014-02-20 23:34:43 -080082 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -070083 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070084
Yingdi Yu2e57a582014-02-20 23:34:43 -080085 BOOST_CHECK_EQUAL(result, true);
86 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070087 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080088 {
89 BOOST_CHECK(false);
90 }
Yingdi Yu04020922014-01-22 12:46:53 -080091
Yingdi Yu28fd32f2014-01-28 19:03:03 -080092 tpm.deleteKeyPairInTpm(keyName);
Yingdi Yu04020922014-01-22 12:46:53 -080093}
94
Yingdi Yu7036ce22014-06-19 18:53:37 -070095BOOST_AUTO_TEST_CASE(RandomGenerator)
Yingdi Yu4b752752014-02-18 12:24:03 -080096{
97 SecTpmFile tpm;
98
99 size_t scale = 1000;
100 size_t size = 256 * scale;
101 uint8_t* block = new uint8_t[size];
102 tpm.generateRandomBlock(block, size);
103
Yingdi Yu7036ce22014-06-19 18:53:37 -0700104 std::map<uint8_t, int> counter;
105 for (size_t i = 0; i < size; i++)
106 {
107 counter[block[i]] += 1;
108 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800109
110 float dev = 0.0;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700111 for (size_t i = 0; i != 255; i++)
112 {
113 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
114 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800115
116 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
117
118}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800119
Yingdi Yu7036ce22014-06-19 18:53:37 -0700120BOOST_AUTO_TEST_CASE(ImportExportKey)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800121{
122 using namespace CryptoPP;
123
Yingdi Yu7036ce22014-06-19 18:53:37 -0700124 std::string imported =
125"MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIIJzwsbTIVqgCAggAMBQGCCqG"
126"SIb3DQMHBAiEsSKdgGkCEQSCBMjaYsEeHM/EPzGNDAPaSxE1ASKxjyNkXEVPQbtGx/ju1PlQ"
127"QakTCzPpia8ziVGihrVfuacHrxxbLh+ufNZtbVVsv9kGQL2g15BN2Nic8VqyTxplKrduFkEb"
128"eOipqu627gb6eTsrSj7kubXfs/w9OAdhRZFaEc7I/S6rcPUaDai4i3O03VuvuTOrzZL/unhD"
129"CDzOa3pr2aUQlO/5Pck6GbSYL1/4aW/fB7djZrZAjsGPTqIYpPa16oWlrzsILQ4650Zlvdn8"
130"feh6elTrA4qy6RljZfhIHxzGAeuGbVVLT7zavs01y+QE88sxPpeZ0RGtlVxCYp+xoB5hOO/x"
131"Xm3n0r+HVIaBV9rHLuUSUiHVAtzwuOj/XKh7YBNXeF58lcv8Npsx10etrPuV5hRQelYirKo1"
132"QbT+Aq7sLdWI09eGClDHjvRgw54lCFEUAVUFLxhBnzfVUfw/UiR2SheixH+c7cY3gOOvSMQh"
133"1oL9Omii4S6yhzfbD3hRN/wBKC0NgiIOLRR+Ti0mEEbTxYQrVcHC9rpRcnr1fFAiQvOXrOw0"
134"HyxsbnOeYpoURgLJmUyItPi09xUMCHwUPZ/sI6aG5eRqVUGgcYaQib7MBbGG/D2PlWaKZx1w"
135"iEYtYUI7rrc7Qc2ltp4i1u46ZLtSOxUi8kM7qK2Yp1AOtcWFlGn0XBXpGsHanZRzs8KzfDjQ"
136"OPmRIuFXFmuJweqUGmg6c8P3wHYueenB4oowYELYaDwmmV96obXMG5hbsKne5kcem875d6K+"
137"hL2QzjAl+na4tUZyXYXDdUO2lgTT8IItujBn6c+b8vDcZ24NcYyWPHHb16J4uvKi/6Zb245g"
138"/N0NC6hB43KE+2F0BH0eyXRzCnen6AjF8nvx/wgYrXsFlsU47P8gaL1e7V6QECkY4vIk6//b"
139"LINmpcgZoAv23fgjvYYTMfS/IyPsooS5DJarbHzbsgAWOuqP2ewYulrKqLi0vaUyoErRedQC"
140"8J5p9c4KF56++ameiwxbyKnDynHIIFCOM8eamLnDH4VvU9E2mtfZxmi8kWDWx/NhKEK9zbQv"
141"WuaGDpOysgPK4myTp2QGQNQDvBB92gMcrVTcY//3OGHSXeJrkUfDKLOUDyz7ROghEt1vpRTL"
142"j9Xdbm/01O/0MLBqyXDY119b1qAVVY7Y2CxRIRFdw2V76INWNIwPi46t/VUGwMBGmEux8S8+"
143"79Mv7UyGhKvSUr88pWCS1KdSz1HhN1VWjDvyXPyg96+SecpZMwXCdkUCLdLdD3/8rJLt7wS5"
144"3K5/1V4sacds8GHx7NckhQ/KV0VDlUEFuaOCS4Sd3XXetXs4IFSqtKH+Rn0jX7d8Igcw7X2b"
145"eaV1gxOVquDEwLDT5+wXURw96ahq0caL/4YfoBUQIOQ6HGtPTWJz1Yax0pGu9F30VEY/ObrK"
146"L/B6peJKfqhQGJ+MObqOiP6yHBZH75EImLAkksSVmREdNVjwWkz9D9vzPoAnmP8sQ3/NZarB"
147"Ak+ynQjc19t+CCecPrS2Tlh0cJZkHRiGFF7J5a3ci7yBvg3HfD86AXMRBQuSG1UG8TrzC6BK"
148"+qIQ8DWecQwBMZ3nv/Flo2Fejd/G4/wWe6ObMytC5SB8pJNOq9ri0Ff6Zh3rPrwaSv1PPgFJ"
149"GOw=";
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800150
Yingdi Yu7036ce22014-06-19 18:53:37 -0700151 std::string decoded;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700152 BOOST_CHECK_NO_THROW(StringSource source(imported,
153 true,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700154 new Base64Decoder(new StringSink(decoded))));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800155
156 SecTpmFile tpm;
157
Yingdi Yu5e96e002014-04-23 18:32:15 -0700158 Name keyName("/TestSecTpmFile/ImportKey/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -0700159 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800160
Yingdi Yu7036ce22014-06-19 18:53:37 -0700161 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
162 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700163
Yingdi Yu5e96e002014-04-23 18:32:15 -0700164 BOOST_REQUIRE_NO_THROW(
165 tpm.importPrivateKeyPkcs5IntoTpm(keyName,
166 reinterpret_cast<const uint8_t*>(decoded.c_str()),
167 decoded.size(),
168 "1234"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800169
Yingdi Yu7036ce22014-06-19 18:53:37 -0700170 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
171 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800172
Yingdi Yu7036ce22014-06-19 18:53:37 -0700173 shared_ptr<PublicKey> publicKey;
174 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800175
176 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800177 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700178 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
179 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800180
Yingdi Yu2e57a582014-02-20 23:34:43 -0800181 try
182 {
183 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700184
Yingdi Yu7036ce22014-06-19 18:53:37 -0700185 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800186 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700187 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
188 rsaPublicKey.Load(queue);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700189
Yingdi Yu7036ce22014-06-19 18:53:37 -0700190 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
191 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
192 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700193
Yingdi Yu7036ce22014-06-19 18:53:37 -0700194 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800195 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700196 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800197 {
198 BOOST_CHECK(false);
199 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800200
Yingdi Yu2e57a582014-02-20 23:34:43 -0800201 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700202 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "5678"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800203
204 tpm.deleteKeyPairInTpm(keyName);
205
Yingdi Yu7036ce22014-06-19 18:53:37 -0700206 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
207 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800208
Yingdi Yu5e96e002014-04-23 18:32:15 -0700209 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, exported->buf(), exported->size(),
210 "5678"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700211
Yingdi Yu7036ce22014-06-19 18:53:37 -0700212 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
213 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700214
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800215 const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800216 Block sigBlock2;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700217 BOOST_CHECK_NO_THROW(sigBlock2 = tpm.signInTpm(content2, sizeof(content2),
218 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800219
Yingdi Yu2e57a582014-02-20 23:34:43 -0800220 try
221 {
222 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700223
Yingdi Yu7036ce22014-06-19 18:53:37 -0700224 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800225 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700226 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
227 rsaPublicKey.Load(queue);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800228
Yingdi Yu7036ce22014-06-19 18:53:37 -0700229 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
230 bool isVerified = verifier.VerifyMessage(content2, sizeof(content2),
231 sigBlock2.value(), sigBlock2.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700232
Yingdi Yu7036ce22014-06-19 18:53:37 -0700233 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800234 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700235 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800236 {
237 BOOST_CHECK(false);
238 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700239
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800240 tpm.deleteKeyPairInTpm(keyName);
241
Yingdi Yu7036ce22014-06-19 18:53:37 -0700242 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
243 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800244}
245
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700246BOOST_AUTO_TEST_CASE(EcdsaSigning)
247{
248 SecTpmFile tpm;
249
250 Name keyName("/TestSecTpmFile/EcdsaSigning/ksk-" +
251 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
252 EcdsaKeyParams params;
253 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
254
255 Data data("/TestSecTpmFile/EcdsaSigning/Data/1");
256 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
257
258 Block sigBlock;
259 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
260 keyName, DIGEST_ALGORITHM_SHA256));
261
262 shared_ptr<PublicKey> pubkeyPtr;
263 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
264
265 try
266 {
267 using namespace CryptoPP;
268
269 ECDSA<ECP, SHA256>::PublicKey publicKey;
270 ByteQueue queue;
271 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
272 publicKey.Load(queue);
273
274 uint8_t buffer[64];
275 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
276 sigBlock.value(), sigBlock.value_size(), DSA_DER);
277
278 ECDSA<ECP, SHA256>::Verifier verifier(publicKey);
279 bool result = verifier.VerifyMessage(content, sizeof(content),
280 buffer, usedSize);
281
282 BOOST_CHECK_EQUAL(result, true);
283 }
284 catch (CryptoPP::Exception& e)
285 {
286 BOOST_CHECK(false);
287 }
288
289 tpm.deleteKeyPairInTpm(keyName);
290}
291
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700292
293BOOST_AUTO_TEST_CASE(ImportExportEcdsaKey)
294{
295 using namespace CryptoPP;
296
297 std::string imported =
298 "MIGMMEAGCSqGSIb3DQEFDTAzMBsGCSqGSIb3DQEFDDAOBAhqkJiLfzFWtQICCAAw"
299 "FAYIKoZIhvcNAwcECJ1HLtP8OZC6BEgNv9OH2mZdbkxvqTVlRBkUqPbbP3580OG6"
300 "f0htqWSRppcb4IEKYfuPt2qPCYKL2GcAN2pU3eJqhiM7LFTSFaxgBRFozzIwusk=";
301
302 std::string decoded;
303 BOOST_CHECK_NO_THROW(StringSource source(imported,
304 true,
305 new Base64Decoder(new StringSink(decoded))));
306
307 SecTpmFile tpm;
308
309 Name keyName("/TestSecTpmFile/ImportExportEcdsaKey/ksk-" +
310 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
311
312 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
313 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
314
315 BOOST_REQUIRE_NO_THROW(
316 tpm.importPrivateKeyPkcs5IntoTpm(keyName,
317 reinterpret_cast<const uint8_t*>(decoded.c_str()),
318 decoded.size(),
319 "5678"));
320
321 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
322 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
323
324 shared_ptr<PublicKey> publicKey;
325 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
326
327 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
328 Block sigBlock;
329 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
330 keyName, DIGEST_ALGORITHM_SHA256));
331
332 try
333 {
334 using namespace CryptoPP;
335
336 ECDSA<ECP, SHA256>::PublicKey ecdsaPublicKey;
337 ByteQueue queue;
338 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
339 ecdsaPublicKey.Load(queue);
340
341 uint8_t buffer[64];
342 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
343 sigBlock.value(), sigBlock.value_size(), DSA_DER);
344
345 ECDSA<ECP, SHA256>::Verifier verifier(ecdsaPublicKey);
346 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
347 buffer, usedSize);
348
349 BOOST_CHECK_EQUAL(isVerified, true);
350 }
351 catch (CryptoPP::Exception& e)
352 {
353 BOOST_CHECK(false);
354 }
355
356 ConstBufferPtr exported;
357 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
358
359 tpm.deleteKeyPairInTpm(keyName);
360
361 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
362 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
363
364 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, exported->buf(), exported->size(),
365 "1234"));
366
367 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
368 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
369
370 const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08};
371 Block sigBlock2;
372 BOOST_CHECK_NO_THROW(sigBlock2 = tpm.signInTpm(content2, sizeof(content2),
373 keyName, DIGEST_ALGORITHM_SHA256));
374
375 try
376 {
377 using namespace CryptoPP;
378
379 ECDSA<ECP, SHA256>::PublicKey ecdsaPublicKey;
380 ByteQueue queue;
381 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
382 ecdsaPublicKey.Load(queue);
383
384 uint8_t buffer[64];
385 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
386 sigBlock2.value(), sigBlock2.value_size(),
387 DSA_DER);
388
389 ECDSA<ECP, SHA256>::Verifier verifier(ecdsaPublicKey);
390 bool isVerified = verifier.VerifyMessage(content2, sizeof(content2),
391 buffer, usedSize);
392
393 BOOST_CHECK_EQUAL(isVerified, true);
394
395 }
396 catch (CryptoPP::Exception& e)
397 {
398 BOOST_CHECK(false);
399 }
400
401 tpm.deleteKeyPairInTpm(keyName);
402
403 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
404 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
405}
406
407
Yingdi Yu04020922014-01-22 12:46:53 -0800408BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800409
410} // namespace ndn