blob: 887b19c233918bea9dbd48eade1a7f4da07949b2 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu28fd32f2014-01-28 19:03:03 -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 Yu28fd32f2014-01-28 19:03:03 -080011 */
12
Yingdi Yuf56c68f2014-04-24 21:50:13 -070013#include "security/sec-tpm-osx.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070014#include "security/cryptopp.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080015
Yingdi Yuf56c68f2014-04-24 21:50:13 -070016#include "util/time.hpp"
17
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070018#include <boost/lexical_cast.hpp>
19
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070020#include "boost-test.hpp"
21
Yingdi Yu28fd32f2014-01-28 19:03:03 -080022using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080023namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080024
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070025BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmOsx)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080026
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070027BOOST_AUTO_TEST_CASE(Delete)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080028{
29 SecTpmOsx tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070030
Yingdi Yu5e96e002014-04-23 18:32:15 -070031 Name keyName("/TestSecTpmOsx/Delete/ksk-" +
32 boost::lexical_cast<string>(
33 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080034 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070035
Yingdi Yu28fd32f2014-01-28 19:03:03 -080036 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
37 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070038
Yingdi Yu28fd32f2014-01-28 19:03:03 -080039 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070040
Yingdi Yu28fd32f2014-01-28 19:03:03 -080041 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
42 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
43}
44
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070045BOOST_AUTO_TEST_CASE(SignVerify)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080046{
47 SecTpmOsx tpm;
48
Yingdi Yu5e96e002014-04-23 18:32:15 -070049 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" +
50 boost::lexical_cast<string>(
51 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080052 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070053
Yingdi Yube4150e2014-02-18 13:02:46 -080054 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080055 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
56
Yingdi Yu2e57a582014-02-20 23:34:43 -080057 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070058 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
59 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080060
Yingdi Yu2e57a582014-02-20 23:34:43 -080061 shared_ptr<PublicKey> pubkeyPtr;
62 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
63 try
64 {
65 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080066
Yingdi Yu2e57a582014-02-20 23:34:43 -080067 RSA::PublicKey publicKey;
68 ByteQueue queue;
69 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
70 publicKey.Load(queue);
71
72 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
73 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -070074 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070075
Yingdi Yu2e57a582014-02-20 23:34:43 -080076 BOOST_CHECK_EQUAL(result, true);
77 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070078 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080079 {
80 BOOST_CHECK(false);
81 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080082
83 tpm.deleteKeyPairInTpm(keyName);
84}
85
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -070086BOOST_AUTO_TEST_CASE(RandomGenerator)
Yingdi Yu4b752752014-02-18 12:24:03 -080087{
88 SecTpmOsx tpm;
89
90 size_t scale = 1000;
91 size_t size = 256 * scale;
92 uint8_t* block = new uint8_t[size];
93 tpm.generateRandomBlock(block, size);
94
95 map<uint8_t, int> counter;
96 for(size_t i = 0; i < size; i++)
97 counter[block[i]] += 1;
98
99 float dev = 0.0;
100 for(size_t i = 0; i != 255; i++)
101 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
102
103 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
104
105}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800106
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700107BOOST_AUTO_TEST_CASE(ExportImportKey)
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800108{
109 using namespace CryptoPP;
110
111 SecTpmOsx tpm;
112
Yingdi Yu5e96e002014-04-23 18:32:15 -0700113 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" +
114 boost::lexical_cast<string>(
115 time::toUnixTimestamp(time::system_clock::now()).count()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700116
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800117 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
118
119 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
120 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
121
Yingdi Yu2e57a582014-02-20 23:34:43 -0800122 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700123 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800124 shared_ptr<PublicKey> pubkeyPtr;
125 BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800126
127 tpm.deleteKeyPairInTpm(keyName);
128
129 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
130 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
131
Yingdi Yu5e96e002014-04-23 18:32:15 -0700132 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
133 exported->buf(), exported->size(),
134 "1234"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700135
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800136 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
137 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
138
139 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800140 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700141 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
142 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800143
Yingdi Yu2e57a582014-02-20 23:34:43 -0800144 try
145 {
146 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800147
Yingdi Yu2e57a582014-02-20 23:34:43 -0800148 RSA::PublicKey publicKey;
149 ByteQueue queue;
150 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
151 publicKey.Load(queue);
152
153 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
154 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -0700155 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700156
Yingdi Yu2e57a582014-02-20 23:34:43 -0800157 BOOST_CHECK_EQUAL(result, true);
158 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700159 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800160 {
161 BOOST_CHECK(false);
162 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700163
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800164 tpm.deleteKeyPairInTpm(keyName);
165 // This is some problem related to Mac OS Key chain, and we will fix it later.
166 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
167 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
168}
169
Alexander Afanasyevf82d13a2014-04-30 14:30:19 -0700170BOOST_AUTO_TEST_CASE(NonExistingKey)
171{
172 using namespace CryptoPP;
173
174 SecTpmOsx tpm;
175
176 Name keyName("/TestSecTpmOsx/NonExistingKey");
177
178 BOOST_REQUIRE_THROW(tpm.getPublicKeyFromTpm(keyName), SecTpmOsx::Error);
179
180 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
181 BOOST_REQUIRE_THROW(tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256),
182 SecTpmOsx::Error);
183
184 BOOST_REQUIRE_THROW(tpm.signInTpm(0, 1, keyName, DIGEST_ALGORITHM_SHA256),
185 SecTpmOsx::Error);
186}
187
188
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800189BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800190
191} // namespace ndn