blob: 941b06db0644b8a395fb767cc353854890ed082d [file] [log] [blame]
Yingdi Yu28fd32f2014-01-28 19:03:03 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Yingdi Yu <yingdi0@cs.ucla.edu>
4 * See COPYING for copyright and distribution information.
5 */
6
Yingdi Yu28fd32f2014-01-28 19:03:03 -08007#include <boost/test/unit_test.hpp>
8
9#include "security/key-chain.hpp"
Yingdi Yube4150e2014-02-18 13:02:46 -080010#include "util/time.hpp"
Junxiao Shi482ccc52014-03-31 13:05:24 -070011#include "security/cryptopp.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080012
13using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080014namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080015
16BOOST_AUTO_TEST_SUITE(TestSecTpmOsx)
17
18BOOST_AUTO_TEST_CASE (Delete)
19{
20 SecTpmOsx tpm;
21
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070022 Name keyName("/TestSecTpmOsx/Delete/ksk-" + boost::lexical_cast<string>(
23 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080024 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080025
26 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
27 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
28
29 tpm.deleteKeyPairInTpm(keyName);
30
31 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
32 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
33}
34
35BOOST_AUTO_TEST_CASE (SignVerify)
36{
37 SecTpmOsx tpm;
38
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" + boost::lexical_cast<string>(
40 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080041 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080042
Yingdi Yube4150e2014-02-18 13:02:46 -080043 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080044 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
45
Yingdi Yu2e57a582014-02-20 23:34:43 -080046 Block sigBlock;
47 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080048
Yingdi Yu2e57a582014-02-20 23:34:43 -080049 shared_ptr<PublicKey> pubkeyPtr;
50 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
51 try
52 {
53 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080054
Yingdi Yu2e57a582014-02-20 23:34:43 -080055 RSA::PublicKey publicKey;
56 ByteQueue queue;
57 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
58 publicKey.Load(queue);
59
60 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
61 bool result = verifier.VerifyMessage(content, sizeof(content),
62 sigBlock.value(), sigBlock.value_size());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080063
Yingdi Yu2e57a582014-02-20 23:34:43 -080064 BOOST_CHECK_EQUAL(result, true);
65 }
66 catch(CryptoPP::Exception& e)
67 {
68 BOOST_CHECK(false);
69 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080070
71 tpm.deleteKeyPairInTpm(keyName);
72}
73
Yingdi Yu4b752752014-02-18 12:24:03 -080074BOOST_AUTO_TEST_CASE (RandomGenerator)
75{
76 SecTpmOsx tpm;
77
78 size_t scale = 1000;
79 size_t size = 256 * scale;
80 uint8_t* block = new uint8_t[size];
81 tpm.generateRandomBlock(block, size);
82
83 map<uint8_t, int> counter;
84 for(size_t i = 0; i < size; i++)
85 counter[block[i]] += 1;
86
87 float dev = 0.0;
88 for(size_t i = 0; i != 255; i++)
89 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
90
91 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
92
93}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080094
95BOOST_AUTO_TEST_CASE (ExportImportKey)
96{
97 using namespace CryptoPP;
98
99 SecTpmOsx tpm;
100
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + boost::lexical_cast<string>(
102 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800103
104 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
105
106 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
107 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
108
Yingdi Yu2e57a582014-02-20 23:34:43 -0800109 ConstBufferPtr exported;
110 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs8FromTpm(keyName, "1234"));
111 shared_ptr<PublicKey> pubkeyPtr;
112 BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800113
114 tpm.deleteKeyPairInTpm(keyName);
115
116 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
117 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
118
Yingdi Yube4150e2014-02-18 13:02:46 -0800119 BOOST_REQUIRE(tpm.importPrivateKeyPkcs8IntoTpm(keyName, exported->buf(), exported->size(), "1234"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800120
121 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
122 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
123
124 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800125 Block sigBlock;
126 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800127
Yingdi Yu2e57a582014-02-20 23:34:43 -0800128 try
129 {
130 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800131
Yingdi Yu2e57a582014-02-20 23:34:43 -0800132 RSA::PublicKey publicKey;
133 ByteQueue queue;
134 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
135 publicKey.Load(queue);
136
137 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
138 bool result = verifier.VerifyMessage(content, sizeof(content),
139 sigBlock.value(), sigBlock.value_size());
140
141 BOOST_CHECK_EQUAL(result, true);
142 }
143 catch(CryptoPP::Exception& e)
144 {
145 BOOST_CHECK(false);
146 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800147
148 tpm.deleteKeyPairInTpm(keyName);
149 // This is some problem related to Mac OS Key chain, and we will fix it later.
150 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
151 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
152}
153
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800154BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800155
156} // namespace ndn