blob: a179e30a1ea0d4354c13e43210bebcbd723a7056 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu28fd32f2014-01-28 19:03:03 -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 Yu28fd32f2014-01-28 19:03:03 -080020 */
21
Yingdi Yuf56c68f2014-04-24 21:50:13 -070022#include "security/sec-tpm-osx.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070023#include "security/cryptopp.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080024
Yingdi Yuf56c68f2014-04-24 21:50:13 -070025#include "util/time.hpp"
26
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070027#include <boost/lexical_cast.hpp>
28
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070029#include "boost-test.hpp"
30
Yingdi Yu28fd32f2014-01-28 19:03:03 -080031using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080032namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080033
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070034BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmOsx)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080035
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070036BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080037{
38 SecTpmOsx tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070039
Yingdi Yu5e96e002014-04-23 18:32:15 -070040 Name keyName("/TestSecTpmOsx/Delete/ksk-" +
41 boost::lexical_cast<string>(
42 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080043 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
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
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070054BOOST_AUTO_TEST_CASE(SignVerify)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080055{
56 SecTpmOsx tpm;
57
Yingdi Yu5e96e002014-04-23 18:32:15 -070058 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" +
59 boost::lexical_cast<string>(
60 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080061 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070062
Yingdi Yube4150e2014-02-18 13:02:46 -080063 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080064 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 Yu28fd32f2014-01-28 19:03:03 -080069
Yingdi Yu2e57a582014-02-20 23:34:43 -080070 shared_ptr<PublicKey> pubkeyPtr;
71 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
72 try
73 {
74 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080075
Yingdi Yu2e57a582014-02-20 23:34:43 -080076 RSA::PublicKey publicKey;
77 ByteQueue queue;
78 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
79 publicKey.Load(queue);
80
81 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
82 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 Yu28fd32f2014-01-28 19:03:03 -080091
92 tpm.deleteKeyPairInTpm(keyName);
93}
94
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070095BOOST_AUTO_TEST_CASE(RandomGenerator)
Yingdi Yu4b752752014-02-18 12:24:03 -080096{
97 SecTpmOsx 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
104 map<uint8_t, int> counter;
105 for(size_t i = 0; i < size; i++)
106 counter[block[i]] += 1;
107
108 float dev = 0.0;
109 for(size_t i = 0; i != 255; i++)
110 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
111
112 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
113
114}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800115
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700116BOOST_AUTO_TEST_CASE(ExportImportKey)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117{
118 using namespace CryptoPP;
119
120 SecTpmOsx tpm;
121
Yingdi Yu5e96e002014-04-23 18:32:15 -0700122 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" +
123 boost::lexical_cast<string>(
124 time::toUnixTimestamp(time::system_clock::now()).count()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700125
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800126 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
127
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 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700132 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800133 shared_ptr<PublicKey> pubkeyPtr;
134 BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800135
136 tpm.deleteKeyPairInTpm(keyName);
137
138 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
139 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
140
Yingdi Yu5e96e002014-04-23 18:32:15 -0700141 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
142 exported->buf(), exported->size(),
143 "1234"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700144
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800145 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
146 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
147
148 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800149 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700150 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
151 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800152
Yingdi Yu2e57a582014-02-20 23:34:43 -0800153 try
154 {
155 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800156
Yingdi Yu2e57a582014-02-20 23:34:43 -0800157 RSA::PublicKey publicKey;
158 ByteQueue queue;
159 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
160 publicKey.Load(queue);
161
162 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
163 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -0700164 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700165
Yingdi Yu2e57a582014-02-20 23:34:43 -0800166 BOOST_CHECK_EQUAL(result, true);
167 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700168 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800169 {
170 BOOST_CHECK(false);
171 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700172
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800173 tpm.deleteKeyPairInTpm(keyName);
174 // This is some problem related to Mac OS Key chain, and we will fix it later.
175 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
176 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
177}
178
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700179BOOST_AUTO_TEST_CASE(NonExistingKey)
180{
181 using namespace CryptoPP;
182
183 SecTpmOsx tpm;
184
185 Name keyName("/TestSecTpmOsx/NonExistingKey");
186
187 BOOST_REQUIRE_THROW(tpm.getPublicKeyFromTpm(keyName), SecTpmOsx::Error);
188
189 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
190 BOOST_REQUIRE_THROW(tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256),
191 SecTpmOsx::Error);
192
193 BOOST_REQUIRE_THROW(tpm.signInTpm(0, 1, keyName, DIGEST_ALGORITHM_SHA256),
194 SecTpmOsx::Error);
195}
196
197
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800198BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800199
200} // namespace ndn