blob: 1bf895e3b3fdff88383ee2ffc86535e8d648c29f [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu04020922014-01-22 12:46:53 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Yingdi Yu04020922014-01-22 12:46:53 -080011 */
12
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "security/key-chain.hpp"
Yingdi Yube4150e2014-02-18 13:02:46 -080014#include "util/time.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070015#include "security/cryptopp.hpp"
Yingdi Yu04020922014-01-22 12:46:53 -080016
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070017#include "boost-test.hpp"
18
Yingdi Yu04020922014-01-22 12:46:53 -080019using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu04020922014-01-22 12:46:53 -080021
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070022BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmFile)
Yingdi Yu04020922014-01-22 12:46:53 -080023
Yingdi Yu28fd32f2014-01-28 19:03:03 -080024BOOST_AUTO_TEST_CASE (Delete)
25{
26 SecTpmFile tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027
Yingdi Yu5e96e002014-04-23 18:32:15 -070028 Name keyName("/TestSecTpmFile/Delete/ksk-" +
29 boost::lexical_cast<string>(time::toUnixTimestamp(time::system_clock::now())));
Yingdi Yu9a335352014-01-31 11:57:46 -080030 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070031
Yingdi Yu28fd32f2014-01-28 19:03:03 -080032 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
33 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070034
Yingdi Yu28fd32f2014-01-28 19:03:03 -080035 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070036
Yingdi Yu28fd32f2014-01-28 19:03:03 -080037 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
38 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
39}
40
Yingdi Yu04020922014-01-22 12:46:53 -080041BOOST_AUTO_TEST_CASE (SignVerify)
42{
43 SecTpmFile tpm;
44
Yingdi Yu5e96e002014-04-23 18:32:15 -070045 Name keyName("/TestSecTpmFile/SignVerify/ksk-" +
46 boost::lexical_cast<string>(time::toUnixTimestamp(time::system_clock::now())));
Yingdi Yu9a335352014-01-31 11:57:46 -080047 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070048
Yingdi Yu04020922014-01-22 12:46:53 -080049 Data data("/tmp/test/1");
50 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
51
Yingdi Yu2e57a582014-02-20 23:34:43 -080052 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070053 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
54 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu2e57a582014-02-20 23:34:43 -080055 shared_ptr<PublicKey> pubkeyPtr;
56 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu04020922014-01-22 12:46:53 -080057
Yingdi Yu2e57a582014-02-20 23:34:43 -080058 try
59 {
60 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070061
Yingdi Yu2e57a582014-02-20 23:34:43 -080062 RSA::PublicKey publicKey;
63 ByteQueue queue;
64 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
65 publicKey.Load(queue);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070066
Yingdi Yu2e57a582014-02-20 23:34:43 -080067 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
68 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -070069 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070070
Yingdi Yu2e57a582014-02-20 23:34:43 -080071 BOOST_CHECK_EQUAL(result, true);
72 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070073 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080074 {
75 BOOST_CHECK(false);
76 }
Yingdi Yu04020922014-01-22 12:46:53 -080077
Yingdi Yu28fd32f2014-01-28 19:03:03 -080078 tpm.deleteKeyPairInTpm(keyName);
Yingdi Yu04020922014-01-22 12:46:53 -080079}
80
Yingdi Yu4b752752014-02-18 12:24:03 -080081BOOST_AUTO_TEST_CASE (RandomGenerator)
82{
83 SecTpmFile tpm;
84
85 size_t scale = 1000;
86 size_t size = 256 * scale;
87 uint8_t* block = new uint8_t[size];
88 tpm.generateRandomBlock(block, size);
89
90 map<uint8_t, int> counter;
91 for(size_t i = 0; i < size; i++)
92 counter[block[i]] += 1;
93
94 float dev = 0.0;
95 for(size_t i = 0; i != 255; i++)
96 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
97
98 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
99
100}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800101
102BOOST_AUTO_TEST_CASE (ImportExportKey)
103{
104 using namespace CryptoPP;
105
106 string imported(
107"3082050E304006092A864886F70D01050D3033301B06092A864886F70D01050C300E0408209CF0B1B4C856A802020800301406082A864886F70D0307040884B1229D80690211048204C8DA62C11E1CCFC43F318D0C03DA4B11350122B18F23645C454F41BB46C7F8EED4F95041A9130B33E989AF338951A286B55FB9A707AF1C5B2E1FAE7CD66D6D556CBFD90640BDA0D7904DD8D89CF15AB24F1A652AB76E16411B78E8A9AAEEB6EE06FA793B2B4A3EE4B9B5DFB3FC3D38076145915A11CEC8FD2EAB70F51A0DA8B88B73B4DD5BAFB933ABCD92FFBA7843083CCE6B7A6BD9A51094EFF93DC93A19B4982F5FF8696FDF07B76366B6408EC18F4EA218A4F6B5EA85A5AF3B082D0E3AE74665BDD9FC7DE87A7A54EB038AB2E9196365F8481F1CC601EB866D554B4FBCDABECD35CBE404F3CB313E9799D111AD955C42629FB1A01E6138EFF15E6DE7D2BF8754868157DAC72EE5125221D502DCF0B8E8FF5CA87B601357785E7C95CBFC369B31D747ADACFB95E614507A5622ACAA3541B4FE02AEEC2DD588D3D7860A50C78EF460C39E250851140155052F18419F37D551FC3F5224764A17A2C47F9CEDC63780E3AF48C421D682FD3A68A2E12EB28737DB0F785137FC01282D0D82220E2D147E4E2D261046D3C5842B55C1C2F6BA51727AF57C502242F397ACEC341F2C6C6E739E629A144602C9994C88B4F8B4F7150C087C143D9FEC23A686E5E46A5541A071869089BECC05B186FC3D8F95668A671D7088462D61423BAEB73B41CDA5B69E22D6EE3A64BB523B1522F2433BA8AD98A7500EB5C5859469F45C15E91AC1DA9D9473B3C2B37C38D038F99122E157166B89C1EA941A683A73C3F7C0762E79E9C1E28A306042D8683C26995F7AA1B5CC1B985BB0A9DEE6471E9BCEF977A2BE84BD90CE3025FA76B8B546725D85C37543B69604D3F0822DBA3067E9CF9BF2F0DC676E0D718C963C71DBD7A278BAF2A2FFA65BDB8E60FCDD0D0BA841E37284FB6174047D1EC974730A77A7E808C5F27BF1FF0818AD7B0596C538ECFF2068BD5EED5E90102918E2F224EBFFDB2C8366A5C819A00BF6DDF823BD861331F4BF2323ECA284B90C96AB6C7CDBB200163AEA8FD9EC18BA5ACAA8B8B4BDA532A04AD179D402F09E69F5CE0A179EBEF9A99E8B0C5BC8A9C3CA71C820508E33C79A98B9C31F856F53D1369AD7D9C668BC9160D6C7F3612842BDCDB42F5AE6860E93B2B203CAE26C93A7640640D403BC107DDA031CAD54DC63FFF73861D25DE26B9147C328B3940F2CFB44E82112DD6FA514CB8FD5DD6E6FF4D4EFF430B06AC970D8D75F5BD6A015558ED8D82C5121115DC3657BE88356348C0F8B8EADFD5506C0C046984BB1F12F3EEFD32FED4C8684ABD252BF3CA56092D4A752CF51E13755568C3BF25CFCA0F7AF9279CA593305C27645022DD2DD0F7FFCAC92EDEF04B9DCAE7FD55E2C69C76CF061F1ECD724850FCA574543954105B9A3824B849DDD75DEB57B382054AAB4A1FE467D235FB77C220730ED7D9B79A575831395AAE0C4C0B0D3E7EC17511C3DE9A86AD1C68BFF861FA0151020E43A1C6B4F4D6273D586B1D291AEF45DF454463F39BACA2FF07AA5E24A7EA850189F8C39BA8E88FEB21C1647EF910898B02492C49599111D3558F05A4CFD0FDBF33E802798FF2C437FCD65AAC1024FB29D08DCD7DB7E08279C3EB4B64E58747096641D1886145EC9E5ADDC8BBC81BE0DC77C3F3A017311050B921B5506F13AF30BA04AFAA210F0359E710C01319DE7BFF165A3615E8DDFC6E3FC167BA39B332B42E5207CA4934EABDAE2D057FA661DEB3EBC1A4AFD4F3E014918EC");
108
109 string decoded;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700110 BOOST_CHECK_NO_THROW(StringSource source(imported,
111 true,
112 new HexDecoder(new StringSink(decoded)))); //StringSource
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800113
114 SecTpmFile tpm;
115
Yingdi Yu5e96e002014-04-23 18:32:15 -0700116 Name keyName("/TestSecTpmFile/ImportKey/ksk-" +
117 boost::lexical_cast<string>(time::toUnixTimestamp(time::system_clock::now())));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800118
119 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
120 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700121
Yingdi Yu5e96e002014-04-23 18:32:15 -0700122 BOOST_REQUIRE_NO_THROW(
123 tpm.importPrivateKeyPkcs5IntoTpm(keyName,
124 reinterpret_cast<const uint8_t*>(decoded.c_str()),
125 decoded.size(),
126 "1234"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800127
128 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
129 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
130
Yingdi Yu2e57a582014-02-20 23:34:43 -0800131 shared_ptr<PublicKey> pubkeyPtr;
132 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800133
134 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800135 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700136 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
137 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800138
Yingdi Yu2e57a582014-02-20 23:34:43 -0800139 try
140 {
141 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700142
Yingdi Yu2e57a582014-02-20 23:34:43 -0800143 RSA::PublicKey publicKey;
144 ByteQueue queue;
145 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
146 publicKey.Load(queue);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700147
Yingdi Yu2e57a582014-02-20 23:34:43 -0800148 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
149 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -0700150 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700151
Yingdi Yu2e57a582014-02-20 23:34:43 -0800152 BOOST_CHECK_EQUAL(result, true);
153 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700154 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800155 {
156 BOOST_CHECK(false);
157 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800158
Yingdi Yu2e57a582014-02-20 23:34:43 -0800159 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700160 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "5678"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800161
162 tpm.deleteKeyPairInTpm(keyName);
163
164 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
165 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
166
Yingdi Yu5e96e002014-04-23 18:32:15 -0700167 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, exported->buf(), exported->size(),
168 "5678"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700169
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800170 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
171 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700172
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800173 const uint8_t content2[] = {0x05, 0x06, 0x07, 0x08};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800174 Block sigBlock2;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700175 BOOST_CHECK_NO_THROW(sigBlock2 = tpm.signInTpm(content2, sizeof(content2),
176 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800177
Yingdi Yu2e57a582014-02-20 23:34:43 -0800178 try
179 {
180 using namespace CryptoPP;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700181
Yingdi Yu2e57a582014-02-20 23:34:43 -0800182 RSA::PublicKey publicKey;
183 ByteQueue queue;
184 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
185 publicKey.Load(queue);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800186
Yingdi Yu2e57a582014-02-20 23:34:43 -0800187 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
188 bool result = verifier.VerifyMessage(content2, sizeof(content2),
Yingdi Yu5e96e002014-04-23 18:32:15 -0700189 sigBlock2.value(), sigBlock2.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700190
Yingdi Yu2e57a582014-02-20 23:34:43 -0800191 BOOST_CHECK_EQUAL(result, true);
192 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700193 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800194 {
195 BOOST_CHECK(false);
196 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700197
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800198 tpm.deleteKeyPairInTpm(keyName);
199
200 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
201 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
202}
203
Yingdi Yu04020922014-01-22 12:46:53 -0800204BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800205
206} // namespace ndn