blob: 0da42339ee8fab0884658a809643d05b643989bf [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 Afanasyevb1db7c62014-04-03 14:57:25 -070018#include "boost-test.hpp"
19
Yingdi Yu28fd32f2014-01-28 19:03:03 -080020using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080021namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080022
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070023BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmOsx)
Yingdi Yu28fd32f2014-01-28 19:03:03 -080024
25BOOST_AUTO_TEST_CASE (Delete)
26{
27 SecTpmOsx tpm;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028
Yingdi Yu5e96e002014-04-23 18:32:15 -070029 Name keyName("/TestSecTpmOsx/Delete/ksk-" +
30 boost::lexical_cast<string>(
31 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080032 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070033
Yingdi Yu28fd32f2014-01-28 19:03:03 -080034 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
35 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070036
Yingdi Yu28fd32f2014-01-28 19:03:03 -080037 tpm.deleteKeyPairInTpm(keyName);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070038
Yingdi Yu28fd32f2014-01-28 19:03:03 -080039 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
40 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
41}
42
43BOOST_AUTO_TEST_CASE (SignVerify)
44{
45 SecTpmOsx tpm;
46
Yingdi Yu5e96e002014-04-23 18:32:15 -070047 Name keyName("/TestSecTpmOsx/SignVerify/ksk-" +
48 boost::lexical_cast<string>(
49 time::toUnixTimestamp(time::system_clock::now()).count()));
Yingdi Yu9a335352014-01-31 11:57:46 -080050 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070051
Yingdi Yube4150e2014-02-18 13:02:46 -080052 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080053 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
54
Yingdi Yu2e57a582014-02-20 23:34:43 -080055 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -070056 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
57 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080058
Yingdi Yu2e57a582014-02-20 23:34:43 -080059 shared_ptr<PublicKey> pubkeyPtr;
60 BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
61 try
62 {
63 using namespace CryptoPP;
Yingdi Yu28fd32f2014-01-28 19:03:03 -080064
Yingdi Yu2e57a582014-02-20 23:34:43 -080065 RSA::PublicKey publicKey;
66 ByteQueue queue;
67 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
68 publicKey.Load(queue);
69
70 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
71 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -070072 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070073
Yingdi Yu2e57a582014-02-20 23:34:43 -080074 BOOST_CHECK_EQUAL(result, true);
75 }
Yingdi Yu5e96e002014-04-23 18:32:15 -070076 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -080077 {
78 BOOST_CHECK(false);
79 }
Yingdi Yu28fd32f2014-01-28 19:03:03 -080080
81 tpm.deleteKeyPairInTpm(keyName);
82}
83
Yingdi Yu4b752752014-02-18 12:24:03 -080084BOOST_AUTO_TEST_CASE (RandomGenerator)
85{
86 SecTpmOsx tpm;
87
88 size_t scale = 1000;
89 size_t size = 256 * scale;
90 uint8_t* block = new uint8_t[size];
91 tpm.generateRandomBlock(block, size);
92
93 map<uint8_t, int> counter;
94 for(size_t i = 0; i < size; i++)
95 counter[block[i]] += 1;
96
97 float dev = 0.0;
98 for(size_t i = 0; i != 255; i++)
99 dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale);
100
101 BOOST_CHECK_CLOSE(dev / 256, 0.001, 100);
102
103}
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800104
105BOOST_AUTO_TEST_CASE (ExportImportKey)
106{
107 using namespace CryptoPP;
108
109 SecTpmOsx tpm;
110
Yingdi Yu5e96e002014-04-23 18:32:15 -0700111 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" +
112 boost::lexical_cast<string>(
113 time::toUnixTimestamp(time::system_clock::now()).count()));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700114
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800115 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
116
117 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
118 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
119
Yingdi Yu2e57a582014-02-20 23:34:43 -0800120 ConstBufferPtr exported;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700121 BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234"));
Yingdi Yu2e57a582014-02-20 23:34:43 -0800122 shared_ptr<PublicKey> pubkeyPtr;
123 BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800124
125 tpm.deleteKeyPairInTpm(keyName);
126
127 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
128 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
129
Yingdi Yu5e96e002014-04-23 18:32:15 -0700130 BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName,
131 exported->buf(), exported->size(),
132 "1234"));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700133
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800134 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true);
135 BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true);
136
137 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
Yingdi Yu2e57a582014-02-20 23:34:43 -0800138 Block sigBlock;
Yingdi Yu5e96e002014-04-23 18:32:15 -0700139 BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content),
140 keyName, DIGEST_ALGORITHM_SHA256));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800141
Yingdi Yu2e57a582014-02-20 23:34:43 -0800142 try
143 {
144 using namespace CryptoPP;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800145
Yingdi Yu2e57a582014-02-20 23:34:43 -0800146 RSA::PublicKey publicKey;
147 ByteQueue queue;
148 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
149 publicKey.Load(queue);
150
151 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
152 bool result = verifier.VerifyMessage(content, sizeof(content),
Yingdi Yu5e96e002014-04-23 18:32:15 -0700153 sigBlock.value(), sigBlock.value_size());
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700154
Yingdi Yu2e57a582014-02-20 23:34:43 -0800155 BOOST_CHECK_EQUAL(result, true);
156 }
Yingdi Yu5e96e002014-04-23 18:32:15 -0700157 catch (CryptoPP::Exception& e)
Yingdi Yu2e57a582014-02-20 23:34:43 -0800158 {
159 BOOST_CHECK(false);
160 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700161
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800162 tpm.deleteKeyPairInTpm(keyName);
163 // This is some problem related to Mac OS Key chain, and we will fix it later.
164 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false);
165 // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false);
166}
167
Yingdi Yu28fd32f2014-01-28 19:03:03 -0800168BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800169
170} // namespace ndn