blob: d75eec16a3eb7fd38af1d6217f9467f35470574e [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
7#if __clang__
8#pragma clang diagnostic ignored "-Wtautological-compare"
9#endif
10
11#include <boost/test/unit_test.hpp>
12
13#include "security/key-chain.hpp"
Yingdi Yube4150e2014-02-18 13:02:46 -080014#include "util/time.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080015#include <cryptopp/rsa.h>
Yingdi Yube4150e2014-02-18 13:02:46 -080016#include <cryptopp/files.h>
17#include <cryptopp/hex.h>
Yingdi Yu28fd32f2014-01-28 19:03:03 -080018
19using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080021
22BOOST_AUTO_TEST_SUITE(TestSecTpmOsx)
23
24BOOST_AUTO_TEST_CASE (Delete)
25{
26 SecTpmOsx tpm;
27
Yingdi Yu2e57a582014-02-20 23:34:43 -080028 Name keyName("/TestSecTpmOsx/Delete/ksk-" + boost::lexical_cast<string>(time::now()));
Yingdi Yu9a335352014-01-31 11:57:46 -080029 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080030
31 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
32 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
33
34 tpm.deleteKeyPairInTpm(keyName);
35
36 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
37 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
38}
39
40BOOST_AUTO_TEST_CASE (SignVerify)
41{
42 SecTpmOsx tpm;
43
Yingdi Yu2e57a582014-02-20 23:34:43 -080044 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" + boost::lexical_cast<string>(time::now()));
Yingdi Yu9a335352014-01-31 11:57:46 -080045 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080046
Yingdi Yube4150e2014-02-18 13:02:46 -080047 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080048 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
49
Yingdi Yu2e57a582014-02-20 23:34:43 -080050 Block sigBlock;
51 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080052
Yingdi Yu2e57a582014-02-20 23:34:43 -080053 shared_ptr<PublicKey> pubkeyPtr;
54 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
55 try
56 {
57 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080058
Yingdi Yu2e57a582014-02-20 23:34:43 -080059 RSA::PublicKey publicKey;
60 ByteQueue queue;
61 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
62 publicKey.Load(queue);
63
64 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
65 bool result = verifier.VerifyMessage(content, sizeof(content),
66 sigBlock.value(), sigBlock.value_size());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080067
Yingdi Yu2e57a582014-02-20 23:34:43 -080068 BOOST_CHECK_EQUAL(result, true);
69 }
70 catch(CryptoPP::Exception& e)
71 {
72 BOOST_CHECK(false);
73 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080074
75 tpm.deleteKeyPairInTpm(keyName);
76}
77
Yingdi Yu4b752752014-02-18 12:24:03 -080078BOOST_AUTO_TEST_CASE (RandomGenerator)
79{
80 SecTpmOsx tpm;
81
82 size_t scale = 1000;
83 size_t size = 256 * scale;
84 uint8_t* block = new uint8_t[size];
85 tpm.generateRandomBlock(block, size);
86
87 map<uint8_t, int> counter;
88 for(size_t i = 0; i < size; i++)
89 counter[block[i]] += 1;
90
91 float dev = 0.0;
92 for(size_t i = 0; i != 255; i++)
93 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
94
95 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
96
97}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080098
99BOOST_AUTO_TEST_CASE (ExportImportKey)
100{
101 using namespace CryptoPP;
102
103 SecTpmOsx tpm;
104
Yingdi Yube4150e2014-02-18 13:02:46 -0800105 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + boost::lexical_cast<string>(time::now()));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800106
107 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
108
109 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
110 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
111
Yingdi Yu2e57a582014-02-20 23:34:43 -0800112 ConstBufferPtr exported;
113 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs8FromTpm(keyName, "1234"));
114 shared_ptr<PublicKey> pubkeyPtr;
115 BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800116
117 tpm.deleteKeyPairInTpm(keyName);
118
119 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
120 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
121
Yingdi Yube4150e2014-02-18 13:02:46 -0800122 BOOST_REQUIRE(tpm.importPrivateKeyPkcs8IntoTpm(keyName, exported->buf(), exported->size(), "1234"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800123
124 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
125 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
126
127 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800128 Block sigBlock;
129 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800130
Yingdi Yu2e57a582014-02-20 23:34:43 -0800131 try
132 {
133 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800134
Yingdi Yu2e57a582014-02-20 23:34:43 -0800135 RSA::PublicKey publicKey;
136 ByteQueue queue;
137 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
138 publicKey.Load(queue);
139
140 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
141 bool result = verifier.VerifyMessage(content, sizeof(content),
142 sigBlock.value(), sigBlock.value_size());
143
144 BOOST_CHECK_EQUAL(result, true);
145 }
146 catch(CryptoPP::Exception& e)
147 {
148 BOOST_CHECK(false);
149 }
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800150
151 tpm.deleteKeyPairInTpm(keyName);
152 // This is some problem related to Mac OS Key chain, and we will fix it later.
153 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
154 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
155}
156
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800157BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800158
159} // namespace ndn