blob: e8aa9a83139fb2041a6a11ec49e43e94822dc323 [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 Yu28fd32f2014-01-28 19:03:03 -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 Yu28fd32f2014-01-28 19:03:03 -080016
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070017#include "boost-test.hpp"
18
Yingdi Yu28fd32f2014-01-28 19:03:03 -080019using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080021
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070022BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmOsx)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080023
24BOOST_AUTO_TEST_CASE (Delete)
25{
26 SecTpmOsx tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027
Yingdi Yu5e96e002014-04-23 18:32:15 -070028 Name keyName("/TestSecTpmOsx/Delete/ksk-" +
29 boost::lexical_cast<string>(
30 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080031 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070032
Yingdi Yu28fd32f2014-01-28 19:03:03 -080033 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
34 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070035
Yingdi Yu28fd32f2014-01-28 19:03:03 -080036 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070037
Yingdi Yu28fd32f2014-01-28 19:03:03 -080038 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
39 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
40}
41
42BOOST_AUTO_TEST_CASE (SignVerify)
43{
44 SecTpmOsx tpm;
45
Yingdi Yu5e96e002014-04-23 18:32:15 -070046 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" +
47 boost::lexical_cast<string>(
48 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080049 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070050
Yingdi Yube4150e2014-02-18 13:02:46 -080051 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080052 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
53
Yingdi Yu2e57a582014-02-20 23:34:43 -080054 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070055 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
56 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080057
Yingdi Yu2e57a582014-02-20 23:34:43 -080058 shared_ptr<PublicKey> pubkeyPtr;
59 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
60 try
61 {
62 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080063
Yingdi Yu2e57a582014-02-20 23:34:43 -080064 RSA::PublicKey publicKey;
65 ByteQueue queue;
66 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
67 publicKey.Load(queue);
68
69 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
70 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -070071 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070072
Yingdi Yu2e57a582014-02-20 23:34:43 -080073 BOOST_CHECK_EQUAL(result, true);
74 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070075 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080076 {
77 BOOST_CHECK(false);
78 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080079
80 tpm.deleteKeyPairInTpm(keyName);
81}
82
Yingdi Yu4b752752014-02-18 12:24:03 -080083BOOST_AUTO_TEST_CASE (RandomGenerator)
84{
85 SecTpmOsx tpm;
86
87 size_t scale = 1000;
88 size_t size = 256 * scale;
89 uint8_t* block = new uint8_t[size];
90 tpm.generateRandomBlock(block, size);
91
92 map<uint8_t, int> counter;
93 for(size_t i = 0; i < size; i++)
94 counter[block[i]] += 1;
95
96 float dev = 0.0;
97 for(size_t i = 0; i != 255; i++)
98 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
99
100 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
101
102}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800103
104BOOST_AUTO_TEST_CASE (ExportImportKey)
105{
106 using namespace CryptoPP;
107
108 SecTpmOsx tpm;
109
Yingdi Yu5e96e002014-04-23 18:32:15 -0700110 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" +
111 boost::lexical_cast<string>(
112 time::toUnixTimestamp(time::system_clock::now()).count()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700113
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800114 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
115
116 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
117 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
118
Yingdi Yu2e57a582014-02-20 23:34:43 -0800119 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700120 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800121 shared_ptr<PublicKey> pubkeyPtr;
122 BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123
124 tpm.deleteKeyPairInTpm(keyName);
125
126 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
127 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
128
Yingdi Yu5e96e002014-04-23 18:32:15 -0700129 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
130 exported->buf(), exported->size(),
131 "1234"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700132
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800133 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
134 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
135
136 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800137 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700138 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
139 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800140
Yingdi Yu2e57a582014-02-20 23:34:43 -0800141 try
142 {
143 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800144
Yingdi Yu2e57a582014-02-20 23:34:43 -0800145 RSA::PublicKey publicKey;
146 ByteQueue queue;
147 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
148 publicKey.Load(queue);
149
150 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
151 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -0700152 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700153
Yingdi Yu2e57a582014-02-20 23:34:43 -0800154 BOOST_CHECK_EQUAL(result, true);
155 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700156 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800157 {
158 BOOST_CHECK(false);
159 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700160
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800161 tpm.deleteKeyPairInTpm(keyName);
162 // This is some problem related to Mac OS Key chain, and we will fix it later.
163 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
164 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
165}
166
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800167BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800168
169} // namespace ndn