blob: 79e3645c238b968a64d832eb69a86486d262b165 [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 Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080022#include "security/v1/sec-tpm-file.hpp"
23#include "security/v1/key-chain.hpp"
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070024#include "security/v1/cryptopp.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070025#include "util/time.hpp"
26
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include "boost-test.hpp"
28
Yingdi Yu41546342014-11-30 23:37:53 -080029#include <boost/filesystem.hpp>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include <boost/lexical_cast.hpp>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070031
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070033namespace security {
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080034namespace v1 {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080035namespace tests {
Yingdi Yu04020922014-01-22 12:46:53 -080036
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080038BOOST_AUTO_TEST_SUITE(V1)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_SUITE(TestSecTpmFile)
Yingdi Yu04020922014-01-22 12:46:53 -080040
Yingdi Yu7036ce22014-06-19 18:53:37 -070041BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080042{
43 SecTpmFile tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070044
Yingdi Yu5e96e002014-04-23 18:32:15 -070045 Name keyName("/TestSecTpmFile/Delete/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070046 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
47 RsaKeyParams params(2048);
48 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070049
Yingdi Yu99b2a002015-08-12 12:47:44 -070050 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
51 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070052
Yingdi Yu28fd32f2014-01-28 19:03:03 -080053 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070054
Yingdi Yu99b2a002015-08-12 12:47:44 -070055 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
56 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080057}
58
Yingdi Yu7036ce22014-06-19 18:53:37 -070059BOOST_AUTO_TEST_CASE(SignVerify)
Yingdi Yu04020922014-01-22 12:46:53 -080060{
61 SecTpmFile tpm;
62
Yingdi Yu5e96e002014-04-23 18:32:15 -070063 Name keyName("/TestSecTpmFile/SignVerify/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -070064 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
65 RsaKeyParams params(2048);
66 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070067
Yingdi Yu04020922014-01-22 12:46:53 -080068 Data data("/tmp/test/1");
69 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
70
Yingdi Yu2e57a582014-02-20 23:34:43 -080071 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070072 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -070073 keyName, DigestAlgorithm::SHA256));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070074 shared_ptr<v1::PublicKey> publicKey;
Yingdi Yu7036ce22014-06-19 18:53:37 -070075 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu04020922014-01-22 12:46:53 -080076
Yingdi Yu2e57a582014-02-20 23:34:43 -080077 try
78 {
79 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070080
Yingdi Yu7036ce22014-06-19 18:53:37 -070081 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -080082 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -070083 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
84 rsaPublicKey.Load(queue);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070085
Yingdi Yu7036ce22014-06-19 18:53:37 -070086 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
Yingdi Yu2e57a582014-02-20 23:34:43 -080087 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -070088 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070089
Yingdi Yu2e57a582014-02-20 23:34:43 -080090 BOOST_CHECK_EQUAL(result, true);
91 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070092 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080093 {
94 BOOST_CHECK(false);
95 }
Yingdi Yu04020922014-01-22 12:46:53 -080096
Yingdi Yu28fd32f2014-01-28 19:03:03 -080097 tpm.deleteKeyPairInTpm(keyName);
Yingdi Yu04020922014-01-22 12:46:53 -080098}
99
Yingdi Yu7036ce22014-06-19 18:53:37 -0700100BOOST_AUTO_TEST_CASE(RandomGenerator)
Yingdi Yu4b752752014-02-18 12:24:03 -0800101{
102 SecTpmFile tpm;
103
Davide Pesavento41abef82016-08-08 07:41:59 +0200104 constexpr size_t scale = 1000;
105 constexpr size_t size = 256 * scale;
106 auto block = unique_ptr<uint8_t[]>{new uint8_t[size]};
107 tpm.generateRandomBlock(block.get(), size);
Yingdi Yu4b752752014-02-18 12:24:03 -0800108
Davide Pesavento41abef82016-08-08 07:41:59 +0200109 std::map<uint8_t, size_t> counter;
110 for (size_t i = 0; i < size; i++) {
111 counter[block[i]] += 1;
112 }
Yingdi Yu4b752752014-02-18 12:24:03 -0800113
Davide Pesavento41abef82016-08-08 07:41:59 +0200114 double dev = 0.0;
115 for (uint8_t i = 0; i < 255; i++) {
116 dev += static_cast<double>((counter[i] - scale) * (counter[i] - scale)) / (scale * scale);
117 }
118 dev /= 256;
Yingdi Yu4b752752014-02-18 12:24:03 -0800119
Davide Pesavento41abef82016-08-08 07:41:59 +0200120 BOOST_CHECK_CLOSE(dev, 0.001, 100);
Yingdi Yu4b752752014-02-18 12:24:03 -0800121}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800122
Yingdi Yu7036ce22014-06-19 18:53:37 -0700123BOOST_AUTO_TEST_CASE(ImportExportKey)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800124{
125 using namespace CryptoPP;
126
Yingdi Yu7036ce22014-06-19 18:53:37 -0700127 std::string imported =
128"MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIIJzwsbTIVqgCAggAMBQGCCqG"
129"SIb3DQMHBAiEsSKdgGkCEQSCBMjaYsEeHM/EPzGNDAPaSxE1ASKxjyNkXEVPQbtGx/ju1PlQ"
130"QakTCzPpia8ziVGihrVfuacHrxxbLh+ufNZtbVVsv9kGQL2g15BN2Nic8VqyTxplKrduFkEb"
131"eOipqu627gb6eTsrSj7kubXfs/w9OAdhRZFaEc7I/S6rcPUaDai4i3O03VuvuTOrzZL/unhD"
132"CDzOa3pr2aUQlO/5Pck6GbSYL1/4aW/fB7djZrZAjsGPTqIYpPa16oWlrzsILQ4650Zlvdn8"
133"feh6elTrA4qy6RljZfhIHxzGAeuGbVVLT7zavs01y+QE88sxPpeZ0RGtlVxCYp+xoB5hOO/x"
134"Xm3n0r+HVIaBV9rHLuUSUiHVAtzwuOj/XKh7YBNXeF58lcv8Npsx10etrPuV5hRQelYirKo1"
135"QbT+Aq7sLdWI09eGClDHjvRgw54lCFEUAVUFLxhBnzfVUfw/UiR2SheixH+c7cY3gOOvSMQh"
136"1oL9Omii4S6yhzfbD3hRN/wBKC0NgiIOLRR+Ti0mEEbTxYQrVcHC9rpRcnr1fFAiQvOXrOw0"
137"HyxsbnOeYpoURgLJmUyItPi09xUMCHwUPZ/sI6aG5eRqVUGgcYaQib7MBbGG/D2PlWaKZx1w"
138"iEYtYUI7rrc7Qc2ltp4i1u46ZLtSOxUi8kM7qK2Yp1AOtcWFlGn0XBXpGsHanZRzs8KzfDjQ"
139"OPmRIuFXFmuJweqUGmg6c8P3wHYueenB4oowYELYaDwmmV96obXMG5hbsKne5kcem875d6K+"
140"hL2QzjAl+na4tUZyXYXDdUO2lgTT8IItujBn6c+b8vDcZ24NcYyWPHHb16J4uvKi/6Zb245g"
141"/N0NC6hB43KE+2F0BH0eyXRzCnen6AjF8nvx/wgYrXsFlsU47P8gaL1e7V6QECkY4vIk6//b"
142"LINmpcgZoAv23fgjvYYTMfS/IyPsooS5DJarbHzbsgAWOuqP2ewYulrKqLi0vaUyoErRedQC"
143"8J5p9c4KF56++ameiwxbyKnDynHIIFCOM8eamLnDH4VvU9E2mtfZxmi8kWDWx/NhKEK9zbQv"
144"WuaGDpOysgPK4myTp2QGQNQDvBB92gMcrVTcY//3OGHSXeJrkUfDKLOUDyz7ROghEt1vpRTL"
145"j9Xdbm/01O/0MLBqyXDY119b1qAVVY7Y2CxRIRFdw2V76INWNIwPi46t/VUGwMBGmEux8S8+"
146"79Mv7UyGhKvSUr88pWCS1KdSz1HhN1VWjDvyXPyg96+SecpZMwXCdkUCLdLdD3/8rJLt7wS5"
147"3K5/1V4sacds8GHx7NckhQ/KV0VDlUEFuaOCS4Sd3XXetXs4IFSqtKH+Rn0jX7d8Igcw7X2b"
148"eaV1gxOVquDEwLDT5+wXURw96ahq0caL/4YfoBUQIOQ6HGtPTWJz1Yax0pGu9F30VEY/ObrK"
149"L/B6peJKfqhQGJ+MObqOiP6yHBZH75EImLAkksSVmREdNVjwWkz9D9vzPoAnmP8sQ3/NZarB"
150"Ak+ynQjc19t+CCecPrS2Tlh0cJZkHRiGFF7J5a3ci7yBvg3HfD86AXMRBQuSG1UG8TrzC6BK"
151"+qIQ8DWecQwBMZ3nv/Flo2Fejd/G4/wWe6ObMytC5SB8pJNOq9ri0Ff6Zh3rPrwaSv1PPgFJ"
152"GOw=";
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800153
Yingdi Yu7036ce22014-06-19 18:53:37 -0700154 std::string decoded;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700155 BOOST_CHECK_NO_THROW(StringSource source(imported,
156 true,
Yingdi Yu7036ce22014-06-19 18:53:37 -0700157 new Base64Decoder(new StringSink(decoded))));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800158
159 SecTpmFile tpm;
160
Yingdi Yu5e96e002014-04-23 18:32:15 -0700161 Name keyName("/TestSecTpmFile/ImportKey/ksk-" +
Yingdi Yu7036ce22014-06-19 18:53:37 -0700162 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800163
Yingdi Yu99b2a002015-08-12 12:47:44 -0700164 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
165 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700166
Yingdi Yu5e96e002014-04-23 18:32:15 -0700167 BOOST_REQUIRE_NO_THROW(
168 tpm.importPrivateKeyPkcs5IntoTpm(keyName,
169 reinterpret_cast<const uint8_t*>(decoded.c_str()),
170 decoded.size(),
171 "1234"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800172
Yingdi Yu99b2a002015-08-12 12:47:44 -0700173 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
174 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800175
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700176 shared_ptr<v1::PublicKey> publicKey;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700177 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800178
179 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800180 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700181 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700182 keyName, DigestAlgorithm::SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800183
Yingdi Yu2e57a582014-02-20 23:34:43 -0800184 try
185 {
186 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700187
Yingdi Yu7036ce22014-06-19 18:53:37 -0700188 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800189 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700190 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
191 rsaPublicKey.Load(queue);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700192
Yingdi Yu7036ce22014-06-19 18:53:37 -0700193 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
194 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
195 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700196
Yingdi Yu7036ce22014-06-19 18:53:37 -0700197 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800198 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700199 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800200 {
201 BOOST_CHECK(false);
202 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800203
Yingdi Yu2e57a582014-02-20 23:34:43 -0800204 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700205 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "5678"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800206
207 tpm.deleteKeyPairInTpm(keyName);
208
Yingdi Yu99b2a002015-08-12 12:47:44 -0700209 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
210 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800211
Yingdi Yu5e96e002014-04-23 18:32:15 -0700212 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, exported->buf(), exported->size(),
213 "5678"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700214
Yingdi Yu99b2a002015-08-12 12:47:44 -0700215 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
216 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700217
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800218 const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800219 Block sigBlock2;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700220 BOOST_CHECK_NO_THROW(sigBlock2 = tpm.signInTpm(content2, sizeof(content2),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700221 keyName, DigestAlgorithm::SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800222
Yingdi Yu2e57a582014-02-20 23:34:43 -0800223 try
224 {
225 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700226
Yingdi Yu7036ce22014-06-19 18:53:37 -0700227 RSA::PublicKey rsaPublicKey;
Yingdi Yu2e57a582014-02-20 23:34:43 -0800228 ByteQueue queue;
Yingdi Yu7036ce22014-06-19 18:53:37 -0700229 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
230 rsaPublicKey.Load(queue);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800231
Yingdi Yu7036ce22014-06-19 18:53:37 -0700232 RSASS<PKCS1v15, SHA256>::Verifier verifier(rsaPublicKey);
233 bool isVerified = verifier.VerifyMessage(content2, sizeof(content2),
234 sigBlock2.value(), sigBlock2.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700235
Yingdi Yu7036ce22014-06-19 18:53:37 -0700236 BOOST_CHECK_EQUAL(isVerified, true);
Yingdi Yu2e57a582014-02-20 23:34:43 -0800237 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700238 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800239 {
240 BOOST_CHECK(false);
241 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700242
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800243 tpm.deleteKeyPairInTpm(keyName);
244
Yingdi Yu99b2a002015-08-12 12:47:44 -0700245 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
246 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800247}
248
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700249BOOST_AUTO_TEST_CASE(EcdsaSigning)
250{
251 SecTpmFile tpm;
252
253 Name keyName("/TestSecTpmFile/EcdsaSigning/ksk-" +
254 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
255 EcdsaKeyParams params;
256 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, params));
257
258 Data data("/TestSecTpmFile/EcdsaSigning/Data/1");
259 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
260
261 Block sigBlock;
262 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700263 keyName, DigestAlgorithm::SHA256));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700264
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700265 shared_ptr<v1::PublicKey> pubkeyPtr;
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700266 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
267
268 try
269 {
270 using namespace CryptoPP;
271
272 ECDSA<ECP, SHA256>::PublicKey publicKey;
273 ByteQueue queue;
274 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
275 publicKey.Load(queue);
276
277 uint8_t buffer[64];
278 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
279 sigBlock.value(), sigBlock.value_size(), DSA_DER);
280
281 ECDSA<ECP, SHA256>::Verifier verifier(publicKey);
282 bool result = verifier.VerifyMessage(content, sizeof(content),
283 buffer, usedSize);
284
285 BOOST_CHECK_EQUAL(result, true);
286 }
287 catch (CryptoPP::Exception& e)
288 {
289 BOOST_CHECK(false);
290 }
291
292 tpm.deleteKeyPairInTpm(keyName);
293}
294
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700295
296BOOST_AUTO_TEST_CASE(ImportExportEcdsaKey)
297{
298 using namespace CryptoPP;
299
300 std::string imported =
301 "MIGMMEAGCSqGSIb3DQEFDTAzMBsGCSqGSIb3DQEFDDAOBAhqkJiLfzFWtQICCAAw"
302 "FAYIKoZIhvcNAwcECJ1HLtP8OZC6BEgNv9OH2mZdbkxvqTVlRBkUqPbbP3580OG6"
303 "f0htqWSRppcb4IEKYfuPt2qPCYKL2GcAN2pU3eJqhiM7LFTSFaxgBRFozzIwusk=";
304
305 std::string decoded;
306 BOOST_CHECK_NO_THROW(StringSource source(imported,
307 true,
308 new Base64Decoder(new StringSink(decoded))));
309
310 SecTpmFile tpm;
311
312 Name keyName("/TestSecTpmFile/ImportExportEcdsaKey/ksk-" +
313 boost::lexical_cast<std::string>(time::toUnixTimestamp(time::system_clock::now())));
314
Yingdi Yu99b2a002015-08-12 12:47:44 -0700315 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
316 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700317
318 BOOST_REQUIRE_NO_THROW(
319 tpm.importPrivateKeyPkcs5IntoTpm(keyName,
320 reinterpret_cast<const uint8_t*>(decoded.c_str()),
321 decoded.size(),
322 "5678"));
323
Yingdi Yu99b2a002015-08-12 12:47:44 -0700324 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
325 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700326
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700327 shared_ptr<v1::PublicKey> publicKey;
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700328 BOOST_CHECK_NO_THROW(publicKey = tpm.getPublicKeyFromTpm(keyName));
329
330 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
331 Block sigBlock;
332 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700333 keyName, DigestAlgorithm::SHA256));
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700334
335 try
336 {
337 using namespace CryptoPP;
338
339 ECDSA<ECP, SHA256>::PublicKey ecdsaPublicKey;
340 ByteQueue queue;
341 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
342 ecdsaPublicKey.Load(queue);
343
344 uint8_t buffer[64];
345 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
346 sigBlock.value(), sigBlock.value_size(), DSA_DER);
347
348 ECDSA<ECP, SHA256>::Verifier verifier(ecdsaPublicKey);
349 bool isVerified = verifier.VerifyMessage(content, sizeof(content),
350 buffer, usedSize);
351
352 BOOST_CHECK_EQUAL(isVerified, true);
353 }
354 catch (CryptoPP::Exception& e)
355 {
356 BOOST_CHECK(false);
357 }
358
359 ConstBufferPtr exported;
360 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
361
362 tpm.deleteKeyPairInTpm(keyName);
363
Yingdi Yu99b2a002015-08-12 12:47:44 -0700364 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
365 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700366
367 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, exported->buf(), exported->size(),
368 "1234"));
369
Yingdi Yu99b2a002015-08-12 12:47:44 -0700370 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), true);
371 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), true);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700372
373 const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08};
374 Block sigBlock2;
375 BOOST_CHECK_NO_THROW(sigBlock2 = tpm.signInTpm(content2, sizeof(content2),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700376 keyName, DigestAlgorithm::SHA256));
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700377
378 try
379 {
380 using namespace CryptoPP;
381
382 ECDSA<ECP, SHA256>::PublicKey ecdsaPublicKey;
383 ByteQueue queue;
384 queue.Put(reinterpret_cast<const byte*>(publicKey->get().buf()), publicKey->get().size());
385 ecdsaPublicKey.Load(queue);
386
387 uint8_t buffer[64];
388 size_t usedSize = DSAConvertSignatureFormat(buffer, 64, DSA_P1363,
389 sigBlock2.value(), sigBlock2.value_size(),
390 DSA_DER);
391
392 ECDSA<ECP, SHA256>::Verifier verifier(ecdsaPublicKey);
393 bool isVerified = verifier.VerifyMessage(content2, sizeof(content2),
394 buffer, usedSize);
395
396 BOOST_CHECK_EQUAL(isVerified, true);
397
398 }
399 catch (CryptoPP::Exception& e)
400 {
401 BOOST_CHECK(false);
402 }
403
404 tpm.deleteKeyPairInTpm(keyName);
405
Yingdi Yu99b2a002015-08-12 12:47:44 -0700406 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
407 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700408}
409
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100410BOOST_AUTO_TEST_SUITE_END() // TestSecTpmFile
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800411BOOST_AUTO_TEST_SUITE_END() // V1
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100412BOOST_AUTO_TEST_SUITE_END() // Security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800413
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800414} // namespace tests
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -0800415} // namespace v1
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700416} // namespace security
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800417} // namespace ndn