Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * 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 Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 13 | #include "security/sec-tpm-osx.hpp" |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 14 | #include "security/cryptopp.hpp" |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 15 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 16 | #include "util/time.hpp" |
| 17 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 18 | #include "boost-test.hpp" |
| 19 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 20 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 21 | namespace ndn { |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 22 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 23 | BOOST_AUTO_TEST_SUITE(SecurityTestSecTpmOsx) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 25 | BOOST_AUTO_TEST_CASE(Delete) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 26 | { |
| 27 | SecTpmOsx tpm; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 29 | Name keyName("/TestSecTpmOsx/Delete/ksk-" + |
| 30 | boost::lexical_cast<string>( |
| 31 | time::toUnixTimestamp(time::system_clock::now()).count())); |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 32 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 33 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 34 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true); |
| 35 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 36 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 37 | tpm.deleteKeyPairInTpm(keyName); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 39 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false); |
| 40 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false); |
| 41 | } |
| 42 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 43 | BOOST_AUTO_TEST_CASE(SignVerify) |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 44 | { |
| 45 | SecTpmOsx tpm; |
| 46 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 47 | Name keyName("/TestSecTpmOsx/SignVerify/ksk-" + |
| 48 | boost::lexical_cast<string>( |
| 49 | time::toUnixTimestamp(time::system_clock::now()).count())); |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 50 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 51 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame] | 52 | Data data("/TestSecTpmOsx/SignVaerify/Data/1"); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 53 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 54 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 55 | Block sigBlock; |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 56 | BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), |
| 57 | keyName, DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 58 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 59 | shared_ptr<PublicKey> pubkeyPtr; |
| 60 | BOOST_CHECK_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName)); |
| 61 | try |
| 62 | { |
| 63 | using namespace CryptoPP; |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 64 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 65 | 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 Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 72 | sigBlock.value(), sigBlock.value_size()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 73 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 74 | BOOST_CHECK_EQUAL(result, true); |
| 75 | } |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 76 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 77 | { |
| 78 | BOOST_CHECK(false); |
| 79 | } |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 80 | |
| 81 | tpm.deleteKeyPairInTpm(keyName); |
| 82 | } |
| 83 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(RandomGenerator) |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 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 Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(ExportImportKey) |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 106 | { |
| 107 | using namespace CryptoPP; |
| 108 | |
| 109 | SecTpmOsx tpm; |
| 110 | |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 111 | Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + |
| 112 | boost::lexical_cast<string>( |
| 113 | time::toUnixTimestamp(time::system_clock::now()).count())); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 114 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 115 | 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 Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 120 | ConstBufferPtr exported; |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 121 | BOOST_CHECK_NO_THROW(exported = tpm.exportPrivateKeyPkcs5FromTpm(keyName, "1234")); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 122 | shared_ptr<PublicKey> pubkeyPtr; |
| 123 | BOOST_REQUIRE_NO_THROW(pubkeyPtr = tpm.getPublicKeyFromTpm(keyName)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 124 | |
| 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 Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 130 | BOOST_REQUIRE(tpm.importPrivateKeyPkcs5IntoTpm(keyName, |
| 131 | exported->buf(), exported->size(), |
| 132 | "1234")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 133 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 134 | 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 Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 138 | Block sigBlock; |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 139 | BOOST_CHECK_NO_THROW(sigBlock = tpm.signInTpm(content, sizeof(content), |
| 140 | keyName, DIGEST_ALGORITHM_SHA256)); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 141 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 142 | try |
| 143 | { |
| 144 | using namespace CryptoPP; |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 145 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 146 | 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 Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 153 | sigBlock.value(), sigBlock.value_size()); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(result, true); |
| 156 | } |
Yingdi Yu | 5e96e00 | 2014-04-23 18:32:15 -0700 | [diff] [blame] | 157 | catch (CryptoPP::Exception& e) |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 158 | { |
| 159 | BOOST_CHECK(false); |
| 160 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 161 | |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 162 | 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 | |
Alexander Afanasyev | f82d13a | 2014-04-30 14:30:19 -0700 | [diff] [blame] | 168 | BOOST_AUTO_TEST_CASE(NonExistingKey) |
| 169 | { |
| 170 | using namespace CryptoPP; |
| 171 | |
| 172 | SecTpmOsx tpm; |
| 173 | |
| 174 | Name keyName("/TestSecTpmOsx/NonExistingKey"); |
| 175 | |
| 176 | BOOST_REQUIRE_THROW(tpm.getPublicKeyFromTpm(keyName), SecTpmOsx::Error); |
| 177 | |
| 178 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 179 | BOOST_REQUIRE_THROW(tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256), |
| 180 | SecTpmOsx::Error); |
| 181 | |
| 182 | BOOST_REQUIRE_THROW(tpm.signInTpm(0, 1, keyName, DIGEST_ALGORITHM_SHA256), |
| 183 | SecTpmOsx::Error); |
| 184 | } |
| 185 | |
| 186 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 187 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 188 | |
| 189 | } // namespace ndn |